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

Archive Download this file

Revision: 271