3 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions
7 of the BSD License which accompanies this distribution. The
8 full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UsbBotPeim.h"
22 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
= {
23 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
28 EFI_PEI_RECOVERY_BLOCK_IO_PPI mRecoveryBlkIoPpi
= {
29 BotGetNumberOfBlockDevices
,
34 EFI_PEI_PPI_DESCRIPTOR mPpiList
= {
35 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
36 &gEfiPeiVirtualBlockIoPpiGuid
,
41 Detect whether the removable media is present and whether it has changed.
43 @param[in] PeiServices General-purpose services that are available to every
45 @param[in] PeiBotDev Indicates the PEI_BOT_DEVICE instance.
47 @retval EFI_SUCCESS The media status is successfully checked.
48 @retval Other Failed to detect media.
53 IN EFI_PEI_SERVICES
**PeiServices
,
54 IN PEI_BOT_DEVICE
*PeiBotDev
58 Initializes the Usb Bot.
60 @param FileHandle Handle of the file being invoked.
61 @param PeiServices Describes the list of possible PEI Services.
63 @retval EFI_SUCCESS Usb bot driver is successfully initialized.
64 @retval EFI_OUT_OF_RESOURCES Can't initialize the driver.
69 PeimInitializeUsbBot (
70 IN EFI_PEI_FILE_HANDLE FileHandle
,
71 IN CONST EFI_PEI_SERVICES
**PeiServices
75 UINTN UsbIoPpiInstance
;
76 EFI_PEI_PPI_DESCRIPTOR
*TempPpiDescriptor
;
77 PEI_USB_IO_PPI
*UsbIoPpi
;
80 // Shadow this PEIM to run from memory
82 if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle
))) {
87 // locate all usb io PPIs
89 for (UsbIoPpiInstance
= 0; UsbIoPpiInstance
< PEI_FAT_MAX_USB_IO_PPI
; UsbIoPpiInstance
++) {
91 Status
= PeiServicesLocatePpi (
97 if (EFI_ERROR (Status
)) {
102 // Register a notify function
104 return PeiServicesNotifyPpi (&mNotifyList
);
108 UsbIo installation notification function.
110 This function finds out all the current USB IO PPIs in the system and add them
113 @param PeiServices Indirect reference to the PEI Services Table.
114 @param NotifyDesc Address of the notification descriptor data structure.
115 @param InvokePpi Address of the PPI that was invoked.
117 @retval EFI_SUCCESS The function completes successfully.
123 IN EFI_PEI_SERVICES
**PeiServices
,
124 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
128 PEI_USB_IO_PPI
*UsbIoPpi
;
130 UsbIoPpi
= (PEI_USB_IO_PPI
*) InvokePpi
;
132 InitUsbBot (PeiServices
, UsbIoPpi
);
138 Initialize the usb bot device.
140 @param[in] PeiServices General-purpose services that are available to every
142 @param[in] UsbIoPpi Indicates the PEI_USB_IO_PPI instance.
144 @retval EFI_SUCCESS The usb bot device is initialized successfully.
145 @retval Other Failed to initialize media.
150 IN EFI_PEI_SERVICES
**PeiServices
,
151 IN PEI_USB_IO_PPI
*UsbIoPpi
154 PEI_BOT_DEVICE
*PeiBotDevice
;
156 EFI_USB_INTERFACE_DESCRIPTOR
*InterfaceDesc
;
158 EFI_PHYSICAL_ADDRESS AllocateAddress
;
159 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDesc
;
163 // Check its interface
165 Status
= UsbIoPpi
->UsbGetInterfaceDescriptor (
170 if (EFI_ERROR (Status
)) {
174 // Check if it is the BOT device we support
176 if ((InterfaceDesc
->InterfaceClass
!= 0x08) || (InterfaceDesc
->InterfaceProtocol
!= 0x50)) {
178 return EFI_NOT_FOUND
;
181 MemPages
= sizeof (PEI_BOT_DEVICE
) / EFI_PAGE_SIZE
+ 1;
182 Status
= PeiServicesAllocatePages (
187 if (EFI_ERROR (Status
)) {
191 PeiBotDevice
= (PEI_BOT_DEVICE
*) ((UINTN
) AllocateAddress
);
193 PeiBotDevice
->Signature
= PEI_BOT_DEVICE_SIGNATURE
;
194 PeiBotDevice
->UsbIoPpi
= UsbIoPpi
;
195 PeiBotDevice
->AllocateAddress
= (UINTN
) AllocateAddress
;
196 PeiBotDevice
->BotInterface
= InterfaceDesc
;
201 PeiBotDevice
->Media
.DeviceType
= UsbMassStorage
;
202 PeiBotDevice
->Media
.BlockSize
= 0x200;
205 // Check its Bulk-in/Bulk-out endpoint
207 for (Index
= 0; Index
< 2; Index
++) {
208 Status
= UsbIoPpi
->UsbGetEndpointDescriptor (
215 if (EFI_ERROR (Status
)) {
219 if ((EndpointDesc
->EndpointAddress
& 0x80) != 0) {
220 PeiBotDevice
->BulkInEndpoint
= EndpointDesc
;
222 PeiBotDevice
->BulkOutEndpoint
= EndpointDesc
;
227 &(PeiBotDevice
->BlkIoPpi
),
229 sizeof (EFI_PEI_RECOVERY_BLOCK_IO_PPI
)
232 &(PeiBotDevice
->BlkIoPpiList
),
234 sizeof (EFI_PEI_PPI_DESCRIPTOR
)
236 PeiBotDevice
->BlkIoPpiList
.Ppi
= &PeiBotDevice
->BlkIoPpi
;
238 Status
= PeiUsbInquiry (PeiServices
, PeiBotDevice
);
239 if (EFI_ERROR (Status
)) {
243 Status
= PeiServicesAllocatePages (
248 if (EFI_ERROR (Status
)) {
252 PeiBotDevice
->SensePtr
= (ATAPI_REQUEST_SENSE_DATA
*) ((UINTN
) AllocateAddress
);
254 Status
= PeiServicesInstallPpi (&PeiBotDevice
->BlkIoPpiList
);
256 if (EFI_ERROR (Status
)) {
264 Gets the count of block I/O devices that one specific block driver detects.
266 This function is used for getting the count of block I/O devices that one
267 specific block driver detects. To the PEI ATAPI driver, it returns the number
268 of all the detected ATAPI devices it detects during the enumeration process.
269 To the PEI legacy floppy driver, it returns the number of all the legacy
270 devices it finds during its enumeration process. If no device is detected,
271 then the function will return zero.
273 @param[in] PeiServices General-purpose services that are available
275 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
277 @param[out] NumberBlockDevices The number of block I/O devices discovered.
279 @retval EFI_SUCCESS Operation performed successfully.
284 BotGetNumberOfBlockDevices (
285 IN EFI_PEI_SERVICES
**PeiServices
,
286 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
287 OUT UINTN
*NumberBlockDevices
291 // For Usb devices, this value should be always 1
293 *NumberBlockDevices
= 1;
298 Gets a block device's media information.
300 This function will provide the caller with the specified block device's media
301 information. If the media changes, calling this function will update the media
302 information accordingly.
304 @param[in] PeiServices General-purpose services that are available to every
306 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
307 @param[in] DeviceIndex Specifies the block device to which the function wants
308 to talk. Because the driver that implements Block I/O
309 PPIs will manage multiple block devices, the PPIs that
310 want to talk to a single device must specify the
311 device index that was assigned during the enumeration
312 process. This index is a number from one to
314 @param[out] MediaInfo The media information of the specified block media.
315 The caller is responsible for the ownership of this
318 @retval EFI_SUCCESS Media information about the specified block device
319 was obtained successfully.
320 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
327 IN EFI_PEI_SERVICES
**PeiServices
,
328 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
329 IN UINTN DeviceIndex
,
330 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
333 PEI_BOT_DEVICE
*PeiBotDev
;
336 PeiBotDev
= PEI_BOT_DEVICE_FROM_THIS (This
);
339 // First test unit ready
341 PeiUsbTestUnitReady (
346 Status
= PeiBotDetectMedia (
351 if (EFI_ERROR (Status
)) {
352 return EFI_DEVICE_ERROR
;
358 sizeof (EFI_PEI_BLOCK_IO_MEDIA
)
365 Reads the requested number of blocks from the specified block device.
367 The function reads the requested number of blocks from the device. All the
368 blocks are read, or an error is returned. If there is no media in the device,
369 the function returns EFI_NO_MEDIA.
371 @param[in] PeiServices General-purpose services that are available to
373 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
374 @param[in] DeviceIndex Specifies the block device to which the function wants
375 to talk. Because the driver that implements Block I/O
376 PPIs will manage multiple block devices, the PPIs that
377 want to talk to a single device must specify the device
378 index that was assigned during the enumeration process.
379 This index is a number from one to NumberBlockDevices.
380 @param[in] StartLBA The starting logical block address (LBA) to read from
382 @param[in] BufferSize The size of the Buffer in bytes. This number must be
383 a multiple of the intrinsic block size of the device.
384 @param[out] Buffer A pointer to the destination buffer for the data.
385 The caller is responsible for the ownership of the
388 @retval EFI_SUCCESS The data was read correctly from the device.
389 @retval EFI_DEVICE_ERROR The device reported an error while attempting
390 to perform the read operation.
391 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
392 valid, or the buffer is not properly aligned.
393 @retval EFI_NO_MEDIA There is no media in the device.
394 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
395 the intrinsic block size of the device.
401 IN EFI_PEI_SERVICES
**PeiServices
,
402 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
403 IN UINTN DeviceIndex
,
404 IN EFI_PEI_LBA StartLBA
,
409 PEI_BOT_DEVICE
*PeiBotDev
;
412 UINTN NumberOfBlocks
;
414 Status
= EFI_SUCCESS
;
415 PeiBotDev
= PEI_BOT_DEVICE_FROM_THIS (This
);
420 if (Buffer
== NULL
) {
421 return EFI_INVALID_PARAMETER
;
424 if (BufferSize
== 0) {
428 if (!PeiBotDev
->Media
.MediaPresent
) {
432 BlockSize
= PeiBotDev
->Media
.BlockSize
;
434 if (BufferSize
% BlockSize
!= 0) {
435 Status
= EFI_BAD_BUFFER_SIZE
;
438 if (StartLBA
> PeiBotDev
->Media
.LastBlock
) {
439 Status
= EFI_INVALID_PARAMETER
;
442 NumberOfBlocks
= BufferSize
/ (PeiBotDev
->Media
.BlockSize
);
444 if (Status
== EFI_SUCCESS
) {
446 Status
= PeiUsbTestUnitReady (
450 if (Status
== EFI_SUCCESS
) {
451 Status
= PeiUsbRead10 (
461 // To generate sense data for DetectMedia use.
463 PeiUsbTestUnitReady (
469 if (EFI_ERROR (Status
)) {
471 // if any error encountered, detect what happened to the media and
472 // update the media info accordingly.
474 Status
= PeiBotDetectMedia (
478 if (Status
!= EFI_SUCCESS
) {
479 return EFI_DEVICE_ERROR
;
482 NumberOfBlocks
= BufferSize
/ PeiBotDev
->Media
.BlockSize
;
484 if (!(PeiBotDev
->Media
.MediaPresent
)) {
488 if (BufferSize
% (PeiBotDev
->Media
.BlockSize
) != 0) {
489 return EFI_BAD_BUFFER_SIZE
;
492 if (StartLBA
> PeiBotDev
->Media
.LastBlock
) {
493 return EFI_INVALID_PARAMETER
;
496 if ((StartLBA
+ NumberOfBlocks
- 1) > PeiBotDev
->Media
.LastBlock
) {
497 return EFI_INVALID_PARAMETER
;
500 Status
= PeiUsbRead10 (
514 return EFI_DEVICE_ERROR
;
519 Buffer
= (UINT8
*) Buffer
+ PeiBotDev
->Media
.BlockSize
;
521 if (NumberOfBlocks
== 0) {
525 Status
= PeiUsbRead10 (
538 return EFI_DEVICE_ERROR
;
545 Detect whether the removable media is present and whether it has changed.
547 @param[in] PeiServices General-purpose services that are available to every
549 @param[in] PeiBotDev Indicates the PEI_BOT_DEVICE instance.
551 @retval EFI_SUCCESS The media status is successfully checked.
552 @retval Other Failed to detect media.
557 IN EFI_PEI_SERVICES
**PeiServices
,
558 IN PEI_BOT_DEVICE
*PeiBotDev
562 EFI_STATUS FloppyStatus
;
564 BOOLEAN NeedReadCapacity
;
565 EFI_PHYSICAL_ADDRESS AllocateAddress
;
566 ATAPI_REQUEST_SENSE_DATA
*SensePtr
;
570 // if there is no media present,or media not changed,
571 // the request sense command will detect faster than read capacity command.
572 // read capacity command can be bypassed, thus improve performance.
575 NeedReadCapacity
= TRUE
;
577 Status
= PeiServicesAllocatePages (
582 if (EFI_ERROR (Status
)) {
586 SensePtr
= PeiBotDev
->SensePtr
;
587 ZeroMem (SensePtr
, EFI_PAGE_SIZE
);
589 Status
= PeiUsbRequestSense (
596 if (Status
== EFI_SUCCESS
) {
600 if (IsNoMedia (SensePtr
, SenseCounts
)) {
601 NeedReadCapacity
= FALSE
;
602 PeiBotDev
->Media
.MediaPresent
= FALSE
;
603 PeiBotDev
->Media
.LastBlock
= 0;
608 if (IsMediaChange (SensePtr
, SenseCounts
)) {
609 PeiBotDev
->Media
.MediaPresent
= TRUE
;
614 if (IsMediaError (SensePtr
, SenseCounts
)) {
616 // if media error encountered, make it look like no media present.
618 PeiBotDev
->Media
.MediaPresent
= FALSE
;
619 PeiBotDev
->Media
.LastBlock
= 0;
626 if (NeedReadCapacity
) {
628 // Retry at most 4 times to detect media info
630 for (Retry
= 0; Retry
< 4; Retry
++) {
631 switch (PeiBotDev
->DeviceType
) {
633 Status
= PeiUsbReadCapacity (
640 Status
= PeiUsbReadFormattedCapacity (
644 if (EFI_ERROR(Status
)||
645 !PeiBotDev
->Media
.MediaPresent
) {
647 // retry the ReadCapacity command
649 PeiBotDev
->DeviceType
= USBFLOPPY
;
650 Status
= EFI_DEVICE_ERROR
;
655 Status
= PeiUsbReadCapacity (
659 if (EFI_ERROR (Status
)) {
661 // retry the ReadFormatCapacity command
663 PeiBotDev
->DeviceType
= USBFLOPPY2
;
668 return EFI_INVALID_PARAMETER
;
672 ZeroMem (SensePtr
, EFI_PAGE_SIZE
);
674 if (Status
== EFI_SUCCESS
) {
678 FloppyStatus
= PeiUsbRequestSense (
686 // If Request Sense data failed,retry.
688 if (EFI_ERROR (FloppyStatus
)) {
694 if (IsNoMedia (SensePtr
, SenseCounts
)) {
695 PeiBotDev
->Media
.MediaPresent
= FALSE
;
696 PeiBotDev
->Media
.LastBlock
= 0;
700 if (IsMediaError (SensePtr
, SenseCounts
)) {
702 // if media error encountered, make it look like no media present.
704 PeiBotDev
->Media
.MediaPresent
= FALSE
;
705 PeiBotDev
->Media
.LastBlock
= 0;
712 // tell whether the readcapacity process is successful or not
713 // ("Status" variable record the latest status returned
716 if (Status
!= EFI_SUCCESS
) {
717 return EFI_DEVICE_ERROR
;