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