Chameleon

Chameleon Svn Source Tree

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

1/*
2 * Add (c) here
3 *
4 * Copyright .... All rights reserved.
5 *
6 */
7
8#include "smbios_getters.h"
9#include "bootstruct.h"
10
11#ifndef DEBUG_SMBIOS
12#define DEBUG_SMBIOS 0
13#endif
14
15#if DEBUG_SMBIOS
16#define DBG(x...)printf(x)
17#else
18#define DBG(x...)
19#endif
20
21//Slice - for ACPI patcher templates
22intModelLength = 0;
23charMacModel[8] = "MacBook";
24unsigned int ModelRev = 0x00010001;
25uint64_t smbios_p;
26char*gSMBIOSBoardModel;
27
28bool getProcessorInformationExternalClock(returnType *value)
29{
30value->word = Platform->CPU.FSBFrequency/1000000;
31return true;
32}
33
34bool getProcessorInformationMaximumClock(returnType *value)
35{
36value->word = Platform->CPU.CPUFrequency/1000000;
37return true;
38}
39
40bool getSMBOemProcessorBusSpeed(returnType *value)
41{
42if (Platform->CPU.Vendor == 0x756E6547) // Intel
43{
44switch (Platform->CPU.Family)
45{
46case 0x06:
47{
48switch (Platform->CPU.Model)
49{
50case 0x0D:// ???
51case CPU_MODEL_YONAH:// Intel Mobile Core Solo, Duo
52case CPU_MODEL_MEROM:// Intel Mobile Core 2 Solo, Duo, Xeon 30xx, Xeon 51xx, Xeon X53xx, Xeon E53xx, Xeon X32xx
53case CPU_MODEL_PENRYN:// Intel Core 2 Solo, Duo, Quad, Extreme, Xeon X54xx, Xeon X33xx
54case CPU_MODEL_ATOM:// Intel Atom (45nm)
55return false;
56
57case 0x19:// ??? Intel Core i5 650 @3.20 GHz
58case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
59case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
60case CPU_MODEL_DALES:
61case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
62case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
63case CPU_MODEL_NEHALEM_EX:// Intel Xeon X75xx, Xeon X65xx, Xeon E75xx, Xeon E65x
64case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
65{
66// thanks to dgobe for i3/i5/i7 bus speed detection
67int nhm_bus = 0x3F;
68static long possible_nhm_bus[] = {0xFF, 0x7F, 0x3F};
69unsigned long did, vid;
70int i;
71
72// Nehalem supports Scrubbing
73// First, locate the PCI bus where the MCH is located
74for(i = 0; i < sizeof(possible_nhm_bus); i++)
75{
76vid = pci_config_read16(PCIADDR(possible_nhm_bus[i], 3, 4), 0x00);
77did = pci_config_read16(PCIADDR(possible_nhm_bus[i], 3, 4), 0x02);
78vid &= 0xFFFF;
79did &= 0xFF00;
80
81if(vid == 0x8086 && did >= 0x2C00)
82nhm_bus = possible_nhm_bus[i];
83}
84
85unsigned long qpimult, qpibusspeed;
86qpimult = pci_config_read32(PCIADDR(nhm_bus, 2, 1), 0x50);
87qpimult &= 0x7F;
88DBG("qpimult %d\n", qpimult);
89qpibusspeed = (qpimult * 2 * (Platform->CPU.FSBFrequency/1000000));
90// Rek: rounding decimals to match original mac profile info
91if (qpibusspeed%100 != 0)qpibusspeed = ((qpibusspeed+50)/100)*100;
92DBG("qpibusspeed %d\n", qpibusspeed);
93value->word = qpibusspeed;
94return true;
95}
96}
97}
98}
99}
100return false;
101}
102
103uint16_t simpleGetSMBOemProcessorType(void)
104{
105if (Platform->CPU.NoCores >= 4)
106{
107return 0x0501;// Quad-Core Xeon
108}
109else if (Platform->CPU.NoCores == 1)
110{
111return 0x0201;// Core Solo
112};
113
114return 0x0301;// Core 2 Duo
115}
116
117bool getSMBOemProcessorType(returnType *value)
118{
119static bool done = false;
120
121value->word = simpleGetSMBOemProcessorType();
122
123if (Platform->CPU.Vendor == 0x756E6547) // Intel
124{
125if (!done)
126{
127verbose("CPU is %s, family 0x%x, model 0x%x\n", Platform->CPU.BrandString, Platform->CPU.Family, Platform->CPU.Model);
128done = true;
129}
130
131switch (Platform->CPU.Family)
132{
133case 0x06:
134{
135switch (Platform->CPU.Model)
136{
137case 0x0D:// ???
138case CPU_MODEL_YONAH:// Intel Mobile Core Solo, Duo
139case CPU_MODEL_MEROM:// Intel Mobile Core 2 Solo, Duo, Xeon 30xx, Xeon 51xx, Xeon X53xx, Xeon E53xx, Xeon X32xx
140case CPU_MODEL_PENRYN:// Intel Core 2 Solo, Duo, Quad, Extreme, Xeon X54xx, Xeon X33xx
141case CPU_MODEL_ATOM:// Intel Atom (45nm)
142return true;
143
144case CPU_MODEL_NEHALEM:// Intel Core i7, Xeon W35xx, Xeon X55xx, Xeon E55xx LGA1366 (45nm)
145if (strstr(Platform->CPU.BrandString, "Xeon(R)"))
146value->word = 0x0501;// Xeon
147else
148value->word = 0x0701;// Core i7
149
150return true;
151
152case CPU_MODEL_FIELDS:// Intel Core i5, i7, Xeon X34xx LGA1156 (45nm)
153if (strstr(Platform->CPU.BrandString, "Core(TM) i5"))
154value->word = 0x0601;// Core i5
155else
156value->word = 0x0701;// Core i7
157return true;
158
159case CPU_MODEL_DALES:
160if (strstr(Platform->CPU.BrandString, "Core(TM) i5"))
161value->word = 0x0601;// Core i5
162else
163value->word = 0x0701;// Core i7
164return true;
165
166case CPU_MODEL_SANDY:// Intel Core i3, i5, i7 LGA1155 (32nm)
167 case CPU_MODEL_SANDY_XEON:// Intel Xeon E3
168case CPU_MODEL_DALES_32NM:// Intel Core i3, i5 LGA1156 (32nm)
169if (strstr(Platform->CPU.BrandString, "Core(TM) i3"))
170value->word = 0x0901;// Core i3
171else
172if (strstr(Platform->CPU.BrandString, "Core(TM) i5"))
173value->word = 0x0601;// Core i5
174else
175value->word = 0x0701;// Core i7
176return true;
177
178case CPU_MODEL_WESTMERE:// Intel Core i7, Xeon X56xx, Xeon E56xx, Xeon W36xx LGA1366 (32nm) 6 Core
179case CPU_MODEL_WESTMERE_EX:// Intel Xeon E7
180value->word = 0x0501;// Core i7
181return true;
182
183case 0x19:// ??? Intel Core i5 650 @3.20 GHz
184value->word = 0x0601;// Core i5
185return true;
186}
187}
188}
189}
190
191return false;
192}
193
194bool getSMBMemoryDeviceMemoryType(returnType *value)
195{
196static int idx = -1;
197intmap;
198
199idx++;
200if (idx < MAX_RAM_SLOTS)
201{
202map = Platform->DMI.DIMM[idx];
203if (Platform->RAM.DIMM[map].InUse && Platform->RAM.DIMM[map].Type != 0)
204{
205DBG("RAM Detected Type = %d\n", Platform->RAM.DIMM[map].Type);
206value->byte = Platform->RAM.DIMM[map].Type;
207return true;
208}
209}
210
211return false;
212//value->byte = SMB_MEM_TYPE_DDR2;
213//return true;
214}
215
216bool getSMBMemoryDeviceMemorySpeed(returnType *value)
217{
218static int idx = -1;
219intmap;
220
221idx++;
222if (idx < MAX_RAM_SLOTS)
223{
224map = Platform->DMI.DIMM[idx];
225if (Platform->RAM.DIMM[map].InUse && Platform->RAM.DIMM[map].Frequency != 0)
226{
227DBG("RAM Detected Freq = %d Mhz\n", Platform->RAM.DIMM[map].Frequency);
228value->dword = Platform->RAM.DIMM[map].Frequency;
229return true;
230}
231}
232
233return false;
234//value->dword = 800;
235//return true;
236}
237
238bool getSMBMemoryDeviceManufacturer(returnType *value)
239{
240static int idx = -1;
241intmap;
242
243idx++;
244if (idx < MAX_RAM_SLOTS)
245{
246map = Platform->DMI.DIMM[idx];
247if (Platform->RAM.DIMM[map].InUse && strlen(Platform->RAM.DIMM[map].Vendor) > 0)
248{
249DBG("RAM Detected Vendor[%d]='%s'\n", idx, Platform->RAM.DIMM[map].Vendor);
250value->string = Platform->RAM.DIMM[map].Vendor;
251return true;
252}
253}
254
255if (!bootInfo->memDetect)
256return false;
257value->string = NOT_AVAILABLE;
258return true;
259}
260
261bool getSMBMemoryDeviceSerialNumber(returnType *value)
262{
263static int idx = -1;
264intmap;
265
266idx++;
267
268 DBG("getSMBMemoryDeviceSerialNumber index: %d, MAX_RAM_SLOTS: %d\n",idx,MAX_RAM_SLOTS);
269
270if (idx < MAX_RAM_SLOTS)
271{
272map = Platform->DMI.DIMM[idx];
273if (Platform->RAM.DIMM[map].InUse && strlen(Platform->RAM.DIMM[map].SerialNo) > 0)
274{
275DBG("map=%d, RAM Detected SerialNo[%d]='%s'\n", map, idx, Platform->RAM.DIMM[map].SerialNo);
276value->string = Platform->RAM.DIMM[map].SerialNo;
277return true;
278}
279}
280
281if (!bootInfo->memDetect)
282return false;
283value->string = NOT_AVAILABLE;
284return true;
285}
286
287bool getSMBMemoryDevicePartNumber(returnType *value)
288{
289static int idx = -1;
290intmap;
291
292idx++;
293if (idx < MAX_RAM_SLOTS)
294{
295map = Platform->DMI.DIMM[idx];
296if (Platform->RAM.DIMM[map].InUse && strlen(Platform->RAM.DIMM[map].PartNo) > 0)
297{
298DBG("map=%d, RAM Detected PartNo[%d]='%s'\n", map, idx, Platform->RAM.DIMM[map].PartNo);
299value->string = Platform->RAM.DIMM[map].PartNo;
300return true;
301}
302}
303
304if (!bootInfo->memDetect)
305return false;
306value->string = NOT_AVAILABLE;
307return true;
308}
309
310
311// getting smbios addr with fast compare ops, late checksum testing ...
312#define COMPARE_DWORD(a,b) ( *((uint32_t *) a) == *((uint32_t *) b) )
313static const char * const SMTAG = "_SM_";
314static const char* const DMITAG = "_DMI_";
315
316SMBEntryPoint *getAddressOfSmbiosTable(void)
317{
318SMBEntryPoint*smbios;
319/*
320 * The logic is to start at 0xf0000 and end at 0xfffff iterating 16 bytes at a time looking
321 * for the SMBIOS entry-point structure anchor (literal ASCII "_SM_").
322 */
323smbios = (SMBEntryPoint*)SMBIOS_RANGE_START;
324while (smbios <= (SMBEntryPoint *)SMBIOS_RANGE_END) {
325if (COMPARE_DWORD(smbios->anchor, SMTAG) &&
326COMPARE_DWORD(smbios->dmi.anchor, DMITAG) &&
327smbios->dmi.anchor[4] == DMITAG[4] &&
328checksum8(smbios, sizeof(SMBEntryPoint)) == 0)
329 {
330return smbios;
331 }
332smbios = (SMBEntryPoint*)(((char*)smbios) + 16);
333}
334printf("ERROR: Unable to find SMBIOS!\n");
335pause();
336return NULL;
337}
338
339void getSmbiosMacModel(void)
340{
341#define MAX_MODEL_LEN32
342
343//Slice - I want to use MacModel for ACPITables so I need char* representation
344const char*value = getStringForKey("SMproductname", &bootInfo->smbiosConfig);
345int i, n=0, first=0, rev1=0, rev2=0;
346for (i=0; i<8; i++)
347{
348char c = value[i];
349if (isalpha(c))
350{
351MacModel[i]=c;
352n++;
353} else
354if ((c) >= '0' && (c) <= '9')
355{
356if (first)
357{
358rev1 = rev1 * 10 + (int)(c) & 0xf;
359} else
360rev2 = rev2 * 10 + (int)(c) & 0xf;
361} else
362first = 1;
363//printf("char=%c first=%d rev1=%d rev2=%d\n", c, first, rev1, rev2);
364}
365for (i=n; i<8; i++) {
366MacModel[i] = 0x20;
367}
368ModelRev = (rev2 << 16) + rev1;
369//ModelLength = (len + 1) * 2;
370//printf("Model=%s %08x\n", MacModel, ModelRev);
371//getc();
372
373}
374
375//static struct SMBEntryPoint *orig = NULL; // cached
376//static struct SMBEntryPoint *patched = NULL; // cached
377void getSmbiosProductName()
378{
379//struct SMBEntryPoint*smbios;
380SMBSystemInformation*p;
381char*tempString;
382inttmpLen;
383
384//smbios = getSmbios(SMBIOS_ORIGINAL);
385//if (smbios==NULL) return 0;
386
387p = (SMBSystemInformation*) FindFirstDmiTableOfType(1, 0x19); // Type 1: (3.3.2) System Information
388if (p==NULL) return; // NULL;
389
390tempString = (char*)smbiosStringAtIndex((SMBStructHeader*)p, p->productName, &tmpLen);
391tempString[tmpLen] = 0;
392
393gSMBIOSBoardModel = malloc(tmpLen + 1);
394if(gSMBIOSBoardModel)
395{
396strncpy(gSMBIOSBoardModel, tempString, tmpLen);
397Node* node = DT__FindNode("/", false);
398DT__AddProperty(node, "orig-model", tmpLen, gSMBIOSBoardModel);
399}
400verbose("Actual model name is '%s'\n", tempString);
401}
402
403const char * smbiosStringAtIndex(SMBStructHeader* smHeader, int index, int* length )
404{
405 const char * last = 0;
406 const char * next = (const char *) smHeader + smHeader->length;
407
408 if ( length ) *length = 0;
409 while ( index-- )
410 {
411 last = 0;
412const char * cp = 0;
413for ( cp = next; *cp || cp[1]; cp++ )
414 {
415 if ( *cp == '\0' )
416 {
417 last = next;
418 next = cp + 1;
419 break;
420 }
421 }
422 if ( last == 0 ) break;
423 }
424
425 if ( last )
426 {
427 while (*last == ' ') last++;
428 if (length)
429 {
430 UInt8 len;
431 for ( len = next - last - 1; len && last[len - 1] == ' '; len-- )
432 ;
433 *length = len; // number of chars not counting the terminating NULL
434 }
435 }
436
437 return last ? last : "";
438}
439
440//Slice
441//#define MEGA 1000000LL - now in mem.h
442void scan_cpu_DMI(void) //PlatformInfo_t *p)
443{
444// int i=0;
445int maxClock = 0;
446 SMBStructHeader * dmihdr = NULL;
447 SMBProcessorInformation* cpuInfo; // Type 4
448
449for (dmihdr = FindFirstDmiTableOfType(4, 30); dmihdr; dmihdr = FindNextDmiTableOfType(4, 30))
450{
451cpuInfo = (SMBProcessorInformation*)dmihdr;
452if (cpuInfo->processorType != 3) { // CPU
453continue;
454}
455//TODO validate
456#if 1 //NOTYET
457msglog("Platform CPU Info:\n FSB=%d\n MaxSpeed=%d\n CurrentSpeed=%d\n", Platform->CPU.FSBFrequency/MEGA, Platform->CPU.TSCFrequency/MEGA, Platform->CPU.CPUFrequency/MEGA);
458
459if ((cpuInfo->externalClock) && (cpuInfo->externalClock < 400)) { //<400MHz
460Platform->CPU.FSBFrequency = (cpuInfo->externalClock) * MEGA;
461}
462maxClock = cpuInfo->maximumClock;
463if (cpuInfo->maximumClock < cpuInfo->currentClock) {
464maxClock = cpuInfo->currentClock;
465}
466if ((maxClock) && (maxClock < 10000)) { //<10GHz
467Platform->CPU.TSCFrequency = maxClock * MEGA;
468}
469if ((cpuInfo->currentClock) && (cpuInfo->currentClock < 10000)) { //<10GHz
470Platform->CPU.CPUFrequency = cpuInfo->currentClock * MEGA;
471}
472#endif
473msglog("DMI CPU Info:\n FSB=%d\n MaxSpeed=%d\n CurrentSpeed=%d\n", cpuInfo->externalClock, cpuInfo->maximumClock, cpuInfo->currentClock);
474msglog("DMI CPU Info 2:\n Family=%x\n Socket=%x\n Cores=%d Enabled=%d Threads=%d\n", cpuInfo->processorFamily, cpuInfo->processorUpgrade, cpuInfo->coreCount, cpuInfo->coreEnabled, cpuInfo->Threads);
475#if 1 //NOTYET
476if ((cpuInfo->coreCount) && (cpuInfo->coreCount<Platform->CPU.NoCores)) {
477if (cpuInfo->coreEnabled < cpuInfo->coreCount) {
478cpuInfo->coreCount = cpuInfo->coreEnabled;
479}
480Platform->CPU.NoCores = cpuInfo->coreCount;
481}
482if ((cpuInfo->Threads) && (cpuInfo->Threads<Platform->CPU.NoThreads)) {
483Platform->CPU.NoThreads = cpuInfo->Threads;
484}
485#endif
486
487return;
488}
489
490return;
491}
492//Slice - check other DMI info
493bool scanDMI(void)
494{
495SMBStructHeader * dmihdr = NULL;
496 SMBSystemEnclosure* encInfo; // Type 3
497
498for (dmihdr = FindFirstDmiTableOfType(3, 13); dmihdr; dmihdr = FindNextDmiTableOfType(3, 13))
499{
500encInfo = (SMBSystemEnclosure*)dmihdr;
501msglog("DMI Chassis Info:\n Type=%x\n Boot-up State=%x\n Power Supply=%x Thermal State=%x\n", encInfo->type, encInfo->bootupState, encInfo->powerSupplyState, encInfo->thermalState);
502switch (encInfo->type) {
503case 1:
504case 2:
505return FALSE;
506case 3:
507case 4:
508case 6:
509case 7:
510Platform->CPU.Mobile = FALSE;
511break;
512case 8:
513case 9:
514case 0x0A:
515case 0x0B:
516case 0x0E:
517Platform->CPU.Mobile = TRUE;
518break;
519
520default:
521break;
522}
523return TRUE;
524}
525return FALSE;
526}
527

Archive Download this file

Revision: 1171