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