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