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