]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
MdeModulePkg/UsbMass: Revert POWER_ON(29h) ASC handling logic
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.c
... / ...
CommitLineData
1/** @file\r
2 Implementation of the command set of USB Mass Storage Specification\r
3 for Bootability, Revision 1.0.\r
4\r
5Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
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
14**/\r
15\r
16#include "UsbMass.h"\r
17\r
18/**\r
19 Execute REQUEST SENSE Command to retrieve sense data from device.\r
20\r
21 @param UsbMass The device whose sense data is requested.\r
22\r
23 @retval EFI_SUCCESS The command is executed successfully.\r
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
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
46 // Request the sense data from the device\r
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
52 SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
53 SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA);\r
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
62 UsbMass->Lun,\r
63 USB_BOOT_GENERAL_CMD_TIMEOUT,\r
64 &CmdResult\r
65 );\r
66 if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) {\r
67 DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult));\r
68 if (!EFI_ERROR (Status)) {\r
69 Status = EFI_DEVICE_ERROR;\r
70 }\r
71 return Status;\r
72 }\r
73\r
74 //\r
75 // If sense data is retrieved successfully, interpret the sense data\r
76 // and update the media status if necessary.\r
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
83 if (SenseData.Asc == USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION) {\r
84 //\r
85 // It is not an error if a device does not have additional sense information\r
86 //\r
87 Status = EFI_SUCCESS;\r
88 } else {\r
89 Status = EFI_NO_RESPONSE;\r
90 }\r
91 break;\r
92\r
93 case USB_BOOT_SENSE_RECOVERED:\r
94 //\r
95 // Suppose hardware can handle this case, and recover later by itself\r
96 //\r
97 Status = EFI_NOT_READY;\r
98 break;\r
99\r
100 case USB_BOOT_SENSE_NOT_READY:\r
101 Status = EFI_DEVICE_ERROR;\r
102 if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {\r
103 Media->MediaPresent = FALSE;\r
104 Status = EFI_NO_MEDIA;\r
105 } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {\r
106 Status = EFI_NOT_READY;\r
107 }\r
108 break;\r
109\r
110 case USB_BOOT_SENSE_ILLEGAL_REQUEST:\r
111 Status = EFI_INVALID_PARAMETER;\r
112 break;\r
113\r
114 case USB_BOOT_SENSE_UNIT_ATTENTION:\r
115 Status = EFI_DEVICE_ERROR;\r
116 if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) {\r
117 //\r
118 // If MediaChange, reset ReadOnly and new MediaId\r
119 //\r
120 Status = EFI_MEDIA_CHANGED;\r
121 Media->ReadOnly = FALSE;\r
122 Media->MediaId++;\r
123 } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {\r
124 Status = EFI_NOT_READY;\r
125 } else if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {\r
126 Status = EFI_NOT_READY;\r
127 }\r
128 break;\r
129\r
130 case USB_BOOT_SENSE_DATA_PROTECT:\r
131 Status = EFI_WRITE_PROTECTED;\r
132 Media->ReadOnly = TRUE;\r
133 break;\r
134\r
135 default:\r
136 Status = EFI_DEVICE_ERROR;\r
137 break;\r
138 }\r
139\r
140 DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",\r
141 Status,\r
142 USB_BOOT_SENSE_KEY (SenseData.SenseKey),\r
143 SenseData.Asc,\r
144 SenseData.Ascq\r
145 ));\r
146\r
147 return Status;\r
148}\r
149\r
150\r
151/**\r
152 Execute the USB mass storage bootability commands.\r
153\r
154 This function executes the USB mass storage bootability commands.\r
155 If execution failed, retrieve the error by REQUEST_SENSE, then\r
156 update the device's status, such as ReadyOnly.\r
157\r
158 @param UsbMass The device to issue commands to\r
159 @param Cmd The command to execute\r
160 @param CmdLen The length of the command\r
161 @param DataDir The direction of data transfer\r
162 @param Data The buffer to hold the data\r
163 @param DataLen The length of expected data\r
164 @param Timeout The timeout used to transfer\r
165\r
166 @retval EFI_SUCCESS Command is executed successfully\r
167 @retval Others Command execution failed.\r
168\r
169**/\r
170EFI_STATUS\r
171UsbBootExecCmd (\r
172 IN USB_MASS_DEVICE *UsbMass,\r
173 IN VOID *Cmd,\r
174 IN UINT8 CmdLen,\r
175 IN EFI_USB_DATA_DIRECTION DataDir,\r
176 IN VOID *Data,\r
177 IN UINT32 DataLen,\r
178 IN UINT32 Timeout\r
179 )\r
180{\r
181 USB_MASS_TRANSPORT *Transport;\r
182 EFI_STATUS Status;\r
183 UINT32 CmdResult;\r
184\r
185 Transport = UsbMass->Transport;\r
186 Status = Transport->ExecCommand (\r
187 UsbMass->Context,\r
188 Cmd,\r
189 CmdLen,\r
190 DataDir,\r
191 Data,\r
192 DataLen,\r
193 UsbMass->Lun,\r
194 Timeout,\r
195 &CmdResult\r
196 );\r
197\r
198 if (Status == EFI_TIMEOUT) {\r
199 DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8 *)Cmd));\r
200 return EFI_TIMEOUT;\r
201 }\r
202\r
203 //\r
204 // If ExecCommand() returns no error and CmdResult is success,\r
205 // then the commnad transfer is successful.\r
206 //\r
207 if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {\r
208 return EFI_SUCCESS;\r
209 }\r
210\r
211 //\r
212 // If command execution failed, then retrieve error info via sense request.\r
213 //\r
214 return UsbBootRequestSense (UsbMass);\r
215}\r
216\r
217\r
218/**\r
219 Execute the USB mass storage bootability commands with retrial.\r
220\r
221 This function executes USB mass storage bootability commands.\r
222 If the device isn't ready, wait for it. If the device is ready\r
223 and error occurs, retry the command again until it exceeds the\r
224 limit of retrial times.\r
225 \r
226 @param UsbMass The device to issue commands to\r
227 @param Cmd The command to execute\r
228 @param CmdLen The length of the command\r
229 @param DataDir The direction of data transfer\r
230 @param Data The buffer to hold the data\r
231 @param DataLen The length of expected data\r
232 @param Timeout The timeout used to transfer\r
233\r
234 @retval EFI_SUCCESS The command is executed successfully.\r
235 @retval EFI_MEDIA_CHANGED The device media has been changed.\r
236 @retval Others Command execution failed after retrial.\r
237\r
238**/\r
239EFI_STATUS\r
240UsbBootExecCmdWithRetry (\r
241 IN USB_MASS_DEVICE *UsbMass,\r
242 IN VOID *Cmd,\r
243 IN UINT8 CmdLen,\r
244 IN EFI_USB_DATA_DIRECTION DataDir,\r
245 IN VOID *Data,\r
246 IN UINT32 DataLen,\r
247 IN UINT32 Timeout\r
248 )\r
249{\r
250 EFI_STATUS Status;\r
251 UINTN Retry;\r
252 EFI_EVENT TimeoutEvt;\r
253\r
254 Retry = 0;\r
255 Status = EFI_SUCCESS;\r
256 Status = gBS->CreateEvent (\r
257 EVT_TIMER,\r
258 TPL_CALLBACK,\r
259 NULL,\r
260 NULL,\r
261 &TimeoutEvt\r
262 );\r
263 if (EFI_ERROR (Status)) {\r
264 return Status;\r
265 }\r
266\r
267 Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(60));\r
268 if (EFI_ERROR (Status)) {\r
269 goto EXIT;\r
270 }\r
271\r
272 //\r
273 // Execute the cmd and retry if it fails.\r
274 //\r
275 while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {\r
276 Status = UsbBootExecCmd (\r
277 UsbMass,\r
278 Cmd,\r
279 CmdLen,\r
280 DataDir,\r
281 Data,\r
282 DataLen,\r
283 Timeout\r
284 );\r
285 if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {\r
286 break;\r
287 }\r
288 //\r
289 // If the sense data shows the drive is not ready, we need execute the cmd again.\r
290 // We limit the upper boundary to 60 seconds.\r
291 //\r
292 if (Status == EFI_NOT_READY) {\r
293 continue;\r
294 }\r
295 //\r
296 // If the status is other error, then just retry 5 times.\r
297 //\r
298 if (Retry++ >= USB_BOOT_COMMAND_RETRY) {\r
299 break;\r
300 }\r
301 }\r
302\r
303EXIT:\r
304 if (TimeoutEvt != NULL) {\r
305 gBS->CloseEvent (TimeoutEvt);\r
306 }\r
307\r
308 return Status;\r
309}\r
310\r
311\r
312/**\r
313 Execute TEST UNIT READY command to check if the device is ready.\r
314\r
315 @param UsbMass The device to test\r
316\r
317 @retval EFI_SUCCESS The device is ready.\r
318 @retval Others Device not ready.\r
319\r
320**/\r
321EFI_STATUS\r
322UsbBootIsUnitReady (\r
323 IN USB_MASS_DEVICE *UsbMass\r
324 )\r
325{\r
326 USB_BOOT_TEST_UNIT_READY_CMD TestCmd;\r
327\r
328 ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD));\r
329\r
330 TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE;\r
331 TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
332\r
333 return UsbBootExecCmdWithRetry (\r
334 UsbMass,\r
335 &TestCmd,\r
336 (UINT8) sizeof (USB_BOOT_TEST_UNIT_READY_CMD),\r
337 EfiUsbNoData,\r
338 NULL,\r
339 0,\r
340 USB_BOOT_GENERAL_CMD_TIMEOUT\r
341 );\r
342}\r
343\r
344\r
345/**\r
346 Execute INQUIRY Command to request information regarding parameters of\r
347 the device be sent to the host computer.\r
348\r
349 @param UsbMass The device to inquire.\r
350\r
351 @retval EFI_SUCCESS INQUIRY Command is executed successfully.\r
352 @retval Others INQUIRY Command is not executed successfully.\r
353\r
354**/\r
355EFI_STATUS\r
356UsbBootInquiry (\r
357 IN USB_MASS_DEVICE *UsbMass\r
358 )\r
359{\r
360 USB_BOOT_INQUIRY_CMD InquiryCmd;\r
361 EFI_BLOCK_IO_MEDIA *Media;\r
362 EFI_STATUS Status;\r
363\r
364 Media = &(UsbMass->BlockIoMedia);\r
365\r
366 ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));\r
367 ZeroMem (&UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));\r
368\r
369 InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE;\r
370 InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
371 InquiryCmd.AllocLen = (UINT8) sizeof (USB_BOOT_INQUIRY_DATA);\r
372\r
373 Status = UsbBootExecCmdWithRetry (\r
374 UsbMass,\r
375 &InquiryCmd,\r
376 (UINT8) sizeof (USB_BOOT_INQUIRY_CMD),\r
377 EfiUsbDataIn,\r
378 &UsbMass->InquiryData,\r
379 sizeof (USB_BOOT_INQUIRY_DATA),\r
380 USB_BOOT_GENERAL_CMD_TIMEOUT\r
381 );\r
382 if (EFI_ERROR (Status)) {\r
383 return Status;\r
384 }\r
385\r
386 //\r
387 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit\r
388 // from the inquiry data.\r
389 //\r
390 UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (UsbMass->InquiryData.Pdt));\r
391 Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (UsbMass->InquiryData.Removable));\r
392 //\r
393 // Set block size to the default value of 512 Bytes, in case no media is present at first time.\r
394 //\r
395 Media->BlockSize = 0x0200;\r
396\r
397 return Status;\r
398}\r
399\r
400/**\r
401 Execute READ CAPACITY 16 bytes command to request information regarding\r
402 the capacity of the installed medium of the device.\r
403\r
404 This function executes READ CAPACITY 16 bytes command to get the capacity\r
405 of the USB mass storage media, including the presence, block size,\r
406 and last block number.\r
407\r
408 @param UsbMass The device to retireve disk gemotric.\r
409\r
410 @retval EFI_SUCCESS The disk geometry is successfully retrieved.\r
411 @retval EFI_NOT_READY The returned block size is zero.\r
412 @retval Other READ CAPACITY 16 bytes command execution failed.\r
413 \r
414**/\r
415EFI_STATUS\r
416UsbBootReadCapacity16 (\r
417 IN USB_MASS_DEVICE *UsbMass\r
418 )\r
419{\r
420 UINT8 CapacityCmd[16];\r
421 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData;\r
422 EFI_BLOCK_IO_MEDIA *Media;\r
423 EFI_STATUS Status;\r
424 UINT32 BlockSize;\r
425\r
426 Media = &UsbMass->BlockIoMedia;\r
427\r
428 Media->MediaPresent = FALSE;\r
429 Media->LastBlock = 0;\r
430 Media->BlockSize = 0;\r
431\r
432 ZeroMem (CapacityCmd, sizeof (CapacityCmd));\r
433 ZeroMem (&CapacityData, sizeof (CapacityData));\r
434\r
435 CapacityCmd[0] = EFI_SCSI_OP_READ_CAPACITY16;\r
436 CapacityCmd[1] = 0x10;\r
437 //\r
438 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.\r
439 //\r
440 ZeroMem ((CapacityCmd + 2), 8);\r
441\r
442 CapacityCmd[13] = sizeof (CapacityData);\r
443 \r
444 Status = UsbBootExecCmdWithRetry (\r
445 UsbMass,\r
446 CapacityCmd,\r
447 (UINT8) sizeof (CapacityCmd),\r
448 EfiUsbDataIn,\r
449 &CapacityData,\r
450 sizeof (CapacityData),\r
451 USB_BOOT_GENERAL_CMD_TIMEOUT\r
452 );\r
453 if (EFI_ERROR (Status)) {\r
454 return Status;\r
455 }\r
456\r
457 //\r
458 // Get the information on media presence, block size, and last block number\r
459 // from READ CAPACITY data.\r
460 //\r
461 Media->MediaPresent = TRUE;\r
462 Media->LastBlock = SwapBytes64 (ReadUnaligned64 ((CONST UINT64 *) &(CapacityData.LastLba7)));\r
463\r
464 BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) &(CapacityData.BlockSize3)));\r
465 \r
466 Media->LowestAlignedLba = (CapacityData.LowestAlignLogic2 << 8) |\r
467 CapacityData.LowestAlignLogic1;\r
468 Media->LogicalBlocksPerPhysicalBlock = (1 << CapacityData.LogicPerPhysical);\r
469 if (BlockSize == 0) {\r
470 //\r
471 // Get sense data \r
472 //\r
473 return UsbBootRequestSense (UsbMass);\r
474 } else {\r
475 Media->BlockSize = BlockSize;\r
476 }\r
477\r
478 return Status;\r
479}\r
480\r
481\r
482/**\r
483 Execute READ CAPACITY command to request information regarding\r
484 the capacity of the installed medium of the device.\r
485\r
486 This function executes READ CAPACITY command to get the capacity\r
487 of the USB mass storage media, including the presence, block size,\r
488 and last block number.\r
489\r
490 @param UsbMass The device to retireve disk gemotric.\r
491\r
492 @retval EFI_SUCCESS The disk geometry is successfully retrieved.\r
493 @retval EFI_NOT_READY The returned block size is zero.\r
494 @retval Other READ CAPACITY command execution failed.\r
495 \r
496**/\r
497EFI_STATUS\r
498UsbBootReadCapacity (\r
499 IN USB_MASS_DEVICE *UsbMass\r
500 )\r
501{\r
502 USB_BOOT_READ_CAPACITY_CMD CapacityCmd;\r
503 USB_BOOT_READ_CAPACITY_DATA CapacityData;\r
504 EFI_BLOCK_IO_MEDIA *Media;\r
505 EFI_STATUS Status;\r
506 UINT32 BlockSize;\r
507\r
508 Media = &UsbMass->BlockIoMedia;\r
509\r
510 ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));\r
511 ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));\r
512\r
513 CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;\r
514 CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
515\r
516 Status = UsbBootExecCmdWithRetry (\r
517 UsbMass,\r
518 &CapacityCmd,\r
519 (UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD),\r
520 EfiUsbDataIn,\r
521 &CapacityData,\r
522 sizeof (USB_BOOT_READ_CAPACITY_DATA),\r
523 USB_BOOT_GENERAL_CMD_TIMEOUT\r
524 );\r
525 if (EFI_ERROR (Status)) {\r
526 return Status;\r
527 }\r
528\r
529 //\r
530 // Get the information on media presence, block size, and last block number\r
531 // from READ CAPACITY data.\r
532 //\r
533 Media->MediaPresent = TRUE;\r
534 Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));\r
535\r
536 BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));\r
537 if (BlockSize == 0) {\r
538 //\r
539 // Get sense data \r
540 //\r
541 return UsbBootRequestSense (UsbMass);\r
542 } else {\r
543 Media->BlockSize = BlockSize;\r
544 }\r
545\r
546 if (Media->LastBlock == 0xFFFFFFFF) {\r
547 Status = UsbBootReadCapacity16 (UsbMass);\r
548 if (!EFI_ERROR (Status)) {\r
549 UsbMass->Cdb16Byte = TRUE;\r
550 }\r
551 }\r
552\r
553 return Status;\r
554}\r
555\r
556/**\r
557 Retrieves SCSI mode sense information via MODE SENSE(6) command.\r
558\r
559 @param UsbMass The device whose sense data is requested.\r
560\r
561 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.\r
562 @retval Other Command execution failed.\r
563\r
564**/\r
565EFI_STATUS\r
566UsbScsiModeSense (\r
567 IN USB_MASS_DEVICE *UsbMass\r
568 )\r
569{\r
570 EFI_STATUS Status;\r
571 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;\r
572 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;\r
573 EFI_BLOCK_IO_MEDIA *Media;\r
574\r
575 Media = &UsbMass->BlockIoMedia;\r
576\r
577 ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));\r
578 ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));\r
579\r
580 //\r
581 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec\r
582 //\r
583 ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;\r
584 ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);\r
585 ModeSenseCmd.PageCode = 0x3F;\r
586 ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);\r
587\r
588 Status = UsbBootExecCmdWithRetry (\r
589 UsbMass,\r
590 &ModeSenseCmd,\r
591 (UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD),\r
592 EfiUsbDataIn,\r
593 &ModeParaHeader,\r
594 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),\r
595 USB_BOOT_GENERAL_CMD_TIMEOUT\r
596 );\r
597\r
598 //\r
599 // Format of device-specific parameter byte of the mode parameter header is defined in\r
600 // Section 8.2.10 of SCSI-2 Spec.\r
601 // BIT7 of this byte is indicates whether the medium is write protected.\r
602 //\r
603 if (!EFI_ERROR (Status)) {\r
604 Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);\r
605 }\r
606\r
607 return Status;\r
608}\r
609\r
610\r
611/**\r
612 Get the parameters for the USB mass storage media.\r
613\r
614 This function get the parameters for the USB mass storage media,\r
615 It is used both to initialize the media during the Start() phase\r
616 of Driver Binding Protocol and to re-initialize it when the media is\r
617 changed. Althought the RemoveableMedia is unlikely to change,\r
618 it is also included here.\r
619\r
620 @param UsbMass The device to retrieve disk gemotric.\r
621\r
622 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.\r
623 @retval Other Failed to get the parameters.\r
624\r
625**/\r
626EFI_STATUS\r
627UsbBootGetParams (\r
628 IN USB_MASS_DEVICE *UsbMass\r
629 )\r
630{\r
631 EFI_BLOCK_IO_MEDIA *Media;\r
632 EFI_STATUS Status;\r
633 UINT8 CmdSet;\r
634\r
635 Media = &(UsbMass->BlockIoMedia);\r
636 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;\r
637\r
638 Status = UsbBootInquiry (UsbMass);\r
639 if (EFI_ERROR (Status)) {\r
640 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));\r
641 return Status;\r
642 }\r
643\r
644 //\r
645 // According to USB Mass Storage Specification for Bootability, only following\r
646 // 4 Peripheral Device Types are in spec.\r
647 //\r
648 if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) &&\r
649 (UsbMass->Pdt != USB_PDT_CDROM) &&\r
650 (UsbMass->Pdt != USB_PDT_OPTICAL) &&\r
651 (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {\r
652 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));\r
653 return EFI_UNSUPPORTED;\r
654 }\r
655\r
656 //\r
657 // Don't use the Removable bit in inquiry data to test whether the media\r
658 // is removable because many flash disks wrongly set this bit.\r
659 //\r
660 if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {\r
661 //\r
662 // CD-Rom device and Non-CD optical device\r
663 //\r
664 UsbMass->OpticalStorage = TRUE;\r
665 //\r
666 // Default value 2048 Bytes, in case no media present at first time\r
667 //\r
668 Media->BlockSize = 0x0800;\r
669 }\r
670\r
671 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {\r
672 //\r
673 // ModeSense is required for the device with PDT of 0x00/0x07/0x0E,\r
674 // which is from [MassStorageBootabilitySpec-Page7].\r
675 // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI\r
676 // could get the information of WriteProtected.\r
677 // Since not all device support this command, so skip if fail.\r
678 //\r
679 UsbScsiModeSense (UsbMass);\r
680 }\r
681\r
682 return UsbBootReadCapacity (UsbMass);\r
683}\r
684\r
685\r
686/**\r
687 Detect whether the removable media is present and whether it has changed.\r
688\r
689 @param UsbMass The device to check.\r
690\r
691 @retval EFI_SUCCESS The media status is successfully checked.\r
692 @retval Other Failed to detect media.\r
693\r
694**/\r
695EFI_STATUS\r
696UsbBootDetectMedia (\r
697 IN USB_MASS_DEVICE *UsbMass\r
698 )\r
699{\r
700 EFI_BLOCK_IO_MEDIA OldMedia;\r
701 EFI_BLOCK_IO_MEDIA *Media;\r
702 UINT8 CmdSet;\r
703 EFI_TPL OldTpl;\r
704 EFI_STATUS Status;\r
705\r
706 Media = &UsbMass->BlockIoMedia;\r
707\r
708 CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));\r
709\r
710 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;\r
711\r
712 Status = UsbBootIsUnitReady (UsbMass);\r
713 if (EFI_ERROR (Status)) {\r
714 goto ON_ERROR;\r
715 }\r
716\r
717 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {\r
718 //\r
719 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,\r
720 // according to Section 4 of USB Mass Storage Specification for Bootability.\r
721 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI\r
722 // could get the information of Write Protected.\r
723 // Since not all device support this command, skip if fail.\r
724 //\r
725 UsbScsiModeSense (UsbMass);\r
726 }\r
727\r
728 Status = UsbBootReadCapacity (UsbMass);\r
729 if (EFI_ERROR (Status)) {\r
730 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));\r
731 goto ON_ERROR;\r
732 }\r
733\r
734 return EFI_SUCCESS;\r
735\r
736ON_ERROR:\r
737 //\r
738 // Detect whether it is necessary to reinstall the Block I/O Protocol.\r
739 //\r
740 // MediaId may change in RequestSense for MediaChanged\r
741 // MediaPresent may change in RequestSense for NoMedia\r
742 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged\r
743 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity\r
744 //\r
745 if ((Media->MediaId != OldMedia.MediaId) ||\r
746 (Media->MediaPresent != OldMedia.MediaPresent) ||\r
747 (Media->ReadOnly != OldMedia.ReadOnly) ||\r
748 (Media->BlockSize != OldMedia.BlockSize) ||\r
749 (Media->LastBlock != OldMedia.LastBlock)) {\r
750\r
751 //\r
752 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.\r
753 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().\r
754 //\r
755 OldTpl = EfiGetCurrentTpl ();\r
756 gBS->RestoreTPL (TPL_CALLBACK);\r
757\r
758 gBS->ReinstallProtocolInterface (\r
759 UsbMass->Controller,\r
760 &gEfiBlockIoProtocolGuid,\r
761 &UsbMass->BlockIo,\r
762 &UsbMass->BlockIo\r
763 );\r
764\r
765 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);\r
766 gBS->RaiseTPL (OldTpl);\r
767\r
768 //\r
769 // Update MediaId after reinstalling Block I/O Protocol.\r
770 //\r
771 if (Media->MediaPresent != OldMedia.MediaPresent) {\r
772 if (Media->MediaPresent) {\r
773 Media->MediaId = 1;\r
774 } else {\r
775 Media->MediaId = 0;\r
776 }\r
777 }\r
778\r
779 if ((Media->ReadOnly != OldMedia.ReadOnly) ||\r
780 (Media->BlockSize != OldMedia.BlockSize) ||\r
781 (Media->LastBlock != OldMedia.LastBlock)) {\r
782 Media->MediaId++;\r
783 }\r
784 }\r
785\r
786 return Status;\r
787}\r
788\r
789\r
790/**\r
791 Read some blocks from the device.\r
792\r
793 @param UsbMass The USB mass storage device to read from\r
794 @param Lba The start block number\r
795 @param TotalBlock Total block number to read\r
796 @param Buffer The buffer to read to\r
797\r
798 @retval EFI_SUCCESS Data are read into the buffer\r
799 @retval Others Failed to read all the data\r
800\r
801**/\r
802EFI_STATUS\r
803UsbBootReadBlocks (\r
804 IN USB_MASS_DEVICE *UsbMass,\r
805 IN UINT32 Lba,\r
806 IN UINTN TotalBlock,\r
807 OUT UINT8 *Buffer\r
808 )\r
809{\r
810 USB_BOOT_READ10_CMD ReadCmd;\r
811 EFI_STATUS Status;\r
812 UINT16 Count;\r
813 UINT32 BlockSize;\r
814 UINT32 ByteSize;\r
815 UINT32 Timeout;\r
816\r
817 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
818 Status = EFI_SUCCESS;\r
819\r
820 while (TotalBlock > 0) {\r
821 //\r
822 // Split the total blocks into smaller pieces to ease the pressure\r
823 // on the device. We must split the total block because the READ10\r
824 // command only has 16 bit transfer length (in the unit of block).\r
825 //\r
826 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
827 ByteSize = (UINT32)Count * BlockSize;\r
828\r
829 //\r
830 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
831 //\r
832 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
833\r
834 //\r
835 // Fill in the command then execute\r
836 //\r
837 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));\r
838\r
839 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;\r
840 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
841 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));\r
842 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));\r
843\r
844 Status = UsbBootExecCmdWithRetry (\r
845 UsbMass,\r
846 &ReadCmd,\r
847 (UINT8) sizeof (USB_BOOT_READ10_CMD),\r
848 EfiUsbDataIn,\r
849 Buffer,\r
850 ByteSize,\r
851 Timeout\r
852 );\r
853 if (EFI_ERROR (Status)) {\r
854 return Status;\r
855 }\r
856 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));\r
857 Lba += Count;\r
858 Buffer += Count * BlockSize;\r
859 TotalBlock -= Count;\r
860 }\r
861\r
862 return Status;\r
863}\r
864\r
865\r
866/**\r
867 Write some blocks to the device.\r
868\r
869 @param UsbMass The USB mass storage device to write to\r
870 @param Lba The start block number\r
871 @param TotalBlock Total block number to write\r
872 @param Buffer Pointer to the source buffer for the data.\r
873\r
874 @retval EFI_SUCCESS Data are written into the buffer\r
875 @retval Others Failed to write all the data\r
876\r
877**/\r
878EFI_STATUS\r
879UsbBootWriteBlocks (\r
880 IN USB_MASS_DEVICE *UsbMass,\r
881 IN UINT32 Lba,\r
882 IN UINTN TotalBlock,\r
883 IN UINT8 *Buffer\r
884 )\r
885{\r
886 USB_BOOT_WRITE10_CMD WriteCmd;\r
887 EFI_STATUS Status;\r
888 UINT16 Count;\r
889 UINT32 BlockSize;\r
890 UINT32 ByteSize;\r
891 UINT32 Timeout;\r
892\r
893 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
894 Status = EFI_SUCCESS;\r
895\r
896 while (TotalBlock > 0) {\r
897 //\r
898 // Split the total blocks into smaller pieces to ease the pressure\r
899 // on the device. We must split the total block because the WRITE10\r
900 // command only has 16 bit transfer length (in the unit of block).\r
901 //\r
902 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
903 ByteSize = (UINT32)Count * BlockSize;\r
904\r
905 //\r
906 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
907 //\r
908 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
909\r
910 //\r
911 // Fill in the write10 command block\r
912 //\r
913 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));\r
914\r
915 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;\r
916 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
917 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));\r
918 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));\r
919\r
920 Status = UsbBootExecCmdWithRetry (\r
921 UsbMass,\r
922 &WriteCmd,\r
923 (UINT8) sizeof (USB_BOOT_WRITE10_CMD),\r
924 EfiUsbDataOut,\r
925 Buffer,\r
926 ByteSize,\r
927 Timeout\r
928 );\r
929 if (EFI_ERROR (Status)) {\r
930 return Status;\r
931 }\r
932 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));\r
933\r
934 Lba += Count;\r
935 Buffer += Count * BlockSize;\r
936 TotalBlock -= Count;\r
937 }\r
938\r
939 return Status;\r
940}\r
941\r
942/**\r
943 Read some blocks from the device by SCSI 16 byte cmd.\r
944\r
945 @param UsbMass The USB mass storage device to read from\r
946 @param Lba The start block number\r
947 @param TotalBlock Total block number to read\r
948 @param Buffer The buffer to read to\r
949\r
950 @retval EFI_SUCCESS Data are read into the buffer\r
951 @retval Others Failed to read all the data\r
952\r
953**/\r
954EFI_STATUS\r
955UsbBootReadBlocks16 (\r
956 IN USB_MASS_DEVICE *UsbMass,\r
957 IN UINT64 Lba,\r
958 IN UINTN TotalBlock,\r
959 OUT UINT8 *Buffer\r
960 )\r
961{\r
962 UINT8 ReadCmd[16];\r
963 EFI_STATUS Status;\r
964 UINT16 Count;\r
965 UINT32 BlockSize;\r
966 UINT32 ByteSize;\r
967 UINT32 Timeout;\r
968\r
969 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
970 Status = EFI_SUCCESS;\r
971\r
972 while (TotalBlock > 0) {\r
973 //\r
974 // Split the total blocks into smaller pieces.\r
975 //\r
976 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
977 ByteSize = (UINT32)Count * BlockSize;\r
978\r
979 //\r
980 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
981 //\r
982 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
983\r
984 //\r
985 // Fill in the command then execute\r
986 //\r
987 ZeroMem (ReadCmd, sizeof (ReadCmd));\r
988\r
989 ReadCmd[0] = EFI_SCSI_OP_READ16;\r
990 ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));\r
991 WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba));\r
992 WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count));\r
993\r
994 Status = UsbBootExecCmdWithRetry (\r
995 UsbMass,\r
996 ReadCmd,\r
997 (UINT8) sizeof (ReadCmd),\r
998 EfiUsbDataIn,\r
999 Buffer,\r
1000 ByteSize,\r
1001 Timeout\r
1002 );\r
1003 if (EFI_ERROR (Status)) {\r
1004 return Status;\r
1005 }\r
1006 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));\r
1007 Lba += Count;\r
1008 Buffer += Count * BlockSize;\r
1009 TotalBlock -= Count;\r
1010 }\r
1011\r
1012 return Status;\r
1013}\r
1014\r
1015\r
1016/**\r
1017 Write some blocks to the device by SCSI 16 byte cmd.\r
1018\r
1019 @param UsbMass The USB mass storage device to write to\r
1020 @param Lba The start block number\r
1021 @param TotalBlock Total block number to write\r
1022 @param Buffer Pointer to the source buffer for the data.\r
1023\r
1024 @retval EFI_SUCCESS Data are written into the buffer\r
1025 @retval Others Failed to write all the data\r
1026\r
1027**/\r
1028EFI_STATUS\r
1029UsbBootWriteBlocks16 (\r
1030 IN USB_MASS_DEVICE *UsbMass,\r
1031 IN UINT64 Lba,\r
1032 IN UINTN TotalBlock,\r
1033 IN UINT8 *Buffer\r
1034 )\r
1035{\r
1036 UINT8 WriteCmd[16];\r
1037 EFI_STATUS Status;\r
1038 UINT16 Count;\r
1039 UINT32 BlockSize;\r
1040 UINT32 ByteSize;\r
1041 UINT32 Timeout;\r
1042\r
1043 BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
1044 Status = EFI_SUCCESS;\r
1045\r
1046 while (TotalBlock > 0) {\r
1047 //\r
1048 // Split the total blocks into smaller pieces.\r
1049 //\r
1050 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r
1051 ByteSize = (UINT32)Count * BlockSize;\r
1052\r
1053 //\r
1054 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
1055 //\r
1056 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
1057\r
1058 //\r
1059 // Fill in the write16 command block\r
1060 //\r
1061 ZeroMem (WriteCmd, sizeof (WriteCmd));\r
1062\r
1063 WriteCmd[0] = EFI_SCSI_OP_WRITE16;\r
1064 WriteCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));\r
1065 WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba));\r
1066 WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count));\r
1067\r
1068 Status = UsbBootExecCmdWithRetry (\r
1069 UsbMass,\r
1070 WriteCmd,\r
1071 (UINT8) sizeof (WriteCmd),\r
1072 EfiUsbDataOut,\r
1073 Buffer,\r
1074 ByteSize,\r
1075 Timeout\r
1076 );\r
1077 if (EFI_ERROR (Status)) {\r
1078 return Status;\r
1079 }\r
1080 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));\r
1081 Lba += Count;\r
1082 Buffer += Count * BlockSize;\r
1083 TotalBlock -= Count;\r
1084 }\r
1085\r
1086 return Status;\r
1087}\r
1088\r
1089/**\r
1090 Use the USB clear feature control transfer to clear the endpoint stall condition.\r
1091\r
1092 @param UsbIo The USB I/O Protocol instance\r
1093 @param EndpointAddr The endpoint to clear stall for\r
1094\r
1095 @retval EFI_SUCCESS The endpoint stall condition is cleared.\r
1096 @retval Others Failed to clear the endpoint stall condition.\r
1097\r
1098**/\r
1099EFI_STATUS\r
1100UsbClearEndpointStall (\r
1101 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
1102 IN UINT8 EndpointAddr\r
1103 )\r
1104{\r
1105 EFI_USB_DEVICE_REQUEST Request;\r
1106 EFI_STATUS Status;\r
1107 UINT32 CmdResult;\r
1108 UINT32 Timeout;\r
1109\r
1110 Request.RequestType = 0x02;\r
1111 Request.Request = USB_REQ_CLEAR_FEATURE;\r
1112 Request.Value = USB_FEATURE_ENDPOINT_HALT;\r
1113 Request.Index = EndpointAddr;\r
1114 Request.Length = 0;\r
1115 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;\r
1116\r
1117 Status = UsbIo->UsbControlTransfer (\r
1118 UsbIo,\r
1119 &Request,\r
1120 EfiUsbNoData,\r
1121 Timeout,\r
1122 NULL,\r
1123 0,\r
1124 &CmdResult\r
1125 );\r
1126\r
1127 return Status;\r
1128}\r