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