fwupd/libfwupdplugin/fu-hwids-fdt.c
Richard Hughes 2b0f92506b Refactor the hwids functionality
This refactors the code as it was getting very confusing; before FuSmbios was
reading both SMBIOS and the kernel-provided DT -- and various things were
injecting overrides in three different place. To properly support FDT remove
one layer of indirection.

This also lets us use the compatible strings to enable plugins specifying the
flag _REQUIRE_HWID -- which means we only load the plugin if it's got a chance
of working. e.g.

    [aspeed,ast2500]
2023-01-18 07:04:44 +00:00

74 lines
2.3 KiB
C

/*
* Copyright (C) 2023 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#define G_LOG_DOMAIN "FuContext"
#include "config.h"
#include "fu-context-private.h"
#include "fu-fdt-firmware.h"
#include "fu-hwids-private.h"
gboolean
fu_hwids_fdt_setup(FuContext *ctx, FuHwids *self, GError **error)
{
g_auto(GStrv) compatible = NULL;
g_autoptr(FuFirmware) fdt_img = NULL;
g_autoptr(FuFdtImage) fdt_img_fwver = NULL;
g_autoptr(FuFirmware) fdt = NULL;
struct {
const gchar *hwid;
const gchar *key;
} map[] = {{FU_HWIDS_KEY_MANUFACTURER, "vendor"},
{FU_HWIDS_KEY_FAMILY, "model-name"},
{FU_HWIDS_KEY_PRODUCT_NAME, "model"},
{NULL, NULL}};
/* adds compatible GUIDs */
fdt = fu_context_get_fdt(ctx, error);
if (fdt == NULL)
return FALSE;
fdt_img = fu_firmware_get_image_by_id(fdt, NULL, error);
if (fdt_img == NULL)
return FALSE;
if (!fu_fdt_image_get_attr_strlist(FU_FDT_IMAGE(fdt_img), "compatible", &compatible, error))
return FALSE;
for (guint i = 0; compatible[i] != NULL; i++) {
g_autofree gchar *guid = fwupd_guid_hash_string(compatible[i]);
g_debug("using %s for DT compatible %s", guid, compatible[i]);
fu_hwids_add_guid(self, guid);
}
/* root node */
for (guint i = 0; map[i].key != NULL; i++) {
g_autofree gchar *tmp = NULL;
fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), map[i].key, &tmp, NULL);
fu_hwids_add_value(self, map[i].hwid, tmp);
}
/* fallback */
if (g_strv_length(compatible) > 0)
fu_hwids_add_value(self, FU_HWIDS_KEY_MANUFACTURER, compatible[0]);
if (g_strv_length(compatible) > 1)
fu_hwids_add_value(self, FU_HWIDS_KEY_PRODUCT_NAME, compatible[1]);
if (g_strv_length(compatible) > 2)
fu_hwids_add_value(self, FU_HWIDS_KEY_FAMILY, compatible[2]);
if (fu_context_get_chassis_kind(ctx) == FU_SMBIOS_CHASSIS_KIND_UNKNOWN) {
if (fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), "battery", NULL, NULL))
fu_context_set_chassis_kind(ctx, FU_SMBIOS_CHASSIS_KIND_PORTABLE);
}
fdt_img_fwver =
fu_fdt_firmware_get_image_by_path(FU_FDT_FIRMWARE(fdt), "ibm,firmware-versions", NULL);
if (fdt_img_fwver != NULL) {
g_autofree gchar *version = NULL;
fu_fdt_image_get_attr_str(FU_FDT_IMAGE(fdt_img), "version", &version, NULL);
fu_hwids_add_value(self, FU_HWIDS_KEY_BIOS_VERSION, version);
}
/* success */
return TRUE;
}