3
|
1 #include <efi.h>
|
|
2 #include <efilib.h>
|
|
3
|
|
4 #include <efi.h>
|
|
5 #include <efilib.h>
|
|
6
|
|
7 struct __attribute__((packed)) BootParam {
|
|
8 UINT64 kernel_entry;
|
|
9 UINT64 rsdp_addr;
|
|
10 // struct GraphicConfig graphic_config;
|
|
11 // struct gdt bootstrap_gdt[3];
|
|
12 // struct gdt_desc bootstrap_gdt_desc;
|
|
13 UINT64 kernel_addr;
|
|
14 };
|
|
15
|
|
16 struct MemoryMap {
|
|
17 UINTN BufferSize;
|
|
18 VOID *Buffer;
|
|
19 UINTN MapSize;
|
|
20 UINTN MapKey;
|
|
21 UINTN DescriptorSize;
|
|
22 UINT32 DescriptorVersion;
|
|
23 };
|
0
|
24
|
|
25
|
5
|
26 EFI_STATUS LoadFile(CHAR16 *FileName, EFI_PHYSICAL_ADDRESS *FileAddr, UINTN *FilePageSize) {
|
3
|
27 EFI_STATUS Status;
|
|
28 EFI_FILE_IO_INTERFACE *IOVolume;
|
|
29 EFI_FILE_HANDLE Root;
|
|
30 EFI_FILE_HANDLE File;
|
|
31 Print(L"LoadFile\n");
|
|
32
|
5
|
33 // Locate all handles that support the Simple File System Protocol.
|
|
34 UINTN HandleCount = 5;
|
|
35 void **HandleBuffer;
|
|
36 Status = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &HandleCount, &HandleBuffer);
|
3
|
37 if (EFI_ERROR(Status)) {
|
5
|
38 Print(L"Failed to locate handles for file system protocol: %r\n", Status);
|
3
|
39 return Status;
|
|
40 }
|
0
|
41
|
5
|
42 for (UINTN Index = 0; Index < HandleCount; Index++) {
|
|
43 // Try to open the file system
|
|
44 Status = uefi_call_wrapper(BS->HandleProtocol, 3, HandleBuffer[Index], &FileSystemProtocol, (void **)&IOVolume);
|
|
45
|
|
46 if (!EFI_ERROR(Status)) {
|
|
47 // Open the root directory
|
|
48 Status = uefi_call_wrapper(IOVolume->OpenVolume, 2, IOVolume, &Root);
|
|
49 if (EFI_ERROR(Status)) {
|
|
50 Print(L"Failed to open root directory: %r\n", Status);
|
|
51 continue;
|
|
52 }
|
|
53
|
|
54 // Now open a file
|
|
55 Status = uefi_call_wrapper(Root->Open, 5, Root, &File,FileName , EFI_FILE_MODE_READ, 0);
|
|
56 if (EFI_ERROR(Status)) {
|
6
|
57 // Print(L"Failed to open file: %r\n", Status);
|
5
|
58 uefi_call_wrapper(Root->Close, 1, Root);
|
|
59 continue;
|
|
60 }
|
|
61
|
|
62 EFI_FILE_INFO *FileInfo;
|
|
63 UINTN FileInfoSize;
|
|
64 FileInfoSize = sizeof(EFI_FILE_INFO) + 1024;
|
|
65 FileInfo = AllocatePool(FileInfoSize);
|
|
66 Status = uefi_call_wrapper(File->GetInfo, 4, File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
|
|
67 if (EFI_ERROR(Status)) {
|
|
68 Print(L"Failed to get file info.\n");
|
|
69 FreePool(FileInfo);
|
|
70 return Status;
|
|
71 }
|
|
72
|
|
73 UINTN FileSize = FileInfo->FileSize;
|
|
74 Print(L"FileSize = %d\n", FileSize);
|
|
75 *FilePageSize = (FileSize + 4095) / 4096;
|
|
76 *FileAddr = 0;
|
|
77 Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, *FilePageSize, FileAddr);
|
|
78 if (EFI_ERROR(Status)) {
|
|
79 Print(L"Failed to allocate pages.\n");
|
|
80 FreePool(FileInfo);
|
|
81 return Status;
|
|
82 }
|
|
83
|
|
84 Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
|
|
85 if (EFI_ERROR(Status)) {
|
|
86 Print(L"Failed to load file.\n");
|
|
87 FreePool(FileInfo);
|
|
88 return Status;
|
|
89 }
|
|
90 Print(L"Successfully loaded file: %s\n", FileName);
|
|
91
|
|
92 FreePool(FileInfo);
|
|
93 // Close the file and root directory
|
|
94 uefi_call_wrapper(File->Close, 1, File);
|
|
95 uefi_call_wrapper(Root->Close, 1, Root);
|
|
96 }
|
3
|
97 }
|
|
98
|
|
99
|
|
100 return Status;
|
|
101 }
|
|
102
|
|
103 EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
|
|
104
|
|
105 EFI_STATUS Status;
|
|
106 CHAR16 *KernelFileName = L"\\kernel";
|
|
107 EFI_PHYSICAL_ADDRESS KernelBaseAddr;
|
|
108
|
|
109 InitializeLib(ImageHandle, SystemTable);
|
|
110 Print(L"Initializeing\n");
|
|
111 UINTN BufferPageSize;
|
5
|
112
|
3
|
113 Print(L"LoadFile: %s\n", KernelFileName);
|
5
|
114
|
|
115 Status = LoadFile(KernelFileName, &KernelBaseAddr, &BufferPageSize);
|
0
|
116 if (EFI_ERROR(Status)) {
|
|
117 return Status;
|
|
118 }
|
6
|
119 Print(L"Boot 1\n");
|
3
|
120
|
|
121 struct BootParam boot_param;
|
|
122 boot_param.kernel_addr = KernelBaseAddr;
|
|
123 boot_param.kernel_entry = KernelBaseAddr;
|
|
124 // GetGraphicMode(ImageHandle, &(boot_param.graphic_config));
|
|
125
|
|
126 typedef unsigned long (EntryPoint)(struct BootParam*);
|
|
127 EntryPoint *Entry = (EntryPoint*)(KernelBaseAddr);
|
|
128
|
|
129 // VOID *vendor_table;
|
|
130 // Status = SystemTable->BootServices->GetSystemConfigurationTable(&gEfiAcpiTableGuid, &vendor_table);
|
|
131 // if (EFI_ERROR(Status)) {
|
|
132 // Print(L"Failed to get system configuration table.\n");
|
|
133 // return Status;
|
|
134 // }
|
|
135 // boot_param.rsdp_addr = (UINT64)vendor_table;
|
6
|
136 Print(L"Boot 2\n");
|
3
|
137
|
|
138 struct MemoryMap MemoryMap = {4096, NULL, 4096, 0, 0, 0};
|
|
139 Status = uefi_call_wrapper(SystemTable->BootServices->AllocatePool, 3, EfiLoaderData, MemoryMap.BufferSize, &MemoryMap.Buffer);
|
0
|
140 if (EFI_ERROR(Status)) {
|
3
|
141 Print(L"Failed to allocate memory to get memory map\n");
|
0
|
142 return Status;
|
|
143 }
|
3
|
144
|
|
145 Status = uefi_call_wrapper(SystemTable->BootServices->GetMemoryMap, 5, &MemoryMap.MapSize, (EFI_MEMORY_DESCRIPTOR*)MemoryMap.Buffer, &MemoryMap.MapKey, &MemoryMap.DescriptorSize, &MemoryMap.DescriptorVersion);
|
|
146 if (EFI_ERROR(Status)) {
|
|
147 Print(L"Failed to get memory map.\n");
|
|
148 return Status;
|
|
149 }
|
6
|
150 Print(L"Boot 3\n");
|
0
|
151
|
3
|
152 Status = uefi_call_wrapper(SystemTable->BootServices->ExitBootServices, 2, ImageHandle, MemoryMap.MapKey);
|
|
153 if (EFI_ERROR(Status)) {
|
|
154 Print(L"Failed to exit boot services\n");
|
|
155 return Status;
|
|
156 }
|
|
157
|
6
|
158 Print(L"Start Entry\n");
|
3
|
159 Entry(&boot_param);
|
|
160
|
|
161 return Status;
|
0
|
162 }
|
3
|
163
|
|
164
|
|
165
|
|
166
|