summaryrefslogtreecommitdiff
path: root/Core/EM/usb/int13/UsbInt13.c
blob: bae3b214c9718e9dbb1be9dc6a8ff11c45cc25d1 (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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2014, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************

//****************************************************************************
// $Header: /Alaska/SOURCE/Modules/USB/ALASKA/Int13/UsbInt13.c 30    5/26/15 10:47p Wilsonlee $
//
// $Revision: 30 $
//
// $Date: 5/26/15 10:47p $
//
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/USB/ALASKA/Int13/UsbInt13.c $
// 
// 30    5/26/15 10:47p Wilsonlee
// [TAG]  		EIP219177
// [Category]  	Improvement
// [Description]  	Fixed static code analysis issues in Usb module.
// [Files]  		UsbInt13.c, ehci.c, usbbus.c
// 
// 29    12/24/14 1:12a Wilsonlee
// [TAG]  		EIP192517
// [Category]  	Improvement
// [Description]  	USB Driver handles 0x100 NumHeads as a valid value.
// [Files]  		usbmass.c, usbdef.h, UsbInt13.c, UsbInt13.h, UI13.bin,
// Bfiusb.equ, Bfiusb.asm
// 
// 28    4/30/14 5:29a Wilsonlee
// [TAG]  		EIP164842
// [Category]  	Improvement
// [Description]  	Check if the devices have put into to our queue before
// we put them.
// [Files]  		UsbInt13.c, amiusb.c, ehci.c, ohci.c, usb.c, usbdef.h,
// usbmass.c, xhci.c, amiusbhc.c, efiusbmass.c, uhcd.c, usbbus.c, usbsb.c
// 
// 27    12/15/13 10:13p Wilsonlee
// [TAG]  		EIP136594
// [Category]  	New Feature
// [Description]  	Support 64 bits LBA of usb mass storages.
// [Files]  		Bfiusb.asm, Bfiusb.equ, UsbInt13.c, UsbInt13.h, amiusb.c,
// usbdef.h, usbmass.c, UsbMass.h, efiusbmass.c, UI13.bin
// 
// 26    11/25/13 10:56p Wilsonlee
// [TAG]  		EIP142992
// [Category]  	Improvement
// [Description]  	Install usb legacy boot device only if the class is
// mass storage.
// [Files]  		UsbInt13.c
// 
// 25    7/23/13 2:11a Wilsonlee
// [TAG]  		EIP127941
// [Category]  	Improvement
// [Description]  	Replace UI13HDDFunc08 with UI13FDDFunc08 if the media
// descriptor is a fixed disk.
// [Files]  		UsbInt13.h, UsbInt13.c, usbmass.c, usbdef.h, Bfiusb.asm,
// Bfiusb.equ
// 
// 24    7/03/13 5:21a Ryanchou
// [TAG]  		EIP123988
// [Category]  	Improvement
// [Description]  	Move the code creating BBS table to end of POST.
// [Files]  		UsbBbs.c, UsbInt13.c, UsbInt13.cif, UsbInt13.h,
// UsbInt13.mak, UsbInt13.sdl, efiusbmass.c, uhcd.c, uhcd.h,
// AmiUsbController.h
// 
// 23    3/14/12 11:34a Ryanchou
// [TAG]  		EIP83895
// [Category]  	Improvement
// [Description]  	Null-pointer dereference if installation of USBINT13
// Legacy BIN fails.
// [Files]  		UsbInt13.c
// 
// 22    1/06/12 12:59a Rajeshms
// [TAG]  		EIP62737 
// [Category]  	Improvement
// [Description]  	Added USB Device number into USB mass device name
// string based on SDL Token.
// [Files]  		Usb.sdl, usbport.c, usbmass.c, UsbInt13.h, UsbInt13.c,
// usbbus.c, Bfiusb.equ
// 
// 21    11/04/11 7:51a Ryanchou
// [TAG]  		EIP73024
// [Category]  	Improvement
// [Description]  	Initial the variable value.
// [Files]  		UsbInt13.c
// 
// 20    10/14/11 4:53a Rajeshms
// [TAG]  		EIP70214
// [Category]  	New Feature
// [Description]  	 USB Devices will be reported as BBS_USB devices if
// BBS_USB_DEVICE_TYPE_SUPPORT is enabled and in setup USB devices will
// also be shown under  USB Device BBS Priorities in Boot Page.
// [Files]  		UsbInt13.c
// 
// 19    7/13/11 4:15a Ryanchou
// [TAG]  		EIP59332
// [Category]  	Improvement
// [Description]  	Modified the Stop function for UHCD and USBBUS to
// properly stop devices and uninstall the protocols.
// [Files]  		uhcd.c, uhcd.h, uhci.c, usbbus.c, UsbInt13.c, usbmisc.c
// 
// 18    3/04/11 11:42a Olegi
// [TAG]  		EIP51927
// [Category]  	Improvement
// [Description]  	Support for the interface path reporting in USB INT13
// function 48.
// [Files]  		UsbInt13.c
// UsbInt13.h
// 
// 17    10/28/10 12:28a Ryanchou
// EIP45643: Fixed system hangs when hot plug USB mass storage quickly on
// USB 3.0 port.
// 
// 16    6/03/09 2:22p Olegi
// 
// 15    5/21/09 5:13p Olegi
// Added hotplug devices support.
// 
// 14    1/21/09 3:39p Olegi
// Bugfix in the previous change.
// 
// 13    1/21/09 10:57a Olegi
// Dependency on AMIUSB and CSM has been added, callback notification is
// removed.
// 
// 12    1/13/09 1:18p Olegi
// Bugfix in the previous change related to EIP#18693.
// 
// 11    1/13/09 11:44a Olegi
// Modified the callback notify function installation to cover the
// situation when the protocol we want to get notified is already
// installed. EIP#18693.
// 
// 10    12/05/08 9:51a Olegi
// GetBbsInfo call moved to ReadyToBoot callback.
// 
// 9     11/25/08 6:01p Olegi
// Support for OEM USB Boot Override feature. EIP#17052.
// 
// 8     11/07/08 9:28a Olegi
// 
// 7     11/07/08 9:06a Olegi
// 
// 6     11/03/08 3:53p Olegi
// 
// 5     11/03/08 9:53a Olegi
// Update USB SMM data information.
// 
// 4     10/24/08 3:06p Olegi
// 
// 3     6/27/08 5:49p Olegi
// 
// 2     6/27/08 5:48p Olegi
// 
// 1     5/16/08 12:12p Olegi
// First check-in.
// 
//****************************************************************************

//<AMI_FHDR_START>
//****************************************************************************
//
//  Name:           UsbInt13.c
//  Description:    USB Int13 driver
//
//****************************************************************************
//<AMI_FHDR_END>

#include "AmiDxeLib.h"

#include "Protocol\AmiUsbController.h"
#include <Protocol\ComponentName.h>
#include <Protocol\LegacyBiosExt.h>
#include <Protocol\LegacyBios.h>
#include "UsbInt13.h"
#include "token.h"
#include "pci.h"
#include "usbdef.h"
#include "protocol\usbpolicy.h"

EFI_STATUS InitInt13RuntimeImage();

EFI_LEGACY_BIOS_EXT_PROTOCOL        *gBiosExtensions = NULL;
LEGACY16_TO_EFI_DATA_TABLE_STRUC    *gLegacy16Data = 0;
BOOLEAN                             gCdromInstalled = FALSE;
UINT13_DATA                         *gI13BinData = NULL;
EFI_USB_PROTOCOL                    *gAmiUsb = NULL;
UINT8                               gBootOverrideDeviceIndx = 0;
USB_GLOBAL_DATA                     *gUsbData = NULL;


UINT8   gHotplugFddName[] = "USB Hotplug FDD";
USB_MASS_DEV gHotplugFloppy;

UINT8   gHotplugHddName[] = "USB Hotplug HDD";
USB_MASS_DEV gHotplugHardDrive;

UINT8   gHotplugCdromName[] = "USB Hotplug CDROM";
USB_MASS_DEV gHotplugCDROM;

USB_PCI_LOCATION* gUsbPciLocationTable = NULL;
            
//<AMI_PHDR_START>
//---------------------------------------------------------------------------
// Procedure:	UsbInt13EntryPoint
//
// Description:	USB INT13 driver entry point. Installs callback notification
//              on gEfiUSBProtocolGuid installation.
//
// Input:		IN EFI_HANDLE        ImageHandle,
//				IN EFI_SYSTEM_TABLE  *SystemTable
//
// Output:		EFI_STATUS
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
UsbInt13EntryPoint(
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable
)
{
    EFI_EVENT   Event;
    EFI_STATUS  Status;

    InitAmiLib(ImageHandle, SystemTable);

    Status = pBS->LocateProtocol(&gEfiUsbProtocolGuid, NULL, &gAmiUsb);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) return Status;

    gUsbData = gAmiUsb->USBDataPtr;

    Status = InitInt13RuntimeImage();
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) return Status;

    gAmiUsb->InstallUsbLegacyBootDevices = InstallUsbLegacyBootDevices;
    gAmiUsb->UsbInstallLegacyDevice = UsbInstallLegacyDevice;
    gAmiUsb->UsbUninstallLegacyDevice = UsbUninstallLegacyDevice;

    Status = CreateReadyToBootEvent(TPL_CALLBACK, ReadyToBootNotify, NULL, &Event);
    ASSERT_EFI_ERROR(Status);

    return Status;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
