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