summaryrefslogtreecommitdiffstats
path: root/DuetPkg/DxeIpl/HobGeneration.c
blob: c5b6c7c1b22cce6902a53986d475d5767e389cda (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
/** @file

Copyright (c) 2006 - 2010, Intel Corporation                                                         
All rights reserved. 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.             

Module Name:
  HobGeneration.c

Abstract:

Revision History:

**/
#include "DxeIpl.h"
#include "HobGeneration.h"
#include "PpisNeededByDxeCore.h"
#include "FlashLayout.h"
#include "Debug.h"

#define EFI_DXE_FILE_GUID \
  { 0xb1644c1a, 0xc16a, 0x4c5b, {0x88, 0xde, 0xea, 0xfb, 0xa9, 0x7e, 0x74, 0xd8 }}

#define EFI_CPUID_EXTENDED_FUNCTION  0x80000000
#define CPUID_EXTENDED_ADD_SIZE      0x80000008

HOB_TEMPLATE  gHobTemplate = {
  { // Phit
    {  // Header
      EFI_HOB_TYPE_HANDOFF,                 // HobType
      sizeof (EFI_HOB_HANDOFF_INFO_TABLE),  // HobLength
      0                                     // Reserved
    },
    EFI_HOB_HANDOFF_TABLE_VERSION,          // Version
    BOOT_WITH_FULL_CONFIGURATION,           // BootMode
    0,                                      // EfiMemoryTop
    0,                                      // EfiMemoryBottom
    0,                                      // EfiFreeMemoryTop
    0,                                      // EfiFreeMemoryBottom
    0                                       // EfiEndOfHobList
  }, 
  { // Bfv
    {
      EFI_HOB_TYPE_FV,                      // HobType
      sizeof (EFI_HOB_FIRMWARE_VOLUME),     // HobLength
      0                                     // Reserved
    },
    0,                                      // BaseAddress
    0                                       // Length
  },
  { // BfvResource
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,     // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR), // HobLength
      0                                     // Reserved
    },
    {
      0                                     // Owner Guid
    },
    EFI_RESOURCE_FIRMWARE_DEVICE,           // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT    |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
     EFI_RESOURCE_ATTRIBUTE_TESTED |
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),  // ResourceAttribute
    0,                                              // PhysicalStart
    0                                               // ResourceLength
  },
  { // Cpu
    { // Header
      EFI_HOB_TYPE_CPU,                     // HobType
      sizeof (EFI_HOB_CPU),                 // HobLength
      0                                     // Reserved
    },
    52,                                     // SizeOfMemorySpace - Architecture Max
    16,                                     // SizeOfIoSpace,
    {
      0, 0, 0, 0, 0, 0                      // Reserved[6]
    }
  },
  {   // Stack HOB
    {   // header
      EFI_HOB_TYPE_MEMORY_ALLOCATION,               // Hob type
      sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK),     // Hob size
      0                                             // reserved
    },
    {
      EFI_HOB_MEMORY_ALLOC_STACK_GUID,
      0x0,                                          // EFI_PHYSICAL_ADDRESS  MemoryBaseAddress;
      0x0,                                          // UINT64                MemoryLength;
      EfiBootServicesData,                          // EFI_MEMORY_TYPE       MemoryType;  
      {0, 0, 0, 0}                                  // Reserved              Reserved[4]; 
    }
  },
  { // MemoryAllocation for HOB's & Images
    {
      EFI_HOB_TYPE_MEMORY_ALLOCATION,               // HobType
      sizeof (EFI_HOB_MEMORY_ALLOCATION),           // HobLength
      0                                             // Reserved
    },
    {
      {
        0, //EFI_HOB_MEMORY_ALLOC_MODULE_GUID       // Name
      },
      0x0,                                          // EFI_PHYSICAL_ADDRESS  MemoryBaseAddress;
      0x0,                                          // UINT64                MemoryLength;
      EfiBootServicesData,                          // EFI_MEMORY_TYPE       MemoryType;  
      {
        0, 0, 0, 0                                  // Reserved              Reserved[4]; 
      }
    }
   },
  { // MemoryFreeUnder1MB for unused memory that DXE core will claim
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,             // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR),         // HobLength
      0                                             // Reserved
    },
    {
      0                                             // Owner Guid
    },
    EFI_RESOURCE_SYSTEM_MEMORY,                     // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT                 |
     EFI_RESOURCE_ATTRIBUTE_TESTED                  |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED             |
     EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE             | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE       | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),     
    0x0,                                            // PhysicalStart
    0                                               // ResourceLength
  },
  { // MemoryFreeAbove1MB for unused memory that DXE core will claim
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,             // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR),         // HobLength
      0                                             // Reserved
    },
    {
      0                                             // Owner Guid
    },
    EFI_RESOURCE_SYSTEM_MEMORY,                     // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT                 |
     EFI_RESOURCE_ATTRIBUTE_TESTED                  |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED             |
     EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE             | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE       | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),     
    0x0,                                            // PhysicalStart
    0                                               // ResourceLength
  },
  { // MemoryFreeAbove4GB for unused memory that DXE core will claim
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,             // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR),         // HobLength
      0                                             // Reserved
    },
    {
      0                                             // Owner Guid
    },
    EFI_RESOURCE_SYSTEM_MEMORY,                     // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT                 |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED             |
     EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE             | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE       | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),     
    0x0,                                            // PhysicalStart
    0                                               // ResourceLength
  },
  {   // Memory Allocation Module for DxeCore
    {   // header
      EFI_HOB_TYPE_MEMORY_ALLOCATION,               // Hob type
      sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE),    // Hob size
      0                                             // reserved
    },
    {
      EFI_HOB_MEMORY_ALLOC_MODULE_GUID,
      0x0,                                          // EFI_PHYSICAL_ADDRESS  MemoryBaseAddress;
      0x0,                                          // UINT64                MemoryLength;
      EfiBootServicesCode,                          // EFI_MEMORY_TYPE       MemoryType;  
      {
        0, 0, 0, 0                                  // UINT8                 Reserved[4]; 
      },
    },
    EFI_DXE_FILE_GUID,
    0x0                                             //  EFI_PHYSICAL_ADDRESS of EntryPoint;
  },
  { // MemoryDxeCore
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,             // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR),         // HobLength
      0                                             // Reserved
    },
    {
      0                                             // Owner Guid
    },
    EFI_RESOURCE_SYSTEM_MEMORY,                     // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT                 |