//  Name:           InitInt13RuntimeImage
//
//  Description:    Initialization of data structures and placement of runtime
//                  code of USB INT13
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
InitInt13RuntimeImage()
{
    EFI_STATUS  Status;
    VOID        *Image;
    UINTN       ImageSize = 0;

    //
    // Check the version of CSM16, support is available for ver 7.55 or later
    //
    {
        UINT8 MjCsmVer = *(UINT8*)0xF0018;
        UINT8 MnCsmVer = *(UINT8*)0xF0019;

        if (MjCsmVer<8 && MnCsmVer<0x55) return EFI_UNSUPPORTED;
    }

    gLegacy16Data = (LEGACY16_TO_EFI_DATA_TABLE_STRUC*)(UINTN)(0xF0000 + *(UINT16*)0xFFF4C);

    //
    // Get the USB INT13 runtime image
    //
    Status = pBS->LocateProtocol(
        &gEfiLegacyBiosExtProtocolGuid, NULL, &gBiosExtensions);
    if (EFI_ERROR(Status)) return Status;

    Status = gBiosExtensions->GetEmbeddedRom(
        CSM16_MODULEID, CSM16_VENDORID, CSM16_USB_RT_DID, &Image, &ImageSize);
    if (EFI_ERROR(Status)) return Status;

    //
    // Do the necessary RT data initialization here using Image before it is shadowed
    //..............................
    {
#pragma pack(push, 1)
        // Update USB SMI information
        typedef struct _USB_SMM_RTS {
            UINT8   MiscInfo;
            UINT16  SmmAttr;
            UINT32  SmmPort;
            UINT32  SmmData;
        } USB_SMM_RTS;
    
        static USB_SMM_RTS UsbSmmRt = {1, 0, SW_SMI_IO_ADDRESS, USB_SWSMI};
    
        *(USB_SMM_RTS*)((UINTN)Image + ((UINT13_DATA*)Image)->UsbSmmDataOffset) = UsbSmmRt;

#pragma pack(pop)
    }

    // Copy image to shadow E000/F000 area
    (UINTN)gI13BinData = gBiosExtensions->CopyLegacyTable(Image, (UINT16)ImageSize, 0x10, 2);
    if (gI13BinData == NULL) {
        return EFI_OUT_OF_RESOURCES;
    }

    gUsbPciLocationTable =
        (USB_PCI_LOCATION*)((UINTN)gI13BinData + ((UINT13_DATA*)gI13BinData)->UsbPciLocationTableOffset);

    return EFI_SUCCESS;
}



