Debian Codegraph @

src/udev/dmi_memory_id/dmi_memory_id.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * System Memory information
4 *
5 * Copyright (C) 2000-2002 Alan Cox <[email protected]>
6 * Copyright (C) 2002-2020 Jean Delvare <[email protected]>
7 * Copyright (C) 2020 Bastien Nocera <[email protected]>
8 *
9 * Unless specified otherwise, all references are aimed at the "System
10 * Management BIOS Reference Specification, Version 3.7.0" document,
11 * available from http://www.dmtf.org/standards/smbios.
12 *
13 * Note to contributors:
14 * Please reference every value you add or modify, especially if the
15 * information does not come from the above mentioned specification.
16 *
17 * Additional references:
18 * - Intel AP-485 revision 36
19 * "Intel Processor Identification and the CPUID Instruction"
20 * http://www.intel.com/support/processors/sb/cs-009861.htm
21 * - DMTF Common Information Model
22 * CIM Schema version 2.19.1
23 * http://www.dmtf.org/standards/cim/
24 * - IPMI 2.0 revision 1.0
25 * "Intelligent Platform Management Interface Specification"
26 * http://developer.intel.com/design/servers/ipmi/spec.htm
27 * - AMD publication #25481 revision 2.28
28 * "CPUID Specification"
29 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25481.pdf
30 * - BIOS Integrity Services Application Programming Interface version 1.0
31 * http://www.intel.com/design/archives/wfm/downloads/bisspec.htm
32 * - DMTF DSP0239 version 1.1.0
33 * "Management Component Transport Protocol (MCTP) IDs and Codes"
34 * http://www.dmtf.org/standards/pmci
35 * - "TPM Main, Part 2 TPM Structures"
36 * Specification version 1.2, level 2, revision 116
37 * https://trustedcomputinggroup.org/tpm-main-specification/
38 * - "PC Client Platform TPM Profile (PTP) Specification"
39 * Family "2.0", Level 00, Revision 00.43, January 26, 2015
40 * https://trustedcomputinggroup.org/pc-client-platform-tpm-profile-ptp-specification/
41 * - "RedFish Host Interface Specification" (DMTF DSP0270)
42 * https://www.dmtf.org/sites/default/files/DSP0270_1.0.1.pdf
43 */
44
45 #include <stdio.h>
46
47 #include "alloc-util.h"
48 #include "build.h"
49 #include "fileio.h"
50 #include "format-table.h"
51 #include "help-util.h"
52 #include "main-func.h"
53 #include "options.h"
54 #include "string-util.h"
55 #include "udev-util.h"
56 #include "unaligned.h"
57 #include "utf8.h"
58
59 #define SUPPORTED_SMBIOS_VER 0x030300
60
61 #define OUT_OF_SPEC_STR "<OUT OF SPEC>"
62
63 #define SYS_FIRMWARE_DIR "/sys/firmware/dmi/tables"
64 #define SYS_ENTRY_FILE SYS_FIRMWARE_DIR "/smbios_entry_point"
65 #define SYS_TABLE_FILE SYS_FIRMWARE_DIR "/DMI"
66
67 /*
68 * Per SMBIOS v2.8.0 and later, all structures assume a little-endian
69 * ordering convention.
70 */
71 #define WORD(x) (unaligned_read_le16(x))
72 #define DWORD(x) (unaligned_read_le32(x))
73 #define QWORD(x) (unaligned_read_le64(x))
74
75 struct dmi_header {
76 uint8_t type;
77 uint8_t length;
78 uint16_t handle;
79 const uint8_t *data;
80 };
81
82 static const char *arg_source_file = NULL;
83
84 static bool verify_checksum(const uint8_t *buf, size_t len) {
85 uint8_t sum = 0;
86
87 for (size_t a = 0; a < len; a++)
88 sum += buf[a];
89 return sum == 0;
90 }
91
92 /*
93 * Type-independent Stuff
94 */
95
96 static const char* dmi_string(const struct dmi_header *dm, uint8_t s) {
97 const char *bp = (const char *) dm->data;
98
99 if (s == 0)
100 return "Not Specified";
101
102 bp += dm->length;
103 for (; s > 1 && !isempty(bp); s--)
104 bp += strlen(bp) + 1;
105
106 if (isempty(bp))
107 return "<BAD INDEX>";
108
109 return bp;
110 }
111
112 typedef enum {
113 MEMORY_SIZE_UNIT_BYTES,
114 MEMORY_SIZE_UNIT_KB
115 } MemorySizeUnit;
116
117 static void dmi_print_memory_size(
118 const char *attr_prefix, const char *attr_suffix,
119 int slot_num, uint64_t code, MemorySizeUnit unit) {
120 if (unit == MEMORY_SIZE_UNIT_KB)
121 code <<= 10;
122
123 if (slot_num >= 0)
124 printf("%s_%i_%s=%"PRIu64"\n", attr_prefix, slot_num, attr_suffix, code);
125 else
126 printf("%s_%s=%"PRIu64"\n", attr_prefix, attr_suffix, code);
127 }
128
129 /*
130 * 7.17 Physical Memory Array (Type 16)
131 */
132
133 static void dmi_memory_array_location(uint8_t code) {
134 /* 7.17.1 */
135 static const char *location[] = {
136 [0x01] = "Other",
137 [0x02] = "Unknown",
138 [0x03] = "System Board Or Motherboard",
139 [0x04] = "ISA Add-on Card",
140 [0x05] = "EISA Add-on Card",
141 [0x06] = "PCI Add-on Card",
142 [0x07] = "MCA Add-on Card",
143 [0x08] = "PCMCIA Add-on Card",
144 [0x09] = "Proprietary Add-on Card",
145 [0x0A] = "NuBus",
146 };
147 static const char *location_0xA0[] = {
148 [0x00] = "PC-98/C20 Add-on Card", /* 0xA0 */
149 [0x01] = "PC-98/C24 Add-on Card", /* 0xA1 */
150 [0x02] = "PC-98/E Add-on Card", /* 0xA2 */
151 [0x03] = "PC-98/Local Bus Add-on Card", /* 0xA3 */
152 [0x04] = "CXL Add-on Card", /* 0xA4 */
153 };
154 const char *str = OUT_OF_SPEC_STR;
155
156 if (code < ELEMENTSOF(location) && location[code])
157 str = location[code];
158 else if (code >= 0xA0 && code < (ELEMENTSOF(location_0xA0) + 0xA0))
159 str = location_0xA0[code - 0xA0];
160
161 printf("MEMORY_ARRAY_LOCATION=%s\n", str);
162 }
163
164 static void dmi_memory_array_ec_type(uint8_t code) {
165 /* 7.17.3 */
166 static const char *type[] = {
167 [0x01] = "Other",
168 [0x02] = "Unknown",
169 [0x03] = "None",
170 [0x04] = "Parity",
171 [0x05] = "Single-bit ECC",
172 [0x06] = "Multi-bit ECC",
173 [0x07] = "CRC",
174 };
175
176 if (code != 0x03) /* Do not print "None". */
177 printf("MEMORY_ARRAY_EC_TYPE=%s\n",
178 code < ELEMENTSOF(type) && type[code] ? type[code] : OUT_OF_SPEC_STR);
179 }
180
181 /*
182 * 7.18 Memory Device (Type 17)
183 */
184
185 static void dmi_memory_device_string(
186 const char *attr_suffix, unsigned slot_num,
187 const struct dmi_header *h, uint8_t s) {
188 char *str;
189
190 str = strdupa_safe(dmi_string(h, s));
191 str = strstrip(str);
192 if (!isempty(str) && utf8_is_valid(str) && !string_has_cc(str, /* ok= */ NULL))
193 printf("MEMORY_DEVICE_%u_%s=%s\n", slot_num, attr_suffix, str);
194 }
195
196 static void dmi_memory_device_width(
197 const char *attr_suffix,
198 unsigned slot_num, uint16_t code) {
199
200 /* If no memory module is present, width may be 0 */
201 if (!IN_SET(code, 0, 0xFFFF))
202 printf("MEMORY_DEVICE_%u_%s=%u\n", slot_num, attr_suffix, code);
203 }
204
205 static void dmi_memory_device_size(unsigned slot_num, uint16_t code) {
206 if (code == 0)
207 return (void) printf("MEMORY_DEVICE_%u_PRESENT=0\n", slot_num);
208 if (code == 0xFFFF)
209 return;
210
211 uint64_t s = code & 0x7FFF;
212 if (!(code & 0x8000))
213 s <<= 10;
214 dmi_print_memory_size("MEMORY_DEVICE", "SIZE", slot_num, s, MEMORY_SIZE_UNIT_KB);
215 }
216
217 static void dmi_memory_device_extended_size(unsigned slot_num, uint32_t code) {
218 uint64_t capacity = (uint64_t) code * 1024 * 1024;
219
220 printf("MEMORY_DEVICE_%u_SIZE=%"PRIu64"\n", slot_num, capacity);
221 }
222
223 static void dmi_memory_device_rank(unsigned slot_num, uint8_t code) {
224 code &= 0x0F;
225 if (code != 0)
226 printf("MEMORY_DEVICE_%u_RANK=%u\n", slot_num, code);
227 }
228
229 static void dmi_memory_device_voltage_value(
230 const char *attr_suffix,
231 unsigned slot_num, uint16_t code) {
232 if (code == 0)
233 return;
234 if (code % 100 != 0)
235 printf("MEMORY_DEVICE_%u_%s=%g\n", slot_num, attr_suffix, (double)code / 1000);
236 else
237 printf("MEMORY_DEVICE_%u_%s=%.1g\n", slot_num, attr_suffix, (double)code / 1000);
238 }
239
240 static void dmi_memory_device_form_factor(unsigned slot_num, uint8_t code) {
241 /* 7.18.1 */
242 static const char *form_factor[] = {
243 [0x01] = "Other",
244 [0x02] = "Unknown",
245 [0x03] = "SIMM",
246 [0x04] = "SIP",
247 [0x05] = "Chip",
248 [0x06] = "DIP",
249 [0x07] = "ZIP",
250 [0x08] = "Proprietary Card",
251 [0x09] = "DIMM",
252 [0x0A] = "TSOP",
253 [0x0B] = "Row Of Chips",
254 [0x0C] = "RIMM",
255 [0x0D] = "SODIMM",
256 [0x0E] = "SRIMM",
257 [0x0F] = "FB-DIMM",
258 [0x10] = "Die",
259 };
260
261 printf("MEMORY_DEVICE_%u_FORM_FACTOR=%s\n", slot_num,
262 code < ELEMENTSOF(form_factor) && form_factor[code] ? form_factor[code] : OUT_OF_SPEC_STR);
263 }
264
265 static void dmi_memory_device_set(unsigned slot_num, uint8_t code) {
266 if (code == 0xFF)
267 printf("MEMORY_DEVICE_%u_SET=%s\n", slot_num, "Unknown");
268 else if (code != 0)
269 printf("MEMORY_DEVICE_%u_SET=%"PRIu8"\n", slot_num, code);
270 }
271
272 static void dmi_memory_device_type(unsigned slot_num, uint8_t code) {
273 /* 7.18.2 */
274 static const char *type[] = {
275 [0x01] = "Other",
276 [0x02] = "Unknown",
277 [0x03] = "DRAM",
278 [0x04] = "EDRAM",
279 [0x05] = "VRAM",
280 [0x06] = "SRAM",
281 [0x07] = "RAM",
282 [0x08] = "ROM",
283 [0x09] = "Flash",
284 [0x0A] = "EEPROM",
285 [0x0B] = "FEPROM",
286 [0x0C] = "EPROM",
287 [0x0D] = "CDRAM",
288 [0x0E] = "3DRAM",
289 [0x0F] = "SDRAM",
290 [0x10] = "SGRAM",
291 [0x11] = "RDRAM",
292 [0x12] = "DDR",
293 [0x13] = "DDR2",
294 [0x14] = "DDR2 FB-DIMM",
295 [0x15] = "Reserved",
296 [0x16] = "Reserved",
297 [0x17] = "Reserved",
298 [0x18] = "DDR3",
299 [0x19] = "FBD2",
300 [0x1A] = "DDR4",
301 [0x1B] = "LPDDR",
302 [0x1C] = "LPDDR2",
303 [0x1D] = "LPDDR3",
304 [0x1E] = "LPDDR4",
305 [0x1F] = "Logical non-volatile device",
306 [0x20] = "HBM",
307 [0x21] = "HBM2",
308 [0x22] = "DDR5",
309 [0x23] = "LPDDR5",
310 [0x24] = "HBM3",
311 };
312
313 printf("MEMORY_DEVICE_%u_TYPE=%s\n", slot_num,
314 code < ELEMENTSOF(type) && type[code] ? type[code] : OUT_OF_SPEC_STR);
315 }
316
317 static void dmi_memory_device_type_detail(unsigned slot_num, uint16_t code) {
318 /* 7.18.3 */
319 static const char *detail[] = {
320 [1] = "Other",
321 [2] = "Unknown",
322 [3] = "Fast-paged",
323 [4] = "Static Column",
324 [5] = "Pseudo-static",
325 [6] = "RAMBUS",
326 [7] = "Synchronous",
327 [8] = "CMOS",
328 [9] = "EDO",
329 [10] = "Window DRAM",
330 [11] = "Cache DRAM",
331 [12] = "Non-Volatile",
332 [13] = "Registered (Buffered)",
333 [14] = "Unbuffered (Unregistered)",
334 [15] = "LRDIMM",
335 };
336
337 if ((code & 0xFFFE) == 0)
338 printf("MEMORY_DEVICE_%u_TYPE_DETAIL=%s\n", slot_num, "None");
339 else {
340 bool first_element = true;
341
342 printf("MEMORY_DEVICE_%u_TYPE_DETAIL=", slot_num);
343 for (size_t i = 1; i < ELEMENTSOF(detail); i++)
344 if (code & (1 << i)) {
345 printf("%s%s", first_element ? "" : " ", detail[i]);
346 first_element = false;
347 }
348 printf("\n");
349 }
350 }
351
352 static void dmi_memory_device_speed(
353 const char *attr_suffix,
354 unsigned slot_num, uint16_t code) {
355 if (code != 0)
356 printf("MEMORY_DEVICE_%u_%s=%u\n", slot_num, attr_suffix, code);
357 }
358
359 static void dmi_memory_device_technology(unsigned slot_num, uint8_t code) {
360 /* 7.18.6 */
361 static const char * const technology[] = {
362 [0x01] = "Other",
363 [0x02] = "Unknown",
364 [0x03] = "DRAM",
365 [0x04] = "NVDIMM-N",
366 [0x05] = "NVDIMM-F",
367 [0x06] = "NVDIMM-P",
368 [0x07] = "Intel Optane persistent memory",
369 };
370
371 printf("MEMORY_DEVICE_%u_MEMORY_TECHNOLOGY=%s\n", slot_num,
372 code < ELEMENTSOF(technology) && technology[code] ? technology[code] : OUT_OF_SPEC_STR);
373 }
374
375 static void dmi_memory_device_operating_mode_capability(unsigned slot_num, uint16_t code) {
376 /* 7.18.7 */
377 static const char * const mode[] = {
378 [1] = "Other",
379 [2] = "Unknown",
380 [3] = "Volatile memory",
381 [4] = "Byte-accessible persistent memory",
382 [5] = "Block-accessible persistent memory",
383 };
384
385 if ((code & 0xFFFE) != 0) {
386 bool first_element = true;
387
388 printf("MEMORY_DEVICE_%u_MEMORY_OPERATING_MODE_CAPABILITY=", slot_num);
389 for (size_t i = 1; i < ELEMENTSOF(mode); i++)
390 if (code & (1 << i)) {
391 printf("%s%s", first_element ? "" : " ", mode[i]);
392 first_element = false;
393 }
394 printf("\n");
395 }
396 }
397
398 static void dmi_memory_device_manufacturer_id(
399 const char *attr_suffix,
400 unsigned slot_num, uint16_t code) {
401 /* 7.18.8 */
402 /* 7.18.10 */
403 /* LSB is 7-bit Odd Parity number of continuation codes */
404 if (code != 0)
405 printf("MEMORY_DEVICE_%u_%s=Bank %d, Hex 0x%02X\n", slot_num, attr_suffix,
406 (code & 0x7F) + 1, (uint16_t) (code >> 8));
407 }
408
409 static void dmi_memory_device_product_id(
410 const char *attr_suffix,
411 unsigned slot_num, uint16_t code) {
412 /* 7.18.9 */
413 /* 7.18.11 */
414 if (code != 0)
415 printf("MEMORY_DEVICE_%u_%s=0x%04X\n", slot_num, attr_suffix, code);
416 }
417
418 static void dmi_memory_device_size_detail(
419 const char *attr_suffix,
420 unsigned slot_num, uint64_t code) {
421 /* 7.18.12 */
422 /* 7.18.13 */
423 if (!IN_SET(code, 0x0LU, 0xFFFFFFFFFFFFFFFFLU))
424 dmi_print_memory_size("MEMORY_DEVICE", attr_suffix, slot_num, code, MEMORY_SIZE_UNIT_BYTES);
425 }
426
427 static void dmi_decode(const struct dmi_header *h,
428 unsigned *next_slot_num) {
429 const uint8_t *data = h->data;
430 unsigned slot_num;
431
432 /*
433 * Note: DMI types 37 and 42 are untested
434 */
435 switch (h->type) {
436 case 16: /* 7.17 Physical Memory Array */
437 log_debug("Physical Memory Array");
438 if (h->length < 0x0F)
439 break;
440
441 if (data[0x05] != 0x03) /* 7.17.2, Use == "System Memory" */
442 break;
443
444 log_debug("Use: System Memory");
445 dmi_memory_array_location(data[0x04]);
446 dmi_memory_array_ec_type(data[0x06]);
447 if (DWORD(data + 0x07) != 0x80000000)
448 dmi_print_memory_size("MEMORY_ARRAY", "MAX_CAPACITY", -1, DWORD(data + 0x07), MEMORY_SIZE_UNIT_KB);
449 else if (h->length >= 0x17)
450 dmi_print_memory_size("MEMORY_ARRAY", "MAX_CAPACITY", -1, QWORD(data + 0x0F), MEMORY_SIZE_UNIT_BYTES);
451
452 break;
453
454 case 17: /* 7.18 Memory Device */
455 slot_num = *next_slot_num;
456 *next_slot_num = slot_num + 1;
457
458 log_debug("Memory Device: %u", slot_num);
459 if (h->length < 0x15)
460 break;
461
462 dmi_memory_device_width("TOTAL_WIDTH", slot_num, WORD(data + 0x08));
463 dmi_memory_device_width("DATA_WIDTH", slot_num, WORD(data + 0x0A));
464 if (h->length >= 0x20 && WORD(data + 0x0C) == 0x7FFF)
465 dmi_memory_device_extended_size(slot_num, DWORD(data + 0x1C));
466 else
467 dmi_memory_device_size(slot_num, WORD(data + 0x0C));
468 dmi_memory_device_form_factor(slot_num, data[0x0E]);
469 dmi_memory_device_set(slot_num, data[0x0F]);
470 dmi_memory_device_string("LOCATOR", slot_num, h, data[0x10]);
471 dmi_memory_device_string("BANK_LOCATOR", slot_num, h, data[0x11]);
472 dmi_memory_device_type(slot_num, data[0x12]);
473 dmi_memory_device_type_detail(slot_num, WORD(data + 0x13));
474 if (h->length < 0x17)
475 break;
476
477 dmi_memory_device_speed("SPEED_MTS", slot_num, WORD(data + 0x15));
478 if (h->length < 0x1B)
479 break;
480
481 dmi_memory_device_string("MANUFACTURER", slot_num, h, data[0x17]);
482 dmi_memory_device_string("SERIAL_NUMBER", slot_num, h, data[0x18]);
483 dmi_memory_device_string("ASSET_TAG", slot_num, h, data[0x19]);
484 dmi_memory_device_string("PART_NUMBER", slot_num, h, data[0x1A]);
485 if (h->length < 0x1C)
486 break;
487
488 dmi_memory_device_rank(slot_num, data[0x1B]);
489 if (h->length < 0x22)
490 break;
491
492 dmi_memory_device_speed("CONFIGURED_SPEED_MTS", slot_num, WORD(data + 0x20));
493 if (h->length < 0x28)
494 break;
495
496 dmi_memory_device_voltage_value("MINIMUM_VOLTAGE", slot_num, WORD(data + 0x22));
497 dmi_memory_device_voltage_value("MAXIMUM_VOLTAGE", slot_num, WORD(data + 0x24));
498 dmi_memory_device_voltage_value("CONFIGURED_VOLTAGE", slot_num, WORD(data + 0x26));
499 if (h->length < 0x34)
500 break;
501
502 dmi_memory_device_technology(slot_num, data[0x28]);
503 dmi_memory_device_operating_mode_capability(slot_num, WORD(data + 0x29));
504 dmi_memory_device_string("FIRMWARE_VERSION", slot_num, h, data[0x2B]);
505 dmi_memory_device_manufacturer_id("MODULE_MANUFACTURER_ID", slot_num, WORD(data + 0x2C));
506 dmi_memory_device_product_id("MODULE_PRODUCT_ID", slot_num, WORD(data + 0x2E));
507 dmi_memory_device_manufacturer_id("MEMORY_SUBSYSTEM_CONTROLLER_MANUFACTURER_ID",
508 slot_num, WORD(data + 0x30));
509 dmi_memory_device_product_id("MEMORY_SUBSYSTEM_CONTROLLER_PRODUCT_ID",
510 slot_num, WORD(data + 0x32));
511 if (h->length < 0x3C)
512 break;
513
514 dmi_memory_device_size_detail("NON_VOLATILE_SIZE", slot_num, QWORD(data + 0x34));
515 if (h->length < 0x44)
516 break;
517
518 dmi_memory_device_size_detail("VOLATILE_SIZE", slot_num, QWORD(data + 0x3C));
519 if (h->length < 0x4C)
520 break;
521
522 dmi_memory_device_size_detail("CACHE_SIZE", slot_num, QWORD(data + 0x44));
523 if (h->length < 0x54)
524 break;
525
526 dmi_memory_device_size_detail("LOGICAL_SIZE", slot_num, QWORD(data + 0x4C));
527
528 break;
529 }
530 }
531
532 static void dmi_table_decode(const uint8_t *buf, size_t len, uint16_t num) {
533 const uint8_t *data = buf;
534 unsigned next_slot_num = 0;
535
536 /* 4 is the length of an SMBIOS structure header */
537 for (uint16_t i = 0; (i < num || num == 0) && data + 4 <= buf + len; i++) {
538 struct dmi_header h = (struct dmi_header) {
539 .type = data[0],
540 .length = data[1],
541 .handle = WORD(data + 2),
542 .data = data,
543 };
544 bool display = !IN_SET(h.type, 126, 127);
545 const uint8_t *next;
546
547 /* If a short entry is found (less than 4 bytes), not only it
548 * is invalid, but we cannot reliably locate the next entry.
549 * Better stop at this point, and let the user know their
550 * table is broken. */
551 if (h.length < 4)
552 break;
553
554 /* In quiet mode, stop decoding at end of table marker */
555 if (h.type == 127)
556 break;
557
558 /* Look for the next handle */
559 next = data + h.length;
560 while ((size_t)(next - buf + 1) < len && (next[0] != 0 || next[1] != 0))
561 next++;
562 next += 2;
563
564 /* Make sure the whole structure fits in the table */
565 if ((size_t)(next - buf) > len)
566 break;
567
568 if (display)
569 dmi_decode(&h, &next_slot_num);
570
571 data = next;
572 }
573 if (next_slot_num > 0)
574 printf("MEMORY_ARRAY_NUM_DEVICES=%u\n", next_slot_num);
575 }
576
577 static int dmi_table(int64_t base, uint32_t len, uint16_t num, const char *devmem, bool no_file_offset) {
578 _cleanup_free_ uint8_t *buf = NULL;
579 size_t size;
580 int r;
581
582 /*
583 * When reading from sysfs or from a dump file, the file may be
584 * shorter than announced. For SMBIOS v3 this is expected, as we
585 * only know the maximum table size, not the actual table size.
586 * For older implementations (and for SMBIOS v3 too), this
587 * would be the result of the kernel truncating the table on
588 * parse error.
589 */
590 r = read_full_file_full(AT_FDCWD, devmem, no_file_offset ? 0 : base, len,
591 0, NULL, (char **) &buf, &size);
592 if (r < 0)
593 return log_error_errno(r, "Failed to read table: %m");
594
595 dmi_table_decode(buf, size, num);
596
597 return 0;
598 }
599
600 /* Same thing for SMBIOS3 entry points */
601 static int smbios3_decode(const uint8_t *buf, const char *devmem, bool no_file_offset) {
602 uint64_t offset;
603
604 /* Don't let checksum run beyond the buffer */
605 if (buf[0x06] > 0x20)
606 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
607 "Entry point length too large (%"PRIu8" bytes, expected %u).",
608 buf[0x06], 0x18U);
609
610 if (!verify_checksum(buf, buf[0x06]))
611 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to verify checksum.");
612
613 offset = QWORD(buf + 0x10);
614
615 #if __SIZEOF_SIZE_T__ != 8
616 if (!no_file_offset && (offset >> 32) != 0)
617 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "64-bit addresses not supported on 32-bit systems.");
618 #endif
619
620 return dmi_table(offset, DWORD(buf + 0x0C), 0, devmem, no_file_offset);
621 }
622
623 static int smbios_decode(const uint8_t *buf, const char *devmem, bool no_file_offset) {
624 /* Don't let checksum run beyond the buffer */
625 if (buf[0x05] > 0x20)
626 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
627 "Entry point length too large (%"PRIu8" bytes, expected %u).",
628 buf[0x05], 0x1FU);
629
630 if (!verify_checksum(buf, buf[0x05])
631 || memcmp(buf + 0x10, "_DMI_", 5) != 0
632 || !verify_checksum(buf + 0x10, 0x0F))
633 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to verify checksum.");
634
635 return dmi_table(DWORD(buf + 0x18), WORD(buf + 0x16), WORD(buf + 0x1C),
636 devmem, no_file_offset);
637 }
638
639 static int legacy_decode(const uint8_t *buf, const char *devmem, bool no_file_offset) {
640 if (!verify_checksum(buf, 0x0F))
641 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to verify checksum.");
642
643 return dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06), WORD(buf + 0x0C),
644 devmem, no_file_offset);
645 }
646
647 static int help(void) {
648 _cleanup_(table_unrefp) Table *options = NULL;
649 int r;
650
651 r = option_parser_get_help_table(&options);
652 if (r < 0)
653 return r;
654
655 help_cmdline("[OPTIONS...]");
656 help_section("Options");
657
658 return table_print_or_warn(options);
659 }
660
661 static int parse_argv(int argc, char *argv[]) {
662 assert(argc >= 0);
663 assert(argv);
664
665 OptionParser opts = { argc, argv };
666
667 FOREACH_OPTION_OR_RETURN(c, &opts)
668 switch (c) {
669
670 OPTION_COMMON_HELP:
671 return help();
672
673 OPTION_COMMON_VERSION_WITH_HIDDEN_V:
674 return version();
675
676 OPTION('F', "from-dump", "FILE",
677 "Read DMI information from a binary file"):
678 arg_source_file = opts.arg;
679 break;
680 }
681
682 if (option_parser_get_n_args(&opts) > 0)
683 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
684 "This program takes no arguments.");
685
686 return 1;
687 }
688
689 static int run(int argc, char *argv[]) {
690 _cleanup_free_ uint8_t *buf = NULL;
691 bool no_file_offset = false;
692 size_t size;
693 int r;
694
695 (void) udev_parse_config();
696 log_setup();
697
698 r = parse_argv(argc, argv);
699 if (r <= 0)
700 return r;
701
702 /* Read from dump if so instructed */
703 r = read_full_file_full(AT_FDCWD,
704 arg_source_file ?: SYS_ENTRY_FILE,
705 0, 0x20, 0, NULL, (char **) &buf, &size);
706 if (r < 0)
707 return log_full_errno(!arg_source_file && r == -ENOENT ? LOG_DEBUG : LOG_ERR,
708 r, "Reading \"%s\" failed: %m",
709 arg_source_file ?: SYS_ENTRY_FILE);
710
711 if (!arg_source_file) {
712 arg_source_file = SYS_TABLE_FILE;
713 no_file_offset = true;
714 }
715
716 if (size >= 24 && memory_startswith(buf, size, "_SM3_"))
717 return smbios3_decode(buf, arg_source_file, no_file_offset);
718 if (size >= 31 && memory_startswith(buf, size, "_SM_"))
719 return smbios_decode(buf, arg_source_file, no_file_offset);
720 if (size >= 15 && memory_startswith(buf, size, "_DMI_"))
721 return legacy_decode(buf, arg_source_file, no_file_offset);
722
723 return -EINVAL;
724 }
725
726 DEFINE_MAIN_FUNCTION(run);