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