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