//     EFI_RESOURCE_ATTRIBUTE_TESTED                  | // Do not mark as TESTED, or DxeCore will find it and use it before check Allocation
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED             |
     EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE             | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE       | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | 
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),     
    0x0,                                            // PhysicalStart
    0                                               // ResourceLength
  },
  { // Memory Map Hints to reduce fragmentation in the memory map
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,                    // Hob type
        sizeof (MEMORY_TYPE_INFORMATION_HOB),           // Hob size
        0,                                              // reserved
      },
      EFI_MEMORY_TYPE_INFORMATION_GUID
    },
    {
      {
        EfiACPIReclaimMemory,
        0x80
      },  // 0x80 pages = 512k for ASL
      {
        EfiACPIMemoryNVS,
        0x100
      },  // 0x100 pages = 1024k for S3, SMM, etc
      {
        EfiReservedMemoryType,
        0x04
      },  // 16k for BIOS Reserved
      {
        EfiRuntimeServicesData,
        0x100
      },
      {
        EfiRuntimeServicesCode,
        0x100
      },
      {
        EfiBootServicesCode,
        0x200
      },
      {
        EfiBootServicesData,
        0x200
      },
      {
        EfiLoaderCode,
        0x100
      },
      {
        EfiLoaderData,
        0x100
      },
      {
        EfiMaxMemoryType,
        0
      }
    }
  },
  { // Pointer to ACPI Table
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (TABLE_HOB),                // Hob size
        0                                  // reserved
      },
      EFI_ACPI_TABLE_GUID
    },
    0
  },
  { // Pointer to ACPI20 Table
    {
      {  
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (TABLE_HOB),                // Hob size
        0                                  // reserved
      },
      EFI_ACPI_20_TABLE_GUID
    },
    0
  },
  { // Pointer to SMBIOS Table
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (TABLE_HOB),                // Hob size
        0                                  // reserved
      },
      SMBIOS_TABLE_GUID
    },
    0
  },
  { // Pointer to MPS Table
    {
      {
         EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (TABLE_HOB),                // Hob size
        0,                                 // reserved
      },
      EFI_MPS_TABLE_GUID
    },
    0
  },
  /**
  { // Pointer to FlushInstructionCache
    EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
    sizeof (PROTOCOL_HOB),             // Hob size
    0,                                 // reserved
    EFI_PEI_FLUSH_INSTRUCTION_CACHE_GUID,
    NULL
  },
  { // Pointer to TransferControl
    EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
    sizeof (PROTOCOL_HOB),             // Hob size
    0,                                 // reserved
    EFI_PEI_TRANSFER_CONTROL_GUID,
    NULL
  },
  { // Pointer to PeCoffLoader
    EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
    sizeof (PROTOCOL_HOB),             // Hob size
    0,                                 // reserved
    EFI_PEI_PE_COFF_LOADER_GUID,
    NULL
  },
  { // Pointer to EfiDecompress
    EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
    sizeof (PROTOCOL_HOB),             // Hob size
    0,                                 // reserved
    EFI_DECOMPRESS_PROTOCOL_GUID,
    NULL
  },
  { // Pointer to TianoDecompress
    EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
    sizeof (PROTOCOL_HOB),             // Hob size
    0,                                 // reserved
    EFI_TIANO_DECOMPRESS_PROTOCOL_GUID,
    NULL
  },
  **/
  { // Pointer to ReportStatusCode
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (PROTOCOL_HOB),             // Hob size
        0                                  // reserved
      },
      EFI_STATUS_CODE_RUNTIME_PROTOCOL_GUID
    },
    NULL
  },
  { // EFILDR Memory Descriptor
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (MEMORY_DESC_HOB),          // Hob size
        0                                  // reserved
      },
      EFI_LDR_MEMORY_DESCRIPTOR_GUID
    },
    0,
    NULL
  },
  { // Pci Express Base Address Hob
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (PCI_EXPRESS_BASE_HOB),     // Hob size
        0                                  // reserved
      },
      EFI_PCI_EXPRESS_BASE_ADDRESS_GUID
    },
    {
      0,
      0,
      0,
    }
  },
  { // Acpi Description Hob
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (ACPI_DESCRIPTION_HOB),     // Hob size
        0                                  // reserved
      },
      EFI_ACPI_DESCRIPTION_GUID
    },
    {
      {
        0,
      },
    }
  },
  { // NV Storage FV Resource
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,     // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR), // HobLength
      0                                     // Reserved
    },
    {
      0                                     // Owner Guid
    },
    EFI_RESOURCE_FIRMWARE_DEVICE,           // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT    |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
     EFI_RESOURCE_ATTRIBUTE_TESTED |
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),  // ResourceAttribute
    0,                                              // PhysicalStart (Fixed later)
    NV_STORAGE_FVB_SIZE                             // ResourceLength
  },
  { // FVB holding NV Storage
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (FVB_HOB),
        0
      },
      EFI_FLASH_MAP_HOB_GUID
    },
    {
      {0, 0, 0},                       // Reserved[3]
      EFI_FLASH_AREA_GUID_DEFINED,     // AreaType
      EFI_SYSTEM_NV_DATA_FV_GUID ,     // AreaTypeGuid
      1,
      {
        {
          EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV, // SubAreaData.Attributes
          0,                             // SubAreaData.Reserved
          0,                             // SubAreaData.Base (Fixed later)
          NV_STORAGE_FVB_SIZE,           // SubAreaData.Length
          EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID // SubAreaData.FileSystem
        }
      }, 
      0,                               // VolumeSignature (Fixed later)
      NV_STORAGE_FILE_PATH,            // Mapped file without padding
                                       //  TotalFVBSize = FileSize + PaddingSize = multiple of BLOCK_SIZE
      NV_STORAGE_SIZE + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH,
                                       // ActuralSize
      EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH
    }
  },
  { // NV Storage Hob
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (FVB_HOB),                  // Hob size
        0                                  // reserved
      },
      EFI_FLASH_MAP_HOB_GUID
    },
    {
      {0, 0, 0},                       // Reserved[3]
      EFI_FLASH_AREA_EFI_VARIABLES,    // AreaType
      { 0 },                           // AreaTypeGuid
      1,
      {
        {
          EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV, // SubAreaData.Attributes
          0,                             // SubAreaData.Reserved
          0,                             // SubAreaData.Base (Fixed later)
          NV_STORAGE_SIZE,               // SubAreaData.Length
          EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID // SubAreaData.FileSystem
        }
      }, 
      0,
      NV_STORAGE_FILE_PATH,
      NV_STORAGE_SIZE,
      0
    }
  },
  { // NV Ftw FV Resource
    {
      EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,     // HobType
      sizeof (EFI_HOB_RESOURCE_DESCRIPTOR), // HobLength
      0                                     // Reserved
    },
    {
      0                                     // Owner Guid
    },
    EFI_RESOURCE_FIRMWARE_DEVICE,           // ResourceType
    (EFI_RESOURCE_ATTRIBUTE_PRESENT    |
     EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
     EFI_RESOURCE_ATTRIBUTE_TESTED |
     EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE),  // ResourceAttribute
    0,                                              // PhysicalStart (Fixed later)
    NV_FTW_FVB_SIZE                                 // ResourceLength
  },  
  { // FVB holding FTW spaces including Working & Spare space
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (FVB_HOB),
        0
      },
      EFI_FLASH_MAP_HOB_GUID
    },
    {
      {0, 0, 0},                       // Reserved[3]
      EFI_FLASH_AREA_GUID_DEFINED,     // AreaType
      EFI_SYSTEM_NV_DATA_FV_GUID,      // AreaTypeGuid
      1,
      {
        {
          EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV, // SubAreaData.Attributes
          0,                             // SubAreaData.Reserved
          0,                             // SubAreaData.Base (Fixed later)
          NV_FTW_FVB_SIZE,               // SubAreaData.Length
          EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID   // SubAreaData.FileSystem
        }
      }, 
      0,
      L"",                             // Empty String indicates using memory
      0,
      0
    }
  },
  { // NV Ftw working Hob
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (FVB_HOB),                  // Hob size
        0                                  // reserved
      },
      EFI_FLASH_MAP_HOB_GUID
    },
    {
      {0, 0, 0},                       // Reserved[3]
      EFI_FLASH_AREA_FTW_STATE,        // AreaType
      { 0 },                           // AreaTypeGuid
      1,
      {
        {
          EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV, // SubAreaData.Attributes
          0,                             // SubAreaData.Reserved
          0,                             // SubAreaData.Base (Fixed later)
          NV_FTW_WORKING_SIZE,           // SubAreaData.Length
          EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID // SubAreaData.FileSystem
        }
      }, 
      0,                               // VolumeSignature
      L"",
      0,
      0
    }
  },
  { // NV Ftw spare Hob
    {
      {
        EFI_HOB_TYPE_GUID_EXTENSION,       // Hob type
        sizeof (FVB_HOB),                  // Hob size
        0                                  // reserved
      },
      EFI_FLASH_MAP_HOB_GUID
    },
    {
      {0, 0, 0},                       // Reserved[3]
      EFI_FLASH_AREA_FTW_BACKUP,       // AreaType
      { 0 },                           // AreaTypeGuid
      1,
      {
        {
          EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV, // SubAreaData.Attributes
          0,                             // SubAreaData.Reserved
          0,                             // SubAreaData.Base (Fixed later)
          NV_FTW_SPARE_SIZE,             // SubAreaData.Length
          EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL_GUID // SubAreaData.FileSystem
        }
      }, 
      0,
      L"",
      0,
      0
    }
  },
  { // EndOfHobList
    EFI_HOB_TYPE_END_OF_HOB_LIST,      // HobType
    sizeof (EFI_HOB_GENERIC_HEADER),   // HobLength
    0                                  // Reserved
  }
};

