]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/IsaFloppyDxe/IsaFloppyBlock.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[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 VOID
87 FddReportStatus (
88 IN EFI_BLOCK_IO_PROTOCOL *This,
89 IN BOOLEAN Read
90 )
91 {
92 FDC_BLK_IO_DEV *FdcDev;
93
94 FdcDev = FDD_BLK_IO_FROM_THIS (This);
95
96 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
97 EFI_ERROR_CODE,
98 ((Read) ? EFI_P_EC_INPUT_ERROR : EFI_P_EC_OUTPUT_ERROR) | EFI_PERIPHERAL_REMOVABLE_MEDIA,
99 FdcDev->DevicePath
100 );
101 }
102
103 /**
104 Read the requested number of blocks from the device
105
106 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
107 @param MediaId UINT32: The media id that the read request is for
108 @param LBA EFI_LBA: The starting logic block address to read from on the device
109 @param BufferSize UINTN: The size of the Buffer in bytes
110 @param Buffer VOID *: A pointer to the destination buffer for the data
111
112 @retval EFI_SUCCESS: The data was read correctly from the device
113 @retval EFI_DEVICE_ERROR:The device reported an error while attempting to perform
114 the read operation
115 @retval EFI_NO_MEDIA: There is no media in the device
116 @retval EFI_MEDIA_CHANGED: The MediaId is not for the current media
117 @retval EFI_BAD_BUFFER_SIZE: The BufferSize parameter is not a multiple of the
118 intrinsic block size of the device
119 @retval EFI_INVALID_PARAMETER:The read request contains LBAs that are not valid,
120 or the buffer is not on proper alignment
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 FddReadBlocks (
126 IN EFI_BLOCK_IO_PROTOCOL *This,
127 IN UINT32 MediaId,
128 IN EFI_LBA LBA,
129 IN UINTN BufferSize,
130 OUT VOID *Buffer
131 )
132 {
133 EFI_STATUS Status;
134
135 Status = FddReadWriteBlocks (This, MediaId, LBA, BufferSize, READ, Buffer);
136
137 if (EFI_ERROR (Status)) {
138 FddReportStatus (This, TRUE);
139 }
140
141 return Status;
142 }
143
144 /**
145 Write a specified number of blocks to the device
146
147 @param This EFI_BLOCK_IO *: A pointer to the Block I/O protocol interface
148 @param MediaId UINT32: The media id that the write request is for
149 @param LBA EFI_LBA: The starting logic block address to be written
150 @param BufferSize UINTN: The size in bytes in Buffer
151 @param Buffer VOID *: A pointer to the source buffer for the data
152
153 @retval EFI_SUCCESS: The data were written correctly to the device
154 @retval EFI_WRITE_PROTECTED: The device can not be written to
155 @retval EFI_NO_MEDIA: There is no media in the device
156 @retval EFI_MEDIA_CHANGED: The MediaId is not for the current media
157 @retval EFI_DEVICE_ERROR: The device reported an error while attempting to perform
158 the write operation
159 @retval EFI_BAD_BUFFER_SIZE: The BufferSize parameter is not a multiple of the
160 intrinsic block size of the device
161 @retval EFI_INVALID_PARAMETER:The write request contains LBAs that are not valid,
162 or the buffer is not on proper alignment
163 **/
164 EFI_STATUS
165 EFIAPI
166 FddWriteBlocks (
167 IN EFI_BLOCK_IO_PROTOCOL *This,
168 IN UINT32 MediaId,
169 IN EFI_LBA LBA,
170 IN UINTN BufferSize,
171 IN VOID *Buffer
172 )
173 {
174 EFI_STATUS Status;
175
176 Status = FddReadWriteBlocks (This, MediaId, LBA, BufferSize, WRITE, Buffer);
177
178 if (EFI_ERROR (Status)) {
179 FddReportStatus (This, FALSE);
180 }
181
182 return Status;
183 }
184
185 /**
186 Read or Write a number of blocks to floppy device
187
188 @param This Pointer to instance of EFI_BLOCK_IO_PROTOCOL
189 @param MediaId The media id of read/write request
190 @param LBA The starting logic block address to read from on the device
191 @param BufferSize The size of the Buffer in bytes
192 @param Operation - GC_TODO: add argument description
193 @param Buffer - GC_TODO: add argument description
194
195 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
196 @retval EFI_SUCCESS - GC_TODO: Add description for return value
197 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
198 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
199 @retval EFI_NO_MEDIA - GC_TODO: Add description for return value
200 @retval EFI_MEDIA_CHANGED - GC_TODO: Add description for return value
201 @retval EFI_WRITE_PROTECTED - GC_TODO: Add description for return value
202 @retval EFI_BAD_BUFFER_SIZE - GC_TODO: Add description for return value
203 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
204 @retval EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
205 @retval EFI_SUCCESS - GC_TODO: Add description for return value
206 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
207 @retval EFI_DEVICE_ERROR - GC_TODO: Add description for return value
208 @retval EFI_SUCCESS - GC_TODO: Add description for return value
209
210 **/
211 EFI_STATUS
212 FddReadWriteBlocks (
213 IN EFI_BLOCK_IO_PROTOCOL *This,
214 IN UINT32 MediaId,
215 IN EFI_LBA LBA,
216 IN UINTN BufferSize,
217 IN BOOLEAN Operation,
218 OUT VOID *Buffer
219 )
220 {
221 EFI_BLOCK_IO_MEDIA *Media;
222 FDC_BLK_IO_DEV *FdcDev;
223 UINTN BlockSize;
224 UINTN NumberOfBlocks;
225 UINTN BlockCount;
226 EFI_STATUS Status;
227 //
228 // EFI_STATUS CacheStatus;
229 //
230 EFI_LBA LBA0;
231 UINT8 *Pointer;
232
233 //
234 // Get the intrinsic block size
235 //
236 Media = This->Media;
237 BlockSize = Media->BlockSize;
238 FdcDev = FDD_BLK_IO_FROM_THIS (This);
239
240 if (Operation == WRITE) {
241 if (LBA == 0) {
242 FdcFreeCache (FdcDev);
243 }
244 }
245 //
246 // Check the Parameter is valid
247 //
248 if (Buffer == NULL) {
249 return EFI_INVALID_PARAMETER;
250 }
251
252 if (BufferSize == 0) {
253 return EFI_SUCCESS;
254 }
255 //
256 // Set the drive motor on
257 //
258 Status = MotorOn (FdcDev);
259 if (EFI_ERROR (Status)) {
260 return EFI_DEVICE_ERROR;
261 }
262 //
263 // Check to see if media can be detected
264 //
265 Status = DetectMedia (FdcDev);
266 if (EFI_ERROR (Status)) {
267 MotorOff (FdcDev);
268 FdcFreeCache (FdcDev);
269 return EFI_DEVICE_ERROR;
270 }
271 //
272 // Check to see if media is present
273 //
274 if (!(Media->MediaPresent)) {
275 MotorOff (FdcDev);
276 FdcFreeCache (FdcDev);
277
278 /*
279 if (FdcDev->Cache) {
280 gBS->FreePool (FdcDev->Cache);
281 FdcDev->Cache = NULL;
282 }
283 */
284 return EFI_NO_MEDIA;
285 }
286 //
287 // Check to see if media has been changed
288 //
289 if (MediaId != Media->MediaId) {
290 MotorOff (FdcDev);
291 FdcFreeCache (FdcDev);
292 return EFI_MEDIA_CHANGED;
293 }
294
295 if (Operation == WRITE) {
296 if (Media->ReadOnly) {
297 MotorOff (FdcDev);
298 return EFI_WRITE_PROTECTED;
299 }
300 }
301 //
302 // Check the parameters for this read/write operation
303 //
304 if (BufferSize % BlockSize != 0) {
305 MotorOff (FdcDev);
306 return EFI_BAD_BUFFER_SIZE;
307 }
308
309 if (LBA > Media->LastBlock) {
310 MotorOff (FdcDev);
311 return EFI_INVALID_PARAMETER;
312 }
313
314 if (((BufferSize / BlockSize) + LBA - 1) > Media->LastBlock) {
315 MotorOff (FdcDev);
316 return EFI_INVALID_PARAMETER;
317 }
318
319 if (Operation == READ) {
320 //
321 // See if the data that is being read is already in the cache
322 //
323 if (FdcDev->Cache) {
324 if (LBA == 0 && BufferSize == BlockSize) {
325 MotorOff (FdcDev);
326 CopyMem ((UINT8 *) Buffer, (UINT8 *) FdcDev->Cache, BlockSize);
327 return EFI_SUCCESS;
328 }
329 }
330 }
331 //
332 // Set up Floppy Disk Controller
333 //
334 Status = Setup (FdcDev);
335 if (EFI_ERROR (Status)) {
336 MotorOff (FdcDev);
337 return EFI_DEVICE_ERROR;
338 }
339
340 NumberOfBlocks = BufferSize / BlockSize;
341 LBA0 = LBA;
342 Pointer = Buffer;
343
344 //
345 // read blocks in the same cylinder.
346 // in a cylinder , there are 18 * 2 = 36 blocks
347 //
348 BlockCount = GetTransferBlockCount (FdcDev, LBA, NumberOfBlocks);
349 while ((BlockCount != 0) && !EFI_ERROR (Status)) {
350 Status = ReadWriteDataSector (FdcDev, Buffer, LBA, BlockCount, Operation);
351 if (EFI_ERROR (Status)) {
352 MotorOff (FdcDev);
353 FddReset (FdcDev);
354 return EFI_DEVICE_ERROR;
355 }
356
357 LBA += BlockCount;
358 NumberOfBlocks -= BlockCount;
359 Buffer = (VOID *) ((UINTN) Buffer + BlockCount * BlockSize);
360 BlockCount = GetTransferBlockCount (FdcDev, LBA, NumberOfBlocks);
361 }
362
363 Buffer = Pointer;
364
365 //
366 // Turn the motor off
367 //
368 MotorOff (FdcDev);
369
370 if (Operation == READ) {
371 //
372 // Cache the data read
373 //
374 if (LBA0 == 0 && !FdcDev->Cache) {
375 FdcDev->Cache = AllocateCopyPool (BlockSize, Buffer);
376 }
377 }
378
379 return EFI_SUCCESS;
380
381 }
382
383 /**
384 Common interface for free cache
385
386 @param FdcDev Pointer of FDC_BLK_IO_DEV instance
387
388 **/
389 VOID
390 FdcFreeCache (
391 IN FDC_BLK_IO_DEV *FdcDev
392 )
393 {
394 if (FdcDev->Cache) {
395 gBS->FreePool (FdcDev->Cache);
396 FdcDev->Cache = NULL;
397 }
398 }