annotate gnu-efi-3.0.12/apps/bootloader.c @ 6:117ef669f2ce

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