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