]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Usb/UsbMassStorage/Dxe/UsbMassStorage.c
Partially make EdkModulePkg pass intel IPF compiler with /W4 /WX switched on.
[mirror_edk2.git] / EdkModulePkg / Bus / Usb / UsbMassStorage / Dxe / UsbMassStorage.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 UsbMassStorage.c
15
16 Abstract:
17
18 USB Mass Storage Driver
19
20 Revision History
21
22 --*/
23
24 #include "UsbMassStorage.h"
25 #include "UsbMassStorageHelper.h"
26
27 extern EFI_COMPONENT_NAME_PROTOCOL gUsbMassStorageComponentName;
28
29 //
30 // Prototypes
31 // Driver model protocol interface
32 //
33 EFI_STATUS
34 EFIAPI
35 USBMassStorageDriverBindingEntryPoint (
36 IN EFI_HANDLE ImageHandle,
37 IN EFI_SYSTEM_TABLE *SystemTable
38 );
39
40 EFI_STATUS
41 EFIAPI
42 USBFloppyDriverBindingSupported (
43 IN EFI_DRIVER_BINDING_PROTOCOL *This,
44 IN EFI_HANDLE Controller,
45 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
46 );
47
48 EFI_STATUS
49 EFIAPI
50 USBFloppyDriverBindingStart (
51 IN EFI_DRIVER_BINDING_PROTOCOL *This,
52 IN EFI_HANDLE Controller,
53 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
54 );
55
56 EFI_STATUS
57 EFIAPI
58 USBFloppyDriverBindingStop (
59 IN EFI_DRIVER_BINDING_PROTOCOL *This,
60 IN EFI_HANDLE Controller,
61 IN UINTN NumberOfChildren,
62 IN EFI_HANDLE *ChildHandleBuffer
63 );
64
65 //
66 // Block I/O Protocol Interface
67 //
68 STATIC
69 EFI_STATUS
70 EFIAPI
71 USBFloppyReset (
72 IN EFI_BLOCK_IO_PROTOCOL *This,
73 IN BOOLEAN ExtendedVerification
74 );
75
76 STATIC
77 EFI_STATUS
78 EFIAPI
79 USBFloppyReadBlocks (
80 IN EFI_BLOCK_IO_PROTOCOL *This,
81 IN UINT32 MediaId,
82 IN EFI_LBA LBA,
83 IN UINTN BufferSize,
84 OUT VOID *Buffer
85 );
86
87 STATIC
88 EFI_STATUS
89 EFIAPI
90 USBFloppyWriteBlocks (
91 IN EFI_BLOCK_IO_PROTOCOL *This,
92 IN UINT32 MediaId,
93 IN EFI_LBA LBA,
94 IN UINTN BufferSize,
95 IN VOID *Buffer
96 );
97
98 STATIC
99 EFI_STATUS
100 EFIAPI
101 USBFloppyFlushBlocks (
102 IN EFI_BLOCK_IO_PROTOCOL *This
103 );
104
105 //
106 // USB Floppy Driver Global Variables
107 //
108 EFI_DRIVER_BINDING_PROTOCOL gUSBFloppyDriverBinding = {
109 USBFloppyDriverBindingSupported,
110 USBFloppyDriverBindingStart,
111 USBFloppyDriverBindingStop,
112 0xa,
113 NULL,
114 NULL
115 };
116
117 EFI_STATUS
118 EFIAPI
119 USBFloppyDriverBindingSupported (
120 IN EFI_DRIVER_BINDING_PROTOCOL *This,
121 IN EFI_HANDLE Controller,
122 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
123 )
124 /*++
125
126 Routine Description:
127 Test to see if this driver supports ControllerHandle. Any ControllerHandle
128 that has UsbHcProtocol installed will be supported.
129
130 Arguments:
131 This - Protocol instance pointer.
132 Controller - Handle of device to test
133 RemainingDevicePath - Not used
134
135 Returns:
136 EFI_SUCCESS - This driver supports this device.
137 EFI_UNSUPPORTED - This driver does not support this device.
138
139 --*/
140 {
141 EFI_STATUS OpenStatus;
142 EFI_USB_ATAPI_PROTOCOL *AtapiProtocol;
143
144 //
145 // check whether EFI_USB_ATAPI_PROTOCOL exists, if it does,
146 // then the controller must be a USB Mass Storage Controller
147 //
148 OpenStatus = gBS->OpenProtocol (
149 Controller,
150 &gEfiUsbAtapiProtocolGuid,
151 (VOID **) &AtapiProtocol,
152 This->DriverBindingHandle,
153 Controller,
154 EFI_OPEN_PROTOCOL_BY_DRIVER
155 );
156 if (EFI_ERROR (OpenStatus)) {
157 return OpenStatus;
158 }
159
160 gBS->CloseProtocol (
161 Controller,
162 &gEfiUsbAtapiProtocolGuid,
163 This->DriverBindingHandle,
164 Controller
165 );
166
167 return EFI_SUCCESS;
168 }
169
170 EFI_STATUS
171 EFIAPI
172 USBFloppyDriverBindingStart (
173 IN EFI_DRIVER_BINDING_PROTOCOL *This,
174 IN EFI_HANDLE Controller,
175 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
176 )
177 /*++
178
179 Routine Description:
180 Starting the Usb Bus Driver
181
182 Arguments:
183 This - Protocol instance pointer.
184 Controller - Handle of device to test
185 RemainingDevicePath - Not used
186
187 Returns:
188 EFI_SUCCESS - This driver supports this device.
189 EFI_UNSUPPORTED - This driver does not support this device.
190 EFI_DEVICE_ERROR - This driver cannot be started due to device
191 Error
192 EFI_OUT_OF_RESOURCES- Can't allocate memory resources
193 EFI_ALREADY_STARTED - Thios driver has been started
194 --*/
195 {
196 EFI_STATUS Status;
197 EFI_USB_ATAPI_PROTOCOL *AtapiProtocol;
198 USB_FLOPPY_DEV *UsbFloppyDevice;
199
200 UsbFloppyDevice = NULL;
201 //
202 // Check whether Usb Atapi Protocol attached on the controller handle.
203 //
204 Status = gBS->OpenProtocol (
205 Controller,
206 &gEfiUsbAtapiProtocolGuid,
207 (VOID **) &AtapiProtocol,
208 This->DriverBindingHandle,
209 Controller,
210 EFI_OPEN_PROTOCOL_BY_DRIVER
211 );
212 if (EFI_ERROR (Status)) {
213 return Status;
214 }
215
216 Status = gBS->AllocatePool (
217 EfiBootServicesData,
218 sizeof (USB_FLOPPY_DEV),
219 (VOID **) &UsbFloppyDevice
220 );
221 if (EFI_ERROR (Status)) {
222 gBS->CloseProtocol (
223 Controller,
224 &gEfiUsbAtapiProtocolGuid,
225 This->DriverBindingHandle,
226 Controller
227 );
228 return Status;
229 }
230
231 ZeroMem (UsbFloppyDevice, sizeof (USB_FLOPPY_DEV));
232
233 UsbFloppyDevice->Handle = Controller;
234 UsbFloppyDevice->BlkIo.Media = &UsbFloppyDevice->BlkMedia;
235 UsbFloppyDevice->Signature = USB_FLOPPY_DEV_SIGNATURE;
236 UsbFloppyDevice->BlkIo.Reset = USBFloppyReset;
237 UsbFloppyDevice->BlkIo.ReadBlocks = USBFloppyReadBlocks;
238 UsbFloppyDevice->BlkIo.WriteBlocks = USBFloppyWriteBlocks;
239 UsbFloppyDevice->BlkIo.FlushBlocks = USBFloppyFlushBlocks;
240 UsbFloppyDevice->AtapiProtocol = AtapiProtocol;
241
242 //
243 // Identify drive type and retrieve media information.
244 //
245 Status = USBFloppyIdentify (UsbFloppyDevice);
246 if (EFI_ERROR (Status)) {
247 if (UsbFloppyDevice->SenseData != NULL) {
248 gBS->FreePool (UsbFloppyDevice->SenseData);
249 }
250
251 gBS->FreePool (UsbFloppyDevice);
252 gBS->CloseProtocol (
253 Controller,
254 &gEfiUsbAtapiProtocolGuid,
255 This->DriverBindingHandle,
256 Controller
257 );
258 return Status;
259 }
260 //
261 // Install Block I/O protocol for the usb floppy device.
262 //
263 Status = gBS->InstallProtocolInterface (
264 &Controller,
265 &gEfiBlockIoProtocolGuid,
266 EFI_NATIVE_INTERFACE,
267 &UsbFloppyDevice->BlkIo
268 );
269 if (EFI_ERROR (Status)) {
270 if (UsbFloppyDevice->SenseData != NULL) {
271 gBS->FreePool (UsbFloppyDevice->SenseData);
272 }
273
274 gBS->FreePool (UsbFloppyDevice);
275 gBS->CloseProtocol (
276 Controller,
277 &gEfiUsbAtapiProtocolGuid,
278 This->DriverBindingHandle,
279 Controller
280 );
281 return Status;
282 }
283
284 return EFI_SUCCESS;
285
286 }
287
288
289 EFI_STATUS
290 EFIAPI
291 USBFloppyDriverBindingStop (
292 IN EFI_DRIVER_BINDING_PROTOCOL *This,
293 IN EFI_HANDLE Controller,
294 IN UINTN NumberOfChildren,
295 IN EFI_HANDLE *ChildHandleBuffer
296 )
297 /*++
298
299 Routine Description:
300 Stop this driver on ControllerHandle. Support stoping any child handles
301 created by this driver.
302
303 Arguments:
304 This - Protocol instance pointer.
305 Controller - Handle of device to stop driver on
306 NumberOfChildren - Number of Children in the ChildHandleBuffer
307 ChildHandleBuffer - List of handles for the children we need to stop.
308
309 Returns:
310 EFI_SUCCESS
311 EFI_DEVICE_ERROR
312 others
313
314 --*/
315 {
316 EFI_STATUS Status;
317 USB_FLOPPY_DEV *UsbFloppyDevice;
318 EFI_BLOCK_IO_PROTOCOL *BlkIo;
319
320 //
321 // First find USB_FLOPPY_DEV
322 //
323 gBS->OpenProtocol (
324 Controller,
325 &gEfiBlockIoProtocolGuid,
326 (VOID **) &BlkIo,
327 This->DriverBindingHandle,
328 Controller,
329 EFI_OPEN_PROTOCOL_GET_PROTOCOL
330 );
331
332 UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (BlkIo);
333
334 //
335 // Uninstall Block I/O protocol from the device handle
336 //
337 Status = gBS->UninstallProtocolInterface (
338 Controller,
339 &gEfiBlockIoProtocolGuid,
340 &UsbFloppyDevice->BlkIo
341 );
342 if (EFI_ERROR (Status)) {
343 return Status;
344 }
345 //
346 // Stop using EFI_USB_ATAPI_PROTOCOL
347 //
348 gBS->CloseProtocol (
349 Controller,
350 &gEfiUsbAtapiProtocolGuid,
351 This->DriverBindingHandle,
352 Controller
353 );
354
355 if (UsbFloppyDevice->SenseData != NULL) {
356 gBS->FreePool (UsbFloppyDevice->SenseData);
357 }
358
359 gBS->FreePool (UsbFloppyDevice);
360
361 return EFI_SUCCESS;
362 }
363
364
365 STATIC
366 EFI_STATUS
367 EFIAPI
368 USBFloppyReset (
369 IN EFI_BLOCK_IO_PROTOCOL *This,
370 IN BOOLEAN ExtendedVerification
371 )
372 /*++
373
374 Routine Description:
375 Implements EFI_BLOCK_IO_PROTOCOL.Reset() function.
376
377 Arguments:
378 This The EFI_BLOCK_IO_PROTOCOL instance.
379 ExtendedVerification
380 Indicates that the driver may perform a more exhaustive
381 verification operation of the device during reset.
382 (This parameter is ingored in this driver.)
383
384 Returns:
385 EFI_SUCCESS - Success
386 --*/
387 {
388 USB_FLOPPY_DEV *UsbFloppyDevice;
389 EFI_USB_ATAPI_PROTOCOL *UsbAtapiInterface;
390 EFI_STATUS Status;
391
392 UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (This);
393
394 UsbAtapiInterface = UsbFloppyDevice->AtapiProtocol;
395
396 //
397 // directly calling EFI_USB_ATAPI_PROTOCOL.Reset() to implement reset.
398 //
399 Status = UsbAtapiInterface->UsbAtapiReset (UsbAtapiInterface, TRUE);
400
401 return Status;
402 }
403
404 STATIC
405 EFI_STATUS
406 EFIAPI
407 USBFloppyReadBlocks (
408 IN EFI_BLOCK_IO_PROTOCOL *This,
409 IN UINT32 MediaId,
410 IN EFI_LBA LBA,
411 IN UINTN BufferSize,
412 OUT VOID *Buffer
413 )
414 /*++
415
416 Routine Description:
417 Implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks() function.
418
419 Arguments:
420 This The EFI_BLOCK_IO_PROTOCOL instance.
421 MediaId The media id that the read request is for.
422 LBA The starting logical block address to read from on the device.
423 BufferSize
424 The size of the Buffer in bytes. This must be a multiple of
425 the intrinsic block size of the device.
426 Buffer A pointer to the destination buffer for the data. The caller
427 is responsible for either having implicit or explicit ownership
428 of the buffer.
429
430 Returns:
431 EFI_INVALID_PARAMETER - Parameter is error
432 EFI_SUCCESS - Success
433 EFI_DEVICE_ERROR - Hardware Error
434 EFI_NO_MEDIA - No media
435 EFI_MEDIA_CHANGED - Media Change
436 EFI_BAD_BUFFER_SIZE - Buffer size is bad
437 --*/
438 {
439 USB_FLOPPY_DEV *UsbFloppyDevice;
440 EFI_STATUS Status;
441 EFI_BLOCK_IO_MEDIA *Media;
442 UINTN BlockSize;
443 UINTN NumberOfBlocks;
444 BOOLEAN MediaChange;
445 EFI_TPL OldTpl;
446
447 OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);
448 Status = EFI_SUCCESS;
449 MediaChange = FALSE;
450
451 UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (This);
452
453 //
454 // Check parameters
455 //
456 if (!Buffer) {
457 Status = EFI_INVALID_PARAMETER;
458 goto Done;
459 }
460
461 if (BufferSize == 0) {
462 Status = EFI_SUCCESS;
463 goto Done;
464 }
465
466 UsbFloppyTestUnitReady (UsbFloppyDevice);
467
468 Status = UsbFloppyDetectMedia (UsbFloppyDevice, &MediaChange);
469 if (EFI_ERROR (Status)) {
470
471 Status = EFI_DEVICE_ERROR;
472 goto Done;
473 }
474
475 if (MediaChange) {
476 gBS->RestoreTPL (OldTpl);
477 gBS->ReinstallProtocolInterface (
478 UsbFloppyDevice->Handle,
479 &gEfiBlockIoProtocolGuid,
480 &UsbFloppyDevice->BlkIo,
481 &UsbFloppyDevice->BlkIo
482 );
483 gBS->RaiseTPL (EFI_TPL_NOTIFY);
484 }
485
486 Media = UsbFloppyDevice->BlkIo.Media;
487 BlockSize = Media->BlockSize;
488 NumberOfBlocks = BufferSize / BlockSize;
489
490 if (!(Media->MediaPresent)) {
491 Status = EFI_NO_MEDIA;
492 goto Done;
493 }
494
495 if (MediaId != Media->MediaId) {
496 Status = EFI_MEDIA_CHANGED;
497 goto Done;
498 }
499
500 if (BufferSize % BlockSize != 0) {
501 Status = EFI_BAD_BUFFER_SIZE;
502 goto Done;
503 }
504
505 if (LBA > Media->LastBlock) {
506 Status = EFI_INVALID_PARAMETER;
507 goto Done;
508 }
509
510 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
511 Status = EFI_INVALID_PARAMETER;
512 goto Done;
513 }
514
515 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
516 Status = EFI_INVALID_PARAMETER;
517 goto Done;
518 }
519
520 if (!EFI_ERROR (Status)) {
521
522 Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, 1);
523 if (EFI_ERROR (Status)) {
524 This->Reset (This, TRUE);
525 Status = EFI_DEVICE_ERROR;
526 goto Done;
527 }
528
529 LBA += 1;
530 NumberOfBlocks -= 1;
531 Buffer = (UINT8 *) Buffer + This->Media->BlockSize;
532
533 if (NumberOfBlocks == 0) {
534 Status = EFI_SUCCESS;
535 goto Done;
536 }
537
538 Status = USBFloppyRead10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);
539 if (EFI_ERROR (Status)) {
540 This->Reset (This, TRUE);
541 Status = EFI_DEVICE_ERROR;
542 }
543 }
544
545 Done:
546 gBS->RestoreTPL (OldTpl);
547 return Status;
548 }
549
550 STATIC
551 EFI_STATUS
552 EFIAPI
553 USBFloppyWriteBlocks (
554 IN EFI_BLOCK_IO_PROTOCOL *This,
555 IN UINT32 MediaId,
556 IN EFI_LBA LBA,
557 IN UINTN BufferSize,
558 IN VOID *Buffer
559 )
560 /*++
561
562 Routine Description:
563 Implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks() function.
564
565 Arguments:
566 This The EFI_BLOCK_IO_PROTOCOL instance.
567 MediaId The media id that the write request is for.
568 LBA The starting logical block address to be written.
569 The caller is responsible for writing to only
570 legitimate locations.
571 BufferSize
572 The size of the Buffer in bytes. This must be a multiple of
573 the intrinsic block size of the device.
574 Buffer A pointer to the source buffer for the data. The caller
575 is responsible for either having implicit or explicit ownership
576 of the buffer.
577
578 Returns:
579 EFI_INVALID_PARAMETER - Parameter is error
580 EFI_SUCCESS - Success
581 EFI_DEVICE_ERROR - Hardware Error
582 EFI_NO_MEDIA - No media
583 EFI_MEDIA_CHANGED - Media Change
584 EFI_BAD_BUFFER_SIZE - Buffer size is bad
585
586 --*/
587 {
588 USB_FLOPPY_DEV *UsbFloppyDevice;
589 EFI_STATUS Status;
590 EFI_BLOCK_IO_MEDIA *Media;
591 UINTN BlockSize;
592 UINTN NumberOfBlocks;
593 BOOLEAN MediaChange;
594 EFI_TPL OldTpl;
595
596 OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);
597 Status = EFI_SUCCESS;
598 MediaChange = FALSE;
599
600 UsbFloppyDevice = USB_FLOPPY_DEV_FROM_THIS (This);
601
602 //
603 // Check parameters
604 //
605 if (!Buffer) {
606 Status = EFI_INVALID_PARAMETER;
607 goto Done;
608 }
609
610 if (BufferSize == 0) {
611 Status = EFI_SUCCESS;
612 goto Done;
613 }
614
615 UsbFloppyTestUnitReady (UsbFloppyDevice);
616
617 Status = UsbFloppyDetectMedia (UsbFloppyDevice, &MediaChange);
618 if (EFI_ERROR (Status)) {
619
620 Status = EFI_DEVICE_ERROR;
621 goto Done;
622 }
623
624 if (MediaChange) {
625 gBS->RestoreTPL (OldTpl);
626 gBS->ReinstallProtocolInterface (
627 UsbFloppyDevice->Handle,
628 &gEfiBlockIoProtocolGuid,
629 &UsbFloppyDevice->BlkIo,
630 &UsbFloppyDevice->BlkIo
631 );
632 gBS->RaiseTPL (EFI_TPL_NOTIFY);
633 }
634
635 Media = UsbFloppyDevice->BlkIo.Media;
636 BlockSize = Media->BlockSize;
637 NumberOfBlocks = BufferSize / BlockSize;
638
639 if (!(Media->MediaPresent)) {
640 Status = EFI_NO_MEDIA;
641 goto Done;
642 }
643
644 if (MediaId != Media->MediaId) {
645 Status = EFI_MEDIA_CHANGED;
646 goto Done;
647 }
648
649 if (BufferSize % BlockSize != 0) {
650 Status = EFI_BAD_BUFFER_SIZE;
651 goto Done;
652 }
653
654 if (LBA > Media->LastBlock) {
655 Status = EFI_INVALID_PARAMETER;
656 goto Done;
657 }
658
659 if ((LBA + NumberOfBlocks - 1) > Media->LastBlock) {
660 Status = EFI_INVALID_PARAMETER;
661 goto Done;
662 }
663
664 if ((Media->IoAlign > 1) && (((UINTN) Buffer & (Media->IoAlign - 1)) != 0)) {
665 Status = EFI_INVALID_PARAMETER;
666 goto Done;
667 }
668
669 if (UsbFloppyDevice->BlkMedia.ReadOnly) {
670 Status = EFI_WRITE_PROTECTED;
671 goto Done;
672 }
673
674 if (!EFI_ERROR (Status)) {
675 Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, 1);
676 if (EFI_ERROR (Status)) {
677 This->Reset (This, TRUE);
678 Status = EFI_DEVICE_ERROR;
679 goto Done;
680 }
681
682 LBA += 1;
683 NumberOfBlocks -= 1;
684 Buffer = (UINT8 *) Buffer + This->Media->BlockSize;
685
686 if (NumberOfBlocks == 0) {
687 Status = EFI_SUCCESS;
688 goto Done;
689 }
690
691 Status = USBFloppyWrite10 (UsbFloppyDevice, Buffer, LBA, NumberOfBlocks);
692 if (EFI_ERROR (Status)) {
693 This->Reset (This, TRUE);
694 Status = EFI_DEVICE_ERROR;
695 }
696 }
697
698 Done:
699 gBS->RestoreTPL (OldTpl);
700 return Status;
701 }
702
703 STATIC
704 EFI_STATUS
705 EFIAPI
706 USBFloppyFlushBlocks (
707 IN EFI_BLOCK_IO_PROTOCOL *This
708 )
709 /*++
710
711 Routine Description:
712 Implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks() function.
713 (In this driver, this function just returns EFI_SUCCESS.)
714
715 Arguments:
716 This The EFI_BLOCK_IO_PROTOCOL instance.
717
718 Returns:
719 EFI_SUCCESS - Success
720 --*/
721 {
722 return EFI_SUCCESS;
723 }