X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FUsb%2FUsbMassStorageDxe%2FUsbMassBoot.c;h=1b200bb4ec9f89b20857de2811853ba1de066f98;hp=80fdf470558d3626fb6e64ee5f5a4fbd8167c8d7;hb=1ccdbf2a3e61fe9494fcd39432107ba0eb74f584;hpb=e61d30b02a1463387aeea7f28f2ee4870b5fcc8a diff --git a/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c b/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c index 80fdf47055..1b200bb4ec 100644 --- a/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c +++ b/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c @@ -1,6 +1,8 @@ /** @file + Implementation of the command set of USB Mass Storage Specification + for Bootability, Revision 1.0. -Copyright (c) 2007, Intel Corporation +Copyright (c) 2007 - 2010, Intel Corporation All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -9,99 +11,21 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - UsbMassBoot.c - -Abstract: - - This file implement the command set of "USB Mass Storage Specification - for Bootability". - -Revision History - - **/ #include "UsbMassImpl.h" - -/** - Read an UINT32 from the buffer to avoid byte alignment problems, then - convert that to the little endia. The USB mass storage bootability spec - use big endia - - @param Buf The buffer contains the first byte of the UINT32 - in big endia. - - @return The UINT32 value read from the buffer in little endia. - -**/ -STATIC -UINT32 -UsbBootGetUint32 ( - IN UINT8 *Buf - ) -{ - UINT32 Value; - - CopyMem (&Value, Buf, sizeof (UINT32)); - return USB_BOOT_SWAP32 (Value); -} - - /** - Put an UINT32 in little endia to the buffer. The data is converted to - big endia before writing. + Execute REQUEST SENSE Command to retrieve sense data from device. - @param Buf The buffer to write data to - @param Data32 The data to write. + @param UsbMass The device whose sense data is requested. - @return None - -**/ -STATIC -VOID -UsbBootPutUint32 ( - IN UINT8 *Buf, - IN UINT32 Data32 - ) -{ - Data32 = USB_BOOT_SWAP32 (Data32); - CopyMem (Buf, &Data32, sizeof (UINT32)); -} - - -/** - Put an UINT16 in little endia to the buffer. The data is converted to - big endia before writing. - - @param Buf The buffer to write data to - @param Data16 The data to write - - @return None - -**/ -STATIC -VOID -UsbBootPutUint16 ( - IN UINT8 *Buf, - IN UINT16 Data16 - ) -{ - Data16 = USB_BOOT_SWAP16 (Data16); - CopyMem (Buf, &Data16, sizeof (UINT16)); -} - - -/** - Request sense information via sending Request Sense - Packet Command. - - @param UsbMass The device to be requested sense data - - @retval EFI_DEVICE_ERROR Hardware error - @retval EFI_SUCCESS Success + @retval EFI_SUCCESS The command is excuted successfully. + @retval EFI_DEVICE_ERROR Failed to request sense. + @retval EFI_NO_RESPONSE The device media doesn't response this request. + @retval EFI_INVALID_PARAMETER The command has some invalid parameters. + @retval EFI_WRITE_PROTECTED The device is write protected. + @retval EFI_MEDIA_CHANGED The device media has been changed. **/ EFI_STATUS @@ -119,13 +43,13 @@ UsbBootRequestSense ( Transport = UsbMass->Transport; // - // Request the sense data from the device if command failed + // Request the sense data from the device // ZeroMem (&SenseCmd, sizeof (USB_BOOT_REQUEST_SENSE_CMD)); ZeroMem (&SenseData, sizeof (USB_BOOT_REQUEST_SENSE_DATA)); SenseCmd.OpCode = USB_BOOT_REQUEST_SENSE_OPCODE; - SenseCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); + SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); SenseCmd.AllocLen = sizeof (USB_BOOT_REQUEST_SENSE_DATA); Status = Transport->ExecCommand ( @@ -135,22 +59,30 @@ UsbBootRequestSense ( EfiUsbDataIn, &SenseData, sizeof (USB_BOOT_REQUEST_SENSE_DATA), + UsbMass->Lun, USB_BOOT_GENERAL_CMD_TIMEOUT, &CmdResult ); if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) { - DEBUG ((mUsbMscError, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult)); - return EFI_DEVICE_ERROR; + DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult)); + if (!EFI_ERROR (Status)) { + Status = EFI_DEVICE_ERROR; + } + return Status; } // - // Interpret the sense data and update the media status if necessary. + // If sense data is retrieved successfully, interpret the sense data + // and update the media status if necessary. // Media = &UsbMass->BlockIoMedia; switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) { case USB_BOOT_SENSE_NO_SENSE: + Status = EFI_NO_RESPONSE; + break; + case USB_BOOT_SENSE_RECOVERED: // // Suppose hardware can handle this case, and recover later by itself @@ -159,26 +91,12 @@ UsbBootRequestSense ( break; case USB_BOOT_SENSE_NOT_READY: - switch (SenseData.ASC) { - case USB_BOOT_ASC_NO_MEDIA: - Status = EFI_NO_MEDIA; - Media->MediaPresent = FALSE; - break; - - case USB_BOOT_ASC_MEDIA_UPSIDE_DOWN: - Status = EFI_DEVICE_ERROR; + Status = EFI_DEVICE_ERROR; + if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) { Media->MediaPresent = FALSE; - break; - - case USB_BOOT_ASC_NOT_READY: - if (SenseData.ASCQ == USB_BOOT_ASCQ_IN_PROGRESS || - SenseData.ASCQ == USB_BOOT_ASCQ_DEVICE_BUSY) { - // - // Regular timeout, and need retry once more - // - DEBUG ((mUsbMscInfo, "UsbBootRequestSense: Not ready and need retry once more\n")); - Status = EFI_NOT_READY; - } + Status = EFI_NO_MEDIA; + } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) { + Status = EFI_NOT_READY; } break; @@ -188,15 +106,19 @@ UsbBootRequestSense ( case USB_BOOT_SENSE_UNIT_ATTENTION: Status = EFI_DEVICE_ERROR; - if (SenseData.ASC == USB_BOOT_ASC_MEDIA_CHANGE) { + if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) { + // + // If MediaChange, reset ReadOnly and new MediaId + // Status = EFI_MEDIA_CHANGED; - UsbMass->BlockIoMedia.MediaId++; + Media->ReadOnly = FALSE; + Media->MediaId++; } break; - case USB_BOOT_SNESE_DATA_PROTECT: - Status = EFI_WRITE_PROTECTED; - UsbMass->BlockIoMedia.ReadOnly = TRUE; + case USB_BOOT_SENSE_DATA_PROTECT: + Status = EFI_WRITE_PROTECTED; + Media->ReadOnly = TRUE; break; default: @@ -204,11 +126,11 @@ UsbBootRequestSense ( break; } - DEBUG ((mUsbMscInfo, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n", + DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n", Status, USB_BOOT_SENSE_KEY (SenseData.SenseKey), - SenseData.ASC, - SenseData.ASCQ + SenseData.Asc, + SenseData.Ascq )); return Status; @@ -216,9 +138,11 @@ UsbBootRequestSense ( /** - Execute the USB mass storage bootability commands. If execution - failed, retrieve the error by REQUEST_SENSE then update the device's - status, such as ReadyOnly. + Execute the USB mass storage bootability commands. + + This function executes the USB mass storage bootability commands. + If execution failed, retrieve the error by REQUEST_SENSE, then + update the device's status, such as ReadyOnly. @param UsbMass The device to issue commands to @param Cmd The command to execute @@ -228,14 +152,10 @@ UsbBootRequestSense ( @param DataLen The length of expected data @param Timeout The timeout used to transfer - @retval EFI_SUCCESS The command is excuted OK - @retval EFI_DEVICE_ERROR Failed to request sense - @retval EFI_INVALID_PARAMETER The command has some invalid parameters - @retval EFI_WRITE_PROTECTED The device is write protected - @retval EFI_MEDIA_CHANGED The device media has been changed + @retval EFI_SUCCESS Command is excuted successfully + @retval Others Command execution failed. **/ -STATIC EFI_STATUS UsbBootExecCmd ( IN USB_MASS_DEVICE *UsbMass, @@ -259,41 +179,49 @@ UsbBootExecCmd ( DataDir, Data, DataLen, + UsbMass->Lun, Timeout, &CmdResult ); // - // ExecCommand return success and get the right CmdResult means - // the commnad transfer is OK. + // If ExecCommand() returns no error and CmdResult is success, + // then the commnad transfer is successful. // - if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR(Status)) { + if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) { return EFI_SUCCESS; } + DEBUG ((EFI_D_INFO, "UsbBootExecCmd: Fail to Exec 0x%x Cmd /w %r\n", + *(UINT8 *)Cmd ,Status)); + + // + // If command execution failed, then retrieve error info via sense request. + // return UsbBootRequestSense (UsbMass); } /** - Execute the USB mass storage bootability commands. If execution - failed, retrieve the error by REQUEST_SENSE then update the device's - status, such as ReadyOnly. + Execute the USB mass storage bootability commands with retrial. + This function executes USB mass storage bootability commands. + If the device isn't ready, wait for it. If the device is ready + and error occurs, retry the command again until it exceeds the + limit of retrial times. + @param UsbMass The device to issue commands to @param Cmd The command to execute @param CmdLen The length of the command @param DataDir The direction of data transfer @param Data The buffer to hold the data @param DataLen The length of expected data + @param Timeout The timeout used to transfer - @retval EFI_SUCCESS The command is excuted OK - @retval EFI_DEVICE_ERROR Failed to request sense - @retval EFI_INVALID_PARAMETER The command has some invalid parameters - @retval EFI_WRITE_PROTECTED The device is write protected - @retval EFI_MEDIA_CHANGED The device media has been changed + @retval EFI_SUCCESS The command is executed successfully. + @retval EFI_MEDIA_CHANGED The device media has been changed. + @retval Others Command execution failed after retrial. **/ -STATIC EFI_STATUS UsbBootExecCmdWithRetry ( IN USB_MASS_DEVICE *UsbMass, @@ -306,18 +234,12 @@ UsbBootExecCmdWithRetry ( ) { EFI_STATUS Status; - INT16 Index; + UINTN Retry; + UINT8 Terminate; - // - // If the device isn't ready, wait some time. If the device is ready, - // retry the command again. - // Status = EFI_SUCCESS; - for (Index = 0; Index < USB_BOOT_COMMAND_RETRY; Index++) { - // - // Execute the command with an increasingly larger timeout value. - // + for (Retry = 0, Terminate = 0; Retry < USB_BOOT_COMMAND_RETRY; Retry++) { Status = UsbBootExecCmd ( UsbMass, Cmd, @@ -325,17 +247,17 @@ UsbBootExecCmdWithRetry ( DataDir, Data, DataLen, - Timeout * (Index + 1) + Timeout ); - if (Status == EFI_SUCCESS || - Status == EFI_MEDIA_CHANGED) { + if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED) { break; } // - // Need retry once more, so reset index + // If the device isn't ready, just wait for it without limit on retrial times. // - if (Status == EFI_NOT_READY) { - Index = 0; + if (Status == EFI_NOT_READY && Terminate < 3) { + Retry = 0; + Terminate++; } } @@ -343,14 +265,12 @@ UsbBootExecCmdWithRetry ( } - /** - Use the TEST UNIT READY command to check whether it is ready. - If it is ready, update the parameters. + Execute TEST UNIT READY command to check if the device is ready. @param UsbMass The device to test - @retval EFI_SUCCESS The device is ready and parameters are updated. + @retval EFI_SUCCESS The device is ready. @retval Others Device not ready. **/ @@ -364,7 +284,7 @@ UsbBootIsUnitReady ( ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD)); TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE; - TestCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); + TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); return UsbBootExecCmdWithRetry ( UsbMass, @@ -379,13 +299,13 @@ UsbBootIsUnitReady ( /** - Inquiry Command requests that information regrarding parameters of - the Device be sent to the Host. + Execute INQUIRY Command to request information regarding parameters of + the device be sent to the host computer. - @param UsbMass The device to inquiry. + @param UsbMass The device to inquire. - @retval EFI_SUCCESS The device is ready and parameters are updated. - @retval Others Device not ready. + @retval EFI_SUCCESS INQUIRY Command is executed successfully. + @retval Others INQUIRY Command is not executed successfully. **/ EFI_STATUS @@ -400,14 +320,11 @@ UsbBootInquiry ( Media = &(UsbMass->BlockIoMedia); - // - // Use the Inquiry command to get the RemovableMedia setting. - // ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD)); ZeroMem (&InquiryData, sizeof (USB_BOOT_INQUIRY_DATA)); InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE; - InquiryCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); + InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); InquiryCmd.AllocLen = sizeof (InquiryData); Status = UsbBootExecCmdWithRetry ( @@ -417,16 +334,20 @@ UsbBootInquiry ( EfiUsbDataIn, &InquiryData, sizeof (USB_BOOT_INQUIRY_DATA), - USB_BOOT_INQUIRY_CMD_TIMEOUT + USB_BOOT_GENERAL_CMD_TIMEOUT ); if (EFI_ERROR (Status)) { return Status; } - UsbMass->Pdt = USB_BOOT_PDT (InquiryData.Pdt); - Media->RemovableMedia = USB_BOOT_REMOVABLE (InquiryData.Removable); // - // Default value 512 Bytes, in case no media present at first time + // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit + // from the inquiry data. + // + UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (InquiryData.Pdt)); + Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (InquiryData.Removable)); + // + // Set block size to the default value of 512 Bytes, in case no media is present at first time. // Media->BlockSize = 0x0200; @@ -435,17 +356,19 @@ UsbBootInquiry ( /** - Get the capacity of the USB mass storage media, including - the presentation, block size, and last block number. This - function is used to get the disk parameters at the start if - it is a non-removable media or to detect the media if it is - removable. + Execute READ CAPACITY command to request information regarding + the capacity of the installed medium of the device. - @param UsbMass The device to retireve disk gemotric. + This function executes READ CAPACITY command to get the capacity + of the USB mass storage media, including the presence, block size, + and last block number. - @retval EFI_SUCCESS The disk gemotric is successfully retrieved. - @retval EFI_DEVICE_ERROR Something is inconsistent with the disk gemotric. + @param UsbMass The device to retireve disk gemotric. + @retval EFI_SUCCESS The disk geometry is successfully retrieved. + @retval EFI_NOT_READY The returned block size is zero. + @retval Other READ CAPACITY command execution failed. + **/ EFI_STATUS UsbBootReadCapacity ( @@ -456,17 +379,15 @@ UsbBootReadCapacity ( USB_BOOT_READ_CAPACITY_DATA CapacityData; EFI_BLOCK_IO_MEDIA *Media; EFI_STATUS Status; + UINT32 BlockSize; Media = &UsbMass->BlockIoMedia; - // - // Use the READ CAPACITY command to get the block length and last blockno - // ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD)); ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA)); CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE; - CapacityCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); + CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); Status = UsbBootExecCmdWithRetry ( UsbMass, @@ -481,86 +402,97 @@ UsbBootReadCapacity ( return Status; } + // + // Get the information on media presence, block size, and last block number + // from READ CAPACITY data. + // Media->MediaPresent = TRUE; - Media->LastBlock = UsbBootGetUint32 (CapacityData.LastLba); - Media->BlockSize = UsbBootGetUint32 (CapacityData.BlockLen); + Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba)); - DEBUG ((mUsbMscInfo, "UsbBootReadCapacity Success LBA=%d BlockSize=%d\n", - Media->LastBlock, Media->BlockSize)); + BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen)); + if (BlockSize == 0) { + // + // Get sense data + // + return UsbBootRequestSense (UsbMass); + } else { + Media->BlockSize = BlockSize; + } - return EFI_SUCCESS; -} + DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n", + Media->LastBlock, Media->BlockSize)); + return Status; +} /** - Retrieves mode sense information via sending Mode Sense - Packet Command. + Retrieves SCSI mode sense information via MODE SENSE(6) command. - @param UsbMass The USB_FLOPPY_DEV instance. + @param UsbMass The device whose sense data is requested. - @retval EFI_DEVICE_ERROR Hardware error - @retval EFI_SUCCESS Success + @retval EFI_SUCCESS SCSI mode sense information retrieved successfully. + @retval Other Command execution failed. **/ EFI_STATUS -UsbBootModeSense ( +UsbScsiModeSense ( IN USB_MASS_DEVICE *UsbMass ) { - EFI_STATUS Status; - USB_BOOT_MODE_SENSE_CMD ModeSenseCmd; - USB_BOOT_MODE_PARA_HEADER ModeParaHeader; - UINT8 CommandSet; + EFI_STATUS Status; + USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd; + USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader; + EFI_BLOCK_IO_MEDIA *Media; + + Media = &UsbMass->BlockIoMedia; - ZeroMem (&ModeSenseCmd, sizeof (USB_BOOT_MODE_SENSE_CMD)); - ZeroMem (&ModeParaHeader, sizeof (USB_BOOT_MODE_PARA_HEADER)); + ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD)); + ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER)); // - // overuse Context Pointer, the first field of Bot or Cbi is EFI_USB_INTERFACE_DESCRIPTOR + // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec // - CommandSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass; - - if (CommandSet == USB_MASS_STORE_SCSI) { - // - // Not UFI Command Set, no ModeSense Command - // - return EFI_SUCCESS; - } - - ModeSenseCmd.OpCode = USB_BOOT_MODE_SENSE10_OPCODE; - ModeSenseCmd.PageCode = 0x3f; - ModeSenseCmd.ParaListLenLsb = (UINT8) sizeof (USB_BOOT_MODE_PARA_HEADER); + ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE; + ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun); + ModeSenseCmd.PageCode = 0x3F; + ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER); Status = UsbBootExecCmdWithRetry ( UsbMass, &ModeSenseCmd, - sizeof (USB_BOOT_MODE_SENSE_CMD), + sizeof (USB_SCSI_MODE_SENSE6_CMD), EfiUsbDataIn, &ModeParaHeader, - sizeof (USB_BOOT_MODE_PARA_HEADER), + sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER), USB_BOOT_GENERAL_CMD_TIMEOUT ); + // - // Did nothing with the Header here - // But probably should + // Format of device-specific parameter byte of the mode parameter header is defined in + // Section 8.2.10 of SCSI-2 Spec. + // BIT7 of this byte is indicates whether the medium is write protected. // + if (!EFI_ERROR (Status)) { + Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0); + } return Status; } /** - Get the parameters for the USB mass storage media, including - the RemovableMedia, block size, and last block number. This - function is used both to initialize the media during the - DriverBindingStart and to re-initialize it when the media is + Get the parameters for the USB mass storage media. + + This function get the parameters for the USB mass storage media, + It is used both to initialize the media during the Start() phase + of Driver Binding Protocol and to re-initialize it when the media is changed. Althought the RemoveableMedia is unlikely to change, - I include it here. + it is also included here. - @param UsbMass The device to retireve disk gemotric. + @param UsbMass The device to retrieve disk gemotric. @retval EFI_SUCCESS The disk gemotric is successfully retrieved. - @retval EFI_DEVICE_ERROR Something is inconsistent with the disk gemotric. + @retval Other Failed to get the parameters. **/ EFI_STATUS @@ -570,36 +502,41 @@ UsbBootGetParams ( { EFI_BLOCK_IO_MEDIA *Media; EFI_STATUS Status; + UINT8 CmdSet; + + Media = &(UsbMass->BlockIoMedia); + CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass; Status = UsbBootInquiry (UsbMass); if (EFI_ERROR (Status)) { - DEBUG ((mUsbMscError, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status)); + DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status)); return Status; } - Media = &(UsbMass->BlockIoMedia); // - // Don't use the Removable bit in inquirydata to test whether the media + // Don't use the Removable bit in inquiry data to test whether the media // is removable because many flash disks wrongly set this bit. // if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) { // - // CD-Rom or Optical device + // CD-Rom device and Non-CD optical device // UsbMass->OpticalStorage = TRUE; // // Default value 2048 Bytes, in case no media present at first time // Media->BlockSize = 0x0800; - } else { + } + + if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) { // - // Non CD-Rom device need ModeSenseCmd between InquiryCmd and ReadCapacityCmd + // ModeSense is required for the device with PDT of 0x00/0x07/0x0E, + // which is from [MassStorageBootabilitySpec-Page7]. + // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI + // could get the information of WriteProtected. + // Since not all device support this command, so skip if fail. // - Status = UsbBootModeSense (UsbMass); - if (EFI_ERROR (Status)) { - DEBUG ((mUsbMscError, "UsbBootGetParams: UsbBootModeSense (%r)\n", Status)); - return Status; - } + UsbScsiModeSense (UsbMass); } return UsbBootReadCapacity (UsbMass); @@ -608,12 +545,11 @@ UsbBootGetParams ( /** Detect whether the removable media is present and whether it has changed. - The Non-removable media doesn't need it. - @param UsbMass The device to retireve disk gemotric. + @param UsbMass The device to check. - @retval EFI_SUCCESS The disk gemotric is successfully retrieved. - @retval EFI_DEVICE_ERROR Something is inconsistent with the disk gemotric. + @retval EFI_SUCCESS The media status is successfully checked. + @retval Other Failed to detect media. **/ EFI_STATUS @@ -623,64 +559,87 @@ UsbBootDetectMedia ( { EFI_BLOCK_IO_MEDIA OldMedia; EFI_BLOCK_IO_MEDIA *Media; + UINT8 CmdSet; + EFI_TPL OldTpl; EFI_STATUS Status; Media = &UsbMass->BlockIoMedia; - CopyMem ( - &OldMedia, - &(UsbMass->BlockIoMedia), - sizeof (EFI_BLOCK_IO_MEDIA) - ); + CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA)); + + CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass; - // - // First test whether the device is ready and get status - // If media changed or ready, need read the device's capacity - // Status = UsbBootIsUnitReady (UsbMass); - if ((Status == EFI_SUCCESS && Media->MediaPresent) || - (Status == EFI_MEDIA_CHANGED)) { - if ((UsbMass->Pdt != USB_PDT_CDROM) && - (UsbMass->Pdt != USB_PDT_OPTICAL)) { - // - // Non CD-Rom device need ModeSenseCmd between InquiryCmd and ReadCapacityCmd - // - UsbBootModeSense (UsbMass); - } - DEBUG ((mUsbMscInfo, "UsbBootDetectMedia: Need Read Capacity\n")); - Status = UsbBootReadCapacity (UsbMass); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) { + // + // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E, + // according to Section 4 of USB Mass Storage Specification for Bootability. + // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI + // could get the information of Write Protected. + // Since not all device support this command, skip if fail. + // + UsbScsiModeSense (UsbMass); } + + Status = UsbBootReadCapacity (UsbMass); if (EFI_ERROR (Status)) { - return Status; + DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status)); + goto ON_ERROR; } + return EFI_SUCCESS; + +ON_ERROR: + // + // Detect whether it is necessary to reinstall the Block I/O Protocol. // - // Detect whether it is necessary to reinstall the BlockIO + // MediaId may change in RequestSense for MediaChanged + // MediaPresent may change in RequestSense for NoMedia + // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged + // MediaPresent/BlockSize/LastBlock may change in ReadCapacity // if ((Media->MediaId != OldMedia.MediaId) || (Media->MediaPresent != OldMedia.MediaPresent) || (Media->ReadOnly != OldMedia.ReadOnly) || (Media->BlockSize != OldMedia.BlockSize) || (Media->LastBlock != OldMedia.LastBlock)) { - DEBUG ((mUsbMscInfo, "UsbBootDetectMedia: Need reinstall BlockIoProtocol\n")); - Media->MediaId++; + + // + // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY. + // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface(). + // + OldTpl = EfiGetCurrentTpl (); + gBS->RestoreTPL (TPL_CALLBACK); + gBS->ReinstallProtocolInterface ( UsbMass->Controller, &gEfiBlockIoProtocolGuid, &UsbMass->BlockIo, &UsbMass->BlockIo ); + + ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK); + gBS->RaiseTPL (OldTpl); + // - // Check whether media present or media changed or write protected + // Update MediaId after reinstalling Block I/O Protocol. // - if (Media->MediaPresent == FALSE) { - Status = EFI_NO_MEDIA; - } - if (Media->MediaId != OldMedia.MediaId) { - Status = EFI_MEDIA_CHANGED; + if (Media->MediaPresent != OldMedia.MediaPresent) { + if (Media->MediaPresent) { + Media->MediaId = 1; + } else { + Media->MediaId = 0; + } } - if (Media->ReadOnly != OldMedia.ReadOnly) { - Status = EFI_WRITE_PROTECTED; + + if ((Media->ReadOnly != OldMedia.ReadOnly) || + (Media->BlockSize != OldMedia.BlockSize) || + (Media->LastBlock != OldMedia.LastBlock)) { + Media->MediaId++; } } @@ -728,13 +687,9 @@ UsbBootReadBlocks ( ByteSize = (UINT32)Count * BlockSize; // - // Optical device need longer timeout than other device + // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1] // - if (UsbMass->OpticalStorage == TRUE) { - Timeout = (UINT32)Count * USB_BOOT_OPTICAL_BLOCK_TIMEOUT; - } else { - Timeout = (UINT32)Count * USB_BOOT_GENERAL_BLOCK_TIMEOUT; - } + Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT; // // Fill in the command then execute @@ -742,9 +697,9 @@ UsbBootReadBlocks ( ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD)); ReadCmd.OpCode = USB_BOOT_READ10_OPCODE; - ReadCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); - UsbBootPutUint32 (ReadCmd.Lba, Lba); - UsbBootPutUint16 (ReadCmd.TransferLen, Count); + ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); + WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba)); + WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count)); Status = UsbBootExecCmdWithRetry ( UsbMass, @@ -774,7 +729,7 @@ UsbBootReadBlocks ( @param UsbMass The USB mass storage device to write to @param Lba The start block number @param TotalBlock Total block number to write - @param Buffer The buffer to write to + @param Buffer Pointer to the source buffer for the data. @retval EFI_SUCCESS Data are written into the buffer @retval Others Failed to write all the data @@ -785,7 +740,7 @@ UsbBootWriteBlocks ( IN USB_MASS_DEVICE *UsbMass, IN UINT32 Lba, IN UINTN TotalBlock, - OUT UINT8 *Buffer + IN UINT8 *Buffer ) { USB_BOOT_WRITE10_CMD WriteCmd; @@ -808,13 +763,9 @@ UsbBootWriteBlocks ( ByteSize = (UINT32)Count * BlockSize; // - // Optical device need longer timeout than other device + // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1] // - if (UsbMass->OpticalStorage == TRUE) { - Timeout = (UINT32)Count * USB_BOOT_OPTICAL_BLOCK_TIMEOUT; - } else { - Timeout = (UINT32)Count * USB_BOOT_GENERAL_BLOCK_TIMEOUT; - } + Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT; // // Fill in the write10 command block @@ -822,9 +773,9 @@ UsbBootWriteBlocks ( ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD)); WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE; - WriteCmd.Lun = USB_BOOT_LUN (UsbMass->Lun); - UsbBootPutUint32 (WriteCmd.Lba, Lba); - UsbBootPutUint16 (WriteCmd.TransferLen, Count); + WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); + WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba)); + WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count)); Status = UsbBootExecCmdWithRetry ( UsbMass, @@ -847,16 +798,14 @@ UsbBootWriteBlocks ( return Status; } - /** - Use the USB clear feature control transfer to clear the endpoint - stall condition. + Use the USB clear feature control transfer to clear the endpoint stall condition. - @param UsbIo The USB IO protocol to use + @param UsbIo The USB I/O Protocol instance @param EndpointAddr The endpoint to clear stall for - @retval EFI_SUCCESS The endpoint stall condtion is clear - @retval Others Failed to clear the endpoint stall condtion + @retval EFI_SUCCESS The endpoint stall condition is cleared. + @retval Others Failed to clear the endpoint stall condition. **/ EFI_STATUS @@ -875,7 +824,7 @@ UsbClearEndpointStall ( Request.Value = USB_FEATURE_ENDPOINT_HALT; Request.Index = EndpointAddr; Request.Length = 0; - Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_STALL_1_MS; + Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND; Status = UsbIo->UsbControlTransfer ( UsbIo,