]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
e3ff281643f02cbd306c85874e971b6ec49d93bf
[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 - 2011, 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 /**
391 Execute READ CAPACITY command to request information regarding
392 the capacity of the installed medium of the device.
393
394 This function executes READ CAPACITY command to get the capacity
395 of the USB mass storage media, including the presence, block size,
396 and last block number.
397
398 @param UsbMass The device to retireve disk gemotric.
399
400 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
401 @retval EFI_NOT_READY The returned block size is zero.
402 @retval Other READ CAPACITY command execution failed.
403
404 **/
405 EFI_STATUS
406 UsbBootReadCapacity (
407 IN USB_MASS_DEVICE *UsbMass
408 )
409 {
410 USB_BOOT_READ_CAPACITY_CMD CapacityCmd;
411 USB_BOOT_READ_CAPACITY_DATA CapacityData;
412 EFI_BLOCK_IO_MEDIA *Media;
413 EFI_STATUS Status;
414 UINT32 BlockSize;
415
416 Media = &UsbMass->BlockIoMedia;
417
418 ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));
419 ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));
420
421 CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;
422 CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
423
424 Status = UsbBootExecCmdWithRetry (
425 UsbMass,
426 &CapacityCmd,
427 (UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD),
428 EfiUsbDataIn,
429 &CapacityData,
430 sizeof (USB_BOOT_READ_CAPACITY_DATA),
431 USB_BOOT_GENERAL_CMD_TIMEOUT
432 );
433 if (EFI_ERROR (Status)) {
434 return Status;
435 }
436
437 //
438 // Get the information on media presence, block size, and last block number
439 // from READ CAPACITY data.
440 //
441 Media->MediaPresent = TRUE;
442 Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));
443
444 BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));
445 if (BlockSize == 0) {
446 //
447 // Get sense data
448 //
449 return UsbBootRequestSense (UsbMass);
450 } else {
451 Media->BlockSize = BlockSize;
452 }
453
454 DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n",
455 Media->LastBlock, Media->BlockSize));
456
457 return Status;
458 }
459
460 /**
461 Retrieves SCSI mode sense information via MODE SENSE(6) command.
462
463 @param UsbMass The device whose sense data is requested.
464
465 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
466 @retval Other Command execution failed.
467
468 **/
469 EFI_STATUS
470 UsbScsiModeSense (
471 IN USB_MASS_DEVICE *UsbMass
472 )
473 {
474 EFI_STATUS Status;
475 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;
476 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;
477 EFI_BLOCK_IO_MEDIA *Media;
478
479 Media = &UsbMass->BlockIoMedia;
480
481 ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));
482 ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));
483
484 //
485 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
486 //
487 ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;
488 ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);
489 ModeSenseCmd.PageCode = 0x3F;
490 ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);
491
492 Status = UsbBootExecCmdWithRetry (
493 UsbMass,
494 &ModeSenseCmd,
495 (UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD),
496 EfiUsbDataIn,
497 &ModeParaHeader,
498 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),
499 USB_BOOT_GENERAL_CMD_TIMEOUT
500 );
501
502 //
503 // Format of device-specific parameter byte of the mode parameter header is defined in
504 // Section 8.2.10 of SCSI-2 Spec.
505 // BIT7 of this byte is indicates whether the medium is write protected.
506 //
507 if (!EFI_ERROR (Status)) {
508 Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);
509 }
510
511 return Status;
512 }
513
514
515 /**
516 Get the parameters for the USB mass storage media.
517
518 This function get the parameters for the USB mass storage media,
519 It is used both to initialize the media during the Start() phase
520 of Driver Binding Protocol and to re-initialize it when the media is
521 changed. Althought the RemoveableMedia is unlikely to change,
522 it is also included here.
523
524 @param UsbMass The device to retrieve disk gemotric.
525
526 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
527 @retval Other Failed to get the parameters.
528
529 **/
530 EFI_STATUS
531 UsbBootGetParams (
532 IN USB_MASS_DEVICE *UsbMass
533 )
534 {
535 EFI_BLOCK_IO_MEDIA *Media;
536 EFI_STATUS Status;
537
538 Media = &(UsbMass->BlockIoMedia);
539
540 Status = UsbBootInquiry (UsbMass);
541 if (EFI_ERROR (Status)) {
542 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));
543 return Status;
544 }
545
546 //
547 // Don't use the Removable bit in inquiry data to test whether the media
548 // is removable because many flash disks wrongly set this bit.
549 //
550 if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {
551 //
552 // CD-Rom device and Non-CD optical device
553 //
554 UsbMass->OpticalStorage = TRUE;
555 //
556 // Default value 2048 Bytes, in case no media present at first time
557 //
558 Media->BlockSize = 0x0800;
559 }
560
561 Status = UsbBootDetectMedia (UsbMass);
562
563 return Status;
564 }
565
566
567 /**
568 Detect whether the removable media is present and whether it has changed.
569
570 @param UsbMass The device to check.
571
572 @retval EFI_SUCCESS The media status is successfully checked.
573 @retval Other Failed to detect media.
574
575 **/
576 EFI_STATUS
577 UsbBootDetectMedia (
578 IN USB_MASS_DEVICE *UsbMass
579 )
580 {
581 EFI_BLOCK_IO_MEDIA OldMedia;
582 EFI_BLOCK_IO_MEDIA *Media;
583 UINT8 CmdSet;
584 EFI_TPL OldTpl;
585 EFI_STATUS Status;
586
587 Media = &UsbMass->BlockIoMedia;
588
589 CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));
590
591 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
592
593 Status = UsbBootIsUnitReady (UsbMass);
594 if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {
595 goto ON_ERROR;
596 }
597
598 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
599 //
600 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
601 // according to Section 4 of USB Mass Storage Specification for Bootability.
602 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
603 // could get the information of Write Protected.
604 // Since not all device support this command, skip if fail.
605 //
606 UsbScsiModeSense (UsbMass);
607 }
608
609 Status = UsbBootReadCapacity (UsbMass);
610 if (EFI_ERROR (Status)) {
611 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));
612 goto ON_ERROR;
613 }
614
615 return EFI_SUCCESS;
616
617 ON_ERROR:
618 //
619 // Detect whether it is necessary to reinstall the Block I/O Protocol.
620 //
621 // MediaId may change in RequestSense for MediaChanged
622 // MediaPresent may change in RequestSense for NoMedia
623 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
624 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
625 //
626 if ((Media->MediaId != OldMedia.MediaId) ||
627 (Media->MediaPresent != OldMedia.MediaPresent) ||
628 (Media->ReadOnly != OldMedia.ReadOnly) ||
629 (Media->BlockSize != OldMedia.BlockSize) ||
630 (Media->LastBlock != OldMedia.LastBlock)) {
631
632 //
633 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
634 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
635 //
636 OldTpl = EfiGetCurrentTpl ();
637 gBS->RestoreTPL (TPL_CALLBACK);
638
639 gBS->ReinstallProtocolInterface (
640 UsbMass->Controller,
641 &gEfiBlockIoProtocolGuid,
642 &UsbMass->BlockIo,
643 &UsbMass->BlockIo
644 );
645
646 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
647 gBS->RaiseTPL (OldTpl);
648
649 //
650 // Update MediaId after reinstalling Block I/O Protocol.
651 //
652 if (Media->MediaPresent != OldMedia.MediaPresent) {
653 if (Media->MediaPresent) {
654 Media->MediaId = 1;
655 } else {
656 Media->MediaId = 0;
657 }
658 }
659
660 if ((Media->ReadOnly != OldMedia.ReadOnly) ||
661 (Media->BlockSize != OldMedia.BlockSize) ||
662 (Media->LastBlock != OldMedia.LastBlock)) {
663 Media->MediaId++;
664 }
665 }
666
667 return Status;
668 }
669
670
671 /**
672 Read some blocks from the device.
673
674 @param UsbMass The USB mass storage device to read from
675 @param Lba The start block number
676 @param TotalBlock Total block number to read
677 @param Buffer The buffer to read to
678
679 @retval EFI_SUCCESS Data are read into the buffer
680 @retval Others Failed to read all the data
681
682 **/
683 EFI_STATUS
684 UsbBootReadBlocks (
685 IN USB_MASS_DEVICE *UsbMass,
686 IN UINT32 Lba,
687 IN UINTN TotalBlock,
688 OUT UINT8 *Buffer
689 )
690 {
691 USB_BOOT_READ10_CMD ReadCmd;
692 EFI_STATUS Status;
693 UINT16 Count;
694 UINT32 BlockSize;
695 UINT32 ByteSize;
696 UINT32 Timeout;
697
698 BlockSize = UsbMass->BlockIoMedia.BlockSize;
699 Status = EFI_SUCCESS;
700
701 while (TotalBlock > 0) {
702 //
703 // Split the total blocks into smaller pieces to ease the pressure
704 // on the device. We must split the total block because the READ10
705 // command only has 16 bit transfer length (in the unit of block).
706 //
707 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
708 ByteSize = (UINT32)Count * BlockSize;
709
710 //
711 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
712 //
713 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
714
715 //
716 // Fill in the command then execute
717 //
718 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));
719
720 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;
721 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
722 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));
723 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));
724
725 Status = UsbBootExecCmdWithRetry (
726 UsbMass,
727 &ReadCmd,
728 (UINT8) sizeof (USB_BOOT_READ10_CMD),
729 EfiUsbDataIn,
730 Buffer,
731 ByteSize,
732 Timeout
733 );
734 if (EFI_ERROR (Status)) {
735 return Status;
736 }
737
738 Lba += Count;
739 Buffer += Count * BlockSize;
740 TotalBlock -= Count;
741 }
742
743 return Status;
744 }
745
746
747 /**
748 Write some blocks to the device.
749
750 @param UsbMass The USB mass storage device to write to
751 @param Lba The start block number
752 @param TotalBlock Total block number to write
753 @param Buffer Pointer to the source buffer for the data.
754
755 @retval EFI_SUCCESS Data are written into the buffer
756 @retval Others Failed to write all the data
757
758 **/
759 EFI_STATUS
760 UsbBootWriteBlocks (
761 IN USB_MASS_DEVICE *UsbMass,
762 IN UINT32 Lba,
763 IN UINTN TotalBlock,
764 IN UINT8 *Buffer
765 )
766 {
767 USB_BOOT_WRITE10_CMD WriteCmd;
768 EFI_STATUS Status;
769 UINT16 Count;
770 UINT32 BlockSize;
771 UINT32 ByteSize;
772 UINT32 Timeout;
773
774 BlockSize = UsbMass->BlockIoMedia.BlockSize;
775 Status = EFI_SUCCESS;
776
777 while (TotalBlock > 0) {
778 //
779 // Split the total blocks into smaller pieces to ease the pressure
780 // on the device. We must split the total block because the WRITE10
781 // command only has 16 bit transfer length (in the unit of block).
782 //
783 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
784 ByteSize = (UINT32)Count * BlockSize;
785
786 //
787 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
788 //
789 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
790
791 //
792 // Fill in the write10 command block
793 //
794 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
795
796 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
797 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
798 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
799 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
800
801 Status = UsbBootExecCmdWithRetry (
802 UsbMass,
803 &WriteCmd,
804 (UINT8) sizeof (USB_BOOT_WRITE10_CMD),
805 EfiUsbDataOut,
806 Buffer,
807 ByteSize,
808 Timeout
809 );
810 if (EFI_ERROR (Status)) {
811 return Status;
812 }
813
814 Lba += Count;
815 Buffer += Count * BlockSize;
816 TotalBlock -= Count;
817 }
818
819 return Status;
820 }
821
822 /**
823 Use the USB clear feature control transfer to clear the endpoint stall condition.
824
825 @param UsbIo The USB I/O Protocol instance
826 @param EndpointAddr The endpoint to clear stall for
827
828 @retval EFI_SUCCESS The endpoint stall condition is cleared.
829 @retval Others Failed to clear the endpoint stall condition.
830
831 **/
832 EFI_STATUS
833 UsbClearEndpointStall (
834 IN EFI_USB_IO_PROTOCOL *UsbIo,
835 IN UINT8 EndpointAddr
836 )
837 {
838 EFI_USB_DEVICE_REQUEST Request;
839 EFI_STATUS Status;
840 UINT32 CmdResult;
841 UINT32 Timeout;
842
843 Request.RequestType = 0x02;
844 Request.Request = USB_REQ_CLEAR_FEATURE;
845 Request.Value = USB_FEATURE_ENDPOINT_HALT;
846 Request.Index = EndpointAddr;
847 Request.Length = 0;
848 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
849
850 Status = UsbIo->UsbControlTransfer (
851 UsbIo,
852 &Request,
853 EfiUsbNoData,
854 Timeout,
855 NULL,
856 0,
857 &CmdResult
858 );
859
860 return Status;
861 }