/src/fwupd/libfwupdplugin/fu-hwids-fdt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023 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-fdt-firmware.h" |
13 | | #include "fu-hwids-private.h" |
14 | | |
15 | | gboolean |
16 | | fu_hwids_fdt_setup(FuContext *ctx, FuHwids *self, GError **error) |
17 | 0 | { |
18 | 0 | g_autofree gchar *chassis_type = NULL; |
19 | 0 | g_auto(GStrv) compatible = NULL; |
20 | 0 | g_autoptr(FuFirmware) fdt_img = NULL; |
21 | 0 | g_autoptr(FuFdtImage) fdt_img_baseb = NULL; |
22 | 0 | g_autoptr(FuFdtImage) fdt_img_fwver = NULL; |
23 | 0 | g_autoptr(FuFirmware) fdt = NULL; |
24 | 0 | struct { |
25 | 0 | const gchar *hwid; |
26 | 0 | const gchar *key; |
27 | 0 | } map[] = { |
28 | 0 | {FU_HWIDS_KEY_MANUFACTURER, "vendor"}, |
29 | 0 | {FU_HWIDS_KEY_FAMILY, "model-name"}, |
30 | 0 | {FU_HWIDS_KEY_PRODUCT_NAME, "model"}, |
31 | 0 | }; |
32 | |
|
33 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(ctx), FALSE); |
34 | 0 | g_return_val_if_fail(FU_IS_HWIDS(self), FALSE); |
35 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
36 | | |
37 | | /* adds compatible GUIDs */ |
38 | 0 | fdt = fu_context_get_fdt(ctx, error); |
39 | 0 | if (fdt == NULL) |
40 | 0 | return FALSE; |
41 | 0 | fdt_img = fu_firmware_get_image_by_id(fdt, NULL, error); |
42 | 0 | if (fdt_img == NULL) |
43 | 0 | return FALSE; |
44 | 0 | if (!fu_fdt_image_get_attr_strlist(FU_FDT_IMAGE(fdt_img), "compatible", &compatible, error)) |
45 | 0 | return FALSE; |
46 | 0 | for (guint i = 0; compatible[i] != NULL; i++) { |
47 | 0 | g_autofree gchar *guid = fwupd_guid_hash_string(compatible[i]); |
48 | 0 | g_debug("using %s for DT compatible %s", guid, compatible[i]); |
49 | 0 | fu_hwids_add_guid(self, guid); |
50 | 0 | } |
51 | | |
52 | | /* root node */ |
53 | 0 | for (guint i = 0; i < G_N_ELEMENTS(map); i++) { |
54 | 0 | g_autofree gchar *tmp = NULL; |
55 | 0 | fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), map[i].key, &tmp, NULL); |
56 | 0 | if (tmp == NULL) |
57 | 0 | continue; |
58 | 0 | fu_hwids_add_value(self, map[i].hwid, tmp); |
59 | 0 | } |
60 | | |
61 | | /* chassis kind */ |
62 | 0 | fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), "chassis-type", &chassis_type, NULL); |
63 | 0 | if (chassis_type != NULL) { |
64 | 0 | struct { |
65 | 0 | FuSmbiosChassisKind chassis_kind; |
66 | 0 | const gchar *dt; |
67 | 0 | } chassis_map[] = { |
68 | 0 | {FU_SMBIOS_CHASSIS_KIND_CONVERTIBLE, "convertible"}, |
69 | 0 | {FU_SMBIOS_CHASSIS_KIND_EMBEDDED_PC, "embedded"}, |
70 | 0 | {FU_SMBIOS_CHASSIS_KIND_HAND_HELD, "handset"}, |
71 | 0 | {FU_SMBIOS_CHASSIS_KIND_LAPTOP, "laptop"}, |
72 | 0 | {FU_SMBIOS_CHASSIS_KIND_TABLET, "tablet"}, |
73 | 0 | }; |
74 | 0 | for (guint i = 0; i < G_N_ELEMENTS(chassis_map); i++) { |
75 | 0 | if (g_strcmp0(chassis_type, chassis_map[i].dt) == 0) { |
76 | 0 | fu_context_set_chassis_kind(ctx, chassis_map[i].chassis_kind); |
77 | 0 | break; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | /* fallback */ |
83 | 0 | if (g_strv_length(compatible) > 0) { |
84 | 0 | g_auto(GStrv) compatible0 = g_strsplit(compatible[0], ",", -1); |
85 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_MANUFACTURER, compatible0[0]); |
86 | 0 | if (g_strv_length(compatible0) > 1) |
87 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_PRODUCT_NAME, compatible0[1]); |
88 | 0 | } |
89 | 0 | if (g_strv_length(compatible) > 1) |
90 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_FAMILY, compatible[1]); |
91 | 0 | if (fu_context_get_chassis_kind(ctx) == FU_SMBIOS_CHASSIS_KIND_UNKNOWN) { |
92 | 0 | if (fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), "battery", NULL, NULL)) |
93 | 0 | fu_context_set_chassis_kind(ctx, FU_SMBIOS_CHASSIS_KIND_PORTABLE); |
94 | 0 | } |
95 | 0 | fdt_img_fwver = |
96 | 0 | fu_fdt_firmware_get_image_by_path(FU_FDT_FIRMWARE(fdt), "/ibm,firmware-versions", NULL); |
97 | 0 | if (fdt_img_fwver != NULL) { |
98 | 0 | g_autofree gchar *version = NULL; |
99 | 0 | fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img_fwver), "version", &version, NULL); |
100 | 0 | if (version != NULL) |
101 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_BIOS_VERSION, version); |
102 | 0 | } |
103 | | |
104 | | /* fall back to the firmware unix time */ |
105 | 0 | if (fdt_img_fwver == NULL) { |
106 | 0 | fdt_img_fwver = fu_fdt_firmware_get_image_by_path(FU_FDT_FIRMWARE(fdt), |
107 | 0 | "/chosen/bootloader", |
108 | 0 | NULL); |
109 | 0 | } |
110 | 0 | if (fdt_img_fwver != NULL) { |
111 | 0 | guint32 timestamp = 0; |
112 | 0 | fu_fdt_image_get_attr_u32(FU_FDT_IMAGE(fdt_img_fwver), |
113 | 0 | "build-timestamp", |
114 | 0 | ×tamp, |
115 | 0 | NULL); |
116 | 0 | if (timestamp != 0) { |
117 | 0 | g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc(timestamp); |
118 | 0 | g_autofree gchar *version = g_date_time_format(dt, "%Y%m%d"); |
119 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_BIOS_VERSION, version); |
120 | 0 | } |
121 | 0 | } |
122 | |
|
123 | 0 | fdt_img_baseb = fu_fdt_firmware_get_image_by_path( |
124 | 0 | FU_FDT_FIRMWARE(fdt), |
125 | 0 | "/vpd/root-node-vpd@a000/enclosure@1e00/backplane@800", |
126 | 0 | NULL); |
127 | 0 | if (fdt_img_baseb != NULL) { |
128 | 0 | g_autofree gchar *vendor = NULL; |
129 | 0 | g_autofree gchar *product = NULL; |
130 | 0 | fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img_baseb), "vendor", &vendor, NULL); |
131 | 0 | fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img_baseb), |
132 | 0 | "part-number", |
133 | 0 | &product, |
134 | 0 | NULL); |
135 | 0 | if (vendor != NULL) |
136 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, vendor); |
137 | 0 | if (product != NULL) |
138 | 0 | fu_hwids_add_value(self, FU_HWIDS_KEY_BASEBOARD_PRODUCT, product); |
139 | 0 | } |
140 | | |
141 | | /* success */ |
142 | 0 | return TRUE; |
143 | 0 | } |