]>
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 |
1ccdbf2a | 5 | Copyright (c) 2007 - 2010, Intel Corporation\r |
e237e7ae | 6 | All rights reserved. This program and the accompanying materials\r |
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 |
cc5166ff | 16 | #include "UsbMassImpl.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 |
e237e7ae | 53 | SenseCmd.AllocLen = sizeof (USB_BOOT_REQUEST_SENSE_DATA);\r |
54 | \r | |
55 | Status = Transport->ExecCommand (\r | |
56 | UsbMass->Context,\r | |
57 | &SenseCmd,\r | |
58 | sizeof (USB_BOOT_REQUEST_SENSE_CMD),\r | |
59 | EfiUsbDataIn,\r | |
60 | &SenseData,\r | |
61 | sizeof (USB_BOOT_REQUEST_SENSE_DATA),\r | |
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 | |
186 | //\r | |
d80ed2a7 | 187 | // If ExecCommand() returns no error and CmdResult is success,\r |
188 | // then the commnad transfer is successful.\r | |
e237e7ae | 189 | //\r |
d80ed2a7 | 190 | if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {\r |
e237e7ae | 191 | return EFI_SUCCESS;\r |
192 | }\r | |
193 | \r | |
1c619535 | 194 | DEBUG ((EFI_D_INFO, "UsbBootExecCmd: Fail to Exec 0x%x Cmd /w %r\n",\r |
50fa1b3a | 195 | *(UINT8 *)Cmd ,Status));\r |
196 | \r | |
d80ed2a7 | 197 | //\r |
198 | // If command execution failed, then retrieve error info via sense request.\r | |
199 | //\r | |
e237e7ae | 200 | return UsbBootRequestSense (UsbMass);\r |
201 | }\r | |
202 | \r | |
203 | \r | |
204 | /**\r | |
d80ed2a7 | 205 | Execute the USB mass storage bootability commands with retrial.\r |
e237e7ae | 206 | \r |
d80ed2a7 | 207 | This function executes USB mass storage bootability commands.\r |
208 | If the device isn't ready, wait for it. If the device is ready\r | |
209 | and error occurs, retry the command again until it exceeds the\r | |
210 | limit of retrial times.\r | |
211 | \r | |
e237e7ae | 212 | @param UsbMass The device to issue commands to\r |
213 | @param Cmd The command to execute\r | |
214 | @param CmdLen The length of the command\r | |
215 | @param DataDir The direction of data transfer\r | |
216 | @param Data The buffer to hold the data\r | |
217 | @param DataLen The length of expected data\r | |
cc5166ff | 218 | @param Timeout The timeout used to transfer\r |
e237e7ae | 219 | \r |
d80ed2a7 | 220 | @retval EFI_SUCCESS The command is executed successfully.\r |
221 | @retval EFI_MEDIA_CHANGED The device media has been changed.\r | |
222 | @retval Others Command execution failed after retrial.\r | |
e237e7ae | 223 | \r |
224 | **/\r | |
e237e7ae | 225 | EFI_STATUS\r |
226 | UsbBootExecCmdWithRetry (\r | |
227 | IN USB_MASS_DEVICE *UsbMass,\r | |
228 | IN VOID *Cmd,\r | |
229 | IN UINT8 CmdLen,\r | |
230 | IN EFI_USB_DATA_DIRECTION DataDir,\r | |
231 | IN VOID *Data,\r | |
232 | IN UINT32 DataLen,\r | |
233 | IN UINT32 Timeout\r | |
234 | )\r | |
235 | {\r | |
236 | EFI_STATUS Status;\r | |
d80ed2a7 | 237 | UINTN Retry;\r |
efe9186f | 238 | UINT8 Terminate;\r |
e237e7ae | 239 | \r |
e237e7ae | 240 | Status = EFI_SUCCESS;\r |
241 | \r | |
efe9186f | 242 | for (Retry = 0, Terminate = 0; Retry < USB_BOOT_COMMAND_RETRY; Retry++) {\r |
e237e7ae | 243 | Status = UsbBootExecCmd (\r |
244 | UsbMass,\r | |
245 | Cmd,\r | |
246 | CmdLen,\r | |
247 | DataDir,\r | |
248 | Data,\r | |
249 | DataLen,\r | |
1c619535 | 250 | Timeout\r |
e237e7ae | 251 | );\r |
d80ed2a7 | 252 | if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED) {\r |
e237e7ae | 253 | break;\r |
254 | }\r | |
255 | //\r | |
d80ed2a7 | 256 | // If the device isn't ready, just wait for it without limit on retrial times.\r |
e237e7ae | 257 | //\r |
efe9186f | 258 | if (Status == EFI_NOT_READY && Terminate < 3) {\r |
d80ed2a7 | 259 | Retry = 0;\r |
efe9186f | 260 | Terminate++;\r |
e237e7ae | 261 | }\r |
262 | }\r | |
263 | \r | |
264 | return Status;\r | |
265 | }\r | |
266 | \r | |
267 | \r | |
e237e7ae | 268 | /**\r |
d80ed2a7 | 269 | Execute TEST UNIT READY command to check if the device is ready.\r |
e237e7ae | 270 | \r |
271 | @param UsbMass The device to test\r | |
272 | \r | |
d80ed2a7 | 273 | @retval EFI_SUCCESS The device is ready.\r |
e237e7ae | 274 | @retval Others Device not ready.\r |
275 | \r | |
276 | **/\r | |
277 | EFI_STATUS\r | |
278 | UsbBootIsUnitReady (\r | |
279 | IN USB_MASS_DEVICE *UsbMass\r | |
280 | )\r | |
281 | {\r | |
282 | USB_BOOT_TEST_UNIT_READY_CMD TestCmd;\r | |
283 | \r | |
284 | ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD));\r | |
285 | \r | |
286 | TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE;\r | |
c52fa98c | 287 | TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r |
e237e7ae | 288 | \r |
289 | return UsbBootExecCmdWithRetry (\r | |
290 | UsbMass,\r | |
291 | &TestCmd,\r | |
292 | sizeof (USB_BOOT_TEST_UNIT_READY_CMD),\r | |
293 | EfiUsbNoData,\r | |
294 | NULL,\r | |
295 | 0,\r | |
296 | USB_BOOT_GENERAL_CMD_TIMEOUT\r | |
297 | );\r | |
298 | }\r | |
299 | \r | |
300 | \r | |
301 | /**\r | |
d80ed2a7 | 302 | Execute INQUIRY Command to request information regarding parameters of\r |
303 | the device be sent to the host computer.\r | |
e237e7ae | 304 | \r |
d80ed2a7 | 305 | @param UsbMass The device to inquire.\r |
e237e7ae | 306 | \r |
d80ed2a7 | 307 | @retval EFI_SUCCESS INQUIRY Command is executed successfully.\r |
308 | @retval Others INQUIRY Command is not executed successfully.\r | |
e237e7ae | 309 | \r |
310 | **/\r | |
311 | EFI_STATUS\r | |
312 | UsbBootInquiry (\r | |
313 | IN USB_MASS_DEVICE *UsbMass\r | |
314 | )\r | |
315 | {\r | |
316 | USB_BOOT_INQUIRY_CMD InquiryCmd;\r | |
317 | USB_BOOT_INQUIRY_DATA InquiryData;\r | |
318 | EFI_BLOCK_IO_MEDIA *Media;\r | |
319 | EFI_STATUS Status;\r | |
320 | \r | |
321 | Media = &(UsbMass->BlockIoMedia);\r | |
322 | \r | |
e237e7ae | 323 | ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));\r |
324 | ZeroMem (&InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));\r | |
325 | \r | |
326 | InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE;\r | |
c52fa98c | 327 | InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r |
e237e7ae | 328 | InquiryCmd.AllocLen = sizeof (InquiryData);\r |
329 | \r | |
330 | Status = UsbBootExecCmdWithRetry (\r | |
331 | UsbMass,\r | |
332 | &InquiryCmd,\r | |
333 | sizeof (USB_BOOT_INQUIRY_CMD),\r | |
334 | EfiUsbDataIn,\r | |
335 | &InquiryData,\r | |
336 | sizeof (USB_BOOT_INQUIRY_DATA),\r | |
50fa1b3a | 337 | USB_BOOT_GENERAL_CMD_TIMEOUT\r |
e237e7ae | 338 | );\r |
339 | if (EFI_ERROR (Status)) {\r | |
340 | return Status;\r | |
341 | }\r | |
342 | \r | |
d80ed2a7 | 343 | //\r |
344 | // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit\r | |
345 | // from the inquiry data.\r | |
346 | //\r | |
c52fa98c | 347 | UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (InquiryData.Pdt));\r |
348 | Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (InquiryData.Removable));\r | |
e237e7ae | 349 | //\r |
d80ed2a7 | 350 | // Set block size to the default value of 512 Bytes, in case no media is present at first time.\r |
e237e7ae | 351 | //\r |
352 | Media->BlockSize = 0x0200;\r | |
353 | \r | |
354 | return Status;\r | |
355 | }\r | |
356 | \r | |
357 | \r | |
358 | /**\r | |
d80ed2a7 | 359 | Execute READ CAPACITY command to request information regarding\r |
360 | the capacity of the installed medium of the device.\r | |
361 | \r | |
362 | This function executes READ CAPACITY command to get the capacity\r | |
363 | of the USB mass storage media, including the presence, block size,\r | |
364 | and last block number.\r | |
e237e7ae | 365 | \r |
366 | @param UsbMass The device to retireve disk gemotric.\r | |
367 | \r | |
d80ed2a7 | 368 | @retval EFI_SUCCESS The disk geometry is successfully retrieved.\r |
369 | @retval EFI_NOT_READY The returned block size is zero.\r | |
370 | @retval Other READ CAPACITY command execution failed.\r | |
cc5166ff | 371 | \r |
e237e7ae | 372 | **/\r |
373 | EFI_STATUS\r | |
374 | UsbBootReadCapacity (\r | |
375 | IN USB_MASS_DEVICE *UsbMass\r | |
376 | )\r | |
377 | {\r | |
378 | USB_BOOT_READ_CAPACITY_CMD CapacityCmd;\r | |
379 | USB_BOOT_READ_CAPACITY_DATA CapacityData;\r | |
380 | EFI_BLOCK_IO_MEDIA *Media;\r | |
381 | EFI_STATUS Status;\r | |
88e349f1 | 382 | UINT32 BlockSize;\r |
e237e7ae | 383 | \r |
384 | Media = &UsbMass->BlockIoMedia;\r | |
385 | \r | |
e237e7ae | 386 | ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));\r |
387 | ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));\r | |
388 | \r | |
389 | CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;\r | |
c52fa98c | 390 | CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r |
e237e7ae | 391 | \r |
392 | Status = UsbBootExecCmdWithRetry (\r | |
393 | UsbMass,\r | |
394 | &CapacityCmd,\r | |
395 | sizeof (USB_BOOT_READ_CAPACITY_CMD),\r | |
396 | EfiUsbDataIn,\r | |
397 | &CapacityData,\r | |
398 | sizeof (USB_BOOT_READ_CAPACITY_DATA),\r | |
399 | USB_BOOT_GENERAL_CMD_TIMEOUT\r | |
400 | );\r | |
401 | if (EFI_ERROR (Status)) {\r | |
402 | return Status;\r | |
403 | }\r | |
404 | \r | |
d80ed2a7 | 405 | //\r |
406 | // Get the information on media presence, block size, and last block number\r | |
407 | // from READ CAPACITY data.\r | |
408 | //\r | |
e237e7ae | 409 | Media->MediaPresent = TRUE;\r |
d80ed2a7 | 410 | Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));\r |
e237e7ae | 411 | \r |
88e349f1 | 412 | BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));\r |
413 | if (BlockSize == 0) {\r | |
efe9186f | 414 | //\r |
415 | // Get sense data \r | |
416 | //\r | |
417 | return UsbBootRequestSense (UsbMass);\r | |
88e349f1 | 418 | } else {\r |
419 | Media->BlockSize = BlockSize;\r | |
50fa1b3a | 420 | }\r |
421 | \r | |
1c619535 | 422 | DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n",\r |
50fa1b3a | 423 | Media->LastBlock, Media->BlockSize));\r |
e237e7ae | 424 | \r |
efe9186f | 425 | return Status;\r |
e237e7ae | 426 | }\r |
427 | \r | |
e237e7ae | 428 | /**\r |
d80ed2a7 | 429 | Retrieves SCSI mode sense information via MODE SENSE(6) command.\r |
e237e7ae | 430 | \r |
d80ed2a7 | 431 | @param UsbMass The device whose sense data is requested.\r |
e237e7ae | 432 | \r |
d80ed2a7 | 433 | @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.\r |
434 | @retval Other Command execution failed.\r | |
e237e7ae | 435 | \r |
436 | **/\r | |
437 | EFI_STATUS\r | |
50fa1b3a | 438 | UsbScsiModeSense (\r |
e237e7ae | 439 | IN USB_MASS_DEVICE *UsbMass\r |
440 | )\r | |
441 | {\r | |
ca12415a | 442 | EFI_STATUS Status;\r |
50fa1b3a | 443 | USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;\r |
444 | USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;\r | |
445 | EFI_BLOCK_IO_MEDIA *Media;\r | |
446 | \r | |
ca12415a | 447 | Media = &UsbMass->BlockIoMedia;\r |
e237e7ae | 448 | \r |
50fa1b3a | 449 | ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));\r |
450 | ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));\r | |
e237e7ae | 451 | \r |
452 | //\r | |
d80ed2a7 | 453 | // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec\r |
e237e7ae | 454 | //\r |
50fa1b3a | 455 | ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;\r |
687a2e5f | 456 | ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);\r |
50fa1b3a | 457 | ModeSenseCmd.PageCode = 0x3F;\r |
458 | ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);\r | |
e237e7ae | 459 | \r |
460 | Status = UsbBootExecCmdWithRetry (\r | |
461 | UsbMass,\r | |
462 | &ModeSenseCmd,\r | |
50fa1b3a | 463 | sizeof (USB_SCSI_MODE_SENSE6_CMD),\r |
e237e7ae | 464 | EfiUsbDataIn,\r |
465 | &ModeParaHeader,\r | |
50fa1b3a | 466 | sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),\r |
e237e7ae | 467 | USB_BOOT_GENERAL_CMD_TIMEOUT\r |
468 | );\r | |
50fa1b3a | 469 | \r |
e237e7ae | 470 | //\r |
d80ed2a7 | 471 | // Format of device-specific parameter byte of the mode parameter header is defined in\r |
472 | // Section 8.2.10 of SCSI-2 Spec.\r | |
473 | // BIT7 of this byte is indicates whether the medium is write protected.\r | |
e237e7ae | 474 | //\r |
50fa1b3a | 475 | if (!EFI_ERROR (Status)) {\r |
d80ed2a7 | 476 | Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);\r |
50fa1b3a | 477 | }\r |
e237e7ae | 478 | \r |
479 | return Status;\r | |
480 | }\r | |
481 | \r | |
482 | \r | |
483 | /**\r | |
d80ed2a7 | 484 | Get the parameters for the USB mass storage media.\r |
485 | \r | |
486 | This function get the parameters for the USB mass storage media,\r | |
487 | It is used both to initialize the media during the Start() phase\r | |
488 | of Driver Binding Protocol and to re-initialize it when the media is\r | |
e237e7ae | 489 | changed. Althought the RemoveableMedia is unlikely to change,\r |
d80ed2a7 | 490 | it is also included here.\r |
e237e7ae | 491 | \r |
d80ed2a7 | 492 | @param UsbMass The device to retrieve disk gemotric.\r |
e237e7ae | 493 | \r |
494 | @retval EFI_SUCCESS The disk gemotric is successfully retrieved.\r | |
d80ed2a7 | 495 | @retval Other Failed to get the parameters.\r |
e237e7ae | 496 | \r |
497 | **/\r | |
498 | EFI_STATUS\r | |
499 | UsbBootGetParams (\r | |
500 | IN USB_MASS_DEVICE *UsbMass\r | |
501 | )\r | |
502 | {\r | |
503 | EFI_BLOCK_IO_MEDIA *Media;\r | |
504 | EFI_STATUS Status;\r | |
50fa1b3a | 505 | UINT8 CmdSet;\r |
506 | \r | |
ca12415a | 507 | Media = &(UsbMass->BlockIoMedia);\r |
50fa1b3a | 508 | CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;\r |
e237e7ae | 509 | \r |
510 | Status = UsbBootInquiry (UsbMass);\r | |
511 | if (EFI_ERROR (Status)) {\r | |
1c619535 | 512 | DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));\r |
e237e7ae | 513 | return Status;\r |
514 | }\r | |
515 | \r | |
e237e7ae | 516 | //\r |
d80ed2a7 | 517 | // Don't use the Removable bit in inquiry data to test whether the media\r |
e237e7ae | 518 | // is removable because many flash disks wrongly set this bit.\r |
519 | //\r | |
520 | if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {\r | |
521 | //\r | |
50fa1b3a | 522 | // CD-Rom device and Non-CD optical device\r |
e237e7ae | 523 | //\r |
524 | UsbMass->OpticalStorage = TRUE;\r | |
525 | //\r | |
526 | // Default value 2048 Bytes, in case no media present at first time\r | |
527 | //\r | |
528 | Media->BlockSize = 0x0800;\r | |
50fa1b3a | 529 | }\r |
530 | \r | |
531 | if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {\r | |
e237e7ae | 532 | //\r |
50fa1b3a | 533 | // ModeSense is required for the device with PDT of 0x00/0x07/0x0E,\r |
534 | // which is from [MassStorageBootabilitySpec-Page7].\r | |
535 | // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI\r | |
536 | // could get the information of WriteProtected.\r | |
537 | // Since not all device support this command, so skip if fail.\r | |
e237e7ae | 538 | //\r |
50fa1b3a | 539 | UsbScsiModeSense (UsbMass);\r |
e237e7ae | 540 | }\r |
541 | \r | |
542 | return UsbBootReadCapacity (UsbMass);\r | |
543 | }\r | |
544 | \r | |
545 | \r | |
546 | /**\r | |
547 | Detect whether the removable media is present and whether it has changed.\r | |
e237e7ae | 548 | \r |
d80ed2a7 | 549 | @param UsbMass The device to check.\r |
e237e7ae | 550 | \r |
d80ed2a7 | 551 | @retval EFI_SUCCESS The media status is successfully checked.\r |
552 | @retval Other Failed to detect media.\r | |
e237e7ae | 553 | \r |
554 | **/\r | |
555 | EFI_STATUS\r | |
556 | UsbBootDetectMedia (\r | |
557 | IN USB_MASS_DEVICE *UsbMass\r | |
558 | )\r | |
559 | {\r | |
560 | EFI_BLOCK_IO_MEDIA OldMedia;\r | |
561 | EFI_BLOCK_IO_MEDIA *Media;\r | |
50fa1b3a | 562 | UINT8 CmdSet;\r |
563 | EFI_TPL OldTpl;\r | |
e237e7ae | 564 | EFI_STATUS Status;\r |
565 | \r | |
566 | Media = &UsbMass->BlockIoMedia;\r | |
e61d30b0 | 567 | \r |
d80ed2a7 | 568 | CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));\r |
e237e7ae | 569 | \r |
50fa1b3a | 570 | CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;\r |
571 | \r | |
e237e7ae | 572 | Status = UsbBootIsUnitReady (UsbMass);\r |
50fa1b3a | 573 | if (EFI_ERROR (Status)) {\r |
50fa1b3a | 574 | goto ON_ERROR;\r |
e237e7ae | 575 | }\r |
50fa1b3a | 576 | \r |
577 | if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {\r | |
578 | //\r | |
d80ed2a7 | 579 | // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,\r |
580 | // according to Section 4 of USB Mass Storage Specification for Bootability.\r | |
581 | // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI\r | |
582 | // could get the information of Write Protected.\r | |
583 | // Since not all device support this command, skip if fail.\r | |
50fa1b3a | 584 | //\r |
585 | UsbScsiModeSense (UsbMass);\r | |
586 | }\r | |
587 | \r | |
588 | Status = UsbBootReadCapacity (UsbMass);\r | |
e237e7ae | 589 | if (EFI_ERROR (Status)) {\r |
1c619535 | 590 | DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));\r |
50fa1b3a | 591 | goto ON_ERROR;\r |
e237e7ae | 592 | }\r |
593 | \r | |
50fa1b3a | 594 | return EFI_SUCCESS;\r |
595 | \r | |
596 | ON_ERROR:\r | |
e237e7ae | 597 | //\r |
d80ed2a7 | 598 | // Detect whether it is necessary to reinstall the Block I/O Protocol.\r |
e237e7ae | 599 | //\r |
50fa1b3a | 600 | // MediaId may change in RequestSense for MediaChanged\r |
601 | // MediaPresent may change in RequestSense for NoMedia\r | |
602 | // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged\r | |
603 | // MediaPresent/BlockSize/LastBlock may change in ReadCapacity\r | |
604 | //\r | |
e237e7ae | 605 | if ((Media->MediaId != OldMedia.MediaId) ||\r |
606 | (Media->MediaPresent != OldMedia.MediaPresent) ||\r | |
607 | (Media->ReadOnly != OldMedia.ReadOnly) ||\r | |
608 | (Media->BlockSize != OldMedia.BlockSize) ||\r | |
609 | (Media->LastBlock != OldMedia.LastBlock)) {\r | |
50fa1b3a | 610 | \r |
d80ed2a7 | 611 | //\r |
612 | // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.\r | |
613 | // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().\r | |
614 | //\r | |
615 | OldTpl = EfiGetCurrentTpl ();\r | |
50fa1b3a | 616 | gBS->RestoreTPL (TPL_CALLBACK);\r |
617 | \r | |
e237e7ae | 618 | gBS->ReinstallProtocolInterface (\r |
619 | UsbMass->Controller,\r | |
620 | &gEfiBlockIoProtocolGuid,\r | |
621 | &UsbMass->BlockIo,\r | |
622 | &UsbMass->BlockIo\r | |
623 | );\r | |
50fa1b3a | 624 | \r |
d80ed2a7 | 625 | ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);\r |
50fa1b3a | 626 | gBS->RaiseTPL (OldTpl);\r |
627 | \r | |
e237e7ae | 628 | //\r |
d80ed2a7 | 629 | // Update MediaId after reinstalling Block I/O Protocol.\r |
e237e7ae | 630 | //\r |
50fa1b3a | 631 | if (Media->MediaPresent != OldMedia.MediaPresent) {\r |
d80ed2a7 | 632 | if (Media->MediaPresent) {\r |
50fa1b3a | 633 | Media->MediaId = 1;\r |
634 | } else {\r | |
635 | Media->MediaId = 0;\r | |
636 | }\r | |
e237e7ae | 637 | }\r |
50fa1b3a | 638 | \r |
639 | if ((Media->ReadOnly != OldMedia.ReadOnly) ||\r | |
640 | (Media->BlockSize != OldMedia.BlockSize) ||\r | |
641 | (Media->LastBlock != OldMedia.LastBlock)) {\r | |
642 | Media->MediaId++;\r | |
e237e7ae | 643 | }\r |
644 | }\r | |
645 | \r | |
646 | return Status;\r | |
647 | }\r | |
648 | \r | |
649 | \r | |
650 | /**\r | |
651 | Read some blocks from the device.\r | |
652 | \r | |
653 | @param UsbMass The USB mass storage device to read from\r | |
654 | @param Lba The start block number\r | |
655 | @param TotalBlock Total block number to read\r | |
656 | @param Buffer The buffer to read to\r | |
657 | \r | |
658 | @retval EFI_SUCCESS Data are read into the buffer\r | |
659 | @retval Others Failed to read all the data\r | |
660 | \r | |
661 | **/\r | |
662 | EFI_STATUS\r | |
663 | UsbBootReadBlocks (\r | |
664 | IN USB_MASS_DEVICE *UsbMass,\r | |
665 | IN UINT32 Lba,\r | |
666 | IN UINTN TotalBlock,\r | |
667 | OUT UINT8 *Buffer\r | |
668 | )\r | |
669 | {\r | |
670 | USB_BOOT_READ10_CMD ReadCmd;\r | |
671 | EFI_STATUS Status;\r | |
672 | UINT16 Count;\r | |
673 | UINT32 BlockSize;\r | |
674 | UINT32 ByteSize;\r | |
675 | UINT32 Timeout;\r | |
676 | \r | |
677 | BlockSize = UsbMass->BlockIoMedia.BlockSize;\r | |
678 | Status = EFI_SUCCESS;\r | |
679 | \r | |
680 | while (TotalBlock > 0) {\r | |
681 | //\r | |
682 | // Split the total blocks into smaller pieces to ease the pressure\r | |
683 | // on the device. We must split the total block because the READ10\r | |
684 | // command only has 16 bit transfer length (in the unit of block).\r | |
685 | //\r | |
686 | Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r | |
687 | ByteSize = (UINT32)Count * BlockSize;\r | |
688 | \r | |
689 | //\r | |
50fa1b3a | 690 | // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r |
e237e7ae | 691 | //\r |
50fa1b3a | 692 | Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r |
e237e7ae | 693 | \r |
694 | //\r | |
695 | // Fill in the command then execute\r | |
696 | //\r | |
697 | ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));\r | |
698 | \r | |
699 | ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;\r | |
c52fa98c | 700 | ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r |
d80ed2a7 | 701 | WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));\r |
702 | WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));\r | |
e237e7ae | 703 | \r |
704 | Status = UsbBootExecCmdWithRetry (\r | |
705 | UsbMass,\r | |
706 | &ReadCmd,\r | |
707 | sizeof (USB_BOOT_READ10_CMD),\r | |
708 | EfiUsbDataIn,\r | |
709 | Buffer,\r | |
710 | ByteSize,\r | |
711 | Timeout\r | |
712 | );\r | |
713 | if (EFI_ERROR (Status)) {\r | |
714 | return Status;\r | |
715 | }\r | |
716 | \r | |
717 | Lba += Count;\r | |
718 | Buffer += Count * BlockSize;\r | |
719 | TotalBlock -= Count;\r | |
720 | }\r | |
721 | \r | |
722 | return Status;\r | |
723 | }\r | |
724 | \r | |
725 | \r | |
726 | /**\r | |
727 | Write some blocks to the device.\r | |
728 | \r | |
729 | @param UsbMass The USB mass storage device to write to\r | |
730 | @param Lba The start block number\r | |
731 | @param TotalBlock Total block number to write\r | |
d80ed2a7 | 732 | @param Buffer Pointer to the source buffer for the data.\r |
e237e7ae | 733 | \r |
734 | @retval EFI_SUCCESS Data are written into the buffer\r | |
735 | @retval Others Failed to write all the data\r | |
736 | \r | |
737 | **/\r | |
738 | EFI_STATUS\r | |
739 | UsbBootWriteBlocks (\r | |
740 | IN USB_MASS_DEVICE *UsbMass,\r | |
741 | IN UINT32 Lba,\r | |
742 | IN UINTN TotalBlock,\r | |
d80ed2a7 | 743 | IN UINT8 *Buffer\r |
e237e7ae | 744 | )\r |
745 | {\r | |
746 | USB_BOOT_WRITE10_CMD WriteCmd;\r | |
747 | EFI_STATUS Status;\r | |
748 | UINT16 Count;\r | |
749 | UINT32 BlockSize;\r | |
750 | UINT32 ByteSize;\r | |
751 | UINT32 Timeout;\r | |
752 | \r | |
753 | BlockSize = UsbMass->BlockIoMedia.BlockSize;\r | |
754 | Status = EFI_SUCCESS;\r | |
755 | \r | |
756 | while (TotalBlock > 0) {\r | |
757 | //\r | |
758 | // Split the total blocks into smaller pieces to ease the pressure\r | |
759 | // on the device. We must split the total block because the WRITE10\r | |
760 | // command only has 16 bit transfer length (in the unit of block).\r | |
761 | //\r | |
762 | Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\r | |
763 | ByteSize = (UINT32)Count * BlockSize;\r | |
764 | \r | |
765 | //\r | |
50fa1b3a | 766 | // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r |
e237e7ae | 767 | //\r |
50fa1b3a | 768 | Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r |
e237e7ae | 769 | \r |
770 | //\r | |
771 | // Fill in the write10 command block\r | |
772 | //\r | |
773 | ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));\r | |
774 | \r | |
775 | WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;\r | |
c52fa98c | 776 | WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r |
d80ed2a7 | 777 | WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));\r |
778 | WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));\r | |
e237e7ae | 779 | \r |
780 | Status = UsbBootExecCmdWithRetry (\r | |
781 | UsbMass,\r | |
782 | &WriteCmd,\r | |
783 | sizeof (USB_BOOT_WRITE10_CMD),\r | |
784 | EfiUsbDataOut,\r | |
785 | Buffer,\r | |
786 | ByteSize,\r | |
787 | Timeout\r | |
788 | );\r | |
789 | if (EFI_ERROR (Status)) {\r | |
790 | return Status;\r | |
791 | }\r | |
792 | \r | |
793 | Lba += Count;\r | |
794 | Buffer += Count * BlockSize;\r | |
795 | TotalBlock -= Count;\r | |
796 | }\r | |
797 | \r | |
798 | return Status;\r | |
799 | }\r | |
800 | \r | |
e237e7ae | 801 | /**\r |
d80ed2a7 | 802 | Use the USB clear feature control transfer to clear the endpoint stall condition.\r |
e237e7ae | 803 | \r |
d80ed2a7 | 804 | @param UsbIo The USB I/O Protocol instance\r |
e237e7ae | 805 | @param EndpointAddr The endpoint to clear stall for\r |
806 | \r | |
d80ed2a7 | 807 | @retval EFI_SUCCESS The endpoint stall condition is cleared.\r |
808 | @retval Others Failed to clear the endpoint stall condition.\r | |
e237e7ae | 809 | \r |
810 | **/\r | |
811 | EFI_STATUS\r | |
812 | UsbClearEndpointStall (\r | |
813 | IN EFI_USB_IO_PROTOCOL *UsbIo,\r | |
814 | IN UINT8 EndpointAddr\r | |
815 | )\r | |
816 | {\r | |
817 | EFI_USB_DEVICE_REQUEST Request;\r | |
818 | EFI_STATUS Status;\r | |
819 | UINT32 CmdResult;\r | |
820 | UINT32 Timeout;\r | |
821 | \r | |
822 | Request.RequestType = 0x02;\r | |
823 | Request.Request = USB_REQ_CLEAR_FEATURE;\r | |
824 | Request.Value = USB_FEATURE_ENDPOINT_HALT;\r | |
825 | Request.Index = EndpointAddr;\r | |
826 | Request.Length = 0;\r | |
41e8ff27 | 827 | Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;\r |
e237e7ae | 828 | \r |
829 | Status = UsbIo->UsbControlTransfer (\r | |
830 | UsbIo,\r | |
831 | &Request,\r | |
832 | EfiUsbNoData,\r | |
833 | Timeout,\r | |
834 | NULL,\r | |
835 | 0,\r | |
836 | &CmdResult\r | |
837 | );\r | |
838 | \r | |
839 | return Status;\r | |
840 | }\r |