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