blob: 6d3797f33c5139076ecc0777c4e43f21cb5cb465 [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughesb5976832018-05-18 10:02:09 +01002 * Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com>
3 *
Mario Limonciello51308e62018-05-28 20:05:46 -05004 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesb5976832018-05-18 10:02:09 +01005 */
6
Richard Hughesb08e7bc2018-09-11 10:51:13 +01007#define G_LOG_DOMAIN "FuMain"
8
Richard Hughesb5976832018-05-18 10:02:09 +01009#include "config.h"
10
11#include <fwupd.h>
Mario Limonciello3f243a92019-01-21 22:05:23 -060012#include <glib/gstdio.h>
Richard Hughesb5976832018-05-18 10:02:09 +010013#include <glib/gi18n.h>
14#include <glib-unix.h>
15#include <locale.h>
16#include <stdlib.h>
17#include <unistd.h>
Richard Hughes3d178be2018-08-30 11:14:24 +010018#include <libsoup/soup.h>
Richard Hughesb5976832018-05-18 10:02:09 +010019
Mario Limonciello7a3df4b2019-01-31 10:27:22 -060020#include "fu-device-private.h"
Richard Hughes98ca9932018-05-18 10:24:07 +010021#include "fu-engine.h"
Mario Limonciello96a0dd52019-02-25 13:50:03 -060022#include "fu-history.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010023#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010024#include "fu-progressbar.h"
25#include "fu-smbios.h"
26#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050027#include "fu-debug.h"
Mario Limonciello1e35e4c2019-01-28 11:13:02 -060028#include "fwupd-common-private.h"
Mario Limonciello3143bad2019-02-27 07:31:00 -060029#include "fwupd-device-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010030
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050031#define SYSTEMD_SERVICE "org.freedesktop.systemd1"
32#define SYSTEMD_OBJECT_PATH "/org/freedesktop/systemd1"
33#define SYSTEMD_MANAGER_INTERFACE "org.freedesktop.systemd1.Manager"
34#define SYSTEMD_FWUPD_UNIT "fwupd.service"
35
Richard Hughesb5976832018-05-18 10:02:09 +010036/* custom return code */
37#define EXIT_NOTHING_TO_DO 2
38
Mario Limonciello3f243a92019-01-21 22:05:23 -060039typedef enum {
40 FU_UTIL_OPERATION_UNKNOWN,
41 FU_UTIL_OPERATION_UPDATE,
42 FU_UTIL_OPERATION_INSTALL,
43 FU_UTIL_OPERATION_LAST
44} FuUtilOperation;
45
Richard Hughesb5976832018-05-18 10:02:09 +010046typedef struct {
47 GCancellable *cancellable;
48 GMainLoop *loop;
49 GOptionContext *context;
50 GPtrArray *cmd_array;
Richard Hughes98ca9932018-05-18 10:24:07 +010051 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010052 FuProgressbar *progressbar;
Mario Limonciello3f243a92019-01-21 22:05:23 -060053 gboolean no_reboot_check;
Mario Limonciello53ce25d2019-02-01 16:09:03 +000054 gboolean prepare_blob;
55 gboolean cleanup_blob;
Mario Limonciello3143bad2019-02-27 07:31:00 -060056 gboolean enable_json_state;
Richard Hughes460226a2018-05-21 20:56:21 +010057 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050058 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050059 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060060 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050061 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060062 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060063 FwupdDeviceFlags completion_flags;
Richard Hughesb5976832018-05-18 10:02:09 +010064} FuUtilPrivate;
65
66typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
67 gchar **values,
68 GError **error);
69
70typedef struct {
71 gchar *name;
72 gchar *arguments;
73 gchar *description;
74 FuUtilPrivateCb callback;
75} FuUtilItem;
76
77static void
78fu_util_item_free (FuUtilItem *item)
79{
80 g_free (item->name);
81 g_free (item->arguments);
82 g_free (item->description);
83 g_free (item);
84}
85
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050086static gboolean
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060087fu_util_stop_daemon (GError **error)
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050088{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050089 g_autoptr(GDBusConnection) connection = NULL;
90 g_autoptr(GDBusProxy) proxy = NULL;
91 g_autoptr(GVariant) val = NULL;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050092
93 /* try to stop any already running daemon */
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060094 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
Mario Limonciello8101bfc2019-02-07 13:47:44 +000095 if (connection == NULL) {
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060096 g_prefix_error (error, "failed to get bus: ");
97 return FALSE;
Mario Limonciello8101bfc2019-02-07 13:47:44 +000098 }
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050099 proxy = g_dbus_proxy_new_sync (connection,
100 G_DBUS_PROXY_FLAGS_NONE,
101 NULL,
102 SYSTEMD_SERVICE,
103 SYSTEMD_OBJECT_PATH,
104 SYSTEMD_MANAGER_INTERFACE,
105 NULL,
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600106 error);
Mario Limonciello67b82af2018-11-01 13:37:00 -0500107 if (proxy == NULL) {
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600108 g_prefix_error (error, "failed to find %s: ", SYSTEMD_SERVICE);
109 return FALSE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500110 }
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600111 val = g_dbus_proxy_call_sync (proxy,
112 "GetUnit",
113 g_variant_new ("(s)",
114 SYSTEMD_FWUPD_UNIT),
115 G_DBUS_CALL_FLAGS_NONE,
116 -1,
117 NULL,
118 error);
119 if (val == NULL) {
120 g_prefix_error (error, "failed to find %s: ", SYSTEMD_FWUPD_UNIT);
121 return FALSE;
122 }
123 g_variant_unref (val);
124 val = g_dbus_proxy_call_sync (proxy,
125 "StopUnit",
126 g_variant_new ("(ss)",
127 SYSTEMD_FWUPD_UNIT,
128 "replace"),
129 G_DBUS_CALL_FLAGS_NONE,
130 -1,
131 NULL,
132 error);
133 return val != NULL;
134}
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500135
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600136static gboolean
Mario Limonciello3143bad2019-02-27 07:31:00 -0600137fu_util_save_current_state (FuUtilPrivate *priv, GError **error)
138{
139 g_autoptr(JsonBuilder) builder = NULL;
140 g_autoptr(JsonGenerator) json_generator = NULL;
141 g_autoptr(JsonNode) json_root = NULL;
142 g_autoptr(GPtrArray) devices = NULL;
143 g_autofree gchar *state = NULL;
144 g_autofree gchar *dirname = NULL;
145 g_autofree gchar *filename = NULL;
146
147 if (!priv->enable_json_state)
148 return TRUE;
149
150 devices = fu_engine_get_devices (priv->engine, error);
151 if (devices == NULL)
152 return FALSE;
153
154 /* create header */
155 builder = json_builder_new ();
156 json_builder_begin_object (builder);
157
158 /* add each device */
159 json_builder_set_member_name (builder, "Devices");
160 json_builder_begin_array (builder);
161 for (guint i = 0; i < devices->len; i++) {
162 FwupdDevice *dev = g_ptr_array_index (devices, i);
163 json_builder_begin_object (builder);
164 fwupd_device_to_json (dev, builder);
165 json_builder_end_object (builder);
166 }
167 json_builder_end_array (builder);
168 json_builder_end_object (builder);
169
170 /* export as a string */
171 json_root = json_builder_get_root (builder);
172 json_generator = json_generator_new ();
173 json_generator_set_pretty (json_generator, TRUE);
174 json_generator_set_root (json_generator, json_root);
175 state = json_generator_to_data (json_generator, NULL);
176 if (state == NULL)
177 return FALSE;
178 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
179 filename = g_build_filename (dirname, "state.json", NULL);
180 return g_file_set_contents (filename, state, -1, error);
181}
182
183static gboolean
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600184fu_util_start_engine (FuUtilPrivate *priv, GError **error)
185{
186 g_autoptr(GError) error_local = NULL;
187
188 if (!fu_util_stop_daemon (&error_local))
189 g_debug ("Failed top stop daemon: %s", error_local->message);
Richard Hughesf425d292019-01-18 17:57:39 +0000190 if (!fu_engine_load (priv->engine, error))
191 return FALSE;
192 if (fu_engine_get_tainted (priv->engine)) {
193 g_printerr ("WARNING: This tool has loaded 3rd party code and "
194 "is no longer supported by the upstream developers!\n");
195 }
196 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500197}
198
Richard Hughesb5976832018-05-18 10:02:09 +0100199static gint
200fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
201{
202 return g_strcmp0 ((*item1)->name, (*item2)->name);
203}
204
205static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500206fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
207{
208 g_autofree gchar *path = g_path_get_dirname (value);
209 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
210 g_prefix_error (error,
211 "Unable to access %s. You may need to copy %s to %s: ",
212 path, value, g_getenv ("HOME"));
213 }
214}
215
216static void
Richard Hughesb5976832018-05-18 10:02:09 +0100217fu_util_add (GPtrArray *array,
218 const gchar *name,
219 const gchar *arguments,
220 const gchar *description,
221 FuUtilPrivateCb callback)
222{
223 g_auto(GStrv) names = NULL;
224
225 g_return_if_fail (name != NULL);
226 g_return_if_fail (description != NULL);
227 g_return_if_fail (callback != NULL);
228
229 /* add each one */
230 names = g_strsplit (name, ",", -1);
231 for (guint i = 0; names[i] != NULL; i++) {
232 FuUtilItem *item = g_new0 (FuUtilItem, 1);
233 item->name = g_strdup (names[i]);
234 if (i == 0) {
235 item->description = g_strdup (description);
236 } else {
237 /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */
238 item->description = g_strdup_printf (_("Alias to %s"),
239 names[0]);
240 }
241 item->arguments = g_strdup (arguments);
242 item->callback = callback;
243 g_ptr_array_add (array, item);
244 }
245}
246
247static gchar *
248fu_util_get_descriptions (GPtrArray *array)
249{
250 gsize len;
251 const gsize max_len = 35;
252 GString *string;
253
254 /* print each command */
255 string = g_string_new ("");
256 for (guint i = 0; i < array->len; i++) {
257 FuUtilItem *item = g_ptr_array_index (array, i);
258 g_string_append (string, " ");
259 g_string_append (string, item->name);
260 len = strlen (item->name) + 2;
261 if (item->arguments != NULL) {
262 g_string_append (string, " ");
263 g_string_append (string, item->arguments);
264 len += strlen (item->arguments) + 1;
265 }
266 if (len < max_len) {
267 for (gsize j = len; j < max_len + 1; j++)
268 g_string_append_c (string, ' ');
269 g_string_append (string, item->description);
270 g_string_append_c (string, '\n');
271 } else {
272 g_string_append_c (string, '\n');
273 for (gsize j = 0; j < max_len + 1; j++)
274 g_string_append_c (string, ' ');
275 g_string_append (string, item->description);
276 g_string_append_c (string, '\n');
277 }
278 }
279
280 /* remove trailing newline */
281 if (string->len > 0)
282 g_string_set_size (string, string->len - 1);
283
284 return g_string_free (string, FALSE);
285}
286
287static gboolean
288fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error)
289{
290 /* find command */
291 for (guint i = 0; i < priv->cmd_array->len; i++) {
292 FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i);
293 if (g_strcmp0 (item->name, command) == 0)
294 return item->callback (priv, values, error);
295 }
296
297 /* not found */
298 g_set_error_literal (error,
299 FWUPD_ERROR,
300 FWUPD_ERROR_INVALID_ARGS,
301 /* TRANSLATORS: error message */
302 _("Command not found"));
303 return FALSE;
304}
305
306static void
307fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
308{
309 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
310 /* TRANSLATORS: this is when a device ctrl+c's a watch */
311 g_print ("%s\n", _("Cancelled"));
312 g_main_loop_quit (priv->loop);
313}
314
315static gboolean
316fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
317{
318 g_autofree gchar *tmp = NULL;
319 g_autoptr(FuSmbios) smbios = NULL;
320 if (g_strv_length (values) < 1) {
321 g_set_error_literal (error,
322 FWUPD_ERROR,
323 FWUPD_ERROR_INVALID_ARGS,
324 "Invalid arguments");
325 return FALSE;
326 }
327 smbios = fu_smbios_new ();
328 if (!fu_smbios_setup_from_file (smbios, values[0], error))
329 return FALSE;
330 tmp = fu_smbios_to_string (smbios);
331 g_print ("%s\n", tmp);
332 return TRUE;
333}
334
Richard Hughesb5976832018-05-18 10:02:09 +0100335static gboolean
336fu_util_sigint_cb (gpointer user_data)
337{
338 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
339 g_debug ("Handling SIGINT");
340 g_cancellable_cancel (priv->cancellable);
341 return FALSE;
342}
343
344static void
345fu_util_private_free (FuUtilPrivate *priv)
346{
347 if (priv->cmd_array != NULL)
348 g_ptr_array_unref (priv->cmd_array);
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500349 if (priv->current_device != NULL)
350 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100351 if (priv->engine != NULL)
352 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100353 if (priv->loop != NULL)
354 g_main_loop_unref (priv->loop);
355 if (priv->cancellable != NULL)
356 g_object_unref (priv->cancellable);
357 if (priv->progressbar != NULL)
358 g_object_unref (priv->progressbar);
359 if (priv->context != NULL)
360 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600361 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100362 g_free (priv);
363}
364
365#pragma clang diagnostic push
366#pragma clang diagnostic ignored "-Wunused-function"
367G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
368#pragma clang diagnostic pop
369
Richard Hughes98ca9932018-05-18 10:24:07 +0100370
371static void
372fu_main_engine_device_added_cb (FuEngine *engine,
373 FuDevice *device,
374 FuUtilPrivate *priv)
375{
376 g_autofree gchar *tmp = fu_device_to_string (device);
377 g_debug ("ADDED:\n%s", tmp);
378}
379
380static void
381fu_main_engine_device_removed_cb (FuEngine *engine,
382 FuDevice *device,
383 FuUtilPrivate *priv)
384{
385 g_autofree gchar *tmp = fu_device_to_string (device);
386 g_debug ("REMOVED:\n%s", tmp);
387}
388
389static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100390fu_main_engine_status_changed_cb (FuEngine *engine,
391 FwupdStatus status,
392 FuUtilPrivate *priv)
393{
394 fu_progressbar_update (priv->progressbar, status, 0);
395}
396
397static void
398fu_main_engine_percentage_changed_cb (FuEngine *engine,
399 guint percentage,
400 FuUtilPrivate *priv)
401{
402 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
403}
404
405static gboolean
406fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
407{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500408 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100409 return FALSE;
410 g_main_loop_run (priv->loop);
411 return TRUE;
412}
413
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100414static gint
415fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
416{
417 return fu_plugin_name_compare (*item1, *item2);
418}
419
420static gboolean
421fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
422{
423 GPtrArray *plugins;
424 guint cnt = 0;
425
426 /* load engine */
427 if (!fu_engine_load_plugins (priv->engine, error))
428 return FALSE;
429
430 /* print */
431 plugins = fu_engine_get_plugins (priv->engine);
432 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
433 for (guint i = 0; i < plugins->len; i++) {
434 FuPlugin *plugin = g_ptr_array_index (plugins, i);
435 if (!fu_plugin_get_enabled (plugin))
436 continue;
437 g_print ("%s\n", fu_plugin_get_name (plugin));
438 cnt++;
439 }
440 if (cnt == 0) {
441 /* TRANSLATORS: nothing found */
442 g_print ("%s\n", _("No plugins found"));
443 return TRUE;
444 }
445
446 return TRUE;
447}
448
Richard Hughes98ca9932018-05-18 10:24:07 +0100449static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600450fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
451{
452 g_autoptr(GPtrArray) devices = NULL;
453
454 /* load engine */
455 if (!fu_util_start_engine (priv, error))
456 return FALSE;
457
458 /* get devices from daemon */
459 devices = fu_engine_get_devices (priv->engine, error);
460 if (devices == NULL)
461 return FALSE;
462 for (guint i = 0; i < devices->len; i++) {
463 FwupdDevice *dev = g_ptr_array_index (devices, i);
464 g_autoptr(GPtrArray) rels = NULL;
465 g_autoptr(GError) error_local = NULL;
466
467 /* not going to have results, so save a D-Bus round-trip */
468 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
469 continue;
470
471 /* get the releases for this device and filter for validity */
472 rels = fu_engine_get_upgrades (priv->engine,
473 fwupd_device_get_id (dev),
474 &error_local);
475 if (rels == NULL) {
476 g_printerr ("%s\n", error_local->message);
477 continue;
478 }
479 g_print ("%s", fwupd_device_to_string (dev));
480 g_print (" Release information:\n");
481 /* print all releases */
482 for (guint j = 0; j < rels->len; j++) {
483 FwupdRelease *rel = g_ptr_array_index (rels, j);
484 g_print ("%s\n", fwupd_release_to_string (rel));
485 }
486 }
487
Mario Limonciello3143bad2019-02-27 07:31:00 -0600488 /* save the device state for other applications to see */
489 if (!fu_util_save_current_state (priv, error))
490 return FALSE;
491
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600492 /* success */
493 return TRUE;
494}
495
496static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500497fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
498{
499 g_autoptr(GPtrArray) array = NULL;
500 gint fd;
501
502 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500503 if (!fu_util_start_engine (priv, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500504 return FALSE;
505
506 /* check args */
507 if (g_strv_length (values) != 1) {
508 g_set_error_literal (error,
509 FWUPD_ERROR,
510 FWUPD_ERROR_INVALID_ARGS,
511 "Invalid arguments");
512 return FALSE;
513 }
514
515 /* open file */
516 fd = open (values[0], O_RDONLY);
517 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500518 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500519 g_set_error (error,
520 FWUPD_ERROR,
521 FWUPD_ERROR_INVALID_FILE,
522 "failed to open %s",
523 values[0]);
524 return FALSE;
525 }
526 array = fu_engine_get_details (priv->engine, fd, error);
527 close (fd);
528
529 if (array == NULL)
530 return FALSE;
531 for (guint i = 0; i < array->len; i++) {
532 FwupdDevice *dev = g_ptr_array_index (array, i);
533 g_autofree gchar *tmp = NULL;
534 tmp = fwupd_device_to_string (dev);
Mario Limoncielloece90a42018-07-25 17:35:55 -0500535 g_print ("%s\n", tmp);
Mario Limonciello716ab272018-05-29 12:34:37 -0500536 }
537 return TRUE;
538}
539
540static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100541fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
542{
543 g_autoptr(GPtrArray) devs = NULL;
544
545 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500546 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100547 return FALSE;
548
549 /* print */
550 devs = fu_engine_get_devices (priv->engine, error);
551 if (devs == NULL)
552 return FALSE;
553 if (devs->len == 0) {
554 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500555 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100556 return TRUE;
557 }
558 for (guint i = 0; i < devs->len; i++) {
559 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500560 if (priv->show_all_devices || fu_util_is_interesting_device (dev)) {
561 g_autofree gchar *tmp = fwupd_device_to_string (dev);
562 g_print ("%s\n", tmp);
563 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100564 }
565
Mario Limonciello3143bad2019-02-27 07:31:00 -0600566 /* save the device state for other applications to see */
567 if (!fu_util_save_current_state (priv, error))
568 return FALSE;
569
Richard Hughes98ca9932018-05-18 10:24:07 +0100570 return TRUE;
571}
572
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500573static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100574fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500575{
576 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100577 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500578 if (!priv->show_all_devices &&
579 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500580 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100581 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500582 GNode *child = g_node_append_data (root, dev_tmp);
583 fu_util_build_device_tree (priv, child, devs, dev_tmp);
584 }
585 }
586}
587
588static gboolean
589fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
590{
591 g_autoptr(GNode) root = g_node_new (NULL);
592 g_autoptr(GPtrArray) devs = NULL;
593
594 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500595 if (!fu_util_start_engine (priv, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500596 return FALSE;
597
598 /* print */
599 devs = fu_engine_get_devices (priv->engine, error);
600 if (devs == NULL)
601 return FALSE;
602
603 /* print */
604 if (devs->len == 0) {
605 /* TRANSLATORS: nothing attached that can be upgraded */
606 g_print ("%s\n", _("No hardware detected with firmware update capability"));
607 return TRUE;
608 }
609 fu_util_build_device_tree (priv, root, devs, NULL);
610 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
611 fu_util_print_device_tree, priv);
612
613 return TRUE;
614}
615
616
Richard Hughes98ca9932018-05-18 10:24:07 +0100617static FuDevice *
618fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
619{
620 FuDevice *dev;
621 guint idx;
622 g_autoptr(GPtrArray) devices = NULL;
623
624 /* get devices from daemon */
625 devices = fu_engine_get_devices (priv->engine, error);
626 if (devices == NULL)
627 return NULL;
628
629 /* exactly one */
630 if (devices->len == 1) {
631 dev = g_ptr_array_index (devices, 0);
632 return g_object_ref (dev);
633 }
634
635 /* TRANSLATORS: get interactive prompt */
636 g_print ("%s\n", _("Choose a device:"));
637 /* TRANSLATORS: this is to abort the interactive prompt */
638 g_print ("0.\t%s\n", _("Cancel"));
639 for (guint i = 0; i < devices->len; i++) {
640 dev = g_ptr_array_index (devices, i);
641 g_print ("%u.\t%s (%s)\n",
642 i + 1,
643 fu_device_get_id (dev),
644 fu_device_get_name (dev));
645 }
646 idx = fu_util_prompt_for_number (devices->len);
647 if (idx == 0) {
648 g_set_error_literal (error,
649 FWUPD_ERROR,
650 FWUPD_ERROR_NOTHING_TO_DO,
651 "Request canceled");
652 return NULL;
653 }
654 dev = g_ptr_array_index (devices, idx - 1);
655 return g_object_ref (dev);
656}
657
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500658static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600659fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500660 FwupdDevice *device,
661 FuUtilPrivate *priv)
662{
663 g_autofree gchar *str = NULL;
664
665 /* same as last time, so ignore */
666 if (priv->current_device != NULL &&
667 fwupd_device_compare (priv->current_device, device) == 0)
668 return;
669
670 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600671 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
672 /* TRANSLATORS: %1 is a device name */
673 str = g_strdup_printf (_("Updating %s…"),
674 fwupd_device_get_name (device));
675 fu_progressbar_set_title (priv->progressbar, str);
676 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
677 /* TRANSLATORS: %1 is a device name */
678 str = g_strdup_printf (_("Installing on %s…"),
679 fwupd_device_get_name (device));
680 fu_progressbar_set_title (priv->progressbar, str);
681 } else {
682 g_warning ("no FuUtilOperation set");
683 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500684 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600685
686 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
687 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
688 else if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
689 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
Mario Limonciello32241f42019-01-24 10:12:41 -0600690
691 if (priv->current_message == NULL) {
692 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
693 if (tmp != NULL)
694 priv->current_message = g_strdup (tmp);
695 }
696}
697
698static void
699fu_util_display_current_message (FuUtilPrivate *priv)
700{
701 if (priv->current_message == NULL)
702 return;
703 g_print ("%s\n", priv->current_message);
704 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500705}
706
Richard Hughes98ca9932018-05-18 10:24:07 +0100707static gboolean
708fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
709{
710 g_autoptr(FuDevice) device = NULL;
711 g_autoptr(GBytes) blob_fw = NULL;
712
713 /* invalid args */
714 if (g_strv_length (values) == 0) {
715 g_set_error_literal (error,
716 FWUPD_ERROR,
717 FWUPD_ERROR_INVALID_ARGS,
718 "Invalid arguments");
719 return FALSE;
720 }
721
722 /* parse blob */
723 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500724 if (blob_fw == NULL) {
725 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100726 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500727 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100728
729 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500730 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100731 return FALSE;
732
733 /* get device */
734 if (g_strv_length (values) >= 2) {
735 device = fu_engine_get_device (priv->engine, values[1], error);
736 if (device == NULL)
737 return FALSE;
738 } else {
739 device = fu_util_prompt_for_device (priv, error);
740 if (device == NULL)
741 return FALSE;
742 }
743
Mario Limonciello3f243a92019-01-21 22:05:23 -0600744 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500745 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600746 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500747
Richard Hughes98ca9932018-05-18 10:24:07 +0100748 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000749 if (priv->prepare_blob) {
750 g_autoptr(GPtrArray) devices = NULL;
751 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
752 g_ptr_array_add (devices, g_object_ref (device));
753 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
754 g_prefix_error (error, "failed to prepare composite action: ");
755 return FALSE;
756 }
757 }
Richard Hughes84af6e72019-02-01 18:19:41 +0000758 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600759 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000760 if (priv->cleanup_blob) {
761 g_autoptr(FuDevice) device_new = NULL;
762 g_autoptr(GError) error_local = NULL;
763
764 /* get the possibly new device from the old ID */
765 device_new = fu_engine_get_device (priv->engine,
766 fu_device_get_id (device),
767 &error_local);
768 if (device_new == NULL) {
769 g_debug ("failed to find new device: %s",
770 error_local->message);
771 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600772 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000773 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
774 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
775 g_prefix_error (error, "failed to cleanup composite action: ");
776 return FALSE;
777 }
778 }
779 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600780
Mario Limonciello32241f42019-01-24 10:12:41 -0600781 fu_util_display_current_message (priv);
782
Mario Limonciello3f243a92019-01-21 22:05:23 -0600783 /* success */
784 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100785}
786
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100787static gint
788fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
789{
790 FuInstallTask *task1 = *((FuInstallTask **) a);
791 FuInstallTask *task2 = *((FuInstallTask **) b);
792 return fu_install_task_compare (task1, task2);
793}
794
795static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100796fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
797{
798 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
799 { "curl", uri, "--output", fn, NULL },
800 { NULL } };
801 for (guint i = 0; argv[i][0] != NULL; i++) {
802 g_autoptr(GError) error_local = NULL;
803 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
804 g_debug ("%s", error_local->message);
805 continue;
806 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000807 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100808 }
809 g_set_error_literal (error,
810 FWUPD_ERROR,
811 FWUPD_ERROR_NOT_FOUND,
812 "no supported out-of-process downloaders found");
813 return FALSE;
814}
815
816static gchar *
817fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
818{
819 g_autofree gchar *filename = NULL;
820 g_autoptr(SoupURI) uri = NULL;
821
822 /* a local file */
823 uri = soup_uri_new (perhapsfn);
824 if (uri == NULL)
825 return g_strdup (perhapsfn);
826
827 /* download the firmware to a cachedir */
828 filename = fu_util_get_user_cache_path (perhapsfn);
829 if (!fu_common_mkdir_parent (filename, error))
830 return NULL;
831 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
832 return NULL;
833 return g_steal_pointer (&filename);
834}
835
836static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100837fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
838{
Richard Hughes3d178be2018-08-30 11:14:24 +0100839 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100840 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100841 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100842 g_autoptr(GPtrArray) devices_possible = NULL;
843 g_autoptr(GPtrArray) errors = NULL;
844 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100845 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100846
Mario Limonciello8949e892018-05-25 08:03:06 -0500847 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500848 if (!fu_util_start_engine (priv, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500849 return FALSE;
850
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100851 /* handle both forms */
852 if (g_strv_length (values) == 1) {
853 devices_possible = fu_engine_get_devices (priv->engine, error);
854 if (devices_possible == NULL)
855 return FALSE;
856 } else if (g_strv_length (values) == 2) {
857 FuDevice *device = fu_engine_get_device (priv->engine,
858 values[1],
859 error);
860 if (device == NULL)
861 return FALSE;
862 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
863 g_ptr_array_add (devices_possible, device);
864 } else {
865 g_set_error_literal (error,
866 FWUPD_ERROR,
867 FWUPD_ERROR_INVALID_ARGS,
868 "Invalid arguments");
869 return FALSE;
870 }
871
Richard Hughes3d178be2018-08-30 11:14:24 +0100872 /* download if required */
873 filename = fu_util_download_if_required (priv, values[0], error);
874 if (filename == NULL)
875 return FALSE;
876
Richard Hughes481aa2a2018-09-18 20:51:46 +0100877 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100878 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500879 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100880 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100881 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500882 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100883 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
884 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100885 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600886 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100887 if (components == NULL)
888 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100889
Richard Hughes481aa2a2018-09-18 20:51:46 +0100890 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100891 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
892 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100893 for (guint i = 0; i < components->len; i++) {
894 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100895
896 /* do any devices pass the requirements */
897 for (guint j = 0; j < devices_possible->len; j++) {
898 FuDevice *device = g_ptr_array_index (devices_possible, j);
899 g_autoptr(FuInstallTask) task = NULL;
900 g_autoptr(GError) error_local = NULL;
901
902 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100903 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100904 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100905 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100906 &error_local)) {
907 g_debug ("requirement on %s:%s failed: %s",
908 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100909 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100910 error_local->message);
911 g_ptr_array_add (errors, g_steal_pointer (&error_local));
912 continue;
913 }
914
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600915 /* if component should have an update message from CAB */
916 fu_device_incorporate_from_component (device, component);
917
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100918 /* success */
919 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
920 }
921 }
922
923 /* order the install tasks by the device priority */
924 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
925
926 /* nothing suitable */
927 if (install_tasks->len == 0) {
928 GError *error_tmp = fu_common_error_array_get_best (errors);
929 g_propagate_error (error, error_tmp);
930 return FALSE;
931 }
932
Mario Limonciello3f243a92019-01-21 22:05:23 -0600933 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500934 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600935 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500936
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100937 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100938 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
939 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100940
Mario Limonciello32241f42019-01-24 10:12:41 -0600941 fu_util_display_current_message (priv);
942
Mario Limonciello3f243a92019-01-21 22:05:23 -0600943 /* we don't want to ask anything */
944 if (priv->no_reboot_check) {
945 g_debug ("skipping reboot check");
946 return TRUE;
947 }
948
Mario Limonciello3143bad2019-02-27 07:31:00 -0600949 /* save the device state for other applications to see */
950 if (!fu_util_save_current_state (priv, error))
951 return FALSE;
952
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100953 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600954 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100955}
956
Richard Hughes98ca9932018-05-18 10:24:07 +0100957static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600958fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
959{
960 g_autoptr(GPtrArray) devices = NULL;
961
962 /* load engine */
963 if (!fu_util_start_engine (priv, error))
964 return FALSE;
965
Mario Limonciello3f243a92019-01-21 22:05:23 -0600966 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
967 g_signal_connect (priv->engine, "device-changed",
968 G_CALLBACK (fu_util_update_device_changed_cb), priv);
969
Mario Limonciello46aaee82019-01-10 12:58:00 -0600970 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000971 if (devices == NULL)
972 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600973 for (guint i = 0; i < devices->len; i++) {
974 FwupdDevice *dev = g_ptr_array_index (devices, i);
975 FwupdRelease *rel;
976 const gchar *remote_id;
977 const gchar *device_id;
978 const gchar *uri_tmp;
979 g_autoptr(GPtrArray) rels = NULL;
980 g_autoptr(GError) error_local = NULL;
981
982 if (!fu_util_is_interesting_device (dev))
983 continue;
984 /* only show stuff that has metadata available */
985 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
986 continue;
987
988 device_id = fu_device_get_id (dev);
989 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
990 if (rels == NULL) {
991 g_printerr ("%s\n", error_local->message);
992 continue;
993 }
994
995 rel = g_ptr_array_index (rels, 0);
996 uri_tmp = fwupd_release_get_uri (rel);
997 remote_id = fwupd_release_get_remote_id (rel);
998 if (remote_id != NULL) {
999 FwupdRemote *remote;
1000 g_auto(GStrv) argv = NULL;
1001
1002 remote = fu_engine_get_remote_by_id (priv->engine,
1003 remote_id,
1004 &error_local);
1005 if (remote == NULL) {
1006 g_printerr ("%s\n", error_local->message);
1007 continue;
1008 }
1009
1010 argv = g_new0 (gchar *, 2);
1011 /* local remotes have the firmware already */
Mario Limonciello4f24d0b2019-01-26 01:19:59 -06001012 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
Mario Limonciello46aaee82019-01-10 12:58:00 -06001013 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
1014 g_autofree gchar *path = g_path_get_dirname (fn_cache);
1015 argv[0] = g_build_filename (path, uri_tmp, NULL);
Mario Limonciello4f24d0b2019-01-26 01:19:59 -06001016 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
1017 argv[0] = g_strdup (uri_tmp + 7);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001018 /* web remote, fu_util_install will download file */
1019 } else {
1020 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
1021 }
1022 if (!fu_util_install (priv, argv, &error_local)) {
1023 g_printerr ("%s\n", error_local->message);
1024 continue;
1025 }
Mario Limonciello32241f42019-01-24 10:12:41 -06001026 fu_util_display_current_message (priv);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001027 }
1028 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001029
1030 /* we don't want to ask anything */
1031 if (priv->no_reboot_check) {
1032 g_debug ("skipping reboot check");
1033 return TRUE;
1034 }
1035
Mario Limonciello3143bad2019-02-27 07:31:00 -06001036 /* save the device state for other applications to see */
1037 if (!fu_util_save_current_state (priv, error))
1038 return FALSE;
1039
Mario Limonciello3f243a92019-01-21 22:05:23 -06001040 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001041}
1042
1043static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +01001044fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
1045{
1046 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001047 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001048
1049 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -05001050 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001051 return FALSE;
1052
1053 /* invalid args */
1054 if (g_strv_length (values) == 0) {
1055 g_set_error_literal (error,
1056 FWUPD_ERROR,
1057 FWUPD_ERROR_INVALID_ARGS,
1058 "Invalid arguments");
1059 return FALSE;
1060 }
1061
1062 /* get device */
1063 if (g_strv_length (values) >= 1) {
1064 device = fu_engine_get_device (priv->engine, values[0], error);
1065 if (device == NULL)
1066 return FALSE;
1067 } else {
1068 device = fu_util_prompt_for_device (priv, error);
1069 if (device == NULL)
1070 return FALSE;
1071 }
1072
1073 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001074 locker = fu_device_locker_new (device, error);
1075 if (locker == NULL)
1076 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001077 return fu_device_detach (device, error);
1078}
1079
1080static gboolean
1081fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1082{
1083 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001084 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001085
1086 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -05001087 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001088 return FALSE;
1089
1090 /* invalid args */
1091 if (g_strv_length (values) == 0) {
1092 g_set_error_literal (error,
1093 FWUPD_ERROR,
1094 FWUPD_ERROR_INVALID_ARGS,
1095 "Invalid arguments");
1096 return FALSE;
1097 }
1098
1099 /* get device */
1100 if (g_strv_length (values) >= 1) {
1101 device = fu_engine_get_device (priv->engine, values[0], error);
1102 if (device == NULL)
1103 return FALSE;
1104 } else {
1105 device = fu_util_prompt_for_device (priv, error);
1106 if (device == NULL)
1107 return FALSE;
1108 }
1109
1110 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001111 locker = fu_device_locker_new (device, error);
1112 if (locker == NULL)
1113 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001114 return fu_device_attach (device, error);
1115}
1116
Richard Hughes1d894f12018-08-31 13:05:51 +01001117static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001118fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1119{
1120 gboolean has_pending = FALSE;
1121 g_autoptr(FuHistory) history = fu_history_new ();
1122 g_autoptr(GPtrArray) devices = NULL;
1123
1124 /* check the history database before starting the daemon */
1125 if (g_strv_length (values) == 0) {
1126 devices = fu_history_get_devices (history, error);
1127 if (devices == NULL)
1128 return FALSE;
1129 } else if (g_strv_length (values) == 1) {
1130 FuDevice *device;
1131 device = fu_history_get_device_by_id (history, values[0], error);
1132 if (device == NULL)
1133 return FALSE;
1134 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1135 g_ptr_array_add (devices, device);
1136 } else {
1137 g_set_error_literal (error,
1138 FWUPD_ERROR,
1139 FWUPD_ERROR_INVALID_ARGS,
1140 "Invalid arguments");
1141 return FALSE;
1142 }
1143
1144 /* nothing to do */
1145 for (guint i = 0; i < devices->len; i++) {
1146 FuDevice *dev = g_ptr_array_index (devices, i);
1147 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1148 fu_engine_add_plugin_filter (priv->engine,
1149 fu_device_get_plugin (dev));
1150 has_pending = TRUE;
1151 }
1152 }
1153 if (!has_pending) {
1154 g_set_error_literal (error,
1155 FWUPD_ERROR,
1156 FWUPD_ERROR_NOTHING_TO_DO,
1157 "No firmware to activate");
1158 return FALSE;
1159
1160 }
1161
1162 /* load engine */
1163 if (!fu_util_start_engine (priv, error))
1164 return FALSE;
1165
1166 /* activate anything with _NEEDS_ACTIVATION */
1167 for (guint i = 0; i < devices->len; i++) {
1168 FuDevice *device = g_ptr_array_index (devices, i);
1169 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1170 continue;
1171 /* TRANSLATORS: shown when shutting down to switch to the new version */
1172 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1173 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1174 return FALSE;
1175 }
1176
1177 return TRUE;
1178}
1179
1180static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001181fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1182{
1183 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1184 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1185 const gchar *hwid_keys[] = {
1186 FU_HWIDS_KEY_BIOS_VENDOR,
1187 FU_HWIDS_KEY_BIOS_VERSION,
1188 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1189 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1190 FU_HWIDS_KEY_MANUFACTURER,
1191 FU_HWIDS_KEY_FAMILY,
1192 FU_HWIDS_KEY_PRODUCT_NAME,
1193 FU_HWIDS_KEY_PRODUCT_SKU,
1194 FU_HWIDS_KEY_ENCLOSURE_KIND,
1195 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1196 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1197 NULL };
1198
1199 /* read DMI data */
1200 if (g_strv_length (values) == 0) {
1201 if (!fu_smbios_setup (smbios, error))
1202 return FALSE;
1203 } else if (g_strv_length (values) == 1) {
1204 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1205 return FALSE;
1206 } else {
1207 g_set_error_literal (error,
1208 FWUPD_ERROR,
1209 FWUPD_ERROR_INVALID_ARGS,
1210 "Invalid arguments");
1211 return FALSE;
1212 }
1213 if (!fu_hwids_setup (hwids, smbios, error))
1214 return FALSE;
1215
1216 /* show debug output */
1217 g_print ("Computer Information\n");
1218 g_print ("--------------------\n");
1219 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1220 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1221 if (tmp == NULL)
1222 continue;
1223 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1224 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1225 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1226 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1227 } else {
1228 g_print ("%s: %s\n", hwid_keys[i], tmp);
1229 }
1230 }
1231
1232 /* show GUIDs */
1233 g_print ("\nHardware IDs\n");
1234 g_print ("------------\n");
1235 for (guint i = 0; i < 15; i++) {
1236 const gchar *keys = NULL;
1237 g_autofree gchar *guid = NULL;
1238 g_autofree gchar *key = NULL;
1239 g_autofree gchar *keys_str = NULL;
1240 g_auto(GStrv) keysv = NULL;
1241 g_autoptr(GError) error_local = NULL;
1242
1243 /* get the GUID */
1244 key = g_strdup_printf ("HardwareID-%u", i);
1245 keys = fu_hwids_get_replace_keys (hwids, key);
1246 guid = fu_hwids_get_guid (hwids, key, &error_local);
1247 if (guid == NULL) {
1248 g_print ("%s\n", error_local->message);
1249 continue;
1250 }
1251
1252 /* show what makes up the GUID */
1253 keysv = g_strsplit (keys, "&", -1);
1254 keys_str = g_strjoinv (" + ", keysv);
1255 g_print ("{%s} <- %s\n", guid, keys_str);
1256 }
1257
1258 return TRUE;
1259}
1260
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001261static gboolean
1262fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1263{
1264 const gchar *script_fn = "startup.sh";
1265 const gchar *output_fn = "firmware.bin";
1266 g_autoptr(GBytes) archive_blob = NULL;
1267 g_autoptr(GBytes) firmware_blob = NULL;
1268 if (g_strv_length (values) < 2) {
1269 g_set_error_literal (error,
1270 FWUPD_ERROR,
1271 FWUPD_ERROR_INVALID_ARGS,
1272 "Invalid arguments");
1273 return FALSE;
1274 }
1275 archive_blob = fu_common_get_contents_bytes (values[0], error);
1276 if (archive_blob == NULL)
1277 return FALSE;
1278 if (g_strv_length (values) > 2)
1279 script_fn = values[2];
1280 if (g_strv_length (values) > 3)
1281 output_fn = values[3];
1282 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1283 if (firmware_blob == NULL)
1284 return FALSE;
1285 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1286}
1287
Mario Limonciello62f84862018-10-18 13:15:23 -05001288static void
1289fu_util_device_added_cb (FwupdClient *client,
1290 FwupdDevice *device,
1291 gpointer user_data)
1292{
1293 g_autofree gchar *tmp = fwupd_device_to_string (device);
1294 /* TRANSLATORS: this is when a device is hotplugged */
1295 g_print ("%s\n%s", _("Device added:"), tmp);
1296}
1297
1298static void
1299fu_util_device_removed_cb (FwupdClient *client,
1300 FwupdDevice *device,
1301 gpointer user_data)
1302{
1303 g_autofree gchar *tmp = fwupd_device_to_string (device);
1304 /* TRANSLATORS: this is when a device is hotplugged */
1305 g_print ("%s\n%s", _("Device removed:"), tmp);
1306}
1307
1308static void
1309fu_util_device_changed_cb (FwupdClient *client,
1310 FwupdDevice *device,
1311 gpointer user_data)
1312{
1313 g_autofree gchar *tmp = fwupd_device_to_string (device);
1314 /* TRANSLATORS: this is when a device has been updated */
1315 g_print ("%s\n%s", _("Device changed:"), tmp);
1316}
1317
1318static void
1319fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1320{
1321 /* TRANSLATORS: this is when the daemon state changes */
1322 g_print ("%s\n", _("Changed"));
1323}
1324
1325static gboolean
1326fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1327{
1328 g_autoptr(FwupdClient) client = fwupd_client_new ();
1329
1330 /* get all the devices */
1331 if (!fwupd_client_connect (client, priv->cancellable, error))
1332 return FALSE;
1333
1334 /* watch for any hotplugged device */
1335 g_signal_connect (client, "changed",
1336 G_CALLBACK (fu_util_changed_cb), priv);
1337 g_signal_connect (client, "device-added",
1338 G_CALLBACK (fu_util_device_added_cb), priv);
1339 g_signal_connect (client, "device-removed",
1340 G_CALLBACK (fu_util_device_removed_cb), priv);
1341 g_signal_connect (client, "device-changed",
1342 G_CALLBACK (fu_util_device_changed_cb), priv);
1343 g_signal_connect (priv->cancellable, "cancelled",
1344 G_CALLBACK (fu_util_cancelled_cb), priv);
1345 g_main_loop_run (priv->loop);
1346 return TRUE;
1347}
1348
Richard Hughesb5976832018-05-18 10:02:09 +01001349int
1350main (int argc, char *argv[])
1351{
Richard Hughes460226a2018-05-21 20:56:21 +01001352 gboolean allow_older = FALSE;
1353 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001354 gboolean force = FALSE;
1355 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001356 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001357 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001358 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001359 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1360 g_autoptr(GError) error = NULL;
1361 g_autofree gchar *cmd_descriptions = NULL;
1362 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001363 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1364 /* TRANSLATORS: command line option */
1365 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001366 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1367 /* TRANSLATORS: command line option */
1368 _("Allow re-installing existing firmware versions"), NULL },
1369 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1370 /* TRANSLATORS: command line option */
1371 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001372 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1373 /* TRANSLATORS: command line option */
1374 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001375 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1376 /* TRANSLATORS: command line option */
1377 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001378 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1379 /* TRANSLATORS: command line option */
1380 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001381 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1382 /* TRANSLATORS: command line option */
1383 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001384 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001385 /* TRANSLATORS: command line option */
1386 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001387 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001388 /* TRANSLATORS: command line option */
1389 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001390 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1391 /* TRANSLATORS: command line option */
1392 _("Save device state into a JSON file between executions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001393 { NULL}
1394 };
1395
1396 setlocale (LC_ALL, "");
1397
1398 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1399 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1400 textdomain (GETTEXT_PACKAGE);
1401
1402 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001403 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001404 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001405 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001406
1407 /* create helper object */
1408 priv->loop = g_main_loop_new (NULL, FALSE);
1409 priv->progressbar = fu_progressbar_new ();
1410
1411 /* add commands */
1412 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
1413 fu_util_add (priv->cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001414 "build-firmware",
1415 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1416 /* TRANSLATORS: command description */
1417 _("Build firmware using a sandbox"),
1418 fu_util_firmware_builder);
1419 fu_util_add (priv->cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001420 "smbios-dump",
1421 "FILE",
1422 /* TRANSLATORS: command description */
1423 _("Dump SMBIOS data from a file"),
1424 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +01001425 fu_util_add (priv->cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001426 "get-plugins",
1427 NULL,
1428 /* TRANSLATORS: command description */
1429 _("Get all enabled plugins registered with the system"),
1430 fu_util_get_plugins);
1431 fu_util_add (priv->cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001432 "get-details",
1433 NULL,
1434 /* TRANSLATORS: command description */
1435 _("Gets details about a firmware file"),
1436 fu_util_get_details);
1437 fu_util_add (priv->cmd_array,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001438 "get-updates",
1439 NULL,
1440 /* TRANSLATORS: command description */
1441 _("Gets the list of updates for connected hardware"),
1442 fu_util_get_updates);
1443 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001444 "get-devices",
1445 NULL,
1446 /* TRANSLATORS: command description */
1447 _("Get all devices that support firmware updates"),
1448 fu_util_get_devices);
1449 fu_util_add (priv->cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001450 "get-topology",
1451 NULL,
1452 /* TRANSLATORS: command description */
1453 _("Get all devices according to the system topology"),
1454 fu_util_get_topology);
1455 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001456 "watch",
1457 NULL,
1458 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001459 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001460 fu_util_watch);
1461 fu_util_add (priv->cmd_array,
1462 "install-blob",
1463 "FILENAME DEVICE-ID",
1464 /* TRANSLATORS: command description */
1465 _("Install a firmware blob on a device"),
1466 fu_util_install_blob);
1467 fu_util_add (priv->cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001468 "install",
1469 "FILE [ID]",
1470 /* TRANSLATORS: command description */
1471 _("Install a firmware file on this hardware"),
1472 fu_util_install);
1473 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001474 "attach",
1475 "DEVICE-ID",
1476 /* TRANSLATORS: command description */
1477 _("Attach to firmware mode"),
1478 fu_util_attach);
1479 fu_util_add (priv->cmd_array,
1480 "detach",
1481 "DEVICE-ID",
1482 /* TRANSLATORS: command description */
1483 _("Detach to bootloader mode"),
1484 fu_util_detach);
Richard Hughes1d894f12018-08-31 13:05:51 +01001485 fu_util_add (priv->cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001486 "activate",
1487 "[DEVICE-ID]",
1488 /* TRANSLATORS: command description */
1489 _("Activate pending devices"),
1490 fu_util_activate);
1491 fu_util_add (priv->cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001492 "hwids",
1493 "[FILE]",
1494 /* TRANSLATORS: command description */
1495 _("Return all the hardware IDs for the machine"),
1496 fu_util_hwids);
Mario Limonciello62f84862018-10-18 13:15:23 -05001497 fu_util_add (priv->cmd_array,
1498 "monitor",
1499 NULL,
1500 /* TRANSLATORS: command description */
1501 _("Monitor the daemon for events"),
1502 fu_util_monitor);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001503 fu_util_add (priv->cmd_array,
1504 "update",
1505 NULL,
1506 /* TRANSLATORS: command description */
1507 _("Update all devices that match local metadata"),
1508 fu_util_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001509
1510 /* do stuff on ctrl+c */
1511 priv->cancellable = g_cancellable_new ();
1512 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1513 SIGINT, fu_util_sigint_cb,
1514 priv, NULL);
1515 g_signal_connect (priv->cancellable, "cancelled",
1516 G_CALLBACK (fu_util_cancelled_cb), priv);
1517
1518 /* sort by command name */
1519 g_ptr_array_sort (priv->cmd_array,
1520 (GCompareFunc) fu_sort_command_name_cb);
1521
Mario Limonciello3f243a92019-01-21 22:05:23 -06001522 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001523 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001524 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001525 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1526 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001527
Richard Hughesb5976832018-05-18 10:02:09 +01001528 /* get a list of the commands */
1529 priv->context = g_option_context_new (NULL);
1530 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
1531 g_option_context_set_summary (priv->context, cmd_descriptions);
1532 g_option_context_set_description (priv->context,
1533 "This tool allows an administrator to use the fwupd plugins "
1534 "without being installed on the host system.");
1535
1536 /* TRANSLATORS: program name */
1537 g_set_application_name (_("Firmware Utility"));
1538 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001539 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001540 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1541 if (!ret) {
1542 /* TRANSLATORS: the user didn't read the man page */
1543 g_print ("%s: %s\n", _("Failed to parse arguments"),
1544 error->message);
1545 return EXIT_FAILURE;
1546 }
1547
Richard Hughes460226a2018-05-21 20:56:21 +01001548 /* set flags */
1549 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1550 if (allow_reinstall)
1551 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1552 if (allow_older)
1553 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1554 if (force)
1555 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1556
Richard Hughes98ca9932018-05-18 10:24:07 +01001557 /* load engine */
1558 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1559 g_signal_connect (priv->engine, "device-added",
1560 G_CALLBACK (fu_main_engine_device_added_cb),
1561 priv);
1562 g_signal_connect (priv->engine, "device-removed",
1563 G_CALLBACK (fu_main_engine_device_removed_cb),
1564 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001565 g_signal_connect (priv->engine, "status-changed",
1566 G_CALLBACK (fu_main_engine_status_changed_cb),
1567 priv);
1568 g_signal_connect (priv->engine, "percentage-changed",
1569 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1570 priv);
1571
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001572 /* just show versions and exit */
1573 if (version) {
1574 g_autofree gchar *version_str = fu_util_get_versions ();
1575 g_print ("%s\n", version_str);
1576 return EXIT_SUCCESS;
1577 }
1578
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001579 /* any plugin whitelist specified */
1580 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1581 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1582
Richard Hughesb5976832018-05-18 10:02:09 +01001583 /* run the specified command */
1584 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
1585 if (!ret) {
1586 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1587 g_autofree gchar *tmp = NULL;
1588 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1589 g_print ("%s\n\n%s", error->message, tmp);
1590 return EXIT_FAILURE;
1591 }
1592 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1593 g_print ("%s\n", error->message);
1594 return EXIT_NOTHING_TO_DO;
1595 }
1596 g_print ("%s\n", error->message);
1597 return EXIT_FAILURE;
1598 }
1599
1600 /* success */
1601 return EXIT_SUCCESS;
1602}