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