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