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 | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 22 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 23 | #include "fu-device-private.h" |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 24 | #include "fu-engine.h" |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 25 | #include "fu-history.h" |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 26 | #include "fu-plugin-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 27 | #include "fu-progressbar.h" |
Mario Limonciello | 6b0e663 | 2019-11-22 13:04:32 -0600 | [diff] [blame] | 28 | #include "fu-smbios-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 29 | #include "fu-util-common.h" |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 30 | #include "fu-debug.h" |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 31 | #include "fwupd-common-private.h" |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 32 | #include "fwupd-device-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 33 | |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 34 | #ifdef HAVE_SYSTEMD |
| 35 | #include "fu-systemd.h" |
| 36 | #endif |
| 37 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 38 | /* custom return code */ |
| 39 | #define EXIT_NOTHING_TO_DO 2 |
| 40 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 41 | typedef enum { |
| 42 | FU_UTIL_OPERATION_UNKNOWN, |
| 43 | FU_UTIL_OPERATION_UPDATE, |
| 44 | FU_UTIL_OPERATION_INSTALL, |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 45 | FU_UTIL_OPERATION_READ, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 46 | FU_UTIL_OPERATION_LAST |
| 47 | } FuUtilOperation; |
| 48 | |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 49 | struct FuUtilPrivate { |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 50 | GCancellable *cancellable; |
| 51 | GMainLoop *loop; |
| 52 | GOptionContext *context; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 53 | FuEngine *engine; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 54 | FuProgressbar *progressbar; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 55 | gboolean no_reboot_check; |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 56 | gboolean no_safety_check; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 57 | gboolean prepare_blob; |
| 58 | gboolean cleanup_blob; |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 59 | gboolean enable_json_state; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 60 | FwupdInstallFlags flags; |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 61 | gboolean show_all_devices; |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 62 | gboolean disable_ssl_strict; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 63 | /* only valid in update and downgrade */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 64 | FuUtilOperation current_operation; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 65 | FwupdDevice *current_device; |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 66 | gchar *current_message; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 67 | FwupdDeviceFlags completion_flags; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 68 | FwupdDeviceFlags filter_include; |
| 69 | FwupdDeviceFlags filter_exclude; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 70 | }; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 71 | |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 72 | static gboolean |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 73 | fu_util_save_current_state (FuUtilPrivate *priv, GError **error) |
| 74 | { |
| 75 | g_autoptr(JsonBuilder) builder = NULL; |
| 76 | g_autoptr(JsonGenerator) json_generator = NULL; |
| 77 | g_autoptr(JsonNode) json_root = NULL; |
| 78 | g_autoptr(GPtrArray) devices = NULL; |
| 79 | g_autofree gchar *state = NULL; |
| 80 | g_autofree gchar *dirname = NULL; |
| 81 | g_autofree gchar *filename = NULL; |
| 82 | |
| 83 | if (!priv->enable_json_state) |
| 84 | return TRUE; |
| 85 | |
| 86 | devices = fu_engine_get_devices (priv->engine, error); |
| 87 | if (devices == NULL) |
| 88 | return FALSE; |
| 89 | |
| 90 | /* create header */ |
| 91 | builder = json_builder_new (); |
| 92 | json_builder_begin_object (builder); |
| 93 | |
| 94 | /* add each device */ |
| 95 | json_builder_set_member_name (builder, "Devices"); |
| 96 | json_builder_begin_array (builder); |
| 97 | for (guint i = 0; i < devices->len; i++) { |
| 98 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 99 | json_builder_begin_object (builder); |
| 100 | fwupd_device_to_json (dev, builder); |
| 101 | json_builder_end_object (builder); |
| 102 | } |
| 103 | json_builder_end_array (builder); |
| 104 | json_builder_end_object (builder); |
| 105 | |
| 106 | /* export as a string */ |
| 107 | json_root = json_builder_get_root (builder); |
| 108 | json_generator = json_generator_new (); |
| 109 | json_generator_set_pretty (json_generator, TRUE); |
| 110 | json_generator_set_root (json_generator, json_root); |
| 111 | state = json_generator_to_data (json_generator, NULL); |
| 112 | if (state == NULL) |
| 113 | return FALSE; |
| 114 | dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 115 | filename = g_build_filename (dirname, "state.json", NULL); |
| 116 | return g_file_set_contents (filename, state, -1, error); |
| 117 | } |
| 118 | |
| 119 | static gboolean |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 120 | fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error) |
Mario Limonciello | e8dd4d7 | 2019-02-26 15:28:04 -0600 | [diff] [blame] | 121 | { |
| 122 | g_autoptr(GError) error_local = NULL; |
| 123 | |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 124 | #ifdef HAVE_SYSTEMD |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 125 | if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local)) |
Mario Limonciello | 8692d01 | 2019-10-12 18:01:55 -0500 | [diff] [blame] | 126 | g_debug ("Failed to stop daemon: %s", error_local->message); |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 127 | #endif |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 128 | if (!fu_engine_load (priv->engine, flags, error)) |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 129 | return FALSE; |
| 130 | if (fu_engine_get_tainted (priv->engine)) { |
| 131 | g_printerr ("WARNING: This tool has loaded 3rd party code and " |
| 132 | "is no longer supported by the upstream developers!\n"); |
| 133 | } |
| 134 | return TRUE; |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 137 | static void |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 138 | fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error) |
| 139 | { |
| 140 | g_autofree gchar *path = g_path_get_dirname (value); |
| 141 | if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { |
| 142 | g_prefix_error (error, |
| 143 | "Unable to access %s. You may need to copy %s to %s: ", |
| 144 | path, value, g_getenv ("HOME")); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 149 | fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data) |
| 150 | { |
| 151 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 152 | /* TRANSLATORS: this is when a device ctrl+c's a watch */ |
| 153 | g_print ("%s\n", _("Cancelled")); |
| 154 | g_main_loop_quit (priv->loop); |
| 155 | } |
| 156 | |
| 157 | static gboolean |
| 158 | fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
| 159 | { |
| 160 | g_autofree gchar *tmp = NULL; |
| 161 | g_autoptr(FuSmbios) smbios = NULL; |
| 162 | if (g_strv_length (values) < 1) { |
| 163 | g_set_error_literal (error, |
| 164 | FWUPD_ERROR, |
| 165 | FWUPD_ERROR_INVALID_ARGS, |
| 166 | "Invalid arguments"); |
| 167 | return FALSE; |
| 168 | } |
| 169 | smbios = fu_smbios_new (); |
| 170 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 171 | return FALSE; |
| 172 | tmp = fu_smbios_to_string (smbios); |
| 173 | g_print ("%s\n", tmp); |
| 174 | return TRUE; |
| 175 | } |
| 176 | |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 177 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 178 | static gboolean |
| 179 | fu_util_sigint_cb (gpointer user_data) |
| 180 | { |
| 181 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 182 | g_debug ("Handling SIGINT"); |
| 183 | g_cancellable_cancel (priv->cancellable); |
| 184 | return FALSE; |
| 185 | } |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 186 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 187 | |
| 188 | static void |
| 189 | fu_util_private_free (FuUtilPrivate *priv) |
| 190 | { |
Mario Limonciello | cc50e1a | 2018-08-14 17:45:24 -0500 | [diff] [blame] | 191 | if (priv->current_device != NULL) |
| 192 | g_object_unref (priv->current_device); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 193 | if (priv->engine != NULL) |
| 194 | g_object_unref (priv->engine); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 195 | if (priv->loop != NULL) |
| 196 | g_main_loop_unref (priv->loop); |
| 197 | if (priv->cancellable != NULL) |
| 198 | g_object_unref (priv->cancellable); |
| 199 | if (priv->progressbar != NULL) |
| 200 | g_object_unref (priv->progressbar); |
| 201 | if (priv->context != NULL) |
| 202 | g_option_context_free (priv->context); |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 203 | g_free (priv->current_message); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 204 | g_free (priv); |
| 205 | } |
| 206 | |
| 207 | #pragma clang diagnostic push |
| 208 | #pragma clang diagnostic ignored "-Wunused-function" |
| 209 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free) |
| 210 | #pragma clang diagnostic pop |
| 211 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 212 | |
| 213 | static void |
| 214 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 215 | FuDevice *device, |
| 216 | FuUtilPrivate *priv) |
| 217 | { |
| 218 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 219 | g_debug ("ADDED:\n%s", tmp); |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 224 | FuDevice *device, |
| 225 | FuUtilPrivate *priv) |
| 226 | { |
| 227 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 228 | g_debug ("REMOVED:\n%s", tmp); |
| 229 | } |
| 230 | |
| 231 | static void |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 232 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 233 | FwupdStatus status, |
| 234 | FuUtilPrivate *priv) |
| 235 | { |
| 236 | fu_progressbar_update (priv->progressbar, status, 0); |
| 237 | } |
| 238 | |
| 239 | static void |
| 240 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 241 | guint percentage, |
| 242 | FuUtilPrivate *priv) |
| 243 | { |
| 244 | fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage); |
| 245 | } |
| 246 | |
| 247 | static gboolean |
| 248 | fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 249 | { |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 250 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 251 | return FALSE; |
| 252 | g_main_loop_run (priv->loop); |
| 253 | return TRUE; |
| 254 | } |
| 255 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 256 | static gint |
| 257 | fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2) |
| 258 | { |
| 259 | return fu_plugin_name_compare (*item1, *item2); |
| 260 | } |
| 261 | |
| 262 | static gboolean |
| 263 | fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error) |
| 264 | { |
| 265 | GPtrArray *plugins; |
| 266 | guint cnt = 0; |
| 267 | |
| 268 | /* load engine */ |
| 269 | if (!fu_engine_load_plugins (priv->engine, error)) |
| 270 | return FALSE; |
| 271 | |
| 272 | /* print */ |
| 273 | plugins = fu_engine_get_plugins (priv->engine); |
| 274 | g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb); |
| 275 | for (guint i = 0; i < plugins->len; i++) { |
| 276 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
| 277 | if (!fu_plugin_get_enabled (plugin)) |
| 278 | continue; |
| 279 | g_print ("%s\n", fu_plugin_get_name (plugin)); |
| 280 | cnt++; |
| 281 | } |
| 282 | if (cnt == 0) { |
| 283 | /* TRANSLATORS: nothing found */ |
| 284 | g_print ("%s\n", _("No plugins found")); |
| 285 | return TRUE; |
| 286 | } |
| 287 | |
| 288 | return TRUE; |
| 289 | } |
| 290 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 291 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 292 | fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev) |
| 293 | { |
| 294 | if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) { |
| 295 | if (!fwupd_device_has_flag (dev, priv->filter_include)) |
| 296 | return FALSE; |
| 297 | } |
| 298 | if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) { |
| 299 | if (fwupd_device_has_flag (dev, priv->filter_exclude)) |
| 300 | return FALSE; |
| 301 | } |
| 302 | return TRUE; |
| 303 | } |
| 304 | |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 305 | static gchar * |
| 306 | fu_util_get_tree_title (FuUtilPrivate *priv) |
| 307 | { |
| 308 | return g_strdup (fu_engine_get_host_product (priv->engine)); |
| 309 | } |
| 310 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 311 | static gboolean |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 312 | fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error) |
| 313 | { |
| 314 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 315 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 316 | g_autofree gchar *title = fu_util_get_tree_title (priv); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 317 | |
| 318 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 319 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 320 | return FALSE; |
| 321 | |
| 322 | /* get devices from daemon */ |
| 323 | devices = fu_engine_get_devices (priv->engine, error); |
| 324 | if (devices == NULL) |
| 325 | return FALSE; |
| 326 | for (guint i = 0; i < devices->len; i++) { |
| 327 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 328 | g_autoptr(GPtrArray) rels = NULL; |
| 329 | g_autoptr(GError) error_local = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 330 | GNode *child; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 331 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 332 | /* not going to have results, so save a engine round-trip */ |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 333 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 334 | continue; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 335 | if (!fu_util_filter_device (priv, dev)) |
| 336 | continue; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 337 | |
| 338 | /* get the releases for this device and filter for validity */ |
| 339 | rels = fu_engine_get_upgrades (priv->engine, |
| 340 | fwupd_device_get_id (dev), |
| 341 | &error_local); |
| 342 | if (rels == NULL) { |
| 343 | g_printerr ("%s\n", error_local->message); |
| 344 | continue; |
| 345 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 346 | child = g_node_append_data (root, dev); |
| 347 | |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 348 | for (guint j = 0; j < rels->len; j++) { |
| 349 | FwupdRelease *rel = g_ptr_array_index (rels, j); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 350 | g_node_append_data (child, g_object_ref (rel)); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 351 | } |
| 352 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 353 | if (g_node_n_nodes (root, G_TRAVERSE_ALL) > 1) |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 354 | fu_util_print_tree (root, title); |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 355 | /* save the device state for other applications to see */ |
| 356 | if (!fu_util_save_current_state (priv, error)) |
| 357 | return FALSE; |
| 358 | |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 359 | /* success */ |
| 360 | return TRUE; |
| 361 | } |
| 362 | |
| 363 | static gboolean |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 364 | fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error) |
| 365 | { |
| 366 | g_autoptr(GPtrArray) array = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 367 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 368 | g_autofree gchar *title = fu_util_get_tree_title (priv); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 369 | gint fd; |
| 370 | |
| 371 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 372 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 373 | return FALSE; |
| 374 | |
| 375 | /* check args */ |
| 376 | if (g_strv_length (values) != 1) { |
| 377 | g_set_error_literal (error, |
| 378 | FWUPD_ERROR, |
| 379 | FWUPD_ERROR_INVALID_ARGS, |
| 380 | "Invalid arguments"); |
| 381 | return FALSE; |
| 382 | } |
| 383 | |
| 384 | /* open file */ |
| 385 | fd = open (values[0], O_RDONLY); |
| 386 | if (fd < 0) { |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 387 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 388 | g_set_error (error, |
| 389 | FWUPD_ERROR, |
| 390 | FWUPD_ERROR_INVALID_FILE, |
| 391 | "failed to open %s", |
| 392 | values[0]); |
| 393 | return FALSE; |
| 394 | } |
| 395 | array = fu_engine_get_details (priv->engine, fd, error); |
| 396 | close (fd); |
| 397 | |
| 398 | if (array == NULL) |
| 399 | return FALSE; |
| 400 | for (guint i = 0; i < array->len; i++) { |
| 401 | FwupdDevice *dev = g_ptr_array_index (array, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 402 | if (!fu_util_filter_device (priv, dev)) |
| 403 | continue; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 404 | g_node_append_data (root, dev); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 405 | } |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 406 | fu_util_print_tree (root, title); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 407 | |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 408 | return TRUE; |
| 409 | } |
| 410 | |
| 411 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 412 | fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error) |
| 413 | { |
| 414 | g_autoptr(GString) str = g_string_new (NULL); |
| 415 | |
| 416 | for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) { |
| 417 | const gchar *tmp = fwupd_device_flag_to_string (i); |
| 418 | if (tmp == NULL) |
| 419 | break; |
| 420 | if (i != FWUPD_DEVICE_FLAG_INTERNAL) |
| 421 | g_string_append (str, " "); |
| 422 | g_string_append (str, tmp); |
| 423 | g_string_append (str, " ~"); |
| 424 | g_string_append (str, tmp); |
| 425 | } |
| 426 | g_print ("%s\n", str->str); |
| 427 | |
| 428 | return TRUE; |
| 429 | } |
| 430 | |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 431 | static void |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 432 | 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] | 433 | { |
| 434 | for (guint i = 0; i < devs->len; i++) { |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 435 | FuDevice *dev_tmp = g_ptr_array_index (devs, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 436 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp))) |
| 437 | continue; |
Mario Limonciello | d1775bc | 2018-07-17 00:28:52 -0500 | [diff] [blame] | 438 | if (!priv->show_all_devices && |
| 439 | !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp))) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 440 | continue; |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 441 | if (fu_device_get_parent (dev_tmp) == dev) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 442 | GNode *child = g_node_append_data (root, dev_tmp); |
| 443 | fu_util_build_device_tree (priv, child, devs, dev_tmp); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static gboolean |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 449 | fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 450 | { |
| 451 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 452 | g_autofree gchar *title = fu_util_get_tree_title (priv); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 453 | g_autoptr(GPtrArray) devs = NULL; |
| 454 | |
| 455 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 456 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 457 | return FALSE; |
| 458 | |
| 459 | /* print */ |
| 460 | devs = fu_engine_get_devices (priv->engine, error); |
| 461 | if (devs == NULL) |
| 462 | return FALSE; |
| 463 | |
| 464 | /* print */ |
| 465 | if (devs->len == 0) { |
| 466 | /* TRANSLATORS: nothing attached that can be upgraded */ |
| 467 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
| 468 | return TRUE; |
| 469 | } |
| 470 | fu_util_build_device_tree (priv, root, devs, NULL); |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 471 | fu_util_print_tree (root, title); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 472 | |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 473 | /* save the device state for other applications to see */ |
| 474 | return fu_util_save_current_state (priv, error); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 477 | static FuDevice * |
| 478 | fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) |
| 479 | { |
| 480 | FuDevice *dev; |
| 481 | guint idx; |
| 482 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 483 | g_autoptr(GPtrArray) devices_filtered = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 484 | |
| 485 | /* get devices from daemon */ |
| 486 | devices = fu_engine_get_devices (priv->engine, error); |
| 487 | if (devices == NULL) |
| 488 | return NULL; |
| 489 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 490 | /* filter results */ |
| 491 | devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 492 | for (guint i = 0; i < devices->len; i++) { |
| 493 | dev = g_ptr_array_index (devices, i); |
| 494 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev))) |
| 495 | continue; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 496 | g_ptr_array_add (devices_filtered, g_object_ref (dev)); |
| 497 | } |
| 498 | |
| 499 | /* nothing */ |
| 500 | if (devices_filtered->len == 0) { |
| 501 | g_set_error_literal (error, |
| 502 | FWUPD_ERROR, |
| 503 | FWUPD_ERROR_NOTHING_TO_DO, |
| 504 | "No supported devices"); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 508 | /* exactly one */ |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 509 | if (devices_filtered->len == 1) { |
| 510 | dev = g_ptr_array_index (devices_filtered, 0); |
Mario Limonciello | a61b4d8 | 2019-10-22 08:37:45 -0500 | [diff] [blame] | 511 | /* TRANSLATORS: Device has been chosen by the daemon for the user */ |
| 512 | g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev)); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 513 | return g_object_ref (dev); |
| 514 | } |
| 515 | |
| 516 | /* TRANSLATORS: get interactive prompt */ |
| 517 | g_print ("%s\n", _("Choose a device:")); |
| 518 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 519 | g_print ("0.\t%s\n", _("Cancel")); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 520 | for (guint i = 0; i < devices_filtered->len; i++) { |
| 521 | dev = g_ptr_array_index (devices_filtered, i); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 522 | g_print ("%u.\t%s (%s)\n", |
| 523 | i + 1, |
| 524 | fu_device_get_id (dev), |
| 525 | fu_device_get_name (dev)); |
| 526 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 527 | idx = fu_util_prompt_for_number (devices_filtered->len); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 528 | if (idx == 0) { |
| 529 | g_set_error_literal (error, |
| 530 | FWUPD_ERROR, |
| 531 | FWUPD_ERROR_NOTHING_TO_DO, |
| 532 | "Request canceled"); |
| 533 | return NULL; |
| 534 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 535 | dev = g_ptr_array_index (devices_filtered, idx - 1); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 536 | return g_object_ref (dev); |
| 537 | } |
| 538 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 539 | static void |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 540 | fu_util_update_device_changed_cb (FwupdClient *client, |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 541 | FwupdDevice *device, |
| 542 | FuUtilPrivate *priv) |
| 543 | { |
| 544 | g_autofree gchar *str = NULL; |
| 545 | |
Richard Hughes | 809abea | 2019-03-23 11:06:18 +0000 | [diff] [blame] | 546 | /* allowed to set whenever the device has changed */ |
| 547 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) |
| 548 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN; |
| 549 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) |
| 550 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT; |
| 551 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 552 | /* same as last time, so ignore */ |
| 553 | if (priv->current_device != NULL && |
| 554 | fwupd_device_compare (priv->current_device, device) == 0) |
| 555 | return; |
| 556 | |
| 557 | /* show message in progressbar */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 558 | if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) { |
| 559 | /* TRANSLATORS: %1 is a device name */ |
| 560 | str = g_strdup_printf (_("Updating %s…"), |
| 561 | fwupd_device_get_name (device)); |
| 562 | fu_progressbar_set_title (priv->progressbar, str); |
| 563 | } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) { |
| 564 | /* TRANSLATORS: %1 is a device name */ |
| 565 | str = g_strdup_printf (_("Installing on %s…"), |
| 566 | fwupd_device_get_name (device)); |
| 567 | fu_progressbar_set_title (priv->progressbar, str); |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 568 | } else if (priv->current_operation == FU_UTIL_OPERATION_READ) { |
| 569 | /* TRANSLATORS: %1 is a device name */ |
| 570 | str = g_strdup_printf (_("Reading from %s…"), |
| 571 | fwupd_device_get_name (device)); |
| 572 | fu_progressbar_set_title (priv->progressbar, str); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 573 | } else { |
| 574 | g_warning ("no FuUtilOperation set"); |
| 575 | } |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 576 | g_set_object (&priv->current_device, device); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 577 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 578 | if (priv->current_message == NULL) { |
| 579 | const gchar *tmp = fwupd_device_get_update_message (priv->current_device); |
| 580 | if (tmp != NULL) |
| 581 | priv->current_message = g_strdup (tmp); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void |
| 586 | fu_util_display_current_message (FuUtilPrivate *priv) |
| 587 | { |
| 588 | if (priv->current_message == NULL) |
| 589 | return; |
| 590 | g_print ("%s\n", priv->current_message); |
| 591 | g_clear_pointer (&priv->current_message, g_free); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 592 | } |
| 593 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 594 | static gboolean |
| 595 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 596 | { |
| 597 | g_autoptr(FuDevice) device = NULL; |
| 598 | g_autoptr(GBytes) blob_fw = NULL; |
| 599 | |
| 600 | /* invalid args */ |
| 601 | if (g_strv_length (values) == 0) { |
| 602 | g_set_error_literal (error, |
| 603 | FWUPD_ERROR, |
| 604 | FWUPD_ERROR_INVALID_ARGS, |
| 605 | "Invalid arguments"); |
| 606 | return FALSE; |
| 607 | } |
| 608 | |
| 609 | /* parse blob */ |
| 610 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 611 | if (blob_fw == NULL) { |
| 612 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 613 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 614 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 615 | |
| 616 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 617 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 618 | return FALSE; |
| 619 | |
| 620 | /* get device */ |
| 621 | if (g_strv_length (values) >= 2) { |
| 622 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 623 | if (device == NULL) |
| 624 | return FALSE; |
| 625 | } else { |
| 626 | device = fu_util_prompt_for_device (priv, error); |
| 627 | if (device == NULL) |
| 628 | return FALSE; |
| 629 | } |
| 630 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 631 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 632 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 633 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 634 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 635 | /* write bare firmware */ |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 636 | if (priv->prepare_blob) { |
| 637 | g_autoptr(GPtrArray) devices = NULL; |
| 638 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 639 | g_ptr_array_add (devices, g_object_ref (device)); |
| 640 | if (!fu_engine_composite_prepare (priv->engine, devices, error)) { |
| 641 | g_prefix_error (error, "failed to prepare composite action: "); |
| 642 | return FALSE; |
| 643 | } |
| 644 | } |
Mario Limonciello | 035818b | 2019-03-26 11:12:16 -0500 | [diff] [blame] | 645 | priv->flags = FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 646 | 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] | 647 | return FALSE; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 648 | if (priv->cleanup_blob) { |
| 649 | g_autoptr(FuDevice) device_new = NULL; |
| 650 | g_autoptr(GError) error_local = NULL; |
| 651 | |
| 652 | /* get the possibly new device from the old ID */ |
| 653 | device_new = fu_engine_get_device (priv->engine, |
| 654 | fu_device_get_id (device), |
| 655 | &error_local); |
| 656 | if (device_new == NULL) { |
| 657 | g_debug ("failed to find new device: %s", |
| 658 | error_local->message); |
| 659 | } else { |
Mario Limonciello | bb3fa5e | 2019-02-07 21:13:02 -0600 | [diff] [blame] | 660 | 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] | 661 | g_ptr_array_add (devices_new, g_steal_pointer (&device_new)); |
| 662 | if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) { |
| 663 | g_prefix_error (error, "failed to cleanup composite action: "); |
| 664 | return FALSE; |
| 665 | } |
| 666 | } |
| 667 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 668 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 669 | fu_util_display_current_message (priv); |
| 670 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 671 | /* success */ |
| 672 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 675 | static gboolean |
| 676 | fu_util_firmware_read (FuUtilPrivate *priv, gchar **values, GError **error) |
| 677 | { |
| 678 | g_autoptr(FuDevice) device = NULL; |
| 679 | g_autoptr(GBytes) blob_empty = g_bytes_new (NULL, 0); |
| 680 | g_autoptr(GBytes) blob_fw = NULL; |
| 681 | |
| 682 | /* invalid args */ |
| 683 | if (g_strv_length (values) == 0) { |
| 684 | g_set_error_literal (error, |
| 685 | FWUPD_ERROR, |
| 686 | FWUPD_ERROR_INVALID_ARGS, |
| 687 | "Invalid arguments"); |
| 688 | return FALSE; |
| 689 | } |
| 690 | |
| 691 | /* file already exists */ |
| 692 | if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0 && |
| 693 | g_file_test (values[0], G_FILE_TEST_EXISTS)) { |
| 694 | g_set_error_literal (error, |
| 695 | FWUPD_ERROR, |
| 696 | FWUPD_ERROR_INVALID_ARGS, |
| 697 | "Filename already exists"); |
| 698 | return FALSE; |
| 699 | } |
| 700 | |
Richard Hughes | 02792c0 | 2019-11-01 14:21:20 +0000 | [diff] [blame] | 701 | /* write a zero length file to ensure the destination is writable to |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 702 | * avoid failing at the end of a potentially lengthy operation */ |
| 703 | if (!fu_common_set_contents_bytes (values[0], blob_empty, error)) |
| 704 | return FALSE; |
| 705 | |
| 706 | /* load engine */ |
| 707 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 708 | return FALSE; |
| 709 | |
| 710 | /* get device */ |
| 711 | if (g_strv_length (values) >= 2) { |
| 712 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 713 | if (device == NULL) |
| 714 | return FALSE; |
| 715 | } else { |
| 716 | device = fu_util_prompt_for_device (priv, error); |
| 717 | if (device == NULL) |
| 718 | return FALSE; |
| 719 | } |
| 720 | priv->current_operation = FU_UTIL_OPERATION_READ; |
| 721 | g_signal_connect (priv->engine, "device-changed", |
| 722 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 723 | |
| 724 | /* dump firmware */ |
| 725 | blob_fw = fu_engine_firmware_read (priv->engine, device, priv->flags, error); |
| 726 | if (blob_fw == NULL) |
| 727 | return FALSE; |
| 728 | return fu_common_set_contents_bytes (values[0], blob_fw, error); |
| 729 | } |
| 730 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 731 | static gint |
| 732 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 733 | { |
| 734 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 735 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 736 | return fu_install_task_compare (task1, task2); |
| 737 | } |
| 738 | |
| 739 | static gboolean |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 740 | fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error) |
| 741 | { |
Filipe LaÃns | e091427 | 2019-09-20 10:04:43 +0100 | [diff] [blame] | 742 | const gchar *argv[][5] = { { "wget", uri, "-O", fn, NULL }, |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 743 | { "curl", uri, "--output", fn, NULL }, |
| 744 | { NULL } }; |
| 745 | for (guint i = 0; argv[i][0] != NULL; i++) { |
| 746 | g_autoptr(GError) error_local = NULL; |
| 747 | if (!fu_common_find_program_in_path (argv[i][0], &error_local)) { |
| 748 | g_debug ("%s", error_local->message); |
| 749 | continue; |
| 750 | } |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 751 | return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error); |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 752 | } |
| 753 | g_set_error_literal (error, |
| 754 | FWUPD_ERROR, |
| 755 | FWUPD_ERROR_NOT_FOUND, |
| 756 | "no supported out-of-process downloaders found"); |
| 757 | return FALSE; |
| 758 | } |
| 759 | |
| 760 | static gchar * |
| 761 | fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error) |
| 762 | { |
| 763 | g_autofree gchar *filename = NULL; |
| 764 | g_autoptr(SoupURI) uri = NULL; |
| 765 | |
| 766 | /* a local file */ |
| 767 | uri = soup_uri_new (perhapsfn); |
| 768 | if (uri == NULL) |
| 769 | return g_strdup (perhapsfn); |
| 770 | |
| 771 | /* download the firmware to a cachedir */ |
| 772 | filename = fu_util_get_user_cache_path (perhapsfn); |
| 773 | if (!fu_common_mkdir_parent (filename, error)) |
| 774 | return NULL; |
| 775 | if (!fu_util_download_out_of_process (perhapsfn, filename, error)) |
| 776 | return NULL; |
| 777 | return g_steal_pointer (&filename); |
| 778 | } |
| 779 | |
| 780 | static gboolean |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 781 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 782 | { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 783 | g_autofree gchar *filename = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 784 | g_autoptr(GBytes) blob_cab = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 785 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 786 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 787 | g_autoptr(GPtrArray) errors = NULL; |
| 788 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 789 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 790 | |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 791 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 792 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 793 | return FALSE; |
| 794 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 795 | /* handle both forms */ |
| 796 | if (g_strv_length (values) == 1) { |
| 797 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 798 | if (devices_possible == NULL) |
| 799 | return FALSE; |
| 800 | } else if (g_strv_length (values) == 2) { |
| 801 | FuDevice *device = fu_engine_get_device (priv->engine, |
| 802 | values[1], |
| 803 | error); |
| 804 | if (device == NULL) |
| 805 | return FALSE; |
| 806 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 807 | g_ptr_array_add (devices_possible, device); |
| 808 | } else { |
| 809 | g_set_error_literal (error, |
| 810 | FWUPD_ERROR, |
| 811 | FWUPD_ERROR_INVALID_ARGS, |
| 812 | "Invalid arguments"); |
| 813 | return FALSE; |
| 814 | } |
| 815 | |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 816 | /* download if required */ |
| 817 | filename = fu_util_download_if_required (priv, values[0], error); |
| 818 | if (filename == NULL) |
| 819 | return FALSE; |
| 820 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 821 | /* parse silo */ |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 822 | blob_cab = fu_common_get_contents_bytes (filename, error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 823 | if (blob_cab == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 824 | fu_util_maybe_prefix_sandbox_error (filename, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 825 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 826 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 827 | silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error); |
| 828 | if (silo == NULL) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 829 | return FALSE; |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 830 | components = xb_silo_query (silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 831 | if (components == NULL) |
| 832 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 833 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 834 | /* for each component in the silo */ |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 835 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 836 | 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] | 837 | for (guint i = 0; i < components->len; i++) { |
| 838 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 839 | |
| 840 | /* do any devices pass the requirements */ |
| 841 | for (guint j = 0; j < devices_possible->len; j++) { |
| 842 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 843 | g_autoptr(FuInstallTask) task = NULL; |
| 844 | g_autoptr(GError) error_local = NULL; |
| 845 | |
| 846 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 847 | task = fu_install_task_new (device, component); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 848 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 849 | task, priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 850 | &error_local)) { |
| 851 | g_debug ("requirement on %s:%s failed: %s", |
| 852 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 853 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 854 | error_local->message); |
| 855 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 856 | continue; |
| 857 | } |
| 858 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 859 | /* if component should have an update message from CAB */ |
| 860 | fu_device_incorporate_from_component (device, component); |
| 861 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 862 | /* success */ |
| 863 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* order the install tasks by the device priority */ |
| 868 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 869 | |
| 870 | /* nothing suitable */ |
| 871 | if (install_tasks->len == 0) { |
| 872 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 873 | g_propagate_error (error, error_tmp); |
| 874 | return FALSE; |
| 875 | } |
| 876 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 877 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 878 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 879 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 880 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 881 | /* install all the tasks */ |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 882 | if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error)) |
| 883 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 884 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 885 | fu_util_display_current_message (priv); |
| 886 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 887 | /* we don't want to ask anything */ |
| 888 | if (priv->no_reboot_check) { |
| 889 | g_debug ("skipping reboot check"); |
| 890 | return TRUE; |
| 891 | } |
| 892 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 893 | /* save the device state for other applications to see */ |
| 894 | if (!fu_util_save_current_state (priv, error)) |
| 895 | return FALSE; |
| 896 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 897 | /* success */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 898 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 901 | static gboolean |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 902 | fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 903 | { |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 904 | FwupdRemote *remote; |
| 905 | const gchar *remote_id; |
| 906 | const gchar *uri_tmp; |
| 907 | g_auto(GStrv) argv = NULL; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 908 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 909 | uri_tmp = fwupd_release_get_uri (rel); |
| 910 | if (uri_tmp == NULL) { |
| 911 | g_set_error_literal (error, |
| 912 | FWUPD_ERROR, |
| 913 | FWUPD_ERROR_INVALID_FILE, |
| 914 | "release missing URI"); |
| 915 | return FALSE; |
| 916 | } |
| 917 | remote_id = fwupd_release_get_remote_id (rel); |
| 918 | if (remote_id == NULL) { |
| 919 | g_set_error (error, |
| 920 | FWUPD_ERROR, |
| 921 | FWUPD_ERROR_INVALID_FILE, |
| 922 | "failed to find remote for %s", |
| 923 | uri_tmp); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 924 | return FALSE; |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | remote = fu_engine_get_remote_by_id (priv->engine, |
| 928 | remote_id, |
| 929 | error); |
| 930 | if (remote == NULL) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 931 | return FALSE; |
| 932 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 933 | argv = g_new0 (gchar *, 2); |
| 934 | /* local remotes have the firmware already */ |
| 935 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) { |
| 936 | const gchar *fn_cache = fwupd_remote_get_filename_cache (remote); |
| 937 | g_autofree gchar *path = g_path_get_dirname (fn_cache); |
| 938 | argv[0] = g_build_filename (path, uri_tmp, NULL); |
| 939 | } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 940 | argv[0] = g_strdup (uri_tmp + 7); |
| 941 | /* web remote, fu_util_install will download file */ |
| 942 | } else { |
| 943 | argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error); |
| 944 | } |
| 945 | return fu_util_install (priv, argv, error); |
| 946 | } |
| 947 | |
| 948 | static gboolean |
| 949 | fu_util_update_all (FuUtilPrivate *priv, GError **error) |
| 950 | { |
| 951 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 952 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 953 | devices = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 387bda4 | 2019-02-07 14:20:22 +0000 | [diff] [blame] | 954 | if (devices == NULL) |
| 955 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 956 | for (guint i = 0; i < devices->len; i++) { |
| 957 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 958 | FwupdRelease *rel; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 959 | const gchar *device_id; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 960 | g_autoptr(GPtrArray) rels = NULL; |
| 961 | g_autoptr(GError) error_local = NULL; |
| 962 | |
| 963 | if (!fu_util_is_interesting_device (dev)) |
| 964 | continue; |
| 965 | /* only show stuff that has metadata available */ |
| 966 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 967 | continue; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 968 | if (!fu_util_filter_device (priv, dev)) |
| 969 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 970 | |
| 971 | device_id = fu_device_get_id (dev); |
| 972 | rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local); |
| 973 | if (rels == NULL) { |
| 974 | g_printerr ("%s\n", error_local->message); |
| 975 | continue; |
| 976 | } |
| 977 | |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 978 | if (!priv->no_safety_check) { |
| 979 | if (!fu_util_prompt_warning (dev, |
| 980 | fu_util_get_tree_title (priv), |
| 981 | error)) |
| 982 | return FALSE; |
| 983 | } |
| 984 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 985 | rel = g_ptr_array_index (rels, 0); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 986 | if (!fu_util_install_release (priv, rel, &error_local)) { |
| 987 | g_printerr ("%s\n", error_local->message); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 988 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 989 | } |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 990 | fu_util_display_current_message (priv); |
| 991 | } |
| 992 | return TRUE; |
| 993 | } |
| 994 | |
| 995 | static gboolean |
| 996 | fu_util_update_by_id (FuUtilPrivate *priv, const gchar *device_id, GError **error) |
| 997 | { |
| 998 | FwupdRelease *rel; |
| 999 | g_autoptr(FuDevice) dev = NULL; |
| 1000 | g_autoptr(GPtrArray) rels = NULL; |
| 1001 | |
| 1002 | /* do not allow a partial device-id */ |
| 1003 | dev = fu_engine_get_device (priv->engine, device_id, error); |
| 1004 | if (dev == NULL) |
| 1005 | return FALSE; |
| 1006 | |
| 1007 | /* get the releases for this device and filter for validity */ |
| 1008 | rels = fu_engine_get_upgrades (priv->engine, device_id, error); |
| 1009 | if (rels == NULL) |
| 1010 | return FALSE; |
| 1011 | rel = g_ptr_array_index (rels, 0); |
| 1012 | if (!fu_util_install_release (priv, rel, error)) |
| 1013 | return FALSE; |
| 1014 | fu_util_display_current_message (priv); |
| 1015 | |
| 1016 | return TRUE; |
| 1017 | } |
| 1018 | |
| 1019 | static gboolean |
| 1020 | fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1021 | { |
Mario Limonciello | 0f109b0 | 2019-11-14 10:26:44 -0600 | [diff] [blame] | 1022 | if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) { |
| 1023 | g_set_error_literal (error, |
| 1024 | FWUPD_ERROR, |
| 1025 | FWUPD_ERROR_INVALID_ARGS, |
| 1026 | "--allow-older is not supported for this command"); |
| 1027 | return FALSE; |
| 1028 | } |
| 1029 | |
| 1030 | if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) { |
| 1031 | g_set_error_literal (error, |
| 1032 | FWUPD_ERROR, |
| 1033 | FWUPD_ERROR_INVALID_ARGS, |
| 1034 | "--allow-reinstall is not supported for this command"); |
| 1035 | return FALSE; |
| 1036 | } |
| 1037 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 1038 | if (g_strv_length (values) > 1) { |
| 1039 | g_set_error_literal (error, |
| 1040 | FWUPD_ERROR, |
| 1041 | FWUPD_ERROR_INVALID_ARGS, |
| 1042 | "Invalid arguments"); |
| 1043 | return FALSE; |
| 1044 | } |
| 1045 | |
| 1046 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1047 | return FALSE; |
| 1048 | |
| 1049 | priv->current_operation = FU_UTIL_OPERATION_UPDATE; |
| 1050 | g_signal_connect (priv->engine, "device-changed", |
| 1051 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 1052 | |
| 1053 | if (g_strv_length (values) == 1) { |
| 1054 | if (!fu_util_update_by_id (priv, values[0], error)) |
| 1055 | return FALSE; |
| 1056 | } else { |
| 1057 | if (!fu_util_update_all (priv, error)) |
| 1058 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1059 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1060 | |
| 1061 | /* we don't want to ask anything */ |
| 1062 | if (priv->no_reboot_check) { |
| 1063 | g_debug ("skipping reboot check"); |
| 1064 | return TRUE; |
| 1065 | } |
| 1066 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1067 | /* save the device state for other applications to see */ |
| 1068 | if (!fu_util_save_current_state (priv, error)) |
| 1069 | return FALSE; |
| 1070 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1071 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1075 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1076 | { |
| 1077 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1078 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1079 | |
| 1080 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 1081 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1082 | return FALSE; |
| 1083 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1084 | /* get device */ |
| 1085 | if (g_strv_length (values) >= 1) { |
| 1086 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 1087 | if (device == NULL) |
| 1088 | return FALSE; |
| 1089 | } else { |
| 1090 | device = fu_util_prompt_for_device (priv, error); |
| 1091 | if (device == NULL) |
| 1092 | return FALSE; |
| 1093 | } |
| 1094 | |
| 1095 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1096 | locker = fu_device_locker_new (device, error); |
| 1097 | if (locker == NULL) |
| 1098 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1099 | return fu_device_detach (device, error); |
| 1100 | } |
| 1101 | |
| 1102 | static gboolean |
| 1103 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1104 | { |
| 1105 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1106 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 1109 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1110 | return FALSE; |
| 1111 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1112 | /* get device */ |
| 1113 | if (g_strv_length (values) >= 1) { |
| 1114 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 1115 | if (device == NULL) |
| 1116 | return FALSE; |
| 1117 | } else { |
| 1118 | device = fu_util_prompt_for_device (priv, error); |
| 1119 | if (device == NULL) |
| 1120 | return FALSE; |
| 1121 | } |
| 1122 | |
| 1123 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1124 | locker = fu_device_locker_new (device, error); |
| 1125 | if (locker == NULL) |
| 1126 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1127 | return fu_device_attach (device, error); |
| 1128 | } |
| 1129 | |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1130 | static gboolean |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1131 | fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1132 | { |
| 1133 | gboolean has_pending = FALSE; |
| 1134 | g_autoptr(FuHistory) history = fu_history_new (); |
| 1135 | g_autoptr(GPtrArray) devices = NULL; |
| 1136 | |
| 1137 | /* check the history database before starting the daemon */ |
| 1138 | if (g_strv_length (values) == 0) { |
| 1139 | devices = fu_history_get_devices (history, error); |
| 1140 | if (devices == NULL) |
| 1141 | return FALSE; |
| 1142 | } else if (g_strv_length (values) == 1) { |
| 1143 | FuDevice *device; |
| 1144 | device = fu_history_get_device_by_id (history, values[0], error); |
| 1145 | if (device == NULL) |
| 1146 | return FALSE; |
| 1147 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1148 | g_ptr_array_add (devices, device); |
| 1149 | } else { |
| 1150 | g_set_error_literal (error, |
| 1151 | FWUPD_ERROR, |
| 1152 | FWUPD_ERROR_INVALID_ARGS, |
| 1153 | "Invalid arguments"); |
| 1154 | return FALSE; |
| 1155 | } |
| 1156 | |
| 1157 | /* nothing to do */ |
| 1158 | for (guint i = 0; i < devices->len; i++) { |
| 1159 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 1160 | if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
| 1161 | fu_engine_add_plugin_filter (priv->engine, |
| 1162 | fu_device_get_plugin (dev)); |
| 1163 | has_pending = TRUE; |
| 1164 | } |
| 1165 | } |
| 1166 | if (!has_pending) { |
| 1167 | g_set_error_literal (error, |
| 1168 | FWUPD_ERROR, |
| 1169 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1170 | "No firmware to activate"); |
| 1171 | return FALSE; |
| 1172 | |
| 1173 | } |
| 1174 | |
| 1175 | /* load engine */ |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 1176 | 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] | 1177 | return FALSE; |
| 1178 | |
| 1179 | /* activate anything with _NEEDS_ACTIVATION */ |
| 1180 | for (guint i = 0; i < devices->len; i++) { |
| 1181 | FuDevice *device = g_ptr_array_index (devices, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1182 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (device))) |
| 1183 | continue; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1184 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) |
| 1185 | continue; |
| 1186 | /* TRANSLATORS: shown when shutting down to switch to the new version */ |
| 1187 | g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device)); |
| 1188 | if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error)) |
| 1189 | return FALSE; |
| 1190 | } |
| 1191 | |
| 1192 | return TRUE; |
| 1193 | } |
| 1194 | |
| 1195 | static gboolean |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1196 | fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1197 | { |
| 1198 | g_autoptr(FuSmbios) smbios = fu_smbios_new (); |
| 1199 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
| 1200 | const gchar *hwid_keys[] = { |
| 1201 | FU_HWIDS_KEY_BIOS_VENDOR, |
| 1202 | FU_HWIDS_KEY_BIOS_VERSION, |
| 1203 | FU_HWIDS_KEY_BIOS_MAJOR_RELEASE, |
| 1204 | FU_HWIDS_KEY_BIOS_MINOR_RELEASE, |
| 1205 | FU_HWIDS_KEY_MANUFACTURER, |
| 1206 | FU_HWIDS_KEY_FAMILY, |
| 1207 | FU_HWIDS_KEY_PRODUCT_NAME, |
| 1208 | FU_HWIDS_KEY_PRODUCT_SKU, |
| 1209 | FU_HWIDS_KEY_ENCLOSURE_KIND, |
| 1210 | FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, |
| 1211 | FU_HWIDS_KEY_BASEBOARD_PRODUCT, |
| 1212 | NULL }; |
| 1213 | |
| 1214 | /* read DMI data */ |
| 1215 | if (g_strv_length (values) == 0) { |
| 1216 | if (!fu_smbios_setup (smbios, error)) |
| 1217 | return FALSE; |
| 1218 | } else if (g_strv_length (values) == 1) { |
| 1219 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 1220 | return FALSE; |
| 1221 | } else { |
| 1222 | g_set_error_literal (error, |
| 1223 | FWUPD_ERROR, |
| 1224 | FWUPD_ERROR_INVALID_ARGS, |
| 1225 | "Invalid arguments"); |
| 1226 | return FALSE; |
| 1227 | } |
| 1228 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 1229 | return FALSE; |
| 1230 | |
| 1231 | /* show debug output */ |
| 1232 | g_print ("Computer Information\n"); |
| 1233 | g_print ("--------------------\n"); |
| 1234 | for (guint i = 0; hwid_keys[i] != NULL; i++) { |
| 1235 | const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]); |
| 1236 | if (tmp == NULL) |
| 1237 | continue; |
| 1238 | if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 || |
| 1239 | g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) { |
| 1240 | guint64 val = g_ascii_strtoull (tmp, NULL, 16); |
| 1241 | g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val); |
| 1242 | } else { |
| 1243 | g_print ("%s: %s\n", hwid_keys[i], tmp); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | /* show GUIDs */ |
| 1248 | g_print ("\nHardware IDs\n"); |
| 1249 | g_print ("------------\n"); |
| 1250 | for (guint i = 0; i < 15; i++) { |
| 1251 | const gchar *keys = NULL; |
| 1252 | g_autofree gchar *guid = NULL; |
| 1253 | g_autofree gchar *key = NULL; |
| 1254 | g_autofree gchar *keys_str = NULL; |
| 1255 | g_auto(GStrv) keysv = NULL; |
| 1256 | g_autoptr(GError) error_local = NULL; |
| 1257 | |
| 1258 | /* get the GUID */ |
| 1259 | key = g_strdup_printf ("HardwareID-%u", i); |
| 1260 | keys = fu_hwids_get_replace_keys (hwids, key); |
| 1261 | guid = fu_hwids_get_guid (hwids, key, &error_local); |
| 1262 | if (guid == NULL) { |
| 1263 | g_print ("%s\n", error_local->message); |
| 1264 | continue; |
| 1265 | } |
| 1266 | |
| 1267 | /* show what makes up the GUID */ |
| 1268 | keysv = g_strsplit (keys, "&", -1); |
| 1269 | keys_str = g_strjoinv (" + ", keysv); |
| 1270 | g_print ("{%s} <- %s\n", guid, keys_str); |
| 1271 | } |
| 1272 | |
| 1273 | return TRUE; |
| 1274 | } |
| 1275 | |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1276 | static gboolean |
| 1277 | fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1278 | { |
| 1279 | const gchar *script_fn = "startup.sh"; |
| 1280 | const gchar *output_fn = "firmware.bin"; |
| 1281 | g_autoptr(GBytes) archive_blob = NULL; |
| 1282 | g_autoptr(GBytes) firmware_blob = NULL; |
| 1283 | if (g_strv_length (values) < 2) { |
| 1284 | g_set_error_literal (error, |
| 1285 | FWUPD_ERROR, |
| 1286 | FWUPD_ERROR_INVALID_ARGS, |
| 1287 | "Invalid arguments"); |
| 1288 | return FALSE; |
| 1289 | } |
| 1290 | archive_blob = fu_common_get_contents_bytes (values[0], error); |
| 1291 | if (archive_blob == NULL) |
| 1292 | return FALSE; |
| 1293 | if (g_strv_length (values) > 2) |
| 1294 | script_fn = values[2]; |
| 1295 | if (g_strv_length (values) > 3) |
| 1296 | output_fn = values[3]; |
| 1297 | firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error); |
| 1298 | if (firmware_blob == NULL) |
| 1299 | return FALSE; |
| 1300 | return fu_common_set_contents_bytes (values[1], firmware_blob, error); |
| 1301 | } |
| 1302 | |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1303 | static gboolean |
| 1304 | fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1305 | { |
| 1306 | g_autofree gchar *sig = NULL; |
| 1307 | |
| 1308 | /* check args */ |
| 1309 | if (g_strv_length (values) != 1) { |
| 1310 | g_set_error_literal (error, |
| 1311 | FWUPD_ERROR, |
| 1312 | FWUPD_ERROR_INVALID_ARGS, |
| 1313 | "Invalid arguments: value expected"); |
| 1314 | return FALSE; |
| 1315 | } |
| 1316 | |
| 1317 | /* start engine */ |
| 1318 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1319 | return FALSE; |
| 1320 | sig = fu_engine_self_sign (priv->engine, values[0], |
| 1321 | FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP | |
| 1322 | FU_KEYRING_SIGN_FLAG_ADD_CERT, error); |
| 1323 | if (sig == NULL) |
| 1324 | return FALSE; |
| 1325 | g_print ("%s\n", sig); |
| 1326 | return TRUE; |
| 1327 | } |
| 1328 | |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1329 | static void |
| 1330 | fu_util_device_added_cb (FwupdClient *client, |
| 1331 | FwupdDevice *device, |
| 1332 | gpointer user_data) |
| 1333 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1334 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1335 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1336 | g_print ("%s\n%s", _("Device added:"), tmp); |
| 1337 | } |
| 1338 | |
| 1339 | static void |
| 1340 | fu_util_device_removed_cb (FwupdClient *client, |
| 1341 | FwupdDevice *device, |
| 1342 | gpointer user_data) |
| 1343 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1344 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1345 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1346 | g_print ("%s\n%s", _("Device removed:"), tmp); |
| 1347 | } |
| 1348 | |
| 1349 | static void |
| 1350 | fu_util_device_changed_cb (FwupdClient *client, |
| 1351 | FwupdDevice *device, |
| 1352 | gpointer user_data) |
| 1353 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1354 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1355 | /* TRANSLATORS: this is when a device has been updated */ |
| 1356 | g_print ("%s\n%s", _("Device changed:"), tmp); |
| 1357 | } |
| 1358 | |
| 1359 | static void |
| 1360 | fu_util_changed_cb (FwupdClient *client, gpointer user_data) |
| 1361 | { |
| 1362 | /* TRANSLATORS: this is when the daemon state changes */ |
| 1363 | g_print ("%s\n", _("Changed")); |
| 1364 | } |
| 1365 | |
| 1366 | static gboolean |
| 1367 | fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1368 | { |
| 1369 | g_autoptr(FwupdClient) client = fwupd_client_new (); |
| 1370 | |
| 1371 | /* get all the devices */ |
| 1372 | if (!fwupd_client_connect (client, priv->cancellable, error)) |
| 1373 | return FALSE; |
| 1374 | |
| 1375 | /* watch for any hotplugged device */ |
| 1376 | g_signal_connect (client, "changed", |
| 1377 | G_CALLBACK (fu_util_changed_cb), priv); |
| 1378 | g_signal_connect (client, "device-added", |
| 1379 | G_CALLBACK (fu_util_device_added_cb), priv); |
| 1380 | g_signal_connect (client, "device-removed", |
| 1381 | G_CALLBACK (fu_util_device_removed_cb), priv); |
| 1382 | g_signal_connect (client, "device-changed", |
| 1383 | G_CALLBACK (fu_util_device_changed_cb), priv); |
| 1384 | g_signal_connect (priv->cancellable, "cancelled", |
| 1385 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1386 | g_main_loop_run (priv->loop); |
| 1387 | return TRUE; |
| 1388 | } |
| 1389 | |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1390 | static gboolean |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1391 | fu_util_get_firmware_types (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1392 | { |
| 1393 | g_autoptr(GPtrArray) firmware_types = NULL; |
| 1394 | |
| 1395 | /* load engine */ |
| 1396 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error)) |
| 1397 | return FALSE; |
| 1398 | |
| 1399 | firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine); |
| 1400 | for (guint i = 0; i < firmware_types->len; i++) { |
| 1401 | const gchar *id = g_ptr_array_index (firmware_types, i); |
| 1402 | g_print ("%s\n", id); |
| 1403 | } |
| 1404 | if (firmware_types->len == 0) { |
| 1405 | /* TRANSLATORS: nothing found */ |
| 1406 | g_print ("%s\n", _("No firmware IDs found")); |
| 1407 | return TRUE; |
| 1408 | } |
| 1409 | |
| 1410 | return TRUE; |
| 1411 | } |
| 1412 | |
| 1413 | static gchar * |
| 1414 | fu_util_prompt_for_firmware_type (FuUtilPrivate *priv, GError **error) |
| 1415 | { |
| 1416 | g_autoptr(GPtrArray) firmware_types = NULL; |
| 1417 | guint idx; |
| 1418 | firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine); |
| 1419 | |
| 1420 | /* TRANSLATORS: get interactive prompt */ |
| 1421 | g_print ("%s\n", _("Choose a firmware type:")); |
| 1422 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 1423 | g_print ("0.\t%s\n", _("Cancel")); |
| 1424 | for (guint i = 0; i < firmware_types->len; i++) { |
| 1425 | const gchar *id = g_ptr_array_index (firmware_types, i); |
| 1426 | g_print ("%u.\t%s\n", i + 1, id); |
| 1427 | } |
| 1428 | idx = fu_util_prompt_for_number (firmware_types->len); |
| 1429 | if (idx == 0) { |
| 1430 | g_set_error_literal (error, |
| 1431 | FWUPD_ERROR, |
| 1432 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1433 | "Request canceled"); |
| 1434 | return NULL; |
| 1435 | } |
| 1436 | |
| 1437 | return g_strdup (g_ptr_array_index (firmware_types, idx - 1)); |
| 1438 | } |
| 1439 | |
| 1440 | static gboolean |
| 1441 | fu_util_firmware_parse (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1442 | { |
| 1443 | GType gtype; |
| 1444 | g_autoptr(GBytes) blob = NULL; |
| 1445 | g_autoptr(FuFirmware) firmware = NULL; |
| 1446 | g_autofree gchar *firmware_type = NULL; |
| 1447 | g_autofree gchar *str = NULL; |
| 1448 | |
| 1449 | /* check args */ |
| 1450 | if (g_strv_length (values) == 0 || g_strv_length (values) > 2) { |
| 1451 | g_set_error_literal (error, |
| 1452 | FWUPD_ERROR, |
| 1453 | FWUPD_ERROR_INVALID_ARGS, |
| 1454 | "Invalid arguments: filename required"); |
| 1455 | return FALSE; |
| 1456 | } |
| 1457 | |
| 1458 | if (g_strv_length (values) == 2) |
| 1459 | firmware_type = g_strdup (values[1]); |
| 1460 | |
| 1461 | /* load file */ |
| 1462 | blob = fu_common_get_contents_bytes (values[0], error); |
| 1463 | if (blob == NULL) |
| 1464 | return FALSE; |
| 1465 | |
| 1466 | /* load engine */ |
| 1467 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error)) |
| 1468 | return FALSE; |
| 1469 | |
| 1470 | /* find the GType to use */ |
| 1471 | if (firmware_type == NULL) |
| 1472 | firmware_type = fu_util_prompt_for_firmware_type (priv, error); |
| 1473 | if (firmware_type == NULL) |
| 1474 | return FALSE; |
| 1475 | gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type); |
| 1476 | if (gtype == G_TYPE_INVALID) { |
| 1477 | g_set_error (error, |
| 1478 | G_IO_ERROR, |
| 1479 | G_IO_ERROR_NOT_FOUND, |
| 1480 | "GType %s not supported", firmware_type); |
| 1481 | return FALSE; |
| 1482 | } |
| 1483 | firmware = g_object_new (gtype, NULL); |
| 1484 | if (!fu_firmware_parse (firmware, blob, priv->flags, error)) |
| 1485 | return FALSE; |
| 1486 | str = fu_firmware_to_string (firmware); |
| 1487 | g_print ("%s", str); |
| 1488 | return TRUE; |
| 1489 | } |
| 1490 | |
| 1491 | static gboolean |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1492 | fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1493 | { |
| 1494 | g_autofree gchar *str = NULL; |
| 1495 | g_autoptr(FuDevice) dev = NULL; |
| 1496 | |
| 1497 | /* load engine */ |
| 1498 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1499 | return FALSE; |
| 1500 | |
| 1501 | /* get device */ |
| 1502 | if (g_strv_length (values) == 1) { |
| 1503 | dev = fu_engine_get_device (priv->engine, values[1], error); |
| 1504 | if (dev == NULL) |
| 1505 | return FALSE; |
| 1506 | } else { |
| 1507 | dev = fu_util_prompt_for_device (priv, error); |
| 1508 | if (dev == NULL) |
| 1509 | return FALSE; |
| 1510 | } |
| 1511 | |
| 1512 | /* add checksums */ |
| 1513 | if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error)) |
| 1514 | return FALSE; |
| 1515 | |
| 1516 | /* show checksums */ |
| 1517 | str = fu_device_to_string (dev); |
| 1518 | g_print ("%s\n", str); |
| 1519 | return TRUE; |
| 1520 | } |
| 1521 | |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1522 | static gboolean |
| 1523 | fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1524 | { |
| 1525 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 1526 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 1527 | g_autofree gchar *title = fu_util_get_tree_title (priv); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1528 | |
| 1529 | /* load engine */ |
| 1530 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1531 | return FALSE; |
| 1532 | |
| 1533 | /* get all devices from the history database */ |
| 1534 | devices = fu_engine_get_history (priv->engine, error); |
| 1535 | if (devices == NULL) |
| 1536 | return FALSE; |
| 1537 | |
| 1538 | /* show each device */ |
| 1539 | for (guint i = 0; i < devices->len; i++) { |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 1540 | g_autoptr(GPtrArray) rels = NULL; |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1541 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 1542 | FwupdRelease *rel; |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 1543 | const gchar *remote; |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 1544 | GNode *child; |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 1545 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1546 | if (!fu_util_filter_device (priv, dev)) |
| 1547 | continue; |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 1548 | child = g_node_append_data (root, dev); |
| 1549 | |
| 1550 | rel = fwupd_device_get_release_default (dev); |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 1551 | if (rel == NULL) |
| 1552 | continue; |
| 1553 | remote = fwupd_release_get_remote_id (rel); |
| 1554 | |
| 1555 | /* doesn't actually map to remote */ |
| 1556 | if (remote == NULL) { |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 1557 | g_node_append_data (child, rel); |
Mario Limonciello | ce94d16 | 2019-09-20 13:37:36 -0500 | [diff] [blame] | 1558 | continue; |
| 1559 | } |
| 1560 | |
| 1561 | /* try to lookup releases from client */ |
| 1562 | rels = fu_engine_get_releases (priv->engine, fwupd_device_get_id (dev), error); |
| 1563 | if (rels == NULL) |
| 1564 | return FALSE; |
| 1565 | |
| 1566 | /* map to a release in client */ |
| 1567 | for (guint j = 0; j < rels->len; j++) { |
| 1568 | FwupdRelease *rel2 = g_ptr_array_index (rels, j); |
| 1569 | if (g_strcmp0 (remote, |
| 1570 | fwupd_release_get_remote_id (rel2)) != 0) |
| 1571 | continue; |
| 1572 | if (g_strcmp0 (fwupd_release_get_version (rel), |
| 1573 | fwupd_release_get_version (rel2)) != 0) |
| 1574 | continue; |
| 1575 | g_node_append_data (child, g_object_ref (rel2)); |
| 1576 | rel = NULL; |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | /* didn't match anything */ |
| 1581 | if (rels->len == 0 || rel != NULL) { |
| 1582 | g_node_append_data (child, rel); |
| 1583 | continue; |
| 1584 | } |
Mario Limonciello | 3be596b | 2019-09-20 10:10:08 -0500 | [diff] [blame] | 1585 | |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1586 | } |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 1587 | fu_util_print_tree (root, title); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1588 | |
| 1589 | return TRUE; |
| 1590 | } |
| 1591 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1592 | int |
| 1593 | main (int argc, char *argv[]) |
| 1594 | { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1595 | gboolean allow_older = FALSE; |
| 1596 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1597 | gboolean force = FALSE; |
| 1598 | gboolean ret; |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1599 | gboolean version = FALSE; |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1600 | gboolean interactive = isatty (fileno (stdout)) != 0; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1601 | g_auto(GStrv) plugin_glob = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1602 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 1603 | g_autoptr(GError) error = NULL; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1604 | g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new (); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1605 | g_autofree gchar *cmd_descriptions = NULL; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1606 | g_autofree gchar *filter = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1607 | const GOptionEntry options[] = { |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1608 | { "version", '\0', 0, G_OPTION_ARG_NONE, &version, |
| 1609 | /* TRANSLATORS: command line option */ |
| 1610 | _("Show client and daemon versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1611 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 1612 | /* TRANSLATORS: command line option */ |
Mario Limonciello | 350fc4c | 2019-10-03 07:08:51 -0500 | [diff] [blame] | 1613 | _("Allow reinstalling existing firmware versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1614 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 1615 | /* TRANSLATORS: command line option */ |
| 1616 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1617 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 1618 | /* TRANSLATORS: command line option */ |
| 1619 | _("Override plugin warning"), NULL }, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1620 | { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check, |
| 1621 | /* TRANSLATORS: command line option */ |
| 1622 | _("Do not check for reboot after update"), NULL }, |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 1623 | { "no-safety-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_safety_check, |
| 1624 | /* TRANSLATORS: command line option */ |
| 1625 | _("Do not perform device safety checks"), NULL }, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 1626 | { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices, |
| 1627 | /* TRANSLATORS: command line option */ |
| 1628 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1629 | { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
| 1630 | /* TRANSLATORS: command line option */ |
| 1631 | _("Manually whitelist specific plugins"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1632 | { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1633 | /* TRANSLATORS: command line option */ |
| 1634 | _("Run the plugin composite prepare routine when using install-blob"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1635 | { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1636 | /* TRANSLATORS: command line option */ |
| 1637 | _("Run the plugin composite cleanup routine when using install-blob"), NULL }, |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1638 | { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state, |
| 1639 | /* TRANSLATORS: command line option */ |
| 1640 | _("Save device state into a JSON file between executions"), NULL }, |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 1641 | { "disable-ssl-strict", '\0', 0, G_OPTION_ARG_NONE, &priv->disable_ssl_strict, |
| 1642 | /* TRANSLATORS: command line option */ |
| 1643 | _("Ignore SSL strict checks when downloading files"), NULL }, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1644 | { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter, |
| 1645 | /* TRANSLATORS: command line option */ |
| 1646 | _("Filter with a set of device flags using a ~ prefix to " |
| 1647 | "exclude, e.g. 'internal,~needs-reboot'"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1648 | { NULL} |
| 1649 | }; |
| 1650 | |
| 1651 | setlocale (LC_ALL, ""); |
| 1652 | |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1653 | bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1654 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1655 | textdomain (GETTEXT_PACKAGE); |
| 1656 | |
Richard Hughes | 01c0bad | 2019-11-22 09:08:51 +0000 | [diff] [blame] | 1657 | #ifdef HAVE_GETUID |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1658 | /* ensure root user */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1659 | if (interactive && (getuid () != 0 || geteuid () != 0)) |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1660 | /* TRANSLATORS: we're poking around as a power user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 1661 | g_printerr ("%s\n", _("This program may only work correctly as root")); |
Richard Hughes | 01c0bad | 2019-11-22 09:08:51 +0000 | [diff] [blame] | 1662 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* create helper object */ |
| 1665 | priv->loop = g_main_loop_new (NULL, FALSE); |
| 1666 | priv->progressbar = fu_progressbar_new (); |
| 1667 | |
| 1668 | /* add commands */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1669 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1670 | "build-firmware", |
| 1671 | "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]", |
| 1672 | /* TRANSLATORS: command description */ |
| 1673 | _("Build firmware using a sandbox"), |
| 1674 | fu_util_firmware_builder); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1675 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1676 | "smbios-dump", |
| 1677 | "FILE", |
| 1678 | /* TRANSLATORS: command description */ |
| 1679 | _("Dump SMBIOS data from a file"), |
| 1680 | fu_util_smbios_dump); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1681 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 1682 | "get-plugins", |
| 1683 | NULL, |
| 1684 | /* TRANSLATORS: command description */ |
| 1685 | _("Get all enabled plugins registered with the system"), |
| 1686 | fu_util_get_plugins); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1687 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 1688 | "get-details", |
| 1689 | NULL, |
| 1690 | /* TRANSLATORS: command description */ |
| 1691 | _("Gets details about a firmware file"), |
| 1692 | fu_util_get_details); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1693 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1694 | "get-history", |
| 1695 | NULL, |
| 1696 | /* TRANSLATORS: command description */ |
| 1697 | _("Show history of firmware updates"), |
| 1698 | fu_util_get_history); |
| 1699 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 1700 | "get-updates,get-upgrades", |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 1701 | NULL, |
| 1702 | /* TRANSLATORS: command description */ |
| 1703 | _("Gets the list of updates for connected hardware"), |
| 1704 | fu_util_get_updates); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1705 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 1706 | "get-devices,get-topology", |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1707 | NULL, |
| 1708 | /* TRANSLATORS: command description */ |
| 1709 | _("Get all devices that support firmware updates"), |
| 1710 | fu_util_get_devices); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1711 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1712 | "get-device-flags", |
| 1713 | NULL, |
| 1714 | /* TRANSLATORS: command description */ |
| 1715 | _("Get all device flags supported by fwupd"), |
| 1716 | fu_util_get_device_flags); |
| 1717 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1718 | "watch", |
| 1719 | NULL, |
| 1720 | /* TRANSLATORS: command description */ |
Piotr DrÄ…g | 472fa59 | 2018-06-06 14:53:48 +0200 | [diff] [blame] | 1721 | _("Watch for hardware changes"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1722 | fu_util_watch); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1723 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1724 | "install-blob", |
| 1725 | "FILENAME DEVICE-ID", |
| 1726 | /* TRANSLATORS: command description */ |
| 1727 | _("Install a firmware blob on a device"), |
| 1728 | fu_util_install_blob); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1729 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1730 | "install", |
| 1731 | "FILE [ID]", |
| 1732 | /* TRANSLATORS: command description */ |
| 1733 | _("Install a firmware file on this hardware"), |
| 1734 | fu_util_install); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1735 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1736 | "attach", |
| 1737 | "DEVICE-ID", |
| 1738 | /* TRANSLATORS: command description */ |
| 1739 | _("Attach to firmware mode"), |
| 1740 | fu_util_attach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1741 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1742 | "detach", |
| 1743 | "DEVICE-ID", |
| 1744 | /* TRANSLATORS: command description */ |
| 1745 | _("Detach to bootloader mode"), |
| 1746 | fu_util_detach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1747 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1748 | "activate", |
| 1749 | "[DEVICE-ID]", |
| 1750 | /* TRANSLATORS: command description */ |
| 1751 | _("Activate pending devices"), |
| 1752 | fu_util_activate); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1753 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1754 | "hwids", |
| 1755 | "[FILE]", |
| 1756 | /* TRANSLATORS: command description */ |
| 1757 | _("Return all the hardware IDs for the machine"), |
| 1758 | fu_util_hwids); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1759 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1760 | "monitor", |
| 1761 | NULL, |
| 1762 | /* TRANSLATORS: command description */ |
| 1763 | _("Monitor the daemon for events"), |
| 1764 | fu_util_monitor); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1765 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 1766 | "update,upgrade", |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1767 | NULL, |
| 1768 | /* TRANSLATORS: command description */ |
| 1769 | _("Update all devices that match local metadata"), |
| 1770 | fu_util_update); |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1771 | fu_util_cmd_array_add (cmd_array, |
| 1772 | "self-sign", |
| 1773 | "TEXT", |
| 1774 | /* TRANSLATORS: command description */ |
Richard Hughes | 12a021d | 2019-03-27 09:23:24 +0000 | [diff] [blame] | 1775 | C_("command-description", |
| 1776 | "Sign data using the client certificate"), |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1777 | fu_util_self_sign); |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1778 | fu_util_cmd_array_add (cmd_array, |
| 1779 | "verify-update", |
| 1780 | "[DEVICE_ID]", |
| 1781 | /* TRANSLATORS: command description */ |
| 1782 | _("Update the stored metadata with current contents"), |
| 1783 | fu_util_verify_update); |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1784 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | a58510b | 2019-10-30 10:03:12 +0000 | [diff] [blame] | 1785 | "firmware-read", |
| 1786 | "FILENAME [DEVICE-ID]", |
| 1787 | /* TRANSLATORS: command description */ |
| 1788 | _("Read a firmware blob from a device"), |
| 1789 | fu_util_firmware_read); |
| 1790 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 95c98a9 | 2019-10-22 16:03:15 +0100 | [diff] [blame] | 1791 | "firmware-parse", |
| 1792 | "FILENAME [FIRMWARE_TYPE]", |
| 1793 | /* TRANSLATORS: command description */ |
| 1794 | _("Parse and show details about a firmware file"), |
| 1795 | fu_util_firmware_parse); |
| 1796 | fu_util_cmd_array_add (cmd_array, |
| 1797 | "get-firmware-types", |
| 1798 | NULL, |
| 1799 | /* TRANSLATORS: command description */ |
| 1800 | _("List the available firmware types"), |
| 1801 | fu_util_get_firmware_types); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1802 | |
| 1803 | /* do stuff on ctrl+c */ |
| 1804 | priv->cancellable = g_cancellable_new (); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 1805 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1806 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1807 | SIGINT, fu_util_sigint_cb, |
| 1808 | priv, NULL); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 1809 | #endif |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1810 | g_signal_connect (priv->cancellable, "cancelled", |
| 1811 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1812 | |
| 1813 | /* sort by command name */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1814 | fu_util_cmd_array_sort (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1815 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1816 | /* non-TTY consoles cannot answer questions */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1817 | if (!interactive) { |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1818 | priv->no_reboot_check = TRUE; |
Mario Limonciello | 98b9516 | 2019-10-30 09:20:43 -0500 | [diff] [blame] | 1819 | priv->no_safety_check = TRUE; |
Mario Limonciello | 9b31e6f | 2019-01-31 15:42:19 -0600 | [diff] [blame] | 1820 | fu_progressbar_set_interactive (priv->progressbar, FALSE); |
| 1821 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1822 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1823 | /* get a list of the commands */ |
| 1824 | priv->context = g_option_context_new (NULL); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1825 | cmd_descriptions = fu_util_cmd_array_to_string (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1826 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 1827 | g_option_context_set_description (priv->context, |
| 1828 | "This tool allows an administrator to use the fwupd plugins " |
| 1829 | "without being installed on the host system."); |
| 1830 | |
| 1831 | /* TRANSLATORS: program name */ |
| 1832 | g_set_application_name (_("Firmware Utility")); |
| 1833 | g_option_context_add_main_entries (priv->context, options, NULL); |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 1834 | g_option_context_add_group (priv->context, fu_debug_get_option_group ()); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1835 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 1836 | if (!ret) { |
| 1837 | /* TRANSLATORS: the user didn't read the man page */ |
| 1838 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 1839 | error->message); |
| 1840 | return EXIT_FAILURE; |
| 1841 | } |
| 1842 | |
Richard Hughes | 0e46b22 | 2019-09-05 12:13:35 +0100 | [diff] [blame] | 1843 | /* allow disabling SSL strict mode for broken corporate proxies */ |
| 1844 | if (priv->disable_ssl_strict) { |
| 1845 | /* TRANSLATORS: try to help */ |
| 1846 | g_printerr ("%s\n", _("WARNING: Ignoring SSL strict checks, " |
| 1847 | "to do this automatically in the future " |
| 1848 | "export DISABLE_SSL_STRICT in your environment")); |
| 1849 | g_setenv ("DISABLE_SSL_STRICT", "1", TRUE); |
| 1850 | } |
| 1851 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1852 | /* parse filter flags */ |
| 1853 | if (filter != NULL) { |
| 1854 | if (!fu_util_parse_filter_flags (filter, |
| 1855 | &priv->filter_include, |
| 1856 | &priv->filter_exclude, |
| 1857 | &error)) { |
| 1858 | /* TRANSLATORS: the user didn't read the man page */ |
| 1859 | g_print ("%s: %s\n", _("Failed to parse flags for --filter"), |
| 1860 | error->message); |
| 1861 | return EXIT_FAILURE; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1866 | /* set flags */ |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1867 | if (allow_reinstall) |
| 1868 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 1869 | if (allow_older) |
| 1870 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
| 1871 | if (force) |
| 1872 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
| 1873 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1874 | /* load engine */ |
| 1875 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 1876 | g_signal_connect (priv->engine, "device-added", |
| 1877 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1878 | priv); |
| 1879 | g_signal_connect (priv->engine, "device-removed", |
| 1880 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1881 | priv); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1882 | g_signal_connect (priv->engine, "status-changed", |
| 1883 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1884 | priv); |
| 1885 | g_signal_connect (priv->engine, "percentage-changed", |
| 1886 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1887 | priv); |
| 1888 | |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1889 | /* just show versions and exit */ |
| 1890 | if (version) { |
| 1891 | g_autofree gchar *version_str = fu_util_get_versions (); |
| 1892 | g_print ("%s\n", version_str); |
| 1893 | return EXIT_SUCCESS; |
| 1894 | } |
| 1895 | |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1896 | /* any plugin whitelist specified */ |
| 1897 | for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++) |
| 1898 | fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]); |
| 1899 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1900 | /* run the specified command */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1901 | 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] | 1902 | if (!ret) { |
| 1903 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
| 1904 | g_autofree gchar *tmp = NULL; |
| 1905 | tmp = g_option_context_get_help (priv->context, TRUE, NULL); |
| 1906 | g_print ("%s\n\n%s", error->message, tmp); |
| 1907 | return EXIT_FAILURE; |
| 1908 | } |
| 1909 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1910 | g_print ("%s\n", error->message); |
| 1911 | return EXIT_NOTHING_TO_DO; |
| 1912 | } |
| 1913 | g_print ("%s\n", error->message); |
| 1914 | return EXIT_FAILURE; |
| 1915 | } |
| 1916 | |
| 1917 | /* success */ |
| 1918 | return EXIT_SUCCESS; |
| 1919 | } |