BOOLEAN
HotplugEnabled (
    HOTPLUG_DEVICE DeviceType
)
{
    if (DeviceType == Floppy)
    {
        return ((gUsbData->fdd_hotplug_support == SETUP_DATA_HOTPLUG_ENABLED) ||
            (gUsbData->fdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO));
    }

    if (DeviceType == HardDrive)
    {
        return ((gUsbData->hdd_hotplug_support == SETUP_DATA_HOTPLUG_ENABLED) ||
            (gUsbData->hdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO));
    }

    if (DeviceType == CDROM)
    {
        return ((gUsbData->cdrom_hotplug_support == SETUP_DATA_HOTPLUG_ENABLED) ||
            (gUsbData->cdrom_hotplug_support == SETUP_DATA_HOTPLUG_AUTO));
    }

    return FALSE;
}



EFI_STATUS
InitializeHotplugDevices()
{
    gHotplugFloppy.DevInfo = &gUsbData->FddHotplugDev;
    gHotplugFloppy.LogicalAddress = USB_HOTPLUG_FDD_ADDRESS;
    gHotplugFloppy.Handle = NULL;
    gHotplugFloppy.PciBDF = 0xffff;
    gHotplugFloppy.DevString = gHotplugFddName;
    gHotplugFloppy.StorageType = USB_MASS_DEV_ARMD;

    gHotplugHardDrive.DevInfo = &gUsbData->HddHotplugDev;
    gHotplugHardDrive.LogicalAddress = USB_HOTPLUG_HDD_ADDRESS;
    gHotplugHardDrive.Handle = NULL;
    gHotplugHardDrive.PciBDF = 0xffff;
    gHotplugHardDrive.DevString = gHotplugHddName;
    gHotplugHardDrive.StorageType = USB_MASS_DEV_HDD;

    gHotplugCDROM.DevInfo = &gUsbData->CdromHotplugDev;
    gHotplugCDROM.LogicalAddress = USB_HOTPLUG_CDROM_ADDRESS;
    gHotplugCDROM.Handle = NULL;
    gHotplugCDROM.PciBDF = 0xffff;
    gHotplugCDROM.DevString = gHotplugCdromName;
    gHotplugCDROM.StorageType = USB_MASS_DEV_CDROM;

    if (HotplugEnabled(Floppy))
        UsbInstallLegacyDevice(&gHotplugFloppy);

    if (HotplugEnabled(HardDrive))
        UsbInstallLegacyDevice(&gHotplugHardDrive);

    if (HotplugEnabled(CDROM))
        UsbInstallLegacyDevice(&gHotplugCDROM);

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        ReadyToBootNotify
//
// Description: READY_TO_BOOT event notification callback. It locates BBS
//              table and changes the priority of device located at index
//              gBootOverrideDeviceIndx to 0 (highest). It also verifies
//              the hotplug devices are properly installed.
//
// Input:       Event - event signaled by the DXE Core upon installation
//              Context - event context
//
// Output:    Nothing
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ReadyToBootNotify(EFI_EVENT Event, VOID *Context)
{
    UINT16      Priority;
    UINT16      Index, Index1;
    BBS_TABLE   *BbsTable = NULL;
    UINT16      HddCount;
    HDD_INFO    *HddInfo;
    UINT16      BbsCount;
    EFI_STATUS  Status;
    EFI_LEGACY_BIOS_PROTOCOL        *Bios = NULL;

    //
    // Find BBS table pointer
    //
    Status = pBS->LocateProtocol(
        &gEfiLegacyBiosProtocolGuid, NULL, &Bios);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) return;

    Status = Bios->GetBbsInfo(Bios, &HddCount, &HddInfo, &BbsCount, &BbsTable);
    ASSERT_EFI_ERROR(Status);

    //
    // Kill the Event.
    //
    pBS->CloseEvent( Event );
    
    //
    // Report BBS_USB type devices as other normal Boot devices
    // like HDD/CDROM/Floppy based on the storagetype by changing
    // the devicetype in the BBS table.
    //
#if BBS_USB_DEVICE_TYPE_SUPPORT
    for(Index=0; Index<BbsCount; Index++) {
        if(BbsTable[Index].DeviceType == BBS_USB) {
            switch( (((BbsTable[Index].InitPerReserved) >> 24) & 0xf) ) {
                case BAID_TYPE_RMD_FDD:
                    BbsTable[Index].DeviceType = BBS_FLOPPY;
                    break;
                case BAID_TYPE_RMD_HDD:
                    BbsTable[Index].DeviceType = BBS_HARDDISK;
                    break;
                case BAID_TYPE_CDROM:
                    BbsTable[Index].DeviceType = BBS_CDROM;
                    break;
                default:
                    BbsTable[Index].DeviceType = BBS_UNKNOWN;
            }
        }
    }
#endif

    if (gBootOverrideDeviceIndx == 0)
    {
        return; // No override needed
    }
    

    //
    // Find a device with the highest priority and swap it with priority of
    // a device located at gBootOverrideDeviceIndex.
    //
    Priority = BbsTable[gBootOverrideDeviceIndx].BootPriority;

    for (Index = 0, Index1 = MAX_BBS_ENTRIES_NO;
        Index < MAX_BBS_ENTRIES_NO;
        Index++)
    {
        if (Index == gBootOverrideDeviceIndx) continue;

        if (BbsTable[Index].BootPriority < Priority)
        {
            Index1 = Index;
        }
    }
    //
    // Index1 has entry with lowest priority, otherwise MAX_BBS_ENTRIES_NO
    //
    if (Index1 < MAX_BBS_ENTRIES_NO)
    {
        BbsTable[gBootOverrideDeviceIndx].BootPriority = BbsTable[Index1].BootPriority;
        BbsTable[Index1].BootPriority = Priority;
    }
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        CreateDeviceName
//
// Description: This function retrieves USB device name, copies it into
//              lower memory and returns a pointer to the string.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateDeviceName(
    UINT8   DevIndex,
    UINT8   *DevNameStringSrc,
    UINT16  *StringDestinationSegment,
    UINT16  *StringDestinationOffset,
    UINT16  *MfgStringDestinationSegment,
    UINT16  *MfgStringDestinationOffset
)
{
    UINT8 *DevName = (gI13BinData->UsbMassI13Dev)[DevIndex].DeviceNameString;
    UINT8 i;

    //
    // Copy the string, compact it on the way (no more that one ' ' in a row)
    //
    for (i=0; i < 63 && *DevNameStringSrc != 0; i++, DevNameStringSrc++)
    {
        if ((*DevNameStringSrc == 0x20) && (*(DevNameStringSrc-1) == 0x20)) continue;
        *DevName++ = *DevNameStringSrc;  // DevNameStringSrc incremented unconditionally
    }
    *DevName = 0;   // string terminator

    DevName = (gI13BinData->UsbMassI13Dev)[DevIndex].DeviceNameString;

    *StringDestinationSegment = (UINT16)(((UINTN)DevName & 0xf0000) >> 4);
    *StringDestinationOffset = (UINT16)((UINTN)DevName & 0xffff);

    *MfgStringDestinationSegment = (UINT16)(((UINTN)gI13BinData->MfgGenericName & 0xf0000) >> 4);
    *MfgStringDestinationOffset = (UINT16)((UINTN)gI13BinData->MfgGenericName & 0xffff);

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        CreateBbsEntry
//
// Description: This function takes the device index within USBMASS_INT13_DEV
//              list and prepares BBS entry for this device.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateBbsEntry(
    UINT8           DevIndex,
    IN USB_MASS_DEV *UsbMassDevice,
    OUT BBS_TABLE   *BbsEntry
)
{
    EFI_STATUS  Status;
    UINT8   Handle;
    UINT8   DevAndSysType;
    UINT8   BaidDeviceType = 0;					//(EIP73024)
    BBS_STATUS_FLAGS    StatusFlags = {0};		//(EIP73024)
    UINT16  CheckForUsbCdromOffset;
    UINT32  CheckForUsbCdromAddress;
    UINT8   *PatchAddr;

    ASSERT(DevIndex < USBDEVS_MAX_ENTRIES);

    if (gBiosExtensions == NULL) return EFI_NOT_FOUND;

    pBS->SetMem(BbsEntry, sizeof(BBS_TABLE), 0);

    //
    // Get the HC PCI location
    //
    BbsEntry->Bus = (UINT32)(UsbMassDevice->PciBDF >> 8);
    BbsEntry->Device = (UINT32)((UsbMassDevice->PciBDF & 0xFF) >> 3);
    BbsEntry->Function = (UINT32)(UsbMassDevice->PciBDF & 7);

    //
    // Update class/subclass information
    //
    BbsEntry->Class = PCI_CL_SER_BUS;
    BbsEntry->SubClass = PCI_CL_SER_BUS_SCL_USB;

    StatusFlags.Enabled = 1; StatusFlags.MediaPresent = 1;
    BbsEntry->StatusFlags = StatusFlags;  // Enabled, Unknown media

    //
    // Copy the device name string into low memory at gLegacyMemoryAddress, and
    // update the string pointer in BBS table entry
    //
    Status = CreateDeviceName(
                DevIndex,
                UsbMassDevice->DevString,
                &BbsEntry->DescStringSegment,
                &BbsEntry->DescStringOffset,
                &BbsEntry->MfgStringSegment,
                &BbsEntry->MfgStringOffset
    );
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) return Status;

    DevAndSysType = 0x11;    
    Handle = (UINT8)UsbMassDevice->LogicalAddress;

    switch (UsbMassDevice->StorageType) {
        case USB_MASS_DEV_ARMD:
#if BBS_USB_DEVICE_TYPE_SUPPORT
            BbsEntry->DeviceType = BBS_USB;
#else
            BbsEntry->DeviceType = BBS_FLOPPY;
#endif
            BaidDeviceType = BAID_TYPE_RMD_FDD;
            BbsEntry->BootHandlerSegment = (UINT16)((UINTN)gI13BinData >> 4);
            BbsEntry->BootHandlerOffset = gI13BinData->BcvOffset + DevIndex*4;
            break;

        case USB_MASS_DEV_HDD:
#if BBS_USB_DEVICE_TYPE_SUPPORT
            BbsEntry->DeviceType = BBS_USB;
#else
            BbsEntry->DeviceType = BBS_HARDDISK;
#endif
            BaidDeviceType = BAID_TYPE_RMD_HDD;
            Handle |= 0x80;
            BbsEntry->BootHandlerSegment = (UINT16)((UINTN)gI13BinData >> 4);
            BbsEntry->BootHandlerOffset = gI13BinData->BcvOffset + DevIndex*4;
            break;

        case USB_MASS_DEV_CDROM:
#if BBS_USB_DEVICE_TYPE_SUPPORT
            BbsEntry->DeviceType = BBS_USB;
#else
            BbsEntry->DeviceType = BBS_CDROM;
#endif
            BaidDeviceType = BAID_TYPE_CDROM;            
            BbsEntry->BootHandlerSegment = 0xf000;
            BbsEntry->BootHandlerOffset = gLegacy16Data->CdrBevOffset;
            if (gCdromInstalled) break;
            //
            // Patch farReturnCDROMSupportAPIPointer routine with "call farCheckForUSBCdrom"
            //
            CheckForUsbCdromOffset = gI13BinData->CheckForUsbCDROMOffset;
            CheckForUsbCdromAddress =
                (UINT32)((UINTN)gI13BinData<<12) + (UINT32)CheckForUsbCdromOffset;
        
            PatchAddr = (UINT8*)(UINTN)(0xF0000+gLegacy16Data->CDROMSupportAPIOfs+5);
            *PatchAddr++ = 0x9A; // far call opcode
            *(UINT32*)PatchAddr = CheckForUsbCdromAddress;
            gCdromInstalled = TRUE;
            break;
        default:
            BbsEntry->DeviceType = BBS_UNKNOWN;
    }

    BbsEntry->InitPerReserved = ((UINT32)BaidDeviceType<<24)
                        +((UINT32)Handle<<8)
                        +(UINT32)DevAndSysType;

    *(UINTN*)(&BbsEntry->IBV1) = (UINTN)UsbMassDevice->Handle;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:  InstallUsbLegacyBootDevices
