]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
613008bde76837f174b37335c5a7e47d4bdae30f
[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 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 UINT8 CmdSet;
634
635 Media = &(UsbMass->BlockIoMedia);
636 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
637
638 Status = UsbBootInquiry (UsbMass);
639 if (EFI_ERROR (Status)) {
640 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));
641 return Status;
642 }
643
644 //
645 // According to USB Mass Storage Specification for Bootability, only following
646 // 4 Peripheral Device Types are in spec.
647 //
648 if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) &&
649 (UsbMass->Pdt != USB_PDT_CDROM) &&
650 (UsbMass->Pdt != USB_PDT_OPTICAL) &&
651 (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {
652 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));
653 return EFI_UNSUPPORTED;
654 }
655
656 //
657 // Don't use the Removable bit in inquiry data to test whether the media
658 // is removable because many flash disks wrongly set this bit.
659 //
660 if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {
661 //
662 // CD-Rom device and Non-CD optical device
663 //
664 UsbMass->OpticalStorage = TRUE;
665 //
666 // Default value 2048 Bytes, in case no media present at first time
667 //
668 Media->BlockSize = 0x0800;
669 }
670
671 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
672 //
673 // ModeSense is required for the device with PDT of 0x00/0x07/0x0E,
674 // which is from [MassStorageBootabilitySpec-Page7].
675 // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI
676 // could get the information of WriteProtected.
677 // Since not all device support this command, so skip if fail.
678 //
679 UsbScsiModeSense (UsbMass);
680 }
681
682 return UsbBootReadCapacity (UsbMass);
683 }
684
685
686 /**
687 Detect whether the removable media is present and whether it has changed.
688
689 @param UsbMass The device to check.
690
691 @retval EFI_SUCCESS The media status is successfully checked.
692 @retval Other Failed to detect media.
693
694 **/
695 EFI_STATUS
696 UsbBootDetectMedia (
697 IN USB_MASS_DEVICE *UsbMass
698 )
699 {
700 EFI_BLOCK_IO_MEDIA OldMedia;
701 EFI_BLOCK_IO_MEDIA *Media;
702 UINT8 CmdSet;
703 EFI_TPL OldTpl;
704 EFI_STATUS Status;
705
706 Media = &UsbMass->BlockIoMedia;
707
708 CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));
709
710 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
711
712 Status = UsbBootIsUnitReady (UsbMass);
713 if (EFI_ERROR (Status)) {
714 goto ON_ERROR;
715 }
716
717 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
718 //
719 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
720 // according to Section 4 of USB Mass Storage Specification for Bootability.
721 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
722 // could get the information of Write Protected.
723 // Since not all device support this command, skip if fail.
724 //
725 UsbScsiModeSense (UsbMass);
726 }
727
728 Status = UsbBootReadCapacity (UsbMass);
729 if (EFI_ERROR (Status)) {
730 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));
731 goto ON_ERROR;
732 }
733
734 return EFI_SUCCESS;
735
736 ON_ERROR:
737 //
738 // Detect whether it is necessary to reinstall the Block I/O Protocol.
739 //
740 // MediaId may change in RequestSense for MediaChanged
741 // MediaPresent may change in RequestSense for NoMedia
742 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
743 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
744 //
745 if ((Media->MediaId != OldMedia.MediaId) ||
746 (Media->MediaPresent != OldMedia.MediaPresent) ||
747 (Media->ReadOnly != OldMedia.ReadOnly) ||
748 (Media->BlockSize != OldMedia.BlockSize) ||
749 (Media->LastBlock != OldMedia.LastBlock)) {
750
751 //
752 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
753 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
754 //
755 OldTpl = EfiGetCurrentTpl ();
756 gBS->RestoreTPL (TPL_CALLBACK);
757
758 gBS->ReinstallProtocolInterface (
759 UsbMass->Controller,
760 &gEfiBlockIoProtocolGuid,
761 &UsbMass->BlockIo,
762 &UsbMass->BlockIo
763 );
764
765 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
766 gBS->RaiseTPL (OldTpl);
767
768 //
769 // Update MediaId after reinstalling Block I/O Protocol.
770 //
771 if (Media->MediaPresent != OldMedia.MediaPresent) {
772 if (Media->MediaPresent) {
773 Media->MediaId = 1;
774 } else {
775 Media->MediaId = 0;
776 }
777 }
778
779 if ((Media->ReadOnly != OldMedia.ReadOnly) ||
780 (Media->BlockSize != OldMedia.BlockSize) ||
781 (Media->LastBlock != OldMedia.LastBlock)) {
782 Media->MediaId++;
783 }
784 }
785
786 return Status;
787 }
788
789
790 /**
791 Read some blocks from the device.
792
793 @param UsbMass The USB mass storage device to read from
794 @param Lba The start block number
795 @param TotalBlock Total block number to read
796 @param Buffer The buffer to read to
797
798 @retval EFI_SUCCESS Data are read into the buffer
799 @retval Others Failed to read all the data
800
801 **/
802 EFI_STATUS
803 UsbBootReadBlocks (
804 IN USB_MASS_DEVICE *UsbMass,
805 IN UINT32 Lba,
806 IN UINTN TotalBlock,
807 OUT UINT8 *Buffer
808 )
809 {
810 USB_BOOT_READ10_CMD ReadCmd;
811 EFI_STATUS Status;
812 UINT16 Count;
813 UINT32 BlockSize;
814 UINT32 ByteSize;
815 UINT32 Timeout;
816
817 BlockSize = UsbMass->BlockIoMedia.BlockSize;
818 Status = EFI_SUCCESS;
819
820 while (TotalBlock > 0) {
821 //
822 // Split the total blocks into smaller pieces to ease the pressure
823 // on the device. We must split the total block because the READ10
824 // command only has 16 bit transfer length (in the unit of block).
825 //
826 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
827 ByteSize = (UINT32)Count * BlockSize;
828
829 //
830 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
831 //
832 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
833
834 //
835 // Fill in the command then execute
836 //
837 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));
838
839 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;
840 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
841 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));
842 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));
843
844 Status = UsbBootExecCmdWithRetry (
845 UsbMass,
846 &ReadCmd,
847 (UINT8) sizeof (USB_BOOT_READ10_CMD),
848 EfiUsbDataIn,
849 Buffer,
850 ByteSize,
851 Timeout
852 );
853 if (EFI_ERROR (Status)) {
854 return Status;
855 }
856 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
857 Lba += Count;
858 Buffer += Count * BlockSize;
859 TotalBlock -= Count;
860 }
861
862 return Status;
863 }
864
865
866 /**
867 Write some blocks to the device.
868
869 @param UsbMass The USB mass storage device to write to
870 @param Lba The start block number
871 @param TotalBlock Total block number to write
872 @param Buffer Pointer to the source buffer for the data.
873
874 @retval EFI_SUCCESS Data are written into the buffer
875 @retval Others Failed to write all the data
876
877 **/
878 EFI_STATUS
879 UsbBootWriteBlocks (
880 IN USB_MASS_DEVICE *UsbMass,
881 IN UINT32 Lba,
882 IN UINTN TotalBlock,
883 IN UINT8 *Buffer
884 )
885 {
886 USB_BOOT_WRITE10_CMD WriteCmd;
887 EFI_STATUS Status;
888 UINT16 Count;
889 UINT32 BlockSize;
890 UINT32 ByteSize;
891 UINT32 Timeout;
892
893 BlockSize = UsbMass->BlockIoMedia.BlockSize;
894 Status = EFI_SUCCESS;
895
896 while (TotalBlock > 0) {
897 //
898 // Split the total blocks into smaller pieces to ease the pressure
899 // on the device. We must split the total block because the WRITE10
900 // command only has 16 bit transfer length (in the unit of block).
901 //
902 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
903 ByteSize = (UINT32)Count * BlockSize;
904
905 //
906 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
907 //
908 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
909
910 //
911 // Fill in the write10 command block
912 //
913 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
914
915 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
916 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
917 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
918 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
919
920 Status = UsbBootExecCmdWithRetry (
921 UsbMass,
922 &WriteCmd,
923 (UINT8) sizeof (USB_BOOT_WRITE10_CMD),
924 EfiUsbDataOut,
925 Buffer,
926 ByteSize,
927 Timeout
928 );
929 if (EFI_ERROR (Status)) {
930 return Status;
931 }
932 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
933
934 Lba += Count;
935 Buffer += Count * BlockSize;
936 TotalBlock -= Count;
937 }
938
939 return Status;
940 }
941
942 /**
943 Read some blocks from the device by SCSI 16 byte cmd.
944
945 @param UsbMass The USB mass storage device to read from
946 @param Lba The start block number
947 @param TotalBlock Total block number to read
948 @param Buffer The buffer to read to
949
950 @retval EFI_SUCCESS Data are read into the buffer
951 @retval Others Failed to read all the data
952
953 **/
954 EFI_STATUS
955 UsbBootReadBlocks16 (
956 IN USB_MASS_DEVICE *UsbMass,
957 IN UINT64 Lba,
958 IN UINTN TotalBlock,
959 OUT UINT8 *Buffer
960 )
961 {
962 UINT8 ReadCmd[16];
963 EFI_STATUS Status;
964 UINT16 Count;
965 UINT32 BlockSize;
966 UINT32 ByteSize;
967 UINT32 Timeout;
968
969 BlockSize = UsbMass->BlockIoMedia.BlockSize;
970 Status = EFI_SUCCESS;
971
972 while (TotalBlock > 0) {
973 //
974 // Split the total blocks into smaller pieces.
975 //
976 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
977 ByteSize = (UINT32)Count * BlockSize;
978
979 //
980 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
981 //
982 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
983
984 //
985 // Fill in the command then execute
986 //
987 ZeroMem (ReadCmd, sizeof (ReadCmd));
988
989 ReadCmd[0] = EFI_SCSI_OP_READ16;
990 ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
991 WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba));
992 WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count));
993
994 Status = UsbBootExecCmdWithRetry (
995 UsbMass,
996 ReadCmd,
997 (UINT8) sizeof (ReadCmd),
998 EfiUsbDataIn,
999 Buffer,
1000 ByteSize,
1001 Timeout
1002 );
1003 if (EFI_ERROR (Status)) {
1004 return Status;
1005 }
1006 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
1007 Lba += Count;
1008 Buffer += Count * BlockSize;
1009 TotalBlock -= Count;
1010 }
1011
1012 return Status;
1013 }
1014
1015
1016 /**
1017 Write some blocks to the device by SCSI 16 byte cmd.
1018
1019 @param UsbMass The USB mass storage device to write to
1020 @param Lba The start block number
1021 @param TotalBlock Total block number to write
1022 @param Buffer Pointer to the source buffer for the data.
1023
1024 @retval EFI_SUCCESS Data are written into the buffer
1025 @retval Others Failed to write all the data
1026
1027 **/
1028 EFI_STATUS
1029 UsbBootWriteBlocks16 (
1030 IN USB_MASS_DEVICE *UsbMass,
1031 IN UINT64 Lba,
1032 IN UINTN TotalBlock,
1033 IN UINT8 *Buffer
1034 )
1035 {
1036 UINT8 WriteCmd[16];
1037 EFI_STATUS Status;
1038 UINT16 Count;
1039 UINT32 BlockSize;
1040 UINT32 ByteSize;
1041 UINT32 Timeout;
1042
1043 BlockSize = UsbMass->BlockIoMedia.BlockSize;
1044 Status = EFI_SUCCESS;
1045
1046 while (TotalBlock > 0) {
1047 //
1048 // Split the total blocks into smaller pieces.
1049 //
1050 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
1051 ByteSize = (UINT32)Count * BlockSize;
1052
1053 //
1054 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1055 //
1056 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
1057
1058 //
1059 // Fill in the write16 command block
1060 //
1061 ZeroMem (WriteCmd, sizeof (WriteCmd));
1062
1063 WriteCmd[0] = EFI_SCSI_OP_WRITE16;
1064 WriteCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
1065 WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba));
1066 WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count));
1067
1068 Status = UsbBootExecCmdWithRetry (
1069 UsbMass,
1070 WriteCmd,
1071 (UINT8) sizeof (WriteCmd),
1072 EfiUsbDataOut,
1073 Buffer,
1074 ByteSize,
1075 Timeout
1076 );
1077 if (EFI_ERROR (Status)) {
1078 return Status;
1079 }
1080 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
1081 Lba += Count;
1082 Buffer += Count * BlockSize;
1083 TotalBlock -= Count;
1084 }
1085
1086 return Status;
1087 }
1088
1089 /**
1090 Use the USB clear feature control transfer to clear the endpoint stall condition.
1091
1092 @param UsbIo The USB I/O Protocol instance
1093 @param EndpointAddr The endpoint to clear stall for
1094
1095 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1096 @retval Others Failed to clear the endpoint stall condition.
1097
1098 **/
1099 EFI_STATUS
1100 UsbClearEndpointStall (
1101 IN EFI_USB_IO_PROTOCOL *UsbIo,
1102 IN UINT8 EndpointAddr
1103 )
1104 {
1105 EFI_USB_DEVICE_REQUEST Request;
1106 EFI_STATUS Status;
1107 UINT32 CmdResult;
1108 UINT32 Timeout;
1109
1110 Request.RequestType = 0x02;
1111 Request.Request = USB_REQ_CLEAR_FEATURE;
1112 Request.Value = USB_FEATURE_ENDPOINT_HALT;
1113 Request.Index = EndpointAddr;
1114 Request.Length = 0;
1115 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
1116
1117 Status = UsbIo->UsbControlTransfer (
1118 UsbIo,
1119 &Request,
1120 EfiUsbNoData,
1121 Timeout,
1122 NULL,
1123 0,
1124 &CmdResult
1125 );
1126
1127 return Status;
1128 }