HOB_TEMPLATE  *gHob = &gHobTemplate;

VOID *
PrepareHobMemory (
  IN UINTN                    NumberOfMemoryMapEntries,
  IN EFI_MEMORY_DESCRIPTOR    *EfiMemoryDescriptor
  )
/*++
Description:
  Update the Hob filling MemoryFreeUnder1MB, MemoryAbove1MB, MemoryAbove4GB

Arguments:
  NumberOfMemoryMapEntries - Count of Memory Descriptors
  EfiMemoryDescriptor      - Point to the buffer containing NumberOfMemoryMapEntries Memory Descriptors

Return:
  VOID * : The end address of MemoryAbove1MB (or the top free memory under 4GB)
--*/
{
  UINTN                Index;

  //
  // Prepare Low Memory
  // 0x18 pages is 72 KB.
  //
  gHob->MemoryFreeUnder1MB.ResourceLength = EFI_MEMORY_BELOW_1MB_END - EFI_MEMORY_BELOW_1MB_START;
  gHob->MemoryFreeUnder1MB.PhysicalStart  = EFI_MEMORY_BELOW_1MB_START;

  //
  // Prepare High Memory
  // Assume Memory Map is ordered from low to high
  //
  gHob->MemoryAbove1MB.PhysicalStart   = 0;
  gHob->MemoryAbove1MB.ResourceLength  = 0;
  gHob->MemoryAbove4GB.PhysicalStart   = 0;
  gHob->MemoryAbove4GB.ResourceLength  = 0;

  for (Index = 0; Index < NumberOfMemoryMapEntries; Index++) {
    //
    // Skip regions below 1MB
    //
    if (EfiMemoryDescriptor[Index].PhysicalStart < 0x100000) {
      continue;
    }
    //
    // Process regions above 1MB
    //
    if (EfiMemoryDescriptor[Index].PhysicalStart >= 0x100000) {
      if (EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) {
        if (gHob->MemoryAbove1MB.PhysicalStart == 0) {
          gHob->MemoryAbove1MB.PhysicalStart = EfiMemoryDescriptor[Index].PhysicalStart;
          gHob->MemoryAbove1MB.ResourceLength = LShiftU64 (EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT);
        } else if (gHob->MemoryAbove1MB.PhysicalStart + gHob->MemoryAbove1MB.ResourceLength == EfiMemoryDescriptor[Index].PhysicalStart) {
          gHob->MemoryAbove1MB.ResourceLength += LShiftU64 (EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT);
        }
      }
      if ((EfiMemoryDescriptor[Index].Type == EfiReservedMemoryType) ||
          (EfiMemoryDescriptor[Index].Type >= EfiACPIReclaimMemory) ) {
        continue;
      }
      if ((EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesCode) ||
          (EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesData)) {
        break;
      }
    }
    //
    // Process region above 4GB
    //
    if (EfiMemoryDescriptor[Index].PhysicalStart >= 0x100000000LL) {
      if (EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) {
        if (gHob->MemoryAbove4GB.PhysicalStart == 0) {
          gHob->MemoryAbove4GB.PhysicalStart  = EfiMemoryDescriptor[Index].PhysicalStart;
          gHob->MemoryAbove4GB.ResourceLength = LShiftU64 (EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT);
        }
        if (gHob->MemoryAbove4GB.PhysicalStart + gHob->MemoryAbove4GB.ResourceLength == 
            EfiMemoryDescriptor[Index].PhysicalStart) {
          gHob->MemoryAbove4GB.ResourceLength += LShiftU64 (EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT);
        }
      }
    }
  }

  if (gHob->MemoryAbove4GB.ResourceLength == 0) {
    //
    // If there is no memory above 4GB then change the resource descriptor HOB
    //  into another type. I'm doing this as it's unclear if a resource
    //  descriptor HOB of length zero is valid. Spec does not say it's illegal,
    //  but code in EDK does not seem to handle this case.
    //
    gHob->MemoryAbove4GB.Header.HobType = EFI_HOB_TYPE_UNUSED;
  }

  return (VOID *)(UINTN)(gHob->MemoryAbove1MB.PhysicalStart + gHob->MemoryAbove1MB.ResourceLength);
}

