summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Bus/Usb/UsbBotPei/UsbBotPeim.c
blob: e110626886c412e135f36548b1f2ee9ea54f90b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/** @file

Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "UsbBotPeim.h"
#include "BotPeim.h"

//
// Global function
//
EFI_PEI_NOTIFY_DESCRIPTOR        mNotifyList = {
  EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  &gPeiUsbIoPpiGuid,
  NotifyOnUsbIoPpi
};

EFI_PEI_RECOVERY_BLOCK_IO_PPI    mRecoveryBlkIoPpi = {
  BotGetNumberOfBlockDevices,
  BotGetMediaInfo,
  BotReadBlocks
};

EFI_PEI_RECOVERY_BLOCK_IO2_PPI   mRecoveryBlkIo2Ppi = {
  EFI_PEI_RECOVERY_BLOCK_IO2_PPI_REVISION,
  BotGetNumberOfBlockDevices2,
  BotGetMediaInfo2,
  BotReadBlocks2
};

EFI_PEI_PPI_DESCRIPTOR           mPpiList[2] = {
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI,
    &gEfiPeiVirtualBlockIoPpiGuid,
    NULL
  },
  {
    EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
    &gEfiPeiVirtualBlockIo2PpiGuid,
    NULL
  }
};

/**
  Detect whether the removable media is present and whether it has changed.

  @param[in]  PeiServices   General-purpose services that are available to every
                            PEIM.
  @param[in]  PeiBotDev     Indicates the PEI_BOT_DEVICE instance.

  @retval EFI_SUCCESS       The media status is successfully checked.
  @retval Other             Failed to detect media.

**/
EFI_STATUS
PeiBotDetectMedia (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  PEI_BOT_DEVICE                            *PeiBotDev
  );

/**
  Initializes the Usb Bot.

  @param  FileHandle  Handle of the file being invoked.
  @param  PeiServices Describes the list of possible PEI Services.

  @retval EFI_SUCCESS            Usb bot driver is successfully initialized.
  @retval EFI_OUT_OF_RESOURCES   Can't initialize the driver.

**/
EFI_STATUS
EFIAPI
PeimInitializeUsbBot (
  IN EFI_PEI_FILE_HANDLE       FileHandle,
  IN CONST EFI_PEI_SERVICES    **PeiServices
  )
{
  EFI_STATUS                  Status;
  UINTN                       UsbIoPpiInstance;
  EFI_PEI_PPI_DESCRIPTOR      *TempPpiDescriptor;
  PEI_USB_IO_PPI              *UsbIoPpi;

  //
  // Shadow this PEIM to run from memory
  //
  if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle))) {
    return EFI_SUCCESS;
  }

  //
  // locate all usb io PPIs
  //
  for (UsbIoPpiInstance = 0; UsbIoPpiInstance < PEI_FAT_MAX_USB_IO_PPI; UsbIoPpiInstance++) {

    Status = PeiServicesLocatePpi (
                              &gPeiUsbIoPpiGuid,
                              UsbIoPpiInstance,
                              &TempPpiDescriptor,
                              (VOID **) &UsbIoPpi
                              );
    if (EFI_ERROR (Status)) {
      break;
    }
  }
  //
  // Register a notify function
  //
  return PeiServicesNotifyPpi (&mNotifyList);
}

/**
  UsbIo installation notification function.

  This function finds out all the current USB IO PPIs in the system and add them
  into private data.

  @param  PeiServices      Indirect reference to the PEI Services Table.
  @param  NotifyDesc       Address of the notification descriptor data structure.
  @param  InvokePpi        Address of the PPI that was invoked.

  @retval EFI_SUCCESS      The function completes successfully.

**/
EFI_STATUS
EFIAPI
NotifyOnUsbIoPpi (
  IN  EFI_PEI_SERVICES                              **PeiServices,
  IN  EFI_PEI_NOTIFY_DESCRIPTOR                     *NotifyDesc,
  IN  VOID                                          *InvokePpi
  )
{
  PEI_USB_IO_PPI  *UsbIoPpi;

  UsbIoPpi = (PEI_USB_IO_PPI *) InvokePpi;

  InitUsbBot (PeiServices, UsbIoPpi);

  return EFI_SUCCESS;
}

