diff --git a/libfwupdplugin/fu-context.c b/libfwupdplugin/fu-context.c index eceea3d06..2575574ed 100644 --- a/libfwupdplugin/fu-context.c +++ b/libfwupdplugin/fu-context.c @@ -37,6 +37,7 @@ typedef struct { guint battery_level; guint battery_threshold; FuBiosSettings *host_bios_settings; + gboolean loaded_hwinfo; } FuContextPrivate; enum { SIGNAL_SECURITY_CHANGED, SIGNAL_LAST }; @@ -76,6 +77,10 @@ fu_context_get_smbios_string(FuContext *self, guint8 structure_type, guint8 offs { FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); + if (!priv->loaded_hwinfo) { + g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); + return NULL; + } return fu_smbios_get_string(priv->smbios, structure_type, offset, NULL); } @@ -100,6 +105,11 @@ fu_context_get_smbios_data(FuContext *self, guint8 structure_type, GError **erro g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); /* must be valid and non-zero length */ + if (!priv->loaded_hwinfo) { + g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); + g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED, "no data"); + return NULL; + } blob = fu_smbios_get_data(priv->smbios, structure_type, error); if (blob == NULL) return NULL; @@ -130,6 +140,10 @@ fu_context_get_smbios_integer(FuContext *self, guint8 type, guint8 offset) { FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), G_MAXUINT); + if (!priv->loaded_hwinfo) { + g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); + return G_MAXUINT; + } return fu_smbios_get_integer(priv->smbios, type, offset, NULL); } @@ -224,6 +238,10 @@ fu_context_has_hwid_guid(FuContext *self, const gchar *guid) { FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); + if (!priv->loaded_hwinfo) { + g_critical("cannot use HWIDs before calling ->load_hwinfo()"); + return FALSE; + } return fu_hwids_has_guid(priv->hwids, guid); } @@ -243,6 +261,10 @@ fu_context_get_hwid_guids(FuContext *self) { FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); + if (!priv->loaded_hwinfo) { + g_critical("cannot use HWIDs before calling ->load_hwinfo()"); + return NULL; + } return fu_hwids_get_guids(priv->hwids); } @@ -264,6 +286,10 @@ fu_context_get_hwid_value(FuContext *self, const gchar *key) FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); g_return_val_if_fail(key != NULL, NULL); + if (!priv->loaded_hwinfo) { + g_critical("cannot use HWIDs before calling ->load_hwinfo()"); + return NULL; + } return fu_hwids_get_value(priv->hwids, key); } @@ -286,6 +312,11 @@ fu_context_get_hwid_replace_value(FuContext *self, const gchar *keys, GError **e FuContextPrivate *priv = GET_PRIVATE(self); g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); g_return_val_if_fail(keys != NULL, NULL); + if (!priv->loaded_hwinfo) { + g_critical("cannot use HWIDs before calling ->load_hwinfo()"); + g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED, "no data"); + return NULL; + } return fu_hwids_get_replace_values(priv->hwids, keys, error); } @@ -614,6 +645,7 @@ fu_context_load_hwinfo(FuContext *self, GError **error) g_warning("Failed to load SMBIOS: %s", error_smbios->message); if (!fu_hwids_setup(priv->hwids, priv->smbios, &error_hwids)) g_warning("Failed to load HWIDs: %s", error_hwids->message); + priv->loaded_hwinfo = TRUE; /* set the hwid flags */ guids = fu_context_get_hwid_guids(self); diff --git a/plugins/synaptics-mst/fu-self-test.c b/plugins/synaptics-mst/fu-self-test.c index c41b23325..ffdaa7a5b 100644 --- a/plugins/synaptics-mst/fu-self-test.c +++ b/plugins/synaptics-mst/fu-self-test.c @@ -37,6 +37,9 @@ _test_add_fake_devices_from_dir(FuPlugin *plugin, const gchar *path) ret = fu_context_load_quirks(ctx, FU_QUIRKS_LOAD_FLAG_NO_CACHE, &error); g_assert_no_error(error); g_assert_true(ret); + ret = fu_context_load_hwinfo(ctx, &error); + g_assert_no_error(error); + g_assert_true(ret); while ((basename = g_dir_read_name(dir)) != NULL) { g_autofree gchar *fn = g_build_filename(path, basename, NULL); @@ -81,6 +84,9 @@ fu_plugin_synaptics_mst_none_func(void) ret = fu_context_load_quirks(ctx, FU_QUIRKS_LOAD_FLAG_NO_CACHE, &error); g_assert_no_error(error); g_assert_true(ret); + ret = fu_context_load_hwinfo(ctx, &error); + g_assert_no_error(error); + g_assert_true(ret); plugin = fu_plugin_new_from_gtype(fu_synaptics_mst_plugin_get_type(), ctx); g_signal_connect(FU_PLUGIN(plugin), @@ -121,6 +127,9 @@ fu_plugin_synaptics_mst_tb16_func(void) ret = fu_context_load_quirks(ctx, FU_QUIRKS_LOAD_FLAG_NO_CACHE, &error); g_assert_no_error(error); g_assert_true(ret); + ret = fu_context_load_hwinfo(ctx, &error); + g_assert_no_error(error); + g_assert_true(ret); plugin = fu_plugin_new_from_gtype(fu_synaptics_mst_plugin_get_type(), ctx); g_signal_connect(FU_PLUGIN(plugin), diff --git a/src/fu-engine.c b/src/fu-engine.c index 2d862edbb..2a0d83474 100644 --- a/src/fu-engine.c +++ b/src/fu-engine.c @@ -101,6 +101,7 @@ struct _FuEngine { gboolean only_trusted; gboolean write_history; gboolean host_emulation; + gboolean has_hwinfo; guint percentage; FuHistory *history; FuIdle *idle; @@ -1887,8 +1888,11 @@ fu_engine_check_requirement(FuEngine *self, } /* ensure hardware requirement */ - if (g_strcmp0(xb_node_get_element(req), "hardware") == 0) + if (g_strcmp0(xb_node_get_element(req), "hardware") == 0) { + if (!self->has_hwinfo) + return TRUE; return fu_engine_check_requirement_hardware(self, req, error); + } /* ensure client requirement */ if (g_strcmp0(xb_node_get_element(req), "client") == 0) @@ -2195,18 +2199,20 @@ fu_engine_get_report_metadata(FuEngine *self, GError **error) g_hash_table_insert(hash, g_strdup("HostBkc"), g_strdup(tmp)); /* DMI data */ - tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_PRODUCT_NAME); - if (tmp != NULL) - g_hash_table_insert(hash, g_strdup("HostProduct"), g_strdup(tmp)); - tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_FAMILY); - if (tmp != NULL) - g_hash_table_insert(hash, g_strdup("HostFamily"), g_strdup(tmp)); - tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_PRODUCT_SKU); - if (tmp != NULL) - g_hash_table_insert(hash, g_strdup("HostSku"), g_strdup(tmp)); - tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_MANUFACTURER); - if (tmp != NULL) - g_hash_table_insert(hash, g_strdup("HostVendor"), g_strdup(tmp)); + if (self->has_hwinfo) { + tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_PRODUCT_NAME); + if (tmp != NULL) + g_hash_table_insert(hash, g_strdup("HostProduct"), g_strdup(tmp)); + tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_FAMILY); + if (tmp != NULL) + g_hash_table_insert(hash, g_strdup("HostFamily"), g_strdup(tmp)); + tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_PRODUCT_SKU); + if (tmp != NULL) + g_hash_table_insert(hash, g_strdup("HostSku"), g_strdup(tmp)); + tmp = fu_context_get_hwid_value(self->ctx, FU_HWIDS_KEY_MANUFACTURER); + if (tmp != NULL) + g_hash_table_insert(hash, g_strdup("HostVendor"), g_strdup(tmp)); + } /* kernel version is often important for debugging failures */ #ifdef HAVE_UTSNAME_H @@ -7600,7 +7606,6 @@ fu_engine_load(FuEngine *self, FuEngineLoadFlags flags, FuProgress *progress, GE { FuPlugin *plugin_uefi; FuQuirksLoadFlags quirks_flags = FU_QUIRKS_LOAD_FLAG_NONE; - GPtrArray *guids; const gchar *host_emulate = g_getenv("FWUPD_HOST_EMULATE"); guint backend_cnt = 0; g_autoptr(GPtrArray) checksums_approved = NULL; @@ -7769,6 +7774,7 @@ fu_engine_load(FuEngine *self, FuEngineLoadFlags flags, FuProgress *progress, GE if (flags & FU_ENGINE_LOAD_FLAG_HWINFO) { if (!fu_context_load_hwinfo(self->ctx, error)) return FALSE; + self->has_hwinfo = TRUE; } fu_progress_step_done(progress); @@ -7882,15 +7888,18 @@ fu_engine_load(FuEngine *self, FuEngineLoadFlags flags, FuProgress *progress, GE fu_progress_step_done(progress); /* set quirks for each hwid */ - guids = fu_context_get_hwid_guids(self->ctx); - for (guint i = 0; i < guids->len; i++) { - const gchar *hwid = g_ptr_array_index(guids, i); - fu_engine_load_quirks_for_hwid(self, hwid); + if (self->has_hwinfo) { + GPtrArray *guids = fu_context_get_hwid_guids(self->ctx); + for (guint i = 0; i < guids->len; i++) { + const gchar *hwid = g_ptr_array_index(guids, i); + fu_engine_load_quirks_for_hwid(self, hwid); + } } fu_progress_step_done(progress); /* set up battery threshold */ - fu_engine_context_set_battery_threshold(self->ctx); + if (self->has_hwinfo) + fu_engine_context_set_battery_threshold(self->ctx); /* watch the device list for updates and proxy */ g_signal_connect(FU_DEVICE_LIST(self->device_list), diff --git a/src/fu-self-test.c b/src/fu-self-test.c index 916e59d67..ddf5ab0a0 100644 --- a/src/fu-self-test.c +++ b/src/fu-self-test.c @@ -1480,7 +1480,10 @@ fu_engine_require_hwid_func(gconstpointer user_data) fu_engine_set_silo(engine, silo_empty); /* load engine to get FuConfig set up */ - ret = fu_engine_load(engine, FU_ENGINE_LOAD_FLAG_NO_CACHE, progress, &error); + ret = fu_engine_load(engine, + FU_ENGINE_LOAD_FLAG_NO_CACHE | FU_ENGINE_LOAD_FLAG_HWINFO, + progress, + &error); g_assert_no_error(error); g_assert_true(ret);