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