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