]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
MdeModulePkg/UsbMass: Fix USB key write failure
[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 // Simply reject device whose block size is unacceptable small (==0) or large (>64K).
741 //
742 if ((Media->BlockSize == 0) || (Media->BlockSize > USB_BOOT_MAX_CARRY_SIZE)) {
743 return EFI_DEVICE_ERROR;
744 }
745
746 //
747 // Detect whether it is necessary to reinstall the Block I/O Protocol.
748 //
749 // MediaId may change in RequestSense for MediaChanged
750 // MediaPresent may change in RequestSense for NoMedia
751 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
752 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
753 //
754 if ((Media->MediaId != OldMedia.MediaId) ||
755 (Media->MediaPresent != OldMedia.MediaPresent) ||
756 (Media->ReadOnly != OldMedia.ReadOnly) ||
757 (Media->BlockSize != OldMedia.BlockSize) ||
758 (Media->LastBlock != OldMedia.LastBlock)) {
759
760 //
761 // This function is called from:
762 // Block I/O Protocol APIs, which run at TPL_CALLBACK.
763 // DriverBindingStart(), which raises to TPL_CALLBACK.
764 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
765
766 //
767 // When it is called from DriverBindingStart(), below reinstall fails.
768 // So ignore the return status check.
769 //
770 gBS->ReinstallProtocolInterface (
771 UsbMass->Controller,
772 &gEfiBlockIoProtocolGuid,
773 &UsbMass->BlockIo,
774 &UsbMass->BlockIo
775 );
776
777 //
778 // Reset MediaId after reinstalling Block I/O Protocol.
779 //
780 if (Media->MediaPresent != OldMedia.MediaPresent) {
781 if (Media->MediaPresent) {
782 Media->MediaId = 1;
783 } else {
784 Media->MediaId = 0;
785 }
786 }
787
788 if ((Media->ReadOnly != OldMedia.ReadOnly) ||
789 (Media->BlockSize != OldMedia.BlockSize) ||
790 (Media->LastBlock != OldMedia.LastBlock)) {
791 Media->MediaId++;
792 }
793
794 Status = Media->MediaPresent ? EFI_MEDIA_CHANGED : EFI_NO_MEDIA;
795 }
796
797 return Status;
798 }
799
800
801 /**
802 Read or write some blocks from the device.
803
804 @param UsbMass The USB mass storage device to access
805 @param Write TRUE for write operation.
806 @param Lba The start block number
807 @param TotalBlock Total block number to read or write
808 @param Buffer The buffer to read to or write from
809
810 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
811 @retval Others Failed to read or write all the data
812
813 **/
814 EFI_STATUS
815 UsbBootReadWriteBlocks (
816 IN USB_MASS_DEVICE *UsbMass,
817 IN BOOLEAN Write,
818 IN UINT32 Lba,
819 IN UINTN TotalBlock,
820 IN OUT UINT8 *Buffer
821 )
822 {
823 USB_BOOT_READ_WRITE_10_CMD Cmd;
824 EFI_STATUS Status;
825 UINT32 Count;
826 UINT32 CountMax;
827 UINT32 BlockSize;
828 UINT32 ByteSize;
829 UINT32 Timeout;
830
831 BlockSize = UsbMass->BlockIoMedia.BlockSize;
832 CountMax = USB_BOOT_MAX_CARRY_SIZE / BlockSize;
833 Status = EFI_SUCCESS;
834
835 while (TotalBlock > 0) {
836 //
837 // Split the total blocks into smaller pieces to ease the pressure
838 // on the device. We must split the total block because the READ10
839 // command only has 16 bit transfer length (in the unit of block).
840 //
841 Count = (UINT32)MIN (TotalBlock, CountMax);
842 Count = MIN (MAX_UINT16, Count);
843 ByteSize = Count * BlockSize;
844
845 //
846 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
847 //
848 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
849
850 //
851 // Fill in the command then execute
852 //
853 ZeroMem (&Cmd, sizeof (USB_BOOT_READ_WRITE_10_CMD));
854
855 Cmd.OpCode = Write ? USB_BOOT_WRITE10_OPCODE : USB_BOOT_READ10_OPCODE;
856 Cmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
857 WriteUnaligned32 ((UINT32 *) Cmd.Lba, SwapBytes32 (Lba));
858 WriteUnaligned16 ((UINT16 *) Cmd.TransferLen, SwapBytes16 ((UINT16)Count));
859
860 Status = UsbBootExecCmdWithRetry (
861 UsbMass,
862 &Cmd,
863 (UINT8) sizeof (USB_BOOT_READ_WRITE_10_CMD),
864 Write ? EfiUsbDataOut : EfiUsbDataIn,
865 Buffer,
866 ByteSize,
867 Timeout
868 );
869 if (EFI_ERROR (Status)) {
870 return Status;
871 }
872 DEBUG ((
873 DEBUG_BLKIO, "UsbBoot%sBlocks: LBA (0x%lx), Blk (0x%x)\n",
874 Write ? L"Write" : L"Read",
875 Lba, Count
876 ));
877 Lba += Count;
878 Buffer += ByteSize;
879 TotalBlock -= Count;
880 }
881
882 return Status;
883 }
884
885 /**
886 Read or write some blocks from the device by SCSI 16 byte cmd.
887
888 @param UsbMass The USB mass storage device to access
889 @param Write TRUE for write operation.
890 @param Lba The start block number
891 @param TotalBlock Total block number to read or write
892 @param Buffer The buffer to read to or write from
893
894 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
895 @retval Others Failed to read or write all the data
896 **/
897 EFI_STATUS
898 UsbBootReadWriteBlocks16 (
899 IN USB_MASS_DEVICE *UsbMass,
900 IN BOOLEAN Write,
901 IN UINT64 Lba,
902 IN UINTN TotalBlock,
903 IN OUT UINT8 *Buffer
904 )
905 {
906 UINT8 Cmd[16];
907 EFI_STATUS Status;
908 UINT32 Count;
909 UINT32 CountMax;
910 UINT32 BlockSize;
911 UINT32 ByteSize;
912 UINT32 Timeout;
913
914 BlockSize = UsbMass->BlockIoMedia.BlockSize;
915 CountMax = USB_BOOT_MAX_CARRY_SIZE / BlockSize;
916 Status = EFI_SUCCESS;
917
918 while (TotalBlock > 0) {
919 //
920 // Split the total blocks into smaller pieces.
921 //
922 Count = (UINT32)MIN (TotalBlock, CountMax);
923 ByteSize = Count * BlockSize;
924
925 //
926 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
927 //
928 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
929
930 //
931 // Fill in the command then execute
932 //
933 ZeroMem (Cmd, sizeof (Cmd));
934
935 Cmd[0] = Write ? EFI_SCSI_OP_WRITE16 : EFI_SCSI_OP_READ16;
936 Cmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
937 WriteUnaligned64 ((UINT64 *) &Cmd[2], SwapBytes64 (Lba));
938 WriteUnaligned32 ((UINT32 *) &Cmd[10], SwapBytes32 (Count));
939
940 Status = UsbBootExecCmdWithRetry (
941 UsbMass,
942 Cmd,
943 (UINT8) sizeof (Cmd),
944 Write ? EfiUsbDataOut : EfiUsbDataIn,
945 Buffer,
946 ByteSize,
947 Timeout
948 );
949 if (EFI_ERROR (Status)) {
950 return Status;
951 }
952 DEBUG ((
953 DEBUG_BLKIO, "UsbBoot%sBlocks16: LBA (0x%lx), Blk (0x%x)\n",
954 Write ? L"Write" : L"Read",
955 Lba, Count
956 ));
957 Lba += Count;
958 Buffer += ByteSize;
959 TotalBlock -= Count;
960 }
961
962 return Status;
963 }
964
965 /**
966 Use the USB clear feature control transfer to clear the endpoint stall condition.
967
968 @param UsbIo The USB I/O Protocol instance
969 @param EndpointAddr The endpoint to clear stall for
970
971 @retval EFI_SUCCESS The endpoint stall condition is cleared.
972 @retval Others Failed to clear the endpoint stall condition.
973
974 **/
975 EFI_STATUS
976 UsbClearEndpointStall (
977 IN EFI_USB_IO_PROTOCOL *UsbIo,
978 IN UINT8 EndpointAddr
979 )
980 {
981 EFI_USB_DEVICE_REQUEST Request;
982 EFI_STATUS Status;
983 UINT32 CmdResult;
984 UINT32 Timeout;
985
986 Request.RequestType = 0x02;
987 Request.Request = USB_REQ_CLEAR_FEATURE;
988 Request.Value = USB_FEATURE_ENDPOINT_HALT;
989 Request.Index = EndpointAddr;
990 Request.Length = 0;
991 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
992
993 Status = UsbIo->UsbControlTransfer (
994 UsbIo,
995 &Request,
996 EfiUsbNoData,
997 Timeout,
998 NULL,
999 0,
1000 &CmdResult
1001 );
1002
1003 return Status;
1004 }