Coverage Report

Created: 2025-08-24 07:10

/src/fwupd/libfwupdplugin/fu-hwids-dmi.c
Line
Count
Source (jump to first uncovered line)
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-path.h"
14
#include "fu-smbios-struct.h"
15
#include "fu-string.h"
16
17
gboolean
18
fu_hwids_dmi_setup(FuContext *ctx, FuHwids *self, GError **error)
19
0
{
20
0
  g_autofree gchar *path = fu_path_from_kind(FU_PATH_KIND_SYSFSDIR_DMI);
21
0
  struct {
22
0
    const gchar *hwid;
23
0
    const gchar *key;
24
0
  } map[] = {{FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, "board_vendor"},
25
0
       {FU_HWIDS_KEY_BASEBOARD_PRODUCT, "board_name"},
26
0
       {FU_HWIDS_KEY_BIOS_VENDOR, "bios_vendor"},
27
0
       {FU_HWIDS_KEY_BIOS_VERSION, "bios_version"},
28
0
       {FU_HWIDS_KEY_FAMILY, "product_family"},
29
0
       {FU_HWIDS_KEY_MANUFACTURER, "sys_vendor"},
30
0
       {FU_HWIDS_KEY_PRODUCT_NAME, "product_name"},
31
0
       {FU_HWIDS_KEY_PRODUCT_SKU, "product_sku"},
32
0
       {FU_HWIDS_KEY_ENCLOSURE_KIND, "chassis_type"},
33
0
       {NULL, NULL}};
34
35
  /* the values the kernel parsed; these are world-readable */
36
0
  if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
37
0
    g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "no %s", path);
38
0
    return FALSE;
39
0
  }
40
0
  for (guint i = 0; map[i].key != NULL; i++) {
41
0
    gsize bufsz = 0;
42
0
    g_autofree gchar *buf = NULL;
43
0
    g_autofree gchar *fn = g_build_filename(path, map[i].key, NULL);
44
0
    g_autoptr(GError) error_local = NULL;
45
46
0
    if (!g_file_get_contents(fn, &buf, &bufsz, &error_local)) {
47
0
      g_debug("unable to read SMBIOS data from %s: %s", fn, error_local->message);
48
0
      continue;
49
0
    }
50
0
    if (bufsz == 0)
51
0
      continue;
52
53
    /* trim trailing newline added by kernel */
54
0
    if (buf[bufsz - 1] == '\n')
55
0
      buf[bufsz - 1] = 0;
56
0
    fu_hwids_add_value(self, map[i].hwid, buf);
57
58
0
    if (g_strcmp0(map[i].hwid, FU_HWIDS_KEY_ENCLOSURE_KIND) == 0) {
59
0
      guint64 val = 0;
60
0
      if (!fu_strtoull(buf,
61
0
           &val,
62
0
           FU_SMBIOS_CHASSIS_KIND_OTHER,
63
0
           FU_SMBIOS_CHASSIS_KIND_LAST,
64
0
           FU_INTEGER_BASE_AUTO,
65
0
           &error_local)) {
66
0
        g_warning("ignoring enclosure kind %s", buf);
67
0
        continue;
68
0
      }
69
0
      fu_context_set_chassis_kind(ctx, val);
70
0
    }
71
0
  }
72
73
  /* success */
74
0
  return TRUE;
75
0
}