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