1 | ␊ |
2 | /*␊ |
3 | * Copyright 2007 David F. Elliott.␉ All rights reserved.␊ |
4 | */␊ |
5 | #include "saio_types.h"␊ |
6 | #include "libsaio.h"␊ |
7 | #include "boot.h"␊ |
8 | #include "bootstruct.h"␊ |
9 | #include "efi.h"␊ |
10 | #include "acpi.h"␊ |
11 | #include "fake_efi.h"␊ |
12 | #include "efi_tables.h"␊ |
13 | #include "platform.h"␊ |
14 | #include "acpi_patcher.h"␊ |
15 | #include "smbios.h"␊ |
16 | #include "device_inject.h"␊ |
17 | #include "convert.h"␊ |
18 | #include "pci.h"␊ |
19 | #include "sl.h"␊ |
20 | #include "vers.h"␊ |
21 | ␊ |
22 | #ifndef DEBUG_EFI␊ |
23 | ␉#define DEBUG_EFI 0␊ |
24 | #endif␊ |
25 | ␊ |
26 | #ifndef RANDOMSEED␊ |
27 | ␉#define RANDOMSEED 0␊ |
28 | #endif␊ |
29 | ␊ |
30 | #if DEBUG_EFI␊ |
31 | ␉#define DBG(x...)␉printf(x)␊ |
32 | #else␊ |
33 | ␉#define DBG(x...)␊ |
34 | #endif␊ |
35 | ␊ |
36 | extern void setup_pci_devs(pci_dt_t *pci_dt);␊ |
37 | ␊ |
38 | /*␊ |
39 | * Modern Darwin kernels require some amount of EFI because Apple machines all␊ |
40 | * have EFI. Modifying the kernel source to not require EFI is of course␊ |
41 | * possible but would have to be maintained as a separate patch because it is␊ |
42 | * unlikely that Apple wishes to add legacy support to their kernel.␊ |
43 | *␊ |
44 | * As you can see from the Apple-supplied code in bootstruct.c, it seems that␊ |
45 | * the intention was clearly to modify this booter to provide EFI-like structures␊ |
46 | * to the kernel rather than modifying the kernel to handle non-EFI stuff. This␊ |
47 | * makes a lot of sense from an engineering point of view as it means the kernel␊ |
48 | * for the as yet unreleased EFI-only Macs could still be booted by the non-EFI␊ |
49 | * DTK systems so long as the kernel checked to ensure the boot tables were␊ |
50 | * filled in appropriately.␉Modern xnu requires a system table and a runtime␊ |
51 | * services table and performs no checks whatsoever to ensure the pointers to␊ |
52 | * these tables are non-NULL. Therefore, any modern xnu kernel will page fault␊ |
53 | * early on in the boot process if the system table pointer is zero.␊ |
54 | *␊ |
55 | * Even before that happens, the tsc_init function in modern xnu requires the FSB␊ |
56 | * Frequency to be a property in the /efi/platform node of the device tree or else␊ |
57 | * it panics the bootstrap process very early on.␊ |
58 | *␊ |
59 | * As of this writing, the current implementation found here is good enough␊ |
60 | * to make the currently available xnu kernel boot without modification on a␊ |
61 | * system with an appropriate processor. With a minor source modification to␊ |
62 | * the tsc_init function to remove the explicit check for Core or Core 2␊ |
63 | * processors the kernel can be made to boot on other processors so long as␊ |
64 | * the code can be executed by the processor and the machine contains the␊ |
65 | * necessary hardware.␊ |
66 | */␊ |
67 | ␊ |
68 | /*==========================================================================␊ |
69 | * Utility function to make a device tree string from an EFI_GUID␊ |
70 | */␊ |
71 | static inline char * mallocStringForGuid(EFI_GUID const *pGuid)␊ |
72 | {␊ |
73 | ␉char *string = malloc(37);␊ |
74 | ␉efi_guid_unparse_upper(pGuid, string);␊ |
75 | ␉return string;␊ |
76 | }␊ |
77 | ␊ |
78 | /*==========================================================================␊ |
79 | * Function to map 32 bit physical address to 64 bit virtual address␊ |
80 | */␊ |
81 | static uint64_t ptov64(uint32_t addr)␊ |
82 | {␊ |
83 | ␉return ((uint64_t)addr | 0xFFFFFF8000000000ULL);␊ |
84 | }␊ |
85 | ␊ |
86 | // ==========================================================================␊ |
87 | ␊ |
88 | EFI_UINT32 getCPUTick(void)␊ |
89 | {␊ |
90 | ␉uint32_t out;␊ |
91 | ␉/*␊ |
92 | ␉ * Note: shl $32, %edx leaves 0 in %edx, and or to %eax does nothing - zenith432␊ |
93 | ␉ */␊ |
94 | ␉__asm__ volatile (␊ |
95 | ␉␉"rdtsc\n"␊ |
96 | ␉␉"shl $32,%%edx\n"␊ |
97 | ␉␉"or %%edx,%%eax\n"␊ |
98 | ␉␉: "=a" (out)␊ |
99 | ␉␉:␊ |
100 | ␉␉: "%edx"␊ |
101 | ␉);␊ |
102 | ␉return out;␊ |
103 | }␊ |
104 | ␊ |
105 | /*==========================================================================␊ |
106 | * Fake EFI implementation␊ |
107 | */␊ |
108 | ␊ |
109 | /* Identify ourselves as the EFI firmware vendor */␊ |
110 | static EFI_CHAR16 const FIRMWARE_VENDOR[] = {'C','h','a','m','e','l','e','o','n','_','2','.','3', 0};␊ |
111 | ␊ |
112 | static EFI_UINT32 const FIRMWARE_REVISION = EFI_SYSTEM_TABLE_REVISION;␊ |
113 | ␊ |
114 | /* Default platform system_id (fix by IntVar)␊ |
115 | static EFI_CHAR8 const SYSTEM_ID[] = "0123456789ABCDEF"; //random value gen by uuidgen␊ |
116 | */␊ |
117 | ␊ |
118 | /* Just a ret instruction */␊ |
119 | static uint8_t const VOIDRET_INSTRUCTIONS[] = {0xc3};␊ |
120 | ␊ |
121 | /* movl $0x80000003,%eax; ret */␊ |
122 | static uint8_t const UNSUPPORTEDRET_INSTRUCTIONS_32[] = {0xb8, 0x03, 0x00, 0x00, 0x80, 0xc3};␊ |
123 | static uint8_t const UNSUPPORTEDRET_INSTRUCTIONS_64[] = {0x48, 0xb8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc3};␊ |
124 | ␊ |
125 | EFI_SYSTEM_TABLE_32 *gST32 = NULL;␊ |
126 | EFI_SYSTEM_TABLE_64 *gST64 = NULL;␊ |
127 | Node *gEfiConfigurationTableNode = NULL;␊ |
128 | ␊ |
129 | // ==========================================================================␊ |
130 | ␊ |
131 | extern EFI_STATUS addConfigurationTable(EFI_GUID const *pGuid, void *table, char const *alias)␊ |
132 | {␊ |
133 | ␉EFI_UINTN i = 0;␊ |
134 | ␊ |
135 | ␉//Azi: as is, cpu's with em64t will use EFI64 on pre 10.6 systems,␊ |
136 | ␉// wich seems to cause no problem. In case it does, force i386 arch.␊ |
137 | ␉if (archCpuType == CPU_TYPE_I386)␊ |
138 | ␉{␊ |
139 | ␉␉i = gST32->NumberOfTableEntries;␊ |
140 | ␉}␊ |
141 | ␉else␊ |
142 | ␉{␊ |
143 | ␉␉i = gST64->NumberOfTableEntries;␊ |
144 | ␉}␊ |
145 | ␊ |
146 | ␉// We only do adds, not modifications and deletes like InstallConfigurationTable␊ |
147 | ␉if (i >= MAX_CONFIGURATION_TABLE_ENTRIES)␊ |
148 | ␉{␊ |
149 | ␉␉stop("Fake EFI [ERROR]: Ran out of space for configuration tables [%d]. Increase the reserved size in the code.\n", i);␊ |
150 | ␉}␊ |
151 | ␊ |
152 | ␉if (pGuid == NULL)␊ |
153 | ␉{␊ |
154 | ␉␉return EFI_INVALID_PARAMETER;␊ |
155 | ␉}␊ |
156 | ␊ |
157 | ␉if (table != NULL)␊ |
158 | ␉{␊ |
159 | ␉␉// FIXME␊ |
160 | ␉␉//((EFI_CONFIGURATION_TABLE_64 *)gST->ConfigurationTable)[i].VendorGuid = *pGuid;␊ |
161 | ␉␉//((EFI_CONFIGURATION_TABLE_64 *)gST->ConfigurationTable)[i].VendorTable = (EFI_PTR64)table;␊ |
162 | ␊ |
163 | ␉␉//++gST->NumberOfTableEntries;␊ |
164 | ␊ |
165 | ␉␉Node *tableNode = DT__AddChild(gEfiConfigurationTableNode, mallocStringForGuid(pGuid));␊ |
166 | ␊ |
167 | ␉␉// Use the pointer to the GUID we just stuffed into the system table␊ |
168 | ␉␉DT__AddProperty(tableNode, "guid", sizeof(EFI_GUID), (void *)pGuid);␊ |
169 | ␊ |
170 | ␉␉// The "table" property is the 32-bit (in our implementation) physical address of the table␊ |
171 | ␉␉DT__AddProperty(tableNode, "table", sizeof(void *) * 2, table);␊ |
172 | ␊ |
173 | ␉␉// Assume the alias pointer is a global or static piece of data␊ |
174 | ␉␉if (alias != NULL)␊ |
175 | ␉␉{␊ |
176 | ␉␉␉DT__AddProperty(tableNode, "alias", strlen(alias)+1, (char *)alias);␊ |
177 | ␉␉}␊ |
178 | ␊ |
179 | ␉␉return EFI_SUCCESS;␊ |
180 | ␉}␊ |
181 | ␉return EFI_UNSUPPORTED;␊ |
182 | }␊ |
183 | ␊ |
184 | // ==========================================================================␊ |
185 | ␊ |
186 | //Azi: crc32 done in place, on the cases were it wasn't.␊ |
187 | /*static inline void fixupEfiSystemTableCRC32(EFI_SYSTEM_TABLE_64 *efiSystemTable)␊ |
188 | {␊ |
189 | ␉efiSystemTable->Hdr.CRC32 = 0;␊ |
190 | ␉efiSystemTable->Hdr.CRC32 = crc32(0L, efiSystemTable, efiSystemTable->Hdr.HeaderSize);␊ |
191 | }*/␊ |
192 | ␊ |
193 | /*␊ |
194 | * What we do here is simply allocate a fake EFI system table and a fake EFI␊ |
195 | * runtime services table.␊ |
196 | *␊ |
197 | * Because we build against modern headers with kBootArgsRevision 4 we␊ |
198 | * also take care to set efiMode = 32.␊ |
199 | */␊ |
200 | void setupEfiTables32(void)␊ |
201 | {␊ |
202 | ␉// We use the fake_efi_pages struct so that we only need to do one kernel␊ |
203 | ␉// memory allocation for all needed EFI data. Otherwise, small allocations␊ |
204 | ␉// like the FIRMWARE_VENDOR string would take up an entire page.␊ |
205 | ␉// NOTE WELL: Do NOT assume this struct has any particular layout within itself.␊ |
206 | ␉// It is absolutely not intended to be publicly exposed anywhere␊ |
207 | ␉// We say pages (plural) although right now we are well within the 1 page size␊ |
208 | ␉// and probably will stay that way.␊ |
209 | ␉struct fake_efi_pages␊ |
210 | ␉{␊ |
211 | ␉␉EFI_SYSTEM_TABLE_32 efiSystemTable;␊ |
212 | ␉␉EFI_RUNTIME_SERVICES_32 efiRuntimeServices;␊ |
213 | ␉␉EFI_CONFIGURATION_TABLE_32 efiConfigurationTable[MAX_CONFIGURATION_TABLE_ENTRIES];␊ |
214 | ␉␉EFI_CHAR16 firmwareVendor[sizeof(FIRMWARE_VENDOR)/sizeof(EFI_CHAR16)];␊ |
215 | ␉␉uint8_t voidret_instructions[sizeof(VOIDRET_INSTRUCTIONS)/sizeof(uint8_t)];␊ |
216 | ␉␉uint8_t unsupportedret_instructions[sizeof(UNSUPPORTEDRET_INSTRUCTIONS_32)/sizeof(uint8_t)];␊ |
217 | ␉};␊ |
218 | ␊ |
219 | ␉struct fake_efi_pages *fakeEfiPages = (struct fake_efi_pages *)AllocateKernelMemory(sizeof(struct fake_efi_pages));␊ |
220 | ␊ |
221 | ␉// Zero out all the tables in case fields are added later␊ |
222 | ␉//bzero(fakeEfiPages, sizeof(struct fake_efi_pages));␊ |
223 | ␉␊ |
224 | ␉// --------------------------------------------------------------------␊ |
225 | ␉// Initialize some machine code that will return EFI_UNSUPPORTED for␊ |
226 | ␉// functions returning int and simply return for void functions.␊ |
227 | ␉memcpy(fakeEfiPages->voidret_instructions, VOIDRET_INSTRUCTIONS, sizeof(VOIDRET_INSTRUCTIONS));␊ |
228 | ␉memcpy(fakeEfiPages->unsupportedret_instructions, UNSUPPORTEDRET_INSTRUCTIONS_32, sizeof(UNSUPPORTEDRET_INSTRUCTIONS_32));␊ |
229 | ␉␊ |
230 | ␉// --------------------------------------------------------------------␊ |
231 | ␉// System table␊ |
232 | ␉EFI_SYSTEM_TABLE_32 *efiSystemTable = gST32 = &fakeEfiPages->efiSystemTable;␊ |
233 | ␉efiSystemTable->Hdr.Signature = EFI_SYSTEM_TABLE_SIGNATURE;␊ |
234 | ␉efiSystemTable->Hdr.Revision = EFI_SYSTEM_TABLE_REVISION;␊ |
235 | ␉efiSystemTable->Hdr.HeaderSize = sizeof(EFI_SYSTEM_TABLE_32);␊ |
236 | ␉efiSystemTable->Hdr.CRC32 = 0; // Initialize to zero and then do CRC32␊ |
237 | ␉efiSystemTable->Hdr.Reserved = 0;␊ |
238 | ␉␊ |
239 | ␉efiSystemTable->FirmwareVendor = (EFI_PTR32)&fakeEfiPages->firmwareVendor;␊ |
240 | ␉memcpy(fakeEfiPages->firmwareVendor, FIRMWARE_VENDOR, sizeof(FIRMWARE_VENDOR));␊ |
241 | ␉efiSystemTable->FirmwareRevision = FIRMWARE_REVISION;␊ |
242 | ␉␊ |
243 | ␉// XXX: We may need to have basic implementations of ConIn/ConOut/StdErr␊ |
244 | ␉// The EFI spec states that all handles are invalid after boot services have been␊ |
245 | ␉// exited so we can probably get by with leaving the handles as zero.␊ |
246 | ␉efiSystemTable->ConsoleInHandle = 0;␊ |
247 | ␉efiSystemTable->ConIn = 0;␊ |
248 | ␊ |
249 | ␉efiSystemTable->ConsoleOutHandle = 0;␊ |
250 | ␉efiSystemTable->ConOut = 0;␊ |
251 | ␊ |
252 | ␉efiSystemTable->StandardErrorHandle = 0;␊ |
253 | ␉efiSystemTable->StdErr = 0;␊ |
254 | ␊ |
255 | ␉efiSystemTable->RuntimeServices = (EFI_PTR32)&fakeEfiPages->efiRuntimeServices;␊ |
256 | ␊ |
257 | ␉// According to the EFI spec, BootServices aren't valid after the␊ |
258 | ␉// boot process is exited so we can probably do without it.␊ |
259 | ␉// Apple didn't provide a definition for it in pexpert/i386/efi.h␊ |
260 | ␉// so I'm guessing they don't use it.␊ |
261 | ␉efiSystemTable->BootServices = 0;␊ |
262 | ␊ |
263 | ␉efiSystemTable->NumberOfTableEntries = 0;␊ |
264 | ␉efiSystemTable->ConfigurationTable = (EFI_PTR32)fakeEfiPages->efiConfigurationTable;␊ |
265 | ␊ |
266 | ␉// We're done. Now CRC32 the thing so the kernel will accept it.␊ |
267 | ␉// Must be initialized to zero before CRC32, done above.␊ |
268 | ␉gST32->Hdr.CRC32 = crc32(0L, gST32, gST32->Hdr.HeaderSize);␊ |
269 | ␊ |
270 | ␉// --------------------------------------------------------------------␊ |
271 | ␉// Runtime services␊ |
272 | ␉EFI_RUNTIME_SERVICES_32 *efiRuntimeServices = &fakeEfiPages->efiRuntimeServices;␊ |
273 | ␉efiRuntimeServices->Hdr.Signature = EFI_RUNTIME_SERVICES_SIGNATURE;␊ |
274 | ␉efiRuntimeServices->Hdr.Revision = EFI_RUNTIME_SERVICES_REVISION;␊ |
275 | ␉efiRuntimeServices->Hdr.HeaderSize = sizeof(EFI_RUNTIME_SERVICES_32);␊ |
276 | ␉efiRuntimeServices->Hdr.CRC32 = 0;␊ |
277 | ␉efiRuntimeServices->Hdr.Reserved = 0;␊ |
278 | ␊ |
279 | ␉// There are a number of function pointers in the efiRuntimeServices table.␊ |
280 | ␉// These are the Foundation (e.g. core) services and are expected to be present on␊ |
281 | ␉// all EFI-compliant machines.␉Some kernel extensions (notably AppleEFIRuntime)␊ |
282 | ␉// will call these without checking to see if they are null.␊ |
283 | ␉//␊ |
284 | ␉// We don't really feel like doing an EFI implementation in the bootloader␊ |
285 | ␉// but it is nice if we can at least prevent a complete crash by␊ |
286 | ␉// at least providing some sort of implementation until one can be provided␊ |
287 | ␉// nicely in a kext.␊ |
288 | ␉void (*voidret_fp)() = (void *)fakeEfiPages->voidret_instructions;␊ |
289 | ␉void (*unsupportedret_fp)() = (void *)fakeEfiPages->unsupportedret_instructions;␊ |
290 | ␉efiRuntimeServices->GetTime = (EFI_PTR32)unsupportedret_fp;␊ |
291 | ␉efiRuntimeServices->SetTime = (EFI_PTR32)unsupportedret_fp;␊ |
292 | ␉efiRuntimeServices->GetWakeupTime = (EFI_PTR32)unsupportedret_fp;␊ |
293 | ␉efiRuntimeServices->SetWakeupTime = (EFI_PTR32)unsupportedret_fp;␊ |
294 | ␉efiRuntimeServices->SetVirtualAddressMap = (EFI_PTR32)unsupportedret_fp;␊ |
295 | ␉efiRuntimeServices->ConvertPointer = (EFI_PTR32)unsupportedret_fp;␊ |
296 | ␉efiRuntimeServices->GetVariable = (EFI_PTR32)unsupportedret_fp;␊ |
297 | ␉efiRuntimeServices->GetNextVariableName = (EFI_PTR32)unsupportedret_fp;␊ |
298 | ␉efiRuntimeServices->SetVariable = (EFI_PTR32)unsupportedret_fp;␊ |
299 | ␉efiRuntimeServices->GetNextHighMonotonicCount = (EFI_PTR32)unsupportedret_fp;␊ |
300 | ␉efiRuntimeServices->ResetSystem = (EFI_PTR32)voidret_fp;␊ |
301 | ␊ |
302 | ␉// We're done.␉Now CRC32 the thing so the kernel will accept it␊ |
303 | ␉efiRuntimeServices->Hdr.CRC32 = crc32(0L, efiRuntimeServices, efiRuntimeServices->Hdr.HeaderSize);␊ |
304 | ␊ |
305 | ␉// --------------------------------------------------------------------␊ |
306 | ␉// Finish filling in the rest of the boot args that we need.␊ |
307 | ␉bootArgs->efiSystemTable = (uint32_t)efiSystemTable;␊ |
308 | ␉bootArgs->efiMode = kBootArgsEfiMode32;␊ |
309 | ␊ |
310 | ␉// The bootArgs structure as a whole is bzero'd so we don't need to fill in␊ |
311 | ␉// things like efiRuntimeServices* and what not.␊ |
312 | ␉//␊ |
313 | ␉// In fact, the only code that seems to use that is the hibernate code so it␊ |
314 | ␉// knows not to save the pages.␉ It even checks to make sure its nonzero.␊ |
315 | }␊ |
316 | ␊ |
317 | void setupEfiTables64(void)␊ |
318 | {␊ |
319 | ␉struct fake_efi_pages␊ |
320 | ␉{␊ |
321 | ␉␉EFI_SYSTEM_TABLE_64 efiSystemTable;␊ |
322 | ␉␉EFI_RUNTIME_SERVICES_64 efiRuntimeServices;␊ |
323 | ␉␉EFI_CONFIGURATION_TABLE_64 efiConfigurationTable[MAX_CONFIGURATION_TABLE_ENTRIES];␊ |
324 | ␉␉EFI_CHAR16 firmwareVendor[sizeof(FIRMWARE_VENDOR)/sizeof(EFI_CHAR16)];␊ |
325 | ␉␉uint8_t voidret_instructions[sizeof(VOIDRET_INSTRUCTIONS)/sizeof(uint8_t)];␊ |
326 | ␉␉uint8_t unsupportedret_instructions[sizeof(UNSUPPORTEDRET_INSTRUCTIONS_64)/sizeof(uint8_t)];␊ |
327 | ␉};␊ |
328 | ␊ |
329 | ␉struct fake_efi_pages *fakeEfiPages = (struct fake_efi_pages *)AllocateKernelMemory(sizeof(struct fake_efi_pages));␊ |
330 | ␊ |
331 | ␉// Zero out all the tables in case fields are added later␊ |
332 | ␉//bzero(fakeEfiPages, sizeof(struct fake_efi_pages));␊ |
333 | ␊ |
334 | ␉// --------------------------------------------------------------------␊ |
335 | ␉// Initialize some machine code that will return EFI_UNSUPPORTED for␊ |
336 | ␉// functions returning int and simply return for void functions.␊ |
337 | ␉memcpy(fakeEfiPages->voidret_instructions, VOIDRET_INSTRUCTIONS, sizeof(VOIDRET_INSTRUCTIONS));␊ |
338 | ␉memcpy(fakeEfiPages->unsupportedret_instructions, UNSUPPORTEDRET_INSTRUCTIONS_64, sizeof(UNSUPPORTEDRET_INSTRUCTIONS_64));␊ |
339 | ␊ |
340 | ␉// --------------------------------------------------------------------␊ |
341 | ␉// System table␊ |
342 | ␉EFI_SYSTEM_TABLE_64 *efiSystemTable = gST64 = &fakeEfiPages->efiSystemTable;␊ |
343 | ␉efiSystemTable->Hdr.Signature = EFI_SYSTEM_TABLE_SIGNATURE;␊ |
344 | ␉efiSystemTable->Hdr.Revision = EFI_SYSTEM_TABLE_REVISION;␊ |
345 | ␉efiSystemTable->Hdr.HeaderSize = sizeof(EFI_SYSTEM_TABLE_64);␊ |
346 | ␉efiSystemTable->Hdr.CRC32 = 0; // Initialize to zero and then do CRC32␊ |
347 | ␉efiSystemTable->Hdr.Reserved = 0;␊ |
348 | ␊ |
349 | ␉efiSystemTable->FirmwareVendor = ptov64((EFI_PTR32)&fakeEfiPages->firmwareVendor);␊ |
350 | ␉memcpy(fakeEfiPages->firmwareVendor, FIRMWARE_VENDOR, sizeof(FIRMWARE_VENDOR));␊ |
351 | ␉efiSystemTable->FirmwareRevision = FIRMWARE_REVISION;␊ |
352 | ␊ |
353 | ␉// XXX: We may need to have basic implementations of ConIn/ConOut/StdErr␊ |
354 | ␉// The EFI spec states that all handles are invalid after boot services have been␊ |
355 | ␉// exited so we can probably get by with leaving the handles as zero.␊ |
356 | ␉efiSystemTable->ConsoleInHandle = 0;␊ |
357 | ␉efiSystemTable->ConIn = 0;␊ |
358 | ␊ |
359 | ␉efiSystemTable->ConsoleOutHandle = 0;␊ |
360 | ␉efiSystemTable->ConOut = 0;␊ |
361 | ␊ |
362 | ␉efiSystemTable->StandardErrorHandle = 0;␊ |
363 | ␉efiSystemTable->StdErr = 0;␊ |
364 | ␊ |
365 | ␉efiSystemTable->RuntimeServices = ptov64((EFI_PTR32)&fakeEfiPages->efiRuntimeServices);␊ |
366 | ␉// According to the EFI spec, BootServices aren't valid after the␊ |
367 | ␉// boot process is exited so we can probably do without it.␊ |
368 | ␉// Apple didn't provide a definition for it in pexpert/i386/efi.h␊ |
369 | ␉// so I'm guessing they don't use it.␊ |
370 | ␉efiSystemTable->BootServices = 0;␊ |
371 | ␊ |
372 | ␉efiSystemTable->NumberOfTableEntries = 0;␊ |
373 | ␉efiSystemTable->ConfigurationTable = ptov64((EFI_PTR32)fakeEfiPages->efiConfigurationTable);␊ |
374 | ␊ |
375 | ␉// We're done.␉Now CRC32 the thing so the kernel will accept it␊ |
376 | ␉gST64->Hdr.CRC32 = crc32(0L, gST64, gST64->Hdr.HeaderSize);␊ |
377 | ␊ |
378 | ␉// --------------------------------------------------------------------␊ |
379 | ␉// Runtime services␊ |
380 | ␉EFI_RUNTIME_SERVICES_64 *efiRuntimeServices = &fakeEfiPages->efiRuntimeServices;␊ |
381 | ␉efiRuntimeServices->Hdr.Signature = EFI_RUNTIME_SERVICES_SIGNATURE;␊ |
382 | ␉efiRuntimeServices->Hdr.Revision = EFI_RUNTIME_SERVICES_REVISION;␊ |
383 | ␉efiRuntimeServices->Hdr.HeaderSize = sizeof(EFI_RUNTIME_SERVICES_64);␊ |
384 | ␉efiRuntimeServices->Hdr.CRC32 = 0;␊ |
385 | ␉efiRuntimeServices->Hdr.Reserved = 0;␊ |
386 | ␊ |
387 | ␉// There are a number of function pointers in the efiRuntimeServices table.␊ |
388 | ␉// These are the Foundation (e.g. core) services and are expected to be present on␊ |
389 | ␉// all EFI-compliant machines.␉Some kernel extensions (notably AppleEFIRuntime)␊ |
390 | ␉// will call these without checking to see if they are null.␊ |
391 | ␉//␊ |
392 | ␉// We don't really feel like doing an EFI implementation in the bootloader␊ |
393 | ␉// but it is nice if we can at least prevent a complete crash by␊ |
394 | ␉// at least providing some sort of implementation until one can be provided␊ |
395 | ␉// nicely in a kext.␊ |
396 | ␊ |
397 | ␉void (*voidret_fp)() = (void *)fakeEfiPages->voidret_instructions;␊ |
398 | ␉void (*unsupportedret_fp)() = (void *)fakeEfiPages->unsupportedret_instructions;␊ |
399 | ␉efiRuntimeServices->GetTime = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
400 | ␉efiRuntimeServices->SetTime = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
401 | ␉efiRuntimeServices->GetWakeupTime = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
402 | ␉efiRuntimeServices->SetWakeupTime = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
403 | ␉efiRuntimeServices->SetVirtualAddressMap = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
404 | ␉efiRuntimeServices->ConvertPointer = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
405 | ␉efiRuntimeServices->GetVariable = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
406 | ␉efiRuntimeServices->GetNextVariableName = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
407 | ␉efiRuntimeServices->SetVariable = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
408 | ␉efiRuntimeServices->GetNextHighMonotonicCount = ptov64((EFI_PTR32)unsupportedret_fp);␊ |
409 | ␉efiRuntimeServices->ResetSystem = ptov64((EFI_PTR32)voidret_fp);␊ |
410 | ␊ |
411 | ␉// We're done.␉Now CRC32 the thing so the kernel will accept it␊ |
412 | ␉efiRuntimeServices->Hdr.CRC32 = crc32(0L, efiRuntimeServices, efiRuntimeServices->Hdr.HeaderSize);␊ |
413 | ␊ |
414 | ␉// --------------------------------------------------------------------␊ |
415 | ␉// Finish filling in the rest of the boot args that we need.␊ |
416 | ␉bootArgs->efiSystemTable = (uint32_t)efiSystemTable;␊ |
417 | ␉bootArgs->efiMode = kBootArgsEfiMode64;␊ |
418 | ␊ |
419 | ␉// The bootArgs structure as a whole is bzero'd so we don't need to fill in␊ |
420 | ␉// things like efiRuntimeServices* and what not.␊ |
421 | ␉//␊ |
422 | ␉// In fact, the only code that seems to use that is the hibernate code so it␊ |
423 | ␉// knows not to save the pages.␉ It even checks to make sure its nonzero.␊ |
424 | }␊ |
425 | ␊ |
426 | /*␊ |
427 | * In addition to the EFI tables there is also the EFI device tree node.␊ |
428 | * In particular, we need /efi/platform to have an FSBFrequency key. Without it,␊ |
429 | * the tsc_init function will panic very early on in kernel startup, before␊ |
430 | * the console is available.␊ |
431 | */␊ |
432 | ␊ |
433 | /*==========================================================================␊ |
434 | * FSB Frequency detection␊ |
435 | */␊ |
436 | ␊ |
437 | /* These should be const but DT__AddProperty takes char* */␊ |
438 | static const char TSC_Frequency_prop[] = "TSCFrequency";␊ |
439 | static const char FSB_Frequency_prop[] = "FSBFrequency";␊ |
440 | static const char CPU_Frequency_prop[] = "CPUFrequency";␊ |
441 | ␊ |
442 | /*==========================================================================␊ |
443 | * SMBIOS␊ |
444 | */␊ |
445 | ␊ |
446 | /* From Foundation/Efi/Guid/Smbios/SmBios.c */␊ |
447 | EFI_GUID const gEfiSmbiosTableGuid = EFI_SMBIOS_TABLE_GUID;␊ |
448 | ␊ |
449 | #define SMBIOS_RANGE_START␉␉0x000F0000␊ |
450 | #define SMBIOS_RANGE_END␉␉0x000FFFFF␊ |
451 | ␊ |
452 | /* '_SM_' in little endian: */␊ |
453 | #define SMBIOS_ANCHOR_UINT32_LE 0x5f4d535f␊ |
454 | ␊ |
455 | EFI_GUID gEfiAcpiTableGuid = EFI_ACPI_TABLE_GUID;␊ |
456 | EFI_GUID gEfiAcpi20TableGuid = EFI_ACPI_20_TABLE_GUID;␊ |
457 | ␊ |
458 | ␊ |
459 | /*==========================================================================␊ |
460 | * Fake EFI implementation␊ |
461 | */␊ |
462 | ␊ |
463 | /* These should be const but DT__AddProperty takes char* */␊ |
464 | static const char FIRMWARE_REVISION_PROP[] = "firmware-revision";␊ |
465 | static const char FIRMWARE_ABI_PROP[] = "firmware-abi";␊ |
466 | static const char FIRMWARE_VENDOR_PROP[] = "firmware-vendor";␊ |
467 | static const char FIRMWARE_ABI_32_PROP_VALUE[] = "EFI32";␊ |
468 | static const char FIRMWARE_ABI_64_PROP_VALUE[] = "EFI64";␊ |
469 | static const char EFI_MODE_PROP[] = "efi-mode"; //Bungo␊ |
470 | static const char SYSTEM_ID_PROP[] = "system-id";␊ |
471 | static const char SYSTEM_SERIAL_PROP[] = "SystemSerialNumber";␊ |
472 | static const char SYSTEM_TYPE_PROP[] = "system-type";␊ |
473 | static const char MODEL_PROP[] = "Model";␊ |
474 | static const char BOARDID_PROP[] = "board-id";␊ |
475 | static const char DEV_PATH_SUP[] = "DevicePathsSupported";␊ |
476 | static const char START_POWER_EV[] = "StartupPowerEvents";␊ |
477 | static const char MACHINE_SIG_PROP[] = "machine-signature";␊ |
478 | static EFI_UINT8 const DEVICE_PATHS_SUPPORTED[] = { 0x01, 0x00, 0x00, 0x00 };␊ |
479 | static EFI_UINT8 const STARTUP_POWER_EVENTS[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };␊ |
480 | static EFI_UINT8 const COMPAT_MODE[] = { 0x01, 0x00, 0x00, 0x00 };␊ |
481 | ␊ |
482 | /*␊ |
483 | * Get an smbios option string option to convert to EFI_CHAR16 string␊ |
484 | */␊ |
485 | static EFI_CHAR16 *getSmbiosChar16(const char *key, size_t *len)␊ |
486 | {␊ |
487 | ␉const char␉*src = getStringForKey(key, &bootInfo->smbiosConfig);␊ |
488 | ␉EFI_CHAR16␉*dst = 0;␊ |
489 | ␉size_t␉␉ i = 0;␊ |
490 | ␊ |
491 | ␉if (!key || !(*key) || !len || !src)␊ |
492 | ␉{␊ |
493 | ␉␉return 0;␊ |
494 | ␉}␊ |
495 | ␉␊ |
496 | ␉*len = strlen(src);␊ |
497 | ␉dst = (EFI_CHAR16 *) malloc( ((*len)+1) * 2 );␊ |
498 | ␉for (; i < (*len); i++)␊ |
499 | ␉{␊ |
500 | ␉␉dst[i] = src[i];␊ |
501 | ␉}␊ |
502 | ␉dst[(*len)] = '\0';␊ |
503 | ␉*len = ((*len)+1)*2; // return the CHAR16 bufsize including zero terminated CHAR16␊ |
504 | ␉return dst;␊ |
505 | }␊ |
506 | ␊ |
507 | /*␊ |
508 | * Must be called AFTER setupAcpi because we need to take care of correct␊ |
509 | * FACP content to reflect in ioregs␊ |
510 | */␊ |
511 | void setupSystemType()␊ |
512 | {␊ |
513 | ␉Node *node = DT__FindNode("/", false);␊ |
514 | ␉if (node == 0)␊ |
515 | ␉{␊ |
516 | ␉␉stop("Couldn't get root '/' node");␊ |
517 | ␉}␊ |
518 | ␉// we need to write this property after facp parsing␊ |
519 | ␉// Export system-type only if it has been overrriden by the SystemType option␊ |
520 | ␉DT__AddProperty(node, SYSTEM_TYPE_PROP, sizeof(Platform.Type), &Platform.Type);␊ |
521 | }␊ |
522 | ␊ |
523 | static void setupEfiDeviceTree(void)␊ |
524 | {␊ |
525 | ␉// EFI_CHAR8␉*ret = 0; Bungo: not used␊ |
526 | ␉EFI_CHAR16␉*ret16 = 0;␊ |
527 | ␉size_t␉␉ len = 0;␊ |
528 | ␉Node␉␉*node;␊ |
529 | ␊ |
530 | ␉node = DT__FindNode("/", false);␊ |
531 | ␊ |
532 | ␉if (node == 0)␊ |
533 | ␉{␊ |
534 | ␉␉stop("Couldn't get root node");␊ |
535 | ␉}␊ |
536 | ␊ |
537 | ␉// We could also just do DT__FindNode("/efi/platform", true)␊ |
538 | ␉// But I think eventually we want to fill stuff in the efi node␊ |
539 | ␉// too so we might as well create it so we have a pointer for it too.␊ |
540 | ␉node = DT__AddChild(node, "efi");␊ |
541 | ␊ |
542 | ␉if (archCpuType == CPU_TYPE_I386)␊ |
543 | ␉{␊ |
544 | ␉␉DT__AddProperty(node, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_32_PROP_VALUE), (char *)FIRMWARE_ABI_32_PROP_VALUE);␊ |
545 | ␉}␊ |
546 | ␉else␊ |
547 | ␉{␊ |
548 | ␉␉DT__AddProperty(node, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_64_PROP_VALUE), (char *)FIRMWARE_ABI_64_PROP_VALUE);␊ |
549 | ␉}␊ |
550 | ␊ |
551 | ␉DT__AddProperty(node, EFI_MODE_PROP, sizeof(EFI_UINT8), (EFI_UINT8 *)&bootArgs->efiMode);␊ |
552 | ␊ |
553 | ␉DT__AddProperty(node, FIRMWARE_REVISION_PROP, sizeof(FIRMWARE_REVISION), (EFI_UINT32 *)&FIRMWARE_REVISION);␊ |
554 | ␉DT__AddProperty(node, FIRMWARE_VENDOR_PROP, sizeof(FIRMWARE_VENDOR), (EFI_CHAR16 *)FIRMWARE_VENDOR);␊ |
555 | ␊ |
556 | ␉// TODO: Fill in other efi properties if necessary␊ |
557 | ␊ |
558 | ␉// Set up the /efi/runtime-services table node similar to the way a child node of configuration-table␊ |
559 | ␉// is set up. That is, name and table properties␊ |
560 | ␉Node *runtimeServicesNode = DT__AddChild(node, "runtime-services");␊ |
561 | ␊ |
562 | ␉if (archCpuType == CPU_TYPE_I386)␊ |
563 | ␉{␊ |
564 | ␉␉// The value of the table property is the 32-bit physical address for the RuntimeServices table.␊ |
565 | ␉␉// Since the EFI system table already has a pointer to it, we simply use the address of that pointer␊ |
566 | ␉␉// for the pointer to the property data. Warning.. DT finalization calls free on that but we're not␊ |
567 | ␉␉// the only thing to use a non-malloc'd pointer for something in the DT␊ |
568 | ␊ |
569 | ␉␉DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST32->RuntimeServices);␊ |
570 | ␉}␊ |
571 | ␉else␊ |
572 | ␉{␊ |
573 | ␉␉DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST64->RuntimeServices);␊ |
574 | ␉}␊ |
575 | ␊ |
576 | ␉// Set up the /efi/configuration-table node which will eventually have several child nodes for␊ |
577 | ␉// all of the configuration tables needed by various kernel extensions.␊ |
578 | ␉gEfiConfigurationTableNode = DT__AddChild(node, "configuration-table");␊ |
579 | ␊ |
580 | ␉// New node: /efi/kernel-compatibility␊ |
581 | ␉Node *efiKernelComNode = DT__AddChild(node, "kernel-compatibility");␊ |
582 | ␊ |
583 | ␉if (MacOSVerCurrent >= MacOSVer2Int("10.9"))␊ |
584 | ␉{␊ |
585 | ␉␉DT__AddProperty(efiKernelComNode, "x86_64", sizeof(COMPAT_MODE), (EFI_UINT8 *) &COMPAT_MODE);␊ |
586 | ␉}␊ |
587 | ␉else␊ |
588 | ␉{␊ |
589 | ␉␉DT__AddProperty(efiKernelComNode, "i386", sizeof(COMPAT_MODE), (EFI_UINT8 *) &COMPAT_MODE);␊ |
590 | ␉␉DT__AddProperty(efiKernelComNode, "x86_64", sizeof(COMPAT_MODE), (EFI_UINT8 *) &COMPAT_MODE);␊ |
591 | ␉}␊ |
592 | ␊ |
593 | ␉// Now fill in the /efi/platform Node␊ |
594 | ␉Node *efiPlatformNode = DT__AddChild(node, "platform"); // "/efi/platform"␊ |
595 | ␊ |
596 | ␉// NOTE WELL: If you do add FSB Frequency detection, make sure to store␊ |
597 | ␉// the value in the fsbFrequency global and not an malloc'd pointer␊ |
598 | ␉// because the DT_AddProperty function does not copy its args.␊ |
599 | ␊ |
600 | ␉if (Platform.CPU.FSBFrequency != 0)␊ |
601 | ␉{␊ |
602 | ␉␉DT__AddProperty(efiPlatformNode, FSB_Frequency_prop, sizeof(uint64_t), &Platform.CPU.FSBFrequency);␊ |
603 | ␉}␊ |
604 | ␊ |
605 | ␉// Export TSC and CPU frequencies for use by the kernel or KEXTs␊ |
606 | ␉if (Platform.CPU.TSCFrequency != 0)␊ |
607 | ␉{␊ |
608 | ␉␉DT__AddProperty(efiPlatformNode, TSC_Frequency_prop, sizeof(uint64_t), &Platform.CPU.TSCFrequency);␊ |
609 | ␉}␊ |
610 | ␊ |
611 | ␉if (Platform.CPU.CPUFrequency != 0)␊ |
612 | ␉{␊ |
613 | ␉␉DT__AddProperty(efiPlatformNode, CPU_Frequency_prop, sizeof(uint64_t), &Platform.CPU.CPUFrequency);␊ |
614 | ␉}␊ |
615 | ␊ |
616 | ␉DT__AddProperty(efiPlatformNode,START_POWER_EV, sizeof(STARTUP_POWER_EVENTS), (EFI_UINT8 *) &STARTUP_POWER_EVENTS);␊ |
617 | ␊ |
618 | ␉DT__AddProperty(efiPlatformNode,DEV_PATH_SUP, sizeof(DEVICE_PATHS_SUPPORTED), (EFI_UINT8 *) &DEVICE_PATHS_SUPPORTED);␊ |
619 | ␊ |
620 | ␉// Bungo␊ |
621 | ␉/* Export system-id. Can be disabled with SystemId=No in com.apple.Boot.plist␊ |
622 | ␉if ((ret=getSystemID()))␊ |
623 | ␉{␊ |
624 | ␉␉DT__AddProperty(efiPlatformNode, SYSTEM_ID_PROP, UUID_LEN, (EFI_UINT32 *) ret);␊ |
625 | ␉}␊ |
626 | ␉*/␊ |
627 | ␊ |
628 | ␉DT__AddProperty(efiPlatformNode, SYSTEM_ID_PROP, UUID_LEN, (EFI_UINT32 *)Platform.UUID);␊ |
629 | ␊ |
630 | ␉// Export SystemSerialNumber if present␊ |
631 | ␉if ((ret16=getSmbiosChar16("SMserial", &len)))␊ |
632 | ␉{␊ |
633 | ␉␉DT__AddProperty(efiPlatformNode, SYSTEM_SERIAL_PROP, len, ret16);␊ |
634 | ␉}␊ |
635 | ␊ |
636 | ␉// Export Model if present␊ |
637 | ␉if ((ret16=getSmbiosChar16("SMproductname", &len)))␊ |
638 | ␉{␊ |
639 | ␉␉DT__AddProperty(efiPlatformNode, MODEL_PROP, len, ret16);␊ |
640 | ␉}␊ |
641 | ␊ |
642 | ␉// Fill /efi/device-properties node.␊ |
643 | ␉setupDeviceProperties(node);␊ |
644 | }␊ |
645 | ␊ |
646 | /*␊ |
647 | * Must be called AFTER getSmbios␊ |
648 | */␊ |
649 | void setupBoardId()␊ |
650 | {␊ |
651 | ␉Node *node;␊ |
652 | ␉node = DT__FindNode("/", false);␊ |
653 | ␉if (node == 0)␊ |
654 | ␉{␊ |
655 | ␉␉stop("Couldn't get root '/' node");␊ |
656 | ␉}␊ |
657 | ␉const char *boardid = getStringForKey("SMboardproduct", &bootInfo->smbiosConfig); // SMboardserial␊ |
658 | ␉if (boardid)␊ |
659 | ␉{␊ |
660 | ␉␉DT__AddProperty(node, BOARDID_PROP, strlen(boardid)+1, (EFI_CHAR16 *)boardid);␊ |
661 | ␉}␊ |
662 | }␊ |
663 | ␊ |
664 | /*␊ |
665 | * Populate the chosen node␊ |
666 | */␊ |
667 | void setupChosenNode()␊ |
668 | {␊ |
669 | ␉Node *chosenNode;␊ |
670 | ␉chosenNode = DT__FindNode("/chosen", false);␊ |
671 | ␉unsigned long adler32 = 0;␊ |
672 | ␊ |
673 | ␉if (chosenNode == NULL)␊ |
674 | ␉{␊ |
675 | ␉␉stop("setupChosenNode: Couldn't get '/chosen' node");␊ |
676 | ␉}␊ |
677 | ␊ |
678 | ␉// Only accept a UUID with the correct length.␊ |
679 | ␉if (strlen(gBootUUIDString) == 36)␊ |
680 | ␉{␊ |
681 | ␉␉DT__AddProperty(chosenNode, "boot-uuid", 37, gBootUUIDString);␊ |
682 | ␉}␊ |
683 | ␊ |
684 | ␉DT__AddProperty(chosenNode, "boot-args", sizeof(bootArgs->CommandLine), (EFI_UINT8 *)bootArgs->CommandLine);␊ |
685 | ␊ |
686 | ␉// Adding the default kernel name (mach_kernel) for kextcache.␊ |
687 | ␉DT__AddProperty(chosenNode, "boot-file", sizeof(bootInfo->bootFile), bootInfo->bootFile);␊ |
688 | ␊ |
689 | //␉DT__AddProperty(chosenNode, "boot-device-path", bootDPsize, gBootDP);␊ |
690 | ␊ |
691 | //␉DT__AddProperty(chosenNode, "boot-file-path", bootFPsize, gBootFP);␊ |
692 | ␊ |
693 | ␉DT__AddProperty(chosenNode, "boot-kernelcache-adler32", sizeof(unsigned long), &adler32);␊ |
694 | ␊ |
695 | ␉DT__AddProperty(chosenNode, MACHINE_SIG_PROP, sizeof(Platform.HWSignature), (EFI_UINT32 *)&Platform.HWSignature);␊ |
696 | ␊ |
697 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.10") ) // Yosemite+␊ |
698 | ␉{␊ |
699 | ␉␉//␊ |
700 | ␉␉// Pike R. Alpha - 12 October 2014␊ |
701 | ␉␉//␊ |
702 | ␉␉UInt8 index = 0;␊ |
703 | ␉␉EFI_UINT16 PMTimerValue = 0, PMRepeatCount = 0xffff;␊ |
704 | ␊ |
705 | #if RANDOMSEED␊ |
706 | ␉␉EFI_UINT32 randomValue = 0, cpuTick = 0;␊ |
707 | ␉␉EFI_UINT32 ecx = 0, edx = 0, esi = 0, edi = 0;␊ |
708 | #else␊ |
709 | ␉␉EFI_UINT32 randomValue, tempValue, cpuTick;␊ |
710 | ␉␉EFI_UINT32 ecx, esi, edi = 0;␊ |
711 | ␉␉EFI_UINT64 rcx, rdx, rsi, rdi;␊ |
712 | ␊ |
713 | ␉␉randomValue = tempValue = ecx = esi = edi = 0;␉␉␉␉␉// xor␉␉%ecx,␉%ecx␊ |
714 | ␉␉cpuTick = rcx = rdx = rsi = rdi = 0;␊ |
715 | #endif␊ |
716 | ␉␉// LEAF_1 - Feature Information (Function 01h).␊ |
717 | ␉␉if (Platform.CPU.CPUID[CPUID_1][2] & 0x40000000)␉␉␉␉// Checking ecx:bit-30␊ |
718 | ␉␉{␊ |
719 | ␉␉␉//␊ |
720 | ␉␉␉// i5/i7 Ivy Bridge and Haswell processors with RDRAND support.␊ |
721 | ␉␉␉//␊ |
722 | ␉␉␉EFI_UINT32 seedBuffer[16] = {0};␊ |
723 | ␉␉␉//␊ |
724 | ␉␉␉// Main loop to get 16 dwords (four bytes each).␊ |
725 | ␉␉␉//␊ |
726 | ␉␉␉for (index = 0; index < 16; index++)␉␉␉␉␉// 0x17e12:␊ |
727 | ␉␉␉{␊ |
728 | ␉␉␉␉randomValue = computeRand();␉␉␉␉␉// callq␉0x18e20␊ |
729 | ␉␉␉␉cpuTick = (EFI_UINT32) getCPUTick();␉␉␉␉// callq␉0x121a7␊ |
730 | ␉␉␉␉randomValue = (randomValue ^ cpuTick);␉␉␉␉// xor␉␉%rdi,␉%rax␊ |
731 | ␉␉␉␉seedBuffer[index] = randomValue;␉␉␉␉// mov␉␉%rax,(%r15,%rsi,8)␊ |
732 | ␉␉␉}␉␉␉␉␉␉␉␉␉// jb␉␉0x17e12␊ |
733 | ␊ |
734 | ␉␉␉DT__AddProperty(chosenNode, "random-seed", sizeof(seedBuffer), (EFI_UINT32 *) &seedBuffer);␊ |
735 | ␉␉}␊ |
736 | ␉␉else␊ |
737 | ␉␉{␊ |
738 | ␉␉␉//␊ |
739 | ␉␉␉// All other processors without RDRAND support.␊ |
740 | ␉␉␉//␊ |
741 | ␉␉␉EFI_UINT8 seedBuffer[64] = {0};␊ |
742 | ␉␉␉//␊ |
743 | ␉␉␉// Main loop to get the 64 bytes.␊ |
744 | ␉␉␉//␊ |
745 | ␉␉␉do␉␉␉␉␉␉␉␉␉// 0x17e55:␊ |
746 | ␉␉␉{␊ |
747 | ␉␉␉␉//␊ |
748 | ␉␉␉␉// FIXME: PM Timer is usually @ 0x408, but its position is relocatable␊ |
749 | ␉␉␉␉// via PCI-to-ISA bridge. The location is reported in ACPI FADT,␊ |
750 | ␉␉␉␉// PM Timer Block address - zenith432␊ |
751 | ␉␉␉␉//␊ |
752 | ␉␉␉␉PMTimerValue = inw(0x408);␉␉␉␉␉// in␉␉(%dx),␉%ax␊ |
753 | ␉␉␉␉esi = PMTimerValue;␉␉␉␉␉␉// movzwl␉%ax,␉%esi␊ |
754 | ␊ |
755 | ␉␉␉␉if (esi < ecx)␉␉␉␉␉␉␉// cmp␉␉%ecx,␉%esi␊ |
756 | ␉␉␉␉{␊ |
757 | ␉␉␉␉␉/*␊ |
758 | ␉␉␉␉␉ * This is a workaround to prevent an infinite loop␊ |
759 | ␉␉␉␉␉ * if PMTimer is not at port 0x408 - zenith432␊ |
760 | ␉␉␉␉␉ */␊ |
761 | ␉␉␉␉␉if (PMRepeatCount)␊ |
762 | ␉␉␉␉␉{␊ |
763 | ␉␉␉␉␉␉--PMRepeatCount;␊ |
764 | ␉␉␉␉␉␉continue;␉␉␉␉␉␉// jb␉␉0x17e55␉␉(retry)␊ |
765 | ␉␉␉␉␉}␊ |
766 | ␉␉␉␉}␊ |
767 | ␉␉␉␉else␊ |
768 | ␉␉␉␉{␊ |
769 | ␉␉␉␉␉PMRepeatCount = 0xffff;␊ |
770 | ␉␉␉␉}␊ |
771 | ␊ |
772 | ␉␉␉␉cpuTick = (EFI_UINT32) getCPUTick();␉␉␉␉// callq␉0x121a7␊ |
773 | //␉␉␉␉printf("value: 0x%x\n", getCPUTick());␊ |
774 | ␊ |
775 | #if RANDOMSEED␊ |
776 | ␉␉␉␉ecx = (cpuTick >> 8);␉␉␉␉␉␉// mov␉␉%rax,␉%rcx␊ |
777 | ␉␉␉␉// shr␉␉$0x8,␉%rcx␊ |
778 | ␉␉␉␉edx = (cpuTick >> 0x10);␉␉␉␉␉// mov␉␉%rax,␉%rdx␊ |
779 | ␉␉␉␉// shr␉␉$0x10,␉%rdx␊ |
780 | ␉␉␉␉edi = esi;␉␉␉␉␉␉␉// mov␉␉%rsi,␉%rdi␊ |
781 | ␉␉␉␉edi = (edi ^ cpuTick);␉␉␉␉␉␉// xor␉␉%rax,␉%rdi␊ |
782 | ␉␉␉␉edi = (edi ^ ecx);␉␉␉␉␉␉// xor␉␉%rcx,␉%rdi␊ |
783 | ␉␉␉␉edi = (edi ^ edx);␉␉␉␉␉␉// xor␉␉%rdx,␉%rdi␊ |
784 | ␊ |
785 | ␉␉␉␉seedBuffer[index] = (edi & 0xff);␊ |
786 | #else␊ |
787 | ␉␉␉␉rcx = (cpuTick >> 8);␉␉␉␉␉␉// mov␉␉%rax,␉%rcx␊ |
788 | ␉␉␉␉// shr␉␉$0x8,␉%rcx␊ |
789 | ␉␉␉␉rdx = (cpuTick >> 0x10);␉␉␉␉␉// mov␉␉%rax,␉%rdx␊ |
790 | ␉␉␉␉// shr␉␉$0x10,␉%rdx␊ |
791 | ␉␉␉␉/*␊ |
792 | ␉␉␉␉ * Note: In x86 assembly, rXX is upper part of eXX register.␊ |
793 | ␉␉␉␉ * In C they're different variables.␊ |
794 | ␉␉␉␉ * The code is identical with or without RANDOMSEED. - zenith432␊ |
795 | ␉␉␉␉ */␊ |
796 | ␉␉␉␉rdi = rsi = esi;␉␉␉␉␉␉// mov␉␉%rsi,␉%rdi␊ |
797 | ␉␉␉␉rdi = (rdi ^ cpuTick);␉␉␉␉␉␉// xor␉␉%rax,␉%rdi␊ |
798 | ␉␉␉␉rdi = (rdi ^ rcx);␉␉␉␉␉␉// xor␉␉%rcx,␉%rdi␊ |
799 | ␉␉␉␉rdi = (rdi ^ rdx);␉␉␉␉␉␉// xor␉␉%rdx,␉%rdi␊ |
800 | ␉␉␉␉edi = (EFI_UINT32) rdi;␊ |
801 | ␊ |
802 | ␉␉␉␉seedBuffer[index] = (rdi & 0xff);␉␉␉␉// mov␉␉%dil,␉(%r15,%r12,1)␊ |
803 | #endif␊ |
804 | ␉␉␉␉edi = (edi & 0x2f);␉␉␉␉␉␉// and␉␉$0x2f,␉%edi␊ |
805 | ␉␉␉␉edi = (edi + esi);␉␉␉␉␉␉// add␉␉%esi,␉%edi␊ |
806 | ␉␉␉␉index++;␉␉␉␉␉␉␉// inc␉␉r12␊ |
807 | ␉␉␉␉ecx = (edi & 0xffff);␉␉␉␉␉␉// movzwl␉%di,␉%ecx␊ |
808 | ␊ |
809 | ␉␉␉} while (index < 64);␉␉␉␉␉␉␉// cmp␉␉%r14d,␉%r12d␊ |
810 | ␉␉␉// jne␉␉0x17e55␉␉(next)␊ |
811 | ␊ |
812 | ␉␉␉DT__AddProperty(chosenNode, "random-seed", sizeof(seedBuffer), (EFI_UINT8 *) &seedBuffer);␊ |
813 | ␊ |
814 | ␉␉}␊ |
815 | ␉}␊ |
816 | ␊ |
817 | ␉// Micky1979 : MIMIC booter entry for El Capitan␊ |
818 | ␉if ( MacOSVerCurrent >= MacOSVer2Int("10.11") ) // El Capitan␊ |
819 | ␉{␊ |
820 | ␉␉verbose("Adding booter spec to the Platform Expert \n");␊ |
821 | ␉␉// booter-build-time (Fri May 22 19:06:42 PDT 2015) DP1␊ |
822 | ␉␉// booter-build-time (Fri Jul 24 17:39:22 PDT 2015) DP7␊ |
823 | ␉␉DT__AddProperty(chosenNode, "booter-build-time", sizeof(I386BOOT_BUILDDATE), I386BOOT_BUILDDATE);␊ |
824 | ␉␉// booter-name (boot.efi)␊ |
825 | ␉␉DT__AddProperty(chosenNode, "booter-name", sizeof("Enoch"), "Enoch");␊ |
826 | ␉␉// booter-version (version:295.0.0.1.1) DP1␊ |
827 | ␉␉// booter-version (version:304) DP7␊ |
828 | ␉␉DT__AddProperty(chosenNode, "booter-version", sizeof(I386BOOT_CHAMELEONREVISION), I386BOOT_CHAMELEONREVISION);␊ |
829 | ␉}␊ |
830 | }␊ |
831 | ␊ |
832 | /*␊ |
833 | * Load the smbios.plist override config file if any␊ |
834 | */␊ |
835 | static void setupSmbiosConfigFile(const char *filename)␊ |
836 | {␊ |
837 | ␉char␉␉dirSpecSMBIOS[128];␊ |
838 | ␉const char␉*override_pathname = NULL;␊ |
839 | ␉int␉␉len = 0, err = 0;␊ |
840 | ␉extern void scan_mem();␊ |
841 | ␊ |
842 | ␉// Take in account user overriding␊ |
843 | ␉if (getValueForKey(kSMBIOSKey, &override_pathname, &len, &bootInfo->chameleonConfig) && len > 0)␊ |
844 | ␉{␊ |
845 | ␉␉// Specify a path to a file, e.g. SMBIOS=/Extra/macProXY.plist␊ |
846 | ␉␉strcpy(dirSpecSMBIOS, override_pathname);␊ |
847 | ␉␉err = loadConfigFile(dirSpecSMBIOS, &bootInfo->smbiosConfig);␊ |
848 | ␉}␊ |
849 | ␉else␊ |
850 | ␉{␊ |
851 | ␉␉// Check selected volume's Extra.␊ |
852 | ␉␉sprintf(dirSpecSMBIOS, "/Extra/%s", filename);␊ |
853 | ␉␉err = loadConfigFile(dirSpecSMBIOS, &bootInfo->smbiosConfig);␊ |
854 | ␉}␊ |
855 | ␊ |
856 | ␉if (err)␊ |
857 | ␉{␊ |
858 | ␉␉verbose("No SMBIOS replacement found.\n");␊ |
859 | ␉}␊ |
860 | ␊ |
861 | ␉// get a chance to scan mem dynamically if user asks for it while having the config options␊ |
862 | ␉// loaded as well, as opposed to when it was in scan_platform(); also load the orig. smbios␊ |
863 | ␉// so that we can access dmi info, without patching the smbios yet.␊ |
864 | ␉scan_mem(); ␊ |
865 | }␊ |
866 | ␊ |
867 | /*␊ |
868 | * Installs all the needed configuration table entries␊ |
869 | */␊ |
870 | static void setupEfiConfigurationTable()␊ |
871 | {␊ |
872 | ␉smbios_p = (EFI_PTR32)getSmbios(SMBIOS_PATCHED);␊ |
873 | ␉addConfigurationTable(&gEfiSmbiosTableGuid, &smbios_p, NULL);␊ |
874 | ␊ |
875 | ␉setupBoardId(); //need to be called after getSmbios␊ |
876 | ␊ |
877 | ␉// Setup ACPI with DSDT overrides (mackerintel's patch)␊ |
878 | ␉setupAcpi();␊ |
879 | ␊ |
880 | ␉// We've obviously changed the count.. so fix up the CRC32␊ |
881 | ␉if (archCpuType == CPU_TYPE_I386)␊ |
882 | ␉{␊ |
883 | ␉␉gST32->Hdr.CRC32 = 0;␊ |
884 | ␉␉gST32->Hdr.CRC32 = crc32(0L, gST32, gST32->Hdr.HeaderSize);␊ |
885 | ␉}␊ |
886 | ␉else␊ |
887 | ␉{␊ |
888 | ␉␉gST64->Hdr.CRC32 = 0;␊ |
889 | ␉␉gST64->Hdr.CRC32 = crc32(0L, gST64, gST64->Hdr.HeaderSize);␊ |
890 | ␉}␊ |
891 | ␊ |
892 | ␉// Setup the chosen node␊ |
893 | ␉setupChosenNode();␊ |
894 | }␊ |
895 | ␊ |
896 | void saveOriginalSMBIOS(void)␊ |
897 | {␊ |
898 | ␉Node *node;␊ |
899 | ␉SMBEntryPoint *origeps;␊ |
900 | ␉void *tableAddress;␊ |
901 | ␊ |
902 | ␉node = DT__FindNode("/efi/platform", false);␊ |
903 | ␉if (!node)␊ |
904 | ␉{␊ |
905 | ␉␉DBG("saveOriginalSMBIOS: '/efi/platform' node not found\n");␊ |
906 | ␉␉return;␊ |
907 | ␉}␊ |
908 | ␊ |
909 | ␉origeps = getSmbios(SMBIOS_ORIGINAL);␊ |
910 | ␉if (!origeps)␊ |
911 | ␉{␊ |
912 | ␉␉DBG("saveOriginalSMBIOS: original SMBIOS not found\n");␊ |
913 | ␉␉return;␊ |
914 | ␉}␊ |
915 | ␊ |
916 | ␉tableAddress = (void *)AllocateKernelMemory(origeps->dmi.tableLength);␊ |
917 | ␉if (!tableAddress)␊ |
918 | ␉{␊ |
919 | ␉␉DBG("saveOriginalSMBIOS: can not allocate memory for original SMBIOS\n");␊ |
920 | ␉␉return;␊ |
921 | ␉}␊ |
922 | ␊ |
923 | ␉memcpy(tableAddress, (void *)origeps->dmi.tableAddress, origeps->dmi.tableLength);␊ |
924 | ␉DT__AddProperty(node, "SMBIOS", origeps->dmi.tableLength, tableAddress);␊ |
925 | }␊ |
926 | ␊ |
927 | /*␊ |
928 | * Entrypoint from boot.c␊ |
929 | */␊ |
930 | void setupFakeEfi(void)␊ |
931 | {␊ |
932 | ␉// Generate efi device strings␊ |
933 | ␉setup_pci_devs(root_pci_dev);␊ |
934 | ␊ |
935 | ␉readSMBIOSInfo(getSmbios(SMBIOS_ORIGINAL));␊ |
936 | ␊ |
937 | ␉// load smbios.plist file if any␊ |
938 | ␉setupSmbiosConfigFile("smbios.plist");␊ |
939 | ␊ |
940 | ␉setupSMBIOSTable();␊ |
941 | ␊ |
942 | ␉// Initialize the base table␊ |
943 | ␉if (archCpuType == CPU_TYPE_I386)␊ |
944 | ␉{␊ |
945 | ␉␉setupEfiTables32();␊ |
946 | ␉}␊ |
947 | ␉else␊ |
948 | ␉{␊ |
949 | ␉␉setupEfiTables64();␊ |
950 | ␉}␊ |
951 | ␊ |
952 | ␉// Initialize the device tree␊ |
953 | ␉setupEfiDeviceTree();␊ |
954 | ␊ |
955 | ␉saveOriginalSMBIOS();␊ |
956 | ␊ |
957 | ␉// Add configuration table entries to both the services table and the device tree␊ |
958 | ␉setupEfiConfigurationTable();␊ |
959 | }␊ |
960 | |