Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | 5c9b1fc | 2021-01-07 14:20:49 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuEngine" |
| 8 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 11 | #include <gio/gio.h> |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 12 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 13 | #include <gio/gunixinputstream.h> |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 14 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 15 | #include <glib-object.h> |
Richard Hughes | bd4d285 | 2017-09-13 14:05:14 +0100 | [diff] [blame] | 16 | #include <string.h> |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 17 | #ifdef HAVE_UTSNAME_H |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 18 | #include <sys/utsname.h> |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 19 | #endif |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 21 | |
| 22 | #include "fwupd-common-private.h" |
| 23 | #include "fwupd-enums-private.h" |
| 24 | #include "fwupd-error.h" |
| 25 | #include "fwupd-release-private.h" |
| 26 | #include "fwupd-remote-private.h" |
| 27 | #include "fwupd-resources.h" |
Mario Limonciello | 75835b4 | 2021-01-21 12:47:32 -0600 | [diff] [blame] | 28 | #include "fwupd-security-attr-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 29 | |
Richard Hughes | 3891171 | 2021-03-12 14:19:14 +0000 | [diff] [blame] | 30 | #include "fu-backend.h" |
Richard Hughes | c6eb416 | 2020-02-27 10:02:36 +0000 | [diff] [blame] | 31 | #include "fu-cabinet.h" |
Richard Hughes | deea2da | 2017-12-15 15:40:44 +0000 | [diff] [blame] | 32 | #include "fu-common-cab.h" |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 33 | #include "fu-common.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 34 | #include "fu-config.h" |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 35 | #include "fu-context-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 36 | #include "fu-debug.h" |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 37 | #include "fu-device-list.h" |
Richard Hughes | 9dde04f | 2017-09-13 12:07:15 +0100 | [diff] [blame] | 38 | #include "fu-device-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 39 | #include "fu-engine.h" |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 40 | #include "fu-engine-helper.h" |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 41 | #include "fu-engine-request.h" |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 42 | #include "fu-idle.h" |
Richard Hughes | 7383ce2 | 2018-05-08 14:14:35 +0100 | [diff] [blame] | 43 | #include "fu-keyring-utils.h" |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 44 | #include "fu-hash.h" |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 45 | #include "fu-history.h" |
Richard Hughes | 37d0943 | 2018-09-09 10:39:45 +0100 | [diff] [blame] | 46 | #include "fu-mutex.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 47 | #include "fu-plugin.h" |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 48 | #include "fu-plugin-list.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 49 | #include "fu-plugin-private.h" |
| 50 | #include "fu-quirks.h" |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 51 | #include "fu-remote-list.h" |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 52 | #include "fu-security-attr.h" |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 53 | #include "fu-security-attrs-private.h" |
Richard Hughes | 405baeb | 2018-09-06 14:23:08 +0100 | [diff] [blame] | 54 | #include "fu-udev-device-private.h" |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 55 | |
| 56 | #ifdef HAVE_GUDEV |
| 57 | #include "fu-udev-backend.h" |
| 58 | #endif |
| 59 | #ifdef HAVE_GUSB |
| 60 | #include "fu-usb-backend.h" |
| 61 | #endif |
Ricardo Cañuelo | 536fb85 | 2021-02-12 12:48:58 +0100 | [diff] [blame] | 62 | #ifdef HAVE_BLUEZ |
| 63 | #include "fu-bluez-backend.h" |
| 64 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 65 | |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 66 | #include "fu-dfu-firmware.h" |
Richard Hughes | 98972f4 | 2021-01-29 11:35:02 +0000 | [diff] [blame] | 67 | #include "fu-dfuse-firmware.h" |
Benson Leung | acba98b | 2020-05-29 14:39:41 -0700 | [diff] [blame] | 68 | #include "fu-fmap-firmware.h" |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 69 | #include "fu-ihex-firmware.h" |
| 70 | #include "fu-srec-firmware.h" |
| 71 | |
Richard Hughes | afda962 | 2020-06-10 12:48:31 +0100 | [diff] [blame] | 72 | /* only needed until we hard depend on jcat 0.1.3 */ |
| 73 | #include <libjcat/jcat-version.h> |
| 74 | |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 75 | #ifdef HAVE_SYSTEMD |
| 76 | #include "fu-systemd.h" |
| 77 | #endif |
| 78 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 79 | static void fu_engine_finalize (GObject *obj); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 80 | static void fu_engine_ensure_security_attrs (FuEngine *self); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 81 | |
| 82 | struct _FuEngine |
| 83 | { |
| 84 | GObject parent_instance; |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 85 | FuAppFlags app_flags; |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 86 | GPtrArray *backends; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 87 | FuConfig *config; |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 88 | FuRemoteList *remote_list; |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 89 | FuDeviceList *device_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 90 | FwupdStatus status; |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 91 | gboolean tainted; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 92 | guint percentage; |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 93 | FuHistory *history; |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 94 | FuIdle *idle; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 95 | XbSilo *silo; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 96 | guint coldplug_id; |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 97 | FuPluginList *plugin_list; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 98 | GPtrArray *plugin_filter; |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 99 | FuContext *ctx; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 100 | GHashTable *runtime_versions; |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 101 | GHashTable *compile_versions; |
Richard Hughes | 3444cf6 | 2020-06-22 15:10:28 +0100 | [diff] [blame] | 102 | GHashTable *approved_firmware; /* (nullable) */ |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 103 | GHashTable *blocked_firmware; /* (nullable) */ |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 104 | gchar *host_machine_id; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 105 | JcatContext *jcat_context; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 106 | gboolean loaded; |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 107 | gchar *host_security_id; |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 108 | FuSecurityAttrs *host_security_attrs; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | enum { |
| 112 | SIGNAL_CHANGED, |
| 113 | SIGNAL_DEVICE_ADDED, |
| 114 | SIGNAL_DEVICE_REMOVED, |
| 115 | SIGNAL_DEVICE_CHANGED, |
| 116 | SIGNAL_STATUS_CHANGED, |
| 117 | SIGNAL_PERCENTAGE_CHANGED, |
| 118 | SIGNAL_LAST |
| 119 | }; |
| 120 | |
| 121 | static guint signals[SIGNAL_LAST] = { 0 }; |
| 122 | |
| 123 | G_DEFINE_TYPE (FuEngine, fu_engine, G_TYPE_OBJECT) |
| 124 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 125 | static void |
| 126 | fu_engine_emit_changed (FuEngine *self) |
| 127 | { |
| 128 | g_signal_emit (self, signals[SIGNAL_CHANGED], 0); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 129 | fu_engine_idle_reset (self); |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 130 | |
| 131 | /* update the motd */ |
| 132 | if (self->loaded && |
| 133 | fu_config_get_update_motd (self->config)) { |
| 134 | g_autoptr(GError) error_local = NULL; |
| 135 | if (!fu_engine_update_motd (self, &error_local)) |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 136 | g_debug ("failed to update MOTD: %s", |
| 137 | error_local->message); |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 138 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void |
Richard Hughes | 170c0c1 | 2017-11-22 11:26:24 +0000 | [diff] [blame] | 142 | fu_engine_emit_device_changed (FuEngine *self, FuDevice *device) |
| 143 | { |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 144 | /* invalidate host security attributes */ |
| 145 | g_clear_pointer (&self->host_security_id, g_free); |
Richard Hughes | 170c0c1 | 2017-11-22 11:26:24 +0000 | [diff] [blame] | 146 | g_signal_emit (self, signals[SIGNAL_DEVICE_CHANGED], 0, device); |
| 147 | } |
| 148 | |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 149 | FuContext * |
| 150 | fu_engine_get_context (FuEngine *self) |
Mario Limonciello | da0d188 | 2020-05-05 14:16:39 -0500 | [diff] [blame] | 151 | { |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 152 | return self->ctx; |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 155 | /** |
| 156 | * fu_engine_get_status: |
| 157 | * @self: A #FuEngine |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 158 | * |
| 159 | * Gets the current engine status. |
| 160 | * |
| 161 | * Returns: a #FwupdStatus, e.g. %FWUPD_STATUS_DECOMPRESSING |
| 162 | **/ |
| 163 | FwupdStatus |
| 164 | fu_engine_get_status (FuEngine *self) |
| 165 | { |
| 166 | g_return_val_if_fail (FU_IS_ENGINE (self), 0); |
| 167 | return self->status; |
| 168 | } |
| 169 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 170 | static void |
| 171 | fu_engine_set_status (FuEngine *self, FwupdStatus status) |
| 172 | { |
| 173 | if (self->status == status) |
| 174 | return; |
| 175 | self->status = status; |
| 176 | |
| 177 | /* emit changed */ |
| 178 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 179 | fwupd_status_to_string (status)); |
| 180 | g_signal_emit (self, signals[SIGNAL_STATUS_CHANGED], 0, status); |
| 181 | } |
| 182 | |
| 183 | static void |
| 184 | fu_engine_set_percentage (FuEngine *self, guint percentage) |
| 185 | { |
| 186 | if (self->percentage == percentage) |
| 187 | return; |
| 188 | self->percentage = percentage; |
| 189 | |
| 190 | /* emit changed */ |
| 191 | g_signal_emit (self, signals[SIGNAL_PERCENTAGE_CHANGED], 0, percentage); |
| 192 | } |
| 193 | |
| 194 | static void |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 195 | fu_engine_progress_notify_cb (FuDevice *device, GParamSpec *pspec, FuEngine *self) |
| 196 | { |
Richard Hughes | 2a77afc | 2019-01-30 11:19:48 +0000 | [diff] [blame] | 197 | if (fu_device_get_status (device) == FWUPD_STATUS_UNKNOWN) |
| 198 | return; |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 199 | fu_engine_set_percentage (self, fu_device_get_progress (device)); |
Richard Hughes | ec85ebc | 2018-08-10 09:52:30 +0100 | [diff] [blame] | 200 | fu_engine_emit_device_changed (self, device); |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void |
| 204 | fu_engine_status_notify_cb (FuDevice *device, GParamSpec *pspec, FuEngine *self) |
| 205 | { |
| 206 | fu_engine_set_status (self, fu_device_get_status (device)); |
Richard Hughes | ec85ebc | 2018-08-10 09:52:30 +0100 | [diff] [blame] | 207 | fu_engine_emit_device_changed (self, device); |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void |
Richard Hughes | fbcebe0 | 2017-12-11 16:37:19 +0000 | [diff] [blame] | 211 | fu_engine_watch_device (FuEngine *self, FuDevice *device) |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 212 | { |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 213 | g_autoptr(FuDevice) device_old = fu_device_list_get_old (self->device_list, device); |
Richard Hughes | fbcebe0 | 2017-12-11 16:37:19 +0000 | [diff] [blame] | 214 | if (device_old != NULL) { |
| 215 | g_signal_handlers_disconnect_by_func (device_old, |
| 216 | fu_engine_progress_notify_cb, |
| 217 | self); |
| 218 | g_signal_handlers_disconnect_by_func (device_old, |
| 219 | fu_engine_status_notify_cb, |
| 220 | self); |
| 221 | } |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 222 | g_signal_connect (device, "notify::progress", |
| 223 | G_CALLBACK (fu_engine_progress_notify_cb), self); |
| 224 | g_signal_connect (device, "notify::status", |
| 225 | G_CALLBACK (fu_engine_status_notify_cb), self); |
Richard Hughes | fbcebe0 | 2017-12-11 16:37:19 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void |
Richard Hughes | 4d76d18 | 2021-04-06 13:35:15 +0100 | [diff] [blame] | 229 | fu_engine_ensure_device_battery_inhibit (FuEngine *self, FuDevice *device) |
| 230 | { |
| 231 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_REQUIRE_AC) && |
| 232 | (fu_context_get_battery_state (self->ctx) == FU_BATTERY_STATE_DISCHARGING || |
| 233 | fu_context_get_battery_state (self->ctx) == FU_BATTERY_STATE_EMPTY)) { |
| 234 | fu_device_inhibit (device, "battery-system", |
| 235 | "Cannot install update when not on AC power"); |
| 236 | return; |
| 237 | } |
| 238 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_REQUIRE_AC) && |
| 239 | fu_context_get_battery_level (self->ctx) != FU_BATTERY_VALUE_INVALID && |
| 240 | fu_context_get_battery_threshold (self->ctx) != FU_BATTERY_VALUE_INVALID && |
| 241 | fu_context_get_battery_level (self->ctx) < fu_context_get_battery_threshold (self->ctx)) { |
| 242 | g_autofree gchar *reason = NULL; |
| 243 | reason = g_strdup_printf ("Cannot install update when system battery is not at least %u%%", |
| 244 | fu_context_get_battery_threshold (self->ctx)); |
| 245 | fu_device_inhibit (device, "battery-system", reason); |
| 246 | return; |
| 247 | } |
| 248 | fu_device_uninhibit (device, "battery-system"); |
| 249 | } |
| 250 | |
| 251 | static void |
Richard Hughes | fbcebe0 | 2017-12-11 16:37:19 +0000 | [diff] [blame] | 252 | fu_engine_device_added_cb (FuDeviceList *device_list, FuDevice *device, FuEngine *self) |
| 253 | { |
| 254 | fu_engine_watch_device (self, device); |
Richard Hughes | 4d76d18 | 2021-04-06 13:35:15 +0100 | [diff] [blame] | 255 | fu_engine_ensure_device_battery_inhibit (self, device); |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 256 | g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device); |
| 257 | } |
| 258 | |
| 259 | static void |
Mario Limonciello | e260ead | 2018-09-01 09:19:24 -0500 | [diff] [blame] | 260 | fu_engine_device_runner_device_removed (FuEngine *self, FuDevice *device) |
| 261 | { |
| 262 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
| 263 | for (guint j = 0; j < plugins->len; j++) { |
| 264 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 265 | fu_plugin_runner_device_removed (plugin_tmp, device); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 270 | fu_engine_device_removed_cb (FuDeviceList *device_list, FuDevice *device, FuEngine *self) |
| 271 | { |
Mario Limonciello | e260ead | 2018-09-01 09:19:24 -0500 | [diff] [blame] | 272 | fu_engine_device_runner_device_removed (self, device); |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 273 | g_signal_handlers_disconnect_by_data (device, self); |
| 274 | g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device); |
| 275 | } |
| 276 | |
| 277 | static void |
| 278 | fu_engine_device_changed_cb (FuDeviceList *device_list, FuDevice *device, FuEngine *self) |
| 279 | { |
Richard Hughes | fbcebe0 | 2017-12-11 16:37:19 +0000 | [diff] [blame] | 280 | fu_engine_watch_device (self, device); |
Richard Hughes | 4a03601 | 2017-11-30 16:33:24 +0000 | [diff] [blame] | 281 | fu_engine_emit_device_changed (self, device); |
| 282 | } |
| 283 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 284 | /* convert hex and decimal versions to dotted style */ |
| 285 | static gchar * |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 286 | fu_engine_get_release_version (FuEngine *self, FuDevice *dev, XbNode *rel, GError **error) |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 287 | { |
Mario Limonciello | f675c21 | 2020-02-25 11:12:35 -0600 | [diff] [blame] | 288 | FwupdVersionFormat fmt = fu_device_get_version_format (dev); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 289 | const gchar *version; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 290 | guint64 ver_uint32; |
| 291 | |
| 292 | /* get version */ |
| 293 | version = xb_node_get_attr (rel, "version"); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 294 | if (version == NULL) { |
| 295 | g_set_error_literal (error, |
| 296 | FWUPD_ERROR, |
| 297 | FWUPD_ERROR_NOT_SUPPORTED, |
| 298 | "version unset"); |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 299 | return NULL; |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 300 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 301 | |
| 302 | /* already dotted notation */ |
| 303 | if (g_strstr_len (version, -1, ".") != NULL) |
| 304 | return g_strdup (version); |
| 305 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 306 | /* don't touch my version! */ |
Richard Hughes | c84b36c | 2019-04-27 09:24:42 +0100 | [diff] [blame] | 307 | if (fmt == FWUPD_VERSION_FORMAT_PLAIN) |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 308 | return g_strdup (version); |
| 309 | |
| 310 | /* parse as integer */ |
| 311 | ver_uint32 = fu_common_strtoull (version); |
Mario Limonciello | f675c21 | 2020-02-25 11:12:35 -0600 | [diff] [blame] | 312 | if (fmt == FWUPD_VERSION_FORMAT_UNKNOWN || |
| 313 | ver_uint32 == 0 || ver_uint32 > G_MAXUINT32) |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 314 | return g_strdup (version); |
| 315 | |
| 316 | /* convert to dotted decimal */ |
| 317 | return fu_common_version_from_uint32 ((guint32) ver_uint32, fmt); |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Richard Hughes | b856f0b | 2021-01-28 10:59:49 +0000 | [diff] [blame] | 320 | static gint |
| 321 | fu_engine_scheme_compare_cb (gconstpointer a, gconstpointer b, gpointer user_data) |
| 322 | { |
| 323 | FuEngine *self = FU_ENGINE (user_data); |
| 324 | const gchar *location1 = *((const gchar ** )a); |
| 325 | const gchar *location2 = *((const gchar **) b); |
| 326 | g_autofree gchar *scheme1 = fu_common_uri_get_scheme (location1); |
| 327 | g_autofree gchar *scheme2 = fu_common_uri_get_scheme (location2); |
| 328 | guint prio1 = fu_config_get_uri_scheme_prio (self->config, scheme1); |
| 329 | guint prio2 = fu_config_get_uri_scheme_prio (self->config, scheme2); |
| 330 | if (prio1 < prio2) |
| 331 | return -1; |
| 332 | if (prio1 > prio2) |
| 333 | return 1; |
| 334 | return 0; |
| 335 | } |
| 336 | |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 337 | static gboolean |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 338 | fu_engine_set_release_from_artifact (FuEngine *self, |
| 339 | FwupdRelease *rel, |
| 340 | FwupdRemote *remote, |
| 341 | XbNode *artifact, |
| 342 | GError **error) |
| 343 | { |
Richard Hughes | c2f7ed3 | 2021-01-28 20:11:01 +0000 | [diff] [blame] | 344 | const gchar *filename; |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 345 | guint64 size; |
| 346 | g_autoptr(GPtrArray) locations = NULL; |
| 347 | g_autoptr(GPtrArray) checksums = NULL; |
| 348 | |
Richard Hughes | c2f7ed3 | 2021-01-28 20:11:01 +0000 | [diff] [blame] | 349 | /* filename */ |
| 350 | filename = xb_node_query_text (artifact, "filename", NULL); |
| 351 | if (filename != NULL) |
| 352 | fwupd_release_set_filename (rel, filename); |
| 353 | |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 354 | /* location */ |
| 355 | locations = xb_node_query (artifact, "location", 0, NULL); |
| 356 | if (locations != NULL) { |
| 357 | for (guint i = 0; i < locations->len; i++) { |
| 358 | XbNode *n = g_ptr_array_index (locations, i); |
Richard Hughes | b856f0b | 2021-01-28 10:59:49 +0000 | [diff] [blame] | 359 | g_autofree gchar *scheme = NULL; |
| 360 | |
| 361 | /* check the scheme is allowed */ |
| 362 | scheme = fu_common_uri_get_scheme (xb_node_get_text (n)); |
| 363 | if (scheme != NULL) { |
| 364 | guint prio = fu_config_get_uri_scheme_prio (self->config, scheme); |
| 365 | if (prio == G_MAXUINT) |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | /* build the complete URI */ |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 370 | if (remote != NULL) { |
| 371 | g_autofree gchar *uri = NULL; |
| 372 | uri = fwupd_remote_build_firmware_uri (remote, |
| 373 | xb_node_get_text (n), |
| 374 | NULL); |
| 375 | if (uri != NULL) { |
| 376 | fwupd_release_add_location (rel, uri); |
| 377 | continue; |
| 378 | } |
| 379 | } |
| 380 | fwupd_release_add_location (rel, xb_node_get_text (n)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* checksum */ |
| 385 | checksums = xb_node_query (artifact, "checksum", 0, NULL); |
| 386 | if (checksums != NULL) { |
| 387 | for (guint i = 0; i < checksums->len; i++) { |
| 388 | XbNode *n = g_ptr_array_index (checksums, i); |
| 389 | fwupd_release_add_checksum (rel, xb_node_get_text (n)); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* size */ |
| 394 | size = xb_node_query_text_as_uint (artifact, "size[@type='installed']", NULL); |
| 395 | if (size != G_MAXUINT64) |
| 396 | fwupd_release_set_size (rel, size); |
| 397 | |
| 398 | /* success */ |
| 399 | return TRUE; |
| 400 | } |
| 401 | |
| 402 | static gboolean |
Richard Hughes | bd4d285 | 2017-09-13 14:05:14 +0100 | [diff] [blame] | 403 | fu_engine_set_release_from_appstream (FuEngine *self, |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 404 | FuDevice *dev, |
Richard Hughes | bd4d285 | 2017-09-13 14:05:14 +0100 | [diff] [blame] | 405 | FwupdRelease *rel, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 406 | XbNode *component, |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 407 | XbNode *release, |
| 408 | GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 409 | { |
Richard Hughes | c6afb51 | 2017-08-22 10:22:20 +0100 | [diff] [blame] | 410 | FwupdRemote *remote = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 411 | const gchar *tmp; |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 412 | const gchar *remote_id; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 413 | guint64 tmp64; |
| 414 | g_autofree gchar *version_rel = NULL; |
Richard Hughes | 02ac92c | 2019-03-29 17:56:46 +0000 | [diff] [blame] | 415 | g_autoptr(GPtrArray) cats = NULL; |
Richard Hughes | 0ad59cb | 2019-09-16 10:33:05 +0100 | [diff] [blame] | 416 | g_autoptr(GPtrArray) issues = NULL; |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 417 | g_autoptr(XbNode) artifact = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 418 | g_autoptr(XbNode) description = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 419 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 420 | /* set from the component */ |
| 421 | tmp = xb_node_query_text (component, "id", NULL); |
| 422 | if (tmp != NULL) |
| 423 | fwupd_release_set_appstream_id (rel, tmp); |
| 424 | tmp = xb_node_query_text (component, "url[@type='homepage']", NULL); |
| 425 | if (tmp != NULL) |
| 426 | fwupd_release_set_homepage (rel, tmp); |
| 427 | tmp = xb_node_query_text (component, "project_license", NULL); |
| 428 | if (tmp != NULL) |
| 429 | fwupd_release_set_license (rel, tmp); |
| 430 | tmp = xb_node_query_text (component, "name", NULL); |
| 431 | if (tmp != NULL) |
| 432 | fwupd_release_set_name (rel, tmp); |
| 433 | tmp = xb_node_query_text (component, "summary", NULL); |
| 434 | if (tmp != NULL) |
| 435 | fwupd_release_set_summary (rel, tmp); |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 436 | tmp = xb_node_query_text (component, "branch", NULL); |
| 437 | if (tmp != NULL) |
| 438 | fwupd_release_set_branch (rel, tmp); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 439 | tmp = xb_node_query_text (component, "developer_name", NULL); |
| 440 | if (tmp != NULL) |
| 441 | fwupd_release_set_vendor (rel, tmp); |
| 442 | |
Mario Limonciello | 92ff9c7 | 2020-03-09 16:54:56 -0500 | [diff] [blame] | 443 | /* refresh the device and release to the new version format too */ |
| 444 | fu_engine_md_refresh_device_from_component (self, dev, component); |
| 445 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 446 | /* the version is fixed up at runtime */ |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 447 | version_rel = fu_engine_get_release_version (self, dev, release, error); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 448 | if (version_rel == NULL) |
| 449 | return FALSE; |
| 450 | fwupd_release_set_version (rel, version_rel); |
Richard Hughes | 74fa2ca | 2018-01-10 21:33:39 +0000 | [diff] [blame] | 451 | |
Richard Hughes | c6afb51 | 2017-08-22 10:22:20 +0100 | [diff] [blame] | 452 | /* find the remote */ |
Richard Hughes | 33171fd | 2018-11-09 13:29:11 +0000 | [diff] [blame] | 453 | remote_id = xb_node_query_text (component, "../custom/value[@key='fwupd::RemoteId']", NULL); |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 454 | if (remote_id != NULL) { |
| 455 | fwupd_release_set_remote_id (rel, remote_id); |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 456 | remote = fu_remote_list_get_by_id (self->remote_list, remote_id); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 457 | if (remote == NULL) |
| 458 | g_warning ("no remote found for release %s", version_rel); |
Richard Hughes | bd4d285 | 2017-09-13 14:05:14 +0100 | [diff] [blame] | 459 | } |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 460 | artifact = xb_node_query_first (release, "artifacts/artifact", NULL); |
| 461 | if (artifact != NULL) { |
| 462 | if (!fu_engine_set_release_from_artifact (self, |
| 463 | rel, |
| 464 | remote, |
| 465 | artifact, |
| 466 | error)) |
| 467 | return FALSE; |
| 468 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 469 | description = xb_node_query_first (release, "description", NULL); |
| 470 | if (description != NULL) { |
| 471 | g_autofree gchar *xml = NULL; |
| 472 | xml = xb_node_export (description, XB_NODE_EXPORT_FLAG_ONLY_CHILDREN, NULL); |
| 473 | if (xml != NULL) |
| 474 | fwupd_release_set_description (rel, xml); |
| 475 | } |
Richard Hughes | 5213fa5 | 2021-01-28 10:56:22 +0000 | [diff] [blame] | 476 | if (artifact == NULL) { |
| 477 | tmp = xb_node_query_text (release, "location", NULL); |
| 478 | if (tmp != NULL) { |
| 479 | g_autofree gchar *uri = NULL; |
| 480 | if (remote != NULL) |
| 481 | uri = fwupd_remote_build_firmware_uri (remote, tmp, NULL); |
| 482 | if (uri == NULL) |
| 483 | uri = g_strdup (tmp); |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 484 | fwupd_release_add_location (rel, uri); |
Richard Hughes | 5213fa5 | 2021-01-28 10:56:22 +0000 | [diff] [blame] | 485 | } else if (remote != NULL && |
| 486 | fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 487 | g_autofree gchar *uri = NULL; |
| 488 | tmp = xb_node_query_text (component, "../custom/value[@key='fwupd::FilenameCache']", NULL); |
| 489 | if (tmp != NULL) { |
| 490 | uri = g_strdup_printf ("file://%s", tmp); |
| 491 | fwupd_release_add_location (rel, uri); |
| 492 | } |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 493 | } |
Richard Hughes | c6afb51 | 2017-08-22 10:22:20 +0100 | [diff] [blame] | 494 | } |
Richard Hughes | c2f7ed3 | 2021-01-28 20:11:01 +0000 | [diff] [blame] | 495 | if (artifact == NULL) { |
| 496 | tmp = xb_node_query_text (release, "checksum[@target='content']", NULL); |
| 497 | if (tmp != NULL) |
| 498 | fwupd_release_set_filename (rel, tmp); |
| 499 | } |
Richard Hughes | 7185828 | 2019-01-26 12:15:55 +0000 | [diff] [blame] | 500 | tmp = xb_node_query_text (release, "url[@type='details']", NULL); |
| 501 | if (tmp != NULL) |
| 502 | fwupd_release_set_details_url (rel, tmp); |
| 503 | tmp = xb_node_query_text (release, "url[@type='source']", NULL); |
| 504 | if (tmp != NULL) |
| 505 | fwupd_release_set_source_url (rel, tmp); |
Richard Hughes | 5213fa5 | 2021-01-28 10:56:22 +0000 | [diff] [blame] | 506 | if (artifact == NULL) { |
| 507 | tmp = xb_node_query_text (release, "checksum[@target='container']", NULL); |
| 508 | if (tmp != NULL) |
| 509 | fwupd_release_add_checksum (rel, tmp); |
| 510 | } |
| 511 | if (artifact == NULL) { |
| 512 | tmp64 = xb_node_query_text_as_uint (release, "size[@type='installed']", NULL); |
| 513 | if (tmp64 != G_MAXUINT64) |
| 514 | fwupd_release_set_size (rel, tmp64); |
| 515 | } |
| 516 | if (fwupd_release_get_size (rel) == 0) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 517 | GBytes *sz = xb_node_get_data (release, "fwupd::ReleaseSize"); |
| 518 | if (sz != NULL) { |
| 519 | const guint64 *sizeptr = g_bytes_get_data (sz, NULL); |
| 520 | fwupd_release_set_size (rel, *sizeptr); |
| 521 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 522 | } |
Richard Hughes | 52c1a4d | 2020-04-02 11:21:06 +0100 | [diff] [blame] | 523 | tmp = xb_node_get_attr (release, "urgency"); |
| 524 | if (tmp != NULL) |
| 525 | fwupd_release_set_urgency (rel, fwupd_release_urgency_from_string (tmp)); |
Richard Hughes | 6e0c8f8 | 2018-11-11 21:46:41 +0000 | [diff] [blame] | 526 | tmp64 = xb_node_get_attr_as_uint (release, "install_duration"); |
Richard Hughes | 319dbcb | 2018-12-18 17:59:05 +0000 | [diff] [blame] | 527 | if (tmp64 != G_MAXUINT64) |
Richard Hughes | 6e0c8f8 | 2018-11-11 21:46:41 +0000 | [diff] [blame] | 528 | fwupd_release_set_install_duration (rel, tmp64); |
Richard Hughes | 14797f8 | 2020-04-02 10:52:04 +0100 | [diff] [blame] | 529 | tmp64 = xb_node_get_attr_as_uint (release, "timestamp"); |
| 530 | if (tmp64 != G_MAXUINT64) |
| 531 | fwupd_release_set_created (rel, tmp64); |
Richard Hughes | 02ac92c | 2019-03-29 17:56:46 +0000 | [diff] [blame] | 532 | cats = xb_node_query (component, "categories/category", 0, NULL); |
| 533 | if (cats != NULL) { |
| 534 | for (guint i = 0; i < cats->len; i++) { |
| 535 | XbNode *n = g_ptr_array_index (cats, i); |
| 536 | fwupd_release_add_category (rel, xb_node_get_text (n)); |
| 537 | } |
| 538 | } |
Richard Hughes | 0ad59cb | 2019-09-16 10:33:05 +0100 | [diff] [blame] | 539 | issues = xb_node_query (component, "issues/issue", 0, NULL); |
| 540 | if (issues != NULL) { |
| 541 | for (guint i = 0; i < issues->len; i++) { |
| 542 | XbNode *n = g_ptr_array_index (issues, i); |
| 543 | fwupd_release_add_issue (rel, xb_node_get_text (n)); |
| 544 | } |
| 545 | } |
Richard Hughes | 868db4e | 2019-09-26 14:56:57 +0100 | [diff] [blame] | 546 | tmp = xb_node_query_text (component, "screenshots/screenshot/caption", NULL); |
| 547 | if (tmp != NULL) |
| 548 | fwupd_release_set_detach_caption (rel, tmp); |
| 549 | tmp = xb_node_query_text (component, "screenshots/screenshot/image", NULL); |
| 550 | if (tmp != NULL) |
| 551 | fwupd_release_set_detach_image (rel, tmp); |
Richard Hughes | adeb2b1 | 2018-12-14 11:56:41 +0000 | [diff] [blame] | 552 | tmp = xb_node_query_text (component, "custom/value[@key='LVFS::UpdateProtocol']", NULL); |
| 553 | if (tmp != NULL) |
| 554 | fwupd_release_set_protocol (rel, tmp); |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 555 | tmp = xb_node_query_text (component, "custom/value[@key='LVFS::UpdateMessage']", NULL); |
| 556 | if (tmp != NULL) |
| 557 | fwupd_release_set_update_message (rel, tmp); |
Richard Hughes | 4fd4b98 | 2020-06-25 18:58:57 +0100 | [diff] [blame] | 558 | tmp = xb_node_query_text (component, "custom/value[@key='LVFS::UpdateImage']", NULL); |
| 559 | if (tmp != NULL) |
| 560 | fwupd_release_set_update_image (rel, tmp); |
Richard Hughes | b856f0b | 2021-01-28 10:59:49 +0000 | [diff] [blame] | 561 | |
| 562 | /* sort the locations by scheme */ |
| 563 | g_ptr_array_sort_with_data (fwupd_release_get_locations (rel), |
| 564 | fu_engine_scheme_compare_cb, self); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 565 | return TRUE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 568 | /* finds the remote-id for the first firmware in the silo that matches this |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 569 | * container checksum */ |
| 570 | static const gchar * |
| 571 | fu_engine_get_remote_id_for_checksum (FuEngine *self, const gchar *csum) |
| 572 | { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 573 | g_autofree gchar *xpath = NULL; |
| 574 | g_autoptr(XbNode) key = NULL; |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 575 | xpath = g_strdup_printf ("components/component[@type='firmware']/releases/release/" |
Richard Hughes | 33171fd | 2018-11-09 13:29:11 +0000 | [diff] [blame] | 576 | "checksum[@target='container'][text()='%s']/../../" |
| 577 | "../../custom/value[@key='fwupd::RemoteId']", csum); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 578 | key = xb_silo_query_first (self->silo, xpath, NULL); |
| 579 | if (key == NULL) |
| 580 | return NULL; |
| 581 | return xb_node_get_text (key); |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 584 | /** |
| 585 | * fu_engine_unlock: |
| 586 | * @self: A #FuEngine |
| 587 | * @device_id: A device ID |
| 588 | * @error: A #GError, or %NULL |
| 589 | * |
| 590 | * Unlocks a device. |
| 591 | * |
| 592 | * Returns: %TRUE for success |
| 593 | **/ |
| 594 | gboolean |
| 595 | fu_engine_unlock (FuEngine *self, const gchar *device_id, GError **error) |
| 596 | { |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 597 | FuPlugin *plugin; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 598 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 599 | |
| 600 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 601 | g_return_val_if_fail (device_id != NULL, FALSE); |
| 602 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 603 | |
| 604 | /* check the device exists */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 605 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 606 | if (device == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 607 | return FALSE; |
| 608 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 609 | /* get the plugin */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 610 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 611 | fu_device_get_plugin (device), |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 612 | error); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 613 | if (plugin == NULL) |
| 614 | return FALSE; |
| 615 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 616 | /* run the correct plugin that added this */ |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 617 | if (!fu_plugin_runner_unlock (plugin, device, error)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 618 | return FALSE; |
| 619 | |
| 620 | /* make the UI update */ |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 621 | fu_engine_emit_device_changed (self, device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 622 | fu_engine_emit_changed (self); |
| 623 | return TRUE; |
| 624 | } |
| 625 | |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 626 | gboolean |
| 627 | fu_engine_modify_config (FuEngine *self, const gchar *key, const gchar *value, GError **error) |
| 628 | { |
| 629 | const gchar *keys[] = { |
| 630 | "ArchiveSizeMax", |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 631 | "DisabledDevices", |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 632 | "BlockedFirmware", |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 633 | "DisabledPlugins", |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 634 | "IdleTimeout", |
Mario Limonciello | 38027e6 | 2019-04-17 15:16:07 +0100 | [diff] [blame] | 635 | "VerboseDomains", |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 636 | "UpdateMotd", |
Mario Limonciello | 4fa95a7 | 2020-03-28 10:50:57 -0500 | [diff] [blame] | 637 | "EnumerateAllDevices", |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 638 | NULL }; |
| 639 | |
| 640 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 641 | g_return_val_if_fail (key != NULL, FALSE); |
| 642 | g_return_val_if_fail (value != NULL, FALSE); |
| 643 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 644 | |
| 645 | /* check keys are valid */ |
| 646 | if (!g_strv_contains (keys, key)) { |
| 647 | g_set_error (error, |
| 648 | FWUPD_ERROR, |
| 649 | FWUPD_ERROR_NOT_FOUND, |
| 650 | "key %s not supported", key); |
| 651 | return FALSE; |
| 652 | } |
| 653 | |
| 654 | /* modify, effective next reboot */ |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 655 | return fu_config_set_key_value (self->config, key, value, error); |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 658 | /** |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 659 | * fu_engine_modify_remote: |
| 660 | * @self: A #FuEngine |
| 661 | * @remote_id: A remote ID |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 662 | * @key: the key, e.g. `Enabled` |
| 663 | * @value: the key, e.g. `true` |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 664 | * @error: A #GError, or %NULL |
| 665 | * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 666 | * Updates the verification silo entry for a specific device. |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 667 | * |
| 668 | * Returns: %TRUE for success |
| 669 | **/ |
| 670 | gboolean |
| 671 | fu_engine_modify_remote (FuEngine *self, |
| 672 | const gchar *remote_id, |
| 673 | const gchar *key, |
| 674 | const gchar *value, |
| 675 | GError **error) |
| 676 | { |
Richard Hughes | 9bc9deb | 2020-05-19 19:49:51 +0100 | [diff] [blame] | 677 | const gchar *keys[] = { |
Richard Hughes | cc93f7c | 2020-06-01 12:04:03 +0100 | [diff] [blame] | 678 | "ApprovalRequired", |
Richard Hughes | 9bc9deb | 2020-05-19 19:49:51 +0100 | [diff] [blame] | 679 | "AutomaticReports", |
| 680 | "AutomaticSecurityReports", |
| 681 | "Enabled", |
| 682 | "FirmwareBaseURI", |
| 683 | "MetadataURI", |
| 684 | "ReportURI", |
| 685 | "SecurityReportURI", |
| 686 | NULL, |
| 687 | }; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 688 | |
| 689 | /* check keys are valid */ |
| 690 | if (!g_strv_contains (keys, key)) { |
| 691 | g_set_error (error, |
| 692 | FWUPD_ERROR, |
| 693 | FWUPD_ERROR_NOT_FOUND, |
| 694 | "key %s not supported", key); |
| 695 | return FALSE; |
| 696 | } |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 697 | return fu_remote_list_set_key_value (self->remote_list, remote_id, key, value, error); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 701 | * fu_engine_modify_device: |
| 702 | * @self: A #FuEngine |
| 703 | * @device_id: A device ID |
| 704 | * @key: the key, e.g. `Flags` |
| 705 | * @value: the key, e.g. `reported` |
| 706 | * @error: A #GError, or %NULL |
| 707 | * |
| 708 | * Sets the reported flag for a specific device. This ensures that other |
| 709 | * front-end clients for fwupd do not report the same event. |
| 710 | * |
| 711 | * Returns: %TRUE for success |
| 712 | **/ |
| 713 | gboolean |
| 714 | fu_engine_modify_device (FuEngine *self, |
| 715 | const gchar *device_id, |
| 716 | const gchar *key, |
| 717 | const gchar *value, |
| 718 | GError **error) |
| 719 | { |
| 720 | g_autoptr(FuDevice) device = NULL; |
| 721 | |
| 722 | /* find the correct device */ |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 723 | device = fu_history_get_device_by_id (self->history, device_id, error); |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 724 | if (device == NULL) |
| 725 | return FALSE; |
| 726 | |
| 727 | /* support adding a subset of the device flags */ |
| 728 | if (g_strcmp0 (key, "Flags") == 0) { |
| 729 | FwupdDeviceFlags flag = fwupd_device_flag_from_string (value); |
| 730 | if (flag == FWUPD_DEVICE_FLAG_UNKNOWN) { |
| 731 | g_set_error (error, |
| 732 | FWUPD_ERROR, |
| 733 | FWUPD_ERROR_NOT_SUPPORTED, |
| 734 | "key %s not a valid flag", key); |
| 735 | return FALSE; |
| 736 | } |
Richard Hughes | ad54f65 | 2018-01-30 17:22:37 +0000 | [diff] [blame] | 737 | if (flag != FWUPD_DEVICE_FLAG_REPORTED && |
| 738 | flag != FWUPD_DEVICE_FLAG_NOTIFIED) { |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 739 | g_set_error (error, |
| 740 | FWUPD_ERROR, |
| 741 | FWUPD_ERROR_NOT_SUPPORTED, |
| 742 | "flag %s cannot be set from client", key); |
| 743 | return FALSE; |
| 744 | } |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 745 | fu_device_add_flag (device, flag); |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 746 | return fu_history_modify_device (self->history, device, error); |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* others invalid */ |
| 750 | g_set_error (error, |
| 751 | FWUPD_ERROR, |
| 752 | FWUPD_ERROR_NOT_SUPPORTED, |
| 753 | "key %s not supported", key); |
| 754 | return FALSE; |
| 755 | } |
| 756 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 757 | static const gchar * |
| 758 | fu_engine_checksum_type_to_string (GChecksumType checksum_type) |
| 759 | { |
| 760 | if (checksum_type == G_CHECKSUM_SHA1) |
| 761 | return "sha1"; |
| 762 | if (checksum_type == G_CHECKSUM_SHA256) |
| 763 | return "sha256"; |
| 764 | if (checksum_type == G_CHECKSUM_SHA512) |
| 765 | return "sha512"; |
| 766 | return "sha1"; |
| 767 | } |
| 768 | |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 769 | /** |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 770 | * fu_engine_verify_update: |
| 771 | * @self: A #FuEngine |
| 772 | * @device_id: A device ID |
| 773 | * @error: A #GError, or %NULL |
| 774 | * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 775 | * Updates the verification silo entry for a specific device. |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 776 | * |
| 777 | * Returns: %TRUE for success |
| 778 | **/ |
| 779 | gboolean |
| 780 | fu_engine_verify_update (FuEngine *self, const gchar *device_id, GError **error) |
| 781 | { |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 782 | FuPlugin *plugin; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 783 | GPtrArray *checksums; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 784 | GPtrArray *guids; |
| 785 | g_autofree gchar *fn = NULL; |
| 786 | g_autofree gchar *localstatedir = NULL; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 787 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 788 | g_autoptr(GFile) file = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 789 | g_autoptr(XbBuilder) builder = xb_builder_new (); |
| 790 | g_autoptr(XbBuilderNode) component = NULL; |
| 791 | g_autoptr(XbBuilderNode) provides = NULL; |
| 792 | g_autoptr(XbBuilderNode) release = NULL; |
| 793 | g_autoptr(XbBuilderNode) releases = NULL; |
| 794 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 795 | |
| 796 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 797 | g_return_val_if_fail (device_id != NULL, FALSE); |
| 798 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 799 | |
| 800 | /* check the devices still exists */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 801 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 802 | if (device == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 803 | return FALSE; |
| 804 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 805 | /* get the plugin */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 806 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 807 | fu_device_get_plugin (device), |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 808 | error); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 809 | if (plugin == NULL) |
| 810 | return FALSE; |
| 811 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 812 | /* get the checksum */ |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 813 | checksums = fu_device_get_checksums (device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 814 | if (checksums->len == 0) { |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 815 | if (!fu_plugin_runner_verify (plugin, device, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 816 | FU_PLUGIN_VERIFY_FLAG_NONE, |
| 817 | error)) |
| 818 | return FALSE; |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 819 | fu_engine_emit_device_changed (self, device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* we got nothing */ |
| 823 | if (checksums->len == 0) { |
| 824 | g_set_error_literal (error, |
| 825 | FWUPD_ERROR, |
| 826 | FWUPD_ERROR_NOT_SUPPORTED, |
| 827 | "device verification not supported"); |
| 828 | return FALSE; |
| 829 | } |
| 830 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 831 | /* build XML */ |
| 832 | component = xb_builder_node_insert (NULL, "component", |
| 833 | "type", "firmware", |
| 834 | NULL); |
| 835 | provides = xb_builder_node_insert (component, "provides", NULL); |
| 836 | guids = fu_device_get_guids (device); |
| 837 | for (guint i = 0; i < guids->len; i++) { |
| 838 | const gchar *guid = g_ptr_array_index (guids, i); |
| 839 | g_autoptr(XbBuilderNode) provide = NULL; |
| 840 | provide = xb_builder_node_insert (provides, "firmware", |
| 841 | "type", "flashed", |
| 842 | NULL); |
| 843 | xb_builder_node_set_text (provide, guid, -1); |
| 844 | } |
| 845 | releases = xb_builder_node_insert (component, "releases", NULL); |
| 846 | release = xb_builder_node_insert (releases, "release", |
| 847 | "version", fu_device_get_version (device), |
| 848 | NULL); |
| 849 | for (guint i = 0; i < checksums->len; i++) { |
| 850 | const gchar *checksum = g_ptr_array_index (checksums, i); |
| 851 | GChecksumType kind = fwupd_checksum_guess_kind (checksum); |
| 852 | g_autoptr(XbBuilderNode) csum = NULL; |
Richard Hughes | aaa60c6 | 2018-11-07 11:05:50 +0000 | [diff] [blame] | 853 | csum = xb_builder_node_insert (release, "checksum", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 854 | "type", fu_engine_checksum_type_to_string (kind), |
| 855 | "target", "content", |
| 856 | NULL); |
| 857 | xb_builder_node_set_text (csum, checksum, -1); |
| 858 | } |
| 859 | xb_builder_import_node (builder, component); |
| 860 | |
| 861 | /* save silo */ |
| 862 | localstatedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 863 | fn = g_strdup_printf ("%s/verify/%s.xml", localstatedir, device_id); |
Richard Hughes | 11f612c | 2019-03-15 16:27:18 +0000 | [diff] [blame] | 864 | if (!fu_common_mkdir_parent (fn, error)) |
| 865 | return FALSE; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 866 | file = g_file_new_for_path (fn); |
| 867 | silo = xb_builder_compile (builder, XB_BUILDER_COMPILE_FLAG_NONE, NULL, error); |
| 868 | if (silo == NULL) |
| 869 | return FALSE; |
| 870 | if (!xb_silo_export_file (silo, file, |
| 871 | XB_NODE_EXPORT_FLAG_FORMAT_MULTILINE, |
| 872 | NULL, error)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 873 | return FALSE; |
| 874 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 875 | /* success */ |
| 876 | return TRUE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 879 | XbNode * |
| 880 | fu_engine_get_component_by_guids (FuEngine *self, FuDevice *device) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 881 | { |
| 882 | GPtrArray *guids = fu_device_get_guids (device); |
Richard Hughes | ab1bc89 | 2018-11-15 15:04:35 +0000 | [diff] [blame] | 883 | g_autoptr(GString) xpath = g_string_new (NULL); |
| 884 | g_autoptr(XbNode) component = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 885 | for (guint i = 0; i < guids->len; i++) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 886 | const gchar *guid = g_ptr_array_index (guids, i); |
Richard Hughes | ab1bc89 | 2018-11-15 15:04:35 +0000 | [diff] [blame] | 887 | xb_string_append_union (xpath, |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 888 | "components/component[@type='firmware']/" |
Richard Hughes | ab1bc89 | 2018-11-15 15:04:35 +0000 | [diff] [blame] | 889 | "provides/firmware[@type='flashed'][text()='%s']/" |
| 890 | "../..", guid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 891 | } |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 892 | component = xb_silo_query_first (self->silo, xpath->str, NULL); |
Richard Hughes | ab1bc89 | 2018-11-15 15:04:35 +0000 | [diff] [blame] | 893 | if (component != NULL) |
| 894 | return g_steal_pointer (&component); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 895 | return NULL; |
| 896 | } |
| 897 | |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 898 | static XbNode * |
| 899 | fu_engine_verify_from_local_metadata (FuEngine *self, |
| 900 | FuDevice *device, |
| 901 | GError **error) |
| 902 | { |
| 903 | g_autofree gchar *fn = NULL; |
| 904 | g_autofree gchar *localstatedir = NULL; |
| 905 | g_autofree gchar *xpath = NULL; |
| 906 | g_autoptr(GFile) file = NULL; |
| 907 | g_autoptr(XbBuilder) builder = xb_builder_new (); |
| 908 | g_autoptr(XbBuilderSource) source = xb_builder_source_new (); |
| 909 | g_autoptr(XbNode) release = NULL; |
| 910 | g_autoptr(XbSilo) silo = NULL; |
| 911 | |
| 912 | localstatedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 913 | fn = g_strdup_printf ("%s/verify/%s.xml", |
| 914 | localstatedir, |
| 915 | fu_device_get_id (device)); |
| 916 | file = g_file_new_for_path (fn); |
| 917 | if (!g_file_query_exists (file, NULL)) { |
| 918 | g_set_error (error, |
| 919 | G_IO_ERROR, |
| 920 | G_IO_ERROR_NOT_FOUND, |
| 921 | "failed to find %s", fn); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
| 925 | if (!xb_builder_source_load_file (source, file, |
| 926 | XB_BUILDER_SOURCE_FLAG_NONE, |
| 927 | NULL, error)) |
| 928 | return NULL; |
| 929 | xb_builder_import_source (builder, source); |
| 930 | silo = xb_builder_compile (builder, |
| 931 | XB_BUILDER_COMPILE_FLAG_NONE, |
| 932 | NULL, error); |
| 933 | if (silo == NULL) |
| 934 | return NULL; |
| 935 | xpath = g_strdup_printf ("component/releases/release[@version='%s']", |
| 936 | fu_device_get_version (device)); |
| 937 | release = xb_silo_query_first (silo, xpath, error); |
| 938 | if (release == NULL) |
| 939 | return NULL; |
| 940 | |
Richard Hughes | 6f4f1ca | 2020-09-24 10:43:26 +0100 | [diff] [blame] | 941 | /* silo has to have same lifecycle as node */ |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 942 | g_object_set_data_full (G_OBJECT (release), "XbSilo", |
| 943 | g_steal_pointer (&silo), |
| 944 | (GDestroyNotify) g_object_unref); |
| 945 | return g_steal_pointer (&release); |
| 946 | } |
| 947 | |
| 948 | static XbNode * |
| 949 | fu_engine_verify_from_system_metadata (FuEngine *self, |
| 950 | FuDevice *device, |
| 951 | GError **error) |
| 952 | { |
| 953 | FwupdVersionFormat fmt = fu_device_get_version_format (device); |
| 954 | GPtrArray *guids = fu_device_get_guids (device); |
| 955 | g_autoptr(XbQuery) query = NULL; |
| 956 | |
| 957 | /* prepare query with bound GUID parameter */ |
| 958 | query = xb_query_new_full (self->silo, |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 959 | "components/component[@type='firmware']/" |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 960 | "provides/firmware[@type='flashed'][text()=?]/" |
| 961 | "../../releases/release", |
| 962 | XB_QUERY_FLAG_OPTIMIZE | |
| 963 | XB_QUERY_FLAG_USE_INDEXES, |
| 964 | error); |
| 965 | if (query == NULL) |
| 966 | return NULL; |
| 967 | |
| 968 | /* use prepared query for each GUID */ |
| 969 | for (guint i = 0; i < guids->len; i++) { |
| 970 | const gchar *guid = g_ptr_array_index (guids, i); |
| 971 | g_autoptr(GError) error_local = NULL; |
| 972 | g_autoptr(GPtrArray) releases = NULL; |
Philip Withnall | aa48b6b | 2020-08-19 14:17:58 +0100 | [diff] [blame] | 973 | #if LIBXMLB_CHECK_VERSION(0,3,0) |
| 974 | g_auto(XbQueryContext) context = XB_QUERY_CONTEXT_INIT (); |
| 975 | #endif |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 976 | |
| 977 | /* bind GUID and then query */ |
Philip Withnall | aa48b6b | 2020-08-19 14:17:58 +0100 | [diff] [blame] | 978 | #if LIBXMLB_CHECK_VERSION(0,3,0) |
| 979 | xb_value_bindings_bind_str (xb_query_context_get_bindings (&context), 0, guid, NULL); |
| 980 | releases = xb_silo_query_with_context (self->silo, query, &context, &error_local); |
| 981 | #else |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 982 | if (!xb_query_bind_str (query, 0, guid, error)) { |
| 983 | g_prefix_error (error, "failed to bind string: "); |
| 984 | return NULL; |
| 985 | } |
| 986 | releases = xb_silo_query_full (self->silo, query, &error_local); |
Philip Withnall | aa48b6b | 2020-08-19 14:17:58 +0100 | [diff] [blame] | 987 | #endif |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 988 | if (releases == NULL) { |
| 989 | if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) || |
| 990 | g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) { |
| 991 | g_debug ("could not find %s: %s", |
| 992 | guid, error_local->message); |
| 993 | continue; |
| 994 | } |
| 995 | g_propagate_error (error, g_steal_pointer (&error_local)); |
| 996 | return NULL; |
| 997 | } |
| 998 | for (guint j = 0; j < releases->len; j++) { |
| 999 | XbNode *rel = g_ptr_array_index (releases, j); |
| 1000 | const gchar *rel_ver = xb_node_get_attr (rel, "version"); |
| 1001 | g_autofree gchar *tmp_ver = fu_common_version_parse_from_format (rel_ver, fmt); |
| 1002 | if (fu_common_vercmp_full (tmp_ver, fu_device_get_version (device), fmt) == 0) |
| 1003 | return g_object_ref (rel); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* not found */ |
| 1008 | g_set_error_literal (error, |
| 1009 | G_IO_ERROR, |
| 1010 | G_IO_ERROR_NOT_FOUND, |
| 1011 | "failed to find release"); |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1015 | /** |
| 1016 | * fu_engine_verify: |
| 1017 | * @self: A #FuEngine |
| 1018 | * @device_id: A device ID |
| 1019 | * @error: A #GError, or %NULL |
| 1020 | * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1021 | * Verifies a device firmware checksum using the verification silo entry. |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1022 | * |
| 1023 | * Returns: %TRUE for success |
| 1024 | **/ |
| 1025 | gboolean |
| 1026 | fu_engine_verify (FuEngine *self, const gchar *device_id, GError **error) |
| 1027 | { |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 1028 | FuPlugin *plugin; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1029 | GPtrArray *checksums; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 1030 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1031 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1032 | g_autoptr(GString) xpath_csum = g_string_new (NULL); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1033 | g_autoptr(XbNode) csum = NULL; |
| 1034 | g_autoptr(XbNode) release = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1035 | |
| 1036 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 1037 | g_return_val_if_fail (device_id != NULL, FALSE); |
| 1038 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 1039 | |
| 1040 | /* check the id exists */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 1041 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 1042 | if (device == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1043 | return FALSE; |
| 1044 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 1045 | /* get the plugin */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 1046 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 1047 | fu_device_get_plugin (device), |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 1048 | error); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 1049 | if (plugin == NULL) |
| 1050 | return FALSE; |
| 1051 | |
Mario Limonciello | 8fa0b38 | 2019-10-14 07:36:04 -0500 | [diff] [blame] | 1052 | /* update the device firmware hashes if possible */ |
| 1053 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_CAN_VERIFY_IMAGE)) { |
| 1054 | if (!fu_plugin_runner_verify (plugin, device, |
| 1055 | FU_PLUGIN_VERIFY_FLAG_NONE, error)) |
| 1056 | return FALSE; |
| 1057 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1058 | |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1059 | /* find component in local metadata */ |
| 1060 | release = fu_engine_verify_from_local_metadata (self, device, &error_local); |
| 1061 | if (release == NULL) { |
| 1062 | if (!g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) && |
| 1063 | !g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) { |
| 1064 | g_propagate_error (error, g_steal_pointer (&error_local)); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1065 | return FALSE; |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1066 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1069 | /* try again with the system metadata */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1070 | if (release == NULL) { |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1071 | g_autoptr(GError) error_system = NULL; |
| 1072 | release = fu_engine_verify_from_system_metadata (self, device, &error_system); |
| 1073 | if (release == NULL) { |
| 1074 | if (!g_error_matches (error_system, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) && |
| 1075 | !g_error_matches (error_system, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) { |
| 1076 | g_propagate_error (error, g_steal_pointer (&error_system)); |
Mario Limonciello | 91d3609 | 2019-10-14 08:44:39 -0500 | [diff] [blame] | 1077 | return FALSE; |
Richard Hughes | d63cedc | 2020-06-21 13:56:50 +0100 | [diff] [blame] | 1078 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1079 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1080 | } |
| 1081 | if (release == NULL) { |
| 1082 | g_set_error (error, |
| 1083 | FWUPD_ERROR, |
| 1084 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1085 | "No release found for version %s", |
| 1086 | fu_device_get_version (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1087 | return FALSE; |
| 1088 | } |
| 1089 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1090 | /* get the matching checksum */ |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 1091 | checksums = fu_device_get_checksums (device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1092 | if (checksums->len == 0) { |
| 1093 | g_set_error (error, |
| 1094 | FWUPD_ERROR, |
| 1095 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1096 | "No device checksums for %s", |
| 1097 | fu_device_get_version (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1098 | return FALSE; |
| 1099 | } |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1100 | |
| 1101 | /* do any of the checksums in the release match any in the device */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1102 | for (guint j = 0; j < checksums->len; j++) { |
| 1103 | const gchar *hash_tmp = g_ptr_array_index (checksums, j); |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1104 | xb_string_append_union (xpath_csum, |
| 1105 | "checksum[@target='device'][text()='%s']", |
| 1106 | hash_tmp); |
| 1107 | xb_string_append_union (xpath_csum, |
| 1108 | "checksum[@target='content'][text()='%s']", |
| 1109 | hash_tmp); |
| 1110 | } |
| 1111 | csum = xb_node_query_first (release, xpath_csum->str, NULL); |
| 1112 | if (csum == NULL) { |
| 1113 | g_autoptr(GString) checksums_device = g_string_new (NULL); |
| 1114 | g_autoptr(GString) checksums_metadata = g_string_new (NULL); |
| 1115 | g_autoptr(GPtrArray) csums = NULL; |
| 1116 | g_autoptr(GString) xpath = g_string_new (NULL); |
| 1117 | |
| 1118 | /* get all checksums to display a useful error */ |
| 1119 | xb_string_append_union (xpath, "checksum[@target='device']"); |
Mario Limonciello | 2a0e20b | 2019-10-14 12:09:35 -0500 | [diff] [blame] | 1120 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_CAN_VERIFY_IMAGE)) |
| 1121 | xb_string_append_union (xpath, "checksum[@target='content']"); |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1122 | csums = xb_node_query (release, xpath->str, 0, NULL); |
| 1123 | if (csums == NULL) { |
| 1124 | g_set_error (error, |
| 1125 | FWUPD_ERROR, |
| 1126 | FWUPD_ERROR_NOT_FOUND, |
Mario Limonciello | 2a0e20b | 2019-10-14 12:09:35 -0500 | [diff] [blame] | 1127 | "No stored checksums for %s", |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1128 | fu_device_get_version (device)); |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1129 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1130 | } |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1131 | for (guint i = 0; i < csums->len; i++) { |
| 1132 | XbNode *csum_tmp = g_ptr_array_index (csums, i); |
| 1133 | xb_string_append_union (checksums_metadata, |
| 1134 | "%s", xb_node_get_text (csum_tmp)); |
| 1135 | } |
| 1136 | for (guint i = 0; i < checksums->len; i++) { |
| 1137 | const gchar *hash_tmp = g_ptr_array_index (checksums, i); |
| 1138 | xb_string_append_union (checksums_device, "%s", hash_tmp); |
| 1139 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1140 | g_set_error (error, |
| 1141 | FWUPD_ERROR, |
| 1142 | FWUPD_ERROR_NOT_FOUND, |
Mario Limonciello | 2a0e20b | 2019-10-14 12:09:35 -0500 | [diff] [blame] | 1143 | "For %s %s expected %s, got %s", |
| 1144 | fu_device_get_name (device), |
Richard Hughes | 35ac072 | 2020-06-23 15:05:14 +0100 | [diff] [blame] | 1145 | fu_device_get_version (device), |
Richard Hughes | 45bbfc9 | 2018-12-12 10:57:08 +0000 | [diff] [blame] | 1146 | checksums_metadata->str, |
| 1147 | checksums_device->str); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1148 | return FALSE; |
| 1149 | } |
| 1150 | |
| 1151 | /* success */ |
| 1152 | return TRUE; |
| 1153 | } |
| 1154 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1155 | static gboolean |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1156 | fu_engine_require_vercmp (XbNode *req, |
| 1157 | const gchar *version, |
| 1158 | FwupdVersionFormat fmt, |
| 1159 | GError **error) |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 1160 | { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1161 | gboolean ret = FALSE; |
| 1162 | gint rc = 0; |
| 1163 | const gchar *tmp = xb_node_get_attr (req, "compare"); |
| 1164 | const gchar *version_req = xb_node_get_attr (req, "version"); |
| 1165 | |
| 1166 | if (g_strcmp0 (tmp, "eq") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1167 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1168 | ret = rc == 0; |
| 1169 | } else if (g_strcmp0 (tmp, "ne") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1170 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1171 | ret = rc != 0; |
| 1172 | } else if (g_strcmp0 (tmp, "lt") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1173 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1174 | ret = rc < 0; |
| 1175 | } else if (g_strcmp0 (tmp, "gt") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1176 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1177 | ret = rc > 0; |
| 1178 | } else if (g_strcmp0 (tmp, "le") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1179 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1180 | ret = rc <= 0; |
| 1181 | } else if (g_strcmp0 (tmp, "ge") == 0) { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1182 | rc = fu_common_vercmp_full (version, version_req, fmt); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1183 | ret = rc >= 0; |
| 1184 | } else if (g_strcmp0 (tmp, "glob") == 0) { |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 1185 | ret = fu_common_fnmatch (version_req, version); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1186 | } else if (g_strcmp0 (tmp, "regex") == 0) { |
| 1187 | ret = g_regex_match_simple (version_req, version, 0, 0); |
| 1188 | } else { |
| 1189 | g_set_error (error, |
| 1190 | FWUPD_ERROR, |
| 1191 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1192 | "failed to compare [%s] and [%s]", |
| 1193 | version_req, |
| 1194 | version); |
| 1195 | return FALSE; |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 1196 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1197 | |
| 1198 | /* set error */ |
| 1199 | if (!ret) { |
| 1200 | g_set_error (error, |
| 1201 | FWUPD_ERROR, |
| 1202 | FWUPD_ERROR_INTERNAL, |
| 1203 | "failed predicate [%s %s %s]", |
| 1204 | version_req, tmp, version); |
| 1205 | } |
| 1206 | return ret; |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1209 | static gboolean |
Richard Hughes | b62c3a4 | 2019-04-08 12:13:47 +0100 | [diff] [blame] | 1210 | fu_engine_check_requirement_not_child (FuEngine *self, XbNode *req, |
| 1211 | FuDevice *device, GError **error) |
| 1212 | { |
| 1213 | GPtrArray *children = fu_device_get_children (device); |
| 1214 | |
| 1215 | /* only <firmware> supported */ |
| 1216 | if (g_strcmp0 (xb_node_get_element (req), "firmware") != 0) { |
| 1217 | g_set_error (error, |
| 1218 | FWUPD_ERROR, |
| 1219 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1220 | "cannot handle not-child %s requirement", |
| 1221 | xb_node_get_element (req)); |
| 1222 | return FALSE; |
| 1223 | } |
| 1224 | |
| 1225 | /* check each child */ |
| 1226 | for (guint i = 0; i < children->len; i++) { |
| 1227 | FuDevice *child = g_ptr_array_index (children, i); |
| 1228 | const gchar *version = fu_device_get_version (child); |
Richard Hughes | ec14e4b | 2019-11-01 12:07:10 +0000 | [diff] [blame] | 1229 | if (version == NULL) { |
| 1230 | g_set_error (error, |
| 1231 | FWUPD_ERROR, |
| 1232 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1233 | "no version provided by %s, child of %s", |
| 1234 | fu_device_get_name (child), |
| 1235 | fu_device_get_name (device)); |
| 1236 | return FALSE; |
| 1237 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1238 | if (fu_engine_require_vercmp (req, version, |
| 1239 | fu_device_get_version_format (child), |
| 1240 | NULL)) { |
Richard Hughes | b62c3a4 | 2019-04-08 12:13:47 +0100 | [diff] [blame] | 1241 | g_set_error (error, |
| 1242 | FWUPD_ERROR, |
| 1243 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1244 | "Not compatible with child device version %s", |
| 1245 | version); |
| 1246 | return FALSE; |
| 1247 | } |
| 1248 | } |
| 1249 | return TRUE; |
| 1250 | } |
| 1251 | |
| 1252 | static gboolean |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 1253 | fu_engine_check_requirement_vendor_id (FuEngine *self, XbNode *req, |
| 1254 | FuDevice *device, GError **error) |
| 1255 | { |
| 1256 | GPtrArray *vendor_ids; |
| 1257 | const gchar *vendor_ids_metadata; |
| 1258 | g_autofree gchar *vendor_ids_device = NULL; |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* devices without vendor IDs should not exist! */ |
| 1261 | vendor_ids = fu_device_get_vendor_ids (device); |
| 1262 | if (vendor_ids->len == 0) { |
| 1263 | g_set_error (error, |
| 1264 | FWUPD_ERROR, |
| 1265 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1266 | "device [%s] has no vendor ID", |
| 1267 | fu_device_get_id (device)); |
| 1268 | return FALSE; |
| 1269 | } |
| 1270 | |
| 1271 | /* metadata with empty vendor IDs should not exist! */ |
| 1272 | vendor_ids_metadata = xb_node_get_attr (req, "version"); |
| 1273 | if (vendor_ids_metadata == NULL) { |
| 1274 | g_set_error_literal (error, |
| 1275 | FWUPD_ERROR, |
| 1276 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1277 | "metadata has no vendor ID"); |
| 1278 | return FALSE; |
| 1279 | } |
| 1280 | |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 1281 | /* it is always safe to use a regex, even for simple strings */ |
Richard Hughes | e52e1b4 | 2021-01-26 09:59:15 +0000 | [diff] [blame] | 1282 | vendor_ids_device = fu_common_strjoin_array ("|", vendor_ids); |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 1283 | if (!g_regex_match_simple (vendor_ids_metadata, vendor_ids_device, 0, 0)) { |
| 1284 | g_set_error (error, |
| 1285 | FWUPD_ERROR, |
| 1286 | FWUPD_ERROR_INVALID_FILE, |
| 1287 | "Not compatible with vendor %s: got %s", |
| 1288 | vendor_ids_device, |
| 1289 | vendor_ids_metadata); |
| 1290 | return FALSE; |
| 1291 | } |
| 1292 | |
| 1293 | /* success */ |
| 1294 | return TRUE; |
| 1295 | } |
| 1296 | |
| 1297 | static gboolean |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 1298 | fu_engine_check_requirement_firmware (FuEngine *self, XbNode *req, FuDevice *device, |
| 1299 | FwupdInstallFlags flags, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1300 | { |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1301 | guint64 depth; |
| 1302 | g_autoptr(FuDevice) device_actual = g_object_ref (device); |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1303 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1304 | |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1305 | /* look at the parent device */ |
| 1306 | depth = xb_node_get_attr_as_uint (req, "depth"); |
| 1307 | if (depth != G_MAXUINT64) { |
| 1308 | for (guint64 i = 0; i < depth; i++) { |
| 1309 | FuDevice *device_tmp = fu_device_get_parent (device_actual); |
| 1310 | if (device_actual == NULL) { |
| 1311 | g_set_error (error, |
| 1312 | FWUPD_ERROR, |
| 1313 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1314 | "No parent device for %s " |
| 1315 | "(%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT ")", |
| 1316 | fu_device_get_name (device_actual), i, depth); |
| 1317 | return FALSE; |
| 1318 | } |
| 1319 | g_set_object (&device_actual, device_tmp); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1323 | /* old firmware version */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1324 | if (xb_node_get_text (req) == NULL) { |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1325 | const gchar *version = fu_device_get_version (device_actual); |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1326 | if (!fu_engine_require_vercmp (req, version, |
| 1327 | fu_device_get_version_format (device_actual), |
| 1328 | &error_local)) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1329 | if (g_strcmp0 (xb_node_get_attr (req, "compare"), "ge") == 0) { |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1330 | g_set_error (error, |
| 1331 | FWUPD_ERROR, |
| 1332 | FWUPD_ERROR_INVALID_FILE, |
| 1333 | "Not compatible with firmware version %s, requires >= %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1334 | version, xb_node_get_attr (req, "version")); |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1335 | } else { |
| 1336 | g_set_error (error, |
| 1337 | FWUPD_ERROR, |
| 1338 | FWUPD_ERROR_INVALID_FILE, |
| 1339 | "Not compatible with firmware version: %s", |
| 1340 | error_local->message); |
| 1341 | } |
| 1342 | return FALSE; |
| 1343 | } |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1344 | return TRUE; |
| 1345 | } |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1346 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1347 | /* bootloader version */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1348 | if (g_strcmp0 (xb_node_get_text (req), "bootloader") == 0) { |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1349 | const gchar *version = fu_device_get_version_bootloader (device_actual); |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1350 | if (!fu_engine_require_vercmp (req, version, |
| 1351 | fu_device_get_version_format (device_actual), |
| 1352 | &error_local)) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1353 | if (g_strcmp0 (xb_node_get_attr (req, "compare"), "ge") == 0) { |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1354 | g_set_error (error, |
| 1355 | FWUPD_ERROR, |
Mario Limonciello | c23e612 | 2019-12-12 14:30:27 -0600 | [diff] [blame] | 1356 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1357 | "Not compatible with bootloader version %s, requires >= %s", |
Mario Limonciello | c23e612 | 2019-12-12 14:30:27 -0600 | [diff] [blame] | 1358 | version, xb_node_get_attr (req, "version")); |
| 1359 | |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1360 | } else { |
Mario Limonciello | c23e612 | 2019-12-12 14:30:27 -0600 | [diff] [blame] | 1361 | g_debug ("Bootloader is not compatible: %s", error_local->message); |
| 1362 | g_set_error_literal (error, |
| 1363 | FWUPD_ERROR, |
| 1364 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1365 | "Bootloader is not compatible"); |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1366 | } |
| 1367 | return FALSE; |
| 1368 | } |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1369 | return TRUE; |
| 1370 | } |
Richard Hughes | 88adcbe | 2017-11-21 14:33:56 +0000 | [diff] [blame] | 1371 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1372 | /* vendor ID */ |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 1373 | if (g_strcmp0 (xb_node_get_text (req), "vendor-id") == 0) { |
| 1374 | if (flags & FWUPD_INSTALL_FLAG_IGNORE_VID_PID) |
| 1375 | return TRUE; |
| 1376 | return fu_engine_check_requirement_vendor_id (self, req, device_actual, error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1377 | } |
| 1378 | |
Richard Hughes | b62c3a4 | 2019-04-08 12:13:47 +0100 | [diff] [blame] | 1379 | /* child version */ |
| 1380 | if (g_strcmp0 (xb_node_get_text (req), "not-child") == 0) |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1381 | return fu_engine_check_requirement_not_child (self, req, device_actual, error); |
Richard Hughes | b62c3a4 | 2019-04-08 12:13:47 +0100 | [diff] [blame] | 1382 | |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1383 | /* another device */ |
Richard Hughes | 592baed | 2019-02-03 18:30:24 +0000 | [diff] [blame] | 1384 | if (fwupd_guid_is_valid (xb_node_get_text (req))) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1385 | const gchar *guid = xb_node_get_text (req); |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1386 | const gchar *version; |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* find if the other device exists */ |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1389 | if (depth == G_MAXUINT64) { |
| 1390 | g_autoptr(FuDevice) device_tmp = NULL; |
| 1391 | device_tmp = fu_device_list_get_by_guid (self->device_list, guid, error); |
| 1392 | if (device_tmp == NULL) |
| 1393 | return FALSE; |
| 1394 | g_set_object (&device_actual, device_tmp); |
| 1395 | |
| 1396 | /* verify the parent device has the GUID */ |
| 1397 | } else { |
| 1398 | if (!fu_device_has_guid (device_actual, guid)) { |
| 1399 | g_set_error (error, |
| 1400 | FWUPD_ERROR, |
| 1401 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1402 | "No GUID of %s on parent device %s", |
| 1403 | guid, fu_device_get_name (device_actual)); |
| 1404 | return FALSE; |
| 1405 | } |
| 1406 | } |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1407 | |
| 1408 | /* get the version of the other device */ |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1409 | version = fu_device_get_version (device_actual); |
Richard Hughes | aaf0ce7 | 2019-07-16 08:43:57 +0100 | [diff] [blame] | 1410 | if (version != NULL && |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1411 | xb_node_get_attr (req, "compare") != NULL && |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1412 | !fu_engine_require_vercmp (req, version, |
| 1413 | fu_device_get_version_format (device_actual), |
| 1414 | &error_local)) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1415 | if (g_strcmp0 (xb_node_get_attr (req, "compare"), "ge") == 0) { |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1416 | g_set_error (error, |
| 1417 | FWUPD_ERROR, |
| 1418 | FWUPD_ERROR_INVALID_FILE, |
| 1419 | "Not compatible with %s version %s, requires >= %s", |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1420 | fu_device_get_name (device_actual), |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1421 | version, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1422 | xb_node_get_attr (req, "version")); |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1423 | } else { |
| 1424 | g_set_error (error, |
| 1425 | FWUPD_ERROR, |
| 1426 | FWUPD_ERROR_INVALID_FILE, |
| 1427 | "Not compatible with %s: %s", |
Richard Hughes | f7006d2 | 2019-11-14 13:42:52 +0000 | [diff] [blame] | 1428 | fu_device_get_name (device_actual), |
Richard Hughes | 12c8499 | 2018-10-02 11:07:28 +0100 | [diff] [blame] | 1429 | error_local->message); |
| 1430 | } |
| 1431 | return FALSE; |
| 1432 | } |
| 1433 | return TRUE; |
| 1434 | |
| 1435 | } |
| 1436 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1437 | /* not supported */ |
| 1438 | g_set_error (error, |
| 1439 | FWUPD_ERROR, |
| 1440 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1441 | "cannot handle firmware requirement '%s'", |
| 1442 | xb_node_get_text (req)); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1443 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1444 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1445 | |
| 1446 | static gboolean |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1447 | fu_engine_check_requirement_id (FuEngine *self, XbNode *req, GError **error) |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1448 | { |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1449 | g_autoptr(GError) error_local = NULL; |
| 1450 | const gchar *version = g_hash_table_lookup (self->runtime_versions, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1451 | xb_node_get_text (req)); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1452 | if (version == NULL) { |
| 1453 | g_set_error (error, |
| 1454 | FWUPD_ERROR, |
| 1455 | FWUPD_ERROR_NOT_FOUND, |
| 1456 | "no version available for %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1457 | xb_node_get_text (req)); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1458 | return FALSE; |
| 1459 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 1460 | if (!fu_engine_require_vercmp (req, version, FWUPD_VERSION_FORMAT_UNKNOWN, &error_local)) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1461 | if (g_strcmp0 (xb_node_get_attr (req, "compare"), "ge") == 0) { |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1462 | g_set_error (error, |
| 1463 | FWUPD_ERROR, |
| 1464 | FWUPD_ERROR_INVALID_FILE, |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1465 | "Not compatible with %s version %s, requires >= %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1466 | xb_node_get_text (req), version, |
| 1467 | xb_node_get_attr (req, "version")); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1468 | } else { |
| 1469 | g_set_error (error, |
| 1470 | FWUPD_ERROR, |
| 1471 | FWUPD_ERROR_INVALID_FILE, |
| 1472 | "Not compatible with %s version: %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1473 | xb_node_get_text (req), error_local->message); |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1474 | } |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1475 | return FALSE; |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 1478 | g_debug ("requirement %s %s %s -> %s passed", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1479 | xb_node_get_attr (req, "version"), |
| 1480 | xb_node_get_attr (req, "compare"), |
| 1481 | version, xb_node_get_text (req)); |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1482 | return TRUE; |
| 1483 | } |
Richard Hughes | 2ec78d6 | 2017-11-03 21:48:54 +0000 | [diff] [blame] | 1484 | |
| 1485 | static gboolean |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1486 | fu_engine_check_requirement_hardware (FuEngine *self, XbNode *req, GError **error) |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1487 | { |
Richard Hughes | 3d71c16 | 2018-04-30 16:40:44 +0100 | [diff] [blame] | 1488 | g_auto(GStrv) hwid_split = NULL; |
| 1489 | |
| 1490 | /* split and treat as OR */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1491 | hwid_split = g_strsplit (xb_node_get_text (req), "|", -1); |
Richard Hughes | 3d71c16 | 2018-04-30 16:40:44 +0100 | [diff] [blame] | 1492 | for (guint i = 0; hwid_split[i] != NULL; i++) { |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 1493 | if (fu_context_has_hwid_guid (self->ctx, hwid_split[i])) { |
Richard Hughes | 3d71c16 | 2018-04-30 16:40:44 +0100 | [diff] [blame] | 1494 | g_debug ("HWID provided %s", hwid_split[i]); |
| 1495 | return TRUE; |
| 1496 | } |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1497 | } |
Richard Hughes | 3d71c16 | 2018-04-30 16:40:44 +0100 | [diff] [blame] | 1498 | |
| 1499 | /* nothing matched */ |
| 1500 | g_set_error (error, |
| 1501 | FWUPD_ERROR, |
| 1502 | FWUPD_ERROR_INVALID_FILE, |
| 1503 | "no HWIDs matched %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1504 | xb_node_get_text (req)); |
Richard Hughes | 3d71c16 | 2018-04-30 16:40:44 +0100 | [diff] [blame] | 1505 | return FALSE; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static gboolean |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1509 | fu_engine_check_requirement_client (FuEngine *self, |
| 1510 | FuEngineRequest *request, |
| 1511 | XbNode *req, |
| 1512 | GError **error) |
| 1513 | { |
| 1514 | FwupdFeatureFlags flags; |
| 1515 | g_auto(GStrv) feature_split = NULL; |
| 1516 | |
| 1517 | /* split and treat as AND */ |
| 1518 | feature_split = g_strsplit (xb_node_get_text (req), "|", -1); |
| 1519 | flags = fu_engine_request_get_feature_flags (request); |
| 1520 | for (guint i = 0; feature_split[i] != NULL; i++) { |
| 1521 | FwupdFeatureFlags flag = fwupd_feature_flag_from_string (feature_split[i]); |
| 1522 | |
Richard Hughes | 0ec634e | 2020-12-07 20:55:18 +0000 | [diff] [blame] | 1523 | /* not recognized */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1524 | if (flag == FWUPD_FEATURE_FLAG_LAST) { |
| 1525 | g_set_error (error, |
| 1526 | FWUPD_ERROR, |
| 1527 | FWUPD_ERROR_NOT_FOUND, |
| 1528 | "client requirement %s unknown", |
| 1529 | feature_split[i]); |
| 1530 | return FALSE; |
| 1531 | } |
| 1532 | |
| 1533 | /* not supported */ |
| 1534 | if ((flags & flag) == 0) { |
| 1535 | g_set_error (error, |
| 1536 | FWUPD_ERROR, |
| 1537 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1538 | "client requirement %s not supported", |
| 1539 | feature_split[i]); |
| 1540 | return FALSE; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /* success */ |
| 1545 | return TRUE; |
| 1546 | } |
| 1547 | |
| 1548 | static gboolean |
| 1549 | fu_engine_check_requirement (FuEngine *self, |
| 1550 | FuEngineRequest *request, |
| 1551 | XbNode *req, |
| 1552 | FuDevice *device, |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 1553 | FwupdInstallFlags flags, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1554 | GError **error) |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1555 | { |
| 1556 | /* ensure component requirement */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1557 | if (g_strcmp0 (xb_node_get_element (req), "id") == 0) |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1558 | return fu_engine_check_requirement_id (self, req, error); |
| 1559 | |
| 1560 | /* ensure firmware requirement */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1561 | if (g_strcmp0 (xb_node_get_element (req), "firmware") == 0) { |
Richard Hughes | 881f624 | 2018-08-06 11:03:06 +0100 | [diff] [blame] | 1562 | if (device == NULL) |
| 1563 | return TRUE; |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 1564 | return fu_engine_check_requirement_firmware (self, req, device, |
| 1565 | flags, error); |
Richard Hughes | 881f624 | 2018-08-06 11:03:06 +0100 | [diff] [blame] | 1566 | } |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1567 | |
| 1568 | /* ensure hardware requirement */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1569 | if (g_strcmp0 (xb_node_get_element (req), "hardware") == 0) |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1570 | return fu_engine_check_requirement_hardware (self, req, error); |
| 1571 | |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1572 | /* ensure client requirement */ |
| 1573 | if (g_strcmp0 (xb_node_get_element (req), "client") == 0) |
| 1574 | return fu_engine_check_requirement_client (self, request, req, error); |
| 1575 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1576 | /* not supported */ |
| 1577 | g_set_error (error, |
| 1578 | FWUPD_ERROR, |
| 1579 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1580 | "cannot handle requirement type %s", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1581 | xb_node_get_element (req)); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1582 | return FALSE; |
| 1583 | } |
| 1584 | |
| 1585 | gboolean |
Mario Limonciello | 381c27c | 2020-10-23 16:32:26 -0500 | [diff] [blame] | 1586 | fu_engine_check_trust (FuInstallTask *task, GError **error) |
| 1587 | { |
| 1588 | #ifndef HAVE_POLKIT |
| 1589 | if ((fu_install_task_get_trust_flags (task) & FWUPD_TRUST_FLAG_PAYLOAD) == 0) { |
| 1590 | g_set_error_literal (error, |
| 1591 | FWUPD_ERROR, |
| 1592 | FWUPD_ERROR_INVALID_FILE, |
| 1593 | "archive signature missing or not trusted"); |
| 1594 | return FALSE; |
| 1595 | } |
| 1596 | #endif |
| 1597 | return TRUE; |
| 1598 | } |
| 1599 | |
| 1600 | gboolean |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1601 | fu_engine_check_requirements (FuEngine *self, |
| 1602 | FuEngineRequest *request, |
| 1603 | FuInstallTask *task, |
| 1604 | FwupdInstallFlags flags, |
| 1605 | GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1606 | { |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 1607 | FuDevice *device = fu_install_task_get_device (task); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1608 | g_autoptr(GError) error_local = NULL; |
| 1609 | g_autoptr(GPtrArray) reqs = NULL; |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 1610 | |
| 1611 | /* all install task checks require a device */ |
| 1612 | if (device != NULL) { |
| 1613 | if (!fu_install_task_check_requirements (task, flags, error)) |
| 1614 | return FALSE; |
| 1615 | } |
| 1616 | |
| 1617 | /* do engine checks */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1618 | reqs = xb_node_query (fu_install_task_get_component (task), |
| 1619 | "requires/*", 0, &error_local); |
Mario Limonciello | 7db8e88 | 2020-10-20 08:52:51 -0500 | [diff] [blame] | 1620 | if (reqs != NULL) { |
| 1621 | for (guint i = 0; i < reqs->len; i++) { |
| 1622 | XbNode *req = g_ptr_array_index (reqs, i); |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 1623 | if (!fu_engine_check_requirement (self, request, |
| 1624 | req, device, |
| 1625 | flags, error)) |
Mario Limonciello | 7db8e88 | 2020-10-20 08:52:51 -0500 | [diff] [blame] | 1626 | return FALSE; |
| 1627 | } |
| 1628 | } else if (!g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) && |
| 1629 | !g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) { |
| 1630 | g_propagate_error (error, g_steal_pointer (&error_local)); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 1631 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1632 | } |
Mario Limonciello | 7db8e88 | 2020-10-20 08:52:51 -0500 | [diff] [blame] | 1633 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1634 | return TRUE; |
| 1635 | } |
| 1636 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1637 | void |
| 1638 | fu_engine_idle_reset (FuEngine *self) |
| 1639 | { |
| 1640 | fu_idle_reset (self->idle); |
| 1641 | } |
| 1642 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1643 | static gchar * |
Richard Hughes | 59c2ebe | 2018-01-12 09:47:40 +0000 | [diff] [blame] | 1644 | fu_engine_get_boot_time (void) |
| 1645 | { |
| 1646 | g_autofree gchar *buf = NULL; |
| 1647 | g_auto(GStrv) lines = NULL; |
| 1648 | if (!g_file_get_contents ("/proc/stat", &buf, NULL, NULL)) |
| 1649 | return NULL; |
| 1650 | lines = g_strsplit (buf, "\n", -1); |
| 1651 | for (guint i = 0; lines[i] != NULL; i++) { |
| 1652 | if (g_str_has_prefix (lines[i], "btime ")) |
| 1653 | return g_strdup (lines[i] + 6); |
| 1654 | } |
| 1655 | return NULL; |
| 1656 | } |
| 1657 | |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1658 | static gboolean |
| 1659 | fu_engine_get_report_metadata_os_release (GHashTable *hash, GError **error) |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1660 | { |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1661 | g_autoptr(GHashTable) os_release = NULL; |
| 1662 | struct { |
| 1663 | const gchar *key; |
| 1664 | const gchar *val; |
| 1665 | } distro_kv[] = { |
| 1666 | { "ID", "DistroId" }, |
| 1667 | { "VERSION_ID", "DistroVersion" }, |
| 1668 | { "VARIANT_ID", "DistroVariant" }, |
| 1669 | { NULL, NULL } |
| 1670 | }; |
| 1671 | |
| 1672 | /* get all required os-release keys */ |
| 1673 | os_release = fwupd_get_os_release (error); |
| 1674 | if (os_release == NULL) |
| 1675 | return FALSE; |
| 1676 | for (guint i = 0; distro_kv[i].key != NULL; i++) { |
| 1677 | const gchar *tmp = g_hash_table_lookup (os_release, distro_kv[i].key); |
| 1678 | if (tmp != NULL) { |
| 1679 | g_hash_table_insert (hash, |
| 1680 | g_strdup (distro_kv[i].val), |
| 1681 | g_strdup (tmp)); |
| 1682 | } |
| 1683 | } |
| 1684 | return TRUE; |
| 1685 | } |
| 1686 | |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1687 | static gboolean |
| 1688 | fu_engine_get_report_metadata_kernel_cmdline (GHashTable *hash, GError **error) |
| 1689 | { |
| 1690 | gsize bufsz = 0; |
| 1691 | g_autofree gchar *buf = NULL; |
| 1692 | const gchar *ignore[] = { |
| 1693 | "", |
Richard Hughes | c05ac2d | 2020-05-20 22:35:09 +0100 | [diff] [blame] | 1694 | "auto", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1695 | "boot", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1696 | "BOOT_IMAGE", |
| 1697 | "console", |
| 1698 | "cryptdevice", |
Richard Hughes | 7f7f0ae | 2020-06-15 12:10:58 +0100 | [diff] [blame] | 1699 | "cryptkey", |
| 1700 | "earlycon", |
| 1701 | "earlyprintk", |
| 1702 | "ether", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1703 | "initrd", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1704 | "ip", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1705 | "LANG", |
| 1706 | "loglevel", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1707 | "luks.key", |
| 1708 | "luks.name", |
| 1709 | "luks.options", |
| 1710 | "luks.uuid", |
| 1711 | "mount.usr", |
| 1712 | "mount.usrflags", |
| 1713 | "mount.usrfstype", |
Richard Hughes | 7f7f0ae | 2020-06-15 12:10:58 +0100 | [diff] [blame] | 1714 | "netdev", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1715 | "netroot", |
| 1716 | "nfsaddrs", |
| 1717 | "nfs.nfs4_unique_id", |
| 1718 | "nfsroot", |
Richard Hughes | c05ac2d | 2020-05-20 22:35:09 +0100 | [diff] [blame] | 1719 | "noplymouth", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1720 | "ostree", |
| 1721 | "quiet", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1722 | "rd.dm.uuid", |
| 1723 | "rd.luks.allow-discards", |
| 1724 | "rd.luks.key", |
| 1725 | "rd.luks.name", |
| 1726 | "rd.luks.options", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1727 | "rd.luks.uuid", |
| 1728 | "rd.lvm.lv", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1729 | "rd.lvm.vg", |
Richard Hughes | 4a62329 | 2020-05-21 20:58:28 +0100 | [diff] [blame] | 1730 | "rd.md.uuid", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1731 | "rd.systemd.mask", |
| 1732 | "rd.systemd.wants", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1733 | "resume", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1734 | "resumeflags", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1735 | "rhgb", |
| 1736 | "ro", |
| 1737 | "root", |
| 1738 | "rootflags", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1739 | "roothash", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1740 | "rw", |
| 1741 | "showopts", |
| 1742 | "splash", |
| 1743 | "swap", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1744 | "systemd.mask", |
Richard Hughes | 7f7f0ae | 2020-06-15 12:10:58 +0100 | [diff] [blame] | 1745 | "systemd.unit", |
Richard Hughes | d94ce34 | 2020-06-12 17:13:14 +0100 | [diff] [blame] | 1746 | "systemd.verity_root_data", |
| 1747 | "systemd.verity_root_hash", |
| 1748 | "systemd.wants", |
Richard Hughes | c05ac2d | 2020-05-20 22:35:09 +0100 | [diff] [blame] | 1749 | "verbose", |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1750 | "vt.handoff", |
| 1751 | "zfs", |
| 1752 | NULL, /* last entry */ |
| 1753 | }; |
| 1754 | |
| 1755 | /* get a PII-safe kernel command line */ |
| 1756 | if (!g_file_get_contents ("/proc/cmdline", &buf, &bufsz, error)) |
| 1757 | return FALSE; |
| 1758 | if (bufsz > 0) { |
| 1759 | g_auto(GStrv) tokens = fu_common_strnsplit (buf, bufsz - 1, " ", -1); |
| 1760 | g_autoptr(GString) cmdline_safe = g_string_new (NULL); |
| 1761 | for (guint i = 0; tokens[i] != NULL; i++) { |
Jan Tojnar | a8b3f9e | 2020-11-07 16:57:12 +0100 | [diff] [blame] | 1762 | g_auto(GStrv) kv = NULL; |
| 1763 | if (strlen (tokens[i]) == 0) |
| 1764 | continue; |
| 1765 | kv = g_strsplit (tokens[i], "=", 2); |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1766 | if (g_strv_contains (ignore, kv[0])) |
| 1767 | continue; |
| 1768 | if (cmdline_safe->len > 0) |
| 1769 | g_string_append (cmdline_safe, " "); |
| 1770 | g_string_append (cmdline_safe, tokens[i]); |
| 1771 | } |
| 1772 | if (cmdline_safe->len > 0) { |
| 1773 | g_hash_table_insert (hash, |
| 1774 | g_strdup ("KernelCmdline"), |
| 1775 | g_strdup (cmdline_safe->str)); |
| 1776 | } |
| 1777 | } |
| 1778 | return TRUE; |
| 1779 | } |
| 1780 | |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1781 | GHashTable * |
| 1782 | fu_engine_get_report_metadata (FuEngine *self, GError **error) |
| 1783 | { |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1784 | const gchar *tmp; |
Richard Hughes | 59c2ebe | 2018-01-12 09:47:40 +0000 | [diff] [blame] | 1785 | gchar *btime; |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 1786 | #ifdef HAVE_UTSNAME_H |
Mario Limonciello | ed1ac2a | 2018-04-17 14:43:28 -0500 | [diff] [blame] | 1787 | struct utsname name_tmp; |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 1788 | #endif |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1789 | g_autoptr(GHashTable) hash = NULL; |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 1790 | g_autoptr(GList) compile_keys = g_hash_table_get_keys (self->compile_versions); |
| 1791 | g_autoptr(GList) runtime_keys = g_hash_table_get_keys (self->runtime_versions); |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1792 | |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 1793 | /* convert all the runtime and compile-time versions */ |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1794 | hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 1795 | for (GList *l = compile_keys; l != NULL; l = l->next) { |
| 1796 | const gchar *id = l->data; |
| 1797 | const gchar *version = g_hash_table_lookup (self->compile_versions, id); |
| 1798 | g_hash_table_insert (hash, |
| 1799 | g_strdup_printf ("CompileVersion(%s)", id), |
| 1800 | g_strdup (version)); |
| 1801 | } |
| 1802 | for (GList *l = runtime_keys; l != NULL; l = l->next) { |
| 1803 | const gchar *id = l->data; |
| 1804 | const gchar *version = g_hash_table_lookup (self->runtime_versions, id); |
| 1805 | g_hash_table_insert (hash, |
| 1806 | g_strdup_printf ("RuntimeVersion(%s)", id), |
| 1807 | g_strdup (version)); |
| 1808 | } |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1809 | if (!fu_engine_get_report_metadata_os_release (hash, error)) |
| 1810 | return NULL; |
Richard Hughes | dc867dd | 2020-05-20 18:47:20 +0100 | [diff] [blame] | 1811 | if (!fu_engine_get_report_metadata_kernel_cmdline (hash, error)) |
| 1812 | return NULL; |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1813 | |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1814 | /* DMI data */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 1815 | tmp = fu_context_get_hwid_value (self->ctx, FU_HWIDS_KEY_PRODUCT_NAME); |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1816 | if (tmp != NULL) |
| 1817 | g_hash_table_insert (hash, g_strdup ("HostProduct"), g_strdup (tmp)); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 1818 | tmp = fu_context_get_hwid_value (self->ctx, FU_HWIDS_KEY_FAMILY); |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1819 | if (tmp != NULL) |
| 1820 | g_hash_table_insert (hash, g_strdup ("HostFamily"), g_strdup (tmp)); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 1821 | tmp = fu_context_get_hwid_value (self->ctx, FU_HWIDS_KEY_PRODUCT_SKU); |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1822 | if (tmp != NULL) |
| 1823 | g_hash_table_insert (hash, g_strdup ("HostSku"), g_strdup (tmp)); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 1824 | tmp = fu_context_get_hwid_value (self->ctx, FU_HWIDS_KEY_MANUFACTURER); |
Richard Hughes | a778ac9 | 2020-05-20 18:44:24 +0100 | [diff] [blame] | 1825 | if (tmp != NULL) |
| 1826 | g_hash_table_insert (hash, g_strdup ("HostVendor"), g_strdup (tmp)); |
| 1827 | |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1828 | /* kernel version is often important for debugging failures */ |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 1829 | #ifdef HAVE_UTSNAME_H |
Mario Limonciello | ed1ac2a | 2018-04-17 14:43:28 -0500 | [diff] [blame] | 1830 | memset (&name_tmp, 0, sizeof (struct utsname)); |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1831 | if (uname (&name_tmp) >= 0) { |
| 1832 | g_hash_table_insert (hash, |
| 1833 | g_strdup ("CpuArchitecture"), |
| 1834 | g_strdup (name_tmp.machine)); |
Richard Hughes | 08bb922 | 2020-05-20 18:43:59 +0100 | [diff] [blame] | 1835 | g_hash_table_insert (hash, |
| 1836 | g_strdup ("KernelVersion"), |
| 1837 | g_strdup (name_tmp.release)); |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1838 | } |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 1839 | #endif |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1840 | |
Richard Hughes | 59c2ebe | 2018-01-12 09:47:40 +0000 | [diff] [blame] | 1841 | /* add the kernel boot time so we can detect a reboot */ |
| 1842 | btime = fu_engine_get_boot_time (); |
| 1843 | if (btime != NULL) |
| 1844 | g_hash_table_insert (hash, g_strdup ("BootTime"), btime); |
| 1845 | |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 1846 | return g_steal_pointer (&hash); |
Richard Hughes | 473c520 | 2018-01-11 21:06:16 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1849 | /** |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1850 | * fu_engine_composite_prepare: |
| 1851 | * @self: A #FuEngine |
| 1852 | * @devices: (element-type #FuDevice): devices that will be updated |
| 1853 | * @error: A #GError, or %NULL |
| 1854 | * |
| 1855 | * Calls into the plugin loader, informing each plugin of the pending upgrade(s). |
| 1856 | * |
| 1857 | * Any failure in any plugin will abort all of the actions before they are started. |
| 1858 | * |
| 1859 | * Returns: %TRUE for success |
| 1860 | **/ |
| 1861 | gboolean |
| 1862 | fu_engine_composite_prepare (FuEngine *self, GPtrArray *devices, GError **error) |
| 1863 | { |
| 1864 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
| 1865 | for (guint j = 0; j < plugins->len; j++) { |
| 1866 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 1867 | if (!fu_plugin_runner_composite_prepare (plugin_tmp, devices, error)) |
| 1868 | return FALSE; |
| 1869 | } |
| 1870 | return TRUE; |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * fu_engine_composite_cleanup: |
| 1875 | * @self: A #FuEngine |
| 1876 | * @devices: (element-type #FuDevice): devices that will be updated |
| 1877 | * @error: A #GError, or %NULL |
| 1878 | * |
| 1879 | * Calls into the plugin loader, informing each plugin of the pending upgrade(s). |
| 1880 | * |
| 1881 | * Returns: %TRUE for success |
| 1882 | **/ |
| 1883 | gboolean |
| 1884 | fu_engine_composite_cleanup (FuEngine *self, GPtrArray *devices, GError **error) |
| 1885 | { |
| 1886 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
| 1887 | for (guint j = 0; j < plugins->len; j++) { |
| 1888 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 1889 | if (!fu_plugin_runner_composite_cleanup (plugin_tmp, devices, error)) |
| 1890 | return FALSE; |
| 1891 | } |
| 1892 | return TRUE; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * fu_engine_install_tasks: |
| 1897 | * @self: A #FuEngine |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1898 | * @request: A #FuEngineRequest |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1899 | * @install_tasks: (element-type FuInstallTask): A #FuDevice |
| 1900 | * @blob_cab: The #GBytes of the .cab file |
| 1901 | * @flags: The #FwupdInstallFlags, e.g. %FWUPD_DEVICE_FLAG_UPDATABLE |
| 1902 | * @error: A #GError, or %NULL |
| 1903 | * |
| 1904 | * Installs a specific firmware file on one or more install tasks. |
| 1905 | * |
| 1906 | * By this point all the requirements and tests should have been done in |
| 1907 | * fu_engine_check_requirements() so this should not fail before running |
| 1908 | * the plugin loader. |
| 1909 | * |
| 1910 | * Returns: %TRUE for success |
| 1911 | **/ |
| 1912 | gboolean |
| 1913 | fu_engine_install_tasks (FuEngine *self, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1914 | FuEngineRequest *request, |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1915 | GPtrArray *install_tasks, |
| 1916 | GBytes *blob_cab, |
| 1917 | FwupdInstallFlags flags, |
| 1918 | GError **error) |
| 1919 | { |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1920 | g_autoptr(FuIdleLocker) locker = NULL; |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1921 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1922 | g_autoptr(GPtrArray) devices_new = NULL; |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1923 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1924 | /* do not allow auto-shutdown during this time */ |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 1925 | locker = fu_idle_locker_new (self->idle, "update"); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1926 | g_assert (locker != NULL); |
| 1927 | |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1928 | /* notify the plugins about the composite action */ |
| 1929 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1930 | for (guint i = 0; i < install_tasks->len; i++) { |
| 1931 | FuInstallTask *task = g_ptr_array_index (install_tasks, i); |
Richard Hughes | ca6af0b | 2020-04-16 17:08:33 +0100 | [diff] [blame] | 1932 | g_debug ("composite update %u: %s", i + 1, |
| 1933 | fu_device_get_id (fu_install_task_get_device (task))); |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1934 | g_ptr_array_add (devices, g_object_ref (fu_install_task_get_device (task))); |
| 1935 | } |
| 1936 | if (!fu_engine_composite_prepare (self, devices, error)) { |
| 1937 | g_prefix_error (error, "failed to prepare composite action: "); |
| 1938 | return FALSE; |
| 1939 | } |
| 1940 | |
| 1941 | /* all authenticated, so install all the things */ |
| 1942 | for (guint i = 0; i < install_tasks->len; i++) { |
| 1943 | FuInstallTask *task = g_ptr_array_index (install_tasks, i); |
| 1944 | if (!fu_engine_install (self, task, blob_cab, flags, error)) { |
| 1945 | g_autoptr(GError) error_local = NULL; |
| 1946 | if (!fu_engine_composite_cleanup (self, devices, &error_local)) { |
| 1947 | g_warning ("failed to cleanup failed composite action: %s", |
| 1948 | error_local->message); |
| 1949 | } |
| 1950 | return FALSE; |
| 1951 | } |
| 1952 | } |
| 1953 | |
Richard Hughes | 96019e8 | 2019-01-30 11:12:57 +0000 | [diff] [blame] | 1954 | /* set all the device statuses back to unknown */ |
| 1955 | for (guint i = 0; i < install_tasks->len; i++) { |
| 1956 | FuInstallTask *task = g_ptr_array_index (install_tasks, i); |
| 1957 | FuDevice *device = fu_install_task_get_device (task); |
| 1958 | fu_device_set_status (device, FWUPD_STATUS_UNKNOWN); |
| 1959 | } |
| 1960 | |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1961 | /* get a new list of devices in case they replugged */ |
| 1962 | devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1963 | for (guint i = 0; i < devices->len; i++) { |
| 1964 | FuDevice *device; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 1965 | g_autoptr(FuDevice) device_new = NULL; |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1966 | g_autoptr(GError) error_local = NULL; |
| 1967 | device = g_ptr_array_index (devices, i); |
| 1968 | device_new = fu_device_list_get_by_id (self->device_list, |
| 1969 | fu_device_get_id (device), |
| 1970 | &error_local); |
| 1971 | if (device_new == NULL) { |
Mario Limonciello | 769d768 | 2018-09-28 08:43:37 -0500 | [diff] [blame] | 1972 | g_debug ("failed to find new device: %s", |
| 1973 | error_local->message); |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1974 | continue; |
| 1975 | } |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 1976 | g_ptr_array_add (devices_new, g_steal_pointer (&device_new)); |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1979 | /* notify the plugins about the composite action */ |
Mario Limonciello | b6fa473 | 2018-09-07 15:12:33 +0100 | [diff] [blame] | 1980 | if (!fu_engine_composite_cleanup (self, devices_new, error)) { |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1981 | g_prefix_error (error, "failed to cleanup composite action: "); |
| 1982 | return FALSE; |
| 1983 | } |
| 1984 | |
| 1985 | /* success */ |
| 1986 | return TRUE; |
| 1987 | } |
| 1988 | |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 1989 | static FwupdRelease * |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 1990 | fu_engine_create_release_metadata (FuEngine *self, |
| 1991 | FuDevice *device, |
| 1992 | FuPlugin *plugin, |
| 1993 | GError **error) |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 1994 | { |
Richard Hughes | dad3597 | 2019-12-06 11:00:25 +0000 | [diff] [blame] | 1995 | GPtrArray *metadata_sources; |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 1996 | g_autoptr(FwupdRelease) release = fwupd_release_new (); |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 1997 | g_autoptr(GHashTable) metadata_device = NULL; |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 1998 | g_autoptr(GHashTable) metadata_hash = NULL; |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 1999 | |
| 2000 | /* build the version metadata */ |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame] | 2001 | metadata_hash = fu_engine_get_report_metadata (self, error); |
| 2002 | if (metadata_hash == NULL) |
| 2003 | return NULL; |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2004 | fwupd_release_add_metadata (release, metadata_hash); |
Richard Hughes | 1d900f7 | 2020-06-22 15:17:39 +0100 | [diff] [blame] | 2005 | if (fu_plugin_get_report_metadata (plugin) != NULL) |
| 2006 | fwupd_release_add_metadata (release, fu_plugin_get_report_metadata (plugin)); |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 2007 | metadata_device = fu_device_report_metadata_pre (device); |
| 2008 | if (metadata_device != NULL) |
| 2009 | fwupd_release_add_metadata (release, metadata_device); |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2010 | |
Richard Hughes | dad3597 | 2019-12-06 11:00:25 +0000 | [diff] [blame] | 2011 | /* allow other plugins to contribute metadata too */ |
| 2012 | metadata_sources = fu_plugin_get_rules (plugin, FU_PLUGIN_RULE_METADATA_SOURCE); |
Richard Hughes | 11c5941 | 2020-06-22 15:29:48 +0100 | [diff] [blame] | 2013 | if (metadata_sources != NULL) { |
| 2014 | for (guint i = 0; i < metadata_sources->len; i++) { |
| 2015 | FuPlugin *plugin_tmp; |
| 2016 | const gchar *plugin_name = g_ptr_array_index (metadata_sources, i); |
| 2017 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | dad3597 | 2019-12-06 11:00:25 +0000 | [diff] [blame] | 2018 | |
Richard Hughes | 11c5941 | 2020-06-22 15:29:48 +0100 | [diff] [blame] | 2019 | plugin_tmp = fu_plugin_list_find_by_name (self->plugin_list, |
| 2020 | plugin_name, |
| 2021 | &error_local); |
| 2022 | if (plugin_tmp == NULL) { |
| 2023 | g_warning ("could not add metadata for %s: %s", |
| 2024 | plugin_name, |
| 2025 | error_local->message); |
| 2026 | continue; |
| 2027 | } |
| 2028 | if (fu_plugin_get_report_metadata (plugin_tmp) != NULL) { |
| 2029 | fwupd_release_add_metadata (release, |
| 2030 | fu_plugin_get_report_metadata (plugin_tmp)); |
| 2031 | } |
Richard Hughes | 1d900f7 | 2020-06-22 15:17:39 +0100 | [diff] [blame] | 2032 | } |
Richard Hughes | dad3597 | 2019-12-06 11:00:25 +0000 | [diff] [blame] | 2033 | } |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2034 | return g_steal_pointer (&release); |
| 2035 | } |
| 2036 | |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 2037 | static gboolean |
| 2038 | fu_engine_is_running_offline (FuEngine *self) |
| 2039 | { |
| 2040 | #ifdef HAVE_SYSTEMD |
| 2041 | g_autofree gchar *default_target = NULL; |
| 2042 | g_autoptr(GError) error = NULL; |
| 2043 | default_target = fu_systemd_get_default_target (&error); |
| 2044 | if (default_target == NULL) { |
| 2045 | g_warning ("failed to get default.target: %s", error->message); |
| 2046 | return FALSE; |
| 2047 | } |
| 2048 | return g_strcmp0 (default_target, "system-update.target") == 0; |
| 2049 | #else |
| 2050 | return FALSE; |
| 2051 | #endif |
| 2052 | } |
| 2053 | |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2054 | static gboolean |
| 2055 | fu_engine_offline_setup (GError **error) |
| 2056 | { |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 2057 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2058 | gint rc; |
| 2059 | g_autofree gchar *filename = NULL; |
| 2060 | g_autofree gchar *symlink_target = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 2061 | g_autofree gchar *trigger = fu_common_get_path (FU_PATH_KIND_OFFLINE_TRIGGER); |
| 2062 | |
| 2063 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2064 | |
| 2065 | /* does already exist */ |
| 2066 | filename = fu_common_realpath (trigger, NULL); |
| 2067 | if (g_strcmp0 (filename, symlink_target) == 0) { |
| 2068 | g_debug ("%s already points to %s, skipping creation", |
| 2069 | trigger, symlink_target); |
| 2070 | return TRUE; |
| 2071 | } |
| 2072 | |
| 2073 | /* create symlink for the systemd-system-update-generator */ |
| 2074 | rc = symlink (symlink_target, trigger); |
| 2075 | if (rc < 0) { |
| 2076 | g_set_error (error, |
| 2077 | FWUPD_ERROR, |
| 2078 | FWUPD_ERROR_INTERNAL, |
| 2079 | "Failed to create symlink %s to %s: %s", |
| 2080 | trigger, symlink_target, strerror (errno)); |
| 2081 | return FALSE; |
| 2082 | } |
| 2083 | return TRUE; |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 2084 | #else |
| 2085 | g_set_error (error, |
| 2086 | FWUPD_ERROR, |
| 2087 | FWUPD_ERROR_NOT_SUPPORTED, |
| 2088 | "Not supported as <gio-unix.h> not available"); |
| 2089 | return FALSE; |
| 2090 | #endif |
| 2091 | |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static gboolean |
| 2095 | fu_engine_offline_invalidate (GError **error) |
| 2096 | { |
| 2097 | g_autofree gchar *trigger = fu_common_get_path (FU_PATH_KIND_OFFLINE_TRIGGER); |
| 2098 | g_autoptr(GError) error_local = NULL; |
| 2099 | g_autoptr(GFile) file1 = NULL; |
| 2100 | |
| 2101 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2102 | |
| 2103 | file1 = g_file_new_for_path (trigger); |
| 2104 | if (!g_file_query_exists (file1, NULL)) |
| 2105 | return TRUE; |
| 2106 | if (!g_file_delete (file1, NULL, &error_local)) { |
| 2107 | g_set_error (error, |
| 2108 | FWUPD_ERROR, |
| 2109 | FWUPD_ERROR_INTERNAL, |
| 2110 | "Cannot delete %s: %s", |
| 2111 | trigger, |
| 2112 | error_local->message); |
| 2113 | return FALSE; |
| 2114 | } |
| 2115 | return TRUE; |
| 2116 | } |
| 2117 | |
| 2118 | /** |
| 2119 | * fu_engine_schedule_update: |
| 2120 | * @self: a #FuEngine |
| 2121 | * @device: a #FuDevice |
| 2122 | * @release: A #FwupdRelease |
| 2123 | * @blob_cab: A #GBytes |
| 2124 | * @flags: #FwupdInstallFlags |
| 2125 | * @error: A #GError or NULL |
| 2126 | * |
| 2127 | * Schedule an offline update for the device |
| 2128 | * |
| 2129 | * Returns: #TRUE for success, #FALSE for failure |
| 2130 | * |
| 2131 | * Since: 1.3.5 |
| 2132 | **/ |
| 2133 | gboolean |
| 2134 | fu_engine_schedule_update (FuEngine *self, |
| 2135 | FuDevice *device, |
| 2136 | FwupdRelease *release, |
| 2137 | GBytes *blob_cab, |
| 2138 | FwupdInstallFlags flags, |
| 2139 | GError **error) |
| 2140 | { |
| 2141 | gchar tmpname[] = {"XXXXXX.cab"}; |
| 2142 | g_autofree gchar *dirname = NULL; |
| 2143 | g_autofree gchar *filename = NULL; |
| 2144 | g_autoptr(FuHistory) history = NULL; |
| 2145 | g_autoptr(GFile) file = NULL; |
| 2146 | |
| 2147 | /* id already exists */ |
| 2148 | history = fu_history_new (); |
| 2149 | if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
| 2150 | g_autoptr(FuDevice) res_tmp = NULL; |
| 2151 | res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL); |
| 2152 | if (res_tmp != NULL && |
| 2153 | fu_device_get_update_state (res_tmp) == FWUPD_UPDATE_STATE_PENDING) { |
| 2154 | g_set_error (error, |
| 2155 | FWUPD_ERROR, |
| 2156 | FWUPD_ERROR_ALREADY_PENDING, |
| 2157 | "%s is already scheduled to be updated", |
| 2158 | fu_device_get_id (device)); |
| 2159 | return FALSE; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | /* create directory */ |
| 2164 | dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 2165 | file = g_file_new_for_path (dirname); |
| 2166 | if (!g_file_query_exists (file, NULL)) { |
| 2167 | if (!g_file_make_directory_with_parents (file, NULL, error)) |
| 2168 | return FALSE; |
| 2169 | } |
| 2170 | |
| 2171 | /* get a random filename */ |
| 2172 | for (guint i = 0; i < 6; i++) |
| 2173 | tmpname[i] = (gchar) g_random_int_range ('A', 'Z'); |
| 2174 | filename = g_build_filename (dirname, tmpname, NULL); |
| 2175 | |
| 2176 | /* just copy to the temp file */ |
| 2177 | fu_device_set_status (device, FWUPD_STATUS_SCHEDULING); |
| 2178 | if (!g_file_set_contents (filename, |
| 2179 | g_bytes_get_data (blob_cab, NULL), |
| 2180 | (gssize) g_bytes_get_size (blob_cab), |
| 2181 | error)) |
| 2182 | return FALSE; |
| 2183 | |
| 2184 | /* schedule for next boot */ |
| 2185 | g_debug ("schedule %s to be installed to %s on next boot", |
| 2186 | filename, fu_device_get_id (device)); |
| 2187 | fwupd_release_set_filename (release, filename); |
| 2188 | |
| 2189 | /* add to database */ |
| 2190 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT); |
| 2191 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING); |
| 2192 | if (!fu_history_add_device (history, device, release, error)) |
| 2193 | return FALSE; |
| 2194 | |
| 2195 | /* next boot we run offline */ |
| 2196 | fu_device_set_progress (device, 100); |
| 2197 | return fu_engine_offline_setup (error); |
| 2198 | } |
| 2199 | |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2200 | static gboolean |
| 2201 | fu_engine_install_release (FuEngine *self, |
Richard Hughes | 293a64d | 2020-02-14 12:08:08 +0000 | [diff] [blame] | 2202 | FuDevice *device_orig, |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2203 | XbNode *component, |
| 2204 | XbNode *rel, |
| 2205 | FwupdInstallFlags flags, |
| 2206 | GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 2207 | { |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2208 | FuPlugin *plugin; |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 2209 | FwupdVersionFormat fmt; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 2210 | GBytes *blob_fw; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 2211 | const gchar *tmp; |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2212 | g_autofree gchar *version_orig = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 2213 | g_autofree gchar *version_rel = NULL; |
Richard Hughes | 68db74b | 2019-03-14 09:52:05 +0000 | [diff] [blame] | 2214 | g_autoptr(FuDevice) device_tmp = NULL; |
Richard Hughes | 293a64d | 2020-02-14 12:08:08 +0000 | [diff] [blame] | 2215 | g_autoptr(FuDevice) device = g_object_ref (device_orig); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 2216 | g_autoptr(GBytes) blob_fw2 = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 2217 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 2218 | |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 2219 | /* get per-release firmware blob */ |
| 2220 | blob_fw = xb_node_get_data (rel, "fwupd::FirmwareBlob"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 2221 | if (blob_fw == NULL) { |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 2222 | g_set_error_literal (error, |
| 2223 | FWUPD_ERROR, |
| 2224 | FWUPD_ERROR_INTERNAL, |
| 2225 | "Failed to get firmware blob from release"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 2226 | return FALSE; |
| 2227 | } |
| 2228 | |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 2229 | /* use a bubblewrap helper script to build the firmware */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 2230 | tmp = g_object_get_data (G_OBJECT (component), "fwupd::BuilderScript"); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 2231 | if (tmp != NULL) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 2232 | const gchar *tmp2 = g_object_get_data (G_OBJECT (component), "fwupd::BuilderOutput"); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 2233 | if (tmp2 == NULL) |
| 2234 | tmp2 = "firmware.bin"; |
| 2235 | blob_fw2 = fu_common_firmware_builder (blob_fw, tmp, tmp2, error); |
| 2236 | if (blob_fw2 == NULL) |
| 2237 | return FALSE; |
| 2238 | } else { |
| 2239 | blob_fw2 = g_bytes_ref (blob_fw); |
| 2240 | } |
| 2241 | |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2242 | /* get the plugin */ |
| 2243 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2244 | fu_device_get_plugin (device), |
| 2245 | error); |
| 2246 | if (plugin == NULL) |
| 2247 | return FALSE; |
| 2248 | |
Richard Hughes | f8e353e | 2019-03-25 14:26:57 +0000 | [diff] [blame] | 2249 | /* schedule this for the next reboot if not in system-update.target, |
| 2250 | * but first check if allowed on battery power */ |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 2251 | version_rel = fu_engine_get_release_version (self, device, rel, error); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 2252 | if (version_rel == NULL) { |
| 2253 | g_prefix_error (error, "failed to get release version: "); |
| 2254 | return FALSE; |
| 2255 | } |
Richard Hughes | f8e353e | 2019-03-25 14:26:57 +0000 | [diff] [blame] | 2256 | |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2257 | /* add device to database */ |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2258 | if ((flags & FWUPD_INSTALL_FLAG_NO_HISTORY) == 0) { |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2259 | g_autoptr(FwupdRelease) release_tmp = NULL; |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 2260 | release_tmp = fu_engine_create_release_metadata (self, device, plugin, error); |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2261 | if (release_tmp == NULL) |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2262 | return FALSE; |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2263 | tmp = xb_node_query_text (component, |
| 2264 | "releases/release/checksum[@target='container']", |
| 2265 | NULL); |
Richard Hughes | b9bbe4c | 2019-03-25 14:03:27 +0000 | [diff] [blame] | 2266 | if (tmp != NULL) |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2267 | fwupd_release_add_checksum (release_tmp, tmp); |
| 2268 | fwupd_release_set_version (release_tmp, version_rel); |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2269 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_FAILED); |
Richard Hughes | 81d7a5c | 2019-03-25 14:26:04 +0000 | [diff] [blame] | 2270 | if (!fu_history_add_device (self->history, device, release_tmp, error)) |
Richard Hughes | afca203 | 2019-02-01 18:05:30 +0000 | [diff] [blame] | 2271 | return FALSE; |
| 2272 | } |
| 2273 | |
| 2274 | /* install firmware blob */ |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2275 | version_orig = g_strdup (fu_device_get_version (device)); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2276 | if (!fu_engine_install_blob (self, device, blob_fw2, flags, &error_local)) { |
| 2277 | fu_device_set_status (device, FWUPD_STATUS_IDLE); |
Richard Hughes | cce6a1c | 2019-04-16 17:25:48 +0100 | [diff] [blame] | 2278 | if (g_error_matches (error_local, |
| 2279 | FWUPD_ERROR, |
Richard Hughes | 7886c08 | 2019-04-18 10:00:17 +0100 | [diff] [blame] | 2280 | FWUPD_ERROR_AC_POWER_REQUIRED) || |
| 2281 | g_error_matches (error_local, |
| 2282 | FWUPD_ERROR, |
Richard Hughes | 4266ac4 | 2019-07-11 16:49:50 +0100 | [diff] [blame] | 2283 | FWUPD_ERROR_BATTERY_LEVEL_TOO_LOW) || |
| 2284 | g_error_matches (error_local, |
| 2285 | FWUPD_ERROR, |
Mario Limonciello | 2e06dcd | 2019-10-30 19:28:52 -0500 | [diff] [blame] | 2286 | FWUPD_ERROR_NEEDS_USER_ACTION) || |
| 2287 | g_error_matches (error_local, |
| 2288 | FWUPD_ERROR, |
Richard Hughes | 7886c08 | 2019-04-18 10:00:17 +0100 | [diff] [blame] | 2289 | FWUPD_ERROR_BROKEN_SYSTEM)) { |
Richard Hughes | cce6a1c | 2019-04-16 17:25:48 +0100 | [diff] [blame] | 2290 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_FAILED_TRANSIENT); |
| 2291 | } else { |
| 2292 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_FAILED); |
| 2293 | } |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2294 | fu_device_set_update_error (device, error_local->message); |
| 2295 | if ((flags & FWUPD_INSTALL_FLAG_NO_HISTORY) == 0 && |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 2296 | !fu_history_modify_device (self->history, device, error)) { |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2297 | return FALSE; |
| 2298 | } |
| 2299 | g_propagate_error (error, g_steal_pointer (&error_local)); |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2300 | return FALSE; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2301 | } |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2302 | |
Richard Hughes | 68db74b | 2019-03-14 09:52:05 +0000 | [diff] [blame] | 2303 | /* the device may have changed */ |
| 2304 | device_tmp = fu_device_list_get_by_id (self->device_list, |
| 2305 | fu_device_get_id (device), |
| 2306 | error); |
| 2307 | if (device_tmp == NULL) { |
| 2308 | g_prefix_error (error, "failed to get device after install: "); |
| 2309 | return FALSE; |
| 2310 | } |
| 2311 | g_set_object (&device, device_tmp); |
| 2312 | |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2313 | /* update database */ |
| 2314 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) || |
| 2315 | fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) { |
| 2316 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_NEEDS_REBOOT); |
| 2317 | if ((flags & FWUPD_INSTALL_FLAG_NO_HISTORY) == 0 && |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 2318 | !fu_history_modify_device (self->history, device, error)) |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2319 | return FALSE; |
| 2320 | /* success */ |
| 2321 | return TRUE; |
| 2322 | } |
| 2323 | |
| 2324 | /* for online updates, verify the version changed if not a re-install */ |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 2325 | fmt = fu_device_get_version_format (device); |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2326 | if (version_rel != NULL && |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 2327 | fu_common_vercmp_full (version_orig, version_rel, fmt) != 0 && |
Mario Limonciello | 9e96d41 | 2020-12-01 07:35:30 -0600 | [diff] [blame] | 2328 | fu_common_vercmp_full (version_orig, fu_device_get_version (device), fmt) == 0 && |
Mario Limonciello | 3165e94 | 2021-01-05 13:15:42 -0600 | [diff] [blame] | 2329 | !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
Richard Hughes | 68db74b | 2019-03-14 09:52:05 +0000 | [diff] [blame] | 2330 | g_autofree gchar *str = NULL; |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2331 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_FAILED); |
Richard Hughes | 68db74b | 2019-03-14 09:52:05 +0000 | [diff] [blame] | 2332 | str = g_strdup_printf ("device version not updated on success, %s != %s", |
| 2333 | version_rel, fu_device_get_version (device)); |
| 2334 | fu_device_set_update_error (device, str); |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /* success */ |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2338 | if ((flags & FWUPD_INSTALL_FLAG_NO_HISTORY) == 0 && |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 2339 | !fu_history_modify_device (self->history, device, error)) |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2340 | return FALSE; |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 2341 | |
| 2342 | /* make the UI update */ |
| 2343 | fu_engine_emit_changed (self); |
| 2344 | |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 2345 | return TRUE; |
Richard Hughes | 6e7419d | 2018-05-18 10:11:36 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Richard Hughes | af14073 | 2019-12-22 18:42:12 +0000 | [diff] [blame] | 2348 | typedef struct { |
| 2349 | gboolean ret; |
| 2350 | GError **error; |
| 2351 | FuEngine *self; |
| 2352 | FuDevice *device; |
| 2353 | } FuEngineSortHelper; |
| 2354 | |
| 2355 | static gint |
| 2356 | fu_engine_sort_release_versions_cb (gconstpointer a, gconstpointer b, gpointer user_data) |
| 2357 | { |
| 2358 | FuEngineSortHelper *helper = (FuEngineSortHelper *) user_data; |
| 2359 | XbNode *na = *((XbNode **) a); |
| 2360 | XbNode *nb = *((XbNode **) b); |
| 2361 | g_autofree gchar *va = NULL; |
| 2362 | g_autofree gchar *vb = NULL; |
| 2363 | |
| 2364 | /* already failed */ |
| 2365 | if (!helper->ret) |
| 2366 | return 0; |
| 2367 | |
| 2368 | /* get the semver from the release */ |
| 2369 | va = fu_engine_get_release_version (helper->self, helper->device, na, helper->error); |
| 2370 | if (va == NULL) { |
| 2371 | g_prefix_error (helper->error, "failed to get release version: "); |
| 2372 | return 0; |
| 2373 | } |
| 2374 | vb = fu_engine_get_release_version (helper->self, helper->device, nb, helper->error); |
| 2375 | if (vb == NULL) { |
| 2376 | g_prefix_error (helper->error, "failed to get release version: "); |
| 2377 | return 0; |
| 2378 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 2379 | return fu_common_vercmp_full (va, vb, fu_device_get_version_format (helper->device)); |
Richard Hughes | af14073 | 2019-12-22 18:42:12 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | static gboolean |
| 2383 | fu_engine_sort_releases (FuEngine *self, FuDevice *device, GPtrArray *rels, GError **error) |
| 2384 | { |
| 2385 | FuEngineSortHelper helper = { |
| 2386 | .ret = TRUE, |
| 2387 | .self = self, |
| 2388 | .device = device, |
| 2389 | .error = error, |
| 2390 | }; |
| 2391 | g_ptr_array_sort_with_data (rels, fu_engine_sort_release_versions_cb, &helper); |
| 2392 | return helper.ret; |
| 2393 | } |
| 2394 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 2395 | /** |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2396 | * fu_engine_install: |
| 2397 | * @self: A #FuEngine |
| 2398 | * @task: A #FuInstallTask |
| 2399 | * @blob_cab: The #GBytes of the .cab file |
| 2400 | * @flags: The #FwupdInstallFlags, e.g. %FWUPD_DEVICE_FLAG_UPDATABLE |
| 2401 | * @error: A #GError, or %NULL |
| 2402 | * |
| 2403 | * Installs a specific firmware file on a device. |
| 2404 | * |
| 2405 | * By this point all the requirements and tests should have been done in |
| 2406 | * fu_engine_check_requirements() so this should not fail before running |
| 2407 | * the plugin loader. |
| 2408 | * |
| 2409 | * Returns: %TRUE for success |
| 2410 | **/ |
| 2411 | gboolean |
| 2412 | fu_engine_install (FuEngine *self, |
| 2413 | FuInstallTask *task, |
| 2414 | GBytes *blob_cab, |
| 2415 | FwupdInstallFlags flags, |
| 2416 | GError **error) |
| 2417 | { |
| 2418 | XbNode *component = fu_install_task_get_component (task); |
| 2419 | g_autoptr(FuDevice) device = NULL; |
| 2420 | g_autoptr(GError) error_local = NULL; |
| 2421 | g_autoptr(XbNode) rel_newest = NULL; |
Richard Hughes | 664b8aa | 2020-06-22 21:22:42 +0100 | [diff] [blame] | 2422 | #if LIBXMLB_CHECK_VERSION(0,2,0) |
| 2423 | g_autoptr(XbQuery) query = NULL; |
| 2424 | #endif |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2425 | |
| 2426 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 2427 | g_return_val_if_fail (XB_IS_NODE (component), FALSE); |
| 2428 | g_return_val_if_fail (blob_cab != NULL, FALSE); |
| 2429 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2430 | |
| 2431 | /* not in bootloader mode */ |
| 2432 | device = g_object_ref (fu_install_task_get_device (task)); |
| 2433 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_BOOTLOADER)) { |
| 2434 | const gchar *caption = NULL; |
| 2435 | caption = xb_node_query_text (component, |
| 2436 | "screenshots/screenshot/caption", |
| 2437 | NULL); |
| 2438 | if (caption != NULL) { |
| 2439 | g_set_error (error, |
| 2440 | FWUPD_ERROR, |
| 2441 | FWUPD_ERROR_NEEDS_USER_ACTION, |
| 2442 | "Device %s needs to manually be put in update mode: %s", |
| 2443 | fu_device_get_name (device), caption); |
| 2444 | } else { |
| 2445 | g_set_error (error, |
| 2446 | FWUPD_ERROR, |
| 2447 | FWUPD_ERROR_NEEDS_USER_ACTION, |
| 2448 | "Device %s needs to manually be put in update mode", |
| 2449 | fu_device_get_name (device)); |
| 2450 | } |
| 2451 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_FAILED_TRANSIENT); |
| 2452 | if (error != NULL) |
| 2453 | fu_device_set_update_error (device, (*error)->message); |
| 2454 | return FALSE; |
| 2455 | } |
| 2456 | |
| 2457 | /* get the newest version */ |
Richard Hughes | 664b8aa | 2020-06-22 21:22:42 +0100 | [diff] [blame] | 2458 | #if LIBXMLB_CHECK_VERSION(0,2,0) |
| 2459 | query = xb_query_new_full (xb_node_get_silo (component), |
| 2460 | "releases/release", |
| 2461 | XB_QUERY_FLAG_FORCE_NODE_CACHE, |
| 2462 | error); |
| 2463 | if (query == NULL) |
| 2464 | return FALSE; |
| 2465 | rel_newest = xb_node_query_first_full (component, query, &error_local); |
| 2466 | #else |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2467 | rel_newest = xb_node_query_first (component, "releases/release", &error_local); |
Richard Hughes | 664b8aa | 2020-06-22 21:22:42 +0100 | [diff] [blame] | 2468 | #endif |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2469 | if (rel_newest == NULL) { |
| 2470 | g_set_error (error, |
| 2471 | FWUPD_ERROR, |
| 2472 | FWUPD_ERROR_INVALID_FILE, |
| 2473 | "No releases in the firmware component: %s", |
| 2474 | error_local->message); |
| 2475 | return FALSE; |
| 2476 | } |
| 2477 | |
| 2478 | /* schedule this for the next reboot if not in system-update.target, |
| 2479 | * but first check if allowed on battery power */ |
| 2480 | if ((flags & FWUPD_INSTALL_FLAG_OFFLINE) > 0 && |
| 2481 | !fu_engine_is_running_offline (self)) { |
| 2482 | FuPlugin *plugin; |
| 2483 | g_autoptr(FwupdRelease) release_tmp = NULL; |
| 2484 | g_autofree gchar *version_rel = NULL; |
| 2485 | version_rel = fu_engine_get_release_version (self, device, rel_newest, error); |
| 2486 | if (version_rel == NULL) { |
| 2487 | g_prefix_error (error, "failed to get release version: "); |
| 2488 | return FALSE; |
| 2489 | } |
| 2490 | plugin = fu_plugin_list_find_by_name (self->plugin_list, "upower", NULL); |
| 2491 | if (plugin != NULL) { |
| 2492 | if (!fu_plugin_runner_update_prepare (plugin, flags, device, error)) |
| 2493 | return FALSE; |
| 2494 | } |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 2495 | release_tmp = fu_engine_create_release_metadata (self, device, plugin, error); |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2496 | if (release_tmp == NULL) |
| 2497 | return FALSE; |
| 2498 | fwupd_release_set_version (release_tmp, version_rel); |
| 2499 | return fu_engine_schedule_update (self, device, release_tmp, |
| 2500 | blob_cab, flags, error); |
| 2501 | } |
| 2502 | |
Richard Hughes | af14073 | 2019-12-22 18:42:12 +0000 | [diff] [blame] | 2503 | /* install each intermediate release, or install only the newest version */ |
| 2504 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_INSTALL_ALL_RELEASES)) { |
| 2505 | g_autoptr(GPtrArray) rels = NULL; |
Richard Hughes | 664b8aa | 2020-06-22 21:22:42 +0100 | [diff] [blame] | 2506 | #if LIBXMLB_CHECK_VERSION(0,2,0) |
| 2507 | rels = xb_node_query_full (component, query, &error_local); |
| 2508 | #else |
Richard Hughes | af14073 | 2019-12-22 18:42:12 +0000 | [diff] [blame] | 2509 | rels = xb_node_query (component, "releases/release", 0, &error_local); |
Richard Hughes | 664b8aa | 2020-06-22 21:22:42 +0100 | [diff] [blame] | 2510 | #endif |
Richard Hughes | af14073 | 2019-12-22 18:42:12 +0000 | [diff] [blame] | 2511 | if (rels == NULL) { |
| 2512 | g_set_error (error, |
| 2513 | FWUPD_ERROR, |
| 2514 | FWUPD_ERROR_INVALID_FILE, |
| 2515 | "No releases in the firmware component: %s", |
| 2516 | error_local->message); |
| 2517 | return FALSE; |
| 2518 | } |
| 2519 | if (!fu_engine_sort_releases (self, device, rels, error)) |
| 2520 | return FALSE; |
| 2521 | for (guint i = 0; i < rels->len; i++) { |
| 2522 | XbNode *rel = g_ptr_array_index (rels, i); |
| 2523 | if (!fu_engine_install_release (self, device, component, rel, flags, error)) |
| 2524 | return FALSE; |
| 2525 | } |
| 2526 | } else { |
| 2527 | if (!fu_engine_install_release (self, device, component, rel_newest, flags, error)) |
| 2528 | return FALSE; |
| 2529 | } |
Richard Hughes | 4bd2e04 | 2019-12-22 12:19:52 +0000 | [diff] [blame] | 2530 | |
| 2531 | /* success */ |
| 2532 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS); |
| 2533 | return TRUE; |
| 2534 | } |
| 2535 | |
| 2536 | /** |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 2537 | * fu_engine_get_plugins: |
| 2538 | * @self: A #FuPluginList |
| 2539 | * |
| 2540 | * Gets all the plugins that have been added. |
| 2541 | * |
| 2542 | * Returns: (transfer none) (element-type FuPlugin): the plugins |
| 2543 | * |
| 2544 | * Since: 1.0.8 |
| 2545 | **/ |
| 2546 | GPtrArray * |
| 2547 | fu_engine_get_plugins (FuEngine *self) |
| 2548 | { |
| 2549 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 2550 | return fu_plugin_list_get_all (self->plugin_list); |
| 2551 | } |
| 2552 | |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 2553 | /** |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2554 | * fu_engine_get_device: |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 2555 | * @self: A #FuEngine |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2556 | * @device_id: A device ID |
| 2557 | * @error: A #GError, or %NULL |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 2558 | * |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2559 | * Gets a specific device. |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 2560 | * |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2561 | * Returns: (transfer full): a device, or %NULL if not found |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 2562 | **/ |
| 2563 | FuDevice * |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2564 | fu_engine_get_device (FuEngine *self, const gchar *device_id, GError **error) |
Richard Hughes | d583baf | 2018-11-23 13:57:22 +0000 | [diff] [blame] | 2565 | { |
| 2566 | g_autoptr(FuDevice) device1 = NULL; |
| 2567 | g_autoptr(FuDevice) device2 = NULL; |
Richard Hughes | c3afed3 | 2020-03-10 16:05:29 +0000 | [diff] [blame] | 2568 | g_autoptr(FuDevice) root = NULL; |
Richard Hughes | d583baf | 2018-11-23 13:57:22 +0000 | [diff] [blame] | 2569 | |
| 2570 | /* find device */ |
| 2571 | device1 = fu_device_list_get_by_id (self->device_list, device_id, error); |
| 2572 | if (device1 == NULL) |
| 2573 | return NULL; |
| 2574 | |
Richard Hughes | d583baf | 2018-11-23 13:57:22 +0000 | [diff] [blame] | 2575 | /* wait for device to disconnect and reconnect */ |
Richard Hughes | c3afed3 | 2020-03-10 16:05:29 +0000 | [diff] [blame] | 2576 | root = fu_device_get_root (device1); |
| 2577 | if (fu_device_has_flag (device1, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG)) { |
| 2578 | if (!fu_device_list_wait_for_replug (self->device_list, device1, error)) { |
| 2579 | g_prefix_error (error, "failed to wait for detach replug: "); |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | } else if (fu_device_has_flag (root, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG)) { |
| 2583 | if (!fu_device_list_wait_for_replug (self->device_list, root, error)) { |
| 2584 | g_prefix_error (error, "failed to wait for detach replug: "); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | } else { |
| 2588 | /* no replug required */ |
| 2589 | return g_steal_pointer (&device1); |
Richard Hughes | d583baf | 2018-11-23 13:57:22 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | /* get the new device */ |
| 2593 | device2 = fu_device_list_get_by_id (self->device_list, device_id, error); |
| 2594 | if (device2 == NULL) { |
| 2595 | g_prefix_error (error, "failed to get device after replug: "); |
| 2596 | return NULL; |
| 2597 | } |
| 2598 | |
| 2599 | /* success */ |
| 2600 | return g_steal_pointer (&device2); |
| 2601 | } |
| 2602 | |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2603 | /* same as FuDevice->prepare, but with the device open */ |
| 2604 | static gboolean |
| 2605 | fu_engine_device_prepare (FuEngine *self, |
| 2606 | FuDevice *device, |
| 2607 | FwupdInstallFlags flags, |
| 2608 | GError **error) |
| 2609 | { |
| 2610 | g_autoptr(FuDeviceLocker) locker = fu_device_locker_new (device, error); |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2611 | if (locker == NULL) { |
| 2612 | g_prefix_error (error, "failed to open device for prepare: "); |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2613 | return FALSE; |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2614 | } |
Richard Hughes | a76a0da | 2021-03-05 11:39:34 +0000 | [diff] [blame] | 2615 | |
Richard Hughes | 62a8b0c | 2021-03-31 16:43:36 +0100 | [diff] [blame] | 2616 | /* check battery level is sane */ |
Richard Hughes | a76a0da | 2021-03-05 11:39:34 +0000 | [diff] [blame] | 2617 | if (fu_device_get_battery_level (device) > 0 && |
Richard Hughes | 62a8b0c | 2021-03-31 16:43:36 +0100 | [diff] [blame] | 2618 | fu_device_get_battery_level (device) < fu_device_get_battery_threshold (device)) { |
Richard Hughes | a76a0da | 2021-03-05 11:39:34 +0000 | [diff] [blame] | 2619 | g_set_error (error, |
| 2620 | FWUPD_ERROR, |
| 2621 | FWUPD_ERROR_BATTERY_LEVEL_TOO_LOW, |
| 2622 | "battery level is too low: %u%%", |
| 2623 | fu_device_get_battery_level (device)); |
| 2624 | return FALSE; |
| 2625 | } |
| 2626 | |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2627 | return fu_device_prepare (device, flags, error); |
| 2628 | } |
| 2629 | |
| 2630 | /* same as FuDevice->cleanup, but with the device open */ |
| 2631 | static gboolean |
| 2632 | fu_engine_device_cleanup (FuEngine *self, |
| 2633 | FuDevice *device, |
| 2634 | FwupdInstallFlags flags, |
| 2635 | GError **error) |
| 2636 | { |
Mario Limonciello | e2b8a27 | 2019-11-09 22:10:35 -0600 | [diff] [blame] | 2637 | g_autoptr(FuDeviceLocker) locker = NULL; |
| 2638 | |
| 2639 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_WILL_DISAPPEAR)) { |
| 2640 | g_debug ("skipping device cleanup due to will-disappear flag"); |
| 2641 | return TRUE; |
| 2642 | } |
| 2643 | |
| 2644 | locker = fu_device_locker_new (device, error); |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2645 | if (locker == NULL) { |
| 2646 | g_prefix_error (error, "failed to open device for cleanup: "); |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2647 | return FALSE; |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2648 | } |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2649 | return fu_device_cleanup (device, flags, error); |
| 2650 | } |
| 2651 | |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2652 | static gboolean |
| 2653 | fu_engine_update_prepare (FuEngine *self, |
| 2654 | FwupdInstallFlags flags, |
| 2655 | const gchar *device_id, |
| 2656 | GError **error) |
| 2657 | { |
| 2658 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2659 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2660 | g_autoptr(FuDevice) device = NULL; |
| 2661 | |
| 2662 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2663 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2664 | if (device == NULL) |
| 2665 | return FALSE; |
Richard Hughes | 1a58062 | 2020-08-03 10:02:25 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* don't rely on a plugin clearing this */ |
| 2668 | fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_ANOTHER_WRITE_REQUIRED); |
| 2669 | |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2670 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2671 | g_debug ("prepare -> %s", str); |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2672 | if (!fu_engine_device_prepare (self, device, flags, error)) |
Mario Limonciello | 44b9e46 | 2019-10-22 14:32:10 -0500 | [diff] [blame] | 2673 | return FALSE; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2674 | for (guint j = 0; j < plugins->len; j++) { |
| 2675 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 2676 | if (!fu_plugin_runner_update_prepare (plugin_tmp, flags, device, error)) |
| 2677 | return FALSE; |
| 2678 | } |
Richard Hughes | 802bb31 | 2019-10-17 13:32:23 +0100 | [diff] [blame] | 2679 | |
| 2680 | /* wait for device to disconnect and reconnect */ |
| 2681 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG)) { |
| 2682 | if (!fu_device_list_wait_for_replug (self->device_list, device, error)) { |
| 2683 | g_prefix_error (error, "failed to wait for prepare replug: "); |
| 2684 | return FALSE; |
| 2685 | } |
| 2686 | } |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2687 | return TRUE; |
| 2688 | } |
| 2689 | |
| 2690 | static gboolean |
| 2691 | fu_engine_update_cleanup (FuEngine *self, |
| 2692 | FwupdInstallFlags flags, |
| 2693 | const gchar *device_id, |
| 2694 | GError **error) |
| 2695 | { |
| 2696 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2697 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2698 | g_autoptr(FuDevice) device = NULL; |
| 2699 | |
| 2700 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2701 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2702 | if (device == NULL) |
| 2703 | return FALSE; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2704 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2705 | g_debug ("cleanup -> %s", str); |
Richard Hughes | 2031ce3 | 2019-10-30 14:16:24 +0000 | [diff] [blame] | 2706 | if (!fu_engine_device_cleanup (self, device, flags, error)) |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2707 | return FALSE; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2708 | for (guint j = 0; j < plugins->len; j++) { |
| 2709 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 2710 | if (!fu_plugin_runner_update_cleanup (plugin_tmp, flags, device, error)) |
| 2711 | return FALSE; |
| 2712 | } |
Richard Hughes | 802bb31 | 2019-10-17 13:32:23 +0100 | [diff] [blame] | 2713 | |
| 2714 | /* wait for device to disconnect and reconnect */ |
| 2715 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG)) { |
| 2716 | if (!fu_device_list_wait_for_replug (self->device_list, device, error)) { |
| 2717 | g_prefix_error (error, "failed to wait for cleanup replug: "); |
| 2718 | return FALSE; |
| 2719 | } |
| 2720 | } |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2721 | return TRUE; |
| 2722 | } |
| 2723 | |
| 2724 | static gboolean |
| 2725 | fu_engine_update_detach (FuEngine *self, const gchar *device_id, GError **error) |
| 2726 | { |
| 2727 | FuPlugin *plugin; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2728 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2729 | g_autoptr(FuDevice) device = NULL; |
| 2730 | |
| 2731 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2732 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2733 | if (device == NULL) |
| 2734 | return FALSE; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2735 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2736 | g_debug ("detach -> %s", str); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2737 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2738 | fu_device_get_plugin (device), |
| 2739 | error); |
| 2740 | if (plugin == NULL) |
| 2741 | return FALSE; |
| 2742 | if (!fu_plugin_runner_update_detach (plugin, device, error)) |
| 2743 | return FALSE; |
| 2744 | return TRUE; |
| 2745 | } |
| 2746 | |
| 2747 | static gboolean |
| 2748 | fu_engine_update_attach (FuEngine *self, const gchar *device_id, GError **error) |
| 2749 | { |
| 2750 | FuPlugin *plugin; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2751 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2752 | g_autoptr(FuDevice) device = NULL; |
| 2753 | |
| 2754 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2755 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2756 | if (device == NULL) { |
| 2757 | g_prefix_error (error, "failed to get device after update: "); |
| 2758 | return FALSE; |
| 2759 | } |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2760 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2761 | g_debug ("attach -> %s", str); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2762 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2763 | fu_device_get_plugin (device), |
| 2764 | error); |
| 2765 | if (plugin == NULL) |
| 2766 | return FALSE; |
Mario Limonciello | e1b4d8e | 2019-10-12 11:19:10 -0500 | [diff] [blame] | 2767 | |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2768 | if (!fu_plugin_runner_update_attach (plugin, device, error)) |
| 2769 | return FALSE; |
| 2770 | return TRUE; |
| 2771 | } |
| 2772 | |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 2773 | gboolean |
| 2774 | fu_engine_activate (FuEngine *self, const gchar *device_id, GError **error) |
| 2775 | { |
| 2776 | FuPlugin *plugin; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2777 | g_autofree gchar *str = NULL; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 2778 | g_autoptr(FuDevice) device = NULL; |
| 2779 | |
| 2780 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 2781 | g_return_val_if_fail (device_id != NULL, FALSE); |
| 2782 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2783 | |
| 2784 | /* check the device exists */ |
| 2785 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
| 2786 | if (device == NULL) |
| 2787 | return FALSE; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2788 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2789 | g_debug ("activate -> %s", str); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 2790 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2791 | fu_device_get_plugin (device), |
| 2792 | error); |
| 2793 | if (plugin == NULL) |
| 2794 | return FALSE; |
| 2795 | g_debug ("Activating %s", fu_device_get_name (device)); |
| 2796 | |
| 2797 | if (!fu_plugin_runner_activate (plugin, device, error)) |
| 2798 | return FALSE; |
| 2799 | |
| 2800 | fu_engine_emit_device_changed (self, device); |
| 2801 | fu_engine_emit_changed (self); |
| 2802 | |
| 2803 | return TRUE; |
| 2804 | } |
| 2805 | |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2806 | static gboolean |
| 2807 | fu_engine_update_reload (FuEngine *self, const gchar *device_id, GError **error) |
| 2808 | { |
| 2809 | FuPlugin *plugin; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2810 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2811 | g_autoptr(FuDevice) device = NULL; |
| 2812 | |
| 2813 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2814 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2815 | if (device == NULL) { |
| 2816 | g_prefix_error (error, "failed to get device after update: "); |
| 2817 | return FALSE; |
| 2818 | } |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2819 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2820 | g_debug ("reload -> %s", str); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2821 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2822 | fu_device_get_plugin (device), |
| 2823 | error); |
| 2824 | if (plugin == NULL) |
| 2825 | return FALSE; |
Mario Limonciello | e1b4d8e | 2019-10-12 11:19:10 -0500 | [diff] [blame] | 2826 | |
| 2827 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_WILL_DISAPPEAR)) { |
| 2828 | g_debug ("skipping reload due to will-disappear flag"); |
| 2829 | return TRUE; |
| 2830 | } |
| 2831 | |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2832 | if (!fu_plugin_runner_update_reload (plugin, device, error)) { |
| 2833 | g_prefix_error (error, "failed to reload device: "); |
| 2834 | return FALSE; |
| 2835 | } |
| 2836 | return TRUE; |
| 2837 | } |
| 2838 | |
| 2839 | static gboolean |
| 2840 | fu_engine_update (FuEngine *self, |
| 2841 | const gchar *device_id, |
| 2842 | GBytes *blob_fw2, |
| 2843 | FwupdInstallFlags flags, |
| 2844 | GError **error) |
| 2845 | { |
| 2846 | FuPlugin *plugin; |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2847 | g_autofree gchar *str = NULL; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2848 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2849 | g_autoptr(FuDevice) device_pending = NULL; |
| 2850 | |
| 2851 | /* cancel the pending action */ |
| 2852 | if (!fu_engine_offline_invalidate (error)) |
| 2853 | return FALSE; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2854 | |
| 2855 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2856 | device = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2857 | if (device == NULL) { |
| 2858 | g_prefix_error (error, "failed to get device after detach: "); |
| 2859 | return FALSE; |
| 2860 | } |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2861 | device_pending = fu_history_get_device_by_id (self->history, device_id, NULL); |
Richard Hughes | 791c91b | 2019-10-17 13:33:30 +0100 | [diff] [blame] | 2862 | str = fu_device_to_string (device); |
Mario Limonciello | 67a8b89 | 2020-09-28 13:44:39 -0500 | [diff] [blame] | 2863 | g_debug ("update -> %s", str); |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2864 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 2865 | fu_device_get_plugin (device), |
| 2866 | error); |
| 2867 | if (plugin == NULL) |
| 2868 | return FALSE; |
| 2869 | if (!fu_plugin_runner_update (plugin, device, blob_fw2, flags, error)) { |
| 2870 | g_autoptr(GError) error_attach = NULL; |
| 2871 | g_autoptr(GError) error_cleanup = NULL; |
| 2872 | |
| 2873 | /* attack back into runtime then cleanup */ |
| 2874 | if (!fu_plugin_runner_update_attach (plugin, |
| 2875 | device, |
| 2876 | &error_attach)) { |
| 2877 | g_warning ("failed to attach device after failed update: %s", |
| 2878 | error_attach->message); |
| 2879 | } |
| 2880 | if (!fu_engine_update_cleanup (self, flags, device_id, &error_cleanup)) { |
| 2881 | g_warning ("failed to update-cleanup after failed update: %s", |
| 2882 | error_cleanup->message); |
| 2883 | } |
| 2884 | return FALSE; |
| 2885 | } |
Richard Hughes | 019a1bc | 2019-11-26 10:19:33 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* cleanup */ |
| 2888 | if (device_pending != NULL) { |
| 2889 | const gchar *tmp; |
| 2890 | FwupdRelease *release; |
| 2891 | |
| 2892 | /* update history database */ |
| 2893 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS); |
| 2894 | if (!fu_history_modify_device (self->history, device, error)) |
| 2895 | return FALSE; |
| 2896 | |
| 2897 | /* delete cab file */ |
| 2898 | release = fu_device_get_release_default (device_pending); |
| 2899 | tmp = fwupd_release_get_filename (release); |
| 2900 | if (tmp != NULL && g_str_has_prefix (tmp, FWUPD_LIBEXECDIR)) { |
| 2901 | g_autoptr(GError) error_delete = NULL; |
| 2902 | g_autoptr(GFile) file = NULL; |
| 2903 | file = g_file_new_for_path (tmp); |
| 2904 | if (!g_file_delete (file, NULL, &error_delete)) { |
| 2905 | g_set_error (error, |
| 2906 | FWUPD_ERROR, |
| 2907 | FWUPD_ERROR_INVALID_FILE, |
| 2908 | "Failed to delete %s: %s", |
| 2909 | tmp, error_delete->message); |
| 2910 | return FALSE; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2914 | return TRUE; |
| 2915 | } |
| 2916 | |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2917 | GBytes * |
Richard Hughes | fbd8b5d | 2020-09-24 11:48:59 +0100 | [diff] [blame] | 2918 | fu_engine_firmware_dump (FuEngine *self, |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2919 | FuDevice *device, |
| 2920 | FwupdInstallFlags flags, |
| 2921 | GError **error) |
| 2922 | { |
| 2923 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2924 | |
Richard Hughes | 26e57dd | 2020-10-05 13:49:23 +0100 | [diff] [blame] | 2925 | /* open, read, close */ |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2926 | locker = fu_device_locker_new (device, error); |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2927 | if (locker == NULL) { |
| 2928 | g_prefix_error (error, "failed to open device for firmware read: "); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2929 | return NULL; |
Richard Hughes | 8bb6498 | 2020-04-16 17:03:18 +0100 | [diff] [blame] | 2930 | } |
Richard Hughes | 26e57dd | 2020-10-05 13:49:23 +0100 | [diff] [blame] | 2931 | return fu_device_dump_firmware (device, error); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Richard Hughes | 6e7419d | 2018-05-18 10:11:36 +0100 | [diff] [blame] | 2934 | gboolean |
| 2935 | fu_engine_install_blob (FuEngine *self, |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2936 | FuDevice *device, |
| 2937 | GBytes *blob_fw, |
Richard Hughes | 6e7419d | 2018-05-18 10:11:36 +0100 | [diff] [blame] | 2938 | FwupdInstallFlags flags, |
| 2939 | GError **error) |
| 2940 | { |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2941 | guint retries = 0; |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2942 | g_autofree gchar *device_id = NULL; |
Mario Limonciello | 4afe7fb | 2018-08-05 23:14:39 -0500 | [diff] [blame] | 2943 | g_autoptr(GTimer) timer = g_timer_new (); |
Richard Hughes | 6e7419d | 2018-05-18 10:11:36 +0100 | [diff] [blame] | 2944 | |
Richard Hughes | adcc16a | 2017-08-21 12:26:46 +0100 | [diff] [blame] | 2945 | /* test the firmware is not an empty blob */ |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2946 | if (g_bytes_get_size (blob_fw) == 0) { |
Richard Hughes | adcc16a | 2017-08-21 12:26:46 +0100 | [diff] [blame] | 2947 | g_set_error (error, |
| 2948 | FWUPD_ERROR, |
| 2949 | FWUPD_ERROR_INVALID_FILE, |
| 2950 | "Firmware is invalid as has zero size"); |
| 2951 | return FALSE; |
| 2952 | } |
| 2953 | |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 2954 | /* mark this as modified even if we actually fail to do the update */ |
| 2955 | fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 2956 | |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2957 | /* plugins can set FWUPD_DEVICE_FLAG_ANOTHER_WRITE_REQUIRED to run again, but they |
| 2958 | * must return TRUE rather than an error */ |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2959 | device_id = g_strdup (fu_device_get_id (device)); |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2960 | do { |
Richard Hughes | 1a58062 | 2020-08-03 10:02:25 +0100 | [diff] [blame] | 2961 | g_autoptr(FuDevice) device_tmp = NULL; |
| 2962 | |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2963 | /* check for a loop */ |
| 2964 | if (++retries > 5) { |
| 2965 | g_set_error_literal (error, |
| 2966 | FWUPD_ERROR, |
| 2967 | FWUPD_ERROR_INTERNAL, |
| 2968 | "aborting device write loop, limit 5"); |
| 2969 | return FALSE; |
| 2970 | } |
Aleksander Morgado | 37b985c | 2019-01-20 11:23:32 +0100 | [diff] [blame] | 2971 | |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2972 | /* signal to all the plugins the update is about to happen */ |
| 2973 | if (!fu_engine_update_prepare (self, flags, device_id, error)) |
| 2974 | return FALSE; |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 2975 | |
Richard Hughes | 5297678 | 2019-02-01 21:23:01 +0000 | [diff] [blame] | 2976 | /* detach to bootloader mode */ |
| 2977 | if (!fu_engine_update_detach (self, device_id, error)) |
| 2978 | return FALSE; |
| 2979 | |
| 2980 | /* install */ |
| 2981 | if (!fu_engine_update (self, device_id, blob_fw, flags, error)) |
| 2982 | return FALSE; |
| 2983 | |
| 2984 | /* attach into runtime mode */ |
| 2985 | if (!fu_engine_update_attach (self, device_id, error)) |
| 2986 | return FALSE; |
| 2987 | |
Richard Hughes | 1a58062 | 2020-08-03 10:02:25 +0100 | [diff] [blame] | 2988 | /* the device and plugin both may have changed */ |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 2989 | device_tmp = fu_engine_get_device (self, device_id, error); |
Richard Hughes | 1a58062 | 2020-08-03 10:02:25 +0100 | [diff] [blame] | 2990 | if (device_tmp == NULL) |
| 2991 | return FALSE; |
| 2992 | if (!fu_device_has_flag (device_tmp, FWUPD_DEVICE_FLAG_ANOTHER_WRITE_REQUIRED)) |
| 2993 | break; |
| 2994 | |
| 2995 | } while (TRUE); |
Aleksander Morgado | 37b985c | 2019-01-20 11:23:32 +0100 | [diff] [blame] | 2996 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 2997 | /* get the new version number */ |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 2998 | if (!fu_engine_update_reload (self, device_id, error)) |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 2999 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3000 | |
| 3001 | /* signal to all the plugins the update has happened */ |
Richard Hughes | 03df0d5 | 2019-02-01 18:31:39 +0000 | [diff] [blame] | 3002 | if (!fu_engine_update_cleanup (self, flags, device_id, error)) |
| 3003 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3004 | |
| 3005 | /* make the UI update */ |
Richard Hughes | 7772dcf | 2018-09-10 15:49:59 +0100 | [diff] [blame] | 3006 | fu_engine_set_status (self, FWUPD_STATUS_IDLE); |
Mario Limonciello | 4afe7fb | 2018-08-05 23:14:39 -0500 | [diff] [blame] | 3007 | g_debug ("Updating %s took %f seconds", fu_device_get_name (device), |
| 3008 | g_timer_elapsed (timer, NULL)); |
Richard Hughes | 76e0f94 | 2018-05-14 16:24:00 +0100 | [diff] [blame] | 3009 | return TRUE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 3012 | static FuDevice * |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 3013 | fu_engine_get_item_by_id_fallback_history (FuEngine *self, const gchar *id, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3014 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3015 | g_autoptr(GPtrArray) devices = NULL; |
| 3016 | |
| 3017 | /* not a wildcard */ |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 3018 | if (g_strcmp0 (id, FWUPD_DEVICE_ID_ANY) != 0) { |
| 3019 | g_autoptr(FuDevice) dev = NULL; |
| 3020 | g_autoptr(GError) error_local = NULL; |
| 3021 | |
| 3022 | /* get this one device */ |
| 3023 | dev = fu_history_get_device_by_id (self->history, id, &error_local); |
| 3024 | if (dev == NULL) { |
| 3025 | g_set_error (error, |
| 3026 | FWUPD_ERROR, |
| 3027 | FWUPD_ERROR_NOTHING_TO_DO, |
| 3028 | "Failed to find %s in history database: %s", |
| 3029 | id, error_local->message); |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | |
| 3033 | /* only useful */ |
| 3034 | if (fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_SUCCESS || |
Richard Hughes | cce6a1c | 2019-04-16 17:25:48 +0100 | [diff] [blame] | 3035 | fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_FAILED_TRANSIENT || |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 3036 | fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_FAILED) { |
| 3037 | return g_steal_pointer (&dev); |
| 3038 | } |
| 3039 | |
| 3040 | /* nothing in database */ |
| 3041 | g_set_error (error, |
| 3042 | FWUPD_ERROR, |
| 3043 | FWUPD_ERROR_NOTHING_TO_DO, |
| 3044 | "Device %s has no results to report", |
| 3045 | fu_device_get_id (dev)); |
| 3046 | return NULL; |
| 3047 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3048 | |
| 3049 | /* allow '*' for any */ |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 3050 | devices = fu_history_get_devices (self->history, error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3051 | if (devices == NULL) |
| 3052 | return NULL; |
| 3053 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 3054 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 3055 | if (fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_SUCCESS || |
Richard Hughes | cce6a1c | 2019-04-16 17:25:48 +0100 | [diff] [blame] | 3056 | fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_FAILED_TRANSIENT || |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 3057 | fu_device_get_update_state (dev) == FWUPD_UPDATE_STATE_FAILED) |
| 3058 | return g_object_ref (dev); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3059 | } |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 3060 | g_set_error_literal (error, |
| 3061 | FWUPD_ERROR, |
| 3062 | FWUPD_ERROR_NOTHING_TO_DO, |
| 3063 | "Failed to find any useful results to report"); |
| 3064 | return NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3065 | } |
| 3066 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3067 | /* for the self tests */ |
| 3068 | void |
| 3069 | fu_engine_set_silo (FuEngine *self, XbSilo *silo) |
Richard Hughes | bd4d285 | 2017-09-13 14:05:14 +0100 | [diff] [blame] | 3070 | { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3071 | g_return_if_fail (FU_IS_ENGINE (self)); |
| 3072 | g_return_if_fail (XB_IS_SILO (silo)); |
| 3073 | g_set_object (&self->silo, silo); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | static gboolean |
Richard Hughes | 1af48b1 | 2018-12-03 11:51:17 +0000 | [diff] [blame] | 3077 | fu_engine_appstream_upgrade_cb (XbBuilderFixup *self, |
| 3078 | XbBuilderNode *bn, |
| 3079 | gpointer user_data, |
| 3080 | GError **error) |
| 3081 | { |
| 3082 | if (g_strcmp0 (xb_builder_node_get_element (bn), "metadata") == 0) |
| 3083 | xb_builder_node_set_element (bn, "custom"); |
| 3084 | return TRUE; |
| 3085 | } |
| 3086 | |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 3087 | static XbBuilderSource * |
| 3088 | fu_engine_create_metadata_builder_source (FuEngine *self, |
| 3089 | const gchar *fn, |
| 3090 | GError **error) |
| 3091 | { |
| 3092 | g_autoptr(GBytes) blob = NULL; |
| 3093 | g_autoptr(XbSilo) silo = NULL; |
| 3094 | g_autoptr(XbBuilderSource) source = xb_builder_source_new (); |
| 3095 | g_autofree gchar *xml = NULL; |
| 3096 | |
| 3097 | g_debug ("building metadata for %s", fn); |
| 3098 | blob = fu_common_get_contents_bytes (fn, error); |
| 3099 | if (blob == NULL) |
| 3100 | return NULL; |
| 3101 | |
| 3102 | /* convert the silo for the CAB into a XbBuilderSource */ |
| 3103 | silo = fu_engine_get_silo_from_blob (self, blob, error); |
| 3104 | if (silo == NULL) |
| 3105 | return NULL; |
| 3106 | xml = xb_silo_export (silo, XB_NODE_EXPORT_FLAG_NONE, error); |
| 3107 | if (xml == NULL) |
| 3108 | return NULL; |
| 3109 | if (!xb_builder_source_load_xml (source, xml, |
| 3110 | XB_BUILDER_SOURCE_FLAG_NONE, |
| 3111 | error)) |
| 3112 | return NULL; |
| 3113 | return g_steal_pointer (&source); |
| 3114 | } |
| 3115 | |
| 3116 | static gboolean |
| 3117 | fu_engine_create_metadata (FuEngine *self, XbBuilder *builder, |
| 3118 | FwupdRemote *remote, GError **error) |
| 3119 | { |
| 3120 | g_autoptr(GPtrArray) files = NULL; |
| 3121 | const gchar *path; |
| 3122 | |
| 3123 | /* find all files in directory */ |
| 3124 | path = fwupd_remote_get_filename_cache (remote); |
| 3125 | files = fu_common_get_files_recursive (path, error); |
| 3126 | if (files == NULL) |
| 3127 | return FALSE; |
| 3128 | |
| 3129 | /* add each source */ |
| 3130 | for (guint i = 0; i < files->len; i++) { |
| 3131 | g_autoptr(XbBuilderNode) custom = NULL; |
| 3132 | g_autoptr(XbBuilderSource) source = NULL; |
| 3133 | g_autoptr(GError) error_local = NULL; |
| 3134 | const gchar *fn = g_ptr_array_index (files, i); |
Crag Wang | ae42298 | 2020-03-05 23:42:53 +0800 | [diff] [blame] | 3135 | g_autofree gchar *fn_lowercase = g_ascii_strdown (fn, -1); |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 3136 | |
| 3137 | /* check is cab file */ |
Crag Wang | ae42298 | 2020-03-05 23:42:53 +0800 | [diff] [blame] | 3138 | if (!g_str_has_suffix (fn_lowercase, ".cab")) { |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 3139 | g_debug ("ignoring: %s", fn); |
| 3140 | continue; |
| 3141 | } |
| 3142 | |
| 3143 | /* build source for file */ |
| 3144 | source = fu_engine_create_metadata_builder_source (self, fn, &error_local); |
| 3145 | if (source == NULL) { |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 3146 | g_warning ("failed to create builder source: %s", |
| 3147 | error_local->message); |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 3148 | continue; |
| 3149 | } |
| 3150 | |
| 3151 | /* add metadata */ |
| 3152 | custom = xb_builder_node_new ("custom"); |
| 3153 | xb_builder_node_insert_text (custom, |
| 3154 | "value", fn, |
| 3155 | "key", "fwupd::FilenameCache", |
| 3156 | NULL); |
| 3157 | xb_builder_node_insert_text (custom, |
| 3158 | "value", fwupd_remote_get_id (remote), |
| 3159 | "key", "fwupd::RemoteId", |
| 3160 | NULL); |
| 3161 | xb_builder_source_set_info (source, custom); |
| 3162 | xb_builder_import_source (builder, source); |
| 3163 | } |
| 3164 | return TRUE; |
| 3165 | } |
| 3166 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3167 | static void |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3168 | fu_engine_ensure_device_supported (FuEngine *self, FuDevice *device) |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3169 | { |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3170 | gboolean is_supported = FALSE; |
| 3171 | g_autoptr(GError) error = NULL; |
| 3172 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3173 | g_autoptr(FuEngineRequest) request = fu_engine_request_new (); |
| 3174 | |
| 3175 | /* all flags set */ |
| 3176 | fu_engine_request_set_feature_flags (request, ~0); |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3177 | |
| 3178 | /* get all releases that pass the requirements */ |
| 3179 | releases = fu_engine_get_releases_for_device (self, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3180 | request, |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3181 | device, |
| 3182 | &error); |
| 3183 | if (releases == NULL) { |
| 3184 | if (!g_error_matches (error, |
| 3185 | FWUPD_ERROR, |
| 3186 | FWUPD_ERROR_NOTHING_TO_DO) && |
| 3187 | !g_error_matches (error, |
| 3188 | FWUPD_ERROR, |
| 3189 | FWUPD_ERROR_NOT_SUPPORTED)) { |
| 3190 | g_warning ("failed to get releases for %s: %s", |
| 3191 | fu_device_get_name (device), |
| 3192 | error->message); |
| 3193 | } |
| 3194 | } else { |
| 3195 | if (releases->len > 0) |
| 3196 | is_supported = TRUE; |
| 3197 | } |
| 3198 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3199 | /* was supported, now unsupported */ |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3200 | if (!is_supported) { |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3201 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_SUPPORTED)) { |
| 3202 | fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_SUPPORTED); |
| 3203 | fu_engine_emit_device_changed (self, device); |
| 3204 | } |
| 3205 | return; |
| 3206 | } |
| 3207 | |
| 3208 | /* was unsupported, now supported */ |
| 3209 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_SUPPORTED)) { |
| 3210 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_SUPPORTED); |
| 3211 | fu_engine_emit_device_changed (self, device); |
| 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | static void |
| 3216 | fu_engine_md_refresh_device_name (FuEngine *self, FuDevice *device, XbNode *component) |
| 3217 | { |
| 3218 | const gchar *name = NULL; |
| 3219 | |
| 3220 | /* require data */ |
| 3221 | if (component == NULL) |
| 3222 | return; |
| 3223 | |
| 3224 | /* copy 1:1 */ |
| 3225 | name = xb_node_query_text (component, "name", NULL); |
Richard Hughes | 2d22ee2 | 2020-10-01 13:25:15 +0100 | [diff] [blame] | 3226 | if (name != NULL) { |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3227 | fu_device_set_name (device, name); |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3228 | fu_device_remove_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME); |
Richard Hughes | 2d22ee2 | 2020-10-01 13:25:15 +0100 | [diff] [blame] | 3229 | } |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Richard Hughes | 3e445ec | 2020-11-06 11:30:22 +0000 | [diff] [blame] | 3232 | static void |
| 3233 | fu_engine_md_refresh_device_icon (FuEngine *self, FuDevice *device, XbNode *component) |
| 3234 | { |
| 3235 | const gchar *icon = NULL; |
| 3236 | |
| 3237 | /* require data */ |
| 3238 | if (component == NULL) |
| 3239 | return; |
| 3240 | |
| 3241 | /* copy 1:1 */ |
| 3242 | icon = xb_node_query_text (component, "icon", NULL); |
| 3243 | if (icon != NULL) { |
| 3244 | fu_device_add_icon (device, icon); |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3245 | fu_device_remove_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON); |
Richard Hughes | 3e445ec | 2020-11-06 11:30:22 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3249 | static const gchar * |
| 3250 | fu_common_device_category_to_name (const gchar *cat) |
| 3251 | { |
| 3252 | if (g_strcmp0 (cat, "X-EmbeddedController") == 0) |
| 3253 | return "Embedded Controller"; |
| 3254 | if (g_strcmp0 (cat, "X-ManagementEngine") == 0) |
| 3255 | return "Intel Management Engine"; |
| 3256 | if (g_strcmp0 (cat, "X-CorporateManagementEngine") == 0) |
| 3257 | return "Intel Management Engine"; |
| 3258 | if (g_strcmp0 (cat, "X-ConsumerManagementEngine") == 0) |
| 3259 | return "Intel Management Engine"; |
| 3260 | if (g_strcmp0 (cat, "X-ThunderboltController") == 0) |
| 3261 | return "Thunderbolt Controller"; |
| 3262 | if (g_strcmp0 (cat, "X-PlatformSecurityProcessor") == 0) |
| 3263 | return "Platform Security Processor"; |
Mario Limonciello | 7a9bb7e | 2020-04-02 10:28:41 -0500 | [diff] [blame] | 3264 | if (g_strcmp0 (cat, "X-CpuMicrocode") == 0) |
| 3265 | return "CPU Microcode"; |
Richard Hughes | 2d3b8c8 | 2021-03-22 17:40:06 +0000 | [diff] [blame] | 3266 | if (g_strcmp0 (cat, "X-Battery") == 0) |
| 3267 | return "Battery"; |
| 3268 | if (g_strcmp0 (cat, "X-Camera") == 0) |
| 3269 | return "Camera"; |
| 3270 | if (g_strcmp0 (cat, "X-TPM") == 0) |
| 3271 | return "TPM"; |
| 3272 | if (g_strcmp0 (cat, "X-Touchpad") == 0) |
| 3273 | return "Touchpad"; |
| 3274 | if (g_strcmp0 (cat, "X-Mouse") == 0) |
| 3275 | return "Mouse"; |
| 3276 | if (g_strcmp0 (cat, "X-Keyboard") == 0) |
| 3277 | return "Keyboard"; |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3278 | return NULL; |
| 3279 | } |
| 3280 | |
| 3281 | static void |
| 3282 | fu_engine_md_refresh_device_name_category (FuEngine *self, FuDevice *device, XbNode *component) |
| 3283 | { |
| 3284 | const gchar *name = NULL; |
| 3285 | g_autoptr(GPtrArray) cats = NULL; |
| 3286 | |
| 3287 | /* require data */ |
| 3288 | if (component == NULL) |
| 3289 | return; |
| 3290 | |
| 3291 | /* get AppStream and safe-compat categories */ |
| 3292 | cats = xb_node_query (component, "categories/category|X-categories/category", 0, NULL); |
| 3293 | if (cats == NULL) |
| 3294 | return; |
| 3295 | for (guint i = 0; i < cats->len; i++) { |
| 3296 | XbNode *n = g_ptr_array_index (cats, i); |
| 3297 | name = fu_common_device_category_to_name (xb_node_get_text (n)); |
| 3298 | if (name != NULL) |
| 3299 | break; |
| 3300 | } |
Richard Hughes | 2d22ee2 | 2020-10-01 13:25:15 +0100 | [diff] [blame] | 3301 | if (name != NULL) { |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3302 | fu_device_set_name (device, name); |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3303 | fu_device_remove_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME_CATEGORY); |
Richard Hughes | 2d22ee2 | 2020-10-01 13:25:15 +0100 | [diff] [blame] | 3304 | } |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | static void |
Richard Hughes | b097603 | 2020-02-24 14:17:04 +0000 | [diff] [blame] | 3308 | _g_ptr_array_reverse (GPtrArray *array) |
| 3309 | { |
| 3310 | guint last_idx = array->len - 1; |
| 3311 | for (guint i = 0; i < array->len / 2; i++) { |
| 3312 | gpointer tmp = array->pdata[i]; |
| 3313 | array->pdata[i] = array->pdata[last_idx - i]; |
| 3314 | array->pdata[last_idx - i] = tmp; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | static void |
| 3319 | fu_engine_md_refresh_device_verfmt (FuEngine *self, FuDevice *device, XbNode *component) |
| 3320 | { |
| 3321 | FwupdVersionFormat verfmt = FWUPD_VERSION_FORMAT_UNKNOWN; |
| 3322 | g_autoptr(GPtrArray) verfmts = NULL; |
| 3323 | |
| 3324 | /* require data */ |
| 3325 | if (component == NULL) |
| 3326 | return; |
| 3327 | |
| 3328 | /* get metadata */ |
| 3329 | verfmts = xb_node_query (component, "custom/value[@key='LVFS::VersionFormat']", 0, NULL); |
| 3330 | if (verfmts == NULL) |
| 3331 | return; |
| 3332 | _g_ptr_array_reverse (verfmts); |
| 3333 | for (guint i = 0; i < verfmts->len; i++) { |
| 3334 | XbNode *value = g_ptr_array_index (verfmts, i); |
| 3335 | verfmt = fwupd_version_format_from_string (xb_node_get_text (value)); |
| 3336 | if (verfmt != FWUPD_VERSION_FORMAT_UNKNOWN) |
| 3337 | break; |
| 3338 | } |
| 3339 | |
| 3340 | /* found and different to existing */ |
| 3341 | if (verfmt != FWUPD_VERSION_FORMAT_UNKNOWN && |
| 3342 | fu_device_get_version_format (device) != verfmt) { |
| 3343 | fu_device_set_version_format (device, verfmt); |
| 3344 | if (fu_device_get_version_raw (device) != 0x0) { |
| 3345 | g_autofree gchar *version = NULL; |
| 3346 | version = fu_common_version_from_uint32 (fu_device_get_version_raw (device), verfmt); |
| 3347 | fu_device_set_version (device, version); |
| 3348 | } |
| 3349 | if (fu_device_get_version_lowest_raw (device) != 0x0) { |
| 3350 | g_autofree gchar *version = NULL; |
| 3351 | version = fu_common_version_from_uint32 (fu_device_get_version_lowest_raw (device), verfmt); |
| 3352 | fu_device_set_version_lowest (device, version); |
| 3353 | } |
| 3354 | if (fu_device_get_version_bootloader_raw (device) != 0x0) { |
| 3355 | g_autofree gchar *version = NULL; |
| 3356 | version = fu_common_version_from_uint32 (fu_device_get_version_bootloader_raw (device), verfmt); |
| 3357 | fu_device_set_version_bootloader (device, version); |
| 3358 | } |
| 3359 | } |
Richard Hughes | 2d22ee2 | 2020-10-01 13:25:15 +0100 | [diff] [blame] | 3360 | |
| 3361 | /* do not try to do this again */ |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3362 | fu_device_remove_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT); |
Richard Hughes | b097603 | 2020-02-24 14:17:04 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 3365 | void |
Mario Limonciello | f430da0 | 2020-03-09 14:03:03 -0500 | [diff] [blame] | 3366 | fu_engine_md_refresh_device_from_component (FuEngine *self, FuDevice *device, XbNode *component) |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3367 | { |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3368 | /* set the name */ |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3369 | if (fu_device_has_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME)) |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3370 | fu_engine_md_refresh_device_name (self, device, component); |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3371 | if (fu_device_has_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_NAME_CATEGORY)) |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3372 | fu_engine_md_refresh_device_name_category (self, device, component); |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3373 | if (fu_device_has_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON)) |
Richard Hughes | 3e445ec | 2020-11-06 11:30:22 +0000 | [diff] [blame] | 3374 | fu_engine_md_refresh_device_icon (self, device, component); |
Richard Hughes | b097603 | 2020-02-24 14:17:04 +0000 | [diff] [blame] | 3375 | |
| 3376 | /* fix the version */ |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 3377 | if (fu_device_has_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT)) |
Richard Hughes | b097603 | 2020-02-24 14:17:04 +0000 | [diff] [blame] | 3378 | fu_engine_md_refresh_device_verfmt (self, device, component); |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | static void |
| 3382 | fu_engine_md_refresh_devices (FuEngine *self) |
| 3383 | { |
| 3384 | g_autoptr(GPtrArray) devices = fu_device_list_get_all (self->device_list); |
| 3385 | for (guint i = 0; i < devices->len; i++) { |
| 3386 | FuDevice *device = g_ptr_array_index (devices, i); |
Mario Limonciello | f430da0 | 2020-03-09 14:03:03 -0500 | [diff] [blame] | 3387 | g_autoptr(XbNode) component = fu_engine_get_component_by_guids (self, device); |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 3388 | |
| 3389 | /* set or clear the SUPPORTED flag */ |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 3390 | fu_engine_ensure_device_supported (self, device); |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 3391 | |
| 3392 | /* fixup the name and format as needed */ |
Mario Limonciello | f430da0 | 2020-03-09 14:03:03 -0500 | [diff] [blame] | 3393 | fu_engine_md_refresh_device_from_component (self, device, component); |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3394 | } |
| 3395 | } |
| 3396 | |
Richard Hughes | 1af48b1 | 2018-12-03 11:51:17 +0000 | [diff] [blame] | 3397 | static gboolean |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 3398 | fu_engine_load_metadata_store (FuEngine *self, FuEngineLoadFlags flags, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3399 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3400 | GPtrArray *remotes; |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 3401 | XbBuilderCompileFlags compile_flags = XB_BUILDER_COMPILE_FLAG_IGNORE_INVALID; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3402 | g_autofree gchar *cachedirpkg = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3403 | g_autofree gchar *xmlbfn = NULL; |
| 3404 | g_autoptr(GFile) xmlb = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3405 | g_autoptr(GPtrArray) components = NULL; |
| 3406 | g_autoptr(XbBuilder) builder = xb_builder_new (); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3407 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3408 | /* clear existing silo */ |
| 3409 | g_clear_object (&self->silo); |
| 3410 | |
| 3411 | /* verbose profiling */ |
Richard Hughes | c46aa81 | 2020-09-28 14:20:26 +0100 | [diff] [blame] | 3412 | if (g_getenv ("FWUPD_XMLB_VERBOSE") != NULL) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3413 | xb_builder_set_profile_flags (builder, |
| 3414 | XB_SILO_PROFILE_FLAG_XPATH | |
| 3415 | XB_SILO_PROFILE_FLAG_DEBUG); |
| 3416 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3417 | |
| 3418 | /* load each enabled metadata file */ |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 3419 | remotes = fu_remote_list_get_all (self->remote_list); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3420 | for (guint i = 0; i < remotes->len; i++) { |
| 3421 | const gchar *path = NULL; |
Richard Hughes | 5f733f2 | 2017-11-26 16:14:20 +0000 | [diff] [blame] | 3422 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3423 | g_autoptr(GFile) file = NULL; |
Richard Hughes | 1af48b1 | 2018-12-03 11:51:17 +0000 | [diff] [blame] | 3424 | g_autoptr(XbBuilderFixup) fixup = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3425 | g_autoptr(XbBuilderNode) custom = NULL; |
| 3426 | g_autoptr(XbBuilderSource) source = xb_builder_source_new (); |
| 3427 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3428 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
Mario Limonciello | 933e430 | 2020-10-02 16:33:12 -0500 | [diff] [blame] | 3429 | if (!fwupd_remote_get_enabled (remote)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3430 | continue; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3431 | path = fwupd_remote_get_filename_cache (remote); |
Mario Limonciello | 933e430 | 2020-10-02 16:33:12 -0500 | [diff] [blame] | 3432 | if (!g_file_test (path, G_FILE_TEST_EXISTS)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3433 | continue; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3434 | |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 3435 | /* generate all metadata on demand */ |
| 3436 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 3437 | g_debug ("building metadata for remote '%s'", |
| 3438 | fwupd_remote_get_id (remote)); |
| 3439 | if (!fu_engine_create_metadata (self, builder, remote, &error_local)) { |
| 3440 | g_warning ("failed to generate remote %s: %s", |
| 3441 | fwupd_remote_get_id (remote), |
| 3442 | error_local->message); |
| 3443 | } |
| 3444 | continue; |
| 3445 | } |
| 3446 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3447 | /* save the remote-id in the custom metadata space */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3448 | file = g_file_new_for_path (path); |
| 3449 | if (!xb_builder_source_load_file (source, file, |
| 3450 | XB_BUILDER_SOURCE_FLAG_NONE, |
| 3451 | NULL, &error_local)) { |
Richard Hughes | 068d343 | 2017-09-16 08:26:46 +0100 | [diff] [blame] | 3452 | g_warning ("failed to load remote %s: %s", |
| 3453 | fwupd_remote_get_id (remote), |
| 3454 | error_local->message); |
| 3455 | continue; |
| 3456 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3457 | |
Richard Hughes | 1af48b1 | 2018-12-03 11:51:17 +0000 | [diff] [blame] | 3458 | /* fix up any legacy installed files */ |
| 3459 | fixup = xb_builder_fixup_new ("AppStreamUpgrade", |
| 3460 | fu_engine_appstream_upgrade_cb, |
| 3461 | self, NULL); |
| 3462 | xb_builder_fixup_set_max_depth (fixup, 3); |
| 3463 | xb_builder_source_add_fixup (source, fixup); |
| 3464 | |
Richard Hughes | 33171fd | 2018-11-09 13:29:11 +0000 | [diff] [blame] | 3465 | /* add metadata */ |
| 3466 | custom = xb_builder_node_new ("custom"); |
| 3467 | xb_builder_node_insert_text (custom, |
| 3468 | "value", path, |
| 3469 | "key", "fwupd::FilenameCache", |
| 3470 | NULL); |
| 3471 | xb_builder_node_insert_text (custom, |
| 3472 | "value", fwupd_remote_get_id (remote), |
| 3473 | "key", "fwupd::RemoteId", |
| 3474 | NULL); |
| 3475 | xb_builder_source_set_info (source, custom); |
| 3476 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3477 | /* we need to watch for changes? */ |
| 3478 | xb_builder_import_source (builder, source); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3479 | } |
| 3480 | |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 3481 | /* on a read-only filesystem don't care about the cache GUID */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 3482 | if (flags & FU_ENGINE_LOAD_FLAG_READONLY) |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 3483 | compile_flags |= XB_BUILDER_COMPILE_FLAG_IGNORE_GUID; |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 3484 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3485 | /* ensure silo is up to date */ |
| 3486 | cachedirpkg = fu_common_get_path (FU_PATH_KIND_CACHEDIR_PKG); |
| 3487 | xmlbfn = g_build_filename (cachedirpkg, "metadata.xmlb", NULL); |
| 3488 | xmlb = g_file_new_for_path (xmlbfn); |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 3489 | self->silo = xb_builder_ensure (builder, xmlb, compile_flags, NULL, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3490 | if (self->silo == NULL) |
| 3491 | return FALSE; |
| 3492 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3493 | /* print what we've got */ |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 3494 | components = xb_silo_query (self->silo, |
| 3495 | "components/component[@type='firmware']", |
| 3496 | 0, NULL); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3497 | if (components != NULL) |
| 3498 | g_debug ("%u components now in silo", components->len); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3499 | |
Richard Hughes | 634da03 | 2018-11-05 11:42:20 +0000 | [diff] [blame] | 3500 | /* build the index */ |
| 3501 | if (!xb_silo_query_build_index (self->silo, |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 3502 | "components/component", |
Richard Hughes | 634da03 | 2018-11-05 11:42:20 +0000 | [diff] [blame] | 3503 | "type", error)) |
| 3504 | return FALSE; |
| 3505 | if (!xb_silo_query_build_index (self->silo, |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 3506 | "components/component[@type='firmware']/provides/firmware", |
| 3507 | "type", error)) |
| 3508 | return FALSE; |
| 3509 | if (!xb_silo_query_build_index (self->silo, |
| 3510 | "components/component[@type='firmware']/provides/firmware", |
Richard Hughes | 634da03 | 2018-11-05 11:42:20 +0000 | [diff] [blame] | 3511 | NULL, error)) |
| 3512 | return FALSE; |
| 3513 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3514 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3515 | return TRUE; |
| 3516 | } |
| 3517 | |
Mario Limonciello | 263cab9 | 2019-08-20 17:16:00 -0500 | [diff] [blame] | 3518 | static void |
| 3519 | fu_engine_config_changed_cb (FuConfig *config, FuEngine *self) |
| 3520 | { |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 3521 | fu_idle_set_timeout (self->idle, fu_config_get_idle_timeout (config)); |
| 3522 | } |
| 3523 | |
| 3524 | static void |
| 3525 | fu_engine_remote_list_changed_cb (FuRemoteList *remote_list, FuEngine *self) |
| 3526 | { |
Mario Limonciello | 263cab9 | 2019-08-20 17:16:00 -0500 | [diff] [blame] | 3527 | g_autoptr(GError) error_local = NULL; |
| 3528 | if (!fu_engine_load_metadata_store (self, FU_ENGINE_LOAD_FLAG_NONE, |
| 3529 | &error_local)) |
| 3530 | g_warning ("Failed to reload metadata store: %s", |
| 3531 | error_local->message); |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 3532 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3533 | /* set device properties from the metadata */ |
| 3534 | fu_engine_md_refresh_devices (self); |
| 3535 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 3536 | /* invalidate host security attributes */ |
| 3537 | g_clear_pointer (&self->host_security_id, g_free); |
| 3538 | |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 3539 | /* make the UI update */ |
| 3540 | fu_engine_emit_changed (self); |
Mario Limonciello | 263cab9 | 2019-08-20 17:16:00 -0500 | [diff] [blame] | 3541 | } |
| 3542 | |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3543 | static gint |
| 3544 | fu_engine_sort_jcat_results_timestamp_cb (gconstpointer a, gconstpointer b) |
| 3545 | { |
| 3546 | JcatResult *ra = *((JcatResult **) a); |
| 3547 | JcatResult *rb = *((JcatResult **) b); |
| 3548 | if (jcat_result_get_timestamp (ra) < jcat_result_get_timestamp (rb)) |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3549 | return 1; |
Richard Hughes | afda962 | 2020-06-10 12:48:31 +0100 | [diff] [blame] | 3550 | if (jcat_result_get_timestamp (ra) > jcat_result_get_timestamp (rb)) |
| 3551 | return -1; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | static JcatResult * |
Richard Hughes | afda962 | 2020-06-10 12:48:31 +0100 | [diff] [blame] | 3556 | fu_engine_get_newest_signature_jcat_result (GPtrArray *results, GError **error) |
| 3557 | { |
| 3558 | /* sort by timestamp, newest first */ |
| 3559 | g_ptr_array_sort (results, fu_engine_sort_jcat_results_timestamp_cb); |
| 3560 | |
| 3561 | /* get the first signature, ignoring the checksums */ |
| 3562 | for (guint i = 0; i < results->len; i++) { |
| 3563 | JcatResult *result = g_ptr_array_index (results, i); |
| 3564 | #if LIBJCAT_CHECK_VERSION(0, 1, 3) |
| 3565 | if (jcat_result_get_method (result) == JCAT_BLOB_METHOD_SIGNATURE) |
| 3566 | return g_object_ref (result); |
| 3567 | #else |
| 3568 | guint verify_kind = 0; |
| 3569 | g_autoptr(JcatEngine) engine = NULL; |
| 3570 | g_object_get (result, "engine", &engine, NULL); |
| 3571 | g_object_get (engine, "verify-kind", &verify_kind, NULL); |
| 3572 | if (verify_kind == 2) /* SIGNATURE */ |
| 3573 | return g_object_ref (result); |
| 3574 | #endif |
| 3575 | } |
| 3576 | |
| 3577 | /* should never happen due to %JCAT_VERIFY_FLAG_REQUIRE_SIGNATURE */ |
| 3578 | g_set_error_literal (error, |
| 3579 | FWUPD_ERROR, |
| 3580 | FWUPD_ERROR_INVALID_FILE, |
| 3581 | "no signature method in results"); |
| 3582 | return NULL; |
| 3583 | } |
| 3584 | |
| 3585 | static JcatResult * |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3586 | fu_engine_get_system_jcat_result (FuEngine *self, FwupdRemote *remote, GError **error) |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3587 | { |
| 3588 | g_autoptr(GBytes) blob = NULL; |
| 3589 | g_autoptr(GBytes) blob_sig = NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3590 | g_autoptr(GInputStream) istream = NULL; |
| 3591 | g_autoptr(GPtrArray) results = NULL; |
| 3592 | g_autoptr(JcatItem) jcat_item = NULL; |
| 3593 | g_autoptr(JcatFile) jcat_file = jcat_file_new (); |
| 3594 | |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3595 | blob = fu_common_get_contents_bytes (fwupd_remote_get_filename_cache (remote), error); |
| 3596 | if (blob == NULL) |
| 3597 | return NULL; |
| 3598 | blob_sig = fu_common_get_contents_bytes (fwupd_remote_get_filename_cache_sig (remote), error); |
| 3599 | if (blob_sig == NULL) |
| 3600 | return NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3601 | istream = g_memory_input_stream_new_from_bytes (blob_sig); |
| 3602 | if (!jcat_file_import_stream (jcat_file, istream, |
| 3603 | JCAT_IMPORT_FLAG_NONE, |
| 3604 | NULL, error)) |
| 3605 | return NULL; |
| 3606 | jcat_item = jcat_file_get_item_default (jcat_file, error); |
| 3607 | if (jcat_item == NULL) |
| 3608 | return NULL; |
| 3609 | results = jcat_context_verify_item (self->jcat_context, |
| 3610 | blob, jcat_item, |
| 3611 | JCAT_VERIFY_FLAG_REQUIRE_CHECKSUM | |
| 3612 | JCAT_VERIFY_FLAG_REQUIRE_SIGNATURE, |
| 3613 | error); |
| 3614 | if (results == NULL) |
| 3615 | return NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3616 | |
Richard Hughes | afda962 | 2020-06-10 12:48:31 +0100 | [diff] [blame] | 3617 | /* return the newest signature */ |
| 3618 | return fu_engine_get_newest_signature_jcat_result (results, error); |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | static gboolean |
| 3622 | fu_engine_validate_result_timestamp (JcatResult *jcat_result, |
| 3623 | JcatResult *jcat_result_old, |
| 3624 | GError **error) |
| 3625 | { |
| 3626 | gint64 delta = 0; |
| 3627 | |
| 3628 | g_return_val_if_fail (JCAT_IS_RESULT (jcat_result), FALSE); |
| 3629 | g_return_val_if_fail (JCAT_IS_RESULT (jcat_result_old), FALSE); |
| 3630 | |
Richard Hughes | 64ebf91 | 2020-05-29 07:51:38 +0100 | [diff] [blame] | 3631 | if (jcat_result_get_timestamp (jcat_result) == 0) { |
| 3632 | g_set_error (error, |
| 3633 | FWUPD_ERROR, |
| 3634 | FWUPD_ERROR_INVALID_FILE, |
| 3635 | "no signing timestamp"); |
| 3636 | return FALSE; |
| 3637 | } |
| 3638 | if (jcat_result_get_timestamp (jcat_result_old) > 0) { |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3639 | delta = jcat_result_get_timestamp (jcat_result) - |
| 3640 | jcat_result_get_timestamp (jcat_result_old); |
| 3641 | } |
| 3642 | if (delta < 0) { |
| 3643 | g_set_error (error, |
| 3644 | FWUPD_ERROR, |
| 3645 | FWUPD_ERROR_INVALID_FILE, |
| 3646 | "new signing timestamp was %" |
| 3647 | G_GINT64_FORMAT " seconds older", |
| 3648 | -delta); |
| 3649 | return FALSE; |
| 3650 | } |
| 3651 | if (delta > 0) |
| 3652 | g_debug ("timestamp increased, so no rollback"); |
| 3653 | return TRUE; |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3656 | /** |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3657 | * fu_engine_update_metadata_bytes: |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3658 | * @self: A #FuEngine |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 3659 | * @remote_id: A remote ID, e.g. `lvfs` |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3660 | * @bytes_raw: Blob of metadata |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3661 | * @bytes_sig: Blob of metadata signature, typically Jcat binary format |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3662 | * @error: A #GError, or %NULL |
| 3663 | * |
| 3664 | * Updates the metadata for a specific remote. |
| 3665 | * |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3666 | * Returns: %TRUE for success |
| 3667 | **/ |
| 3668 | gboolean |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3669 | fu_engine_update_metadata_bytes (FuEngine *self, const gchar *remote_id, |
| 3670 | GBytes *bytes_raw, GBytes *bytes_sig, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3671 | { |
Richard Hughes | 7403dc5 | 2017-08-10 15:34:10 +0100 | [diff] [blame] | 3672 | FwupdKeyringKind keyring_kind; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3673 | FwupdRemote *remote; |
Richard Hughes | 8bcc5f3 | 2020-11-30 09:41:10 +0000 | [diff] [blame] | 3674 | JcatVerifyFlags jcat_flags = JCAT_VERIFY_FLAG_REQUIRE_SIGNATURE; |
| 3675 | g_autoptr(JcatFile) jcat_file = jcat_file_new (); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3676 | |
| 3677 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 3678 | g_return_val_if_fail (remote_id != NULL, FALSE); |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3679 | g_return_val_if_fail (bytes_raw != NULL, FALSE); |
| 3680 | g_return_val_if_fail (bytes_sig != NULL, FALSE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3681 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 3682 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3683 | /* check remote is valid */ |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 3684 | remote = fu_remote_list_get_by_id (self->remote_list, remote_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3685 | if (remote == NULL) { |
| 3686 | g_set_error (error, |
| 3687 | FWUPD_ERROR, |
| 3688 | FWUPD_ERROR_NOT_FOUND, |
| 3689 | "remote %s not found", remote_id); |
| 3690 | return FALSE; |
| 3691 | } |
| 3692 | if (!fwupd_remote_get_enabled (remote)) { |
| 3693 | g_set_error (error, |
| 3694 | FWUPD_ERROR, |
| 3695 | FWUPD_ERROR_NOT_SUPPORTED, |
| 3696 | "remote %s not enabled", remote_id); |
| 3697 | return FALSE; |
| 3698 | } |
| 3699 | |
Richard Hughes | 8bcc5f3 | 2020-11-30 09:41:10 +0000 | [diff] [blame] | 3700 | /* verify JCatFile, or create a dummy one from legacy data */ |
Richard Hughes | 7403dc5 | 2017-08-10 15:34:10 +0100 | [diff] [blame] | 3701 | keyring_kind = fwupd_remote_get_keyring_kind (remote); |
Richard Hughes | 8bcc5f3 | 2020-11-30 09:41:10 +0000 | [diff] [blame] | 3702 | if (keyring_kind == FWUPD_KEYRING_KIND_JCAT) { |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3703 | g_autoptr(GInputStream) istream = NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3704 | istream = g_memory_input_stream_new_from_bytes (bytes_sig); |
| 3705 | if (!jcat_file_import_stream (jcat_file, istream, |
| 3706 | JCAT_IMPORT_FLAG_NONE, |
| 3707 | NULL, error)) |
Richard Hughes | 7403dc5 | 2017-08-10 15:34:10 +0100 | [diff] [blame] | 3708 | return FALSE; |
Richard Hughes | 8bcc5f3 | 2020-11-30 09:41:10 +0000 | [diff] [blame] | 3709 | jcat_flags |= JCAT_VERIFY_FLAG_REQUIRE_CHECKSUM; |
| 3710 | } else if (keyring_kind == FWUPD_KEYRING_KIND_GPG) { |
| 3711 | g_autoptr(JcatBlob) jcab_blob = NULL; |
| 3712 | g_autoptr(JcatItem) jcat_item = jcat_item_new (""); |
| 3713 | jcab_blob = jcat_blob_new (JCAT_BLOB_KIND_GPG, bytes_sig); |
| 3714 | jcat_item_add_blob (jcat_item, jcab_blob); |
| 3715 | jcat_file_add_item (jcat_file, jcat_item); |
| 3716 | } else if (keyring_kind == FWUPD_KEYRING_KIND_PKCS7) { |
| 3717 | g_autoptr(JcatBlob) jcab_blob = NULL; |
| 3718 | g_autoptr(JcatItem) jcat_item = jcat_item_new (""); |
| 3719 | jcab_blob = jcat_blob_new (JCAT_BLOB_KIND_PKCS7, bytes_sig); |
| 3720 | jcat_item_add_blob (jcat_item, jcab_blob); |
| 3721 | jcat_file_add_item (jcat_file, jcat_item); |
| 3722 | } |
| 3723 | |
| 3724 | /* verify file */ |
| 3725 | if (keyring_kind != FWUPD_KEYRING_KIND_NONE) { |
| 3726 | g_autoptr(GError) error_local = NULL; |
| 3727 | g_autoptr(GPtrArray) results = NULL; |
| 3728 | g_autoptr(JcatItem) jcat_item = NULL; |
| 3729 | g_autoptr(JcatResult) jcat_result = NULL; |
| 3730 | g_autoptr(JcatResult) jcat_result_old = NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3731 | |
| 3732 | /* this should only be signing one thing */ |
| 3733 | jcat_item = jcat_file_get_item_default (jcat_file, error); |
| 3734 | if (jcat_item == NULL) |
Richard Hughes | 14047d7 | 2017-08-18 10:58:47 +0100 | [diff] [blame] | 3735 | return FALSE; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3736 | results = jcat_context_verify_item (self->jcat_context, |
| 3737 | bytes_raw, jcat_item, |
Richard Hughes | 8bcc5f3 | 2020-11-30 09:41:10 +0000 | [diff] [blame] | 3738 | jcat_flags, error); |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3739 | if (results == NULL) |
Richard Hughes | 7403dc5 | 2017-08-10 15:34:10 +0100 | [diff] [blame] | 3740 | return FALSE; |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3741 | |
Richard Hughes | afda962 | 2020-06-10 12:48:31 +0100 | [diff] [blame] | 3742 | /* return the newest signature */ |
| 3743 | jcat_result = fu_engine_get_newest_signature_jcat_result (results, error); |
| 3744 | if (jcat_result == NULL) |
| 3745 | return FALSE; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3746 | |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3747 | /* verify the metadata was signed later than the existing |
| 3748 | * metadata for this remote to mitigate a rollback attack */ |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3749 | jcat_result_old = fu_engine_get_system_jcat_result (self, remote, &error_local); |
| 3750 | if (jcat_result_old == NULL) { |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3751 | if (g_error_matches (error_local, |
| 3752 | G_FILE_ERROR, |
| 3753 | G_FILE_ERROR_NOENT)) { |
| 3754 | g_debug ("no existing valid keyrings: %s", |
| 3755 | error_local->message); |
| 3756 | } else { |
| 3757 | g_warning ("could not get existing keyring result: %s", |
| 3758 | error_local->message); |
| 3759 | } |
| 3760 | } else { |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3761 | if (!fu_engine_validate_result_timestamp (jcat_result, |
| 3762 | jcat_result_old, |
| 3763 | error)) |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3764 | return FALSE; |
Richard Hughes | f69a481 | 2017-08-16 12:27:51 +0100 | [diff] [blame] | 3765 | } |
Richard Hughes | 7403dc5 | 2017-08-10 15:34:10 +0100 | [diff] [blame] | 3766 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3767 | |
Richard Hughes | 99e621d | 2017-08-16 12:24:18 +0100 | [diff] [blame] | 3768 | /* save XML and signature to remotes.d */ |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 3769 | if (!fu_common_set_contents_bytes (fwupd_remote_get_filename_cache (remote), |
| 3770 | bytes_raw, error)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3771 | return FALSE; |
Richard Hughes | 99e621d | 2017-08-16 12:24:18 +0100 | [diff] [blame] | 3772 | if (keyring_kind != FWUPD_KEYRING_KIND_NONE) { |
| 3773 | if (!fu_common_set_contents_bytes (fwupd_remote_get_filename_cache_sig (remote), |
| 3774 | bytes_sig, error)) |
| 3775 | return FALSE; |
| 3776 | } |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 3777 | if (!fu_engine_load_metadata_store (self, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 3778 | return FALSE; |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 3779 | |
| 3780 | /* refresh SUPPORTED flag on devices */ |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 3781 | fu_engine_md_refresh_devices (self); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 3782 | |
| 3783 | /* invalidate host security attributes */ |
| 3784 | g_clear_pointer (&self->host_security_id, g_free); |
| 3785 | |
| 3786 | /* make the UI update */ |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 3787 | fu_engine_emit_changed (self); |
| 3788 | return TRUE; |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3789 | } |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 3790 | |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3791 | /** |
| 3792 | * fu_engine_update_metadata: |
| 3793 | * @self: A #FuEngine |
| 3794 | * @remote_id: A remote ID, e.g. `lvfs` |
| 3795 | * @fd: file descriptor of the metadata |
| 3796 | * @fd_sig: file descriptor of the metadata signature |
| 3797 | * @error: A #GError, or %NULL |
| 3798 | * |
| 3799 | * Updates the metadata for a specific remote. |
| 3800 | * |
| 3801 | * Note: this will close the fds when done |
| 3802 | * |
| 3803 | * Returns: %TRUE for success |
| 3804 | **/ |
| 3805 | gboolean |
| 3806 | fu_engine_update_metadata (FuEngine *self, const gchar *remote_id, |
| 3807 | gint fd, gint fd_sig, GError **error) |
| 3808 | { |
| 3809 | #ifdef HAVE_GIO_UNIX |
| 3810 | g_autoptr(GBytes) bytes_raw = NULL; |
| 3811 | g_autoptr(GBytes) bytes_sig = NULL; |
| 3812 | g_autoptr(GInputStream) stream_fd = NULL; |
| 3813 | g_autoptr(GInputStream) stream_sig = NULL; |
| 3814 | |
| 3815 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 3816 | g_return_val_if_fail (remote_id != NULL, FALSE); |
| 3817 | g_return_val_if_fail (fd > 0, FALSE); |
| 3818 | g_return_val_if_fail (fd_sig > 0, FALSE); |
| 3819 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 3820 | |
| 3821 | /* ensures the fd's are closed on error */ |
| 3822 | stream_fd = g_unix_input_stream_new (fd, TRUE); |
| 3823 | stream_sig = g_unix_input_stream_new (fd_sig, TRUE); |
| 3824 | |
| 3825 | /* read the entire file into memory */ |
| 3826 | bytes_raw = g_input_stream_read_bytes (stream_fd, 0x100000, NULL, error); |
| 3827 | if (bytes_raw == NULL) |
| 3828 | return FALSE; |
| 3829 | |
| 3830 | /* read signature */ |
Richard Hughes | 64d868a | 2020-01-17 17:40:00 +0000 | [diff] [blame] | 3831 | bytes_sig = g_input_stream_read_bytes (stream_sig, 0x100000, NULL, error); |
Richard Hughes | be5b019 | 2020-01-17 14:32:23 +0000 | [diff] [blame] | 3832 | if (bytes_sig == NULL) |
| 3833 | return FALSE; |
| 3834 | |
| 3835 | /* update with blobs */ |
| 3836 | return fu_engine_update_metadata_bytes (self, remote_id, |
| 3837 | bytes_raw, bytes_sig, |
| 3838 | error); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 3839 | #else |
| 3840 | g_set_error (error, |
| 3841 | FWUPD_ERROR, |
| 3842 | FWUPD_ERROR_NOT_SUPPORTED, |
| 3843 | "Not supported as <glib-unix.h> is unavailable"); |
| 3844 | return FALSE; |
| 3845 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3846 | } |
| 3847 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3848 | /** |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3849 | * fu_engine_get_silo_from_blob: |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3850 | * @self: A #FuEngine |
| 3851 | * @blob_cab: A #GBytes |
| 3852 | * @error: A #GError, or %NULL |
| 3853 | * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3854 | * Creates a silo from a .cab file blob. |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3855 | * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3856 | * Returns: (transfer container): a #XbSilo, or %NULL |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3857 | **/ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3858 | XbSilo * |
| 3859 | fu_engine_get_silo_from_blob (FuEngine *self, GBytes *blob_cab, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3860 | { |
Richard Hughes | c6eb416 | 2020-02-27 10:02:36 +0000 | [diff] [blame] | 3861 | g_autoptr(FuCabinet) cabinet = fu_cabinet_new (); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3862 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3863 | |
| 3864 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 3865 | g_return_val_if_fail (blob_cab != NULL, NULL); |
| 3866 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 3867 | |
| 3868 | /* load file */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3869 | fu_engine_set_status (self, FWUPD_STATUS_DECOMPRESSING); |
Richard Hughes | c6eb416 | 2020-02-27 10:02:36 +0000 | [diff] [blame] | 3870 | fu_cabinet_set_size_max (cabinet, fu_engine_get_archive_size_max (self)); |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 3871 | fu_cabinet_set_jcat_context (cabinet, self->jcat_context); |
Richard Hughes | c6eb416 | 2020-02-27 10:02:36 +0000 | [diff] [blame] | 3872 | if (!fu_cabinet_parse (cabinet, blob_cab, FU_CABINET_PARSE_FLAG_NONE, error)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3873 | return NULL; |
Richard Hughes | c6eb416 | 2020-02-27 10:02:36 +0000 | [diff] [blame] | 3874 | silo = fu_cabinet_get_silo (cabinet); |
Mario Limonciello | 5735fd6 | 2017-07-13 15:47:52 -0500 | [diff] [blame] | 3875 | fu_engine_set_status (self, FWUPD_STATUS_IDLE); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3876 | return g_steal_pointer (&silo); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3877 | } |
| 3878 | |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3879 | static FuDevice * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3880 | fu_engine_get_result_from_component (FuEngine *self, |
| 3881 | FuEngineRequest *request, |
| 3882 | XbNode *component, |
| 3883 | GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3884 | { |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 3885 | FwupdReleaseFlags release_flags = FWUPD_RELEASE_FLAG_NONE; |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 3886 | g_autoptr(FuInstallTask) task = NULL; |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3887 | g_autoptr(FuDevice) dev = NULL; |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 3888 | g_autoptr(FwupdRelease) rel = NULL; |
Salud Lemus | 980f2d9 | 2018-08-28 09:25:40 -0700 | [diff] [blame] | 3889 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3890 | g_autoptr(GPtrArray) provides = NULL; |
| 3891 | g_autoptr(XbNode) description = NULL; |
| 3892 | g_autoptr(XbNode) release = NULL; |
Richard Hughes | 890dd12 | 2020-06-25 22:49:04 +0100 | [diff] [blame] | 3893 | #if LIBXMLB_CHECK_VERSION(0,2,0) |
| 3894 | g_autoptr(XbQuery) query = NULL; |
| 3895 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3896 | |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3897 | dev = fu_device_new (); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3898 | provides = xb_node_query (component, |
Richard Hughes | 634da03 | 2018-11-05 11:42:20 +0000 | [diff] [blame] | 3899 | "provides/firmware[@type=$'flashed']", |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3900 | 0, &error_local); |
| 3901 | if (provides == NULL) { |
| 3902 | g_set_error (error, |
| 3903 | FWUPD_ERROR, |
| 3904 | FWUPD_ERROR_INTERNAL, |
| 3905 | "failed to get release: %s", |
| 3906 | error_local->message); |
| 3907 | return NULL; |
| 3908 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3909 | for (guint i = 0; i < provides->len; i++) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3910 | XbNode *prov = XB_NODE (g_ptr_array_index (provides, i)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3911 | const gchar *guid; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 3912 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3913 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3914 | /* is a online or offline update appropriate */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3915 | guid = xb_node_get_text (prov); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3916 | if (guid == NULL) |
| 3917 | continue; |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 3918 | device = fu_device_list_get_by_guid (self->device_list, guid, NULL); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 3919 | if (device != NULL) { |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3920 | fu_device_set_name (dev, fu_device_get_name (device)); |
| 3921 | fu_device_set_flags (dev, fu_device_get_flags (device)); |
| 3922 | fu_device_set_id (dev, fu_device_get_id (device)); |
Mario Limonciello | f430da0 | 2020-03-09 14:03:03 -0500 | [diff] [blame] | 3923 | fu_device_set_version_raw (dev, fu_device_get_version_raw (device)); |
Mario Limonciello | 8bcdfaa | 2020-02-25 11:18:15 -0600 | [diff] [blame] | 3924 | fu_device_set_version_format (dev, fu_device_get_version_format (device)); |
| 3925 | fu_device_set_version (dev, fu_device_get_version (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | /* add GUID */ |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3929 | fu_device_add_guid (dev, guid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3930 | } |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3931 | if (fu_device_get_guids(dev)->len == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3932 | g_set_error_literal (error, |
| 3933 | FWUPD_ERROR, |
| 3934 | FWUPD_ERROR_INTERNAL, |
| 3935 | "component has no GUIDs"); |
| 3936 | return NULL; |
| 3937 | } |
| 3938 | |
| 3939 | /* check we can install it */ |
Mario Limonciello | 13d0773 | 2020-10-23 17:18:43 -0500 | [diff] [blame] | 3940 | task = fu_install_task_new (NULL, component); |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3941 | if (!fu_engine_check_requirements (self, request, task, |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 3942 | FWUPD_INSTALL_FLAG_IGNORE_VID_PID, |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 3943 | error)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3944 | return NULL; |
| 3945 | |
| 3946 | /* verify trust */ |
Richard Hughes | 890dd12 | 2020-06-25 22:49:04 +0100 | [diff] [blame] | 3947 | #if LIBXMLB_CHECK_VERSION(0,2,0) |
| 3948 | query = xb_query_new_full (xb_node_get_silo (component), |
| 3949 | "releases/release", |
| 3950 | XB_QUERY_FLAG_FORCE_NODE_CACHE, |
| 3951 | error); |
| 3952 | if (query == NULL) |
| 3953 | return NULL; |
| 3954 | release = xb_node_query_first_full (component, query, &error_local); |
| 3955 | #else |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3956 | release = xb_node_query_first (component, |
| 3957 | "releases/release", |
| 3958 | &error_local); |
Richard Hughes | 890dd12 | 2020-06-25 22:49:04 +0100 | [diff] [blame] | 3959 | #endif |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3960 | if (release == NULL) { |
| 3961 | g_set_error (error, |
| 3962 | FWUPD_ERROR, |
| 3963 | FWUPD_ERROR_INTERNAL, |
| 3964 | "failed to get release: %s", |
| 3965 | error_local->message); |
| 3966 | return NULL; |
| 3967 | } |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 3968 | if (!fu_keyring_get_release_flags (release, |
| 3969 | &release_flags, |
| 3970 | &error_local)) { |
Salud Lemus | 980f2d9 | 2018-08-28 09:25:40 -0700 | [diff] [blame] | 3971 | if (g_error_matches (error_local, |
| 3972 | FWUPD_ERROR, |
| 3973 | FWUPD_ERROR_NOT_SUPPORTED)) { |
| 3974 | g_warning ("Ignoring verification: %s", |
| 3975 | error_local->message); |
| 3976 | } else { |
| 3977 | g_propagate_error (error, g_steal_pointer (&error_local)); |
| 3978 | return NULL; |
| 3979 | } |
| 3980 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3981 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3982 | /* create a result with all the metadata in */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3983 | description = xb_node_query_first (component, "description", NULL); |
| 3984 | if (description != NULL) { |
| 3985 | g_autofree gchar *xml = NULL; |
| 3986 | xml = xb_node_export (description, |
| 3987 | XB_NODE_EXPORT_FLAG_ONLY_CHILDREN, |
| 3988 | NULL); |
| 3989 | if (xml != NULL) |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3990 | fu_device_set_description (dev, xml); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 3991 | } |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 3992 | rel = fwupd_release_new (); |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 3993 | fwupd_release_set_flags (rel, release_flags); |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3994 | if (!fu_engine_set_release_from_appstream (self, dev, rel, component, release, error)) |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 3995 | return NULL; |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 3996 | fu_device_add_release (dev, rel); |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 3997 | return g_steal_pointer (&dev); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 3998 | } |
| 3999 | |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4000 | static gint |
| 4001 | fu_engine_get_details_sort_cb (gconstpointer a, gconstpointer b) |
| 4002 | { |
| 4003 | FuDevice *device1 = *((FuDevice **) a); |
| 4004 | FuDevice *device2 = *((FuDevice **) b); |
| 4005 | if (!fu_device_has_flag (device1, FWUPD_DEVICE_FLAG_UPDATABLE) && |
| 4006 | fu_device_has_flag (device2, FWUPD_DEVICE_FLAG_UPDATABLE)) |
| 4007 | return 1; |
| 4008 | if (fu_device_has_flag (device1, FWUPD_DEVICE_FLAG_UPDATABLE) && |
| 4009 | !fu_device_has_flag (device2, FWUPD_DEVICE_FLAG_UPDATABLE)) |
| 4010 | return -1; |
| 4011 | return 0; |
| 4012 | } |
| 4013 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4014 | /** |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 4015 | * fu_engine_get_details: |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4016 | * @self: A #FuEngine |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4017 | * @request: A #FuEngineRequest |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4018 | * @fd: A file descriptor |
| 4019 | * @error: A #GError, or %NULL |
| 4020 | * |
| 4021 | * Gets the details about a local file. |
| 4022 | * |
| 4023 | * Note: this will close the fd when done |
| 4024 | * |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 4025 | * Returns: (transfer container) (element-type FuDevice): results |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4026 | **/ |
| 4027 | GPtrArray * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4028 | fu_engine_get_details (FuEngine *self, FuEngineRequest *request, gint fd, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4029 | { |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4030 | const gchar *remote_id; |
| 4031 | g_autofree gchar *csum = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4032 | g_autoptr(GBytes) blob = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4033 | g_autoptr(GError) error_local = NULL; |
| 4034 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4035 | g_autoptr(GPtrArray) details = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4036 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4037 | |
| 4038 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4039 | g_return_val_if_fail (fd > 0, NULL); |
| 4040 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4041 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4042 | /* get all components */ |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 4043 | blob = fu_common_get_contents_fd (fd, |
| 4044 | fu_engine_get_archive_size_max (self), |
| 4045 | error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4046 | if (blob == NULL) |
| 4047 | return NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4048 | silo = fu_engine_get_silo_from_blob (self, blob, error); |
| 4049 | if (silo == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4050 | return NULL; |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 4051 | components = xb_silo_query (silo, |
| 4052 | "components/component[@type='firmware']", |
| 4053 | 0, &error_local); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4054 | if (components == NULL) { |
| 4055 | g_set_error (error, |
| 4056 | FWUPD_ERROR, |
| 4057 | FWUPD_ERROR_INVALID_FILE, |
| 4058 | "no components: %s", |
| 4059 | error_local->message); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4060 | return NULL; |
| 4061 | } |
| 4062 | |
Richard Hughes | ec6190c | 2018-11-25 12:19:33 +0000 | [diff] [blame] | 4063 | /* build the index */ |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 4064 | if (!xb_silo_query_build_index (silo, "components/component[@type='firmware']/provides/firmware", |
Richard Hughes | ec6190c | 2018-11-25 12:19:33 +0000 | [diff] [blame] | 4065 | "type", error)) |
Richard Hughes | 2eee258 | 2019-03-11 13:17:39 +0000 | [diff] [blame] | 4066 | return NULL; |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 4067 | if (!xb_silo_query_build_index (silo, "components/component[@type='firmware']/provides/firmware", |
Richard Hughes | ec6190c | 2018-11-25 12:19:33 +0000 | [diff] [blame] | 4068 | NULL, error)) |
Richard Hughes | 2eee258 | 2019-03-11 13:17:39 +0000 | [diff] [blame] | 4069 | return NULL; |
Richard Hughes | ec6190c | 2018-11-25 12:19:33 +0000 | [diff] [blame] | 4070 | |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4071 | /* does this exist in any enabled remote */ |
| 4072 | csum = g_compute_checksum_for_bytes (G_CHECKSUM_SHA1, blob); |
| 4073 | remote_id = fu_engine_get_remote_id_for_checksum (self, csum); |
| 4074 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4075 | /* create results with all the metadata in */ |
| 4076 | details = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4077 | for (guint i = 0; i < components->len; i++) { |
| 4078 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 4079 | FuDevice *dev; |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4080 | dev = fu_engine_get_result_from_component (self, request, component, error); |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4081 | if (dev == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4082 | return NULL; |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4083 | if (remote_id != NULL) { |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 4084 | FwupdRelease *rel = fu_device_get_release_default (dev); |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4085 | fwupd_release_set_remote_id (rel, remote_id); |
Richard Hughes | 75449d9 | 2019-04-17 13:36:31 +0100 | [diff] [blame] | 4086 | fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED); |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4087 | } |
Richard Hughes | cf10029 | 2021-01-04 10:36:37 +0000 | [diff] [blame] | 4088 | if (fu_device_has_internal_flag (dev, FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT)) |
Richard Hughes | 137649d | 2020-05-28 13:36:24 +0100 | [diff] [blame] | 4089 | fu_engine_md_refresh_device_verfmt (self, dev, component); |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4090 | |
| 4091 | /* if this matched a device on the system, ensure all the |
| 4092 | * requirements passed before setting UPDATABLE */ |
| 4093 | if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE)) { |
| 4094 | g_autoptr(FuInstallTask) task = fu_install_task_new (dev, component); |
| 4095 | g_autoptr(GError) error_req = NULL; |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4096 | if (!fu_engine_check_requirements (self, request, task, |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4097 | FWUPD_INSTALL_FLAG_OFFLINE | |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 4098 | FWUPD_INSTALL_FLAG_IGNORE_VID_PID | |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4099 | FWUPD_INSTALL_FLAG_ALLOW_REINSTALL | |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 4100 | FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH | |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4101 | FWUPD_INSTALL_FLAG_ALLOW_OLDER, |
| 4102 | &error_req)) { |
| 4103 | g_debug ("%s failed requirement checks: %s", |
| 4104 | fu_device_get_id (dev), |
| 4105 | error_req->message); |
| 4106 | fu_device_remove_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE); |
| 4107 | } else { |
| 4108 | g_debug ("%s passed requirement checks", |
| 4109 | fu_device_get_id (dev)); |
| 4110 | } |
| 4111 | } |
| 4112 | |
Richard Hughes | 534255c | 2018-01-28 19:51:56 +0000 | [diff] [blame] | 4113 | g_ptr_array_add (details, dev); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4114 | } |
Richard Hughes | b3d3f21 | 2020-05-21 21:14:49 +0100 | [diff] [blame] | 4115 | |
| 4116 | /* order multiple devices so that the one that passes the requirement |
| 4117 | * is listed first */ |
| 4118 | g_ptr_array_sort (details, fu_engine_get_details_sort_cb); |
| 4119 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4120 | return g_steal_pointer (&details); |
| 4121 | } |
| 4122 | |
Mario Limonciello | 343095d | 2018-10-23 17:21:13 -0500 | [diff] [blame] | 4123 | static gint |
Mario Limonciello | 6ff8a16 | 2019-08-23 12:55:10 -0500 | [diff] [blame] | 4124 | fu_engine_sort_devices_by_priority_name (gconstpointer a, gconstpointer b) |
Mario Limonciello | 343095d | 2018-10-23 17:21:13 -0500 | [diff] [blame] | 4125 | { |
| 4126 | FuDevice *dev_a = *((FuDevice **) a); |
| 4127 | FuDevice *dev_b = *((FuDevice **) b); |
| 4128 | gint prio_a = fu_device_get_priority (dev_a); |
| 4129 | gint prio_b = fu_device_get_priority (dev_b); |
Mario Limonciello | 6ff8a16 | 2019-08-23 12:55:10 -0500 | [diff] [blame] | 4130 | const gchar *name_a = fu_device_get_name (dev_a); |
| 4131 | const gchar *name_b = fu_device_get_name (dev_b); |
Mario Limonciello | 343095d | 2018-10-23 17:21:13 -0500 | [diff] [blame] | 4132 | |
| 4133 | if (prio_a > prio_b) |
| 4134 | return -1; |
| 4135 | if (prio_a < prio_b) |
| 4136 | return 1; |
Mario Limonciello | 6ff8a16 | 2019-08-23 12:55:10 -0500 | [diff] [blame] | 4137 | if (g_strcmp0 (name_a, name_b) > 0) |
| 4138 | return 1; |
| 4139 | if (g_strcmp0 (name_a, name_b) < 0) |
| 4140 | return -1; |
Mario Limonciello | 343095d | 2018-10-23 17:21:13 -0500 | [diff] [blame] | 4141 | return 0; |
| 4142 | } |
| 4143 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4144 | /** |
| 4145 | * fu_engine_get_devices: |
| 4146 | * @self: A #FuEngine |
| 4147 | * @error: A #GError, or %NULL |
| 4148 | * |
| 4149 | * Gets the list of devices. |
| 4150 | * |
| 4151 | * Returns: (transfer container) (element-type FwupdDevice): results |
| 4152 | **/ |
| 4153 | GPtrArray * |
| 4154 | fu_engine_get_devices (FuEngine *self, GError **error) |
| 4155 | { |
Richard Hughes | f2eccde | 2017-09-20 11:18:03 +0100 | [diff] [blame] | 4156 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4157 | |
| 4158 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4159 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4160 | |
Richard Hughes | 70425fe | 2017-11-22 12:33:27 +0000 | [diff] [blame] | 4161 | devices = fu_device_list_get_active (self->device_list); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4162 | if (devices->len == 0) { |
| 4163 | g_set_error_literal (error, |
| 4164 | FWUPD_ERROR, |
| 4165 | FWUPD_ERROR_NOTHING_TO_DO, |
| 4166 | "No detected devices"); |
| 4167 | return NULL; |
| 4168 | } |
Mario Limonciello | 6ff8a16 | 2019-08-23 12:55:10 -0500 | [diff] [blame] | 4169 | g_ptr_array_sort (devices, fu_engine_sort_devices_by_priority_name); |
Richard Hughes | f2eccde | 2017-09-20 11:18:03 +0100 | [diff] [blame] | 4170 | return g_steal_pointer (&devices); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4171 | } |
| 4172 | |
Richard Hughes | 12040b5 | 2018-05-14 13:32:10 +0100 | [diff] [blame] | 4173 | /** |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 4174 | * fu_engine_get_devices_by_guid: |
| 4175 | * @self: A #FuEngine |
| 4176 | * @guid: A GUID |
| 4177 | * @error: A #GError, or %NULL |
| 4178 | * |
| 4179 | * Gets a specific device. |
| 4180 | * |
| 4181 | * Returns: (transfer full): a device, or %NULL if not found |
| 4182 | **/ |
| 4183 | GPtrArray * |
| 4184 | fu_engine_get_devices_by_guid (FuEngine *self, const gchar *guid, GError **error) |
| 4185 | { |
| 4186 | g_autoptr(GPtrArray) devices = NULL; |
| 4187 | g_autoptr(GPtrArray) devices_tmp = NULL; |
| 4188 | |
| 4189 | /* find the devices by GUID */ |
| 4190 | devices_tmp = fu_device_list_get_all (self->device_list); |
| 4191 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 4192 | for (guint i = 0; i < devices_tmp->len; i++) { |
| 4193 | FuDevice *dev_tmp = g_ptr_array_index (devices_tmp, i); |
| 4194 | if (fu_device_has_guid (dev_tmp, guid)) |
| 4195 | g_ptr_array_add (devices, g_object_ref (dev_tmp)); |
| 4196 | } |
| 4197 | |
| 4198 | /* nothing */ |
| 4199 | if (devices->len == 0) { |
| 4200 | g_set_error (error, |
| 4201 | FWUPD_ERROR, |
| 4202 | FWUPD_ERROR_NOT_FOUND, |
| 4203 | "failed to find any device providing %s", guid); |
| 4204 | return NULL; |
| 4205 | } |
| 4206 | |
| 4207 | /* success */ |
| 4208 | return g_steal_pointer (&devices); |
| 4209 | } |
| 4210 | |
Richard Hughes | 63bc42e | 2021-04-22 09:32:07 +0100 | [diff] [blame] | 4211 | /** |
| 4212 | * fu_engine_get_devices_by_composite_id: |
| 4213 | * @self: A #FuEngine |
| 4214 | * @composite_id: A device ID |
| 4215 | * @error: A #GError, or %NULL |
| 4216 | * |
| 4217 | * Gets all devices that match a specific composite ID. |
| 4218 | * |
| 4219 | * Returns: (transfer full) (element-type FuDevice): devices |
| 4220 | **/ |
| 4221 | GPtrArray * |
| 4222 | fu_engine_get_devices_by_composite_id (FuEngine *self, |
| 4223 | const gchar *composite_id, |
| 4224 | GError **error) |
| 4225 | { |
| 4226 | g_autoptr(GPtrArray) devices = NULL; |
| 4227 | g_autoptr(GPtrArray) devices_tmp = NULL; |
| 4228 | |
| 4229 | /* find the devices by composite ID */ |
| 4230 | devices_tmp = fu_device_list_get_all (self->device_list); |
| 4231 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 4232 | for (guint i = 0; i < devices_tmp->len; i++) { |
| 4233 | FuDevice *dev_tmp = g_ptr_array_index (devices_tmp, i); |
| 4234 | if (g_strcmp0 (fu_device_get_composite_id (dev_tmp), composite_id) == 0) |
| 4235 | g_ptr_array_add (devices, g_object_ref (dev_tmp)); |
| 4236 | } |
| 4237 | |
| 4238 | /* nothing */ |
| 4239 | if (devices->len == 0) { |
| 4240 | g_set_error (error, |
| 4241 | FWUPD_ERROR, |
| 4242 | FWUPD_ERROR_NOT_FOUND, |
| 4243 | "failed to find any device with composite ID %s", |
| 4244 | composite_id); |
| 4245 | return NULL; |
| 4246 | } |
| 4247 | |
| 4248 | /* success */ |
| 4249 | return g_steal_pointer (&devices); |
| 4250 | } |
| 4251 | |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4252 | static void |
| 4253 | fu_engine_get_history_set_hsi_attrs (FuEngine *self, FuDevice *device) |
| 4254 | { |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4255 | g_autoptr(GPtrArray) vals = NULL; |
| 4256 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 4257 | /* ensure up to date */ |
| 4258 | fu_engine_ensure_security_attrs (self); |
| 4259 | |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4260 | /* add attributes */ |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 4261 | vals = fu_security_attrs_get_all (self->host_security_attrs); |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4262 | for (guint i = 0; i < vals->len; i++) { |
| 4263 | FwupdSecurityAttr *attr = g_ptr_array_index (vals, i); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 4264 | const gchar *tmp; |
| 4265 | tmp = fwupd_security_attr_result_to_string (fwupd_security_attr_get_result (attr)); |
| 4266 | fu_device_set_metadata (device, fwupd_security_attr_get_appstream_id (attr), tmp); |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | /* computed value */ |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 4270 | fu_device_set_metadata (device, "HSI", self->host_security_id); |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4271 | } |
| 4272 | |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 4273 | /** |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 4274 | * fu_engine_get_history: |
| 4275 | * @self: A #FuEngine |
| 4276 | * @error: A #GError, or %NULL |
| 4277 | * |
| 4278 | * Gets the list of history. |
| 4279 | * |
| 4280 | * Returns: (transfer container) (element-type FwupdDevice): results |
| 4281 | **/ |
| 4282 | GPtrArray * |
| 4283 | fu_engine_get_history (FuEngine *self, GError **error) |
| 4284 | { |
| 4285 | g_autoptr(GPtrArray) devices = NULL; |
| 4286 | |
| 4287 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4288 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4289 | |
| 4290 | devices = fu_history_get_devices (self->history, error); |
| 4291 | if (devices == NULL) |
| 4292 | return NULL; |
| 4293 | if (devices->len == 0) { |
| 4294 | g_set_error_literal (error, |
| 4295 | FWUPD_ERROR, |
| 4296 | FWUPD_ERROR_NOTHING_TO_DO, |
| 4297 | "No history"); |
| 4298 | return NULL; |
| 4299 | } |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 4300 | |
Richard Hughes | cef874f | 2020-05-17 21:03:13 +0100 | [diff] [blame] | 4301 | /* if this is the system firmware device, add the HSI attrs */ |
| 4302 | for (guint i = 0; i < devices->len; i++) { |
| 4303 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 4304 | if (fu_device_has_instance_id (dev, "main-system-firmware")) |
| 4305 | fu_engine_get_history_set_hsi_attrs (self, dev); |
| 4306 | } |
| 4307 | |
Richard Hughes | 611f1a9 | 2018-01-11 11:54:28 +0000 | [diff] [blame] | 4308 | /* try to set the remote ID for each device */ |
| 4309 | for (guint i = 0; i < devices->len; i++) { |
| 4310 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 4311 | FwupdRelease *rel; |
| 4312 | GPtrArray *csums; |
| 4313 | |
| 4314 | /* get the checksums */ |
| 4315 | rel = fu_device_get_release_default (dev); |
| 4316 | if (rel == NULL) |
| 4317 | continue; |
| 4318 | |
| 4319 | /* find the checksum that matches */ |
| 4320 | csums = fwupd_release_get_checksums (rel); |
| 4321 | for (guint j = 0; j < csums->len; j++) { |
| 4322 | const gchar *csum = g_ptr_array_index (csums, j); |
| 4323 | const gchar *remote_id = fu_engine_get_remote_id_for_checksum (self, csum); |
| 4324 | if (remote_id != NULL) { |
| 4325 | fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED); |
| 4326 | fwupd_release_set_remote_id (rel, remote_id); |
| 4327 | break; |
| 4328 | } |
| 4329 | } |
| 4330 | } |
| 4331 | |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 4332 | return g_steal_pointer (&devices); |
| 4333 | } |
| 4334 | |
Richard Hughes | 50ff31d | 2020-09-09 17:07:38 +0100 | [diff] [blame] | 4335 | #if !GLIB_CHECK_VERSION(2,62,0) |
| 4336 | static GPtrArray * |
| 4337 | g_ptr_array_copy (GPtrArray *array, GCopyFunc func, gpointer user_data) |
| 4338 | { |
| 4339 | GPtrArray *new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 4340 | for (guint i = 0; i < array->len; i++) { |
| 4341 | GObject *obj = g_ptr_array_index (array, i); |
| 4342 | g_ptr_array_add (new, g_object_ref (obj)); |
| 4343 | } |
| 4344 | return new; |
| 4345 | } |
| 4346 | #endif |
| 4347 | |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 4348 | /** |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4349 | * fu_engine_get_remotes: |
| 4350 | * @self: A #FuEngine |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4351 | * @error: A #GError, or %NULL |
| 4352 | * |
| 4353 | * Gets the list of remotes in use by the engine. |
| 4354 | * |
| 4355 | * Returns: (transfer container) (element-type FwupdRemote): results |
| 4356 | **/ |
| 4357 | GPtrArray * |
| 4358 | fu_engine_get_remotes (FuEngine *self, GError **error) |
| 4359 | { |
| 4360 | GPtrArray *remotes; |
| 4361 | |
| 4362 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4363 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4364 | |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 4365 | remotes = fu_remote_list_get_all (self->remote_list); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4366 | if (remotes->len == 0) { |
| 4367 | g_set_error (error, |
| 4368 | FWUPD_ERROR, |
| 4369 | FWUPD_ERROR_INTERNAL, |
| 4370 | "No remotes configured"); |
| 4371 | return NULL; |
| 4372 | } |
Richard Hughes | 50ff31d | 2020-09-09 17:07:38 +0100 | [diff] [blame] | 4373 | |
| 4374 | /* deep copy so the remote list can be kept up to date */ |
| 4375 | return g_ptr_array_copy (remotes, (GCopyFunc) g_object_ref, NULL); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 4378 | /** |
| 4379 | * fu_engine_get_remote_by_id: |
| 4380 | * @self: A #FuEngine |
| 4381 | * @remote_id: A string representation of a remote |
| 4382 | * @error: A #GError, or %NULL |
| 4383 | * |
| 4384 | * Gets the FwupdRemote object. |
| 4385 | * |
| 4386 | * Returns: FwupdRemote |
| 4387 | **/ |
| 4388 | FwupdRemote * |
| 4389 | fu_engine_get_remote_by_id (FuEngine *self, const gchar *remote_id, GError **error) |
| 4390 | { |
| 4391 | g_autoptr(GPtrArray) remotes = NULL; |
| 4392 | |
| 4393 | remotes = fu_engine_get_remotes (self, error); |
| 4394 | if (remotes == NULL) |
| 4395 | return NULL; |
| 4396 | |
| 4397 | for (guint i = 0; i < remotes->len; i++) { |
| 4398 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
| 4399 | if (g_strcmp0 (remote_id, fwupd_remote_get_id (remote)) == 0) |
| 4400 | return remote; |
| 4401 | } |
| 4402 | |
| 4403 | g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, |
| 4404 | "Couldn't find remote %s", remote_id); |
| 4405 | |
| 4406 | return NULL; |
| 4407 | } |
| 4408 | |
| 4409 | |
Richard Hughes | 225f3a9 | 2017-09-13 19:38:51 +0100 | [diff] [blame] | 4410 | static gint |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4411 | fu_engine_sort_releases_cb (gconstpointer a, gconstpointer b, gpointer user_data) |
Richard Hughes | 225f3a9 | 2017-09-13 19:38:51 +0100 | [diff] [blame] | 4412 | { |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4413 | FuDevice *device = FU_DEVICE (user_data); |
Richard Hughes | 225f3a9 | 2017-09-13 19:38:51 +0100 | [diff] [blame] | 4414 | FwupdRelease *rel_a = FWUPD_RELEASE (*((FwupdRelease **) a)); |
| 4415 | FwupdRelease *rel_b = FWUPD_RELEASE (*((FwupdRelease **) b)); |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4416 | gint rc; |
| 4417 | |
| 4418 | /* first by branch */ |
| 4419 | rc = g_strcmp0 (fwupd_release_get_branch (rel_b), fwupd_release_get_branch (rel_a)); |
| 4420 | if (rc != 0) |
| 4421 | return rc; |
| 4422 | |
| 4423 | /* then by version */ |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4424 | return fu_common_vercmp_full (fwupd_release_get_version (rel_b), |
| 4425 | fwupd_release_get_version (rel_a), |
| 4426 | fu_device_get_version_format (device)); |
Richard Hughes | 225f3a9 | 2017-09-13 19:38:51 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4429 | static gboolean |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4430 | fu_engine_check_release_is_approved (FuEngine *self, FwupdRelease *rel) |
| 4431 | { |
| 4432 | GPtrArray *csums = fwupd_release_get_checksums (rel); |
Richard Hughes | 3444cf6 | 2020-06-22 15:10:28 +0100 | [diff] [blame] | 4433 | if (self->approved_firmware == NULL) |
| 4434 | return FALSE; |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4435 | for (guint i = 0; i < csums->len; i++) { |
| 4436 | const gchar *csum = g_ptr_array_index (csums, i); |
| 4437 | g_debug ("checking %s against approved list", csum); |
| 4438 | if (g_hash_table_lookup (self->approved_firmware, csum) != NULL) |
| 4439 | return TRUE; |
| 4440 | } |
| 4441 | return FALSE; |
| 4442 | } |
| 4443 | |
| 4444 | static gboolean |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 4445 | fu_engine_check_release_is_blocked (FuEngine *self, FwupdRelease *rel) |
| 4446 | { |
| 4447 | GPtrArray *csums = fwupd_release_get_checksums (rel); |
| 4448 | if (self->blocked_firmware == NULL) |
| 4449 | return FALSE; |
| 4450 | for (guint i = 0; i < csums->len; i++) { |
| 4451 | const gchar *csum = g_ptr_array_index (csums, i); |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 4452 | if (g_hash_table_lookup (self->blocked_firmware, csum) != NULL) |
| 4453 | return TRUE; |
| 4454 | } |
| 4455 | return FALSE; |
| 4456 | } |
| 4457 | |
| 4458 | static gboolean |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4459 | fu_engine_add_releases_for_device_component (FuEngine *self, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4460 | FuEngineRequest *request, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4461 | FuDevice *device, |
| 4462 | XbNode *component, |
| 4463 | GPtrArray *releases, |
| 4464 | GError **error) |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 4465 | { |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4466 | FwupdFeatureFlags feature_flags; |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4467 | FwupdVersionFormat fmt = fu_device_get_version_format (device); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4468 | g_autoptr(GError) error_local = NULL; |
| 4469 | g_autoptr(FuInstallTask) task = fu_install_task_new (device, component); |
| 4470 | g_autoptr(GPtrArray) releases_tmp = NULL; |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 4471 | |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4472 | if (!fu_engine_check_requirements (self, request, task, |
Richard Hughes | ce75627 | 2019-03-24 12:21:34 +0000 | [diff] [blame] | 4473 | FWUPD_INSTALL_FLAG_OFFLINE | |
Richard Hughes | bf43978 | 2020-11-03 11:32:29 +0000 | [diff] [blame] | 4474 | FWUPD_INSTALL_FLAG_IGNORE_VID_PID | |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 4475 | FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4476 | FWUPD_INSTALL_FLAG_ALLOW_REINSTALL | |
| 4477 | FWUPD_INSTALL_FLAG_ALLOW_OLDER, |
| 4478 | error)) |
| 4479 | return FALSE; |
| 4480 | |
| 4481 | /* get all releases */ |
| 4482 | releases_tmp = xb_node_query (component, "releases/release", 0, &error_local); |
| 4483 | if (releases_tmp == NULL) { |
| 4484 | if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
| 4485 | return TRUE; |
| 4486 | if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) |
| 4487 | return TRUE; |
| 4488 | g_propagate_error (error, g_steal_pointer (&error_local)); |
| 4489 | return FALSE; |
| 4490 | } |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4491 | feature_flags = fu_engine_request_get_feature_flags (request); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4492 | for (guint i = 0; i < releases_tmp->len; i++) { |
| 4493 | XbNode *release = g_ptr_array_index (releases_tmp, i); |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4494 | const gchar *remote_id; |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 4495 | const gchar *update_message; |
Richard Hughes | 4fd4b98 | 2020-06-25 18:58:57 +0100 | [diff] [blame] | 4496 | const gchar *update_image; |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4497 | gint vercmp; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4498 | GPtrArray *checksums; |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 4499 | GPtrArray *locations; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4500 | g_autoptr(FwupdRelease) rel = fwupd_release_new (); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 4501 | g_autoptr(GError) error_loop = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4502 | |
| 4503 | /* create new FwupdRelease for the XbNode */ |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 4504 | if (!fu_engine_set_release_from_appstream (self, |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 4505 | device, |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 4506 | rel, |
| 4507 | component, |
| 4508 | release, |
| 4509 | &error_loop)) { |
| 4510 | g_warning ("failed to set release for component: %s", |
| 4511 | error_loop->message); |
| 4512 | continue; |
| 4513 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4514 | |
Richard Hughes | 6e0c8f8 | 2018-11-11 21:46:41 +0000 | [diff] [blame] | 4515 | /* fall back to quirk-provided value */ |
| 4516 | if (fwupd_release_get_install_duration (rel) == 0) |
| 4517 | fwupd_release_set_install_duration (rel, fu_device_get_install_duration (device)); |
| 4518 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4519 | /* invalid */ |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 4520 | locations = fwupd_release_get_locations (rel); |
| 4521 | if (locations->len == 0) |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 4522 | continue; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4523 | checksums = fwupd_release_get_checksums (rel); |
| 4524 | if (checksums->len == 0) |
| 4525 | continue; |
| 4526 | |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4527 | /* different branch */ |
| 4528 | if (g_strcmp0 (fwupd_release_get_branch (rel), |
| 4529 | fu_device_get_branch (device)) != 0) { |
| 4530 | if ((feature_flags & FWUPD_FEATURE_FLAG_SWITCH_BRANCH) == 0) { |
| 4531 | g_debug ("client does not understand branches, skipping %s:%s", |
| 4532 | fwupd_release_get_branch (rel), |
| 4533 | fwupd_release_get_version (rel)); |
| 4534 | continue; |
| 4535 | } |
| 4536 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_IS_ALTERNATE_BRANCH); |
| 4537 | } |
| 4538 | |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4539 | /* test for upgrade or downgrade */ |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4540 | vercmp = fu_common_vercmp_full (fwupd_release_get_version (rel), |
| 4541 | fu_device_get_version (device), |
| 4542 | fmt); |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4543 | if (vercmp > 0) |
| 4544 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_IS_UPGRADE); |
| 4545 | else if (vercmp < 0) |
| 4546 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_IS_DOWNGRADE); |
| 4547 | |
| 4548 | /* lower than allowed to downgrade to */ |
| 4549 | if (fu_device_get_version_lowest (device) != NULL && |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4550 | fu_common_vercmp_full (fwupd_release_get_version (rel), |
| 4551 | fu_device_get_version_lowest (device), |
| 4552 | fmt) < 0) { |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4553 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_BLOCKED_VERSION); |
| 4554 | } |
| 4555 | |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 4556 | /* manually blocked */ |
| 4557 | if (fu_engine_check_release_is_blocked (self, rel)) |
| 4558 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_BLOCKED_APPROVAL); |
| 4559 | |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 4560 | /* check if remote is filtering firmware */ |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4561 | remote_id = fwupd_release_get_remote_id (rel); |
| 4562 | if (remote_id != NULL) { |
| 4563 | FwupdRemote *remote = fu_engine_get_remote_by_id (self, remote_id, NULL); |
| 4564 | if (remote != NULL && |
| 4565 | fwupd_remote_get_approval_required (remote) && |
| 4566 | !fu_engine_check_release_is_approved (self, rel)) { |
| 4567 | fwupd_release_add_flag (rel, FWUPD_RELEASE_FLAG_BLOCKED_APPROVAL); |
| 4568 | } |
| 4569 | } |
| 4570 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 4571 | /* add update message if exists but device doesn't already have one */ |
| 4572 | update_message = fwupd_release_get_update_message (rel); |
| 4573 | if (fwupd_device_get_update_message (FWUPD_DEVICE (device)) == NULL && |
| 4574 | update_message != NULL) { |
Richard Hughes | 4fd4b98 | 2020-06-25 18:58:57 +0100 | [diff] [blame] | 4575 | fwupd_device_set_update_message (FWUPD_DEVICE (device), update_message); |
| 4576 | } |
| 4577 | update_image = fwupd_release_get_update_image (rel); |
| 4578 | if (fwupd_device_get_update_image (FWUPD_DEVICE (device)) == NULL && |
| 4579 | update_image != NULL) { |
| 4580 | fwupd_device_set_update_image (FWUPD_DEVICE (device), update_image); |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 4581 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4582 | /* success */ |
| 4583 | g_ptr_array_add (releases, g_steal_pointer (&rel)); |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4586 | /* success */ |
| 4587 | return TRUE; |
Richard Hughes | 650dade | 2017-12-14 14:43:11 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Richard Hughes | 88fdc59 | 2020-10-15 10:00:41 +0100 | [diff] [blame] | 4590 | static const gchar * |
| 4591 | fu_engine_get_branch_fallback (const gchar *nullable_branch) |
| 4592 | { |
| 4593 | if (nullable_branch == NULL) |
| 4594 | return "default"; |
| 4595 | return nullable_branch; |
| 4596 | } |
| 4597 | |
Filipe Laíns | d3a4fd3 | 2020-03-30 21:05:14 +0100 | [diff] [blame] | 4598 | GPtrArray * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4599 | fu_engine_get_releases_for_device (FuEngine *self, |
| 4600 | FuEngineRequest *request, |
| 4601 | FuDevice *device, |
| 4602 | GError **error) |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4603 | { |
| 4604 | GPtrArray *device_guids; |
| 4605 | GPtrArray *releases; |
| 4606 | const gchar *version; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4607 | g_autoptr(GError) error_all = NULL; |
| 4608 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4609 | g_autoptr(GPtrArray) branches = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4610 | g_autoptr(GPtrArray) components = NULL; |
| 4611 | g_autoptr(GString) xpath = g_string_new (NULL); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4612 | |
| 4613 | /* get device version */ |
| 4614 | version = fu_device_get_version (device); |
| 4615 | if (version == NULL) { |
| 4616 | g_set_error (error, |
| 4617 | FWUPD_ERROR, |
| 4618 | FWUPD_ERROR_NOT_SUPPORTED, |
| 4619 | "no version set"); |
| 4620 | return NULL; |
| 4621 | } |
| 4622 | |
| 4623 | /* only show devices that can be updated */ |
Richard Hughes | 6b64bc7 | 2021-04-22 12:06:04 +0100 | [diff] [blame] | 4624 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE) && |
| 4625 | !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE_HIDDEN)) { |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4626 | g_set_error_literal (error, |
| 4627 | FWUPD_ERROR, |
| 4628 | FWUPD_ERROR_NOT_SUPPORTED, |
| 4629 | "is not updatable"); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4630 | return NULL; |
| 4631 | } |
| 4632 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4633 | /* get all the components that provide any of these GUIDs */ |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4634 | device_guids = fu_device_get_guids (device); |
| 4635 | for (guint i = 0; i < device_guids->len; i++) { |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4636 | const gchar *guid = g_ptr_array_index (device_guids, i); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4637 | xb_string_append_union (xpath, |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 4638 | "components/component[@type='firmware']/" |
Richard Hughes | 634da03 | 2018-11-05 11:42:20 +0000 | [diff] [blame] | 4639 | "provides/firmware[@type=$'flashed'][text()=$'%s']/" |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4640 | "../..", guid); |
| 4641 | } |
| 4642 | components = xb_silo_query (self->silo, xpath->str, 0, &error_local); |
| 4643 | if (components == NULL) { |
Richard Hughes | a07a80f | 2018-12-03 14:57:33 +0000 | [diff] [blame] | 4644 | if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) || |
| 4645 | g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) { |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4646 | g_set_error_literal (error, |
| 4647 | FWUPD_ERROR, |
| 4648 | FWUPD_ERROR_NOTHING_TO_DO, |
| 4649 | "No releases found"); |
Richard Hughes | b095df6 | 2018-11-05 11:41:36 +0000 | [diff] [blame] | 4650 | return NULL; |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4651 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4652 | g_propagate_error (error, g_steal_pointer (&error_local)); |
Richard Hughes | b095df6 | 2018-11-05 11:41:36 +0000 | [diff] [blame] | 4653 | return NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | /* find all the releases that pass all the requirements */ |
| 4657 | releases = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 4658 | for (guint i = 0; i < components->len; i++) { |
| 4659 | XbNode *component = XB_NODE (g_ptr_array_index (components, i)); |
| 4660 | g_autoptr(GError) error_tmp = NULL; |
| 4661 | if (!fu_engine_add_releases_for_device_component (self, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4662 | request, |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4663 | device, |
| 4664 | component, |
| 4665 | releases, |
| 4666 | &error_tmp)) { |
| 4667 | if (error_all == NULL) { |
| 4668 | error_all = g_steal_pointer (&error_tmp); |
| 4669 | continue; |
| 4670 | } |
| 4671 | |
| 4672 | /* assume the domain and code is the same */ |
| 4673 | g_prefix_error (&error_all, "%s, ", error_tmp->message); |
| 4674 | } |
| 4675 | } |
| 4676 | |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4677 | /* are there multiple branches available */ |
| 4678 | branches = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 88fdc59 | 2020-10-15 10:00:41 +0100 | [diff] [blame] | 4679 | g_ptr_array_add (branches, |
| 4680 | g_strdup (fu_engine_get_branch_fallback (fu_device_get_branch (device)))); |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4681 | for (guint i = 0; i < releases->len; i++) { |
| 4682 | FwupdRelease *rel_tmp = FWUPD_RELEASE (g_ptr_array_index (releases, i)); |
Richard Hughes | 88fdc59 | 2020-10-15 10:00:41 +0100 | [diff] [blame] | 4683 | const gchar *branch_tmp = fu_engine_get_branch_fallback (fwupd_release_get_branch (rel_tmp)); |
Richard Hughes | db8533f | 2020-12-14 12:04:30 +0000 | [diff] [blame] | 4684 | #if GLIB_CHECK_VERSION(2,54,3) |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4685 | if (g_ptr_array_find_with_equal_func (branches, branch_tmp, |
| 4686 | g_str_equal, NULL)) |
| 4687 | continue; |
Richard Hughes | db8533f | 2020-12-14 12:04:30 +0000 | [diff] [blame] | 4688 | #endif |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4689 | g_ptr_array_add (branches, g_strdup (branch_tmp)); |
| 4690 | } |
Richard Hughes | f64e7a9 | 2020-10-01 18:05:36 +0100 | [diff] [blame] | 4691 | if (branches->len > 1) |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4692 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_HAS_MULTIPLE_BRANCHES); |
| 4693 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4694 | /* return the compound error */ |
| 4695 | if (releases->len == 0) { |
Richard Hughes | d56ad5b | 2018-11-12 10:48:06 +0000 | [diff] [blame] | 4696 | if (error_all != NULL) { |
Richard Hughes | 5b715e1 | 2019-04-08 13:33:59 +0100 | [diff] [blame] | 4697 | g_propagate_prefixed_error (error, g_steal_pointer (&error_all), |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4698 | "No releases found: "); |
Richard Hughes | d56ad5b | 2018-11-12 10:48:06 +0000 | [diff] [blame] | 4699 | return NULL; |
| 4700 | } |
| 4701 | g_set_error (error, |
| 4702 | FWUPD_ERROR, |
| 4703 | FWUPD_ERROR_NOTHING_TO_DO, |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4704 | "No releases found"); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 4705 | return NULL; |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4706 | } |
| 4707 | return releases; |
| 4708 | } |
| 4709 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4710 | /** |
| 4711 | * fu_engine_get_releases: |
| 4712 | * @self: A #FuEngine |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4713 | * @request: A #FuEngineRequest |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4714 | * @device_id: A device ID |
| 4715 | * @error: A #GError, or %NULL |
| 4716 | * |
| 4717 | * Gets the releases available for a specific device. |
| 4718 | * |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 4719 | * Returns: (transfer container) (element-type FwupdDevice): results |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4720 | **/ |
| 4721 | GPtrArray * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4722 | fu_engine_get_releases (FuEngine *self, |
| 4723 | FuEngineRequest *request, |
| 4724 | const gchar *device_id, |
| 4725 | GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4726 | { |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 4727 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4728 | g_autoptr(GPtrArray) releases = NULL; |
| 4729 | |
| 4730 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4731 | g_return_val_if_fail (device_id != NULL, NULL); |
| 4732 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4733 | |
| 4734 | /* find the device */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 4735 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4736 | if (device == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4737 | return NULL; |
| 4738 | |
| 4739 | /* get all the releases for the device */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4740 | releases = fu_engine_get_releases_for_device (self, request, device, error); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4741 | if (releases == NULL) |
| 4742 | return NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4743 | if (releases->len == 0) { |
| 4744 | g_set_error_literal (error, |
| 4745 | FWUPD_ERROR, |
| 4746 | FWUPD_ERROR_NOTHING_TO_DO, |
| 4747 | "No releases for device"); |
| 4748 | return NULL; |
| 4749 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4750 | g_ptr_array_sort_with_data (releases, fu_engine_sort_releases_cb, device); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4751 | return g_steal_pointer (&releases); |
| 4752 | } |
| 4753 | |
| 4754 | /** |
| 4755 | * fu_engine_get_downgrades: |
| 4756 | * @self: A #FuEngine |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4757 | * @request: A #FuEngineRequest |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4758 | * @device_id: A device ID |
| 4759 | * @error: A #GError, or %NULL |
| 4760 | * |
| 4761 | * Gets the downgrades available for a specific device. |
| 4762 | * |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 4763 | * Returns: (transfer container) (element-type FwupdDevice): results |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4764 | **/ |
| 4765 | GPtrArray * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4766 | fu_engine_get_downgrades (FuEngine *self, |
| 4767 | FuEngineRequest *request, |
| 4768 | const gchar *device_id, |
| 4769 | GError **error) |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4770 | { |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 4771 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4772 | g_autoptr(GPtrArray) releases = NULL; |
| 4773 | g_autoptr(GPtrArray) releases_tmp = NULL; |
| 4774 | g_autoptr(GString) error_str = g_string_new (NULL); |
| 4775 | |
| 4776 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4777 | g_return_val_if_fail (device_id != NULL, NULL); |
| 4778 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4779 | |
| 4780 | /* find the device */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 4781 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4782 | if (device == NULL) |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4783 | return NULL; |
| 4784 | |
| 4785 | /* get all the releases for the device */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4786 | releases_tmp = fu_engine_get_releases_for_device (self, request, device, error); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4787 | if (releases_tmp == NULL) |
| 4788 | return NULL; |
| 4789 | releases = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 4790 | for (guint i = 0; i < releases_tmp->len; i++) { |
| 4791 | FwupdRelease *rel_tmp = g_ptr_array_index (releases_tmp, i); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4792 | |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4793 | /* same as installed */ |
| 4794 | if (!fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_UPGRADE) && |
| 4795 | !fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_DOWNGRADE)) { |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4796 | g_string_append_printf (error_str, "%s=same, ", |
| 4797 | fwupd_release_get_version (rel_tmp)); |
| 4798 | g_debug ("ignoring %s as the same as %s", |
| 4799 | fwupd_release_get_version (rel_tmp), |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4800 | fu_device_get_version (device)); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4801 | continue; |
| 4802 | } |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4803 | |
| 4804 | /* newer than current */ |
| 4805 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_UPGRADE)) { |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4806 | g_string_append_printf (error_str, "%s=newer, ", |
| 4807 | fwupd_release_get_version (rel_tmp)); |
| 4808 | g_debug ("ignoring %s as newer than %s", |
| 4809 | fwupd_release_get_version (rel_tmp), |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4810 | fu_device_get_version (device)); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4811 | continue; |
| 4812 | } |
| 4813 | |
Richard Hughes | dce9120 | 2019-04-08 12:47:45 +0100 | [diff] [blame] | 4814 | /* don't show releases we are not allowed to downgrade to */ |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 4815 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_BLOCKED_VERSION)) { |
| 4816 | g_string_append_printf (error_str, "%s=lowest, ", |
| 4817 | fwupd_release_get_version (rel_tmp)); |
| 4818 | g_debug ("ignoring %s as older than lowest %s", |
| 4819 | fwupd_release_get_version (rel_tmp), |
| 4820 | fu_device_get_version_lowest (device)); |
| 4821 | continue; |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4822 | } |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 4823 | |
| 4824 | /* different branch */ |
| 4825 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_ALTERNATE_BRANCH)) { |
| 4826 | g_debug ("ignoring release %s as branch %s, and device is %s", |
| 4827 | fwupd_release_get_version (rel_tmp), |
| 4828 | fwupd_release_get_branch (rel_tmp), |
| 4829 | fu_device_get_branch (device)); |
| 4830 | continue; |
| 4831 | } |
| 4832 | |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4833 | g_ptr_array_add (releases, g_object_ref (rel_tmp)); |
| 4834 | } |
| 4835 | if (error_str->len > 2) |
| 4836 | g_string_truncate (error_str, error_str->len - 2); |
| 4837 | if (releases->len == 0) { |
| 4838 | if (error_str->len > 0) { |
| 4839 | g_set_error (error, |
| 4840 | FWUPD_ERROR, |
| 4841 | FWUPD_ERROR_NOTHING_TO_DO, |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4842 | "current version is %s: %s", |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4843 | fu_device_get_version (device), |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4844 | error_str->str); |
| 4845 | } else { |
| 4846 | g_set_error (error, |
| 4847 | FWUPD_ERROR, |
| 4848 | FWUPD_ERROR_NOTHING_TO_DO, |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4849 | "current version is %s", |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4850 | fu_device_get_version (device)); |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 4851 | } |
| 4852 | return NULL; |
| 4853 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 4854 | g_ptr_array_sort_with_data (releases, fu_engine_sort_releases_cb, device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4855 | return g_steal_pointer (&releases); |
| 4856 | } |
| 4857 | |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4858 | GPtrArray * |
| 4859 | fu_engine_get_approved_firmware (FuEngine *self) |
| 4860 | { |
| 4861 | GPtrArray *checksums = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 3444cf6 | 2020-06-22 15:10:28 +0100 | [diff] [blame] | 4862 | if (self->approved_firmware != NULL) { |
| 4863 | g_autoptr(GList) keys = g_hash_table_get_keys (self->approved_firmware); |
| 4864 | for (GList *l = keys; l != NULL; l = l->next) { |
| 4865 | const gchar *csum = l->data; |
| 4866 | g_ptr_array_add (checksums, g_strdup (csum)); |
| 4867 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4868 | } |
| 4869 | return checksums; |
| 4870 | } |
| 4871 | |
| 4872 | void |
| 4873 | fu_engine_add_approved_firmware (FuEngine *self, const gchar *checksum) |
| 4874 | { |
Richard Hughes | 3444cf6 | 2020-06-22 15:10:28 +0100 | [diff] [blame] | 4875 | if (self->approved_firmware == NULL) { |
| 4876 | self->approved_firmware = g_hash_table_new_full (g_str_hash, |
| 4877 | g_str_equal, |
| 4878 | g_free, |
| 4879 | NULL); |
| 4880 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 4881 | g_hash_table_add (self->approved_firmware, g_strdup (checksum)); |
| 4882 | } |
| 4883 | |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 4884 | GPtrArray * |
| 4885 | fu_engine_get_blocked_firmware (FuEngine *self) |
| 4886 | { |
| 4887 | GPtrArray *checksums = g_ptr_array_new_with_free_func (g_free); |
| 4888 | if (self->blocked_firmware != NULL) { |
| 4889 | g_autoptr(GList) keys = g_hash_table_get_keys (self->blocked_firmware); |
| 4890 | for (GList *l = keys; l != NULL; l = l->next) { |
| 4891 | const gchar *csum = l->data; |
| 4892 | g_ptr_array_add (checksums, g_strdup (csum)); |
| 4893 | } |
| 4894 | } |
| 4895 | return checksums; |
| 4896 | } |
| 4897 | |
| 4898 | void |
| 4899 | fu_engine_add_blocked_firmware (FuEngine *self, const gchar *checksum) |
| 4900 | { |
| 4901 | if (self->blocked_firmware == NULL) { |
| 4902 | self->blocked_firmware = g_hash_table_new_full (g_str_hash, |
| 4903 | g_str_equal, |
| 4904 | g_free, |
| 4905 | NULL); |
| 4906 | } |
| 4907 | g_hash_table_add (self->blocked_firmware, g_strdup (checksum)); |
| 4908 | } |
| 4909 | |
| 4910 | gboolean |
| 4911 | fu_engine_set_blocked_firmware (FuEngine *self, GPtrArray *checksums, GError **error) |
| 4912 | { |
| 4913 | /* update in-memory hash */ |
| 4914 | if (self->blocked_firmware != NULL) { |
| 4915 | g_hash_table_unref (self->blocked_firmware); |
| 4916 | self->blocked_firmware = NULL; |
| 4917 | } |
| 4918 | for (guint i = 0; i < checksums->len; i++) { |
| 4919 | const gchar *csum = g_ptr_array_index (checksums, i); |
| 4920 | fu_engine_add_blocked_firmware (self, csum); |
| 4921 | } |
| 4922 | |
| 4923 | /* save database */ |
| 4924 | if (!fu_history_clear_blocked_firmware (self->history, error)) |
| 4925 | return FALSE; |
| 4926 | for (guint i = 0; i < checksums->len; i++) { |
| 4927 | const gchar *csum = g_ptr_array_index (checksums, i); |
| 4928 | if (!fu_history_add_blocked_firmware (self->history, csum, error)) |
| 4929 | return FALSE; |
| 4930 | } |
| 4931 | return TRUE; |
| 4932 | } |
| 4933 | |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4934 | gchar * |
| 4935 | fu_engine_self_sign (FuEngine *self, |
| 4936 | const gchar *value, |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4937 | JcatSignFlags flags, |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4938 | GError **error) |
| 4939 | { |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4940 | g_autoptr(JcatBlob) jcat_signature = NULL; |
| 4941 | g_autoptr(JcatEngine) jcat_engine = NULL; |
| 4942 | g_autoptr(JcatResult) jcat_result = NULL; |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4943 | g_autoptr(GBytes) payload = NULL; |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4944 | |
| 4945 | /* create detached signature and verify */ |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4946 | jcat_engine = jcat_context_get_engine (self->jcat_context, |
| 4947 | JCAT_BLOB_KIND_PKCS7, |
| 4948 | error); |
| 4949 | if (jcat_engine == NULL) |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4950 | return NULL; |
| 4951 | payload = g_bytes_new (value, strlen (value)); |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4952 | jcat_signature = jcat_engine_self_sign (jcat_engine, payload, flags, error); |
| 4953 | if (jcat_signature == NULL) |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4954 | return NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4955 | jcat_result = jcat_engine_self_verify (jcat_engine, payload, |
| 4956 | jcat_blob_get_data (jcat_signature), |
| 4957 | JCAT_VERIFY_FLAG_NONE, error); |
| 4958 | if (jcat_result == NULL) |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4959 | return NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 4960 | return jcat_blob_get_data_as_string (jcat_signature); |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 4963 | /** |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4964 | * fu_engine_get_upgrades: |
| 4965 | * @self: A #FuEngine |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4966 | * @request: A #FuEngineRequest |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4967 | * @device_id: A device ID |
| 4968 | * @error: A #GError, or %NULL |
| 4969 | * |
| 4970 | * Gets the upgrades available for a specific device. |
| 4971 | * |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 4972 | * Returns: (transfer container) (element-type FwupdDevice): results |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4973 | **/ |
| 4974 | GPtrArray * |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 4975 | fu_engine_get_upgrades (FuEngine *self, |
| 4976 | FuEngineRequest *request, |
| 4977 | const gchar *device_id, |
| 4978 | GError **error) |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4979 | { |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 4980 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4981 | g_autoptr(GPtrArray) releases = NULL; |
| 4982 | g_autoptr(GPtrArray) releases_tmp = NULL; |
| 4983 | g_autoptr(GString) error_str = g_string_new (NULL); |
| 4984 | |
| 4985 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 4986 | g_return_val_if_fail (device_id != NULL, NULL); |
| 4987 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 4988 | |
| 4989 | /* find the device */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 4990 | device = fu_device_list_get_by_id (self->device_list, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 4991 | if (device == NULL) |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 4992 | return NULL; |
| 4993 | |
Mario Limonciello | fc13935 | 2018-09-13 10:06:39 -0500 | [diff] [blame] | 4994 | /* don't show upgrades again until we reboot */ |
| 4995 | if (fu_device_get_update_state (device) == FWUPD_UPDATE_STATE_NEEDS_REBOOT) { |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 4996 | g_set_error_literal (error, |
| 4997 | FWUPD_ERROR, |
| 4998 | FWUPD_ERROR_NOTHING_TO_DO, |
| 4999 | "A reboot is pending"); |
Mario Limonciello | fc13935 | 2018-09-13 10:06:39 -0500 | [diff] [blame] | 5000 | return NULL; |
| 5001 | } |
| 5002 | |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5003 | /* get all the releases for the device */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 5004 | releases_tmp = fu_engine_get_releases_for_device (self, request, device, error); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5005 | if (releases_tmp == NULL) |
| 5006 | return NULL; |
| 5007 | releases = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 5008 | for (guint i = 0; i < releases_tmp->len; i++) { |
| 5009 | FwupdRelease *rel_tmp = g_ptr_array_index (releases_tmp, i); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5010 | |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 5011 | /* same as installed */ |
| 5012 | if (!fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_UPGRADE) && |
| 5013 | !fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_DOWNGRADE)) { |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5014 | g_string_append_printf (error_str, "%s=same, ", |
| 5015 | fwupd_release_get_version (rel_tmp)); |
Mario Limonciello | 7a3664f | 2020-09-28 15:04:44 -0500 | [diff] [blame] | 5016 | g_debug ("ignoring %s == %s", |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5017 | fwupd_release_get_version (rel_tmp), |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5018 | fu_device_get_version (device)); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5019 | continue; |
| 5020 | } |
Richard Hughes | 8e0cc80 | 2019-03-04 14:10:17 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* older than current */ |
| 5023 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_DOWNGRADE)) { |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5024 | g_string_append_printf (error_str, "%s=older, ", |
| 5025 | fwupd_release_get_version (rel_tmp)); |
Mario Limonciello | 7a3664f | 2020-09-28 15:04:44 -0500 | [diff] [blame] | 5026 | g_debug ("ignoring %s < %s", |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5027 | fwupd_release_get_version (rel_tmp), |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5028 | fu_device_get_version (device)); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5029 | continue; |
| 5030 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 5031 | |
| 5032 | /* not approved */ |
| 5033 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_BLOCKED_APPROVAL)) { |
| 5034 | g_string_append_printf (error_str, "%s=not-approved, ", |
| 5035 | fwupd_release_get_version (rel_tmp)); |
| 5036 | g_debug ("ignoring %s as not approved as required by %s", |
| 5037 | fwupd_release_get_version (rel_tmp), |
| 5038 | fwupd_release_get_remote_id (rel_tmp)); |
| 5039 | continue; |
| 5040 | } |
| 5041 | |
Richard Hughes | 460c4b7 | 2020-09-25 20:59:28 +0100 | [diff] [blame] | 5042 | /* different branch */ |
| 5043 | if (fwupd_release_has_flag (rel_tmp, FWUPD_RELEASE_FLAG_IS_ALTERNATE_BRANCH)) { |
| 5044 | g_debug ("ignoring release %s as branch %s, and device is %s", |
| 5045 | fwupd_release_get_version (rel_tmp), |
| 5046 | fwupd_release_get_branch (rel_tmp), |
| 5047 | fu_device_get_branch (device)); |
| 5048 | continue; |
| 5049 | } |
| 5050 | |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5051 | g_ptr_array_add (releases, g_object_ref (rel_tmp)); |
| 5052 | } |
| 5053 | if (error_str->len > 2) |
| 5054 | g_string_truncate (error_str, error_str->len - 2); |
| 5055 | if (releases->len == 0) { |
| 5056 | if (error_str->len > 0) { |
| 5057 | g_set_error (error, |
| 5058 | FWUPD_ERROR, |
| 5059 | FWUPD_ERROR_NOTHING_TO_DO, |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 5060 | "current version is %s: %s", |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5061 | fu_device_get_version (device), |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5062 | error_str->str); |
| 5063 | } else { |
| 5064 | g_set_error (error, |
| 5065 | FWUPD_ERROR, |
| 5066 | FWUPD_ERROR_NOTHING_TO_DO, |
Mario Limonciello | 7e4949c | 2019-12-09 10:21:20 -0600 | [diff] [blame] | 5067 | "current version is %s", |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5068 | fu_device_get_version (device)); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5069 | } |
| 5070 | return NULL; |
| 5071 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 5072 | g_ptr_array_sort_with_data (releases, fu_engine_sort_releases_cb, device); |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 5073 | return g_steal_pointer (&releases); |
| 5074 | } |
| 5075 | |
| 5076 | /** |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5077 | * fu_engine_clear_results: |
| 5078 | * @self: A #FuEngine |
| 5079 | * @device_id: A device ID |
| 5080 | * @error: A #GError, or %NULL |
| 5081 | * |
| 5082 | * Clear the historical state of a specific device operation. |
| 5083 | * |
| 5084 | * Returns: %TRUE for success |
| 5085 | **/ |
| 5086 | gboolean |
| 5087 | fu_engine_clear_results (FuEngine *self, const gchar *device_id, GError **error) |
| 5088 | { |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5089 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5090 | FuPlugin *plugin; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5091 | |
| 5092 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 5093 | g_return_val_if_fail (device_id != NULL, FALSE); |
| 5094 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 5095 | |
| 5096 | /* find the device */ |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 5097 | device = fu_engine_get_item_by_id_fallback_history (self, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5098 | if (device == NULL) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5099 | return FALSE; |
| 5100 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5101 | /* already set on the database */ |
| 5102 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NOTIFIED)) { |
| 5103 | g_set_error_literal (error, |
| 5104 | FWUPD_ERROR, |
| 5105 | FWUPD_ERROR_NOT_SUPPORTED, |
| 5106 | "device already has notified flag"); |
| 5107 | return FALSE; |
| 5108 | } |
| 5109 | |
| 5110 | /* call into the plugin if it still exists */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5111 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5112 | fu_device_get_plugin (device), |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5113 | error); |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5114 | if (plugin != NULL) { |
| 5115 | if (!fu_plugin_runner_clear_results (plugin, device, error)) |
| 5116 | return FALSE; |
| 5117 | } |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5118 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5119 | /* override */ |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 5120 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_NOTIFIED); |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 5121 | return fu_history_modify_device (self->history, device, error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | /** |
| 5125 | * fu_engine_get_results: |
| 5126 | * @self: A #FuEngine |
| 5127 | * @device_id: A device ID |
| 5128 | * @error: A #GError, or %NULL |
| 5129 | * |
| 5130 | * Gets the historical state of a specific device operation. |
| 5131 | * |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 5132 | * Returns: (transfer container): a #FwupdDevice, or %NULL |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5133 | **/ |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 5134 | FwupdDevice * |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5135 | fu_engine_get_results (FuEngine *self, const gchar *device_id, GError **error) |
| 5136 | { |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5137 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5138 | |
| 5139 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 5140 | g_return_val_if_fail (device_id != NULL, NULL); |
| 5141 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 5142 | |
| 5143 | /* find the device */ |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 5144 | device = fu_engine_get_item_by_id_fallback_history (self, device_id, error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5145 | if (device == NULL) |
Richard Hughes | fe221dc | 2018-05-29 09:33:44 +0100 | [diff] [blame] | 5146 | return NULL; |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5147 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5148 | /* the notification has already been shown to the user */ |
| 5149 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NOTIFIED)) { |
| 5150 | g_set_error (error, |
| 5151 | FWUPD_ERROR, |
| 5152 | FWUPD_ERROR_NOTHING_TO_DO, |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5153 | "User has already been notified about %s [%s]", |
| 5154 | fu_device_get_name (device), |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5155 | fu_device_get_id (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5156 | return NULL; |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5157 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5158 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 5159 | /* success */ |
Richard Hughes | eec8a3c | 2018-01-02 20:37:31 +0000 | [diff] [blame] | 5160 | return g_object_ref (FWUPD_DEVICE (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | static void |
| 5164 | fu_engine_plugins_setup (FuEngine *self) |
| 5165 | { |
Richard Hughes | e671c05 | 2018-09-18 10:17:15 +0100 | [diff] [blame] | 5166 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5167 | for (guint i = 0; i < plugins->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5168 | g_autoptr(GError) error = NULL; |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5169 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5170 | if (!fu_plugin_runner_startup (plugin, &error)) { |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5171 | fu_plugin_add_flag (plugin, FWUPD_PLUGIN_FLAG_DISABLED); |
| 5172 | if (g_error_matches (error, |
| 5173 | FWUPD_ERROR, |
| 5174 | FWUPD_ERROR_NOT_SUPPORTED)) { |
| 5175 | fu_plugin_add_flag (plugin, FWUPD_PLUGIN_FLAG_NO_HARDWARE); |
| 5176 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5177 | g_message ("disabling plugin because: %s", error->message); |
| 5178 | } |
| 5179 | } |
| 5180 | } |
| 5181 | |
| 5182 | static void |
Richard Hughes | 4ae7b5e | 2021-04-01 14:43:43 +0100 | [diff] [blame] | 5183 | fu_engine_plugins_coldplug (FuEngine *self) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5184 | { |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5185 | GPtrArray *plugins; |
Richard Hughes | 535664c | 2017-07-24 10:30:09 +0100 | [diff] [blame] | 5186 | g_autoptr(GString) str = g_string_new (NULL); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5187 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5188 | /* prepare */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5189 | plugins = fu_plugin_list_get_all (self->plugin_list); |
| 5190 | for (guint i = 0; i < plugins->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5191 | g_autoptr(GError) error = NULL; |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5192 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5193 | if (!fu_plugin_runner_coldplug_prepare (plugin, &error)) |
| 5194 | g_warning ("failed to prepare coldplug: %s", error->message); |
| 5195 | } |
| 5196 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5197 | /* exec */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5198 | for (guint i = 0; i < plugins->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5199 | g_autoptr(GError) error = NULL; |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5200 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 4ae7b5e | 2021-04-01 14:43:43 +0100 | [diff] [blame] | 5201 | if (!fu_plugin_runner_coldplug (plugin, &error)) { |
| 5202 | fu_plugin_add_flag (plugin, FWUPD_PLUGIN_FLAG_DISABLED); |
| 5203 | g_message ("disabling plugin because: %s", |
| 5204 | error->message); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /* cleanup */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5209 | for (guint i = 0; i < plugins->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5210 | g_autoptr(GError) error = NULL; |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5211 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5212 | if (!fu_plugin_runner_coldplug_cleanup (plugin, &error)) |
| 5213 | g_warning ("failed to cleanup coldplug: %s", error->message); |
| 5214 | } |
| 5215 | |
Richard Hughes | 535664c | 2017-07-24 10:30:09 +0100 | [diff] [blame] | 5216 | /* print what we do have */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5217 | for (guint i = 0; i < plugins->len; i++) { |
| 5218 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5219 | if (fu_plugin_has_flag (plugin, FWUPD_PLUGIN_FLAG_DISABLED)) |
Richard Hughes | 535664c | 2017-07-24 10:30:09 +0100 | [diff] [blame] | 5220 | continue; |
| 5221 | g_string_append_printf (str, "%s, ", fu_plugin_get_name (plugin)); |
| 5222 | } |
| 5223 | if (str->len > 2) { |
| 5224 | g_string_truncate (str, str->len - 2); |
Mario Limonciello | 59cd470 | 2018-08-09 12:43:47 -0500 | [diff] [blame] | 5225 | g_debug ("using plugins: %s", str->str); |
Richard Hughes | 535664c | 2017-07-24 10:30:09 +0100 | [diff] [blame] | 5226 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | static void |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 5230 | fu_engine_plugin_device_register (FuEngine *self, FuDevice *device) |
| 5231 | { |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5232 | GPtrArray *plugins; |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 5233 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_REGISTERED)) { |
| 5234 | g_warning ("already registered %s, ignoring", |
| 5235 | fu_device_get_id (device)); |
| 5236 | return; |
| 5237 | } |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5238 | plugins = fu_plugin_list_get_all (self->plugin_list); |
| 5239 | for (guint i = 0; i < plugins->len; i++) { |
| 5240 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 5241 | fu_plugin_runner_device_register (plugin, device); |
| 5242 | } |
| 5243 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_REGISTERED); |
| 5244 | } |
| 5245 | |
| 5246 | static void |
| 5247 | fu_engine_plugin_device_register_cb (FuPlugin *plugin, |
| 5248 | FuDevice *device, |
| 5249 | gpointer user_data) |
| 5250 | { |
| 5251 | FuEngine *self = FU_ENGINE (user_data); |
| 5252 | fu_engine_plugin_device_register (self, device); |
| 5253 | } |
| 5254 | |
| 5255 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5256 | fu_engine_plugin_device_added_cb (FuPlugin *plugin, |
| 5257 | FuDevice *device, |
| 5258 | gpointer user_data) |
| 5259 | { |
Richard Hughes | 4299ba0 | 2020-04-14 15:40:56 +0100 | [diff] [blame] | 5260 | FuEngine *self = FU_ENGINE (user_data); |
Richard Hughes | fecc438 | 2020-04-15 11:24:34 +0100 | [diff] [blame] | 5261 | |
| 5262 | /* plugin has prio and device not already set from quirk */ |
| 5263 | if (fu_plugin_get_priority (plugin) > 0 && |
| 5264 | fu_device_get_priority (device) == 0) { |
| 5265 | g_debug ("auto-setting %s priority to %u", |
| 5266 | fu_device_get_id (device), |
| 5267 | fu_plugin_get_priority (plugin)); |
| 5268 | fu_device_set_priority (device, fu_plugin_get_priority (plugin)); |
| 5269 | } |
| 5270 | |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5271 | fu_engine_add_device (self, device); |
| 5272 | } |
| 5273 | |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5274 | static void |
| 5275 | fu_engine_adopt_children (FuEngine *self, FuDevice *device) |
| 5276 | { |
| 5277 | GPtrArray *guids; |
| 5278 | g_autoptr(GPtrArray) devices = fu_device_list_get_active (self->device_list); |
| 5279 | |
| 5280 | /* find the parent GUID in any existing device */ |
| 5281 | guids = fu_device_get_parent_guids (device); |
| 5282 | for (guint j = 0; j < guids->len; j++) { |
| 5283 | const gchar *guid = g_ptr_array_index (guids, j); |
| 5284 | for (guint i = 0; i < devices->len; i++) { |
| 5285 | FuDevice *device_tmp = g_ptr_array_index (devices, i); |
| 5286 | if (fu_device_get_parent (device) != NULL) |
| 5287 | continue; |
| 5288 | if (fu_device_has_guid (device_tmp, guid)) { |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5289 | g_debug ("setting parent of %s [%s] to be %s [%s]", |
| 5290 | fu_device_get_name (device), |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5291 | fu_device_get_id (device), |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5292 | fu_device_get_name (device_tmp), |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5293 | fu_device_get_id (device_tmp)); |
Richard Hughes | 2ab379e | 2020-04-09 16:03:36 +0100 | [diff] [blame] | 5294 | fu_device_set_parent (device, device_tmp); |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5295 | break; |
| 5296 | } |
| 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | /* the new device is the parent to an existing child */ |
| 5301 | guids = fu_device_get_guids (device); |
| 5302 | for (guint j = 0; j < guids->len; j++) { |
| 5303 | const gchar *guid = g_ptr_array_index (guids, j); |
| 5304 | for (guint i = 0; i < devices->len; i++) { |
| 5305 | FuDevice *device_tmp = g_ptr_array_index (devices, i); |
| 5306 | if (fu_device_get_parent (device_tmp) != NULL) |
| 5307 | continue; |
| 5308 | if (fu_device_has_parent_guid (device_tmp, guid)) { |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5309 | g_debug ("setting parent of %s [%s] to be %s [%s]", |
| 5310 | fu_device_get_name (device_tmp), |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5311 | fu_device_get_id (device_tmp), |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5312 | fu_device_get_name (device), |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5313 | fu_device_get_id (device)); |
Richard Hughes | 2ab379e | 2020-04-09 16:03:36 +0100 | [diff] [blame] | 5314 | fu_device_set_parent (device_tmp, device); |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5315 | } |
| 5316 | } |
| 5317 | } |
| 5318 | } |
| 5319 | |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 5320 | static void |
Richard Hughes | 35ca4cb | 2020-04-14 17:09:01 +0100 | [diff] [blame] | 5321 | fu_engine_set_proxy_device (FuEngine *self, FuDevice *device) |
| 5322 | { |
| 5323 | GPtrArray *guids; |
| 5324 | g_autoptr(FuDevice) proxy = NULL; |
| 5325 | g_autoptr(GPtrArray) devices = NULL; |
| 5326 | |
| 5327 | if (fu_device_get_proxy (device) != NULL) |
| 5328 | return; |
| 5329 | if (fu_device_get_proxy_guid (device) == NULL) |
| 5330 | return; |
| 5331 | |
| 5332 | /* find the proxy GUID in any existing device */ |
| 5333 | proxy = fu_device_list_get_by_guid (self->device_list, |
| 5334 | fu_device_get_proxy_guid (device), |
| 5335 | NULL); |
| 5336 | if (proxy != NULL) { |
| 5337 | g_debug ("setting proxy of %s to %s for %s", |
| 5338 | fu_device_get_id (proxy), |
| 5339 | fu_device_get_id (device), |
| 5340 | fu_device_get_proxy_guid (device)); |
| 5341 | fu_device_set_proxy (device, proxy); |
| 5342 | return; |
| 5343 | } |
| 5344 | |
| 5345 | /* are we the parent of an existing device */ |
| 5346 | guids = fu_device_get_guids (device); |
| 5347 | for (guint j = 0; j < guids->len; j++) { |
| 5348 | const gchar *guid = g_ptr_array_index (guids, j); |
| 5349 | devices = fu_device_list_get_active (self->device_list); |
| 5350 | for (guint i = 0; i < devices->len; i++) { |
| 5351 | FuDevice *device_tmp = g_ptr_array_index (devices, i); |
| 5352 | if (g_strcmp0 (fu_device_get_proxy_guid (device_tmp), guid) == 0) { |
| 5353 | g_debug ("adding proxy of %s to %s for %s", |
| 5354 | fu_device_get_id (device), |
| 5355 | fu_device_get_id (device_tmp), |
| 5356 | guid); |
| 5357 | fu_device_set_proxy (device_tmp, device); |
| 5358 | return; |
| 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | |
| 5363 | /* nothing found */ |
| 5364 | g_warning ("did not find proxy device %s", |
| 5365 | fu_device_get_proxy_guid (device)); |
| 5366 | } |
| 5367 | |
| 5368 | static void |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 5369 | fu_engine_device_inherit_history (FuEngine *self, FuDevice *device) |
| 5370 | { |
| 5371 | g_autoptr(FuDevice) device_history = NULL; |
| 5372 | |
| 5373 | /* any success or failed update? */ |
| 5374 | device_history = fu_history_get_device_by_id (self->history, |
| 5375 | fu_device_get_id (device), |
| 5376 | NULL); |
| 5377 | if (device_history == NULL) |
| 5378 | return; |
| 5379 | |
| 5380 | /* the device is still running the old firmware version and so if it |
| 5381 | * required activation before, it still requires it now -- note: |
| 5382 | * we can't just check for version_new=version to allow for re-installs */ |
Mario Limonciello | 0f50a31 | 2021-04-06 11:30:26 -0500 | [diff] [blame] | 5383 | if (fu_device_has_internal_flag (device, FU_DEVICE_INTERNAL_FLAG_INHERIT_ACTIVATION) && |
| 5384 | fu_device_has_flag (device_history, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 5385 | FwupdRelease *release = fu_device_get_release_default (device_history); |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 5386 | if (fu_common_vercmp_full (fu_device_get_version (device), |
| 5387 | fwupd_release_get_version (release), |
| 5388 | fu_device_get_version_format (device)) != 0) { |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 5389 | g_debug ("inheriting needs-activation for %s as version %s != %s", |
| 5390 | fu_device_get_name (device), |
| 5391 | fu_device_get_version (device), |
| 5392 | fwupd_release_get_version (release)); |
| 5393 | fu_device_add_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION); |
| 5394 | } |
| 5395 | } |
| 5396 | } |
| 5397 | |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5398 | void |
| 5399 | fu_engine_add_device (FuEngine *self, FuDevice *device) |
| 5400 | { |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5401 | GPtrArray *disabled_devices; |
Richard Hughes | 32684f2 | 2017-07-13 09:32:21 +0100 | [diff] [blame] | 5402 | GPtrArray *device_guids; |
Mario Limonciello | e038419 | 2020-04-16 14:36:49 -0500 | [diff] [blame] | 5403 | g_autoptr(XbNode) component = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5404 | |
| 5405 | /* device has no GUIDs set! */ |
Richard Hughes | 32684f2 | 2017-07-13 09:32:21 +0100 | [diff] [blame] | 5406 | device_guids = fu_device_get_guids (device); |
| 5407 | if (device_guids->len == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5408 | g_warning ("no GUIDs for device %s [%s]", |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5409 | fu_device_get_name (device), |
| 5410 | fu_device_get_id (device)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5411 | return; |
| 5412 | } |
| 5413 | |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5414 | /* is this GUID disabled */ |
| 5415 | disabled_devices = fu_config_get_disabled_devices (self->config); |
| 5416 | for (guint i = 0; i < disabled_devices->len; i++) { |
| 5417 | const gchar *disabled_guid = g_ptr_array_index (disabled_devices, i); |
Richard Hughes | 32684f2 | 2017-07-13 09:32:21 +0100 | [diff] [blame] | 5418 | for (guint j = 0; j < device_guids->len; j++) { |
| 5419 | const gchar *device_guid = g_ptr_array_index (device_guids, j); |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5420 | if (g_strcmp0 (disabled_guid, device_guid) == 0) { |
| 5421 | g_debug ("%s [%s] is disabled [%s], ignoring from %s", |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 5422 | fu_device_get_name (device), |
| 5423 | fu_device_get_id (device), |
| 5424 | device_guid, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5425 | fu_device_get_plugin (device)); |
Richard Hughes | 32684f2 | 2017-07-13 09:32:21 +0100 | [diff] [blame] | 5426 | return; |
| 5427 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5428 | } |
| 5429 | } |
| 5430 | |
Richard Hughes | 7f76500 | 2019-12-20 09:41:29 +0000 | [diff] [blame] | 5431 | /* does the device not have an assigned protocol */ |
| 5432 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE) && |
Richard Hughes | b3f9841 | 2021-03-01 13:17:57 +0000 | [diff] [blame] | 5433 | fu_device_get_protocols (device)->len == 0) { |
Mario Limonciello | 33b40ed | 2020-01-08 15:11:34 -0600 | [diff] [blame] | 5434 | g_warning ("device %s [%s] does not define an update protocol", |
Richard Hughes | 7f76500 | 2019-12-20 09:41:29 +0000 | [diff] [blame] | 5435 | fu_device_get_id (device), |
| 5436 | fu_device_get_name (device)); |
| 5437 | } |
| 5438 | |
Richard Hughes | f307775 | 2018-06-27 12:45:06 +0100 | [diff] [blame] | 5439 | /* if this device is locked get some metadata from AppStream */ |
Mario Limonciello | e038419 | 2020-04-16 14:36:49 -0500 | [diff] [blame] | 5440 | component = fu_engine_get_component_by_guids (self, device); |
Richard Hughes | f307775 | 2018-06-27 12:45:06 +0100 | [diff] [blame] | 5441 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_LOCKED)) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 5442 | if (component != NULL) { |
| 5443 | g_autoptr(XbNode) release = NULL; |
| 5444 | release = xb_node_query_first (component, |
| 5445 | "releases/release", |
| 5446 | NULL); |
Richard Hughes | f307775 | 2018-06-27 12:45:06 +0100 | [diff] [blame] | 5447 | if (release != NULL) { |
| 5448 | g_autoptr(FwupdRelease) rel = fwupd_release_new (); |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 5449 | g_autoptr(GError) error_local = NULL; |
| 5450 | if (!fu_engine_set_release_from_appstream (self, |
Richard Hughes | 2c40b37 | 2019-04-17 13:41:47 +0100 | [diff] [blame] | 5451 | device, |
Richard Hughes | 0b3e9fd | 2019-04-08 11:13:20 +0100 | [diff] [blame] | 5452 | rel, |
| 5453 | component, |
| 5454 | release, |
| 5455 | &error_local)) { |
| 5456 | g_warning ("failed to set AppStream release: %s", |
| 5457 | error_local->message); |
| 5458 | } else { |
| 5459 | fu_device_add_release (device, rel); |
| 5460 | } |
Richard Hughes | f307775 | 2018-06-27 12:45:06 +0100 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | } |
| 5464 | |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 5465 | /* adopt any required children, which may or may not already exist */ |
| 5466 | fu_engine_adopt_children (self, device); |
| 5467 | |
Richard Hughes | 35ca4cb | 2020-04-14 17:09:01 +0100 | [diff] [blame] | 5468 | /* set the proxy device if specified by GUID */ |
| 5469 | fu_engine_set_proxy_device (self, device); |
| 5470 | |
Richard Hughes | e48351e | 2018-06-22 12:32:39 +0100 | [diff] [blame] | 5471 | /* set any alternate objects on the device from the ID */ |
| 5472 | if (fu_device_get_alternate_id (device) != NULL) { |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 5473 | g_autoptr(FuDevice) device_alt = NULL; |
Richard Hughes | e48351e | 2018-06-22 12:32:39 +0100 | [diff] [blame] | 5474 | device_alt = fu_device_list_get_by_id (self->device_list, |
| 5475 | fu_device_get_alternate_id (device), |
| 5476 | NULL); |
| 5477 | if (device_alt != NULL) |
| 5478 | fu_device_set_alternate (device, device_alt); |
| 5479 | } |
| 5480 | |
Richard Hughes | 5079f26 | 2019-04-29 09:34:27 +0100 | [diff] [blame] | 5481 | if (fu_device_get_version_format (device) == FWUPD_VERSION_FORMAT_UNKNOWN && |
| 5482 | fu_common_version_guess_format (fu_device_get_version (device)) == FWUPD_VERSION_FORMAT_NUMBER) { |
Mario Limonciello | 253b825 | 2019-04-25 10:48:17 -0400 | [diff] [blame] | 5483 | fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE); |
| 5484 | fu_device_set_update_error (device, "VersionFormat is ambiguous for this device"); |
| 5485 | } |
| 5486 | |
Richard Hughes | d8ea8da | 2020-02-19 15:04:49 +0000 | [diff] [blame] | 5487 | /* no vendor-id, and so no way to lock it down! */ |
| 5488 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE) && |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 5489 | fu_device_get_vendor_ids(device)->len == 0) { |
Richard Hughes | d8ea8da | 2020-02-19 15:04:49 +0000 | [diff] [blame] | 5490 | fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE); |
| 5491 | fu_device_set_update_error (device, "No vendor ID set"); |
| 5492 | } |
| 5493 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 5494 | /* notify all plugins about this new device */ |
| 5495 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_REGISTERED)) |
| 5496 | fu_engine_plugin_device_register (self, device); |
| 5497 | |
Richard Hughes | 7f76500 | 2019-12-20 09:41:29 +0000 | [diff] [blame] | 5498 | /* does the device *still* not have a vendor ID? */ |
| 5499 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE) && |
Richard Hughes | eddaed0 | 2021-01-03 12:08:38 +0000 | [diff] [blame] | 5500 | fu_device_get_vendor_ids(device)->len == 0) { |
Richard Hughes | 7f76500 | 2019-12-20 09:41:29 +0000 | [diff] [blame] | 5501 | g_warning ("device %s [%s] does not define a vendor-id!", |
| 5502 | fu_device_get_id (device), |
| 5503 | fu_device_get_name (device)); |
| 5504 | } |
| 5505 | |
Richard Hughes | d76ed3d | 2018-11-15 15:05:36 +0000 | [diff] [blame] | 5506 | /* create new device */ |
| 5507 | fu_device_list_add (self->device_list, device); |
| 5508 | |
Richard Hughes | 81f9552 | 2020-09-13 15:12:57 +0100 | [diff] [blame] | 5509 | /* fix order */ |
| 5510 | fu_device_list_depsolve_order (self->device_list, device); |
| 5511 | |
Mario Limonciello | e038419 | 2020-04-16 14:36:49 -0500 | [diff] [blame] | 5512 | /* fixup the name and format as needed from cached metadata */ |
| 5513 | if (component != NULL) |
| 5514 | fu_engine_md_refresh_device_from_component (self, device, component); |
| 5515 | |
Richard Hughes | 1ee1800 | 2020-03-31 21:46:49 +0100 | [diff] [blame] | 5516 | /* match the metadata so clients can tell if the device is worthy */ |
| 5517 | fu_engine_ensure_device_supported (self, device); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 5518 | |
| 5519 | /* sometimes inherit flags from recent history */ |
| 5520 | fu_engine_device_inherit_history (self, device); |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 5521 | |
| 5522 | fu_engine_emit_changed (self); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | static void |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 5526 | fu_engine_plugin_rules_changed_cb (FuPlugin *plugin, gpointer user_data) |
| 5527 | { |
| 5528 | FuEngine *self = FU_ENGINE (user_data); |
| 5529 | GPtrArray *rules = fu_plugin_get_rules (plugin, FU_PLUGIN_RULE_INHIBITS_IDLE); |
Richard Hughes | 11c5941 | 2020-06-22 15:29:48 +0100 | [diff] [blame] | 5530 | if (rules == NULL) |
| 5531 | return; |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 5532 | for (guint j = 0; j < rules->len; j++) { |
| 5533 | const gchar *tmp = g_ptr_array_index (rules, j); |
| 5534 | fu_idle_inhibit (self->idle, tmp); |
| 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | static void |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5539 | fu_engine_context_security_changed_cb (FuContext *ctx, gpointer user_data) |
Richard Hughes | 399859e | 2020-05-11 19:44:03 +0100 | [diff] [blame] | 5540 | { |
| 5541 | FuEngine *self = FU_ENGINE (user_data); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5542 | |
| 5543 | /* invalidate host security attributes */ |
| 5544 | g_clear_pointer (&self->host_security_id, g_free); |
| 5545 | |
| 5546 | /* make UI refresh */ |
Richard Hughes | 399859e | 2020-05-11 19:44:03 +0100 | [diff] [blame] | 5547 | fu_engine_emit_changed (self); |
| 5548 | } |
| 5549 | |
| 5550 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5551 | fu_engine_plugin_device_removed_cb (FuPlugin *plugin, |
| 5552 | FuDevice *device, |
| 5553 | gpointer user_data) |
| 5554 | { |
| 5555 | FuEngine *self = (FuEngine *) user_data; |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5556 | FuPlugin *plugin_old; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 5557 | g_autoptr(FuDevice) device_tmp = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5558 | g_autoptr(GError) error = NULL; |
| 5559 | |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 5560 | device_tmp = fu_device_list_get_by_id (self->device_list, |
| 5561 | fu_device_get_id (device), |
| 5562 | &error); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5563 | if (device_tmp == NULL) { |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 5564 | g_debug ("failed to find device %s: %s", |
| 5565 | fu_device_get_id (device), |
| 5566 | error->message); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5567 | return; |
| 5568 | } |
| 5569 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5570 | /* get the plugin */ |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5571 | plugin_old = fu_plugin_list_find_by_name (self->plugin_list, |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5572 | fu_device_get_plugin (device), |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5573 | &error); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5574 | if (plugin_old == NULL) { |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 5575 | g_debug ("failed to find plugin %s: %s", |
| 5576 | fu_device_get_plugin (device), |
| 5577 | error->message); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5578 | return; |
| 5579 | } |
| 5580 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5581 | /* check this came from the same plugin */ |
| 5582 | if (g_strcmp0 (fu_plugin_get_name (plugin), |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5583 | fu_plugin_get_name (plugin_old)) != 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5584 | g_debug ("ignoring duplicate removal from %s", |
| 5585 | fu_plugin_get_name (plugin)); |
| 5586 | return; |
| 5587 | } |
| 5588 | |
| 5589 | /* make the UI update */ |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 5590 | fu_device_list_remove (self->device_list, device); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5591 | fu_engine_emit_changed (self); |
| 5592 | } |
| 5593 | |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 5594 | /* this is called by the self tests as well */ |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5595 | void |
| 5596 | fu_engine_add_plugin (FuEngine *self, FuPlugin *plugin) |
| 5597 | { |
Mario Limonciello | 52e75ba | 2019-11-22 13:21:19 -0600 | [diff] [blame] | 5598 | if (fu_plugin_is_open (plugin)) { |
| 5599 | /* plugin does not match built version */ |
| 5600 | if (fu_plugin_get_build_hash (plugin) == NULL) { |
| 5601 | const gchar *name = fu_plugin_get_name (plugin); |
| 5602 | g_warning ("%s should call fu_plugin_set_build_hash()", |
| 5603 | name); |
| 5604 | self->tainted = TRUE; |
| 5605 | } else if (g_strcmp0 (fu_plugin_get_build_hash (plugin), |
| 5606 | FU_BUILD_HASH) != 0) { |
| 5607 | const gchar *name = fu_plugin_get_name (plugin); |
| 5608 | g_warning ("%s has incorrect built version %s", |
| 5609 | name, fu_plugin_get_build_hash (plugin)); |
| 5610 | self->tainted = TRUE; |
| 5611 | } |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5614 | fu_plugin_list_add (self->plugin_list, plugin); |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5617 | static gboolean |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5618 | fu_engine_is_plugin_name_disabled (FuEngine *self, const gchar *name) |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5619 | { |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5620 | GPtrArray *disabled = fu_config_get_disabled_plugins (self->config); |
| 5621 | for (guint i = 0; i < disabled->len; i++) { |
| 5622 | const gchar *name_tmp = g_ptr_array_index (disabled, i); |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5623 | if (g_strcmp0 (name_tmp, name) == 0) |
| 5624 | return TRUE; |
| 5625 | } |
| 5626 | return FALSE; |
| 5627 | } |
| 5628 | |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5629 | static gboolean |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5630 | fu_engine_is_plugin_name_enabled (FuEngine *self, const gchar *name) |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5631 | { |
| 5632 | if (self->plugin_filter->len == 0) |
| 5633 | return TRUE; |
| 5634 | for (guint i = 0; i < self->plugin_filter->len; i++) { |
| 5635 | const gchar *name_tmp = g_ptr_array_index (self->plugin_filter, i); |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 5636 | if (fu_common_fnmatch (name_tmp, name)) |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5637 | return TRUE; |
| 5638 | } |
| 5639 | return FALSE; |
| 5640 | } |
| 5641 | |
| 5642 | void |
| 5643 | fu_engine_add_plugin_filter (FuEngine *self, const gchar *plugin_glob) |
| 5644 | { |
Mario Limonciello | 1a52051 | 2020-05-26 10:44:53 -0500 | [diff] [blame] | 5645 | GString *str; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5646 | g_return_if_fail (FU_IS_ENGINE (self)); |
| 5647 | g_return_if_fail (plugin_glob != NULL); |
Mario Limonciello | 1a52051 | 2020-05-26 10:44:53 -0500 | [diff] [blame] | 5648 | str = g_string_new (plugin_glob); |
| 5649 | fu_common_string_replace (str, "-", "_"); |
| 5650 | g_ptr_array_add (self->plugin_filter, g_string_free (str, FALSE)); |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5651 | } |
| 5652 | |
Richard Hughes | aabdc37 | 2018-11-14 10:11:08 +0000 | [diff] [blame] | 5653 | static gboolean |
| 5654 | fu_engine_plugin_check_supported_cb (FuPlugin *plugin, const gchar *guid, FuEngine *self) |
| 5655 | { |
| 5656 | g_autoptr(XbNode) n = NULL; |
| 5657 | g_autofree gchar *xpath = NULL; |
Mario Limonciello | 4fa95a7 | 2020-03-28 10:50:57 -0500 | [diff] [blame] | 5658 | |
| 5659 | if (fu_config_get_enumerate_all_devices (self->config)) |
| 5660 | return TRUE; |
| 5661 | |
Richard Hughes | 4cbe99c | 2020-11-22 13:14:33 +0000 | [diff] [blame] | 5662 | xpath = g_strdup_printf ("components/component[@type='firmware']/" |
Richard Hughes | aabdc37 | 2018-11-14 10:11:08 +0000 | [diff] [blame] | 5663 | "provides/firmware[@type='flashed'][text()='%s']", |
| 5664 | guid); |
| 5665 | n = xb_silo_query_first (self->silo, xpath, NULL); |
| 5666 | return n != NULL; |
| 5667 | } |
| 5668 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 5669 | gboolean |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 5670 | fu_engine_get_tainted (FuEngine *self) |
| 5671 | { |
| 5672 | return self->tainted; |
| 5673 | } |
| 5674 | |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 5675 | const gchar * |
| 5676 | fu_engine_get_host_product (FuEngine *self) |
| 5677 | { |
Mario Limonciello | 8ad4ac0 | 2020-01-15 09:58:53 -0600 | [diff] [blame] | 5678 | const gchar *result = NULL; |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 5679 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5680 | result = fu_context_get_hwid_value (self->ctx, FU_HWIDS_KEY_PRODUCT_NAME); |
Mario Limonciello | 8ad4ac0 | 2020-01-15 09:58:53 -0600 | [diff] [blame] | 5681 | return result != NULL ? result : "Unknown Product"; |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 5682 | } |
| 5683 | |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 5684 | const gchar * |
| 5685 | fu_engine_get_host_machine_id (FuEngine *self) |
| 5686 | { |
| 5687 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 5688 | return self->host_machine_id; |
| 5689 | } |
| 5690 | |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5691 | static void |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5692 | fu_engine_ensure_security_attrs_tainted (FuEngine *self) |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5693 | { |
Mario Limonciello | a834284 | 2020-05-13 09:47:16 -0500 | [diff] [blame] | 5694 | gboolean disabled_plugins = FALSE; |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5695 | GPtrArray *disabled = fu_config_get_disabled_plugins (self->config); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5696 | g_autoptr(FwupdSecurityAttr) attr = fwupd_security_attr_new (FWUPD_SECURITY_ATTR_ID_FWUPD_PLUGINS); |
Richard Hughes | cae111d | 2020-05-15 15:20:24 +0100 | [diff] [blame] | 5697 | fwupd_security_attr_set_plugin (attr, "core"); |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5698 | fwupd_security_attr_add_flag (attr, FWUPD_SECURITY_ATTR_FLAG_RUNTIME_ISSUE); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5699 | |
| 5700 | fu_security_attrs_append (self->host_security_attrs, attr); |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5701 | for (guint i = 0; i < disabled->len; i++) { |
| 5702 | const gchar *name_tmp = g_ptr_array_index (disabled, i); |
Richard Hughes | 21c44d1 | 2021-03-03 10:04:00 +0000 | [diff] [blame] | 5703 | if (!g_str_has_prefix (name_tmp, "test") && |
Mario Limonciello | a834284 | 2020-05-13 09:47:16 -0500 | [diff] [blame] | 5704 | g_strcmp0 (name_tmp, "invalid") != 0) { |
| 5705 | disabled_plugins = TRUE; |
| 5706 | break; |
| 5707 | } |
| 5708 | } |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5709 | if (self->tainted) { |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5710 | fwupd_security_attr_set_result (attr, FWUPD_SECURITY_ATTR_RESULT_TAINTED); |
| 5711 | return; |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5712 | } |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5713 | if (self->plugin_filter->len > 0 || disabled_plugins) { |
| 5714 | fwupd_security_attr_set_result (attr, FWUPD_SECURITY_ATTR_RESULT_NOT_ENABLED); |
| 5715 | return; |
| 5716 | } |
| 5717 | |
| 5718 | /* success */ |
| 5719 | fwupd_security_attr_add_flag (attr, FWUPD_SECURITY_ATTR_FLAG_SUCCESS); |
| 5720 | fwupd_security_attr_set_result (attr, FWUPD_SECURITY_ATTR_RESULT_NOT_TAINTED); |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5721 | } |
| 5722 | |
Richard Hughes | 377feea | 2020-10-17 15:52:03 +0100 | [diff] [blame] | 5723 | static gchar * |
| 5724 | fu_engine_attrs_calculate_hsi_for_chassis (FuEngine *self) |
| 5725 | { |
| 5726 | guint val; |
Richard Hughes | 377feea | 2020-10-17 15:52:03 +0100 | [diff] [blame] | 5727 | |
| 5728 | /* get chassis type from SMBIOS data */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5729 | val = fu_context_get_smbios_integer (self->ctx, |
| 5730 | FU_SMBIOS_STRUCTURE_TYPE_CHASSIS, |
| 5731 | 0x05); |
| 5732 | if (val == G_MAXUINT) |
Richard Hughes | 377feea | 2020-10-17 15:52:03 +0100 | [diff] [blame] | 5733 | return g_strdup ("HSI-INVALID:chassis"); |
Richard Hughes | 377feea | 2020-10-17 15:52:03 +0100 | [diff] [blame] | 5734 | |
| 5735 | /* verify HSI makes sense for this chassis type */ |
| 5736 | switch (val) { |
| 5737 | case FU_SMBIOS_CHASSIS_KIND_DESKTOP: |
| 5738 | case FU_SMBIOS_CHASSIS_KIND_LOW_PROFILE_DESKTOP: |
| 5739 | case FU_SMBIOS_CHASSIS_KIND_MINI_TOWER: |
| 5740 | case FU_SMBIOS_CHASSIS_KIND_TOWER: |
| 5741 | case FU_SMBIOS_CHASSIS_KIND_PORTABLE: |
| 5742 | case FU_SMBIOS_CHASSIS_KIND_LAPTOP: |
| 5743 | case FU_SMBIOS_CHASSIS_KIND_NOTEBOOK: |
| 5744 | case FU_SMBIOS_CHASSIS_KIND_ALL_IN_ONE: |
| 5745 | case FU_SMBIOS_CHASSIS_KIND_SUB_NOTEBOOK: |
| 5746 | case FU_SMBIOS_CHASSIS_KIND_LUNCH_BOX: |
| 5747 | case FU_SMBIOS_CHASSIS_KIND_MAIN_SERVER: |
| 5748 | case FU_SMBIOS_CHASSIS_KIND_TABLET: |
| 5749 | case FU_SMBIOS_CHASSIS_KIND_CONVERTIBLE: |
| 5750 | case FU_SMBIOS_CHASSIS_KIND_DETACHABLE: |
| 5751 | case FU_SMBIOS_CHASSIS_KIND_IOT_GATEWAY: |
| 5752 | case FU_SMBIOS_CHASSIS_KIND_EMBEDDED_PC: |
| 5753 | case FU_SMBIOS_CHASSIS_KIND_MINI_PC: |
| 5754 | case FU_SMBIOS_CHASSIS_KIND_STICK_PC: |
| 5755 | return fu_security_attrs_calculate_hsi (self->host_security_attrs, |
| 5756 | FU_SECURITY_ATTRS_FLAG_ADD_VERSION); |
| 5757 | default: |
| 5758 | break; |
| 5759 | } |
| 5760 | |
| 5761 | /* failed */ |
| 5762 | return g_strdup_printf ("HSI-INVALID:chassis[0x%02x]", val); |
| 5763 | } |
| 5764 | |
| 5765 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5766 | static void |
| 5767 | fu_engine_ensure_security_attrs (FuEngine *self) |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5768 | { |
| 5769 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
Richard Hughes | dd61689 | 2021-03-17 21:42:28 +0000 | [diff] [blame] | 5770 | g_autoptr(GPtrArray) devices = fu_device_list_get_all (self->device_list); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5771 | g_autoptr(GPtrArray) items = NULL; |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5772 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5773 | /* already valid */ |
| 5774 | if (self->host_security_id != NULL) |
| 5775 | return; |
| 5776 | |
| 5777 | /* clear old values */ |
| 5778 | fu_security_attrs_remove_all (self->host_security_attrs); |
| 5779 | |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5780 | /* built in */ |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5781 | fu_engine_ensure_security_attrs_tainted (self); |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5782 | |
Richard Hughes | dd61689 | 2021-03-17 21:42:28 +0000 | [diff] [blame] | 5783 | /* call into devices */ |
| 5784 | for (guint i = 0; i < devices->len; i++) { |
| 5785 | FuDevice *device = g_ptr_array_index (devices, i); |
| 5786 | fu_device_add_security_attrs (device, self->host_security_attrs); |
| 5787 | } |
| 5788 | |
Richard Hughes | ad45158 | 2020-05-11 22:14:47 +0100 | [diff] [blame] | 5789 | /* call into plugins */ |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5790 | for (guint j = 0; j < plugins->len; j++) { |
| 5791 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5792 | fu_plugin_runner_add_security_attrs (plugin_tmp, self->host_security_attrs); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5793 | } |
| 5794 | |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5795 | /* set the fallback names for clients without native translations */ |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5796 | items = fu_security_attrs_get_all (self->host_security_attrs); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5797 | for (guint i = 0; i < items->len; i++) { |
| 5798 | FwupdSecurityAttr *attr = g_ptr_array_index (items, i); |
| 5799 | if (fwupd_security_attr_get_name (attr) == NULL) { |
Richard Hughes | b99df2e | 2020-07-01 19:28:35 +0100 | [diff] [blame] | 5800 | g_autofree gchar *name_tmp = fu_security_attr_get_name (attr); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5801 | if (name_tmp == NULL) { |
| 5802 | g_warning ("failed to get fallback for %s", |
| 5803 | fwupd_security_attr_get_appstream_id (attr)); |
| 5804 | continue; |
| 5805 | } |
| 5806 | fwupd_security_attr_set_name (attr, name_tmp); |
| 5807 | } |
| 5808 | } |
| 5809 | |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5810 | /* set the obsoletes flag for each attr */ |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5811 | fu_security_attrs_depsolve (self->host_security_attrs); |
Richard Hughes | b246bca | 2020-05-18 14:31:35 +0100 | [diff] [blame] | 5812 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5813 | /* distil into one simple string */ |
| 5814 | g_free (self->host_security_id); |
Richard Hughes | 377feea | 2020-10-17 15:52:03 +0100 | [diff] [blame] | 5815 | self->host_security_id = fu_engine_attrs_calculate_hsi_for_chassis (self); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | const gchar * |
| 5819 | fu_engine_get_host_security_id (FuEngine *self) |
| 5820 | { |
| 5821 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5822 | fu_engine_ensure_security_attrs (self); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 5823 | return self->host_security_id; |
| 5824 | } |
| 5825 | |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 5826 | FuSecurityAttrs * |
| 5827 | fu_engine_get_host_security_attrs (FuEngine *self) |
| 5828 | { |
| 5829 | g_return_val_if_fail (FU_IS_ENGINE (self), NULL); |
| 5830 | fu_engine_ensure_security_attrs (self); |
| 5831 | return g_object_ref (self->host_security_attrs); |
| 5832 | } |
| 5833 | |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 5834 | gboolean |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5835 | fu_engine_load_plugins (FuEngine *self, GError **error) |
| 5836 | { |
| 5837 | const gchar *fn; |
| 5838 | g_autoptr(GDir) dir = NULL; |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 5839 | g_autofree gchar *plugin_path = NULL; |
Richard Hughes | 00d6f47 | 2019-11-22 17:04:02 +0000 | [diff] [blame] | 5840 | g_autofree gchar *suffix = g_strdup_printf (".%s", G_MODULE_SUFFIX); |
Richard Hughes | 2428147 | 2020-10-05 11:45:47 +0100 | [diff] [blame] | 5841 | g_autoptr(GPtrArray) plugins_disabled = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5842 | g_autoptr(GPtrArray) plugins_disabled_rt = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 5843 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5844 | /* search */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 5845 | plugin_path = fu_common_get_path (FU_PATH_KIND_PLUGINDIR_PKG); |
| 5846 | dir = g_dir_open (plugin_path, 0, error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5847 | if (dir == NULL) |
| 5848 | return FALSE; |
| 5849 | while ((fn = g_dir_read_name (dir)) != NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5850 | g_autofree gchar *filename = NULL; |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5851 | g_autofree gchar *name = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5852 | g_autoptr(FuPlugin) plugin = NULL; |
| 5853 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5854 | g_autoptr(GPtrArray) firmware_gtypes = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5855 | |
| 5856 | /* ignore non-plugins */ |
Richard Hughes | 00d6f47 | 2019-11-22 17:04:02 +0000 | [diff] [blame] | 5857 | if (!g_str_has_suffix (fn, suffix)) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5858 | continue; |
| 5859 | |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 5860 | /* is disabled */ |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5861 | name = fu_plugin_guess_name_from_fn (fn); |
| 5862 | if (name == NULL) |
| 5863 | continue; |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5864 | if (fu_engine_is_plugin_name_disabled (self, name) || |
| 5865 | !fu_engine_is_plugin_name_enabled (self, name)) { |
Richard Hughes | 2428147 | 2020-10-05 11:45:47 +0100 | [diff] [blame] | 5866 | g_ptr_array_add (plugins_disabled, g_steal_pointer (&name)); |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 5867 | continue; |
| 5868 | } |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5869 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5870 | /* open module */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 5871 | filename = g_build_filename (plugin_path, fn, NULL); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5872 | plugin = fu_plugin_new (self->ctx); |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 5873 | fu_plugin_set_name (plugin, name); |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 5874 | |
| 5875 | /* if loaded from fu_engine_load() open the plugin */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5876 | firmware_gtypes = fu_context_get_firmware_gtype_ids (self->ctx); |
| 5877 | if (firmware_gtypes->len > 0) { |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 5878 | if (!fu_plugin_open (plugin, filename, &error_local)) { |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 5879 | g_warning ("cannot load: %s", error_local->message); |
Mario Limonciello | c3a8173 | 2020-10-20 09:16:18 -0500 | [diff] [blame] | 5880 | fu_engine_add_plugin (self, plugin); |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 5881 | continue; |
| 5882 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5883 | } |
| 5884 | |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5885 | /* runtime disabled */ |
| 5886 | if (fu_plugin_has_flag (plugin, FWUPD_PLUGIN_FLAG_DISABLED)) { |
| 5887 | g_ptr_array_add (plugins_disabled_rt, g_steal_pointer (&name)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5888 | continue; |
| 5889 | } |
| 5890 | |
| 5891 | /* watch for changes */ |
| 5892 | g_signal_connect (plugin, "device-added", |
| 5893 | G_CALLBACK (fu_engine_plugin_device_added_cb), |
| 5894 | self); |
| 5895 | g_signal_connect (plugin, "device-removed", |
| 5896 | G_CALLBACK (fu_engine_plugin_device_removed_cb), |
| 5897 | self); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 5898 | g_signal_connect (plugin, "device-register", |
| 5899 | G_CALLBACK (fu_engine_plugin_device_register_cb), |
| 5900 | self); |
Richard Hughes | aabdc37 | 2018-11-14 10:11:08 +0000 | [diff] [blame] | 5901 | g_signal_connect (plugin, "check-supported", |
| 5902 | G_CALLBACK (fu_engine_plugin_check_supported_cb), |
| 5903 | self); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 5904 | g_signal_connect (plugin, "rules-changed", |
| 5905 | G_CALLBACK (fu_engine_plugin_rules_changed_cb), |
| 5906 | self); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5907 | |
| 5908 | /* add */ |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 5909 | fu_engine_add_plugin (self, plugin); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5910 | } |
| 5911 | |
Richard Hughes | 2428147 | 2020-10-05 11:45:47 +0100 | [diff] [blame] | 5912 | /* show list */ |
| 5913 | if (plugins_disabled->len > 0) { |
| 5914 | g_autofree gchar *str = NULL; |
| 5915 | g_ptr_array_add (plugins_disabled, NULL); |
| 5916 | str = g_strjoinv (", ", (gchar **) plugins_disabled->pdata); |
| 5917 | g_debug ("plugins disabled: %s", str); |
| 5918 | } |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5919 | if (plugins_disabled_rt->len > 0) { |
Richard Hughes | 2428147 | 2020-10-05 11:45:47 +0100 | [diff] [blame] | 5920 | g_autofree gchar *str = NULL; |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 5921 | g_ptr_array_add (plugins_disabled_rt, NULL); |
| 5922 | str = g_strjoinv (", ", (gchar **) plugins_disabled_rt->pdata); |
| 5923 | g_debug ("plugins runtime-disabled: %s", str); |
Richard Hughes | 2428147 | 2020-10-05 11:45:47 +0100 | [diff] [blame] | 5924 | } |
| 5925 | |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5926 | /* depsolve into the correct order */ |
| 5927 | if (!fu_plugin_list_depsolve (self->plugin_list, error)) |
| 5928 | return FALSE; |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 5929 | |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 5930 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5931 | return TRUE; |
| 5932 | } |
| 5933 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 5934 | static gboolean |
| 5935 | fu_engine_cleanup_state (GError **error) |
| 5936 | { |
| 5937 | const gchar *filenames[] = { |
| 5938 | "/var/cache/app-info/xmls/fwupd-verify.xml", |
| 5939 | "/var/cache/app-info/xmls/fwupd.xml", |
| 5940 | NULL }; |
| 5941 | for (guint i = 0; filenames[i] != NULL; i++) { |
| 5942 | g_autoptr(GFile) file = g_file_new_for_path (filenames[i]); |
| 5943 | if (g_file_query_exists (file, NULL)) { |
| 5944 | if (!g_file_delete (file, NULL, error)) |
| 5945 | return FALSE; |
| 5946 | } |
| 5947 | } |
| 5948 | return TRUE; |
| 5949 | } |
| 5950 | |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 5951 | guint64 |
| 5952 | fu_engine_get_archive_size_max (FuEngine *self) |
| 5953 | { |
| 5954 | return fu_config_get_archive_size_max (self->config); |
| 5955 | } |
| 5956 | |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5957 | static void |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5958 | fu_engine_backend_device_removed_cb (FuBackend *backend, FuDevice *device, FuEngine *self) |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5959 | { |
| 5960 | g_autoptr(GPtrArray) devices = NULL; |
| 5961 | |
Richard Hughes | f3880bc | 2019-11-26 12:02:30 +0000 | [diff] [blame] | 5962 | /* debug */ |
| 5963 | if (g_getenv ("FWUPD_PROBE_VERBOSE") != NULL) { |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5964 | g_debug ("%s removed %s", |
| 5965 | fu_backend_get_name (backend), |
Richard Hughes | 253e6e4 | 2021-02-24 14:59:24 +0000 | [diff] [blame] | 5966 | fu_device_get_backend_id (device)); |
Richard Hughes | f3880bc | 2019-11-26 12:02:30 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5969 | /* go through each device and remove any that match */ |
Richard Hughes | c125ec0 | 2018-09-05 19:35:17 +0100 | [diff] [blame] | 5970 | devices = fu_device_list_get_all (self->device_list); |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5971 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5972 | FuDevice *device_tmp = g_ptr_array_index (devices, i); |
Richard Hughes | 253e6e4 | 2021-02-24 14:59:24 +0000 | [diff] [blame] | 5973 | if (g_strcmp0 (fu_device_get_backend_id (device_tmp), |
| 5974 | fu_device_get_backend_id (device)) == 0) { |
| 5975 | g_debug ("auto-removing backend device"); |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5976 | fu_device_list_remove (self->device_list, device_tmp); |
Richard Hughes | c125ec0 | 2018-09-05 19:35:17 +0100 | [diff] [blame] | 5977 | } |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5978 | } |
| 5979 | } |
| 5980 | |
| 5981 | static void |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5982 | fu_engine_backend_device_added_cb (FuBackend *backend, FuDevice *device, FuEngine *self) |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 5983 | { |
Richard Hughes | 6dec401 | 2018-08-27 19:13:00 +0100 | [diff] [blame] | 5984 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 45a1815 | 2019-10-30 15:29:04 +0000 | [diff] [blame] | 5985 | g_autoptr(GPtrArray) possible_plugins = NULL; |
Richard Hughes | 6dec401 | 2018-08-27 19:13:00 +0100 | [diff] [blame] | 5986 | |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5987 | /* super useful for plugin development */ |
Richard Hughes | f3880bc | 2019-11-26 12:02:30 +0000 | [diff] [blame] | 5988 | if (g_getenv ("FWUPD_PROBE_VERBOSE") != NULL) { |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5989 | g_autofree gchar *str = fu_device_to_string (FU_DEVICE (device)); |
| 5990 | g_debug ("%s added %s", fu_backend_get_name (backend), str); |
Richard Hughes | f3880bc | 2019-11-26 12:02:30 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Richard Hughes | 6dec401 | 2018-08-27 19:13:00 +0100 | [diff] [blame] | 5993 | /* add any extra quirks */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 5994 | fu_device_set_context (device, self->ctx); |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 5995 | if (!fu_device_probe (device, &error_local)) { |
Richard Hughes | 6dec401 | 2018-08-27 19:13:00 +0100 | [diff] [blame] | 5996 | g_warning ("failed to probe device %s: %s", |
Richard Hughes | 253e6e4 | 2021-02-24 14:59:24 +0000 | [diff] [blame] | 5997 | fu_device_get_backend_id (device), |
Richard Hughes | 6dec401 | 2018-08-27 19:13:00 +0100 | [diff] [blame] | 5998 | error_local->message); |
| 5999 | return; |
| 6000 | } |
Richard Hughes | 6dbe8fe | 2018-06-28 12:16:26 +0100 | [diff] [blame] | 6001 | |
Richard Hughes | f3c3adb | 2021-02-18 18:50:28 +0000 | [diff] [blame] | 6002 | /* super useful for plugin development */ |
| 6003 | if (g_getenv ("FWUPD_PROBE_VERBOSE") != NULL) { |
| 6004 | g_autofree gchar *str = fu_device_to_string (FU_DEVICE (device)); |
| 6005 | g_debug ("%s added %s", fu_backend_get_name (backend), str); |
| 6006 | } |
| 6007 | |
Richard Hughes | 23170a8 | 2018-08-29 09:11:52 +0100 | [diff] [blame] | 6008 | /* can be specified using a quirk */ |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6009 | possible_plugins = fu_device_get_possible_plugins (device); |
Richard Hughes | 45a1815 | 2019-10-30 15:29:04 +0000 | [diff] [blame] | 6010 | for (guint i = 0; i < possible_plugins->len; i++) { |
| 6011 | FuPlugin *plugin; |
| 6012 | const gchar *plugin_name = g_ptr_array_index (possible_plugins, i); |
| 6013 | g_autoptr(GError) error = NULL; |
Mario Limonciello | 0e50032 | 2019-10-17 18:41:04 -0500 | [diff] [blame] | 6014 | |
Richard Hughes | 9ed4d47 | 2020-10-05 11:37:26 +0100 | [diff] [blame] | 6015 | plugin = fu_plugin_list_find_by_name (self->plugin_list, plugin_name, NULL); |
| 6016 | if (plugin == NULL) |
Richard Hughes | 45a1815 | 2019-10-30 15:29:04 +0000 | [diff] [blame] | 6017 | continue; |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6018 | if (!fu_plugin_runner_backend_device_added (plugin, device, &error)) { |
Richard Hughes | 45a1815 | 2019-10-30 15:29:04 +0000 | [diff] [blame] | 6019 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED)) { |
| 6020 | if (g_getenv ("FWUPD_PROBE_VERBOSE") != NULL) { |
| 6021 | g_debug ("%s ignoring: %s", |
| 6022 | fu_plugin_get_name (plugin), |
| 6023 | error->message); |
Mario Limonciello | 0e50032 | 2019-10-17 18:41:04 -0500 | [diff] [blame] | 6024 | } |
Mario Limonciello | 0e50032 | 2019-10-17 18:41:04 -0500 | [diff] [blame] | 6025 | continue; |
| 6026 | } |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6027 | g_warning ("failed to add device %s: %s", |
Richard Hughes | 253e6e4 | 2021-02-24 14:59:24 +0000 | [diff] [blame] | 6028 | fu_device_get_backend_id (device), |
Richard Hughes | 45a1815 | 2019-10-30 15:29:04 +0000 | [diff] [blame] | 6029 | error->message); |
| 6030 | continue; |
Richard Hughes | 6dbe8fe | 2018-06-28 12:16:26 +0100 | [diff] [blame] | 6031 | } |
Richard Hughes | 6dbe8fe | 2018-06-28 12:16:26 +0100 | [diff] [blame] | 6032 | } |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 6033 | } |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6034 | |
| 6035 | static void |
| 6036 | fu_engine_backend_device_changed_cb (FuBackend *backend, FuDevice *device, FuEngine *self) |
| 6037 | { |
| 6038 | GPtrArray *plugins = fu_plugin_list_get_all (self->plugin_list); |
| 6039 | g_autoptr(GPtrArray) devices = NULL; |
| 6040 | |
| 6041 | /* debug */ |
| 6042 | if (g_getenv ("FWUPD_PROBE_VERBOSE") != NULL) { |
| 6043 | g_debug ("%s changed %s", |
| 6044 | fu_backend_get_name (backend), |
| 6045 | fu_device_get_physical_id (device)); |
| 6046 | } |
| 6047 | |
| 6048 | /* emit changed on any that match */ |
| 6049 | devices = fu_device_list_get_all (self->device_list); |
| 6050 | for (guint i = 0; i < devices->len; i++) { |
| 6051 | FuDevice *device_tmp = g_ptr_array_index (devices, i); |
| 6052 | if (!FU_IS_UDEV_DEVICE (device_tmp)) |
| 6053 | continue; |
| 6054 | if (g_strcmp0 (fu_udev_device_get_sysfs_path (FU_UDEV_DEVICE (device_tmp)), |
| 6055 | fu_udev_device_get_sysfs_path (FU_UDEV_DEVICE (device))) == 0) { |
| 6056 | fu_udev_device_emit_changed (FU_UDEV_DEVICE (device)); |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | /* run all plugins */ |
| 6061 | for (guint j = 0; j < plugins->len; j++) { |
| 6062 | FuPlugin *plugin_tmp = g_ptr_array_index (plugins, j); |
| 6063 | g_autoptr(GError) error = NULL; |
| 6064 | if (!fu_plugin_runner_backend_device_changed (plugin_tmp, device, &error)) { |
| 6065 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED)) { |
| 6066 | g_debug ("%s ignoring: %s", |
| 6067 | fu_plugin_get_name (plugin_tmp), |
| 6068 | error->message); |
| 6069 | continue; |
| 6070 | } |
| 6071 | g_warning ("%s failed to change udev device %s: %s", |
| 6072 | fu_plugin_get_name (plugin_tmp), |
| 6073 | fu_udev_device_get_sysfs_path (FU_UDEV_DEVICE (device)), |
| 6074 | error->message); |
| 6075 | } |
| 6076 | } |
| 6077 | } |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 6078 | |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 6079 | static void |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6080 | fu_engine_load_quirks_for_hwid (FuEngine *self, const gchar *hwid) |
| 6081 | { |
| 6082 | FuPlugin *plugin; |
| 6083 | const gchar *value; |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6084 | g_auto(GStrv) plugins = NULL; |
| 6085 | |
| 6086 | /* does prefixed quirk exist */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6087 | value = fu_context_lookup_quirk_by_id (self->ctx, hwid, FU_QUIRKS_PLUGIN); |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6088 | if (value == NULL) |
| 6089 | return; |
| 6090 | plugins = g_strsplit (value, ",", -1); |
| 6091 | for (guint i = 0; plugins[i] != NULL; i++) { |
| 6092 | g_autoptr(GError) error_local = NULL; |
| 6093 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 6094 | plugins[i], &error_local); |
| 6095 | if (plugin == NULL) { |
| 6096 | g_debug ("no %s plugin for HwId %s: %s", |
| 6097 | plugins[i], hwid, error_local->message); |
| 6098 | continue; |
| 6099 | } |
| 6100 | g_debug ("enabling %s due to HwId %s", plugins[i], hwid); |
| 6101 | fu_plugin_remove_flag (plugin, FWUPD_PLUGIN_FLAG_REQUIRE_HWID); |
| 6102 | } |
| 6103 | } |
| 6104 | |
| 6105 | static void |
Richard Hughes | 51a869a | 2019-10-07 11:23:42 +0100 | [diff] [blame] | 6106 | fu_engine_load_quirks (FuEngine *self, FuQuirksLoadFlags quirks_flags) |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 6107 | { |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6108 | GPtrArray *guids = fu_context_get_hwid_guids (self->ctx); |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 6109 | g_autoptr(GError) error = NULL; |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6110 | |
| 6111 | /* rebuild silo if required */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6112 | if (!fu_context_load_quirks (self->ctx, quirks_flags, &error)) { |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 6113 | g_warning ("Failed to load quirks: %s", error->message); |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6114 | return; |
| 6115 | } |
| 6116 | |
| 6117 | /* search each hwid */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6118 | for (guint i = 0; i < guids->len; i++) { |
| 6119 | const gchar *hwid = g_ptr_array_index (guids, i); |
Richard Hughes | d94286b | 2021-03-01 21:12:18 +0000 | [diff] [blame] | 6120 | fu_engine_load_quirks_for_hwid (self, hwid); |
| 6121 | } |
Richard Hughes | f77d706 | 2017-11-27 12:06:36 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6124 | static gboolean |
| 6125 | fu_engine_update_history_device (FuEngine *self, FuDevice *dev_history, GError **error) |
| 6126 | { |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6127 | FuPlugin *plugin; |
| 6128 | FwupdRelease *rel_history; |
Richard Hughes | f271142 | 2018-01-12 09:49:05 +0000 | [diff] [blame] | 6129 | g_autofree gchar *btime = NULL; |
Richard Hughes | 5a9a6bd | 2018-09-10 10:02:20 +0100 | [diff] [blame] | 6130 | g_autoptr(FuDevice) dev = NULL; |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 6131 | g_autoptr(GHashTable) metadata_device = NULL; |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6132 | |
| 6133 | /* is in the device list */ |
Richard Hughes | 4012754 | 2018-01-12 20:25:55 +0000 | [diff] [blame] | 6134 | dev = fu_device_list_get_by_id (self->device_list, |
| 6135 | fu_device_get_id (dev_history), |
| 6136 | error); |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6137 | if (dev == NULL) |
| 6138 | return FALSE; |
| 6139 | |
| 6140 | /* does the installed version match what we tried to install |
| 6141 | * before fwupd was restarted */ |
| 6142 | rel_history = fu_device_get_release_default (dev_history); |
| 6143 | if (rel_history == NULL) { |
| 6144 | g_set_error_literal (error, |
| 6145 | FWUPD_ERROR, |
| 6146 | FWUPD_ERROR_INTERNAL, |
| 6147 | "no release for history FuDevice"); |
| 6148 | return FALSE; |
| 6149 | } |
| 6150 | |
Richard Hughes | f271142 | 2018-01-12 09:49:05 +0000 | [diff] [blame] | 6151 | /* is this the same boot time as when we scheduled the update, |
| 6152 | * i.e. has fwupd been restarted before we rebooted */ |
| 6153 | btime = fu_engine_get_boot_time (); |
| 6154 | if (g_strcmp0 (fwupd_release_get_metadata_item (rel_history, "BootTime"), |
| 6155 | btime) == 0) { |
| 6156 | g_debug ("service restarted, but no reboot has taken place"); |
Richard Hughes | 60441f9 | 2021-01-13 14:03:42 +0000 | [diff] [blame] | 6157 | |
| 6158 | /* if it needed reboot then, it also needs it now... */ |
| 6159 | if (fu_device_get_update_state (dev_history) == FWUPD_UPDATE_STATE_NEEDS_REBOOT) { |
| 6160 | g_debug ("inheriting needs-reboot for %s", |
| 6161 | fu_device_get_name (dev)); |
| 6162 | fu_device_set_update_state (dev, FWUPD_UPDATE_STATE_NEEDS_REBOOT); |
| 6163 | } |
Richard Hughes | f271142 | 2018-01-12 09:49:05 +0000 | [diff] [blame] | 6164 | return TRUE; |
| 6165 | } |
| 6166 | |
Richard Hughes | b114661 | 2020-06-16 14:56:16 +0100 | [diff] [blame] | 6167 | /* save any additional report metadata */ |
| 6168 | metadata_device = fu_device_report_metadata_post (dev); |
| 6169 | if (metadata_device != NULL && g_hash_table_size (metadata_device) > 0) { |
| 6170 | fwupd_release_add_metadata (rel_history, metadata_device); |
| 6171 | if (!fu_history_set_device_metadata (self->history, |
| 6172 | fu_device_get_id (dev_history), |
| 6173 | fwupd_release_get_metadata (rel_history), |
| 6174 | error)) { |
| 6175 | g_prefix_error (error, "failed to set metadata: "); |
| 6176 | return FALSE; |
| 6177 | } |
| 6178 | } |
| 6179 | |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6180 | /* the system is running with the new firmware version */ |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 6181 | if (fu_common_vercmp_full (fu_device_get_version (dev), |
| 6182 | fwupd_release_get_version (rel_history), |
| 6183 | fu_device_get_version_format (dev)) == 0) { |
Richard Hughes | 4e886a4 | 2018-12-12 10:33:26 +0000 | [diff] [blame] | 6184 | GPtrArray *checksums; |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6185 | g_debug ("installed version %s matching history %s", |
| 6186 | fu_device_get_version (dev), |
| 6187 | fwupd_release_get_version (rel_history)); |
Richard Hughes | 4e886a4 | 2018-12-12 10:33:26 +0000 | [diff] [blame] | 6188 | |
| 6189 | /* copy over runtime checksums if set from probe() */ |
| 6190 | checksums = fu_device_get_checksums (dev); |
| 6191 | for (guint i = 0; i < checksums->len; i++) { |
| 6192 | const gchar *csum = g_ptr_array_index (checksums, i); |
| 6193 | fu_device_add_checksum (dev_history, csum); |
| 6194 | } |
Richard Hughes | f50ff2c | 2020-02-25 09:45:15 +0000 | [diff] [blame] | 6195 | fu_device_set_version_format (dev_history, fu_device_get_version_format (dev)); |
| 6196 | fu_device_set_version (dev_history, fu_device_get_version (dev)); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 6197 | fu_device_remove_flag (dev_history, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION); |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 6198 | fu_device_set_update_state (dev_history, FWUPD_UPDATE_STATE_SUCCESS); |
Richard Hughes | e261bb6 | 2020-06-10 19:03:00 +0100 | [diff] [blame] | 6199 | fu_device_set_update_error (dev_history, NULL); |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 6200 | return fu_history_modify_device (self->history, dev_history, error); |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
Richard Hughes | 7e070c9 | 2018-01-12 16:50:05 +0000 | [diff] [blame] | 6203 | /* does the plugin know the update failure */ |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6204 | plugin = fu_plugin_list_find_by_name (self->plugin_list, |
| 6205 | fu_device_get_plugin (dev), |
| 6206 | error); |
| 6207 | if (plugin == NULL) |
| 6208 | return FALSE; |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6209 | if (!fu_plugin_runner_get_results (plugin, dev, error)) |
| 6210 | return FALSE; |
Richard Hughes | 7e070c9 | 2018-01-12 16:50:05 +0000 | [diff] [blame] | 6211 | |
| 6212 | /* the plugin either can't tell us the error, or doesn't know itself */ |
Richard Hughes | cce6a1c | 2019-04-16 17:25:48 +0100 | [diff] [blame] | 6213 | if (fu_device_get_update_state (dev) != FWUPD_UPDATE_STATE_FAILED && |
| 6214 | fu_device_get_update_state (dev) != FWUPD_UPDATE_STATE_FAILED_TRANSIENT) { |
Richard Hughes | 7e070c9 | 2018-01-12 16:50:05 +0000 | [diff] [blame] | 6215 | g_debug ("falling back to generic failure"); |
Richard Hughes | e261bb6 | 2020-06-10 19:03:00 +0100 | [diff] [blame] | 6216 | fu_device_set_update_state (dev_history, FWUPD_UPDATE_STATE_FAILED); |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 6217 | fu_device_set_update_error (dev_history, "failed to run update on reboot"); |
Richard Hughes | e261bb6 | 2020-06-10 19:03:00 +0100 | [diff] [blame] | 6218 | } else { |
| 6219 | fu_device_set_update_state (dev_history, fu_device_get_update_state (dev)); |
| 6220 | fu_device_set_update_error (dev_history, fu_device_get_update_error (dev)); |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6221 | } |
Richard Hughes | 7e070c9 | 2018-01-12 16:50:05 +0000 | [diff] [blame] | 6222 | |
| 6223 | /* update the state in the database */ |
Richard Hughes | 0bbef29 | 2019-11-01 12:15:15 +0000 | [diff] [blame] | 6224 | return fu_history_modify_device (self->history, dev_history, error); |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | static gboolean |
| 6228 | fu_engine_update_history_database (FuEngine *self, GError **error) |
| 6229 | { |
| 6230 | g_autoptr(GPtrArray) devices = NULL; |
| 6231 | |
| 6232 | /* get any devices */ |
| 6233 | devices = fu_history_get_devices (self->history, error); |
| 6234 | if (devices == NULL) |
| 6235 | return FALSE; |
| 6236 | for (guint i = 0; i < devices->len; i++) { |
| 6237 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 6238 | g_autoptr(GError) error_local = NULL; |
| 6239 | |
| 6240 | /* not in the required state */ |
Richard Hughes | b2a1d97 | 2021-01-30 17:03:01 +0000 | [diff] [blame] | 6241 | if (fu_device_get_update_state (dev) != FWUPD_UPDATE_STATE_NEEDS_REBOOT && |
| 6242 | fu_device_get_update_state (dev) != FWUPD_UPDATE_STATE_PENDING) |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6243 | continue; |
| 6244 | |
| 6245 | /* try to save the new update-state, but ignoring any error */ |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 6246 | if (!fu_engine_update_history_device (self, dev, &error_local)) { |
| 6247 | g_warning ("failed to update history database: %s", |
| 6248 | error_local->message); |
| 6249 | } |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6250 | } |
| 6251 | return TRUE; |
| 6252 | } |
| 6253 | |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6254 | static void |
| 6255 | fu_engine_ensure_client_certificate (FuEngine *self) |
| 6256 | { |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6257 | g_autoptr(GBytes) blob = g_bytes_new_static ("test\0", 5); |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6258 | g_autoptr(GError) error = NULL; |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6259 | g_autoptr(JcatBlob) jcat_sig = NULL; |
| 6260 | g_autoptr(JcatEngine) jcat_engine = NULL; |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6261 | |
| 6262 | /* create keyring and sign dummy data to ensure certificate exists */ |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6263 | jcat_engine = jcat_context_get_engine (self->jcat_context, |
| 6264 | JCAT_BLOB_KIND_PKCS7, |
| 6265 | &error); |
| 6266 | if (jcat_engine == NULL) { |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6267 | g_message ("failed to create keyring: %s", error->message); |
| 6268 | return; |
| 6269 | } |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6270 | jcat_sig = jcat_engine_self_sign (jcat_engine, blob, JCAT_SIGN_FLAG_NONE, &error); |
| 6271 | if (jcat_sig == NULL) { |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6272 | g_message ("failed to sign using keyring: %s", error->message); |
| 6273 | return; |
| 6274 | } |
| 6275 | g_debug ("client certificate exists and working"); |
| 6276 | } |
| 6277 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6278 | /** |
| 6279 | * fu_engine_load: |
| 6280 | * @self: A #FuEngine |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 6281 | * @flags: #FuEngineLoadFlags, e.g. %FU_ENGINE_LOAD_FLAG_READONLY |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6282 | * @error: A #GError, or %NULL |
| 6283 | * |
| 6284 | * Load the firmware update engine so it is ready for use. |
| 6285 | * |
| 6286 | * Returns: %TRUE for success |
| 6287 | **/ |
| 6288 | gboolean |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 6289 | fu_engine_load (FuEngine *self, FuEngineLoadFlags flags, GError **error) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6290 | { |
Richard Hughes | 51a869a | 2019-10-07 11:23:42 +0100 | [diff] [blame] | 6291 | FuQuirksLoadFlags quirks_flags = FU_QUIRKS_LOAD_FLAG_NONE; |
Richard Hughes | 404f052 | 2021-02-16 18:25:32 +0000 | [diff] [blame] | 6292 | guint backend_cnt = 0; |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6293 | g_autoptr(GPtrArray) checksums_approved = NULL; |
| 6294 | g_autoptr(GPtrArray) checksums_blocked = NULL; |
Norbert Kamiński | a863e6a | 2021-04-02 15:50:35 +0200 | [diff] [blame] | 6295 | #ifndef _WIN32 |
Daniel Campello | b5ae60c | 2020-02-27 17:06:28 -0700 | [diff] [blame] | 6296 | g_autoptr(GError) error_local = NULL; |
| 6297 | #endif |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6298 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6299 | g_return_val_if_fail (FU_IS_ENGINE (self), FALSE); |
| 6300 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 6301 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 6302 | /* avoid re-loading a second time if fu-tool or fu-util request to */ |
| 6303 | if (self->loaded) |
| 6304 | return TRUE; |
| 6305 | |
Richard Hughes | 45a0073 | 2019-11-22 16:57:14 +0000 | [diff] [blame] | 6306 | /* TODO: Read registry key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography] "MachineGuid" */ |
| 6307 | #ifndef _WIN32 |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 6308 | /* cache machine ID so we can use it from a sandboxed app */ |
Daniel Campello | b5ae60c | 2020-02-27 17:06:28 -0700 | [diff] [blame] | 6309 | self->host_machine_id = fwupd_build_machine_id ("fwupd", &error_local); |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 6310 | if (self->host_machine_id == NULL) |
Richard Hughes | 521c2e2 | 2020-10-01 14:12:41 +0100 | [diff] [blame] | 6311 | g_debug ("failed to build machine-id: %s", error_local->message); |
Richard Hughes | 45a0073 | 2019-11-22 16:57:14 +0000 | [diff] [blame] | 6312 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6313 | /* read config file */ |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6314 | if (!fu_config_load (self->config, error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6315 | g_prefix_error (error, "Failed to load config: "); |
| 6316 | return FALSE; |
| 6317 | } |
| 6318 | |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6319 | /* read remotes */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 6320 | if (flags & FU_ENGINE_LOAD_FLAG_REMOTES) { |
| 6321 | FuRemoteListLoadFlags remote_list_flags = FU_REMOTE_LIST_LOAD_FLAG_NONE; |
| 6322 | if (flags & FU_ENGINE_LOAD_FLAG_READONLY) |
| 6323 | remote_list_flags |= FU_REMOTE_LIST_LOAD_FLAG_READONLY_FS; |
| 6324 | if (!fu_remote_list_load (self->remote_list, remote_list_flags, error)) { |
| 6325 | g_prefix_error (error, "Failed to load remotes: "); |
| 6326 | return FALSE; |
| 6327 | } |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
Richard Hughes | f3c7d42 | 2019-03-12 10:29:42 +0000 | [diff] [blame] | 6330 | /* create client certificate */ |
| 6331 | fu_engine_ensure_client_certificate (self); |
| 6332 | |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6333 | /* get hardcoded approved and blocked firmware */ |
| 6334 | checksums_approved = fu_config_get_approved_firmware (self->config); |
| 6335 | for (guint i = 0; i < checksums_approved->len; i++) { |
| 6336 | const gchar *csum = g_ptr_array_index (checksums_approved, i); |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6337 | fu_engine_add_approved_firmware (self, csum); |
| 6338 | } |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6339 | checksums_blocked = fu_config_get_blocked_firmware (self->config); |
| 6340 | for (guint i = 0; i < checksums_blocked->len; i++) { |
| 6341 | const gchar *csum = g_ptr_array_index (checksums_blocked, i); |
| 6342 | fu_engine_add_blocked_firmware (self, csum); |
| 6343 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6344 | |
| 6345 | /* get extra firmware saved to the database */ |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6346 | checksums_approved = fu_history_get_approved_firmware (self->history, error); |
| 6347 | if (checksums_approved == NULL) |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6348 | return FALSE; |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6349 | for (guint i = 0; i < checksums_approved->len; i++) { |
| 6350 | const gchar *csum = g_ptr_array_index (checksums_approved, i); |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6351 | fu_engine_add_approved_firmware (self, csum); |
| 6352 | } |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6353 | checksums_blocked = fu_history_get_blocked_firmware (self->history, error); |
| 6354 | if (checksums_blocked == NULL) |
| 6355 | return FALSE; |
| 6356 | for (guint i = 0; i < checksums_blocked->len; i++) { |
| 6357 | const gchar *csum = g_ptr_array_index (checksums_blocked, i); |
| 6358 | fu_engine_add_blocked_firmware (self, csum); |
| 6359 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 6360 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 6361 | /* set up idle exit */ |
| 6362 | if ((self->app_flags & FU_APP_FLAGS_NO_IDLE_SOURCES) == 0) |
| 6363 | fu_idle_set_timeout (self->idle, fu_config_get_idle_timeout (self->config)); |
| 6364 | |
Richard Hughes | 0521733 | 2021-03-01 15:51:15 +0000 | [diff] [blame] | 6365 | /* load SMBIOS and the hwids */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6366 | if (flags & FU_ENGINE_LOAD_FLAG_HWINFO) |
| 6367 | fu_context_load_hwinfo (self->ctx, NULL); |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 6368 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6369 | /* load AppStream metadata */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 6370 | if (!fu_engine_load_metadata_store (self, flags, error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6371 | g_prefix_error (error, "Failed to load AppStream data: "); |
| 6372 | return FALSE; |
| 6373 | } |
| 6374 | |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 6375 | /* add the "built-in" firmware types */ |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6376 | fu_context_add_firmware_gtype (self->ctx, "raw", FU_TYPE_FIRMWARE); |
| 6377 | fu_context_add_firmware_gtype (self->ctx, "dfu", FU_TYPE_DFU_FIRMWARE); |
| 6378 | fu_context_add_firmware_gtype (self->ctx, "dfuse", FU_TYPE_DFUSE_FIRMWARE); |
| 6379 | fu_context_add_firmware_gtype (self->ctx, "fmap", FU_TYPE_FMAP_FIRMWARE); |
| 6380 | fu_context_add_firmware_gtype (self->ctx, "ihex", FU_TYPE_IHEX_FIRMWARE); |
| 6381 | fu_context_add_firmware_gtype (self->ctx, "srec", FU_TYPE_SREC_FIRMWARE); |
| 6382 | fu_context_add_firmware_gtype (self->ctx, "smbios", FU_TYPE_SMBIOS); |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 6383 | |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6384 | /* set up backends */ |
| 6385 | for (guint i = 0; i < self->backends->len; i++) { |
| 6386 | FuBackend *backend = g_ptr_array_index (self->backends, i); |
Richard Hughes | 404f052 | 2021-02-16 18:25:32 +0000 | [diff] [blame] | 6387 | g_autoptr(GError) error_backend = NULL; |
| 6388 | if (!fu_backend_setup (backend, &error_backend)) { |
| 6389 | g_debug ("failed to setup backend %s: %s", |
| 6390 | fu_backend_get_name (backend), |
| 6391 | error_backend->message); |
| 6392 | continue; |
| 6393 | } |
| 6394 | backend_cnt++; |
| 6395 | } |
| 6396 | if (backend_cnt == 0) { |
| 6397 | g_set_error_literal (error, |
| 6398 | FWUPD_ERROR, |
| 6399 | FWUPD_ERROR_NOT_SUPPORTED, |
| 6400 | "all backends failed setup"); |
| 6401 | return FALSE; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6402 | } |
| 6403 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6404 | /* delete old data files */ |
| 6405 | if (!fu_engine_cleanup_state (error)) { |
| 6406 | g_prefix_error (error, "Failed to clean up: "); |
| 6407 | return FALSE; |
| 6408 | } |
| 6409 | |
| 6410 | /* load plugin */ |
| 6411 | if (!fu_engine_load_plugins (self, error)) { |
| 6412 | g_prefix_error (error, "Failed to load plugins: "); |
| 6413 | return FALSE; |
| 6414 | } |
| 6415 | |
Richard Hughes | 0521733 | 2021-03-01 15:51:15 +0000 | [diff] [blame] | 6416 | /* on a read-only filesystem don't care about the cache GUID */ |
| 6417 | if (flags & FU_ENGINE_LOAD_FLAG_READONLY) |
| 6418 | quirks_flags |= FU_QUIRKS_LOAD_FLAG_READONLY_FS; |
| 6419 | fu_engine_load_quirks (self, quirks_flags); |
| 6420 | |
Richard Hughes | 170c0c1 | 2017-11-22 11:26:24 +0000 | [diff] [blame] | 6421 | /* watch the device list for updates and proxy */ |
| 6422 | g_signal_connect (self->device_list, "added", |
| 6423 | G_CALLBACK (fu_engine_device_added_cb), |
| 6424 | self); |
| 6425 | g_signal_connect (self->device_list, "removed", |
| 6426 | G_CALLBACK (fu_engine_device_removed_cb), |
| 6427 | self); |
| 6428 | g_signal_connect (self->device_list, "changed", |
| 6429 | G_CALLBACK (fu_engine_device_changed_cb), |
| 6430 | self); |
Mario Limonciello | 6463f31 | 2018-08-09 13:28:46 -0500 | [diff] [blame] | 6431 | fu_engine_set_status (self, FWUPD_STATUS_LOADING); |
| 6432 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6433 | /* add devices */ |
| 6434 | fu_engine_plugins_setup (self); |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 6435 | if (flags & FU_ENGINE_LOAD_FLAG_COLDPLUG) |
Richard Hughes | 4ae7b5e | 2021-04-01 14:43:43 +0100 | [diff] [blame] | 6436 | fu_engine_plugins_coldplug (self); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6437 | |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6438 | /* coldplug backends */ |
| 6439 | if (flags & FU_ENGINE_LOAD_FLAG_COLDPLUG) { |
| 6440 | for (guint i = 0; i < self->backends->len; i++) { |
| 6441 | FuBackend *backend = g_ptr_array_index (self->backends, i); |
Richard Hughes | 404f052 | 2021-02-16 18:25:32 +0000 | [diff] [blame] | 6442 | g_autoptr(GError) error_backend = NULL; |
| 6443 | if (!fu_backend_get_enabled (backend)) |
| 6444 | continue; |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6445 | g_signal_connect (backend, "device-added", |
| 6446 | G_CALLBACK (fu_engine_backend_device_added_cb), |
| 6447 | self); |
| 6448 | g_signal_connect (backend, "device-removed", |
| 6449 | G_CALLBACK (fu_engine_backend_device_removed_cb), |
| 6450 | self); |
| 6451 | g_signal_connect (backend, "device-changed", |
| 6452 | G_CALLBACK (fu_engine_backend_device_changed_cb), |
| 6453 | self); |
Richard Hughes | 404f052 | 2021-02-16 18:25:32 +0000 | [diff] [blame] | 6454 | if (!fu_backend_coldplug (backend, &error_backend)) { |
| 6455 | g_warning ("failed to coldplug backend %s: %s", |
| 6456 | fu_backend_get_name (backend), |
| 6457 | error_backend->message); |
| 6458 | continue; |
| 6459 | } |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6460 | } |
| 6461 | } |
Richard Hughes | 9d6e0e7 | 2018-08-24 20:20:17 +0100 | [diff] [blame] | 6462 | |
Richard Hughes | f43381f | 2020-02-24 10:11:31 +0000 | [diff] [blame] | 6463 | /* set device properties from the metadata */ |
| 6464 | fu_engine_md_refresh_devices (self); |
| 6465 | |
Richard Hughes | a2f8e45 | 2018-01-11 10:11:17 +0000 | [diff] [blame] | 6466 | /* update the db for devices that were updated during the reboot */ |
| 6467 | if (!fu_engine_update_history_database (self, error)) |
| 6468 | return FALSE; |
| 6469 | |
Mario Limonciello | 6463f31 | 2018-08-09 13:28:46 -0500 | [diff] [blame] | 6470 | fu_engine_set_status (self, FWUPD_STATUS_IDLE); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 6471 | self->loaded = TRUE; |
Mario Limonciello | 6463f31 | 2018-08-09 13:28:46 -0500 | [diff] [blame] | 6472 | |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 6473 | /* let clients know engine finished starting up */ |
| 6474 | fu_engine_emit_changed (self); |
| 6475 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6476 | /* success */ |
| 6477 | return TRUE; |
| 6478 | } |
| 6479 | |
| 6480 | static void |
| 6481 | fu_engine_class_init (FuEngineClass *klass) |
| 6482 | { |
| 6483 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 6484 | object_class->finalize = fu_engine_finalize; |
| 6485 | |
| 6486 | signals[SIGNAL_CHANGED] = |
| 6487 | g_signal_new ("changed", |
| 6488 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6489 | 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, |
| 6490 | G_TYPE_NONE, 0); |
| 6491 | signals[SIGNAL_DEVICE_ADDED] = |
| 6492 | g_signal_new ("device-added", |
| 6493 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6494 | 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 6495 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 6496 | signals[SIGNAL_DEVICE_REMOVED] = |
| 6497 | g_signal_new ("device-removed", |
| 6498 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6499 | 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 6500 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | a5bb4d8 | 2017-06-19 20:22:25 +0100 | [diff] [blame] | 6501 | signals[SIGNAL_DEVICE_CHANGED] = |
| 6502 | g_signal_new ("device-changed", |
| 6503 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6504 | 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 6505 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6506 | signals[SIGNAL_STATUS_CHANGED] = |
| 6507 | g_signal_new ("status-changed", |
| 6508 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6509 | 0, NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 6510 | G_TYPE_NONE, 1, G_TYPE_UINT); |
| 6511 | signals[SIGNAL_PERCENTAGE_CHANGED] = |
| 6512 | g_signal_new ("percentage-changed", |
| 6513 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 6514 | 0, NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 6515 | G_TYPE_NONE, 1, G_TYPE_UINT); |
| 6516 | } |
| 6517 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6518 | void |
| 6519 | fu_engine_add_runtime_version (FuEngine *self, |
| 6520 | const gchar *component_id, |
| 6521 | const gchar *version) |
| 6522 | { |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6523 | fu_context_add_runtime_version (self->ctx, component_id, version); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6524 | } |
| 6525 | |
Richard Hughes | f517c9a | 2019-03-22 19:50:35 +0000 | [diff] [blame] | 6526 | void |
| 6527 | fu_engine_add_app_flag (FuEngine *self, FuAppFlags app_flags) |
| 6528 | { |
| 6529 | g_return_if_fail (FU_IS_ENGINE (self)); |
| 6530 | self->app_flags |= app_flags; |
| 6531 | } |
| 6532 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6533 | static void |
Richard Hughes | 4d76d18 | 2021-04-06 13:35:15 +0100 | [diff] [blame] | 6534 | fu_engine_context_battery_changed_cb (FuContext *ctx, GParamSpec *pspec, FuEngine *self) |
| 6535 | { |
| 6536 | g_autoptr(GPtrArray) devices = fu_device_list_get_all (self->device_list); |
| 6537 | |
| 6538 | /* apply policy on any existing devices */ |
| 6539 | for (guint i = 0; i < devices->len; i++) { |
| 6540 | FuDevice *device = g_ptr_array_index (devices, i); |
| 6541 | fu_engine_ensure_device_battery_inhibit (self, device); |
| 6542 | } |
| 6543 | } |
| 6544 | |
| 6545 | static void |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 6546 | fu_engine_idle_status_notify_cb (FuIdle *idle, GParamSpec *pspec, FuEngine *self) |
| 6547 | { |
| 6548 | FwupdStatus status = fu_idle_get_status (idle); |
| 6549 | if (status == FWUPD_STATUS_SHUTDOWN) |
| 6550 | fu_engine_set_status (self, status); |
| 6551 | } |
| 6552 | |
| 6553 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6554 | fu_engine_init (FuEngine *self) |
| 6555 | { |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 6556 | #ifdef HAVE_UTSNAME_H |
Richard Hughes | f679e5e | 2019-08-28 15:31:34 +0100 | [diff] [blame] | 6557 | struct utsname uname_tmp; |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 6558 | #endif |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6559 | g_autofree gchar *keyring_path = NULL; |
| 6560 | g_autofree gchar *pkidir_fw = NULL; |
| 6561 | g_autofree gchar *pkidir_md = NULL; |
| 6562 | g_autofree gchar *sysconfdir = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6563 | self->percentage = 0; |
| 6564 | self->status = FWUPD_STATUS_IDLE; |
| 6565 | self->config = fu_config_new (); |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6566 | self->remote_list = fu_remote_list_new (); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 6567 | self->device_list = fu_device_list_new (); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6568 | self->ctx = fu_context_new (); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 6569 | self->idle = fu_idle_new (); |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 6570 | self->history = fu_history_new (); |
Richard Hughes | e7e9545 | 2017-11-22 09:05:53 +0000 | [diff] [blame] | 6571 | self->plugin_list = fu_plugin_list_new (); |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 6572 | self->plugin_filter = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 6573 | self->host_security_attrs = fu_security_attrs_new (); |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6574 | self->backends = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6575 | self->runtime_versions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6576 | self->compile_versions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6577 | |
| 6578 | fu_context_set_runtime_versions (self->ctx, self->runtime_versions); |
| 6579 | fu_context_set_compile_versions (self->ctx, self->compile_versions); |
| 6580 | |
| 6581 | g_signal_connect (self->ctx, "security-changed", |
| 6582 | G_CALLBACK (fu_engine_context_security_changed_cb), |
| 6583 | self); |
Richard Hughes | 4d76d18 | 2021-04-06 13:35:15 +0100 | [diff] [blame] | 6584 | g_signal_connect (self->ctx, "notify::battery-state", |
| 6585 | G_CALLBACK (fu_engine_context_battery_changed_cb), |
| 6586 | self); |
| 6587 | g_signal_connect (self->ctx, "notify::battery-level", |
| 6588 | G_CALLBACK (fu_engine_context_battery_changed_cb), |
| 6589 | self); |
| 6590 | g_signal_connect (self->ctx, "notify::battery-threshold", |
| 6591 | G_CALLBACK (fu_engine_context_battery_changed_cb), |
| 6592 | self); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6593 | |
Mario Limonciello | 263cab9 | 2019-08-20 17:16:00 -0500 | [diff] [blame] | 6594 | g_signal_connect (self->config, "changed", |
| 6595 | G_CALLBACK (fu_engine_config_changed_cb), |
| 6596 | self); |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6597 | g_signal_connect (self->remote_list, "changed", |
| 6598 | G_CALLBACK (fu_engine_remote_list_changed_cb), |
| 6599 | self); |
Mario Limonciello | 263cab9 | 2019-08-20 17:16:00 -0500 | [diff] [blame] | 6600 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 6601 | g_signal_connect (self->idle, "notify::status", |
| 6602 | G_CALLBACK (fu_engine_idle_status_notify_cb), self); |
| 6603 | |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6604 | /* backends */ |
| 6605 | #ifdef HAVE_GUSB |
| 6606 | g_ptr_array_add (self->backends, fu_usb_backend_new ()); |
| 6607 | #endif |
| 6608 | #ifdef HAVE_GUDEV |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6609 | g_ptr_array_add (self->backends, fu_udev_backend_new (fu_context_get_udev_subsystems (self->ctx))); |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6610 | #endif |
Ricardo Cañuelo | 536fb85 | 2021-02-12 12:48:58 +0100 | [diff] [blame] | 6611 | #ifdef HAVE_BLUEZ |
| 6612 | g_ptr_array_add (self->backends, fu_bluez_backend_new ()); |
| 6613 | #endif |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6614 | |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6615 | /* setup Jcat context */ |
| 6616 | self->jcat_context = jcat_context_new (); |
| 6617 | keyring_path = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 6618 | jcat_context_set_keyring_path (self->jcat_context, keyring_path); |
| 6619 | sysconfdir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR); |
| 6620 | pkidir_fw = g_build_filename (sysconfdir, "pki", "fwupd", NULL); |
| 6621 | jcat_context_add_public_keys (self->jcat_context, pkidir_fw); |
| 6622 | pkidir_md = g_build_filename (sysconfdir, "pki", "fwupd-metadata", NULL); |
| 6623 | jcat_context_add_public_keys (self->jcat_context, pkidir_md); |
| 6624 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6625 | /* add some runtime versions of things the daemon depends on */ |
| 6626 | fu_engine_add_runtime_version (self, "org.freedesktop.fwupd", VERSION); |
Richard Hughes | 2d37c3f | 2018-08-04 15:18:25 +0100 | [diff] [blame] | 6627 | fu_engine_add_runtime_version (self, "com.redhat.fwupdate", "12"); |
Richard Hughes | 416ade7 | 2018-10-10 20:36:53 +0100 | [diff] [blame] | 6628 | fu_engine_add_runtime_version (self, "org.freedesktop.appstream-glib", "0.7.14"); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6629 | #if G_USB_CHECK_VERSION(0,3,1) |
| 6630 | fu_engine_add_runtime_version (self, "org.freedesktop.gusb", g_usb_version_string ()); |
| 6631 | #endif |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6632 | |
Richard Hughes | f679e5e | 2019-08-28 15:31:34 +0100 | [diff] [blame] | 6633 | /* optional kernel version */ |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 6634 | #ifdef HAVE_UTSNAME_H |
Richard Hughes | f679e5e | 2019-08-28 15:31:34 +0100 | [diff] [blame] | 6635 | memset (&uname_tmp, 0, sizeof(uname_tmp)); |
| 6636 | if (uname (&uname_tmp) >= 0) |
| 6637 | fu_engine_add_runtime_version (self, "org.kernel", uname_tmp.release); |
Richard Hughes | fc1e267 | 2019-11-22 08:53:33 +0000 | [diff] [blame] | 6638 | #endif |
Richard Hughes | f679e5e | 2019-08-28 15:31:34 +0100 | [diff] [blame] | 6639 | |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6640 | g_hash_table_insert (self->compile_versions, |
Richard Hughes | 2d37c3f | 2018-08-04 15:18:25 +0100 | [diff] [blame] | 6641 | g_strdup ("com.redhat.fwupdate"), |
| 6642 | g_strdup ("12")); |
| 6643 | g_hash_table_insert (self->compile_versions, |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6644 | g_strdup ("org.freedesktop.fwupd"), |
| 6645 | g_strdup (VERSION)); |
Richard Hughes | 1a3d3b3 | 2021-01-13 18:43:44 +0000 | [diff] [blame] | 6646 | #ifdef HAVE_GUSB |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6647 | g_hash_table_insert (self->compile_versions, |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6648 | g_strdup ("org.freedesktop.gusb"), |
| 6649 | g_strdup_printf ("%i.%i.%i", |
| 6650 | G_USB_MAJOR_VERSION, |
| 6651 | G_USB_MINOR_VERSION, |
| 6652 | G_USB_MICRO_VERSION)); |
Richard Hughes | 1a3d3b3 | 2021-01-13 18:43:44 +0000 | [diff] [blame] | 6653 | #endif |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6654 | } |
| 6655 | |
| 6656 | static void |
| 6657 | fu_engine_finalize (GObject *obj) |
| 6658 | { |
| 6659 | FuEngine *self = FU_ENGINE (obj); |
| 6660 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 6661 | if (self->silo != NULL) |
| 6662 | g_object_unref (self->silo); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6663 | if (self->coldplug_id != 0) |
| 6664 | g_source_remove (self->coldplug_id); |
Richard Hughes | 3444cf6 | 2020-06-22 15:10:28 +0100 | [diff] [blame] | 6665 | if (self->approved_firmware != NULL) |
| 6666 | g_hash_table_unref (self->approved_firmware); |
Richard Hughes | 3120683 | 2020-07-27 15:31:11 +0100 | [diff] [blame] | 6667 | if (self->blocked_firmware != NULL) |
| 6668 | g_hash_table_unref (self->blocked_firmware); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6669 | |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 6670 | g_free (self->host_machine_id); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 6671 | g_free (self->host_security_id); |
Richard Hughes | fb0a938 | 2020-06-23 16:56:28 +0100 | [diff] [blame] | 6672 | g_object_unref (self->host_security_attrs); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 6673 | g_object_unref (self->idle); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6674 | g_object_unref (self->config); |
Richard Hughes | d1808aa | 2019-12-10 15:20:30 +0000 | [diff] [blame] | 6675 | g_object_unref (self->remote_list); |
Richard Hughes | b333e00 | 2021-04-01 10:40:02 +0100 | [diff] [blame] | 6676 | g_object_unref (self->ctx); |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 6677 | g_object_unref (self->history); |
Richard Hughes | 0a7e783 | 2017-11-22 11:01:13 +0000 | [diff] [blame] | 6678 | g_object_unref (self->device_list); |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 6679 | g_object_unref (self->jcat_context); |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 6680 | g_ptr_array_unref (self->plugin_filter); |
Richard Hughes | 117f857 | 2021-02-09 20:02:48 +0000 | [diff] [blame] | 6681 | g_ptr_array_unref (self->backends); |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 6682 | g_hash_table_unref (self->runtime_versions); |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 6683 | g_hash_table_unref (self->compile_versions); |
Mario Limonciello | 3f9a1c1 | 2018-06-06 14:06:40 -0500 | [diff] [blame] | 6684 | g_object_unref (self->plugin_list); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6685 | |
| 6686 | G_OBJECT_CLASS (fu_engine_parent_class)->finalize (obj); |
| 6687 | } |
| 6688 | |
| 6689 | FuEngine * |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 6690 | fu_engine_new (FuAppFlags app_flags) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6691 | { |
| 6692 | FuEngine *self; |
| 6693 | self = g_object_new (FU_TYPE_ENGINE, NULL); |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 6694 | self->app_flags = app_flags; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 6695 | return FU_ENGINE (self); |
| 6696 | } |