]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
Update auth-variable and secure boot UI driver to support only time-based PK, KEK...
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.c
CommitLineData
e237e7ae 1/** @file\r
d80ed2a7 2 Implementation of the command set of USB Mass Storage Specification\r
3 for Bootability, Revision 1.0.\r
cc5166ff 4\r
16d718a5 5Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 6This program and the accompanying materials\r
e237e7ae 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
cc5166ff 14**/\r
e237e7ae 15\r
39840c50 16#include "UsbMass.h"\r
e237e7ae 17\r
e237e7ae 18/**\r
d80ed2a7 19 Execute REQUEST SENSE Command to retrieve sense data from device.\r
e237e7ae 20\r
d80ed2a7 21 @param UsbMass The device whose sense data is requested.\r
e237e7ae 22\r
d80ed2a7 23 @retval EFI_SUCCESS The command is excuted successfully.\r
cc5166ff 24 @retval EFI_DEVICE_ERROR Failed to request sense.\r
25 @retval EFI_NO_RESPONSE The device media doesn't response this request.\r
26 @retval EFI_INVALID_PARAMETER The command has some invalid parameters.\r
27 @retval EFI_WRITE_PROTECTED The device is write protected.\r
28 @retval EFI_MEDIA_CHANGED The device media has been changed.\r
e237e7ae 29\r
30**/\r
31EFI_STATUS\r
32UsbBootRequestSense (\r
33 IN USB_MASS_DEVICE *UsbMass\r
34 )\r
35{\r
36 USB_BOOT_REQUEST_SENSE_CMD SenseCmd;\r
37 USB_BOOT_REQUEST_SENSE_DATA SenseData;\r
38 EFI_BLOCK_IO_MEDIA *Media;\r
39 USB_MASS_TRANSPORT *Transport;\r
40 EFI_STATUS Status;\r
41 UINT32 CmdResult;\r
42\r
43 Transport = UsbMass->Transport;\r
44\r
45 //\r
d80ed2a7 46 // Request the sense data from the device\r
e237e7ae 47 //\r
48 ZeroMem (&SenseCmd, sizeof (USB_BOOT_REQUEST_SENSE_CMD));\r
49 ZeroMem (&SenseData, sizeof (USB_BOOT_REQUEST_SENSE_DATA));\r
50\r
51 SenseCmd.OpCode = USB_BOOT_REQUEST_SENSE_OPCODE;\r
c52fa98c 52 SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
c9325700 53 SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA);\r
e237e7ae 54\r
55 Status = Transport->ExecCommand (\r
56 UsbMass->Context,\r
57 &SenseCmd,\r
58 sizeof (USB_BOOT_REQUEST_SENSE_CMD),\r
59 EfiUsbDataIn,\r
60 &SenseData,\r
61 sizeof (USB_BOOT_REQUEST_SENSE_DATA),\r
c7e39923 62 UsbMass->Lun,\r
e237e7ae 63 USB_BOOT_GENERAL_CMD_TIMEOUT,\r
64 &CmdResult\r
65 );\r
66 if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) {\r
1c619535 67 DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult));\r
88e349f1 68 if (!EFI_ERROR (Status)) {\r
69 Status = EFI_DEVICE_ERROR;\r
70 }\r
50fa1b3a 71 return Status;\r
e237e7ae 72 }\r
73\r
74 //\r
d80ed2a7 75 // If sense data is retrieved successfully, interpret the sense data\r
76 // and update the media status if necessary.\r
e237e7ae 77 //\r
78 Media = &UsbMass->BlockIoMedia;\r
79\r
80 switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) {\r
81\r
82 case USB_BOOT_SENSE_NO_SENSE:\r
50fa1b3a 83 Status = EFI_NO_RESPONSE;\r
84 break;\r
85\r
e237e7ae 86 case USB_BOOT_SENSE_RECOVERED:\r
87 //\r
88 // Suppose hardware can handle this case, and recover later by itself\r
89 //\r
90 Status = EFI_NOT_READY;\r
91 break;\r
92\r
93 case USB_BOOT_SENSE_NOT_READY:\r
50fa1b3a 94 Status = EFI_DEVICE_ERROR;\r
1ccdbf2a 95 if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {\r
d80ed2a7 96 Media->MediaPresent = FALSE;\r
97 Status = EFI_NO_MEDIA;\r
1ccdbf2a 98 } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {\r
d80ed2a7 99 Status = EFI_NOT_READY;\r
e237e7ae 100 }\r
101 break;\r
102\r
103 case USB_BOOT_SENSE_ILLEGAL_REQUEST:\r
104 Status = EFI_INVALID_PARAMETER;\r
105 break;\r
106\r
107 case USB_BOOT_SENSE_UNIT_ATTENTION:\r
108 Status = EFI_DEVICE_ERROR;\r
1ccdbf2a 109 if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) {\r
50fa1b3a 110 //\r
d80ed2a7 111 // If MediaChange, reset ReadOnly and new MediaId\r
50fa1b3a 112 //\r
d80ed2a7 113 Status = EFI_MEDIA_CHANGED;\r
50fa1b3a 114 Media->ReadOnly = FALSE;\r
115 Media->MediaId++;\r
e237e7ae 116 }\r
117 break;\r
118\r
d80ed2a7 119 case USB_BOOT_SENSE_DATA_PROTECT:\r
120 Status = EFI_WRITE_PROTECTED;\r
50fa1b3a 121 Media->ReadOnly = TRUE;\r
e237e7ae 122 break;\r
123\r
124 default:\r
125 Status = EFI_DEVICE_ERROR;\r
126 break;\r
127 }\r
128\r
1c619535 129 DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",\r
e237e7ae 130 Status,\r
131 USB_BOOT_SENSE_KEY (SenseData.SenseKey),\r
1ccdbf2a 132 SenseData.Asc,\r
133 SenseData.Ascq\r
e237e7ae 134 ));\r
135\r
136 return Status;\r
137}\r
138\r
139\r
140/**\r
d80ed2a7 141 Execute the USB mass storage bootability commands.\r
142\r
143 This function executes the USB mass storage bootability commands.\r
144 If execution failed, retrieve the error by REQUEST_SENSE, then\r
145 update the device's status, such as ReadyOnly.\r
e237e7ae 146\r
147 @param UsbMass The device to issue commands to\r
148 @param Cmd The command to execute\r
149 @param CmdLen The length of the command\r
150 @param DataDir The direction of data transfer\r
151 @param Data The buffer to hold the data\r
152 @param DataLen The length of expected data\r
153 @param Timeout The timeout used to transfer\r
154\r
d80ed2a7 155 @retval EFI_SUCCESS Command is excuted successfully\r
156 @retval Others Command execution failed.\r
e237e7ae 157\r
158**/\r
e237e7ae 159EFI_STATUS\r
160UsbBootExecCmd (\r
161 IN USB_MASS_DEVICE *UsbMass,\r
162 IN VOID *Cmd,\r
163 IN UINT8 CmdLen,\r
164 IN EFI_USB_DATA_DIRECTION DataDir,\r
165 IN VOID *Data,\r
166 IN UINT32 DataLen,\r
167 IN UINT32 Timeout\r
168 )\r
169{\r
170 USB_MASS_TRANSPORT *Transport;\r
171 EFI_STATUS Status;\r
172 UINT32 CmdResult;\r
173\r
174 Transport = UsbMass->Transport;\r
175 Status = Transport->ExecCommand (\r
176 UsbMass->Context,\r
177 Cmd,\r
178 CmdLen,\r
179 DataDir,\r
180 Data,\r
181 DataLen,\r
c7e39923 182 UsbMass->Lun,\r
e237e7ae 183 Timeout,\r
184 &CmdResult\r
185 );\r
19bc8527 186\r
187 if (Status == EFI_TIMEOUT) {\r
188 DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8 *)Cmd));\r
189 return EFI_TIMEOUT;\r
190 }\r
191\r
e237e7ae 192 //\r
d80ed2a7 193 // If ExecCommand() returns no error and CmdResult is success,\r
194 // then the commnad transfer is successful.\r
e237e7ae 195 //\r
d80ed2a7 196 if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {\r
e237e7ae 197 return EFI_SUCCESS;\r
198 }\r
199\r
d80ed2a7 200 //\r
201 // If command execution failed, then retrieve error info via sense request.\r
202 //\r
e237e7ae 203 return UsbBootRequestSense (UsbMass);\r
204}\r
205\r
206\r
207/**\r
d80ed2a7 208 Execute the USB mass storage bootability commands with retrial.\r
e237e7ae 209\r
d80ed2a7 210 This function executes USB mass storage bootability commands.\r
211 If the device isn't ready, wait for it. If the device is ready\r
212 and error occurs, retry the command again until it exceeds the\r
213 limit of retrial times.\r
214 \r
e237e7ae 215 @param UsbMass The device to issue commands to\r
216 @param Cmd The command to execute\r
217 @param CmdLen The length of the command\r
218 @param DataDir The direction of data transfer\r
219 @param Data The buffer to hold the data\r
220 @param DataLen The length of expected data\r
cc5166ff 221 @param Timeout The timeout used to transfer\r
e237e7ae 222\r
d80ed2a7 223 @retval EFI_SUCCESS The command is executed successfully.\r
224 @retval EFI_MEDIA_CHANGED The device media has been changed.\r
225 @retval Others Command execution failed after retrial.\r
e237e7ae 226\r
227**/\r
e237e7ae 228EFI_STATUS\r
229UsbBootExecCmdWithRetry (\r
230 IN USB_MASS_DEVICE *UsbMass,\r
231 IN VOID *Cmd,\r
232 IN UINT8 CmdLen,\r
233 IN EFI_USB_DATA_DIRECTION DataDir,\r
234 IN VOID *Data,\r
235 IN UINT32 DataLen,\r
236 IN UINT32 Timeout\r
237 )\r
238{\r
239 EFI_STATUS Status;\r
d80ed2a7 240 UINTN Retry;\r
19bc8527 241 EFI_EVENT TimeoutEvt;\r
242\r
243 Retry = 0;\r
244 Status = EFI_SUCCESS;\r
245 Status = gBS->CreateEvent (\r
246 EVT_TIMER,\r
247 TPL_CALLBACK,\r
248 NULL,\r
249 NULL,\r
250 &TimeoutEvt\r
251 );\r
252 if (EFI_ERROR (Status)) {\r
253 return Status;\r
254 }\r
e237e7ae 255\r
19bc8527 256 Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(60));\r
257 if (EFI_ERROR (Status)) {\r
258 goto EXIT;\r
259 }\r
e237e7ae 260\r
19bc8527 261 //\r
262 // Execute the cmd and retry if it fails.\r
263 //\r
264 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {\r
e237e7ae 265 Status = UsbBootExecCmd (\r
266 UsbMass,\r
267 Cmd,\r
268 CmdLen,\r
269 DataDir,\r
270 Data,\r
271 DataLen,\r
1c619535 272 Timeout\r
e237e7ae 273 );\r
19bc8527 274 if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {\r
e237e7ae 275 break;\r
276 }\r
277 //\r
19bc8527 278 // If the sense data shows the drive is not ready, we need execute the cmd again.\r
279 // We limit the upper boundary to 60 seconds.\r
e237e7ae 280 //\r
19bc8527 281 if (Status == EFI_NOT_READY) {\r
282 continue;\r
e237e7ae 283 }\r
19bc8527 284 //\r
285 // If the status is other error, then just retry 5 times.\r
286 //\r
287 if (Retry++ >= USB_BOOT_COMMAND_RETRY) {\r
288 break;\r
289 }\r
290 }\r
291\r
292EXIT:\r
293 if (TimeoutEvt != NULL) {\r
294 gBS->CloseEvent (TimeoutEvt);\r
e237e7ae 295 }\r
296\r
297 return Status;\r
298}\r
299\r
300\r
e237e7ae 301/**\r
d80ed2a7 302 Execute TEST UNIT READY command to check if the device is ready.\r
e237e7ae 303\r
304 @param UsbMass The device to test\r
305\r
d80ed2a7 306 @retval EFI_SUCCESS The device is ready.\r
e237e7ae 307 @retval Others Device not ready.\r
308\r
309**/\r
310EFI_STATUS\r
311UsbBootIsUnitReady (\r
312 IN USB_MASS_DEVICE *UsbMass\r
313 )\r
314{\r
315 USB_BOOT_TEST_UNIT_READY_CMD TestCmd;\r
316\r
317 ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD));\r
318\r
319 TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE;\r
c52fa98c 320 TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
e237e7ae 321\r
322 return UsbBootExecCmdWithRetry (\r
323 UsbMass,\r
324 &TestCmd,\r
c9325700 325 (UINT8) sizeof (USB_BOOT_TEST_UNIT_READY_CMD),\r
e237e7ae 326 EfiUsbNoData,\r
327 NULL,\r
328 0,\r
329 USB_BOOT_GENERAL_CMD_TIMEOUT\r
330 );\r
331}\r
332\r
333\r
334/**\r
d80ed2a7 335 Execute INQUIRY Command to request information regarding parameters of\r
336 the device be sent to the host computer.\r
e237e7ae 337\r
d80ed2a7 338 @param UsbMass The device to inquire.\r
e237e7ae 339\r
d80ed2a7 340 @retval EFI_SUCCESS INQUIRY Command is executed successfully.\r
341 @retval Others INQUIRY Command is not executed successfully.\r
e237e7ae 342\r
343**/\r
344EFI_STATUS\r
345UsbBootInquiry (\r
346 IN USB_MASS_DEVICE *UsbMass\r
347 )\r
348{\r
349 USB_BOOT_INQUIRY_CMD InquiryCmd;\r
e237e7ae 350 EFI_BLOCK_IO_MEDIA *Media;\r
351 EFI_STATUS Status;\r
352\r
353 Media = &(UsbMass->BlockIoMedia);\r
354\r
e237e7ae 355 ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));\r
39840c50 356 ZeroMem (&UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));\r
e237e7ae 357\r
358 InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE;\r
c52fa98c 359 InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
39840c50 360 InquiryCmd.AllocLen = (UINT8) sizeof (USB_BOOT_INQUIRY_DATA);\r
e237e7ae 361\r
362 Status = UsbBootExecCmdWithRetry (\r
363 UsbMass,\r
364 &InquiryCmd,\r
c9325700 365 (UINT8) sizeof (USB_BOOT_INQUIRY_CMD),\r
e237e7ae 366 EfiUsbDataIn,\r
39840c50 367 &UsbMass->InquiryData,\r
e237e7ae 368 sizeof (USB_BOOT_INQUIRY_DATA),\r
50fa1b3a 369 USB_BOOT_GENERAL_CMD_TIMEOUT\r
e237e7ae 370 );\r
371 if (EFI_ERROR (Status)) {\r
372 return Status;\r
373 }\r
374\r
d80ed2a7 375 //\r
376 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit\r
377 // from the inquiry data.\r
378 //\r
39840c50 379 UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (UsbMass->InquiryData.Pdt));\r
380 Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (UsbMass->InquiryData.Removable));\r
e237e7ae 381 //\r
d80ed2a7 382 // Set block size to the default value of 512 Bytes, in case no media is present at first time.\r
e237e7ae 383 //\r
384 Media->BlockSize = 0x0200;\r
385\r
386 return Status;\r
387}\r
388\r
389\r
390/**\r
d80ed2a7 391 Execute READ CAPACITY command to request information regarding\r
392 the capacity of the installed medium of the device.\r
393\r
394 This function executes READ CAPACITY command to get the capacity\r
395 of the USB mass storage media, including the presence, block size,\r
396 and last block number.\r
e237e7ae 397\r
398 @param UsbMass The device to retireve disk gemotric.\r
399\r
d80ed2a7 400 @retval EFI_SUCCESS The disk geometry is successfully retrieved.\r
401 @retval EFI_NOT_READY The returned block size is zero.\r
402 @retval Other READ CAPACITY command execution failed.\r
cc5166ff 403 \r
e237e7ae 404**/\r
405EFI_STATUS\r
406UsbBootReadCapacity (\r
407 IN USB_MASS_DEVICE *UsbMass\r
408 )\r
409{\r
410 USB_BOOT_READ_CAPACITY_CMD CapacityCmd;\r
411 USB_BOOT_READ_CAPACITY_DATA CapacityData;\r
412 EFI_BLOCK_IO_MEDIA *Media;\r
413 EFI_STATUS Status;\r
88e349f1 414 UINT32 BlockSize;\r
e237e7ae 415\r
416 Media = &UsbMass->BlockIoMedia;\r
417\r
e237e7ae 418 ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));\r
419 ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));\r
420\r
421 CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;\r
c52fa98c 422 CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
e237e7ae 423\r
424 Status = UsbBootExecCmdWithRetry (\r
425 UsbMass,\r
426 &CapacityCmd,\r
c9325700 427 (UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD),\r
e237e7ae 428 EfiUsbDataIn,\r
429 &CapacityData,\r
430 sizeof (USB_BOOT_READ_CAPACITY_DATA),\r
431 USB_BOOT_GENERAL_CMD_TIMEOUT\r
432 );\r
433 if (EFI_ERROR (Status)) {\r
434 return Status;\r
435 }\r
436\r
d80ed2a7 437 //\r
438 // Get the information on media presence, block size, and last block number\r
439 // from READ CAPACITY data.\r
440 //\r
e237e7ae 441 Media->MediaPresent = TRUE;\r
d80ed2a7 442 Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));\r
e237e7ae 443\r
88e349f1 444 BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));\r
445 if (BlockSize == 0) {\r
efe9186f 446 //\r
447 // Get sense data \r
448 //\r
449 return UsbBootRequestSense (UsbMass);\r
88e349f1 450 } else {\r
451 Media->BlockSize = BlockSize;\r
50fa1b3a 452 }\r
453\r
efe9186f 454 return Status;\r
e237e7ae 455}\r
456\r
e237e7ae 457/**\r
d80ed2a7 458 Retrieves SCSI mode sense information via MODE SENSE(6) command.\r
e237e7ae 459\r
d80ed2a7 460 @param UsbMass The device whose sense data is requested.\r
e237e7ae 461\r
d80ed2a7 462 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.\r
463 @retval Other Command execution failed.\r
e237e7ae 464\r
465**/\r
466EFI_STATUS\r
50fa1b3a 467UsbScsiModeSense (\r
e237e7ae 468 IN USB_MASS_DEVICE *UsbMass\r
469 )\r
470{\r
ca12415a 471 EFI_STATUS Status;\r
50fa1b3a 472 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;\r
473 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;\r
474 EFI_BLOCK_IO_MEDIA *Media;\r
475\r
ca12415a 476 Media = &UsbMass->BlockIoMedia;\r
e237e7ae 477\r
50fa1b3a 478 ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));\r
479 ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));\r
e237e7ae 480\r
481 //\r
d80ed2a7 482 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec\r
e237e7ae 483 //\r
50fa1b3a 484 ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;\r
687a2e5f 485 ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);\r
50fa1b3a 486 ModeSenseCmd.PageCode = 0x3F;\r
487 ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);\r
e237e7ae 488\r
489 Status = UsbBootExecCmdWithRetry (\r
490 UsbMass,\r
491 &ModeSenseCmd,\r
c9325700 492 (UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD),\r
e237e7ae 493 EfiUsbDataIn,\r
494 &ModeParaHeader,\r
50fa1b3a 495 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),\r
e237e7ae 496 USB_BOOT_GENERAL_CMD_TIMEOUT\r
497 );\r
50fa1b3a 498\r
e237e7ae 499 //\r
d80ed2a7 500 // Format of device-specific parameter byte of the mode parameter header is defined in\r
501 // Section 8.2.10 of SCSI-2 Spec.\r
502 // BIT7 of this byte is indicates whether the medium is write protected.\r
e237e7ae 503 //\r
50fa1b3a 504 if (!EFI_ERROR (Status)) {\r
d80ed2a7 505 Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);\r
50fa1b3a 506 }\r
e237e7ae 507\r
508 return Status;\r
509}\r
510\r
511\r
512/**\r
d80ed2a7 513 Get the parameters for the USB mass storage media.\r
514\r
515 This function get the parameters for the USB mass storage media,\r
516 It is used both to initialize the media during the Start() phase\r
517 of Driver Binding Protocol and to re-initialize it when the media is\r
e237e7ae 518 changed. Althought the RemoveableMedia is unlikely to change,\r
d80ed2a7 519 it is also included here.\r
e237e7ae 520\r
d80ed2a7 521 @param UsbMass The device to retrieve disk gemotric.\r
e237e7ae 522\r
523 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.\r
d80ed2a7 524 @retval Other Failed to get the parameters.\r
e237e7ae 525\r
526**/\r
527EFI_STATUS\r
528UsbBootGetParams (\r
529 IN USB_MASS_DEVICE *UsbMass\r
530 )\r
531{\r
532 EFI_BLOCK_IO_MEDIA *Media;\r
533 EFI_STATUS Status;\r
50fa1b3a 534\r
ca12415a 535 Media = &(UsbMass->BlockIoMedia);\r
e237e7ae 536\r
537 Status = UsbBootInquiry (UsbMass);\r
538 if (EFI_ERROR (Status)) {\r
1c619535 539 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));\r
e237e7ae 540 return Status;\r
541 }\r
542\r
e237e7ae 543 //\r
d80ed2a7 544 // Don't use the Removable bit in inquiry data to test whether the media\r
e237e7ae 545 // is removable because many flash disks wrongly set this bit.\r
546 //\r
547 if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {\r
548 //\r
50fa1b3a 549 // CD-Rom device and Non-CD optical device\r
e237e7ae 550 //\r
551 UsbMass->OpticalStorage = TRUE;\r
552 //\r
553 // Default value 2048 Bytes, in case no media present at first time\r
554 //\r
555 Media->BlockSize = 0x0800;\r
50fa1b3a 556 }\r
557\r
19bc8527 558 Status = UsbBootDetectMedia (UsbMass);\r
e237e7ae 559\r
19bc8527 560 return Status;\r
e237e7ae 561}\r
562\r
563\r
564/**\r
565 Detect whether the removable media is present and whether it has changed.\r
e237e7ae 566\r
d80ed2a7 567 @param UsbMass The device to check.\r
e237e7ae 568\r
d80ed2a7 569 @retval EFI_SUCCESS The media status is successfully checked.\r
570 @retval Other Failed to detect media.\r
e237e7ae 571\r
572**/\r
573EFI_STATUS\r
574UsbBootDetectMedia (\r
575 IN USB_MASS_DEVICE *UsbMass\r
576 )\r
577{\r
578 EFI_BLOCK_IO_MEDIA OldMedia;\r
579 EFI_BLOCK_IO_MEDIA *Media;\r
50fa1b3a 580 UINT8 CmdSet;\r
581 EFI_TPL OldTpl;\r
e237e7ae 582 EFI_STATUS Status;\r
583\r
584 Media = &UsbMass->BlockIoMedia;\r
e61d30b0 585\r
d80ed2a7 586 CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));\r
e237e7ae 587\r
50fa1b3a 588 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;\r
589\r
e237e7ae 590 Status = UsbBootIsUnitReady (UsbMass);\r
19bc8527 591 if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {\r
50fa1b3a 592 goto ON_ERROR;\r
e237e7ae 593 }\r
50fa1b3a 594\r
595 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {\r
596 //\r
d80ed2a7 597 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,\r
598 // according to Section 4 of USB Mass Storage Specification for Bootability.\r
599 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI\r
600 // could get the information of Write Protected.\r
601 // Since not all device support this command, skip if fail.\r
50fa1b3a 602 //\r
603 UsbScsiModeSense (UsbMass);\r
604 }\r
605\r
606 Status = UsbBootReadCapacity (UsbMass);\r
e237e7ae 607 if (EFI_ERROR (Status)) {\r
1c619535 608 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));\r
50fa1b3a 609 goto ON_ERROR;\r
e237e7ae 610 }\r
611\r
50fa1b3a 612 return EFI_SUCCESS;\r
613\r
614ON_ERROR:\r
e237e7ae 615 //\r
d80ed2a7 616 // Detect whether it is necessary to reinstall the Block I/O Protocol.\r
e237e7ae 617 //\r
50fa1b3a 618 // MediaId may change in RequestSense for MediaChanged\r
619 // MediaPresent may change in RequestSense for NoMedia\r
620 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged\r
621 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity\r
622 //\r
e237e7ae 623 if ((Media->MediaId != OldMedia.MediaId) ||\r
624 (Media->MediaPresent != OldMedia.MediaPresent) ||\r
625 (Media->ReadOnly != OldMedia.ReadOnly) ||\r
626 (Media->BlockSize != OldMedia.BlockSize) ||\r
627 (Media->LastBlock != OldMedia.LastBlock)) {\r
50fa1b3a 628\r
d80ed2a7 629 //\r
630 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.\r
631 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().\r
632 //\r
633 OldTpl = EfiGetCurrentTpl ();\r
50fa1b3a 634 gBS->RestoreTPL (TPL_CALLBACK);\r
635\r
e237e7ae 636 gBS->ReinstallProtocolInterface (\r
637 UsbMass->Controller,\r
638 &gEfiBlockIoProtocolGuid,\r
639 &UsbMass->BlockIo,\r
640 &UsbMass->BlockIo\r
641 );\r
50fa1b3a 642\r
d80ed2a7 643 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);\r
50fa1b3a 644 gBS->RaiseTPL (OldTpl);\r
645\r
e237e7ae 646 //\r
d80ed2a7 647 // Update MediaId after reinstalling Block I/O Protocol.\r
e237e7ae 648 //\r
50fa1b3a 649 if (Media->MediaPresent != OldMedia.MediaPresent) {\r
d80ed2a7 650 if (Media->MediaPresent) {\r
50fa1b3a 651 Media->MediaId = 1;\r
652 } else {\r
653 Media->MediaId = 0;\r
654 }\r
e237e7ae 655 }\r
50fa1b3a 656\r
657 if ((Media->ReadOnly != OldMedia.ReadOnly) ||\r
658 (Media->BlockSize != OldMedia.BlockSize) ||\r
659 (Media->LastBlock != OldMedia.LastBlock)) {\r
660 Media->MediaId++;\r
e237e7ae 661 }\r
662 }\r
663\r
664 return Status;\r
665}\r
666\r
667\r
668/**\r
669 Read some blocks from the device.\r
670\r
671 @param UsbMass The USB mass storage device to read from\r
672 @param Lba The start block number\r
673 @param TotalBlock Total block number to read\r
674 @param Buffer The buffer to read to\r
675\r
676 @retval EFI_SUCCESS Data are read into the buffer\r
677 @retval Others Failed to read all the data\r
678\r
679**/\r
680EFI_STATUS\r
681UsbBootReadBlocks (\r
682 IN USB_MASS_DEVICE *UsbMass,\r
683 IN UINT32 Lba,\r
684 IN UINTN TotalBlock,\r
685 OUT UINT8 *Buffer\r
686 )\r
687{\r
688 USB_BOOT_READ10_CMD ReadCmd;\r
689 EFI_STATUS Status;\r
690 UINT16 Count;\r
691 UINT32 BlockSize;\r
692 UINT32 ByteSize;\r
693 UINT32 Timeout;\r
694\r
695 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
696 Status = EFI_SUCCESS;\r
697\r
698 while (TotalBlock > 0) {\r
699 //\r
700 // Split the total blocks into smaller pieces to ease the pressure\r
701 // on the device. We must split the total block because the READ10\r
702 // command only has 16 bit transfer length (in the unit of block).\r
703 //\r
704 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
705 ByteSize = (UINT32)Count * BlockSize;\r
706\r
707 //\r
50fa1b3a 708 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
e237e7ae 709 //\r
50fa1b3a 710 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
e237e7ae 711\r
712 //\r
713 // Fill in the command then execute\r
714 //\r
715 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));\r
716\r
717 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;\r
c52fa98c 718 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
d80ed2a7 719 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));\r
720 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));\r
e237e7ae 721\r
722 Status = UsbBootExecCmdWithRetry (\r
723 UsbMass,\r
724 &ReadCmd,\r
c9325700 725 (UINT8) sizeof (USB_BOOT_READ10_CMD),\r
e237e7ae 726 EfiUsbDataIn,\r
727 Buffer,\r
728 ByteSize,\r
729 Timeout\r
730 );\r
731 if (EFI_ERROR (Status)) {\r
732 return Status;\r
733 }\r
16d718a5 734 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, TotalBlock));\r
e237e7ae 735 Lba += Count;\r
736 Buffer += Count * BlockSize;\r
737 TotalBlock -= Count;\r
738 }\r
739\r
740 return Status;\r
741}\r
742\r
743\r
744/**\r
745 Write some blocks to the device.\r
746\r
747 @param UsbMass The USB mass storage device to write to\r
748 @param Lba The start block number\r
749 @param TotalBlock Total block number to write\r
d80ed2a7 750 @param Buffer Pointer to the source buffer for the data.\r
e237e7ae 751\r
752 @retval EFI_SUCCESS Data are written into the buffer\r
753 @retval Others Failed to write all the data\r
754\r
755**/\r
756EFI_STATUS\r
757UsbBootWriteBlocks (\r
758 IN USB_MASS_DEVICE *UsbMass,\r
759 IN UINT32 Lba,\r
760 IN UINTN TotalBlock,\r
d80ed2a7 761 IN UINT8 *Buffer\r
e237e7ae 762 )\r
763{\r
764 USB_BOOT_WRITE10_CMD WriteCmd;\r
765 EFI_STATUS Status;\r
766 UINT16 Count;\r
767 UINT32 BlockSize;\r
768 UINT32 ByteSize;\r
769 UINT32 Timeout;\r
770\r
771 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
772 Status = EFI_SUCCESS;\r
773\r
774 while (TotalBlock > 0) {\r
775 //\r
776 // Split the total blocks into smaller pieces to ease the pressure\r
777 // on the device. We must split the total block because the WRITE10\r
778 // command only has 16 bit transfer length (in the unit of block).\r
779 //\r
780 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
781 ByteSize = (UINT32)Count * BlockSize;\r
782\r
783 //\r
50fa1b3a 784 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
e237e7ae 785 //\r
50fa1b3a 786 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
e237e7ae 787\r
788 //\r
789 // Fill in the write10 command block\r
790 //\r
791 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));\r
792\r
793 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;\r
c52fa98c 794 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
d80ed2a7 795 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));\r
796 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));\r
e237e7ae 797\r
798 Status = UsbBootExecCmdWithRetry (\r
799 UsbMass,\r
800 &WriteCmd,\r
c9325700 801 (UINT8) sizeof (USB_BOOT_WRITE10_CMD),\r
e237e7ae 802 EfiUsbDataOut,\r
803 Buffer,\r
804 ByteSize,\r
805 Timeout\r
806 );\r
807 if (EFI_ERROR (Status)) {\r
808 return Status;\r
809 }\r
16d718a5 810 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, TotalBlock));\r
e237e7ae 811 Lba += Count;\r
812 Buffer += Count * BlockSize;\r
813 TotalBlock -= Count;\r
814 }\r
815\r
816 return Status;\r
817}\r
818\r
e237e7ae 819/**\r
d80ed2a7 820 Use the USB clear feature control transfer to clear the endpoint stall condition.\r
e237e7ae 821\r
d80ed2a7 822 @param UsbIo The USB I/O Protocol instance\r
e237e7ae 823 @param EndpointAddr The endpoint to clear stall for\r
824\r
d80ed2a7 825 @retval EFI_SUCCESS The endpoint stall condition is cleared.\r
826 @retval Others Failed to clear the endpoint stall condition.\r
e237e7ae 827\r
828**/\r
829EFI_STATUS\r
830UsbClearEndpointStall (\r
831 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
832 IN UINT8 EndpointAddr\r
833 )\r
834{\r
835 EFI_USB_DEVICE_REQUEST Request;\r
836 EFI_STATUS Status;\r
837 UINT32 CmdResult;\r
838 UINT32 Timeout;\r
839\r
840 Request.RequestType = 0x02;\r
841 Request.Request = USB_REQ_CLEAR_FEATURE;\r
842 Request.Value = USB_FEATURE_ENDPOINT_HALT;\r
843 Request.Index = EndpointAddr;\r
844 Request.Length = 0;\r
41e8ff27 845 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;\r
e237e7ae 846\r
847 Status = UsbIo->UsbControlTransfer (\r
848 UsbIo,\r
849 &Request,\r
850 EfiUsbNoData,\r
851 Timeout,\r
852 NULL,\r
853 0,\r
854 &CmdResult\r
855 );\r
856\r
857 return Status;\r
858}\r