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].Type != 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 */
300struct 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 */
448void 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 #if 0
510 // DEBUG: display this original table 17
511
512 if (oldcur->type==6 || oldcur->type==17)
513 {
514 dumpPhysAddr("orig table:", oldcur, oldcur->length + ( tablesptr-stringsptr));
515 }
516#endif
517// point to next possible space for a string (deducting the second 0 char at the end)
518newtablesptr += tablesptr - stringsptr - 1;
519 if (nstrings == 0) { // if no string was found rewind to the first 0 char of the 0,0 terminator
520newtablesptr--;
521}
522
523 // now for each property in the table update the overrides if any (auto or user)
524for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
525const char*str;
526intsize;
527intnum;
528charaltname[40];
529
530sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
531if (smbios_properties[j].table_type == newcur->type) {
532switch (smbios_properties[j].value_type) {
533case SMSTRING:
534if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
535 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
536{
537memcpy(newtablesptr, str, size);
538newtablesptr[size] = 0;
539newtablesptr += size + 1;
540*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
541} else if (do_auto && smbios_properties[j].auto_str) {
542str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
543size = strlen(str);
544memcpy(newtablesptr, str, size);
545newtablesptr[size] = 0;
546newtablesptr += size + 1;
547*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
548}
549break;
550
551case SMOWORD:
552if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
553 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
554{
555intk=0, t=0, kk=0;
556const char*ptr = str;
557memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
558while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
559ptr++;
560}
561if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
562ptr += 2;
563}
564for (;ptr-str<size && *ptr && k<16;ptr++) {
565if (*ptr>='0' && *ptr<='9') {
566(t=(t<<4)|(*ptr-'0')),kk++;
567}
568if (*ptr>='a' && *ptr<='f') {
569(t=(t<<4)|(*ptr-'a'+10)),kk++;
570}
571if (*ptr>='A' && *ptr<='F') {
572(t=(t<<4)|(*ptr-'A'+10)),kk++;
573}
574if (kk == 2) {
575*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
576k++;
577kk = 0;
578t = 0;
579}
580}
581}
582break;
583
584case SMBYTE:
585if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
586 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
587{
588*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
589} else if (do_auto && smbios_properties[j].auto_int) {
590*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
591}
592break;
593
594case SMWORD:
595if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
596 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
597{
598*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
599} else if (do_auto && smbios_properties[j].auto_int) {
600*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
601}
602break;
603}
604}
605}
606if (nstrings == 0) {
607newtablesptr[0] = 0;
608newtablesptr++;
609}
610newtablesptr[0] = 0;
611newtablesptr++;
612tablespresent[newcur->type]++;
613}
614}
615
616 // for each eventual complementary table not present in the original smbios, do the overrides
617for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {
618intnumnec = -1;
619charbuffer[40];
620
621sprintf(buffer, "SMtable%d", i);
622if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {
623numnec = -1;
624}
625if (numnec == -1 && do_auto && smbios_table_descriptions[i].numfunc) {
626numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);
627}
628while (tablespresent[smbios_table_descriptions[i].type] < numnec) {
629struct smbios_table_header*newcur = (struct smbios_table_header *) newtablesptr;
630intnstrings = 0;
631
632memset(newcur,0, smbios_table_descriptions[i].len);
633while (handles[(nexthandle)/8] & (1 << ((nexthandle) % 8))) {
634nexthandle++;
635}
636newcur->handle = nexthandle;
637handles[nexthandle / 8] |= 1 << (nexthandle % 8);
638newcur->type = smbios_table_descriptions[i].type;
639newcur->length = smbios_table_descriptions[i].len;
640newtablesptr += smbios_table_descriptions[i].len;
641for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
642const char*str;
643intsize;
644intnum;
645charaltname[40];
646
647sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
648if (smbios_properties[j].table_type == newcur->type) {
649switch (smbios_properties[j].value_type) {
650case SMSTRING:
651if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
652 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
653{
654memcpy(newtablesptr, str, size);
655newtablesptr[size] = 0;
656newtablesptr += size + 1;
657*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
658} else if (do_auto && smbios_properties[j].auto_str) {
659str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
660size = strlen(str);
661memcpy(newtablesptr, str, size);
662newtablesptr[size] = 0;
663newtablesptr += size + 1;
664*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
665}
666break;
667
668case SMOWORD:
669if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
670 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
671{
672intk=0, t=0, kk=0;
673const char*ptr = str;
674
675memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
676while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
677ptr++;
678}
679if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
680ptr += 2;
681}
682for (;ptr-str<size && *ptr && k<16;ptr++) {
683if (*ptr>='0' && *ptr<='9') {
684(t=(t<<4)|(*ptr-'0')),kk++;
685}
686if (*ptr>='a' && *ptr<='f') {
687(t=(t<<4)|(*ptr-'a'+10)),kk++;
688}
689if (*ptr>='A' && *ptr<='F') {
690(t=(t<<4)|(*ptr-'A'+10)),kk++;
691}
692if (kk == 2) {
693*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
694k++;
695kk = 0;
696t = 0;
697}
698}
699}
700break;
701
702case SMBYTE:
703if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
704 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
705{
706*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
707} else if (do_auto && smbios_properties[j].auto_int) {
708*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
709}
710break;
711
712case SMWORD:
713if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
714 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
715{
716*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
717} else if (do_auto && smbios_properties[j].auto_int) {
718*((uint16_t*)(((char*)newcur)+smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
719}
720break;
721}
722}
723}
724if (nstrings == 0) {
725newtablesptr[0] = 0;
726newtablesptr++;
727}
728newtablesptr[0] = 0;
729newtablesptr++;
730tablespresent[smbios_table_descriptions[i].type]++;
731}
732}
733
734 // calculate new checksums
735newsmbios->dmi.checksum = 0;
736newsmbios->dmi.checksum = 256 - checksum8(&newsmbios->dmi, sizeof(newsmbios->dmi));
737newsmbios->checksum = 0;
738newsmbios->checksum = 256 - checksum8(newsmbios, sizeof(*newsmbios));
739verbose("Patched DMI Table\n");
740}
741
742struct SMBEntryPoint *getSmbios(int which)
743{
744 static struct SMBEntryPoint *orig = NULL; // cached
745 static struct SMBEntryPoint *patched = NULL; // cached
746
747if (orig == NULL) orig = getAddressOfSmbiosTable();
748
749switch (which) {
750case SMBIOS_ORIGINAL:
751 return orig;
752case SMBIOS_PATCHED:
753 if (patched == NULL) {
754 if (orig==NULL) orig = getAddressOfSmbiosTable();
755 patched = smbios_dry_run(orig);
756 smbios_real_run(orig, patched);
757 }
758 return patched;
759default:
760 printf("ERROR: invalid option for getSmbios() !!\n");
761 return NULL;
762}
763}
764
765#define MAX_DMI_TABLES 64
766typedef struct DmiNumAssocTag {
767 struct DMIHeader * dmi;
768 uint8_t type;
769} DmiNumAssoc;
770
771static DmiNumAssoc DmiTablePair[MAX_DMI_TABLES];
772static int DmiTablePairCount = 0;
773static int current_pos=0;
774static bool ftTablePairInit = true;
775
776/** Find first original dmi Table with a particular type */
777struct DMIHeader* FindFirstDmiTableOfType(int type, int minlength)
778{
779 if (ftTablePairInit)
780 return getSmbiosTableStructure(getSmbios(SMBIOS_ORIGINAL),
781 type, minlength);
782 current_pos = 0;
783 return FindNextDmiTableOfType(type, minlength);
784};
785
786/** Find next original dmi Table with a particular type */
787struct DMIHeader* FindNextDmiTableOfType(int type, int minlength)
788{
789 int i;
790
791 for (i=current_pos; i < DmiTablePairCount; i++) {
792 if (type == DmiTablePair[i].type &&
793 DmiTablePair[i].dmi &&
794 DmiTablePair[i].dmi->length >= minlength ) {
795 current_pos = i+1;
796 return DmiTablePair[i].dmi;
797 }
798 }
799 return NULL; // not found
800};
801
802/**
803 * Get a table structure entry from a type specification and a smbios address
804 * return NULL if table is not found
805 */
806struct DMIHeader *getSmbiosTableStructure(struct SMBEntryPoint*smbios, int type, int min_length)
807{
808 struct DMIHeader * dmihdr=NULL;
809 SMBByte* p;
810 int i;
811
812 if (!ftTablePairInit) {
813 return FindFirstDmiTableOfType(type, min_length);
814 } else {
815 ftTablePairInit = false;
816 bzero(DmiTablePair, sizeof(DmiTablePair));
817
818 if (smbios == NULL || type < 0 ) return NULL;
819#if DEBUG_SMBIOS
820 printf(">>> SMBIOSAddr=0x%08x\n", smbios);
821 printf(">>> DMI: addr=0x%08x, len=%d, count=%d\n", smbios->dmi.tableAddress,
822 smbios->dmi.tableLength, smbios->dmi.structureCount);
823#endif
824 p = (SMBByte *) smbios->dmi.tableAddress;
825 for (i=0;
826 i < smbios->dmi.structureCount &&
827 p + 4 <= (SMBByte *)smbios->dmi.tableAddress + smbios->dmi.tableLength;
828 i++) {
829 dmihdr = (struct DMIHeader *) p;
830
831#if DEBUG_SMBIOS
832 // verbose(">>>>>> DMI(%d): type=0x%02x, len=0x%d\n",i,dmihdr->type,dmihdr->length);
833#endif
834 if (dmihdr->length < 4 || dmihdr->type == 127 /* EOT */) break;
835 DmiTablePair[DmiTablePairCount].dmi = dmihdr;
836 DmiTablePair[DmiTablePairCount].type = dmihdr->type;
837 DmiTablePairCount++;
838#if DEBUG_SMBIOS
839 printf("DMI header found for table type %d, length = %d\n", dmihdr->type, dmihdr->length);
840#endif
841 p = p + dmihdr->length;
842 while ((p - (SMBByte *)smbios->dmi.tableAddress + 1 < smbios->dmi.tableLength) && (p[0] != 0x00 || p[1] != 0x00)) {
843 p++;
844 }
845 p += 2;
846}
847
848 }
849 return FindFirstDmiTableOfType(type, min_length);
850}
851

Archive Download this file

Revision: 103