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