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