annotate gnu-efi-3.0.12/apps/bootloader.c @ 9:33d10c470c6e default tip

now it works.
author kono
date Sat, 20 Jan 2024 16:13:18 +0900
parents c4198b641a84
children
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
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
7 struct rsdp {
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
8 CHAR8 sig[8];
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
9 UINT8 Checksum;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
10 CHAR8 OEMID[6];
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
11 UINT8 Revision;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
12 UINT32 RsdtAddress;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
13 UINT32 Len;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
14 UINT64 XsdtAddress;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
15 UINT8 ExtChecksum;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
16 UINT8 reserved[3];
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
17 };
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
18
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
19 struct __attribute((packed)) GraphicConfig {
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
20 UINT64 frame_base;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
21 UINT64 frame_size;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
22 UINT64 horizontal_resolution;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
23 UINT64 vertical_resolution;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
24 UINT64 pixels_per_scan_line;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
25 };
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
26
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
27 struct __attribute__((packed)) BootParam {
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
28 UINT64 kernel_entry;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
29 UINT64 rsdp_addr;
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
30 struct GraphicConfig graphic_config;
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
31 // struct gdt bootstrap_gdt[3];
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
32 // struct gdt_desc bootstrap_gdt_desc;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
33 UINT64 kernel_addr;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
34 };
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
35
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
36
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
37
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
38 struct MemoryMap {
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
39 UINTN BufferSize;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
40 VOID *Buffer;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
41 UINTN MapSize;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
42 UINTN MapKey;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
43 UINTN DescriptorSize;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
44 UINT32 DescriptorVersion;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
45 };
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
46
7
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
47 void HexDump(UINT8 *bytePtr, UINTN size) {
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
48 for (UINTN i = 0; i < size; i++) {
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
49 if (i % 16 == 0 && i != 0) {
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
50 Print(L"\n");
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
51 }
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
52 Print(L"%02x ", bytePtr[i]);
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
53 }
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
54 Print(L"\n");
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
55 }
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
56
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
57
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
58 EFI_STATUS LoadFile(CHAR16 *FileName, EFI_PHYSICAL_ADDRESS *FileAddr, UINTN *FilePageSize) {
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
59 EFI_STATUS Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
60 EFI_FILE_IO_INTERFACE *IOVolume;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
61 EFI_FILE_HANDLE Root;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
62 EFI_FILE_HANDLE File;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
63 Print(L"LoadFile\n");
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
64
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
65 // Locate all handles that support the Simple File System Protocol.
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
66 UINTN HandleCount = 5;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
67 void **HandleBuffer;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
68 Status = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &HandleCount, &HandleBuffer);
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
69 if (EFI_ERROR(Status)) {
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
70 Print(L"Failed to locate handles for file system protocol: %r\n", Status);
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
71 return Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
72 }
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
73
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
74 for (UINTN Index = 0; Index < HandleCount; Index++) {
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
75 // Try to open the file system
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
76 Status = uefi_call_wrapper(BS->HandleProtocol, 3, HandleBuffer[Index], &FileSystemProtocol, (void **)&IOVolume);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
77
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 // Open the root directory
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
80 Status = uefi_call_wrapper(IOVolume->OpenVolume, 2, IOVolume, &Root);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
81 if (EFI_ERROR(Status)) {
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
82 Print(L"Failed to open root directory: %r\n", Status);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
83 continue;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
84 }
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
85
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
86 // Now open a file
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
87 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
88 if (EFI_ERROR(Status)) {
6
kono
parents: 5
diff changeset
89 // Print(L"Failed to open file: %r\n", Status);
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
90 uefi_call_wrapper(Root->Close, 1, Root);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
91 continue;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
92 }
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
93
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
94 EFI_FILE_INFO *FileInfo;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
95 UINTN FileInfoSize;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
96 FileInfoSize = sizeof(EFI_FILE_INFO) + 1024;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
97 FileInfo = AllocatePool(FileInfoSize);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
98 Status = uefi_call_wrapper(File->GetInfo, 4, File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
99 if (EFI_ERROR(Status)) {
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
100 Print(L"Failed to get file info.\n");
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
101 FreePool(FileInfo);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
102 return Status;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
103 }
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
104
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
105 UINTN FileSize = FileInfo->FileSize;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
106 Print(L"FileSize = %d\n", FileSize);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
107 *FilePageSize = (FileSize + 4095) / 4096;
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
108 *FileAddr = 0x40000000;
9
33d10c470c6e now it works.
kono
parents: 8
diff changeset
109 Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAddress, EfiBootServicesCode, *FilePageSize, FileAddr);
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
110 if (EFI_ERROR(Status)) {
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
111 Print(L"Failed to allocate pages.\n");
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
112 FreePool(FileInfo);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
113 return Status;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
114 }
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
115
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
116 // skip header
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
117 FileSize = 0x1000;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
118 Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
119 if (EFI_ERROR(Status)) {
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
120 Print(L"Failed to load file.\n");
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
121 FreePool(FileInfo);
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
122 return Status;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
123 }
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
124
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
125 // load binary part
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
126 FileSize = FileInfo->FileSize-0x1000;
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
127 Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
128 if (EFI_ERROR(Status)) {
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
129 Print(L"Failed to load file.\n");
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
130 FreePool(FileInfo);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
131 return Status;
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
132 }
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
133 Print(L"Successfully loaded file: %s\n", FileName);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
134
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
135 FreePool(FileInfo);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
136 // Close the file and root directory
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
137 uefi_call_wrapper(File->Close, 1, File);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
138 uefi_call_wrapper(Root->Close, 1, Root);
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
139 }
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
140 }
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
141
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
142
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
143 return Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
144 }
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
145
9
33d10c470c6e now it works.
kono
parents: 8
diff changeset
146 UINT64 ReadCurrentEL() {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
147 UINT64 currentEL;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
148 __asm__ volatile("MRS %0, CurrentEL" : "=r" (currentEL));
33d10c470c6e now it works.
kono
parents: 8
diff changeset
149 return (currentEL >> 2) & 0x3; // Extract EL value
33d10c470c6e now it works.
kono
parents: 8
diff changeset
150 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
151
33d10c470c6e now it works.
kono
parents: 8
diff changeset
152 void DumpLoadedImageProtocol(EFI_LOADED_IMAGE *LoadedImage) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
153 Print(L"Dumping EFI_LOADED_IMAGE_PROTOCOL\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
154 Print(L"Revision: %x\n", LoadedImage->Revision);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
155 Print(L"ParentHandle: %x\n", LoadedImage->ParentHandle);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
156 Print(L"SystemTable: %x\n", LoadedImage->SystemTable);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
157
33d10c470c6e now it works.
kono
parents: 8
diff changeset
158 Print(L"DeviceHandle: %x\n", LoadedImage->DeviceHandle);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
159 Print(L"FilePath: %s\n", DevicePathToStr(LoadedImage->FilePath));
33d10c470c6e now it works.
kono
parents: 8
diff changeset
160 // Note: LoadedImage->Reserved is not dumped as it's meant to be NULL/unused
33d10c470c6e now it works.
kono
parents: 8
diff changeset
161
33d10c470c6e now it works.
kono
parents: 8
diff changeset
162 Print(L"LoadOptionsSize: %x\n", LoadedImage->LoadOptionsSize);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
163 Print(L"LoadOptions: %s\n", LoadedImage->LoadOptions);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
164
33d10c470c6e now it works.
kono
parents: 8
diff changeset
165 Print(L"ImageBase: %x\n", LoadedImage->ImageBase);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
166 Print(L"ImageSize: %lx\n", LoadedImage->ImageSize);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
167 Print(L"ImageCodeType: %x\n", LoadedImage->ImageCodeType);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
168 Print(L"ImageDataType: %x\n", LoadedImage->ImageDataType);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
169
33d10c470c6e now it works.
kono
parents: 8
diff changeset
170 if (LoadedImage->Unload != NULL) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
171 Print(L"Unload: %x\n", LoadedImage->Unload);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
172 } else {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
173 Print(L"Unload: NULL\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
174 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
175 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
176
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
177 EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
178
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
179 EFI_STATUS Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
180 CHAR16 *KernelFileName = L"\\kernel";
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
181 EFI_PHYSICAL_ADDRESS KernelBaseAddr;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
182
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
183 InitializeLib(ImageHandle, SystemTable);
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
184 Print(L"Initializeing\n");
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
185 UINTN BufferPageSize;
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
186
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
187 Print(L"LoadFile: %s\n", KernelFileName);
5
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
188
209a1a162228 FSOpen: Open '\kernel' Success
kono
parents: 3
diff changeset
189 Status = LoadFile(KernelFileName, &KernelBaseAddr, &BufferPageSize);
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
190 if (EFI_ERROR(Status)) {
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
191 return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
192 }
6
kono
parents: 5
diff changeset
193 Print(L"Boot 1\n");
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
194
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
195 struct BootParam boot_param;
7
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
196 boot_param.kernel_addr = KernelBaseAddr ;
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
197 boot_param.kernel_entry = boot_param.kernel_addr ;
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
198 // GetGraphicMode(ImageHandle, &(boot_param.graphic_config));
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
199
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
200
6
kono
parents: 5
diff changeset
201 Print(L"Boot 2\n");
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
202
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
203 void **rsdp = NULL;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
204 EFI_GUID AcpiTableGuid = ACPI_20_TABLE_GUID; // Use ACPI 2.0 table GUID
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
205
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
206 Status = LibGetSystemConfigurationTable(&AcpiTableGuid, (void **)&rsdp);
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
207 if (EFI_ERROR(Status) || rsdp == NULL) {
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
208 Print(L"Error getting ACPI table: %r\n", Status);
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
209 return Status;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
210 } else {
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
211 boot_param.rsdp_addr = (UINT64)rsdp;
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
212 }
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
213
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
214 Print(L"Boot 3\n");
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
215
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
216 struct MemoryMap MemoryMap = {4096, NULL, 4096, 0, 0, 0};
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
217 Status = uefi_call_wrapper(SystemTable->BootServices->AllocatePool, 3, EfiLoaderData, MemoryMap.BufferSize, &MemoryMap.Buffer);
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
218 if (EFI_ERROR(Status)) {
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
219 Print(L"Failed to allocate memory to get memory map\n");
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
220 return Status;
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
221 }
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
222
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
223 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
224 if (EFI_ERROR(Status)) {
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
225 Print(L"Failed to get memory map.\n");
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
226 return Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
227 }
8
c4198b641a84 kernel is correctly loaded on 0x40000000
kono
parents: 7
diff changeset
228 Print(L"Boot r4\n");
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
229
7
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
230 Print(L"Kernel Entry: %llx\n",boot_param.kernel_entry);
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
231 Print(L"RSDP Address: %llx\n",boot_param.rsdp_addr);
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
232 Print(L"Kernel Address: %llx\n",boot_param.kernel_addr);
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
233
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
234 HexDump((UINT8 *)KernelBaseAddr,0x100);
f422d46482db run into the kernel but it crashed
kono
parents: 6
diff changeset
235
9
33d10c470c6e now it works.
kono
parents: 8
diff changeset
236 EFI_DEVICE_PATH *Path;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
237 EFI_LOADED_IMAGE *LoadedImageParent;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
238 EFI_LOADED_IMAGE *LoadedImage;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
239 EFI_HANDLE Image;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
240 CHAR16 *Options = L"root=/dev/sda2 rootfstype=btrfs rw quiet splash";
33d10c470c6e now it works.
kono
parents: 8
diff changeset
241
33d10c470c6e now it works.
kono
parents: 8
diff changeset
242 Status = uefi_call_wrapper(BS->OpenProtocol, 6, ImageHandle, &LoadedImageProtocol,(void**)&LoadedImageParent, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
243 if (EFI_ERROR(Status)) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
244 Print(L"Could not get LoadedImageProtocol handler %r\n", Status);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
245 return Status;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
246 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
247 Print(L"Hello,2!\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
248 if (0) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
249 Path = FileDevicePath(LoadedImageParent->DeviceHandle, L"\\kernel");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
250 if (Path == NULL) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
251 Print(L"Could not get device path.");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
252 return EFI_INVALID_PARAMETER;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
253 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
254 Print(L"Hello,3!\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
255 Status = uefi_call_wrapper(BS->LoadImage, 6, FALSE, ImageHandle, Path, NULL, 0, &Image);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
256 if (EFI_ERROR(Status)) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
257 Print(L"Could not load %r", Status);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
258 FreePool(Path);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
259 return Status;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
260 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
261 Print(L"Hello,4!\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
262 Status = uefi_call_wrapper(BS->OpenProtocol, 6, Image, &LoadedImageProtocol, (void**)&LoadedImage, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
263 if (EFI_ERROR(Status)) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
264 Print(L"Could not get LoadedImageProtocol handler %r\n", Status);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
265 uefi_call_wrapper(BS->UnloadImage, 1, Image);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
266 FreePool(Path);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
267 return Status;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
268 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
269 Print(L"Hello,5!\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
270 LoadedImage->LoadOptions = Options;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
271 LoadedImage->LoadOptionsSize = (StrLen(LoadedImage->LoadOptions)+1) * sizeof(CHAR16);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
272
33d10c470c6e now it works.
kono
parents: 8
diff changeset
273 DumpLoadedImageProtocol (LoadedImage);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
274
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
275 Status = uefi_call_wrapper(SystemTable->BootServices->ExitBootServices, 2, ImageHandle, MemoryMap.MapKey);
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
276 if (EFI_ERROR(Status)) {
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
277 Print(L"Failed to exit boot services\n");
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
278 return Status;
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
279 }
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
280
9
33d10c470c6e now it works.
kono
parents: 8
diff changeset
281 // Start the image
33d10c470c6e now it works.
kono
parents: 8
diff changeset
282 Status = uefi_call_wrapper(SystemTable->BootServices->StartImage, 3, Image, NULL, NULL);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
283
33d10c470c6e now it works.
kono
parents: 8
diff changeset
284 uefi_call_wrapper(BS->UnloadImage, 1, Image);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
285 FreePool(Path);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
286
33d10c470c6e now it works.
kono
parents: 8
diff changeset
287 } else {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
288
33d10c470c6e now it works.
kono
parents: 8
diff changeset
289 UINT64 el = ReadCurrentEL();
33d10c470c6e now it works.
kono
parents: 8
diff changeset
290 Print(L"Current Exception Level: %d\n", el);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
291
33d10c470c6e now it works.
kono
parents: 8
diff changeset
292 Status = uefi_call_wrapper(SystemTable->BootServices->ExitBootServices, 2, ImageHandle, MemoryMap.MapKey);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
293 if (EFI_ERROR(Status)) {
33d10c470c6e now it works.
kono
parents: 8
diff changeset
294 Print(L"Failed to exit boot services\n");
33d10c470c6e now it works.
kono
parents: 8
diff changeset
295 return Status;
33d10c470c6e now it works.
kono
parents: 8
diff changeset
296
33d10c470c6e now it works.
kono
parents: 8
diff changeset
297 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
298
33d10c470c6e now it works.
kono
parents: 8
diff changeset
299
33d10c470c6e now it works.
kono
parents: 8
diff changeset
300 // Inline assembly to jump to the kernel
33d10c470c6e now it works.
kono
parents: 8
diff changeset
301 // __asm__ volatile("br %0" : : "r"(0x40000000));
33d10c470c6e now it works.
kono
parents: 8
diff changeset
302
33d10c470c6e now it works.
kono
parents: 8
diff changeset
303 typedef unsigned long (EntryPoint)(struct BootParam*);
33d10c470c6e now it works.
kono
parents: 8
diff changeset
304 EntryPoint *Entry = (EntryPoint*)(KernelBaseAddr);
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
305 Entry(&boot_param);
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
306
9
33d10c470c6e now it works.
kono
parents: 8
diff changeset
307 }
33d10c470c6e now it works.
kono
parents: 8
diff changeset
308
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
309 return Status;
0
f434d6ab8db1 xv6-UEFI bootloader
nana
parents:
diff changeset
310 }
3
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
311
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
312
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
313
c2a38c3c71ab fix bootloader.c
kono
parents: 1
diff changeset
314