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