Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 2 | * Copyright (C) 2018 Richard Hughes <richard@hughsie.com> |
| 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuInstallTask" |
| 8 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
| 11 | #include <fwupd.h> |
| 12 | |
Richard Hughes | b3f9841 | 2021-03-01 13:17:57 +0000 | [diff] [blame] | 13 | #include "fu-common.h" |
Richard Hughes | 05cbb72 | 2018-10-10 20:20:39 +0100 | [diff] [blame] | 14 | #include "fu-common-version.h" |
Richard Hughes | c02cb83 | 2018-05-20 10:31:04 +0100 | [diff] [blame] | 15 | #include "fu-device-private.h" |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 16 | #include "fu-install-task.h" |
| 17 | #include "fu-keyring-utils.h" |
| 18 | |
| 19 | struct _FuInstallTask |
| 20 | { |
| 21 | GObject parent_instance; |
| 22 | FuDevice *device; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 23 | XbNode *component; |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 24 | FwupdReleaseFlags trust_flags; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 25 | gboolean is_downgrade; |
| 26 | }; |
| 27 | |
| 28 | G_DEFINE_TYPE (FuInstallTask, fu_install_task, G_TYPE_OBJECT) |
| 29 | |
| 30 | /** |
| 31 | * fu_install_task_get_device: |
| 32 | * @self: A #FuInstallTask |
| 33 | * |
| 34 | * Gets the device for this task. |
| 35 | * |
| 36 | * Returns: (transfer none): the device |
| 37 | **/ |
| 38 | FuDevice * |
| 39 | fu_install_task_get_device (FuInstallTask *self) |
| 40 | { |
| 41 | g_return_val_if_fail (FU_IS_INSTALL_TASK (self), NULL); |
| 42 | return self->device; |
| 43 | } |
| 44 | |
| 45 | /** |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 46 | * fu_install_task_get_component: |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 47 | * @self: A #FuInstallTask |
| 48 | * |
| 49 | * Gets the component for this task. |
| 50 | * |
| 51 | * Returns: (transfer none): the component |
| 52 | **/ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 53 | XbNode * |
| 54 | fu_install_task_get_component (FuInstallTask *self) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 55 | { |
| 56 | g_return_val_if_fail (FU_IS_INSTALL_TASK (self), NULL); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 57 | return self->component; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * fu_install_task_get_trust_flags: |
| 62 | * @self: A #FuInstallTask |
| 63 | * |
| 64 | * Gets the trust flags for this task. |
| 65 | * |
| 66 | * NOTE: This is only set after fu_install_task_check_requirements() has been |
| 67 | * called successfully. |
| 68 | * |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 69 | * Returns: the #FwupdReleaseFlags, e.g. #FWUPD_TRUST_FLAG_PAYLOAD |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 70 | **/ |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 71 | FwupdReleaseFlags |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 72 | fu_install_task_get_trust_flags (FuInstallTask *self) |
| 73 | { |
| 74 | g_return_val_if_fail (FU_IS_INSTALL_TASK (self), FALSE); |
| 75 | return self->trust_flags; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * fu_install_task_get_is_downgrade: |
| 80 | * @self: A #FuInstallTask |
| 81 | * |
| 82 | * Gets if this task is to downgrade firmware. |
| 83 | * |
| 84 | * NOTE: This is only set after fu_install_task_check_requirements() has been |
| 85 | * called successfully. |
| 86 | * |
| 87 | * Returns: %TRUE if versions numbers are going backwards |
| 88 | **/ |
| 89 | gboolean |
| 90 | fu_install_task_get_is_downgrade (FuInstallTask *self) |
| 91 | { |
| 92 | g_return_val_if_fail (FU_IS_INSTALL_TASK (self), FALSE); |
| 93 | return self->is_downgrade; |
| 94 | } |
| 95 | |
Richard Hughes | df049ed | 2019-12-03 13:04:13 +0000 | [diff] [blame] | 96 | static gchar * |
| 97 | fu_install_task_verfmts_to_string (GPtrArray *verfmts) |
| 98 | { |
| 99 | GString *str = g_string_new (NULL); |
| 100 | for (guint i = 0; i < verfmts->len; i++) { |
| 101 | XbNode *verfmt = g_ptr_array_index (verfmts, i); |
| 102 | const gchar *tmp = xb_node_get_text (verfmt); |
| 103 | g_string_append_printf (str, "%s;", tmp); |
| 104 | } |
| 105 | if (str->len > 0) |
| 106 | g_string_truncate (str, str->len - 1); |
| 107 | return g_string_free (str, FALSE); |
| 108 | } |
| 109 | |
| 110 | static gboolean |
| 111 | fu_install_task_check_verfmt (FuInstallTask *self, |
| 112 | GPtrArray *verfmts, |
| 113 | FwupdInstallFlags flags, |
| 114 | GError **error) |
| 115 | { |
| 116 | FwupdVersionFormat fmt_dev = fu_device_get_version_format (self->device); |
| 117 | g_autofree gchar *verfmts_str = NULL; |
| 118 | |
| 119 | /* no device format */ |
| 120 | if (fmt_dev == FWUPD_VERSION_FORMAT_UNKNOWN && |
| 121 | (flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
| 122 | verfmts_str = fu_install_task_verfmts_to_string (verfmts); |
| 123 | g_set_error (error, |
| 124 | FWUPD_ERROR, |
| 125 | FWUPD_ERROR_NOT_SUPPORTED, |
| 126 | "release version format '%s' but no device version format", |
| 127 | verfmts_str); |
| 128 | return FALSE; |
| 129 | } |
| 130 | |
| 131 | /* compare all version formats */ |
| 132 | for (guint i = 0; i < verfmts->len; i++) { |
| 133 | XbNode *verfmt = g_ptr_array_index (verfmts, i); |
| 134 | const gchar *tmp = xb_node_get_text (verfmt); |
| 135 | FwupdVersionFormat fmt_rel = fwupd_version_format_from_string (tmp); |
| 136 | if (fmt_dev == fmt_rel) |
| 137 | return TRUE; |
| 138 | } |
| 139 | verfmts_str = fu_install_task_verfmts_to_string (verfmts); |
| 140 | if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
| 141 | g_set_error (error, |
| 142 | FWUPD_ERROR, |
| 143 | FWUPD_ERROR_NOT_SUPPORTED, |
| 144 | "Firmware version formats were different, " |
| 145 | "device was '%s' and release is '%s'", |
| 146 | fwupd_version_format_to_string (fmt_dev), |
| 147 | verfmts_str); |
| 148 | return FALSE; |
| 149 | } |
| 150 | g_warning ("ignoring version format difference %s:%s", |
| 151 | fwupd_version_format_to_string (fmt_dev), |
| 152 | verfmts_str); |
| 153 | return TRUE; |
| 154 | } |
| 155 | |
Richard Hughes | 1eb7c74 | 2020-01-07 10:58:32 +0000 | [diff] [blame] | 156 | static gboolean |
| 157 | fu_install_task_check_requirements_version_check (FuInstallTask *self, GError **error) |
| 158 | { |
| 159 | g_autoptr(GError) error_local = NULL; |
| 160 | g_autoptr(GPtrArray) reqs = NULL; |
| 161 | |
| 162 | reqs = xb_node_query (fu_install_task_get_component (self), |
| 163 | "requires/*", 0, &error_local); |
| 164 | if (reqs == NULL) { |
| 165 | g_set_error_literal (error, |
| 166 | FWUPD_ERROR, |
| 167 | FWUPD_ERROR_NOT_SUPPORTED, |
| 168 | error_local->message); |
| 169 | return FALSE; |
| 170 | } |
| 171 | for (guint i = 0; i < reqs->len; i++) { |
| 172 | XbNode *req = g_ptr_array_index (reqs, i); |
| 173 | if (g_strcmp0 (xb_node_get_element (req), "firmware") == 0 && |
| 174 | xb_node_get_text (req) == NULL) { |
| 175 | return TRUE; |
| 176 | } |
| 177 | } |
| 178 | g_set_error_literal (error, |
| 179 | FWUPD_ERROR, |
| 180 | FWUPD_ERROR_NOT_SUPPORTED, |
| 181 | "no firmware requirement"); |
| 182 | return FALSE; |
| 183 | } |
| 184 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 185 | /** |
| 186 | * fu_install_task_check_requirements: |
| 187 | * @self: A #FuInstallTask |
| 188 | * @flags: A #FwupdInstallFlags, e.g. #FWUPD_INSTALL_FLAG_ALLOW_OLDER |
| 189 | * @error: A #GError, or %NULL |
| 190 | * |
| 191 | * Checks any requirements of this task. This will typically involve checking |
| 192 | * that the device can accept the component (the GUIDs match) and that the |
| 193 | * device can be upgraded with this firmware version. |
| 194 | * |
| 195 | * Returns: %TRUE if the requirements passed |
| 196 | **/ |
| 197 | gboolean |
| 198 | fu_install_task_check_requirements (FuInstallTask *self, |
| 199 | FwupdInstallFlags flags, |
| 200 | GError **error) |
| 201 | { |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 202 | const gchar *branch_new; |
| 203 | const gchar *branch_old; |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 204 | const gchar *protocol; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 205 | const gchar *version; |
Richard Hughes | 1fbcb1a | 2019-12-04 15:05:12 +0000 | [diff] [blame] | 206 | const gchar *version_release_raw; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 207 | const gchar *version_lowest; |
| 208 | gboolean matches_guid = FALSE; |
| 209 | gint vercmp; |
Richard Hughes | 1fbcb1a | 2019-12-04 15:05:12 +0000 | [diff] [blame] | 210 | g_autofree gchar *version_release = NULL; |
Mario Limonciello | 7d35278 | 2018-08-24 11:35:02 -0500 | [diff] [blame] | 211 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 212 | g_autoptr(GPtrArray) provides = NULL; |
Richard Hughes | df049ed | 2019-12-03 13:04:13 +0000 | [diff] [blame] | 213 | g_autoptr(GPtrArray) verfmts = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 214 | g_autoptr(XbNode) release = NULL; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 215 | |
| 216 | g_return_val_if_fail (FU_IS_INSTALL_TASK (self), FALSE); |
| 217 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 218 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 219 | /* does this component provide a GUID the device has */ |
| 220 | provides = xb_node_query (self->component, |
| 221 | "provides/firmware[@type='flashed']", |
| 222 | 0, &error_local); |
| 223 | if (provides == NULL) { |
| 224 | g_set_error (error, |
| 225 | FWUPD_ERROR, |
| 226 | FWUPD_ERROR_NOT_FOUND, |
| 227 | "No supported devices found: %s", |
| 228 | error_local->message); |
| 229 | return FALSE; |
| 230 | } |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 231 | for (guint i = 0; i < provides->len; i++) { |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 232 | XbNode *provide = g_ptr_array_index (provides, i); |
| 233 | if (fu_device_has_guid (self->device, xb_node_get_text (provide))) { |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 234 | matches_guid = TRUE; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | if (!matches_guid) { |
| 239 | g_set_error_literal (error, |
| 240 | FWUPD_ERROR, |
| 241 | FWUPD_ERROR_NOT_FOUND, |
| 242 | "No supported devices found"); |
| 243 | return FALSE; |
| 244 | } |
| 245 | |
Richard Hughes | 21eaeef | 2020-01-14 12:10:01 +0000 | [diff] [blame] | 246 | /* device requires a version check */ |
Richard Hughes | 1eb7c74 | 2020-01-07 10:58:32 +0000 | [diff] [blame] | 247 | if (fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_VERSION_CHECK_REQUIRED)) { |
| 248 | if (!fu_install_task_check_requirements_version_check (self, error)) { |
| 249 | g_prefix_error (error, "device requires firmware with a version check: "); |
| 250 | return FALSE; |
| 251 | } |
| 252 | } |
| 253 | |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 254 | /* does the protocol match */ |
| 255 | protocol = xb_node_query_text (self->component, |
| 256 | "custom/value[@key='LVFS::UpdateProtocol']", |
| 257 | NULL); |
Richard Hughes | b3f9841 | 2021-03-01 13:17:57 +0000 | [diff] [blame] | 258 | if (fu_device_get_protocols (self->device)->len != 0 && protocol != NULL && |
| 259 | !fu_device_has_protocol (self->device, protocol) && |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 260 | (flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
Richard Hughes | b3f9841 | 2021-03-01 13:17:57 +0000 | [diff] [blame] | 261 | g_autofree gchar *str = NULL; |
| 262 | str = fu_common_strjoin_array ("|", fu_device_get_protocols (self->device)); |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 263 | g_set_error (error, |
| 264 | FWUPD_ERROR, |
| 265 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | d7e3f09 | 2020-10-20 18:21:04 +0100 | [diff] [blame] | 266 | "Device %s does not support %s, only %s", |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 267 | fu_device_get_name (self->device), |
Richard Hughes | b3f9841 | 2021-03-01 13:17:57 +0000 | [diff] [blame] | 268 | protocol, str); |
Mario Limonciello | baa9e75 | 2019-12-06 09:21:38 -0600 | [diff] [blame] | 269 | return FALSE; |
| 270 | } |
| 271 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 272 | /* check the device is not locked */ |
| 273 | if (fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_LOCKED)) { |
| 274 | g_set_error (error, |
| 275 | FWUPD_ERROR, |
| 276 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 277 | "Device %s [%s] is locked", |
| 278 | fu_device_get_name (self->device), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 279 | fu_device_get_id (self->device)); |
| 280 | return FALSE; |
| 281 | } |
| 282 | |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 283 | /* check the branch is not switching */ |
| 284 | branch_new = xb_node_query_text (self->component, "branch", NULL); |
| 285 | branch_old = fu_device_get_branch (self->device); |
| 286 | if ((flags & FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH) == 0 && |
| 287 | g_strcmp0 (branch_old, branch_new) != 0) { |
| 288 | g_set_error (error, |
| 289 | FWUPD_ERROR, |
| 290 | FWUPD_ERROR_NOT_SUPPORTED, |
| 291 | "Device %s [%s] would switch firmware branch from %s to %s", |
| 292 | fu_device_get_name (self->device), |
| 293 | fu_device_get_id (self->device), |
| 294 | branch_old != NULL ? branch_old : "default", |
| 295 | branch_new != NULL ? branch_new : "default"); |
| 296 | return FALSE; |
| 297 | } |
| 298 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 299 | /* no update abilities */ |
Richard Hughes | 8500b4f | 2020-04-17 11:47:04 +0100 | [diff] [blame] | 300 | if (!fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_UPDATABLE) && |
| 301 | !fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_UPDATABLE_HIDDEN)) { |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 302 | g_set_error (error, |
| 303 | FWUPD_ERROR, |
| 304 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 305 | "Device %s [%s] does not currently allow updates", |
| 306 | fu_device_get_name (self->device), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 307 | fu_device_get_id (self->device)); |
| 308 | return FALSE; |
| 309 | } |
| 310 | |
| 311 | /* called with online update, test if device is supposed to allow this */ |
| 312 | if ((flags & FWUPD_INSTALL_FLAG_OFFLINE) == 0 && |
Richard Hughes | e628065 | 2019-03-24 08:08:32 +0000 | [diff] [blame] | 313 | (flags & FWUPD_INSTALL_FLAG_FORCE) == 0 && |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 314 | fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_ONLY_OFFLINE)) { |
| 315 | g_set_error (error, |
| 316 | FWUPD_ERROR, |
| 317 | FWUPD_ERROR_NOT_SUPPORTED, |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 318 | "Device %s [%s] only allows offline updates", |
| 319 | fu_device_get_name (self->device), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 320 | fu_device_get_id (self->device)); |
| 321 | return FALSE; |
| 322 | } |
| 323 | |
| 324 | /* get device */ |
| 325 | version = fu_device_get_version (self->device); |
| 326 | if (version == NULL) { |
| 327 | g_set_error (error, |
| 328 | FWUPD_ERROR, |
| 329 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 330 | "Device %s [%s] has no firmware version", |
| 331 | fu_device_get_name (self->device), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 332 | fu_device_get_id (self->device)); |
| 333 | return FALSE; |
| 334 | } |
| 335 | |
| 336 | /* get latest release */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 337 | release = xb_node_query_first (self->component, "releases/release", NULL); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 338 | if (release == NULL) { |
| 339 | g_set_error (error, |
| 340 | FWUPD_ERROR, |
| 341 | FWUPD_ERROR_INVALID_FILE, |
| 342 | "%s [%s] has no firmware update metadata", |
Richard Hughes | 7e77bf3 | 2018-05-08 14:56:46 +0100 | [diff] [blame] | 343 | fu_device_get_name (self->device), |
| 344 | fu_device_get_id (self->device)); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 345 | return FALSE; |
| 346 | } |
| 347 | |
| 348 | /* is this a downgrade or re-install */ |
Richard Hughes | 1fbcb1a | 2019-12-04 15:05:12 +0000 | [diff] [blame] | 349 | version_release_raw = xb_node_get_attr (release, "version"); |
| 350 | if (version_release_raw == NULL) { |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 351 | g_set_error_literal (error, |
| 352 | FWUPD_ERROR, |
| 353 | FWUPD_ERROR_INVALID_FILE, |
| 354 | "Release has no firmware version"); |
| 355 | return FALSE; |
| 356 | } |
| 357 | |
Richard Hughes | 5079f26 | 2019-04-29 09:34:27 +0100 | [diff] [blame] | 358 | /* check the version formats match if set in the release */ |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 359 | if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0 && |
| 360 | (flags & FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH) == 0) { |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 361 | verfmts = xb_node_query (self->component, |
| 362 | "custom/value[@key='LVFS::VersionFormat']", |
| 363 | 0, NULL); |
| 364 | if (verfmts != NULL) { |
| 365 | if (!fu_install_task_check_verfmt (self, verfmts, flags, error)) |
| 366 | return FALSE; |
| 367 | } |
Richard Hughes | ce3cea8 | 2019-04-08 11:31:21 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 370 | /* compare to the lowest supported version, if it exists */ |
| 371 | version_lowest = fu_device_get_version_lowest (self->device); |
Mario Limonciello | da0e90f | 2018-09-04 23:15:56 -0500 | [diff] [blame] | 372 | if (version_lowest != NULL && |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 373 | fu_common_vercmp_full (version_lowest, version, |
| 374 | fu_device_get_version_format (self->device)) > 0 && |
Mario Limonciello | da0e90f | 2018-09-04 23:15:56 -0500 | [diff] [blame] | 375 | (flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 376 | g_set_error (error, |
| 377 | FWUPD_ERROR, |
| 378 | FWUPD_ERROR_VERSION_NEWER, |
| 379 | "Specified firmware is older than the minimum " |
Mario Limonciello | 14c823e | 2018-09-04 23:13:24 -0500 | [diff] [blame] | 380 | "required version '%s < %s'", version, version_lowest); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 381 | return FALSE; |
| 382 | } |
| 383 | |
| 384 | /* check semver */ |
Richard Hughes | 1de7cc9 | 2020-02-13 09:41:57 +0000 | [diff] [blame] | 385 | if (fu_device_get_version_format (self->device) == FWUPD_VERSION_FORMAT_PLAIN) { |
| 386 | version_release = g_strdup (version_release_raw); |
Richard Hughes | 1de7cc9 | 2020-02-13 09:41:57 +0000 | [diff] [blame] | 387 | } else { |
| 388 | version_release = fu_common_version_parse_from_format (version_release_raw, |
| 389 | fu_device_get_version_format (self->device)); |
Richard Hughes | 1de7cc9 | 2020-02-13 09:41:57 +0000 | [diff] [blame] | 390 | } |
Richard Hughes | 9a68084 | 2020-02-20 11:11:13 +0000 | [diff] [blame] | 391 | vercmp = fu_common_vercmp_full (version, version_release, |
| 392 | fu_device_get_version_format (self->device)); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 393 | if (vercmp == 0 && (flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) == 0) { |
| 394 | g_set_error (error, |
| 395 | FWUPD_ERROR, |
| 396 | FWUPD_ERROR_VERSION_SAME, |
| 397 | "Specified firmware is already installed '%s'", |
| 398 | version_release); |
| 399 | return FALSE; |
| 400 | } |
| 401 | self->is_downgrade = vercmp > 0; |
Richard Hughes | 5bbf013 | 2020-10-05 13:44:39 +0100 | [diff] [blame] | 402 | if (self->is_downgrade && |
| 403 | (flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) == 0 && |
| 404 | (flags & FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH) == 0) { |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 405 | g_set_error (error, |
| 406 | FWUPD_ERROR, |
| 407 | FWUPD_ERROR_VERSION_NEWER, |
| 408 | "Specified firmware is older than installed '%s < %s'", |
| 409 | version_release, version); |
| 410 | return FALSE; |
| 411 | } |
| 412 | |
| 413 | /* verify */ |
Richard Hughes | 245885c | 2019-03-04 08:46:02 +0000 | [diff] [blame] | 414 | if (!fu_keyring_get_release_flags (release, &self->trust_flags, &error_local)) { |
Mario Limonciello | 7d35278 | 2018-08-24 11:35:02 -0500 | [diff] [blame] | 415 | if (g_error_matches (error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED)) { |
| 416 | g_warning ("Ignoring verification for %s: %s", |
| 417 | fu_device_get_name (self->device), |
| 418 | error_local->message); |
| 419 | } else { |
| 420 | g_propagate_error (error, g_steal_pointer (&error_local)); |
| 421 | return FALSE; |
| 422 | } |
| 423 | } |
| 424 | return TRUE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * fu_install_task_get_action_id: |
| 429 | * @self: A #FuEngine |
| 430 | * |
| 431 | * Gets the PolicyKit action ID to use for the install operation. |
| 432 | * |
| 433 | * Returns: string, e.g. `org.freedesktop.fwupd.update-internal-trusted` |
| 434 | **/ |
| 435 | const gchar * |
| 436 | fu_install_task_get_action_id (FuInstallTask *self) |
| 437 | { |
| 438 | /* relax authentication checks for removable devices */ |
| 439 | if (!fu_device_has_flag (self->device, FWUPD_DEVICE_FLAG_INTERNAL)) { |
| 440 | if (self->is_downgrade) |
| 441 | return "org.freedesktop.fwupd.downgrade-hotplug"; |
| 442 | if (self->trust_flags & FWUPD_TRUST_FLAG_PAYLOAD) |
| 443 | return "org.freedesktop.fwupd.update-hotplug-trusted"; |
| 444 | return "org.freedesktop.fwupd.update-hotplug"; |
| 445 | } |
| 446 | |
| 447 | /* internal device */ |
| 448 | if (self->is_downgrade) |
| 449 | return "org.freedesktop.fwupd.downgrade-internal"; |
| 450 | if (self->trust_flags & FWUPD_TRUST_FLAG_PAYLOAD) |
| 451 | return "org.freedesktop.fwupd.update-internal-trusted"; |
| 452 | return "org.freedesktop.fwupd.update-internal"; |
| 453 | } |
| 454 | |
| 455 | static void |
| 456 | fu_install_task_init (FuInstallTask *self) |
| 457 | { |
| 458 | self->trust_flags = FWUPD_TRUST_FLAG_NONE; |
| 459 | } |
| 460 | |
| 461 | static void |
| 462 | fu_install_task_finalize (GObject *object) |
| 463 | { |
| 464 | FuInstallTask *self = FU_INSTALL_TASK (object); |
| 465 | |
Richard Hughes | 10fffc7 | 2020-04-16 14:05:02 +0100 | [diff] [blame] | 466 | if (self->component != NULL) |
| 467 | g_object_unref (self->component); |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 468 | if (self->device != NULL) |
| 469 | g_object_unref (self->device); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 470 | |
| 471 | G_OBJECT_CLASS (fu_install_task_parent_class)->finalize (object); |
| 472 | } |
| 473 | |
| 474 | static void |
| 475 | fu_install_task_class_init (FuInstallTaskClass *klass) |
| 476 | { |
| 477 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 478 | object_class->finalize = fu_install_task_finalize; |
| 479 | } |
| 480 | |
| 481 | /** |
Richard Hughes | c02cb83 | 2018-05-20 10:31:04 +0100 | [diff] [blame] | 482 | * fu_install_task_compare: |
| 483 | * @task1: first #FuInstallTask to compare. |
| 484 | * @task2: second #FuInstallTask to compare. |
| 485 | * |
| 486 | * Compares two install tasks. |
| 487 | * |
| 488 | * Returns: 1, 0 or -1 if @task1 is greater, equal, or less than @task2, respectively. |
| 489 | **/ |
| 490 | gint |
| 491 | fu_install_task_compare (FuInstallTask *task1, FuInstallTask *task2) |
| 492 | { |
| 493 | FuDevice *device1 = fu_install_task_get_device (task1); |
| 494 | FuDevice *device2 = fu_install_task_get_device (task2); |
| 495 | if (fu_device_get_order (device1) < fu_device_get_order (device2)) |
| 496 | return -1; |
| 497 | if (fu_device_get_order (device1) > fu_device_get_order (device2)) |
| 498 | return 1; |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /** |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 503 | * fu_install_task_new: |
| 504 | * @device: A #FuDevice |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 505 | * @component: a #XbNode |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 506 | * |
| 507 | * Creates a new install task that may or may not be valid. |
| 508 | * |
| 509 | * Returns: (transfer full): the #FuInstallTask |
| 510 | **/ |
| 511 | FuInstallTask * |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 512 | fu_install_task_new (FuDevice *device, XbNode *component) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 513 | { |
| 514 | FuInstallTask *self; |
| 515 | self = g_object_new (FU_TYPE_TASK, NULL); |
Richard Hughes | 10fffc7 | 2020-04-16 14:05:02 +0100 | [diff] [blame] | 516 | if (component != NULL) |
| 517 | self->component = g_object_ref (component); |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 518 | if (device != NULL) |
| 519 | self->device = g_object_ref (device); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 520 | return FU_INSTALL_TASK (self); |
| 521 | } |