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