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