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
104// defaults for an iMac11,1 core i5/i7
105static const SMStrEntryPair const sm_imacCore_i5_i7_defaults[]={
106{"SMbiosvendor","Apple Inc."},
107{"SMbiosversion","IM111.0034.B00"},
108{"SMbiosdate","06/01/2009"},
109{"SMmanufacter","Apple Inc."},
110{"SMproductname","iMac11,1"},
111{"SMsystemversion","1.0"},
112{"SMserial","SOMESRLNMBR"},
113{"SMfamily","iMac"},
114{"SMboardmanufacter","Apple Computer, Inc."},
115{"SMboardproduct","Mac-F2268DAE"},
116{ "",""}
117};
118
119static const char* sm_get_defstr(const char * key, int table_num)
120{
121inti;
122const SMStrEntryPair*sm_defaults;
123
124if (platformCPUFeature(CPU_FEATURE_MOBILE)) {
125if (Platform.CPU.NoCores > 1) {
126sm_defaults=sm_macbookpro_defaults;
127} else {
128sm_defaults=sm_macbook_defaults;
129}
130} else {
131switch (Platform.CPU.NoCores)
132{
133case 1:
134sm_defaults=sm_macmini_defaults;
135break;
136case 2:
137sm_defaults=sm_imac_defaults;
138break;
139default:
140{
141switch (Platform.CPU.Family)
142{
143case 0x06:
144{
145switch (Platform.CPU.Model)
146{
147case 0x19: // Intel Core i5 650
148case 0x1E: // Intel Core i7 LGA1156 (45nm)
149case 0x1F: // Intel Core i5 LGA1156 (45nm)
150sm_defaults=sm_imacCore_i5_i7_defaults;
151break;
152default:
153sm_defaults=sm_macpro_defaults;
154break;
155}
156break;
157}
158default:
159sm_defaults=sm_macpro_defaults;
160break;
161}
162break;
163}
164}
165}
166
167for (i=0; sm_defaults[i].key[0]; i++) {
168if (!strcmp (sm_defaults[i].key, key)) {
169return sm_defaults[i].value;
170}
171}
172
173// Shouldn't happen
174printf ("Error: no default for '%s' known\n", key);
175sleep (2);
176return "";
177}
178
179static int sm_get_fsb(const char *name, int table_num)
180{
181return Platform.CPU.FSBFrequency/1000000;
182}
183
184static int sm_get_cpu (const char *name, int table_num)
185{
186return Platform.CPU.CPUFrequency/1000000;
187}
188
189static int sm_get_simplecputype()
190{
191if (Platform.CPU.NoCores >= 4)
192{
193return 0x0501; // Quad-Core Xeon
194}
195else if (Platform.CPU.NoCores == 1)
196{
197return 0x0201; // Core Solo
198};
199
200return 0x0301; // Core 2 Duo
201}
202
203static int sm_get_bus_speed (const char *name, int table_num)
204{
205if (Platform.CPU.Vendor == 0x756E6547) // Intel
206{
207switch (Platform.CPU.Family)
208{
209case 0x06:
210{
211switch (Platform.CPU.Model)
212{
213case 0x0F: // Intel Core (65nm)
214case 0x17: // Intel Core (45nm)
215case 0x1C: // Intel Atom (45nm)
216return 0; // TODO: populate bus speed for these processors
217case 0x19: // Intel Core i5 650 @3.20 Ghz
218return 3600; // GT/s / 1000
219case 0x1A: // Intel Core i7 LGA1366 (45nm)
220case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
221case 0x1F: // Intel Core i5, i7 LGA1156 (45nm) ???
222return 4800; // GT/s / 1000
223case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
224return 0; // TODO: populate bus speed for these processors
225case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
226case 0x2E: // Intel Core i7 LGA1366 (45nm) 6 Core ???
227return 0; // TODO: populate bus speed for these processors
228}
229}
230}
231}
232return 0;
233}
234
235static int sm_get_cputype (const char *name, int table_num)
236{
237static bool done = false;
238
239if (Platform.CPU.Vendor == 0x756E6547) // Intel
240{
241if (!done) {
242verbose("CPU is Intel, family 0x%x, model 0x%x, ext.model 0x%x\n", Platform.CPU.Family, Platform.CPU.Model, Platform.CPU.ExtModel);
243done = true;
244}
245
246switch (Platform.CPU.Family)
247{
248case 0x06:
249{
250switch (Platform.CPU.Model)
251{
252case 0x0F: // Intel Core (65nm)
253case 0x17: // Intel Core (45nm)
254case 0x1C: // Intel Atom (45nm)
255return sm_get_simplecputype();
256case 0x1A: // Intel Core i7 LGA1366 (45nm)
257return 0x0701;
258case 0x1E: // Intel Core i5, i7 LGA1156 (45nm)
259// get this opportunity to fill the known processor interconnect speed for cor i5/i7 in GT/s
260return 0x0701;
261case 0x19: // Intel Core i5 650 @3.20 Ghz
262case 0x1F: // Intel Core i5, i7 LGA1156 (45nm) ???
263return 0x0601;
264case 0x25: // Intel Core i3, i5, i7 LGA1156 (32nm)
265return 0x0301;
266case 0x2C: // Intel Core i7 LGA1366 (32nm) 6 Core
267case 0x2E: // Intel Core i7 LGA1366 (45nm) 6 Core ???
268return 0x0601;
269}
270}
271}
272}
273
274return sm_get_simplecputype();
275}
276
277static int sm_get_memtype (const char *name, int table_num)
278{
279intmap;
280
281if (table_num < MAX_RAM_SLOTS) {
282map = Platform.DMI.DIMM[table_num];
283if (Platform.RAM.DIMM[map].InUse && Platform.RAM.DIMM[map].Type != 0) {
284 DBG("RAM Detected Type = %d\n", Platform.RAM.DIMM[map].Type);
285 return Platform.RAM.DIMM[map].Type;
286}
287}
288
289return SMB_MEM_TYPE_DDR2;
290}
291
292static int sm_get_memspeed (const char *name, int table_num)
293{
294intmap;
295
296if (table_num < MAX_RAM_SLOTS) {
297map = Platform.DMI.DIMM[table_num];
298if (Platform.RAM.DIMM[map].InUse && Platform.RAM.DIMM[map].Frequency != 0) {
299 DBG("RAM Detected Freq = %d Mhz\n", Platform.RAM.DIMM[map].Frequency);
300 return Platform.RAM.DIMM[map].Frequency;
301}
302}
303
304return 800;
305}
306
307static const char *sm_get_memvendor (const char *name, int table_num)
308{
309intmap;
310
311if (table_num < MAX_RAM_SLOTS) {
312map = Platform.DMI.DIMM[table_num];
313if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].Vendor) > 0) {
314DBG("RAM Detected Vendor[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].Vendor);
315return Platform.RAM.DIMM[map].Vendor;
316}
317}
318return "N/A";
319}
320
321static const char *sm_get_memserial (const char *name, int table_num)
322{
323intmap;
324
325if (table_num < MAX_RAM_SLOTS) {
326map = Platform.DMI.DIMM[table_num];
327if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].SerialNo) > 0) {
328 DBG("name = %s, map=%d, RAM Detected SerialNo[%d]='%s'\n", name ? name : "",
329 map, table_num, Platform.RAM.DIMM[map].SerialNo);
330 return Platform.RAM.DIMM[map].SerialNo;
331}
332}
333return "N/A";
334}
335
336static const char *sm_get_mempartno (const char *name, int table_num)
337{
338intmap;
339
340if (table_num < MAX_RAM_SLOTS) {
341map = Platform.DMI.DIMM[table_num];
342if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].PartNo) > 0) {
343DBG("Ram Detected PartNo[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].PartNo);
344return Platform.RAM.DIMM[map].PartNo;
345}
346}
347return "N/A";
348}
349
350static int sm_one (int tablen)
351{
352return 1;
353}
354
355struct smbios_property smbios_properties[]=
356{
357{.name="SMbiosvendor",.table_type= 0,.value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
358{.name="SMbiosversion",.table_type= 0,.value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
359{.name="SMbiosdate",.table_type= 0,.value_type=SMSTRING,.offset=0x08,.auto_str=sm_get_defstr},
360{.name="SMmanufacter",.table_type= 1,.value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
361{.name="SMproductname",.table_type= 1,.value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
362{.name="SMsystemversion",.table_type= 1,.value_type=SMSTRING,.offset=0x06,.auto_str=sm_get_defstr},
363{.name="SMserial",.table_type= 1,.value_type=SMSTRING,.offset=0x07,.auto_str=sm_get_defstr},
364{.name="SMUUID",.table_type= 1, .value_type=SMOWORD,.offset=0x08,.auto_oword=0},
365{.name="SMfamily",.table_type= 1,.value_type=SMSTRING,.offset=0x1a,.auto_str=sm_get_defstr},
366{.name="SMboardmanufacter",.table_type= 2, .value_type=SMSTRING,.offset=0x04,.auto_str=sm_get_defstr},
367{.name="SMboardproduct",.table_type= 2, .value_type=SMSTRING,.offset=0x05,.auto_str=sm_get_defstr},
368{.name="SMexternalclock",.table_type= 4,.value_type=SMWORD,.offset=0x12,.auto_int=sm_get_fsb},
369{.name="SMmaximalclock",.table_type= 4,.value_type=SMWORD,.offset=0x14,.auto_int=sm_get_cpu},
370{.name="SMmemdevloc",.table_type=17,.value_type=SMSTRING,.offset=0x10,.auto_str=0},
371{.name="SMmembankloc",.table_type=17,.value_type=SMSTRING,.offset=0x11,.auto_str=0},
372{.name="SMmemtype",.table_type=17,.value_type=SMBYTE,.offset=0x12,.auto_int=sm_get_memtype},
373{.name="SMmemspeed",.table_type=17,.value_type=SMWORD,.offset=0x15,.auto_int=sm_get_memspeed},
374{.name="SMmemmanufacter",.table_type=17,.value_type=SMSTRING,.offset=0x17,.auto_str=sm_get_memvendor},
375{.name="SMmemserial",.table_type=17,.value_type=SMSTRING,.offset=0x18,.auto_str=sm_get_memserial},
376{.name="SMmempart",.table_type=17,.value_type=SMSTRING,.offset=0x1A,.auto_str=sm_get_mempartno},
377{.name="SMcputype",.table_type=131,.value_type=SMWORD,.offset=0x04,.auto_int=sm_get_cputype},
378{.name="SMbusspeed",.table_type=132,.value_type=SMWORD,.offset=0x04,.auto_int=sm_get_bus_speed}
379};
380
381struct smbios_table_description smbios_table_descriptions[]=
382{
383{.type=0,.len=0x18,.numfunc=sm_one},
384{.type=1,.len=0x1b,.numfunc=sm_one},
385{.type=2,.len=0x0f,.numfunc=sm_one},
386{.type=4,.len=0x2a,.numfunc=sm_one},
387{.type=17,.len=0x1c,.numfunc=0},
388{.type=131,.len=0x06,.numfunc=sm_one},
389{.type=132,.len=0x06,.numfunc=sm_one}
390};
391
392// getting smbios addr with fast compare ops, late checksum testing ...
393#define COMPARE_DWORD(a,b) ( *((u_int32_t *) a) == *((u_int32_t *) b) )
394static const char * const SMTAG = "_SM_";
395static const char* const DMITAG= "_DMI_";
396
397static struct SMBEntryPoint *getAddressOfSmbiosTable(void)
398{
399struct SMBEntryPoint*smbios;
400/*
401 * The logic is to start at 0xf0000 and end at 0xfffff iterating 16 bytes at a time looking
402 * for the SMBIOS entry-point structure anchor (literal ASCII "_SM_").
403 */
404smbios = (struct SMBEntryPoint*) SMBIOS_RANGE_START;
405while (smbios <= (struct SMBEntryPoint *)SMBIOS_RANGE_END) {
406 if (COMPARE_DWORD(smbios->anchor, SMTAG) &&
407 COMPARE_DWORD(smbios->dmi.anchor, DMITAG) &&
408 smbios->dmi.anchor[4]==DMITAG[4] &&
409 checksum8(smbios, sizeof(struct SMBEntryPoint)) == 0)
410 {
411 return smbios;
412 }
413 smbios = (struct SMBEntryPoint*) ( ((char*) smbios) + 16 );
414}
415printf("ERROR: Unable to find SMBIOS!\n");
416pause();
417return NULL;
418}
419
420/** Compute necessary space requirements for new smbios */
421static struct SMBEntryPoint *smbios_dry_run(struct SMBEntryPoint *origsmbios)
422{
423struct SMBEntryPoint*ret;
424char*smbiostables;
425char*tablesptr;
426intorigsmbiosnum;
427inti, j;
428inttablespresent[256];
429booldo_auto=true;
430
431bzero(tablespresent, sizeof(tablespresent));
432
433getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);
434
435ret = (struct SMBEntryPoint *)AllocateKernelMemory(sizeof(struct SMBEntryPoint));
436if (origsmbios) {
437smbiostables = (char *)origsmbios->dmi.tableAddress;
438origsmbiosnum = origsmbios->dmi.structureCount;
439} else {
440smbiostables = NULL;
441origsmbiosnum = 0;
442}
443
444// _SM_
445ret->anchor[0] = 0x5f;
446ret->anchor[1] = 0x53;
447ret->anchor[2] = 0x4d;
448ret->anchor[3] = 0x5f;
449ret->entryPointLength = sizeof(*ret);
450ret->majorVersion = 2;
451ret->minorVersion = 1;
452ret->maxStructureSize = 0; // will be calculated later in this function
453ret->entryPointRevision = 0;
454for (i=0;i<5;i++) {
455ret->formattedArea[i] = 0;
456}
457//_DMI_
458ret->dmi.anchor[0] = 0x5f;
459ret->dmi.anchor[1] = 0x44;
460ret->dmi.anchor[2] = 0x4d;
461ret->dmi.anchor[3] = 0x49;
462ret->dmi.anchor[4] = 0x5f;
463ret->dmi.tableLength = 0; // will be calculated later in this function
464ret->dmi.tableAddress = 0; // will be initialized in smbios_real_run()
465ret->dmi.structureCount = 0; // will be calculated later in this function
466ret->dmi.bcdRevision = 0x21;
467tablesptr = smbiostables;
468
469 // add stringlen of overrides to original stringlen, update maxStructure size adequately,
470 // update structure count and tablepresent[type] with count of type.
471if (smbiostables) {
472for (i=0; i<origsmbiosnum; i++) {
473struct smbios_table_header*cur = (struct smbios_table_header *)tablesptr;
474char*stringsptr;
475intstringlen;
476
477tablesptr += cur->length;
478stringsptr = tablesptr;
479for (; tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++);
480tablesptr += 2;
481stringlen = tablesptr - stringsptr - 1;
482if (stringlen == 1) {
483stringlen = 0;
484}
485for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
486const char*str;
487intsize;
488charaltname[40];
489
490sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[cur->type] + 1);
491if (smbios_properties[j].table_type == cur->type &&
492 smbios_properties[j].value_type == SMSTRING &&
493 (getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig) ||
494 getValueForKey(altname,&str, &size, &bootInfo->smbiosConfig)))
495{
496stringlen += size + 1;
497} else if (smbios_properties[j].table_type == cur->type &&
498 smbios_properties[j].value_type == SMSTRING &&
499 do_auto && smbios_properties[j].auto_str)
500{
501stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[cur->type])) + 1;
502}
503}
504if (stringlen == 0) {
505stringlen = 1;
506}
507stringlen++;
508if (ret->maxStructureSize < cur->length+stringlen) {
509ret->maxStructureSize=cur->length+stringlen;
510}
511ret->dmi.tableLength += cur->length+stringlen;
512ret->dmi.structureCount++;
513tablespresent[cur->type]++;
514}
515}
516 // Add eventually table types whose detected count would be < required count, and update ret header with:
517 // new stringlen addons, structure count, and tablepresent[type] count adequately
518for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {
519intnumnec=-1;
520charbuffer[40];
521
522sprintf(buffer, "SMtable%d", i);
523if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {
524numnec = -1;
525}
526if (numnec==-1 && do_auto && smbios_table_descriptions[i].numfunc) {
527numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);
528}
529while (tablespresent[smbios_table_descriptions[i].type] < numnec) {
530intstringlen = 0;
531for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
532const char*str;
533intsize;
534charaltname[40];
535
536sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type] + 1);
537if (smbios_properties[j].table_type == smbios_table_descriptions[i].type &&
538 smbios_properties[j].value_type == SMSTRING &&
539 (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
540 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig)))
541{
542stringlen += size + 1;
543} else if (smbios_properties[j].table_type == smbios_table_descriptions[i].type &&
544 smbios_properties[j].value_type==SMSTRING &&
545 do_auto && smbios_properties[j].auto_str)
546{
547stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type])) + 1;
548}
549}
550if (stringlen == 0) {
551stringlen = 1;
552}
553stringlen++;
554if (ret->maxStructureSize < smbios_table_descriptions[i].len+stringlen) {
555ret->maxStructureSize = smbios_table_descriptions[i].len + stringlen;
556}
557ret->dmi.tableLength += smbios_table_descriptions[i].len + stringlen;
558ret->dmi.structureCount++;
559tablespresent[smbios_table_descriptions[i].type]++;
560}
561}
562return ret;
563}
564
565/** From the origsmbios detected by getAddressOfSmbiosTable() to newsmbios whose entrypoint
566 * struct has been created by smbios_dry_run, update each table struct content of new smbios
567 * int the new allocated table address of size newsmbios->tablelength.
568 */
569static void smbios_real_run(struct SMBEntryPoint * origsmbios, struct SMBEntryPoint * newsmbios)
570{
571char *smbiostables;
572char *tablesptr, *newtablesptr;
573int origsmbiosnum;
574// bitmask of used handles
575uint8_t handles[8192];
576uint16_t nexthandle=0;
577int i, j;
578int tablespresent[256];
579bool do_auto=true;
580
581 static bool done = false; // IMPROVEME: called twice via getSmbios(), but only the second call can get all necessary info !
582
583extern void dumpPhysAddr(const char * title, void * a, int len);
584
585bzero(tablespresent, sizeof(tablespresent));
586bzero(handles, sizeof(handles));
587
588getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);
589
590newsmbios->dmi.tableAddress = (uint32_t)AllocateKernelMemory(newsmbios->dmi.tableLength);
591if (origsmbios) {
592smbiostables = (char *)origsmbios->dmi.tableAddress;
593origsmbiosnum = origsmbios->dmi.structureCount;
594} else {
595smbiostables = NULL;
596origsmbiosnum = 0;
597}
598tablesptr = smbiostables;
599newtablesptr = (char *)newsmbios->dmi.tableAddress;
600
601 // if old smbios exists then update new smbios with old smbios original content first
602if (smbiostables) {
603for (i=0; i<origsmbiosnum; i++) {
604struct smbios_table_header*oldcur = (struct smbios_table_header *) tablesptr;
605struct smbios_table_header*newcur = (struct smbios_table_header *) newtablesptr;
606char*stringsptr;
607intnstrings = 0;
608
609handles[(oldcur->handle) / 8] |= 1 << ((oldcur->handle) % 8);
610
611 // copy table length from old table to new table but not the old strings
612memcpy(newcur,oldcur, oldcur->length);
613
614tablesptr += oldcur->length;
615stringsptr = tablesptr;
616newtablesptr += oldcur->length;
617
618 // calculate the number of strings in the old content
619for (;tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++) {
620if (tablesptr[0] == 0) {
621nstrings++;
622}
623}
624if (tablesptr != stringsptr) {
625nstrings++;
626}
627tablesptr += 2;
628
629 // copy the old strings to new table
630memcpy(newtablesptr, stringsptr, tablesptr-stringsptr);
631
632 // point to next possible space for a string (deducting the second 0 char at the end)
633newtablesptr += tablesptr - stringsptr - 1;
634 if (nstrings == 0) { // if no string was found rewind to the first 0 char of the 0,0 terminator
635newtablesptr--;
636}
637
638 // now for each property in the table update the overrides if any (auto or user)
639for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
640const char*str;
641intsize;
642intnum;
643charaltname[40];
644
645sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
646if (smbios_properties[j].table_type == newcur->type) {
647switch (smbios_properties[j].value_type) {
648case SMSTRING:
649if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
650 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
651{
652memcpy(newtablesptr, str, size);
653newtablesptr[size] = 0;
654newtablesptr += size + 1;
655*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
656} else if (do_auto && smbios_properties[j].auto_str) {
657str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
658size = strlen(str);
659memcpy(newtablesptr, str, size);
660newtablesptr[size] = 0;
661newtablesptr += size + 1;
662*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
663}
664break;
665
666case SMOWORD:
667if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
668 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
669{
670intk=0, t=0, kk=0;
671const char*ptr = str;
672memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
673while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
674ptr++;
675}
676if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
677ptr += 2;
678}
679for (;ptr-str<size && *ptr && k<16;ptr++) {
680if (*ptr>='0' && *ptr<='9') {
681(t=(t<<4)|(*ptr-'0')),kk++;
682}
683if (*ptr>='a' && *ptr<='f') {
684(t=(t<<4)|(*ptr-'a'+10)),kk++;
685}
686if (*ptr>='A' && *ptr<='F') {
687(t=(t<<4)|(*ptr-'A'+10)),kk++;
688}
689if (kk == 2) {
690*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
691k++;
692kk = 0;
693t = 0;
694}
695}
696}
697break;
698
699case SMBYTE:
700if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
701 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
702{
703*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
704} else if (do_auto && smbios_properties[j].auto_int) {
705*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
706}
707break;
708
709case SMWORD:
710if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
711 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
712{
713*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
714} else if (do_auto && smbios_properties[j].auto_int) {
715*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
716}
717break;
718}
719}
720}
721if (nstrings == 0) {
722newtablesptr[0] = 0;
723newtablesptr++;
724}
725newtablesptr[0] = 0;
726newtablesptr++;
727tablespresent[newcur->type]++;
728}
729}
730
731 // for each eventual complementary table not present in the original smbios, do the overrides
732for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {
733intnumnec = -1;
734charbuffer[40];
735
736sprintf(buffer, "SMtable%d", i);
737if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {
738numnec = -1;
739}
740if (numnec == -1 && do_auto && smbios_table_descriptions[i].numfunc) {
741numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);
742}
743while (tablespresent[smbios_table_descriptions[i].type] < numnec) {
744struct smbios_table_header*newcur = (struct smbios_table_header *) newtablesptr;
745intnstrings = 0;
746
747memset(newcur,0, smbios_table_descriptions[i].len);
748while (handles[(nexthandle)/8] & (1 << ((nexthandle) % 8))) {
749nexthandle++;
750}
751newcur->handle = nexthandle;
752handles[nexthandle / 8] |= 1 << (nexthandle % 8);
753newcur->type = smbios_table_descriptions[i].type;
754newcur->length = smbios_table_descriptions[i].len;
755newtablesptr += smbios_table_descriptions[i].len;
756for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {
757const char*str;
758intsize;
759intnum;
760charaltname[40];
761
762sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);
763if (smbios_properties[j].table_type == newcur->type) {
764switch (smbios_properties[j].value_type) {
765case SMSTRING:
766if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
767 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
768{
769memcpy(newtablesptr, str, size);
770newtablesptr[size] = 0;
771newtablesptr += size + 1;
772*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
773} else if (do_auto && smbios_properties[j].auto_str) {
774str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);
775size = strlen(str);
776memcpy(newtablesptr, str, size);
777newtablesptr[size] = 0;
778newtablesptr += size + 1;
779*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;
780}
781break;
782
783case SMOWORD:
784if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||
785 getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))
786{
787intk=0, t=0, kk=0;
788const char*ptr = str;
789
790memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);
791while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {
792ptr++;
793}
794if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {
795ptr += 2;
796}
797for (;ptr-str<size && *ptr && k<16;ptr++) {
798if (*ptr>='0' && *ptr<='9') {
799(t=(t<<4)|(*ptr-'0')),kk++;
800}
801if (*ptr>='a' && *ptr<='f') {
802(t=(t<<4)|(*ptr-'a'+10)),kk++;
803}
804if (*ptr>='A' && *ptr<='F') {
805(t=(t<<4)|(*ptr-'A'+10)),kk++;
806}
807if (kk == 2) {
808*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;
809k++;
810kk = 0;
811t = 0;
812}
813}
814}
815break;
816
817case SMBYTE:
818if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
819 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
820{
821*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
822} else if (do_auto && smbios_properties[j].auto_int) {
823*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
824}
825break;
826
827case SMWORD:
828if (getIntForKey(altname, &num, &bootInfo->smbiosConfig) ||
829 getIntForKey(smbios_properties[j].name, &num, &bootInfo->smbiosConfig))
830{
831*((uint16_t*)(((char*)newcur) + smbios_properties[j].offset)) = num;
832} else if (do_auto && smbios_properties[j].auto_int) {
833*((uint16_t*)(((char*)newcur)+smbios_properties[j].offset)) = smbios_properties[j].auto_int(smbios_properties[j].name, tablespresent[newcur->type]);
834}
835break;
836}
837}
838}
839if (nstrings == 0) {
840newtablesptr[0] = 0;
841newtablesptr++;
842}
843newtablesptr[0] = 0;
844newtablesptr++;
845tablespresent[smbios_table_descriptions[i].type]++;
846}
847}
848
849 // calculate new checksums
850newsmbios->dmi.checksum = 0;
851newsmbios->dmi.checksum = 256 - checksum8(&newsmbios->dmi, sizeof(newsmbios->dmi));
852newsmbios->checksum = 0;
853newsmbios->checksum = 256 - checksum8(newsmbios, sizeof(*newsmbios));
854
855if (!done) {
856verbose("Patched DMI Table\n");
857done=true;
858}
859}
860
861#define MAX_DMI_TABLES 96
862typedef struct DmiNumAssocTag {
863 struct DMIHeader * dmi;
864 uint8_t type;
865} DmiNumAssoc;
866
867static DmiNumAssoc DmiTablePair[MAX_DMI_TABLES];
868static int DmiTablePairCount = 0;
869static int current_pos=0;
870static bool ftTablePairInit = true;
871
872/**
873 * Get a table structure entry from a type specification and a smbios address
874 * return NULL if table is not found
875 */
876static void getSmbiosTableStructure(struct SMBEntryPoint *smbios)
877{
878 struct DMIHeader * dmihdr=NULL;
879 SMBByte* p;
880 int i;
881
882 if (ftTablePairInit && smbios!=NULL) {
883 ftTablePairInit = false;
884#if DEBUG_SMBIOS
885 printf(">>> SMBIOSAddr=0x%08x\n", smbios);
886 printf(">>> DMI: addr=0x%08x, len=%d, count=%d\n", smbios->dmi.tableAddress,
887 smbios->dmi.tableLength, smbios->dmi.structureCount);
888#endif
889 p = (SMBByte *) smbios->dmi.tableAddress;
890 for (i=0;
891 i < smbios->dmi.structureCount &&
892 p + 4 <= (SMBByte *)smbios->dmi.tableAddress + smbios->dmi.tableLength;
893 i++) {
894 dmihdr = (struct DMIHeader *) p;
895
896#if DEBUG_SMBIOS
897 // verbose(">>>>>> DMI(%d): type=0x%02x, len=0x%d\n",i,dmihdr->type,dmihdr->length);
898#endif
899 if (dmihdr->length < 4 || dmihdr->type == 127 /* EOT */) break;
900 if (DmiTablePairCount < MAX_DMI_TABLES) {
901 DmiTablePair[DmiTablePairCount].dmi = dmihdr;
902 DmiTablePair[DmiTablePairCount].type = dmihdr->type;
903 DmiTablePairCount++;
904 }
905 else {
906 printf("DMI table entries list is full! Next entries won't be stored.\n");
907 }
908#if DEBUG_SMBIOS
909 printf("DMI header found for table type %d, length = %d\n", dmihdr->type, dmihdr->length);
910#endif
911 p = p + dmihdr->length;
912 while ((p - (SMBByte *)smbios->dmi.tableAddress + 1 < smbios->dmi.tableLength) && (p[0] != 0x00 || p[1] != 0x00)) {
913 p++;
914 }
915 p += 2;
916}
917
918 }
919}
920
921/** Get original or new smbios entry point, if sucessful, the adresses are cached for next time */
922struct SMBEntryPoint *getSmbios(int which)
923{
924 static struct SMBEntryPoint *orig = NULL; // cached
925 static struct SMBEntryPoint *patched = NULL; // cached
926
927 // whatever we are called with orig or new flag, initialize asap both structures
928 switch (which) {
929 case SMBIOS_ORIGINAL:
930 if (orig==NULL) {
931 orig = getAddressOfSmbiosTable();
932 getSmbiosTableStructure(orig); // generate tables entry list for fast table finding
933 }
934 return orig;
935 case SMBIOS_PATCHED:
936 if (orig==NULL && (orig = getAddressOfSmbiosTable())==NULL ) {
937 printf("Could not find original SMBIOS !!\n");
938 pause();
939 } else {
940 patched = smbios_dry_run(orig);
941 if(patched==NULL) {
942 printf("Could not create new SMBIOS !!\n");
943 pause();
944 }
945 else {
946 smbios_real_run(orig, patched);
947 }
948 }
949
950 return patched;
951 default:
952 printf("ERROR: invalid option for getSmbios() !!\n");
953 break;
954 }
955
956 return NULL;
957}
958
959/** Find first original dmi Table with a particular type */
960struct DMIHeader* FindFirstDmiTableOfType(int type, int minlength)
961{
962 current_pos = 0;
963
964 return FindNextDmiTableOfType(type, minlength);
965};
966
967/** Find next original dmi Table with a particular type */
968struct DMIHeader* FindNextDmiTableOfType(int type, int minlength)
969{
970 int i;
971
972 if (ftTablePairInit) getSmbios(SMBIOS_ORIGINAL);
973
974 for (i=current_pos; i < DmiTablePairCount; i++) {
975 if (type == DmiTablePair[i].type &&
976 DmiTablePair[i].dmi &&
977 DmiTablePair[i].dmi->length >= minlength ) {
978 current_pos = i+1;
979 return DmiTablePair[i].dmi;
980 }
981 }
982 return NULL; // not found
983};
984
985

Archive Download this file

Revision: 276