//
// Description: This function installs USB INT13 devices
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
InstallUsbLegacyBootDevices (
    VOID
)
{
    EFI_STATUS  Status;
    UINTN       NumberOfHandles = 0;
    EFI_HANDLE  *HandleBuffer = NULL;
    UINTN       Index;
    EFI_USB_IO_PROTOCOL     *UsbIo;
    EFI_USB_INTERFACE_DESCRIPTOR InterfaceDesc;
    EFI_BLOCK_IO_PROTOCOL   *BlkIo;
    USB_MASS_DEV            *MassDev;
    DEV_INFO                *DevInfo;

    if (gUsbData->dUSBStateFlag & USB_FLAG_DISABLE_LEGACY_SUPPORT) {
        return EFI_UNSUPPORTED;
    }

	InitializeHotplugDevices();

	Status = pBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid, 
                NULL, &NumberOfHandles, &HandleBuffer);
    if (EFI_ERROR(Status)) {
        return Status;
    }

    for (Index = 0; Index < NumberOfHandles; Index++) {
        Status = pBS->HandleProtocol(HandleBuffer[Index], &gEfiUsbIoProtocolGuid, &UsbIo);
        if (EFI_ERROR(Status)) {
            continue;
        }

        Status = UsbIo->UsbGetInterfaceDescriptor(UsbIo, &InterfaceDesc);
        if (EFI_ERROR(Status)) {
            continue;
        }
        if (InterfaceDesc.InterfaceClass != BASE_CLASS_MASS_STORAGE) {
            continue;
        }

        Status = pBS->HandleProtocol(HandleBuffer[Index], 
                                &gEfiBlockIoProtocolGuid, &BlkIo);
        if (EFI_ERROR(Status)) {
            continue;
        }

        MassDev = (USB_MASS_DEV*)BlkIo;
        DevInfo = (DEV_INFO*)MassDev->DevInfo;
        if ((DevInfo->bPhyDevType != USB_MASS_DEV_UNKNOWN) &&
            !(DevInfo->bPhyDevType != USB_MASS_DEV_CDROM && 
            (DevInfo->wBlockSize > 0x200 && DevInfo->wBlockSize != 0xFFFF))){
            UsbInstallLegacyDevice(MassDev);
        }
    }

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:  UsbInstallLegacyDevice
//
// Description: This function installs USB INT13 device
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
UsbInstallLegacyDevice (
    USB_MASS_DEV    *UsbMassDevice
)
{
    BBS_TABLE   BbsEntry;
    EFI_STATUS  Status;
    UINT8       EntryNumber = 0xff;
    UINT8       Index;
    DEV_INFO    *Device;
    UINT8       HcIndx;
    UINT8       PortIndx;

    //TRACE((-1, "Installing USB INT13 device %x\n", UsbMassDevice));

    //
    // See if device is already in the list, if yes - return error.
    //
    for (Index=0; Index<USBDEVS_MAX_ENTRIES; Index++) {
        if ((gI13BinData->UsbMassI13Dev)[Index].Handle == (UINT8)UsbMassDevice->LogicalAddress) {
            ASSERT(FALSE);  // ERROR: Device already exists
            return EFI_INVALID_PARAMETER;
        }
    }
    //
    // Look for an empty slot in BcvLookupTable
    //
    for (Index=0; Index<USBDEVS_MAX_ENTRIES; Index++) {
        if ((gI13BinData->UsbMassI13Dev)[Index].Handle == 0) {
            break;
        }
    }
    ASSERT(Index<USBDEVS_MAX_ENTRIES);
    if (Index==USBDEVS_MAX_ENTRIES) return EFI_OUT_OF_RESOURCES;

    Status = gBiosExtensions->UnlockShadow(0, 0, 0, 0);
    ASSERT_EFI_ERROR(Status);

    Status = CreateBbsEntry(Index, UsbMassDevice, &BbsEntry);
    ASSERT_EFI_ERROR(Status);

    Status = gBiosExtensions->InsertBbsEntryAt(gBiosExtensions,
            &BbsEntry,
            &EntryNumber);  // This function returns EntryNumber
    ASSERT_EFI_ERROR(Status);
    
    //
    // Entry has been successfully added, update the lookup table
    //
    (gI13BinData->UsbMassI13Dev)[Index].Handle = (UINT8)UsbMassDevice->LogicalAddress;
    (gI13BinData->UsbMassI13Dev)[Index].BbsEntryNo = EntryNumber;
    (gI13BinData->UsbMassI13Dev)[Index].DevBaidType = (UINT8)(BbsEntry.InitPerReserved>>24);

    //
    // Update device geometry related information
    //
    Device = (DEV_INFO*)UsbMassDevice->DevInfo;
    (gI13BinData->UsbMassI13Dev)[Index].NumHeads = Device->NonLBAHeads;
    (gI13BinData->UsbMassI13Dev)[Index].LBANumHeads = Device->Heads;
    (gI13BinData->UsbMassI13Dev)[Index].NumCylinders = Device->wNonLBACylinders;
    (gI13BinData->UsbMassI13Dev)[Index].LBANumCyls = Device->wCylinders;
    (gI13BinData->UsbMassI13Dev)[Index].NumSectors = Device->bNonLBASectors;
    (gI13BinData->UsbMassI13Dev)[Index].LBANumSectors = Device->bSectors;
    (gI13BinData->UsbMassI13Dev)[Index].BytesPerSector = Device->wBlockSize;
    (gI13BinData->UsbMassI13Dev)[Index].MediaType = Device->bMediaType;
    (gI13BinData->UsbMassI13Dev)[Index].LastLBA = Device->MaxLba;
    (gI13BinData->UsbMassI13Dev)[Index].BpbMediaDesc = Device->BpbMediaDesc;

    // Update PCI location of the controller this device is connected to
    gUsbPciLocationTable[Index].Handle = (UINT8)UsbMassDevice->LogicalAddress;
    gUsbPciLocationTable[Index].PciLocation = UsbMassDevice->PciBDF;

    Status = gBiosExtensions->LockShadow(0, 0);
    ASSERT_EFI_ERROR(Status);

	// Set the device as registered
	Device->Flag |= DEV_INFO_MASS_DEV_REGD;

    //
    // See if OEM asks for USB boot override for this device. If yes, store
    // BBS index of this device, later at READY_TO_BOOT this BBS device will
    // be assigned priority 0 (highest).
    //
    // Note1: There is no need to check if gAmiUsb is valid, because this
    // function itself is one of the members of gAmiUsb structure.
    //
    // Note2: This feature will only be available for the devices connected
    // directly to the root port; devices behind the hub(s) will be ignored.
    //
    if (Device->bHubDeviceNumber & 0x80)
    {
        Status = gAmiUsb->UsbGetAssignBootPort(&HcIndx, &PortIndx);
        if ((!EFI_ERROR(Status)) && (gBootOverrideDeviceIndx == 0))
        {
            TRACE((-1,"OemUsbGetAssignBootPort: HC %d, Port %d; current HC %d, Port %d\n",
                HcIndx, PortIndx, Device->bHCNumber, Device->bHubPortNumber));

            if ((Device->bHCNumber == HcIndx) && (Device->bHubPortNumber == PortIndx))
            {
                TRACE((-1,"---OemUsbGetAssignBootPort: BBS Entry# %d\n", EntryNumber));

                gBootOverrideDeviceIndx = EntryNumber;
            }
        }
    }

    //
    // Process the "Auto" settings of Hotplug devices: if the device being installed
    // have already had a "Hotplug" clone, uninstall the clone.
    //

    // Process hotplug floppy
    if ( (UsbMassDevice != &gHotplugFloppy) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_ARMD) &&
         (gUsbData->fdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) )
    {
        TRACE((-1, "Uninstalling Hotplug Floppy (Setup 'Auto' option)\n"));
        UsbUninstallLegacyDevice(&gHotplugFloppy);    // Okay not to be successful
    }

    // Process hotplug HDD
    if ( (UsbMassDevice != &gHotplugHardDrive) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_HDD) &&
         (gUsbData->hdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) )
    {
        TRACE((-1, "Uninstalling Hotplug HDD (Setup 'Auto' option)\n"));
        UsbUninstallLegacyDevice(&gHotplugHardDrive);    // Okay not to be successful
    }
    // Process hotplug CDROM
    if ( (UsbMassDevice != &gHotplugCDROM) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_CDROM) &&
         (gUsbData->cdrom_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) )
    {
        TRACE((-1, "Uninstalling Hotplug CDROM (Setup 'Auto' option)\n"));
        UsbUninstallLegacyDevice(&gHotplugCDROM);    // Okay not to be successful
    }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:  UsbUninstallLegacyDevice
