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