/src/fwupd/libfwupdplugin/fu-hwids-smbios.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuContext" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include "fu-context-private.h" |
12 | | #include "fu-hwids-private.h" |
13 | | #include "fu-smbios-private.h" |
14 | | #include "fu-string.h" |
15 | | |
16 | | typedef gchar *(*FuContextHwidConvertFunc)(FuSmbios *smbios, |
17 | | guint8 type, |
18 | | guint8 offset, |
19 | | GError **error); |
20 | | |
21 | | static gchar * |
22 | | fu_hwids_smbios_convert_string_table_cb(FuSmbios *smbios, |
23 | | guint8 type, |
24 | | guint8 offset, |
25 | | GError **error) |
26 | 0 | { |
27 | 0 | const gchar *tmp = |
28 | 0 | fu_smbios_get_string(smbios, type, FU_SMBIOS_STRUCTURE_LENGTH_ANY, offset, error); |
29 | 0 | if (tmp == NULL) |
30 | 0 | return NULL; |
31 | | /* ComputerHardwareIds.exe seems to strip spaces */ |
32 | 0 | return fu_strstrip(tmp); |
33 | 0 | } |
34 | | |
35 | | static gchar * |
36 | | fu_hwids_smbios_convert_padded_integer_cb(FuSmbios *smbios, |
37 | | guint8 type, |
38 | | guint8 offset, |
39 | | GError **error) |
40 | 0 | { |
41 | 0 | guint tmp = |
42 | 0 | fu_smbios_get_integer(smbios, type, FU_SMBIOS_STRUCTURE_LENGTH_ANY, offset, error); |
43 | 0 | if (tmp == G_MAXUINT) |
44 | 0 | return NULL; |
45 | 0 | return g_strdup_printf("%02x", tmp); |
46 | 0 | } |
47 | | |
48 | | static gchar * |
49 | | fu_hwids_smbios_convert_integer_cb(FuSmbios *smbios, guint8 type, guint8 offset, GError **error) |
50 | 0 | { |
51 | 0 | guint tmp = |
52 | 0 | fu_smbios_get_integer(smbios, type, FU_SMBIOS_STRUCTURE_LENGTH_ANY, offset, error); |
53 | 0 | if (tmp == G_MAXUINT) |
54 | 0 | return NULL; |
55 | 0 | return g_strdup_printf("%x", tmp); |
56 | 0 | } |
57 | | |
58 | | gboolean |
59 | | fu_hwids_smbios_setup(FuContext *ctx, FuHwids *self, GError **error) |
60 | 0 | { |
61 | 0 | FuSmbios *smbios = fu_context_get_smbios(ctx); |
62 | 0 | struct { |
63 | 0 | const gchar *key; |
64 | 0 | guint8 type; |
65 | 0 | guint8 offset; |
66 | 0 | FuContextHwidConvertFunc func; |
67 | 0 | } map[] = {{FU_HWIDS_KEY_MANUFACTURER, |
68 | 0 | FU_SMBIOS_STRUCTURE_TYPE_SYSTEM, |
69 | 0 | 0x04, |
70 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
71 | 0 | {FU_HWIDS_KEY_ENCLOSURE_KIND, |
72 | 0 | FU_SMBIOS_STRUCTURE_TYPE_CHASSIS, |
73 | 0 | 0x05, |
74 | 0 | fu_hwids_smbios_convert_integer_cb}, |
75 | 0 | {FU_HWIDS_KEY_FAMILY, |
76 | 0 | FU_SMBIOS_STRUCTURE_TYPE_SYSTEM, |
77 | 0 | 0x1a, |
78 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
79 | 0 | {FU_HWIDS_KEY_PRODUCT_NAME, |
80 | 0 | FU_SMBIOS_STRUCTURE_TYPE_SYSTEM, |
81 | 0 | 0x05, |
82 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
83 | 0 | {FU_HWIDS_KEY_PRODUCT_SKU, |
84 | 0 | FU_SMBIOS_STRUCTURE_TYPE_SYSTEM, |
85 | 0 | 0x19, |
86 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
87 | 0 | {FU_HWIDS_KEY_BIOS_VENDOR, |
88 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
89 | 0 | 0x04, |
90 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
91 | 0 | {FU_HWIDS_KEY_BIOS_VERSION, |
92 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
93 | 0 | 0x05, |
94 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
95 | 0 | {FU_HWIDS_KEY_BIOS_MAJOR_RELEASE, |
96 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
97 | 0 | 0x14, |
98 | 0 | fu_hwids_smbios_convert_padded_integer_cb}, |
99 | 0 | {FU_HWIDS_KEY_BIOS_MINOR_RELEASE, |
100 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
101 | 0 | 0x15, |
102 | 0 | fu_hwids_smbios_convert_padded_integer_cb}, |
103 | 0 | {FU_HWIDS_KEY_FIRMWARE_MAJOR_RELEASE, |
104 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
105 | 0 | 0x16, |
106 | 0 | fu_hwids_smbios_convert_padded_integer_cb}, |
107 | 0 | {FU_HWIDS_KEY_FIRMWARE_MINOR_RELEASE, |
108 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BIOS, |
109 | 0 | 0x17, |
110 | 0 | fu_hwids_smbios_convert_padded_integer_cb}, |
111 | 0 | {FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, |
112 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BASEBOARD, |
113 | 0 | 0x04, |
114 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
115 | 0 | {FU_HWIDS_KEY_BASEBOARD_PRODUCT, |
116 | 0 | FU_SMBIOS_STRUCTURE_TYPE_BASEBOARD, |
117 | 0 | 0x05, |
118 | 0 | fu_hwids_smbios_convert_string_table_cb}, |
119 | 0 | {NULL, 0x00, 0x00, NULL}}; |
120 | |
|
121 | 0 | if (!fu_smbios_setup(smbios, error)) |
122 | 0 | return FALSE; |
123 | | |
124 | | /* get all DMI data from SMBIOS */ |
125 | 0 | fu_context_set_chassis_kind(ctx, |
126 | 0 | fu_smbios_get_integer(smbios, |
127 | 0 | FU_SMBIOS_STRUCTURE_TYPE_CHASSIS, |
128 | 0 | FU_SMBIOS_STRUCTURE_LENGTH_ANY, |
129 | 0 | 0x05, |
130 | 0 | NULL)); |
131 | 0 | for (guint i = 0; map[i].key != NULL; i++) { |
132 | 0 | const gchar *contents_hdr = NULL; |
133 | 0 | g_autofree gchar *contents = NULL; |
134 | 0 | g_autoptr(GError) error_local = NULL; |
135 | |
|
136 | 0 | contents = map[i].func(smbios, map[i].type, map[i].offset, &error_local); |
137 | 0 | if (contents == NULL) { |
138 | 0 | g_debug("ignoring %s: %s", map[i].key, error_local->message); |
139 | 0 | continue; |
140 | 0 | } |
141 | 0 | g_info("SMBIOS %s=%s", map[i].key, contents); |
142 | | |
143 | | /* weirdly, remove leading zeros */ |
144 | 0 | contents_hdr = contents; |
145 | 0 | while (contents_hdr[0] == '0' && |
146 | 0 | map[i].func != fu_hwids_smbios_convert_padded_integer_cb) |
147 | 0 | contents_hdr++; |
148 | 0 | fu_hwids_add_value(self, map[i].key, contents_hdr); |
149 | 0 | } |
150 | | |
151 | | /* success */ |
152 | 0 | return TRUE; |
153 | 0 | } |