diff --git a/libfwupdplugin/fu-hwids.c b/libfwupdplugin/fu-hwids.c index 11fb1c088..f56ad675e 100644 --- a/libfwupdplugin/fu-hwids.c +++ b/libfwupdplugin/fu-hwids.c @@ -280,21 +280,10 @@ fu_hwids_convert_padded_integer_cb (FuSmbios *smbios, guint8 type, guint8 offset, GError **error) { - g_autoptr(GBytes) data = NULL; - const guint8 *data_raw; - gsize data_sz = 0; - data = fu_smbios_get_data (smbios, type, error); - if (data == NULL) + guint tmp = fu_smbios_get_integer (smbios, type, offset, error); + if (tmp == G_MAXUINT) return NULL; - data_raw = g_bytes_get_data (data, &data_sz); - if (offset >= data_sz) { - g_set_error_literal (error, - FWUPD_ERROR, - FWUPD_ERROR_INVALID_FILE, - "offset bigger than data"); - return NULL; - } - return g_strdup_printf ("%02x", data_raw[offset]); + return g_strdup_printf ("%02x", tmp); } static gchar * @@ -302,21 +291,10 @@ fu_hwids_convert_integer_cb (FuSmbios *smbios, guint8 type, guint8 offset, GError **error) { - g_autoptr(GBytes) data = NULL; - const guint8 *data_raw; - gsize data_sz = 0; - data = fu_smbios_get_data (smbios, type, error); - if (data == NULL) + guint tmp = fu_smbios_get_integer (smbios, type, offset, error); + if (tmp == G_MAXUINT) return NULL; - data_raw = g_bytes_get_data (data, &data_sz); - if (offset >= data_sz) { - g_set_error_literal (error, - FWUPD_ERROR, - FWUPD_ERROR_INVALID_FILE, - "offset bigger than data"); - return NULL; - } - return g_strdup_printf ("%x", data_raw[offset]); + return g_strdup_printf ("%x", tmp); } /** diff --git a/libfwupdplugin/fu-smbios.c b/libfwupdplugin/fu-smbios.c index 90ec1e4f4..1b5d09df9 100644 --- a/libfwupdplugin/fu-smbios.c +++ b/libfwupdplugin/fu-smbios.c @@ -502,6 +502,53 @@ fu_smbios_get_data (FuSmbios *self, guint8 type, GError **error) return g_bytes_new (item->buf->data, item->buf->len); } +/** + * fu_smbios_get_integer: + * @self: A #FuSmbios + * @type: A structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS + * @offset: A structure offset + * @error: A #GError or %NULL + * + * Reads an integer value from the SMBIOS string table of a specific structure. + * + * The @type and @offset can be referenced from the DMTF SMBIOS specification: + * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf + * + * Returns: an integer, or %G_MAXUINT if invalid or not found + * + * Since: 1.5.0 + **/ +guint +fu_smbios_get_integer (FuSmbios *self, guint8 type, guint8 offset, GError **error) +{ + FuSmbiosItem *item; + + g_return_val_if_fail (FU_IS_SMBIOS (self), 0); + + /* get item */ + item = fu_smbios_get_item_for_type (self, type); + if (item == NULL) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INVALID_FILE, + "no structure with type %02x", type); + return G_MAXUINT; + } + + /* check offset valid */ + if (offset >= item->buf->len) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INVALID_FILE, + "offset bigger than size %u", + item->buf->len); + return G_MAXUINT; + } + + /* success */ + return item->buf->data[offset]; +} + /** * fu_smbios_get_string: * @self: A #FuSmbios diff --git a/libfwupdplugin/fu-smbios.h b/libfwupdplugin/fu-smbios.h index a8084c351..4f3d13c31 100644 --- a/libfwupdplugin/fu-smbios.h +++ b/libfwupdplugin/fu-smbios.h @@ -26,6 +26,10 @@ const gchar *fu_smbios_get_string (FuSmbios *self, guint8 type, guint8 offset, GError **error); +guint fu_smbios_get_integer (FuSmbios *self, + guint8 type, + guint8 offset, + GError **error); GBytes *fu_smbios_get_data (FuSmbios *self, guint8 type, GError **error); diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 3242e4cfa..aca29f593 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -654,6 +654,7 @@ LIBFWUPDPLUGIN_1.5.0 { fu_security_attrs_new; fu_security_attrs_remove_all; fu_security_attrs_to_variant; + fu_smbios_get_integer; fu_udev_device_get_number; fu_udev_device_get_subsystem_model; fu_udev_device_get_subsystem_vendor;