VOID *
PrepareHobStack (
  IN VOID *StackTop
  )
{
  gHob->Stack.AllocDescriptor.MemoryLength      = EFI_MEMORY_STACK_PAGE_NUM * EFI_PAGE_SIZE;
  gHob->Stack.AllocDescriptor.MemoryBaseAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)StackTop - gHob->Stack.AllocDescriptor.MemoryLength;

  return (VOID *)(UINTN)gHob->Stack.AllocDescriptor.MemoryBaseAddress;
}

VOID *
PrepareHobMemoryDescriptor (
  VOID                          *MemoryDescriptorTop,
  UINTN                         MemDescCount,
  EFI_MEMORY_DESCRIPTOR         *MemDesc
  )
{
  gHob->MemoryDescriptor.MemDescCount = MemDescCount;
  gHob->MemoryDescriptor.MemDesc      = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryDescriptorTop - MemDescCount * sizeof(EFI_MEMORY_DESCRIPTOR));
  //
  // Make MemoryDescriptor.MemDesc page aligned
  //
  gHob->MemoryDescriptor.MemDesc      = (EFI_MEMORY_DESCRIPTOR *)((UINTN) gHob->MemoryDescriptor.MemDesc & ~EFI_PAGE_MASK);

  CopyMem (gHob->MemoryDescriptor.MemDesc, MemDesc, MemDescCount * sizeof(EFI_MEMORY_DESCRIPTOR));

  return gHob->MemoryDescriptor.MemDesc;
}

