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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
|
/** @file
Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions
of the BSD License which accompanies this distribution. The
full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "LegacyBiosInterface.h"
#define PHYSICAL_ADDRESS_TO_POINTER(Address) ((VOID *) ((UINTN) Address))
//
// define maximum number of HDD system supports
//
#define MAX_HDD_ENTRIES 0x30
//
// Module Global:
// Since this driver will only ever produce one instance of the Private Data
// protocol you are not required to dynamically allocate the PrivateData.
//
LEGACY_BIOS_INSTANCE mPrivateData;
/**
Do an AllocatePages () of type AllocateMaxAddress for EfiBootServicesCode
memory.
@param AllocateType Allocated Legacy Memory Type
@param StartPageAddress Start address of range
@param Pages Number of pages to allocate
@param Result Result of allocation
@retval EFI_SUCCESS Legacy16 code loaded
@retval Other No protocol installed, unload driver.
**/
EFI_STATUS
AllocateLegacyMemory (
IN EFI_ALLOCATE_TYPE AllocateType,
IN EFI_PHYSICAL_ADDRESS StartPageAddress,
IN UINTN Pages,
OUT EFI_PHYSICAL_ADDRESS *Result
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS MemPage;
//
// Allocate Pages of memory less <= StartPageAddress
//
MemPage = (EFI_PHYSICAL_ADDRESS) (UINTN) StartPageAddress;
Status = gBS->AllocatePages (
AllocateType,
EfiBootServicesCode,
Pages,
&MemPage
);
//
// Do not ASSERT on Status error but let caller decide since some cases
// memory is already taken but that is ok.
//
if (!EFI_ERROR (Status)) {
*Result = (EFI_PHYSICAL_ADDRESS) (UINTN) MemPage;
}
//
// If reach here the status = EFI_SUCCESS
//
return Status;
}
/**
This function is called when EFI needs to reserve an area in the 0xE0000 or 0xF0000
64 KB blocks.
Note: inconsistency with the Framework CSM spec. Per the spec, this function may be
invoked only once. This limitation is relaxed to allow multiple calls in this implemenation.
@param This Protocol instance pointer.
@param LegacyMemorySize Size of required region
@param Region Region to use. 00 = Either 0xE0000 or 0xF0000
block Bit0 = 1 0xF0000 block Bit1 = 1 0xE0000
block
@param Alignment Address alignment. Bit mapped. First non-zero
bit from right is alignment.
@param LegacyMemoryAddress Region Assigned
@retval EFI_SUCCESS Region assigned
@retval EFI_ACCESS_DENIED Procedure previously invoked
@retval Other Region not assigned
**/
EFI_STATUS
EFIAPI
LegacyBiosGetLegacyRegion (
IN EFI_LEGACY_BIOS_PROTOCOL *This,
IN UINTN LegacyMemorySize,
IN UINTN Region,
IN UINTN Alignment,
OUT VOID **LegacyMemoryAddress
)
{
LEGACY_BIOS_INSTANCE *Private;
EFI_IA32_REGISTER_SET Regs;
EFI_STATUS Status;
UINT32 Granularity;
Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
Private->LegacyRegion->UnLock (Private->LegacyRegion, 0xE0000, 0x20000, &Granularity);
ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));
Regs.X.AX = Legacy16GetTableAddress;
Regs.X.BX = (UINT16) Region;
Regs.X.CX = (UINT16) LegacyMemorySize;
Regs.X.DX = (UINT16) Alignment;
Private->LegacyBios.FarCall86 (
&Private->LegacyBios,
Private->Legacy16CallSegment,
Private->Legacy16CallOffset,
&Regs,
NULL,
0
);
if (Regs.X.AX == 0) {
*LegacyMemoryAddress = (VOID *) (UINTN) ((Regs.X.DS << 4) + Regs.X.BX);
Status = EFI_SUCCESS;
} else {
Status = EFI_OUT_OF_RESOURCES;
}
Private->Cpu->FlushDataCache (Private->Cpu, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate);
Private->LegacyRegion->Lock (Private->LegacyRegion, 0xE0000, 0x20000, &Granularity);
return Status;
}
/**
This function is called when copying data to the region assigned by
EFI_LEGACY_BIOS_PROTOCOL.GetLegacyRegion().
@param This Protocol instance pointer.
@param LegacyMemorySize Size of data to copy
@param LegacyMemoryAddress Legacy Region destination address Note: must
be in region assigned by
LegacyBiosGetLegacyRegion
@param LegacyMemorySourceAddress Source of data
@retval EFI_SUCCESS The data was copied successfully.
@retval EFI_ACCESS_DENIED Either the starting or ending address is out of bounds.
**/
EFI_STATUS
EFIAPI
LegacyBiosCopyLegacyRegion (
IN EFI_LEGACY_BIOS_PROTOCOL *This,
IN UINTN LegacyMemorySize,
IN VOID *LegacyMemoryAddress,
IN VOID *LegacyMemorySourceAddress
)
{
LEGACY_BIOS_INSTANCE *Private;
UINT32 Granularity;
if ((LegacyMemoryAddress < (VOID *)(UINTN)0xE0000 ) ||
((UINTN) LegacyMemoryAddress + LegacyMemorySize > (UINTN) 0x100000)
) {
return EFI_ACCESS_DENIED;
}
//
// There is no protection from writes over lapping if this function is
// called multiple times.
//
Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
Private->LegacyRegion->UnLock (Private->LegacyRegion, 0xE0000, 0x20000, &Granularity);
CopyMem (LegacyMemoryAddress, LegacyMemorySourceAddress, LegacyMemorySize);
Private->Cpu->FlushDataCache (Private->Cpu, 0xE0000, 0x20000, EfiCpuFlushTypeWriteBackInvalidate);
Private->LegacyRegion->Lock (Private->LegacyRegion, 0xE0000, 0x20000, &Granularity);
return EFI_SUCCESS;
}
/**
Find Legacy16 BIOS image in the FLASH device and shadow it into memory. Find
the $EFI table in the shadow area. Thunk into the Legacy16 code after it had
been shadowed.
@param Private Legacy BIOS context data
@retval EFI_SUCCESS Legacy16 code loaded
@retval Other No protocol installed, unload driver.
**/
EFI_STATUS
ShadowAndStartLegacy16 (
IN LEGACY_BIOS_INSTANCE *Private
)
{
EFI_STATUS Status;
UINT8 *Ptr;
UINT8 *PtrEnd;
BOOLEAN Done;
EFI_COMPATIBILITY16_TABLE *Table;
UINT8 CheckSum;
EFI_IA32_REGISTER_SET Regs;
EFI_TO_COMPATIBILITY16_INIT_TABLE *EfiToLegacy16InitTable;
EFI_TO_COMPATIBILITY16_BOOT_TABLE *EfiToLegacy16BootTable;
VOID *LegacyBiosImage;
UINTN LegacyBiosImageSize;
UINTN E820Size;
UINT32 *ClearPtr;
BBS_TABLE *BbsTable;
LEGACY_EFI_HDD_TABLE *LegacyEfiHddTable;
UINTN Index;
UINT32 TpmPointer;
VOID *TpmBinaryImage;
UINTN TpmBinaryImageSize;
UINTN Location;
UINTN Alignment;
UINTN TempData;
EFI_PHYSICAL_ADDRESS Address;
UINT16 OldMask;
UINT16 NewMask;
UINT32 Granularity;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
Location = 0;
Alignment = 0;
//
// we allocate the C/D/E/F segment as RT code so no one will use it any more.
//
Address = 0xC0000;
gDS->GetMemorySpaceDescriptor (Address, &Descriptor);
if (Descriptor.GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {
//
// If it is already reserved, we should be safe, or else we allocate it.
//
Status = gBS->AllocatePages (
AllocateAddress,
EfiRuntimeServicesCode,
0x40000/EFI_PAGE_SIZE,
&Address
);
if (EFI_ERROR (Status)) {
//
// Bugbug: need to figure out whether C/D/E/F segment should be marked as reserved memory.
//
DEBUG ((DEBUG_ERROR, "Failed to allocate the C/D/E/F segment Status = %r", Status));
}
}
//
// start testtest
// GetTimerValue (&Ticker);
//
// gRT->SetVariable (L"StartLegacy",
// &gEfiGlobalVariableGuid,
// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
// sizeof (UINT64),
// (VOID *)&Ticker
// );
// end testtest
//
EfiToLegacy16BootTable = &Private->IntThunk->EfiToLegacy16BootTable;
Status = Private->LegacyBiosPlatform->GetPlatformInfo (
Private->LegacyBiosPlatform,
EfiGetPlatformBinarySystemRom,
&LegacyBiosImage,
&LegacyBiosImageSize,
&Location,
&Alignment,
0,
0
);
if (EFI_ERROR (Status)) {
return Status;
}
Private->BiosStart = (UINT32) (0x100000 - LegacyBiosImageSize);
Private->OptionRom = 0xc0000;
Private->LegacyBiosImageSize = (UINT32) LegacyBiosImageSize;
//
// Can only shadow into memory allocated for legacy useage.
//
ASSERT (Private->BiosStart > Private->OptionRom);
//
// Shadow Legacy BIOS. Turn on memory and copy image
//
Private->LegacyRegion->UnLock (Private->LegacyRegion, 0xc0000, 0x40000, &Granularity);
ClearPtr = (VOID *) ((UINTN) 0xc0000);
//
// Initialize region from 0xc0000 to start of BIOS to all ffs. This allows unused
// regions to be used by EMM386 etc.
//
SetMem ((VOID *) ClearPtr, (UINTN) (0x40000 - LegacyBiosImageSize), 0xff);
TempData = Private->BiosStart;
CopyMem (
(VOID *) TempData,
LegacyBiosImage,
(UINTN) LegacyBiosImageSize
);
Private->Cpu->FlushDataCache (Private->Cpu, 0xc0000, 0x40000, EfiCpuFlushTypeWriteBackInvalidate);
//
// Search for Legacy16 table in Shadowed ROM
//
Done = FALSE;
Table = NULL;
for (Ptr = (UINT8 *) TempData; Ptr < (UINT8 *) ((UINTN) 0x100000) && !Done; Ptr += 0x10) {
if (*(UINT32 *) Ptr == SIGNATURE_32 ('I', 'F', 'E', '$')) {
Table = (EFI_COMPATIBILITY16_TABLE *) Ptr;
PtrEnd = Ptr + Table->TableLength;
for (CheckSum = 0; Ptr < PtrEnd; Ptr++) {
CheckSum = (UINT8) (CheckSum +*Ptr);
}
Done = TRUE;
}
}
if (Table == NULL) {
DEBUG ((EFI_D_ERROR, "No Legacy16 table found\n"));
return EFI_NOT_FOUND;
}
if (!Done) {
//
// Legacy16 table header checksum error.
//
DEBUG ((EFI_D_ERROR, "Legacy16 table found with bad talbe header checksum\n"));
}
//
// Remember location of the Legacy16 table
//
Private->Legacy16Table = Table;
Private->Legacy16CallSegment = Table->Compatibility16CallSegment;
Private->Legacy16CallOffset = Table->Compatibility16CallOffset;
EfiToLegacy16InitTable = &Private->IntThunk->EfiToLegacy16InitTable;
Private->Legacy16InitPtr = EfiToLegacy16InitTable;
Private->Legacy16BootPtr = &Private->IntThunk->EfiToLegacy16BootTable;
Private->InternalIrqRoutingTable = NULL;
Private->NumberIrqRoutingEntries = 0;
Private->BbsTablePtr = NULL;
Private->LegacyEfiHddTable = NULL;
Private->DiskEnd = 0;
Private->Disk4075 = 0;
Private->HddTablePtr = &Private->IntThunk->EfiToLegacy16BootTable.HddInfo;
Private->NumberHddControllers = MAX_IDE_CONTROLLER;
Private->Dump[0] = 'D';
Private->Dump[1] = 'U';
Private->Dump[2] = 'M';
Private->Dump[3] = 'P';
ZeroMem (
Private->Legacy16BootPtr,
sizeof (EFI_TO_COMPATIBILITY16_BOOT_TABLE)
);
//
// Store away a copy of the EFI System Table
//
Table->EfiSystemTable = (UINT32) (UINTN) gST;
//
// IPF CSM integration -Bug
//
// Construct the Legacy16 boot memory map. This sets up number of
// E820 entries.
//
LegacyBiosBuildE820 (Private, &E820Size);
//
// Initialize BDA and EBDA standard values needed to load Legacy16 code
//
LegacyBiosInitBda (Private);
LegacyBiosInitCmos (Private);
//
// All legacy interrupt should be masked when do initialization work from legacy 16 code.
//
Private->Legacy8259->GetMask(Private->Legacy8259, &OldMask, NULL, NULL, NULL);
NewMask = 0xFFFF;
Private->Legacy8259->SetMask(Private->Legacy8259, &NewMask, NULL, NULL, NULL);
//
// Call into Legacy16 code to do an INIT
//
ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));
Regs.X.AX = Legacy16InitializeYourself;
Regs.X.ES = EFI_SEGMENT (*((UINT32 *) &EfiToLegacy16InitTable));
Regs.X.BX = EFI_OFFSET (*((UINT32 *) &EfiToLegacy16InitTable));
Private->LegacyBios.FarCall86 (
&Private->LegacyBios,
Table->Compatibility16CallSegment,
Table->Compatibility16CallOffset,
&Regs,
NULL,
0
);
//
// Restore original legacy interrupt mask value
//
Private->Legacy8259->SetMask(Private->Legacy8259, &OldMask, NULL, NULL, NULL);
if (Regs.X.AX != 0) {
return EFI_DEVICE_ERROR;
}
//
// start testtest
// GetTimerValue (&Ticker);
//
// gRT->SetVariable (L"BackFromInitYourself",
// &gEfiGlobalVariableGuid,
// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
// sizeof (UINT64),
// (VOID *)&Ticker
// );
// end testtest
//
// Copy E820 table after InitializeYourself is completed
//
ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));
Regs.X.AX = Legacy16GetTableAddress;
Regs.X.CX = (UINT16) E820Size;
Regs.X.DX = 1;
Private->LegacyBios.FarCall86 (
&Private->LegacyBios,
Table->Compatibility16CallSegment,
Table->Compatibility16CallOffset,
&Regs,
NULL,
0
);
Table->E820Pointer = (UINT32) (Regs.X.DS * 16 + Regs.X.BX);
Table->E820Length = (UINT32) E820Size;
if (Regs.X.AX != 0) {
DEBUG ((EFI_D_ERROR, "Legacy16 E820 length insufficient\n"));
} else {
TempData = Table->E820Pointer;
CopyMem ((VOID *) TempData, Private->E820Table, E820Size);
}
//
// Get PnPInstallationCheck Info.
//
Private->PnPInstallationCheckSegment = Table->PnPInstallationCheckSegment;
Private->PnPInstallationCheckOffset = Table->PnPInstallationCheckOffset;
//
// Check if PCI Express is supported. If yes, Save base address.
//
Status = Private->LegacyBiosPlatform->GetPlatformInfo (
Private->LegacyBiosPlatform,
EfiGetPlatformPciExpressBase,
NULL,
NULL,
&Location,
&Alignment,
0,
0
);
if (!EFI_ERROR (Status)) {
Private->Legacy16Table->PciExpressBase = (UINT32)Location;
Location = 0;
}
//
// Check if TPM is supported. If yes get a region in E0000,F0000 to copy it
// into, copy it and update pointer to binary image. This needs to be
// done prior to any OPROM for security purposes.
//
Status = Private->LegacyBiosPlatform->GetPlatformInfo (
Private->LegacyBiosPlatform,
EfiGetPlatformBinaryTpmBinary,
&TpmBinaryImage,
&TpmBinaryImageSize,
&Location,
&Alignment,
0,
0
);
if (!EFI_ERROR (Status)) {
ZeroMem (&Regs, sizeof (EFI_IA32_REGISTER_SET));
Regs.X.AX = Legacy16GetTableAddress;
Regs.X.CX = (UINT16) TpmBinaryImageSize;
Regs.X.DX = 1;
Private->LegacyBios.FarCall86 (
&Private->LegacyBios,
Table->Compatibility16CallSegment,
Table->Compatibility16CallOffset,
&Regs,
NULL,
0
);
TpmPointer = (UINT32) (Regs.X.DS * 16 + Regs.X.BX);
if (Regs.X.AX != 0) {
DEBUG ((EFI_D_ERROR, "TPM cannot be loaded\n"));
} else {
CopyMem ((VOID *) (UINTN)TpmPointer, TpmBinaryImage, TpmBinaryImageSize);
Table->TpmSegment = Regs.X.DS;
Table->TpmOffset = Regs.X.BX;
}
}
//
// Lock the Legacy BIOS region
//
Private->Cpu->FlushDataCache (Private->Cpu, Private->BiosStart, (UINT32) LegacyBiosImageSize, EfiCpuFlushTypeWriteBackInvalidate);
Private->LegacyRegion->Lock (Private->LegacyRegion, Private->BiosStart, (UINT32) LegacyBiosImageSize, &Granularity);
//
// Get the BbsTable from LOW_MEMORY_THUNK
//
BbsTable = (BBS_TABLE *)(UINTN)Private->IntThunk->BbsTable;
ZeroMem ((VOID *)BbsTable, sizeof (Private->IntThunk->BbsTable));
EfiToLegacy16BootTable->BbsTable = (UINT32)(UINTN)BbsTable;
Private->BbsTablePtr = (VOID *) BbsTable;
//
// Skip Floppy and possible onboard IDE drives
//
EfiToLegacy16BootTable->NumberBbsEntries = 1 + 2 * MAX_IDE_CONTROLLER;
for (Index = 0; Index < (sizeof (Private->IntThunk->BbsTable) / sizeof (BBS_TABLE)); Index++) {
BbsTable[Index].BootPriority = BBS_IGNORE_ENTRY;
}
//
// Allocate space for Legacy HDD table
//
LegacyEfiHddTable = (LEGACY_EFI_HDD_TABLE *) AllocateZeroPool ((UINTN) MAX_HDD_ENTRIES * sizeof (LEGACY_EFI_HDD_TABLE));
ASSERT (LegacyEfiHddTable);
Private->LegacyEfiHddTable = LegacyEfiHddTable;
Private->LegacyEfiHddTableIndex = 0x00;
//
// start testtest
// GetTimerValue (&Ticker);
//
// gRT->SetVariable (L"EndOfLoadFv",
// &gEfiGlobalVariableGuid,
// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
// sizeof (UINT64),
// (VOID *)&Ticker
// );
// end testtest
//
return EFI_SUCCESS;
}
/**
Shadow all legacy16 OPROMs that haven't been shadowed.
Warning: Use this with caution. This routine disconnects all EFI
drivers. If used externally then caller must re-connect EFI
drivers.
@param This Protocol instance pointer.
@retval EFI_SUCCESS OPROMs shadowed
**/
EFI_STATUS
EFIAPI
LegacyBiosShadowAllLegacyOproms (
IN EFI_LEGACY_BIOS_PROTOCOL *This
)
{
LEGACY_BIOS_INSTANCE *Private;
//
// EFI_LEGACY_BIOS_PLATFORM_PROTOCOL *LegacyBiosPlatform;
// EFI_LEGACY16_TABLE *Legacy16Table;
//
Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
//
// LegacyBiosPlatform = Private->LegacyBiosPlatform;
// Legacy16Table = Private->Legacy16Table;
//
// Shadow PCI ROMs. We must do this near the end since this will kick
// of Native EFI drivers that may be needed to collect info for Legacy16
//
// WARNING: PciIo is gone after this call.
//
PciProgramAllInterruptLineRegisters (Private);
PciShadowRoms (Private);
//
// Shadow PXE base code, BIS etc.
//
// LegacyBiosPlatform->ShadowServiceRoms (LegacyBiosPlatform,
// &Private->OptionRom,
// Legacy16Table);
//
return EFI_SUCCESS;
}
/**
Get the PCI BIOS interface version.
@param Private Driver private data.
@return The PCI interface version number in Binary Coded Decimal (BCD) format.
E.g.: 0x0210 indicates 2.10, 0x0300 indicates 3.00
**/
UINT16
GetPciInterfaceVersion (
IN LEGACY_BIOS_INSTANCE *Private
)
{
EFI_IA32_REGISTER_SET Reg;
BOOLEAN ThunkFailed;
UINT16 PciInterfaceVersion;
PciInterfaceVersion = 0;
Reg.X.AX = 0xB101;
Reg.E.EDI = 0;
ThunkFailed = Private->LegacyBios.Int86 (&Private->LegacyBios, 0x1A, &Reg);
if (!ThunkFailed) {
//
// From PCI Firmware 3.0 Specification:
// If the CARRY FLAG [CF] is cleared and AH is set to 00h, it is still necessary to examine the
// contents of [EDX] for the presence of the string "PCI" + (trailing space) to fully validate the
// presence of the PCI function set. [BX] will further indicate the version level, with enough
// granularity to allow for incremental changes in the code that don't affect the function interface.
// Version numbers are stored as Binary Coded Decimal (BCD) values. For example, Version 2.10
// would be returned as a 02h in the [BH] registers and 10h in the [BL] registers.
//
if ((Reg.X.Flags.CF == 0) && (Reg.H.AH == 0) && (Reg.E.EDX == SIGNATURE_32 ('P', 'C', 'I', ' '))) {
PciInterfaceVersion = Reg.X.BX;
}
}
return PciInterfaceVersion;
}
/**
Install Driver to produce Legacy BIOS protocol.
@param ImageHandle Handle of driver image.
@param SystemTable Pointer to system table.
@retval EFI_SUCCESS Legacy BIOS protocol installed
@retval No protocol installed, unload driver.
**/
EFI_STATUS
EFIAPI
LegacyBiosInstall (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
LEGACY_BIOS_INSTANCE *Private;
EFI_TO_COMPATIBILITY16_INIT_TABLE *EfiToLegacy16InitTable;
EFI_PHYSICAL_ADDRESS MemoryAddress;
EFI_PHYSICAL_ADDRESS EbdaReservedBaseAddress;
VOID *MemoryPtr;
EFI_PHYSICAL_ADDRESS MemoryAddressUnder1MB;
UINTN Index;
UINT32 *BaseVectorMaster;
EFI_PHYSICAL_ADDRESS StartAddress;
UINT32 *ClearPtr;
EFI_PHYSICAL_ADDRESS MemStart;
UINT32 IntRedirCode;
UINT32 Granularity;
BOOLEAN DecodeOn;
UINT32 MemorySize;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
UINT64 Length;
UINT8 *SecureBoot;
//
// Load this driver's image to memory
//
Status = RelocateImageUnder4GIfNeeded (ImageHandle, SystemTable);
if (EFI_ERROR (Status)) {
return Status;
}
//
// When UEFI Secure Boot is enabled, CSM module will not start any more.
//
SecureBoot = NULL;
GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);
if ((SecureBoot != NULL) && (*SecureBoot == SECURE_BOOT_MODE_ENABLE)) {
FreePool (SecureBoot);
return EFI_SECURITY_VIOLATION;
}
if (SecureBoot != NULL) {
FreePool (SecureBoot);
}
Private = &mPrivateData;
ZeroMem (Private, sizeof (LEGACY_BIOS_INSTANCE));
//
// Grab a copy of all the protocols we depend on. Any error would
// be a dispatcher bug!.
//
Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &Private->Cpu);
ASSERT_EFI_ERROR (Status);
Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Private->Timer);
ASSERT_EFI_ERROR (Status);
Status = gBS->LocateProtocol (&gEfiLegacyRegion2ProtocolGuid, NULL, (VOID **) &Private->LegacyRegion);
ASSERT_EFI_ERROR (Status);
Status = gBS->LocateProtocol (&gEfiLegacyBiosPlatformProtocolGuid, NULL, (VOID **) &Private->LegacyBiosPlatform);
ASSERT_EFI_ERROR (Status);
Status = gBS->LocateProtocol (&gEfiLegacy8259ProtocolGuid, NULL, (VOID **) &Private->Legacy8259);
ASSERT_EFI_ERROR (Status);
Status = gBS->LocateProtocol (&gEfiLegacyInterruptProtocolGuid, NULL, (VOID **) &Private->LegacyInterrupt);
ASSERT_EFI_ERROR (Status);
//
// Locate Memory Test Protocol if exists
//
Status = gBS->LocateProtocol (
&gEfiGenericMemTestProtocolGuid,
NULL,
(VOID **) &Private->GenericMemoryTest
);
ASSERT_EFI_ERROR (Status);
//
// Make sure all memory from 0-640K is tested
//
for (StartAddress = 0; StartAddress < 0xa0000; ) {
gDS->GetMemorySpaceDescriptor (StartAddress, &Descriptor);
if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {
StartAddress = Descriptor.BaseAddress + Descriptor.Length;
continue;
}
Length = MIN (Descriptor.Length, 0xa0000 - StartAddress);
Private->GenericMemoryTest->CompatibleRangeTest (
Private->GenericMemoryTest,
StartAddress,
Length
);
StartAddress = StartAddress + Length;
}
//
// Make sure all memory from 1MB to 16MB is tested and added to memory map
//
for (StartAddress = BASE_1MB; StartAddress < BASE_16MB; ) {
gDS->GetMemorySpaceDescriptor (StartAddress, &Descriptor);
if (Descriptor.GcdMemoryType != EfiGcdMemoryTypeReserved) {
StartAddress = Descriptor.BaseAddress + Descriptor.Length;
continue;
}
Length = MIN (Descriptor.Length, BASE_16MB - StartAddress);
Private->GenericMemoryTest->CompatibleRangeTest (
Private->GenericMemoryTest,
StartAddress,
Length
);
StartAddress = StartAddress + Length;
}
Private->Signature = LEGACY_BIOS_INSTANCE_SIGNATURE;
Private->LegacyBios.Int86 = LegacyBiosInt86;
Private->LegacyBios.FarCall86 = LegacyBiosFarCall86;
Private->LegacyBios.CheckPciRom = LegacyBiosCheckPciRom;
Private->LegacyBios.InstallPciRom = LegacyBiosInstallPciRom;
Private->LegacyBios.LegacyBoot = LegacyBiosLegacyBoot;
Private->LegacyBios.UpdateKeyboardLedStatus = LegacyBiosUpdateKeyboardLedStatus;
Private->LegacyBios.GetBbsInfo = LegacyBiosGetBbsInfo;
Private->LegacyBios.ShadowAllLegacyOproms = LegacyBiosShadowAllLegacyOproms;
Private->LegacyBios.PrepareToBootEfi = LegacyBiosPrepareToBootEfi;
Private->LegacyBios.GetLegacyRegion = LegacyBiosGetLegacyRegion;
Private->LegacyBios.CopyLegacyRegion = LegacyBiosCopyLegacyRegion;
Private->LegacyBios.BootUnconventionalDevice = LegacyBiosBootUnconventionalDevice;
Private->ImageHandle = ImageHandle;
//
// Enable read attribute of legacy region.
//
DecodeOn = TRUE;
Private->LegacyRegion->Decode (
Private->LegacyRegion,
0xc0000,
0x40000,
&Granularity,
&DecodeOn
);
//
// Set Cachebility for legacy region
// BUGBUG: Comments about this legacy region cacheability setting
// This setting will make D865GCHProduction CSM Unhappy
//
if (PcdGetBool (PcdLegacyBiosCacheLegacyRegion)) {
gDS->SetMemorySpaceAttributes (
0x0,
0xA0000,
EFI_MEMORY_WB
);
gDS->SetMemorySpaceAttributes (
0xc0000,
0x40000,
EFI_MEMORY_WB
);
}
gDS->SetMemorySpaceAttributes (
0xA0000,
0x20000,
EFI_MEMORY_UC
);
//
// Allocate 0 - 4K for real mode interupt vectors and BDA.
//
AllocateLegacyMemory (
AllocateAddress,
0,
1,
&MemoryAddress
);
ASSERT (MemoryAddress == 0x000000000);
ClearPtr = (VOID *) ((UINTN) 0x0000);
//
// Initialize region from 0x0000 to 4k. This initializes interrupt vector
// range.
//
gBS->SetMem ((VOID *) ClearPtr, 0x400, INITIAL_VALUE_BELOW_1K);
ZeroMem ((VOID *) ((UINTN)ClearPtr + 0x400), 0xC00);
//
// Allocate pages for OPROM usage
//
MemorySize = PcdGet32 (PcdEbdaReservedMemorySize);
ASSERT ((MemorySize & 0xFFF) == 0);
Status = AllocateLegacyMemory (
AllocateAddress,
CONVENTIONAL_MEMORY_TOP - MemorySize,
EFI_SIZE_TO_PAGES (MemorySize),
&MemoryAddress
);
ASSERT_EFI_ERROR (Status);
ZeroMem ((VOID *) ((UINTN) MemoryAddress), MemorySize);
//
// Allocate all 32k chunks from 0x60000 ~ 0x88000 for Legacy OPROMs that
// don't use PMM but look for zeroed memory. Note that various non-BBS
// OpROMs expect different areas to be free
//
EbdaReservedBaseAddress = MemoryAddress;
MemoryAddress = PcdGet32 (PcdOpromReservedMemoryBase);
MemorySize = PcdGet32 (PcdOpromReservedMemorySize);
//
// Check if base address and size for reserved memory are 4KB aligned.
//
ASSERT ((MemoryAddress & 0xFFF) == 0);
ASSERT ((MemorySize & 0xFFF) == 0);
//
// Check if the reserved memory is below EBDA reserved range.
//
ASSERT ((MemoryAddress < EbdaReservedBaseAddress) && ((MemoryAddress + MemorySize - 1) < EbdaReservedBaseAddress));
for (MemStart = MemoryAddress; MemStart < MemoryAddress + MemorySize; MemStart += 0x1000) {
Status = AllocateLegacyMemory (
AllocateAddress,
MemStart,
1,
&MemoryAddress
);
if (!EFI_ERROR (Status)) {
MemoryPtr = (VOID *) ((UINTN) MemoryAddress);
ZeroMem (MemoryPtr, 0x1000);
} else {
DEBUG ((EFI_D_ERROR, "WARNING: Allocate legacy memory fail for SCSI card - %x\n", MemStart));
}
}
//
// Allocate low PMM memory and zero it out
//
MemorySize = PcdGet32 (PcdLowPmmMemorySize);
ASSERT ((MemorySize & 0xFFF) == 0);
Status = AllocateLegacyMemory (
AllocateMaxAddress,
CONVENTIONAL_MEMORY_TOP,
EFI_SIZE_TO_PAGES (MemorySize),
&MemoryAddressUnder1MB
);
ASSERT_EFI_ERROR (Status);
ZeroMem ((VOID *) ((UINTN) MemoryAddressUnder1MB), MemorySize);
//
// Allocate space for thunker and Init Thunker
//
Status = AllocateLegacyMemory (
AllocateMaxAddress,
CONVENTIONAL_MEMORY_TOP,
(sizeof (LOW_MEMORY_THUNK) / EFI_PAGE_SIZE) + 2,
&MemoryAddress
);
ASSERT_EFI_ERROR (Status);
Private->IntThunk = (LOW_MEMORY_THUNK *) (UINTN) MemoryAddress;
EfiToLegacy16InitTable = &Private->IntThunk->EfiToLegacy16InitTable;
EfiToLegacy16InitTable->ThunkStart = (UINT32) (EFI_PHYSICAL_ADDRESS) (UINTN) MemoryAddress;
EfiToLegacy16InitTable->ThunkSizeInBytes = (UINT32) (sizeof (LOW_MEMORY_THUNK));
Status = LegacyBiosInitializeThunk (Private);
ASSERT_EFI_ERROR (Status);
//
// Init the legacy memory map in memory < 1 MB.
//
EfiToLegacy16InitTable->BiosLessThan1MB = (UINT32) MemoryAddressUnder1MB;
EfiToLegacy16InitTable->LowPmmMemory = (UINT32) MemoryAddressUnder1MB;
EfiToLegacy16InitTable->LowPmmMemorySizeInBytes = MemorySize;
MemorySize = PcdGet32 (PcdHighPmmMemorySize);
ASSERT ((MemorySize & 0xFFF) == 0);
//
// Allocate high PMM Memory under 16 MB
//
Status = AllocateLegacyMemory (
AllocateMaxAddress,
0x1000000,
EFI_SIZE_TO_PAGES (MemorySize),
&MemoryAddress
);
if (EFI_ERROR (Status)) {
//
// If it fails, allocate high PMM Memory under 4GB
//
Status = AllocateLegacyMemory (
AllocateMaxAddress,
0xFFFFFFFF,
EFI_SIZE_TO_PAGES (MemorySize),
&MemoryAddress
);
}
if (!EFI_ERROR (Status)) {
EfiToLegacy16InitTable->HiPmmMemory = (UINT32) (EFI_PHYSICAL_ADDRESS) (UINTN) MemoryAddress;
EfiToLegacy16InitTable->HiPmmMemorySizeInBytes = MemorySize;
}
//
// ShutdownAPs();
//
// Start the Legacy BIOS;
//
Status = ShadowAndStartLegacy16 (Private);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Initialize interrupt redirection code and entries;
// IDT Vectors 0x68-0x6f must be redirected to IDT Vectors 0x08-0x0f.
//
CopyMem (
Private->IntThunk->InterruptRedirectionCode,
(VOID *) (UINTN) InterruptRedirectionTemplate,
sizeof (Private->IntThunk->InterruptRedirectionCode)
);
//
// Save Unexpected interrupt vector so can restore it just prior to boot
//
BaseVectorMaster = (UINT32 *) (sizeof (UINT32) * PROTECTED_MODE_BASE_VECTOR_MASTER);
Private->BiosUnexpectedInt = BaseVectorMaster[0];
IntRedirCode = (UINT32) (UINTN) Private->IntThunk->InterruptRedirectionCode;
for (Index = 0; Index < 8; Index++) {
BaseVectorMaster[Index] = (EFI_SEGMENT (IntRedirCode + Index * 4) << 16) | EFI_OFFSET (IntRedirCode + Index * 4);
}
//
// Save EFI value
//
Private->ThunkSeg = (UINT16) (EFI_SEGMENT (IntRedirCode));
//
// Make a new handle and install the protocol
//
Private->Handle = NULL;
Status = gBS->InstallProtocolInterface (
&Private->Handle,
&gEfiLegacyBiosProtocolGuid,
EFI_NATIVE_INTERFACE,
&Private->LegacyBios
);
Private->Csm16PciInterfaceVersion = GetPciInterfaceVersion (Private);
DEBUG ((EFI_D_INFO, "CSM16 PCI BIOS Interface Version: %02x.%02x\n",
(UINT8) (Private->Csm16PciInterfaceVersion >> 8),
(UINT8) Private->Csm16PciInterfaceVersion
));
ASSERT (Private->Csm16PciInterfaceVersion != 0);
return Status;
}
|