]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/IsaFloppyDxe/IsaFloppyBlock.c
Fix missing include header file and fix typo in comment.
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaFloppyDxe / IsaFloppyBlock.c
1 /**@file
2 ISA Floppy Driver
3 1. Support two types diskette drive
4 1.44M drive and 2.88M drive (and now only support 1.44M)
5 2. Support two diskette drives
6 3. Use DMA channel 2 to transfer data
7 4. Do not use interrupt
8 5. Support diskette change line signal and write protect
9
10 Implement the Block IO interface
11
12 Copyright (c) 2006 - 2007, Intel Corporation.<BR>
13 All rights reserved. This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20
21 **/
22
23
24 #include "IsaFloppy.h"
25
26 /**
27 Reset the Floppy Logic Drive, call the FddReset function
28
29 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
30 @param ExtendedVerification BOOLEAN: Indicate that the driver may perform a more
31 exhaustive verification operation of the device during
32 reset, now this par is ignored in this driver
33 @retval EFI_SUCCESS: The Floppy Logic Drive is reset
34 @retval EFI_DEVICE_ERROR: The Floppy Logic Drive is not functioning correctly
35 and can not be reset
36
37 **/
38 EFI_STATUS
39 EFIAPI
40 FdcReset (
41 IN EFI_BLOCK_IO_PROTOCOL *This,
42 IN BOOLEAN ExtendedVerification
43 )
44 {
45 FDC_BLK_IO_DEV *FdcDev;
46
47 //
48 // Reset the Floppy Disk Controller
49 //
50 FdcDev = FDD_BLK_IO_FROM_THIS (This);
51
52 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
53 EFI_PROGRESS_CODE,
54 EFI_P_PC_RESET | EFI_PERIPHERAL_REMOVABLE_MEDIA,
55 FdcDev->DevicePath
56 );
57
58 return FddReset (FdcDev);
59 }
60
61 /**
62 Flush block via fdd controller
63
64 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
65 @return EFI_SUCCESS
66
67 **/
68 EFI_STATUS
69 EFIAPI
70 FddFlushBlocks (
71 IN EFI_BLOCK_IO_PROTOCOL *This
72 )
73 {
74 //
75 // Not supported yet
76 //
77 return EFI_SUCCESS;
78 }
79
80 /**
81 Common report status code interface
82
83 @param This Pointer of FDC_BLK_IO_DEV instance
84 @param Read Error type: read or write?
85 **/
86 STATIC
87 VOID
88 FddReportStatus (
89 IN EFI_BLOCK_IO_PROTOCOL *This,
90 IN BOOLEAN Read
91 )
92 {
93 FDC_BLK_IO_DEV *FdcDev;
94
95 FdcDev = FDD_BLK_IO_FROM_THIS (This);
96
97 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
98 EFI_ERROR_CODE,
99 ((Read) ? EFI_P_EC_INPUT_ERROR : EFI_P_EC_OUTPUT_ERROR) | EFI_PERIPHERAL_REMOVABLE_MEDIA,
100 FdcDev->DevicePath
101 );
102 }
103
104 /**
105 Read the requested number of blocks from the device
106
107 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
108 @param MediaId UINT32: The media id that the read request is for
109 @param LBA EFI_LBA: The starting logic block address to read from on the device
110 @param BufferSize UINTN: The size of the Buffer in bytes
111 @param Buffer VOID *: A pointer to the destination buffer for the data
112
113 @retval EFI_SUCCESS: The data was read correctly from the device
114 @retval EFI_DEVICE_ERROR:The device reported an error while attempting to perform
115 the read operation
116 @retval EFI_NO_MEDIA: There is no media in the device
117 @retval EFI_MEDIA_CHANGED: The MediaId is not for the current media
118 @retval EFI_BAD_BUFFER_SIZE: The BufferSize parameter is not a multiple of the
119 intrinsic block size of the device
120 @retval EFI_INVALID_PARAMETER:The read request contains LBAs that are not valid,
121 or the buffer is not on proper alignment
122
123 **/
124 EFI_STATUS
125 EFIAPI
126 FddReadBlocks (
127 IN EFI_BLOCK_IO_PROTOCOL *This,
128 IN UINT32 MediaId,
129 IN EFI_LBA LBA,
130 IN UINTN BufferSize,
131 OUT VOID *Buffer
132 )
133 {
134 EFI_STATUS Status;
135
136 Status = FddReadWriteBlocks (This, MediaId, LBA, BufferSize, READ, Buffer);
137
138 if (EFI_ERROR (Status)) {
139 FddReportStatus (This, TRUE);
140 }
141
142 return Status;
143 }
144
145 /**
146 Write a specified number of blocks to the device
147
148 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
149 @param MediaId UINT32: The media id that the write request is for
150 @param LBA EFI_LBA: The starting logic block address to be written
151 @param BufferSize UINTN: The size in bytes in Buffer
152 @param Buffer VOID *: A pointer to the source buffer for the data
153
154 @retval EFI_SUCCESS: The data were written correctly to the device
155 @retval EFI_WRITE_PROTECTED: The device can not be written to
156 @retval EFI_NO_MEDIA: There is no media in the device
157 @retval EFI_MEDIA_CHANGED: The MediaId is not for the current media
158 @retval EFI_DEVICE_ERROR: The device reported an error while attempting to perform
159 the write operation
160 @retval EFI_BAD_BUFFER_SIZE: The BufferSize parameter is not a multiple of the
161 intrinsic block size of the device
162 @retval EFI_INVALID_PARAMETER:The write request contains LBAs that are not valid,
163 or the buffer is not on proper alignment
164 **/
165 EFI_STATUS
166 EFIAPI
167 FddWriteBlocks (
168 IN EFI_BLOCK_IO_PROTOCOL *This,
169 IN UINT32 MediaId,
170 IN EFI_LBA LBA,
171 IN UINTN BufferSize,
172 IN VOID *Buffer
173 )
174 {
175 EFI_STATUS Status;
176
177 Status = FddReadWriteBlocks (This, MediaId, LBA, BufferSize, WRITE, Buffer);
178
179 if (EFI_ERROR (Status)) {
180 FddReportStatus (This, FALSE);
181 }
182
183 return Status;
184 }
185
186 /**
187 Read or Write a number of blocks to floppy device
188
189 @param This Pointer to instance of EFI_BLOCK_IO_PROTOCOL
190 @param MediaId The media id of read/write request
191 @param LBA The starting logic block address to read from on the device
192 @param BufferSize The size of the Buffer in bytes
193 @param Operation - GC_TODO: add argument description
194 @param Buffer - GC_TODO: add argument description
195
196 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
197 @retval EFI_SUCCESS - GC_TODO: Add description for return value
198 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
199 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
200 @retval EFI_NO_MEDIA - GC_TODO: Add description for return value
201 @retval EFI_MEDIA_CHANGED - GC_TODO: Add description for return value
202 @retval EFI_WRITE_PROTECTED - GC_TODO: Add description for return value
203 @retval EFI_BAD_BUFFER_SIZE - GC_TODO: Add description for return value
204 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
205 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
206 @retval EFI_SUCCESS - GC_TODO: Add description for return value
207 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
208 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
209 @retval EFI_SUCCESS - GC_TODO: Add description for return value
210
211 **/
212 EFI_STATUS
213 FddReadWriteBlocks (
214 IN EFI_BLOCK_IO_PROTOCOL *This,
215 IN UINT32 MediaId,
216 IN EFI_LBA LBA,
217 IN UINTN BufferSize,
218 IN BOOLEAN Operation,
219 OUT VOID *Buffer
220 )
221 {
222 EFI_BLOCK_IO_MEDIA *Media;
223 FDC_BLK_IO_DEV *FdcDev;
224 UINTN BlockSize;
225 UINTN NumberOfBlocks;
226 UINTN BlockCount;
227 EFI_STATUS Status;
228 //
229 // EFI_STATUS CacheStatus;
230 //
231 EFI_LBA LBA0;
232 UINT8 *Pointer;
233
234 //
235 // Get the intrinsic block size
236 //
237 Media = This->Media;
238 BlockSize = Media->BlockSize;
239 FdcDev = FDD_BLK_IO_FROM_THIS (This);
240
241 if (Operation == WRITE) {
242 if (LBA == 0) {
243 FdcFreeCache (FdcDev);
244 }
245 }
246 //
247 // Check the Parameter is valid
248 //
249 if (Buffer == NULL) {
250 return EFI_INVALID_PARAMETER;
251 }
252
253 if (BufferSize == 0) {
254 return EFI_SUCCESS;
255 }
256 //
257 // Set the drive motor on
258 //
259 Status = MotorOn (FdcDev);
260 if (EFI_ERROR (Status)) {
261 return EFI_DEVICE_ERROR;
262 }
263 //
264 // Check to see if media can be detected
265 //
266 Status = DetectMedia (FdcDev);
267 if (EFI_ERROR (Status)) {
268 MotorOff (FdcDev);
269 FdcFreeCache (FdcDev);
270 return EFI_DEVICE_ERROR;
271 }
272 //
273 // Check to see if media is present
274 //
275 if (!(Media->MediaPresent)) {
276 MotorOff (FdcDev);
277 FdcFreeCache (FdcDev);
278
279 /*
280 if (FdcDev->Cache) {
281 gBS->FreePool (FdcDev->Cache);
282 FdcDev->Cache = NULL;
283 }
284 */
285 return EFI_NO_MEDIA;
286 }
287 //
288 // Check to see if media has been changed
289 //
290 if (MediaId != Media->MediaId) {
291 MotorOff (FdcDev);
292 FdcFreeCache (FdcDev);
293 return EFI_MEDIA_CHANGED;
294 }
295
296 if (Operation == WRITE) {
297 if (Media->ReadOnly) {
298 MotorOff (FdcDev);
299 return EFI_WRITE_PROTECTED;
300 }
301 }
302 //
303 // Check the parameters for this read/write operation
304 //
305 if (BufferSize % BlockSize != 0) {
306 MotorOff (FdcDev);
307 return EFI_BAD_BUFFER_SIZE;
308 }
309
310 if (LBA > Media->LastBlock) {
311 MotorOff (FdcDev);
312 return EFI_INVALID_PARAMETER;
313 }
314
315 if (((BufferSize / BlockSize) + LBA - 1) > Media->LastBlock) {
316 MotorOff (FdcDev);
317 return EFI_INVALID_PARAMETER;
318 }
319
320 if (Operation == READ) {
321 //
322 // See if the data that is being read is already in the cache
323 //
324 if (FdcDev->Cache) {
325 if (LBA == 0 && BufferSize == BlockSize) {
326 MotorOff (FdcDev);
327 CopyMem ((UINT8 *) Buffer, (UINT8 *) FdcDev->Cache, BlockSize);
328 return EFI_SUCCESS;
329 }
330 }
331 }
332 //
333 // Set up Floppy Disk Controller
334 //
335 Status = Setup (FdcDev);
336 if (EFI_ERROR (Status)) {
337 MotorOff (FdcDev);
338 return EFI_DEVICE_ERROR;
339 }
340
341 NumberOfBlocks = BufferSize / BlockSize;
342 LBA0 = LBA;
343 Pointer = Buffer;
344
345 //
346 // read blocks in the same cylinder.
347 // in a cylinder , there are 18 * 2 = 36 blocks
348 //
349 BlockCount = GetTransferBlockCount (FdcDev, LBA, NumberOfBlocks);
350 while ((BlockCount != 0) && !EFI_ERROR (Status)) {
351 Status = ReadWriteDataSector (FdcDev, Buffer, LBA, BlockCount, Operation);
352 if (EFI_ERROR (Status)) {
353 MotorOff (FdcDev);
354 FddReset (FdcDev);
355 return EFI_DEVICE_ERROR;
356 }
357
358 LBA += BlockCount;
359 NumberOfBlocks -= BlockCount;
360 Buffer = (VOID *) ((UINTN) Buffer + BlockCount * BlockSize);
361 BlockCount = GetTransferBlockCount (FdcDev, LBA, NumberOfBlocks);
362 }
363
364 Buffer = Pointer;
365
366 //
367 // Turn the motor off
368 //
369 MotorOff (FdcDev);
370
371 if (Operation == READ) {
372 //
373 // Cache the data read
374 //
375 if (LBA0 == 0 && !FdcDev->Cache) {
376 FdcDev->Cache = AllocateCopyPool (BlockSize, Buffer);
377 }
378 }
379
380 return EFI_SUCCESS;
381
382 }
383
384 /**
385 Common interface for free cache
386
387 @param FdcDev Pointer of FDC_BLK_IO_DEV instance
388
389 **/
390 VOID
391 FdcFreeCache (
392 IN FDC_BLK_IO_DEV *FdcDev
393 )
394 {
395 if (FdcDev->Cache) {
396 gBS->FreePool (FdcDev->Cache);
397 FdcDev->Cache = NULL;
398 }
399 }