Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/libsaio/fake_efi.c

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

Archive Download this file

Revision: 2695