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