2007-07-11 10:47:37 +02:00
|
|
|
/** @file
|
2009-01-12 04:11:00 +01:00
|
|
|
Implementation of the command set of USB Mass Storage Specification
|
|
|
|
for Bootability, Revision 1.0.
|
2008-07-23 10:30:25 +02:00
|
|
|
|
2014-04-10 04:45:32 +02:00
|
|
|
Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
|
2010-04-24 11:49:11 +02:00
|
|
|
This program and the accompanying materials
|
2007-07-11 10:47:37 +02:00
|
|
|
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
|
|
|
|
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.
|
|
|
|
|
2008-07-23 10:30:25 +02:00
|
|
|
**/
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2011-06-13 07:27:23 +02:00
|
|
|
#include "UsbMass.h"
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Execute REQUEST SENSE Command to retrieve sense data from device.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbMass The device whose sense data is requested.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The command is excuted successfully.
|
2008-07-23 10:30:25 +02:00
|
|
|
@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.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootRequestSense (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_REQUEST_SENSE_CMD SenseCmd;
|
|
|
|
USB_BOOT_REQUEST_SENSE_DATA SenseData;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
USB_MASS_TRANSPORT *Transport;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 CmdResult;
|
|
|
|
|
|
|
|
Transport = UsbMass->Transport;
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Request the sense data from the device
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
ZeroMem (&SenseCmd, sizeof (USB_BOOT_REQUEST_SENSE_CMD));
|
|
|
|
ZeroMem (&SenseData, sizeof (USB_BOOT_REQUEST_SENSE_DATA));
|
|
|
|
|
|
|
|
SenseCmd.OpCode = USB_BOOT_REQUEST_SENSE_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2010-09-15 09:48:11 +02:00
|
|
|
SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = Transport->ExecCommand (
|
|
|
|
UsbMass->Context,
|
|
|
|
&SenseCmd,
|
|
|
|
sizeof (USB_BOOT_REQUEST_SENSE_CMD),
|
|
|
|
EfiUsbDataIn,
|
|
|
|
&SenseData,
|
|
|
|
sizeof (USB_BOOT_REQUEST_SENSE_DATA),
|
2008-06-27 10:09:00 +02:00
|
|
|
UsbMass->Lun,
|
2007-07-11 10:47:37 +02:00
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT,
|
|
|
|
&CmdResult
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) {
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult));
|
2009-07-23 03:30:16 +02:00
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Status = EFI_DEVICE_ERROR;
|
|
|
|
}
|
2007-07-24 11:54:50 +02:00
|
|
|
return Status;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// If sense data is retrieved successfully, interpret the sense data
|
|
|
|
// and update the media status if necessary.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Media = &UsbMass->BlockIoMedia;
|
|
|
|
|
|
|
|
switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) {
|
|
|
|
|
|
|
|
case USB_BOOT_SENSE_NO_SENSE:
|
2007-07-24 11:54:50 +02:00
|
|
|
Status = EFI_NO_RESPONSE;
|
|
|
|
break;
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
case USB_BOOT_SENSE_RECOVERED:
|
|
|
|
//
|
|
|
|
// Suppose hardware can handle this case, and recover later by itself
|
|
|
|
//
|
|
|
|
Status = EFI_NOT_READY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_BOOT_SENSE_NOT_READY:
|
2007-07-24 11:54:50 +02:00
|
|
|
Status = EFI_DEVICE_ERROR;
|
2010-01-19 07:42:21 +01:00
|
|
|
if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {
|
2009-01-12 04:11:00 +01:00
|
|
|
Media->MediaPresent = FALSE;
|
|
|
|
Status = EFI_NO_MEDIA;
|
2010-01-19 07:42:21 +01:00
|
|
|
} else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {
|
2009-01-12 04:11:00 +01:00
|
|
|
Status = EFI_NOT_READY;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_BOOT_SENSE_ILLEGAL_REQUEST:
|
|
|
|
Status = EFI_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_BOOT_SENSE_UNIT_ATTENTION:
|
|
|
|
Status = EFI_DEVICE_ERROR;
|
2010-01-19 07:42:21 +01:00
|
|
|
if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) {
|
2007-07-24 11:54:50 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// If MediaChange, reset ReadOnly and new MediaId
|
2007-07-24 11:54:50 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
Status = EFI_MEDIA_CHANGED;
|
2007-07-24 11:54:50 +02:00
|
|
|
Media->ReadOnly = FALSE;
|
|
|
|
Media->MediaId++;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
case USB_BOOT_SENSE_DATA_PROTECT:
|
|
|
|
Status = EFI_WRITE_PROTECTED;
|
2007-07-24 11:54:50 +02:00
|
|
|
Media->ReadOnly = TRUE;
|
2007-07-11 10:47:37 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Status = EFI_DEVICE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",
|
2007-07-11 10:47:37 +02:00
|
|
|
Status,
|
|
|
|
USB_BOOT_SENSE_KEY (SenseData.SenseKey),
|
2010-01-19 07:42:21 +01:00
|
|
|
SenseData.Asc,
|
|
|
|
SenseData.Ascq
|
2007-07-11 10:47:37 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
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.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS Command is excuted successfully
|
|
|
|
@retval Others Command execution failed.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootExecCmd (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN VOID *Cmd,
|
|
|
|
IN UINT8 CmdLen,
|
|
|
|
IN EFI_USB_DATA_DIRECTION DataDir,
|
|
|
|
IN VOID *Data,
|
|
|
|
IN UINT32 DataLen,
|
|
|
|
IN UINT32 Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_MASS_TRANSPORT *Transport;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 CmdResult;
|
|
|
|
|
|
|
|
Transport = UsbMass->Transport;
|
|
|
|
Status = Transport->ExecCommand (
|
|
|
|
UsbMass->Context,
|
|
|
|
Cmd,
|
|
|
|
CmdLen,
|
|
|
|
DataDir,
|
|
|
|
Data,
|
|
|
|
DataLen,
|
2008-06-27 10:09:00 +02:00
|
|
|
UsbMass->Lun,
|
2007-07-11 10:47:37 +02:00
|
|
|
Timeout,
|
|
|
|
&CmdResult
|
|
|
|
);
|
2011-09-29 11:14:30 +02:00
|
|
|
|
|
|
|
if (Status == EFI_TIMEOUT) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8 *)Cmd));
|
|
|
|
return EFI_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// If ExecCommand() returns no error and CmdResult is success,
|
|
|
|
// then the commnad transfer is successful.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {
|
2007-07-11 10:47:37 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// If command execution failed, then retrieve error info via sense request.
|
|
|
|
//
|
2007-07-11 10:47:37 +02:00
|
|
|
return UsbBootRequestSense (UsbMass);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Execute the USB mass storage bootability commands with retrial.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
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.
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
@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
|
2008-07-23 10:30:25 +02:00
|
|
|
@param Timeout The timeout used to transfer
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@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.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootExecCmdWithRetry (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN VOID *Cmd,
|
|
|
|
IN UINT8 CmdLen,
|
|
|
|
IN EFI_USB_DATA_DIRECTION DataDir,
|
|
|
|
IN VOID *Data,
|
|
|
|
IN UINT32 DataLen,
|
|
|
|
IN UINT32 Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2009-01-12 04:11:00 +01:00
|
|
|
UINTN Retry;
|
2011-09-29 11:14:30 +02:00
|
|
|
EFI_EVENT TimeoutEvt;
|
|
|
|
|
|
|
|
Retry = 0;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
Status = gBS->CreateEvent (
|
|
|
|
EVT_TIMER,
|
|
|
|
TPL_CALLBACK,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&TimeoutEvt
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2011-09-29 11:14:30 +02:00
|
|
|
Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(60));
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
goto EXIT;
|
|
|
|
}
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2011-09-29 11:14:30 +02:00
|
|
|
//
|
|
|
|
// Execute the cmd and retry if it fails.
|
|
|
|
//
|
|
|
|
while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {
|
2007-07-11 10:47:37 +02:00
|
|
|
Status = UsbBootExecCmd (
|
|
|
|
UsbMass,
|
|
|
|
Cmd,
|
|
|
|
CmdLen,
|
|
|
|
DataDir,
|
|
|
|
Data,
|
|
|
|
DataLen,
|
2008-02-13 10:08:24 +01:00
|
|
|
Timeout
|
2007-07-11 10:47:37 +02:00
|
|
|
);
|
2011-09-29 11:14:30 +02:00
|
|
|
if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {
|
2007-07-11 10:47:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//
|
2011-09-29 11:14:30 +02:00
|
|
|
// If the sense data shows the drive is not ready, we need execute the cmd again.
|
|
|
|
// We limit the upper boundary to 60 seconds.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2011-09-29 11:14:30 +02:00
|
|
|
if (Status == EFI_NOT_READY) {
|
|
|
|
continue;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
2011-09-29 11:14:30 +02:00
|
|
|
//
|
|
|
|
// If the status is other error, then just retry 5 times.
|
|
|
|
//
|
|
|
|
if (Retry++ >= USB_BOOT_COMMAND_RETRY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXIT:
|
|
|
|
if (TimeoutEvt != NULL) {
|
|
|
|
gBS->CloseEvent (TimeoutEvt);
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Execute TEST UNIT READY command to check if the device is ready.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@param UsbMass The device to test
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The device is ready.
|
2007-07-11 10:47:37 +02:00
|
|
|
@retval Others Device not ready.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootIsUnitReady (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_TEST_UNIT_READY_CMD TestCmd;
|
|
|
|
|
|
|
|
ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD));
|
|
|
|
|
|
|
|
TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
return UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&TestCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_BOOT_TEST_UNIT_READY_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbNoData,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Execute INQUIRY Command to request information regarding parameters of
|
|
|
|
the device be sent to the host computer.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbMass The device to inquire.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS INQUIRY Command is executed successfully.
|
|
|
|
@retval Others INQUIRY Command is not executed successfully.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootInquiry (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_INQUIRY_CMD InquiryCmd;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
|
|
|
|
Media = &(UsbMass->BlockIoMedia);
|
|
|
|
|
|
|
|
ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));
|
2011-06-13 07:27:23 +02:00
|
|
|
ZeroMem (&UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2011-06-13 07:27:23 +02:00
|
|
|
InquiryCmd.AllocLen = (UINT8) sizeof (USB_BOOT_INQUIRY_DATA);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&InquiryCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_BOOT_INQUIRY_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbDataIn,
|
2011-06-13 07:27:23 +02:00
|
|
|
&UsbMass->InquiryData,
|
2007-07-11 10:47:37 +02:00
|
|
|
sizeof (USB_BOOT_INQUIRY_DATA),
|
2007-07-24 11:54:50 +02:00
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT
|
2007-07-11 10:47:37 +02:00
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
|
|
|
|
// from the inquiry data.
|
|
|
|
//
|
2011-06-13 07:27:23 +02:00
|
|
|
UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (UsbMass->InquiryData.Pdt));
|
|
|
|
Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (UsbMass->InquiryData.Removable));
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Set block size to the default value of 512 Bytes, in case no media is present at first time.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
Media->BlockSize = 0x0200;
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2012-05-14 09:50:07 +02:00
|
|
|
/**
|
|
|
|
Execute READ CAPACITY 16 bytes command to request information regarding
|
|
|
|
the capacity of the installed medium of the device.
|
|
|
|
|
|
|
|
This function executes READ CAPACITY 16 bytes command to get the capacity
|
|
|
|
of the USB mass storage media, including the presence, block size,
|
|
|
|
and last block number.
|
|
|
|
|
|
|
|
@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 16 bytes command execution failed.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootReadCapacity16 (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT8 CapacityCmd[16];
|
|
|
|
EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 BlockSize;
|
|
|
|
|
|
|
|
Media = &UsbMass->BlockIoMedia;
|
|
|
|
|
|
|
|
Media->MediaPresent = FALSE;
|
|
|
|
Media->LastBlock = 0;
|
|
|
|
Media->BlockSize = 0;
|
|
|
|
|
|
|
|
ZeroMem (CapacityCmd, sizeof (CapacityCmd));
|
|
|
|
ZeroMem (&CapacityData, sizeof (CapacityData));
|
|
|
|
|
|
|
|
CapacityCmd[0] = EFI_SCSI_OP_READ_CAPACITY16;
|
|
|
|
CapacityCmd[1] = 0x10;
|
|
|
|
//
|
|
|
|
// Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
|
|
|
|
//
|
|
|
|
ZeroMem ((CapacityCmd + 2), 8);
|
|
|
|
|
|
|
|
CapacityCmd[13] = sizeof (CapacityData);
|
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
CapacityCmd,
|
|
|
|
(UINT8) sizeof (CapacityCmd),
|
|
|
|
EfiUsbDataIn,
|
|
|
|
&CapacityData,
|
|
|
|
sizeof (CapacityData),
|
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the information on media presence, block size, and last block number
|
|
|
|
// from READ CAPACITY data.
|
|
|
|
//
|
|
|
|
Media->MediaPresent = TRUE;
|
|
|
|
Media->LastBlock = SwapBytes64 (ReadUnaligned64 ((CONST UINT64 *) &(CapacityData.LastLba7)));
|
|
|
|
|
|
|
|
BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) &(CapacityData.BlockSize3)));
|
|
|
|
|
|
|
|
Media->LowestAlignedLba = (CapacityData.LowestAlignLogic2 << 8) |
|
|
|
|
CapacityData.LowestAlignLogic1;
|
|
|
|
Media->LogicalBlocksPerPhysicalBlock = (1 << CapacityData.LogicPerPhysical);
|
|
|
|
if (BlockSize == 0) {
|
|
|
|
//
|
|
|
|
// Get sense data
|
|
|
|
//
|
|
|
|
return UsbBootRequestSense (UsbMass);
|
|
|
|
} else {
|
|
|
|
Media->BlockSize = BlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Execute READ CAPACITY command to request information regarding
|
|
|
|
the capacity of the installed medium of the device.
|
|
|
|
|
|
|
|
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.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@param UsbMass The device to retireve disk gemotric.
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@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.
|
2008-07-23 10:30:25 +02:00
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootReadCapacity (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_READ_CAPACITY_CMD CapacityCmd;
|
|
|
|
USB_BOOT_READ_CAPACITY_DATA CapacityData;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
EFI_STATUS Status;
|
2009-07-23 03:30:16 +02:00
|
|
|
UINT32 BlockSize;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Media = &UsbMass->BlockIoMedia;
|
|
|
|
|
|
|
|
ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));
|
|
|
|
ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));
|
|
|
|
|
|
|
|
CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&CapacityCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbDataIn,
|
|
|
|
&CapacityData,
|
|
|
|
sizeof (USB_BOOT_READ_CAPACITY_DATA),
|
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// Get the information on media presence, block size, and last block number
|
|
|
|
// from READ CAPACITY data.
|
|
|
|
//
|
2007-07-11 10:47:37 +02:00
|
|
|
Media->MediaPresent = TRUE;
|
2009-01-12 04:11:00 +01:00
|
|
|
Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-07-23 03:30:16 +02:00
|
|
|
BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));
|
|
|
|
if (BlockSize == 0) {
|
2009-12-16 01:58:46 +01:00
|
|
|
//
|
|
|
|
// Get sense data
|
|
|
|
//
|
|
|
|
return UsbBootRequestSense (UsbMass);
|
2009-07-23 03:30:16 +02:00
|
|
|
} else {
|
|
|
|
Media->BlockSize = BlockSize;
|
2007-07-24 11:54:50 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 09:50:07 +02:00
|
|
|
if (Media->LastBlock == 0xFFFFFFFF) {
|
|
|
|
Status = UsbBootReadCapacity16 (UsbMass);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
UsbMass->Cdb16Byte = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-16 01:58:46 +01:00
|
|
|
return Status;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Retrieves SCSI mode sense information via MODE SENSE(6) command.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbMass The device whose sense data is requested.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
|
|
|
|
@retval Other Command execution failed.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
2007-07-24 11:54:50 +02:00
|
|
|
UsbScsiModeSense (
|
2007-07-11 10:47:37 +02:00
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
2008-01-02 03:34:20 +01:00
|
|
|
EFI_STATUS Status;
|
2007-07-24 11:54:50 +02:00
|
|
|
USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;
|
|
|
|
USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
|
2008-01-02 03:34:20 +01:00
|
|
|
Media = &UsbMass->BlockIoMedia;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2007-07-24 11:54:50 +02:00
|
|
|
ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));
|
|
|
|
ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;
|
2007-08-23 04:19:41 +02:00
|
|
|
ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);
|
2007-07-24 11:54:50 +02:00
|
|
|
ModeSenseCmd.PageCode = 0x3F;
|
|
|
|
ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&ModeSenseCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbDataIn,
|
|
|
|
&ModeParaHeader,
|
2007-07-24 11:54:50 +02:00
|
|
|
sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),
|
2007-07-11 10:47:37 +02:00
|
|
|
USB_BOOT_GENERAL_CMD_TIMEOUT
|
|
|
|
);
|
2007-07-24 11:54:50 +02:00
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// 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.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
if (!EFI_ERROR (Status)) {
|
2009-01-12 04:11:00 +01:00
|
|
|
Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);
|
2007-07-24 11:54:50 +02:00
|
|
|
}
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
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
|
2007-07-11 10:47:37 +02:00
|
|
|
changed. Althought the RemoveableMedia is unlikely to change,
|
2009-01-12 04:11:00 +01:00
|
|
|
it is also included here.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbMass The device to retrieve disk gemotric.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The disk gemotric is successfully retrieved.
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval Other Failed to get the parameters.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootGetParams (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
|
|
|
EFI_STATUS Status;
|
2007-07-24 11:54:50 +02:00
|
|
|
|
2008-01-02 03:34:20 +01:00
|
|
|
Media = &(UsbMass->BlockIoMedia);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootInquiry (UsbMass);
|
|
|
|
if (EFI_ERROR (Status)) {
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));
|
2007-07-11 10:47:37 +02:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:45:32 +02:00
|
|
|
//
|
|
|
|
// According to USB Mass Storage Specification for Bootability, only following
|
|
|
|
// 4 Peripheral Device Types are in spec.
|
|
|
|
//
|
|
|
|
if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) &&
|
|
|
|
(UsbMass->Pdt != USB_PDT_CDROM) &&
|
|
|
|
(UsbMass->Pdt != USB_PDT_OPTICAL) &&
|
|
|
|
(UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {
|
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Don't use the Removable bit in inquiry data to test whether the media
|
2007-07-11 10:47:37 +02:00
|
|
|
// is removable because many flash disks wrongly set this bit.
|
|
|
|
//
|
|
|
|
if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {
|
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
// CD-Rom device and Non-CD optical device
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
|
|
|
UsbMass->OpticalStorage = TRUE;
|
|
|
|
//
|
|
|
|
// Default value 2048 Bytes, in case no media present at first time
|
|
|
|
//
|
|
|
|
Media->BlockSize = 0x0800;
|
2007-07-24 11:54:50 +02:00
|
|
|
}
|
|
|
|
|
2011-09-29 11:14:30 +02:00
|
|
|
Status = UsbBootDetectMedia (UsbMass);
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2011-09-29 11:14:30 +02:00
|
|
|
return Status;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Detect whether the removable media is present and whether it has changed.
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbMass The device to check.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The media status is successfully checked.
|
|
|
|
@retval Other Failed to detect media.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootDetectMedia (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_BLOCK_IO_MEDIA OldMedia;
|
|
|
|
EFI_BLOCK_IO_MEDIA *Media;
|
2007-07-24 11:54:50 +02:00
|
|
|
UINT8 CmdSet;
|
|
|
|
EFI_TPL OldTpl;
|
2007-07-11 10:47:37 +02:00
|
|
|
EFI_STATUS Status;
|
|
|
|
|
|
|
|
Media = &UsbMass->BlockIoMedia;
|
2007-07-12 04:14:05 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2007-07-24 11:54:50 +02:00
|
|
|
CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
Status = UsbBootIsUnitReady (UsbMass);
|
2011-09-29 11:14:30 +02:00
|
|
|
if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {
|
2007-07-24 11:54:50 +02:00
|
|
|
goto ON_ERROR;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
2007-07-24 11:54:50 +02:00
|
|
|
|
|
|
|
if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
|
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// 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.
|
2007-07-24 11:54:50 +02:00
|
|
|
//
|
|
|
|
UsbScsiModeSense (UsbMass);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = UsbBootReadCapacity (UsbMass);
|
2007-07-11 10:47:37 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2008-02-13 10:08:24 +01:00
|
|
|
DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));
|
2007-07-24 11:54:50 +02:00
|
|
|
goto ON_ERROR;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
|
2007-07-24 11:54:50 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
|
|
|
|
ON_ERROR:
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Detect whether it is necessary to reinstall the Block I/O Protocol.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
// 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
|
|
|
|
//
|
2007-07-11 10:47:37 +02:00
|
|
|
if ((Media->MediaId != OldMedia.MediaId) ||
|
|
|
|
(Media->MediaPresent != OldMedia.MediaPresent) ||
|
|
|
|
(Media->ReadOnly != OldMedia.ReadOnly) ||
|
|
|
|
(Media->BlockSize != OldMedia.BlockSize) ||
|
|
|
|
(Media->LastBlock != OldMedia.LastBlock)) {
|
2007-07-24 11:54:50 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
//
|
|
|
|
// 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 ();
|
2007-07-24 11:54:50 +02:00
|
|
|
gBS->RestoreTPL (TPL_CALLBACK);
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
gBS->ReinstallProtocolInterface (
|
|
|
|
UsbMass->Controller,
|
|
|
|
&gEfiBlockIoProtocolGuid,
|
|
|
|
&UsbMass->BlockIo,
|
|
|
|
&UsbMass->BlockIo
|
|
|
|
);
|
2007-07-24 11:54:50 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
|
2007-07-24 11:54:50 +02:00
|
|
|
gBS->RaiseTPL (OldTpl);
|
|
|
|
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2009-01-12 04:11:00 +01:00
|
|
|
// Update MediaId after reinstalling Block I/O Protocol.
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
if (Media->MediaPresent != OldMedia.MediaPresent) {
|
2009-01-12 04:11:00 +01:00
|
|
|
if (Media->MediaPresent) {
|
2007-07-24 11:54:50 +02:00
|
|
|
Media->MediaId = 1;
|
|
|
|
} else {
|
|
|
|
Media->MediaId = 0;
|
|
|
|
}
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
2007-07-24 11:54:50 +02:00
|
|
|
|
|
|
|
if ((Media->ReadOnly != OldMedia.ReadOnly) ||
|
|
|
|
(Media->BlockSize != OldMedia.BlockSize) ||
|
|
|
|
(Media->LastBlock != OldMedia.LastBlock)) {
|
|
|
|
Media->MediaId++;
|
2007-07-11 10:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read some blocks from the device.
|
|
|
|
|
|
|
|
@param UsbMass The USB mass storage device to read from
|
|
|
|
@param Lba The start block number
|
|
|
|
@param TotalBlock Total block number to read
|
|
|
|
@param Buffer The buffer to read to
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Data are read into the buffer
|
|
|
|
@retval Others Failed to read all the data
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootReadBlocks (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN UINT32 Lba,
|
|
|
|
IN UINTN TotalBlock,
|
|
|
|
OUT UINT8 *Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_READ10_CMD ReadCmd;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 Count;
|
|
|
|
UINT32 BlockSize;
|
|
|
|
UINT32 ByteSize;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
BlockSize = UsbMass->BlockIoMedia.BlockSize;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
|
|
|
|
while (TotalBlock > 0) {
|
|
|
|
//
|
|
|
|
// Split the total blocks into smaller pieces to ease the pressure
|
|
|
|
// on the device. We must split the total block because the READ10
|
|
|
|
// command only has 16 bit transfer length (in the unit of block).
|
|
|
|
//
|
|
|
|
Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
|
|
|
|
ByteSize = (UINT32)Count * BlockSize;
|
|
|
|
|
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
// USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in the command then execute
|
|
|
|
//
|
|
|
|
ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));
|
|
|
|
|
|
|
|
ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2009-01-12 04:11:00 +01:00
|
|
|
WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));
|
|
|
|
WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&ReadCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_BOOT_READ10_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbDataIn,
|
|
|
|
Buffer,
|
|
|
|
ByteSize,
|
|
|
|
Timeout
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
2012-05-14 09:50:07 +02:00
|
|
|
DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
|
2007-07-11 10:47:37 +02:00
|
|
|
Lba += Count;
|
|
|
|
Buffer += Count * BlockSize;
|
|
|
|
TotalBlock -= Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write some blocks to the device.
|
|
|
|
|
|
|
|
@param UsbMass The USB mass storage device to write to
|
|
|
|
@param Lba The start block number
|
|
|
|
@param TotalBlock Total block number to write
|
2009-01-12 04:11:00 +01:00
|
|
|
@param Buffer Pointer to the source buffer for the data.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS Data are written into the buffer
|
|
|
|
@retval Others Failed to write all the data
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootWriteBlocks (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN UINT32 Lba,
|
|
|
|
IN UINTN TotalBlock,
|
2009-01-12 04:11:00 +01:00
|
|
|
IN UINT8 *Buffer
|
2007-07-11 10:47:37 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
USB_BOOT_WRITE10_CMD WriteCmd;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 Count;
|
|
|
|
UINT32 BlockSize;
|
|
|
|
UINT32 ByteSize;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
BlockSize = UsbMass->BlockIoMedia.BlockSize;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
|
|
|
|
while (TotalBlock > 0) {
|
|
|
|
//
|
|
|
|
// Split the total blocks into smaller pieces to ease the pressure
|
|
|
|
// on the device. We must split the total block because the WRITE10
|
|
|
|
// command only has 16 bit transfer length (in the unit of block).
|
|
|
|
//
|
|
|
|
Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
|
|
|
|
ByteSize = (UINT32)Count * BlockSize;
|
|
|
|
|
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
// USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
|
2007-07-11 10:47:37 +02:00
|
|
|
//
|
2007-07-24 11:54:50 +02:00
|
|
|
Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in the write10 command block
|
|
|
|
//
|
|
|
|
ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
|
|
|
|
|
|
|
|
WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
|
2007-07-17 03:48:09 +02:00
|
|
|
WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
|
2009-01-12 04:11:00 +01:00
|
|
|
WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
|
|
|
|
WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
&WriteCmd,
|
2010-09-15 09:48:11 +02:00
|
|
|
(UINT8) sizeof (USB_BOOT_WRITE10_CMD),
|
2007-07-11 10:47:37 +02:00
|
|
|
EfiUsbDataOut,
|
|
|
|
Buffer,
|
|
|
|
ByteSize,
|
|
|
|
Timeout
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
2012-05-14 09:50:07 +02:00
|
|
|
DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
|
|
|
|
|
|
|
|
Lba += Count;
|
|
|
|
Buffer += Count * BlockSize;
|
|
|
|
TotalBlock -= Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Read some blocks from the device by SCSI 16 byte cmd.
|
|
|
|
|
|
|
|
@param UsbMass The USB mass storage device to read from
|
|
|
|
@param Lba The start block number
|
|
|
|
@param TotalBlock Total block number to read
|
|
|
|
@param Buffer The buffer to read to
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS Data are read into the buffer
|
|
|
|
@retval Others Failed to read all the data
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootReadBlocks16 (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN UINT64 Lba,
|
|
|
|
IN UINTN TotalBlock,
|
|
|
|
OUT UINT8 *Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT8 ReadCmd[16];
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 Count;
|
|
|
|
UINT32 BlockSize;
|
|
|
|
UINT32 ByteSize;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
BlockSize = UsbMass->BlockIoMedia.BlockSize;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
|
|
|
|
while (TotalBlock > 0) {
|
|
|
|
//
|
|
|
|
// Split the total blocks into smaller pieces.
|
|
|
|
//
|
|
|
|
Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
|
|
|
|
ByteSize = (UINT32)Count * BlockSize;
|
|
|
|
|
|
|
|
//
|
|
|
|
// USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
|
|
|
|
//
|
|
|
|
Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in the command then execute
|
|
|
|
//
|
|
|
|
ZeroMem (ReadCmd, sizeof (ReadCmd));
|
|
|
|
|
|
|
|
ReadCmd[0] = EFI_SCSI_OP_READ16;
|
|
|
|
ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
|
|
|
|
WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba));
|
|
|
|
WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count));
|
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
ReadCmd,
|
|
|
|
(UINT8) sizeof (ReadCmd),
|
|
|
|
EfiUsbDataIn,
|
|
|
|
Buffer,
|
|
|
|
ByteSize,
|
|
|
|
Timeout
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
|
|
|
|
Lba += Count;
|
|
|
|
Buffer += Count * BlockSize;
|
|
|
|
TotalBlock -= Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write some blocks to the device by SCSI 16 byte cmd.
|
|
|
|
|
|
|
|
@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 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
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbBootWriteBlocks16 (
|
|
|
|
IN USB_MASS_DEVICE *UsbMass,
|
|
|
|
IN UINT64 Lba,
|
|
|
|
IN UINTN TotalBlock,
|
|
|
|
IN UINT8 *Buffer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT8 WriteCmd[16];
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 Count;
|
|
|
|
UINT32 BlockSize;
|
|
|
|
UINT32 ByteSize;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
BlockSize = UsbMass->BlockIoMedia.BlockSize;
|
|
|
|
Status = EFI_SUCCESS;
|
|
|
|
|
|
|
|
while (TotalBlock > 0) {
|
|
|
|
//
|
|
|
|
// Split the total blocks into smaller pieces.
|
|
|
|
//
|
|
|
|
Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
|
|
|
|
ByteSize = (UINT32)Count * BlockSize;
|
|
|
|
|
|
|
|
//
|
|
|
|
// USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
|
|
|
|
//
|
|
|
|
Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fill in the write16 command block
|
|
|
|
//
|
|
|
|
ZeroMem (WriteCmd, sizeof (WriteCmd));
|
|
|
|
|
|
|
|
WriteCmd[0] = EFI_SCSI_OP_WRITE16;
|
|
|
|
WriteCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
|
|
|
|
WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba));
|
|
|
|
WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count));
|
|
|
|
|
|
|
|
Status = UsbBootExecCmdWithRetry (
|
|
|
|
UsbMass,
|
|
|
|
WriteCmd,
|
|
|
|
(UINT8) sizeof (WriteCmd),
|
|
|
|
EfiUsbDataOut,
|
|
|
|
Buffer,
|
|
|
|
ByteSize,
|
|
|
|
Timeout
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
|
2007-07-11 10:47:37 +02:00
|
|
|
Lba += Count;
|
|
|
|
Buffer += Count * BlockSize;
|
|
|
|
TotalBlock -= Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-12 04:11:00 +01:00
|
|
|
Use the USB clear feature control transfer to clear the endpoint stall condition.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@param UsbIo The USB I/O Protocol instance
|
2007-07-11 10:47:37 +02:00
|
|
|
@param EndpointAddr The endpoint to clear stall for
|
|
|
|
|
2009-01-12 04:11:00 +01:00
|
|
|
@retval EFI_SUCCESS The endpoint stall condition is cleared.
|
|
|
|
@retval Others Failed to clear the endpoint stall condition.
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
UsbClearEndpointStall (
|
|
|
|
IN EFI_USB_IO_PROTOCOL *UsbIo,
|
|
|
|
IN UINT8 EndpointAddr
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_USB_DEVICE_REQUEST Request;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT32 CmdResult;
|
|
|
|
UINT32 Timeout;
|
|
|
|
|
|
|
|
Request.RequestType = 0x02;
|
|
|
|
Request.Request = USB_REQ_CLEAR_FEATURE;
|
|
|
|
Request.Value = USB_FEATURE_ENDPOINT_HALT;
|
|
|
|
Request.Index = EndpointAddr;
|
|
|
|
Request.Length = 0;
|
2007-10-08 08:14:13 +02:00
|
|
|
Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
|
2007-07-11 10:47:37 +02:00
|
|
|
|
|
|
|
Status = UsbIo->UsbControlTransfer (
|
|
|
|
UsbIo,
|
|
|
|
&Request,
|
|
|
|
EfiUsbNoData,
|
|
|
|
Timeout,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&CmdResult
|
|
|
|
);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|