]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
2eb30f0c5f5010843b0c74af99366415aee3660b
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.c
1 /** @file
2 Implementation of the command set of USB Mass Storage Specification
3 for Bootability, Revision 1.0.
4
5 Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UsbMass.h"
17
18 /**
19 Execute REQUEST SENSE Command to retrieve sense data from device.
20
21 @param UsbMass The device whose sense data is requested.
22
23 @retval EFI_SUCCESS The command is executed successfully.
24 @retval EFI_DEVICE_ERROR Failed to request sense.
25 @retval EFI_NO_RESPONSE The device media doesn't response this request.
26 @retval EFI_INVALID_PARAMETER The command has some invalid parameters.
27 @retval EFI_WRITE_PROTECTED The device is write protected.
28 @retval EFI_MEDIA_CHANGED The device media has been changed.
29
30 **/
31 EFI_STATUS
32 UsbBootRequestSense (
33 IN USB_MASS_DEVICE *UsbMass
34 )
35 {
36 USB_BOOT_REQUEST_SENSE_CMD SenseCmd;
37 USB_BOOT_REQUEST_SENSE_DATA SenseData;
38 EFI_BLOCK_IO_MEDIA *Media;
39 USB_MASS_TRANSPORT *Transport;
40 EFI_STATUS Status;
41 UINT32 CmdResult;
42
43 Transport = UsbMass->Transport;
44
45 //
46 // Request the sense data from the device
47 //
48 ZeroMem (&SenseCmd, sizeof (USB_BOOT_REQUEST_SENSE_CMD));
49 ZeroMem (&SenseData, sizeof (USB_BOOT_REQUEST_SENSE_DATA));
50
51 SenseCmd.OpCode = USB_BOOT_REQUEST_SENSE_OPCODE;
52 SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
53 SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA);
54
55 Status = Transport->ExecCommand (
56 UsbMass->Context,
57 &SenseCmd,
58 sizeof (USB_BOOT_REQUEST_SENSE_CMD),
59 EfiUsbDataIn,
60 &SenseData,
61 sizeof (USB_BOOT_REQUEST_SENSE_DATA),
62 UsbMass->Lun,
63 USB_BOOT_GENERAL_CMD_TIMEOUT,
64 &CmdResult
65 );
66 if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) {
67 DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult));
68 if (!EFI_ERROR (Status)) {
69 Status = EFI_DEVICE_ERROR;
70 }
71 return Status;
72 }
73
74 //
75 // If sense data is retrieved successfully, interpret the sense data
76 // and update the media status if necessary.
77 //
78 Media = &UsbMass->BlockIoMedia;
79
80 switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) {
81
82 case USB_BOOT_SENSE_NO_SENSE:
83 if (SenseData.Asc == USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION) {
84 //
85 // It is not an error if a device does not have additional sense information
86 //
87 Status = EFI_SUCCESS;
88 } else {
89 Status = EFI_NO_RESPONSE;
90 }
91 break;
92
93 case USB_BOOT_SENSE_RECOVERED:
94 //
95 // Suppose hardware can handle this case, and recover later by itself
96 //
97 Status = EFI_NOT_READY;
98 break;
99
100 case USB_BOOT_SENSE_NOT_READY:
101 Status = EFI_DEVICE_ERROR;
102 if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {
103 Media->MediaPresent = FALSE;
104 Status = EFI_NO_MEDIA;
105 } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {
106 Status = EFI_NOT_READY;
107 }
108 break;
109
110 case USB_BOOT_SENSE_ILLEGAL_REQUEST:
111 Status = EFI_INVALID_PARAMETER;
112 break;
113
114 case USB_BOOT_SENSE_UNIT_ATTENTION:
115 Status = EFI_DEVICE_ERROR;
116 if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) {
117 //
118 // If MediaChange, reset ReadOnly and new MediaId
119 //
120 Status = EFI_MEDIA_CHANGED;
121 Media->ReadOnly = FALSE;
122 Media->MediaId++;
123 }
124 break;
125
126 case USB_BOOT_SENSE_DATA_PROTECT:
127 Status = EFI_WRITE_PROTECTED;
128 Media->ReadOnly = TRUE;
129 break;
130
131 default:
132 Status = EFI_DEVICE_ERROR;
133 break;
134 }
135
136 DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",
137 Status,
138 USB_BOOT_SENSE_KEY (SenseData.SenseKey),
139 SenseData.Asc,
140 SenseData.Ascq
141 ));
142
143 return Status;
144 }
145
146
147 /**
148 Execute the USB mass storage bootability commands.
149
150 This function executes the USB mass storage bootability commands.
151 If execution failed, retrieve the error by REQUEST_SENSE, then
152 update the device's status, such as ReadyOnly.
153
154 @param UsbMass The device to issue commands to
155 @param Cmd The command to execute
156 @param CmdLen The length of the command
157 @param DataDir The direction of data transfer
158 @param Data The buffer to hold the data
159 @param DataLen The length of expected data
160 @param Timeout The timeout used to transfer
161
162 @retval EFI_SUCCESS Command is executed successfully
163 @retval Others Command execution failed.
164
165 **/
166 EFI_STATUS
167 UsbBootExecCmd (
168 IN USB_MASS_DEVICE *UsbMass,
169 IN VOID *Cmd,
170 IN UINT8 CmdLen,
171 IN EFI_USB_DATA_DIRECTION DataDir,
172 IN VOID *Data,
173 IN UINT32 DataLen,
174 IN UINT32 Timeout
175 )
176 {
177 USB_MASS_TRANSPORT *Transport;
178 EFI_STATUS Status;
179 UINT32 CmdResult;
180
181 Transport = UsbMass->Transport;
182 Status = Transport->ExecCommand (
183 UsbMass->Context,
184 Cmd,
185 CmdLen,
186 DataDir,
187 Data,
188 DataLen,
189 UsbMass->Lun,
190 Timeout,
191 &CmdResult
192 );
193
194 if (Status == EFI_TIMEOUT) {
195 DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8 *)Cmd));
196 return EFI_TIMEOUT;
197 }
198
199 //
200 // If ExecCommand() returns no error and CmdResult is success,
201 // then the commnad transfer is successful.
202 //
203 if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {
204 return EFI_SUCCESS;
205 }
206
207 //
208 // If command execution failed, then retrieve error info via sense request.
209 //
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_MEDIA_CHANGED The device media has been changed.
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_MEDIA_CHANGED || 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. Althought 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 ((EFI_D_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 ((EFI_D_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_TPL OldTpl;
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) && (Status != EFI_MEDIA_CHANGED)) {
699 goto ON_ERROR;
700 }
701
702 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
703 //
704 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
705 // according to Section 4 of USB Mass Storage Specification for Bootability.
706 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
707 // could get the information of Write Protected.
708 // Since not all device support this command, skip if fail.
709 //
710 UsbScsiModeSense (UsbMass);
711 }
712
713 Status = UsbBootReadCapacity (UsbMass);
714 if (EFI_ERROR (Status)) {
715 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));
716 goto ON_ERROR;
717 }
718
719 return EFI_SUCCESS;
720
721 ON_ERROR:
722 //
723 // Detect whether it is necessary to reinstall the Block I/O Protocol.
724 //
725 // MediaId may change in RequestSense for MediaChanged
726 // MediaPresent may change in RequestSense for NoMedia
727 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
728 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
729 //
730 if ((Media->MediaId != OldMedia.MediaId) ||
731 (Media->MediaPresent != OldMedia.MediaPresent) ||
732 (Media->ReadOnly != OldMedia.ReadOnly) ||
733 (Media->BlockSize != OldMedia.BlockSize) ||
734 (Media->LastBlock != OldMedia.LastBlock)) {
735
736 //
737 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
738 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
739 //
740 OldTpl = EfiGetCurrentTpl ();
741 gBS->RestoreTPL (TPL_CALLBACK);
742
743 gBS->ReinstallProtocolInterface (
744 UsbMass->Controller,
745 &gEfiBlockIoProtocolGuid,
746 &UsbMass->BlockIo,
747 &UsbMass->BlockIo
748 );
749
750 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
751 gBS->RaiseTPL (OldTpl);
752
753 //
754 // Update MediaId after reinstalling Block I/O Protocol.
755 //
756 if (Media->MediaPresent != OldMedia.MediaPresent) {
757 if (Media->MediaPresent) {
758 Media->MediaId = 1;
759 } else {
760 Media->MediaId = 0;
761 }
762 }
763
764 if ((Media->ReadOnly != OldMedia.ReadOnly) ||
765 (Media->BlockSize != OldMedia.BlockSize) ||
766 (Media->LastBlock != OldMedia.LastBlock)) {
767 Media->MediaId++;
768 }
769 }
770
771 return Status;
772 }
773
774
775 /**
776 Read some blocks from the device.
777
778 @param UsbMass The USB mass storage device to read from
779 @param Lba The start block number
780 @param TotalBlock Total block number to read
781 @param Buffer The buffer to read to
782
783 @retval EFI_SUCCESS Data are read into the buffer
784 @retval Others Failed to read all the data
785
786 **/
787 EFI_STATUS
788 UsbBootReadBlocks (
789 IN USB_MASS_DEVICE *UsbMass,
790 IN UINT32 Lba,
791 IN UINTN TotalBlock,
792 OUT UINT8 *Buffer
793 )
794 {
795 USB_BOOT_READ10_CMD ReadCmd;
796 EFI_STATUS Status;
797 UINT16 Count;
798 UINT32 BlockSize;
799 UINT32 ByteSize;
800 UINT32 Timeout;
801
802 BlockSize = UsbMass->BlockIoMedia.BlockSize;
803 Status = EFI_SUCCESS;
804
805 while (TotalBlock > 0) {
806 //
807 // Split the total blocks into smaller pieces to ease the pressure
808 // on the device. We must split the total block because the READ10
809 // command only has 16 bit transfer length (in the unit of block).
810 //
811 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
812 ByteSize = (UINT32)Count * BlockSize;
813
814 //
815 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
816 //
817 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
818
819 //
820 // Fill in the command then execute
821 //
822 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));
823
824 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;
825 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
826 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));
827 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));
828
829 Status = UsbBootExecCmdWithRetry (
830 UsbMass,
831 &ReadCmd,
832 (UINT8) sizeof (USB_BOOT_READ10_CMD),
833 EfiUsbDataIn,
834 Buffer,
835 ByteSize,
836 Timeout
837 );
838 if (EFI_ERROR (Status)) {
839 return Status;
840 }
841 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
842 Lba += Count;
843 Buffer += Count * BlockSize;
844 TotalBlock -= Count;
845 }
846
847 return Status;
848 }
849
850
851 /**
852 Write some blocks to the device.
853
854 @param UsbMass The USB mass storage device to write to
855 @param Lba The start block number
856 @param TotalBlock Total block number to write
857 @param Buffer Pointer to the source buffer for the data.
858
859 @retval EFI_SUCCESS Data are written into the buffer
860 @retval Others Failed to write all the data
861
862 **/
863 EFI_STATUS
864 UsbBootWriteBlocks (
865 IN USB_MASS_DEVICE *UsbMass,
866 IN UINT32 Lba,
867 IN UINTN TotalBlock,
868 IN UINT8 *Buffer
869 )
870 {
871 USB_BOOT_WRITE10_CMD WriteCmd;
872 EFI_STATUS Status;
873 UINT16 Count;
874 UINT32 BlockSize;
875 UINT32 ByteSize;
876 UINT32 Timeout;
877
878 BlockSize = UsbMass->BlockIoMedia.BlockSize;
879 Status = EFI_SUCCESS;
880
881 while (TotalBlock > 0) {
882 //
883 // Split the total blocks into smaller pieces to ease the pressure
884 // on the device. We must split the total block because the WRITE10
885 // command only has 16 bit transfer length (in the unit of block).
886 //
887 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
888 ByteSize = (UINT32)Count * BlockSize;
889
890 //
891 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
892 //
893 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
894
895 //
896 // Fill in the write10 command block
897 //
898 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
899
900 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
901 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
902 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
903 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
904
905 Status = UsbBootExecCmdWithRetry (
906 UsbMass,
907 &WriteCmd,
908 (UINT8) sizeof (USB_BOOT_WRITE10_CMD),
909 EfiUsbDataOut,
910 Buffer,
911 ByteSize,
912 Timeout
913 );
914 if (EFI_ERROR (Status)) {
915 return Status;
916 }
917 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
918
919 Lba += Count;
920 Buffer += Count * BlockSize;
921 TotalBlock -= Count;
922 }
923
924 return Status;
925 }
926
927 /**
928 Read some blocks from the device by SCSI 16 byte cmd.
929
930 @param UsbMass The USB mass storage device to read from
931 @param Lba The start block number
932 @param TotalBlock Total block number to read
933 @param Buffer The buffer to read to
934
935 @retval EFI_SUCCESS Data are read into the buffer
936 @retval Others Failed to read all the data
937
938 **/
939 EFI_STATUS
940 UsbBootReadBlocks16 (
941 IN USB_MASS_DEVICE *UsbMass,
942 IN UINT64 Lba,
943 IN UINTN TotalBlock,
944 OUT UINT8 *Buffer
945 )
946 {
947 UINT8 ReadCmd[16];
948 EFI_STATUS Status;
949 UINT16 Count;
950 UINT32 BlockSize;
951 UINT32 ByteSize;
952 UINT32 Timeout;
953
954 BlockSize = UsbMass->BlockIoMedia.BlockSize;
955 Status = EFI_SUCCESS;
956
957 while (TotalBlock > 0) {
958 //
959 // Split the total blocks into smaller pieces.
960 //
961 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
962 ByteSize = (UINT32)Count * BlockSize;
963
964 //
965 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
966 //
967 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
968
969 //
970 // Fill in the command then execute
971 //
972 ZeroMem (ReadCmd, sizeof (ReadCmd));
973
974 ReadCmd[0] = EFI_SCSI_OP_READ16;
975 ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
976 WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba));
977 WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count));
978
979 Status = UsbBootExecCmdWithRetry (
980 UsbMass,
981 ReadCmd,
982 (UINT8) sizeof (ReadCmd),
983 EfiUsbDataIn,
984 Buffer,
985 ByteSize,
986 Timeout
987 );
988 if (EFI_ERROR (Status)) {
989 return Status;
990 }
991 DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
992 Lba += Count;
993 Buffer += Count * BlockSize;
994 TotalBlock -= Count;
995 }
996
997 return Status;
998 }
999
1000
1001 /**
1002 Write some blocks to the device by SCSI 16 byte cmd.
1003
1004 @param UsbMass The USB mass storage device to write to
1005 @param Lba The start block number
1006 @param TotalBlock Total block number to write
1007 @param Buffer Pointer to the source buffer for the data.
1008
1009 @retval EFI_SUCCESS Data are written into the buffer
1010 @retval Others Failed to write all the data
1011
1012 **/
1013 EFI_STATUS
1014 UsbBootWriteBlocks16 (
1015 IN USB_MASS_DEVICE *UsbMass,
1016 IN UINT64 Lba,
1017 IN UINTN TotalBlock,
1018 IN UINT8 *Buffer
1019 )
1020 {
1021 UINT8 WriteCmd[16];
1022 EFI_STATUS Status;
1023 UINT16 Count;
1024 UINT32 BlockSize;
1025 UINT32 ByteSize;
1026 UINT32 Timeout;
1027
1028 BlockSize = UsbMass->BlockIoMedia.BlockSize;
1029 Status = EFI_SUCCESS;
1030
1031 while (TotalBlock > 0) {
1032 //
1033 // Split the total blocks into smaller pieces.
1034 //
1035 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
1036 ByteSize = (UINT32)Count * BlockSize;
1037
1038 //
1039 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1040 //
1041 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
1042
1043 //
1044 // Fill in the write16 command block
1045 //
1046 ZeroMem (WriteCmd, sizeof (WriteCmd));
1047
1048 WriteCmd[0] = EFI_SCSI_OP_WRITE16;
1049 WriteCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
1050 WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba));
1051 WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count));
1052
1053 Status = UsbBootExecCmdWithRetry (
1054 UsbMass,
1055 WriteCmd,
1056 (UINT8) sizeof (WriteCmd),
1057 EfiUsbDataOut,
1058 Buffer,
1059 ByteSize,
1060 Timeout
1061 );
1062 if (EFI_ERROR (Status)) {
1063 return Status;
1064 }
1065 DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));
1066 Lba += Count;
1067 Buffer += Count * BlockSize;
1068 TotalBlock -= Count;
1069 }
1070
1071 return Status;
1072 }
1073
1074 /**
1075 Use the USB clear feature control transfer to clear the endpoint stall condition.
1076
1077 @param UsbIo The USB I/O Protocol instance
1078 @param EndpointAddr The endpoint to clear stall for
1079
1080 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1081 @retval Others Failed to clear the endpoint stall condition.
1082
1083 **/
1084 EFI_STATUS
1085 UsbClearEndpointStall (
1086 IN EFI_USB_IO_PROTOCOL *UsbIo,
1087 IN UINT8 EndpointAddr
1088 )
1089 {
1090 EFI_USB_DEVICE_REQUEST Request;
1091 EFI_STATUS Status;
1092 UINT32 CmdResult;
1093 UINT32 Timeout;
1094
1095 Request.RequestType = 0x02;
1096 Request.Request = USB_REQ_CLEAR_FEATURE;
1097 Request.Value = USB_FEATURE_ENDPOINT_HALT;
1098 Request.Index = EndpointAddr;
1099 Request.Length = 0;
1100 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
1101
1102 Status = UsbIo->UsbControlTransfer (
1103 UsbIo,
1104 &Request,
1105 EfiUsbNoData,
1106 Timeout,
1107 NULL,
1108 0,
1109 &CmdResult
1110 );
1111
1112 return Status;
1113 }