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