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
|
/* ========================================
Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
======================================== */
#include <BootKit/BootKit.h>
#include <BootKit/BootThread.h>
#include <FirmwareKit/EFI.h>
#include <FirmwareKit/EFI/API.h>
#include <FirmwareKit/Handover.h>
#include <KernelKit/MSDOS.h>
#include <KernelKit/PE.h>
#include <KernelKit/PEF.h>
#include <NeKit/Macros.h>
#include <NeKit/Ref.h>
#include <modules/CoreGfx/CoreGfx.h>
#include <modules/CoreGfx/TextGfx.h>
/** Graphics related. */
STATIC EfiGraphicsOutputProtocol* kGop = nullptr;
STATIC UInt16 kGopStride = 0U;
STATIC EFI_GUID kGopGuid;
/** Related to jumping to the reset vector. */
EXTERN_C Void rt_reset_hardware();
EXTERN_C Kernel::VoidPtr boot_read_cr3(); // @brief Page directory inside cr3 register.
/**
@brief Finds and stores the GOP object.
*/
STATIC Bool boot_init_fb() noexcept {
kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID);
kGop = nullptr;
if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No;
kGopStride = 4;
return Yes;
}
EFI_GUID kEfiGlobalNamespaceVarGUID = {
0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}};
/// @brief BootloaderMain EFI entrypoint.
/// @param image_handle Handle of this image.
/// @param sys_table The system table of it.
/// @return nothing, never returns.
EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) {
fw_init_efi(sys_table); ///! Init the EFI library.
ST->ConOut->ClearScreen(sys_table->ConOut);
ST->ConOut->SetAttribute(sys_table->ConOut, kEFIYellow);
ST->BootServices->SetWatchdogTimer(0, 0, 0, nullptr);
ST->ConOut->EnableCursor(ST->ConOut, false);
HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader();
UInt32 map_key = 0;
UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor);
EfiMemoryDescriptor* struct_ptr = nullptr;
UInt32 sz_desc = sizeof(EfiMemoryDescriptor);
UInt32 rev_desc = 0;
Boot::BootTextWriter writer;
if (!boot_init_fb()) {
writer.Write("BootZ: Invalid Framebuffer, can't boot to NeKernel.\r");
Boot::Stop();
}
for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) {
Char* vendor_table =
reinterpret_cast<Char*>(sys_table->ConfigurationTable[index_vt].VendorTable);
// ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...)
if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' &&
vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' &&
vendor_table[6] == 'R' && vendor_table[7] == ' ') {
handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table;
break;
}
}
// ------------------------------------------ //
// draw background color.
// ------------------------------------------ //
handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase;
handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution;
handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution;
handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine;
handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat;
handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize;
// ------------------------------------------- //
// Grab MP services, extended to runtime. //
// ------------------------------------------- //
EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID);
EfiMpServicesProtocol* mp = nullptr;
BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp));
handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast<VoidPtr>(mp);
kHandoverHeader = handover_hdr;
FB::cg_clear_video();
UInt32 cnt_enabled = 0;
UInt32 cnt_disabled = 0;
if (mp) {
mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled);
handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1;
} else {
handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO;
}
// Fill handover header now.
handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */
handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */
kHandoverHeader->f_BitMapStart = nullptr; /* Start of bitmap. */
kHandoverHeader->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */
UInt16 trials = 5;
while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize,
&kHandoverHeader->f_BitMapStart) != kEfiOk) {
--trials;
if (!trials) {
writer.Write("BootZ: Unable to allocate sufficient memory, trying again with 2GB...\r");
trials = 3;
kHandoverHeader->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */
while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize,
&kHandoverHeader->f_BitMapStart) != kEfiOk) {
--trials;
if (!trials) {
writer.Write("BootZ: Unable to allocate sufficient memory, aborting...\r");
Boot::Stop();
}
}
}
}
handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableBS] = (VoidPtr) BS;
handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST] = (VoidPtr) ST;
// ------------------------------------------ //
// If we succeed in reading the blob, then execute it.
// ------------------------------------------ //
Boot::BootFileReader reader_syschk(L"chk.efi", image_handle);
reader_syschk.ReadAll(0);
Boot::BootThread* syschk_thread = nullptr;
if (reader_syschk.Blob()) {
syschk_thread = new Boot::BootThread(reader_syschk.Blob());
syschk_thread->SetName("SysChk");
syschk_thread->Start(handover_hdr, NO);
}
BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc);
handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor);
handover_hdr->f_Magic = kHandoverMagic;
handover_hdr->f_Version = kHandoverVersion;
handover_hdr->f_HardwareTables.f_ImageKey = map_key;
handover_hdr->f_HardwareTables.f_ImageHandle = image_handle;
// Provide fimware vendor name.
Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor,
handover_hdr->f_FirmwareVendorLen);
handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor);
// Assign to global 'kHandoverHeader'.
WideChar kernel_path[256U] = L"ne_kernel";
UInt32 kernel_path_sz = StrLen("ne_kernel");
UInt32 sz_ver = sizeof(UInt64);
UInt64 ver = KERNEL_VERSION_BCD;
ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver,
&ver);
if (ver < KERNEL_VERSION_BCD) {
ver = KERNEL_VERSION_BCD;
ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr,
&sz_ver, &ver);
writer.Write("BootZ: Version has been updated: ").Write(ver).Write("\r");
if (ST->RuntimeServices->GetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, nullptr,
&kernel_path_sz, kernel_path) != kEfiOk) {
/// access attributes (in order)
/// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
UInt32 attr = 0x00000001 | 0x00000002 | 0x00000004;
ST->RuntimeServices->SetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, &attr,
&kernel_path_sz, kernel_path);
}
} else {
writer.Write("BootZ: Version: ").Write(ver).Write("\r");
}
// boot to kernel, if not bootnet this.
Boot::BootFileReader reader_kernel(kernel_path, image_handle);
reader_kernel.ReadAll(0);
// ------------------------------------------ //
// If we succeed in reading the blob, then execute it.
// ------------------------------------------ //
if (reader_kernel.Blob()) {
handover_hdr->f_PageStart = boot_read_cr3();
auto kernel_thread = Boot::BootThread(reader_kernel.Blob());
kernel_thread.SetName("NeKernel");
handover_hdr->f_KernelImage = reader_kernel.Blob();
handover_hdr->f_KernelSz = reader_kernel.Size();
kernel_thread.Start(handover_hdr, YES);
}
Boot::BootFileReader reader_netboot(L"net.efi", image_handle);
reader_netboot.ReadAll(0);
if (!reader_netboot.Blob()) return kEfiFail;
auto netboot_thread = Boot::BootThread(reader_netboot.Blob());
netboot_thread.SetName("BootNet");
return netboot_thread.Start(handover_hdr, NO);
}
|