VOID
PrepareHobBfv (
  VOID  *Bfv,
  UINTN BfvLength
  )
{
  //UINTN BfvLengthPageSize;

  //
  // Calculate BFV location at top of the memory region.
  // This is like a RAM Disk. Align to page boundry.
  //
  //BfvLengthPageSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (BfvLength));
 
  gHob->Bfv.BaseAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Bfv;
  gHob->Bfv.Length = BfvLength;

  //
  // Resource descriptor for the FV
  //
  gHob->BfvResource.PhysicalStart = gHob->Bfv.BaseAddress;
  gHob->BfvResource.ResourceLength = gHob->Bfv.Length;
}

VOID
PrepareHobDxeCore (
  VOID                  *DxeCoreEntryPoint,
  EFI_PHYSICAL_ADDRESS  DxeCoreImageBase,
  UINT64                DxeCoreLength
  )
{
  gHob->DxeCore.MemoryAllocationHeader.MemoryBaseAddress = DxeCoreImageBase;
  gHob->DxeCore.MemoryAllocationHeader.MemoryLength = DxeCoreLength;
  gHob->DxeCore.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)DxeCoreEntryPoint;


  gHob->MemoryDxeCore.PhysicalStart   = DxeCoreImageBase;
  gHob->MemoryDxeCore.ResourceLength  = DxeCoreLength;  
}

