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 | b597683 | 2018-05-18 10:02:09 +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 | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuMain" |
| 8 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
| 11 | #include <fwupd.h> |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 12 | #include <glib/gstdio.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 13 | #include <glib/gi18n.h> |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 14 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 15 | #include <glib-unix.h> |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 16 | #endif |
Richard Hughes | 7d82a09 | 2019-11-22 09:42:31 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 18 | #include <locale.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 21 | #include <jcat.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 22 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 23 | #include "fu-device-private.h" |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 24 | #include "fu-engine.h" |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 25 | #include "fu-history.h" |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 26 | #include "fu-plugin-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 27 | #include "fu-progressbar.h" |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 28 | #include "fu-security-attrs-private.h" |
Mario Limonciello | 6b0e663 | 2019-11-22 13:04:32 -0600 | [diff] [blame] | 29 | #include "fu-smbios-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 30 | #include "fu-util-common.h" |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 31 | #include "fu-debug.h" |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 32 | #include "fwupd-common-private.h" |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 33 | #include "fwupd-device-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 34 | |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 35 | #ifdef HAVE_SYSTEMD |
| 36 | #include "fu-systemd.h" |
| 37 | #endif |
| 38 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 39 | /* custom return code */ |
| 40 | #define EXIT_NOTHING_TO_DO 2 |
| 41 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 42 | typedef enum { |
| 43 | FU_UTIL_OPERATION_UNKNOWN, |
| 44 | FU_UTIL_OPERATION_UPDATE, |
| 45 | FU_UTIL_OPERATION_INSTALL, |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 46 | FU_UTIL_OPERATION_READ, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 47 | FU_UTIL_OPERATION_LAST |
| 48 | } FuUtilOperation; |
| 49 | |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 50 | struct FuUtilPrivate { |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 51 | GCancellable *cancellable; |
Richard Hughes | 6ed25f5 | 2021-01-10 19:27:33 +0000 | [diff] [blame] | 52 | GMainContext *main_ctx; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 53 | GMainLoop *loop; |
| 54 | GOptionContext *context; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 55 | FuEngine *engine; |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 56 | FuEngineRequest *request; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 57 | FuProgressbar *progressbar; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 58 | gboolean no_reboot_check; |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 59 | gboolean no_safety_check; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 60 | gboolean prepare_blob; |
| 61 | gboolean cleanup_blob; |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 62 | gboolean enable_json_state; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 63 | FwupdInstallFlags flags; |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 64 | gboolean show_all; |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 65 | gboolean disable_ssl_strict; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 66 | /* only valid in update and downgrade */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 67 | FuUtilOperation current_operation; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 68 | FwupdDevice *current_device; |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 69 | gchar *current_message; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 70 | FwupdDeviceFlags completion_flags; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 71 | FwupdDeviceFlags filter_include; |
| 72 | FwupdDeviceFlags filter_exclude; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 73 | }; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 74 | |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 75 | static gboolean |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 76 | fu_util_save_current_state (FuUtilPrivate *priv, GError **error) |
| 77 | { |
| 78 | g_autoptr(JsonBuilder) builder = NULL; |
| 79 | g_autoptr(JsonGenerator) json_generator = NULL; |
| 80 | g_autoptr(JsonNode) json_root = NULL; |
| 81 | g_autoptr(GPtrArray) devices = NULL; |
| 82 | g_autofree gchar *state = NULL; |
| 83 | g_autofree gchar *dirname = NULL; |
| 84 | g_autofree gchar *filename = NULL; |
| 85 | |
| 86 | if (!priv->enable_json_state) |
| 87 | return TRUE; |
| 88 | |
| 89 | devices = fu_engine_get_devices (priv->engine, error); |
| 90 | if (devices == NULL) |
| 91 | return FALSE; |
Richard Hughes | 0ef4720 | 2020-01-06 13:59:09 +0000 | [diff] [blame] | 92 | fwupd_device_array_ensure_parents (devices); |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 93 | |
| 94 | /* create header */ |
| 95 | builder = json_builder_new (); |
| 96 | json_builder_begin_object (builder); |
| 97 | |
| 98 | /* add each device */ |
| 99 | json_builder_set_member_name (builder, "Devices"); |
| 100 | json_builder_begin_array (builder); |
| 101 | for (guint i = 0; i < devices->len; i++) { |
| 102 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 103 | json_builder_begin_object (builder); |
| 104 | fwupd_device_to_json (dev, builder); |
| 105 | json_builder_end_object (builder); |
| 106 | } |
| 107 | json_builder_end_array (builder); |
| 108 | json_builder_end_object (builder); |
| 109 | |
| 110 | /* export as a string */ |
| 111 | json_root = json_builder_get_root (builder); |
| 112 | json_generator = json_generator_new (); |
| 113 | json_generator_set_pretty (json_generator, TRUE); |
| 114 | json_generator_set_root (json_generator, json_root); |
| 115 | state = json_generator_to_data (json_generator, NULL); |
| 116 | if (state == NULL) |
| 117 | return FALSE; |
| 118 | dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 119 | filename = g_build_filename (dirname, "state.json", NULL); |
| 120 | return g_file_set_contents (filename, state, -1, error); |
| 121 | } |
| 122 | |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 123 | static void |
| 124 | fu_util_show_plugin_warnings (FuUtilPrivate *priv) |
| 125 | { |
| 126 | FwupdPluginFlags flags = FWUPD_PLUGIN_FLAG_NONE; |
| 127 | GPtrArray *plugins; |
| 128 | |
| 129 | /* get a superset so we do not show the same message more than once */ |
| 130 | plugins = fu_engine_get_plugins (priv->engine); |
| 131 | for (guint i = 0; i < plugins->len; i++) { |
| 132 | FwupdPlugin *plugin = g_ptr_array_index (plugins, i); |
| 133 | if (!fwupd_plugin_has_flag (plugin, FWUPD_PLUGIN_FLAG_USER_WARNING)) |
| 134 | continue; |
| 135 | flags |= fwupd_plugin_get_flags (plugin); |
| 136 | } |
| 137 | |
| 138 | /* never show these, they're way too generic */ |
| 139 | flags &= ~FWUPD_PLUGIN_FLAG_DISABLED; |
| 140 | flags &= ~FWUPD_PLUGIN_FLAG_NO_HARDWARE; |
| 141 | |
| 142 | /* print */ |
| 143 | for (guint i = 0; i < 64; i++) { |
Richard Hughes | 1df9b82 | 2020-10-30 15:18:45 +0000 | [diff] [blame] | 144 | FwupdPluginFlags flag = (guint64) 1 << i; |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 145 | const gchar *tmp; |
| 146 | g_autofree gchar *fmt = NULL; |
Richard Hughes | 1df9b82 | 2020-10-30 15:18:45 +0000 | [diff] [blame] | 147 | g_autofree gchar *url= NULL; |
| 148 | g_autoptr(GString) str = g_string_new (NULL); |
| 149 | if ((flags & flag) == 0) |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 150 | continue; |
| 151 | tmp = fu_util_plugin_flag_to_string ((guint64) 1 << i); |
| 152 | if (tmp == NULL) |
| 153 | continue; |
| 154 | /* TRANSLATORS: this is a prefix on the console */ |
| 155 | fmt = fu_util_term_format (_("WARNING:"), FU_UTIL_TERM_COLOR_RED); |
Richard Hughes | 1df9b82 | 2020-10-30 15:18:45 +0000 | [diff] [blame] | 156 | g_string_append_printf (str, "%s %s\n", fmt, tmp); |
| 157 | |
| 158 | url = g_strdup_printf ("https://github.com/fwupd/fwupd/wiki/PluginFlag:%s", |
| 159 | fwupd_plugin_flag_to_string (flag)); |
| 160 | g_string_append (str, " "); |
| 161 | /* TRANSLATORS: %s is a link to a website */ |
| 162 | g_string_append_printf (str, _("See %s for more information."), url); |
| 163 | g_string_append (str, "\n"); |
| 164 | g_printerr ("%s", str->str); |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 168 | static gboolean |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 169 | fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error) |
Mario Limonciello | e8dd4d7 | 2019-02-26 15:28:04 -0600 | [diff] [blame] | 170 | { |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 171 | #ifdef HAVE_SYSTEMD |
Richard Hughes | 3a0ee30 | 2020-12-14 14:22:18 +0000 | [diff] [blame] | 172 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 173 | if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local)) |
Mario Limonciello | 8692d01 | 2019-10-12 18:01:55 -0500 | [diff] [blame] | 174 | g_debug ("Failed to stop daemon: %s", error_local->message); |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 175 | #endif |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 176 | if (!fu_engine_load (priv->engine, flags, error)) |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 177 | return FALSE; |
| 178 | if (fu_engine_get_tainted (priv->engine)) { |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 179 | g_autofree gchar *fmt = NULL; |
| 180 | |
| 181 | /* TRANSLATORS: this is a prefix on the console */ |
| 182 | fmt = fu_util_term_format (_("WARNING:"), FU_UTIL_TERM_COLOR_RED); |
| 183 | g_printerr ("%s This tool has loaded 3rd party code and " |
| 184 | "is no longer supported by the upstream developers!\n", |
| 185 | fmt); |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 186 | } |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 187 | fu_util_show_plugin_warnings (priv); |
Mario Limonciello | bd60de1 | 2020-11-11 13:09:43 -0600 | [diff] [blame] | 188 | fu_util_show_unsupported_warn (); |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 189 | return TRUE; |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 192 | static void |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 193 | fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error) |
| 194 | { |
| 195 | g_autofree gchar *path = g_path_get_dirname (value); |
| 196 | if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { |
| 197 | g_prefix_error (error, |
| 198 | "Unable to access %s. You may need to copy %s to %s: ", |
| 199 | path, value, g_getenv ("HOME")); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static void |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 204 | fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data) |
| 205 | { |
| 206 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 207 | /* TRANSLATORS: this is when a device ctrl+c's a watch */ |
| 208 | g_print ("%s\n", _("Cancelled")); |
| 209 | g_main_loop_quit (priv->loop); |
| 210 | } |
| 211 | |
| 212 | static gboolean |
| 213 | fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
| 214 | { |
| 215 | g_autofree gchar *tmp = NULL; |
| 216 | g_autoptr(FuSmbios) smbios = NULL; |
| 217 | if (g_strv_length (values) < 1) { |
| 218 | g_set_error_literal (error, |
| 219 | FWUPD_ERROR, |
| 220 | FWUPD_ERROR_INVALID_ARGS, |
| 221 | "Invalid arguments"); |
| 222 | return FALSE; |
| 223 | } |
| 224 | smbios = fu_smbios_new (); |
| 225 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 226 | return FALSE; |
| 227 | tmp = fu_smbios_to_string (smbios); |
| 228 | g_print ("%s\n", tmp); |
| 229 | return TRUE; |
| 230 | } |
| 231 | |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 232 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 233 | static gboolean |
| 234 | fu_util_sigint_cb (gpointer user_data) |
| 235 | { |
| 236 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 237 | g_debug ("Handling SIGINT"); |
| 238 | g_cancellable_cancel (priv->cancellable); |
| 239 | return FALSE; |
| 240 | } |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 241 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 242 | |
| 243 | static void |
| 244 | fu_util_private_free (FuUtilPrivate *priv) |
| 245 | { |
Mario Limonciello | cc50e1a | 2018-08-14 17:45:24 -0500 | [diff] [blame] | 246 | if (priv->current_device != NULL) |
| 247 | g_object_unref (priv->current_device); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 248 | if (priv->engine != NULL) |
| 249 | g_object_unref (priv->engine); |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 250 | if (priv->request != NULL) |
| 251 | g_object_unref (priv->request); |
Richard Hughes | 6ed25f5 | 2021-01-10 19:27:33 +0000 | [diff] [blame] | 252 | if (priv->main_ctx != NULL) |
| 253 | g_main_context_unref (priv->main_ctx); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 254 | if (priv->loop != NULL) |
| 255 | g_main_loop_unref (priv->loop); |
| 256 | if (priv->cancellable != NULL) |
| 257 | g_object_unref (priv->cancellable); |
| 258 | if (priv->progressbar != NULL) |
| 259 | g_object_unref (priv->progressbar); |
| 260 | if (priv->context != NULL) |
| 261 | g_option_context_free (priv->context); |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 262 | g_free (priv->current_message); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 263 | g_free (priv); |
| 264 | } |
| 265 | |
| 266 | #pragma clang diagnostic push |
| 267 | #pragma clang diagnostic ignored "-Wunused-function" |
| 268 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free) |
| 269 | #pragma clang diagnostic pop |
| 270 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 271 | |
| 272 | static void |
| 273 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 274 | FuDevice *device, |
| 275 | FuUtilPrivate *priv) |
| 276 | { |
| 277 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 278 | g_debug ("ADDED:\n%s", tmp); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 283 | FuDevice *device, |
| 284 | FuUtilPrivate *priv) |
| 285 | { |
| 286 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 287 | g_debug ("REMOVED:\n%s", tmp); |
| 288 | } |
| 289 | |
| 290 | static void |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 291 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 292 | FwupdStatus status, |
| 293 | FuUtilPrivate *priv) |
| 294 | { |
| 295 | fu_progressbar_update (priv->progressbar, status, 0); |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 300 | guint percentage, |
| 301 | FuUtilPrivate *priv) |
| 302 | { |
| 303 | fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage); |
| 304 | } |
| 305 | |
| 306 | static gboolean |
| 307 | fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 308 | { |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 309 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_COLDPLUG, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 310 | return FALSE; |
| 311 | g_main_loop_run (priv->loop); |
| 312 | return TRUE; |
| 313 | } |
| 314 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 315 | static gint |
| 316 | fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2) |
| 317 | { |
| 318 | return fu_plugin_name_compare (*item1, *item2); |
| 319 | } |
| 320 | |
| 321 | static gboolean |
| 322 | fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error) |
| 323 | { |
| 324 | GPtrArray *plugins; |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 325 | |
| 326 | /* load engine */ |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 327 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 328 | return FALSE; |
| 329 | |
| 330 | /* print */ |
| 331 | plugins = fu_engine_get_plugins (priv->engine); |
| 332 | g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb); |
| 333 | for (guint i = 0; i < plugins->len; i++) { |
| 334 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 335 | g_autofree gchar *str = fu_util_plugin_to_string (FWUPD_PLUGIN (plugin), 0); |
| 336 | g_print ("%s\n", str); |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 337 | } |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 338 | if (plugins->len == 0) { |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 339 | /* TRANSLATORS: nothing found */ |
| 340 | g_print ("%s\n", _("No plugins found")); |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return TRUE; |
| 344 | } |
| 345 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 346 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 347 | fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev) |
| 348 | { |
| 349 | if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) { |
| 350 | if (!fwupd_device_has_flag (dev, priv->filter_include)) |
| 351 | return FALSE; |
| 352 | } |
| 353 | if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) { |
| 354 | if (fwupd_device_has_flag (dev, priv->filter_exclude)) |
| 355 | return FALSE; |
| 356 | } |
| 357 | return TRUE; |
| 358 | } |
| 359 | |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 360 | static gchar * |
| 361 | fu_util_get_tree_title (FuUtilPrivate *priv) |
| 362 | { |
| 363 | return g_strdup (fu_engine_get_host_product (priv->engine)); |
| 364 | } |
| 365 | |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 366 | static FuDevice * |
| 367 | fu_util_prompt_for_device (FuUtilPrivate *priv, GPtrArray *devices_opt, GError **error) |
| 368 | { |
| 369 | FuDevice *dev; |
| 370 | guint idx; |
| 371 | g_autoptr(GPtrArray) devices = NULL; |
| 372 | g_autoptr(GPtrArray) devices_filtered = NULL; |
| 373 | |
| 374 | /* get devices from daemon */ |
| 375 | if (devices_opt != NULL) { |
| 376 | devices = g_ptr_array_ref (devices_opt); |
| 377 | } else { |
| 378 | devices = fu_engine_get_devices (priv->engine, error); |
| 379 | if (devices == NULL) |
| 380 | return NULL; |
| 381 | } |
| 382 | fwupd_device_array_ensure_parents (devices); |
| 383 | |
| 384 | /* filter results */ |
| 385 | devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 386 | for (guint i = 0; i < devices->len; i++) { |
| 387 | dev = g_ptr_array_index (devices, i); |
| 388 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev))) |
| 389 | continue; |
| 390 | g_ptr_array_add (devices_filtered, g_object_ref (dev)); |
| 391 | } |
| 392 | |
| 393 | /* nothing */ |
| 394 | if (devices_filtered->len == 0) { |
| 395 | g_set_error_literal (error, |
| 396 | FWUPD_ERROR, |
| 397 | FWUPD_ERROR_NOTHING_TO_DO, |
| 398 | "No supported devices"); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | /* exactly one */ |
| 403 | if (devices_filtered->len == 1) { |
| 404 | dev = g_ptr_array_index (devices_filtered, 0); |
| 405 | /* TRANSLATORS: Device has been chosen by the daemon for the user */ |
| 406 | g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev)); |
| 407 | return g_object_ref (dev); |
| 408 | } |
| 409 | |
| 410 | /* TRANSLATORS: get interactive prompt */ |
| 411 | g_print ("%s\n", _("Choose a device:")); |
| 412 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 413 | g_print ("0.\t%s\n", _("Cancel")); |
| 414 | for (guint i = 0; i < devices_filtered->len; i++) { |
| 415 | dev = g_ptr_array_index (devices_filtered, i); |
| 416 | g_print ("%u.\t%s (%s)\n", |
| 417 | i + 1, |
| 418 | fu_device_get_id (dev), |
| 419 | fu_device_get_name (dev)); |
| 420 | } |
| 421 | idx = fu_util_prompt_for_number (devices_filtered->len); |
| 422 | if (idx == 0) { |
| 423 | g_set_error_literal (error, |
| 424 | FWUPD_ERROR, |
| 425 | FWUPD_ERROR_NOTHING_TO_DO, |
| 426 | "Request canceled"); |
| 427 | return NULL; |
| 428 | } |
| 429 | dev = g_ptr_array_index (devices_filtered, idx - 1); |
| 430 | return g_object_ref (dev); |
| 431 | } |
| 432 | |
| 433 | static FuDevice * |
| 434 | fu_util_get_device (FuUtilPrivate *priv, const gchar *id, GError **error) |
| 435 | { |
| 436 | if (fwupd_guid_is_valid (id)) { |
| 437 | g_autoptr(GPtrArray) devices = NULL; |
| 438 | devices = fu_engine_get_devices_by_guid (priv->engine, id, error); |
| 439 | if (devices == NULL) |
| 440 | return NULL; |
| 441 | return fu_util_prompt_for_device (priv, devices, error); |
| 442 | } |
| 443 | |
| 444 | /* did this look like a GUID? */ |
| 445 | for (guint i = 0; id[i] != '\0'; i++) { |
| 446 | if (id[i] == '-') { |
| 447 | g_set_error_literal (error, |
| 448 | FWUPD_ERROR, |
| 449 | FWUPD_ERROR_INVALID_ARGS, |
| 450 | "Invalid arguments"); |
| 451 | return NULL; |
| 452 | } |
| 453 | } |
| 454 | return fu_engine_get_device (priv->engine, id, error); |
| 455 | } |
| 456 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 457 | static gboolean |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 458 | fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error) |
| 459 | { |
| 460 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 461 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 462 | g_autofree gchar *title = NULL; |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 463 | gboolean no_updates_header = FALSE; |
| 464 | gboolean latest_header = FALSE; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 465 | |
| 466 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 467 | if (!fu_util_start_engine (priv, |
| 468 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 469 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 470 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 471 | error)) |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 472 | return FALSE; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 473 | title = fu_util_get_tree_title (priv); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 474 | |
Daniel Campello | 9fbd7fe | 2020-09-28 14:53:39 -0600 | [diff] [blame] | 475 | /* parse arguments */ |
| 476 | if (g_strv_length (values) == 0) { |
| 477 | devices = fu_engine_get_devices (priv->engine, error); |
| 478 | if (devices == NULL) |
| 479 | return FALSE; |
| 480 | } else if (g_strv_length (values) == 1) { |
| 481 | FuDevice *device; |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 482 | device = fu_util_get_device (priv, values[0], error); |
Daniel Campello | 9fbd7fe | 2020-09-28 14:53:39 -0600 | [diff] [blame] | 483 | if (device == NULL) |
| 484 | return FALSE; |
| 485 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 486 | g_ptr_array_add (devices, device); |
| 487 | } else { |
| 488 | g_set_error_literal (error, |
| 489 | FWUPD_ERROR, |
| 490 | FWUPD_ERROR_INVALID_ARGS, |
| 491 | "Invalid arguments"); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 492 | return FALSE; |
Daniel Campello | 9fbd7fe | 2020-09-28 14:53:39 -0600 | [diff] [blame] | 493 | } |
| 494 | |
Richard Hughes | 0ef4720 | 2020-01-06 13:59:09 +0000 | [diff] [blame] | 495 | fwupd_device_array_ensure_parents (devices); |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 496 | g_ptr_array_sort (devices, fu_util_sort_devices_by_flags_cb); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 497 | for (guint i = 0; i < devices->len; i++) { |
| 498 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 499 | g_autoptr(GPtrArray) rels = NULL; |
| 500 | g_autoptr(GError) error_local = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 501 | GNode *child; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 502 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 503 | /* not going to have results, so save a engine round-trip */ |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 504 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE)) |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 505 | continue; |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 506 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) { |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 507 | if (!no_updates_header) { |
| 508 | /* TRANSLATORS: message letting the user know no device upgrade available due to missing on LVFS */ |
| 509 | g_printerr ("%s\n", _("Devices with no available firmware updates: ")); |
| 510 | no_updates_header = TRUE; |
| 511 | } |
| 512 | g_printerr (" • %s\n", fwupd_device_get_name (dev)); |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 513 | continue; |
| 514 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 515 | if (!fu_util_filter_device (priv, dev)) |
| 516 | continue; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 517 | |
| 518 | /* get the releases for this device and filter for validity */ |
| 519 | rels = fu_engine_get_upgrades (priv->engine, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 520 | priv->request, |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 521 | fwupd_device_get_id (dev), |
| 522 | &error_local); |
| 523 | if (rels == NULL) { |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 524 | if (!latest_header) { |
| 525 | /* TRANSLATORS: message letting the user know no device upgrade available */ |
| 526 | g_printerr ("%s\n", _("Devices with the latest available firmware version:")); |
| 527 | latest_header = TRUE; |
| 528 | } |
| 529 | g_printerr (" • %s\n", fwupd_device_get_name (dev)); |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 530 | /* discard the actual reason from user, but leave for debugging */ |
| 531 | g_debug ("%s", error_local->message); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 532 | continue; |
| 533 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 534 | child = g_node_append_data (root, dev); |
| 535 | |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 536 | for (guint j = 0; j < rels->len; j++) { |
| 537 | FwupdRelease *rel = g_ptr_array_index (rels, j); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 538 | g_node_append_data (child, g_object_ref (rel)); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 539 | } |
| 540 | } |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 541 | /* save the device state for other applications to see */ |
| 542 | if (!fu_util_save_current_state (priv, error)) |
| 543 | return FALSE; |
| 544 | |
Mario Limonciello | e8946a7 | 2020-09-30 16:31:03 -0500 | [diff] [blame] | 545 | /* updates */ |
| 546 | if (g_node_n_nodes (root, G_TRAVERSE_ALL) <= 1) { |
| 547 | g_set_error_literal (error, |
| 548 | FWUPD_ERROR, |
| 549 | FWUPD_ERROR_NOTHING_TO_DO, |
| 550 | "No updates available for remaining devices"); |
| 551 | return FALSE; |
| 552 | } |
| 553 | |
| 554 | fu_util_print_tree (root, title); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 555 | return TRUE; |
| 556 | } |
| 557 | |
| 558 | static gboolean |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 559 | fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error) |
| 560 | { |
| 561 | g_autoptr(GPtrArray) array = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 562 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 563 | g_autofree gchar *title = NULL; |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 564 | gint fd; |
| 565 | |
| 566 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 567 | if (!fu_util_start_engine (priv, |
| 568 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 569 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 570 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 571 | error)) |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 572 | return FALSE; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 573 | title = fu_util_get_tree_title (priv); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 574 | |
| 575 | /* check args */ |
| 576 | if (g_strv_length (values) != 1) { |
| 577 | g_set_error_literal (error, |
| 578 | FWUPD_ERROR, |
| 579 | FWUPD_ERROR_INVALID_ARGS, |
| 580 | "Invalid arguments"); |
| 581 | return FALSE; |
| 582 | } |
| 583 | |
Mario Limonciello | dc0608d | 2020-02-25 11:25:44 -0600 | [diff] [blame] | 584 | /* implied, important for get-details on a device not in your system */ |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 585 | priv->show_all = TRUE; |
Mario Limonciello | dc0608d | 2020-02-25 11:25:44 -0600 | [diff] [blame] | 586 | |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 587 | /* open file */ |
| 588 | fd = open (values[0], O_RDONLY); |
| 589 | if (fd < 0) { |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 590 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 591 | g_set_error (error, |
| 592 | FWUPD_ERROR, |
| 593 | FWUPD_ERROR_INVALID_FILE, |
| 594 | "failed to open %s", |
| 595 | values[0]); |
| 596 | return FALSE; |
| 597 | } |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 598 | array = fu_engine_get_details (priv->engine, priv->request, fd, error); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 599 | close (fd); |
| 600 | |
| 601 | if (array == NULL) |
| 602 | return FALSE; |
| 603 | for (guint i = 0; i < array->len; i++) { |
| 604 | FwupdDevice *dev = g_ptr_array_index (array, i); |
Mario Limonciello | 984e29c | 2020-02-25 11:30:28 -0600 | [diff] [blame] | 605 | FwupdRelease *rel; |
| 606 | GNode *child; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 607 | if (!fu_util_filter_device (priv, dev)) |
| 608 | continue; |
Mario Limonciello | 984e29c | 2020-02-25 11:30:28 -0600 | [diff] [blame] | 609 | child = g_node_append_data (root, dev); |
| 610 | rel = fwupd_device_get_release_default (dev); |
| 611 | if (rel != NULL) |
| 612 | g_node_append_data (child, rel); |
| 613 | |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 614 | } |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 615 | fu_util_print_tree (root, title); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 616 | |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 617 | return TRUE; |
| 618 | } |
| 619 | |
| 620 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 621 | fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error) |
| 622 | { |
| 623 | g_autoptr(GString) str = g_string_new (NULL); |
| 624 | |
| 625 | for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) { |
| 626 | const gchar *tmp = fwupd_device_flag_to_string (i); |
| 627 | if (tmp == NULL) |
| 628 | break; |
| 629 | if (i != FWUPD_DEVICE_FLAG_INTERNAL) |
| 630 | g_string_append (str, " "); |
| 631 | g_string_append (str, tmp); |
| 632 | g_string_append (str, " ~"); |
| 633 | g_string_append (str, tmp); |
| 634 | } |
| 635 | g_print ("%s\n", str->str); |
| 636 | |
| 637 | return TRUE; |
| 638 | } |
| 639 | |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 640 | static void |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 641 | fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 642 | { |
| 643 | for (guint i = 0; i < devs->len; i++) { |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 644 | FuDevice *dev_tmp = g_ptr_array_index (devs, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 645 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp))) |
| 646 | continue; |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 647 | if (!priv->show_all && |
Mario Limonciello | d1775bc | 2018-07-17 00:28:52 -0500 | [diff] [blame] | 648 | !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp))) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 649 | continue; |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 650 | if (fu_device_get_parent (dev_tmp) == dev) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 651 | GNode *child = g_node_append_data (root, dev_tmp); |
| 652 | fu_util_build_device_tree (priv, child, devs, dev_tmp); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | static gboolean |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 658 | fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 659 | { |
| 660 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 661 | g_autofree gchar *title = NULL; |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 662 | g_autoptr(GPtrArray) devs = NULL; |
| 663 | |
| 664 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 665 | if (!fu_util_start_engine (priv, |
| 666 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 667 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 668 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 669 | error)) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 670 | return FALSE; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 671 | title = fu_util_get_tree_title (priv); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 672 | |
Mario Limonciello | 7619665 | 2021-01-13 22:53:26 -0600 | [diff] [blame] | 673 | /* get devices and build tree */ |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 674 | devs = fu_engine_get_devices (priv->engine, error); |
| 675 | if (devs == NULL) |
| 676 | return FALSE; |
Mario Limonciello | 7619665 | 2021-01-13 22:53:26 -0600 | [diff] [blame] | 677 | if (devs->len > 0) { |
| 678 | fwupd_device_array_ensure_parents (devs); |
| 679 | fu_util_build_device_tree (priv, root, devs, NULL); |
| 680 | } |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 681 | |
| 682 | /* print */ |
Mario Limonciello | 7619665 | 2021-01-13 22:53:26 -0600 | [diff] [blame] | 683 | if (g_node_n_children (root) == 0) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 684 | /* TRANSLATORS: nothing attached that can be upgraded */ |
| 685 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
| 686 | return TRUE; |
| 687 | } |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 688 | fu_util_print_tree (root, title); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 689 | |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 690 | /* save the device state for other applications to see */ |
| 691 | return fu_util_save_current_state (priv, error); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 692 | } |
| 693 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 694 | static void |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 695 | fu_util_update_device_changed_cb (FwupdClient *client, |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 696 | FwupdDevice *device, |
| 697 | FuUtilPrivate *priv) |
| 698 | { |
| 699 | g_autofree gchar *str = NULL; |
| 700 | |
Richard Hughes | 809abea | 2019-03-23 11:06:18 +0000 | [diff] [blame] | 701 | /* allowed to set whenever the device has changed */ |
| 702 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) |
| 703 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN; |
| 704 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) |
| 705 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT; |
| 706 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 707 | /* same as last time, so ignore */ |
| 708 | if (priv->current_device != NULL && |
| 709 | fwupd_device_compare (priv->current_device, device) == 0) |
| 710 | return; |
| 711 | |
Richard Hughes | ee562b5 | 2020-04-07 14:32:52 +0100 | [diff] [blame] | 712 | /* ignore indirect devices that might have changed */ |
| 713 | if (fwupd_device_get_status (device) == FWUPD_STATUS_IDLE || |
| 714 | fwupd_device_get_status (device) == FWUPD_STATUS_UNKNOWN) { |
| 715 | g_debug ("ignoring %s with status %s", |
| 716 | fwupd_device_get_name (device), |
| 717 | fwupd_status_to_string (fwupd_device_get_status (device))); |
| 718 | return; |
| 719 | } |
| 720 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 721 | /* show message in progressbar */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 722 | if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) { |
| 723 | /* TRANSLATORS: %1 is a device name */ |
| 724 | str = g_strdup_printf (_("Updating %s…"), |
| 725 | fwupd_device_get_name (device)); |
| 726 | fu_progressbar_set_title (priv->progressbar, str); |
| 727 | } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) { |
| 728 | /* TRANSLATORS: %1 is a device name */ |
| 729 | str = g_strdup_printf (_("Installing on %s…"), |
| 730 | fwupd_device_get_name (device)); |
| 731 | fu_progressbar_set_title (priv->progressbar, str); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 732 | } else if (priv->current_operation == FU_UTIL_OPERATION_READ) { |
| 733 | /* TRANSLATORS: %1 is a device name */ |
| 734 | str = g_strdup_printf (_("Reading from %s…"), |
| 735 | fwupd_device_get_name (device)); |
| 736 | fu_progressbar_set_title (priv->progressbar, str); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 737 | } else { |
| 738 | g_warning ("no FuUtilOperation set"); |
| 739 | } |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 740 | g_set_object (&priv->current_device, device); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 741 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 742 | if (priv->current_message == NULL) { |
| 743 | const gchar *tmp = fwupd_device_get_update_message (priv->current_device); |
| 744 | if (tmp != NULL) |
| 745 | priv->current_message = g_strdup (tmp); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | static void |
| 750 | fu_util_display_current_message (FuUtilPrivate *priv) |
| 751 | { |
| 752 | if (priv->current_message == NULL) |
| 753 | return; |
| 754 | g_print ("%s\n", priv->current_message); |
| 755 | g_clear_pointer (&priv->current_message, g_free); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 756 | } |
| 757 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 758 | static gboolean |
| 759 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 760 | { |
| 761 | g_autoptr(FuDevice) device = NULL; |
| 762 | g_autoptr(GBytes) blob_fw = NULL; |
| 763 | |
| 764 | /* invalid args */ |
| 765 | if (g_strv_length (values) == 0) { |
| 766 | g_set_error_literal (error, |
| 767 | FWUPD_ERROR, |
| 768 | FWUPD_ERROR_INVALID_ARGS, |
| 769 | "Invalid arguments"); |
| 770 | return FALSE; |
| 771 | } |
| 772 | |
| 773 | /* parse blob */ |
| 774 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 775 | if (blob_fw == NULL) { |
| 776 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 777 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 778 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 779 | |
| 780 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 781 | if (!fu_util_start_engine (priv, |
| 782 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 783 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 784 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 785 | error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 786 | return FALSE; |
| 787 | |
| 788 | /* get device */ |
| 789 | if (g_strv_length (values) >= 2) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 790 | device = fu_util_get_device (priv, values[1], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 791 | if (device == NULL) |
| 792 | return FALSE; |
| 793 | } else { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 794 | device = fu_util_prompt_for_device (priv, NULL, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 795 | if (device == NULL) |
| 796 | return FALSE; |
| 797 | } |
| 798 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 799 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 800 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 801 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 802 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 803 | /* write bare firmware */ |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 804 | if (priv->prepare_blob) { |
| 805 | g_autoptr(GPtrArray) devices = NULL; |
| 806 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 807 | g_ptr_array_add (devices, g_object_ref (device)); |
| 808 | if (!fu_engine_composite_prepare (priv->engine, devices, error)) { |
| 809 | g_prefix_error (error, "failed to prepare composite action: "); |
| 810 | return FALSE; |
| 811 | } |
| 812 | } |
Richard Hughes | f1fa73e | 2020-04-06 16:19:04 +0100 | [diff] [blame] | 813 | priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 814 | if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error)) |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 815 | return FALSE; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 816 | if (priv->cleanup_blob) { |
| 817 | g_autoptr(FuDevice) device_new = NULL; |
| 818 | g_autoptr(GError) error_local = NULL; |
| 819 | |
| 820 | /* get the possibly new device from the old ID */ |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 821 | device_new = fu_util_get_device (priv, |
| 822 | fu_device_get_id (device), |
| 823 | &error_local); |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 824 | if (device_new == NULL) { |
| 825 | g_debug ("failed to find new device: %s", |
| 826 | error_local->message); |
| 827 | } else { |
Mario Limonciello | bb3fa5e | 2019-02-07 21:13:02 -0600 | [diff] [blame] | 828 | g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 829 | g_ptr_array_add (devices_new, g_steal_pointer (&device_new)); |
| 830 | if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) { |
| 831 | g_prefix_error (error, "failed to cleanup composite action: "); |
| 832 | return FALSE; |
| 833 | } |
| 834 | } |
| 835 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 836 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 837 | fu_util_display_current_message (priv); |
| 838 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 839 | /* success */ |
| 840 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 843 | static gboolean |
Richard Hughes | fbd8b5d | 2020-09-24 11:48:59 +0100 | [diff] [blame] | 844 | fu_util_firmware_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 845 | { |
| 846 | g_autoptr(FuDevice) device = NULL; |
| 847 | g_autoptr(GBytes) blob_empty = g_bytes_new (NULL, 0); |
| 848 | g_autoptr(GBytes) blob_fw = NULL; |
| 849 | |
| 850 | /* invalid args */ |
| 851 | if (g_strv_length (values) == 0) { |
| 852 | g_set_error_literal (error, |
| 853 | FWUPD_ERROR, |
| 854 | FWUPD_ERROR_INVALID_ARGS, |
| 855 | "Invalid arguments"); |
| 856 | return FALSE; |
| 857 | } |
| 858 | |
| 859 | /* file already exists */ |
| 860 | if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0 && |
| 861 | g_file_test (values[0], G_FILE_TEST_EXISTS)) { |
| 862 | g_set_error_literal (error, |
| 863 | FWUPD_ERROR, |
| 864 | FWUPD_ERROR_INVALID_ARGS, |
| 865 | "Filename already exists"); |
| 866 | return FALSE; |
| 867 | } |
| 868 | |
Richard Hughes | 02792c0 | 2019-11-01 14:21:20 +0000 | [diff] [blame] | 869 | /* write a zero length file to ensure the destination is writable to |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 870 | * avoid failing at the end of a potentially lengthy operation */ |
| 871 | if (!fu_common_set_contents_bytes (values[0], blob_empty, error)) |
| 872 | return FALSE; |
| 873 | |
| 874 | /* load engine */ |
Richard Hughes | da7b898 | 2021-01-26 21:37:11 +0000 | [diff] [blame^] | 875 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_COLDPLUG, error)) |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 876 | return FALSE; |
| 877 | |
| 878 | /* get device */ |
| 879 | if (g_strv_length (values) >= 2) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 880 | device = fu_util_get_device (priv, values[1], error); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 881 | if (device == NULL) |
| 882 | return FALSE; |
| 883 | } else { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 884 | device = fu_util_prompt_for_device (priv, NULL, error); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 885 | if (device == NULL) |
| 886 | return FALSE; |
| 887 | } |
| 888 | priv->current_operation = FU_UTIL_OPERATION_READ; |
| 889 | g_signal_connect (priv->engine, "device-changed", |
| 890 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 891 | |
| 892 | /* dump firmware */ |
Richard Hughes | fbd8b5d | 2020-09-24 11:48:59 +0100 | [diff] [blame] | 893 | blob_fw = fu_engine_firmware_dump (priv->engine, device, priv->flags, error); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 894 | if (blob_fw == NULL) |
| 895 | return FALSE; |
| 896 | return fu_common_set_contents_bytes (values[0], blob_fw, error); |
| 897 | } |
| 898 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 899 | static gint |
| 900 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 901 | { |
| 902 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 903 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 904 | return fu_install_task_compare (task1, task2); |
| 905 | } |
| 906 | |
| 907 | static gboolean |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 908 | fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error) |
| 909 | { |
Filipe Laíns | e091427 | 2019-09-20 10:04:43 +0100 | [diff] [blame] | 910 | const gchar *argv[][5] = { { "wget", uri, "-O", fn, NULL }, |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 911 | { "curl", uri, "--output", fn, NULL }, |
| 912 | { NULL } }; |
| 913 | for (guint i = 0; argv[i][0] != NULL; i++) { |
| 914 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | db344d5 | 2020-09-09 19:42:27 +0100 | [diff] [blame] | 915 | g_autofree gchar *fn_tmp = NULL; |
| 916 | fn_tmp = fu_common_find_program_in_path (argv[i][0], &error_local); |
| 917 | if (fn_tmp == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 918 | g_debug ("%s", error_local->message); |
| 919 | continue; |
| 920 | } |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 921 | return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error); |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 922 | } |
| 923 | g_set_error_literal (error, |
| 924 | FWUPD_ERROR, |
| 925 | FWUPD_ERROR_NOT_FOUND, |
| 926 | "no supported out-of-process downloaders found"); |
| 927 | return FALSE; |
| 928 | } |
| 929 | |
| 930 | static gchar * |
| 931 | fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error) |
| 932 | { |
| 933 | g_autofree gchar *filename = NULL; |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 934 | |
| 935 | /* a local file */ |
Richard Hughes | 45a0073 | 2019-11-22 16:57:14 +0000 | [diff] [blame] | 936 | if (g_file_test (perhapsfn, G_FILE_TEST_EXISTS)) |
| 937 | return g_strdup (perhapsfn); |
Richard Hughes | 3a73c34 | 2020-11-13 13:25:22 +0000 | [diff] [blame] | 938 | if (!fu_util_is_url (perhapsfn)) |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 939 | return g_strdup (perhapsfn); |
| 940 | |
| 941 | /* download the firmware to a cachedir */ |
| 942 | filename = fu_util_get_user_cache_path (perhapsfn); |
| 943 | if (!fu_common_mkdir_parent (filename, error)) |
| 944 | return NULL; |
| 945 | if (!fu_util_download_out_of_process (perhapsfn, filename, error)) |
| 946 | return NULL; |
| 947 | return g_steal_pointer (&filename); |
| 948 | } |
| 949 | |
| 950 | static gboolean |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 951 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 952 | { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 953 | g_autofree gchar *filename = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 954 | g_autoptr(GBytes) blob_cab = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 955 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 956 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 957 | g_autoptr(GPtrArray) errors = NULL; |
| 958 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 959 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 960 | |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 961 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 962 | if (!fu_util_start_engine (priv, |
| 963 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 964 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 965 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 966 | error)) |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 967 | return FALSE; |
| 968 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 969 | /* handle both forms */ |
| 970 | if (g_strv_length (values) == 1) { |
| 971 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 972 | if (devices_possible == NULL) |
| 973 | return FALSE; |
Richard Hughes | 0ef4720 | 2020-01-06 13:59:09 +0000 | [diff] [blame] | 974 | fwupd_device_array_ensure_parents (devices_possible); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 975 | } else if (g_strv_length (values) == 2) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 976 | FuDevice *device = fu_util_get_device (priv, values[1], error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 977 | if (device == NULL) |
| 978 | return FALSE; |
| 979 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 980 | g_ptr_array_add (devices_possible, device); |
| 981 | } else { |
| 982 | g_set_error_literal (error, |
| 983 | FWUPD_ERROR, |
| 984 | FWUPD_ERROR_INVALID_ARGS, |
| 985 | "Invalid arguments"); |
| 986 | return FALSE; |
| 987 | } |
| 988 | |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 989 | /* download if required */ |
| 990 | filename = fu_util_download_if_required (priv, values[0], error); |
| 991 | if (filename == NULL) |
| 992 | return FALSE; |
| 993 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 994 | /* parse silo */ |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 995 | blob_cab = fu_common_get_contents_bytes (filename, error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 996 | if (blob_cab == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 997 | fu_util_maybe_prefix_sandbox_error (filename, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 998 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 999 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1000 | silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error); |
| 1001 | if (silo == NULL) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1002 | return FALSE; |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 1003 | components = xb_silo_query (silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1004 | if (components == NULL) |
| 1005 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1006 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1007 | /* for each component in the silo */ |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1008 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 1009 | install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1010 | for (guint i = 0; i < components->len; i++) { |
| 1011 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1012 | |
| 1013 | /* do any devices pass the requirements */ |
| 1014 | for (guint j = 0; j < devices_possible->len; j++) { |
| 1015 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 1016 | g_autoptr(FuInstallTask) task = NULL; |
| 1017 | g_autoptr(GError) error_local = NULL; |
| 1018 | |
| 1019 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1020 | task = fu_install_task_new (device, component); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1021 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1022 | priv->request, |
| 1023 | task, |
| 1024 | priv->flags | FWUPD_INSTALL_FLAG_FORCE, |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 1025 | &error_local)) { |
| 1026 | g_debug ("first pass requirement on %s:%s failed: %s", |
| 1027 | fu_device_get_id (device), |
| 1028 | xb_node_query_text (component, "id", NULL), |
| 1029 | error_local->message); |
| 1030 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
| 1034 | /* make a second pass using possibly updated version format now */ |
| 1035 | fu_engine_md_refresh_device_from_component (priv->engine, device, component); |
| 1036 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1037 | priv->request, |
| 1038 | task, |
| 1039 | priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1040 | &error_local)) { |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 1041 | g_debug ("second pass requirement on %s:%s failed: %s", |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1042 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 1043 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1044 | error_local->message); |
| 1045 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 1049 | /* if component should have an update message from CAB */ |
| 1050 | fu_device_incorporate_from_component (device, component); |
| 1051 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1052 | /* success */ |
| 1053 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /* order the install tasks by the device priority */ |
| 1058 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 1059 | |
| 1060 | /* nothing suitable */ |
| 1061 | if (install_tasks->len == 0) { |
| 1062 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 1063 | g_propagate_error (error, error_tmp); |
| 1064 | return FALSE; |
| 1065 | } |
| 1066 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1067 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 1068 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1069 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 1070 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1071 | /* install all the tasks */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1072 | if (!fu_engine_install_tasks (priv->engine, |
| 1073 | priv->request, |
| 1074 | install_tasks, |
| 1075 | blob_cab, |
| 1076 | priv->flags, |
| 1077 | error)) |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 1078 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1079 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 1080 | fu_util_display_current_message (priv); |
| 1081 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1082 | /* we don't want to ask anything */ |
| 1083 | if (priv->no_reboot_check) { |
| 1084 | g_debug ("skipping reboot check"); |
| 1085 | return TRUE; |
| 1086 | } |
| 1087 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1088 | /* save the device state for other applications to see */ |
| 1089 | if (!fu_util_save_current_state (priv, error)) |
| 1090 | return FALSE; |
| 1091 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1092 | /* success */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1093 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1096 | static gboolean |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1097 | fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1098 | { |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1099 | FwupdRemote *remote; |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 1100 | GPtrArray *locations; |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1101 | const gchar *remote_id; |
| 1102 | const gchar *uri_tmp; |
| 1103 | g_auto(GStrv) argv = NULL; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1104 | |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 1105 | /* get the default release only until other parts of fwupd can cope */ |
| 1106 | locations = fwupd_release_get_locations (rel); |
| 1107 | if (locations->len == 0) { |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1108 | g_set_error_literal (error, |
| 1109 | FWUPD_ERROR, |
| 1110 | FWUPD_ERROR_INVALID_FILE, |
| 1111 | "release missing URI"); |
| 1112 | return FALSE; |
| 1113 | } |
Richard Hughes | b8dfacc | 2021-01-25 20:20:59 +0000 | [diff] [blame] | 1114 | uri_tmp = g_ptr_array_index (locations, 0); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1115 | remote_id = fwupd_release_get_remote_id (rel); |
| 1116 | if (remote_id == NULL) { |
| 1117 | g_set_error (error, |
| 1118 | FWUPD_ERROR, |
| 1119 | FWUPD_ERROR_INVALID_FILE, |
| 1120 | "failed to find remote for %s", |
| 1121 | uri_tmp); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1122 | return FALSE; |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | remote = fu_engine_get_remote_by_id (priv->engine, |
| 1126 | remote_id, |
| 1127 | error); |
| 1128 | if (remote == NULL) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1129 | return FALSE; |
| 1130 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1131 | argv = g_new0 (gchar *, 2); |
Daniel Campello | 722f532 | 2020-08-12 11:27:38 -0600 | [diff] [blame] | 1132 | /* local remotes may have the firmware already */ |
Richard Hughes | 3a73c34 | 2020-11-13 13:25:22 +0000 | [diff] [blame] | 1133 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL && |
| 1134 | !fu_util_is_url (uri_tmp)) { |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1135 | const gchar *fn_cache = fwupd_remote_get_filename_cache (remote); |
| 1136 | g_autofree gchar *path = g_path_get_dirname (fn_cache); |
| 1137 | argv[0] = g_build_filename (path, uri_tmp, NULL); |
| 1138 | } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 1139 | argv[0] = g_strdup (uri_tmp + 7); |
| 1140 | /* web remote, fu_util_install will download file */ |
| 1141 | } else { |
| 1142 | argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error); |
| 1143 | } |
| 1144 | return fu_util_install (priv, argv, error); |
| 1145 | } |
| 1146 | |
| 1147 | static gboolean |
| 1148 | fu_util_update_all (FuUtilPrivate *priv, GError **error) |
| 1149 | { |
| 1150 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 1151 | gboolean no_updates_header = FALSE; |
| 1152 | gboolean latest_header = FALSE; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1153 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1154 | devices = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 387bda4 | 2019-02-07 14:20:22 +0000 | [diff] [blame] | 1155 | if (devices == NULL) |
| 1156 | return FALSE; |
Richard Hughes | 0ef4720 | 2020-01-06 13:59:09 +0000 | [diff] [blame] | 1157 | fwupd_device_array_ensure_parents (devices); |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 1158 | g_ptr_array_sort (devices, fu_util_sort_devices_by_flags_cb); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1159 | for (guint i = 0; i < devices->len; i++) { |
| 1160 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 1161 | FwupdRelease *rel; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1162 | const gchar *device_id; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1163 | g_autoptr(GPtrArray) rels = NULL; |
| 1164 | g_autoptr(GError) error_local = NULL; |
| 1165 | |
| 1166 | if (!fu_util_is_interesting_device (dev)) |
| 1167 | continue; |
| 1168 | /* only show stuff that has metadata available */ |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 1169 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE)) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1170 | continue; |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 1171 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) { |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 1172 | if (!no_updates_header) { |
| 1173 | /* TRANSLATORS: message letting the user know no device upgrade available due to missing on LVFS */ |
| 1174 | g_printerr ("%s\n", _("Devices with no available firmware updates: ")); |
| 1175 | no_updates_header = TRUE; |
| 1176 | } |
| 1177 | g_printerr (" • %s\n", fwupd_device_get_name (dev)); |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 1178 | continue; |
| 1179 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1180 | if (!fu_util_filter_device (priv, dev)) |
| 1181 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1182 | |
| 1183 | device_id = fu_device_get_id (dev); |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1184 | rels = fu_engine_get_upgrades (priv->engine, |
| 1185 | priv->request, |
| 1186 | device_id, |
| 1187 | &error_local); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1188 | if (rels == NULL) { |
Mario Limonciello | eb7be16 | 2020-07-01 15:38:47 -0500 | [diff] [blame] | 1189 | if (!latest_header) { |
| 1190 | /* TRANSLATORS: message letting the user know no device upgrade available */ |
| 1191 | g_printerr ("%s\n", _("Devices with the latest available firmware version:")); |
| 1192 | latest_header = TRUE; |
| 1193 | } |
| 1194 | g_printerr (" • %s\n", fwupd_device_get_name (dev)); |
Mario Limonciello | 27164b7 | 2020-02-17 23:19:36 -0600 | [diff] [blame] | 1195 | /* discard the actual reason from user, but leave for debugging */ |
| 1196 | g_debug ("%s", error_local->message); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1197 | continue; |
| 1198 | } |
| 1199 | |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 1200 | if (!priv->no_safety_check) { |
| 1201 | if (!fu_util_prompt_warning (dev, |
| 1202 | fu_util_get_tree_title (priv), |
| 1203 | error)) |
| 1204 | return FALSE; |
| 1205 | } |
| 1206 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1207 | rel = g_ptr_array_index (rels, 0); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1208 | if (!fu_util_install_release (priv, rel, &error_local)) { |
| 1209 | g_printerr ("%s\n", error_local->message); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1210 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1211 | } |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1212 | fu_util_display_current_message (priv); |
| 1213 | } |
| 1214 | return TRUE; |
| 1215 | } |
| 1216 | |
| 1217 | static gboolean |
Mario Limonciello | 9917bb4 | 2020-04-20 13:41:27 -0500 | [diff] [blame] | 1218 | fu_util_update_by_id (FuUtilPrivate *priv, const gchar *id, GError **error) |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1219 | { |
| 1220 | FwupdRelease *rel; |
| 1221 | g_autoptr(FuDevice) dev = NULL; |
| 1222 | g_autoptr(GPtrArray) rels = NULL; |
| 1223 | |
Mario Limonciello | 9917bb4 | 2020-04-20 13:41:27 -0500 | [diff] [blame] | 1224 | /* do not allow a partial device-id, lookup GUIDs */ |
| 1225 | dev = fu_util_get_device (priv, id, error); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1226 | if (dev == NULL) |
| 1227 | return FALSE; |
| 1228 | |
| 1229 | /* get the releases for this device and filter for validity */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1230 | rels = fu_engine_get_upgrades (priv->engine, |
| 1231 | priv->request, |
| 1232 | fu_device_get_id (dev), |
| 1233 | error); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1234 | if (rels == NULL) |
| 1235 | return FALSE; |
| 1236 | rel = g_ptr_array_index (rels, 0); |
| 1237 | if (!fu_util_install_release (priv, rel, error)) |
| 1238 | return FALSE; |
| 1239 | fu_util_display_current_message (priv); |
| 1240 | |
| 1241 | return TRUE; |
| 1242 | } |
| 1243 | |
| 1244 | static gboolean |
| 1245 | fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1246 | { |
Mario Limonciello | 0f109b0 | 2019-11-14 10:26:44 -0600 | [diff] [blame] | 1247 | if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) { |
| 1248 | g_set_error_literal (error, |
| 1249 | FWUPD_ERROR, |
| 1250 | FWUPD_ERROR_INVALID_ARGS, |
| 1251 | "--allow-older is not supported for this command"); |
| 1252 | return FALSE; |
| 1253 | } |
| 1254 | |
| 1255 | if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) { |
| 1256 | g_set_error_literal (error, |
| 1257 | FWUPD_ERROR, |
| 1258 | FWUPD_ERROR_INVALID_ARGS, |
| 1259 | "--allow-reinstall is not supported for this command"); |
| 1260 | return FALSE; |
| 1261 | } |
| 1262 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1263 | if (g_strv_length (values) > 1) { |
| 1264 | g_set_error_literal (error, |
| 1265 | FWUPD_ERROR, |
| 1266 | FWUPD_ERROR_INVALID_ARGS, |
| 1267 | "Invalid arguments"); |
| 1268 | return FALSE; |
| 1269 | } |
| 1270 | |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1271 | if (!fu_util_start_engine (priv, |
| 1272 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1273 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1274 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1275 | error)) |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1276 | return FALSE; |
| 1277 | |
| 1278 | priv->current_operation = FU_UTIL_OPERATION_UPDATE; |
| 1279 | g_signal_connect (priv->engine, "device-changed", |
| 1280 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 1281 | |
| 1282 | if (g_strv_length (values) == 1) { |
| 1283 | if (!fu_util_update_by_id (priv, values[0], error)) |
| 1284 | return FALSE; |
| 1285 | } else { |
| 1286 | if (!fu_util_update_all (priv, error)) |
| 1287 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1288 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1289 | |
| 1290 | /* we don't want to ask anything */ |
| 1291 | if (priv->no_reboot_check) { |
| 1292 | g_debug ("skipping reboot check"); |
| 1293 | return TRUE; |
| 1294 | } |
| 1295 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1296 | /* save the device state for other applications to see */ |
| 1297 | if (!fu_util_save_current_state (priv, error)) |
| 1298 | return FALSE; |
| 1299 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1300 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static gboolean |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 1304 | fu_util_reinstall (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1305 | { |
| 1306 | g_autoptr(FwupdRelease) rel = NULL; |
| 1307 | g_autoptr(GPtrArray) rels = NULL; |
| 1308 | g_autoptr(FuDevice) dev = NULL; |
| 1309 | |
| 1310 | if (g_strv_length (values) != 1) { |
| 1311 | g_set_error_literal (error, |
| 1312 | FWUPD_ERROR, |
| 1313 | FWUPD_ERROR_INVALID_ARGS, |
| 1314 | "Invalid arguments"); |
| 1315 | return FALSE; |
| 1316 | } |
| 1317 | |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1318 | if (!fu_util_start_engine (priv, |
| 1319 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1320 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1321 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1322 | error)) |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 1323 | return FALSE; |
| 1324 | |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 1325 | dev = fu_util_get_device (priv, values[0], error); |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 1326 | if (dev == NULL) |
| 1327 | return FALSE; |
| 1328 | |
| 1329 | /* try to lookup/match release from client */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 1330 | rels = fu_engine_get_releases_for_device (priv->engine, |
| 1331 | priv->request, |
| 1332 | dev, |
| 1333 | error); |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 1334 | if (rels == NULL) |
| 1335 | return FALSE; |
| 1336 | |
| 1337 | for (guint j = 0; j < rels->len; j++) { |
| 1338 | FwupdRelease *rel_tmp = g_ptr_array_index (rels, j); |
| 1339 | if (fu_common_vercmp_full (fwupd_release_get_version (rel_tmp), |
| 1340 | fu_device_get_version (dev), |
| 1341 | fu_device_get_version_format (dev)) == 0) { |
| 1342 | rel = g_object_ref (rel_tmp); |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | if (rel == NULL) { |
| 1347 | g_set_error (error, |
| 1348 | FWUPD_ERROR, |
| 1349 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1350 | "Unable to locate release for %s version %s", |
| 1351 | fu_device_get_name (dev), |
| 1352 | fu_device_get_version (dev)); |
| 1353 | return FALSE; |
| 1354 | } |
| 1355 | |
| 1356 | /* update the console if composite devices are also updated */ |
| 1357 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
| 1358 | g_signal_connect (priv->engine, "device-changed", |
| 1359 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 1360 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 1361 | if (!fu_util_install_release (priv, rel, error)) |
| 1362 | return FALSE; |
| 1363 | fu_util_display_current_message (priv); |
| 1364 | |
| 1365 | /* we don't want to ask anything */ |
| 1366 | if (priv->no_reboot_check) { |
| 1367 | g_debug ("skipping reboot check"); |
| 1368 | return TRUE; |
| 1369 | } |
| 1370 | |
| 1371 | /* save the device state for other applications to see */ |
| 1372 | if (!fu_util_save_current_state (priv, error)) |
| 1373 | return FALSE; |
| 1374 | |
| 1375 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
| 1376 | } |
| 1377 | |
| 1378 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1379 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1380 | { |
| 1381 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1382 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1383 | |
| 1384 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1385 | if (!fu_util_start_engine (priv, |
| 1386 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1387 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1388 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1389 | error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1390 | return FALSE; |
| 1391 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1392 | /* get device */ |
| 1393 | if (g_strv_length (values) >= 1) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 1394 | device = fu_util_get_device (priv, values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1395 | if (device == NULL) |
| 1396 | return FALSE; |
| 1397 | } else { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 1398 | device = fu_util_prompt_for_device (priv, NULL, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1399 | if (device == NULL) |
| 1400 | return FALSE; |
| 1401 | } |
| 1402 | |
| 1403 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1404 | locker = fu_device_locker_new (device, error); |
| 1405 | if (locker == NULL) |
| 1406 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1407 | return fu_device_detach (device, error); |
| 1408 | } |
| 1409 | |
| 1410 | static gboolean |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 1411 | fu_util_unbind_driver (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1412 | { |
| 1413 | g_autoptr(FuDevice) device = NULL; |
| 1414 | g_autoptr(FuDeviceLocker) locker = NULL; |
| 1415 | |
| 1416 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1417 | if (!fu_util_start_engine (priv, |
| 1418 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1419 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1420 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1421 | error)) |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 1422 | return FALSE; |
| 1423 | |
| 1424 | /* get device */ |
| 1425 | if (g_strv_length (values) == 1) { |
| 1426 | device = fu_util_get_device (priv, values[0], error); |
| 1427 | } else { |
| 1428 | device = fu_util_prompt_for_device (priv, NULL, error); |
| 1429 | } |
| 1430 | if (device == NULL) |
| 1431 | return FALSE; |
| 1432 | |
| 1433 | /* run vfunc */ |
| 1434 | locker = fu_device_locker_new (device, error); |
| 1435 | if (locker == NULL) |
| 1436 | return FALSE; |
| 1437 | return fu_device_unbind_driver (device, error); |
| 1438 | } |
| 1439 | |
| 1440 | static gboolean |
| 1441 | fu_util_bind_driver (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1442 | { |
| 1443 | g_autoptr(FuDevice) device = NULL; |
| 1444 | g_autoptr(FuDeviceLocker) locker = NULL; |
| 1445 | |
| 1446 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1447 | if (!fu_util_start_engine (priv, |
| 1448 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1449 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1450 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1451 | error)) |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 1452 | return FALSE; |
| 1453 | |
| 1454 | /* get device */ |
| 1455 | if (g_strv_length (values) == 3) { |
| 1456 | device = fu_util_get_device (priv, values[2], error); |
| 1457 | if (device == NULL) |
| 1458 | return FALSE; |
| 1459 | } else if (g_strv_length (values) == 2) { |
| 1460 | device = fu_util_prompt_for_device (priv, NULL, error); |
| 1461 | if (device == NULL) |
| 1462 | return FALSE; |
| 1463 | } else { |
| 1464 | g_set_error_literal (error, |
| 1465 | FWUPD_ERROR, |
| 1466 | FWUPD_ERROR_INVALID_ARGS, |
| 1467 | "Invalid arguments"); |
| 1468 | return FALSE; |
| 1469 | } |
| 1470 | |
| 1471 | /* run vfunc */ |
| 1472 | locker = fu_device_locker_new (device, error); |
| 1473 | if (locker == NULL) |
| 1474 | return FALSE; |
| 1475 | return fu_device_bind_driver (device, values[0], values[1], error); |
| 1476 | } |
| 1477 | |
| 1478 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1479 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1480 | { |
| 1481 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1482 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1485 | if (!fu_util_start_engine (priv, |
| 1486 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 1487 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 1488 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 1489 | error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1490 | return FALSE; |
| 1491 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1492 | /* get device */ |
| 1493 | if (g_strv_length (values) >= 1) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 1494 | device = fu_util_get_device (priv, values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1495 | if (device == NULL) |
| 1496 | return FALSE; |
| 1497 | } else { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 1498 | device = fu_util_prompt_for_device (priv, NULL, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1499 | if (device == NULL) |
| 1500 | return FALSE; |
| 1501 | } |
| 1502 | |
| 1503 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1504 | locker = fu_device_locker_new (device, error); |
| 1505 | if (locker == NULL) |
| 1506 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1507 | return fu_device_attach (device, error); |
| 1508 | } |
| 1509 | |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1510 | static gboolean |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1511 | fu_util_check_activation_needed (FuUtilPrivate *priv, GError **error) |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1512 | { |
| 1513 | gboolean has_pending = FALSE; |
| 1514 | g_autoptr(FuHistory) history = fu_history_new (); |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1515 | g_autoptr(GPtrArray) devices = fu_history_get_devices (history, error); |
| 1516 | if (devices == NULL) |
| 1517 | return FALSE; |
| 1518 | |
| 1519 | /* only start up the plugins needed */ |
| 1520 | for (guint i = 0; i < devices->len; i++) { |
| 1521 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 1522 | if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
| 1523 | fu_engine_add_plugin_filter (priv->engine, |
| 1524 | fu_device_get_plugin (dev)); |
| 1525 | has_pending = TRUE; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | if (!has_pending) { |
| 1530 | g_set_error_literal (error, |
| 1531 | FWUPD_ERROR, |
| 1532 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1533 | "No devices to activate"); |
| 1534 | return FALSE; |
| 1535 | } |
| 1536 | |
| 1537 | return TRUE; |
| 1538 | } |
| 1539 | |
| 1540 | static gboolean |
| 1541 | fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1542 | { |
| 1543 | gboolean has_pending = FALSE; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1544 | g_autoptr(GPtrArray) devices = NULL; |
| 1545 | |
| 1546 | /* check the history database before starting the daemon */ |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1547 | if (!fu_util_check_activation_needed (priv, error)) |
| 1548 | return FALSE; |
| 1549 | |
| 1550 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1551 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1552 | return FALSE; |
| 1553 | |
| 1554 | /* parse arguments */ |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1555 | if (g_strv_length (values) == 0) { |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1556 | devices = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1557 | if (devices == NULL) |
| 1558 | return FALSE; |
| 1559 | } else if (g_strv_length (values) == 1) { |
| 1560 | FuDevice *device; |
Daniel Campello | f8fb2dc | 2020-09-29 13:48:55 -0600 | [diff] [blame] | 1561 | device = fu_util_get_device (priv, values[0], error); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1562 | if (device == NULL) |
| 1563 | return FALSE; |
| 1564 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1565 | g_ptr_array_add (devices, device); |
| 1566 | } else { |
| 1567 | g_set_error_literal (error, |
| 1568 | FWUPD_ERROR, |
| 1569 | FWUPD_ERROR_INVALID_ARGS, |
| 1570 | "Invalid arguments"); |
| 1571 | return FALSE; |
| 1572 | } |
| 1573 | |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1574 | /* activate anything with _NEEDS_ACTIVATION */ |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1575 | /* order by device priority */ |
| 1576 | g_ptr_array_sort (devices, fu_util_device_order_sort_cb); |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1577 | for (guint i = 0; i < devices->len; i++) { |
| 1578 | FuDevice *device = g_ptr_array_index (devices, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1579 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (device))) |
| 1580 | continue; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1581 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) |
| 1582 | continue; |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1583 | has_pending = TRUE; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1584 | /* TRANSLATORS: shown when shutting down to switch to the new version */ |
| 1585 | g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device)); |
| 1586 | if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error)) |
| 1587 | return FALSE; |
| 1588 | } |
| 1589 | |
Mario Limonciello | 02085a0 | 2020-09-11 14:59:35 -0500 | [diff] [blame] | 1590 | if (!has_pending) { |
| 1591 | g_set_error_literal (error, |
| 1592 | FWUPD_ERROR, |
| 1593 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1594 | "No devices to activate"); |
| 1595 | return FALSE; |
| 1596 | } |
| 1597 | |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1598 | return TRUE; |
| 1599 | } |
| 1600 | |
| 1601 | static gboolean |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1602 | fu_util_export_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1603 | { |
| 1604 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
| 1605 | g_autoptr(FuSmbios) smbios = fu_smbios_new (); |
| 1606 | g_autoptr(GKeyFile) kf = g_key_file_new (); |
| 1607 | g_autoptr(GPtrArray) hwid_keys = NULL; |
| 1608 | |
| 1609 | /* check args */ |
| 1610 | if (g_strv_length (values) != 1) { |
| 1611 | g_set_error_literal (error, |
| 1612 | FWUPD_ERROR, |
| 1613 | FWUPD_ERROR_INVALID_ARGS, |
| 1614 | "Invalid arguments, expected HWIDS-FILE"); |
| 1615 | return FALSE; |
| 1616 | } |
| 1617 | |
| 1618 | /* setup default hwids */ |
| 1619 | if (!fu_smbios_setup (smbios, error)) |
| 1620 | return FALSE; |
| 1621 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 1622 | return FALSE; |
| 1623 | |
| 1624 | /* save all keys */ |
| 1625 | hwid_keys = fu_hwids_get_keys (hwids); |
| 1626 | for (guint i = 0; i < hwid_keys->len; i++) { |
| 1627 | const gchar *hwid_key = g_ptr_array_index (hwid_keys, i); |
| 1628 | const gchar *value = fu_hwids_get_value (hwids, hwid_key); |
| 1629 | g_key_file_set_string (kf, "HwIds", hwid_key, value); |
| 1630 | } |
| 1631 | |
| 1632 | /* success */ |
| 1633 | return g_key_file_save_to_file (kf, values[0], error); |
| 1634 | } |
| 1635 | |
| 1636 | static gboolean |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1637 | fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1638 | { |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1639 | g_autoptr(FuSmbios) smbios = NULL; |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1640 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1641 | g_autoptr(GPtrArray) hwid_keys = fu_hwids_get_keys (hwids); |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1642 | |
| 1643 | /* read DMI data */ |
| 1644 | if (g_strv_length (values) == 0) { |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1645 | smbios = fu_smbios_new (); |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1646 | if (!fu_smbios_setup (smbios, error)) |
| 1647 | return FALSE; |
| 1648 | } else if (g_strv_length (values) == 1) { |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1649 | /* a keyfile with overrides */ |
| 1650 | g_autoptr(GKeyFile) kf = g_key_file_new (); |
| 1651 | if (g_key_file_load_from_file (kf, values[0], G_KEY_FILE_NONE, NULL)) { |
| 1652 | for (guint i = 0; i < hwid_keys->len; i++) { |
| 1653 | const gchar *hwid_key = g_ptr_array_index (hwid_keys, i); |
| 1654 | g_autofree gchar *tmp = NULL; |
| 1655 | tmp = g_key_file_get_string (kf, "HwIds", hwid_key, NULL); |
| 1656 | fu_hwids_add_smbios_override (hwids, hwid_key, tmp); |
| 1657 | } |
| 1658 | /* a DMI blob */ |
| 1659 | } else { |
| 1660 | smbios = fu_smbios_new (); |
| 1661 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 1662 | return FALSE; |
| 1663 | } |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1664 | } else { |
| 1665 | g_set_error_literal (error, |
| 1666 | FWUPD_ERROR, |
| 1667 | FWUPD_ERROR_INVALID_ARGS, |
| 1668 | "Invalid arguments"); |
| 1669 | return FALSE; |
| 1670 | } |
| 1671 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 1672 | return FALSE; |
| 1673 | |
| 1674 | /* show debug output */ |
| 1675 | g_print ("Computer Information\n"); |
| 1676 | g_print ("--------------------\n"); |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1677 | for (guint i = 0; i < hwid_keys->len; i++) { |
| 1678 | const gchar *hwid_key = g_ptr_array_index (hwid_keys, i); |
| 1679 | const gchar *value = fu_hwids_get_value (hwids, hwid_key); |
| 1680 | if (value == NULL) |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1681 | continue; |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1682 | if (g_strcmp0 (hwid_key, FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 || |
| 1683 | g_strcmp0 (hwid_key, FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) { |
| 1684 | guint64 val = g_ascii_strtoull (value, NULL, 16); |
| 1685 | g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_key, val); |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1686 | } else { |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 1687 | g_print ("%s: %s\n", hwid_key, value); |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* show GUIDs */ |
| 1692 | g_print ("\nHardware IDs\n"); |
| 1693 | g_print ("------------\n"); |
| 1694 | for (guint i = 0; i < 15; i++) { |
| 1695 | const gchar *keys = NULL; |
| 1696 | g_autofree gchar *guid = NULL; |
| 1697 | g_autofree gchar *key = NULL; |
| 1698 | g_autofree gchar *keys_str = NULL; |
| 1699 | g_auto(GStrv) keysv = NULL; |
| 1700 | g_autoptr(GError) error_local = NULL; |
| 1701 | |
| 1702 | /* get the GUID */ |
| 1703 | key = g_strdup_printf ("HardwareID-%u", i); |
| 1704 | keys = fu_hwids_get_replace_keys (hwids, key); |
| 1705 | guid = fu_hwids_get_guid (hwids, key, &error_local); |
| 1706 | if (guid == NULL) { |
| 1707 | g_print ("%s\n", error_local->message); |
| 1708 | continue; |
| 1709 | } |
| 1710 | |
| 1711 | /* show what makes up the GUID */ |
| 1712 | keysv = g_strsplit (keys, "&", -1); |
| 1713 | keys_str = g_strjoinv (" + ", keysv); |
| 1714 | g_print ("{%s} <- %s\n", guid, keys_str); |
| 1715 | } |
| 1716 | |
| 1717 | return TRUE; |
| 1718 | } |
| 1719 | |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1720 | static gboolean |
| 1721 | fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1722 | { |
| 1723 | const gchar *script_fn = "startup.sh"; |
| 1724 | const gchar *output_fn = "firmware.bin"; |
| 1725 | g_autoptr(GBytes) archive_blob = NULL; |
| 1726 | g_autoptr(GBytes) firmware_blob = NULL; |
| 1727 | if (g_strv_length (values) < 2) { |
| 1728 | g_set_error_literal (error, |
| 1729 | FWUPD_ERROR, |
| 1730 | FWUPD_ERROR_INVALID_ARGS, |
| 1731 | "Invalid arguments"); |
| 1732 | return FALSE; |
| 1733 | } |
| 1734 | archive_blob = fu_common_get_contents_bytes (values[0], error); |
| 1735 | if (archive_blob == NULL) |
| 1736 | return FALSE; |
| 1737 | if (g_strv_length (values) > 2) |
| 1738 | script_fn = values[2]; |
| 1739 | if (g_strv_length (values) > 3) |
| 1740 | output_fn = values[3]; |
| 1741 | firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error); |
| 1742 | if (firmware_blob == NULL) |
| 1743 | return FALSE; |
| 1744 | return fu_common_set_contents_bytes (values[1], firmware_blob, error); |
| 1745 | } |
| 1746 | |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1747 | static gboolean |
| 1748 | fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1749 | { |
| 1750 | g_autofree gchar *sig = NULL; |
| 1751 | |
| 1752 | /* check args */ |
| 1753 | if (g_strv_length (values) != 1) { |
| 1754 | g_set_error_literal (error, |
| 1755 | FWUPD_ERROR, |
| 1756 | FWUPD_ERROR_INVALID_ARGS, |
| 1757 | "Invalid arguments: value expected"); |
| 1758 | return FALSE; |
| 1759 | } |
| 1760 | |
| 1761 | /* start engine */ |
| 1762 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1763 | return FALSE; |
| 1764 | sig = fu_engine_self_sign (priv->engine, values[0], |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 1765 | JCAT_SIGN_FLAG_ADD_TIMESTAMP | |
| 1766 | JCAT_SIGN_FLAG_ADD_CERT, error); |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1767 | if (sig == NULL) |
| 1768 | return FALSE; |
| 1769 | g_print ("%s\n", sig); |
| 1770 | return TRUE; |
| 1771 | } |
| 1772 | |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1773 | static void |
| 1774 | fu_util_device_added_cb (FwupdClient *client, |
| 1775 | FwupdDevice *device, |
| 1776 | gpointer user_data) |
| 1777 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1778 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1779 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1780 | g_print ("%s\n%s", _("Device added:"), tmp); |
| 1781 | } |
| 1782 | |
| 1783 | static void |
| 1784 | fu_util_device_removed_cb (FwupdClient *client, |
| 1785 | FwupdDevice *device, |
| 1786 | gpointer user_data) |
| 1787 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1788 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1789 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1790 | g_print ("%s\n%s", _("Device removed:"), tmp); |
| 1791 | } |
| 1792 | |
| 1793 | static void |
| 1794 | fu_util_device_changed_cb (FwupdClient *client, |
| 1795 | FwupdDevice *device, |
| 1796 | gpointer user_data) |
| 1797 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1798 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1799 | /* TRANSLATORS: this is when a device has been updated */ |
| 1800 | g_print ("%s\n%s", _("Device changed:"), tmp); |
| 1801 | } |
| 1802 | |
| 1803 | static void |
| 1804 | fu_util_changed_cb (FwupdClient *client, gpointer user_data) |
| 1805 | { |
| 1806 | /* TRANSLATORS: this is when the daemon state changes */ |
| 1807 | g_print ("%s\n", _("Changed")); |
| 1808 | } |
| 1809 | |
| 1810 | static gboolean |
| 1811 | fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1812 | { |
| 1813 | g_autoptr(FwupdClient) client = fwupd_client_new (); |
Richard Hughes | 6ed25f5 | 2021-01-10 19:27:33 +0000 | [diff] [blame] | 1814 | fwupd_client_set_main_context (client, priv->main_ctx); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1815 | |
| 1816 | /* get all the devices */ |
| 1817 | if (!fwupd_client_connect (client, priv->cancellable, error)) |
| 1818 | return FALSE; |
| 1819 | |
| 1820 | /* watch for any hotplugged device */ |
| 1821 | g_signal_connect (client, "changed", |
| 1822 | G_CALLBACK (fu_util_changed_cb), priv); |
| 1823 | g_signal_connect (client, "device-added", |
| 1824 | G_CALLBACK (fu_util_device_added_cb), priv); |
| 1825 | g_signal_connect (client, "device-removed", |
| 1826 | G_CALLBACK (fu_util_device_removed_cb), priv); |
| 1827 | g_signal_connect (client, "device-changed", |
| 1828 | G_CALLBACK (fu_util_device_changed_cb), priv); |
| 1829 | g_signal_connect (priv->cancellable, "cancelled", |
| 1830 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1831 | g_main_loop_run (priv->loop); |
| 1832 | return TRUE; |
| 1833 | } |
| 1834 | |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1835 | static gboolean |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1836 | fu_util_get_firmware_types (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1837 | { |
| 1838 | g_autoptr(GPtrArray) firmware_types = NULL; |
| 1839 | |
| 1840 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1841 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1842 | return FALSE; |
| 1843 | |
| 1844 | firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine); |
| 1845 | for (guint i = 0; i < firmware_types->len; i++) { |
| 1846 | const gchar *id = g_ptr_array_index (firmware_types, i); |
| 1847 | g_print ("%s\n", id); |
| 1848 | } |
| 1849 | if (firmware_types->len == 0) { |
| 1850 | /* TRANSLATORS: nothing found */ |
| 1851 | g_print ("%s\n", _("No firmware IDs found")); |
| 1852 | return TRUE; |
| 1853 | } |
| 1854 | |
| 1855 | return TRUE; |
| 1856 | } |
| 1857 | |
| 1858 | static gchar * |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 1859 | fu_util_prompt_for_firmware_type (FuUtilPrivate *priv, GError **error) |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1860 | { |
| 1861 | g_autoptr(GPtrArray) firmware_types = NULL; |
| 1862 | guint idx; |
| 1863 | firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine); |
| 1864 | |
| 1865 | /* TRANSLATORS: get interactive prompt */ |
| 1866 | g_print ("%s\n", _("Choose a firmware type:")); |
| 1867 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 1868 | g_print ("0.\t%s\n", _("Cancel")); |
| 1869 | for (guint i = 0; i < firmware_types->len; i++) { |
| 1870 | const gchar *id = g_ptr_array_index (firmware_types, i); |
| 1871 | g_print ("%u.\t%s\n", i + 1, id); |
| 1872 | } |
| 1873 | idx = fu_util_prompt_for_number (firmware_types->len); |
| 1874 | if (idx == 0) { |
| 1875 | g_set_error_literal (error, |
| 1876 | FWUPD_ERROR, |
| 1877 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1878 | "Request canceled"); |
| 1879 | return NULL; |
| 1880 | } |
| 1881 | |
| 1882 | return g_strdup (g_ptr_array_index (firmware_types, idx - 1)); |
| 1883 | } |
| 1884 | |
| 1885 | static gboolean |
| 1886 | fu_util_firmware_parse (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1887 | { |
| 1888 | GType gtype; |
| 1889 | g_autoptr(GBytes) blob = NULL; |
| 1890 | g_autoptr(FuFirmware) firmware = NULL; |
| 1891 | g_autofree gchar *firmware_type = NULL; |
| 1892 | g_autofree gchar *str = NULL; |
| 1893 | |
| 1894 | /* check args */ |
| 1895 | if (g_strv_length (values) == 0 || g_strv_length (values) > 2) { |
| 1896 | g_set_error_literal (error, |
| 1897 | FWUPD_ERROR, |
| 1898 | FWUPD_ERROR_INVALID_ARGS, |
| 1899 | "Invalid arguments: filename required"); |
| 1900 | return FALSE; |
| 1901 | } |
| 1902 | |
| 1903 | if (g_strv_length (values) == 2) |
| 1904 | firmware_type = g_strdup (values[1]); |
| 1905 | |
| 1906 | /* load file */ |
| 1907 | blob = fu_common_get_contents_bytes (values[0], error); |
| 1908 | if (blob == NULL) |
| 1909 | return FALSE; |
| 1910 | |
| 1911 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1912 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1913 | return FALSE; |
| 1914 | |
| 1915 | /* find the GType to use */ |
| 1916 | if (firmware_type == NULL) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 1917 | firmware_type = fu_util_prompt_for_firmware_type (priv, error); |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1918 | if (firmware_type == NULL) |
| 1919 | return FALSE; |
| 1920 | gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type); |
| 1921 | if (gtype == G_TYPE_INVALID) { |
| 1922 | g_set_error (error, |
| 1923 | G_IO_ERROR, |
| 1924 | G_IO_ERROR_NOT_FOUND, |
| 1925 | "GType %s not supported", firmware_type); |
| 1926 | return FALSE; |
| 1927 | } |
| 1928 | firmware = g_object_new (gtype, NULL); |
| 1929 | if (!fu_firmware_parse (firmware, blob, priv->flags, error)) |
| 1930 | return FALSE; |
| 1931 | str = fu_firmware_to_string (firmware); |
| 1932 | g_print ("%s", str); |
| 1933 | return TRUE; |
| 1934 | } |
| 1935 | |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 1936 | static gboolean |
| 1937 | fu_util_firmware_extract (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1938 | { |
| 1939 | GType gtype; |
| 1940 | g_autofree gchar *firmware_type = NULL; |
| 1941 | g_autofree gchar *str = NULL; |
| 1942 | g_autoptr(FuFirmware) firmware = NULL; |
| 1943 | g_autoptr(GBytes) blob = NULL; |
| 1944 | g_autoptr(GPtrArray) images = NULL; |
| 1945 | |
| 1946 | /* check args */ |
| 1947 | if (g_strv_length (values) == 0 || g_strv_length (values) > 2) { |
| 1948 | g_set_error_literal (error, |
| 1949 | FWUPD_ERROR, |
| 1950 | FWUPD_ERROR_INVALID_ARGS, |
| 1951 | "Invalid arguments: filename required"); |
| 1952 | return FALSE; |
| 1953 | } |
| 1954 | if (g_strv_length (values) == 2) |
| 1955 | firmware_type = g_strdup (values[1]); |
| 1956 | |
| 1957 | /* load file */ |
| 1958 | blob = fu_common_get_contents_bytes (values[0], error); |
| 1959 | if (blob == NULL) |
| 1960 | return FALSE; |
| 1961 | |
| 1962 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 1963 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 1964 | return FALSE; |
| 1965 | |
| 1966 | /* find the GType to use */ |
| 1967 | if (firmware_type == NULL) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 1968 | firmware_type = fu_util_prompt_for_firmware_type (priv, error); |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 1969 | if (firmware_type == NULL) |
| 1970 | return FALSE; |
| 1971 | gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type); |
| 1972 | if (gtype == G_TYPE_INVALID) { |
| 1973 | g_set_error (error, |
| 1974 | G_IO_ERROR, |
| 1975 | G_IO_ERROR_NOT_FOUND, |
| 1976 | "GType %s not supported", firmware_type); |
| 1977 | return FALSE; |
| 1978 | } |
| 1979 | firmware = g_object_new (gtype, NULL); |
| 1980 | if (!fu_firmware_parse (firmware, blob, priv->flags, error)) |
| 1981 | return FALSE; |
| 1982 | str = fu_firmware_to_string (firmware); |
| 1983 | g_print ("%s", str); |
| 1984 | images = fu_firmware_get_images (firmware); |
| 1985 | for (guint i = 0; i < images->len; i++) { |
| 1986 | FuFirmwareImage *img = g_ptr_array_index (images, i); |
| 1987 | g_autofree gchar *fn = NULL; |
| 1988 | g_autoptr(GBytes) blob_img = NULL; |
| 1989 | |
Richard Hughes | 88dd7c4 | 2020-09-22 16:54:40 +0100 | [diff] [blame] | 1990 | /* get raw image without generated header, footer or crc */ |
| 1991 | blob_img = fu_firmware_image_get_bytes (img); |
| 1992 | if (blob_img == NULL || g_bytes_get_size (blob_img) == 0) |
| 1993 | continue; |
| 1994 | |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 1995 | /* use suitable filename */ |
| 1996 | if (fu_firmware_image_get_filename (img) != NULL) { |
| 1997 | fn = g_strdup (fu_firmware_image_get_filename (img)); |
| 1998 | } else if (fu_firmware_image_get_id (img) != NULL) { |
| 1999 | fn = g_strdup_printf ("id-%s.fw", fu_firmware_image_get_id (img)); |
| 2000 | } else if (fu_firmware_image_get_idx (img) != 0x0) { |
| 2001 | fn = g_strdup_printf ("idx-0x%x.fw", (guint) fu_firmware_image_get_idx (img)); |
| 2002 | } else { |
| 2003 | fn = g_strdup_printf ("img-0x%x.fw", i); |
| 2004 | } |
| 2005 | /* TRANSLATORS: decompressing images from a container firmware */ |
| 2006 | g_print ("%s : %s\n", _("Writing file:"), fn); |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 2007 | if (!fu_common_set_contents_bytes (fn, blob_img, error)) |
| 2008 | return FALSE; |
| 2009 | } |
| 2010 | |
| 2011 | /* success */ |
| 2012 | return TRUE; |
| 2013 | } |
| 2014 | |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2015 | static gboolean |
| 2016 | fu_util_firmware_build (FuUtilPrivate *priv, gchar **values, GError **error) |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2017 | { |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2018 | GType gtype = FU_TYPE_FIRMWARE; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2019 | const gchar *tmp; |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2020 | g_autofree gchar *str = NULL; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2021 | g_autoptr(FuFirmware) firmware = NULL; |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2022 | g_autoptr(FuFirmware) firmware_dst = NULL; |
| 2023 | g_autoptr(GBytes) blob_dst = NULL; |
| 2024 | g_autoptr(GBytes) blob_src = NULL; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2025 | g_autoptr(XbBuilder) builder = xb_builder_new (); |
| 2026 | g_autoptr(XbBuilderSource) source = xb_builder_source_new (); |
| 2027 | g_autoptr(XbNode) n = NULL; |
| 2028 | g_autoptr(XbSilo) silo = NULL; |
| 2029 | |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2030 | /* check args */ |
| 2031 | if (g_strv_length (values) != 2) { |
| 2032 | g_set_error_literal (error, |
| 2033 | FWUPD_ERROR, |
| 2034 | FWUPD_ERROR_INVALID_ARGS, |
| 2035 | "Invalid arguments: filename required"); |
| 2036 | return FALSE; |
| 2037 | } |
| 2038 | |
| 2039 | /* load file */ |
| 2040 | blob_src = fu_common_get_contents_bytes (values[0], error); |
| 2041 | if (blob_src == NULL) |
| 2042 | return FALSE; |
| 2043 | |
| 2044 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2045 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2046 | return FALSE; |
| 2047 | |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2048 | /* parse XML */ |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2049 | if (!xb_builder_source_load_bytes (source, blob_src, |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2050 | XB_BUILDER_SOURCE_FLAG_NONE, |
| 2051 | error)) { |
| 2052 | g_prefix_error (error, "could not parse XML: "); |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2053 | return FALSE; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2054 | } |
| 2055 | xb_builder_import_source (builder, source); |
| 2056 | silo = xb_builder_compile (builder, XB_BUILDER_COMPILE_FLAG_NONE, NULL, error); |
| 2057 | if (silo == NULL) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2058 | return FALSE; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2059 | |
| 2060 | /* create FuFirmware of specific GType */ |
| 2061 | n = xb_silo_query_first (silo, "firmware", error); |
| 2062 | if (n == NULL) |
| 2063 | return FALSE; |
| 2064 | tmp = xb_node_get_attr (n, "gtype"); |
| 2065 | if (tmp != NULL) { |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2066 | gtype = g_type_from_name (tmp); |
| 2067 | if (gtype == G_TYPE_INVALID) { |
| 2068 | g_set_error (error, |
| 2069 | G_IO_ERROR, |
| 2070 | G_IO_ERROR_NOT_FOUND, |
| 2071 | "GType %s not registered", tmp); |
| 2072 | return FALSE; |
| 2073 | } |
| 2074 | } |
| 2075 | tmp = xb_node_get_attr (n, "id"); |
| 2076 | if (tmp != NULL) { |
| 2077 | gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, tmp); |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2078 | if (gtype == G_TYPE_INVALID) { |
| 2079 | g_set_error (error, |
| 2080 | G_IO_ERROR, |
| 2081 | G_IO_ERROR_NOT_FOUND, |
| 2082 | "GType %s not supported", tmp); |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2083 | return FALSE; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2084 | } |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2085 | } |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2086 | firmware = g_object_new (gtype, NULL); |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2087 | if (!fu_firmware_build (firmware, n, error)) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2088 | return FALSE; |
| 2089 | |
| 2090 | /* write new file */ |
| 2091 | blob_dst = fu_firmware_write (firmware, error); |
| 2092 | if (blob_dst == NULL) |
| 2093 | return FALSE; |
| 2094 | if (!fu_common_set_contents_bytes (values[1], blob_dst, error)) |
| 2095 | return FALSE; |
| 2096 | |
| 2097 | /* show what we wrote */ |
| 2098 | firmware_dst = g_object_new (gtype, NULL); |
| 2099 | if (!fu_firmware_parse (firmware_dst, blob_dst, priv->flags, error)) |
| 2100 | return FALSE; |
| 2101 | str = fu_firmware_to_string (firmware_dst); |
| 2102 | g_print ("%s", str); |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2103 | |
| 2104 | /* success */ |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2105 | return TRUE; |
Richard Hughes | 41400a8 | 2020-09-21 13:43:15 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 2108 | static gboolean |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2109 | fu_util_firmware_convert (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2110 | { |
| 2111 | GType gtype_dst; |
| 2112 | GType gtype_src; |
| 2113 | g_autofree gchar *firmware_type_dst = NULL; |
| 2114 | g_autofree gchar *firmware_type_src = NULL; |
| 2115 | g_autofree gchar *str_dst = NULL; |
| 2116 | g_autofree gchar *str_src = NULL; |
| 2117 | g_autoptr(FuFirmware) firmware_dst = NULL; |
| 2118 | g_autoptr(FuFirmware) firmware_src = NULL; |
| 2119 | g_autoptr(GBytes) blob_dst = NULL; |
| 2120 | g_autoptr(GBytes) blob_src = NULL; |
| 2121 | g_autoptr(GPtrArray) images = NULL; |
| 2122 | |
| 2123 | /* check args */ |
| 2124 | if (g_strv_length (values) < 2 || g_strv_length (values) > 4) { |
| 2125 | g_set_error_literal (error, |
| 2126 | FWUPD_ERROR, |
| 2127 | FWUPD_ERROR_INVALID_ARGS, |
| 2128 | "Invalid arguments: filename required"); |
| 2129 | return FALSE; |
| 2130 | } |
| 2131 | |
| 2132 | if (g_strv_length (values) > 2) |
| 2133 | firmware_type_src = g_strdup (values[2]); |
| 2134 | if (g_strv_length (values) > 3) |
| 2135 | firmware_type_dst = g_strdup (values[3]); |
| 2136 | |
| 2137 | /* load file */ |
| 2138 | blob_src = fu_common_get_contents_bytes (values[0], error); |
| 2139 | if (blob_src == NULL) |
| 2140 | return FALSE; |
| 2141 | |
| 2142 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2143 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_READONLY, error)) |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2144 | return FALSE; |
| 2145 | |
| 2146 | /* find the GType to use */ |
| 2147 | if (firmware_type_src == NULL) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2148 | firmware_type_src = fu_util_prompt_for_firmware_type (priv, error); |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2149 | if (firmware_type_src == NULL) |
| 2150 | return FALSE; |
| 2151 | if (firmware_type_dst == NULL) |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2152 | firmware_type_dst = fu_util_prompt_for_firmware_type (priv, error); |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2153 | if (firmware_type_dst == NULL) |
| 2154 | return FALSE; |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2155 | gtype_src = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_src); |
| 2156 | if (gtype_src == G_TYPE_INVALID) { |
| 2157 | g_set_error (error, |
| 2158 | G_IO_ERROR, |
| 2159 | G_IO_ERROR_NOT_FOUND, |
| 2160 | "GType %s not supported", firmware_type_src); |
| 2161 | return FALSE; |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2162 | } |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2163 | firmware_src = g_object_new (gtype_src, NULL); |
| 2164 | if (!fu_firmware_parse (firmware_src, blob_src, priv->flags, error)) |
| 2165 | return FALSE; |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2166 | gtype_dst = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_dst); |
| 2167 | if (gtype_dst == G_TYPE_INVALID) { |
| 2168 | g_set_error (error, |
| 2169 | G_IO_ERROR, |
| 2170 | G_IO_ERROR_NOT_FOUND, |
| 2171 | "GType %s not supported", firmware_type_dst); |
| 2172 | return FALSE; |
| 2173 | } |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2174 | str_src = fu_firmware_to_string (firmware_src); |
| 2175 | g_print ("%s", str_src); |
| 2176 | |
| 2177 | /* copy images */ |
| 2178 | firmware_dst = g_object_new (gtype_dst, NULL); |
| 2179 | images = fu_firmware_get_images (firmware_src); |
| 2180 | for (guint i = 0; i < images->len; i++) { |
| 2181 | FuFirmwareImage *img = g_ptr_array_index (images, i); |
| 2182 | fu_firmware_add_image (firmware_dst, img); |
| 2183 | } |
| 2184 | |
| 2185 | /* write new file */ |
| 2186 | blob_dst = fu_firmware_write (firmware_dst, error); |
| 2187 | if (blob_dst == NULL) |
| 2188 | return FALSE; |
| 2189 | if (!fu_common_set_contents_bytes (values[1], blob_dst, error)) |
| 2190 | return FALSE; |
| 2191 | str_dst = fu_firmware_to_string (firmware_dst); |
| 2192 | g_print ("%s", str_dst); |
| 2193 | |
| 2194 | /* success */ |
| 2195 | return TRUE; |
| 2196 | } |
| 2197 | |
| 2198 | static gboolean |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2199 | fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2200 | { |
| 2201 | g_autofree gchar *str = NULL; |
| 2202 | g_autoptr(FuDevice) dev = NULL; |
| 2203 | |
| 2204 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2205 | if (!fu_util_start_engine (priv, |
| 2206 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 2207 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 2208 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 2209 | error)) |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2210 | return FALSE; |
| 2211 | |
| 2212 | /* get device */ |
| 2213 | if (g_strv_length (values) == 1) { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 2214 | dev = fu_util_get_device (priv, values[1], error); |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2215 | if (dev == NULL) |
| 2216 | return FALSE; |
| 2217 | } else { |
Richard Hughes | 3aaf53c | 2020-04-20 09:39:46 +0100 | [diff] [blame] | 2218 | dev = fu_util_prompt_for_device (priv, NULL, error); |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2219 | if (dev == NULL) |
| 2220 | return FALSE; |
| 2221 | } |
| 2222 | |
| 2223 | /* add checksums */ |
| 2224 | if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error)) |
| 2225 | return FALSE; |
| 2226 | |
| 2227 | /* show checksums */ |
| 2228 | str = fu_device_to_string (dev); |
| 2229 | g_print ("%s\n", str); |
| 2230 | return TRUE; |
| 2231 | } |
| 2232 | |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2233 | static gboolean |
| 2234 | fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2235 | { |
| 2236 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 2237 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 2238 | g_autofree gchar *title = NULL; |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2239 | |
| 2240 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2241 | if (!fu_util_start_engine (priv, |
| 2242 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 2243 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 2244 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 2245 | error)) |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2246 | return FALSE; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 2247 | title = fu_util_get_tree_title (priv); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2248 | |
| 2249 | /* get all devices from the history database */ |
| 2250 | devices = fu_engine_get_history (priv->engine, error); |
| 2251 | if (devices == NULL) |
| 2252 | return FALSE; |
| 2253 | |
| 2254 | /* show each device */ |
| 2255 | for (guint i = 0; i < devices->len; i++) { |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2256 | g_autoptr(GPtrArray) rels = NULL; |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2257 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 2258 | FwupdRelease *rel; |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2259 | const gchar *remote; |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 2260 | GNode *child; |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2261 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 2262 | if (!fu_util_filter_device (priv, dev)) |
| 2263 | continue; |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 2264 | child = g_node_append_data (root, dev); |
| 2265 | |
| 2266 | rel = fwupd_device_get_release_default (dev); |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2267 | if (rel == NULL) |
| 2268 | continue; |
| 2269 | remote = fwupd_release_get_remote_id (rel); |
| 2270 | |
| 2271 | /* doesn't actually map to remote */ |
| 2272 | if (remote == NULL) { |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 2273 | g_node_append_data (child, rel); |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2274 | continue; |
| 2275 | } |
| 2276 | |
| 2277 | /* try to lookup releases from client */ |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 2278 | rels = fu_engine_get_releases (priv->engine, |
| 2279 | priv->request, |
| 2280 | fwupd_device_get_id (dev), |
| 2281 | error); |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 2282 | if (rels == NULL) |
| 2283 | return FALSE; |
| 2284 | |
| 2285 | /* map to a release in client */ |
| 2286 | for (guint j = 0; j < rels->len; j++) { |
| 2287 | FwupdRelease *rel2 = g_ptr_array_index (rels, j); |
| 2288 | if (g_strcmp0 (remote, |
| 2289 | fwupd_release_get_remote_id (rel2)) != 0) |
| 2290 | continue; |
| 2291 | if (g_strcmp0 (fwupd_release_get_version (rel), |
| 2292 | fwupd_release_get_version (rel2)) != 0) |
| 2293 | continue; |
| 2294 | g_node_append_data (child, g_object_ref (rel2)); |
| 2295 | rel = NULL; |
| 2296 | break; |
| 2297 | } |
| 2298 | |
| 2299 | /* didn't match anything */ |
| 2300 | if (rels->len == 0 || rel != NULL) { |
| 2301 | g_node_append_data (child, rel); |
| 2302 | continue; |
| 2303 | } |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 2304 | |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2305 | } |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 2306 | fu_util_print_tree (root, title); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2307 | |
| 2308 | return TRUE; |
| 2309 | } |
| 2310 | |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 2311 | static gboolean |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2312 | fu_util_refresh_remote (FuUtilPrivate *priv, FwupdRemote *remote, GError **error) |
| 2313 | { |
Richard Hughes | c5710d9 | 2020-06-26 11:08:25 +0100 | [diff] [blame] | 2314 | const gchar *metadata_uri = NULL; |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2315 | g_autofree gchar *fn_raw = NULL; |
| 2316 | g_autofree gchar *fn_sig = NULL; |
| 2317 | g_autoptr(GBytes) bytes_raw = NULL; |
| 2318 | g_autoptr(GBytes) bytes_sig = NULL; |
| 2319 | |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2320 | /* signature */ |
Richard Hughes | c5710d9 | 2020-06-26 11:08:25 +0100 | [diff] [blame] | 2321 | metadata_uri = fwupd_remote_get_metadata_uri_sig (remote); |
| 2322 | if (metadata_uri == NULL) { |
Richard Hughes | fb6315f | 2020-09-09 17:05:20 +0100 | [diff] [blame] | 2323 | g_set_error (error, |
| 2324 | FWUPD_ERROR, |
| 2325 | FWUPD_ERROR_NOTHING_TO_DO, |
| 2326 | "no metadata signature URI available for %s", |
| 2327 | fwupd_remote_get_id (remote)); |
Richard Hughes | c5710d9 | 2020-06-26 11:08:25 +0100 | [diff] [blame] | 2328 | return FALSE; |
| 2329 | } |
| 2330 | fn_sig = fu_util_get_user_cache_path (metadata_uri); |
Richard Hughes | f083af3 | 2020-10-07 13:59:09 +0100 | [diff] [blame] | 2331 | if (!fu_common_mkdir_parent (fn_sig, error)) |
| 2332 | return FALSE; |
Richard Hughes | c5710d9 | 2020-06-26 11:08:25 +0100 | [diff] [blame] | 2333 | if (!fu_util_download_out_of_process (metadata_uri, fn_sig, error)) |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2334 | return FALSE; |
| 2335 | bytes_sig = fu_common_get_contents_bytes (fn_sig, error); |
| 2336 | if (bytes_sig == NULL) |
| 2337 | return FALSE; |
Richard Hughes | f083af3 | 2020-10-07 13:59:09 +0100 | [diff] [blame] | 2338 | if (!fwupd_remote_load_signature_bytes (remote, bytes_sig, error)) |
| 2339 | return FALSE; |
| 2340 | |
| 2341 | /* payload */ |
| 2342 | metadata_uri = fwupd_remote_get_metadata_uri (remote); |
| 2343 | if (metadata_uri == NULL) { |
| 2344 | g_set_error (error, |
| 2345 | FWUPD_ERROR, |
| 2346 | FWUPD_ERROR_NOTHING_TO_DO, |
| 2347 | "no metadata URI available for %s", |
| 2348 | fwupd_remote_get_id (remote)); |
| 2349 | return FALSE; |
| 2350 | } |
| 2351 | fn_raw = fu_util_get_user_cache_path (metadata_uri); |
| 2352 | if (!fu_util_download_out_of_process (metadata_uri, fn_raw, error)) |
| 2353 | return FALSE; |
| 2354 | bytes_raw = fu_common_get_contents_bytes (fn_raw, error); |
| 2355 | if (bytes_raw == NULL) |
| 2356 | return FALSE; |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* send to daemon */ |
| 2359 | g_debug ("updating %s", fwupd_remote_get_id (remote)); |
| 2360 | return fu_engine_update_metadata_bytes (priv->engine, |
| 2361 | fwupd_remote_get_id (remote), |
| 2362 | bytes_raw, |
| 2363 | bytes_sig, |
| 2364 | error); |
| 2365 | |
| 2366 | } |
| 2367 | |
| 2368 | static gboolean |
| 2369 | fu_util_refresh (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2370 | { |
| 2371 | g_autoptr(GPtrArray) remotes = NULL; |
| 2372 | |
| 2373 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2374 | if (!fu_util_start_engine (priv, |
| 2375 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 2376 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 2377 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 2378 | error)) |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 2379 | return FALSE; |
| 2380 | |
| 2381 | /* download new metadata */ |
| 2382 | remotes = fu_engine_get_remotes (priv->engine, error); |
| 2383 | if (remotes == NULL) |
| 2384 | return FALSE; |
| 2385 | for (guint i = 0; i < remotes->len; i++) { |
| 2386 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
| 2387 | if (!fwupd_remote_get_enabled (remote)) |
| 2388 | continue; |
| 2389 | if (fwupd_remote_get_kind (remote) != FWUPD_REMOTE_KIND_DOWNLOAD) |
| 2390 | continue; |
| 2391 | if (!fu_util_refresh_remote (priv, remote, error)) |
| 2392 | return FALSE; |
| 2393 | } |
| 2394 | return TRUE; |
| 2395 | } |
| 2396 | |
| 2397 | static gboolean |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 2398 | fu_util_get_remotes (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2399 | { |
| 2400 | g_autoptr(GNode) root = g_node_new (NULL); |
| 2401 | g_autoptr(GPtrArray) remotes = NULL; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 2402 | g_autofree gchar *title = NULL; |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 2403 | |
| 2404 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2405 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_REMOTES, error)) |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 2406 | return FALSE; |
Mario Limonciello | dc9a1a8 | 2020-02-20 14:20:20 -0600 | [diff] [blame] | 2407 | title = fu_util_get_tree_title (priv); |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 2408 | |
| 2409 | /* list remotes */ |
| 2410 | remotes = fu_engine_get_remotes (priv->engine, error); |
| 2411 | if (remotes == NULL) |
| 2412 | return FALSE; |
| 2413 | if (remotes->len == 0) { |
| 2414 | g_set_error_literal (error, |
| 2415 | FWUPD_ERROR, |
| 2416 | FWUPD_ERROR_NOTHING_TO_DO, |
| 2417 | "no remotes available"); |
| 2418 | return FALSE; |
| 2419 | } |
| 2420 | for (guint i = 0; i < remotes->len; i++) { |
| 2421 | FwupdRemote *remote_tmp = g_ptr_array_index (remotes, i); |
| 2422 | g_node_append_data (root, remote_tmp); |
| 2423 | } |
| 2424 | fu_util_print_tree (root, title); |
| 2425 | |
| 2426 | return TRUE; |
| 2427 | } |
| 2428 | |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 2429 | static gboolean |
| 2430 | fu_util_security (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2431 | { |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 2432 | FuSecurityAttrToStringFlags flags = FU_SECURITY_ATTR_TO_STRING_FLAG_NONE; |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 2433 | g_autoptr(FuSecurityAttrs) attrs = NULL; |
| 2434 | g_autoptr(GPtrArray) items = NULL; |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 2435 | g_autofree gchar *str = NULL; |
| 2436 | |
| 2437 | /* not ready yet */ |
| 2438 | if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0) { |
| 2439 | g_set_error_literal (error, |
| 2440 | FWUPD_ERROR, |
| 2441 | FWUPD_ERROR_NOT_SUPPORTED, |
| 2442 | "The HSI specification is not yet complete. " |
| 2443 | "To ignore this warning, use --force"); |
| 2444 | return FALSE; |
| 2445 | } |
| 2446 | |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2447 | if (!fu_util_start_engine (priv, |
| 2448 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 2449 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 2450 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 2451 | error)) |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 2452 | return FALSE; |
| 2453 | |
| 2454 | /* TRANSLATORS: this is a string like 'HSI:2-U' */ |
| 2455 | g_print ("%s \033[1m%s\033[0m\n", _("Host Security ID:"), |
| 2456 | fu_engine_get_host_security_id (priv->engine)); |
| 2457 | |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 2458 | /* show or hide different elements */ |
| 2459 | if (priv->show_all) { |
| 2460 | flags |= FU_SECURITY_ATTR_TO_STRING_FLAG_SHOW_OBSOLETES; |
| 2461 | flags |= FU_SECURITY_ATTR_TO_STRING_FLAG_SHOW_URLS; |
| 2462 | } |
| 2463 | |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 2464 | /* print the "why" */ |
Richard Hughes | 56e7ae5 | 2020-05-17 21:00:23 +0100 | [diff] [blame] | 2465 | attrs = fu_engine_get_host_security_attrs (priv->engine); |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 2466 | items = fu_security_attrs_get_all (attrs); |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 2467 | str = fu_util_security_attrs_to_string (items, flags); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 2468 | g_print ("%s\n", str); |
| 2469 | return TRUE; |
| 2470 | } |
| 2471 | |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2472 | static FuVolume * |
| 2473 | fu_util_prompt_for_volume (GError **error) |
| 2474 | { |
| 2475 | FuVolume *volume; |
| 2476 | guint idx; |
Đoàn Trần Công Danh | e9adb95 | 2021-01-23 11:36:26 +0700 | [diff] [blame] | 2477 | gboolean is_fallback = FALSE; |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2478 | g_autoptr(GPtrArray) volumes = NULL; |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2479 | g_autoptr(GPtrArray) volumes_vfat = g_ptr_array_new (); |
| 2480 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2481 | |
| 2482 | /* exactly one */ |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2483 | volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_ESP, &error_local); |
| 2484 | if (volumes == NULL) { |
Đoàn Trần Công Danh | e9adb95 | 2021-01-23 11:36:26 +0700 | [diff] [blame] | 2485 | is_fallback = TRUE; |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2486 | g_debug ("%s, falling back to %s", error_local->message, FU_VOLUME_KIND_BDP); |
| 2487 | volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_BDP, error); |
| 2488 | if (volumes == NULL) { |
| 2489 | g_prefix_error (error, "%s: ", error_local->message); |
| 2490 | return NULL; |
| 2491 | } |
| 2492 | } |
Đoàn Trần Công Danh | e9adb95 | 2021-01-23 11:36:26 +0700 | [diff] [blame] | 2493 | /* on fallback: only add internal vfat partitions */ |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2494 | for (guint i = 0; i < volumes->len; i++) { |
| 2495 | FuVolume *vol = g_ptr_array_index (volumes, i); |
| 2496 | g_autofree gchar *type = fu_volume_get_id_type (vol); |
| 2497 | if (type == NULL) |
| 2498 | continue; |
Đoàn Trần Công Danh | e9adb95 | 2021-01-23 11:36:26 +0700 | [diff] [blame] | 2499 | if (is_fallback && !fu_volume_is_internal (vol)) |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2500 | continue; |
| 2501 | if (g_strcmp0 (type, "vfat") == 0) |
| 2502 | g_ptr_array_add (volumes_vfat, vol); |
| 2503 | } |
| 2504 | if (volumes_vfat->len == 1) { |
| 2505 | volume = g_ptr_array_index (volumes_vfat, 0); |
Richard Hughes | 5976125 | 2020-08-20 07:51:36 +0100 | [diff] [blame] | 2506 | /* TRANSLATORS: Volume has been chosen by the user */ |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2507 | g_print ("%s: %s\n", _("Selected volume"), fu_volume_get_id (volume)); |
| 2508 | return g_object_ref (volume); |
| 2509 | } |
| 2510 | |
| 2511 | /* TRANSLATORS: get interactive prompt */ |
| 2512 | g_print ("%s\n", _("Choose a volume:")); |
| 2513 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 2514 | g_print ("0.\t%s\n", _("Cancel")); |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2515 | for (guint i = 0; i < volumes_vfat->len; i++) { |
| 2516 | volume = g_ptr_array_index (volumes_vfat, i); |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2517 | g_print ("%u.\t%s\n", i + 1, fu_volume_get_id (volume)); |
| 2518 | } |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2519 | idx = fu_util_prompt_for_number (volumes_vfat->len); |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2520 | if (idx == 0) { |
| 2521 | g_set_error_literal (error, |
| 2522 | FWUPD_ERROR, |
| 2523 | FWUPD_ERROR_NOTHING_TO_DO, |
| 2524 | "Request canceled"); |
| 2525 | return NULL; |
| 2526 | } |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2527 | volume = g_ptr_array_index (volumes_vfat, idx - 1); |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 2528 | return g_object_ref (volume); |
| 2529 | |
| 2530 | } |
| 2531 | |
| 2532 | static gboolean |
| 2533 | fu_util_esp_mount (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2534 | { |
| 2535 | g_autoptr(FuVolume) volume = NULL; |
| 2536 | volume = fu_util_prompt_for_volume (error); |
| 2537 | if (volume == NULL) |
| 2538 | return FALSE; |
| 2539 | return fu_volume_mount (volume, error); |
| 2540 | } |
| 2541 | |
| 2542 | static gboolean |
| 2543 | fu_util_esp_unmount (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2544 | { |
| 2545 | g_autoptr(FuVolume) volume = NULL; |
| 2546 | volume = fu_util_prompt_for_volume (error); |
| 2547 | if (volume == NULL) |
| 2548 | return FALSE; |
| 2549 | return fu_volume_unmount (volume, error); |
| 2550 | } |
| 2551 | |
| 2552 | static gboolean |
| 2553 | fu_util_esp_list (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2554 | { |
| 2555 | g_autofree gchar *mount_point = NULL; |
| 2556 | g_autoptr(FuDeviceLocker) locker = NULL; |
| 2557 | g_autoptr(FuVolume) volume = NULL; |
| 2558 | g_autoptr(GPtrArray) files = NULL; |
| 2559 | |
| 2560 | volume = fu_util_prompt_for_volume (error); |
| 2561 | if (volume == NULL) |
| 2562 | return FALSE; |
| 2563 | locker = fu_volume_locker (volume, error); |
| 2564 | if (locker == NULL) |
| 2565 | return FALSE; |
| 2566 | mount_point = fu_volume_get_mount_point (volume); |
| 2567 | files = fu_common_get_files_recursive (mount_point, error); |
| 2568 | if (files == NULL) |
| 2569 | return FALSE; |
| 2570 | for (guint i = 0; i < files->len; i++) { |
| 2571 | const gchar *fn = g_ptr_array_index (files, i); |
| 2572 | g_print ("%s\n", fn); |
| 2573 | } |
| 2574 | return TRUE; |
| 2575 | } |
| 2576 | |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2577 | static gboolean |
| 2578 | _g_str_equal0 (gconstpointer str1, gconstpointer str2) |
| 2579 | { |
| 2580 | return g_strcmp0 (str1, str2) == 0; |
| 2581 | } |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2582 | |
| 2583 | static gboolean |
| 2584 | fu_util_switch_branch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 2585 | { |
| 2586 | const gchar *branch; |
| 2587 | g_autoptr(FwupdRelease) rel = NULL; |
| 2588 | g_autoptr(GPtrArray) rels = NULL; |
| 2589 | g_autoptr(GPtrArray) branches = g_ptr_array_new_with_free_func (g_free); |
| 2590 | g_autoptr(FuDevice) dev = NULL; |
| 2591 | |
| 2592 | /* load engine */ |
Richard Hughes | c7d870a | 2020-12-10 10:05:35 +0000 | [diff] [blame] | 2593 | if (!fu_util_start_engine (priv, |
| 2594 | FU_ENGINE_LOAD_FLAG_COLDPLUG | |
| 2595 | FU_ENGINE_LOAD_FLAG_HWINFO | |
| 2596 | FU_ENGINE_LOAD_FLAG_REMOTES, |
| 2597 | error)) |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2598 | return FALSE; |
| 2599 | |
| 2600 | /* find the device and check it has multiple branches */ |
Richard Hughes | cb0e614 | 2020-10-22 16:31:56 +0100 | [diff] [blame] | 2601 | priv->filter_include |= FWUPD_DEVICE_FLAG_HAS_MULTIPLE_BRANCHES; |
Richard Hughes | 814a5ee | 2021-01-20 16:25:43 +0000 | [diff] [blame] | 2602 | priv->filter_include |= FWUPD_DEVICE_FLAG_UPDATABLE; |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2603 | if (g_strv_length (values) == 1) |
| 2604 | dev = fu_util_get_device (priv, values[1], error); |
| 2605 | else |
| 2606 | dev = fu_util_prompt_for_device (priv, NULL, error); |
| 2607 | if (dev == NULL) |
| 2608 | return FALSE; |
| 2609 | if (!fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_HAS_MULTIPLE_BRANCHES)) { |
| 2610 | g_set_error_literal (error, |
| 2611 | FWUPD_ERROR, |
| 2612 | FWUPD_ERROR_NOT_SUPPORTED, |
| 2613 | "Multiple branches not available"); |
| 2614 | return FALSE; |
| 2615 | } |
| 2616 | |
| 2617 | /* get all releases, including the alternate branch versions */ |
| 2618 | rels = fu_engine_get_releases (priv->engine, |
| 2619 | priv->request, |
| 2620 | fu_device_get_id (dev), |
| 2621 | error); |
| 2622 | if (rels == NULL) |
| 2623 | return FALSE; |
| 2624 | |
| 2625 | /* get all the unique branches */ |
| 2626 | for (guint i = 0; i < rels->len; i++) { |
| 2627 | FwupdRelease *rel_tmp = g_ptr_array_index (rels, i); |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2628 | const gchar *branch_tmp = fwupd_release_get_branch (rel_tmp); |
Richard Hughes | db8533f | 2020-12-14 12:04:30 +0000 | [diff] [blame] | 2629 | #if GLIB_CHECK_VERSION(2,54,3) |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2630 | if (g_ptr_array_find_with_equal_func (branches, branch_tmp, |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2631 | _g_str_equal0, NULL)) |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2632 | continue; |
Richard Hughes | db8533f | 2020-12-14 12:04:30 +0000 | [diff] [blame] | 2633 | #endif |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2634 | g_ptr_array_add (branches, g_strdup (branch_tmp)); |
| 2635 | } |
| 2636 | |
| 2637 | /* branch name is optional */ |
| 2638 | if (g_strv_length (values) > 1) { |
| 2639 | branch = values[1]; |
| 2640 | } else if (branches->len == 1) { |
| 2641 | branch = g_ptr_array_index (branches, 0); |
| 2642 | } else { |
| 2643 | guint idx; |
| 2644 | |
| 2645 | /* TRANSLATORS: get interactive prompt, where branch is the |
| 2646 | * supplier of the firmware, e.g. "non-free" or "free" */ |
| 2647 | g_print ("%s\n", _("Choose a branch:")); |
| 2648 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 2649 | g_print ("0.\t%s\n", _("Cancel")); |
| 2650 | for (guint i = 0; i < branches->len; i++) { |
| 2651 | const gchar *branch_tmp = g_ptr_array_index (branches, i); |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2652 | g_print ("%u.\t%s\n", i + 1, fu_util_branch_for_display (branch_tmp)); |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2653 | } |
| 2654 | idx = fu_util_prompt_for_number (branches->len); |
| 2655 | if (idx == 0) { |
| 2656 | g_set_error_literal (error, |
| 2657 | FWUPD_ERROR, |
| 2658 | FWUPD_ERROR_NOTHING_TO_DO, |
| 2659 | "Request canceled"); |
| 2660 | return FALSE; |
| 2661 | } |
| 2662 | branch = g_ptr_array_index (branches, idx - 1); |
| 2663 | } |
| 2664 | |
| 2665 | /* sanity check */ |
| 2666 | if (g_strcmp0 (branch, fu_device_get_branch (dev)) == 0) { |
| 2667 | g_set_error (error, |
| 2668 | FWUPD_ERROR, |
| 2669 | FWUPD_ERROR_NOT_SUPPORTED, |
| 2670 | "Device %s is already on branch %s", |
| 2671 | fu_device_get_name (dev), |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2672 | fu_util_branch_for_display (branch)); |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2673 | return FALSE; |
| 2674 | } |
| 2675 | |
| 2676 | /* the releases are ordered by version */ |
| 2677 | for (guint j = 0; j < rels->len; j++) { |
| 2678 | FwupdRelease *rel_tmp = g_ptr_array_index (rels, j); |
| 2679 | if (g_strcmp0 (fwupd_release_get_branch (rel_tmp), branch) == 0) { |
| 2680 | rel = g_object_ref (rel_tmp); |
| 2681 | break; |
| 2682 | } |
| 2683 | } |
| 2684 | if (rel == NULL) { |
| 2685 | g_set_error (error, |
| 2686 | FWUPD_ERROR, |
| 2687 | FWUPD_ERROR_NOT_SUPPORTED, |
| 2688 | "No releases for branch %s", |
Richard Hughes | 89ee9ed | 2021-01-20 16:15:29 +0000 | [diff] [blame] | 2689 | fu_util_branch_for_display (branch)); |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 2690 | return FALSE; |
| 2691 | } |
| 2692 | |
| 2693 | /* we're switching branch */ |
| 2694 | if (!fu_util_switch_branch_warning (FWUPD_DEVICE (dev), rel, FALSE, error)) |
| 2695 | return FALSE; |
| 2696 | |
| 2697 | /* update the console if composite devices are also updated */ |
| 2698 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
| 2699 | g_signal_connect (priv->engine, "device-changed", |
| 2700 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 2701 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 2702 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH; |
| 2703 | if (!fu_util_install_release (priv, rel, error)) |
| 2704 | return FALSE; |
| 2705 | fu_util_display_current_message (priv); |
| 2706 | |
| 2707 | /* we don't want to ask anything */ |
| 2708 | if (priv->no_reboot_check) { |
| 2709 | g_debug ("skipping reboot check"); |
| 2710 | return TRUE; |
| 2711 | } |
| 2712 | |
| 2713 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
| 2714 | } |
| 2715 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2716 | int |
| 2717 | main (int argc, char *argv[]) |
| 2718 | { |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 2719 | gboolean allow_branch_switch = FALSE; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 2720 | gboolean allow_older = FALSE; |
| 2721 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2722 | gboolean force = FALSE; |
| 2723 | gboolean ret; |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 2724 | gboolean version = FALSE; |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 2725 | gboolean ignore_checksum = FALSE; |
| 2726 | gboolean ignore_power = FALSE; |
| 2727 | gboolean ignore_vid_pid = FALSE; |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 2728 | gboolean interactive = isatty (fileno (stdout)) != 0; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 2729 | g_auto(GStrv) plugin_glob = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2730 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 2731 | g_autoptr(GError) error = NULL; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2732 | g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new (); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2733 | g_autofree gchar *cmd_descriptions = NULL; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 2734 | g_autofree gchar *filter = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2735 | const GOptionEntry options[] = { |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 2736 | { "version", '\0', 0, G_OPTION_ARG_NONE, &version, |
| 2737 | /* TRANSLATORS: command line option */ |
| 2738 | _("Show client and daemon versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 2739 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 2740 | /* TRANSLATORS: command line option */ |
Mario Limonciello | 350fc4c | 2019-10-03 07:08:51 -0500 | [diff] [blame] | 2741 | _("Allow reinstalling existing firmware versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 2742 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 2743 | /* TRANSLATORS: command line option */ |
| 2744 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 2745 | { "allow-branch-switch", '\0', 0, G_OPTION_ARG_NONE, &allow_branch_switch, |
| 2746 | /* TRANSLATORS: command line option */ |
| 2747 | _("Allow switching firmware branch"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2748 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 2749 | /* TRANSLATORS: command line option */ |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 2750 | _("Force the action by relaxing some runtime checks"), NULL }, |
| 2751 | { "ignore-checksum", '\0', 0, G_OPTION_ARG_NONE, &ignore_checksum, |
| 2752 | /* TRANSLATORS: command line option */ |
| 2753 | _("Ignore firmware checksum failures"), NULL }, |
| 2754 | { "ignore-vid-pid", '\0', 0, G_OPTION_ARG_NONE, &ignore_vid_pid, |
| 2755 | /* TRANSLATORS: command line option */ |
| 2756 | _("Ignore firmware hardware mismatch failures"), NULL }, |
| 2757 | { "ignore-power", '\0', 0, G_OPTION_ARG_NONE, &ignore_power, |
| 2758 | /* TRANSLATORS: command line option */ |
| 2759 | _("Ignore requirement of external power source"), NULL }, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 2760 | { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check, |
| 2761 | /* TRANSLATORS: command line option */ |
| 2762 | _("Do not check for reboot after update"), NULL }, |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 2763 | { "no-safety-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_safety_check, |
| 2764 | /* TRANSLATORS: command line option */ |
| 2765 | _("Do not perform device safety checks"), NULL }, |
Richard Hughes | 5c82b94 | 2020-09-14 12:24:06 +0100 | [diff] [blame] | 2766 | { "show-all", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all, |
| 2767 | /* TRANSLATORS: command line option */ |
| 2768 | _("Show all results"), NULL }, |
| 2769 | { "show-all-devices", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &priv->show_all, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 2770 | /* TRANSLATORS: command line option */ |
| 2771 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | f8c10c2 | 2020-07-20 21:01:39 +0100 | [diff] [blame] | 2772 | { "plugins", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 2773 | /* TRANSLATORS: command line option */ |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 2774 | _("Manually enable specific plugins"), NULL }, |
Richard Hughes | a1ebcbf | 2020-09-14 15:27:43 +0100 | [diff] [blame] | 2775 | { "plugin-whitelist", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
| 2776 | /* TRANSLATORS: command line option */ |
| 2777 | _("Manually enable specific plugins"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 2778 | { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 2779 | /* TRANSLATORS: command line option */ |
| 2780 | _("Run the plugin composite prepare routine when using install-blob"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 2781 | { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 2782 | /* TRANSLATORS: command line option */ |
| 2783 | _("Run the plugin composite cleanup routine when using install-blob"), NULL }, |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 2784 | { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state, |
| 2785 | /* TRANSLATORS: command line option */ |
| 2786 | _("Save device state into a JSON file between executions"), NULL }, |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 2787 | { "disable-ssl-strict", '\0', 0, G_OPTION_ARG_NONE, &priv->disable_ssl_strict, |
| 2788 | /* TRANSLATORS: command line option */ |
| 2789 | _("Ignore SSL strict checks when downloading files"), NULL }, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 2790 | { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter, |
| 2791 | /* TRANSLATORS: command line option */ |
| 2792 | _("Filter with a set of device flags using a ~ prefix to " |
| 2793 | "exclude, e.g. 'internal,~needs-reboot'"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2794 | { NULL} |
| 2795 | }; |
| 2796 | |
Richard Hughes | 429f72b | 2020-01-16 12:18:19 +0000 | [diff] [blame] | 2797 | #ifdef _WIN32 |
| 2798 | /* workaround Windows setting the codepage to 1252 */ |
| 2799 | g_setenv ("LANG", "C.UTF-8", FALSE); |
| 2800 | #endif |
| 2801 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2802 | setlocale (LC_ALL, ""); |
| 2803 | |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 2804 | bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2805 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 2806 | textdomain (GETTEXT_PACKAGE); |
| 2807 | |
Richard Hughes | 01c0bad | 2019-11-22 09:08:51 +0000 | [diff] [blame] | 2808 | #ifdef HAVE_GETUID |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2809 | /* ensure root user */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 2810 | if (interactive && (getuid () != 0 || geteuid () != 0)) |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2811 | /* TRANSLATORS: we're poking around as a power user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 2812 | g_printerr ("%s\n", _("This program may only work correctly as root")); |
Richard Hughes | 01c0bad | 2019-11-22 09:08:51 +0000 | [diff] [blame] | 2813 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2814 | |
| 2815 | /* create helper object */ |
Richard Hughes | 6ed25f5 | 2021-01-10 19:27:33 +0000 | [diff] [blame] | 2816 | priv->main_ctx = g_main_context_new (); |
| 2817 | priv->loop = g_main_loop_new (priv->main_ctx, FALSE); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2818 | priv->progressbar = fu_progressbar_new (); |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 2819 | priv->request = fu_engine_request_new (); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* add commands */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2822 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 2823 | "build-firmware", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2824 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2825 | _("FILE-IN FILE-OUT [SCRIPT] [OUTPUT]"), |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 2826 | /* TRANSLATORS: command description */ |
| 2827 | _("Build firmware using a sandbox"), |
| 2828 | fu_util_firmware_builder); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2829 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2830 | "smbios-dump", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2831 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2832 | _("FILE"), |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2833 | /* TRANSLATORS: command description */ |
| 2834 | _("Dump SMBIOS data from a file"), |
| 2835 | fu_util_smbios_dump); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2836 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 2837 | "get-plugins", |
| 2838 | NULL, |
| 2839 | /* TRANSLATORS: command description */ |
| 2840 | _("Get all enabled plugins registered with the system"), |
| 2841 | fu_util_get_plugins); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2842 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 2843 | "get-details", |
| 2844 | NULL, |
| 2845 | /* TRANSLATORS: command description */ |
| 2846 | _("Gets details about a firmware file"), |
| 2847 | fu_util_get_details); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2848 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 2849 | "get-history", |
| 2850 | NULL, |
| 2851 | /* TRANSLATORS: command description */ |
| 2852 | _("Show history of firmware updates"), |
| 2853 | fu_util_get_history); |
| 2854 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 2855 | "get-updates,get-upgrades", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2856 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2857 | _("[DEVICE-ID|GUID]"), |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 2858 | /* TRANSLATORS: command description */ |
| 2859 | _("Gets the list of updates for connected hardware"), |
| 2860 | fu_util_get_updates); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2861 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 2862 | "get-devices,get-topology", |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2863 | NULL, |
| 2864 | /* TRANSLATORS: command description */ |
| 2865 | _("Get all devices that support firmware updates"), |
| 2866 | fu_util_get_devices); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2867 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 2868 | "get-device-flags", |
| 2869 | NULL, |
| 2870 | /* TRANSLATORS: command description */ |
| 2871 | _("Get all device flags supported by fwupd"), |
| 2872 | fu_util_get_device_flags); |
| 2873 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2874 | "watch", |
| 2875 | NULL, |
| 2876 | /* TRANSLATORS: command description */ |
Piotr Drąg | 472fa59 | 2018-06-06 14:53:48 +0200 | [diff] [blame] | 2877 | _("Watch for hardware changes"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2878 | fu_util_watch); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2879 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2880 | "install-blob", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2881 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2882 | _("FILENAME DEVICE-ID"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2883 | /* TRANSLATORS: command description */ |
| 2884 | _("Install a firmware blob on a device"), |
| 2885 | fu_util_install_blob); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2886 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 2887 | "install", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2888 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2889 | _("FILE [DEVICE-ID|GUID]"), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 2890 | /* TRANSLATORS: command description */ |
| 2891 | _("Install a firmware file on this hardware"), |
| 2892 | fu_util_install); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2893 | fu_util_cmd_array_add (cmd_array, |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 2894 | "reinstall", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2895 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2896 | _("DEVICE-ID|GUID"), |
Filipe Laíns | b2f377a | 2020-03-30 21:05:50 +0100 | [diff] [blame] | 2897 | /* TRANSLATORS: command description */ |
| 2898 | _("Reinstall firmware on a device"), |
| 2899 | fu_util_reinstall); |
| 2900 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2901 | "attach", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2902 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2903 | _("DEVICE-ID|GUID"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2904 | /* TRANSLATORS: command description */ |
| 2905 | _("Attach to firmware mode"), |
| 2906 | fu_util_attach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2907 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2908 | "detach", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2909 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2910 | _("DEVICE-ID|GUID"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 2911 | /* TRANSLATORS: command description */ |
| 2912 | _("Detach to bootloader mode"), |
| 2913 | fu_util_detach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2914 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 2915 | "unbind-driver", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2916 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2917 | _("[DEVICE-ID|GUID]"), |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 2918 | /* TRANSLATORS: command description */ |
| 2919 | _("Unbind current driver"), |
| 2920 | fu_util_unbind_driver); |
| 2921 | fu_util_cmd_array_add (cmd_array, |
| 2922 | "bind-driver", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2923 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2924 | _("SUBSYSTEM DRIVER [DEVICE-ID|GUID]"), |
Richard Hughes | 34f7d9d | 2020-09-19 15:28:11 +0100 | [diff] [blame] | 2925 | /* TRANSLATORS: command description */ |
| 2926 | _("Bind new kernel driver"), |
| 2927 | fu_util_bind_driver); |
| 2928 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 2929 | "activate", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2930 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2931 | _("[DEVICE-ID|GUID]"), |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 2932 | /* TRANSLATORS: command description */ |
| 2933 | _("Activate pending devices"), |
| 2934 | fu_util_activate); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2935 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 2936 | "hwids", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2937 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 2938 | _("[SMBIOS-FILE|HWIDS-FILE]"), |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 2939 | /* TRANSLATORS: command description */ |
| 2940 | _("Return all the hardware IDs for the machine"), |
| 2941 | fu_util_hwids); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2942 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | f6751cd | 2021-01-15 16:48:25 +0000 | [diff] [blame] | 2943 | "export-hwids", |
| 2944 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2945 | _("HWIDS-FILE"), |
| 2946 | /* TRANSLATORS: command description */ |
| 2947 | _("Save a file that allows generation of hardware IDs"), |
| 2948 | fu_util_export_hwids); |
| 2949 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 2950 | "monitor", |
| 2951 | NULL, |
| 2952 | /* TRANSLATORS: command description */ |
| 2953 | _("Monitor the daemon for events"), |
| 2954 | fu_util_monitor); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 2955 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 2956 | "update,upgrade", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2957 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2958 | _("[DEVICE-ID|GUID]"), |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 2959 | /* TRANSLATORS: command description */ |
| 2960 | _("Update all devices that match local metadata"), |
| 2961 | fu_util_update); |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 2962 | fu_util_cmd_array_add (cmd_array, |
| 2963 | "self-sign", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2964 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2965 | _("TEXT"), |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 2966 | /* TRANSLATORS: command description */ |
Richard Hughes | 12a021d | 2019-03-27 09:23:24 +0000 | [diff] [blame] | 2967 | C_("command-description", |
| 2968 | "Sign data using the client certificate"), |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 2969 | fu_util_self_sign); |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2970 | fu_util_cmd_array_add (cmd_array, |
| 2971 | "verify-update", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2972 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2973 | _("[DEVICE-ID|GUID]"), |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 2974 | /* TRANSLATORS: command description */ |
| 2975 | _("Update the stored metadata with current contents"), |
| 2976 | fu_util_verify_update); |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 2977 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | fbd8b5d | 2020-09-24 11:48:59 +0100 | [diff] [blame] | 2978 | "firmware-dump", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2979 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2980 | _("FILENAME [DEVICE-ID|GUID]"), |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2981 | /* TRANSLATORS: command description */ |
| 2982 | _("Read a firmware blob from a device"), |
Richard Hughes | fbd8b5d | 2020-09-24 11:48:59 +0100 | [diff] [blame] | 2983 | fu_util_firmware_dump); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 2984 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2985 | "firmware-convert", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2986 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2987 | _("FILENAME-SRC FILENAME-DST [FIRMWARE-TYPE-SRC] [FIRMWARE-TYPE-DST]"), |
Richard Hughes | bca63ed | 2020-03-09 20:20:03 +0000 | [diff] [blame] | 2988 | /* TRANSLATORS: command description */ |
| 2989 | _("Convert a firmware file"), |
| 2990 | fu_util_firmware_convert); |
| 2991 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2992 | "firmware-build", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 2993 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 2994 | _("BUILDER-XML FILENAME-DST"), |
Richard Hughes | 0924c93 | 2020-09-22 19:07:05 +0100 | [diff] [blame] | 2995 | /* TRANSLATORS: command description */ |
| 2996 | _("Build a firmware file"), |
| 2997 | fu_util_firmware_build); |
| 2998 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 2999 | "firmware-parse", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 3000 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 3001 | _("FILENAME [FIRMWARE-TYPE]"), |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 3002 | /* TRANSLATORS: command description */ |
| 3003 | _("Parse and show details about a firmware file"), |
| 3004 | fu_util_firmware_parse); |
| 3005 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 3006 | "firmware-extract", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 3007 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 3008 | _("FILENAME [FIRMWARE-TYPE]"), |
Richard Hughes | dd65344 | 2020-09-22 10:23:52 +0100 | [diff] [blame] | 3009 | /* TRANSLATORS: command description */ |
| 3010 | _("Extract a firmware blob to images"), |
| 3011 | fu_util_firmware_extract); |
| 3012 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 3013 | "get-firmware-types", |
| 3014 | NULL, |
| 3015 | /* TRANSLATORS: command description */ |
| 3016 | _("List the available firmware types"), |
| 3017 | fu_util_get_firmware_types); |
Richard Hughes | fd7e994 | 2020-01-17 14:09:13 +0000 | [diff] [blame] | 3018 | fu_util_cmd_array_add (cmd_array, |
| 3019 | "get-remotes", |
| 3020 | NULL, |
| 3021 | /* TRANSLATORS: command description */ |
| 3022 | _("Gets the configured remotes"), |
| 3023 | fu_util_get_remotes); |
Richard Hughes | 4959baa | 2020-01-17 14:33:00 +0000 | [diff] [blame] | 3024 | fu_util_cmd_array_add (cmd_array, |
| 3025 | "refresh", |
| 3026 | NULL, |
| 3027 | /* TRANSLATORS: command description */ |
| 3028 | _("Refresh metadata from remote server"), |
| 3029 | fu_util_refresh); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 3030 | fu_util_cmd_array_add (cmd_array, |
| 3031 | "security", |
| 3032 | NULL, |
| 3033 | /* TRANSLATORS: command description */ |
Richard Hughes | 43053d2 | 2020-10-18 19:49:25 +0100 | [diff] [blame] | 3034 | _("Gets the host security attributes"), |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 3035 | fu_util_security); |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 3036 | fu_util_cmd_array_add (cmd_array, |
| 3037 | "esp-mount", |
| 3038 | NULL, |
| 3039 | /* TRANSLATORS: command description */ |
Richard Hughes | 43053d2 | 2020-10-18 19:49:25 +0100 | [diff] [blame] | 3040 | _("Mounts the ESP"), |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 3041 | fu_util_esp_mount); |
| 3042 | fu_util_cmd_array_add (cmd_array, |
| 3043 | "esp-unmount", |
| 3044 | NULL, |
| 3045 | /* TRANSLATORS: command description */ |
Richard Hughes | 43053d2 | 2020-10-18 19:49:25 +0100 | [diff] [blame] | 3046 | _("Unmounts the ESP"), |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 3047 | fu_util_esp_unmount); |
| 3048 | fu_util_cmd_array_add (cmd_array, |
| 3049 | "esp-list", |
| 3050 | NULL, |
| 3051 | /* TRANSLATORS: command description */ |
Richard Hughes | 43053d2 | 2020-10-18 19:49:25 +0100 | [diff] [blame] | 3052 | _("Lists files on the ESP"), |
Richard Hughes | a83deb4 | 2020-08-12 12:45:36 +0100 | [diff] [blame] | 3053 | fu_util_esp_list); |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 3054 | fu_util_cmd_array_add (cmd_array, |
| 3055 | "switch-branch", |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 3056 | /* TRANSLATORS: command argument: uppercase, spaces->dashes */ |
| 3057 | _("[DEVICE-ID|GUID] [BRANCH]"), |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 3058 | /* TRANSLATORS: command description */ |
| 3059 | _("Switch the firmware branch on the device"), |
| 3060 | fu_util_switch_branch); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3061 | |
| 3062 | /* do stuff on ctrl+c */ |
| 3063 | priv->cancellable = g_cancellable_new (); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 3064 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3065 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 3066 | SIGINT, fu_util_sigint_cb, |
| 3067 | priv, NULL); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 3068 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3069 | g_signal_connect (priv->cancellable, "cancelled", |
| 3070 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 3071 | |
| 3072 | /* sort by command name */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 3073 | fu_util_cmd_array_sort (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3074 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 3075 | /* non-TTY consoles cannot answer questions */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 3076 | if (!interactive) { |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 3077 | priv->no_reboot_check = TRUE; |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 3078 | priv->no_safety_check = TRUE; |
Mario Limonciello | 9b31e6f | 2019-01-31 15:42:19 -0600 | [diff] [blame] | 3079 | fu_progressbar_set_interactive (priv->progressbar, FALSE); |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3080 | } else { |
| 3081 | /* set our implemented feature set */ |
| 3082 | fu_engine_request_set_feature_flags (priv->request, |
| 3083 | FWUPD_FEATURE_FLAG_DETACH_ACTION | |
Mario Limonciello | 02f2cc3 | 2020-10-20 15:39:47 -0500 | [diff] [blame] | 3084 | FWUPD_FEATURE_FLAG_SWITCH_BRANCH | |
Richard Hughes | df89cd5 | 2020-06-26 20:25:18 +0100 | [diff] [blame] | 3085 | FWUPD_FEATURE_FLAG_UPDATE_ACTION); |
Mario Limonciello | 9b31e6f | 2019-01-31 15:42:19 -0600 | [diff] [blame] | 3086 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 3087 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3088 | /* get a list of the commands */ |
| 3089 | priv->context = g_option_context_new (NULL); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 3090 | cmd_descriptions = fu_util_cmd_array_to_string (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3091 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 3092 | g_option_context_set_description (priv->context, |
Richard Hughes | c1e5f94 | 2020-11-25 14:33:46 +0000 | [diff] [blame] | 3093 | /* TRANSLATORS: CLI description */ |
| 3094 | _("This tool allows an administrator to use the fwupd plugins " |
| 3095 | "without being installed on the host system.")); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3096 | |
| 3097 | /* TRANSLATORS: program name */ |
| 3098 | g_set_application_name (_("Firmware Utility")); |
| 3099 | g_option_context_add_main_entries (priv->context, options, NULL); |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 3100 | g_option_context_add_group (priv->context, fu_debug_get_option_group ()); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3101 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 3102 | if (!ret) { |
| 3103 | /* TRANSLATORS: the user didn't read the man page */ |
| 3104 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 3105 | error->message); |
| 3106 | return EXIT_FAILURE; |
| 3107 | } |
| 3108 | |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 3109 | /* allow disabling SSL strict mode for broken corporate proxies */ |
| 3110 | if (priv->disable_ssl_strict) { |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 3111 | g_autofree gchar *fmt = NULL; |
| 3112 | /* TRANSLATORS: this is a prefix on the console */ |
| 3113 | fmt = fu_util_term_format (_("WARNING:"), FU_UTIL_TERM_COLOR_RED); |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 3114 | /* TRANSLATORS: try to help */ |
Richard Hughes | 7bcb8d4 | 2020-10-08 15:47:47 +0100 | [diff] [blame] | 3115 | g_printerr ("%s %s\n", fmt, _("Ignoring SSL strict checks, " |
| 3116 | "to do this automatically in the future " |
| 3117 | "export DISABLE_SSL_STRICT in your environment")); |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 3118 | g_setenv ("DISABLE_SSL_STRICT", "1", TRUE); |
| 3119 | } |
| 3120 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 3121 | /* parse filter flags */ |
| 3122 | if (filter != NULL) { |
| 3123 | if (!fu_util_parse_filter_flags (filter, |
| 3124 | &priv->filter_include, |
| 3125 | &priv->filter_exclude, |
| 3126 | &error)) { |
| 3127 | /* TRANSLATORS: the user didn't read the man page */ |
| 3128 | g_print ("%s: %s\n", _("Failed to parse flags for --filter"), |
| 3129 | error->message); |
| 3130 | return EXIT_FAILURE; |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 3135 | /* set flags */ |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 3136 | if (allow_reinstall) |
| 3137 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 3138 | if (allow_older) |
| 3139 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 3140 | if (allow_branch_switch) |
| 3141 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_BRANCH_SWITCH; |
| 3142 | if (force) { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 3143 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 6450d0d | 2020-10-06 16:05:24 +0100 | [diff] [blame] | 3144 | priv->flags |= FWUPD_INSTALL_FLAG_IGNORE_POWER; |
| 3145 | } |
| 3146 | if (ignore_checksum) |
| 3147 | priv->flags |= FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM; |
| 3148 | if (ignore_vid_pid) |
| 3149 | priv->flags |= FWUPD_INSTALL_FLAG_IGNORE_VID_PID; |
| 3150 | if (ignore_power) |
| 3151 | priv->flags |= FWUPD_INSTALL_FLAG_IGNORE_POWER; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 3152 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 3153 | /* load engine */ |
| 3154 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 3155 | g_signal_connect (priv->engine, "device-added", |
| 3156 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 3157 | priv); |
| 3158 | g_signal_connect (priv->engine, "device-removed", |
| 3159 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 3160 | priv); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 3161 | g_signal_connect (priv->engine, "status-changed", |
| 3162 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 3163 | priv); |
| 3164 | g_signal_connect (priv->engine, "percentage-changed", |
| 3165 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 3166 | priv); |
| 3167 | |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 3168 | /* just show versions and exit */ |
| 3169 | if (version) { |
| 3170 | g_autofree gchar *version_str = fu_util_get_versions (); |
| 3171 | g_print ("%s\n", version_str); |
| 3172 | return EXIT_SUCCESS; |
| 3173 | } |
| 3174 | |
Richard Hughes | 85226fd | 2020-06-30 14:43:48 +0100 | [diff] [blame] | 3175 | /* any plugin allowlist specified */ |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 3176 | for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++) |
| 3177 | fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]); |
| 3178 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3179 | /* run the specified command */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 3180 | ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3181 | if (!ret) { |
Mario Limonciello | 8909631 | 2020-04-30 09:51:14 -0500 | [diff] [blame] | 3182 | g_printerr ("%s\n", error->message); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3183 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
Mario Limonciello | 8909631 | 2020-04-30 09:51:14 -0500 | [diff] [blame] | 3184 | /* TRANSLATORS: error message explaining command to run to how to get help */ |
| 3185 | g_printerr ("\n%s\n", _("Use fwupdtool --help for help")); |
| 3186 | } else if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 3187 | g_debug ("%s\n", error->message); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3188 | return EXIT_NOTHING_TO_DO; |
| 3189 | } |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 3190 | return EXIT_FAILURE; |
| 3191 | } |
| 3192 | |
| 3193 | /* success */ |
| 3194 | return EXIT_SUCCESS; |
| 3195 | } |