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