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