Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/smbios_patcher.c

1/*
2 * Copyright 2008 mackerintel
3 */
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.h"
8#include "acpi.h"
9#include "efi_tables.h"
10#include "fake_efi.h"
11#include "platform.h"
12#include "smbios_patcher.h"
13
14#ifndef DEBUG_SMBIOS
15#define DEBUG_SMBIOS 0
16#endif
17
18#if DEBUG_SMBIOS
19#define DBG(x...)printf(x)
20#else
21#define DBG(x...)
22#endif
23
24typedef struct {
25 const char* key;
26 const char* value;
27} SMStrEntryPair;
28
29// defaults for a MacBook
30static const SMStrEntryPair const sm_macbook_defaults[]={
31{"SMbiosvendor","Apple Inc."},
32{"SMbiosversion","MB41.88Z.0073.B00.0809221748"},
33{"SMbiosdate","04/01/2008"},
34{"SMmanufacter","Apple Inc."},
35{"SMproductname","MacBook4,1"},
36{"SMsystemversion","1.0"},
37{"SMserial","SOMESRLNMBR"},
38{"SMfamily","MacBook"},
39{"SMboardmanufacter","Apple Inc."},
40{"SMboardproduct","Mac-F42D89C8"},
41{ "",""}
42};
43
44// defaults for a MacBook Pro
45static const SMStrEntryPair const sm_macbookpro_defaults[]={
46{"SMbiosvendor","Apple Inc."},
47{"SMbiosversion","MBP41.88Z.0073.B00.0809221748"},
48{"SMbiosdate","04/01/2008"},
49{"SMmanufacter","Apple Inc."},
50{"SMproductname","MacBookPro4,1"},
51{"SMsystemversion","1.0"},
52{"SMserial","SOMESRLNMBR"},
53{"SMfamily","MacBookPro"},
54{"SMboardmanufacter","Apple Inc."},
55{"SMboardproduct","Mac-F42D89C8"},
56{ "",""}
57};
58
59// defaults for a Mac mini
60static const SMStrEntryPair const sm_macmini_defaults[]={
61{"SMbiosvendor","Apple Inc."},
62{"SMbiosversion","MM21.88Z.009A.B00.0706281359"},
63{"SMbiosdate","04/01/2008"},
64{"SMmanufacter","Apple Inc."},
65{"SMproductname","Macmini2,1"},
66{"SMsystemversion","1.0"},
67{"SMserial","SOMESRLNMBR"},
68{"SMfamily","Napa Mac"},
69{"SMboardmanufacter","Apple Inc."},
70{"SMboardproduct","Mac-F4208EAA"},
71{ "",""}
72};
73
74// defaults for an iMac
75static const SMStrEntryPair const sm_imac_defaults[]={
76{"SMbiosvendor","Apple Inc."},
77{"SMbiosversion","IM81.88Z.00C1.B00.0802091538"},
78{"SMbiosdate","04/01/2008"},
79{"SMmanufacter","Apple Inc."},
80{"SMproductname","iMac8,1"},
81{"SMsystemversion","1.0"},
82{"SMserial","SOMESRLNMBR"},
83{"SMfamily","Mac"},
84{"SMboardmanufacter","Apple Inc."},
85{"SMboardproduct","Mac-F227BEC8"},
86{ "",""}
87};
88
89// defaults for a Mac Pro
90static const SMStrEntryPair const sm_macpro_defaults[]={
91{"SMbiosvendor","Apple Computer, Inc."},
92{"SMbiosversion","MP31.88Z.006C.B05.0802291410"},
93{"SMbiosdate","04/01/2008"},
94{"SMmanufacter","Apple Computer, Inc."},
95{"SMproductname","MacPro3,1"},
96{"SMsystemversion","1.0"},
97{"SMserial","SOMESRLNMBR"},
98{"SMfamily","MacPro"},
99{"SMboardmanufacter","Apple Computer, Inc."},
100{"SMboardproduct","Mac-F4208DC8"},
101{ "",""}
102};
103
104static const char* sm_get_defstr(const char * key, int table_num)
105{
106inti;
107const SMStrEntryPair*sm_defaults;
108
109if (platformCPUFeature(CPU_FEATURE_MOBILE)) {
110if (Platform.CPU.NoCores > 1) {
111sm_defaults=sm_macbookpro_defaults;
112} else {
113sm_defaults=sm_macbook_defaults;
114}
115} else {
116switch (Platform.CPU.NoCores) {
117case 1: sm_defaults=sm_macmini_defaults; break;
118case 2: sm_defaults=sm_imac_defaults; break;
119default: sm_defaults=sm_macpro_defaults; break;
120}
121}
122
123for (i=0; sm_defaults[i].key[0]; i++) {
124if (!strcmp (sm_defaults[i].key, key)) {
125return sm_defaults[i].value;
126}
127}
128
129// Shouldn't happen
130printf ("Error: no default for '%s' known\n", key);
131sleep (2);
132return "";
133}
134
135static int sm_get_fsb(const char *name, int table_num)
136{
137return Platform.CPU.FSBFrequency/1000000;
138}
139
140static int sm_get_cpu (const char *name, int table_num)
141{
142return Platform.CPU.CPUFrequency/1000000;
143}
144
145static int sm_get_cputype (const char *name, int table_num)
146{
147if (Platform.CPU.NoCores == 1) {
148return 0x0101; // <01 01> Intel Core Solo?
149} else if (Platform.CPU.NoCores == 2) {
150return 0x0301; // <01 03> Intel Core 2 Duo
151} else if (Platform.CPU.NoCores >= 4) {
152return 0x0501; // <01 05> Quad-Core Intel Xeon
153} else {
154return 0x0301; // Default to Core 2 Duo
155}
156}
157
158static int sm_get_memtype (const char *name, int table_num)
159{
160intmap;
161
162if (table_num < MAX_RAM_SLOTS) {
163map = Platform.DMI.DIMM[table_num];
164if (Platform.RAM.DIMM[map].InUse && Platform.RAM.DIMM[map].Type != 0) {
165 DBG("RAM Detected Type = %d\n", Platform.RAM.DIMM[map].Type);
166 return Platform.RAM.DIMM[map].Type;
167}
168}
169return SMB_MEM_TYPE_DDR2;
170}
171
172static int sm_get_memspeed (const char *name, int table_num)
173{
174intmap;
175
176if (table_num < MAX_RAM_SLOTS) {
177map = Platform.DMI.DIMM[table_num];
178if (Platform.RAM.DIMM[map].InUse && Platform.RAM.DIMM[map].Frequency != 0) {
179 DBG("RAM Detected Freq = %d Mhz\n", Platform.RAM.DIMM[map].Frequency);
180 return Platform.RAM.DIMM[map].Frequency;
181}
182}
183
184return 800;
185}
186
187static const char *sm_get_memvendor (const char *name, int table_num)
188{
189intmap;
190
191if (table_num < MAX_RAM_SLOTS) {
192map = Platform.DMI.DIMM[table_num];
193if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].Vendor) > 0) {
194DBG("RAM Detected Vendor[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].Vendor);
195return Platform.RAM.DIMM[map].Vendor;
196}
197}
198return "N/A";
199}
200
201static const char *sm_get_memserial (const char *name, int table_num)
202{
203intmap;
204
205if (table_num < MAX_RAM_SLOTS) {
206map = Platform.DMI.DIMM[table_num];
207if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].SerialNo) > 0) {
208 DBG("name = %s, map=%d, RAM Detected SerialNo[%d]='%s'\n", name ? name : "",
209 map, table_num, Platform.RAM.DIMM[map].SerialNo);
210 return Platform.RAM.DIMM[map].SerialNo;
211}
212}
213return "N/A";
214}
215
216static const char *sm_get_mempartno (const char *name, int table_num)
217{
218intmap;
219
220if (table_num < MAX_RAM_SLOTS) {
221map = Platform.DMI.DIMM[table_num];
222if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].PartNo) > 0) {
223DBG("Ram Detected PartNo[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].PartNo);
224return Platform.RAM.DIMM[map].PartNo;
225}
226}
227return "N/A";
228}
229
230static int sm_one (int tablen)
231{
232return 1;
233}
234
235struct smbios_property smbios_properties[]=
236{
237{.name="SMbiosvendor",.table_type= 0,.value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
238{.name="SMbiosversion",.table_type= 0,.value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
239{.name="SMbiosdate",.table_type= 0,.value_type=SMSTRING,.offset=0x08,.auto_str=sm_get_defstr},
240{.name="SMmanufacter",.table_type= 1,.value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
241{.name="SMproductname",.table_type= 1,.value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
242{.name="SMsystemversion",.table_type= 1,.value_type=SMSTRING,.offset=0x06,.auto_str=sm_get_defstr},
243{.name="SMserial",.table_type= 1,.value_type=SMSTRING,.offset=0x07,.auto_str=sm_get_defstr},
244{.name="SMUUID",.table_type= 1, .value_type=SMOWORD,.offset=0x08,.auto_oword=0},
245{.name="SMfamily",.table_type= 1,.value_type=SMSTRING,.offset=0x1a,.auto_str=sm_get_defstr},
246{.name="SMboardmanufacter",.table_type= 2, .value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
247{.name="SMboardproduct",.table_type= 2, .value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
248{.name="SMexternalclock",.table_type= 4,.value_type=SMWORD,.offset=0x12,.auto_int=sm_get_fsb},
249{.name="SMmaximalclock",.table_type= 4,.value_type=SMWORD,.offset=0x14,.auto_int=sm_get_cpu},
250{.name="SMmemdevloc",.table_type=17,.value_type=SMSTRING,.offset=0x10,.auto_str=0},
251{.name="SMmembankloc",.table_type=17,.value_type=SMSTRING,.offset=0x11,.auto_str=0},
252{.name="SMmemtype",.table_type=17,.value_type=SMBYTE,.offset=0x12,.auto_int=sm_get_memtype},
253{.name="SMmemspeed",.table_type=17,.value_type=SMWORD,.offset=0x15,.auto_int=sm_get_memspeed},
254{.name="SMmemmanufacter",.table_type=17,.value_type=SMSTRING,.offset=0x17,.auto_str=sm_get_memvendor},
255{.name="SMmemserial",.table_type=17,.value_type=SMSTRING,.offset=0x18,.auto_str=sm_get_memserial},
256{.name="SMmempart",.table_type=17,.value_type=SMSTRING,.offset=0x1A,.auto_str=sm_get_mempartno},
257{.name="SMcputype",.table_type=131,.value_type=SMWORD,.offset=0x04,.auto_int=sm_get_cputype},
258{.name="SMbusspeed",.table_type=132,.value_type=SMWORD,.offset=0x04,.auto_str=0}
259};
260
261struct smbios_table_description smbios_table_descriptions[]=
262{
263{.type=0,.len=0x18,.numfunc=sm_one},
264{.type=1,.len=0x1b,.numfunc=sm_one},
265{.type=2,.len=0x0f,.numfunc=sm_one},
266{.type=4,.len=0x2a,.numfunc=sm_one},
267{.type=17,.len=0x1c,.numfunc=0},
268{.type=131,.len=0x06,.numfunc=sm_one},
269{.type=132,.len=0x06,.numfunc=sm_one}
270};
271
272// getting smbios addr with fast compare ops, late checksum testing ...
273#define COMPARE_DWORD(a,b) ( *((u_int32_t *) a) == *((u_int32_t *) b) )
274static const char * const SMTAG = "_SM_";
275static const char* const DMITAG= "_DMI_";
276
277static struct SMBEntryPoint *getAddressOfSmbiosTable(void)
278{
279struct SMBEntryPoint*smbios;
280
281/*
282 * The logic is to start at 0xf0000 and end at 0xfffff iterating 16 bytes at a time looking
283 * for the SMBIOS entry-point structure anchor (literal ASCII "_SM_").
284 */
285smbios = (struct SMBEntryPoint*) SMBIOS_RANGE_START;
286while (smbios <= (struct SMBEntryPoint *)SMBIOS_RANGE_END) {
287 if (COMPARE_DWORD(smbios->anchor, SMTAG) && COMPARE_DWORD(smbios->dmi.anchor, DMITAG) &&
288 checksum8(smbios, sizeof(struct SMBEntryPoint)) == 0)
289 {
290 return smbios;
291 }
292 smbios = (((void*) smbios) + 16);
293}
294printf("ERROR: Unable to find SMBIOS!\n");
295sleep(5);
296return NULL;
297}
298
299/** Compute necessary space requirements for new smbios */
300static struct SMBEntryPoint *smbios_dry_run(struct SMBEntryPoint *origsmbios)
301{
302struct SMBEntryPoint*ret;
303char*smbiostables;
304char*tablesptr;
305intorigsmbiosnum;
306inti, j;
307inttablespresent[256];
308booldo_auto=true;
309
310bzero(tablespresent, sizeof(tablespresent));
311
312getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);
313
314ret = (struct SMBEntryPoint *)AllocateKernelMemory(sizeof(struct SMBEntryPoint));
315if (origsmbios) {
316smbiostables = (char *)origsmbios->dmi.tableAddress;
317origsmbiosnum = origsmbios->dmi.structureCount;
318} else {
319smbiostables = NULL;
320origsmbiosnum = 0;
321}
322
323// _SM_
324ret->anchor[0] = 0x5f;
325ret->anchor[1] = 0x53;
326ret->anchor[2] = 0x4d;
327ret->anchor[3] = 0x5f;
328ret->entryPointLength = sizeof(*ret);
329ret->majorVersion = 2;
330ret->minorVersion = 1;
331ret->maxStructureSize = 0; // will be calculated later in this function
332ret->entryPointRevision = 0;
333for (i=0;i<5;i++) {
334ret->formattedArea[i] = 0;
335}
336//_DMI_
337ret->dmi.anchor[0] = 0x5f;
338ret->dmi.anchor[1] = 0x44;
339ret->dmi.anchor[2] = 0x4d;
340ret->dmi.anchor[3] = 0x49;
341ret->dmi.anchor[4] = 0x5f;
342ret->dmi.tableLength = 0; // will be calculated later in this function
343ret->dmi.tableAddress = 0; // will be initialized in smbios_real_run()
344ret->dmi.structureCount = 0; // will be calculated later in this function
345ret->dmi.bcdRevision = 0x21;
346tablesptr = smbiostables;
347
348 // add stringlen of overrides to original stringlen, update maxStructure size adequately,
349 // update structure count and tablepresent[type] with count of type.
350if (smbiostables) {
351for (i=0; i<origsmbiosnum; i++) {
352struct smbios_table_header*cur = (struct smbios_table_header *)tablesptr;
353char*stringsptr;
354intstringlen;
355
356tablesptr += cur->length;
357stringsptr = tablesptr;
358for (; tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++);
359tablesptr += 2;
360stringlen = tablesptr - stringsptr - 1;
361if (stringlen == 1) {
362stringlen = 0;
363}
364for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
365const char*str;
366intsize;
367charaltname[40];
368
369sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[cur->type] + 1);
370if (smbios_properties[j].table_type == cur->type &&
371 smbios_properties[j].value_type == SMSTRING &&
372 (getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig) ||
373 getValueForKey(altname,&str, &size, &bootInfo->smbiosConfig)))
374{
375stringlen += size + 1;
376} else if (smbios_properties[j].table_type == cur->type &&
377 smbios_properties[j].value_type == SMSTRING &&
378 do_auto && smbios_properties[j].auto_str)
379{
380stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[cur->type])) + 1;
381}
382}
383if (stringlen == 0) {
384stringlen = 1;
385}
386stringlen++;
387if (ret->maxStructureSize < cur->length+stringlen) {
388ret->maxStructureSize=cur->length+stringlen;
389}
390ret->dmi.tableLength += cur->length+stringlen;
391ret->dmi.structureCount++;
392tablespresent[cur->type]++;
393}
394}
395 // Add eventually table types whose detected count would be < required count, and update ret header with:
396 // new stringlen addons, structure count, and tablepresent[type] count adequately
397for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {
398intnumnec=-1;
399charbuffer[40];
400
401sprintf(buffer, "SMtable%d", i);
402if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {
403numnec = -1;
404}
405if (numnec==-1 && do_auto && smbios_table_descriptions[i].numfunc) {
406numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);
407}
408while (tablespresent[smbios_table_descriptions[i].type] < numnec) {
409intstringlen = 0;
410for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
411const char*str;
412intsize;
413charaltname[40];
414
415sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type] + 1);
416if (smbios_properties[j].table_type == smbios_table_descriptions[i].type &&
417 smbios_properties[j].value_type == SMSTRING &&
418 (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
419 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig)))
420{
421stringlen += size + 1;
422} else if (smbios_properties[j].table_type == smbios_table_descriptions[i].type &&
423 smbios_properties[j].value_type==SMSTRING &&
424 do_auto && smbios_properties[j].auto_str)
425{
426stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type])) + 1;
427}
428}
429if (stringlen == 0) {
430stringlen = 1;
431}
432stringlen++;
433if (ret->maxStructureSize < smbios_table_descriptions[i].len+stringlen) {
434ret->maxStructureSize = smbios_table_descriptions[i].len + stringlen;
435}
436ret->dmi.tableLength += smbios_table_descriptions[i].len + stringlen;
437ret->dmi.structureCount++;
438tablespresent[smbios_table_descriptions[i].type]++;
439}
440}
441return ret;
442}
443
444/** From the origsmbios detected by getAddressOfSmbiosTable() to newsmbios whose entrypoint
445 * struct has been created by smbios_dry_run, update each table struct content of new smbios
446 * int the new allocated table address of size newsmbios->tablelength.
447 */
448static void smbios_real_run(struct SMBEntryPoint * origsmbios, struct SMBEntryPoint * newsmbios)
449{
450char *smbiostables;
451char *tablesptr, *newtablesptr;
452int origsmbiosnum;
453// bitmask of used handles
454uint8_t handles[8192];
455uint16_t nexthandle=0;
456int i, j;
457int tablespresent[256];
458bool do_auto=true;
459
460 extern void dumpPhysAddr(const char * title, void * a, int len);
461
462bzero(tablespresent, sizeof(tablespresent));
463bzero(handles, sizeof(handles));
464
465getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);
466
467newsmbios->dmi.tableAddress = (uint32_t)AllocateKernelMemory(newsmbios->dmi.tableLength);
468if (origsmbios) {
469smbiostables = (char *)origsmbios->dmi.tableAddress;
470origsmbiosnum = origsmbios->dmi.structureCount;
471} else {
472smbiostables = NULL;
473origsmbiosnum = 0;
474}
475tablesptr = smbiostables;
476newtablesptr = (char *)newsmbios->dmi.tableAddress;
477
478 // if old smbios exists then update new smbios with old smbios original content first
479if (smbiostables) {
480for (i=0; i<origsmbiosnum; i++) {
481struct smbios_table_header*oldcur = (struct smbios_table_header *) tablesptr;
482struct smbios_table_header*newcur = (struct smbios_table_header *) newtablesptr;
483char*stringsptr;
484intnstrings = 0;
485
486handles[(oldcur->handle) / 8] |= 1 << ((oldcur->handle) % 8);
487
488 // copy table length from old table to new table but not the old strings
489memcpy(newcur,oldcur, oldcur->length);
490
491tablesptr += oldcur->length;
492stringsptr = tablesptr;
493newtablesptr += oldcur->length;
494
495 // calculate the number of strings in the old content
496for (;tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++) {
497if (tablesptr[0] == 0) {
498nstrings++;
499}
500}
501if (tablesptr != stringsptr) {
502nstrings++;
503}
504tablesptr += 2;
505
506 // copy the old strings to new table
507memcpy(newtablesptr, stringsptr, tablesptr-stringsptr);
508
509 // point to next possible space for a string (deducting the second 0 char at the end)
510newtablesptr += tablesptr - stringsptr - 1;
511 if (nstrings == 0) { // if no string was found rewind to the first 0 char of the 0,0 terminator
512newtablesptr--;
513}
514
515 // now for each property in the table update the overrides if any (auto or user)
516for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
517const char*str;
518intsize;
519intnum;
520charaltname[40];
521
522sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
523if (smbios_properties[j].table_type == newcur->type) {
524switch (smbios_properties[j].value_type) {
525case SMSTRING:
526if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
527 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
528{
529memcpy(newtablesptr, str, size);
530newtablesptr[size] = 0;
531newtablesptr += size + 1;
532*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
533} else if (do_auto && smbios_properties[j].auto_str) {
534str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
535size = strlen(str);
536memcpy(newtablesptr, str, size);
537newtablesptr[size] = 0;
538newtablesptr += size + 1;
539*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
540}
541break;
542
543case SMOWORD:
544if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
545 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
546{
547intk=0, t=0, kk=0;
548const char*ptr = str;
549memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
550while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
551ptr++;
552}
553if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
554ptr += 2;
555}
556for (;ptr-str<size && *ptr && k<16;ptr++) {
557if (*ptr>='0' && *ptr<='9') {
558(t=(t<<4)|(*ptr-'0')),kk++;
559}
560if (*ptr>='a' && *ptr<='f') {
561(t=(t<<4)|(*ptr-'a'+10)),kk++;
562}
563if (*ptr>='A' && *ptr<='F') {
564(t=(t<<4)|(*ptr-'A'+10)),kk++;
565}
566if (kk == 2) {
567*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
568k++;
569kk = 0;
570t = 0;
571}
572}
573}
574break;
575
576case SMBYTE:
577if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
578 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
579{
580*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
581} else if (do_auto && smbios_properties[j].auto_int) {
582*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
583}
584break;
585
586case SMWORD:
587if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
588 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
589{
590*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
591} else if (do_auto && smbios_properties[j].auto_int) {
592*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
593}
594break;
595}
596}
597}
598if (nstrings == 0) {
599newtablesptr[0] = 0;
600newtablesptr++;
601}
602newtablesptr[0] = 0;
603newtablesptr++;
604tablespresent[newcur->type]++;
605}
606}
607
608 // for each eventual complementary table not present in the original smbios, do the overrides
609for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {
610intnumnec = -1;
611charbuffer[40];
612
613sprintf(buffer, "SMtable%d", i);
614if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {
615numnec = -1;
616}
617if (numnec == -1 && do_auto && smbios_table_descriptions[i].numfunc) {
618numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);
619}
620while (tablespresent[smbios_table_descriptions[i].type] < numnec) {
621struct smbios_table_header*newcur = (struct smbios_table_header *) newtablesptr;
622intnstrings = 0;
623
624memset(newcur,0, smbios_table_descriptions[i].len);
625while (handles[(nexthandle)/8] & (1 << ((nexthandle) % 8))) {
626nexthandle++;
627}
628newcur->handle = nexthandle;
629handles[nexthandle / 8] |= 1 << (nexthandle % 8);
630newcur->type = smbios_table_descriptions[i].type;
631newcur->length = smbios_table_descriptions[i].len;
632newtablesptr += smbios_table_descriptions[i].len;
633for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
634const char*str;
635intsize;
636intnum;
637charaltname[40];
638
639sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
640if (smbios_properties[j].table_type == newcur->type) {
641switch (smbios_properties[j].value_type) {
642case SMSTRING:
643if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
644 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
645{
646memcpy(newtablesptr, str, size);
647newtablesptr[size] = 0;
648newtablesptr += size + 1;
649*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
650} else if (do_auto && smbios_properties[j].auto_str) {
651str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
652size = strlen(str);
653memcpy(newtablesptr, str, size);
654newtablesptr[size] = 0;
655newtablesptr += size + 1;
656*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
657}
658break;
659
660case SMOWORD:
661if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
662 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
663{
664intk=0, t=0, kk=0;
665const char*ptr = str;
666
667memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
668while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
669ptr++;
670}
671if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
672ptr += 2;
673}
674for (;ptr-str<size && *ptr && k<16;ptr++) {
675if (*ptr>='0' && *ptr<='9') {
676(t=(t<<4)|(*ptr-'0')),kk++;
677}
678if (*ptr>='a' && *ptr<='f') {
679(t=(t<<4)|(*ptr-'a'+10)),kk++;
680}
681if (*ptr>='A' && *ptr<='F') {
682(t=(t<<4)|(*ptr-'A'+10)),kk++;
683}
684if (kk == 2) {
685*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
686k++;
687kk = 0;
688t = 0;
689}
690}
691}
692break;
693
694case SMBYTE:
695if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
696 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
697{
698*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
699} else if (do_auto && smbios_properties[j].auto_int) {
700*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
701}
702break;
703
704case SMWORD:
705if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
706 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
707{
708*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
709} else if (do_auto && smbios_properties[j].auto_int) {
710*((uint16_t*)(((char*)newcur)+smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
711}
712break;
713}
714}
715}
716if (nstrings == 0) {
717newtablesptr[0] = 0;
718newtablesptr++;
719}
720newtablesptr[0] = 0;
721newtablesptr++;
722tablespresent[smbios_table_descriptions[i].type]++;
723}
724}
725
726 // calculate new checksums
727newsmbios->dmi.checksum = 0;
728newsmbios->dmi.checksum = 256 - checksum8(&newsmbios->dmi, sizeof(newsmbios->dmi));
729newsmbios->checksum = 0;
730newsmbios->checksum = 256 - checksum8(newsmbios, sizeof(*newsmbios));
731verbose("Patched DMI Table\n");
732}
733
734#define MAX_DMI_TABLES 64
735typedef struct DmiNumAssocTag {
736 struct DMIHeader * dmi;
737 uint8_t type;
738} DmiNumAssoc;
739
740static DmiNumAssoc DmiTablePair[MAX_DMI_TABLES];
741static int DmiTablePairCount = 0;
742static int current_pos=0;
743static bool ftTablePairInit = true;
744
745/**
746 * Get a table structure entry from a type specification and a smbios address
747 * return NULL if table is not found
748 */
749static void getSmbiosTableStructure(struct SMBEntryPoint *smbios)
750{
751 struct DMIHeader * dmihdr=NULL;
752 SMBByte* p;
753 int i;
754
755 if (ftTablePairInit && smbios!=NULL) {
756 ftTablePairInit = false;
757#if DEBUG_SMBIOS
758 printf(">>> SMBIOSAddr=0x%08x\n", smbios);
759 printf(">>> DMI: addr=0x%08x, len=%d, count=%d\n", smbios->dmi.tableAddress,
760 smbios->dmi.tableLength, smbios->dmi.structureCount);
761#endif
762 p = (SMBByte *) smbios->dmi.tableAddress;
763 for (i=0;
764 i < smbios->dmi.structureCount &&
765 p + 4 <= (SMBByte *)smbios->dmi.tableAddress + smbios->dmi.tableLength;
766 i++) {
767 dmihdr = (struct DMIHeader *) p;
768
769#if DEBUG_SMBIOS
770 // verbose(">>>>>> DMI(%d): type=0x%02x, len=0x%d\n",i,dmihdr->type,dmihdr->length);
771#endif
772 if (dmihdr->length < 4 || dmihdr->type == 127 /* EOT */) break;
773 DmiTablePair[DmiTablePairCount].dmi = dmihdr;
774 DmiTablePair[DmiTablePairCount].type = dmihdr->type;
775 DmiTablePairCount++;
776#if DEBUG_SMBIOS
777 printf("DMI header found for table type %d, length = %d\n", dmihdr->type, dmihdr->length);
778#endif
779 p = p + dmihdr->length;
780 while ((p - (SMBByte *)smbios->dmi.tableAddress + 1 < smbios->dmi.tableLength) && (p[0] != 0x00 || p[1] != 0x00)) {
781 p++;
782 }
783 p += 2;
784}
785
786 }
787}
788/** Get soriginal or new smbios entry point, if sucessfull, the adresses are cached for next time */
789struct SMBEntryPoint *getSmbios(int which)
790{
791 static struct SMBEntryPoint *orig = NULL; // cached
792 static struct SMBEntryPoint *patched = NULL; // cached
793 // whatever we are called with orig or new flag, initialize asap both structures
794 if (orig == NULL) orig = getAddressOfSmbiosTable();
795 if (patched == NULL) {
796 if (orig==NULL) {
797 printf("Could not find original SMBIOS !!\n");
798 getc();
799 return NULL;
800 }
801 patched = smbios_dry_run(orig);
802 if(patched==NULL) {
803 printf("Could not create new SMBIOS !!\n");
804 getc();
805 }
806 else {
807 smbios_real_run(orig, patched);
808 getSmbiosTableStructure(patched); // generate tables entry list for fast table finding
809 }
810
811 }
812
813 switch (which) {
814 case SMBIOS_ORIGINAL:
815 return orig;
816 case SMBIOS_PATCHED:
817 return patched;
818 default:
819 printf("ERROR: invalid option for getSmbios() !!\n");
820 return NULL;
821 }
822}
823
824/** Find first new dmi Table with a particular type */
825struct DMIHeader* FindFirstDmiTableOfType(int type, int minlength)
826{
827 current_pos = 0;
828 return FindNextDmiTableOfType(type, minlength);
829};
830
831/** Find next new dmi Table with a particular type */
832struct DMIHeader* FindNextDmiTableOfType(int type, int minlength)
833{
834 int i;
835
836 for (i=current_pos; i < DmiTablePairCount; i++) {
837 if (type == DmiTablePair[i].type &&
838 DmiTablePair[i].dmi &&
839 DmiTablePair[i].dmi->length >= minlength ) {
840 current_pos = i+1;
841 return DmiTablePair[i].dmi;
842 }
843 }
844 return NULL; // not found
845};
846
847

Archive Download this file

Revision: 114