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