1 | ␊ |
2 | /*␊ |
3 | * Copyright 2007 David F. Elliott.␉ All rights reserved.␊ |
4 | */␊ |
5 | ␊ |
6 | /*␊ |
7 | * Copyright 2010,2011,2012 Cadet-petit Armel <armelcadetpetit@gmail.com>. All rights reserved.␊ |
8 | */␊ |
9 | ␊ |
10 | #include "libsaio.h"␊ |
11 | #include "bootstruct.h"␊ |
12 | #include "efi.h"␊ |
13 | #include "acpi.h"␊ |
14 | #include "fake_efi.h"␊ |
15 | #include "efi_tables.h"␊ |
16 | #include "platform.h"␊ |
17 | #include "device_inject.h"␊ |
18 | #include "convert.h"␊ |
19 | #include "pci.h"␊ |
20 | #include "sl.h"␊ |
21 | #include "modules.h"␊ |
22 | #include "vers.h"␊ |
23 | ␊ |
24 | #ifndef NO_SMP_SUPPORT␊ |
25 | #include "smp-imps.h"␊ |
26 | #endif␊ |
27 | ␊ |
28 | #ifndef DEBUG_EFI␊ |
29 | #define DEBUG_EFI 0␊ |
30 | #endif␊ |
31 | ␊ |
32 | #if DEBUG_EFI␊ |
33 | #define DBG(x...)␉printf(x)␊ |
34 | #else␊ |
35 | #define DBG(x...)␊ |
36 | #endif␊ |
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 | static inline char * mallocStringForGuid(EFI_GUID const *pGuid);␊ |
67 | static VOID EFI_ST_FIX_CRC32(VOID);␊ |
68 | static EFI_STATUS setupAcpiNoMod(VOID);␊ |
69 | static EFI_CHAR16* getSmbiosChar16(const char * key, size_t* len);␊ |
70 | static EFI_CHAR8* getSmbiosUUID(VOID);␊ |
71 | static int8_t *getSystemID(VOID);␊ |
72 | static VOID setupSystemType(VOID);␊ |
73 | static VOID setupEfiDeviceTree(VOID);␊ |
74 | static VOID setup_Smbios(VOID);␊ |
75 | static VOID setup_machine_signature(VOID);␊ |
76 | static VOID setupEfiConfigurationTable(VOID);␊ |
77 | static EFI_STATUS EFI_FindAcpiTables(VOID);␊ |
78 | ␊ |
79 | /*==========================================================================␊ |
80 | * Utility function to make a device tree string from an EFI_GUID␊ |
81 | */␊ |
82 | ␊ |
83 | static inline char * mallocStringForGuid(EFI_GUID const *pGuid)␊ |
84 | {␊ |
85 | ␉char *string = malloc(37);␊ |
86 | if (!string) {␊ |
87 | #if DEBUG_EFI␊ |
88 | char string_d[37];␊ |
89 | efi_guid_unparse_upper(pGuid, string_d, sizeof(string_d));␊ |
90 | printf("Couldn't allocate Guid String for %s\n", string_d); ␊ |
91 | #endif␊ |
92 | return NULL;␊ |
93 | }␊ |
94 | ␊ |
95 | ␉efi_guid_unparse_upper(pGuid, string, 37);␊ |
96 | ␉return string;␊ |
97 | }␊ |
98 | ␊ |
99 | /*==========================================================================␊ |
100 | * Function to map 32 bit physical address to 64 bit virtual address␊ |
101 | */␊ |
102 | ␊ |
103 | #define ptov64(addr) (uint64_t)((uint64_t)addr | 0xFFFFFF8000000000ULL)␊ |
104 | ␊ |
105 | /*==========================================================================␊ |
106 | * Fake EFI implementation␊ |
107 | */␊ |
108 | ␊ |
109 | static EFI_CHAR16 const FIRMWARE_VENDOR[] = {'A','p','p','l','e', 0}; ␊ |
110 | ␊ |
111 | /* Info About the current Firmware */␊ |
112 | #define FIRMWARE_MAINTENER "cparm, armelcadetpetit@gmail.com" ␊ |
113 | static EFI_CHAR16 const FIRMWARE_NAME[] = {'M','a','s','h','e','r','b','r','u','m','-','2', 0}; ␊ |
114 | static EFI_UINT32 const FIRMWARE_REVISION = 0x00010800; //1.8␊ |
115 | static EFI_UINT32 const DEVICE_SUPPORTED = 0x00000001;␊ |
116 | ␊ |
117 | /* Default platform system_id (fix by IntVar) */␊ |
118 | static EFI_CHAR8 const SYSTEM_ID[] = "0123456789ABCDEF"; //random value gen by uuidgen␊ |
119 | ␊ |
120 | /* Just a ret instruction */␊ |
121 | static uint8_t const VOIDRET_INSTRUCTIONS[] = {0xc3};␊ |
122 | ␊ |
123 | /* movl $0x80000003,%eax; ret */␊ |
124 | static uint8_t const UNSUPPORTEDRET_INSTRUCTIONS[] = {0xb8, 0x03, 0x00, 0x00, 0x80, 0xc3};␊ |
125 | ␊ |
126 | EFI_SYSTEM_TABLE_32 *gST32 = NULL;␊ |
127 | EFI_SYSTEM_TABLE_64 *gST64 = NULL;␊ |
128 | Node *gEfiConfigurationTableNode = NULL;␊ |
129 | ␊ |
130 | /* From Foundation/Efi/Guid/Smbios/SmBios.h */␊ |
131 | /* Modified to wrap Data4 array init with {} */␊ |
132 | #define EFI_SMBIOS_TABLE_GUID {0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}␊ |
133 | ␊ |
134 | #define EFI_ACPI_TABLE_GUID \␊ |
135 | { \␊ |
136 | 0xeb9d2d30, 0x2d88, 0x11d3, { 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \␊ |
137 | }␊ |
138 | ␊ |
139 | #define EFI_ACPI_20_TABLE_GUID \␊ |
140 | { \␊ |
141 | 0x8868e871, 0xe4f1, 0x11d3, { 0xbc, 0x22, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \␊ |
142 | }␊ |
143 | ␊ |
144 | #ifndef NO_SMP_SUPPORT␊ |
145 | #define EFI_MPS_TABLE_GUID \␊ |
146 | { \␊ |
147 | 0xeb9d2d2f,0x2d88,0x11d3,{0x9a,0x16,0x0,0x90,0x27,0x3f,0xc1,0x4d} \␊ |
148 | }␊ |
149 | #endif␊ |
150 | ␊ |
151 | /* From Foundation/Efi/Guid/Smbios/SmBios.c */␊ |
152 | EFI_GUID const␉gEfiSmbiosTableGuid = EFI_SMBIOS_TABLE_GUID;␊ |
153 | ␊ |
154 | EFI_GUID gEfiAcpiTableGuid = EFI_ACPI_TABLE_GUID;␊ |
155 | EFI_GUID gEfiAcpi20TableGuid = EFI_ACPI_20_TABLE_GUID;␊ |
156 | ␊ |
157 | #ifndef NO_SMP_SUPPORT␊ |
158 | EFI_GUID gEfiMpsTableGuid = EFI_MPS_TABLE_GUID;␊ |
159 | #endif␊ |
160 | ␊ |
161 | EFI_UINT32 gNumTables32 = 0;␊ |
162 | EFI_UINT64 gNumTables64 = 0;␊ |
163 | EFI_CONFIGURATION_TABLE_32 gEfiConfigurationTable32[MAX_CONFIGURATION_TABLE_ENTRIES];␊ |
164 | EFI_CONFIGURATION_TABLE_64 gEfiConfigurationTable64[MAX_CONFIGURATION_TABLE_ENTRIES];␊ |
165 | extern EFI_STATUS addConfigurationTable(EFI_GUID const *pGuid, void *table, char const *alias)␊ |
166 | {␊ |
167 | ␉EFI_UINTN i = 0;␊ |
168 | ␉␊ |
169 | if (pGuid == NULL || table == NULL)␊ |
170 | ␉␉return EFI_INVALID_PARAMETER;␊ |
171 | ␊ |
172 | char * GuidStr = mallocStringForGuid(pGuid);␊ |
173 | if (!GuidStr) {␊ |
174 | return EFI_OUT_OF_RESOURCES;␊ |
175 | }␊ |
176 | ␊ |
177 | ␉//Azi: as is, cpu's with em64t will use EFI64 on pre 10.6 systems,␊ |
178 | ␉// wich seems to cause no problem. In case it does, force i386 arch.␊ |
179 | ␉if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
180 | ␉{␊ |
181 | ␉␉i = gNumTables32;␊ |
182 | ␉}␊ |
183 | ␉else␊ |
184 | ␉{␊ |
185 | ␉␉i = (EFI_UINTN)gNumTables64;␊ |
186 | ␉}␊ |
187 | ␉␊ |
188 | ␉// We only do adds, not modifications and deletes like InstallConfigurationTable␊ |
189 | ␉if (i >= MAX_CONFIGURATION_TABLE_ENTRIES)␊ |
190 | ␉{␊ |
191 | ␊ |
192 | ␉␉␊ |
193 | printf("Ran out of space for configuration tables (max = %d). Please, increase the reserved size in the code.\n", (int)MAX_CONFIGURATION_TABLE_ENTRIES);␊ |
194 | return EFI_ABORTED;␊ |
195 | } ␊ |
196 | ␊ |
197 | if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
198 | ␉{ ␊ |
199 | ␊ |
200 | gEfiConfigurationTable32[i].VendorGuid = *pGuid;␊ |
201 | gEfiConfigurationTable32[i].VendorTable = (EFI_PTR32)table;␊ |
202 | ␊ |
203 | ␉␉gNumTables32++;␊ |
204 | ␉}␊ |
205 | ␉else␊ |
206 | ␉{ ␊ |
207 | gEfiConfigurationTable64[i].VendorGuid = *pGuid;␊ |
208 | gEfiConfigurationTable64[i].VendorTable = (EFI_PTR32)table;␊ |
209 | ␉␉gNumTables64++ ;␊ |
210 | ␉} ␊ |
211 | ␉␊ |
212 | ␊ |
213 | Node *tableNode = DT__AddChild(gEfiConfigurationTableNode, GuidStr);␊ |
214 | ␊ |
215 | // Use the pointer to the GUID we just stuffed into the system table␊ |
216 | DT__AddProperty(tableNode, "guid", sizeof(EFI_GUID), (void*)pGuid);␊ |
217 | ␊ |
218 | // The "table" property is the 32-bit (in our implementation) physical address of the table␊ |
219 | DT__AddProperty(tableNode, "table", sizeof(void*) * 2, table);␊ |
220 | ␊ |
221 | // Assume the alias pointer is a global or static piece of data␊ |
222 | if (alias != NULL)␊ |
223 | DT__AddProperty(tableNode, "alias", strlen(alias)+1, (char*)alias);␊ |
224 | ␊ |
225 | return EFI_SUCCESS;␊ |
226 | ␉␊ |
227 | }␊ |
228 | ␊ |
229 | static VOID EFI_ST_FIX_CRC32(void)␊ |
230 | {␊ |
231 | ␉if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
232 | ␉{␊ |
233 | ␉␉gST32->Hdr.CRC32 = 0;␊ |
234 | ␉␉gST32->Hdr.CRC32 = crc32(0L, gST32, gST32->Hdr.HeaderSize);␊ |
235 | ␉}␊ |
236 | ␉else␊ |
237 | ␉{␊ |
238 | ␉␉gST64->Hdr.CRC32 = 0;␊ |
239 | ␉␉gST64->Hdr.CRC32 = crc32(0L, gST64, gST64->Hdr.HeaderSize);␊ |
240 | ␉}␊ |
241 | }␊ |
242 | ␊ |
243 | void finalizeEFIConfigTable(void )␊ |
244 | { ␊ |
245 | if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
246 | ␉{␊ |
247 | ␉␉EFI_SYSTEM_TABLE_32 *efiSystemTable = gST32; ␊ |
248 | ␊ |
249 | efiSystemTable->NumberOfTableEntries = gNumTables32; ␊ |
250 | efiSystemTable->ConfigurationTable = (EFI_PTR32)gEfiConfigurationTable32;␊ |
251 | ␊ |
252 | ␉}␊ |
253 | ␉else␊ |
254 | ␉{␊ |
255 | ␉␉EFI_SYSTEM_TABLE_64 *efiSystemTable = gST64; ␊ |
256 | ␊ |
257 | efiSystemTable->NumberOfTableEntries = gNumTables64; ␊ |
258 | efiSystemTable->ConfigurationTable = ptov64((EFI_PTR32)gEfiConfigurationTable64); ␊ |
259 | ␊ |
260 | ␉}␊ |
261 | EFI_ST_FIX_CRC32();␊ |
262 | ␊ |
263 | #if DEBUG_EFI␊ |
264 | EFI_UINTN i;␊ |
265 | EFI_UINTN num = 0;␊ |
266 | uint32_t table ;␊ |
267 | EFI_GUID Guid;␊ |
268 | ␊ |
269 | if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
270 | ␉{␊ |
271 | ␉␉num = gST32->NumberOfTableEntries; ␊ |
272 | ␊ |
273 | ␉}␊ |
274 | ␉else␊ |
275 | ␉{␊ |
276 | ␉␉num = (EFI_UINTN)gST64->NumberOfTableEntries; ␊ |
277 | ␊ |
278 | ␉}␊ |
279 | ␉msglog("EFI Configuration table :\n");␊ |
280 | for (i=0; i<num; i++)␊ |
281 | ␉{␊ |
282 | if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
283 | {␊ |
284 | table = gEfiConfigurationTable32[i].VendorTable;␊ |
285 | Guid = gEfiConfigurationTable32[i].VendorGuid;␊ |
286 | ␊ |
287 | }␊ |
288 | else␊ |
289 | {␊ |
290 | table = gEfiConfigurationTable64[i].VendorTable;␊ |
291 | Guid = gEfiConfigurationTable64[i].VendorGuid;␊ |
292 | ␊ |
293 | }␊ |
294 | char id[4+1];␊ |
295 | bzero(id,sizeof(id)); ␊ |
296 | if (memcmp(&Guid, &gEfiSmbiosTableGuid, sizeof(EFI_GUID)) == 0)␊ |
297 | ␉␉{␊ |
298 | snprintf(id, sizeof(id),"%s", "_SM_");␊ |
299 | }␊ |
300 | ␉␉else if (memcmp(&Guid, &gEfiAcpiTableGuid, sizeof(EFI_GUID)) == 0)␊ |
301 | ␉␉{␊ |
302 | snprintf(id,sizeof(id), "%s", "RSD1");␊ |
303 | }␊ |
304 | ␉␉else if (memcmp(&Guid, &gEfiAcpi20TableGuid, sizeof(EFI_GUID)) == 0)␊ |
305 | ␉␉{␊ |
306 | snprintf(id, sizeof(id),"%s", "RSD2");␊ |
307 | }␊ |
308 | #ifndef NO_SMP_SUPPORT␊ |
309 | ␉␉else if (memcmp(&Guid, &gEfiMpsTableGuid, sizeof(EFI_GUID)) == 0)␊ |
310 | ␉␉{␊ |
311 | snprintf(id, sizeof(id),"%s", "_MP_");␊ |
312 | } ␊ |
313 | #endif␊ |
314 | ␉␉␊ |
315 | msglog("table [%d]:%s , 32Bit addr : 0x%x\n",i,id,table);␊ |
316 | ␊ |
317 | }␊ |
318 | msglog("\n");␊ |
319 | #endif␊ |
320 | ␊ |
321 | }␊ |
322 | ␊ |
323 | /*␊ |
324 | * What we do here is simply allocate a fake EFI system table and a fake EFI␊ |
325 | * runtime services table.␊ |
326 | *␊ |
327 | * Because we build against modern headers with kBootArgsRevision 4 we␊ |
328 | * also take care to set efiMode = 32.␊ |
329 | */␊ |
330 | ␊ |
331 | ␊ |
332 | #define pto(mode, addr) (mode == 64) ? ptov64((EFI_PTR32)addr) : (EFI_PTR32)addr␊ |
333 | ␊ |
334 | #define setupEfiTables(mode) \␊ |
335 | { \␊ |
336 | struct fake_efi_pages \␊ |
337 | {\␊ |
338 | /* We use the fake_efi_pages struct so that we only need to do one kernel␊ |
339 | * memory allocation for all needed EFI data. Otherwise, small allocations␊ |
340 | * like the FIRMWARE_VENDOR string would take up an entire page.␊ |
341 | * NOTE WELL: Do NOT assume this struct has any particular layout within itself.␊ |
342 | * It is absolutely not intended to be publicly exposed anywhere␊ |
343 | * We say pages (plural) although right now we are well within the 1 page size␊ |
344 | * and probably will stay that way.␊ |
345 | */\␊ |
346 | EFI_SYSTEM_TABLE_##mode efiSystemTable;\␊ |
347 | EFI_RUNTIME_SERVICES_##mode efiRuntimeServices;\␊ |
348 | EFI_CONFIGURATION_TABLE_##mode efiConfigurationTable[MAX_CONFIGURATION_TABLE_ENTRIES];\␊ |
349 | EFI_CHAR16 firmwareVendor[sizeof(FIRMWARE_VENDOR)/sizeof(EFI_CHAR16)];\␊ |
350 | uint8_t voidret_instructions[sizeof(VOIDRET_INSTRUCTIONS)/sizeof(uint8_t)];\␊ |
351 | uint8_t unsupportedret_instructions[sizeof(UNSUPPORTEDRET_INSTRUCTIONS)/sizeof(uint8_t)];\␊ |
352 | };\␊ |
353 | struct fake_efi_pages *fakeEfiPages = (struct fake_efi_pages*)AllocateKernelMemory(sizeof(struct fake_efi_pages));\␊ |
354 | /* Zero out all the tables in case fields are added later*/\␊ |
355 | bzero(fakeEfiPages, sizeof(struct fake_efi_pages));\␊ |
356 | /*--------------------------------------------------------------------␊ |
357 | * Initialize some machine code that will return EFI_UNSUPPORTED for␊ |
358 | * functions returning int and simply return for void functions.*/\␊ |
359 | memcpy(fakeEfiPages->voidret_instructions, VOIDRET_INSTRUCTIONS, sizeof(VOIDRET_INSTRUCTIONS));\␊ |
360 | memcpy(fakeEfiPages->unsupportedret_instructions, UNSUPPORTEDRET_INSTRUCTIONS, sizeof(UNSUPPORTEDRET_INSTRUCTIONS));\␊ |
361 | /*--------------------------------------------------------------------␊ |
362 | * System table*/\␊ |
363 | EFI_SYSTEM_TABLE_##mode *efiSystemTable = gST##mode = &fakeEfiPages->efiSystemTable;\␊ |
364 | efiSystemTable->Hdr.Signature = EFI_SYSTEM_TABLE_SIGNATURE;\␊ |
365 | efiSystemTable->Hdr.Revision = EFI_SYSTEM_TABLE_REVISION;\␊ |
366 | efiSystemTable->Hdr.HeaderSize = sizeof(EFI_SYSTEM_TABLE_##mode);\␊ |
367 | efiSystemTable->Hdr.CRC32 = 0;/*Initialize to zero and then do CRC32*/ \␊ |
368 | efiSystemTable->Hdr.Reserved = 0;\␊ |
369 | efiSystemTable->FirmwareVendor = pto(mode, &fakeEfiPages->firmwareVendor);\␊ |
370 | memcpy(fakeEfiPages->firmwareVendor, FIRMWARE_VENDOR, sizeof(FIRMWARE_VENDOR));\␊ |
371 | efiSystemTable->FirmwareRevision = FIRMWARE_REVISION;\␊ |
372 | /* XXX: We may need to have basic implementations of ConIn/ConOut/StdErr␊ |
373 | * The EFI spec states that all handles are invalid after boot services have been␊ |
374 | * exited so we can probably get by with leaving the handles as zero.␊ |
375 | */\␊ |
376 | efiSystemTable->ConsoleInHandle = 0;\␊ |
377 | efiSystemTable->ConIn = 0;\␊ |
378 | efiSystemTable->ConsoleOutHandle = 0;\␊ |
379 | efiSystemTable->ConOut = 0;\␊ |
380 | efiSystemTable->StandardErrorHandle = 0;\␊ |
381 | efiSystemTable->StdErr = 0;\␊ |
382 | efiSystemTable->RuntimeServices = pto(mode,&fakeEfiPages->efiRuntimeServices) ;\␊ |
383 | /* According to the EFI spec, BootServices aren't valid after the␊ |
384 | * boot process is exited so we can probably do without it.␊ |
385 | * Apple didn't provide a definition for it in pexpert/i386/efi.h␊ |
386 | * so I'm guessing they don't use it.␊ |
387 | */\␊ |
388 | efiSystemTable->BootServices = 0;\␊ |
389 | efiSystemTable->NumberOfTableEntries = 0;\␊ |
390 | efiSystemTable->ConfigurationTable = pto(mode,fakeEfiPages->efiConfigurationTable);\␊ |
391 | /* We're done. Now CRC32 the thing so the kernel will accept it.␊ |
392 | * Must be initialized to zero before CRC32, done above.␊ |
393 | */\␊ |
394 | gST##mode->Hdr.CRC32 = crc32(0L, gST##mode, gST##mode->Hdr.HeaderSize);\␊ |
395 | /*--------------------------------------------------------------------␊ |
396 | * Runtime services*/\␊ |
397 | EFI_RUNTIME_SERVICES_##mode *efiRuntimeServices = &fakeEfiPages->efiRuntimeServices;\␊ |
398 | efiRuntimeServices->Hdr.Signature = EFI_RUNTIME_SERVICES_SIGNATURE;\␊ |
399 | efiRuntimeServices->Hdr.Revision = EFI_RUNTIME_SERVICES_REVISION;\␊ |
400 | efiRuntimeServices->Hdr.HeaderSize = sizeof(EFI_RUNTIME_SERVICES_##mode);\␊ |
401 | efiRuntimeServices->Hdr.CRC32 = 0;\␊ |
402 | efiRuntimeServices->Hdr.Reserved = 0;\␊ |
403 | /* There are a number of function pointers in the efiRuntimeServices table.␊ |
404 | * These are the Foundation (e.g. core) services and are expected to be present on␊ |
405 | * all EFI-compliant machines.␉Some kernel extensions (notably AppleEFIRuntime)␊ |
406 | * will call these without checking to see if they are null.␊ |
407 | *␊ |
408 | * We don't really feel like doing an EFI implementation in the bootloader␊ |
409 | * but it is nice if we can at least prevent a complete crash by␊ |
410 | * at least providing some sort of implementation until one can be provided␊ |
411 | * nicely in a kext.␊ |
412 | */\␊ |
413 | void (*voidret_fp)() = (void*)fakeEfiPages->voidret_instructions;\␊ |
414 | void (*unsupportedret_fp)() = (void*)fakeEfiPages->unsupportedret_instructions;\␊ |
415 | efiRuntimeServices->GetTime = pto(mode,unsupportedret_fp);\␊ |
416 | efiRuntimeServices->SetTime = pto(mode,unsupportedret_fp);\␊ |
417 | efiRuntimeServices->GetWakeupTime = pto(mode,unsupportedret_fp);\␊ |
418 | efiRuntimeServices->SetWakeupTime = pto(mode,unsupportedret_fp);\␊ |
419 | efiRuntimeServices->SetVirtualAddressMap = pto(mode,unsupportedret_fp);\␊ |
420 | efiRuntimeServices->ConvertPointer = pto(mode,unsupportedret_fp);\␊ |
421 | efiRuntimeServices->GetVariable = pto(mode,unsupportedret_fp);\␊ |
422 | efiRuntimeServices->GetNextVariableName = pto(mode,unsupportedret_fp);\␊ |
423 | efiRuntimeServices->SetVariable = pto(mode,unsupportedret_fp);\␊ |
424 | efiRuntimeServices->GetNextHighMonotonicCount = pto(mode,unsupportedret_fp);\␊ |
425 | efiRuntimeServices->ResetSystem = pto(mode,voidret_fp);\␊ |
426 | /*We're done.␉Now CRC32 the thing so the kernel will accept it*/\␊ |
427 | efiRuntimeServices->Hdr.CRC32 = crc32(0L, efiRuntimeServices, efiRuntimeServices->Hdr.HeaderSize);\␊ |
428 | /*--------------------------------------------------------------------␊ |
429 | * Finish filling in the rest of the boot args that we need.*/\␊ |
430 | bootArgs->efiSystemTable = (uint32_t)efiSystemTable;\␊ |
431 | bootArgs->efiMode = kBootArgsEfiMode##mode;\␊ |
432 | /* The bootArgs structure as a whole is bzero'd so we don't need to fill in␊ |
433 | * things like efiRuntimeServices* and what not.␊ |
434 | *␊ |
435 | * In fact, the only code that seems to use that is the hibernate code so it␊ |
436 | * knows not to save the pages.␉ It even checks to make sure its nonzero.␊ |
437 | */\␊ |
438 | }␊ |
439 | ␊ |
440 | /*␊ |
441 | * In addition to the EFI tables there is also the EFI device tree node.␊ |
442 | * In particular, we need /efi/platform to have an FSBFrequency key. Without it,␊ |
443 | * the tsc_init function will panic very early on in kernel startup, before␊ |
444 | * the console is available.␊ |
445 | */␊ |
446 | ␊ |
447 | /*==========================================================================␊ |
448 | * FSB Frequency detection␊ |
449 | */␊ |
450 | ␊ |
451 | /* These should be const but DT__AddProperty takes char* */␊ |
452 | #if UNUSED␊ |
453 | static const char const TSC_Frequency_prop[] = "TSCFrequency";␊ |
454 | static const char const CPU_Frequency_prop[] = "CPUFrequency";␊ |
455 | #endif␊ |
456 | static const char const FSB_Frequency_prop[] = "FSBFrequency";␊ |
457 | ␊ |
458 | /*==========================================================================␊ |
459 | * SMBIOS␊ |
460 | */␊ |
461 | ␊ |
462 | static uint64_t smbios_p;␊ |
463 | ␊ |
464 | void Register_Smbios_Efi(void* smbios)␊ |
465 | {␊ |
466 | smbios_p = ((uint64_t)((uint32_t)smbios));␉␊ |
467 | }␊ |
468 | ␊ |
469 | /*==========================================================================␊ |
470 | * ACPI ␊ |
471 | */␊ |
472 | ␊ |
473 | static uint64_t local_rsd_p␉␉␉= 0;␊ |
474 | static uint64_t kFSBFrequency␉␉= 0;␊ |
475 | static uint32_t␉␉kHardware_signature = 0;␊ |
476 | static uint8_t␉␉kType␉␉␉␉= 0;␊ |
477 | static uint32_t␉␉kAdler32␉␉␉= 0;␊ |
478 | static ACPI_TABLES acpi_tables;␊ |
479 | ␊ |
480 | ␊ |
481 | EFI_STATUS Register_Acpi_Efi(void* rsd_p, unsigned char rev )␊ |
482 | {␊ |
483 | ␉EFI_STATUS Status = EFI_UNSUPPORTED;␊ |
484 | ␉local_rsd_p = ((U64)((U32)rsd_p));␊ |
485 | ␊ |
486 | ␉if (local_rsd_p) {␊ |
487 | ␉␉if (rev == 2)␊ |
488 | ␉␉{␊ |
489 | ␉␉␉Status = addConfigurationTable(&gEfiAcpi20TableGuid, &local_rsd_p, "ACPI_20");␊ |
490 | ␉␉}␊ |
491 | ␉␉else␊ |
492 | ␉␉{␊ |
493 | ␉␉␉Status = addConfigurationTable(&gEfiAcpiTableGuid, &local_rsd_p, "ACPI");␉␉␉␊ |
494 | ␉␉}␊ |
495 | ␉}␊ |
496 | ␉else ␊ |
497 | ␉{␊ |
498 | ␉␉Status = setupAcpiNoMod();␊ |
499 | ␉}␊ |
500 | ␉␉␊ |
501 | ␉␊ |
502 | ␉return Status;␉␊ |
503 | }␊ |
504 | ␊ |
505 | static EFI_STATUS EFI_FindAcpiTables(VOID)␊ |
506 | {␊ |
507 | ␉EFI_STATUS ret = EFI_UNSUPPORTED;␊ |
508 | ␉␊ |
509 | ␉if (local_rsd_p)␊ |
510 | ␉{␊ |
511 | ␉␉return EFI_SUCCESS;␊ |
512 | ␉}␊ |
513 | ␊ |
514 | ␉if (!FindAcpiTables(&acpi_tables))␊ |
515 | ␉{␊ |
516 | ␉␉printf("Failed to detect ACPI tables.\n");␊ |
517 | ␉␉ret = EFI_NOT_FOUND;␊ |
518 | ␉}␊ |
519 | ␉␊ |
520 | ␉local_rsd_p = ((uint64_t)((uint32_t)acpi_tables.RsdPointer));␊ |
521 | ␉␊ |
522 | ␉if (local_rsd_p)␊ |
523 | ␉{␊ |
524 | ␉␉ret = EFI_SUCCESS;␊ |
525 | ␉}␊ |
526 | ␉return ret;␊ |
527 | ␊ |
528 | }␊ |
529 | ␊ |
530 | /* Setup ACPI without any patch. */␊ |
531 | static EFI_STATUS setupAcpiNoMod(VOID)␊ |
532 | { ␉␊ |
533 | ␉EFI_STATUS ret = EFI_UNSUPPORTED;␊ |
534 | ␉␊ |
535 | ␉if (EFI_FindAcpiTables() == EFI_SUCCESS) ␊ |
536 | ␉{␊ |
537 | ␉␉ACPI_TABLE_RSDP* rsdp = (ACPI_TABLE_RSDP*)((uint32_t)local_rsd_p);␊ |
538 | ␉␉if(rsdp->Revision > 0 && (GetChecksum(rsdp, sizeof(ACPI_TABLE_RSDP)) == 0))␊ |
539 | ␉␉{␊ |
540 | ␉␉␉ret = addConfigurationTable(&gEfiAcpi20TableGuid, &local_rsd_p, "ACPI_20");␉␉ ␊ |
541 | ␉␉}␊ |
542 | ␉␉else␊ |
543 | ␉␉{␊ |
544 | ␉␉␉ret = addConfigurationTable(&gEfiAcpiTableGuid, &local_rsd_p, "ACPI");␉␉␊ |
545 | ␉␉}␊ |
546 | ␉} ␊ |
547 | ␉␊ |
548 | ␉return ret;␊ |
549 | }␊ |
550 | ␊ |
551 | EFI_STATUS setup_acpi (VOID)␊ |
552 | {␉␊ |
553 | ␉EFI_STATUS ret = EFI_UNSUPPORTED;␉␊ |
554 | ␉␊ |
555 | ␉do {␊ |
556 | ␉␉␊ |
557 | if ((ret = EFI_FindAcpiTables()) != EFI_SUCCESS)␊ |
558 | { ␊ |
559 | break;␊ |
560 | } ␊ |
561 | ␊ |
562 | {␊ |
563 | ACPI_TABLE_FADT *FacpPointer = (acpi_tables.FacpPointer64 != (void*)0ul) ? (ACPI_TABLE_FADT *)acpi_tables.FacpPointer64 : (ACPI_TABLE_FADT *)acpi_tables.FacpPointer;␊ |
564 | ␊ |
565 | uint8_t type = FacpPointer->PreferredProfile;␊ |
566 | if (type <= MaxSupportedPMProfile) ␊ |
567 | safe_set_env(envType,type);␊ |
568 | } ␊ |
569 | ␊ |
570 | ret = setupAcpiNoMod();␊ |
571 | ␉␉␊ |
572 | ␉} while (0);␉␊ |
573 | ␉␊ |
574 | ␉return ret;␉␊ |
575 | ␉␊ |
576 | }␊ |
577 | ␊ |
578 | /*==========================================================================␊ |
579 | * Fake EFI implementation␊ |
580 | */␊ |
581 | ␊ |
582 | /* These should be const but DT__AddProperty takes char* */␊ |
583 | static const char const FIRMWARE_REVISION_PROP[] = "firmware-revision";␊ |
584 | static const char const FIRMWARE_ABI_PROP[] = "firmware-abi";␊ |
585 | static const char const FIRMWARE_VENDOR_PROP[] = "firmware-vendor";␊ |
586 | static const char const FIRMWARE_NAME_PROP[] = "firmware-name";␊ |
587 | static const char const FIRMWARE_DATE_PROP[] = "firmware-date";␊ |
588 | static const char const FIRMWARE_DEV_PROP[] = "firmware-maintener";␊ |
589 | static const char const FIRMWARE_PUBLISH_PROP[] = "firmware-publisher";␊ |
590 | ␊ |
591 | ␊ |
592 | static const char const FIRMWARE_ABI_32_PROP_VALUE[] = "EFI32";␊ |
593 | static const char const FIRMWARE_ABI_64_PROP_VALUE[] = "EFI64";␊ |
594 | static const char const SYSTEM_ID_PROP[] = "system-id";␊ |
595 | static const char const SYSTEM_SERIAL_PROP[] = "SystemSerialNumber";␊ |
596 | static const char const SYSTEM_TYPE_PROP[] = "system-type";␊ |
597 | static const char const MODEL_PROP[] = "Model";␊ |
598 | static const char const MOTHERBOARD_NAME_PROP[] = "motherboard-name";␊ |
599 | ␊ |
600 | ␊ |
601 | /*␊ |
602 | * Get an smbios option string option to convert to EFI_CHAR16 string␊ |
603 | */␊ |
604 | ␊ |
605 | static EFI_CHAR16* getSmbiosChar16(const char * key, size_t* len)␊ |
606 | {␊ |
607 | ␉if (!GetgPlatformName() && strcmp(key, "SMproductname") == 0)␊ |
608 | ␉␉readSMBIOS(thePlatformName);␊ |
609 | ␉␊ |
610 | ␉const char␉*PlatformName = GetgPlatformName() ;␊ |
611 | ␉␊ |
612 | ␉const char␉*src = (strcmp(key, "SMproductname") == 0) ? PlatformName : getStringForKey(key, DEFAULT_SMBIOS_CONFIG);␊ |
613 | ␉␊ |
614 | ␉EFI_CHAR16*␉ dst = 0;␉␊ |
615 | ␉␊ |
616 | ␉if (!key || !(*key) || !src) return 0;␊ |
617 | ␉␊ |
618 | int tmp_len = strlen(src);␊ |
619 | ␊ |
620 | ␉dst = (EFI_CHAR16*) malloc( ((tmp_len)+1) * 2 );␊ |
621 | if (!dst) ␊ |
622 | {␊ |
623 | *len = 0;␊ |
624 | return NULL;␊ |
625 | }␊ |
626 | ␊ |
627 | *len = tmp_len;␊ |
628 | ␉{␊ |
629 | ␉␉size_t␉␉ i = 0;␊ |
630 | ␉␉for (; i < (*len); i++)␉ dst[i] = src[i];␊ |
631 | ␉}␊ |
632 | ␉dst[(*len)] = '\0';␊ |
633 | ␉*len = ((*len)+1)*2; // return the CHAR16 bufsize in cluding zero terminated CHAR16␊ |
634 | ␉return dst;␊ |
635 | }␊ |
636 | ␊ |
637 | /*␊ |
638 | * Get the SystemID from the bios dmi info␊ |
639 | */␊ |
640 | ␊ |
641 | static EFI_CHAR8* getSmbiosUUID(VOID)␊ |
642 | {␊ |
643 | ␉static EFI_CHAR8␉␉ uuid[UUID_LEN];␊ |
644 | ␉int␉␉␉␉␉␉ i, isZero, isOnes;␊ |
645 | ␉SMBByte␉␉␉␉␉*p;␉␉␊ |
646 | ␉␊ |
647 | p = (SMBByte*)(uint32_t)get_env(envUUID);␊ |
648 | ␊ |
649 | if ( p == NULL )␊ |
650 | ␉{␊ |
651 | DBG("No patched UUID found, fallback to original UUID (if exist) \n");␊ |
652 | ␉␉␊ |
653 | readSMBIOS(theUUID);␉␉␊ |
654 | p = (SMBByte*)(uint32_t)get_env(envUUID);␊ |
655 | ␊ |
656 | }␊ |
657 | ␊ |
658 | ␉for (i=0, isZero=1, isOnes=1; i<UUID_LEN; i++)␊ |
659 | ␉{␊ |
660 | ␉␉if (p[i] != 0x00) isZero = 0;␊ |
661 | ␉␉if (p[i] != 0xff) isOnes = 0;␊ |
662 | ␉}␊ |
663 | ␉␊ |
664 | ␉if (isZero || isOnes) // empty or setable means: no uuid present␊ |
665 | ␉{␊ |
666 | ␉␉verbose("No UUID present in SMBIOS System Information Table\n");␊ |
667 | ␉␉return 0;␊ |
668 | ␉} ␊ |
669 | #if DEBUG_EFI␊ |
670 | ␉else␊ |
671 | ␉␉verbose("Found UUID in SMBIOS System Information Table\n");␊ |
672 | #endif␊ |
673 | ␉␊ |
674 | ␉memcpy(uuid, p, UUID_LEN);␊ |
675 | ␉return uuid;␊ |
676 | }␊ |
677 | ␊ |
678 | /*␊ |
679 | * return a binary UUID value from the overriden SystemID and SMUUID if found, ␊ |
680 | * or from the bios if not, or from a fixed value if no bios value is found ␊ |
681 | */␊ |
682 | ␊ |
683 | static int8_t *getSystemID(VOID)␊ |
684 | {␊ |
685 | static int8_t␉␉␉␉sysid[16];␊ |
686 | ␉// unable to determine UUID for host. Error: 35 fix␊ |
687 | ␉// Rek: new SMsystemid option conforming to smbios notation standards, this option should␊ |
688 | ␉// belong to smbios config only ...␊ |
689 | ␉EFI_CHAR8*␉ret = getUUIDFromString(getStringForKey(kSystemID, DEFAULT_BOOT_CONFIG));␊ |
690 | ␉␊ |
691 | ␉if (!ret) // try bios dmi info UUID extraction␉␊ |
692 | ␉␉ret = getSmbiosUUID();␊ |
693 | ␉␊ |
694 | ␉if (!ret)␊ |
695 | ␉{␊ |
696 | ␉␉// no bios dmi UUID available, set a fixed value for system-id␊ |
697 | ␉␉ret=getUUIDFromString((const char*) SYSTEM_ID);␊ |
698 | ␉␉verbose("Customizing SystemID with : %s\n", getStringFromUUID(ret)); // apply a nice formatting to the displayed output␊ |
699 | ␉}␊ |
700 | ␉else␉␊ |
701 | ␉{␊ |
702 | ␉␉const char *mac = getStringFromUUID(ret);␊ |
703 | ␉␉verbose("MAC address : %c%c:%c%c:%c%c:%c%c:%c%c:%c%c\n",mac[24],mac[25],mac[26],mac[27],mac[28],mac[29]␊ |
704 | ␉␉␉␉,mac[30],mac[31],mac[32],mac[33],mac[34],mac[35]);␉␉ ␊ |
705 | ␉␉␊ |
706 | ␉}␊ |
707 | ␉␊ |
708 | ␉if (ret)␊ |
709 | ␉{ ␊ |
710 | ␉␉memcpy(sysid, ret, UUID_LEN);␊ |
711 | set_env_copy(envSysId, sysid, sizeof(sysid));␊ |
712 | ␉}␊ |
713 | ␉␊ |
714 | ␉return sysid;␊ |
715 | }␊ |
716 | ␊ |
717 | /*␊ |
718 | * Must be called AFTER setup Acpi because we need to take care of correct␊ |
719 | * facp content to reflect in ioregs␊ |
720 | */␊ |
721 | ␊ |
722 | static VOID setupSystemType(VOID)␊ |
723 | {␊ |
724 | ␉Node *node = DT__FindNode("/", false);␊ |
725 | ␉if (node == 0) stop("Couldn't get root node");␊ |
726 | ␉// we need to write this property after facp parsing␊ |
727 | ␉// Export system-type only if it has been overrriden by the SystemType option␊ |
728 | kType = get_env(envType);␊ |
729 | ␉DT__AddProperty(node, SYSTEM_TYPE_PROP, sizeof(uint8_t), &kType);␊ |
730 | }␊ |
731 | ␊ |
732 | static VOID setupEfiDeviceTree(VOID)␊ |
733 | {␉␊ |
734 | ␉Node␉␉*node;␊ |
735 | ␉␊ |
736 | ␉node = DT__FindNode("/", false);␊ |
737 | ␉␊ |
738 | ␉if (node == 0) stop("Couldn't get root node");␊ |
739 | ␊ |
740 | #ifndef NO_BOOT_IMG␊ |
741 | ␉{␊ |
742 | ␉␉long size;␊ |
743 | ␉␉{␊ |
744 | #include "appleClut8.h"␊ |
745 | ␉␉␉size = sizeof(appleClut8);␊ |
746 | ␉␉␉long clut = AllocateKernelMemory(size);␊ |
747 | ␉␉␉bcopy(&appleClut8, (void*)clut, size);␊ |
748 | ␊ |
749 | if (((BVRef)(uint32_t)get_env(envgBootVolume))->OSVersion[3] == '8') ␊ |
750 | {␊ |
751 | AllocateMemoryRange( "FailedCLUT", clut, size);␊ |
752 | ␊ |
753 | } else␊ |
754 | AllocateMemoryRange( "BootCLUT", clut, size);␊ |
755 | ␉␉␉␊ |
756 | ␉␉}␊ |
757 | ␉␉␊ |
758 | ␉␉{␊ |
759 | #include "failedboot.h"␉␊ |
760 | ␉␉␉size = 32 + kFailedBootWidth * kFailedBootHeight;␊ |
761 | ␉␉␉long bootPict = AllocateKernelMemory(size);␊ |
762 | if (((BVRef)(uint32_t)get_env(envgBootVolume))->OSVersion[3] == '8') ␊ |
763 | {␊ |
764 | AllocateMemoryRange( "FailedImage", bootPict, size); ␊ |
765 | ␊ |
766 | } else␊ |
767 | AllocateMemoryRange( "Pict-FailedBoot", bootPict, size); ␊ |
768 | ␊ |
769 | ␉␉␉((boot_progress_element *)bootPict)->width = kFailedBootWidth;␊ |
770 | ␉␉␉((boot_progress_element *)bootPict)->height = kFailedBootHeight;␊ |
771 | ␉␉␉((boot_progress_element *)bootPict)->yOffset = kFailedBootOffset;␉␊ |
772 | ␉␉␉if (((BVRef)(uint32_t)get_env(envgBootVolume))->OSVersion[3] == '8') ␊ |
773 | {␊ |
774 | ((boot_progress_element *)bootPict)->data_size = size - 32; ␊ |
775 | }␊ |
776 | ␉␉␉bcopy((char *)gFailedBootPict, (char *)(bootPict + 32), size - 32);␊ |
777 | ␉␉}␊ |
778 | ␉}␊ |
779 | #endif␊ |
780 | ␉//Fix an error with the Lion's (DP2+) installer␉␊ |
781 | ␉if (execute_hook("getboardproductPatched", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)␊ |
782 | ␉{␊ |
783 | ␉␉Setgboardproduct(getStringForKey("SMboardproduct", DEFAULT_SMBIOS_CONFIG));␊ |
784 | ␉␉␊ |
785 | ␉␉if (!Getgboardproduct()) readSMBIOS(theProducBoard);␊ |
786 | ␉␉␊ |
787 | ␉}␊ |
788 | ␉if (Getgboardproduct())␊ |
789 | ␉{␊ |
790 | ␉␉DT__AddProperty(node, "board-id", strlen(Getgboardproduct())+1, Getgboardproduct());␊ |
791 | ␉}␊ |
792 | ␉␊ |
793 | ␉{␊ |
794 | ␉␉Node *chosenNode = DT__FindNode("/chosen", true);␊ |
795 | ␉␉if (chosenNode)␊ |
796 | ␉␉{␉␉␉␊ |
797 | ␉␉␉DT__AddProperty(chosenNode, "boot-args", strlen(bootArgs->CommandLine)+1, (EFI_CHAR16*)bootArgs->CommandLine);␊ |
798 | ␉␉␉␊ |
799 | ␉␉␉// "boot-uuid" MAIN GOAL IS SYMPLY TO BOOT FROM THE UUID SET IN THE DT AND DECREASE BOOT TIME, SEE IOKitBSDInit.cpp␊ |
800 | ␉␉␉// additionally this value can be used by third-party apps or osx components (ex: pre-10.7 kextcache, ...)␊ |
801 | ␉␉␉if (bootInfo->uuidStr[0]) ␉␉␉␊ |
802 | ␉␉␉␉DT__AddProperty(chosenNode, kBootUUIDKey, strlen(bootInfo->uuidStr)+1, bootInfo->uuidStr);␊ |
803 | ␉␉␉␊ |
804 | ␉␉␉if (GetgRootDevice())␊ |
805 | ␉␉␉{␊ |
806 | ␉␉␉␉␊ |
807 | ␉␉␉␉DT__AddProperty(chosenNode, "boot-device-path", strlen(GetgRootDevice())+1, GetgRootDevice());␉␉␉␊ |
808 | ␉␉␉␉␊ |
809 | ␉␉␉}␉␉␉␊ |
810 | #ifdef rootpath␊ |
811 | ␉␉␉else␊ |
812 | ␉␉␉␉if (gRootPath[0])␊ |
813 | ␉␉␉␉{␊ |
814 | ␉␉␉␉␉␊ |
815 | ␉␉␉␉␉DT__AddProperty(chosenNode, "rootpath", strlen(gRootPath)+1, gRootPath);␉␉␉␊ |
816 | ␉␉␉␉␉␊ |
817 | ␉␉␉␉} ␊ |
818 | ␊ |
819 | #endif␊ |
820 | ␉␉␉␊ |
821 | ␉␉␉// "boot-file" is not used by kextcache if there is no "boot-device-path" or if there is a valid "rootpath" ,␊ |
822 | ␉␉␉// but i let it by default since it may be used by another service␊ |
823 | ␉␉␉DT__AddProperty(chosenNode, "boot-file", strlen(bootInfo->bootFile)+1, (EFI_CHAR16*)bootInfo->bootFile);␉␉␉␊ |
824 | ␉␉␉␊ |
825 | ␉␉␉if ((kAdler32 = (uint32_t)get_env(envAdler32))) ␊ |
826 | ␉␉␉␉DT__AddProperty(chosenNode, "boot-kernelcache-adler32", sizeof(unsigned long), &kAdler32);␊ |
827 | ␉␉␉␊ |
828 | ␉␉}␊ |
829 | ␉}␉␊ |
830 | ␉␊ |
831 | ␉// We could also just do DT__FindNode("/efi/platform", true)␊ |
832 | ␉// But I think eventually we want to fill stuff in the efi node␊ |
833 | ␉// too so we might as well create it so we have a pointer for it too.␊ |
834 | ␉Node *efiNode = DT__AddChild(node, "efi");␊ |
835 | ␉␊ |
836 | ␉{␊ |
837 | ␉␉// Set up the /efi/runtime-services table node similar to the way a child node of configuration-table␊ |
838 | ␉␉// is set up. That is, name and table properties␊ |
839 | ␉␉Node *runtimeServicesNode = DT__AddChild(efiNode, "runtime-services");␉␉␊ |
840 | ␉␉Node *kernelCompatibilityNode = 0; // ??? not sure that it should be used like that (because it's maybe the kernel capability and not the cpu capability)␊ |
841 | ␉␉␊ |
842 | ␉␉if (((BVRef)(uint32_t)get_env(envgBootVolume))->OSVersion[3] > '6')␊ |
843 | ␉␉{␊ |
844 | ␉␉␉kernelCompatibilityNode = DT__AddChild(efiNode, "kernel-compatibility");␉␊ |
845 | ␉␉␉DT__AddProperty(kernelCompatibilityNode, "i386", sizeof(uint32_t), (EFI_UINT32*)&DEVICE_SUPPORTED);␊ |
846 | ␉␉}␊ |
847 | ␉␉␊ |
848 | ␉␉if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
849 | ␉␉{␊ |
850 | ␉␉␉// The value of the table property is the 32-bit physical address for the RuntimeServices table.␊ |
851 | ␉␉␉// Since the EFI system table already has a pointer to it, we simply use the address of that pointer␊ |
852 | ␉␉␉// for the pointer to the property data. Warning.. DT finalization calls free on that but we're not␊ |
853 | ␉␉␉// the only thing to use a non-malloc'd pointer for something in the DT␊ |
854 | ␉␉␉␊ |
855 | ␉␉␉DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST32->RuntimeServices);␊ |
856 | ␉␉␉DT__AddProperty(efiNode, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_32_PROP_VALUE), (char*)FIRMWARE_ABI_32_PROP_VALUE);␊ |
857 | ␉␉}␊ |
858 | ␉␉else␊ |
859 | ␉␉{␊ |
860 | ␉␉␉if (kernelCompatibilityNode) ␊ |
861 | ␉␉␉␉DT__AddProperty(kernelCompatibilityNode, "x86_64", sizeof(uint32_t), (EFI_UINT32*)&DEVICE_SUPPORTED);␊ |
862 | ␉␉␉␊ |
863 | ␉␉␉DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST64->RuntimeServices);␊ |
864 | ␉␉␉DT__AddProperty(efiNode, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_64_PROP_VALUE), (char*)FIRMWARE_ABI_64_PROP_VALUE);␊ |
865 | ␉␉}␊ |
866 | ␉}␊ |
867 | ␉␊ |
868 | ␉DT__AddProperty(efiNode, FIRMWARE_REVISION_PROP, sizeof(FIRMWARE_REVISION), (EFI_UINT32*)&FIRMWARE_REVISION);␊ |
869 | ␉DT__AddProperty(efiNode, FIRMWARE_VENDOR_PROP, sizeof(FIRMWARE_VENDOR), (EFI_CHAR16*)FIRMWARE_VENDOR);␊ |
870 | ␉DT__AddProperty(efiNode, FIRMWARE_NAME_PROP, sizeof(FIRMWARE_NAME), (EFI_CHAR16*)FIRMWARE_NAME);␊ |
871 | ␉DT__AddProperty(efiNode, FIRMWARE_DATE_PROP, strlen(I386BOOT_BUILDDATE)+1, I386BOOT_BUILDDATE);␉␊ |
872 | ␉DT__AddProperty(efiNode, FIRMWARE_DEV_PROP, strlen(FIRMWARE_MAINTENER)+1, FIRMWARE_MAINTENER);␊ |
873 | ␉DT__AddProperty(efiNode, FIRMWARE_PUBLISH_PROP, strlen(FIRMWARE_PUBLISHER)+1, FIRMWARE_PUBLISHER);␊ |
874 | ␉␊ |
875 | ␉{␊ |
876 | ␉␉// Export it for amlsgn support␊ |
877 | ␉␉char * DefaultPlatform = readDefaultPlatformName();␊ |
878 | ␉␉if (DefaultPlatform)␊ |
879 | ␉␉{␊ |
880 | ␉␉␉DT__AddProperty(efiNode, MOTHERBOARD_NAME_PROP, strlen(DefaultPlatform)+1, DefaultPlatform);␊ |
881 | ␉␉}␊ |
882 | ␉␉␊ |
883 | ␉}␊ |
884 | ␉␊ |
885 | ␉// Set up the /efi/configuration-table node which will eventually have several child nodes for␊ |
886 | ␉// all of the configuration tables needed by various kernel extensions.␊ |
887 | ␉gEfiConfigurationTableNode = DT__AddChild(efiNode, "configuration-table");␊ |
888 | ␉␊ |
889 | ␉{␊ |
890 | ␉␉EFI_CHAR16 *serial = 0, *productname = 0;␊ |
891 | int8_t␉␉␉␉*sysid = 0;␊ |
892 | ␉␉size_t␉␉ len = 0;␊ |
893 | ␉␉␊ |
894 | ␉␉// Now fill in the /efi/platform Node␊ |
895 | ␉␉Node *efiPlatformNode = DT__AddChild(efiNode, "platform");␊ |
896 | ␉␉␊ |
897 | ␉␉DT__AddProperty(efiPlatformNode, "DevicePathsSupported", sizeof(uint32_t), (EFI_UINT32*)&DEVICE_SUPPORTED);␊ |
898 | ␉␉␊ |
899 | ␉␉// NOTE WELL: If you do add FSB Frequency detection, make sure to store␊ |
900 | ␉␉// the value in the fsbFrequency global and not an malloc'd pointer␊ |
901 | ␉␉// because the DT_AddProperty function does not copy its args.␊ |
902 | ␉␉␊ |
903 | kFSBFrequency = get_env(envFSBFreq);␊ |
904 | ␉␉if (kFSBFrequency != 0)␊ |
905 | ␉␉␉DT__AddProperty(efiPlatformNode, FSB_Frequency_prop, sizeof(uint64_t), &kFSBFrequency);␉␉␊ |
906 | ␉␉␊ |
907 | #if UNUSED␊ |
908 | ␉␉// Export TSC and CPU frequencies for use by the kernel or KEXTs␊ |
909 | ␉␉Platform.CPU.TSCFrequency = get_env(envTSCFreq);␊ |
910 | if (Platform.CPU.TSCFrequency != 0)␊ |
911 | ␉␉␉DT__AddProperty(efiPlatformNode, TSC_Frequency_prop, sizeof(uint64_t), &Platform.CPU.TSCFrequency);␊ |
912 | ␉␉␊ |
913 | Platform.CPU.CPUFrequency = get_env(envCPUFreq);␊ |
914 | ␉␉if (Platform.CPU.CPUFrequency != 0)␊ |
915 | ␉␉␉DT__AddProperty(efiPlatformNode, CPU_Frequency_prop, sizeof(uint64_t), &Platform.CPU.CPUFrequency);␊ |
916 | #endif␊ |
917 | ␉␉␊ |
918 | ␉␉// Export system-id. Can be disabled with SystemId=No in com.apple.Boot.plist␉␉␊ |
919 | ␉␉if ((sysid = getSystemID()))␊ |
920 | ␉␉␉DT__AddProperty(efiPlatformNode, SYSTEM_ID_PROP, UUID_LEN, (EFI_UINT32*) sysid);␊ |
921 | ␉␉␊ |
922 | ␉␉// Export SystemSerialNumber if present␊ |
923 | ␉␉if ((serial=getSmbiosChar16("SMserial", &len)))␊ |
924 | ␉␉␉DT__AddProperty(efiPlatformNode, SYSTEM_SERIAL_PROP, len, serial);␊ |
925 | ␉␉␊ |
926 | ␉␉// Export Model if present␊ |
927 | ␉␉if ((productname=getSmbiosChar16("SMproductname", &len)))␊ |
928 | ␉␉␉DT__AddProperty(efiPlatformNode, MODEL_PROP, len, productname);␊ |
929 | ␉}␉␉␊ |
930 | ␉␊ |
931 | ␉// Fill /efi/device-properties node.␉␉␉␊ |
932 | ␉setupDeviceProperties(efiNode);␊ |
933 | }␊ |
934 | ␊ |
935 | /*␊ |
936 | * Load the smbios.plist override config file if any␊ |
937 | */␊ |
938 | ␊ |
939 | void setupSmbiosConfigFile(const char *filename)␊ |
940 | {␉␊ |
941 | ␉static bool readSmbConfigFile = true;␊ |
942 | ␉␊ |
943 | ␉if (readSmbConfigFile == true)␊ |
944 | ␉{␊ |
945 | ␉␉char␉␉dirSpecSMBIOS[128] = "";␊ |
946 | ␉␉const char *override_pathname = NULL;␊ |
947 | ␉␉int␉␉␉len = 0, err = 0;␊ |
948 | ␉␉␊ |
949 | ␉␉// Take in account user overriding␊ |
950 | ␉␉if (getValueForKey("SMBIOS", &override_pathname, &len, DEFAULT_BOOT_CONFIG) && len > 0)␊ |
951 | ␉␉{␊ |
952 | ␉␉␉// Specify a path to a file, e.g. SMBIOS=/Extra/macProXY.plist␊ |
953 | ␉␉␉snprintf(dirSpecSMBIOS, sizeof(dirSpecSMBIOS),override_pathname);␊ |
954 | ␉␉␉err = loadConfigFile(dirSpecSMBIOS, DEFAULT_SMBIOS_CONFIG);␊ |
955 | ␉␉}␊ |
956 | ␉␉else␊ |
957 | ␉␉{␊ |
958 | ␉␉␉// Check selected volume's Extra.␊ |
959 | ␉␉␉snprintf(dirSpecSMBIOS, sizeof(dirSpecSMBIOS),"/Extra/%s", filename);␊ |
960 | ␉␉␉if ((err = loadConfigFile(dirSpecSMBIOS, DEFAULT_SMBIOS_CONFIG)))␊ |
961 | ␉␉␉{␊ |
962 | ␉␉␉␉// Check booter volume/rdbt Extra.␊ |
963 | ␉␉␉␉snprintf(dirSpecSMBIOS, sizeof(dirSpecSMBIOS),"bt(0,0)/Extra/%s", filename);␊ |
964 | ␉␉␉␉err = loadConfigFile(dirSpecSMBIOS, DEFAULT_SMBIOS_CONFIG);␊ |
965 | ␉␉␉}␊ |
966 | ␉␉}␊ |
967 | ␉␉␊ |
968 | ␉␉if (err)␊ |
969 | ␉␉{␊ |
970 | ␉␉␉verbose("No SMBIOS config file found.\n");␊ |
971 | ␉␉}␊ |
972 | ␉␉readSmbConfigFile = false;␊ |
973 | ␉}␊ |
974 | }␊ |
975 | ␊ |
976 | static VOID setup_Smbios(VOID)␊ |
977 | {␉␉␉␊ |
978 | ␉if (execute_hook("getSmbiosPatched",NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS)␊ |
979 | ␉{␊ |
980 | ␉␉DBG("Using the original SMBIOS !!\n");␉␊ |
981 | struct SMBEntryPoint *smbios_o = getSmbiosOriginal();␉␊ |
982 | smbios_p = ((uint64_t)((uint32_t)smbios_o)); ␊ |
983 | ␉}␉␊ |
984 | }␊ |
985 | ␊ |
986 | static VOID setup_machine_signature(VOID)␊ |
987 | {␊ |
988 | ␉Node *chosenNode = DT__FindNode("/chosen", false);␊ |
989 | ␉if (chosenNode)␊ |
990 | ␉{␊ |
991 | ␉␉if (get_env(envHardwareSignature) == 0xFFFFFFFF)␊ |
992 | ␉␉{␉␉␉␊ |
993 | ␉␉␉do {␊ |
994 | ␉␉␉␉if (!local_rsd_p)␊ |
995 | ␉␉␉␉{␉␉␉␊ |
996 | ␉␉␉␉␉if ( EFI_FindAcpiTables() != EFI_SUCCESS)␊ |
997 | ␉␉␉␉␉{␊ |
998 | ␉␉␉␉␉␉printf("Failed to detect ACPI tables.\n");␊ |
999 | ␉␉␉␉␉␉break;␊ |
1000 | ␉␉␉␉␉}␉␉␉␉␉␊ |
1001 | ␉␉␉␉}␊ |
1002 | ␉␉␉␉␊ |
1003 | ␉␉␉␉ACPI_TABLE_FACS *FacsPointer = (acpi_tables.FacsPointer64 != (void*)0ul) ? (ACPI_TABLE_FACS *)acpi_tables.FacsPointer64:(ACPI_TABLE_FACS *)acpi_tables.FacsPointer;␉␉␉␉␊ |
1004 | ␊ |
1005 | safe_set_env(envHardwareSignature , FacsPointer->HardwareSignature);␊ |
1006 | ␉␉␉␉␊ |
1007 | ␉␉␉} while (0); ␉␊ |
1008 | ␉␉␉␊ |
1009 | ␉␉␉// Verify that we have a valid hardware signature␊ |
1010 | ␉␉␉if (get_env(envHardwareSignature) == 0xFFFFFFFF) ␊ |
1011 | ␉␉␉{␊ |
1012 | ␉␉␉␉verbose("Warning: hardware_signature is invalid, defaulting to 0 \n"); ␊ |
1013 | safe_set_env(envHardwareSignature , 0);␊ |
1014 | ␉␉␉}␊ |
1015 | ␉␉}␊ |
1016 | ␉␉␊ |
1017 | kHardware_signature = get_env(envHardwareSignature); ␊ |
1018 | ␉␉DT__AddProperty(chosenNode, "machine-signature", sizeof(uint32_t), &kHardware_signature);␊ |
1019 | ␉}␊ |
1020 | ␉␊ |
1021 | }␊ |
1022 | ␊ |
1023 | /*␊ |
1024 | * Installs all the needed configuration table entries␊ |
1025 | */␊ |
1026 | ␊ |
1027 | static VOID setupEfiConfigurationTable(VOID)␊ |
1028 | {␊ |
1029 | if (smbios_p)␊ |
1030 | addConfigurationTable(&gEfiSmbiosTableGuid, &smbios_p, NULL);␊ |
1031 | #ifndef NO_SMP_SUPPORT␊ |
1032 | ␉if (get_env(envVendor) == CPUID_VENDOR_INTEL )␊ |
1033 | ␉{␊ |
1034 | ␉␉int num_cpus;␊ |
1035 | ␉␉␊ |
1036 | ␉␉void *mps_p = imps_probe(&num_cpus);␊ |
1037 | ␉␉␊ |
1038 | ␉␉if (mps_p)␊ |
1039 | ␉␉{␉␉␊ |
1040 | ␉␉␉addConfigurationTable(&gEfiMpsTableGuid, ((uint64_t*)((uint32_t)mps_p)), NULL);␊ |
1041 | ␉␉}␊ |
1042 | ␉␉␊ |
1043 | #if DEBUG_EFI␊ |
1044 | if (num_cpus != get_env(envNoCores))␊ |
1045 | {␊ |
1046 | printf("Warning: SMP nb of core (%d) mismatch with the value found in cpu.c (%d) \n",num_cpus,get_env(envNoCores)); ␊ |
1047 | } ␊ |
1048 | #endif ␊ |
1049 | ␉}␉␊ |
1050 | #endif␊ |
1051 | ␉// PM_Model␊ |
1052 | ␉if (get_env(envIsServer))␊ |
1053 | {␊ |
1054 | safe_set_env(envType , Workstation);␊ |
1055 | ␉}␊ |
1056 | ␉else if (get_env(envIsMobile))//Slice␊ |
1057 | ␉{␉␊ |
1058 | safe_set_env(envType , Mobile);␊ |
1059 | ␉} ␊ |
1060 | ␉else␊ |
1061 | ␉{␊ |
1062 | safe_set_env(envType , Desktop);␊ |
1063 | ␉}␊ |
1064 | ␉␊ |
1065 | ␉// Invalidate the platform hardware signature (this needs to be verified with acpica, but i guess that 0xFFFFFFFF is an invalid signature) ␉␊ |
1066 | safe_set_env(envHardwareSignature , 0xFFFFFFFF);␊ |
1067 | ␉␊ |
1068 | ␉// Setup ACPI (based on the mackerintel's patch)␊ |
1069 | ␉(VOID)setup_acpi();␊ |
1070 | ␉␊ |
1071 | ␉setup_machine_signature();␊ |
1072 | ␉␊ |
1073 | ␉// We now have to write the system-type in ioregs: we cannot do it before in setupDeviceTree()␊ |
1074 | ␉// because we need to take care of facp original content, if it is correct.␊ |
1075 | ␉setupSystemType();␊ |
1076 | ␉␊ |
1077 | ␉// We've obviously changed the count.. so fix up the CRC32␉␊ |
1078 | EFI_ST_FIX_CRC32();␊ |
1079 | }␊ |
1080 | ␊ |
1081 | /*␊ |
1082 | * Entrypoint from boot.c␊ |
1083 | */␊ |
1084 | ␊ |
1085 | void setupFakeEfi(void)␊ |
1086 | {␊ |
1087 | ␉// Collect PCI info &| Generate device prop string␊ |
1088 | ␉setup_pci_devs(root_pci_dev);␊ |
1089 | ␉␊ |
1090 | ␉// load smbios.plist file if any␊ |
1091 | ␉setupSmbiosConfigFile("SMBIOS.plist");␊ |
1092 | ␉setup_Smbios();␊ |
1093 | ␉␊ |
1094 | ␉// Initialize the base table␊ |
1095 | ␉if (get_env(envarchCpuType) == CPU_TYPE_I386)␊ |
1096 | ␉{␊ |
1097 | ␉␉setupEfiTables(32); ␊ |
1098 | ␉}␊ |
1099 | ␉else␊ |
1100 | ␉{␊ |
1101 | ␉␉setupEfiTables(64); ␊ |
1102 | ␉}␊ |
1103 | ␉␊ |
1104 | ␉// Initialize the device tree␊ |
1105 | ␉setupEfiDeviceTree();␊ |
1106 | ␉␊ |
1107 | ␉// Add configuration table entries to both the services table and the device tree␊ |
1108 | ␉setupEfiConfigurationTable();␉␉␊ |
1109 | ␉␊ |
1110 | } |