Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
392 lines (343 loc) · 11.5 KB
/
Copy pathmain.c
File metadata and controls
392 lines (343 loc) · 11.5 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* EFILOADER
* Copyright (C) 2026 a1ive <https://github.com/a1ive/efiloader>
*
* EFILOADER is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EFILOADER is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EFILOADER. If not, see <http://www.gnu.org/licenses/>.
*/
#include <intrin.h>
#include "arch.h"
#include "bootconfig.h"
#include "loader.h"
#include "payload.h"
#include "print.h"
#include "serial.h"
#include "types.h"
EFI_CONTEXT gContext;
static VOID HaltProcessor(VOID)
{
#if defined(_M_IX86) || defined(_M_X64)
__halt();
#else
for (;;)
{
}
#endif
}
static VOID CopyString16(CHAR16* Destination, const CHAR16* Source, UINTN DestinationCharacters)
{
UINTN Index;
if (DestinationCharacters == 0)
{
return;
}
Index = 0;
while ((Index + 1 < DestinationCharacters) && (Source[Index] != 0))
{
Destination[Index] = Source[Index];
Index++;
}
Destination[Index] = 0;
}
#define DEFAULT_IMAGE_PATH L"\\shell.efi"
#if defined(_M_ARM64)
/* SCTLR.M (MMU), SCTLR.C (data cache), SCTLR.I (instruction cache) */
#define ARM64_SCTLR_SWITCH_MASK ((1ULL << 0) | (1ULL << 2) | (1ULL << 12))
/* Returns FALSE for a reserved TG0 encoding. */
static BOOLEAN Arm64Ttbr0Walk(UINT64 Tcr, UINT32* GranuleShift, UINT32* StartLevel)
{
UINT32 Shift;
UINT32 VaBits;
UINT32 Stride;
UINT32 Levels;
switch ((Tcr >> 14) & 3)
{
case 0:
Shift = 12;
break;
case 1:
Shift = 16;
break;
case 2:
Shift = 14;
break;
default:
return FALSE;
}
VaBits = 64 - (UINT32)(Tcr & 0x3F);
if (VaBits <= Shift)
{
return FALSE;
}
Stride = Shift - 3;
Levels = (VaBits - Shift + Stride - 1) / Stride;
if (Levels > 4)
{
return FALSE;
}
*GranuleShift = Shift;
*StartLevel = 4 - Levels;
return TRUE;
}
static BOOLEAN Arm64SameTtbr0Walk(UINT64 ApplicationTcr, UINT64 FirmwareTcr)
{
UINT32 ApplicationShift;
UINT32 ApplicationLevel;
UINT32 FirmwareShift;
UINT32 FirmwareLevel;
if (!Arm64Ttbr0Walk(ApplicationTcr, &ApplicationShift, &ApplicationLevel) ||
!Arm64Ttbr0Walk(FirmwareTcr, &FirmwareShift, &FirmwareLevel))
{
return FALSE;
}
return (ApplicationShift == FirmwareShift) && (ApplicationLevel == FirmwareLevel);
}
#endif
NTSTATUS BLAPI BlApplicationEntry(BOOT_APPLICATION_PARAMETER_BLOCK* BootParameters)
{
CHAR16 TargetImagePath[MAX_PATH] = DEFAULT_IMAGE_PATH;
BL_LOADED_APPLICATION_ENTRY LoadedApplicationEntry;
BL_FIRMWARE_DESCRIPTOR* Firmware = NULL;
EMBEDDED_PAYLOAD EmbeddedPayload;
UINTN FirmwareDescriptorSize;
NTSTATUS PayloadStatus;
NTSTATUS Status = STATUS_UNSUCCESSFUL;
#if defined(_M_ARM64)
UINT32 ApplicationEl;
UINT64 ApplicationTcr;
BOOLEAN TcrWalkMismatch;
#endif
SerialInitialize();
SerialPrint(L"BlApplicationEntry\n");
if (BootParameters == NULL || BootParameters->FirmwareParametersOffset == 0)
{
SerialPrint(L"BP invalid\n");
Status = STATUS_INVALID_PARAMETER;
return Status;
}
Firmware = (BL_FIRMWARE_DESCRIPTOR*)((UINT8*)BootParameters + BootParameters->FirmwareParametersOffset);
FirmwareDescriptorSize = (BootParameters->ReturnArgumentsOffset > BootParameters->FirmwareParametersOffset) ?
((UINTN)BootParameters->ReturnArgumentsOffset - (UINTN)BootParameters->FirmwareParametersOffset) :
sizeof(*Firmware);
SerialPrint(L"BP size=0x%016llX fw=0x%016llX ret=0x%016llX\n",
(UINT64)BootParameters->Size,
(UINT64)BootParameters->FirmwareParametersOffset,
(UINT64)BootParameters->ReturnArgumentsOffset);
SerialPrint(L"BP translation=0x%08X\n", (UINT64)BootParameters->MemoryTranslationType);
#if defined(_M_X64) || defined(_M_ARM64)
/*
* Switching CR3/TTBR continues execution at the current PC/SP and keeps using
* BootParameters, the firmware descriptor, globals and stack locals. That
* only works when the application context is identity mapped, i.e. Boot
* Manager started us with physical memory translation.
*/
if (BootParameters->MemoryTranslationType != BOOT_MEMORY_TRANSLATION_TYPE_PHYSICAL)
{
SerialPrint(L"Unsupported memory translation type %x\n",
(UINT64)BootParameters->MemoryTranslationType);
return STATUS_NOT_SUPPORTED;
}
#endif
SerialPrint(L"FW version=0x%016llX size=0x%016llX image=%p st=%p\n",
(UINT64)Firmware->Version,
(UINT64)FirmwareDescriptorSize,
Firmware->ImageHandle,
(VOID*)Firmware->SystemTable);
if (Firmware->Version < 1)
{
SerialPrint(L"Unsupported FW version %x\n", (UINT64)Firmware->Version);
Status = STATUS_NOT_SUPPORTED;
return Status;
}
#if defined(_M_ARM64)
if (FirmwareDescriptorSize < sizeof(*Firmware))
{
SerialPrint(L"ARM64 FW descriptor too small: 0x%016llX\n",
(UINT64)FirmwareDescriptorSize);
Status = STATUS_NOT_SUPPORTED;
return Status;
}
/*
* The restore sequence only writes *_EL1 registers. When the application
* itself runs at EL2 they would not affect the current translation regime.
*/
ApplicationEl = ArchReadCurrentEl();
if (ApplicationEl != 1)
{
return STATUS_NOT_SUPPORTED;
}
/*
* Different cacheability or MMU enable bits would need cache maintenance
* around the switch, which the restore sequence does not do.
*/
if (((ArchReadSctlr() ^ Firmware->ProcessorState.Sctlr) & ARM64_SCTLR_SWITCH_MASK) != 0)
{
return STATUS_NOT_SUPPORTED;
}
/*
* Instructions between the TCR/TTBR writes and the TLB invalidation are
* only safe on a TLB miss if both contexts walk TTBR0 the same way. A
* mismatch is reported after the switch rather than rejected, because the
* window only fetches from the current, recently used code page.
*/
ApplicationTcr = ArchReadTcr();
TcrWalkMismatch = !Arm64SameTtbr0Walk(ApplicationTcr, Firmware->ProcessorState.Tcr);
#endif
#if defined(_M_X64)
if (Firmware->Version >= 3)
{
SerialPrint(L"FW cr3=0x%016llX gdtr=0x%016llX:0x%016llX idtr=0x%016llX:0x%016llX\n",
(UINT64)Firmware->ProcessorState.Cr3,
(UINT64)Firmware->ProcessorState.Gdtr.Base,
(UINT64)Firmware->ProcessorState.Gdtr.Limit,
(UINT64)Firmware->ProcessorState.Idtr.Base,
(UINT64)Firmware->ProcessorState.Idtr.Limit);
SerialPrint(L"FW cs=0x%016llX ds=0x%016llX es=0x%016llX fs=0x%016llX gs=0x%016llX ss=0x%016llX trans=0x%016llX\n",
(UINT64)Firmware->ProcessorState.Cs,
(UINT64)Firmware->ProcessorState.Ds,
(UINT64)Firmware->ProcessorState.Es,
(UINT64)Firmware->ProcessorState.Fs,
(UINT64)Firmware->ProcessorState.Gs,
(UINT64)Firmware->ProcessorState.Ss,
(UINT64)Firmware->ProcessorState.TranslationEnabled);
}
else if (Firmware->Version == 2)
{
SerialPrint(L"FW cr3=0x%016llX gdtr=0x%016llX:0x%016llX idtr=0x%016llX:0x%016llX\n",
(UINT64)Firmware->ProcessorState.Cr3,
(UINT64)Firmware->ProcessorState.Gdtr.Base,
(UINT64)Firmware->ProcessorState.Gdtr.Limit,
(UINT64)Firmware->ProcessorState.Idtr.Base,
(UINT64)Firmware->ProcessorState.Idtr.Limit);
SerialPrint(L"FW cs=0x%016llX ds=0x%016llX es=0x%016llX fs=0x%016llX gs=0x%016llX ss=0x%016llX\n",
(UINT64)Firmware->ProcessorState.Cs,
(UINT64)Firmware->ProcessorState.Ds,
(UINT64)Firmware->ProcessorState.Es,
(UINT64)Firmware->ProcessorState.Fs,
(UINT64)Firmware->ProcessorState.Gs,
(UINT64)Firmware->ProcessorState.Ss);
}
else
{
SerialPrint(L"FW cr3=0x%016llX\n",
(UINT64)Firmware->ProcessorState.Cr3);
}
#elif defined(_M_ARM64)
SerialPrint(L"FW sctlr=0x%016llX vbar=0x%016llX tpidr=0x%016llX el=0x%016llX irq=0x%016llX\n",
(UINT64)Firmware->ProcessorState.Sctlr,
(UINT64)Firmware->ProcessorState.Vbar,
(UINT64)Firmware->ProcessorState.Tpidr,
(UINT64)Firmware->ProcessorState.CurrentEl,
(UINT64)Firmware->ProcessorState.InterruptsEnabled);
SerialPrint(L"FW ttbr0=0x%016llX ttbr1=0x%016llX tcr=0x%016llX mair=0x%016llX\n",
(UINT64)Firmware->ProcessorState.Ttbr0,
(UINT64)Firmware->ProcessorState.Ttbr1,
(UINT64)Firmware->ProcessorState.Tcr,
(UINT64)Firmware->ProcessorState.Mair);
#else
SerialPrint(L"FW IA32 descriptor size=0x%016llX\n",
(UINT64)FirmwareDescriptorSize);
#endif
Status = EfiInitializeContext(Firmware);
SerialPrint(L"EfiInitializeContext status=0x%08X\n", (UINT64)(UINT32)Status);
if (Status != STATUS_SUCCESS)
{
SerialPrint(L"FW invalid\n");
return Status;
}
PayloadStatus = PayloadLocate((VOID*)(UINTN)BootParameters->ImageBase, BootParameters->ImageSize, &EmbeddedPayload);
SerialPrint(L"PayloadLocate status=0x%08X data=%p size=0x%016llX subsystem=0x%04X type=0x%x\n",
(UINT64)(UINT32)PayloadStatus,
(VOID*)EmbeddedPayload.Data,
(UINT64)EmbeddedPayload.Size,
(UINT64)EmbeddedPayload.Subsystem,
(UINT64)EmbeddedPayload.Type);
Status = BlGetLoadedApplicationEntry(BootParameters, &LoadedApplicationEntry);
SerialPrint(L"BlGetLoadedApplicationEntry status=0x%08X\n", (UINT64)(UINT32)Status);
if (Status == STATUS_SUCCESS)
{
Status = BlGetBootOptionString(LoadedApplicationEntry.BcdData, BCD_BOOTAPP_LOAD_OPTIONS, TargetImagePath, MAX_PATH);
SerialPrint(L"BCD loadoptions status=0x%08X\n", (UINT64)(UINT32)Status);
if (Status != STATUS_SUCCESS)
CopyString16(TargetImagePath, DEFAULT_IMAGE_PATH, MAX_PATH);
SerialPrint(L"Resolved target image path=%s\n", TargetImagePath);
}
SerialPrint(L"Restoring FirmwareContext\n");
#if defined(_M_X64)
if (Firmware->Version >= 2)
{
ArchRestoreFirmwareContext(&Firmware->ProcessorState, NULL);
SerialPrint(L"FirmwareContext restored\n");
}
else
{
X64_DESCRIPTOR_TABLE_REGISTER AppGdtr;
X64_DESCRIPTOR_TABLE_REGISTER AppIdtr;
/*
* v1 descriptors only carry the firmware CR3, so GDTR/IDTR stay as they
* are in the application context. Boot Manager is expected to share the
* firmware tables there (observed with ref/ms7 Win7 bootx64.efi under
* QEMU: GDTR/IDTR match the firmware values); log them so a mismatch on
* other builds can be diagnosed.
*/
_sgdt(&AppGdtr);
__sidt(&AppIdtr);
SerialPrint(L"APP gdtr=0x%016llX:0x%016llX idtr=0x%016llX:0x%016llX\n",
(UINT64)AppGdtr.Base,
(UINT64)AppGdtr.Limit,
(UINT64)AppIdtr.Base,
(UINT64)AppIdtr.Limit);
SerialPrint(L"Restoring Firmware CR3\n");
ArchRestoreFirmwarePageTable(Firmware->ProcessorState.Cr3);
SerialPrint(L"Firmware CR3 restored\n");
}
#elif defined(_M_ARM64)
ArchRestoreFirmwareContext(&Firmware->ProcessorState, NULL);
#else
ArchRestoreFirmwareContext(NULL, NULL);
#endif
EfiPrint(L"EFILOADER: Copyright (c) 2026 A1ive <https://github.com/a1ive/efiloader>\n");
EfiPrint(L"Firmware Descriptor v%x\n", (UINT64)Firmware->Version);
#if defined(_M_ARM64)
/*
* Boot Manager drops an EL2 firmware to EL1 for boot applications and uses
* a private EL2 stub to go back up. That stub is not used here, so the
* firmware and everything it starts keep running at EL1.
*/
if (Firmware->ProcessorState.CurrentEl > ApplicationEl)
{
EfiPrint(L"Warning: firmware EL%x is running at EL%x\n",
(UINT64)Firmware->ProcessorState.CurrentEl,
(UINT64)ApplicationEl);
}
if (TcrWalkMismatch)
{
EfiPrint(L"Warning: TTBR0 walk differs between contexts (tcr 0x%llx -> 0x%llx)\n",
ApplicationTcr,
Firmware->ProcessorState.Tcr);
}
#endif
if (PayloadStatus == STATUS_SUCCESS)
{
PayloadBoot(&EmbeddedPayload);
}
Status = EfiStartApplication(TargetImagePath, NULL, 0, NULL);
/*
* The firmware context switch is one-way: the Boot Manager application
* context is not restored, so never return from here.
*/
for (;;)
{
HaltProcessor();
}
}