3 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "UsbBotPeim.h"
15 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
= {
16 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
21 EFI_PEI_RECOVERY_BLOCK_IO_PPI mRecoveryBlkIoPpi
= {
22 BotGetNumberOfBlockDevices
,
27 EFI_PEI_RECOVERY_BLOCK_IO2_PPI mRecoveryBlkIo2Ppi
= {
28 EFI_PEI_RECOVERY_BLOCK_IO2_PPI_REVISION
,
29 BotGetNumberOfBlockDevices2
,
34 EFI_PEI_PPI_DESCRIPTOR mPpiList
[2] = {
36 EFI_PEI_PPI_DESCRIPTOR_PPI
,
37 &gEfiPeiVirtualBlockIoPpiGuid
,
41 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
42 &gEfiPeiVirtualBlockIo2PpiGuid
,
48 Detect whether the removable media is present and whether it has changed.
50 @param[in] PeiServices General-purpose services that are available to every
52 @param[in] PeiBotDev Indicates the PEI_BOT_DEVICE instance.
54 @retval EFI_SUCCESS The media status is successfully checked.
55 @retval Other Failed to detect media.
60 IN EFI_PEI_SERVICES
**PeiServices
,
61 IN PEI_BOT_DEVICE
*PeiBotDev
65 Initializes the Usb Bot.
67 @param FileHandle Handle of the file being invoked.
68 @param PeiServices Describes the list of possible PEI Services.
70 @retval EFI_SUCCESS Usb bot driver is successfully initialized.
71 @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.
76 PeimInitializeUsbBot (
77 IN EFI_PEI_FILE_HANDLE FileHandle
,
78 IN CONST EFI_PEI_SERVICES
**PeiServices
82 UINTN UsbIoPpiInstance
;
83 EFI_PEI_PPI_DESCRIPTOR
*TempPpiDescriptor
;
84 PEI_USB_IO_PPI
*UsbIoPpi
;
87 // Shadow this PEIM to run from memory
89 if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle
))) {
94 // locate all usb io PPIs
96 for (UsbIoPpiInstance
= 0; UsbIoPpiInstance
< PEI_FAT_MAX_USB_IO_PPI
; UsbIoPpiInstance
++) {
97 Status
= PeiServicesLocatePpi (
103 if (EFI_ERROR (Status
)) {
109 // Register a notify function
111 return PeiServicesNotifyPpi (&mNotifyList
);
115 UsbIo installation notification function.
117 This function finds out all the current USB IO PPIs in the system and add them
120 @param PeiServices Indirect reference to the PEI Services Table.
121 @param NotifyDesc Address of the notification descriptor data structure.
122 @param InvokePpi Address of the PPI that was invoked.
124 @retval EFI_SUCCESS The function completes successfully.
130 IN EFI_PEI_SERVICES
**PeiServices
,
131 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
135 PEI_USB_IO_PPI
*UsbIoPpi
;
137 UsbIoPpi
= (PEI_USB_IO_PPI
*)InvokePpi
;
139 InitUsbBot (PeiServices
, UsbIoPpi
);
145 Initialize the usb bot device.
147 @param[in] PeiServices General-purpose services that are available to every
149 @param[in] UsbIoPpi Indicates the PEI_USB_IO_PPI instance.
151 @retval EFI_SUCCESS The usb bot device is initialized successfully.
152 @retval Other Failed to initialize media.
157 IN EFI_PEI_SERVICES
**PeiServices
,
158 IN PEI_USB_IO_PPI
*UsbIoPpi
161 PEI_BOT_DEVICE
*PeiBotDevice
;
163 EFI_USB_INTERFACE_DESCRIPTOR
*InterfaceDesc
;
165 EFI_PHYSICAL_ADDRESS AllocateAddress
;
166 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDesc
;
170 // Check its interface
172 Status
= UsbIoPpi
->UsbGetInterfaceDescriptor (
177 if (EFI_ERROR (Status
)) {
182 // Check if it is the BOT device we support
184 if ((InterfaceDesc
->InterfaceClass
!= 0x08) || (InterfaceDesc
->InterfaceProtocol
!= 0x50)) {
185 return EFI_NOT_FOUND
;
188 MemPages
= sizeof (PEI_BOT_DEVICE
) / EFI_PAGE_SIZE
+ 1;
189 Status
= PeiServicesAllocatePages (
194 if (EFI_ERROR (Status
)) {
198 PeiBotDevice
= (PEI_BOT_DEVICE
*)((UINTN
)AllocateAddress
);
200 PeiBotDevice
->Signature
= PEI_BOT_DEVICE_SIGNATURE
;
201 PeiBotDevice
->UsbIoPpi
= UsbIoPpi
;
202 PeiBotDevice
->AllocateAddress
= (UINTN
)AllocateAddress
;
203 PeiBotDevice
->BotInterface
= InterfaceDesc
;
208 PeiBotDevice
->Media
.DeviceType
= UsbMassStorage
;
209 PeiBotDevice
->Media
.BlockSize
= 0x200;
210 PeiBotDevice
->Media2
.InterfaceType
= MSG_USB_DP
;
211 PeiBotDevice
->Media2
.BlockSize
= 0x200;
212 PeiBotDevice
->Media2
.RemovableMedia
= FALSE
;
213 PeiBotDevice
->Media2
.ReadOnly
= FALSE
;
216 // Check its Bulk-in/Bulk-out endpoint
218 for (Index
= 0; Index
< 2; Index
++) {
219 Status
= UsbIoPpi
->UsbGetEndpointDescriptor (
226 if (EFI_ERROR (Status
)) {
230 if ((EndpointDesc
->EndpointAddress
& 0x80) != 0) {
231 PeiBotDevice
->BulkInEndpoint
= EndpointDesc
;
233 PeiBotDevice
->BulkOutEndpoint
= EndpointDesc
;
238 &(PeiBotDevice
->BlkIoPpi
),
240 sizeof (EFI_PEI_RECOVERY_BLOCK_IO_PPI
)
243 &(PeiBotDevice
->BlkIo2Ppi
),
245 sizeof (EFI_PEI_RECOVERY_BLOCK_IO2_PPI
)
248 &(PeiBotDevice
->BlkIoPpiList
),
250 sizeof (EFI_PEI_PPI_DESCRIPTOR
)
253 &(PeiBotDevice
->BlkIo2PpiList
),
255 sizeof (EFI_PEI_PPI_DESCRIPTOR
)
257 PeiBotDevice
->BlkIoPpiList
.Ppi
= &PeiBotDevice
->BlkIoPpi
;
258 PeiBotDevice
->BlkIo2PpiList
.Ppi
= &PeiBotDevice
->BlkIo2Ppi
;
260 Status
= PeiUsbInquiry (PeiServices
, PeiBotDevice
);
261 if (EFI_ERROR (Status
)) {
265 Status
= PeiServicesAllocatePages (
270 if (EFI_ERROR (Status
)) {
274 PeiBotDevice
->SensePtr
= (ATAPI_REQUEST_SENSE_DATA
*)((UINTN
)AllocateAddress
);
276 Status
= PeiServicesInstallPpi (&PeiBotDevice
->BlkIoPpiList
);
278 if (EFI_ERROR (Status
)) {
286 Gets the count of block I/O devices that one specific block driver detects.
288 This function is used for getting the count of block I/O devices that one
289 specific block driver detects. To the PEI ATAPI driver, it returns the number
290 of all the detected ATAPI devices it detects during the enumeration process.
291 To the PEI legacy floppy driver, it returns the number of all the legacy
292 devices it finds during its enumeration process. If no device is detected,
293 then the function will return zero.
295 @param[in] PeiServices General-purpose services that are available
297 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
299 @param[out] NumberBlockDevices The number of block I/O devices discovered.
301 @retval EFI_SUCCESS Operation performed successfully.
306 BotGetNumberOfBlockDevices (
307 IN EFI_PEI_SERVICES
**PeiServices
,
308 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
309 OUT UINTN
*NumberBlockDevices
313 // For Usb devices, this value should be always 1
315 *NumberBlockDevices
= 1;
320 Gets a block device's media information.
322 This function will provide the caller with the specified block device's media
323 information. If the media changes, calling this function will update the media
324 information accordingly.
326 @param[in] PeiServices General-purpose services that are available to every
328 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
329 @param[in] DeviceIndex Specifies the block device to which the function wants
330 to talk. Because the driver that implements Block I/O
331 PPIs will manage multiple block devices, the PPIs that
332 want to talk to a single device must specify the
333 device index that was assigned during the enumeration
334 process. This index is a number from one to
336 @param[out] MediaInfo The media information of the specified block media.
337 The caller is responsible for the ownership of this
340 @retval EFI_SUCCESS Media information about the specified block device
341 was obtained successfully.
342 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
349 IN EFI_PEI_SERVICES
**PeiServices
,
350 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
351 IN UINTN DeviceIndex
,
352 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
355 PEI_BOT_DEVICE
*PeiBotDev
;
358 PeiBotDev
= PEI_BOT_DEVICE_FROM_THIS (This
);
361 // First test unit ready
363 PeiUsbTestUnitReady (
368 Status
= PeiBotDetectMedia (
373 if (EFI_ERROR (Status
)) {
374 return EFI_DEVICE_ERROR
;
380 sizeof (EFI_PEI_BLOCK_IO_MEDIA
)
387 Reads the requested number of blocks from the specified block device.
389 The function reads the requested number of blocks from the device. All the
390 blocks are read, or an error is returned. If there is no media in the device,
391 the function returns EFI_NO_MEDIA.
393 @param[in] PeiServices General-purpose services that are available to
395 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
396 @param[in] DeviceIndex Specifies the block device to which the function wants
397 to talk. Because the driver that implements Block I/O
398 PPIs will manage multiple block devices, the PPIs that
399 want to talk to a single device must specify the device
400 index that was assigned during the enumeration process.
401 This index is a number from one to NumberBlockDevices.
402 @param[in] StartLBA The starting logical block address (LBA) to read from
404 @param[in] BufferSize The size of the Buffer in bytes. This number must be
405 a multiple of the intrinsic block size of the device.
406 @param[out] Buffer A pointer to the destination buffer for the data.
407 The caller is responsible for the ownership of the
410 @retval EFI_SUCCESS The data was read correctly from the device.
411 @retval EFI_DEVICE_ERROR The device reported an error while attempting
412 to perform the read operation.
413 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
414 valid, or the buffer is not properly aligned.
415 @retval EFI_NO_MEDIA There is no media in the device.
416 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
417 the intrinsic block size of the device.
423 IN EFI_PEI_SERVICES
**PeiServices
,
424 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
425 IN UINTN DeviceIndex
,
426 IN EFI_PEI_LBA StartLBA
,
431 PEI_BOT_DEVICE
*PeiBotDev
;
434 UINTN NumberOfBlocks
;
436 Status
= EFI_SUCCESS
;
437 PeiBotDev
= PEI_BOT_DEVICE_FROM_THIS (This
);
442 if (Buffer
== NULL
) {
443 return EFI_INVALID_PARAMETER
;
446 if (BufferSize
== 0) {
450 if (!PeiBotDev
->Media
.MediaPresent
) {
454 BlockSize
= PeiBotDev
->Media
.BlockSize
;
456 if (BufferSize
% BlockSize
!= 0) {
457 Status
= EFI_BAD_BUFFER_SIZE
;
460 if (StartLBA
> PeiBotDev
->Media2
.LastBlock
) {
461 Status
= EFI_INVALID_PARAMETER
;
464 NumberOfBlocks
= BufferSize
/ (PeiBotDev
->Media
.BlockSize
);
466 if (Status
== EFI_SUCCESS
) {
467 Status
= PeiUsbTestUnitReady (
471 if (Status
== EFI_SUCCESS
) {
472 Status
= PeiUsbRead10 (
482 // To generate sense data for DetectMedia use.
484 PeiUsbTestUnitReady (
490 if (EFI_ERROR (Status
)) {
492 // if any error encountered, detect what happened to the media and
493 // update the media info accordingly.
495 Status
= PeiBotDetectMedia (
499 if (Status
!= EFI_SUCCESS
) {
500 return EFI_DEVICE_ERROR
;
503 NumberOfBlocks
= BufferSize
/ PeiBotDev
->Media
.BlockSize
;
505 if (!(PeiBotDev
->Media
.MediaPresent
)) {
509 if (BufferSize
% (PeiBotDev
->Media
.BlockSize
) != 0) {
510 return EFI_BAD_BUFFER_SIZE
;
513 if (StartLBA
> PeiBotDev
->Media2
.LastBlock
) {
514 return EFI_INVALID_PARAMETER
;
517 if ((StartLBA
+ NumberOfBlocks
- 1) > PeiBotDev
->Media2
.LastBlock
) {
518 return EFI_INVALID_PARAMETER
;
521 Status
= PeiUsbRead10 (
534 return EFI_DEVICE_ERROR
;
539 Buffer
= (UINT8
*)Buffer
+ PeiBotDev
->Media
.BlockSize
;
541 if (NumberOfBlocks
== 0) {
545 Status
= PeiUsbRead10 (
557 return EFI_DEVICE_ERROR
;
563 Gets the count of block I/O devices that one specific block driver detects.
565 This function is used for getting the count of block I/O devices that one
566 specific block driver detects. To the PEI ATAPI driver, it returns the number
567 of all the detected ATAPI devices it detects during the enumeration process.
568 To the PEI legacy floppy driver, it returns the number of all the legacy
569 devices it finds during its enumeration process. If no device is detected,
570 then the function will return zero.
572 @param[in] PeiServices General-purpose services that are available
574 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
576 @param[out] NumberBlockDevices The number of block I/O devices discovered.
578 @retval EFI_SUCCESS Operation performed successfully.
583 BotGetNumberOfBlockDevices2 (
584 IN EFI_PEI_SERVICES
**PeiServices
,
585 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
586 OUT UINTN
*NumberBlockDevices
590 // For Usb devices, this value should be always 1
592 *NumberBlockDevices
= 1;
597 Gets a block device's media information.
599 This function will provide the caller with the specified block device's media
600 information. If the media changes, calling this function will update the media
601 information accordingly.
603 @param[in] PeiServices General-purpose services that are available to every
605 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
606 @param[in] DeviceIndex Specifies the block device to which the function wants
607 to talk. Because the driver that implements Block I/O
608 PPIs will manage multiple block devices, the PPIs that
609 want to talk to a single device must specify the
610 device index that was assigned during the enumeration
611 process. This index is a number from one to
613 @param[out] MediaInfo The media information of the specified block media.
614 The caller is responsible for the ownership of this
617 @retval EFI_SUCCESS Media information about the specified block device
618 was obtained successfully.
619 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
626 IN EFI_PEI_SERVICES
**PeiServices
,
627 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
628 IN UINTN DeviceIndex
,
629 OUT EFI_PEI_BLOCK_IO2_MEDIA
*MediaInfo
632 PEI_BOT_DEVICE
*PeiBotDev
;
635 PeiBotDev
= PEI_BOT_DEVICE2_FROM_THIS (This
);
637 Status
= BotGetMediaInfo (
639 &PeiBotDev
->BlkIoPpi
,
644 if (EFI_ERROR (Status
)) {
650 &(PeiBotDev
->Media2
),
651 sizeof (EFI_PEI_BLOCK_IO2_MEDIA
)
658 Reads the requested number of blocks from the specified block device.
660 The function reads the requested number of blocks from the device. All the
661 blocks are read, or an error is returned. If there is no media in the device,
662 the function returns EFI_NO_MEDIA.
664 @param[in] PeiServices General-purpose services that are available to
666 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
667 @param[in] DeviceIndex Specifies the block device to which the function wants
668 to talk. Because the driver that implements Block I/O
669 PPIs will manage multiple block devices, the PPIs that
670 want to talk to a single device must specify the device
671 index that was assigned during the enumeration process.
672 This index is a number from one to NumberBlockDevices.
673 @param[in] StartLBA The starting logical block address (LBA) to read from
675 @param[in] BufferSize The size of the Buffer in bytes. This number must be
676 a multiple of the intrinsic block size of the device.
677 @param[out] Buffer A pointer to the destination buffer for the data.
678 The caller is responsible for the ownership of the
681 @retval EFI_SUCCESS The data was read correctly from the device.
682 @retval EFI_DEVICE_ERROR The device reported an error while attempting
683 to perform the read operation.
684 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
685 valid, or the buffer is not properly aligned.
686 @retval EFI_NO_MEDIA There is no media in the device.
687 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
688 the intrinsic block size of the device.
694 IN EFI_PEI_SERVICES
**PeiServices
,
695 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
696 IN UINTN DeviceIndex
,
697 IN EFI_PEI_LBA StartLBA
,
702 PEI_BOT_DEVICE
*PeiBotDev
;
706 return EFI_INVALID_PARAMETER
;
709 Status
= EFI_SUCCESS
;
710 PeiBotDev
= PEI_BOT_DEVICE2_FROM_THIS (This
);
712 Status
= BotReadBlocks (
714 &PeiBotDev
->BlkIoPpi
,
725 Detect whether the removable media is present and whether it has changed.
727 @param[in] PeiServices General-purpose services that are available to every
729 @param[in] PeiBotDev Indicates the PEI_BOT_DEVICE instance.
731 @retval EFI_SUCCESS The media status is successfully checked.
732 @retval Other Failed to detect media.
737 IN EFI_PEI_SERVICES
**PeiServices
,
738 IN PEI_BOT_DEVICE
*PeiBotDev
742 EFI_STATUS FloppyStatus
;
744 BOOLEAN NeedReadCapacity
;
745 EFI_PHYSICAL_ADDRESS AllocateAddress
;
746 ATAPI_REQUEST_SENSE_DATA
*SensePtr
;
750 // if there is no media present,or media not changed,
751 // the request sense command will detect faster than read capacity command.
752 // read capacity command can be bypassed, thus improve performance.
755 NeedReadCapacity
= TRUE
;
757 Status
= PeiServicesAllocatePages (
762 if (EFI_ERROR (Status
)) {
766 SensePtr
= PeiBotDev
->SensePtr
;
767 ZeroMem (SensePtr
, EFI_PAGE_SIZE
);
769 Status
= PeiUsbRequestSense (
776 if (Status
== EFI_SUCCESS
) {
780 if (IsNoMedia (SensePtr
, SenseCounts
)) {
781 NeedReadCapacity
= FALSE
;
782 PeiBotDev
->Media
.MediaPresent
= FALSE
;
783 PeiBotDev
->Media
.LastBlock
= 0;
784 PeiBotDev
->Media2
.MediaPresent
= FALSE
;
785 PeiBotDev
->Media2
.LastBlock
= 0;
790 if (IsMediaChange (SensePtr
, SenseCounts
)) {
791 PeiBotDev
->Media
.MediaPresent
= TRUE
;
792 PeiBotDev
->Media2
.MediaPresent
= TRUE
;
798 if (IsMediaError (SensePtr
, SenseCounts
)) {
800 // if media error encountered, make it look like no media present.
802 PeiBotDev
->Media
.MediaPresent
= FALSE
;
803 PeiBotDev
->Media
.LastBlock
= 0;
804 PeiBotDev
->Media2
.MediaPresent
= FALSE
;
805 PeiBotDev
->Media2
.LastBlock
= 0;
810 if (NeedReadCapacity
) {
812 // Retry at most 4 times to detect media info
814 for (Retry
= 0; Retry
< 4; Retry
++) {
815 switch (PeiBotDev
->DeviceType
) {
817 Status
= PeiUsbReadCapacity (
824 Status
= PeiUsbReadFormattedCapacity (
828 if (EFI_ERROR (Status
) ||
829 !PeiBotDev
->Media
.MediaPresent
)
832 // retry the ReadCapacity command
834 PeiBotDev
->DeviceType
= USBFLOPPY
;
835 Status
= EFI_DEVICE_ERROR
;
841 Status
= PeiUsbReadCapacity (
845 if (EFI_ERROR (Status
)) {
847 // retry the ReadFormatCapacity command
849 PeiBotDev
->DeviceType
= USBFLOPPY2
;
855 return EFI_INVALID_PARAMETER
;
859 ZeroMem (SensePtr
, EFI_PAGE_SIZE
);
861 if (Status
== EFI_SUCCESS
) {
865 FloppyStatus
= PeiUsbRequestSense (
873 // If Request Sense data failed,retry.
875 if (EFI_ERROR (FloppyStatus
)) {
882 if (IsNoMedia (SensePtr
, SenseCounts
)) {
883 PeiBotDev
->Media
.MediaPresent
= FALSE
;
884 PeiBotDev
->Media
.LastBlock
= 0;
885 PeiBotDev
->Media2
.MediaPresent
= FALSE
;
886 PeiBotDev
->Media2
.LastBlock
= 0;
890 if (IsMediaError (SensePtr
, SenseCounts
)) {
892 // if media error encountered, make it look like no media present.
894 PeiBotDev
->Media
.MediaPresent
= FALSE
;
895 PeiBotDev
->Media
.LastBlock
= 0;
896 PeiBotDev
->Media2
.MediaPresent
= FALSE
;
897 PeiBotDev
->Media2
.LastBlock
= 0;
905 // tell whether the readcapacity process is successful or not
906 // ("Status" variable record the latest status returned
909 if (Status
!= EFI_SUCCESS
) {
910 return EFI_DEVICE_ERROR
;