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