VOID *
PrepareHobNvStorage (
  VOID *NvStorageTop
  )
/*
  Initialize Block-Aligned Firmware Block.

  Variable:
           +-------------------+
           |     FV_Header     |
           +-------------------+
           |                   |
           |VAR_STORAGE(0x4000)|
           |                   |
           +-------------------+
  FTW:
           +-------------------+
           |     FV_Header     |
           +-------------------+
           |                   |
           |   Working(0x2000) |
           |                   |
           +-------------------+
           |                   |
           |   Spare(0x10000)  |
           |                   |
           +-------------------+
*/
{
  STATIC VARIABLE_STORE_HEADER VarStoreHeader = {
    VARIABLE_STORE_SIGNATURE,
    0xffffffff,                 // will be fixed in Variable driver
    VARIABLE_STORE_FORMATTED,
    VARIABLE_STORE_HEALTHY,
    0,
    0
  };
  
  STATIC EFI_FIRMWARE_VOLUME_HEADER NvStorageFvbHeader = {
    {
      0,
    },  // ZeroVector[16]
    EFI_SYSTEM_NV_DATA_FV_GUID,
    NV_STORAGE_FVB_SIZE,
    EFI_FVH_SIGNATURE,
    EFI_FVB_READ_ENABLED_CAP |
      EFI_FVB_READ_STATUS |
      EFI_FVB_WRITE_ENABLED_CAP |
      EFI_FVB_WRITE_STATUS |
      EFI_FVB_ERASE_POLARITY,
    EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH,
    0,  // CheckSum
    0,  // ExtHeaderOffset
    {
      0,
    },  // Reserved[1]
    1,  // Revision
    {
      {
        NV_STORAGE_FVB_BLOCK_NUM,
        FV_BLOCK_SIZE,
      }
    }
  };

  STATIC EFI_FV_BLOCK_MAP_ENTRY BlockMapEntryEnd = {0, 0};

  EFI_PHYSICAL_ADDRESS StorageFvbBase;
  EFI_PHYSICAL_ADDRESS FtwFvbBase;

  UINT16               *Ptr;
  UINT16               Checksum;


  //
  // Use first 16-byte Reset Vector of FVB to store extra information
  //   UINT32 Offset 0 stores the volume signature
  //   UINT8  Offset 4 : should init the Variable Store Header if non-zero
  //
  gHob->NvStorageFvb.FvbInfo.VolumeId   = *(UINT32 *) (UINTN) (NV_STORAGE_STATE);
  gHob->NvStorage.   FvbInfo.VolumeId   = *(UINT32 *) (UINTN) (NV_STORAGE_STATE);

  //
  // *(NV_STORAGE_STATE + 4):
  //   2 - Size error
  //   1 - File not exist
  //   0 - File exist with correct size
  //
  if (*(UINT8 *) (UINTN) (NV_STORAGE_STATE + 4) == 2) {
    ClearScreen ();
    PrintString ("Error: Size of Efivar.bin should be 16k!\n");
    CpuDeadLoop();
  }
  
  if (*(UINT8 *) (UINTN) (NV_STORAGE_STATE + 4) != 0) {
    //
    // Efivar.bin doesn't exist
    //  1. Init variable storage header to valid header
    //
    CopyMem (
      (VOID *) (UINTN) NV_STORAGE_START,
      &VarStoreHeader,
      sizeof (VARIABLE_STORE_HEADER)
    );
    //
    //  2. set all bits in variable storage body to 1
    //
    SetMem (
      (VOID *) (UINTN) (NV_STORAGE_START + sizeof (VARIABLE_STORE_HEADER)),
      NV_STORAGE_SIZE - sizeof (VARIABLE_STORE_HEADER),
      0xff
    );
  }

  //
  // Relocate variable storage
  // 
  //  1. Init FVB Header to valid header: First 0x48 bytes
  //     In real platform, these fields are fixed by tools
  //
  //
  Checksum = 0;
  for (
    Ptr = (UINT16 *) &NvStorageFvbHeader; 
    Ptr < (UINT16 *) ((UINTN) (UINT8 *) &NvStorageFvbHeader + sizeof (EFI_FIRMWARE_VOLUME_HEADER));
    ++Ptr
    ) {
    Checksum = (UINT16) (Checksum + (*Ptr));
  }
  NvStorageFvbHeader.Checksum = (UINT16) (0x10000 - Checksum);
  StorageFvbBase = (EFI_PHYSICAL_ADDRESS)(((UINTN)NvStorageTop - NV_STORAGE_FVB_SIZE - NV_FTW_FVB_SIZE) & ~EFI_PAGE_MASK);
  CopyMem ((VOID *) (UINTN) StorageFvbBase, &NvStorageFvbHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
  CopyMem (
    (VOID *) (UINTN) (StorageFvbBase + sizeof (EFI_FIRMWARE_VOLUME_HEADER)),
    &BlockMapEntryEnd,
    sizeof (EFI_FV_BLOCK_MAP_ENTRY)
  );
  
  //
  //  2. Relocate variable data
  //
  CopyMem (
    (VOID *) (UINTN) (StorageFvbBase + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH),
    (VOID *) (UINTN) NV_STORAGE_START,
    NV_STORAGE_SIZE
  );

  //
  //  3. Set the remaining memory to 0xff
  //
  SetMem (
    (VOID *) (UINTN) (StorageFvbBase + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH + NV_STORAGE_SIZE),
    NV_STORAGE_FVB_SIZE - NV_STORAGE_SIZE - EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH,
    0xff
  );

  //
  // Create the FVB holding NV Storage in memory
  //
  gHob->NvStorageFvResource.PhysicalStart =
    gHob->NvStorageFvb.FvbInfo.Entries[0].Base = StorageFvbBase;
  //
  // Create the NV Storage Hob
  //
  gHob->NvStorage.FvbInfo.Entries[0].Base = StorageFvbBase + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH;

  //
  // Create the FVB holding FTW spaces
  //
  FtwFvbBase = (EFI_PHYSICAL_ADDRESS)((UINTN) StorageFvbBase + NV_STORAGE_FVB_SIZE);
  gHob->NvFtwFvResource.PhysicalStart =
    gHob->NvFtwFvb.FvbInfo.Entries[0].Base = FtwFvbBase;
  //
  // Put FTW Working in front
  //
  gHob->NvFtwWorking.FvbInfo.Entries[0].Base = FtwFvbBase + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH;

  //
  // Put FTW Spare area after FTW Working area
  //
  gHob->NvFtwSpare.FvbInfo.Entries[0].Base = 
    (EFI_PHYSICAL_ADDRESS)((UINTN) FtwFvbBase + EFI_RUNTIME_UPDATABLE_FV_HEADER_LENGTH + NV_FTW_WORKING_SIZE);
  
  return (VOID *)(UINTN)StorageFvbBase;
}

VOID
PrepareHobPhit (
  VOID *MemoryTop,
  VOID *FreeMemoryTop
  )
{
  gHob->Phit.EfiMemoryTop        = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryTop;
  gHob->Phit.EfiMemoryBottom     = gHob->Phit.EfiMemoryTop - CONSUMED_MEMORY;
  gHob->Phit.EfiFreeMemoryTop    = (EFI_PHYSICAL_ADDRESS)(UINTN)FreeMemoryTop;
  gHob->Phit.EfiFreeMemoryBottom = gHob->Phit.EfiMemoryBottom + sizeof(HOB_TEMPLATE);

  CopyMem ((VOID *)(UINTN)gHob->Phit.EfiMemoryBottom, gHob, sizeof(HOB_TEMPLATE));
  gHob = (HOB_TEMPLATE *)(UINTN)gHob->Phit.EfiMemoryBottom;

  gHob->Phit.EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)&gHob->EndOfHobList;
}

