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 | ␊ |
24 | typedef struct {␊ |
25 | const char* key;␊ |
26 | const char* value;␊ |
27 | } SMStrEntryPair;␊ |
28 | ␊ |
29 | // defaults for a MacBook␊ |
30 | static 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␊ |
45 | static 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 ␊ |
60 | static 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␊ |
75 | static 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␊ |
90 | static 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 | static const char* sm_get_defstr(const char * key, int table_num)␊ |
105 | {␊ |
106 | ␉int␉i;␊ |
107 | ␉const SMStrEntryPair*␉sm_defaults;␊ |
108 | ␊ |
109 | ␉if (platformCPUFeature(CPU_FEATURE_MOBILE)) {␊ |
110 | ␉␉if (Platform.CPU.NoCores > 1) {␊ |
111 | ␉␉␉sm_defaults=sm_macbookpro_defaults;␊ |
112 | ␉␉} else {␊ |
113 | ␉␉␉sm_defaults=sm_macbook_defaults;␊ |
114 | ␉␉}␊ |
115 | ␉} else {␊ |
116 | ␉␉switch (Platform.CPU.NoCores) {␊ |
117 | ␉␉case 1: sm_defaults=sm_macmini_defaults; break;␊ |
118 | ␉␉case 2: sm_defaults=sm_imac_defaults; break;␊ |
119 | ␉␉default: sm_defaults=sm_macpro_defaults; break;␊ |
120 | ␉␉}␊ |
121 | ␉}␊ |
122 | ␊ |
123 | ␉for (i=0; sm_defaults[i].key[0]; i++) {␊ |
124 | ␉␉if (!strcmp (sm_defaults[i].key, key)) {␊ |
125 | ␉␉␉return sm_defaults[i].value;␊ |
126 | ␉␉}␊ |
127 | ␉}␊ |
128 | ␊ |
129 | ␉// Shouldn't happen␊ |
130 | ␉printf ("Error: no default for '%s' known\n", key);␊ |
131 | ␉sleep (2);␊ |
132 | ␉return "";␊ |
133 | }␊ |
134 | ␊ |
135 | static int sm_get_fsb(const char *name, int table_num)␊ |
136 | {␊ |
137 | ␉return Platform.CPU.FSBFrequency/1000000;␊ |
138 | }␊ |
139 | ␊ |
140 | static int sm_get_cpu (const char *name, int table_num)␊ |
141 | {␊ |
142 | ␉return Platform.CPU.CPUFrequency/1000000;␊ |
143 | }␊ |
144 | ␊ |
145 | static int sm_get_cputype (const char *name, int table_num)␊ |
146 | {␊ |
147 | ␉if (Platform.CPU.NoCores == 1) {␊ |
148 | ␉␉return 0x0101; // <01 01> Intel Core Solo?␊ |
149 | ␉} else if (Platform.CPU.NoCores == 2) {␊ |
150 | ␉␉return 0x0301; // <01 03> Intel Core 2 Duo␊ |
151 | ␉} else if (Platform.CPU.NoCores >= 4) {␊ |
152 | ␉␉return 0x0501; // <01 05> Quad-Core Intel Xeon␊ |
153 | ␉} else {␊ |
154 | ␉␉return 0x0301; // Default to Core 2 Duo␊ |
155 | ␉}␊ |
156 | }␊ |
157 | ␊ |
158 | static int sm_get_memtype (const char *name, int table_num)␊ |
159 | {␊ |
160 | ␉int␉map;␊ |
161 | ␊ |
162 | ␉if (table_num < MAX_RAM_SLOTS) {␊ |
163 | ␉␉map = Platform.DMI.DIMM[table_num];␊ |
164 | ␉␉if (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 | ␉}␊ |
169 | ␉return SMB_MEM_TYPE_DDR2;␊ |
170 | }␊ |
171 | ␊ |
172 | static int sm_get_memspeed (const char *name, int table_num)␊ |
173 | {␊ |
174 | ␉int␉map;␊ |
175 | ␊ |
176 | ␉if (table_num < MAX_RAM_SLOTS) {␊ |
177 | ␉␉map = Platform.DMI.DIMM[table_num];␊ |
178 | ␉␉if (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 | ␊ |
184 | ␉return 800;␊ |
185 | }␊ |
186 | ␊ |
187 | static const char *sm_get_memvendor (const char *name, int table_num)␊ |
188 | {␊ |
189 | ␉int␉map;␊ |
190 | ␊ |
191 | ␉if (table_num < MAX_RAM_SLOTS) {␊ |
192 | ␉␉map = Platform.DMI.DIMM[table_num];␊ |
193 | ␉␉if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].Vendor) > 0) {␊ |
194 | ␉␉␉DBG("RAM Detected Vendor[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].Vendor);␊ |
195 | ␉␉␉return Platform.RAM.DIMM[map].Vendor;␊ |
196 | ␉␉}␊ |
197 | ␉}␊ |
198 | ␉return "N/A";␊ |
199 | }␊ |
200 | ␉␊ |
201 | static const char *sm_get_memserial (const char *name, int table_num)␊ |
202 | {␊ |
203 | ␉int␉map;␊ |
204 | ␊ |
205 | ␉if (table_num < MAX_RAM_SLOTS) {␊ |
206 | ␉␉map = Platform.DMI.DIMM[table_num];␊ |
207 | ␉␉if (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 | ␉}␊ |
213 | ␉return "N/A";␊ |
214 | }␊ |
215 | ␊ |
216 | static const char *sm_get_mempartno (const char *name, int table_num)␊ |
217 | {␊ |
218 | ␉int␉map;␊ |
219 | ␊ |
220 | ␉if (table_num < MAX_RAM_SLOTS) {␊ |
221 | ␉␉map = Platform.DMI.DIMM[table_num];␊ |
222 | ␉␉if (Platform.RAM.DIMM[map].InUse && strlen(Platform.RAM.DIMM[map].PartNo) > 0) {␊ |
223 | ␉␉␉DBG("Ram Detected PartNo[%d]='%s'\n", table_num, Platform.RAM.DIMM[map].PartNo);␊ |
224 | ␉␉␉return Platform.RAM.DIMM[map].PartNo;␊ |
225 | ␉␉}␊ |
226 | ␉}␊ |
227 | ␉return "N/A";␊ |
228 | }␊ |
229 | ␊ |
230 | static int sm_one (int tablen)␊ |
231 | {␊ |
232 | ␉return 1;␊ |
233 | }␊ |
234 | ␊ |
235 | struct 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 | ␊ |
261 | struct 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) )␊ |
274 | static const char * const SMTAG = "_SM_";␊ |
275 | static const char* const DMITAG= "_DMI_";␊ |
276 | ␊ |
277 | static struct SMBEntryPoint *getAddressOfSmbiosTable(void)␊ |
278 | {␊ |
279 | ␉struct 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 | ␉ */␊ |
285 | ␉smbios = (struct SMBEntryPoint*) SMBIOS_RANGE_START;␊ |
286 | ␉while (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 | ␉}␊ |
294 | ␉printf("ERROR: Unable to find SMBIOS!\n");␊ |
295 | ␉sleep(5);␊ |
296 | ␉return NULL;␊ |
297 | }␊ |
298 | ␊ |
299 | /** Compute necessary space requirements for new smbios */␊ |
300 | struct SMBEntryPoint *smbios_dry_run(struct SMBEntryPoint *origsmbios)␊ |
301 | {␊ |
302 | ␉struct SMBEntryPoint␉*ret;␊ |
303 | ␉char␉␉␉*smbiostables;␊ |
304 | ␉char␉␉␉*tablesptr;␊ |
305 | ␉int␉␉␉origsmbiosnum;␊ |
306 | ␉int␉␉␉i, j;␊ |
307 | ␉int␉␉␉tablespresent[256];␊ |
308 | ␉bool␉␉␉do_auto=true;␊ |
309 | ␊ |
310 | ␉bzero(tablespresent, sizeof(tablespresent));␊ |
311 | ␊ |
312 | ␉getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);␊ |
313 | ␊ |
314 | ␉ret = (struct SMBEntryPoint *)AllocateKernelMemory(sizeof(struct SMBEntryPoint));␊ |
315 | ␉if (origsmbios) {␊ |
316 | ␉␉smbiostables = (char *)origsmbios->dmi.tableAddress;␊ |
317 | ␉␉origsmbiosnum = origsmbios->dmi.structureCount;␊ |
318 | ␉} else {␊ |
319 | ␉␉smbiostables = NULL;␊ |
320 | ␉␉origsmbiosnum = 0;␊ |
321 | ␉}␊ |
322 | ␊ |
323 | ␉// _SM_␊ |
324 | ␉ret->anchor[0] = 0x5f;␊ |
325 | ␉ret->anchor[1] = 0x53;␊ |
326 | ␉ret->anchor[2] = 0x4d;␊ |
327 | ␉ret->anchor[3] = 0x5f; ␊ |
328 | ␉ret->entryPointLength = sizeof(*ret);␊ |
329 | ␉ret->majorVersion = 2;␊ |
330 | ␉ret->minorVersion = 1;␊ |
331 | ␉ret->maxStructureSize = 0; // will be calculated later in this function␊ |
332 | ␉ret->entryPointRevision = 0;␊ |
333 | ␉for (i=0;i<5;i++) {␊ |
334 | ␉␉ret->formattedArea[i] = 0;␊ |
335 | ␉}␊ |
336 | ␉//_DMI_␊ |
337 | ␉ret->dmi.anchor[0] = 0x5f;␊ |
338 | ␉ret->dmi.anchor[1] = 0x44;␊ |
339 | ␉ret->dmi.anchor[2] = 0x4d;␊ |
340 | ␉ret->dmi.anchor[3] = 0x49;␊ |
341 | ␉ret->dmi.anchor[4] = 0x5f;␊ |
342 | ␉ret->dmi.tableLength = 0; // will be calculated later in this function␊ |
343 | ␉ret->dmi.tableAddress = 0; // will be initialized in smbios_real_run()␊ |
344 | ␉ret->dmi.structureCount = 0; // will be calculated later in this function␊ |
345 | ␉ret->dmi.bcdRevision = 0x21;␊ |
346 | ␉tablesptr = 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. ␊ |
350 | ␉if (smbiostables) {␊ |
351 | ␉␉for (i=0; i<origsmbiosnum; i++) {␊ |
352 | ␉␉␉struct smbios_table_header␉*cur = (struct smbios_table_header *)tablesptr;␊ |
353 | ␉␉␉char␉␉␉␉*stringsptr;␊ |
354 | ␉␉␉int␉␉␉␉stringlen;␊ |
355 | ␊ |
356 | ␉␉␉tablesptr += cur->length;␊ |
357 | ␉␉␉stringsptr = tablesptr;␊ |
358 | ␉␉␉for (; tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++);␊ |
359 | ␉␉␉tablesptr += 2;␊ |
360 | ␉␉␉stringlen = tablesptr - stringsptr - 1;␊ |
361 | ␉␉␉if (stringlen == 1) {␊ |
362 | ␉␉␉␉stringlen = 0;␊ |
363 | ␉␉␉}␊ |
364 | ␉␉␉for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {␊ |
365 | ␉␉␉␉const char␉*str;␊ |
366 | ␉␉␉␉int␉␉size;␊ |
367 | ␉␉␉␉char␉␉altname[40];␊ |
368 | ␊ |
369 | ␉␉␉␉sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[cur->type] + 1);␉␉␉␉␊ |
370 | ␉␉␉␉if (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 | ␉␉␉␉{␊ |
375 | ␉␉␉␉␉stringlen += 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 | ␉␉␉␉{␊ |
380 | ␉␉␉␉␉stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[cur->type])) + 1;␊ |
381 | ␉␉␉␉}␊ |
382 | ␉␉␉}␊ |
383 | ␉␉␉if (stringlen == 0) {␊ |
384 | ␉␉␉␉stringlen = 1;␊ |
385 | ␉␉␉}␊ |
386 | ␉␉␉stringlen++;␊ |
387 | ␉␉␉if (ret->maxStructureSize < cur->length+stringlen) {␊ |
388 | ␉␉␉␉ret->maxStructureSize=cur->length+stringlen;␊ |
389 | ␉␉␉}␊ |
390 | ␉␉␉ret->dmi.tableLength += cur->length+stringlen;␊ |
391 | ␉␉␉ret->dmi.structureCount++;␊ |
392 | ␉␉␉tablespresent[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␊ |
397 | ␉for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {␊ |
398 | ␉␉int␉numnec=-1;␊ |
399 | ␉␉char␉buffer[40];␊ |
400 | ␊ |
401 | ␉␉sprintf(buffer, "SMtable%d", i);␊ |
402 | ␉␉if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {␊ |
403 | ␉␉␉numnec = -1;␊ |
404 | ␉␉}␊ |
405 | ␉␉if (numnec==-1 && do_auto && smbios_table_descriptions[i].numfunc) {␊ |
406 | ␉␉␉numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);␊ |
407 | ␉␉}␊ |
408 | ␉␉while (tablespresent[smbios_table_descriptions[i].type] < numnec) {␊ |
409 | ␉␉␉int␉stringlen = 0;␊ |
410 | ␉␉␉for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {␊ |
411 | ␉␉␉␉const char␉*str;␊ |
412 | ␉␉␉␉int␉␉size;␊ |
413 | ␉␉␉␉char␉␉altname[40];␊ |
414 | ␊ |
415 | ␉␉␉␉sprintf(altname, "%s_%d",smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type] + 1);␊ |
416 | ␉␉␉␉if (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 | ␉␉␉␉{␊ |
421 | ␉␉␉␉␉stringlen += 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 | ␉␉␉␉{␊ |
426 | ␉␉␉␉␉stringlen += strlen(smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[smbios_table_descriptions[i].type])) + 1;␊ |
427 | ␉␉␉␉}␊ |
428 | ␉␉␉}␊ |
429 | ␉␉␉if (stringlen == 0) {␊ |
430 | ␉␉␉␉stringlen = 1;␊ |
431 | ␉␉␉}␊ |
432 | ␉␉␉stringlen++;␊ |
433 | ␉␉␉if (ret->maxStructureSize < smbios_table_descriptions[i].len+stringlen) {␊ |
434 | ␉␉␉␉ret->maxStructureSize = smbios_table_descriptions[i].len + stringlen;␊ |
435 | ␉␉␉}␊ |
436 | ␉␉␉ret->dmi.tableLength += smbios_table_descriptions[i].len + stringlen;␊ |
437 | ␉␉␉ret->dmi.structureCount++;␊ |
438 | ␉␉␉tablespresent[smbios_table_descriptions[i].type]++;␊ |
439 | ␉␉}␊ |
440 | ␉}␊ |
441 | ␉return 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 | */␊ |
448 | void smbios_real_run(struct SMBEntryPoint * origsmbios, struct SMBEntryPoint * newsmbios)␊ |
449 | {␊ |
450 | ␉char *smbiostables;␊ |
451 | ␉char *tablesptr, *newtablesptr;␊ |
452 | ␉int origsmbiosnum;␊ |
453 | ␉// bitmask of used handles␊ |
454 | ␉uint8_t handles[8192]; ␊ |
455 | ␉uint16_t nexthandle=0;␊ |
456 | ␉int i, j;␊ |
457 | ␉int tablespresent[256];␊ |
458 | ␉bool do_auto=true;␊ |
459 | ␊ |
460 | extern void dumpPhysAddr(const char * title, void * a, int len);␊ |
461 | ␊ |
462 | ␉bzero(tablespresent, sizeof(tablespresent));␊ |
463 | ␉bzero(handles, sizeof(handles));␊ |
464 | ␊ |
465 | ␉getBoolForKey(kSMBIOSdefaults, &do_auto, &bootInfo->bootConfig);␊ |
466 | ␉␊ |
467 | ␉newsmbios->dmi.tableAddress = (uint32_t)AllocateKernelMemory(newsmbios->dmi.tableLength);␊ |
468 | ␉if (origsmbios) {␊ |
469 | ␉␉smbiostables = (char *)origsmbios->dmi.tableAddress;␊ |
470 | ␉␉origsmbiosnum = origsmbios->dmi.structureCount;␊ |
471 | ␉} else {␊ |
472 | ␉␉smbiostables = NULL;␊ |
473 | ␉␉origsmbiosnum = 0;␊ |
474 | ␉}␊ |
475 | ␉tablesptr = smbiostables;␊ |
476 | ␉newtablesptr = (char *)newsmbios->dmi.tableAddress;␊ |
477 | ␊ |
478 | // if old smbios exists then update new smbios with old smbios original content first␊ |
479 | ␉if (smbiostables) {␊ |
480 | ␉␉for (i=0; i<origsmbiosnum; i++) {␊ |
481 | ␉␉␉struct smbios_table_header␉*oldcur = (struct smbios_table_header *) tablesptr;␊ |
482 | ␉␉␉struct smbios_table_header␉*newcur = (struct smbios_table_header *) newtablesptr;␊ |
483 | ␉␉␉char␉␉␉␉*stringsptr;␊ |
484 | ␉␉␉int␉␉␉␉nstrings = 0;␊ |
485 | ␊ |
486 | ␉␉␉handles[(oldcur->handle) / 8] |= 1 << ((oldcur->handle) % 8);␊ |
487 | ␊ |
488 | // copy table length from old table to new table but not the old strings␊ |
489 | ␉␉␉memcpy(newcur,oldcur, oldcur->length);␊ |
490 | ␊ |
491 | ␉␉␉tablesptr += oldcur->length;␊ |
492 | ␉␉␉stringsptr = tablesptr;␊ |
493 | ␉␉␉newtablesptr += oldcur->length;␊ |
494 | ␊ |
495 | // calculate the number of strings in the old content␊ |
496 | ␉␉␉for (;tablesptr[0]!=0 || tablesptr[1]!=0; tablesptr++) {␊ |
497 | ␉␉␉␉if (tablesptr[0] == 0) {␊ |
498 | ␉␉␉␉␉nstrings++;␊ |
499 | ␉␉␉␉}␊ |
500 | ␉␉␉}␊ |
501 | ␉␉␉if (tablesptr != stringsptr) {␊ |
502 | ␉␉␉␉nstrings++;␊ |
503 | ␉␉␉}␊ |
504 | ␉␉␉tablesptr += 2;␊ |
505 | ␊ |
506 | // copy the old strings to new table␊ |
507 | ␉␉␉memcpy(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)␊ |
518 | ␉␉␉newtablesptr += tablesptr - stringsptr - 1;␊ |
519 | if (nstrings == 0) { // if no string was found rewind to the first 0 char of the 0,0 terminator␊ |
520 | ␉␉␉␉newtablesptr--;␊ |
521 | ␉␉␉}␊ |
522 | ␊ |
523 | // now for each property in the table update the overrides if any (auto or user)␊ |
524 | ␉␉␉for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {␊ |
525 | ␉␉␉␉const char␉*str;␊ |
526 | ␉␉␉␉int␉␉size;␊ |
527 | ␉␉␉␉int␉␉num;␊ |
528 | ␉␉␉␉char␉␉altname[40];␊ |
529 | ␊ |
530 | ␉␉␉␉sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);␊ |
531 | ␉␉␉␉if (smbios_properties[j].table_type == newcur->type) {␊ |
532 | ␉␉␉␉␉switch (smbios_properties[j].value_type) {␊ |
533 | ␉␉␉␉␉case SMSTRING:␊ |
534 | ␉␉␉␉␉␉if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||␊ |
535 | ␉␉␉␉␉␉ getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))␊ |
536 | ␉␉␉␉␉␉{␊ |
537 | ␉␉␉␉␉␉␉memcpy(newtablesptr, str, size);␊ |
538 | ␉␉␉␉␉␉␉newtablesptr[size] = 0;␊ |
539 | ␉␉␉␉␉␉␉newtablesptr += size + 1;␊ |
540 | ␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;␊ |
541 | ␉␉␉␉␉␉} else if (do_auto && smbios_properties[j].auto_str) {␊ |
542 | ␉␉␉␉␉␉␉str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);␊ |
543 | ␉␉␉␉␉␉␉size = strlen(str);␊ |
544 | ␉␉␉␉␉␉␉memcpy(newtablesptr, str, size);␊ |
545 | ␉␉␉␉␉␉␉newtablesptr[size] = 0;␊ |
546 | ␉␉␉␉␉␉␉newtablesptr += size + 1;␊ |
547 | ␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;␊ |
548 | ␉␉␉␉␉␉}␊ |
549 | ␉␉␉␉␉␉break;␊ |
550 | ␊ |
551 | ␉␉␉␉␉case SMOWORD:␊ |
552 | ␉␉␉␉␉␉if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||␊ |
553 | ␉␉␉␉␉␉ getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))␊ |
554 | ␉␉␉␉␉␉{␊ |
555 | ␉␉␉␉␉␉␉int␉␉k=0, t=0, kk=0;␊ |
556 | ␉␉␉␉␉␉␉const char␉*ptr = str;␊ |
557 | ␉␉␉␉␉␉␉memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);␊ |
558 | ␉␉␉␉␉␉␉while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {␊ |
559 | ␉␉␉␉␉␉␉␉ptr++;␊ |
560 | ␉␉␉␉␉␉␉}␊ |
561 | ␉␉␉␉␉␉␉if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {␊ |
562 | ␉␉␉␉␉␉␉␉ptr += 2;␊ |
563 | ␉␉␉␉␉␉␉}␊ |
564 | ␉␉␉␉␉␉␉for (;ptr-str<size && *ptr && k<16;ptr++) {␊ |
565 | ␉␉␉␉␉␉␉␉if (*ptr>='0' && *ptr<='9') {␊ |
566 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'0')),kk++;␊ |
567 | ␉␉␉␉␉␉␉␉}␊ |
568 | ␉␉␉␉␉␉␉␉if (*ptr>='a' && *ptr<='f') {␊ |
569 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'a'+10)),kk++;␊ |
570 | ␉␉␉␉␉␉␉␉}␊ |
571 | ␉␉␉␉␉␉␉␉if (*ptr>='A' && *ptr<='F') {␊ |
572 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'A'+10)),kk++;␊ |
573 | ␉␉␉␉␉␉␉␉}␊ |
574 | ␉␉␉␉␉␉␉␉if (kk == 2) {␊ |
575 | ␉␉␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;␊ |
576 | ␉␉␉␉␉␉␉␉␉k++;␊ |
577 | ␉␉␉␉␉␉␉␉␉kk = 0;␊ |
578 | ␉␉␉␉␉␉␉␉␉t = 0;␊ |
579 | ␉␉␉␉␉␉␉␉}␊ |
580 | ␉␉␉␉␉␉␉}␊ |
581 | ␉␉␉␉␉␉}␊ |
582 | ␉␉␉␉␉␉break;␊ |
583 | ␊ |
584 | ␉␉␉␉␉case SMBYTE:␊ |
585 | ␉␉␉␉␉␉if (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 | ␉␉␉␉␉␉}␊ |
592 | ␉␉␉␉␉␉break;␊ |
593 | ␊ |
594 | ␉␉␉␉␉case SMWORD:␊ |
595 | ␉␉␉␉␉␉if (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 | ␉␉␉␉␉␉}␊ |
602 | ␉␉␉␉␉␉break;␊ |
603 | ␉␉␉␉␉}␊ |
604 | ␉␉␉␉}␊ |
605 | ␉␉␉}␊ |
606 | ␉␉␉if (nstrings == 0) {␊ |
607 | ␉␉␉␉newtablesptr[0] = 0;␊ |
608 | ␉␉␉␉newtablesptr++;␊ |
609 | ␉␉␉}␊ |
610 | ␉␉␉newtablesptr[0] = 0;␊ |
611 | ␉␉␉newtablesptr++;␊ |
612 | ␉␉␉tablespresent[newcur->type]++;␊ |
613 | ␉␉}␊ |
614 | ␉}␊ |
615 | ␊ |
616 | // for each eventual complementary table not present in the original smbios, do the overrides␊ |
617 | ␉for (i=0; i<sizeof(smbios_table_descriptions)/sizeof(smbios_table_descriptions[0]); i++) {␊ |
618 | ␉␉int␉numnec = -1;␊ |
619 | ␉␉char␉buffer[40];␊ |
620 | ␊ |
621 | ␉␉sprintf(buffer, "SMtable%d", i);␊ |
622 | ␉␉if (!getIntForKey(buffer, &numnec, &bootInfo->smbiosConfig)) {␊ |
623 | ␉␉␉numnec = -1;␊ |
624 | ␉␉}␊ |
625 | ␉␉if (numnec == -1 && do_auto && smbios_table_descriptions[i].numfunc) {␊ |
626 | ␉␉␉numnec = smbios_table_descriptions[i].numfunc(smbios_table_descriptions[i].type);␊ |
627 | ␉␉}␊ |
628 | ␉␉while (tablespresent[smbios_table_descriptions[i].type] < numnec) {␊ |
629 | ␉␉␉struct smbios_table_header␉*newcur = (struct smbios_table_header *) newtablesptr;␊ |
630 | ␉␉␉int␉␉␉␉nstrings = 0;␊ |
631 | ␊ |
632 | ␉␉␉memset(newcur,0, smbios_table_descriptions[i].len);␊ |
633 | ␉␉␉while (handles[(nexthandle)/8] & (1 << ((nexthandle) % 8))) {␊ |
634 | ␉␉␉␉nexthandle++;␊ |
635 | ␉␉␉}␊ |
636 | ␉␉␉newcur->handle = nexthandle;␊ |
637 | ␉␉␉handles[nexthandle / 8] |= 1 << (nexthandle % 8);␊ |
638 | ␉␉␉newcur->type = smbios_table_descriptions[i].type;␊ |
639 | ␉␉␉newcur->length = smbios_table_descriptions[i].len;␊ |
640 | ␉␉␉newtablesptr += smbios_table_descriptions[i].len;␊ |
641 | ␉␉␉for (j=0; j<sizeof(smbios_properties)/sizeof(smbios_properties[0]); j++) {␊ |
642 | ␉␉␉␉const char␉*str;␊ |
643 | ␉␉␉␉int␉␉size;␊ |
644 | ␉␉␉␉int␉␉num;␊ |
645 | ␉␉␉␉char␉␉altname[40];␊ |
646 | ␊ |
647 | ␉␉␉␉sprintf(altname, "%s_%d", smbios_properties[j].name, tablespresent[newcur->type] + 1);␊ |
648 | ␉␉␉␉if (smbios_properties[j].table_type == newcur->type) {␊ |
649 | ␉␉␉␉␉switch (smbios_properties[j].value_type) {␊ |
650 | ␉␉␉␉␉case SMSTRING:␊ |
651 | ␉␉␉␉␉␉if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||␊ |
652 | ␉␉␉␉␉␉ getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))␊ |
653 | ␉␉␉␉␉␉{␊ |
654 | ␉␉␉␉␉␉␉memcpy(newtablesptr, str, size);␊ |
655 | ␉␉␉␉␉␉␉newtablesptr[size] = 0;␊ |
656 | ␉␉␉␉␉␉␉newtablesptr += size + 1;␊ |
657 | ␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;␊ |
658 | ␉␉␉␉␉␉} else if (do_auto && smbios_properties[j].auto_str) {␊ |
659 | ␉␉␉␉␉␉␉str = smbios_properties[j].auto_str(smbios_properties[j].name, tablespresent[newcur->type]);␊ |
660 | ␉␉␉␉␉␉␉size = strlen(str);␊ |
661 | ␉␉␉␉␉␉␉memcpy(newtablesptr, str, size);␊ |
662 | ␉␉␉␉␉␉␉newtablesptr[size] = 0;␊ |
663 | ␉␉␉␉␉␉␉newtablesptr += size + 1;␊ |
664 | ␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset)) = ++nstrings;␊ |
665 | ␉␉␉␉␉␉}␊ |
666 | ␉␉␉␉␉␉break;␊ |
667 | ␊ |
668 | ␉␉␉␉␉case SMOWORD:␊ |
669 | ␉␉␉␉␉␉if (getValueForKey(altname, &str, &size, &bootInfo->smbiosConfig) ||␊ |
670 | ␉␉␉␉␉␉ getValueForKey(smbios_properties[j].name, &str, &size, &bootInfo->smbiosConfig))␊ |
671 | ␉␉␉␉␉␉{␊ |
672 | ␉␉␉␉␉␉␉int␉␉k=0, t=0, kk=0;␊ |
673 | ␉␉␉␉␉␉␉const char␉*ptr = str;␊ |
674 | ␊ |
675 | ␉␉␉␉␉␉␉memset(((char*)newcur) + smbios_properties[j].offset, 0, 16);␊ |
676 | ␉␉␉␉␉␉␉while (ptr-str<size && *ptr && (*ptr==' ' || *ptr=='\t' || *ptr=='\n')) {␊ |
677 | ␉␉␉␉␉␉␉␉ptr++;␊ |
678 | ␉␉␉␉␉␉␉}␊ |
679 | ␉␉␉␉␉␉␉if (size-(ptr-str)>=2 && ptr[0]=='0' && (ptr[1]=='x' || ptr[1]=='X')) {␊ |
680 | ␉␉␉␉␉␉␉␉ptr += 2;␊ |
681 | ␉␉␉␉␉␉␉}␊ |
682 | ␉␉␉␉␉␉␉for (;ptr-str<size && *ptr && k<16;ptr++) {␊ |
683 | ␉␉␉␉␉␉␉␉if (*ptr>='0' && *ptr<='9') {␊ |
684 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'0')),kk++;␊ |
685 | ␉␉␉␉␉␉␉␉}␊ |
686 | ␉␉␉␉␉␉␉␉if (*ptr>='a' && *ptr<='f') {␊ |
687 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'a'+10)),kk++;␊ |
688 | ␉␉␉␉␉␉␉␉}␊ |
689 | ␉␉␉␉␉␉␉␉if (*ptr>='A' && *ptr<='F') {␊ |
690 | ␉␉␉␉␉␉␉␉␉(t=(t<<4)|(*ptr-'A'+10)),kk++;␊ |
691 | ␉␉␉␉␉␉␉␉}␊ |
692 | ␉␉␉␉␉␉␉␉if (kk == 2) {␊ |
693 | ␉␉␉␉␉␉␉␉␉*((uint8_t*)(((char*)newcur) + smbios_properties[j].offset + k)) = t;␊ |
694 | ␉␉␉␉␉␉␉␉␉k++;␊ |
695 | ␉␉␉␉␉␉␉␉␉kk = 0;␊ |
696 | ␉␉␉␉␉␉␉␉␉t = 0;␊ |
697 | ␉␉␉␉␉␉␉␉}␊ |
698 | ␉␉␉␉␉␉␉}␊ |
699 | ␉␉␉␉␉␉}␊ |
700 | ␉␉␉␉␉␉break;␊ |
701 | ␉␉␉␉␉␉␊ |
702 | ␉␉␉␉␉case SMBYTE:␊ |
703 | ␉␉␉␉␉␉if (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 | ␉␉␉␉␉␉}␊ |
710 | ␉␉␉␉␉␉break;␊ |
711 | ␉␉␉␉␉␉␊ |
712 | ␉␉␉␉␉case SMWORD:␊ |
713 | ␉␉␉␉␉␉if (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 | ␉␉␉␉␉␉}␊ |
720 | ␉␉␉␉␉␉break;␊ |
721 | ␉␉␉␉␉}␊ |
722 | ␉␉␉␉}␊ |
723 | ␉␉␉}␊ |
724 | ␉␉␉if (nstrings == 0) {␊ |
725 | ␉␉␉␉newtablesptr[0] = 0;␊ |
726 | ␉␉␉␉newtablesptr++;␊ |
727 | ␉␉␉}␊ |
728 | ␉␉␉newtablesptr[0] = 0;␊ |
729 | ␉␉␉newtablesptr++;␊ |
730 | ␉␉␉tablespresent[smbios_table_descriptions[i].type]++;␊ |
731 | ␉␉}␊ |
732 | ␉}␊ |
733 | ␊ |
734 | // calculate new checksums␊ |
735 | ␉newsmbios->dmi.checksum = 0;␊ |
736 | ␉newsmbios->dmi.checksum = 256 - checksum8(&newsmbios->dmi, sizeof(newsmbios->dmi));␊ |
737 | ␉newsmbios->checksum = 0;␊ |
738 | ␉newsmbios->checksum = 256 - checksum8(newsmbios, sizeof(*newsmbios));␊ |
739 | ␉verbose("Patched DMI Table\n");␊ |
740 | }␊ |
741 | ␊ |
742 | struct SMBEntryPoint *getSmbios(int which)␊ |
743 | {␊ |
744 | static struct SMBEntryPoint *orig = NULL; // cached␊ |
745 | static struct SMBEntryPoint *patched = NULL; // cached␊ |
746 | ␊ |
747 | ␉if (orig == NULL) orig = getAddressOfSmbiosTable();␊ |
748 | ␊ |
749 | ␉switch (which) {␊ |
750 | ␉case SMBIOS_ORIGINAL:␊ |
751 | return orig;␊ |
752 | ␉case 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;␊ |
759 | ␉default:␊ |
760 | printf("ERROR: invalid option for getSmbios() !!\n");␊ |
761 | return NULL;␊ |
762 | ␉}␊ |
763 | }␊ |
764 | ␊ |
765 | #define MAX_DMI_TABLES 64␊ |
766 | typedef struct DmiNumAssocTag {␊ |
767 | struct DMIHeader * dmi;␊ |
768 | uint8_t type;␊ |
769 | } DmiNumAssoc;␊ |
770 | ␊ |
771 | static DmiNumAssoc DmiTablePair[MAX_DMI_TABLES];␊ |
772 | static int DmiTablePairCount = 0;␊ |
773 | static int current_pos=0;␊ |
774 | static bool ftTablePairInit = true;␊ |
775 | ␊ |
776 | /** Find first original dmi Table with a particular type */␊ |
777 | struct 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 */␊ |
787 | struct 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 | */␊ |
806 | struct 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 | |