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