VOID
PrepareHobCpu (
  VOID
  )
{
  UINT32  CpuidEax;

  //
  // Create a CPU hand-off information
  //
  gHob->Cpu.SizeOfMemorySpace = 36;

  AsmCpuid (EFI_CPUID_EXTENDED_FUNCTION, &CpuidEax, NULL, NULL, NULL);
  if (CpuidEax >= CPUID_EXTENDED_ADD_SIZE) {
    AsmCpuid (CPUID_EXTENDED_ADD_SIZE, &CpuidEax, NULL, NULL, NULL);
    gHob->Cpu.SizeOfMemorySpace = (UINT8)(CpuidEax & 0xFF);
  }
}

VOID
CompleteHobGeneration (
  VOID
  )
{
  gHob->MemoryAllocation.AllocDescriptor.MemoryBaseAddress  = gHob->Phit.EfiFreeMemoryTop;
  //
  // Reserve all the memory under Stack above FreeMemoryTop as allocated
  //
  gHob->MemoryAllocation.AllocDescriptor.MemoryLength       = gHob->Stack.AllocDescriptor.MemoryBaseAddress - gHob->Phit.EfiFreeMemoryTop;

  //
  // adjust Above1MB ResourceLength
  //
  if (gHob->MemoryAbove1MB.PhysicalStart + gHob->MemoryAbove1MB.ResourceLength > gHob->Phit.EfiMemoryTop) {
    gHob->MemoryAbove1MB.ResourceLength = gHob->Phit.EfiMemoryTop - gHob->MemoryAbove1MB.PhysicalStart;
  }
}