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