Chameleon

Chameleon Svn Source Tree

Root/branches/slice/trunkM/i386/libsaio/smbios.c

1/*
2 * SMBIOS Table Patcher, part of the Chameleon Boot Loader Project
3 *
4 * Copyright 2010 by Islam M. Ahmed Zaid. All rights reserved.
5 *
6 */
7
8
9#include "boot.h"
10#include "bootstruct.h"
11#include "smbios_getters.h"
12
13#ifndef DEBUG_SMBIOS
14#define DEBUG_SMBIOS 0
15#endif
16
17#if DEBUG_SMBIOS
18#define DBG(x...)verbose(x)
19#else
20#define DBG(x...)
21#endif
22
23#define SMBPlist&bootInfo->smbiosConfig
24/* ASSUMPTION: 16KB should be enough for the whole thing */
25#define SMB_ALLOC_SIZE16384
26
27
28//-------------------------------------------------------------------------------------------------------------------------
29// SMBIOS Plist Keys
30//-------------------------------------------------------------------------------------------------------------------------
31/* BIOS Information */
32#define kSMBBIOSInformationVendorKey"SMbiosvendor"
33#define kSMBBIOSInformationVersionKey"SMbiosversion"
34#define kSMBBIOSInformationReleaseDateKey"SMbiosdate"
35
36/* System Information */
37#define kSMBSystemInformationManufacturerKey"SMmanufacturer"
38#define kSMBSystemInformationProductNameKey"SMproductname"
39#define kSMBSystemInformationVersionKey"SMsystemversion"
40#define kSMBSystemInformationSerialNumberKey"SMserial"
41#define kSMBSystemInformationFamilyKey"SMfamily"
42
43/* Base Board */
44#define kSMBBaseBoardManufacturerKey"SMboardmanufacturer"
45#define kSMBBaseBoardProductKey"SMboardproduct"
46
47/* Processor Information */
48#define kSMBProcessorInformationExternalClockKey"SMexternalclock"
49#define kSMBProcessorInformationMaximumClockKey"SMmaximalclock"
50
51/* Memory Device */
52#define kSMBMemoryDeviceDeviceLocatorKey"SMmemdevloc"
53#define kSMBMemoryDeviceBankLocatorKey"SMmembankloc"
54#define kSMBMemoryDeviceMemoryTypeKey"SMmemtype"
55#define kSMBMemoryDeviceMemorySpeedKey"SMmemspeed"
56#define kSMBMemoryDeviceManufacturerKey"SMmemmanufacturer"
57#define kSMBMemoryDeviceSerialNumberKey"SMmemserial"
58#define kSMBMemoryDevicePartNumberKey"SMmempart"
59
60/* Apple Specific */
61#define kSMBOemProcessorTypeKey"SMcputype"
62#define kSMBOemProcessorBusSpeedKey"SMbusspeed"
63
64//-------------------------------------------------------------------------------------------------------------------------
65// Default SMBIOS Data
66//-------------------------------------------------------------------------------------------------------------------------
67/* Rewrite: use a struct */
68
69#define kDefaultVendorManufacturer"Apple Inc."
70#define kDefaultBIOSReleaseDate"11/06/2009"
71#define kDefaultSerialNumber"SOMESRLNMBR"
72#define kDefaultBoardProduct"Mac-F4208DC8"
73#define kDefaultSystemVersion"1.0"
74
75// defaults for a Mac mini
76#define kDefaultMacminiFamily"Macmini"
77#define kDefaultMacmini"Macmini1,1"
78#define kDefaultMacminiBIOSVersion" MM11.88Z.009A.B00.0903051113"
79
80// defaults for a MacBook
81#define kDefaultMacBookFamily"MacBook"
82#define kDefaultMacBook"MacBook4,1"
83#define kDefaultMacBookBIOSVersion" MB41.88Z.0073.B00.0809221748"
84
85// defaults for a MacBook Pro
86#define kDefaultMacBookProFamily"MacBookPro"
87#define kDefaultMacBookPro"MacBookPro5,1"
88#define kDefaultMacBookProBIOSVersion" MBP51.88Z.007E.B05.0906151647"
89
90// defaults for an iMac
91#define kDefaultiMacFamily"iMac"
92#define kDefaultiMac"iMac8,1"
93#define kDefaultiMacBIOSVersion" IM81.88Z.00C1.B00.0903051113"
94// defaults for an iMac11,1 core i3/i5/i7
95#define kDefaultiMacNehalem"iMac11,1"
96#define kDefaultiMacNehalemBIOSVersion" IM111.88Z.0034.B00.0802091538"
97// defaults for an iMac12,1
98#define kDefaultiMacSandy"iMac12,1"
99#define kDefaultiMacSandyBIOSVersion" IM121.88Z.0047.B00.1102091756"
100
101// defaults for a Mac Pro
102#define kDefaultMacProFamily"MacPro"
103#define kDefaultMacPro"MacPro3,1"
104#define kDefaultMacProBIOSVersion" MP31.88Z.006C.B05.0903051113"
105// defaults for a Mac Pro 4,1 core i7/Xeon
106#define kDefaultMacProNehalem"MacPro4,1"
107#define kDefaultMacProNehalemBIOSVersion" MP41.88Z.0081.B04.0903051113"
108// defaults for a Mac Pro 5,1 core i7/Xeon
109#define kDefaultMacProWestmere"MacPro5,1"
110#define kDefaultMacProWestmereBIOSVersion" MP51.88Z.007F.B00.1008031144"
111#define kDefaulMacProWestmereBIOSReleaseDate"08/03/10"
112//-------------------------------------------------------------------------------------------------------------------------
113#define MAX_DMI_TABLES 255
114typedef struct DmiNumAssocTag {
115 SMBStructHeader* dmi;
116 uint8_t type;
117} DmiNumAssoc;
118
119static DmiNumAssoc DmiTablePair[MAX_DMI_TABLES];
120static int DmiTablePairCount = 0;
121static int current_pos=0;
122static bool ftTablePairInit = true; //use twice first run and after
123
124#define getFieldOffset(struct, field)((uint8_t)(uint32_t)&(((struct *)0)->field))
125
126/**
127 * Get a table structure entry from a type specification and a smbios address
128 * return NULL if table is not found
129 */
130void getSmbiosTableStructure(struct SMBEntryPoint *smbios)
131{
132 SMBStructHeader* dmihdr=NULL;
133 SMBByte* p;
134 int i;
135
136 if (ftTablePairInit && smbios!=NULL) {
137 ftTablePairInit = false;
138#if DEBUG_SMBIOS
139 verbose(">>> SMBIOSAddr=0x%08x\n", smbios);
140 verbose(">>> DMI: addr=0x%08x, len=%d, count=%d\n", smbios->dmi.tableAddress,
141smbios->dmi.tableLength, smbios->dmi.structureCount);
142#endif
143 p = (SMBByte *) smbios->dmi.tableAddress;
144 for (i=0;
145 i < smbios->dmi.structureCount &&
146 p + 4 <= (SMBByte *)smbios->dmi.tableAddress + smbios->dmi.tableLength;
147 i++) {
148 dmihdr = (SMBStructHeader *) p;
149
150#if DEBUG_SMBIOS
151 // verbose(">>>>>> DMI(%d): type=0x%02x, len=0x%d\n",i,dmihdr->type,dmihdr->length);
152#endif
153 if (dmihdr->length < 4 || dmihdr->type == 127 /* EOT */) break;
154 if (DmiTablePairCount < MAX_DMI_TABLES) {
155 DmiTablePair[DmiTablePairCount].dmi = dmihdr;
156 DmiTablePair[DmiTablePairCount].type = dmihdr->type;
157 DmiTablePairCount++;
158 }
159 else {
160 verbose("DMI table entries list is full! Next entries won't be stored.\n");
161 }
162#if DEBUG_SMBIOS
163 verbose("DMI header found for table type %d, length = %d\n", dmihdr->type, dmihdr->length);
164#endif
165 p = p + dmihdr->length;
166 while ((p - (SMBByte *)smbios->dmi.tableAddress + 1 < smbios->dmi.tableLength) && (p[0] != 0x00 || p[1] != 0x00)) {
167 p++;
168}
169 p += 2;
170}
171
172 }
173}
174
175typedef struct {
176SMBStructHeader *orig;
177SMBStructHeader *new;
178} SMBStructPtrs;
179
180struct {
181char *vendor;
182char *version;
183char *releaseDate;
184} defaultBIOSInfo;
185
186struct {
187char *manufacturer;
188char *productName;
189char *version;
190char *serialNumber;
191char *family;
192} defaultSystemInfo;
193
194struct {
195char *manufacturer;
196char *product;
197} defaultBaseBoard;
198
199
200typedef struct {
201uint8_ttype;
202SMBValueTypevalueType;
203uint8_tfieldOffset;
204char*keyString;
205bool(*getSMBValue)(returnType *);
206char**defaultValue;
207} SMBValueSetter;
208
209SMBValueSetter SMBSetters[] =
210{
211//-------------------------------------------------------------------------------------------------------------------------
212// BIOSInformation
213//-------------------------------------------------------------------------------------------------------------------------
214{kSMBTypeBIOSInformation,kSMBString,getFieldOffset(SMBBIOSInformation, vendor),kSMBBIOSInformationVendorKey,
215NULL,&defaultBIOSInfo.vendor},
216
217{kSMBTypeBIOSInformation,kSMBString,getFieldOffset(SMBBIOSInformation, version),kSMBBIOSInformationVersionKey,
218NULL,&defaultBIOSInfo.version},
219
220{kSMBTypeBIOSInformation,kSMBString,getFieldOffset(SMBBIOSInformation, releaseDate),kSMBBIOSInformationReleaseDateKey,
221NULL,&defaultBIOSInfo.releaseDate},
222
223//-------------------------------------------------------------------------------------------------------------------------
224// SystemInformation
225//-------------------------------------------------------------------------------------------------------------------------
226{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, manufacturer),kSMBSystemInformationManufacturerKey,
227NULL,&defaultSystemInfo.manufacturer},
228
229{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, productName),kSMBSystemInformationProductNameKey,
230NULL,&defaultSystemInfo.productName},
231
232{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, version),kSMBSystemInformationVersionKey,
233NULL,&defaultSystemInfo.version},
234
235{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, serialNumber),kSMBSystemInformationSerialNumberKey,
236NULL,&defaultSystemInfo.serialNumber},
237
238{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, skuNumber),NULL,
239NULL,NULL},
240
241{kSMBTypeSystemInformation,kSMBString,getFieldOffset(SMBSystemInformation, family),kSMBSystemInformationFamilyKey,
242NULL,&defaultSystemInfo.family},
243
244
245//-------------------------------------------------------------------------------------------------------------------------
246// BaseBoard
247//-------------------------------------------------------------------------------------------------------------------------
248{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, manufacturer),kSMBBaseBoardManufacturerKey,
249NULL,&defaultBaseBoard.manufacturer},
250
251{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, product),kSMBBaseBoardProductKey,
252NULL,&defaultBaseBoard.product},
253
254{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, version),NULL,NULL,NULL},
255
256{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, serialNumber),NULL,NULL,NULL},
257
258{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, assetTagNumber),NULL,NULL,NULL},
259
260{kSMBTypeBaseBoard,kSMBString,getFieldOffset(SMBBaseBoard, locationInChassis),NULL,NULL,NULL},
261
262
263//-------------------------------------------------------------------------------------------------------------------------
264// ProcessorInformation
265//-------------------------------------------------------------------------------------------------------------------------
266{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, socketDesignation),NULL,NULL,NULL},
267
268{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, manufacturer),NULL,NULL,NULL},
269
270{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, processorVersion),NULL,NULL,NULL},
271
272{kSMBTypeProcessorInformation,kSMBWord,getFieldOffset(SMBProcessorInformation, externalClock),kSMBProcessorInformationExternalClockKey,
273getProcessorInformationExternalClock,NULL},
274
275{kSMBTypeProcessorInformation,kSMBWord,getFieldOffset(SMBProcessorInformation, maximumClock),kSMBProcessorInformationMaximumClockKey,
276getProcessorInformationMaximumClock,NULL},
277
278{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, serialNumber),NULL,NULL,NULL},
279
280{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, assetTag),NULL,NULL,NULL},
281
282{kSMBTypeProcessorInformation,kSMBString,getFieldOffset(SMBProcessorInformation, partNumber),NULL,NULL,NULL},
283
284//-------------------------------------------------------------------------------------------------------------------------
285// Memory Device
286//-------------------------------------------------------------------------------------------------------------------------
287{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, deviceLocator),kSMBMemoryDeviceDeviceLocatorKey,
288NULL,NULL},
289
290{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, bankLocator),kSMBMemoryDeviceBankLocatorKey,
291NULL,NULL},
292
293{kSMBTypeMemoryDevice,kSMBByte,getFieldOffset(SMBMemoryDevice, memoryType),kSMBMemoryDeviceMemoryTypeKey,
294getSMBMemoryDeviceMemoryType,NULL},
295
296{kSMBTypeMemoryDevice,kSMBWord,getFieldOffset(SMBMemoryDevice, memorySpeed),kSMBMemoryDeviceMemorySpeedKey,
297getSMBMemoryDeviceMemorySpeed,NULL},
298
299{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, manufacturer),kSMBMemoryDeviceManufacturerKey,
300getSMBMemoryDeviceManufacturer,NULL},
301
302{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, serialNumber),kSMBMemoryDeviceSerialNumberKey,
303getSMBMemoryDeviceSerialNumber,NULL},
304
305{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, assetTag),NULL,NULL,NULL},
306
307{kSMBTypeMemoryDevice,kSMBString,getFieldOffset(SMBMemoryDevice, partNumber),kSMBMemoryDevicePartNumberKey,
308getSMBMemoryDevicePartNumber,NULL},
309
310
311//-------------------------------------------------------------------------------------------------------------------------
312// Apple Specific
313//-------------------------------------------------------------------------------------------------------------------------
314{kSMBTypeOemProcessorType,kSMBWord,getFieldOffset(SMBOemProcessorType, ProcessorType),kSMBOemProcessorTypeKey,
315getSMBOemProcessorType,NULL},
316
317{kSMBTypeOemProcessorBusSpeed,kSMBWord,getFieldOffset(SMBOemProcessorBusSpeed, ProcessorBusSpeed),kSMBOemProcessorBusSpeedKey,
318getSMBOemProcessorBusSpeed,NULL}
319};
320
321int numOfSetters = sizeof(SMBSetters) / sizeof(SMBValueSetter);
322
323
324SMBEntryPoint *origeps= 0;
325SMBEntryPoint *neweps= 0;
326
327static uint8_t stringIndex;// increament when a string is added and set the field value accordingly
328static uint8_t stringsSize;// add string size
329
330static SMBWord tableLength= 0;
331static SMBWord handle= 0;
332static SMBWord maxStructSize= 0;
333static SMBWord structureCount= 0;
334
335/* Rewrite this function */
336void setDefaultSMBData(void)
337{
338defaultBIOSInfo.vendor= kDefaultVendorManufacturer;
339defaultBIOSInfo.releaseDate= kDefaultBIOSReleaseDate;
340
341defaultSystemInfo.manufacturer= kDefaultVendorManufacturer;
342defaultSystemInfo.version= kDefaultSystemVersion;
343defaultSystemInfo.serialNumber= kDefaultSerialNumber;
344
345defaultBaseBoard.manufacturer= kDefaultVendorManufacturer;
346defaultBaseBoard.product= kDefaultBoardProduct;
347
348if (Platform->Type == 2)//platformCPUFeature(CPU_FEATURE_MOBILE))
349{
350if (vgaVendor != PCI_VENDOR_ID_INTEL) //Slice
351{
352defaultBIOSInfo.version= kDefaultMacBookProBIOSVersion;
353defaultSystemInfo.productName= kDefaultMacBookPro;
354defaultSystemInfo.family= kDefaultMacBookProFamily;
355}
356else
357{
358defaultBIOSInfo.version= kDefaultMacBookBIOSVersion;
359defaultSystemInfo.productName= kDefaultMacBook;
360defaultSystemInfo.family= kDefaultMacBookFamily;
361}
362}
363else
364{
365switch (Platform->CPU.NoCores)
366{
367case 1:
368defaultBIOSInfo.version= kDefaultMacminiBIOSVersion;
369defaultSystemInfo.productName= kDefaultMacmini;
370defaultSystemInfo.family= kDefaultMacminiFamily;
371break;
372
373case 2:
374defaultBIOSInfo.version= kDefaultiMacBIOSVersion;
375defaultSystemInfo.productName= kDefaultiMac;
376defaultSystemInfo.family= kDefaultiMacFamily;
377break;
378default:
379{
380switch (Platform->CPU.Family)
381{
382case 0x06:
383{
384switch (Platform->CPU.Model)
385{
386case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
387case CPU_MODEL_DALES:
388case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
389case 0x19:// ??? Intel Core i5 650 @3.20 GHz
390defaultBIOSInfo.version= kDefaultiMacNehalemBIOSVersion;
391defaultSystemInfo.productName= kDefaultiMacNehalem;
392defaultSystemInfo.family= kDefaultiMacFamily;
393break;
394
395case CPU_MODEL_SANDY:// Intel Core i3, i5, i7 LGA1155 (32nm)
396case CPU_MODEL_SANDY_XEON:// Intel Xeon E3
397defaultBIOSInfo.version= kDefaultiMacSandyBIOSVersion;
398defaultSystemInfo.productName= kDefaultiMacSandy;
399defaultSystemInfo.family= kDefaultiMacFamily;
400break;
401case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
402case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65x
403defaultBIOSInfo.version= kDefaultMacProNehalemBIOSVersion;
404defaultSystemInfo.productName= kDefaultMacProNehalem;
405defaultSystemInfo.family= kDefaultMacProFamily;
406break;
407
408case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
409case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
410defaultBIOSInfo.version= kDefaultMacProWestmereBIOSVersion;
411defaultBIOSInfo.releaseDate= kDefaulMacProWestmereBIOSReleaseDate;
412defaultSystemInfo.productName= kDefaultMacProWestmere;
413defaultSystemInfo.family= kDefaultMacProFamily;
414break;
415
416default:
417defaultBIOSInfo.version= kDefaultMacProBIOSVersion;
418defaultSystemInfo.productName= kDefaultMacPro;
419defaultSystemInfo.family= kDefaultMacProFamily;
420break;
421}
422break;
423}
424default:
425defaultBIOSInfo.version= kDefaultMacProBIOSVersion;
426defaultSystemInfo.productName= kDefaultMacPro;
427defaultSystemInfo.family= kDefaultMacProFamily;
428break;
429}
430break;
431}
432}
433}
434}
435
436/* Used for SM*n smbios.plist keys */
437bool getSMBValueForKey(SMBStructHeader *structHeader, const char *keyString, const char **string, returnType *value)
438{
439static int idx = -1;
440static int current = -1;
441int len;
442char key[24];
443
444if (current != structHeader->handle)
445{
446idx++;
447current = structHeader->handle;
448}
449
450sprintf(key, "%s%d", keyString, idx);
451
452if (value)
453if (getIntForKey(key, (int *)&(value->dword), SMBPlist))
454return true;
455else
456if (getValueForKey(key, string, &len, SMBPlist))
457return true;
458return false;
459}
460
461char *getSMBStringForField(SMBStructHeader *structHeader, uint8_t field)
462{
463uint8_t *stringPtr = (uint8_t *)structHeader + structHeader->length;
464
465if (!field)
466return NULL;
467
468for (field--; field != 0 && strlen((char *)stringPtr) > 0;
469field--, stringPtr = (uint8_t *)((uint32_t)stringPtr + strlen((char *)stringPtr) + 1));
470
471return (char *)stringPtr;
472}
473
474void setSMBStringForField(SMBStructHeader *structHeader, const char *string, uint8_t *field)
475{
476int strSize;
477
478if (!field)
479return;
480if (!string)
481{
482*field = 0;
483return;
484}
485
486strSize = strlen(string);
487
488// remove any spaces found at the end
489while ((strSize != 0) && (string[strSize - 1] == ' '))
490strSize--;
491
492if (strSize == 0)
493{
494*field = 0;
495return;
496}
497
498memcpy((uint8_t *)structHeader + structHeader->length + stringsSize, string, strSize);
499*field = stringIndex;
500
501stringIndex++;
502stringsSize += strSize + 1;
503}
504
505bool setSMBValue(SMBStructPtrs *structPtr, int idx, returnType *value)
506{
507const char *string = 0;
508int len;
509bool parsed;
510int val;
511
512if (numOfSetters <= idx)
513return false;
514
515switch (SMBSetters[idx].valueType)
516{
517case kSMBString:
518if (SMBSetters[idx].keyString)
519{
520if (getValueForKey(SMBSetters[idx].keyString, &string, &len, SMBPlist))
521break;
522else
523if (structPtr->orig->type == kSMBTypeMemoryDevice)// MemoryDevice only
524if (getSMBValueForKey(structPtr->orig, SMBSetters[idx].keyString, &string, NULL))
525break;
526}
527if (SMBSetters[idx].getSMBValue)
528if (SMBSetters[idx].getSMBValue((returnType *)&string))
529break;
530if ((SMBSetters[idx].defaultValue) && *(SMBSetters[idx].defaultValue))
531{
532string = *(SMBSetters[idx].defaultValue);
533break;
534}
535string = getSMBStringForField(structPtr->orig, *(uint8_t *)value);
536break;
537
538case kSMBByte:
539case kSMBWord:
540case kSMBDWord:
541//case kSMBQWord:
542if (SMBSetters[idx].keyString)
543{
544parsed = getIntForKey(SMBSetters[idx].keyString, &val, SMBPlist);
545if (!parsed)
546if (structPtr->orig->type == kSMBTypeMemoryDevice)// MemoryDevice only
547parsed = getSMBValueForKey(structPtr->orig, SMBSetters[idx].keyString, NULL, (returnType *)&val);
548if (parsed)
549{
550switch (SMBSetters[idx].valueType)
551{
552case kSMBByte:
553value->byte = (uint8_t)val;
554break;
555case kSMBWord:
556value->word = (uint16_t)val;
557break;
558case kSMBDWord:
559default:
560value->dword = (uint32_t)val;
561break;
562}
563return true;
564}
565}
566
567if (SMBSetters[idx].getSMBValue)
568if (SMBSetters[idx].getSMBValue(value))
569return true;
570#if 0
571if (*(SMBSetters[idx].defaultValue))
572{
573value->dword = *(uint32_t *)(SMBSetters[idx].defaultValue);
574return true;
575}
576#endif
577break;
578}
579
580if (SMBSetters[idx].valueType == kSMBString && string)
581setSMBStringForField(structPtr->new, string, &value->byte);
582
583return true;
584}
585
586//-------------------------------------------------------------------------------------------------------------------------
587// Apple Specific
588//-------------------------------------------------------------------------------------------------------------------------
589void addSMBFirmwareVolume(SMBStructPtrs *structPtr)
590{
591return;
592}
593
594void addSMBMemorySPD(SMBStructPtrs *structPtr)
595{
596/* SPD data from Platform->RAM.spd */
597return;
598}
599
600void addSMBOemProcessorType(SMBStructPtrs *structPtr)
601{
602SMBOemProcessorType *p = (SMBOemProcessorType *)structPtr->new;
603
604p->header.type= kSMBTypeOemProcessorType;
605p->header.length= sizeof(SMBOemProcessorType);
606p->header.handle= handle++;
607
608setSMBValue(structPtr, numOfSetters - 2 , (returnType *)&(p->ProcessorType));
609
610structPtr->new = (SMBStructHeader *)((uint8_t *)structPtr->new + sizeof(SMBOemProcessorType) + 2);
611tableLength += sizeof(SMBOemProcessorType) + 2;
612structureCount++;
613}
614
615void addSMBOemProcessorBusSpeed(SMBStructPtrs *structPtr)
616{
617SMBOemProcessorBusSpeed *p = (SMBOemProcessorBusSpeed *)structPtr->new;
618SMBWord tmp = 0;
619switch (Platform->CPU.Family)
620{
621case 0x06:
622{
623switch (Platform->CPU.Model)
624{
625case 0x19:// ??? Intel Core i5 650 @3.20 GHz
626case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
627case CPU_MODEL_DALES:
628case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
629case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
630case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65x
631case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
632case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
633case CPU_MODEL_SANDY:
634case CPU_MODEL_SANDY_XEON:
635tmp = p->ProcessorBusSpeed;
636break;
637
638default:
639tmp = (Platform->CPU.FSBFrequency * 4) / MEGA;
640break;
641//return;
642}
643}
644}
645
646p->header.type= kSMBTypeOemProcessorBusSpeed;
647p->header.length= sizeof(SMBOemProcessorBusSpeed);
648p->header.handle= handle++;
649
650if(!setSMBValue(structPtr, numOfSetters -1, (returnType *)&(p->ProcessorBusSpeed)))
651p->ProcessorBusSpeed = tmp;
652
653structPtr->new = (SMBStructHeader *)((uint8_t *)structPtr->new + sizeof(SMBOemProcessorBusSpeed) + 2);
654tableLength += sizeof(SMBOemProcessorBusSpeed) + 2;
655structureCount++;
656}
657
658//-------------------------------------------------------------------------------------------------------------------------
659// EndOfTable
660//-------------------------------------------------------------------------------------------------------------------------
661void addSMBEndOfTable(SMBStructPtrs *structPtr)
662{
663structPtr->new->type= kSMBTypeEndOfTable;
664structPtr->new->length= sizeof(SMBStructHeader);
665structPtr->new->handle= handle++;
666
667structPtr->new = (SMBStructHeader *)((uint8_t *)structPtr->new + sizeof(SMBStructHeader) + 2);
668tableLength += sizeof(SMBStructHeader) + 2;
669structureCount++;
670}
671
672void setSMBStruct(SMBStructPtrs *structPtr)
673{
674bool setterFound = false;
675
676uint8_t *ptr;
677SMBWord structSize;
678int i;
679
680stringIndex = 1;
681stringsSize = 0;
682
683if (handle < structPtr->orig->handle)
684handle = structPtr->orig->handle;
685
686memcpy((void *)structPtr->new, structPtr->orig, structPtr->orig->length);
687
688for (i = 0; i < numOfSetters; i++)
689if ((structPtr->orig->type == SMBSetters[i].type) && (SMBSetters[i].fieldOffset < structPtr->orig->length))
690{
691setterFound = true;
692setSMBValue(structPtr, i, (returnType *)((uint8_t *)structPtr->new + SMBSetters[i].fieldOffset));
693}
694
695if (setterFound)
696{
697ptr = (uint8_t *)structPtr->new + structPtr->orig->length;
698for (; ((uint16_t *)ptr)[0] != 0; ptr++);
699
700if (((uint16_t *)ptr)[0] == 0)
701ptr += 2;
702
703structSize = ptr - (uint8_t *)structPtr->new;
704}
705else
706{
707ptr = (uint8_t *)structPtr->orig + structPtr->orig->length;
708for (; ((uint16_t *)ptr)[0] != 0; ptr++);
709
710if (((uint16_t *)ptr)[0] == 0)
711ptr += 2;
712
713structSize = ptr - (uint8_t *)structPtr->orig;
714memcpy((void *)structPtr->new, structPtr->orig, structSize);
715}
716
717structPtr->new = (SMBStructHeader *)((uint8_t *)structPtr->new + structSize);
718
719tableLength += structSize;
720
721if (structSize > maxStructSize)
722maxStructSize = structSize;
723
724structureCount++;
725}
726
727void setupNewSMBIOSTable(SMBEntryPoint *eps, SMBStructPtrs *structPtr)
728{
729uint8_t *ptr = (uint8_t *)eps->dmi.tableAddress;
730structPtr->orig = (SMBStructHeader *)ptr;
731
732for (;((eps->dmi.tableAddress + eps->dmi.tableLength) > ((uint32_t)(uint8_t *)structPtr->orig + sizeof(SMBStructHeader)));)
733{
734switch (structPtr->orig->type)
735{
736/* Skip all Apple Specific Structures */
737case kSMBTypeFirmwareVolume:
738case kSMBTypeMemorySPD:
739case kSMBTypeOemProcessorType:
740case kSMBTypeOemProcessorBusSpeed:
741/* And this one too, to be added at the end */
742case kSMBTypeEndOfTable:
743break;
744
745default:
746/* Add */
747setSMBStruct(structPtr);
748break;
749}
750
751ptr = (uint8_t *)((uint32_t)structPtr->orig + structPtr->orig->length);
752for (; ((uint16_t *)ptr)[0] != 0; ptr++);
753
754if (((uint16_t *)ptr)[0] == 0)
755ptr += 2;
756
757structPtr->orig = (SMBStructHeader *)ptr;
758}
759
760addSMBFirmwareVolume(structPtr);
761addSMBMemorySPD(structPtr);
762addSMBOemProcessorType(structPtr);
763addSMBOemProcessorBusSpeed(structPtr);
764
765addSMBEndOfTable(structPtr);
766}
767
768void setupSMBIOSTable(void)
769{
770SMBStructPtrs *structPtr;
771uint8_t *buffer;
772bool setSMB = true;
773
774if (!origeps)
775return;
776
777neweps = origeps;
778
779structPtr = (SMBStructPtrs *)malloc(sizeof(SMBStructPtrs));
780if (!structPtr)
781return;
782
783buffer = malloc(SMB_ALLOC_SIZE);
784if (!buffer)
785return;
786
787bzero(buffer, SMB_ALLOC_SIZE);
788structPtr->new = (SMBStructHeader *)buffer;
789
790getBoolForKey(kSMBIOSdefaults, &setSMB, &bootInfo->chameleonConfig);
791if (setSMB)
792setDefaultSMBData();
793
794setupNewSMBIOSTable(origeps, structPtr);
795
796neweps = (SMBEntryPoint *)AllocateKernelMemory(sizeof(SMBEntryPoint));
797if (!neweps)
798return;
799bzero(neweps, sizeof(SMBEntryPoint));
800
801neweps->anchor[0]= '_';
802neweps->anchor[1]= 'S';
803neweps->anchor[2]= 'M';
804neweps->anchor[3]= '_';
805neweps->entryPointLength= sizeof(SMBEntryPoint);
806neweps->majorVersion= 2;
807neweps->minorVersion= 4;
808neweps->maxStructureSize= maxStructSize;
809neweps->entryPointRevision= 0;
810
811neweps->dmi.anchor[0]= '_';
812neweps->dmi.anchor[1]= 'D';
813neweps->dmi.anchor[2]= 'M';
814neweps->dmi.anchor[3]= 'I';
815neweps->dmi.anchor[4]= '_';
816neweps->dmi.tableLength= tableLength;
817neweps->dmi.tableAddress= AllocateKernelMemory(tableLength);
818neweps->dmi.structureCount= structureCount;
819neweps->dmi.bcdRevision= 0x24;
820
821if (!neweps->dmi.tableAddress)
822return;
823
824memcpy((void *)neweps->dmi.tableAddress, buffer, tableLength);
825
826neweps->dmi.checksum= 0;
827neweps->dmi.checksum= 0x100 - checksum8(&neweps->dmi, sizeof(DMIEntryPoint));
828
829neweps->checksum= 0;
830neweps->checksum= 0x100 - checksum8(neweps, sizeof(SMBEntryPoint));
831
832free(buffer);
833decodeSMBIOSTable(neweps);
834DBG("SMBIOS orig=%x new=%x\n", origeps, neweps);
835}
836
837void *getSmbios(int which)
838{
839switch (which)
840{
841case SMBIOS_ORIGINAL:
842if (!origeps)
843origeps = getAddressOfSmbiosTable();
844return origeps;
845case SMBIOS_PATCHED:
846return neweps;
847}
848
849return 0;
850}
851
852/** Find first original dmi Table with a particular type */
853SMBStructHeader* FindFirstDmiTableOfType(int type, int minlength)
854{
855 current_pos = 0; //static variable
856
857 return FindNextDmiTableOfType(type, minlength);
858};
859
860/** Find next original dmi Table with a particular type */
861SMBStructHeader* FindNextDmiTableOfType(int type, int minlength)
862{
863 int i;
864
865// if (ftTablePairInit) getSmbios(SMBIOS_ORIGINAL);
866
867 for (i=current_pos; i < DmiTablePairCount; i++) {
868 if (type == DmiTablePair[i].type &&
869 DmiTablePair[i].dmi &&
870 DmiTablePair[i].dmi->length >= minlength ) {
871 current_pos = i+1;
872#if DEBUG_SMBIOS
873DBG("SMBIOS table type %d found\n", type);
874printf("Press a key to continue... (DEBUG)\n");
875getchar();
876#endif
877
878 return DmiTablePair[i].dmi;
879 }
880 }
881DBG("SMBIOS table type %d not found\n", type);
882#if DEBUG_ACPI
883printf("Press a key to continue... (DEBUG)\n");
884getchar();
885#endif
886 return NULL; // not found
887};
888
889
890/* Collect any information needed later */
891void readSMBIOSInfo(SMBEntryPoint *eps)
892{
893uint8_t *structPtr = (uint8_t *)eps->dmi.tableAddress;
894SMBStructHeader *structHeader = (SMBStructHeader *)structPtr;
895SMBByte tmp = 0;
896
897int dimmnbr = 0;
898Platform->DMI.MaxMemorySlots= 0;
899Platform->DMI.CntMemorySlots= 0;
900Platform->DMI.MemoryModules= 0;
901
902for (;((eps->dmi.tableAddress + eps->dmi.tableLength) > ((uint32_t)(uint8_t *)structHeader + sizeof(SMBStructHeader)));)
903{
904switch (structHeader->type)
905{
906case kSMBTypeSystemInformation:
907Platform->UUID = ((SMBSystemInformation *)structHeader)->uuid;
908break;
909//Slice - platform mobility should based on Enclosure but not on CPU type
910case kSMBTypeSystemEnclosure:
911tmp = ((SMBSystemEnclosure *)structHeader)->type;
912Platform->Type = (tmp >=8)?2:1;
913break;
914//Slice - values from DMI/SMBIOS are defined correct if overclocked
915// do not need in complex MSR calculation
916case kSMBTypeProcessorInformation:
917tmp = ((SMBProcessorInformation *)structHeader)->externalClock;
918Platform->CPU.FSBFrequency = tmp * MEGA + (tmp & 7) * 110000; //According to Intel 133->133.33MHz
919tmp = ((SMBProcessorInformation *)structHeader)->currentClock;
920Platform->CPU.CPUFrequency = tmp * MEGA + (tmp & 7) * 110000;
921break;
922
923case kSMBTypePhysicalMemoryArray:
924Platform->DMI.MaxMemorySlots += ((SMBPhysicalMemoryArray *)structHeader)->numMemoryDevices;
925break;
926
927case kSMBTypeMemoryDevice:
928Platform->DMI.CntMemorySlots++;
929if (((SMBMemoryDevice *)structHeader)->memorySize != 0)
930Platform->DMI.MemoryModules++;
931if (((SMBMemoryDevice *)structHeader)->memorySpeed > 0)
932Platform->RAM.DIMM[dimmnbr].Frequency = ((SMBMemoryDevice *)structHeader)->memorySpeed;
933dimmnbr++;
934break;
935}
936
937structPtr = (uint8_t *)((uint32_t)structHeader + structHeader->length);
938for (; ((uint16_t *)structPtr)[0] != 0; structPtr++);
939
940if (((uint16_t *)structPtr)[0] == 0)
941structPtr += 2;
942
943structHeader = (SMBStructHeader *)structPtr;
944}
945}
946

Archive Download this file

Revision: 1201