-
Notifications
You must be signed in to change notification settings - Fork 484
/
FitGen.c
4086 lines (3685 loc) · 144 KB
/
FitGen.c
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
/**@file
This utility is part of build process for IA32/X64 FD.
It generates FIT table.
Copyright (c) 2010-2022, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "FitGen.h"
//
// FIT spec
//
#pragma pack (1)
typedef struct {
UINT64 Address;
UINT8 Size[3];
UINT8 Rsvd;
UINT16 Version;
UINT8 Type:7;
UINT8 C_V:1;
UINT8 Checksum;
} FIRMWARE_INTERFACE_TABLE_ENTRY;
//
// Struct for policy
//
typedef struct {
UINT16 IndexPort;
UINT16 DataPort;
UINT8 Width;
UINT8 Bit;
UINT16 Index;
UINT8 Size[3];
UINT8 Rsvd;
UINT16 Version; // == 0
UINT8 Type:7;
UINT8 C_V:1;
UINT8 Checksum;
} FIRMWARE_INTERFACE_TABLE_ENTRY_PORT;
#define FIT_ALIGNMENT 0x3F // 0xF is required previously, but if we need exclude FIT, we must set 64 bytes alignment.
#define BIOS_MODULE_ALIGNMENT 0x3F // 64 bytes for AnC
#define MICROCODE_ALIGNMENT 0x7FF
#define MICROCODE_EXTERNAL_HEADER_SIZE 0x30
#define ACM_PKCS_1_5_RSA_SIGNATURE_SHA256_SIZE 256
#define ACM_PKCS_1_5_RSA_SIGNATURE_SHA384_SIZE 384
#define ACM_XMSS_PUBLIC_KEY_SIZE 64
#define ACM_XMSS_SIGNATURE_SIZE 2692
#define ACM_HEADER_VERSION_5 0x50004
#define ACM_HEADER_VERSION_4 (4 << 16)
#define ACM_HEADER_VERSION_3 (3 << 16)
#define ACM_HEADER_VERSION_0 (0)
#define ACM_MODULE_TYPE_CHIPSET_ACM 2
#define ACM_MODULE_SUBTYPE_CAPABLE_OF_EXECUTE_AT_RESET 0x1
#define ACM_MODULE_SUBTYPE_ANC_MODULE 0x2
#define ACM_MODULE_FLAG_PREPRODUCTION 0x4000
#define ACM_MODULE_FLAG_DEBUG_SIGN 0x8000
#define NIBBLES_TO_BYTE(A, B) (UINT8)(((A & (0x0F)) << 4) | (B & 0x0F))
//
//Flash Map 0 Register (Flash Descriptor Records)
//
typedef struct {
UINT32 Fcba : 8; //Bits[7:0]: Flash Component Base Address
UINT32 Nc : 2; //Bits[9:8]: Number of Components
UINT32 Rsvd0: 1; //Bit10: Reserved
UINT32 Rsvd1: 1; //Bit11: Reserved
UINT32 Rsvd2: 1; //Bit12: Reserved
UINT32 Rsvd3: 3; //Bits[15:13]: Reserved
UINT32 Frba : 8; //Bits[23:16]: Flash Region Base Address
UINT32 Rsvd4: 3; //Bits[26:24]: Reserved
UINT32 Rsvd5: 5; //Bits[31:27]: Reserved
} FLASH_MAP_0_REGISTER;
//
//Flash Region 1 (BIOS) Register (Flash Descriptor Records)
//
typedef struct {
UINT32 RegionBase : 15; //Bits[14:0]: Region base
UINT32 Rsvd : 1; //Bit15: Reserved
UINT32 RegionLimit: 15; //Bits[30:16]: Region limit
UINT32 Rsvd1 : 1; //Bit31: Reserved
} FLASH_REGION_1_BIOS_REGISTER;
#define FLASH_VALID_SIGNATURE 0x0FF0A55A //Flash Valid Signature (Flash Descriptor Records)
#define FLVALSIG_BASE_OFFSET 0x10 //Flash Valid Signature Base Offset
#define FLMAP0_BASE_OFFSET 0x14 //Flash Map 0 Register Base Offset
#define ACMFV_GUID \
{ 0x8a4b197f, 0x1113, 0x43d0, { 0xa2, 0x3f, 0x26, 0xf3, 0x69, 0xb2, 0xb8, 0x41 }}
typedef struct {
UINT16 ModuleType;
UINT16 ModuleSubType;
UINT32 HeaderLen;
UINT32 HeaderVersion;
UINT16 ChipsetID;
UINT16 Flags;
UINT32 ModuleVendor;
UINT32 Date;
UINT32 Size;
UINT16 TxtSvn;
UINT16 SeSvn;
UINT32 CodeControl;
UINT32 ErrorEntryPoint;
UINT32 GDTLimit;
UINT32 GDTBasePtr;
UINT32 SegSel;
UINT32 EntryPoint;
UINT8 Rsvd2[64];
UINT32 KeySize; // 64
UINT32 ScratchSize; // 2 * KeySize + 15
//UINT8 RSAPubKey[64 * 4]; // KeySize * 4
//UINT32 RSAPubExp;
//UINT8 RSASig[256];
// End of AC module header
//UINT8 Scratch[(64 * 2 + 15) * 4]; // ScratchSize * 4
// User Area
//UINT8 UserArea[1];
} ACM_FORMAT;
#define CHIPSET_ACM_INFORMATION_TABLE_VERSION_3 0x03
#define CHIPSET_ACM_INFORMATION_TABLE_VERSION_4 0x04
#define CHIPSET_ACM_INFORMATION_TABLE_VERSION CHIPSET_ACM_INFORMATION_TABLE_VERSION_3
#define CHIPSET_ACM_INFORMATION_TABLE_GUID_V03 \
{ 0x7FC03AAA, 0x18DB46A7, 0x8F69AC2E, 0x5A7F418D }
#define CHIPSET_ACM_TYPE_BIOS 0
#define CHIPSET_ACM_TYPE_SINIT 1
#define DEFAULT_ACM_EXTENDED_MASK 0x00FFFFFF
typedef struct {
UINT32 Guid0;
UINT32 Guid1;
UINT32 Guid2;
UINT32 Guid3;
} ACM_GUID;
typedef struct {
ACM_GUID Guid;
UINT8 ChipsetACMType;
UINT8 Version;
UINT16 Length;
UINT32 ChipsetIDList;
UINT32 OsSinitTableVer;
UINT32 MinMleHeaderVer;
//#if (CHIPSET_ACM_INFORMATION_TABLE_VERSION >= CHIPSET_ACM_INFORMATION_TABLE_VERSION_3)
UINT32 Capabilities;
UINT8 AcmVersion;
UINT8 AcmRevision[3];
//#if (CHIPSET_ACM_INFORMATION_TABLE_VERSION >= CHIPSET_ACM_INFORMATION_TABLE_VERSION_4)
UINT32 ProcessorIDList;
//#endif
//#endif
} CHIPSET_ACM_INFORMATION_TABLE;
#define ACM_CHIPSET_ID_REVISION_ID_MAKE 0x1
typedef struct {
UINT32 Flags;
UINT16 VendorID;
UINT16 DeviceID;
UINT16 RevisionID;
UINT8 Reserved[6];
} ACM_CHIPSET_ID;
typedef struct {
UINT32 Count;
ACM_CHIPSET_ID ChipsetID[1];
} CHIPSET_ID_LIST;
typedef struct {
UINT32 FMS;
UINT32 FMSMask;
UINT64 PlatformID;
UINT64 PlatformMask;
} ACM_PROCESSOR_ID;
typedef struct {
UINT32 Count;
ACM_PROCESSOR_ID ProcessorID[1];
} PROCESSOR_ID_LIST;
typedef union {
struct {
UINT32 Stepping : 4;
UINT32 Model : 4;
UINT32 Family : 4;
UINT32 Type : 2;
UINT32 Reserved1 : 2;
UINT32 ExtendedModel : 4;
UINT32 ExtendedFamily: 8;
UINT32 Reserved2 : 4;
} Bits;
UINT32 Uint32;
} PROCESSOR_ID;
#pragma pack ()
ACM_GUID mChipsetAcmInformationTableGuid03 = CHIPSET_ACM_INFORMATION_TABLE_GUID_V03;
//
// BIOS INFO data structure
// This is self contained data structure for BIOS info
//
#pragma pack (1)
#define BIOS_INFO_SIGNATURE SIGNATURE_64 ('$', 'B', 'I', 'O', 'S', 'I', 'F', '$')
typedef struct {
UINT64 Signature;
UINT32 EntryCount;
UINT32 Reserved;
//BIOS_INFO_STRUCT Struct[EntryCount];
} BIOS_INFO_HEADER;
//
// BIOS_INFO_STRUCT attributes
// bits[0:3] means general attributes
// bits[4:7] means type specific attributes
//
#define BIOS_INFO_STRUCT_ATTRIBUTE_GENERAL_EXCLUDE_FROM_FIT 0x01
#define BIOS_INFO_STRUCT_ATTRIBUTE_MICROCODE_WHOLE_REGION 0x10
#define BIOS_INFO_STRUCT_ATTRIBUTE_BIOS_POST_IBB 0x10
typedef struct {
//
// FitTable entry type
//
UINT8 Type;
//
// BIOS_INFO_STRUCT attributes
//
UINT8 Attributes;
//
// FitTable entry version
//
UINT16 Version;
//
// FitTable entry real size
//
UINT32 Size;
//
// FitTable entry address
//
UINT64 Address;
} BIOS_INFO_STRUCT;
#pragma pack ()
#define MAX_BIOS_MODULE_ENTRY 0x20
#define MAX_MICROCODE_ENTRY 0x20
#define MAX_STARTUP_ACM_ENTRY 0x20
#define MAX_OPTIONAL_ENTRY 0x20
#define MAX_PORT_ENTRY 0x20
#define DEFAULT_FIT_TABLE_POINTER_OFFSET 0x40
#define DEFAULT_FIT_ENTRY_VERSION 0x0100
#define STARTUP_ACM_FIT_ENTRY_200_VERSION 0x0200
#define MAX_MMCFW_MODULE_ENTRY 0x02
#define TOP_FLASH_ADDRESS (gFitTableContext.TopFlashAddressRemapValue)
#define MEMORY_TO_FLASH(FileBuffer, FvBuffer, FvSize) \
(UINTN)(TOP_FLASH_ADDRESS - ((UINTN)(FvBuffer) + (UINTN)(FvSize) - (UINTN)(FileBuffer)))
#define FLASH_TO_MEMORY(Address, FvBuffer, FvSize) \
(VOID *)(UINTN)((UINTN)(FvBuffer) + (UINTN)(FvSize) - (TOP_FLASH_ADDRESS - (UINTN)(Address)))
#define FIT_TABLE_TYPE_HEADER 0
#define FIT_TABLE_TYPE_MICROCODE 1
#define FIT_TABLE_TYPE_STARTUP_ACM 2
#define FIT_TABLE_TYPE_DIAGNST_ACM 3
#define FIT_TABLE_TYPE_PROT_BOOT_POLICY 4
#define FIT_TABLE_TYPE_MMC_FW 5
#define FIT_TABLE_TYPE_BIOS_MODULE 7
#define FIT_TABLE_TYPE_TPM_POLICY 8
#define FIT_TABLE_TYPE_BIOS_POLICY 9
#define FIT_TABLE_TYPE_TXT_POLICY 10
#define FIT_TABLE_TYPE_KEY_MANIFEST 11
#define FIT_TABLE_TYPE_BOOT_POLICY_MANIFEST 12
#define FIT_TABLE_TYPE_FSP_BOOT_MANIFEST 13
#define FIT_TABLE_TYPE_CSE_SECURE_BOOT 16
#define FIT_TABLE_SUBTYPE_FIT_PATCH_MANIFEST 12
#define FIT_TABLE_SUBTYPE_ACM_MANIFEST 13
#define FIT_TABLE_TYPE_VAB_PROVISION_TABLE 26
#define FIT_TABLE_TYPE_VAB_BOOT_IMAGE_MANIFEST 27
#define FIT_TABLE_TYPE_VAB_BOOT_KEY_MANIFEST 28
//
// With OptionalModule Address isn't known until free space has been
// identified and the optional module has been copied into the FLASH
// image buffer (or initialized to be populated later by another program).
// This is very dangerous code as it can truncate 64b pointers to
// allocated memory buffers. The full pointer is in Buffer for that case.
//
typedef struct {
UINT32 Type;
UINT32 SubType; // Used by OptionalModule only
UINT32 Address;
UINT8 *Buffer; // Used by OptionalModule only
UINT32 Size;
UINT32 Version; // Used by OptionalModule and PortModule only
UINT32 FMS; // Used by Entry Type 02 (ACM) Ver. 0x200 only
UINT32 FMSMask; // Used by Entry Type 02 (ACM) Ver. 0x200 only
} FIT_TABLE_CONTEXT_ENTRY;
typedef struct {
BOOLEAN Clear;
UINT32 FitTablePointerOffset;
UINT32 FitTablePointerOffset2;
UINT32 FitEntryNumber;
UINT32 BiosModuleNumber;
UINT32 MicrocodeNumber;
UINT32 MmcFwNumber;
UINT32 StartupAcmNumber;
UINT32 OptionalModuleNumber;
UINT32 PortModuleNumber;
UINT32 GlobalVersion;
UINT32 FitHeaderVersion;
FIT_TABLE_CONTEXT_ENTRY StartupAcm[MAX_STARTUP_ACM_ENTRY];
UINT32 StartupAcmFvSize;
FIT_TABLE_CONTEXT_ENTRY DiagnstAcm;
UINT32 DiagnstAcmVersion;
FIT_TABLE_CONTEXT_ENTRY ProtBootPolicy;
FIT_TABLE_CONTEXT_ENTRY BiosModule[MAX_BIOS_MODULE_ENTRY];
UINT32 BiosModuleVersion;
FIT_TABLE_CONTEXT_ENTRY Microcode[MAX_MICROCODE_ENTRY];
BOOLEAN MicrocodeIsAligned;
UINT32 MicrocodeAlignValue;
UINT32 MicrocodeVersion;
FIT_TABLE_CONTEXT_ENTRY OptionalModule[MAX_OPTIONAL_ENTRY];
FIT_TABLE_CONTEXT_ENTRY PortModule[MAX_PORT_ENTRY];
FIT_TABLE_CONTEXT_ENTRY MmcFw[MAX_MMCFW_MODULE_ENTRY];
UINT64 TopFlashAddressRemapValue;
} FIT_TABLE_CONTEXT;
FIT_TABLE_CONTEXT gFitTableContext = {0};
unsigned int
xtoi (
char *str
);
/**
Pass in supported CPU extended family/extended model/type
/family/model without stepping or CPU FMS >> 4.
@param FitEntry Pointer to Fit Entry table.
@param AcmFamilyModel Acm Family Model stepping.
@param AcmMask ACM mask.
@return STATUS_SUCCESS The file found and data read.
**/
STATUS
SetFirmwareInterfaceTableEntryAcmFms(
FIRMWARE_INTERFACE_TABLE_ENTRY *FitEntry,
UINT32 AcmFamilyModel,
UINT32 AcmMask
)
{
if (FitEntry == NULL) {
return STATUS_ERROR;
}
FitEntry->Checksum = (UINT8)(((AcmFamilyModel & 0x000F0000) >> 16) | (((AcmMask & 0x000F0000) >> 16) << 4));
FitEntry->Rsvd = (UINT8)((AcmMask & 0x0000FF00) >> 8);
FitEntry->Size[2] = (UINT8)(AcmMask & 0x000000FF);
FitEntry->Size[1] = (UINT8)((AcmFamilyModel & 0x0000FF00) >> 8);
FitEntry->Size[0] = (UINT8)(AcmFamilyModel & 0x000000FF);
return STATUS_SUCCESS;
}
/**
Set the FIT Entry Size.
@param FitEntry Pointer to Fit Entry table.
@param SizeEntry Size of FIT entry.
@return STATUS_SUCCESS The file found and data read.
**/
STATUS
SetFirmwareInterfaceTableEntrySize (
FIRMWARE_INTERFACE_TABLE_ENTRY *FitEntry,
UINT32 SizeEntry
)
{
if (FitEntry == NULL) {
return STATUS_ERROR;
}
FitEntry->Size[2] = (UINT8)((SizeEntry & 0x00FF0000) >> 16);
FitEntry->Size[1] = (UINT8)((SizeEntry & 0x0000FF00) >> 8);
FitEntry->Size[0] = (UINT8)(SizeEntry & 0x000000FF);
return STATUS_SUCCESS;
}
/**
Get the FIT Entry Size.
@param FitEntry Pointer to Fit Entry table.
@return FitEntry pointer
**/
UINT32
GetFirmwareInterfaceTableEntrySize (
FIRMWARE_INTERFACE_TABLE_ENTRY *FitEntry
)
{
if (FitEntry == NULL) {
return 0;
}
return (((UINT32)FitEntry->Size[2] << 16) | ((UINT32)FitEntry->Size[1] << 8) | (UINT32)FitEntry->Size[0]);
}
/**
Displays the FIT utility info
@param None
@return None
**/
VOID
PrintUtilityInfo (
VOID
)
{
printf (
"%s - Tiano IA32/X64 FIT table generation Utility for FIT spec revision %i.%i."" Version %i.%i\n\n",
UTILITY_NAME,
FIT_SPEC_VERSION_MAJOR,
FIT_SPEC_VERSION_MINOR,
UTILITY_MAJOR_VERSION,
UTILITY_MINOR_VERSION
);
}
/**
Displays the utility usage syntax to STDOUT.
@param None
@return None
**/
VOID
PrintUsage (
VOID
)
{
printf ("Usage (generate): %s [-D] InputFvRecoveryFile OutputFvRecoveryFile\n"
"\t[-V <FitEntryDefaultVersion>]\n"
"\t[-F <FitTablePointerOffset>] [-F <FitTablePointerOffset>] [-V <FitHeaderVersion>]\n"
"\t[-NA]\n"
"\t[-A <MicrocodeAlignment>]\n"
"\t[-REMAP <TopFlashAddress>\n"
"\t[-CLEAR]\n"
"\t[-L <MicrocodeSlotSize> <MicrocodeFfsGuid>]\n"
"\t[-LF <MicrocodeSlotSize>]\n"
"\t[-I <BiosInfoGuid>]\n"
"\t[-S <StartupAcmAddress StartupAcmSize>|<StartupAcmGuid>] [-I <StartupAcmFMS StartupAcmFMSMask>] [-V <StartupAcmVersion>]\n"
"\t[-U <DiagnstAcmAddress>|<DiagnstAcmGuid>]\n"
"\t[-B <BiosModuleAddress BiosModuleSize>] [-B ...] [-V <BiosModuleVersion>]\n"
"\t[-M <MicrocodeAddress MicrocodeSize>] [-M ...]|[-U <MicrocodeFv MicrocodeBase>|<MicrocodeRegionOffset MicrocodeRegionSize>|<MicrocodeGuid>] [-V <MicrocodeVersion>]\n"
"\t[-O RecordType <RecordDataAddress RecordDataSize>|<RESERVE RecordDataSize>|<RecordDataGuid>|<RecordBinFile>|<CseRecordSubType RecordBinFile> [-V <RecordVersion>]] [-O ... [-V ...]]\n"
"\t[-P RecordType <IndexPort DataPort Width Bit Index> [-V <RecordVersion>]] [-P ... [-V ...]]\n"
"\t[-BP <BootPolicySize>[-V <BootPolicyVersion>]\n"
"\t[-T <FixedFitLocation>]\n"
, UTILITY_NAME);
printf (" Where:\n");
printf ("\t-D - It is FD file instead of FV file. (The tool will search FV file)\n");
printf ("\tInputFvRecoveryFile - Name of the input FvRecovery.fv file.\n");
printf ("\tOutputFvRecoveryFile - Name of the output FvRecovery.fv file.\n");
printf ("\tFitTablePointerOffset - FIT table pointer offset. 0x%x as default. 0x18 for current soon to be obsoleted CPUs. User can set both.\n", DEFAULT_FIT_TABLE_POINTER_OFFSET);
printf ("\tBiosInfoGuid - Guid of BiosInfo Module. If this module exists, StartupAcm/Bios/Microcode can be optional.\n");
printf ("\tStartupAcmAddress - Address of StartupAcm.\n");
printf ("\tStartupAcmSize - The maximum size value that could place the StartupAcm in.\n");
printf ("\tStartupAcmGuid - Guid of StartupAcm Module, if StartupAcm is in a BiosModule, it will be excluded form that.\n");
printf ("\tStartupAcmFMS - Value of PROCESSOR ID (Family/Model/Stepping value called \"FMS\") - see detail on FIT spec (1.3).\n");
printf ("\tStartupAcmFMSMask - Value use for uCode (if it recognizes 0x200 Type2 entry) to do bitmask logic operation with CPU processor ID.\n");
printf ("\t If the result match to StartupAcmFMS, corresponding ACM will be loaded - see detail on FIT spec (1.3).\n");
printf ("\tDiagnstAcmAddress - Address of DiagnstAcm.\n");
printf ("\tDiagnstAcmGuid - Guid of DiagnstAcm Module, if DiagnstAcm is in a BiosModule, it will be excluded from that.\n");
printf ("\tBiosModuleAddress - Address of BiosModule. User should ensure there is no overlap.\n");
printf ("\tBiosModuleSize - Size of BiosModule.\n");
printf ("\tMicrocodeAddress - Address of Microcode.\n");
printf ("\tMicrocodeSize - Size of Microcode.\n");
printf ("\tMicrocodeFv - Name of Microcode.fv file.\n");
printf ("\tMicrocodeBase - The base address of Microcode.fv in final FD image.\n");
printf ("\tMicrocodeRegionOffset - Offset of Microcode region in input FD image.\n");
printf ("\tMicrocodeRegionSize - Size of Microcode region in input FD image.\n");
printf ("\tMicrocodeGuid - Guid of Microcode Module.\n");
printf ("\tMicrocodeSlotSize - Occupied region size of each Microcode binary.\n");
printf ("\tMicrocodeFfsGuid - Guid of FFS which is used to save Microcode binary");
printf ("\t-LF - Microcode Slot mode without FFS check, treat all Microcode FV as slot mode. In this case the Microcode FV should only contain one FFS.\n");
printf ("\t-NA - No 0x800 aligned Microcode requirement. No -NA means Microcode is aligned with option MicrocodeAlignment value.\n");
printf ("\tMicrocodeAlignment - HEX value of Microcode alignment. Ignored if \"-NA\" is specified. Default value is 0x800. The Microcode update data must start at a 16-byte aligned linear address.\n");
printf ("\tRecordType - FIT entry record type. User should ensure it is ordered.\n");
printf ("\tRecordDataAddress - FIT entry record data address.\n");
printf ("\tRecordDataSize - FIT entry record data size.\n");
printf ("\tRecordDataGuid - FIT entry record data GUID.\n");
printf ("\tRecordBinFile - FIT entry record data binary file.\n");
printf ("\tCseRecordSubType - FIT entry record subtype. Use to further distinguish CSE entries (see FIT spec revision 1.2 chapter 4.12).\n");
printf ("\tBootPolicySize - FIT entry size for type 04 boot policy.\n");
printf ("\tFitEntryDefaultVersion - The default version for all FIT table entries. 0x%04x is used if this is not specified.\n", DEFAULT_FIT_ENTRY_VERSION);
printf ("\tFitHeaderVersion - The version for FIT header. (Override default version)\n");
printf ("\tStartupAcmVersion - The version for StartupAcm. (Override default version)\n");
printf ("\tBiosModuleVersion - The version for BiosModule. (Override default version)\n");
printf ("\tMicrocodeVersion - The version for Microcode. (Override default version)\n");
printf ("\tRecordVersion - The version for Record. (Override default version)\n");
printf ("\tBootPolicyVersion - The version for BootPolicy. (Override default version)\n");
printf ("\tIndexPort - The Index Port Number.\n");
printf ("\tDataPort - The Data Port Number.\n");
printf ("\tWidth - The Width of the port.\n");
printf ("\tBit - The Bit Number of the port.\n");
printf ("\tIndex - The Index Number of the port.\n");
printf ("\tFixedFitLocation - Fixed FIT location in flash address. FIT table will be generated at this location and Option Modules will be directly put right before it.\n");
printf ("\nUsage (view): %s [-view] InputFile -F <FitTablePointerOffset>\n", UTILITY_NAME);
printf (" Where:\n");
printf ("\tInputFile - Name of the input file.\n");
printf ("\tFitTablePointerOffset - FIT table pointer offset from end of file. 0x%x as default.\n", DEFAULT_FIT_TABLE_POINTER_OFFSET);
printf ("\nTool return values:\n");
printf ("\tSTATUS_SUCCESS=%d, STATUS_WARNING=%d, STATUS_ERROR=%d\n", STATUS_SUCCESS, STATUS_WARNING, STATUS_ERROR);
}
/**
Set Value of memory.
@param Buffer The pointer where we need to set the memory.
@param Length Size of memory to be set.
@param Value Value of memory to be set.
@return Buffer The pointer address.
**/
VOID *
SetMem (
OUT VOID *Buffer,
IN UINTN Length,
IN UINT8 Value
)
{
//
// Declare the local variables that actually move the data elements as
// volatile to prevent the optimizer from replacing this function with
// the intrinsic memset()
//
volatile UINT8 *Pointer;
Pointer = (UINT8*)Buffer;
while (Length-- > 0) {
*(Pointer++) = Value;
}
return Buffer;
}
/**
check the input Path.
@param String Passed input path.
@return TRUE If the input path is correct.
@return FLASE if the input path is not correct.
**/
BOOLEAN
CheckPath (
IN CHAR8 * String
)
{
//
//Return FLASE if input file path include % character or is NULL
//
CHAR8 *StrPtr;
StrPtr = String;
if (StrPtr == NULL) {
return FALSE;
}
if (*StrPtr == 0) {
return FALSE;
}
while (*StrPtr != '\0') {
if (*StrPtr == '%') {
return FALSE;
}
StrPtr++;
}
return TRUE;
}
/**
Get fixed FIT location from argument.
@param argc Number of command line parameters.
@param argv Array of pointers to parameter strings.
@return FitLocation The FIT location specified by Argument.
@return 0 Argument parse fail.
**/
UINT32
GetFixedFitLocation (
IN INTN argc,
IN CHAR8 **argv
)
{
UINT32 FitLocation;
INTN Index;
FitLocation = 0;
for (Index = 0; Index + 1 < argc; Index ++) {
if ((strcmp (argv[Index], "-T") == 0) ||
(strcmp (argv[Index], "-t") == 0) ) {
FitLocation = xtoi (argv[Index + 1]);
break;
}
}
return FitLocation;
}
/**
Read input file.
@param FileName The input file name.
@param FileData The input file data, the memory is aligned.
@param FileSize The input file size.
@param FileBufferRaw The memory to hold input file data. The caller must free the memory.
@return STATUS_SUCCESS The file found and data read.
@return STATUS_ERROR The file data is not read.
@return STATUS_WARNING The file is not found.
**/
STATUS
ReadInputFile (
IN CHAR8 *FileName,
OUT UINT8 **FileData,
OUT UINT32 *FileSize,
OUT UINT8 **FileBufferRaw OPTIONAL
)
{
FILE *FpIn;
UINT32 TempResult;
//
//Check the File Path
//
if (!CheckPath(FileName)) {
Error (NULL, 0, 0, "File path is invalid!", NULL);
return STATUS_ERROR;
}
//
// Open the Input FvRecovery.fv file
//
if ((FpIn = fopen (FileName, "rb")) == NULL) {
//
// Return WARNING, let caller make decision
//
// Error (NULL, 0, 0, "Unable to open file", FileName);
return STATUS_WARNING;
}
//
// Get the Input FvRecovery.fv file size
//
fseek (FpIn, 0, SEEK_END);
*FileSize = ftell (FpIn);
//
// Read the contents of input file to memory buffer
//
if (FileBufferRaw != NULL) {
*FileBufferRaw = (UINT8 *) malloc (*FileSize + 0x10000);
if (NULL == *FileBufferRaw) {
Error (NULL, 0, 0, "No sufficient memory to allocate!", NULL);
fclose (FpIn);
return STATUS_ERROR;
}
TempResult = 0x10000 - (UINT32) ((UINTN)*FileBufferRaw & 0x0FFFF);
*FileData = (UINT8 *)((UINTN)*FileBufferRaw + TempResult);
} else {
*FileData = (UINT8 *) malloc (*FileSize);
if (NULL == *FileData) {
Error (NULL, 0, 0, "No sufficient memory to allocate!", NULL);
fclose (FpIn);
return STATUS_ERROR;
}
}
fseek (FpIn, 0, SEEK_SET);
TempResult = fread (*FileData, 1, *FileSize, FpIn);
if (TempResult != *FileSize) {
Error (NULL, 0, 0, "Read input file error!", NULL);
if (FileBufferRaw != NULL) {
free ((VOID *)*FileBufferRaw);
} else {
free ((VOID *)*FileData);
}
fclose (FpIn);
return STATUS_ERROR;
}
//
// Close the input FvRecovery.fv file
//
fclose (FpIn);
return STATUS_SUCCESS;
}
/**
Find next FvHeader in the FileBuffer.
@param FileBuffer The start FileBuffer which needs to be searched.
@param FileLength The whole File Length.
@return FvHeader The FvHeader is found successfully.
@return NULL The FvHeader is not found.
**/
UINT8 *
FindNextFvHeader (
IN UINT8 *FileBuffer,
IN UINTN FileLength
)
{
UINT8 *FileHeader;
EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
UINT16 FileChecksum;
FileHeader = FileBuffer;
for (; (UINTN)FileBuffer < (UINTN)FileHeader + FileLength; FileBuffer += 8) {
FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FileBuffer;
if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
//
// potential candidate
//
//
// Check checksum
//
if (FvHeader->FvLength > FileLength) {
continue;
}
if (FvHeader->HeaderLength >= FileLength) {
continue;
}
FileChecksum = CalculateChecksum16 ((UINT16 *)FileBuffer, FvHeader->HeaderLength / sizeof (UINT16));
if (FileChecksum != 0) {
continue;
}
//
// Check revision and reserved field
//
#if (PI_SPECIFICATION_VERSION < 0x00010000)
if ((FvHeader->Revision == EFI_FVH_REVISION) &&
(FvHeader->Reserved[0] == 0) &&
(FvHeader->Reserved[1] == 0) &&
(FvHeader->Reserved[2] == 0) ){
return FileBuffer;
}
#else
if ((FvHeader->Revision == EFI_FVH_PI_REVISION) &&
(FvHeader->Reserved[0] == 0) ){
return FileBuffer;
}
#endif
}
}
return NULL;
}
/**
Find File with GUID in an FV.
@param FvBuffer FV binary buffer.
@param FvSize FV size.
@param Guid File GUID value to be searched.
@param FileSize Guid File size.
@return FileLocation Guid File location.
@return NULL Guid File is not found.
**/
UINT8 *
FindFileFromFvByGuid (
IN UINT8 *FvBuffer,
IN UINT32 FvSize,
IN EFI_GUID *Guid,
OUT UINT32 *FileSize
)
{
EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
EFI_FFS_FILE_HEADER *FileHeader;
UINT64 FvLength;
EFI_GUID *TempGuid;
UINT8 *FixPoint;
UINTN Offset;
UINTN FileLength;
UINTN FileOccupiedSize;
//
// Find the FFS file
//
FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FindNextFvHeader (FvBuffer, FvSize);
if (NULL == FvHeader) {
return NULL;
}
while (TRUE) {
FvLength = FvHeader->FvLength;
//
// Prepare to walk the FV image
//
InitializeFvLib (FvHeader, (UINT32)FvLength);
FileHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FvHeader + FvHeader->HeaderLength);
Offset = (UINTN) FileHeader - (UINTN) FvHeader;
while (Offset < FvLength) {
TempGuid = (EFI_GUID *)&(FileHeader->Name);
FileLength = (*(UINT32 *)(FileHeader->Size)) & 0x00FFFFFF;
FileOccupiedSize = GETOCCUPIEDSIZE(FileLength, 8);
if ((CompareGuid (TempGuid, Guid)) == 0) {
//
// Good! Find it.
//
FixPoint = ((UINT8 *)FileHeader + sizeof(EFI_FFS_FILE_HEADER));
//
// Find the position of file module, the offset
// between the position and the end of FvRecovery.fv file
// should not exceed 128kB to prevent reset vector from
// outside legacy E and F segment
//
if ((UINTN)FvHeader + FvLength - (UINTN)FixPoint > 0x20000) {
// printf ("WARNING: The position of file module is not in E and F segment!\n");
// return NULL;
}
*FileSize = FileLength - sizeof(EFI_FFS_FILE_HEADER);
#if (PI_SPECIFICATION_VERSION < 0x00010000)
if (FileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
*FileSize -= sizeof(EFI_FFS_FILE_TAIL);
}
#endif
return FixPoint;
}
FileHeader = (EFI_FFS_FILE_HEADER *)((UINTN)FileHeader + FileOccupiedSize);
Offset = (UINTN) FileHeader - (UINTN) FvHeader;
}
//
// Not found, check next FV
//
if ((UINTN)FvBuffer + FvSize > (UINTN)FvHeader + FvLength) {
FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FindNextFvHeader ((UINT8 *)FvHeader + (UINTN)FvLength, (UINTN)FvBuffer + FvSize - ((UINTN)FvHeader + (UINTN)FvLength));
if (FvHeader == NULL) {
break;
}
} else {
break;
}
}
//
// Not found
//
return NULL;
}
/**
Check whether a string is a GUID.
@param StringData The String.
@param Guid Guid to hold the value
@return TRUE StringData is a GUID, and Guid field is filled.
@return FALSE StringData is not a GUID.
**/
BOOLEAN
IsGuidData (
IN CHAR8 *StringData,
OUT EFI_GUID *Guid
)
{
if (strlen (StringData) != strlen ("00000000-0000-0000-0000-000000000000")) {
return FALSE;
}
if ((StringData[8] != '-') ||
(StringData[13] != '-') ||
(StringData[18] != '-') ||
(StringData[23] != '-') ) {
return FALSE;
}
StringToGuid (StringData, Guid);
return TRUE;
}
/**
Get FIT entry number and fill global FIT table context, from argument.
@param argc Number of command line parameters.
@param argv Array of pointers to parameter strings.
@param FdBuffer FD binary buffer.
@param FdSize FD size.
@return FitEntryNumber The FIT entry number.
@return 0 Argument parse fail.
**/
VOID
CheckOverlap (
IN UINT32 Address,
IN UINT32 Size
)
{
INTN Index;
for (Index = 0; Index < (INTN)gFitTableContext.BiosModuleNumber; Index ++) {
if ((gFitTableContext.BiosModule[Index].Address <= Address) &&
((gFitTableContext.BiosModule[Index].Size - Size) >= (Address - gFitTableContext.BiosModule[Index].Address))) {
UINT32 TempSize;
INT32 SubIndex;
//
// Found overlap, split BiosModuleEntry
// Currently only support StartupAcm in 1 BiosModule. It does not support StartupAcm across 2 BiosModule or more.
//
if (gFitTableContext.BiosModuleNumber >= MAX_BIOS_MODULE_ENTRY) {
Error (NULL, 0, 0, "Too many Bios Module!", NULL);
return ;
}
if (Address != gFitTableContext.BiosModule[Index].Address) {
//
// Skip the entry whose start address is same as StartupAcm
//
gFitTableContext.BiosModule[gFitTableContext.BiosModuleNumber].Type = FIT_TABLE_TYPE_BIOS_MODULE;
gFitTableContext.BiosModule[gFitTableContext.BiosModuleNumber].Address = gFitTableContext.BiosModule[Index].Address;
gFitTableContext.BiosModule[gFitTableContext.BiosModuleNumber].Size = Address - gFitTableContext.BiosModule[Index].Address;
gFitTableContext.BiosModuleNumber ++;
gFitTableContext.FitEntryNumber++;
}
TempSize = gFitTableContext.BiosModule[Index].Address + gFitTableContext.BiosModule[Index].Size;
gFitTableContext.BiosModule[Index].Address = Address + Size;
gFitTableContext.BiosModule[Index].Size = TempSize - gFitTableContext.BiosModule[Index].Address;
if (gFitTableContext.BiosModule[Index].Size == 0) {
//
// remove the entry if size is 0
//
for (SubIndex = Index; SubIndex < (INTN)gFitTableContext.BiosModuleNumber - 1; SubIndex ++) {
gFitTableContext.BiosModule[SubIndex].Address = gFitTableContext.BiosModule[SubIndex + 1].Address;
gFitTableContext.BiosModule[SubIndex].Size = gFitTableContext.BiosModule[SubIndex + 1].Size;
}
gFitTableContext.BiosModuleNumber --;
gFitTableContext.FitEntryNumber--;
}
break;
}
}
}
/**
Get FIT entry number and fill global FIT table context, from argument.
@param argc Number of command line parameters.
@param argv Array of pointers to parameter strings.
@param FdBuffer FD binary buffer.
@param FdSize FD size.
@return FitEntryNumber The FIT entry number.
@return 0 Argument parse fail.
**/
UINT8 *
GetMicrocodeBufferFromFv (
EFI_FIRMWARE_VOLUME_HEADER *FvHeader
)
{
UINT8 *MicrocodeBuffer;
EFI_FFS_FILE_HEADER *FfsHeader;
MicrocodeBuffer = NULL;
//
// Skip FV header + FV extension header + FFS header
//
FfsHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *) FvHeader + FvHeader->HeaderLength);
while ((UINT8 *) FfsHeader < (UINT8 *) FvHeader + FvHeader->FvLength) {
if (FfsHeader->Type == EFI_FV_FILETYPE_RAW) {
//
// Find the first RAW ffs file as Microcode Buffer