Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/libsaio/fake_efi.c

1
2/*
3 * Copyright 2007 David F. Elliott. All rights reserved.
4 */
5
6#include "saio_types.h"
7#include "libsaio.h"
8#include "boot.h"
9#include "bootstruct.h"
10#include "efi.h"
11#include "acpi.h"
12#include "fake_efi.h"
13#include "efi_tables.h"
14#include "platform.h"
15#include "acpi_patcher.h"
16#include "smbios.h"
17#include "device_inject.h"
18#include "convert.h"
19#include "pci.h"
20#include "sl.h"
21
22extern void setup_pci_devs(pci_dt_t *pci_dt);
23
24/*
25 * Modern Darwin kernels require some amount of EFI because Apple machines all
26 * have EFI. Modifying the kernel source to not require EFI is of course
27 * possible but would have to be maintained as a separate patch because it is
28 * unlikely that Apple wishes to add legacy support to their kernel.
29 *
30 * As you can see from the Apple-supplied code in bootstruct.c, it seems that
31 * the intention was clearly to modify this booter to provide EFI-like structures
32 * to the kernel rather than modifying the kernel to handle non-EFI stuff. This
33 * makes a lot of sense from an engineering point of view as it means the kernel
34 * for the as yet unreleased EFI-only Macs could still be booted by the non-EFI
35 * DTK systems so long as the kernel checked to ensure the boot tables were
36 * filled in appropriately.Modern xnu requires a system table and a runtime
37 * services table and performs no checks whatsoever to ensure the pointers to
38 * these tables are non-NULL. Therefore, any modern xnu kernel will page fault
39 * early on in the boot process if the system table pointer is zero.
40 *
41 * Even before that happens, the tsc_init function in modern xnu requires the FSB
42 * Frequency to be a property in the /efi/platform node of the device tree or else
43 * it panics the bootstrap process very early on.
44 *
45 * As of this writing, the current implementation found here is good enough
46 * to make the currently available xnu kernel boot without modification on a
47 * system with an appropriate processor. With a minor source modification to
48 * the tsc_init function to remove the explicit check for Core or Core 2
49 * processors the kernel can be made to boot on other processors so long as
50 * the code can be executed by the processor and the machine contains the
51 * necessary hardware.
52 */
53
54/*==========================================================================
55 * Utility function to make a device tree string from an EFI_GUID
56 */
57static inline char * mallocStringForGuid(EFI_GUID const *pGuid)
58{
59char *string = malloc(37);
60efi_guid_unparse_upper(pGuid, string);
61return string;
62}
63
64/*==========================================================================
65 * Function to map 32 bit physical address to 64 bit virtual address
66 */
67static uint64_t ptov64(uint32_t addr)
68{
69return ((uint64_t)addr | 0xFFFFFF8000000000ULL);
70}
71
72/*==========================================================================
73 * Fake EFI implementation
74 */
75
76/* Identify ourselves as the EFI firmware vendor */
77static EFI_CHAR16 const FIRMWARE_VENDOR[] = {'C','h','a','m','e','l','e','o','n','_','2','.','1', 0};
78static EFI_UINT32 const FIRMWARE_REVISION = 132; /* FIXME: Find a constant for this. */
79
80/* Default platform system_id (fix by IntVar) */
81static EFI_CHAR8 const SYSTEM_ID[] = "0123456789ABCDEF"; //random value gen by uuidgen
82
83/* Just a ret instruction */
84static uint8_t const VOIDRET_INSTRUCTIONS[] = {0xc3};
85
86/* movl $0x80000003,%eax; ret */
87static uint8_t const UNSUPPORTEDRET_INSTRUCTIONS[] = {0x48, 0xb8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc3};
88
89EFI_SYSTEM_TABLE_32 *gST32 = NULL;
90EFI_SYSTEM_TABLE_64 *gST64 = NULL;
91Node *gEfiConfigurationTableNode = NULL;
92
93extern EFI_STATUS addConfigurationTable(EFI_GUID const *pGuid, void *table, char const *alias)
94{
95EFI_UINTN i = 0;
96
97//Azi: as is, cpu's with em64t will use EFI64 on pre 10.6 systems,
98// wich seems to cause no problem. In case it does, force i386 arch.
99if (archCpuType == CPU_TYPE_I386)
100{
101i = gST32->NumberOfTableEntries;
102}
103else
104{
105i = gST64->NumberOfTableEntries;
106}
107
108// We only do adds, not modifications and deletes like InstallConfigurationTable
109if (i >= MAX_CONFIGURATION_TABLE_ENTRIES)
110stop("Ran out of space for configuration tables. Increase the reserved size in the code.\n");
111
112if (pGuid == NULL)
113return EFI_INVALID_PARAMETER;
114
115if (table != NULL)
116{
117// FIXME
118//((EFI_CONFIGURATION_TABLE_64 *)gST->ConfigurationTable)[i].VendorGuid = *pGuid;
119//((EFI_CONFIGURATION_TABLE_64 *)gST->ConfigurationTable)[i].VendorTable = (EFI_PTR64)table;
120
121//++gST->NumberOfTableEntries;
122
123Node *tableNode = DT__AddChild(gEfiConfigurationTableNode, mallocStringForGuid(pGuid));
124
125// Use the pointer to the GUID we just stuffed into the system table
126DT__AddProperty(tableNode, "guid", sizeof(EFI_GUID), (void*)pGuid);
127
128// The "table" property is the 32-bit (in our implementation) physical address of the table
129DT__AddProperty(tableNode, "table", sizeof(void*) * 2, table);
130
131// Assume the alias pointer is a global or static piece of data
132if (alias != NULL)
133DT__AddProperty(tableNode, "alias", strlen(alias)+1, (char*)alias);
134
135return EFI_SUCCESS;
136}
137return EFI_UNSUPPORTED;
138}
139
140//Azi: crc32 done in place, on the cases were it wasn't.
141/*static inline void fixupEfiSystemTableCRC32(EFI_SYSTEM_TABLE_64 *efiSystemTable)
142{
143efiSystemTable->Hdr.CRC32 = 0;
144efiSystemTable->Hdr.CRC32 = crc32(0L, efiSystemTable, efiSystemTable->Hdr.HeaderSize);
145}*/
146
147/*
148 * What we do here is simply allocate a fake EFI system table and a fake EFI
149 * runtime services table.
150 *
151 * Because we build against modern headers with kBootArgsRevision 4 we
152 * also take care to set efiMode = 32.
153 */
154void setupEfiTables32(void)
155{
156// We use the fake_efi_pages struct so that we only need to do one kernel
157// memory allocation for all needed EFI data. Otherwise, small allocations
158// like the FIRMWARE_VENDOR string would take up an entire page.
159// NOTE WELL: Do NOT assume this struct has any particular layout within itself.
160// It is absolutely not intended to be publicly exposed anywhere
161// We say pages (plural) although right now we are well within the 1 page size
162// and probably will stay that way.
163struct fake_efi_pages
164{
165EFI_SYSTEM_TABLE_32 efiSystemTable;
166EFI_RUNTIME_SERVICES_32 efiRuntimeServices;
167EFI_CONFIGURATION_TABLE_32 efiConfigurationTable[MAX_CONFIGURATION_TABLE_ENTRIES];
168EFI_CHAR16 firmwareVendor[sizeof(FIRMWARE_VENDOR)/sizeof(EFI_CHAR16)];
169uint8_t voidret_instructions[sizeof(VOIDRET_INSTRUCTIONS)/sizeof(uint8_t)];
170uint8_t unsupportedret_instructions[sizeof(UNSUPPORTEDRET_INSTRUCTIONS)/sizeof(uint8_t)];
171};
172
173struct fake_efi_pages *fakeEfiPages = (struct fake_efi_pages*)AllocateKernelMemory(sizeof(struct fake_efi_pages));
174
175// Zero out all the tables in case fields are added later
176//bzero(fakeEfiPages, sizeof(struct fake_efi_pages));
177
178// --------------------------------------------------------------------
179// Initialize some machine code that will return EFI_UNSUPPORTED for
180// functions returning int and simply return for void functions.
181memcpy(fakeEfiPages->voidret_instructions, VOIDRET_INSTRUCTIONS, sizeof(VOIDRET_INSTRUCTIONS));
182memcpy(fakeEfiPages->unsupportedret_instructions, UNSUPPORTEDRET_INSTRUCTIONS, sizeof(UNSUPPORTEDRET_INSTRUCTIONS));
183
184// --------------------------------------------------------------------
185// System table
186EFI_SYSTEM_TABLE_32 *efiSystemTable = gST32 = &fakeEfiPages->efiSystemTable;
187efiSystemTable->Hdr.Signature = EFI_SYSTEM_TABLE_SIGNATURE;
188efiSystemTable->Hdr.Revision = EFI_SYSTEM_TABLE_REVISION;
189efiSystemTable->Hdr.HeaderSize = sizeof(EFI_SYSTEM_TABLE_32);
190efiSystemTable->Hdr.CRC32 = 0; // Initialize to zero and then do CRC32
191efiSystemTable->Hdr.Reserved = 0;
192
193efiSystemTable->FirmwareVendor = (EFI_PTR32)&fakeEfiPages->firmwareVendor;
194memcpy(fakeEfiPages->firmwareVendor, FIRMWARE_VENDOR, sizeof(FIRMWARE_VENDOR));
195efiSystemTable->FirmwareRevision = FIRMWARE_REVISION;
196
197// XXX: We may need to have basic implementations of ConIn/ConOut/StdErr
198// The EFI spec states that all handles are invalid after boot services have been
199// exited so we can probably get by with leaving the handles as zero.
200efiSystemTable->ConsoleInHandle = 0;
201efiSystemTable->ConIn = 0;
202
203efiSystemTable->ConsoleOutHandle = 0;
204efiSystemTable->ConOut = 0;
205
206efiSystemTable->StandardErrorHandle = 0;
207efiSystemTable->StdErr = 0;
208
209efiSystemTable->RuntimeServices = (EFI_PTR32)&fakeEfiPages->efiRuntimeServices;
210
211// According to the EFI spec, BootServices aren't valid after the
212// boot process is exited so we can probably do without it.
213// Apple didn't provide a definition for it in pexpert/i386/efi.h
214// so I'm guessing they don't use it.
215efiSystemTable->BootServices = 0;
216
217efiSystemTable->NumberOfTableEntries = 0;
218efiSystemTable->ConfigurationTable = (EFI_PTR32)fakeEfiPages->efiConfigurationTable;
219
220// We're done. Now CRC32 the thing so the kernel will accept it.
221// Must be initialized to zero before CRC32, done above.
222gST32->Hdr.CRC32 = crc32(0L, gST32, gST32->Hdr.HeaderSize);
223
224// --------------------------------------------------------------------
225// Runtime services
226EFI_RUNTIME_SERVICES_32 *efiRuntimeServices = &fakeEfiPages->efiRuntimeServices;
227efiRuntimeServices->Hdr.Signature = EFI_RUNTIME_SERVICES_SIGNATURE;
228efiRuntimeServices->Hdr.Revision = EFI_RUNTIME_SERVICES_REVISION;
229efiRuntimeServices->Hdr.HeaderSize = sizeof(EFI_RUNTIME_SERVICES_32);
230efiRuntimeServices->Hdr.CRC32 = 0;
231efiRuntimeServices->Hdr.Reserved = 0;
232
233// There are a number of function pointers in the efiRuntimeServices table.
234// These are the Foundation (e.g. core) services and are expected to be present on
235// all EFI-compliant machines.Some kernel extensions (notably AppleEFIRuntime)
236// will call these without checking to see if they are null.
237//
238// We don't really feel like doing an EFI implementation in the bootloader
239// but it is nice if we can at least prevent a complete crash by
240// at least providing some sort of implementation until one can be provided
241// nicely in a kext.
242void (*voidret_fp)() = (void*)fakeEfiPages->voidret_instructions;
243void (*unsupportedret_fp)() = (void*)fakeEfiPages->unsupportedret_instructions;
244efiRuntimeServices->GetTime = (EFI_PTR32)unsupportedret_fp;
245efiRuntimeServices->SetTime = (EFI_PTR32)unsupportedret_fp;
246efiRuntimeServices->GetWakeupTime = (EFI_PTR32)unsupportedret_fp;
247efiRuntimeServices->SetWakeupTime = (EFI_PTR32)unsupportedret_fp;
248efiRuntimeServices->SetVirtualAddressMap = (EFI_PTR32)unsupportedret_fp;
249efiRuntimeServices->ConvertPointer = (EFI_PTR32)unsupportedret_fp;
250efiRuntimeServices->GetVariable = (EFI_PTR32)unsupportedret_fp;
251efiRuntimeServices->GetNextVariableName = (EFI_PTR32)unsupportedret_fp;
252efiRuntimeServices->SetVariable = (EFI_PTR32)unsupportedret_fp;
253efiRuntimeServices->GetNextHighMonotonicCount = (EFI_PTR32)unsupportedret_fp;
254efiRuntimeServices->ResetSystem = (EFI_PTR32)voidret_fp;
255
256// We're done.Now CRC32 the thing so the kernel will accept it
257efiRuntimeServices->Hdr.CRC32 = crc32(0L, efiRuntimeServices, efiRuntimeServices->Hdr.HeaderSize);
258
259// --------------------------------------------------------------------
260// Finish filling in the rest of the boot args that we need.
261bootArgs->efiSystemTable = (uint32_t)efiSystemTable;
262bootArgs->efiMode = kBootArgsEfiMode32;
263
264// The bootArgs structure as a whole is bzero'd so we don't need to fill in
265// things like efiRuntimeServices* and what not.
266//
267// In fact, the only code that seems to use that is the hibernate code so it
268// knows not to save the pages. It even checks to make sure its nonzero.
269}
270
271void setupEfiTables64(void)
272{
273struct fake_efi_pages
274{
275EFI_SYSTEM_TABLE_64 efiSystemTable;
276EFI_RUNTIME_SERVICES_64 efiRuntimeServices;
277EFI_CONFIGURATION_TABLE_64 efiConfigurationTable[MAX_CONFIGURATION_TABLE_ENTRIES];
278EFI_CHAR16 firmwareVendor[sizeof(FIRMWARE_VENDOR)/sizeof(EFI_CHAR16)];
279uint8_t voidret_instructions[sizeof(VOIDRET_INSTRUCTIONS)/sizeof(uint8_t)];
280uint8_t unsupportedret_instructions[sizeof(UNSUPPORTEDRET_INSTRUCTIONS)/sizeof(uint8_t)];
281};
282
283struct fake_efi_pages *fakeEfiPages = (struct fake_efi_pages*)AllocateKernelMemory(sizeof(struct fake_efi_pages));
284
285// Zero out all the tables in case fields are added later
286//bzero(fakeEfiPages, sizeof(struct fake_efi_pages));
287
288// --------------------------------------------------------------------
289// Initialize some machine code that will return EFI_UNSUPPORTED for
290// functions returning int and simply return for void functions.
291memcpy(fakeEfiPages->voidret_instructions, VOIDRET_INSTRUCTIONS, sizeof(VOIDRET_INSTRUCTIONS));
292memcpy(fakeEfiPages->unsupportedret_instructions, UNSUPPORTEDRET_INSTRUCTIONS, sizeof(UNSUPPORTEDRET_INSTRUCTIONS));
293
294// --------------------------------------------------------------------
295// System table
296EFI_SYSTEM_TABLE_64 *efiSystemTable = gST64 = &fakeEfiPages->efiSystemTable;
297efiSystemTable->Hdr.Signature = EFI_SYSTEM_TABLE_SIGNATURE;
298efiSystemTable->Hdr.Revision = EFI_SYSTEM_TABLE_REVISION;
299efiSystemTable->Hdr.HeaderSize = sizeof(EFI_SYSTEM_TABLE_64);
300efiSystemTable->Hdr.CRC32 = 0; // Initialize to zero and then do CRC32
301efiSystemTable->Hdr.Reserved = 0;
302
303efiSystemTable->FirmwareVendor = ptov64((EFI_PTR32)&fakeEfiPages->firmwareVendor);
304memcpy(fakeEfiPages->firmwareVendor, FIRMWARE_VENDOR, sizeof(FIRMWARE_VENDOR));
305efiSystemTable->FirmwareRevision = FIRMWARE_REVISION;
306
307// XXX: We may need to have basic implementations of ConIn/ConOut/StdErr
308// The EFI spec states that all handles are invalid after boot services have been
309// exited so we can probably get by with leaving the handles as zero.
310efiSystemTable->ConsoleInHandle = 0;
311efiSystemTable->ConIn = 0;
312
313efiSystemTable->ConsoleOutHandle = 0;
314efiSystemTable->ConOut = 0;
315
316efiSystemTable->StandardErrorHandle = 0;
317efiSystemTable->StdErr = 0;
318
319efiSystemTable->RuntimeServices = ptov64((EFI_PTR32)&fakeEfiPages->efiRuntimeServices);
320// According to the EFI spec, BootServices aren't valid after the
321// boot process is exited so we can probably do without it.
322// Apple didn't provide a definition for it in pexpert/i386/efi.h
323// so I'm guessing they don't use it.
324efiSystemTable->BootServices = 0;
325
326efiSystemTable->NumberOfTableEntries = 0;
327efiSystemTable->ConfigurationTable = ptov64((EFI_PTR32)fakeEfiPages->efiConfigurationTable);
328
329// We're done.Now CRC32 the thing so the kernel will accept it
330gST64->Hdr.CRC32 = crc32(0L, gST64, gST64->Hdr.HeaderSize);
331
332// --------------------------------------------------------------------
333// Runtime services
334EFI_RUNTIME_SERVICES_64 *efiRuntimeServices = &fakeEfiPages->efiRuntimeServices;
335efiRuntimeServices->Hdr.Signature = EFI_RUNTIME_SERVICES_SIGNATURE;
336efiRuntimeServices->Hdr.Revision = EFI_RUNTIME_SERVICES_REVISION;
337efiRuntimeServices->Hdr.HeaderSize = sizeof(EFI_RUNTIME_SERVICES_64);
338efiRuntimeServices->Hdr.CRC32 = 0;
339efiRuntimeServices->Hdr.Reserved = 0;
340
341// There are a number of function pointers in the efiRuntimeServices table.
342// These are the Foundation (e.g. core) services and are expected to be present on
343// all EFI-compliant machines.Some kernel extensions (notably AppleEFIRuntime)
344// will call these without checking to see if they are null.
345//
346// We don't really feel like doing an EFI implementation in the bootloader
347// but it is nice if we can at least prevent a complete crash by
348// at least providing some sort of implementation until one can be provided
349// nicely in a kext.
350
351void (*voidret_fp)() = (void*)fakeEfiPages->voidret_instructions;
352void (*unsupportedret_fp)() = (void*)fakeEfiPages->unsupportedret_instructions;
353efiRuntimeServices->GetTime = ptov64((EFI_PTR32)unsupportedret_fp);
354efiRuntimeServices->SetTime = ptov64((EFI_PTR32)unsupportedret_fp);
355efiRuntimeServices->GetWakeupTime = ptov64((EFI_PTR32)unsupportedret_fp);
356efiRuntimeServices->SetWakeupTime = ptov64((EFI_PTR32)unsupportedret_fp);
357efiRuntimeServices->SetVirtualAddressMap = ptov64((EFI_PTR32)unsupportedret_fp);
358efiRuntimeServices->ConvertPointer = ptov64((EFI_PTR32)unsupportedret_fp);
359efiRuntimeServices->GetVariable = ptov64((EFI_PTR32)unsupportedret_fp);
360efiRuntimeServices->GetNextVariableName = ptov64((EFI_PTR32)unsupportedret_fp);
361efiRuntimeServices->SetVariable = ptov64((EFI_PTR32)unsupportedret_fp);
362efiRuntimeServices->GetNextHighMonotonicCount = ptov64((EFI_PTR32)unsupportedret_fp);
363efiRuntimeServices->ResetSystem = ptov64((EFI_PTR32)voidret_fp);
364
365// We're done.Now CRC32 the thing so the kernel will accept it
366efiRuntimeServices->Hdr.CRC32 = crc32(0L, efiRuntimeServices, efiRuntimeServices->Hdr.HeaderSize);
367
368// --------------------------------------------------------------------
369// Finish filling in the rest of the boot args that we need.
370bootArgs->efiSystemTable = (uint32_t)efiSystemTable;
371bootArgs->efiMode = kBootArgsEfiMode64;
372
373// The bootArgs structure as a whole is bzero'd so we don't need to fill in
374// things like efiRuntimeServices* and what not.
375//
376// In fact, the only code that seems to use that is the hibernate code so it
377// knows not to save the pages. It even checks to make sure its nonzero.
378}
379
380/*
381 * In addition to the EFI tables there is also the EFI device tree node.
382 * In particular, we need /efi/platform to have an FSBFrequency key. Without it,
383 * the tsc_init function will panic very early on in kernel startup, before
384 * the console is available.
385 */
386
387/*==========================================================================
388 * FSB Frequency detection
389 */
390
391/* These should be const but DT__AddProperty takes char* */
392static const char TSC_Frequency_prop[] = "TSCFrequency";
393static const char FSB_Frequency_prop[] = "FSBFrequency";
394static const char CPU_Frequency_prop[] = "CPUFrequency";
395
396/*==========================================================================
397 * SMBIOS
398 */
399
400/* From Foundation/Efi/Guid/Smbios/SmBios.c */
401EFI_GUID constgEfiSmbiosTableGuid = EFI_SMBIOS_TABLE_GUID;
402
403#define SMBIOS_RANGE_START0x000F0000
404#define SMBIOS_RANGE_END0x000FFFFF
405
406/* '_SM_' in little endian: */
407#define SMBIOS_ANCHOR_UINT32_LE 0x5f4d535f
408
409#define EFI_ACPI_TABLE_GUID \
410{ \
4110xeb9d2d30, 0x2d88, 0x11d3, { 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \
412}
413
414#define EFI_ACPI_20_TABLE_GUID \
415{ \
4160x8868e871, 0xe4f1, 0x11d3, { 0xbc, 0x22, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81 } \
417}
418
419EFI_GUID gEfiAcpiTableGuid = EFI_ACPI_TABLE_GUID;
420EFI_GUID gEfiAcpi20TableGuid = EFI_ACPI_20_TABLE_GUID;
421
422
423/*==========================================================================
424 * Fake EFI implementation
425 */
426
427/* These should be const but DT__AddProperty takes char* */
428static const char FIRMWARE_REVISION_PROP[] = "firmware-revision";
429static const char FIRMWARE_ABI_PROP[] = "firmware-abi";
430static const char FIRMWARE_VENDOR_PROP[] = "firmware-vendor";
431static const char FIRMWARE_ABI_32_PROP_VALUE[] = "EFI32";
432static const char FIRMWARE_ABI_64_PROP_VALUE[] = "EFI64";
433static const char SYSTEM_ID_PROP[] = "system-id";
434static const char SYSTEM_SERIAL_PROP[] = "SystemSerialNumber";
435static const char SYSTEM_TYPE_PROP[] = "system-type";
436static const char MODEL_PROP[] = "Model";
437static const char BOARDID_PROP[] = "board-id";
438
439/*
440 * Get an smbios option string option to convert to EFI_CHAR16 string
441 */
442static EFI_CHAR16* getSmbiosChar16(const char * key, size_t* len)
443{
444const char*src = getStringForKey(key, &bootInfo->smbiosConfig);
445EFI_CHAR16* dst = 0;
446size_t i = 0;
447
448if (!key || !(*key) || !len || !src) return 0;
449
450*len = strlen(src);
451dst = (EFI_CHAR16*) malloc( ((*len)+1) * 2 );
452for (; i < (*len); i++) dst[i] = src[i];
453dst[(*len)] = '\0';
454*len = ((*len)+1)*2; // return the CHAR16 bufsize in cluding zero terminated CHAR16
455return dst;
456}
457
458/*
459 * Get the SystemID from the bios dmi info
460 */
461staticEFI_CHAR8* getSmbiosUUID()
462{
463static EFI_CHAR8 uuid[UUID_LEN];
464int i, isZero, isOnes;
465SMBByte*p;
466
467p = (SMBByte*)Platform.UUID;
468
469for (i=0, isZero=1, isOnes=1; i<UUID_LEN; i++)
470{
471if (p[i] != 0x00) isZero = 0;
472if (p[i] != 0xff) isOnes = 0;
473}
474
475if (isZero || isOnes) // empty or setable means: no uuid present
476{
477verbose("No UUID present in SMBIOS System Information Table\n");
478return 0;
479}
480
481memcpy(uuid, p, UUID_LEN);
482return uuid;
483}
484
485/*
486 * return a binary UUID value from the overriden SystemID and SMUUID if found,
487 * or from the bios if not, or from a fixed value if no bios value is found
488 */
489static EFI_CHAR8* getSystemID()
490{
491// unable to determine UUID for host. Error: 35 fix
492// Rek: new SMsystemid option conforming to smbios notation standards, this option should
493// belong to smbios config only ...
494const char *sysId = getStringForKey(kSystemID, &bootInfo->chameleonConfig);
495EFI_CHAR8*ret = getUUIDFromString(sysId);
496
497if (!sysId || !ret) // try bios dmi info UUID extraction
498{
499ret = getSmbiosUUID();
500sysId = 0;
501}
502
503if (!ret) // no bios dmi UUID available, set a fixed value for system-id
504ret=getUUIDFromString((sysId = (const char*) SYSTEM_ID));
505
506verbose("Customizing SystemID with : %s\n", getStringFromUUID(ret)); // apply a nice formatting to the displayed output
507return ret;
508}
509
510/*
511 * Must be called AFTER setup Acpi because we need to take care of correct
512 * facp content to reflect in ioregs
513 */
514void setupSystemType()
515{
516Node *node = DT__FindNode("/", false);
517if (node == 0) stop("Couldn't get root node");
518// we need to write this property after facp parsing
519// Export system-type only if it has been overrriden by the SystemType option
520DT__AddProperty(node, SYSTEM_TYPE_PROP, sizeof(Platform.Type), &Platform.Type);
521}
522
523void setupEfiDeviceTree(void)
524{
525EFI_CHAR8* ret = 0;
526EFI_CHAR16* ret16 = 0;
527size_t len = 0;
528Node*node;
529
530node = DT__FindNode("/", false);
531
532if (node == 0) stop("Couldn't get root node");
533
534// We could also just do DT__FindNode("/efi/platform", true)
535// But I think eventually we want to fill stuff in the efi node
536// too so we might as well create it so we have a pointer for it too.
537node = DT__AddChild(node, "efi");
538
539if (archCpuType == CPU_TYPE_I386)
540{
541DT__AddProperty(node, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_32_PROP_VALUE), (char*)FIRMWARE_ABI_32_PROP_VALUE);
542}
543else
544{
545DT__AddProperty(node, FIRMWARE_ABI_PROP, sizeof(FIRMWARE_ABI_64_PROP_VALUE), (char*)FIRMWARE_ABI_64_PROP_VALUE);
546}
547
548DT__AddProperty(node, FIRMWARE_REVISION_PROP, sizeof(FIRMWARE_REVISION), (EFI_UINT32*)&FIRMWARE_REVISION);
549DT__AddProperty(node, FIRMWARE_VENDOR_PROP, sizeof(FIRMWARE_VENDOR), (EFI_CHAR16*)FIRMWARE_VENDOR);
550
551// TODO: Fill in other efi properties if necessary
552
553// Set up the /efi/runtime-services table node similar to the way a child node of configuration-table
554// is set up. That is, name and table properties
555Node *runtimeServicesNode = DT__AddChild(node, "runtime-services");
556
557if (archCpuType == CPU_TYPE_I386)
558{
559// The value of the table property is the 32-bit physical address for the RuntimeServices table.
560// Since the EFI system table already has a pointer to it, we simply use the address of that pointer
561// for the pointer to the property data. Warning.. DT finalization calls free on that but we're not
562// the only thing to use a non-malloc'd pointer for something in the DT
563
564DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST32->RuntimeServices);
565}
566else
567{
568DT__AddProperty(runtimeServicesNode, "table", sizeof(uint64_t), &gST64->RuntimeServices);
569}
570
571// Set up the /efi/configuration-table node which will eventually have several child nodes for
572// all of the configuration tables needed by various kernel extensions.
573gEfiConfigurationTableNode = DT__AddChild(node, "configuration-table");
574
575// Now fill in the /efi/platform Node
576Node *efiPlatformNode = DT__AddChild(node, "platform");
577
578// NOTE WELL: If you do add FSB Frequency detection, make sure to store
579// the value in the fsbFrequency global and not an malloc'd pointer
580// because the DT_AddProperty function does not copy its args.
581
582if (Platform.CPU.FSBFrequency != 0)
583DT__AddProperty(efiPlatformNode, FSB_Frequency_prop, sizeof(uint64_t), &Platform.CPU.FSBFrequency);
584
585// Export TSC and CPU frequencies for use by the kernel or KEXTs
586if (Platform.CPU.TSCFrequency != 0)
587DT__AddProperty(efiPlatformNode, TSC_Frequency_prop, sizeof(uint64_t), &Platform.CPU.TSCFrequency);
588
589if (Platform.CPU.CPUFrequency != 0)
590DT__AddProperty(efiPlatformNode, CPU_Frequency_prop, sizeof(uint64_t), &Platform.CPU.CPUFrequency);
591
592// Export system-id. Can be disabled with SystemId=No in com.apple.Boot.plist
593if ((ret=getSystemID()))
594DT__AddProperty(efiPlatformNode, SYSTEM_ID_PROP, UUID_LEN, (EFI_UINT32*) ret);
595
596// Export SystemSerialNumber if present
597if ((ret16=getSmbiosChar16("SMserial", &len)))
598DT__AddProperty(efiPlatformNode, SYSTEM_SERIAL_PROP, len, ret16);
599
600// Export Model if present
601if ((ret16=getSmbiosChar16("SMproductname", &len)))
602DT__AddProperty(efiPlatformNode, MODEL_PROP, len, ret16);
603
604// Fill /efi/device-properties node.
605setupDeviceProperties(node);
606}
607
608/*
609 * Must be called AFTER getSmbios
610 */
611void setupBoardId()
612{
613Node *node;
614node = DT__FindNode("/", false);
615if (node == 0) {
616stop("Couldn't get root node");
617}
618const char *boardid = getStringForKey("SMboardproduct", &bootInfo->smbiosConfig);
619if (boardid)
620DT__AddProperty(node, BOARDID_PROP, strlen(boardid)+1, (EFI_CHAR16*)boardid);
621}
622
623/*
624 * Populate the chosen node
625 */
626void setupChosenNode()
627{
628Node *chosenNode;
629chosenNode = DT__FindNode("/chosen", false);
630if (chosenNode == 0)
631stop("Couldn't get chosen node");
632
633int bootUUIDLength = strlen(gBootUUIDString);
634if (bootUUIDLength)
635DT__AddProperty(chosenNode, "boot-uuid", bootUUIDLength + 1, gBootUUIDString);
636}
637
638/*
639 * Load the smbios.plist override config file if any
640 */
641static void setupSmbiosConfigFile(const char *filename)
642{
643chardirSpecSMBIOS[128];
644const char *override_pathname = NULL;
645intlen = 0, err = 0;
646extern void scan_mem();
647
648// Take in account user overriding
649if (getValueForKey(kSMBIOSKey, &override_pathname, &len, &bootInfo->chameleonConfig) && len > 0)
650{
651// Specify a path to a file, e.g. SMBIOS=/Extra/macProXY.plist
652sprintf(dirSpecSMBIOS, override_pathname);
653err = loadConfigFile(dirSpecSMBIOS, &bootInfo->smbiosConfig);
654}
655else
656{
657// Check selected volume's Extra.
658sprintf(dirSpecSMBIOS, "/Extra/%s", filename);
659if ( (err = loadConfigFile(dirSpecSMBIOS, &bootInfo->smbiosConfig)) )
660{
661// Check booter volume/rdbt Extra.
662sprintf(dirSpecSMBIOS, "bt(0,0)/Extra/%s", filename);
663err = loadConfigFile(dirSpecSMBIOS, &bootInfo->smbiosConfig);
664}
665}
666
667if (err)
668{
669verbose("No SMBIOS replacement found.\n");
670}
671
672// get a chance to scan mem dynamically if user asks for it while having the config options
673// loaded as well, as opposed to when it was in scan_platform(); also load the orig. smbios
674// so that we can access dmi info, without patching the smbios yet.
675scan_mem();
676}
677
678/*
679 * Installs all the needed configuration table entries
680 */
681static void setupEfiConfigurationTable()
682{
683smbios_p = (EFI_PTR32)getSmbios(SMBIOS_PATCHED);
684addConfigurationTable(&gEfiSmbiosTableGuid, &smbios_p, NULL);
685
686setupBoardId(); //need to be called after getSmbios
687
688// Setup ACPI with DSDT overrides (mackerintel's patch)
689setupAcpi();
690
691// We've obviously changed the count.. so fix up the CRC32
692if (archCpuType == CPU_TYPE_I386)
693{
694gST32->Hdr.CRC32 = 0;
695gST32->Hdr.CRC32 = crc32(0L, gST32, gST32->Hdr.HeaderSize);
696}
697else
698{
699gST64->Hdr.CRC32 = 0;
700gST64->Hdr.CRC32 = crc32(0L, gST64, gST64->Hdr.HeaderSize);
701}
702
703// Setup the chosen node
704setupChosenNode();
705}
706
707void saveOriginalSMBIOS(void)
708{
709Node *node;
710SMBEntryPoint *origeps;
711void *tableAddress;
712
713node = DT__FindNode("/efi/platform", false);
714if (!node)
715{
716verbose("/efi/platform node not found\n");
717return;
718}
719
720origeps = getSmbios(SMBIOS_ORIGINAL);
721if (!origeps)
722{
723return;
724}
725
726tableAddress = (void *)AllocateKernelMemory(origeps->dmi.tableLength);
727if (!tableAddress)
728{
729return;
730}
731
732memcpy(tableAddress, (void *)origeps->dmi.tableAddress, origeps->dmi.tableLength);
733DT__AddProperty(node, "SMBIOS", origeps->dmi.tableLength, tableAddress);
734}
735
736/*
737 * Entrypoint from boot.c
738 */
739void setupFakeEfi(void)
740{
741// Generate efi device strings
742setup_pci_devs(root_pci_dev);
743
744readSMBIOSInfo(getSmbios(SMBIOS_ORIGINAL));
745
746// load smbios.plist file if any
747setupSmbiosConfigFile("smbios.plist");
748
749setupSMBIOSTable();
750
751// Initialize the base table
752if (archCpuType == CPU_TYPE_I386)
753{
754setupEfiTables32();
755}
756else
757{
758setupEfiTables64();
759}
760
761// Initialize the device tree
762setupEfiDeviceTree();
763
764saveOriginalSMBIOS();
765
766// Add configuration table entries to both the services table and the device tree
767setupEfiConfigurationTable();
768}
769

Archive Download this file

Revision: 2249