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