/
Loader.c
2786 lines (2357 loc) · 124 KB
/
Loader.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
//==================================================================================================================================
// Simple UEFI Bootloader: Kernel Loader and Entry Point Jump
//==================================================================================================================================
//
// Version 2.3
//
// Author:
// KNNSpeed
//
// Source Code:
// https://github.com/KNNSpeed/Simple-UEFI-Bootloader
//
// This file contains the multiplatform kernel file loader and bootstrapper.
//
#include "Bootloader.h"
//==================================================================================================================================
// GoTime: Kernel Loader
//==================================================================================================================================
//
// Load Kernel (64-bit PE32+, ELF, or Mach-O), exit boot services, and jump to the entry point of kernel file
//
EFI_STATUS GoTime(EFI_HANDLE ImageHandle, GPU_CONFIG * Graphics, EFI_CONFIGURATION_TABLE * SysCfgTables, UINTN NumSysCfgTables, UINT32 UEFIVer)
{
#ifdef GOP_DEBUG_ENABLED
// Integrity check
for(UINT64 k = 0; k < Graphics->NumberOfFrameBuffers; k++)
{
Print(L"GPU Mode: %u of %u\r\n", Graphics->GPUArray[k].Mode, Graphics->GPUArray[k].MaxMode - 1);
Print(L"GPU FB: 0x%016llx\r\n", Graphics->GPUArray[k].FrameBufferBase);
Print(L"GPU FB Size: 0x%016llx\r\n", Graphics->GPUArray[k].FrameBufferSize);
Print(L"GPU SizeOfInfo: %u Bytes\r\n", Graphics->GPUArray[k].SizeOfInfo);
Print(L"GPU Info Ver: 0x%x\r\n", Graphics->GPUArray[k].Info->Version);
Print(L"GPU Info Res: %ux%u\r\n", Graphics->GPUArray[k].Info->HorizontalResolution, Graphics->GPUArray[k].Info->VerticalResolution);
Print(L"GPU Info PxFormat: 0x%x\r\n", Graphics->GPUArray[k].Info->PixelFormat);
Print(L"GPU Info PxInfo (R,G,B,Rsvd Masks): 0x%08x, 0x%08x, 0x%08x, 0x%08x\r\n", Graphics->GPUArray[k].Info->PixelInformation.RedMask, Graphics->GPUArray[k].Info->PixelInformation.GreenMask, Graphics->GPUArray[k].Info->PixelInformation.BlueMask, Graphics->GPUArray[k].Info->PixelInformation.ReservedMask);
Print(L"GPU Info PxPerScanLine: %u\r\n", Graphics->GPUArray[k].Info->PixelsPerScanLine);
Keywait(L"\0");
}
#endif
#ifdef LOADER_DEBUG_ENABLED
Print(L"GO GO GO!!!\r\n");
#endif
EFI_STATUS GoTimeStatus;
// These hold data for the loader params at the end
EFI_PHYSICAL_ADDRESS KernelBaseAddress = 0;
UINTN KernelPages = 0;
// Load kernel file from somewhere on this drive
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
// Get a pointer to the (loaded image) pointer of BOOTX64.EFI
// Pointer 1 -> Pointer 2 -> BOOTX64.EFI
// OpenProtocol wants Pointer 1 as input to give you Pointer 2.
GoTimeStatus = ST->BootServices->OpenProtocol(ImageHandle, &LoadedImageProtocol, (void**)&LoadedImage, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"LoadedImage OpenProtocol error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Need these for later
CHAR16 * ESPRootTemp = DevicePathToStr(DevicePathFromHandle(LoadedImage->DeviceHandle));
UINT64 ESPRootSize = StrSize(ESPRootTemp);
// DevicePathToStr allocates memory of type Loadedimage->ImageDataType (this is set by firmware)
// Instead we want a known data type, so reallocate it:
CHAR16 * ESPRoot;
GoTimeStatus = ST->BootServices->AllocatePool(EfiLoaderData, ESPRootSize, (void**)&ESPRoot);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"ESPRoot AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
CopyMem(ESPRoot, ESPRootTemp, ESPRootSize);
GoTimeStatus = BS->FreePool(ESPRootTemp);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Error freeing ESPRootTemp pool. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
// Parent device of BOOTX64.EFI (the ImageHandle originally passed in is this very file)
// Loadedimage is an EFI_LOADED_IMAGE_PROTOCOL pointer that points to BOOTX64.EFI
GoTimeStatus = ST->BootServices->OpenProtocol(LoadedImage->DeviceHandle, &FileSystemProtocol, (void**)&FileSystem, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"FileSystem OpenProtocol error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
EFI_FILE *CurrentDriveRoot;
GoTimeStatus = FileSystem->OpenVolume(FileSystem, &CurrentDriveRoot);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"OpenVolume error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
///
// Below Kernel64.txt loading & parsing code adapted from V2.1 of https://github.com/KNNSpeed/UEFI-Stub-Loader
// Locate Kernel64.txt, which should be in the same directory as this program
// ((FILEPATH_DEVICE_PATH*)LoadedImage->FilePath)->PathName is, e.g., \EFI\BOOT\BOOTX64.EFI
CHAR16 * BootFilePath = ((FILEPATH_DEVICE_PATH*)LoadedImage->FilePath)->PathName;
#ifdef LOADER_DEBUG_ENABLED
Print(L"BootFilePath: %s\r\n", BootFilePath);
#endif
UINTN TxtFilePathPrefixLength = 0;
UINTN BootFilePathLength = 0;
while(BootFilePath[BootFilePathLength] != L'\0')
{
if(BootFilePath[BootFilePathLength] == L'\\')
{
TxtFilePathPrefixLength = BootFilePathLength; // Could use ++BootFilePathLength here instead of the two separate += below, but it's less clear and it doesn't make any meaningful difference to do so.
}
BootFilePathLength++;
}
BootFilePathLength += 1; // For Null Term
TxtFilePathPrefixLength += 1; // To account for the last '\' in the file path (file path prefix does not get null-terminated)
#ifdef LOADER_DEBUG_ENABLED
Print(L"BootFilePathLength: %llu, TxtFilePathPrefixLength: %llu, BootFilePath Size: %llu \r\n", BootFilePathLength, TxtFilePathPrefixLength, StrSize(BootFilePath));
Keywait(L"\0");
#endif
CONST CHAR16 TxtFileName[13] = L"Kernel64.txt";
UINTN TxtFilePathPrefixSize = TxtFilePathPrefixLength * sizeof(CHAR16);
UINTN TxtFilePathSize = TxtFilePathPrefixSize + sizeof(TxtFileName);
CHAR16 * TxtFilePath;
GoTimeStatus = ST->BootServices->AllocatePool(EfiBootServicesData, TxtFilePathSize, (void**)&TxtFilePath);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"TxtFilePathPrefix AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Don't really need this. Data is measured to be the right size, meaning every byte in TxtFilePath gets overwritten.
// ZeroMem(TxtFilePath, TxtFilePathSize);
CopyMem(TxtFilePath, BootFilePath, TxtFilePathPrefixSize);
CopyMem(&TxtFilePath[TxtFilePathPrefixLength], TxtFileName, sizeof(TxtFileName));
#ifdef LOADER_DEBUG_ENABLED
Print(L"TxtFilePath: %s, TxtFilePath Size: %llu\r\n", TxtFilePath, TxtFilePathSize);
Keywait(L"\0");
#endif
// Get ready to open the Kernel64.txt file
EFI_FILE *KernelcmdFile;
// Open the Kernel64.txt file and assign it to the KernelcmdFile EFI_FILE variable
// It turns out the Open command can support directory trees with "\" like in Windows. Neat!
GoTimeStatus = CurrentDriveRoot->Open(CurrentDriveRoot, &KernelcmdFile, TxtFilePath, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
if (EFI_ERROR(GoTimeStatus))
{
Keywait(L"Kernel64.txt file is missing\r\n");
return GoTimeStatus;
}
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"Kernel64.txt file opened.\r\n");
#endif
// Now to get Kernel64.txt's file size
UINTN Txt_FileInfoSize;
// Need to know the size of the file metadata to get the file metadata...
GoTimeStatus = KernelcmdFile->GetInfo(KernelcmdFile, &gEfiFileInfoGuid, &Txt_FileInfoSize, NULL);
// GetInfo will intentionally error out and provide the correct Txt_FileInfoSize value
#ifdef LOADER_DEBUG_ENABLED
Print(L"Txt_FileInfoSize: %llu Bytes\r\n", Txt_FileInfoSize);
#endif
// Prep metadata destination
EFI_FILE_INFO *Txt_FileInfo;
// Reserve memory for file info/attributes and such to prevent it from getting run over
GoTimeStatus = ST->BootServices->AllocatePool(EfiBootServicesData, Txt_FileInfoSize, (void**)&Txt_FileInfo);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Txt_FileInfo AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Actually get the metadata
GoTimeStatus = KernelcmdFile->GetInfo(KernelcmdFile, &gEfiFileInfoGuid, &Txt_FileInfoSize, Txt_FileInfo);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"GetInfo error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef SHOW_KERNEL_METADATA
// Show metadata
Print(L"FileName: %s\r\n", Txt_FileInfo->FileName);
Print(L"Size: %llu\r\n", Txt_FileInfo->Size);
Print(L"FileSize: %llu\r\n", Txt_FileInfo->FileSize);
Print(L"PhysicalSize: %llu\r\n", Txt_FileInfo->PhysicalSize);
Print(L"Attribute: %llx\r\n", Txt_FileInfo->Attribute);
/*
NOTE: Attributes:
#define EFI_FILE_READ_ONLY 0x0000000000000001
#define EFI_FILE_HIDDEN 0x0000000000000002
#define EFI_FILE_SYSTEM 0x0000000000000004
#define EFI_FILE_RESERVED 0x0000000000000008
#define EFI_FILE_DIRECTORY 0x0000000000000010
#define EFI_FILE_ARCHIVE 0x0000000000000020
#define EFI_FILE_VALID_ATTR 0x0000000000000037
*/
Print(L"Created: %02hhu/%02hhu/%04hu - %02hhu:%02hhu:%02hhu.%u\r\n", Txt_FileInfo->CreateTime.Month, Txt_FileInfo->CreateTime.Day, Txt_FileInfo->CreateTime.Year, Txt_FileInfo->CreateTime.Hour, Txt_FileInfo->CreateTime.Minute, Txt_FileInfo->CreateTime.Second, Txt_FileInfo->CreateTime.Nanosecond);
Print(L"Last Modified: %02hhu/%02hhu/%04hu - %02hhu:%02hhu:%02hhu.%u\r\n", Txt_FileInfo->ModificationTime.Month, Txt_FileInfo->ModificationTime.Day, Txt_FileInfo->ModificationTime.Year, Txt_FileInfo->ModificationTime.Hour, Txt_FileInfo->ModificationTime.Minute, Txt_FileInfo->ModificationTime.Second, Txt_FileInfo->ModificationTime.Nanosecond);
Keywait(L"\0");
#endif
// Read text file into memory now that we know the file size
CHAR16 * KernelcmdArray;
// Reserve memory for text file
GoTimeStatus = ST->BootServices->AllocatePool(EfiBootServicesData, Txt_FileInfo->FileSize, (void**)&KernelcmdArray);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"KernelcmdArray AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Actually read the file
GoTimeStatus = KernelcmdFile->Read(KernelcmdFile, &Txt_FileInfo->FileSize, KernelcmdArray);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"KernelcmdArray read error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"KernelcmdFile read into memory.\r\n");
#endif
// UTF-16 format check
UINT16 BOM_check = UTF16_BOM_LE;
if(!compare(KernelcmdArray, &BOM_check, 2))
{
BOM_check = UTF16_BOM_BE; // Check endianness
if(compare(KernelcmdArray, &BOM_check, 2))
{
Print(L"Error: Kernel64.txt has the wrong endianness for this system.\r\n");
}
else // Probably missing a BOM
{
Print(L"Error: Kernel64.txt not formatted as UTF-16/UCS-2 with BOM.\r\n\n");
Print(L"Q: What is a BOM?\r\n\n");
Print(L"A: The BOM (Byte Order Mark) is a 2-byte identification sequence\r\n");
Print(L"(U+FEFF) at the start of a UTF16/UCS-2-encoded file.\r\n");
Print(L"Unfortunately not all editors add it in, and without\r\n");
Print(L"a BOM present programs like this one cannot easily tell that a\r\n");
Print(L"text file is encoded in UTF16/UCS-2.\r\n\n");
Print(L"Windows Notepad & Wordpad and Linux gedit & xed all add BOMs when\r\n");
Print(L"saving files as .txt with encoding set to \"Unicode\" (Windows)\r\n");
Print(L"or \"UTF16\" (Linux), so use one of them to make Kernel64.txt.\r\n\n");
}
Keywait(L"Please fix the file and try again.\r\n");
return GoTimeStatus;
}
// Parse Kernel64.txt file for location of kernel image and command line
// Kernel image location line will be of format e.g. \EFI\ubuntu\vmlinuz.efi followed by \n or \r\n
// Command line will just go until the next \n or \r\n, and should just be loaded as a UTF-16 string
// Get size of kernel image path & command line and populate the data retention variables
UINT64 FirstLineLength = 0;
UINT64 KernelPathSize = 0;
for(UINT64 i = 1; i < ((Txt_FileInfo->FileSize) >> 1); i++) // i starts at 1 to skip the BOM, ((Txt_FileInfo->FileSize) >> 1) is the number of 2-byte units in the file
{
if(KernelcmdArray[i] == L'\n')
{
// Account for the L'\n'
FirstLineLength = i + 1;
// The extra +1 is to start the command line parse in the correct place
break;
}
else if(KernelcmdArray[i] == L'\r')
{
// There'll be a \n after the \r
FirstLineLength = i + 1 + 1;
// The extra +1 is to start the command line parse in the correct place
break;
}
if(KernelcmdArray[i] != L' ') // There might be an errant space or two. Ignore them.
{
KernelPathSize++;
}
}
UINT64 KernelPathLen = KernelPathSize; // Need this for later
// Need to add null terminator. Multiply by size of CHAR16 (2 bytes) to get size.
KernelPathSize = (KernelPathSize + 1) << 1; // (KernelPathSize + 1) * sizeof(CHAR16)
#ifdef LOADER_DEBUG_ENABLED
Print(L"KernelPathSize: %llu\r\n", KernelPathSize);
#endif
// Command line's turn
UINT64 CmdlineSize = 0; // Interestingly, the Linux kernel only takes 256 to 4096 chars for load options depending on architecture. Here's 2^63 UTF-16 characters (-1 to account for null terminator).
for(UINT64 j = FirstLineLength; j < ((Txt_FileInfo->FileSize) >> 1); j++)
{
if((KernelcmdArray[j] == L'\n') || (KernelcmdArray[j] == L'\r')) // Reached the end of the line
{
break;
}
CmdlineSize++;
}
UINT64 CmdlineLen = CmdlineSize; // Need this for later
// Need to add null terminator. Multiply by size of CHAR16 (2 bytes) to get size.
CmdlineSize = (CmdlineSize + 1) << 1; // (CmdlineSize + 1) * sizeof(CHAR16)
#ifdef LOADER_DEBUG_ENABLED
Print(L"CmdlineSize: %llu\r\n", CmdlineSize);
#endif
CHAR16 * KernelPath; // EFI Kernel file's Path
GoTimeStatus = ST->BootServices->AllocatePool(EfiLoaderData, KernelPathSize, (void**)&KernelPath);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"KernelPath AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
CHAR16 * Cmdline; // Command line to pass to EFI kernel
GoTimeStatus = ST->BootServices->AllocatePool(EfiLoaderData, CmdlineSize, (void**)&Cmdline);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Cmdline AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
for(UINT64 i = 1; i < FirstLineLength; i++)
{
if((KernelcmdArray[i] == L'\n') || (KernelcmdArray[i] == L'\r'))
{
break;
}
if(KernelcmdArray[i] != L' ') // There might be an errant space or two. Ignore them.
{
KernelPath[i-1] = KernelcmdArray[i]; // i-1 to ignore the 2 bytes of UTF-16 BOM
}
}
KernelPath[KernelPathLen] = L'\0'; // Need to null-terminate this string
// Command line's turn
for(UINT64 j = FirstLineLength; j < ((Txt_FileInfo->FileSize) >> 1); j++)
{
if((KernelcmdArray[j] == L'\n') || (KernelcmdArray[j] == L'\r')) // Reached the end of the line
{
break;
}
Cmdline[j-FirstLineLength] = KernelcmdArray[j];
}
Cmdline[CmdlineLen] = L'\0'; // Need to null-terminate this string
#ifdef LOADER_DEBUG_ENABLED
Print(L"Kernel image path: %s\r\nKernel image path size: %u\r\n", KernelPath, KernelPathSize);
Print(L"Kernel command line: %s\r\nKernel command line size: %u\r\n", Cmdline, CmdlineSize);
Keywait(L"Loading image... (might take a second or two after pressing a key)\r\n");
#endif
// Free pools allocated from before as they are no longer needed
GoTimeStatus = BS->FreePool(TxtFilePath);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Error freeing TxtFilePathPrefix pool. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
GoTimeStatus = BS->FreePool(KernelcmdArray);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Error freeing KernelcmdArray pool. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
GoTimeStatus = BS->FreePool(Txt_FileInfo);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Error freeing Txt_FileInfo pool. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
///
EFI_FILE *KernelFile;
// Open the kernel file from current drive root and point to it with KernelFile
GoTimeStatus = CurrentDriveRoot->Open(CurrentDriveRoot, &KernelFile, KernelPath, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
if (EFI_ERROR(GoTimeStatus))
{
Print(L"%s file is missing\r\n", KernelPath);
return GoTimeStatus;
}
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"Kernel file opened.\r\n");
#endif
// Get address of start of file
// ...Don't need to do this
// UINT64 FileStartPosition;
// KernelFile->GetPosition(KernelFile, &FileStartPosition);
// Keywait(L"Kernel file start position acquired.\r\n");
// Default ImageBase for 64-bit PE DLLs
EFI_PHYSICAL_ADDRESS Header_memory = 0x400000;
UINTN FileInfoSize;
GoTimeStatus = KernelFile->GetInfo(KernelFile, &gEfiFileInfoGuid, &FileInfoSize, NULL);
// GetInfo will intentionally error out and provide the correct fileinfosize value
#ifdef LOADER_DEBUG_ENABLED
Print(L"FileInfoSize: %llu Bytes\r\n", FileInfoSize);
#endif
EFI_FILE_INFO *FileInfo;
GoTimeStatus = ST->BootServices->AllocatePool(EfiLoaderData, FileInfoSize, (void**)&FileInfo); // Reserve memory for file info/attributes and such, to prevent it from getting run over
if(EFI_ERROR(GoTimeStatus))
{
Print(L"FileInfo AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Actually get the metadata
GoTimeStatus = KernelFile->GetInfo(KernelFile, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"GetInfo error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef SHOW_KERNEL_METADATA
// Show metadata
Print(L"FileName: %s\r\n", FileInfo->FileName);
Print(L"Size: %llu\r\n", FileInfo->Size);
Print(L"FileSize: %llu\r\n", FileInfo->FileSize);
Print(L"PhysicalSize: %llu\r\n", FileInfo->PhysicalSize);
Print(L"Attribute: %llx\r\n", FileInfo->Attribute);
/*
NOTE: Attributes:
#define EFI_FILE_READ_ONLY 0x0000000000000001
#define EFI_FILE_HIDDEN 0x0000000000000002
#define EFI_FILE_SYSTEM 0x0000000000000004
#define EFI_FILE_RESERVED 0x0000000000000008
#define EFI_FILE_DIRECTORY 0x0000000000000010
#define EFI_FILE_ARCHIVE 0x0000000000000020
#define EFI_FILE_VALID_ATTR 0x0000000000000037
*/
Print(L"Created: %02hhu/%02hhu/%04hu - %02hhu:%02hhu:%02hhu.%u\r\n", FileInfo->CreateTime.Month, FileInfo->CreateTime.Day, FileInfo->CreateTime.Year, FileInfo->CreateTime.Hour, FileInfo->CreateTime.Minute, FileInfo->CreateTime.Second, FileInfo->CreateTime.Nanosecond);
Print(L"Last Modified: %02hhu/%02hhu/%04hu - %02hhu:%02hhu:%02hhu.%u\r\n", FileInfo->ModificationTime.Month, FileInfo->ModificationTime.Day, FileInfo->ModificationTime.Year, FileInfo->ModificationTime.Hour, FileInfo->ModificationTime.Minute, FileInfo->ModificationTime.Second, FileInfo->ModificationTime.Nanosecond);
#endif
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"GetInfo memory allocated and populated.\r\n");
#endif
// Read file header
// UINTN size = 0x40; // Size of DOS header
UINTN size = sizeof(IMAGE_DOS_HEADER);
IMAGE_DOS_HEADER DOSheader;
GoTimeStatus = KernelFile->Read(KernelFile, &size, &DOSheader);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"DOSheader read error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"DOS Header read from file.\r\n");
#endif
// For the entry point jump, we need to know if the file uses ms_abi (is a PE image) or sysv_abi (*NIX image) calling convention
UINT8 KernelisPE = 0;
// Check header
if(DOSheader.e_magic == IMAGE_DOS_SIGNATURE) // MZ
{
//----------------------------------------------------------------------------------------------------------------------------------
// 64-Bit PE32+ Loader
//----------------------------------------------------------------------------------------------------------------------------------
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"DOS header passed.\r\n");
Print(L"e_lfanew: 0x%x\r\n", DOSheader.e_lfanew);
// Print(L"FileStart: 0x%llx\r\n", &FileStartPosition);
// Print(L"Corrected filestart: 0x%llx\r\n", &FileStartPosition + (UINT64)DOSheader.e_lfanew);
#endif
// Get to the PE section of the header (Use the pointer at offset 0x3C, which contains the offset of the PE header relative to the start of the file)
GoTimeStatus = KernelFile->SetPosition(KernelFile, (UINT64)DOSheader.e_lfanew); // Go to PE Header
if(EFI_ERROR(GoTimeStatus))
{
Print(L"SetPosition error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// size = 4 + IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_NT_OPTIONAL64_HEADER; // 264 bytes
size = sizeof(IMAGE_NT_HEADERS64);
IMAGE_NT_HEADERS64 PEHeader;
GoTimeStatus = KernelFile->Read(KernelFile, &size, &PEHeader);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"PE header read error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef LOADER_DEBUG_ENABLED
Print(L"PE Header Signature: 0x%x\r\n", PEHeader.Signature);
#endif
//NOTE: SetPosition is RELATIVE TO THE FILE. Phoenix wiki is unclear:
// the position is not "absolute" (implying absolute memory location with
// respect to system memory address 0x0), it's "absolute relative to the
// file start"
if(PEHeader.Signature == IMAGE_NT_SIGNATURE)
{
// PE
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"PE header passed.\r\n");
#endif
if(PEHeader.FileHeader.Machine == IMAGE_FILE_MACHINE_X64 && PEHeader.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) // PE Headers have Signature, FileHeader, and OptionalHeader
{
// PE32+
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"PE32+ header passed.\r\n");
#endif
if (PEHeader.OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_EFI_APPLICATION) // Was it compiled with -Wl,--subsystem,10 (MINGW-W64 GCC)?
{
GoTimeStatus = EFI_INVALID_PARAMETER;
Print(L"Not a UEFI PE32+ application...\r\n");
Print(L"Subsystem: %hu\r\n", PEHeader.OptionalHeader.Subsystem); // If it's 3, it was compiled as a Windows CUI (command line) program, and instead needs to be linked with the above GCC flag.
return GoTimeStatus;
}
// It's a PE image
KernelisPE = 1;
#ifdef LOADER_DEBUG_ENABLED
Keywait(L"UEFI PE32+ header passed.\r\n");
#endif
UINT64 i; // Iterator
UINT64 virt_size = 0; // Size of all the data sections combined, which we need to know in order to allocate the right number of pages
UINT64 Numofsections = (UINT64)PEHeader.FileHeader.NumberOfSections; // Number of sections described at end of PE headers
size = IMAGE_SIZEOF_SECTION_HEADER*Numofsections; // Size of section header table in file
// IMAGE_SECTION_HEADER *section_headers_table_pointer = section_headers_table; // Pointer to the first section header is the same as a pointer to the table
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"Numofsections: %llu, size: %llu\r\n", Numofsections, size);
// Print(L"section_headers_table_pointer: 0x%llx\r\n§ion_headers_table[0]: 0x%llx\r\n", section_headers_table_pointer, §ion_headers_table[0]);
Keywait(L"\0");
#endif
IMAGE_SECTION_HEADER * section_headers_table; // This table is an array of section headers
GoTimeStatus = ST->BootServices->AllocatePool(EfiBootServicesData, size, (void**)§ion_headers_table);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Section headers table AllocatePool error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Cursor is already at the end of the PE Header
GoTimeStatus = KernelFile->Read(KernelFile, &size, §ion_headers_table[0]); // Run right over the section table, it should be exactly the size to hold this data
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Section headers table read error. 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Section headers table comes right after the PE Optional Header
for(i = 0; i < Numofsections; i++) // Go through each section of the "sections" section to get the address boundary of the last section
{
IMAGE_SECTION_HEADER *specific_section_header = §ion_headers_table[i];
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"current section address: 0x%x, size: 0x%x\r\n", specific_section_header->VirtualAddress, specific_section_header->Misc.VirtualSize);
Print(L"current section address + size 0x%x\r\n", specific_section_header->VirtualAddress + specific_section_header->Misc.VirtualSize);
#endif
virt_size = (virt_size > (UINT64)(specific_section_header->VirtualAddress + specific_section_header->Misc.VirtualSize) ? virt_size: (UINT64)(specific_section_header->VirtualAddress + specific_section_header->Misc.VirtualSize));
}
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"virt_size: 0x%llx\r\n", virt_size);
Keywait(L"Section Headers table passed.\r\n");
#endif
UINTN Header_size = (UINTN)PEHeader.OptionalHeader.SizeOfHeaders;
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"Total image size: %llu Bytes\r\nHeaders total size: %llu Bytes\r\n", (UINT64)PEHeader.OptionalHeader.SizeOfImage, Header_size);
#endif
// NOTE: This implies the max file size for a PE executable is 4GB (SizeOfImage is a UINT32).
// In any event, this has to be loaded from FAT32. You can't have a file larger than 4GB (32-bit max) on FAT32 anyways.
// A 4GB bootloader, or even kernel, would be insane. You'd need to use a 64-bit linux ELF for those.
UINT64 pages = EFI_SIZE_TO_PAGES(virt_size); // To get number of pages (typically 4KB per), rounded up
KernelPages = pages;
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"pages: %llu\r\n", pages);
Print(L"Expected ImageBase: 0x%llx\r\n", PEHeader.OptionalHeader.ImageBase);
Keywait(L"\0");
#ifdef MEMMAP_PRINT_ENABLED
print_memmap();
Keywait(L"Done printing MemMap.\r\n");
#endif
#endif
EFI_PHYSICAL_ADDRESS AllocatedMemory = PEHeader.OptionalHeader.ImageBase;
// EFI_PHYSICAL_ADDRESS AllocatedMemory = 0x400000;
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"Address of AllocatedMemory: 0x%llx\r\n", &AllocatedMemory);
#endif
GoTimeStatus = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages, &AllocatedMemory);
// GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &AllocatedMemory);
// AllocatedMemory = 0xFFFFFFFF; // This appears to be what AllocateAnyPages does.
// GoTimeStatus = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, pages, &AllocatedMemory);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not allocate pages for PE32+ sections. Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
#ifdef PE_LOADER_DEBUG_ENABLED
Print(L"AllocatedMemory location: 0x%llx\r\n", AllocatedMemory);
#ifdef MEMMAP_PRINT_ENABLED
print_memmap();
Keywait(L"Done printing MemMap.\r\n");
#endif
Keywait(L"Zeroing\r\n");
#endif
// Zero the allocated pages
ZeroMem((VOID*)AllocatedMemory, (pages << EFI_PAGE_SHIFT));
#ifdef PE_LOADER_DEBUG_ENABLED
Keywait(L"MemZeroed\r\n");
#endif
#ifndef MEMORY_CHECK_DISABLED
// If that memory isn't actually free due to weird firmware behavior...
// Iterate through the entirety of what was just allocated and check to make sure it's all zeros
// Start buggy firmware workaround
if(VerifyZeroMem(pages << EFI_PAGE_SHIFT, AllocatedMemory))
{
// From UEFI Specification 2.7, Errata A (http://www.uefi.org/specifications):
// MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by
// UEFI OS loaders that are provided by operating system vendors.
#ifdef MEMORY_CHECK_INFO
Print(L"Non-zero memory location allocated. Verifying cause...\r\n");
#endif
// Compare what's there with the kernel file's first bytes; the system might have been reset and the non-zero
// memory is what remains of last time. This can be safely overwritten to avoid cluttering up system RAM.
// Sure hope there aren't any other page-aligned kernel images floating around in memory marked as free
UINT64 MemCheck = IMAGE_DOS_SIGNATURE; // Good thing we know what to expect!
if(compare((EFI_PHYSICAL_ADDRESS*)AllocatedMemory, &MemCheck, 2))
{
// Do nothing, we're fine
#ifdef MEMORY_CHECK_INFO
Print(L"System was reset. No issues.\r\n");
#endif
}
else // Not our remains, proceed with discovery of viable memory address
{
#ifdef MEMORY_CHECK_INFO
Print(L"Searching for actually free memory...\r\nPerhaps the firmware is buggy?\r\n");
#endif
// Free the pages (well, return them to the system as they were...)
GoTimeStatus = BS->FreePages(AllocatedMemory, pages);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not free pages for PE32+ sections. Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// NOTE: CANNOT create an array of all compatible free addresses because the array takes up memory. So does the memory map.
// This results in a paradox, so we need to scan the memory map every time we need to find a new address...
// It appears that AllocateAnyPages uses a "MaxAddress" approach. This will go bottom-up instead.
EFI_PHYSICAL_ADDRESS NewAddress = 0; // Start at zero
EFI_PHYSICAL_ADDRESS OldAllocatedMemory = AllocatedMemory;
GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &NewAddress); // Need to check 0x0
while(GoTimeStatus != EFI_SUCCESS)
{ // Keep checking free memory addresses until one works
if(GoTimeStatus == EFI_NOT_FOUND)
{
// 0's not a good address (not enough contiguous pages could be found), get another one
NewAddress = ActuallyFreeAddress(pages, NewAddress);
// Make sure the new address isn't the known bad one
if(NewAddress == OldAllocatedMemory)
{
// Get a new address if it is
NewAddress = ActuallyFreeAddress(pages, NewAddress);
}
else if(NewAddress >= 0x100000000) // Need to stay under 4GB for PE32+
{
NewAddress = ~0ULL;
}
}
else if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not get an address for PE32+ pages. Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
if(NewAddress == ~0ULL)
{
// If you get this, you had no memory free anywhere.
Print(L"No memory marked as EfiConventionalMemory...\r\n");
return GoTimeStatus;
}
// Allocate the new address
GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &NewAddress);
// This loop shouldn't run more than once, but in the event something is at 0x0 we need to
// leave the loop with an allocated address
}
// Got a new address that's been allocated--save it
AllocatedMemory = NewAddress;
// Verify it's empty
while((NewAddress != ~0ULL) && VerifyZeroMem(pages << EFI_PAGE_SHIFT, AllocatedMemory)) // Loop this in case the firmware is really screwed
{ // It's not empty :(
// Sure hope there aren't any other page-aligned kernel images floating around in memory marked as free
if(compare((EFI_PHYSICAL_ADDRESS*)AllocatedMemory, &MemCheck, 2))
{
// Do nothing, we're fine
#ifdef MEMORY_CHECK_INFO
Print(L"System appears to have been reset. No issues.\r\n");
#endif
break;
}
else
{ // Gotta keep looking for a good memory address
#ifdef MEMORY_DEBUG_ENABLED
Print(L"Still searching... 0x%llx\r\n", AllocatedMemory);
#endif
// It's not actually free...
GoTimeStatus = BS->FreePages(AllocatedMemory, pages);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not free pages for PE32+ sections (loop). Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// Allocate a new address
GoTimeStatus = EFI_NOT_FOUND;
while((GoTimeStatus != EFI_SUCCESS) && (NewAddress != ~0ULL))
{
if(GoTimeStatus == EFI_NOT_FOUND)
{
// Get an address (ideally, this should be very rare)
NewAddress = ActuallyFreeAddress(pages, NewAddress);
// Make sure the new address isn't the known bad one
if(NewAddress == OldAllocatedMemory)
{
// Get a new address if it is
NewAddress = ActuallyFreeAddress(pages, NewAddress);
}
else if(NewAddress >= 0x100000000) // Need to stay under 4GB
{
NewAddress = ~0ULL; // Get out of this loop, do a more thorough check
break;
}
// This loop will run until we get a good address (shouldn't be more than once, if ever)
}
else if(EFI_ERROR(GoTimeStatus))
{
// EFI_OUT_OF_RESOURCES means the firmware's just not gonna load anything.
Print(L"Could not get an address for PE32+ pages (loop). Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
// NOTE: The number of times the message "No more free addresses" pops up
// helps indicate which NewAddress assignment hit the end.
GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &NewAddress);
} // loop
// It's a new address
AllocatedMemory = NewAddress;
// Verify new address is empty (in loop), if not then free it and try again.
} // else
} // End VerifyZeroMem while loop
// Ran out of easy addresses, time for a more thorough check
// Hopefully no one ever gets here
if(AllocatedMemory == ~0ULL)
{ // NewAddress is also -1
#ifdef BY_PAGE_SEARCH_DISABLED // Set this to disable ByPage searching
Print(L"No easy addresses found with enough space and containing only zeros.\r\nConsider enabling page-by-page search.\r\n");
return GoTimeStatus;
#endif
#ifndef BY_PAGE_SEARCH_DISABLED
#ifdef MEMORY_CHECK_INFO
Print(L"Performing page-by-page search.\r\nThis might take a while...\r\n");
#endif
#ifdef MEMORY_DEBUG_ENABLED
Keywait(L"About to search page by page\r\n");
#endif
NewAddress = 0x80000000 - EFI_PAGE_SIZE; // Start over
// Allocate the page's address
GoTimeStatus = EFI_NOT_FOUND;
while(GoTimeStatus != EFI_SUCCESS)
{
if(GoTimeStatus == EFI_NOT_FOUND)
{
// Nope, get another one
NewAddress = ActuallyFreeAddressByPage(pages, NewAddress);
// Make sure the new address isn't the known bad one
if(NewAddress == OldAllocatedMemory)
{
// Get a new address if it is
NewAddress = ActuallyFreeAddressByPage(pages, NewAddress);
}
else if(NewAddress >= 0x100000000) // Need to stay under 4GB
{
NewAddress = ActuallyFreeAddress(pages, 0); // Start from the first suitable EfiConventionalMemory address.
// This is for BIOS vendors who blanketly set 0x80000000 in an EfiReservedMemoryType section.
}
}
else if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not get an address for PE32+ pages by page. Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
if(NewAddress == ~0ULL)
{
// If you somehow get this, you really had no memory free anywhere.
Print(L"Hmm... How did you get here?\r\n");
return GoTimeStatus;
}
GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &NewAddress);
}
AllocatedMemory = NewAddress;
while(VerifyZeroMem(pages << EFI_PAGE_SHIFT, AllocatedMemory))
{
// Sure hope there aren't any other page-aligned kernel images floating around in memory marked as free
if(compare((EFI_PHYSICAL_ADDRESS*)AllocatedMemory, &MemCheck, 2))
{
// Do nothing, we're fine
#ifdef MEMORY_CHECK_INFO
Print(L"System might have been reset. Hopefully no issues.\r\n");
#endif
break;
}
else
{
#ifdef MEMORY_DEBUG_ENABLED
Print(L"Still searching by page... 0x%llx\r\n", AllocatedMemory);
#endif
// It's not actually free...
GoTimeStatus = BS->FreePages(AllocatedMemory, pages);
if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not free pages for PE32+ sections by page (loop). Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
GoTimeStatus = EFI_NOT_FOUND;
while(GoTimeStatus != EFI_SUCCESS)
{
if(GoTimeStatus == EFI_NOT_FOUND)
{
// Nope, get another one
NewAddress = ActuallyFreeAddressByPage(pages, NewAddress);
// Make sure the new address isn't the known bad one
if(NewAddress == OldAllocatedMemory)
{
// Get a new address if it is
NewAddress = ActuallyFreeAddressByPage(pages, NewAddress);
}
else if(NewAddress >= 0x100000000) // Need to stay under 4GB
{
Print(L"Too much junk below 4GB. Complain to your motherboard vendor.\r\nTry using a 64-bit ELF or MACH-O kernel binary instead of PE32+.\r\n");
NewAddress = ActuallyFreeAddress(pages, 0); // Either the BIOS vendor didn't read the spec or you need to use RAM space above 4GB.
}
}
else if(EFI_ERROR(GoTimeStatus))
{
Print(L"Could not get an address for PE32+ pages by page (loop). Error code: 0x%llx\r\n", GoTimeStatus);
return GoTimeStatus;
}
if(NewAddress == ~0ULL)
{
// Well, darn. Something's up with the system memory. Maybe you have 4GB or less?
Print(L"Do you have 4GB or less of RAM? Looks like you need > 4GB for this.\r\nThat also means you'll need to use 64-bit ELF or MACH-O kernels.\r\n");
return GoTimeStatus;
}
GoTimeStatus = BS->AllocatePages(AllocateAddress, EfiLoaderData, pages, &NewAddress);
} // loop
AllocatedMemory = NewAddress;
} // else
} // end ByPage VerifyZeroMem loop
#endif
} // End "big guns"
// Got a good address!
#ifdef MEMORY_CHECK_INFO
Print(L"Found!\r\n");
#endif
} // End discovery of viable memory address (else)
// Can move on now
#ifdef MEMORY_CHECK_INFO
Print(L"New AllocatedMemory location: 0x%llx\r\n", AllocatedMemory);
#endif
} // End VerifyZeroMem buggy firmware workaround (outermost if)
else
{
#ifdef MEMORY_CHECK_INFO
Print(L"Allocated memory was zeroed OK\r\n");
#endif
}
#endif
#ifdef PE_LOADER_DEBUG_ENABLED
Keywait(L"Allocate Pages passed.\r\n");
#ifdef MEMMAP_PRINT_ENABLED
// Check the address given to AllocatedMemory as listed in the MemMap
print_memmap();
Keywait(L"Done printing MemMap.\r\n");
#endif
// Map headers
Print(L"\nLoading Headers:\r\n");
Print(L"Check:\r\nSectionAddress: 0x%llx\r\nData there: 0x%016llx%016llx (Should be 0)\r\n", AllocatedMemory, *(EFI_PHYSICAL_ADDRESS*)(AllocatedMemory + 8), *(EFI_PHYSICAL_ADDRESS*)AllocatedMemory); // Print the first 128 bits of data at that address to compare
Keywait(L"\0");
#endif