/**
  Initialize the usb bot device.

  @param[in]  PeiServices   General-purpose services that are available to every
                            PEIM.
  @param[in]  UsbIoPpi      Indicates the PEI_USB_IO_PPI instance.

  @retval EFI_SUCCESS       The usb bot device is initialized successfully.
  @retval Other             Failed to initialize media.

**/
EFI_STATUS
InitUsbBot (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  PEI_USB_IO_PPI                            *UsbIoPpi
  )
{
  PEI_BOT_DEVICE                *PeiBotDevice;
  EFI_STATUS                    Status;
  EFI_USB_INTERFACE_DESCRIPTOR  *InterfaceDesc;
  UINTN                         MemPages;
  EFI_PHYSICAL_ADDRESS          AllocateAddress;
  EFI_USB_ENDPOINT_DESCRIPTOR   *EndpointDesc;
  UINT8                         Index;

  //
  // Check its interface
  //
  Status = UsbIoPpi->UsbGetInterfaceDescriptor (
                      PeiServices,
                      UsbIoPpi,
                      &InterfaceDesc
                      );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Check if it is the BOT device we support
  //
  if ((InterfaceDesc->InterfaceClass != 0x08) || (InterfaceDesc->InterfaceProtocol != 0x50)) {

    return EFI_NOT_FOUND;
  }

  MemPages = sizeof (PEI_BOT_DEVICE) / EFI_PAGE_SIZE + 1;
  Status = PeiServicesAllocatePages (
             EfiBootServicesCode,
             MemPages,
             &AllocateAddress
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  PeiBotDevice                  = (PEI_BOT_DEVICE *) ((UINTN) AllocateAddress);

  PeiBotDevice->Signature       = PEI_BOT_DEVICE_SIGNATURE;
  PeiBotDevice->UsbIoPpi        = UsbIoPpi;
  PeiBotDevice->AllocateAddress = (UINTN) AllocateAddress;
  PeiBotDevice->BotInterface    = InterfaceDesc;

  //
  // Default value
  //
  PeiBotDevice->Media.DeviceType  = UsbMassStorage;
  PeiBotDevice->Media.BlockSize   = 0x200;
  PeiBotDevice->Media2.InterfaceType = MSG_USB_DP;
  PeiBotDevice->Media2.BlockSize     = 0x200;
  PeiBotDevice->Media2.RemovableMedia = FALSE;
  PeiBotDevice->Media2.ReadOnly       = FALSE;

  //
  // Check its Bulk-in/Bulk-out endpoint
  //
  for (Index = 0; Index < 2; Index++) {
    Status = UsbIoPpi->UsbGetEndpointDescriptor (
                        PeiServices,
                        UsbIoPpi,
                        Index,
                        &EndpointDesc
                        );

    if (EFI_ERROR (Status)) {
      return Status;
    }

    if ((EndpointDesc->EndpointAddress & 0x80) != 0) {
      PeiBotDevice->BulkInEndpoint = EndpointDesc;
    } else {
      PeiBotDevice->BulkOutEndpoint = EndpointDesc;
    }
  }

  CopyMem (
    &(PeiBotDevice->BlkIoPpi),
    &mRecoveryBlkIoPpi,
    sizeof (EFI_PEI_RECOVERY_BLOCK_IO_PPI)
    );
  CopyMem (
    &(PeiBotDevice->BlkIo2Ppi),
    &mRecoveryBlkIo2Ppi,
    sizeof (EFI_PEI_RECOVERY_BLOCK_IO2_PPI)
    );
  CopyMem (
    &(PeiBotDevice->BlkIoPpiList),
    &mPpiList[0],
    sizeof (EFI_PEI_PPI_DESCRIPTOR)
    );
  CopyMem (
    &(PeiBotDevice->BlkIo2PpiList),
    &mPpiList[1],
    sizeof (EFI_PEI_PPI_DESCRIPTOR)
    );
  PeiBotDevice->BlkIoPpiList.Ppi  = &PeiBotDevice->BlkIoPpi;
  PeiBotDevice->BlkIo2PpiList.Ppi = &PeiBotDevice->BlkIo2Ppi;

  Status                          = PeiUsbInquiry (PeiServices, PeiBotDevice);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = PeiServicesAllocatePages (
             EfiBootServicesCode,
             1,
             &AllocateAddress
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  PeiBotDevice->SensePtr = (ATAPI_REQUEST_SENSE_DATA *) ((UINTN) AllocateAddress);

  Status = PeiServicesInstallPpi (&PeiBotDevice->BlkIoPpiList);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  return EFI_SUCCESS;
}

/**
  Gets the count of block I/O devices that one specific block driver detects.

  This function is used for getting the count of block I/O devices that one
  specific block driver detects.  To the PEI ATAPI driver, it returns the number
  of all the detected ATAPI devices it detects during the enumeration process.
  To the PEI legacy floppy driver, it returns the number of all the legacy
  devices it finds during its enumeration process. If no device is detected,
  then the function will return zero.

  @param[in]  PeiServices          General-purpose services that are available
                                   to every PEIM.
  @param[in]  This                 Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
                                   instance.
  @param[out] NumberBlockDevices   The number of block I/O devices discovered.

  @retval     EFI_SUCCESS          Operation performed successfully.

**/
EFI_STATUS
EFIAPI
BotGetNumberOfBlockDevices (
  IN  EFI_PEI_SERVICES                         **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI            *This,
  OUT UINTN                                    *NumberBlockDevices
  )
{
  //
  // For Usb devices, this value should be always 1
  //
  *NumberBlockDevices = 1;
  return EFI_SUCCESS;
}

/**
  Gets a block device's media information.

  This function will provide the caller with the specified block device's media
  information. If the media changes, calling this function will update the media
  information accordingly.

  @param[in]  PeiServices   General-purpose services that are available to every
                            PEIM
  @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
  @param[in]  DeviceIndex   Specifies the block device to which the function wants
                            to talk. Because the driver that implements Block I/O
                            PPIs will manage multiple block devices, the PPIs that
                            want to talk to a single device must specify the
                            device index that was assigned during the enumeration
                            process. This index is a number from one to
                            NumberBlockDevices.
  @param[out] MediaInfo     The media information of the specified block media.
                            The caller is responsible for the ownership of this
                            data structure.

  @retval EFI_SUCCESS        Media information about the specified block device
                             was obtained successfully.
  @retval EFI_DEVICE_ERROR   Cannot get the media information due to a hardware
                             error.

**/
EFI_STATUS
EFIAPI
BotGetMediaInfo (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI             *This,
  IN  UINTN                                     DeviceIndex,
  OUT EFI_PEI_BLOCK_IO_MEDIA                    *MediaInfo
  )
{
  PEI_BOT_DEVICE  *PeiBotDev;
  EFI_STATUS      Status;

  PeiBotDev = PEI_BOT_DEVICE_FROM_THIS (This);

  //
  // First test unit ready
  //
  PeiUsbTestUnitReady (
    PeiServices,
    PeiBotDev
    );

  Status = PeiBotDetectMedia (
            PeiServices,
            PeiBotDev
            );

  if (EFI_ERROR (Status)) {
    return EFI_DEVICE_ERROR;
  }

  CopyMem (
    MediaInfo,
    &(PeiBotDev->Media),
    sizeof (EFI_PEI_BLOCK_IO_MEDIA)
    );

  return EFI_SUCCESS;
}

/**
  Reads the requested number of blocks from the specified block device.

  The function reads the requested number of blocks from the device. All the
  blocks are read, or an error is returned. If there is no media in the device,
  the function returns EFI_NO_MEDIA.

  @param[in]  PeiServices   General-purpose services that are available to
                            every PEIM.
  @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
  @param[in]  DeviceIndex   Specifies the block device to which the function wants
                            to talk. Because the driver that implements Block I/O
                            PPIs will manage multiple block devices, the PPIs that
                            want to talk to a single device must specify the device
                            index that was assigned during the enumeration process.
                            This index is a number from one to NumberBlockDevices.
  @param[in]  StartLBA      The starting logical block address (LBA) to read from
                            on the device
  @param[in]  BufferSize    The size of the Buffer in bytes. This number must be
                            a multiple of the intrinsic block size of the device.
  @param[out] Buffer        A pointer to the destination buffer for the data.
                            The caller is responsible for the ownership of the
                            buffer.

  @retval EFI_SUCCESS             The data was read correctly from the device.
  @retval EFI_DEVICE_ERROR        The device reported an error while attempting
                                  to perform the read operation.
  @retval EFI_INVALID_PARAMETER   The read request contains LBAs that are not
                                  valid, or the buffer is not properly aligned.
  @retval EFI_NO_MEDIA            There is no media in the device.
  @retval EFI_BAD_BUFFER_SIZE     The BufferSize parameter is not a multiple of
                                  the intrinsic block size of the device.

**/
EFI_STATUS
EFIAPI
BotReadBlocks (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI             *This,
  IN  UINTN                                     DeviceIndex,
  IN  EFI_PEI_LBA                               StartLBA,
  IN  UINTN                                     BufferSize,
  OUT VOID                                      *Buffer
  )
{
  PEI_BOT_DEVICE  *PeiBotDev;
  EFI_STATUS      Status;
  UINTN           BlockSize;
  UINTN           NumberOfBlocks;

  Status    = EFI_SUCCESS;
  PeiBotDev = PEI_BOT_DEVICE_FROM_THIS (This);

  //
  // Check parameters
  //
  if (Buffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (BufferSize == 0) {
    return EFI_SUCCESS;
  }

  if (!PeiBotDev->Media.MediaPresent) {
    return EFI_NO_MEDIA;
  }

  BlockSize = PeiBotDev->Media.BlockSize;

  if (BufferSize % BlockSize != 0) {
    Status = EFI_BAD_BUFFER_SIZE;
  }

  if (StartLBA > PeiBotDev->Media2.LastBlock) {
    Status = EFI_INVALID_PARAMETER;
  }

  NumberOfBlocks = BufferSize / (PeiBotDev->Media.BlockSize);

  if (Status == EFI_SUCCESS) {

    Status = PeiUsbTestUnitReady (
              PeiServices,
              PeiBotDev
              );
    if (Status == EFI_SUCCESS) {
      Status = PeiUsbRead10 (
                PeiServices,
                PeiBotDev,
                Buffer,
                StartLBA,
                1
                );
    }
  } else {
    //
    // To generate sense data for DetectMedia use.
    //
    PeiUsbTestUnitReady (
      PeiServices,
      PeiBotDev
      );
  }

  if (EFI_ERROR (Status)) {
    //
    // if any error encountered, detect what happened to the media and
    // update the media info accordingly.
    //
    Status = PeiBotDetectMedia (
              PeiServices,
              PeiBotDev
              );
    if (Status != EFI_SUCCESS) {
      return EFI_DEVICE_ERROR;
    }

    NumberOfBlocks = BufferSize / PeiBotDev->Media.BlockSize;

    if (!(PeiBotDev->Media.MediaPresent)) {
      return EFI_NO_MEDIA;
    }

    if (BufferSize % (PeiBotDev->Media.BlockSize) != 0) {
      return EFI_BAD_BUFFER_SIZE;
    }

    if (StartLBA > PeiBotDev->Media2.LastBlock) {
      return EFI_INVALID_PARAMETER;
    }

    if ((StartLBA + NumberOfBlocks - 1) > PeiBotDev->Media2.LastBlock) {
      return EFI_INVALID_PARAMETER;
    }

    Status = PeiUsbRead10 (
              PeiServices,
              PeiBotDev,
              Buffer,
              StartLBA,
              NumberOfBlocks
              );

    switch (Status) {

    case EFI_SUCCESS:
      return EFI_SUCCESS;

    default:
      return EFI_DEVICE_ERROR;
    }
  } else {
    StartLBA += 1;
    NumberOfBlocks -= 1;
    Buffer = (UINT8 *) Buffer + PeiBotDev->Media.BlockSize;

    if (NumberOfBlocks == 0) {
      return EFI_SUCCESS;
    }

    Status = PeiUsbRead10 (
              PeiServices,
              PeiBotDev,
              Buffer,
              StartLBA,
              NumberOfBlocks
              );
    switch (Status) {

    case EFI_SUCCESS:
      return EFI_SUCCESS;

    default:
      return EFI_DEVICE_ERROR;

    }
  }
}

/**
  Gets the count of block I/O devices that one specific block driver detects.

  This function is used for getting the count of block I/O devices that one
  specific block driver detects.  To the PEI ATAPI driver, it returns the number
  of all the detected ATAPI devices it detects during the enumeration process.
  To the PEI legacy floppy driver, it returns the number of all the legacy
  devices it finds during its enumeration process. If no device is detected,
  then the function will return zero.

  @param[in]  PeiServices          General-purpose services that are available
                                   to every PEIM.
  @param[in]  This                 Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
                                   instance.
  @param[out] NumberBlockDevices   The number of block I/O devices discovered.

  @retval     EFI_SUCCESS          Operation performed successfully.

**/
EFI_STATUS
EFIAPI
BotGetNumberOfBlockDevices2 (
  IN  EFI_PEI_SERVICES                         **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI           *This,
  OUT UINTN                                    *NumberBlockDevices
  )
{
  //
  // For Usb devices, this value should be always 1
  //
  *NumberBlockDevices = 1;
  return EFI_SUCCESS;
}

/**
  Gets a block device's media information.

  This function will provide the caller with the specified block device's media
  information. If the media changes, calling this function will update the media
  information accordingly.

  @param[in]  PeiServices   General-purpose services that are available to every
                            PEIM
  @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
  @param[in]  DeviceIndex   Specifies the block device to which the function wants
                            to talk. Because the driver that implements Block I/O
                            PPIs will manage multiple block devices, the PPIs that
                            want to talk to a single device must specify the
                            device index that was assigned during the enumeration
                            process. This index is a number from one to
                            NumberBlockDevices.
  @param[out] MediaInfo     The media information of the specified block media.
                            The caller is responsible for the ownership of this
                            data structure.

  @retval EFI_SUCCESS        Media information about the specified block device
                             was obtained successfully.
  @retval EFI_DEVICE_ERROR   Cannot get the media information due to a hardware
                             error.

**/
EFI_STATUS
EFIAPI
BotGetMediaInfo2 (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI            *This,
  IN  UINTN                                     DeviceIndex,
  OUT EFI_PEI_BLOCK_IO2_MEDIA                   *MediaInfo
  )
{
  PEI_BOT_DEVICE  *PeiBotDev;
  EFI_STATUS      Status;

  PeiBotDev = PEI_BOT_DEVICE2_FROM_THIS (This);

  Status = BotGetMediaInfo (
             PeiServices,
             &PeiBotDev->BlkIoPpi,
             DeviceIndex,
             &PeiBotDev->Media
             );

  if (EFI_ERROR (Status)) {
    return Status;
  }

  CopyMem (
    MediaInfo,
    &(PeiBotDev->Media2),
    sizeof (EFI_PEI_BLOCK_IO2_MEDIA)
    );

  return EFI_SUCCESS;
}

/**
  Reads the requested number of blocks from the specified block device.

  The function reads the requested number of blocks from the device. All the
  blocks are read, or an error is returned. If there is no media in the device,
  the function returns EFI_NO_MEDIA.

  @param[in]  PeiServices   General-purpose services that are available to
                            every PEIM.
  @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
  @param[in]  DeviceIndex   Specifies the block device to which the function wants
                            to talk. Because the driver that implements Block I/O
                            PPIs will manage multiple block devices, the PPIs that
                            want to talk to a single device must specify the device
                            index that was assigned during the enumeration process.
                            This index is a number from one to NumberBlockDevices.
  @param[in]  StartLBA      The starting logical block address (LBA) to read from
                            on the device
  @param[in]  BufferSize    The size of the Buffer in bytes. This number must be
                            a multiple of the intrinsic block size of the device.
  @param[out] Buffer        A pointer to the destination buffer for the data.
                            The caller is responsible for the ownership of the
                            buffer.

  @retval EFI_SUCCESS             The data was read correctly from the device.
  @retval EFI_DEVICE_ERROR        The device reported an error while attempting
                                  to perform the read operation.
  @retval EFI_INVALID_PARAMETER   The read request contains LBAs that are not
                                  valid, or the buffer is not properly aligned.
  @retval EFI_NO_MEDIA            There is no media in the device.
  @retval EFI_BAD_BUFFER_SIZE     The BufferSize parameter is not a multiple of
                                  the intrinsic block size of the device.

**/
EFI_STATUS
EFIAPI
BotReadBlocks2 (
  IN  EFI_PEI_SERVICES                          **PeiServices,
  IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI            *This,
  IN  UINTN                                     DeviceIndex,
  IN  EFI_PEI_LBA                               StartLBA,
  IN  UINTN                                     BufferSize,
  OUT VOID                                      *Buffer
  )
{
  PEI_BOT_DEVICE  *PeiBotDev;
  EFI_STATUS      Status;

  if (This == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status    = EFI_SUCCESS;
  PeiBotDev = PEI_BOT_DEVICE2_FROM_THIS (This);

  Status = BotReadBlocks (
             PeiServices,
             &PeiBotDev->BlkIoPpi,
             DeviceIndex,
             StartLBA,
             BufferSize,
             Buffer
             );

  return Status;
}

/**
  Detect whether the removable media is present and whether it has changed.

  @param[in]  PeiServices   General-purpose services that are available to every
                            PEIM.
  @param[in]  PeiBotDev     Indicates the PEI_BOT_DEVICE instance.

  @retval EFI_SUCCESS       The media status is successfully checked.
  @retval Other             Failed to detect media.

**/
EFI_STATUS
PeiBotDetectMedia (
  IN  EFI_PEI_SERVICES        **PeiServices,
  IN  PEI_BOT_DEVICE          *PeiBotDev
  )
{
  EFI_STATUS                  Status;
  EFI_STATUS                  FloppyStatus;
  UINTN                       SenseCounts;
  BOOLEAN                     NeedReadCapacity;
  EFI_PHYSICAL_ADDRESS        AllocateAddress;
  ATAPI_REQUEST_SENSE_DATA    *SensePtr;
  UINTN                       Retry;

  //
  // if there is no media present,or media not changed,
  // the request sense command will detect faster than read capacity command.
  // read capacity command can be bypassed, thus improve performance.
  //
  SenseCounts       = 0;
  NeedReadCapacity  = TRUE;

  Status = PeiServicesAllocatePages (
             EfiBootServicesCode,
             1,
             &AllocateAddress
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  SensePtr = PeiBotDev->SensePtr;
  ZeroMem (SensePtr, EFI_PAGE_SIZE);

  Status = PeiUsbRequestSense (
            PeiServices,
            PeiBotDev,
            &SenseCounts,
            (UINT8 *) SensePtr
            );

  if (Status == EFI_SUCCESS) {
    //
    // No Media
    //
    if (IsNoMedia (SensePtr, SenseCounts)) {
      NeedReadCapacity              = FALSE;
      PeiBotDev->Media.MediaPresent = FALSE;
      PeiBotDev->Media.LastBlock    = 0;
      PeiBotDev->Media2.MediaPresent = FALSE;
      PeiBotDev->Media2.LastBlock    = 0;
    } else {
      //
      // Media Changed
      //
      if (IsMediaChange (SensePtr, SenseCounts)) {
        PeiBotDev->Media.MediaPresent  = TRUE;
        PeiBotDev->Media2.MediaPresent = TRUE;
      }
      //
      // Media Error
      //
      if (IsMediaError (SensePtr, SenseCounts)) {
        //
        // if media error encountered, make it look like no media present.
        //
        PeiBotDev->Media.MediaPresent = FALSE;
        PeiBotDev->Media.LastBlock    = 0;
        PeiBotDev->Media2.MediaPresent = FALSE;
        PeiBotDev->Media2.LastBlock    = 0;
      }

    }

  }

  if (NeedReadCapacity) {
    //
    // Retry at most 4 times to detect media info
    //
    for (Retry = 0; Retry < 4; Retry++) {
      switch (PeiBotDev->DeviceType) {
      case USBCDROM:
        Status = PeiUsbReadCapacity (
                  PeiServices,
                  PeiBotDev
                  );
        break;

      case USBFLOPPY2:
        Status = PeiUsbReadFormattedCapacity (
                  PeiServices,
                  PeiBotDev
                  );
          if (EFI_ERROR(Status)||
              !PeiBotDev->Media.MediaPresent) {
          //
          // retry the ReadCapacity command
          //
          PeiBotDev->DeviceType = USBFLOPPY;
          Status = EFI_DEVICE_ERROR;
        }
        break;

      case USBFLOPPY:
        Status = PeiUsbReadCapacity (
                  PeiServices,
                  PeiBotDev
                  );
        if (EFI_ERROR (Status)) {
          //
          // retry the ReadFormatCapacity command
          //
          PeiBotDev->DeviceType = USBFLOPPY2;
        }
        break;

      default:
        return EFI_INVALID_PARAMETER;
      }

      SenseCounts = 0;
      ZeroMem (SensePtr, EFI_PAGE_SIZE);

      if (Status == EFI_SUCCESS) {
        break;
      }

      FloppyStatus = PeiUsbRequestSense (
                      PeiServices,
                      PeiBotDev,
                      &SenseCounts,
                      (UINT8 *) SensePtr
                      );

      //
      // If Request Sense data failed,retry.
      //
      if (EFI_ERROR (FloppyStatus)) {
        continue;
      }
      //
      // No Media
      //
      if (IsNoMedia (SensePtr, SenseCounts)) {
        PeiBotDev->Media.MediaPresent = FALSE;
        PeiBotDev->Media.LastBlock    = 0;
        PeiBotDev->Media2.MediaPresent = FALSE;
        PeiBotDev->Media2.LastBlock    = 0;
        break;
      }

      if (IsMediaError (SensePtr, SenseCounts)) {
        //
        // if media error encountered, make it look like no media present.
        //
        PeiBotDev->Media.MediaPresent = FALSE;
        PeiBotDev->Media.LastBlock    = 0;
        PeiBotDev->Media2.MediaPresent = FALSE;
        PeiBotDev->Media2.LastBlock    = 0;
        break;
      }
    }
    //
    // ENDFOR
    //
    // tell whether the readcapacity process is successful or not
    // ("Status" variable record the latest status returned
    // by ReadCapacity )
    //
    if (Status != EFI_SUCCESS) {
      return EFI_DEVICE_ERROR;
    }
  }

  return EFI_SUCCESS;
}