]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
Fixed a bug that the system hangs with an assert in DiskIo.c that is division overflo...
[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 - 2008, Intel Corporation
6 All rights reserved. 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 "UsbMassImpl.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 = 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 ExecCommand() returns no error and CmdResult is success,
188 // then the commnad transfer is successful.
189 //
190 if ((CmdResult == USB_MASS_CMD_SUCCESS) && !EFI_ERROR (Status)) {
191 return EFI_SUCCESS;
192 }
193
194 DEBUG ((EFI_D_INFO, "UsbBootExecCmd: Fail to Exec 0x%x Cmd /w %r\n",
195 *(UINT8 *)Cmd ,Status));
196
197 //
198 // If command execution failed, then retrieve error info via sense request.
199 //
200 return UsbBootRequestSense (UsbMass);
201 }
202
203
204 /**
205 Execute the USB mass storage bootability commands with retrial.
206
207 This function executes USB mass storage bootability commands.
208 If the device isn't ready, wait for it. If the device is ready
209 and error occurs, retry the command again until it exceeds the
210 limit of retrial times.
211
212 @param UsbMass The device to issue commands to
213 @param Cmd The command to execute
214 @param CmdLen The length of the command
215 @param DataDir The direction of data transfer
216 @param Data The buffer to hold the data
217 @param DataLen The length of expected data
218 @param Timeout The timeout used to transfer
219
220 @retval EFI_SUCCESS The command is executed successfully.
221 @retval EFI_MEDIA_CHANGED The device media has been changed.
222 @retval Others Command execution failed after retrial.
223
224 **/
225 EFI_STATUS
226 UsbBootExecCmdWithRetry (
227 IN USB_MASS_DEVICE *UsbMass,
228 IN VOID *Cmd,
229 IN UINT8 CmdLen,
230 IN EFI_USB_DATA_DIRECTION DataDir,
231 IN VOID *Data,
232 IN UINT32 DataLen,
233 IN UINT32 Timeout
234 )
235 {
236 EFI_STATUS Status;
237 UINTN Retry;
238
239 Status = EFI_SUCCESS;
240
241 for (Retry = 0; Retry < USB_BOOT_COMMAND_RETRY; Retry++) {
242
243 Status = UsbBootExecCmd (
244 UsbMass,
245 Cmd,
246 CmdLen,
247 DataDir,
248 Data,
249 DataLen,
250 Timeout
251 );
252 if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED) {
253 break;
254 }
255 //
256 // If the device isn't ready, just wait for it without limit on retrial times.
257 //
258 if (Status == EFI_NOT_READY) {
259 Retry = 0;
260 }
261 }
262
263 return Status;
264 }
265
266
267 /**
268 Execute TEST UNIT READY command to check if the device is ready.
269
270 @param UsbMass The device to test
271
272 @retval EFI_SUCCESS The device is ready.
273 @retval Others Device not ready.
274
275 **/
276 EFI_STATUS
277 UsbBootIsUnitReady (
278 IN USB_MASS_DEVICE *UsbMass
279 )
280 {
281 USB_BOOT_TEST_UNIT_READY_CMD TestCmd;
282
283 ZeroMem (&TestCmd, sizeof (USB_BOOT_TEST_UNIT_READY_CMD));
284
285 TestCmd.OpCode = USB_BOOT_TEST_UNIT_READY_OPCODE;
286 TestCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
287
288 return UsbBootExecCmdWithRetry (
289 UsbMass,
290 &TestCmd,
291 sizeof (USB_BOOT_TEST_UNIT_READY_CMD),
292 EfiUsbNoData,
293 NULL,
294 0,
295 USB_BOOT_GENERAL_CMD_TIMEOUT
296 );
297 }
298
299
300 /**
301 Execute INQUIRY Command to request information regarding parameters of
302 the device be sent to the host computer.
303
304 @param UsbMass The device to inquire.
305
306 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
307 @retval Others INQUIRY Command is not executed successfully.
308
309 **/
310 EFI_STATUS
311 UsbBootInquiry (
312 IN USB_MASS_DEVICE *UsbMass
313 )
314 {
315 USB_BOOT_INQUIRY_CMD InquiryCmd;
316 USB_BOOT_INQUIRY_DATA InquiryData;
317 EFI_BLOCK_IO_MEDIA *Media;
318 EFI_STATUS Status;
319
320 Media = &(UsbMass->BlockIoMedia);
321
322 ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));
323 ZeroMem (&InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));
324
325 InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE;
326 InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
327 InquiryCmd.AllocLen = sizeof (InquiryData);
328
329 Status = UsbBootExecCmdWithRetry (
330 UsbMass,
331 &InquiryCmd,
332 sizeof (USB_BOOT_INQUIRY_CMD),
333 EfiUsbDataIn,
334 &InquiryData,
335 sizeof (USB_BOOT_INQUIRY_DATA),
336 USB_BOOT_GENERAL_CMD_TIMEOUT
337 );
338 if (EFI_ERROR (Status)) {
339 return Status;
340 }
341
342 //
343 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
344 // from the inquiry data.
345 //
346 UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (InquiryData.Pdt));
347 Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (InquiryData.Removable));
348 //
349 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
350 //
351 Media->BlockSize = 0x0200;
352
353 return Status;
354 }
355
356
357 /**
358 Execute READ CAPACITY command to request information regarding
359 the capacity of the installed medium of the device.
360
361 This function executes READ CAPACITY command to get the capacity
362 of the USB mass storage media, including the presence, block size,
363 and last block number.
364
365 @param UsbMass The device to retireve disk gemotric.
366
367 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
368 @retval EFI_NOT_READY The returned block size is zero.
369 @retval Other READ CAPACITY command execution failed.
370
371 **/
372 EFI_STATUS
373 UsbBootReadCapacity (
374 IN USB_MASS_DEVICE *UsbMass
375 )
376 {
377 USB_BOOT_READ_CAPACITY_CMD CapacityCmd;
378 USB_BOOT_READ_CAPACITY_DATA CapacityData;
379 EFI_BLOCK_IO_MEDIA *Media;
380 EFI_STATUS Status;
381 UINT32 BlockSize;
382
383 Media = &UsbMass->BlockIoMedia;
384
385 ZeroMem (&CapacityCmd, sizeof (USB_BOOT_READ_CAPACITY_CMD));
386 ZeroMem (&CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA));
387
388 CapacityCmd.OpCode = USB_BOOT_READ_CAPACITY_OPCODE;
389 CapacityCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
390
391 Status = UsbBootExecCmdWithRetry (
392 UsbMass,
393 &CapacityCmd,
394 sizeof (USB_BOOT_READ_CAPACITY_CMD),
395 EfiUsbDataIn,
396 &CapacityData,
397 sizeof (USB_BOOT_READ_CAPACITY_DATA),
398 USB_BOOT_GENERAL_CMD_TIMEOUT
399 );
400 if (EFI_ERROR (Status)) {
401 return Status;
402 }
403
404 //
405 // Get the information on media presence, block size, and last block number
406 // from READ CAPACITY data.
407 //
408 Media->MediaPresent = TRUE;
409 Media->LastBlock = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));
410
411 BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));
412 if (BlockSize == 0) {
413 return EFI_NOT_READY;
414 } else {
415 Media->BlockSize = BlockSize;
416 }
417
418 DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n",
419 Media->LastBlock, Media->BlockSize));
420
421 return EFI_SUCCESS;
422 }
423
424 /**
425 Retrieves SCSI mode sense information via MODE SENSE(6) command.
426
427 @param UsbMass The device whose sense data is requested.
428
429 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
430 @retval Other Command execution failed.
431
432 **/
433 EFI_STATUS
434 UsbScsiModeSense (
435 IN USB_MASS_DEVICE *UsbMass
436 )
437 {
438 EFI_STATUS Status;
439 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd;
440 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader;
441 EFI_BLOCK_IO_MEDIA *Media;
442
443 Media = &UsbMass->BlockIoMedia;
444
445 ZeroMem (&ModeSenseCmd, sizeof (USB_SCSI_MODE_SENSE6_CMD));
446 ZeroMem (&ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER));
447
448 //
449 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
450 //
451 ModeSenseCmd.OpCode = USB_SCSI_MODE_SENSE6_OPCODE;
452 ModeSenseCmd.Lun = (UINT8) USB_BOOT_LUN (UsbMass->Lun);
453 ModeSenseCmd.PageCode = 0x3F;
454 ModeSenseCmd.AllocateLen = (UINT8) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER);
455
456 Status = UsbBootExecCmdWithRetry (
457 UsbMass,
458 &ModeSenseCmd,
459 sizeof (USB_SCSI_MODE_SENSE6_CMD),
460 EfiUsbDataIn,
461 &ModeParaHeader,
462 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),
463 USB_BOOT_GENERAL_CMD_TIMEOUT
464 );
465
466 //
467 // Format of device-specific parameter byte of the mode parameter header is defined in
468 // Section 8.2.10 of SCSI-2 Spec.
469 // BIT7 of this byte is indicates whether the medium is write protected.
470 //
471 if (!EFI_ERROR (Status)) {
472 Media->ReadOnly = (BOOLEAN) ((ModeParaHeader.DevicePara & BIT7) != 0);
473 }
474
475 return Status;
476 }
477
478
479 /**
480 Get the parameters for the USB mass storage media.
481
482 This function get the parameters for the USB mass storage media,
483 It is used both to initialize the media during the Start() phase
484 of Driver Binding Protocol and to re-initialize it when the media is
485 changed. Althought the RemoveableMedia is unlikely to change,
486 it is also included here.
487
488 @param UsbMass The device to retrieve disk gemotric.
489
490 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
491 @retval Other Failed to get the parameters.
492
493 **/
494 EFI_STATUS
495 UsbBootGetParams (
496 IN USB_MASS_DEVICE *UsbMass
497 )
498 {
499 EFI_BLOCK_IO_MEDIA *Media;
500 EFI_STATUS Status;
501 UINT8 CmdSet;
502
503 Media = &(UsbMass->BlockIoMedia);
504 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
505
506 Status = UsbBootInquiry (UsbMass);
507 if (EFI_ERROR (Status)) {
508 DEBUG ((EFI_D_ERROR, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status));
509 return Status;
510 }
511
512 //
513 // Don't use the Removable bit in inquiry data to test whether the media
514 // is removable because many flash disks wrongly set this bit.
515 //
516 if ((UsbMass->Pdt == USB_PDT_CDROM) || (UsbMass->Pdt == USB_PDT_OPTICAL)) {
517 //
518 // CD-Rom device and Non-CD optical device
519 //
520 UsbMass->OpticalStorage = TRUE;
521 //
522 // Default value 2048 Bytes, in case no media present at first time
523 //
524 Media->BlockSize = 0x0800;
525 }
526
527 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
528 //
529 // ModeSense is required for the device with PDT of 0x00/0x07/0x0E,
530 // which is from [MassStorageBootabilitySpec-Page7].
531 // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI
532 // could get the information of WriteProtected.
533 // Since not all device support this command, so skip if fail.
534 //
535 UsbScsiModeSense (UsbMass);
536 }
537
538 return UsbBootReadCapacity (UsbMass);
539 }
540
541
542 /**
543 Detect whether the removable media is present and whether it has changed.
544
545 @param UsbMass The device to check.
546
547 @retval EFI_SUCCESS The media status is successfully checked.
548 @retval Other Failed to detect media.
549
550 **/
551 EFI_STATUS
552 UsbBootDetectMedia (
553 IN USB_MASS_DEVICE *UsbMass
554 )
555 {
556 EFI_BLOCK_IO_MEDIA OldMedia;
557 EFI_BLOCK_IO_MEDIA *Media;
558 UINT8 CmdSet;
559 EFI_TPL OldTpl;
560 EFI_STATUS Status;
561
562 Media = &UsbMass->BlockIoMedia;
563
564 CopyMem (&OldMedia, &(UsbMass->BlockIoMedia), sizeof (EFI_BLOCK_IO_MEDIA));
565
566 CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass;
567
568 Status = UsbBootIsUnitReady (UsbMass);
569 if (EFI_ERROR (Status)) {
570 goto ON_ERROR;
571 }
572
573 if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) {
574 //
575 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
576 // according to Section 4 of USB Mass Storage Specification for Bootability.
577 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
578 // could get the information of Write Protected.
579 // Since not all device support this command, skip if fail.
580 //
581 UsbScsiModeSense (UsbMass);
582 }
583
584 Status = UsbBootReadCapacity (UsbMass);
585 if (EFI_ERROR (Status)) {
586 DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status));
587 goto ON_ERROR;
588 }
589
590 return EFI_SUCCESS;
591
592 ON_ERROR:
593 //
594 // Detect whether it is necessary to reinstall the Block I/O Protocol.
595 //
596 // MediaId may change in RequestSense for MediaChanged
597 // MediaPresent may change in RequestSense for NoMedia
598 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
599 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
600 //
601 if ((Media->MediaId != OldMedia.MediaId) ||
602 (Media->MediaPresent != OldMedia.MediaPresent) ||
603 (Media->ReadOnly != OldMedia.ReadOnly) ||
604 (Media->BlockSize != OldMedia.BlockSize) ||
605 (Media->LastBlock != OldMedia.LastBlock)) {
606
607 //
608 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
609 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
610 //
611 OldTpl = EfiGetCurrentTpl ();
612 gBS->RestoreTPL (TPL_CALLBACK);
613
614 gBS->ReinstallProtocolInterface (
615 UsbMass->Controller,
616 &gEfiBlockIoProtocolGuid,
617 &UsbMass->BlockIo,
618 &UsbMass->BlockIo
619 );
620
621 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK);
622 gBS->RaiseTPL (OldTpl);
623
624 //
625 // Update MediaId after reinstalling Block I/O Protocol.
626 //
627 if (Media->MediaPresent != OldMedia.MediaPresent) {
628 if (Media->MediaPresent) {
629 Media->MediaId = 1;
630 } else {
631 Media->MediaId = 0;
632 }
633 }
634
635 if ((Media->ReadOnly != OldMedia.ReadOnly) ||
636 (Media->BlockSize != OldMedia.BlockSize) ||
637 (Media->LastBlock != OldMedia.LastBlock)) {
638 Media->MediaId++;
639 }
640 }
641
642 return Status;
643 }
644
645
646 /**
647 Read some blocks from the device.
648
649 @param UsbMass The USB mass storage device to read from
650 @param Lba The start block number
651 @param TotalBlock Total block number to read
652 @param Buffer The buffer to read to
653
654 @retval EFI_SUCCESS Data are read into the buffer
655 @retval Others Failed to read all the data
656
657 **/
658 EFI_STATUS
659 UsbBootReadBlocks (
660 IN USB_MASS_DEVICE *UsbMass,
661 IN UINT32 Lba,
662 IN UINTN TotalBlock,
663 OUT UINT8 *Buffer
664 )
665 {
666 USB_BOOT_READ10_CMD ReadCmd;
667 EFI_STATUS Status;
668 UINT16 Count;
669 UINT32 BlockSize;
670 UINT32 ByteSize;
671 UINT32 Timeout;
672
673 BlockSize = UsbMass->BlockIoMedia.BlockSize;
674 Status = EFI_SUCCESS;
675
676 while (TotalBlock > 0) {
677 //
678 // Split the total blocks into smaller pieces to ease the pressure
679 // on the device. We must split the total block because the READ10
680 // command only has 16 bit transfer length (in the unit of block).
681 //
682 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
683 ByteSize = (UINT32)Count * BlockSize;
684
685 //
686 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
687 //
688 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
689
690 //
691 // Fill in the command then execute
692 //
693 ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));
694
695 ReadCmd.OpCode = USB_BOOT_READ10_OPCODE;
696 ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
697 WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));
698 WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));
699
700 Status = UsbBootExecCmdWithRetry (
701 UsbMass,
702 &ReadCmd,
703 sizeof (USB_BOOT_READ10_CMD),
704 EfiUsbDataIn,
705 Buffer,
706 ByteSize,
707 Timeout
708 );
709 if (EFI_ERROR (Status)) {
710 return Status;
711 }
712
713 Lba += Count;
714 Buffer += Count * BlockSize;
715 TotalBlock -= Count;
716 }
717
718 return Status;
719 }
720
721
722 /**
723 Write some blocks to the device.
724
725 @param UsbMass The USB mass storage device to write to
726 @param Lba The start block number
727 @param TotalBlock Total block number to write
728 @param Buffer Pointer to the source buffer for the data.
729
730 @retval EFI_SUCCESS Data are written into the buffer
731 @retval Others Failed to write all the data
732
733 **/
734 EFI_STATUS
735 UsbBootWriteBlocks (
736 IN USB_MASS_DEVICE *UsbMass,
737 IN UINT32 Lba,
738 IN UINTN TotalBlock,
739 IN UINT8 *Buffer
740 )
741 {
742 USB_BOOT_WRITE10_CMD WriteCmd;
743 EFI_STATUS Status;
744 UINT16 Count;
745 UINT32 BlockSize;
746 UINT32 ByteSize;
747 UINT32 Timeout;
748
749 BlockSize = UsbMass->BlockIoMedia.BlockSize;
750 Status = EFI_SUCCESS;
751
752 while (TotalBlock > 0) {
753 //
754 // Split the total blocks into smaller pieces to ease the pressure
755 // on the device. We must split the total block because the WRITE10
756 // command only has 16 bit transfer length (in the unit of block).
757 //
758 Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);
759 ByteSize = (UINT32)Count * BlockSize;
760
761 //
762 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
763 //
764 Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;
765
766 //
767 // Fill in the write10 command block
768 //
769 ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
770
771 WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
772 WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
773 WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
774 WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
775
776 Status = UsbBootExecCmdWithRetry (
777 UsbMass,
778 &WriteCmd,
779 sizeof (USB_BOOT_WRITE10_CMD),
780 EfiUsbDataOut,
781 Buffer,
782 ByteSize,
783 Timeout
784 );
785 if (EFI_ERROR (Status)) {
786 return Status;
787 }
788
789 Lba += Count;
790 Buffer += Count * BlockSize;
791 TotalBlock -= Count;
792 }
793
794 return Status;
795 }
796
797 /**
798 Use the USB clear feature control transfer to clear the endpoint stall condition.
799
800 @param UsbIo The USB I/O Protocol instance
801 @param EndpointAddr The endpoint to clear stall for
802
803 @retval EFI_SUCCESS The endpoint stall condition is cleared.
804 @retval Others Failed to clear the endpoint stall condition.
805
806 **/
807 EFI_STATUS
808 UsbClearEndpointStall (
809 IN EFI_USB_IO_PROTOCOL *UsbIo,
810 IN UINT8 EndpointAddr
811 )
812 {
813 EFI_USB_DEVICE_REQUEST Request;
814 EFI_STATUS Status;
815 UINT32 CmdResult;
816 UINT32 Timeout;
817
818 Request.RequestType = 0x02;
819 Request.Request = USB_REQ_CLEAR_FEATURE;
820 Request.Value = USB_FEATURE_ENDPOINT_HALT;
821 Request.Index = EndpointAddr;
822 Request.Length = 0;
823 Timeout = USB_BOOT_GENERAL_CMD_TIMEOUT / USB_MASS_1_MILLISECOND;
824
825 Status = UsbIo->UsbControlTransfer (
826 UsbIo,
827 &Request,
828 EfiUsbNoData,
829 Timeout,
830 NULL,
831 0,
832 &CmdResult
833 );
834
835 return Status;
836 }