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