//
// Description: This function uninstalls USB INT13 device
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
UsbUninstallLegacyDevice (
    USB_MASS_DEV*   UsbMassDevice
)
{
    EFI_STATUS  Status;
    UINT8       Index;

    TRACE((-1, "Uninstalling INT13 device %x\n", UsbMassDevice));

    Status = gBiosExtensions->UnlockShadow(0, 0, 0, 0);
    ASSERT_EFI_ERROR(Status);

    for (Index=0; Index<USBDEVS_MAX_ENTRIES; Index++) {
        if ((gI13BinData->UsbMassI13Dev)[Index].Handle == (UINT8)UsbMassDevice->LogicalAddress) {
            (gI13BinData->UsbMassI13Dev)[Index].Handle = 0; // Mark as unused
            Status = gBiosExtensions->RemoveBbsEntryAt(
                        gBiosExtensions,
                        (gI13BinData->UsbMassI13Dev)[Index].BbsEntryNo
            );
            ASSERT_EFI_ERROR(Status);

            if ((gBootOverrideDeviceIndx != 0)
                && (gBootOverrideDeviceIndx == (gI13BinData->UsbMassI13Dev)[Index].BbsEntryNo))
            {
                gBootOverrideDeviceIndx = 0;
            }
            gUsbPciLocationTable[Index].Handle = 0; // Make invalid handle

            break;
        }
    }
    ASSERT_EFI_ERROR(Status);

    gBiosExtensions->LockShadow(0, 0);

	((DEV_INFO*)UsbMassDevice->DevInfo)->Flag &= ~(DEV_INFO_VALID_STRUC | DEV_INFO_MASS_DEV_REGD);

    //
    // Process the "Auto" settings of Hotplug devices: if the device being uninstalled
    // is the last one of a kind, and "Auto" Setup option is selected for the hotplug
    // device of this kind, then install the hotplug device.
    //

    // Process hotplug floppy
    if ( (UsbMassDevice != &gHotplugFloppy) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_ARMD) &&
         (gUsbData->fdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) &&
         (gUsbData->NumberOfFDDs == 0))
    {
        TRACE((-1, "Installing Hotplug Floppy (Setup 'Auto' option)\n"));
        UsbInstallLegacyDevice(&gHotplugFloppy);
    }

    if ( (UsbMassDevice != &gHotplugHardDrive) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_HDD) &&
         (gUsbData->hdd_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) &&
         (gUsbData->NumberOfHDDs == 0))
    {
        TRACE((-1, "Installing Hotplug HDD (Setup 'Auto' option)\n"));
        UsbInstallLegacyDevice(&gHotplugHardDrive);
    }
    if ( (UsbMassDevice != &gHotplugCDROM) &&
         (UsbMassDevice->StorageType == USB_MASS_DEV_CDROM) &&
         (gUsbData->cdrom_hotplug_support == SETUP_DATA_HOTPLUG_AUTO) &&
         (gUsbData->NumberOfCDROMs == 0))
    {
        TRACE((-1, "Installing Hotplug CDROM (Setup 'Auto' option)\n"));
        UsbInstallLegacyDevice(&gHotplugCDROM);
    }

    return Status;
}


//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2014, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************