trivial: Add fu_smbios_get_integer() helper

This commit is contained in:
Richard Hughes 2020-10-17 15:51:24 +01:00
parent f2a3e01f96
commit 9f93b8bc66
4 changed files with 58 additions and 28 deletions

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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);

View File

@ -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;