Chameleon

Chameleon Svn Source Tree

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

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

Archive Download this file

Revision: 1840