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