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> |
| 12 | #include <glib/gi18n.h> |
| 13 | #include <glib-unix.h> |
| 14 | #include <locale.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 17 | #include <libsoup/soup.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 18 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 19 | #include "fu-engine.h" |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 20 | #include "fu-plugin-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 21 | #include "fu-progressbar.h" |
| 22 | #include "fu-smbios.h" |
| 23 | #include "fu-util-common.h" |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 24 | #include "fu-debug.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 25 | |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 26 | #define SYSTEMD_SERVICE "org.freedesktop.systemd1" |
| 27 | #define SYSTEMD_OBJECT_PATH "/org/freedesktop/systemd1" |
| 28 | #define SYSTEMD_MANAGER_INTERFACE "org.freedesktop.systemd1.Manager" |
| 29 | #define SYSTEMD_FWUPD_UNIT "fwupd.service" |
| 30 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 31 | /* this is only valid in this file */ |
| 32 | #define FWUPD_ERROR_INVALID_ARGS (FWUPD_ERROR_LAST+1) |
| 33 | |
| 34 | /* custom return code */ |
| 35 | #define EXIT_NOTHING_TO_DO 2 |
| 36 | |
| 37 | typedef struct { |
| 38 | GCancellable *cancellable; |
| 39 | GMainLoop *loop; |
| 40 | GOptionContext *context; |
| 41 | GPtrArray *cmd_array; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 42 | FuEngine *engine; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 43 | FuProgressbar *progressbar; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 44 | FwupdInstallFlags flags; |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 45 | gboolean show_all_devices; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 46 | /* only valid in update and downgrade */ |
| 47 | FwupdDevice *current_device; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 48 | } FuUtilPrivate; |
| 49 | |
| 50 | typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util, |
| 51 | gchar **values, |
| 52 | GError **error); |
| 53 | |
| 54 | typedef struct { |
| 55 | gchar *name; |
| 56 | gchar *arguments; |
| 57 | gchar *description; |
| 58 | FuUtilPrivateCb callback; |
| 59 | } FuUtilItem; |
| 60 | |
| 61 | static void |
| 62 | fu_util_item_free (FuUtilItem *item) |
| 63 | { |
| 64 | g_free (item->name); |
| 65 | g_free (item->arguments); |
| 66 | g_free (item->description); |
| 67 | g_free (item); |
| 68 | } |
| 69 | |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 70 | static gboolean |
| 71 | fu_util_start_engine (FuUtilPrivate *priv, GError **error) |
| 72 | { |
| 73 | #ifdef HAVE_SYSTEMD |
| 74 | g_autoptr(GDBusConnection) connection = NULL; |
| 75 | g_autoptr(GDBusProxy) proxy = NULL; |
| 76 | g_autoptr(GVariant) val = NULL; |
| 77 | g_autoptr(GError) error_local = NULL; |
| 78 | |
| 79 | /* try to stop any already running daemon */ |
| 80 | connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error); |
| 81 | if (connection == NULL) |
| 82 | return FALSE; |
| 83 | proxy = g_dbus_proxy_new_sync (connection, |
| 84 | G_DBUS_PROXY_FLAGS_NONE, |
| 85 | NULL, |
| 86 | SYSTEMD_SERVICE, |
| 87 | SYSTEMD_OBJECT_PATH, |
| 88 | SYSTEMD_MANAGER_INTERFACE, |
| 89 | NULL, |
| 90 | error); |
| 91 | if (proxy == NULL) |
| 92 | return FALSE; |
| 93 | val = g_dbus_proxy_call_sync (proxy, |
| 94 | "GetUnit", |
| 95 | g_variant_new ("(s)", |
| 96 | SYSTEMD_FWUPD_UNIT), |
| 97 | G_DBUS_CALL_FLAGS_NONE, |
| 98 | -1, |
| 99 | NULL, |
| 100 | &error_local); |
| 101 | if (val == NULL) { |
| 102 | g_debug ("Unable to find %s: %s", |
| 103 | SYSTEMD_FWUPD_UNIT, |
| 104 | error_local->message); |
| 105 | } else { |
| 106 | g_clear_object (&val); |
| 107 | val = g_dbus_proxy_call_sync (proxy, |
| 108 | "StopUnit", |
| 109 | g_variant_new ("(ss)", |
| 110 | SYSTEMD_FWUPD_UNIT, |
| 111 | "replace"), |
| 112 | G_DBUS_CALL_FLAGS_NONE, |
| 113 | -1, |
| 114 | NULL, |
| 115 | error); |
| 116 | if (val == NULL) |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | #endif |
| 121 | return fu_engine_load (priv->engine, error); |
| 122 | } |
| 123 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 124 | static gint |
| 125 | fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2) |
| 126 | { |
| 127 | return g_strcmp0 ((*item1)->name, (*item2)->name); |
| 128 | } |
| 129 | |
| 130 | static void |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 131 | fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error) |
| 132 | { |
| 133 | g_autofree gchar *path = g_path_get_dirname (value); |
| 134 | if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { |
| 135 | g_prefix_error (error, |
| 136 | "Unable to access %s. You may need to copy %s to %s: ", |
| 137 | path, value, g_getenv ("HOME")); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 142 | fu_util_add (GPtrArray *array, |
| 143 | const gchar *name, |
| 144 | const gchar *arguments, |
| 145 | const gchar *description, |
| 146 | FuUtilPrivateCb callback) |
| 147 | { |
| 148 | g_auto(GStrv) names = NULL; |
| 149 | |
| 150 | g_return_if_fail (name != NULL); |
| 151 | g_return_if_fail (description != NULL); |
| 152 | g_return_if_fail (callback != NULL); |
| 153 | |
| 154 | /* add each one */ |
| 155 | names = g_strsplit (name, ",", -1); |
| 156 | for (guint i = 0; names[i] != NULL; i++) { |
| 157 | FuUtilItem *item = g_new0 (FuUtilItem, 1); |
| 158 | item->name = g_strdup (names[i]); |
| 159 | if (i == 0) { |
| 160 | item->description = g_strdup (description); |
| 161 | } else { |
| 162 | /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */ |
| 163 | item->description = g_strdup_printf (_("Alias to %s"), |
| 164 | names[0]); |
| 165 | } |
| 166 | item->arguments = g_strdup (arguments); |
| 167 | item->callback = callback; |
| 168 | g_ptr_array_add (array, item); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static gchar * |
| 173 | fu_util_get_descriptions (GPtrArray *array) |
| 174 | { |
| 175 | gsize len; |
| 176 | const gsize max_len = 35; |
| 177 | GString *string; |
| 178 | |
| 179 | /* print each command */ |
| 180 | string = g_string_new (""); |
| 181 | for (guint i = 0; i < array->len; i++) { |
| 182 | FuUtilItem *item = g_ptr_array_index (array, i); |
| 183 | g_string_append (string, " "); |
| 184 | g_string_append (string, item->name); |
| 185 | len = strlen (item->name) + 2; |
| 186 | if (item->arguments != NULL) { |
| 187 | g_string_append (string, " "); |
| 188 | g_string_append (string, item->arguments); |
| 189 | len += strlen (item->arguments) + 1; |
| 190 | } |
| 191 | if (len < max_len) { |
| 192 | for (gsize j = len; j < max_len + 1; j++) |
| 193 | g_string_append_c (string, ' '); |
| 194 | g_string_append (string, item->description); |
| 195 | g_string_append_c (string, '\n'); |
| 196 | } else { |
| 197 | g_string_append_c (string, '\n'); |
| 198 | for (gsize j = 0; j < max_len + 1; j++) |
| 199 | g_string_append_c (string, ' '); |
| 200 | g_string_append (string, item->description); |
| 201 | g_string_append_c (string, '\n'); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* remove trailing newline */ |
| 206 | if (string->len > 0) |
| 207 | g_string_set_size (string, string->len - 1); |
| 208 | |
| 209 | return g_string_free (string, FALSE); |
| 210 | } |
| 211 | |
| 212 | static gboolean |
| 213 | fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error) |
| 214 | { |
| 215 | /* find command */ |
| 216 | for (guint i = 0; i < priv->cmd_array->len; i++) { |
| 217 | FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i); |
| 218 | if (g_strcmp0 (item->name, command) == 0) |
| 219 | return item->callback (priv, values, error); |
| 220 | } |
| 221 | |
| 222 | /* not found */ |
| 223 | g_set_error_literal (error, |
| 224 | FWUPD_ERROR, |
| 225 | FWUPD_ERROR_INVALID_ARGS, |
| 226 | /* TRANSLATORS: error message */ |
| 227 | _("Command not found")); |
| 228 | return FALSE; |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data) |
| 233 | { |
| 234 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 235 | /* TRANSLATORS: this is when a device ctrl+c's a watch */ |
| 236 | g_print ("%s\n", _("Cancelled")); |
| 237 | g_main_loop_quit (priv->loop); |
| 238 | } |
| 239 | |
| 240 | static gboolean |
| 241 | fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
| 242 | { |
| 243 | g_autofree gchar *tmp = NULL; |
| 244 | g_autoptr(FuSmbios) smbios = NULL; |
| 245 | if (g_strv_length (values) < 1) { |
| 246 | g_set_error_literal (error, |
| 247 | FWUPD_ERROR, |
| 248 | FWUPD_ERROR_INVALID_ARGS, |
| 249 | "Invalid arguments"); |
| 250 | return FALSE; |
| 251 | } |
| 252 | smbios = fu_smbios_new (); |
| 253 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 254 | return FALSE; |
| 255 | tmp = fu_smbios_to_string (smbios); |
| 256 | g_print ("%s\n", tmp); |
| 257 | return TRUE; |
| 258 | } |
| 259 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 260 | static gboolean |
| 261 | fu_util_sigint_cb (gpointer user_data) |
| 262 | { |
| 263 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 264 | g_debug ("Handling SIGINT"); |
| 265 | g_cancellable_cancel (priv->cancellable); |
| 266 | return FALSE; |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | fu_util_private_free (FuUtilPrivate *priv) |
| 271 | { |
| 272 | if (priv->cmd_array != NULL) |
| 273 | g_ptr_array_unref (priv->cmd_array); |
Mario Limonciello | cc50e1a | 2018-08-14 17:45:24 -0500 | [diff] [blame] | 274 | if (priv->current_device != NULL) |
| 275 | g_object_unref (priv->current_device); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 276 | if (priv->engine != NULL) |
| 277 | g_object_unref (priv->engine); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 278 | if (priv->loop != NULL) |
| 279 | g_main_loop_unref (priv->loop); |
| 280 | if (priv->cancellable != NULL) |
| 281 | g_object_unref (priv->cancellable); |
| 282 | if (priv->progressbar != NULL) |
| 283 | g_object_unref (priv->progressbar); |
| 284 | if (priv->context != NULL) |
| 285 | g_option_context_free (priv->context); |
| 286 | g_free (priv); |
| 287 | } |
| 288 | |
| 289 | #pragma clang diagnostic push |
| 290 | #pragma clang diagnostic ignored "-Wunused-function" |
| 291 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free) |
| 292 | #pragma clang diagnostic pop |
| 293 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 294 | |
| 295 | static void |
| 296 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 297 | FuDevice *device, |
| 298 | FuUtilPrivate *priv) |
| 299 | { |
| 300 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 301 | g_debug ("ADDED:\n%s", tmp); |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 306 | FuDevice *device, |
| 307 | FuUtilPrivate *priv) |
| 308 | { |
| 309 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 310 | g_debug ("REMOVED:\n%s", tmp); |
| 311 | } |
| 312 | |
| 313 | static void |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 314 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 315 | FwupdStatus status, |
| 316 | FuUtilPrivate *priv) |
| 317 | { |
| 318 | fu_progressbar_update (priv->progressbar, status, 0); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 323 | guint percentage, |
| 324 | FuUtilPrivate *priv) |
| 325 | { |
| 326 | fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage); |
| 327 | } |
| 328 | |
| 329 | static gboolean |
| 330 | fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 331 | { |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 332 | if (!fu_util_start_engine (priv, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 333 | return FALSE; |
| 334 | g_main_loop_run (priv->loop); |
| 335 | return TRUE; |
| 336 | } |
| 337 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 338 | static gint |
| 339 | fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2) |
| 340 | { |
| 341 | return fu_plugin_name_compare (*item1, *item2); |
| 342 | } |
| 343 | |
| 344 | static gboolean |
| 345 | fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error) |
| 346 | { |
| 347 | GPtrArray *plugins; |
| 348 | guint cnt = 0; |
| 349 | |
| 350 | /* load engine */ |
| 351 | if (!fu_engine_load_plugins (priv->engine, error)) |
| 352 | return FALSE; |
| 353 | |
| 354 | /* print */ |
| 355 | plugins = fu_engine_get_plugins (priv->engine); |
| 356 | g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb); |
| 357 | for (guint i = 0; i < plugins->len; i++) { |
| 358 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
| 359 | if (!fu_plugin_get_enabled (plugin)) |
| 360 | continue; |
| 361 | g_print ("%s\n", fu_plugin_get_name (plugin)); |
| 362 | cnt++; |
| 363 | } |
| 364 | if (cnt == 0) { |
| 365 | /* TRANSLATORS: nothing found */ |
| 366 | g_print ("%s\n", _("No plugins found")); |
| 367 | return TRUE; |
| 368 | } |
| 369 | |
| 370 | return TRUE; |
| 371 | } |
| 372 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 373 | static gboolean |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 374 | fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error) |
| 375 | { |
| 376 | g_autoptr(GPtrArray) array = NULL; |
| 377 | gint fd; |
| 378 | |
| 379 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 380 | if (!fu_util_start_engine (priv, error)) |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 381 | return FALSE; |
| 382 | |
| 383 | /* check args */ |
| 384 | if (g_strv_length (values) != 1) { |
| 385 | g_set_error_literal (error, |
| 386 | FWUPD_ERROR, |
| 387 | FWUPD_ERROR_INVALID_ARGS, |
| 388 | "Invalid arguments"); |
| 389 | return FALSE; |
| 390 | } |
| 391 | |
| 392 | /* open file */ |
| 393 | fd = open (values[0], O_RDONLY); |
| 394 | if (fd < 0) { |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 395 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 396 | g_set_error (error, |
| 397 | FWUPD_ERROR, |
| 398 | FWUPD_ERROR_INVALID_FILE, |
| 399 | "failed to open %s", |
| 400 | values[0]); |
| 401 | return FALSE; |
| 402 | } |
| 403 | array = fu_engine_get_details (priv->engine, fd, error); |
| 404 | close (fd); |
| 405 | |
| 406 | if (array == NULL) |
| 407 | return FALSE; |
| 408 | for (guint i = 0; i < array->len; i++) { |
| 409 | FwupdDevice *dev = g_ptr_array_index (array, i); |
| 410 | g_autofree gchar *tmp = NULL; |
| 411 | tmp = fwupd_device_to_string (dev); |
Mario Limonciello | ece90a4 | 2018-07-25 17:35:55 -0500 | [diff] [blame] | 412 | g_print ("%s\n", tmp); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 413 | } |
| 414 | return TRUE; |
| 415 | } |
| 416 | |
| 417 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 418 | fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error) |
| 419 | { |
| 420 | g_autoptr(GPtrArray) devs = NULL; |
| 421 | |
| 422 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 423 | if (!fu_util_start_engine (priv, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 424 | return FALSE; |
| 425 | |
| 426 | /* print */ |
| 427 | devs = fu_engine_get_devices (priv->engine, error); |
| 428 | if (devs == NULL) |
| 429 | return FALSE; |
| 430 | if (devs->len == 0) { |
| 431 | /* TRANSLATORS: nothing attached */ |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 432 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 433 | return TRUE; |
| 434 | } |
| 435 | for (guint i = 0; i < devs->len; i++) { |
| 436 | FwupdDevice *dev = g_ptr_array_index (devs, i); |
Mario Limonciello | d1775bc | 2018-07-17 00:28:52 -0500 | [diff] [blame] | 437 | if (priv->show_all_devices || fu_util_is_interesting_device (dev)) { |
| 438 | g_autofree gchar *tmp = fwupd_device_to_string (dev); |
| 439 | g_print ("%s\n", tmp); |
| 440 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return TRUE; |
| 444 | } |
| 445 | |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 446 | static void |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 447 | 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] | 448 | { |
| 449 | for (guint i = 0; i < devs->len; i++) { |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 450 | FuDevice *dev_tmp = g_ptr_array_index (devs, i); |
Mario Limonciello | d1775bc | 2018-07-17 00:28:52 -0500 | [diff] [blame] | 451 | if (!priv->show_all_devices && |
| 452 | !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp))) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 453 | continue; |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 454 | if (fu_device_get_parent (dev_tmp) == dev) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 455 | GNode *child = g_node_append_data (root, dev_tmp); |
| 456 | fu_util_build_device_tree (priv, child, devs, dev_tmp); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static gboolean |
| 462 | fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error) |
| 463 | { |
| 464 | g_autoptr(GNode) root = g_node_new (NULL); |
| 465 | g_autoptr(GPtrArray) devs = NULL; |
| 466 | |
| 467 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 468 | if (!fu_util_start_engine (priv, error)) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 469 | return FALSE; |
| 470 | |
| 471 | /* print */ |
| 472 | devs = fu_engine_get_devices (priv->engine, error); |
| 473 | if (devs == NULL) |
| 474 | return FALSE; |
| 475 | |
| 476 | /* print */ |
| 477 | if (devs->len == 0) { |
| 478 | /* TRANSLATORS: nothing attached that can be upgraded */ |
| 479 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
| 480 | return TRUE; |
| 481 | } |
| 482 | fu_util_build_device_tree (priv, root, devs, NULL); |
| 483 | g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, |
| 484 | fu_util_print_device_tree, priv); |
| 485 | |
| 486 | return TRUE; |
| 487 | } |
| 488 | |
| 489 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 490 | static FuDevice * |
| 491 | fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) |
| 492 | { |
| 493 | FuDevice *dev; |
| 494 | guint idx; |
| 495 | g_autoptr(GPtrArray) devices = NULL; |
| 496 | |
| 497 | /* get devices from daemon */ |
| 498 | devices = fu_engine_get_devices (priv->engine, error); |
| 499 | if (devices == NULL) |
| 500 | return NULL; |
| 501 | |
| 502 | /* exactly one */ |
| 503 | if (devices->len == 1) { |
| 504 | dev = g_ptr_array_index (devices, 0); |
| 505 | return g_object_ref (dev); |
| 506 | } |
| 507 | |
| 508 | /* TRANSLATORS: get interactive prompt */ |
| 509 | g_print ("%s\n", _("Choose a device:")); |
| 510 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 511 | g_print ("0.\t%s\n", _("Cancel")); |
| 512 | for (guint i = 0; i < devices->len; i++) { |
| 513 | dev = g_ptr_array_index (devices, i); |
| 514 | g_print ("%u.\t%s (%s)\n", |
| 515 | i + 1, |
| 516 | fu_device_get_id (dev), |
| 517 | fu_device_get_name (dev)); |
| 518 | } |
| 519 | idx = fu_util_prompt_for_number (devices->len); |
| 520 | if (idx == 0) { |
| 521 | g_set_error_literal (error, |
| 522 | FWUPD_ERROR, |
| 523 | FWUPD_ERROR_NOTHING_TO_DO, |
| 524 | "Request canceled"); |
| 525 | return NULL; |
| 526 | } |
| 527 | dev = g_ptr_array_index (devices, idx - 1); |
| 528 | return g_object_ref (dev); |
| 529 | } |
| 530 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 531 | static void |
| 532 | fu_util_install_device_changed_cb (FwupdClient *client, |
| 533 | FwupdDevice *device, |
| 534 | FuUtilPrivate *priv) |
| 535 | { |
| 536 | g_autofree gchar *str = NULL; |
| 537 | |
| 538 | /* same as last time, so ignore */ |
| 539 | if (priv->current_device != NULL && |
| 540 | fwupd_device_compare (priv->current_device, device) == 0) |
| 541 | return; |
| 542 | |
| 543 | /* show message in progressbar */ |
| 544 | /* TRANSLATORS: %1 is a device name */ |
| 545 | str = g_strdup_printf (_("Installing %s"), |
| 546 | fwupd_device_get_name (device)); |
| 547 | fu_progressbar_set_title (priv->progressbar, str); |
| 548 | g_set_object (&priv->current_device, device); |
| 549 | } |
| 550 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 551 | static gboolean |
| 552 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 553 | { |
| 554 | g_autoptr(FuDevice) device = NULL; |
| 555 | g_autoptr(GBytes) blob_fw = NULL; |
| 556 | |
| 557 | /* invalid args */ |
| 558 | if (g_strv_length (values) == 0) { |
| 559 | g_set_error_literal (error, |
| 560 | FWUPD_ERROR, |
| 561 | FWUPD_ERROR_INVALID_ARGS, |
| 562 | "Invalid arguments"); |
| 563 | return FALSE; |
| 564 | } |
| 565 | |
| 566 | /* parse blob */ |
| 567 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 568 | if (blob_fw == NULL) { |
| 569 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 570 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 571 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 572 | |
| 573 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 574 | if (!fu_util_start_engine (priv, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 575 | return FALSE; |
| 576 | |
| 577 | /* get device */ |
| 578 | if (g_strv_length (values) >= 2) { |
| 579 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 580 | if (device == NULL) |
| 581 | return FALSE; |
| 582 | } else { |
| 583 | device = fu_util_prompt_for_device (priv, error); |
| 584 | if (device == NULL) |
| 585 | return FALSE; |
| 586 | } |
| 587 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 588 | g_signal_connect (priv->engine, "device-changed", |
| 589 | G_CALLBACK (fu_util_install_device_changed_cb), priv); |
| 590 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 591 | /* write bare firmware */ |
| 592 | return fu_engine_install_blob (priv->engine, device, |
| 593 | NULL, /* blob_cab */ |
| 594 | blob_fw, |
| 595 | NULL, /* version */ |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 596 | priv->flags, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 597 | error); |
| 598 | } |
| 599 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 600 | static gint |
| 601 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 602 | { |
| 603 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 604 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 605 | return fu_install_task_compare (task1, task2); |
| 606 | } |
| 607 | |
| 608 | static gboolean |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 609 | fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error) |
| 610 | { |
| 611 | const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL }, |
| 612 | { "curl", uri, "--output", fn, NULL }, |
| 613 | { NULL } }; |
| 614 | for (guint i = 0; argv[i][0] != NULL; i++) { |
| 615 | g_autoptr(GError) error_local = NULL; |
| 616 | if (!fu_common_find_program_in_path (argv[i][0], &error_local)) { |
| 617 | g_debug ("%s", error_local->message); |
| 618 | continue; |
| 619 | } |
| 620 | return fu_common_spawn_sync (argv[i], NULL, NULL, NULL, error); |
| 621 | } |
| 622 | g_set_error_literal (error, |
| 623 | FWUPD_ERROR, |
| 624 | FWUPD_ERROR_NOT_FOUND, |
| 625 | "no supported out-of-process downloaders found"); |
| 626 | return FALSE; |
| 627 | } |
| 628 | |
| 629 | static gchar * |
| 630 | fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error) |
| 631 | { |
| 632 | g_autofree gchar *filename = NULL; |
| 633 | g_autoptr(SoupURI) uri = NULL; |
| 634 | |
| 635 | /* a local file */ |
| 636 | uri = soup_uri_new (perhapsfn); |
| 637 | if (uri == NULL) |
| 638 | return g_strdup (perhapsfn); |
| 639 | |
| 640 | /* download the firmware to a cachedir */ |
| 641 | filename = fu_util_get_user_cache_path (perhapsfn); |
| 642 | if (!fu_common_mkdir_parent (filename, error)) |
| 643 | return NULL; |
| 644 | if (!fu_util_download_out_of_process (perhapsfn, filename, error)) |
| 645 | return NULL; |
| 646 | return g_steal_pointer (&filename); |
| 647 | } |
| 648 | |
| 649 | static gboolean |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 650 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 651 | { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 652 | g_autofree gchar *filename = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 653 | g_autoptr(GBytes) blob_cab = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 654 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 655 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 656 | g_autoptr(GPtrArray) errors = NULL; |
| 657 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 658 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 659 | |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 660 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 661 | if (!fu_util_start_engine (priv, error)) |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 662 | return FALSE; |
| 663 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 664 | /* handle both forms */ |
| 665 | if (g_strv_length (values) == 1) { |
| 666 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 667 | if (devices_possible == NULL) |
| 668 | return FALSE; |
| 669 | } else if (g_strv_length (values) == 2) { |
| 670 | FuDevice *device = fu_engine_get_device (priv->engine, |
| 671 | values[1], |
| 672 | error); |
| 673 | if (device == NULL) |
| 674 | return FALSE; |
| 675 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 676 | g_ptr_array_add (devices_possible, device); |
| 677 | } else { |
| 678 | g_set_error_literal (error, |
| 679 | FWUPD_ERROR, |
| 680 | FWUPD_ERROR_INVALID_ARGS, |
| 681 | "Invalid arguments"); |
| 682 | return FALSE; |
| 683 | } |
| 684 | |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 685 | /* download if required */ |
| 686 | filename = fu_util_download_if_required (priv, values[0], error); |
| 687 | if (filename == NULL) |
| 688 | return FALSE; |
| 689 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 690 | /* parse silo */ |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 691 | blob_cab = fu_common_get_contents_bytes (filename, error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 692 | if (blob_cab == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 693 | fu_util_maybe_prefix_sandbox_error (filename, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 694 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 695 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 696 | silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error); |
| 697 | if (silo == NULL) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 698 | return FALSE; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 699 | components = xb_silo_query (silo, "component", 0, error); |
| 700 | if (components == NULL) |
| 701 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 702 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 703 | /* for each component in the silo */ |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 704 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 705 | 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^] | 706 | for (guint i = 0; i < components->len; i++) { |
| 707 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 708 | |
| 709 | /* do any devices pass the requirements */ |
| 710 | for (guint j = 0; j < devices_possible->len; j++) { |
| 711 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 712 | g_autoptr(FuInstallTask) task = NULL; |
| 713 | g_autoptr(GError) error_local = NULL; |
| 714 | |
| 715 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 716 | task = fu_install_task_new (device, component); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 717 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 718 | task, priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 719 | &error_local)) { |
| 720 | g_debug ("requirement on %s:%s failed: %s", |
| 721 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame^] | 722 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 723 | error_local->message); |
| 724 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | /* success */ |
| 729 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* order the install tasks by the device priority */ |
| 734 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 735 | |
| 736 | /* nothing suitable */ |
| 737 | if (install_tasks->len == 0) { |
| 738 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 739 | g_propagate_error (error, error_tmp); |
| 740 | return FALSE; |
| 741 | } |
| 742 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 743 | g_signal_connect (priv->engine, "device-changed", |
| 744 | G_CALLBACK (fu_util_install_device_changed_cb), priv); |
| 745 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 746 | /* install all the tasks */ |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 747 | if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error)) |
| 748 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 749 | |
| 750 | /* success */ |
| 751 | return TRUE; |
| 752 | } |
| 753 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 754 | static gboolean |
| 755 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 756 | { |
| 757 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 758 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 759 | |
| 760 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 761 | if (!fu_util_start_engine (priv, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 762 | return FALSE; |
| 763 | |
| 764 | /* invalid args */ |
| 765 | if (g_strv_length (values) == 0) { |
| 766 | g_set_error_literal (error, |
| 767 | FWUPD_ERROR, |
| 768 | FWUPD_ERROR_INVALID_ARGS, |
| 769 | "Invalid arguments"); |
| 770 | return FALSE; |
| 771 | } |
| 772 | |
| 773 | /* get device */ |
| 774 | if (g_strv_length (values) >= 1) { |
| 775 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 776 | if (device == NULL) |
| 777 | return FALSE; |
| 778 | } else { |
| 779 | device = fu_util_prompt_for_device (priv, error); |
| 780 | if (device == NULL) |
| 781 | return FALSE; |
| 782 | } |
| 783 | |
| 784 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 785 | locker = fu_device_locker_new (device, error); |
| 786 | if (locker == NULL) |
| 787 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 788 | return fu_device_detach (device, error); |
| 789 | } |
| 790 | |
| 791 | static gboolean |
| 792 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 793 | { |
| 794 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 795 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 796 | |
| 797 | /* load engine */ |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 798 | if (!fu_util_start_engine (priv, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 799 | return FALSE; |
| 800 | |
| 801 | /* invalid args */ |
| 802 | if (g_strv_length (values) == 0) { |
| 803 | g_set_error_literal (error, |
| 804 | FWUPD_ERROR, |
| 805 | FWUPD_ERROR_INVALID_ARGS, |
| 806 | "Invalid arguments"); |
| 807 | return FALSE; |
| 808 | } |
| 809 | |
| 810 | /* get device */ |
| 811 | if (g_strv_length (values) >= 1) { |
| 812 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 813 | if (device == NULL) |
| 814 | return FALSE; |
| 815 | } else { |
| 816 | device = fu_util_prompt_for_device (priv, error); |
| 817 | if (device == NULL) |
| 818 | return FALSE; |
| 819 | } |
| 820 | |
| 821 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 822 | locker = fu_device_locker_new (device, error); |
| 823 | if (locker == NULL) |
| 824 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 825 | return fu_device_attach (device, error); |
| 826 | } |
| 827 | |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 828 | static gboolean |
| 829 | fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 830 | { |
| 831 | g_autoptr(FuSmbios) smbios = fu_smbios_new (); |
| 832 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
| 833 | const gchar *hwid_keys[] = { |
| 834 | FU_HWIDS_KEY_BIOS_VENDOR, |
| 835 | FU_HWIDS_KEY_BIOS_VERSION, |
| 836 | FU_HWIDS_KEY_BIOS_MAJOR_RELEASE, |
| 837 | FU_HWIDS_KEY_BIOS_MINOR_RELEASE, |
| 838 | FU_HWIDS_KEY_MANUFACTURER, |
| 839 | FU_HWIDS_KEY_FAMILY, |
| 840 | FU_HWIDS_KEY_PRODUCT_NAME, |
| 841 | FU_HWIDS_KEY_PRODUCT_SKU, |
| 842 | FU_HWIDS_KEY_ENCLOSURE_KIND, |
| 843 | FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, |
| 844 | FU_HWIDS_KEY_BASEBOARD_PRODUCT, |
| 845 | NULL }; |
| 846 | |
| 847 | /* read DMI data */ |
| 848 | if (g_strv_length (values) == 0) { |
| 849 | if (!fu_smbios_setup (smbios, error)) |
| 850 | return FALSE; |
| 851 | } else if (g_strv_length (values) == 1) { |
| 852 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 853 | return FALSE; |
| 854 | } else { |
| 855 | g_set_error_literal (error, |
| 856 | FWUPD_ERROR, |
| 857 | FWUPD_ERROR_INVALID_ARGS, |
| 858 | "Invalid arguments"); |
| 859 | return FALSE; |
| 860 | } |
| 861 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 862 | return FALSE; |
| 863 | |
| 864 | /* show debug output */ |
| 865 | g_print ("Computer Information\n"); |
| 866 | g_print ("--------------------\n"); |
| 867 | for (guint i = 0; hwid_keys[i] != NULL; i++) { |
| 868 | const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]); |
| 869 | if (tmp == NULL) |
| 870 | continue; |
| 871 | if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 || |
| 872 | g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) { |
| 873 | guint64 val = g_ascii_strtoull (tmp, NULL, 16); |
| 874 | g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val); |
| 875 | } else { |
| 876 | g_print ("%s: %s\n", hwid_keys[i], tmp); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* show GUIDs */ |
| 881 | g_print ("\nHardware IDs\n"); |
| 882 | g_print ("------------\n"); |
| 883 | for (guint i = 0; i < 15; i++) { |
| 884 | const gchar *keys = NULL; |
| 885 | g_autofree gchar *guid = NULL; |
| 886 | g_autofree gchar *key = NULL; |
| 887 | g_autofree gchar *keys_str = NULL; |
| 888 | g_auto(GStrv) keysv = NULL; |
| 889 | g_autoptr(GError) error_local = NULL; |
| 890 | |
| 891 | /* get the GUID */ |
| 892 | key = g_strdup_printf ("HardwareID-%u", i); |
| 893 | keys = fu_hwids_get_replace_keys (hwids, key); |
| 894 | guid = fu_hwids_get_guid (hwids, key, &error_local); |
| 895 | if (guid == NULL) { |
| 896 | g_print ("%s\n", error_local->message); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | /* show what makes up the GUID */ |
| 901 | keysv = g_strsplit (keys, "&", -1); |
| 902 | keys_str = g_strjoinv (" + ", keysv); |
| 903 | g_print ("{%s} <- %s\n", guid, keys_str); |
| 904 | } |
| 905 | |
| 906 | return TRUE; |
| 907 | } |
| 908 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 909 | int |
| 910 | main (int argc, char *argv[]) |
| 911 | { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 912 | gboolean allow_older = FALSE; |
| 913 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 914 | gboolean force = FALSE; |
| 915 | gboolean ret; |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 916 | gboolean version = FALSE; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 917 | g_auto(GStrv) plugin_glob = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 918 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 919 | g_autoptr(GError) error = NULL; |
| 920 | g_autofree gchar *cmd_descriptions = NULL; |
| 921 | const GOptionEntry options[] = { |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 922 | { "version", '\0', 0, G_OPTION_ARG_NONE, &version, |
| 923 | /* TRANSLATORS: command line option */ |
| 924 | _("Show client and daemon versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 925 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 926 | /* TRANSLATORS: command line option */ |
| 927 | _("Allow re-installing existing firmware versions"), NULL }, |
| 928 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 929 | /* TRANSLATORS: command line option */ |
| 930 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 931 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 932 | /* TRANSLATORS: command line option */ |
| 933 | _("Override plugin warning"), NULL }, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 934 | { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices, |
| 935 | /* TRANSLATORS: command line option */ |
| 936 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 937 | { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
| 938 | /* TRANSLATORS: command line option */ |
| 939 | _("Manually whitelist specific plugins"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 940 | { NULL} |
| 941 | }; |
| 942 | |
| 943 | setlocale (LC_ALL, ""); |
| 944 | |
| 945 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 946 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 947 | textdomain (GETTEXT_PACKAGE); |
| 948 | |
| 949 | /* ensure root user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 950 | if (getuid () != 0 || geteuid () != 0) |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 951 | /* TRANSLATORS: we're poking around as a power user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 952 | g_printerr ("%s\n", _("This program may only work correctly as root")); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 953 | |
| 954 | /* create helper object */ |
| 955 | priv->loop = g_main_loop_new (NULL, FALSE); |
| 956 | priv->progressbar = fu_progressbar_new (); |
| 957 | |
| 958 | /* add commands */ |
| 959 | priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free); |
| 960 | fu_util_add (priv->cmd_array, |
| 961 | "smbios-dump", |
| 962 | "FILE", |
| 963 | /* TRANSLATORS: command description */ |
| 964 | _("Dump SMBIOS data from a file"), |
| 965 | fu_util_smbios_dump); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 966 | fu_util_add (priv->cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 967 | "get-plugins", |
| 968 | NULL, |
| 969 | /* TRANSLATORS: command description */ |
| 970 | _("Get all enabled plugins registered with the system"), |
| 971 | fu_util_get_plugins); |
| 972 | fu_util_add (priv->cmd_array, |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 973 | "get-details", |
| 974 | NULL, |
| 975 | /* TRANSLATORS: command description */ |
| 976 | _("Gets details about a firmware file"), |
| 977 | fu_util_get_details); |
| 978 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 979 | "get-devices", |
| 980 | NULL, |
| 981 | /* TRANSLATORS: command description */ |
| 982 | _("Get all devices that support firmware updates"), |
| 983 | fu_util_get_devices); |
| 984 | fu_util_add (priv->cmd_array, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 985 | "get-topology", |
| 986 | NULL, |
| 987 | /* TRANSLATORS: command description */ |
| 988 | _("Get all devices according to the system topology"), |
| 989 | fu_util_get_topology); |
| 990 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 991 | "watch", |
| 992 | NULL, |
| 993 | /* TRANSLATORS: command description */ |
Piotr Drąg | 472fa59 | 2018-06-06 14:53:48 +0200 | [diff] [blame] | 994 | _("Watch for hardware changes"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 995 | fu_util_watch); |
| 996 | fu_util_add (priv->cmd_array, |
| 997 | "install-blob", |
| 998 | "FILENAME DEVICE-ID", |
| 999 | /* TRANSLATORS: command description */ |
| 1000 | _("Install a firmware blob on a device"), |
| 1001 | fu_util_install_blob); |
| 1002 | fu_util_add (priv->cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1003 | "install", |
| 1004 | "FILE [ID]", |
| 1005 | /* TRANSLATORS: command description */ |
| 1006 | _("Install a firmware file on this hardware"), |
| 1007 | fu_util_install); |
| 1008 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1009 | "attach", |
| 1010 | "DEVICE-ID", |
| 1011 | /* TRANSLATORS: command description */ |
| 1012 | _("Attach to firmware mode"), |
| 1013 | fu_util_attach); |
| 1014 | fu_util_add (priv->cmd_array, |
| 1015 | "detach", |
| 1016 | "DEVICE-ID", |
| 1017 | /* TRANSLATORS: command description */ |
| 1018 | _("Detach to bootloader mode"), |
| 1019 | fu_util_detach); |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1020 | fu_util_add (priv->cmd_array, |
| 1021 | "hwids", |
| 1022 | "[FILE]", |
| 1023 | /* TRANSLATORS: command description */ |
| 1024 | _("Return all the hardware IDs for the machine"), |
| 1025 | fu_util_hwids); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1026 | |
| 1027 | /* do stuff on ctrl+c */ |
| 1028 | priv->cancellable = g_cancellable_new (); |
| 1029 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1030 | SIGINT, fu_util_sigint_cb, |
| 1031 | priv, NULL); |
| 1032 | g_signal_connect (priv->cancellable, "cancelled", |
| 1033 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1034 | |
| 1035 | /* sort by command name */ |
| 1036 | g_ptr_array_sort (priv->cmd_array, |
| 1037 | (GCompareFunc) fu_sort_command_name_cb); |
| 1038 | |
| 1039 | /* get a list of the commands */ |
| 1040 | priv->context = g_option_context_new (NULL); |
| 1041 | cmd_descriptions = fu_util_get_descriptions (priv->cmd_array); |
| 1042 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 1043 | g_option_context_set_description (priv->context, |
| 1044 | "This tool allows an administrator to use the fwupd plugins " |
| 1045 | "without being installed on the host system."); |
| 1046 | |
| 1047 | /* TRANSLATORS: program name */ |
| 1048 | g_set_application_name (_("Firmware Utility")); |
| 1049 | g_option_context_add_main_entries (priv->context, options, NULL); |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 1050 | g_option_context_add_group (priv->context, fu_debug_get_option_group ()); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1051 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 1052 | if (!ret) { |
| 1053 | /* TRANSLATORS: the user didn't read the man page */ |
| 1054 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 1055 | error->message); |
| 1056 | return EXIT_FAILURE; |
| 1057 | } |
| 1058 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1059 | /* set flags */ |
| 1060 | priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
| 1061 | if (allow_reinstall) |
| 1062 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 1063 | if (allow_older) |
| 1064 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
| 1065 | if (force) |
| 1066 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
| 1067 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1068 | /* load engine */ |
| 1069 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 1070 | g_signal_connect (priv->engine, "device-added", |
| 1071 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1072 | priv); |
| 1073 | g_signal_connect (priv->engine, "device-removed", |
| 1074 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1075 | priv); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1076 | g_signal_connect (priv->engine, "status-changed", |
| 1077 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1078 | priv); |
| 1079 | g_signal_connect (priv->engine, "percentage-changed", |
| 1080 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1081 | priv); |
| 1082 | |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1083 | /* just show versions and exit */ |
| 1084 | if (version) { |
| 1085 | g_autofree gchar *version_str = fu_util_get_versions (); |
| 1086 | g_print ("%s\n", version_str); |
| 1087 | return EXIT_SUCCESS; |
| 1088 | } |
| 1089 | |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1090 | /* any plugin whitelist specified */ |
| 1091 | for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++) |
| 1092 | fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]); |
| 1093 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1094 | /* run the specified command */ |
| 1095 | ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error); |
| 1096 | if (!ret) { |
| 1097 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
| 1098 | g_autofree gchar *tmp = NULL; |
| 1099 | tmp = g_option_context_get_help (priv->context, TRUE, NULL); |
| 1100 | g_print ("%s\n\n%s", error->message, tmp); |
| 1101 | return EXIT_FAILURE; |
| 1102 | } |
| 1103 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1104 | g_print ("%s\n", error->message); |
| 1105 | return EXIT_NOTHING_TO_DO; |
| 1106 | } |
| 1107 | g_print ("%s\n", error->message); |
| 1108 | return EXIT_FAILURE; |
| 1109 | } |
| 1110 | |
| 1111 | /* success */ |
| 1112 | return EXIT_SUCCESS; |
| 1113 | } |