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