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
|
/* -------------------------------------------
Copyright (C) 2024, ELMH GROUP, all rights reserved.
------------------------------------------- */
#include <BootKit/BootKit.h>
#include <Modules/FB/FB.h>
#include <Modules/FB/Text.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 <NewKit/Macros.h>
#include <NewKit/Ref.h>
#include <BootKit/Thread.h>
#include <Modules/FB/FB.h>
// Makes the compiler shut up.
#ifndef kMachineModel
#define kMachineModel "ZKA"
#endif // !kMachineModel
#ifndef kExpectedWidth
#define kExpectedWidth 1280
#endif
#ifndef kExpectedHeight
#define kExpectedHeight 720
#endif
/** Graphics related. */
STATIC EfiGraphicsOutputProtocol* kGop = nullptr;
STATIC UInt16 kGopStride = 0U;
STATIC EfiGUID kGopGuid;
EXTERN_C Void rt_reset_hardware();
EXTERN EfiBootServices* BS;
/**
@brief Finds and stores the GOP object.
*/
STATIC Bool boot_init_fb() noexcept
{
kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID);
kGop = nullptr;
if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop) != kEfiOk)
return No;
kGopStride = 4;
for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i)
{
EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr;
UInt32 sz = 0U;
kGop->QueryMode(kGop, i, &sz, &infoPtr);
if (infoPtr->HorizontalResolution == kExpectedWidth &&
infoPtr->VerticalResolution == kExpectedHeight)
{
kGop->SetMode(kGop, i);
return Yes;
}
}
return No;
}
EXTERN_C VoidPtr boot_read_cr3();
EXTERN_C Void boot_write_cr3(VoidPtr new_cr3);
EXTERN EfiBootServices* BS;
/// @brief Main EFI entrypoint.
/// @param ImageHandle Handle of this image.
/// @param SystemTable The system table of it.
/// @return nothing, never returns.
EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle,
EfiSystemTable* SystemTable)
{
InitEFI(SystemTable); ///! Init the EFI library.
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;
#ifdef ZBA_USE_FB
if (!boot_init_fb())
return 1; ///! Init the GOP.
for (SizeT index_vt = 0; index_vt < SystemTable->NumberOfTableEntries;
++index_vt)
{
Char* vendor_table = reinterpret_cast<Char*>(
SystemTable->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;
#endif // ZBA_USE_FB
// ------------------------------------------- //
// Grab MP services, extended to runtime. //
// ------------------------------------------- //
auto guid_mp = EfiGUID(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;
#ifdef ZBA_USE_FB
cg_init();
CGDrawInRegion(cg_color(0x00, 0x00, 0x00), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0);
cg_fini();
cg_fini();
#endif // ZBA_USE_FB
UInt32 cnt_enabled = 0;
UInt32 cnt_disabled = 0;
mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled);
#ifdef ZBA_USE_FB
CGDrawString("ZBA (c) ELMH GROUP.", 10, 10, RGB(0xFF, 0xFF, 0xFF));
CGDrawString((cnt_enabled > 1) ? "SMP detected." : "Single processor configuration detected.", 20, 10, RGB(0xFF, 0xFF, 0xFF));
#endif // ZBA_USE_FB
handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1;
// Fill handover header now.
Boot::BDiskFormatFactory<BootDeviceATA> partition_factory;
// ---------------------------------------------------- //
// The following checks for an exisiting partition
// inside the disk, if it doesn't have one,
// format the disk.
// ---------------------------------------------------- //
#ifdef ZKA_AUTO_FORMAT
if (!partition_factory.IsPartitionValid())
{
CGDrawString("Formatting EPM disk...", 30, 10, RGB(0xFF, 0xFF, 0xFF));
Boot::BDiskFormatFactory<BootDeviceATA>::BFileDescriptor root;
root.fFileName[0] = kNeFSRoot[0];
root.fFileName[1] = 0;
root.fKind = kNeFSCatalogKindDir;
partition_factory.Format("FileSystem (A:)", &root, 1);
rt_reset_hardware();
}
else
{
CGDrawString("Booting from EPM disk...", 30, 10, RGB(0xFF, 0xFF, 0xFF));
}
#endif // ZKA_AUTO_FORMAT
BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc);
struct_ptr = new EfiMemoryDescriptor[sz_desc];
BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc);
auto kDefaultMemoryMap = 0; // Grab any usable entries.
//-----------------------------------------------------------//
// A simple loop which finds a usable memory region for us.
//-----------------------------------------------------------//
SizeT lookup_index = 0UL;
for (; struct_ptr[lookup_index].Kind != EfiMemoryType::EfiConventionalMemory; ++lookup_index)
{
ZKA_UNUSED(0);
}
kDefaultMemoryMap = lookup_index;
//-----------------------------------------------------------//
// Update handover file specific table and phyiscal start field.
//-----------------------------------------------------------//
handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */
handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap. */
while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk)
{
if (handover_hdr->f_BitMapStart)
{
BS->FreePool(handover_hdr->f_BitMapStart);
handover_hdr->f_BitMapStart = nullptr;
}
}
handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS;
handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST;
Boot::BFileReader reader_syschk(L"syschk.sys", ImageHandle);
reader_syschk.ReadAll(0);
Boot::BThread* syschk_thread = nullptr;
// ------------------------------------------ //
// If we succeed in reading the blob, then execute it.
// ------------------------------------------ //
if (reader_syschk.Blob())
{
syschk_thread = new Boot::BThread(reader_syschk.Blob());
syschk_thread->SetName("System Check (ZBA EFI Driver)");
}
syschk_thread->Start(handover_hdr, NO);
// nullify these fields, to avoid being reused later.
handover_hdr->f_FirmwareCustomTables[0] = nullptr;
handover_hdr->f_FirmwareCustomTables[1] = nullptr;
handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(SystemTable->FirmwareVendor);
handover_hdr->f_Magic = kHandoverMagic;
handover_hdr->f_Version = kHandoverVersion;
// Provide fimware vendor name.
Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, SystemTable->FirmwareVendor,
handover_hdr->f_FirmwareVendorLen);
handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(SystemTable->FirmwareVendor);
// Assign to global 'kHandoverHeader'.
Boot::BFileReader reader_kernel(L"minoskrnl.exe", ImageHandle);
reader_kernel.ReadAll(0);
Boot::BThread* kernel_thread = nullptr;
// ------------------------------------------ //
// If we succeed in reading the blob, then execute it.
// ------------------------------------------ //
if (reader_kernel.Blob())
{
kernel_thread = new Boot::BThread(reader_kernel.Blob());
kernel_thread->SetName("Minimal OS Kernel.");
handover_hdr->f_KernelImage = reader_kernel.Blob();
}
else
{
#ifdef ZBA_USE_FB
CGDrawString("ZBA: Please recover your kernel image.", 30, 10, RGB(0xFF, 0xFF, 0xFF));
#endif // ZBA_USE_FB
EFI::Stop();
}
Boot::BFileReader chime_wav(L"zka\\startup.wav", ImageHandle);
Boot::BFileReader ttf_font(L"zka\\urbanist.ttf", ImageHandle);
chime_wav.ReadAll(0);
ttf_font.ReadAll(0);
if (chime_wav.Blob() &&
ttf_font.Blob())
{
handover_hdr->f_StartupChime = chime_wav.Blob();
handover_hdr->f_ChimeSz = chime_wav.Size();
handover_hdr->f_KernelImage = reader_kernel.Blob();
handover_hdr->f_KernelSz = reader_kernel.Size();
handover_hdr->f_TTFallbackFont = ttf_font.Blob();
handover_hdr->f_FontSz = ttf_font.Size();
}
else
{
#ifdef ZBA_USE_FB
CGDrawString("ZBA: OS resources are not present, please reinstall the OS.", 30, 10, RGB(0xFF, 0xFF, 0xFF));
#endif // ZBA_USE_FB
EFI::Stop();
}
EFI::ExitBootServices(map_key, ImageHandle);
// ---------------------------------------------------- //
// Finally load the OS kernel.
// ---------------------------------------------------- //
kernel_thread->Start(handover_hdr, YES);
CANT_REACH();
}
|