Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
| 3 | * Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com> |
| 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include <fwupd.h> |
| 25 | #include <glib/gi18n.h> |
| 26 | #include <glib-unix.h> |
| 27 | #include <locale.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 31 | #include "fu-engine.h" |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame^] | 32 | #include "fu-plugin-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 33 | #include "fu-progressbar.h" |
| 34 | #include "fu-smbios.h" |
| 35 | #include "fu-util-common.h" |
| 36 | |
| 37 | /* this is only valid in this file */ |
| 38 | #define FWUPD_ERROR_INVALID_ARGS (FWUPD_ERROR_LAST+1) |
| 39 | |
| 40 | /* custom return code */ |
| 41 | #define EXIT_NOTHING_TO_DO 2 |
| 42 | |
| 43 | typedef struct { |
| 44 | GCancellable *cancellable; |
| 45 | GMainLoop *loop; |
| 46 | GOptionContext *context; |
| 47 | GPtrArray *cmd_array; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 48 | FuEngine *engine; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 49 | FuProgressbar *progressbar; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 50 | FwupdInstallFlags flags; |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 51 | gboolean show_all_devices; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 52 | } FuUtilPrivate; |
| 53 | |
| 54 | typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util, |
| 55 | gchar **values, |
| 56 | GError **error); |
| 57 | |
| 58 | typedef struct { |
| 59 | gchar *name; |
| 60 | gchar *arguments; |
| 61 | gchar *description; |
| 62 | FuUtilPrivateCb callback; |
| 63 | } FuUtilItem; |
| 64 | |
| 65 | static void |
| 66 | fu_util_item_free (FuUtilItem *item) |
| 67 | { |
| 68 | g_free (item->name); |
| 69 | g_free (item->arguments); |
| 70 | g_free (item->description); |
| 71 | g_free (item); |
| 72 | } |
| 73 | |
| 74 | static gint |
| 75 | fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2) |
| 76 | { |
| 77 | return g_strcmp0 ((*item1)->name, (*item2)->name); |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | fu_util_add (GPtrArray *array, |
| 82 | const gchar *name, |
| 83 | const gchar *arguments, |
| 84 | const gchar *description, |
| 85 | FuUtilPrivateCb callback) |
| 86 | { |
| 87 | g_auto(GStrv) names = NULL; |
| 88 | |
| 89 | g_return_if_fail (name != NULL); |
| 90 | g_return_if_fail (description != NULL); |
| 91 | g_return_if_fail (callback != NULL); |
| 92 | |
| 93 | /* add each one */ |
| 94 | names = g_strsplit (name, ",", -1); |
| 95 | for (guint i = 0; names[i] != NULL; i++) { |
| 96 | FuUtilItem *item = g_new0 (FuUtilItem, 1); |
| 97 | item->name = g_strdup (names[i]); |
| 98 | if (i == 0) { |
| 99 | item->description = g_strdup (description); |
| 100 | } else { |
| 101 | /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */ |
| 102 | item->description = g_strdup_printf (_("Alias to %s"), |
| 103 | names[0]); |
| 104 | } |
| 105 | item->arguments = g_strdup (arguments); |
| 106 | item->callback = callback; |
| 107 | g_ptr_array_add (array, item); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static gchar * |
| 112 | fu_util_get_descriptions (GPtrArray *array) |
| 113 | { |
| 114 | gsize len; |
| 115 | const gsize max_len = 35; |
| 116 | GString *string; |
| 117 | |
| 118 | /* print each command */ |
| 119 | string = g_string_new (""); |
| 120 | for (guint i = 0; i < array->len; i++) { |
| 121 | FuUtilItem *item = g_ptr_array_index (array, i); |
| 122 | g_string_append (string, " "); |
| 123 | g_string_append (string, item->name); |
| 124 | len = strlen (item->name) + 2; |
| 125 | if (item->arguments != NULL) { |
| 126 | g_string_append (string, " "); |
| 127 | g_string_append (string, item->arguments); |
| 128 | len += strlen (item->arguments) + 1; |
| 129 | } |
| 130 | if (len < max_len) { |
| 131 | for (gsize j = len; j < max_len + 1; j++) |
| 132 | g_string_append_c (string, ' '); |
| 133 | g_string_append (string, item->description); |
| 134 | g_string_append_c (string, '\n'); |
| 135 | } else { |
| 136 | g_string_append_c (string, '\n'); |
| 137 | for (gsize j = 0; j < max_len + 1; j++) |
| 138 | g_string_append_c (string, ' '); |
| 139 | g_string_append (string, item->description); |
| 140 | g_string_append_c (string, '\n'); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* remove trailing newline */ |
| 145 | if (string->len > 0) |
| 146 | g_string_set_size (string, string->len - 1); |
| 147 | |
| 148 | return g_string_free (string, FALSE); |
| 149 | } |
| 150 | |
| 151 | static gboolean |
| 152 | fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error) |
| 153 | { |
| 154 | /* find command */ |
| 155 | for (guint i = 0; i < priv->cmd_array->len; i++) { |
| 156 | FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i); |
| 157 | if (g_strcmp0 (item->name, command) == 0) |
| 158 | return item->callback (priv, values, error); |
| 159 | } |
| 160 | |
| 161 | /* not found */ |
| 162 | g_set_error_literal (error, |
| 163 | FWUPD_ERROR, |
| 164 | FWUPD_ERROR_INVALID_ARGS, |
| 165 | /* TRANSLATORS: error message */ |
| 166 | _("Command not found")); |
| 167 | return FALSE; |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data) |
| 172 | { |
| 173 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 174 | /* TRANSLATORS: this is when a device ctrl+c's a watch */ |
| 175 | g_print ("%s\n", _("Cancelled")); |
| 176 | g_main_loop_quit (priv->loop); |
| 177 | } |
| 178 | |
| 179 | static gboolean |
| 180 | fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
| 181 | { |
| 182 | g_autofree gchar *tmp = NULL; |
| 183 | g_autoptr(FuSmbios) smbios = NULL; |
| 184 | if (g_strv_length (values) < 1) { |
| 185 | g_set_error_literal (error, |
| 186 | FWUPD_ERROR, |
| 187 | FWUPD_ERROR_INVALID_ARGS, |
| 188 | "Invalid arguments"); |
| 189 | return FALSE; |
| 190 | } |
| 191 | smbios = fu_smbios_new (); |
| 192 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 193 | return FALSE; |
| 194 | tmp = fu_smbios_to_string (smbios); |
| 195 | g_print ("%s\n", tmp); |
| 196 | return TRUE; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | fu_util_ignore_cb (const gchar *log_domain, GLogLevelFlags log_level, |
| 201 | const gchar *message, gpointer user_data) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | static gboolean |
| 206 | fu_util_sigint_cb (gpointer user_data) |
| 207 | { |
| 208 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 209 | g_debug ("Handling SIGINT"); |
| 210 | g_cancellable_cancel (priv->cancellable); |
| 211 | return FALSE; |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | fu_util_private_free (FuUtilPrivate *priv) |
| 216 | { |
| 217 | if (priv->cmd_array != NULL) |
| 218 | g_ptr_array_unref (priv->cmd_array); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 219 | if (priv->engine != NULL) |
| 220 | g_object_unref (priv->engine); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 221 | if (priv->loop != NULL) |
| 222 | g_main_loop_unref (priv->loop); |
| 223 | if (priv->cancellable != NULL) |
| 224 | g_object_unref (priv->cancellable); |
| 225 | if (priv->progressbar != NULL) |
| 226 | g_object_unref (priv->progressbar); |
| 227 | if (priv->context != NULL) |
| 228 | g_option_context_free (priv->context); |
| 229 | g_free (priv); |
| 230 | } |
| 231 | |
| 232 | #pragma clang diagnostic push |
| 233 | #pragma clang diagnostic ignored "-Wunused-function" |
| 234 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free) |
| 235 | #pragma clang diagnostic pop |
| 236 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 237 | |
| 238 | static void |
| 239 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 240 | FuDevice *device, |
| 241 | FuUtilPrivate *priv) |
| 242 | { |
| 243 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 244 | g_debug ("ADDED:\n%s", tmp); |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 249 | FuDevice *device, |
| 250 | FuUtilPrivate *priv) |
| 251 | { |
| 252 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 253 | g_debug ("REMOVED:\n%s", tmp); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | fu_main_engine_device_changed_cb (FuEngine *engine, |
| 258 | FuDevice *device, |
| 259 | FuUtilPrivate *priv) |
| 260 | { |
| 261 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 262 | g_debug ("CHANGED:\n%s", tmp); |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 267 | FwupdStatus status, |
| 268 | FuUtilPrivate *priv) |
| 269 | { |
| 270 | fu_progressbar_update (priv->progressbar, status, 0); |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 275 | guint percentage, |
| 276 | FuUtilPrivate *priv) |
| 277 | { |
| 278 | fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage); |
| 279 | } |
| 280 | |
| 281 | static gboolean |
| 282 | fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 283 | { |
| 284 | if (!fu_engine_load (priv->engine, error)) |
| 285 | return FALSE; |
| 286 | g_main_loop_run (priv->loop); |
| 287 | return TRUE; |
| 288 | } |
| 289 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame^] | 290 | static gint |
| 291 | fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2) |
| 292 | { |
| 293 | return fu_plugin_name_compare (*item1, *item2); |
| 294 | } |
| 295 | |
| 296 | static gboolean |
| 297 | fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error) |
| 298 | { |
| 299 | GPtrArray *plugins; |
| 300 | guint cnt = 0; |
| 301 | |
| 302 | /* load engine */ |
| 303 | if (!fu_engine_load_plugins (priv->engine, error)) |
| 304 | return FALSE; |
| 305 | |
| 306 | /* print */ |
| 307 | plugins = fu_engine_get_plugins (priv->engine); |
| 308 | g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb); |
| 309 | for (guint i = 0; i < plugins->len; i++) { |
| 310 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
| 311 | if (!fu_plugin_get_enabled (plugin)) |
| 312 | continue; |
| 313 | g_print ("%s\n", fu_plugin_get_name (plugin)); |
| 314 | cnt++; |
| 315 | } |
| 316 | if (cnt == 0) { |
| 317 | /* TRANSLATORS: nothing found */ |
| 318 | g_print ("%s\n", _("No plugins found")); |
| 319 | return TRUE; |
| 320 | } |
| 321 | |
| 322 | return TRUE; |
| 323 | } |
| 324 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 325 | static gboolean |
| 326 | fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error) |
| 327 | { |
| 328 | g_autoptr(GPtrArray) devs = NULL; |
| 329 | |
| 330 | /* load engine */ |
| 331 | if (!fu_engine_load (priv->engine, error)) |
| 332 | return FALSE; |
| 333 | |
| 334 | /* print */ |
| 335 | devs = fu_engine_get_devices (priv->engine, error); |
| 336 | if (devs == NULL) |
| 337 | return FALSE; |
| 338 | if (devs->len == 0) { |
| 339 | /* TRANSLATORS: nothing attached */ |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 340 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 341 | return TRUE; |
| 342 | } |
| 343 | for (guint i = 0; i < devs->len; i++) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 344 | g_autofree gchar *tmp = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 345 | FwupdDevice *dev = g_ptr_array_index (devs, i); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 346 | if (!(fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE) || priv->show_all_devices)) |
| 347 | continue; |
| 348 | tmp = fwupd_device_to_string (dev); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 349 | g_print ("%s\n", tmp); |
| 350 | } |
| 351 | |
| 352 | return TRUE; |
| 353 | } |
| 354 | |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 355 | static void |
| 356 | fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FwupdDevice *dev) |
| 357 | { |
| 358 | for (guint i = 0; i < devs->len; i++) { |
| 359 | FwupdDevice *dev_tmp = g_ptr_array_index (devs, i); |
| 360 | if (!(fwupd_device_has_flag (dev_tmp, FWUPD_DEVICE_FLAG_UPDATABLE) || priv->show_all_devices)) |
| 361 | continue; |
| 362 | if (fwupd_device_get_parent (dev_tmp) == dev) { |
| 363 | GNode *child = g_node_append_data (root, dev_tmp); |
| 364 | fu_util_build_device_tree (priv, child, devs, dev_tmp); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static gboolean |
| 370 | fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error) |
| 371 | { |
| 372 | g_autoptr(GNode) root = g_node_new (NULL); |
| 373 | g_autoptr(GPtrArray) devs = NULL; |
| 374 | |
| 375 | /* load engine */ |
| 376 | if (!fu_engine_load (priv->engine, error)) |
| 377 | return FALSE; |
| 378 | |
| 379 | /* print */ |
| 380 | devs = fu_engine_get_devices (priv->engine, error); |
| 381 | if (devs == NULL) |
| 382 | return FALSE; |
| 383 | |
| 384 | /* print */ |
| 385 | if (devs->len == 0) { |
| 386 | /* TRANSLATORS: nothing attached that can be upgraded */ |
| 387 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
| 388 | return TRUE; |
| 389 | } |
| 390 | fu_util_build_device_tree (priv, root, devs, NULL); |
| 391 | g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, |
| 392 | fu_util_print_device_tree, priv); |
| 393 | |
| 394 | return TRUE; |
| 395 | } |
| 396 | |
| 397 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 398 | static FuDevice * |
| 399 | fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) |
| 400 | { |
| 401 | FuDevice *dev; |
| 402 | guint idx; |
| 403 | g_autoptr(GPtrArray) devices = NULL; |
| 404 | |
| 405 | /* get devices from daemon */ |
| 406 | devices = fu_engine_get_devices (priv->engine, error); |
| 407 | if (devices == NULL) |
| 408 | return NULL; |
| 409 | |
| 410 | /* exactly one */ |
| 411 | if (devices->len == 1) { |
| 412 | dev = g_ptr_array_index (devices, 0); |
| 413 | return g_object_ref (dev); |
| 414 | } |
| 415 | |
| 416 | /* TRANSLATORS: get interactive prompt */ |
| 417 | g_print ("%s\n", _("Choose a device:")); |
| 418 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 419 | g_print ("0.\t%s\n", _("Cancel")); |
| 420 | for (guint i = 0; i < devices->len; i++) { |
| 421 | dev = g_ptr_array_index (devices, i); |
| 422 | g_print ("%u.\t%s (%s)\n", |
| 423 | i + 1, |
| 424 | fu_device_get_id (dev), |
| 425 | fu_device_get_name (dev)); |
| 426 | } |
| 427 | idx = fu_util_prompt_for_number (devices->len); |
| 428 | if (idx == 0) { |
| 429 | g_set_error_literal (error, |
| 430 | FWUPD_ERROR, |
| 431 | FWUPD_ERROR_NOTHING_TO_DO, |
| 432 | "Request canceled"); |
| 433 | return NULL; |
| 434 | } |
| 435 | dev = g_ptr_array_index (devices, idx - 1); |
| 436 | return g_object_ref (dev); |
| 437 | } |
| 438 | |
| 439 | static gboolean |
| 440 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 441 | { |
| 442 | g_autoptr(FuDevice) device = NULL; |
| 443 | g_autoptr(GBytes) blob_fw = NULL; |
| 444 | |
| 445 | /* invalid args */ |
| 446 | if (g_strv_length (values) == 0) { |
| 447 | g_set_error_literal (error, |
| 448 | FWUPD_ERROR, |
| 449 | FWUPD_ERROR_INVALID_ARGS, |
| 450 | "Invalid arguments"); |
| 451 | return FALSE; |
| 452 | } |
| 453 | |
| 454 | /* parse blob */ |
| 455 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
| 456 | if (blob_fw == NULL) |
| 457 | return FALSE; |
| 458 | |
| 459 | /* load engine */ |
| 460 | if (!fu_engine_load (priv->engine, error)) |
| 461 | return FALSE; |
| 462 | |
| 463 | /* get device */ |
| 464 | if (g_strv_length (values) >= 2) { |
| 465 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 466 | if (device == NULL) |
| 467 | return FALSE; |
| 468 | } else { |
| 469 | device = fu_util_prompt_for_device (priv, error); |
| 470 | if (device == NULL) |
| 471 | return FALSE; |
| 472 | } |
| 473 | |
| 474 | /* write bare firmware */ |
| 475 | return fu_engine_install_blob (priv->engine, device, |
| 476 | NULL, /* blob_cab */ |
| 477 | blob_fw, |
| 478 | NULL, /* version */ |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 479 | priv->flags, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 480 | error); |
| 481 | } |
| 482 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 483 | static gint |
| 484 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 485 | { |
| 486 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 487 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 488 | return fu_install_task_compare (task1, task2); |
| 489 | } |
| 490 | |
| 491 | static gboolean |
| 492 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 493 | { |
| 494 | GPtrArray *apps; |
| 495 | g_autoptr(AsStore) store = NULL; |
| 496 | g_autoptr(GBytes) blob_cab = NULL; |
| 497 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 498 | g_autoptr(GPtrArray) errors = NULL; |
| 499 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 500 | |
| 501 | /* handle both forms */ |
| 502 | if (g_strv_length (values) == 1) { |
| 503 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 504 | if (devices_possible == NULL) |
| 505 | return FALSE; |
| 506 | } else if (g_strv_length (values) == 2) { |
| 507 | FuDevice *device = fu_engine_get_device (priv->engine, |
| 508 | values[1], |
| 509 | error); |
| 510 | if (device == NULL) |
| 511 | return FALSE; |
| 512 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 513 | g_ptr_array_add (devices_possible, device); |
| 514 | } else { |
| 515 | g_set_error_literal (error, |
| 516 | FWUPD_ERROR, |
| 517 | FWUPD_ERROR_INVALID_ARGS, |
| 518 | "Invalid arguments"); |
| 519 | return FALSE; |
| 520 | } |
| 521 | |
| 522 | /* parse store */ |
| 523 | blob_cab = fu_common_get_contents_bytes (values[0], error); |
| 524 | if (blob_cab == NULL) |
| 525 | return FALSE; |
| 526 | store = fu_engine_get_store_from_blob (priv->engine, blob_cab, error); |
| 527 | if (store == NULL) |
| 528 | return FALSE; |
| 529 | apps = as_store_get_apps (store); |
| 530 | |
| 531 | /* for each component in the store */ |
| 532 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 533 | install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 534 | for (guint i = 0; i < apps->len; i++) { |
| 535 | AsApp *app = g_ptr_array_index (apps, i); |
| 536 | |
| 537 | /* do any devices pass the requirements */ |
| 538 | for (guint j = 0; j < devices_possible->len; j++) { |
| 539 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 540 | g_autoptr(FuInstallTask) task = NULL; |
| 541 | g_autoptr(GError) error_local = NULL; |
| 542 | |
| 543 | /* is this component valid for the device */ |
| 544 | task = fu_install_task_new (device, app); |
| 545 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 546 | task, priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 547 | &error_local)) { |
| 548 | g_debug ("requirement on %s:%s failed: %s", |
| 549 | fu_device_get_id (device), |
| 550 | as_app_get_id (app), |
| 551 | error_local->message); |
| 552 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | /* success */ |
| 557 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* order the install tasks by the device priority */ |
| 562 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 563 | |
| 564 | /* nothing suitable */ |
| 565 | if (install_tasks->len == 0) { |
| 566 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 567 | g_propagate_error (error, error_tmp); |
| 568 | return FALSE; |
| 569 | } |
| 570 | |
| 571 | /* install all the tasks */ |
| 572 | for (guint i = 0; i < install_tasks->len; i++) { |
| 573 | FuInstallTask *task = g_ptr_array_index (install_tasks, i); |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 574 | if (!fu_engine_install (priv->engine, task, blob_cab, priv->flags, error)) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 575 | return FALSE; |
| 576 | } |
| 577 | |
| 578 | /* success */ |
| 579 | return TRUE; |
| 580 | } |
| 581 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 582 | static gboolean |
| 583 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 584 | { |
| 585 | g_autoptr(FuDevice) device = NULL; |
| 586 | |
| 587 | /* load engine */ |
| 588 | if (!fu_engine_load (priv->engine, error)) |
| 589 | return FALSE; |
| 590 | |
| 591 | /* invalid args */ |
| 592 | if (g_strv_length (values) == 0) { |
| 593 | g_set_error_literal (error, |
| 594 | FWUPD_ERROR, |
| 595 | FWUPD_ERROR_INVALID_ARGS, |
| 596 | "Invalid arguments"); |
| 597 | return FALSE; |
| 598 | } |
| 599 | |
| 600 | /* get device */ |
| 601 | if (g_strv_length (values) >= 1) { |
| 602 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 603 | if (device == NULL) |
| 604 | return FALSE; |
| 605 | } else { |
| 606 | device = fu_util_prompt_for_device (priv, error); |
| 607 | if (device == NULL) |
| 608 | return FALSE; |
| 609 | } |
| 610 | |
| 611 | /* run vfunc */ |
| 612 | return fu_device_detach (device, error); |
| 613 | } |
| 614 | |
| 615 | static gboolean |
| 616 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 617 | { |
| 618 | g_autoptr(FuDevice) device = NULL; |
| 619 | |
| 620 | /* load engine */ |
| 621 | if (!fu_engine_load (priv->engine, error)) |
| 622 | return FALSE; |
| 623 | |
| 624 | /* invalid args */ |
| 625 | if (g_strv_length (values) == 0) { |
| 626 | g_set_error_literal (error, |
| 627 | FWUPD_ERROR, |
| 628 | FWUPD_ERROR_INVALID_ARGS, |
| 629 | "Invalid arguments"); |
| 630 | return FALSE; |
| 631 | } |
| 632 | |
| 633 | /* get device */ |
| 634 | if (g_strv_length (values) >= 1) { |
| 635 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 636 | if (device == NULL) |
| 637 | return FALSE; |
| 638 | } else { |
| 639 | device = fu_util_prompt_for_device (priv, error); |
| 640 | if (device == NULL) |
| 641 | return FALSE; |
| 642 | } |
| 643 | |
| 644 | /* run vfunc */ |
| 645 | return fu_device_attach (device, error); |
| 646 | } |
| 647 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 648 | int |
| 649 | main (int argc, char *argv[]) |
| 650 | { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 651 | gboolean allow_older = FALSE; |
| 652 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 653 | gboolean force = FALSE; |
| 654 | gboolean ret; |
| 655 | gboolean verbose = FALSE; |
| 656 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 657 | g_autoptr(GError) error = NULL; |
| 658 | g_autofree gchar *cmd_descriptions = NULL; |
| 659 | const GOptionEntry options[] = { |
| 660 | { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, |
| 661 | /* TRANSLATORS: command line option */ |
| 662 | _("Show extra debugging information"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 663 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 664 | /* TRANSLATORS: command line option */ |
| 665 | _("Allow re-installing existing firmware versions"), NULL }, |
| 666 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 667 | /* TRANSLATORS: command line option */ |
| 668 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 669 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 670 | /* TRANSLATORS: command line option */ |
| 671 | _("Override plugin warning"), NULL }, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 672 | { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices, |
| 673 | /* TRANSLATORS: command line option */ |
| 674 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 675 | { NULL} |
| 676 | }; |
| 677 | |
| 678 | setlocale (LC_ALL, ""); |
| 679 | |
| 680 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 681 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 682 | textdomain (GETTEXT_PACKAGE); |
| 683 | |
| 684 | /* ensure root user */ |
| 685 | if (getuid () != 0 || geteuid () != 0) { |
| 686 | /* TRANSLATORS: we're poking around as a power user */ |
| 687 | g_print ("%s\n", _("This program can only be used when root")); |
| 688 | return EXIT_FAILURE; |
| 689 | } |
| 690 | |
| 691 | /* create helper object */ |
| 692 | priv->loop = g_main_loop_new (NULL, FALSE); |
| 693 | priv->progressbar = fu_progressbar_new (); |
| 694 | |
| 695 | /* add commands */ |
| 696 | priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free); |
| 697 | fu_util_add (priv->cmd_array, |
| 698 | "smbios-dump", |
| 699 | "FILE", |
| 700 | /* TRANSLATORS: command description */ |
| 701 | _("Dump SMBIOS data from a file"), |
| 702 | fu_util_smbios_dump); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 703 | fu_util_add (priv->cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame^] | 704 | "get-plugins", |
| 705 | NULL, |
| 706 | /* TRANSLATORS: command description */ |
| 707 | _("Get all enabled plugins registered with the system"), |
| 708 | fu_util_get_plugins); |
| 709 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 710 | "get-devices", |
| 711 | NULL, |
| 712 | /* TRANSLATORS: command description */ |
| 713 | _("Get all devices that support firmware updates"), |
| 714 | fu_util_get_devices); |
| 715 | fu_util_add (priv->cmd_array, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 716 | "get-topology", |
| 717 | NULL, |
| 718 | /* TRANSLATORS: command description */ |
| 719 | _("Get all devices according to the system topology"), |
| 720 | fu_util_get_topology); |
| 721 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 722 | "watch", |
| 723 | NULL, |
| 724 | /* TRANSLATORS: command description */ |
| 725 | _("Watch for hardare changes"), |
| 726 | fu_util_watch); |
| 727 | fu_util_add (priv->cmd_array, |
| 728 | "install-blob", |
| 729 | "FILENAME DEVICE-ID", |
| 730 | /* TRANSLATORS: command description */ |
| 731 | _("Install a firmware blob on a device"), |
| 732 | fu_util_install_blob); |
| 733 | fu_util_add (priv->cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 734 | "install", |
| 735 | "FILE [ID]", |
| 736 | /* TRANSLATORS: command description */ |
| 737 | _("Install a firmware file on this hardware"), |
| 738 | fu_util_install); |
| 739 | fu_util_add (priv->cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 740 | "attach", |
| 741 | "DEVICE-ID", |
| 742 | /* TRANSLATORS: command description */ |
| 743 | _("Attach to firmware mode"), |
| 744 | fu_util_attach); |
| 745 | fu_util_add (priv->cmd_array, |
| 746 | "detach", |
| 747 | "DEVICE-ID", |
| 748 | /* TRANSLATORS: command description */ |
| 749 | _("Detach to bootloader mode"), |
| 750 | fu_util_detach); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 751 | |
| 752 | /* do stuff on ctrl+c */ |
| 753 | priv->cancellable = g_cancellable_new (); |
| 754 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 755 | SIGINT, fu_util_sigint_cb, |
| 756 | priv, NULL); |
| 757 | g_signal_connect (priv->cancellable, "cancelled", |
| 758 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 759 | |
| 760 | /* sort by command name */ |
| 761 | g_ptr_array_sort (priv->cmd_array, |
| 762 | (GCompareFunc) fu_sort_command_name_cb); |
| 763 | |
| 764 | /* get a list of the commands */ |
| 765 | priv->context = g_option_context_new (NULL); |
| 766 | cmd_descriptions = fu_util_get_descriptions (priv->cmd_array); |
| 767 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 768 | g_option_context_set_description (priv->context, |
| 769 | "This tool allows an administrator to use the fwupd plugins " |
| 770 | "without being installed on the host system."); |
| 771 | |
| 772 | /* TRANSLATORS: program name */ |
| 773 | g_set_application_name (_("Firmware Utility")); |
| 774 | g_option_context_add_main_entries (priv->context, options, NULL); |
| 775 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 776 | if (!ret) { |
| 777 | /* TRANSLATORS: the user didn't read the man page */ |
| 778 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 779 | error->message); |
| 780 | return EXIT_FAILURE; |
| 781 | } |
| 782 | |
| 783 | /* set verbose? */ |
| 784 | if (verbose) { |
| 785 | g_setenv ("G_MESSAGES_DEBUG", "all", FALSE); |
| 786 | } else { |
| 787 | g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, |
| 788 | fu_util_ignore_cb, NULL); |
| 789 | } |
| 790 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 791 | /* set flags */ |
| 792 | priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
| 793 | if (allow_reinstall) |
| 794 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 795 | if (allow_older) |
| 796 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
| 797 | if (force) |
| 798 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
| 799 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 800 | /* load engine */ |
| 801 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 802 | g_signal_connect (priv->engine, "device-added", |
| 803 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 804 | priv); |
| 805 | g_signal_connect (priv->engine, "device-removed", |
| 806 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 807 | priv); |
| 808 | g_signal_connect (priv->engine, "device-changed", |
| 809 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 810 | priv); |
| 811 | g_signal_connect (priv->engine, "status-changed", |
| 812 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 813 | priv); |
| 814 | g_signal_connect (priv->engine, "percentage-changed", |
| 815 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 816 | priv); |
| 817 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 818 | /* run the specified command */ |
| 819 | ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error); |
| 820 | if (!ret) { |
| 821 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
| 822 | g_autofree gchar *tmp = NULL; |
| 823 | tmp = g_option_context_get_help (priv->context, TRUE, NULL); |
| 824 | g_print ("%s\n\n%s", error->message, tmp); |
| 825 | return EXIT_FAILURE; |
| 826 | } |
| 827 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 828 | g_print ("%s\n", error->message); |
| 829 | return EXIT_NOTHING_TO_DO; |
| 830 | } |
| 831 | g_print ("%s\n", error->message); |
| 832 | return EXIT_FAILURE; |
| 833 | } |
| 834 | |
| 835 | /* success */ |
| 836 | return EXIT_SUCCESS; |
| 837 | } |