blob: b1fa88b68bf2261959e1aadcb565e181e5fdfd0f [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 Hughesc77e1112019-03-01 10:16:26 +000046struct FuUtilPrivate {
Richard Hughesb5976832018-05-18 10:02:09 +010047 GCancellable *cancellable;
48 GMainLoop *loop;
49 GOptionContext *context;
Richard Hughes98ca9932018-05-18 10:24:07 +010050 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010051 FuProgressbar *progressbar;
Mario Limonciello3f243a92019-01-21 22:05:23 -060052 gboolean no_reboot_check;
Mario Limonciello53ce25d2019-02-01 16:09:03 +000053 gboolean prepare_blob;
54 gboolean cleanup_blob;
Mario Limonciello3143bad2019-02-27 07:31:00 -060055 gboolean enable_json_state;
Richard Hughes460226a2018-05-21 20:56:21 +010056 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050057 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050058 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060059 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050060 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060061 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060062 FwupdDeviceFlags completion_flags;
Richard Hughesc77e1112019-03-01 10:16:26 +000063};
Richard Hughesb5976832018-05-18 10:02:09 +010064
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050065static gboolean
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060066fu_util_stop_daemon (GError **error)
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050067{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050068 g_autoptr(GDBusConnection) connection = NULL;
69 g_autoptr(GDBusProxy) proxy = NULL;
70 g_autoptr(GVariant) val = NULL;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050071
72 /* try to stop any already running daemon */
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060073 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
Mario Limonciello8101bfc2019-02-07 13:47:44 +000074 if (connection == NULL) {
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060075 g_prefix_error (error, "failed to get bus: ");
76 return FALSE;
Mario Limonciello8101bfc2019-02-07 13:47:44 +000077 }
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050078 proxy = g_dbus_proxy_new_sync (connection,
79 G_DBUS_PROXY_FLAGS_NONE,
80 NULL,
81 SYSTEMD_SERVICE,
82 SYSTEMD_OBJECT_PATH,
83 SYSTEMD_MANAGER_INTERFACE,
84 NULL,
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060085 error);
Mario Limonciello67b82af2018-11-01 13:37:00 -050086 if (proxy == NULL) {
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060087 g_prefix_error (error, "failed to find %s: ", SYSTEMD_SERVICE);
88 return FALSE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050089 }
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -060090 val = g_dbus_proxy_call_sync (proxy,
91 "GetUnit",
92 g_variant_new ("(s)",
93 SYSTEMD_FWUPD_UNIT),
94 G_DBUS_CALL_FLAGS_NONE,
95 -1,
96 NULL,
97 error);
98 if (val == NULL) {
99 g_prefix_error (error, "failed to find %s: ", SYSTEMD_FWUPD_UNIT);
100 return FALSE;
101 }
102 g_variant_unref (val);
103 val = g_dbus_proxy_call_sync (proxy,
104 "StopUnit",
105 g_variant_new ("(ss)",
106 SYSTEMD_FWUPD_UNIT,
107 "replace"),
108 G_DBUS_CALL_FLAGS_NONE,
109 -1,
110 NULL,
111 error);
112 return val != NULL;
113}
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500114
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600115static gboolean
Mario Limonciello3143bad2019-02-27 07:31:00 -0600116fu_util_save_current_state (FuUtilPrivate *priv, GError **error)
117{
118 g_autoptr(JsonBuilder) builder = NULL;
119 g_autoptr(JsonGenerator) json_generator = NULL;
120 g_autoptr(JsonNode) json_root = NULL;
121 g_autoptr(GPtrArray) devices = NULL;
122 g_autofree gchar *state = NULL;
123 g_autofree gchar *dirname = NULL;
124 g_autofree gchar *filename = NULL;
125
126 if (!priv->enable_json_state)
127 return TRUE;
128
129 devices = fu_engine_get_devices (priv->engine, error);
130 if (devices == NULL)
131 return FALSE;
132
133 /* create header */
134 builder = json_builder_new ();
135 json_builder_begin_object (builder);
136
137 /* add each device */
138 json_builder_set_member_name (builder, "Devices");
139 json_builder_begin_array (builder);
140 for (guint i = 0; i < devices->len; i++) {
141 FwupdDevice *dev = g_ptr_array_index (devices, i);
142 json_builder_begin_object (builder);
143 fwupd_device_to_json (dev, builder);
144 json_builder_end_object (builder);
145 }
146 json_builder_end_array (builder);
147 json_builder_end_object (builder);
148
149 /* export as a string */
150 json_root = json_builder_get_root (builder);
151 json_generator = json_generator_new ();
152 json_generator_set_pretty (json_generator, TRUE);
153 json_generator_set_root (json_generator, json_root);
154 state = json_generator_to_data (json_generator, NULL);
155 if (state == NULL)
156 return FALSE;
157 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
158 filename = g_build_filename (dirname, "state.json", NULL);
159 return g_file_set_contents (filename, state, -1, error);
160}
161
162static gboolean
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000163fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error)
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600164{
165 g_autoptr(GError) error_local = NULL;
166
167 if (!fu_util_stop_daemon (&error_local))
168 g_debug ("Failed top stop daemon: %s", error_local->message);
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000169 if (!fu_engine_load (priv->engine, flags, error))
Richard Hughesf425d292019-01-18 17:57:39 +0000170 return FALSE;
171 if (fu_engine_get_tainted (priv->engine)) {
172 g_printerr ("WARNING: This tool has loaded 3rd party code and "
173 "is no longer supported by the upstream developers!\n");
174 }
175 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500176}
177
Richard Hughesb5976832018-05-18 10:02:09 +0100178static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500179fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
180{
181 g_autofree gchar *path = g_path_get_dirname (value);
182 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
183 g_prefix_error (error,
184 "Unable to access %s. You may need to copy %s to %s: ",
185 path, value, g_getenv ("HOME"));
186 }
187}
188
189static void
Richard Hughesb5976832018-05-18 10:02:09 +0100190fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
191{
192 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
193 /* TRANSLATORS: this is when a device ctrl+c's a watch */
194 g_print ("%s\n", _("Cancelled"));
195 g_main_loop_quit (priv->loop);
196}
197
198static gboolean
199fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
200{
201 g_autofree gchar *tmp = NULL;
202 g_autoptr(FuSmbios) smbios = NULL;
203 if (g_strv_length (values) < 1) {
204 g_set_error_literal (error,
205 FWUPD_ERROR,
206 FWUPD_ERROR_INVALID_ARGS,
207 "Invalid arguments");
208 return FALSE;
209 }
210 smbios = fu_smbios_new ();
211 if (!fu_smbios_setup_from_file (smbios, values[0], error))
212 return FALSE;
213 tmp = fu_smbios_to_string (smbios);
214 g_print ("%s\n", tmp);
215 return TRUE;
216}
217
Richard Hughesb5976832018-05-18 10:02:09 +0100218static gboolean
219fu_util_sigint_cb (gpointer user_data)
220{
221 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
222 g_debug ("Handling SIGINT");
223 g_cancellable_cancel (priv->cancellable);
224 return FALSE;
225}
226
227static void
228fu_util_private_free (FuUtilPrivate *priv)
229{
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500230 if (priv->current_device != NULL)
231 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100232 if (priv->engine != NULL)
233 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100234 if (priv->loop != NULL)
235 g_main_loop_unref (priv->loop);
236 if (priv->cancellable != NULL)
237 g_object_unref (priv->cancellable);
238 if (priv->progressbar != NULL)
239 g_object_unref (priv->progressbar);
240 if (priv->context != NULL)
241 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600242 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100243 g_free (priv);
244}
245
246#pragma clang diagnostic push
247#pragma clang diagnostic ignored "-Wunused-function"
248G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
249#pragma clang diagnostic pop
250
Richard Hughes98ca9932018-05-18 10:24:07 +0100251
252static void
253fu_main_engine_device_added_cb (FuEngine *engine,
254 FuDevice *device,
255 FuUtilPrivate *priv)
256{
257 g_autofree gchar *tmp = fu_device_to_string (device);
258 g_debug ("ADDED:\n%s", tmp);
259}
260
261static void
262fu_main_engine_device_removed_cb (FuEngine *engine,
263 FuDevice *device,
264 FuUtilPrivate *priv)
265{
266 g_autofree gchar *tmp = fu_device_to_string (device);
267 g_debug ("REMOVED:\n%s", tmp);
268}
269
270static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100271fu_main_engine_status_changed_cb (FuEngine *engine,
272 FwupdStatus status,
273 FuUtilPrivate *priv)
274{
275 fu_progressbar_update (priv->progressbar, status, 0);
276}
277
278static void
279fu_main_engine_percentage_changed_cb (FuEngine *engine,
280 guint percentage,
281 FuUtilPrivate *priv)
282{
283 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
284}
285
286static gboolean
287fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
288{
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000289 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100290 return FALSE;
291 g_main_loop_run (priv->loop);
292 return TRUE;
293}
294
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100295static gint
296fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
297{
298 return fu_plugin_name_compare (*item1, *item2);
299}
300
301static gboolean
302fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
303{
304 GPtrArray *plugins;
305 guint cnt = 0;
306
307 /* load engine */
308 if (!fu_engine_load_plugins (priv->engine, error))
309 return FALSE;
310
311 /* print */
312 plugins = fu_engine_get_plugins (priv->engine);
313 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
314 for (guint i = 0; i < plugins->len; i++) {
315 FuPlugin *plugin = g_ptr_array_index (plugins, i);
316 if (!fu_plugin_get_enabled (plugin))
317 continue;
318 g_print ("%s\n", fu_plugin_get_name (plugin));
319 cnt++;
320 }
321 if (cnt == 0) {
322 /* TRANSLATORS: nothing found */
323 g_print ("%s\n", _("No plugins found"));
324 return TRUE;
325 }
326
327 return TRUE;
328}
329
Richard Hughes98ca9932018-05-18 10:24:07 +0100330static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600331fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
332{
333 g_autoptr(GPtrArray) devices = NULL;
334
335 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000336 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600337 return FALSE;
338
339 /* get devices from daemon */
340 devices = fu_engine_get_devices (priv->engine, error);
341 if (devices == NULL)
342 return FALSE;
343 for (guint i = 0; i < devices->len; i++) {
344 FwupdDevice *dev = g_ptr_array_index (devices, i);
345 g_autoptr(GPtrArray) rels = NULL;
346 g_autoptr(GError) error_local = NULL;
347
348 /* not going to have results, so save a D-Bus round-trip */
349 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
350 continue;
351
352 /* get the releases for this device and filter for validity */
353 rels = fu_engine_get_upgrades (priv->engine,
354 fwupd_device_get_id (dev),
355 &error_local);
356 if (rels == NULL) {
357 g_printerr ("%s\n", error_local->message);
358 continue;
359 }
360 g_print ("%s", fwupd_device_to_string (dev));
361 g_print (" Release information:\n");
362 /* print all releases */
363 for (guint j = 0; j < rels->len; j++) {
364 FwupdRelease *rel = g_ptr_array_index (rels, j);
365 g_print ("%s\n", fwupd_release_to_string (rel));
366 }
367 }
368
Mario Limonciello3143bad2019-02-27 07:31:00 -0600369 /* save the device state for other applications to see */
370 if (!fu_util_save_current_state (priv, error))
371 return FALSE;
372
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600373 /* success */
374 return TRUE;
375}
376
377static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500378fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
379{
380 g_autoptr(GPtrArray) array = NULL;
381 gint fd;
382
383 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000384 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500385 return FALSE;
386
387 /* check args */
388 if (g_strv_length (values) != 1) {
389 g_set_error_literal (error,
390 FWUPD_ERROR,
391 FWUPD_ERROR_INVALID_ARGS,
392 "Invalid arguments");
393 return FALSE;
394 }
395
396 /* open file */
397 fd = open (values[0], O_RDONLY);
398 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500399 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500400 g_set_error (error,
401 FWUPD_ERROR,
402 FWUPD_ERROR_INVALID_FILE,
403 "failed to open %s",
404 values[0]);
405 return FALSE;
406 }
407 array = fu_engine_get_details (priv->engine, fd, error);
408 close (fd);
409
410 if (array == NULL)
411 return FALSE;
412 for (guint i = 0; i < array->len; i++) {
413 FwupdDevice *dev = g_ptr_array_index (array, i);
414 g_autofree gchar *tmp = NULL;
415 tmp = fwupd_device_to_string (dev);
Mario Limoncielloece90a42018-07-25 17:35:55 -0500416 g_print ("%s\n", tmp);
Mario Limonciello716ab272018-05-29 12:34:37 -0500417 }
418 return TRUE;
419}
420
421static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100422fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
423{
424 g_autoptr(GPtrArray) devs = NULL;
425
426 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000427 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100428 return FALSE;
429
430 /* print */
431 devs = fu_engine_get_devices (priv->engine, error);
432 if (devs == NULL)
433 return FALSE;
434 if (devs->len == 0) {
435 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500436 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100437 return TRUE;
438 }
439 for (guint i = 0; i < devs->len; i++) {
440 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500441 if (priv->show_all_devices || fu_util_is_interesting_device (dev)) {
442 g_autofree gchar *tmp = fwupd_device_to_string (dev);
443 g_print ("%s\n", tmp);
444 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100445 }
446
Mario Limonciello3143bad2019-02-27 07:31:00 -0600447 /* save the device state for other applications to see */
448 if (!fu_util_save_current_state (priv, error))
449 return FALSE;
450
Richard Hughes98ca9932018-05-18 10:24:07 +0100451 return TRUE;
452}
453
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500454static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100455fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500456{
457 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100458 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500459 if (!priv->show_all_devices &&
460 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500461 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100462 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500463 GNode *child = g_node_append_data (root, dev_tmp);
464 fu_util_build_device_tree (priv, child, devs, dev_tmp);
465 }
466 }
467}
468
469static gboolean
470fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
471{
472 g_autoptr(GNode) root = g_node_new (NULL);
473 g_autoptr(GPtrArray) devs = NULL;
474
475 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000476 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500477 return FALSE;
478
479 /* print */
480 devs = fu_engine_get_devices (priv->engine, error);
481 if (devs == NULL)
482 return FALSE;
483
484 /* print */
485 if (devs->len == 0) {
486 /* TRANSLATORS: nothing attached that can be upgraded */
487 g_print ("%s\n", _("No hardware detected with firmware update capability"));
488 return TRUE;
489 }
490 fu_util_build_device_tree (priv, root, devs, NULL);
491 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
492 fu_util_print_device_tree, priv);
493
494 return TRUE;
495}
496
497
Richard Hughes98ca9932018-05-18 10:24:07 +0100498static FuDevice *
499fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
500{
501 FuDevice *dev;
502 guint idx;
503 g_autoptr(GPtrArray) devices = NULL;
504
505 /* get devices from daemon */
506 devices = fu_engine_get_devices (priv->engine, error);
507 if (devices == NULL)
508 return NULL;
509
510 /* exactly one */
511 if (devices->len == 1) {
512 dev = g_ptr_array_index (devices, 0);
513 return g_object_ref (dev);
514 }
515
516 /* TRANSLATORS: get interactive prompt */
517 g_print ("%s\n", _("Choose a device:"));
518 /* TRANSLATORS: this is to abort the interactive prompt */
519 g_print ("0.\t%s\n", _("Cancel"));
520 for (guint i = 0; i < devices->len; i++) {
521 dev = g_ptr_array_index (devices, i);
522 g_print ("%u.\t%s (%s)\n",
523 i + 1,
524 fu_device_get_id (dev),
525 fu_device_get_name (dev));
526 }
527 idx = fu_util_prompt_for_number (devices->len);
528 if (idx == 0) {
529 g_set_error_literal (error,
530 FWUPD_ERROR,
531 FWUPD_ERROR_NOTHING_TO_DO,
532 "Request canceled");
533 return NULL;
534 }
535 dev = g_ptr_array_index (devices, idx - 1);
536 return g_object_ref (dev);
537}
538
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500539static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600540fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500541 FwupdDevice *device,
542 FuUtilPrivate *priv)
543{
544 g_autofree gchar *str = NULL;
545
546 /* same as last time, so ignore */
547 if (priv->current_device != NULL &&
548 fwupd_device_compare (priv->current_device, device) == 0)
549 return;
550
551 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600552 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
553 /* TRANSLATORS: %1 is a device name */
554 str = g_strdup_printf (_("Updating %s…"),
555 fwupd_device_get_name (device));
556 fu_progressbar_set_title (priv->progressbar, str);
557 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
558 /* TRANSLATORS: %1 is a device name */
559 str = g_strdup_printf (_("Installing on %s…"),
560 fwupd_device_get_name (device));
561 fu_progressbar_set_title (priv->progressbar, str);
562 } else {
563 g_warning ("no FuUtilOperation set");
564 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500565 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600566
567 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
568 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
569 else if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
570 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
Mario Limonciello32241f42019-01-24 10:12:41 -0600571
572 if (priv->current_message == NULL) {
573 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
574 if (tmp != NULL)
575 priv->current_message = g_strdup (tmp);
576 }
577}
578
579static void
580fu_util_display_current_message (FuUtilPrivate *priv)
581{
582 if (priv->current_message == NULL)
583 return;
584 g_print ("%s\n", priv->current_message);
585 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500586}
587
Richard Hughes98ca9932018-05-18 10:24:07 +0100588static gboolean
589fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
590{
591 g_autoptr(FuDevice) device = NULL;
592 g_autoptr(GBytes) blob_fw = NULL;
593
594 /* invalid args */
595 if (g_strv_length (values) == 0) {
596 g_set_error_literal (error,
597 FWUPD_ERROR,
598 FWUPD_ERROR_INVALID_ARGS,
599 "Invalid arguments");
600 return FALSE;
601 }
602
603 /* parse blob */
604 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500605 if (blob_fw == NULL) {
606 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100607 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500608 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100609
610 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000611 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100612 return FALSE;
613
614 /* get device */
615 if (g_strv_length (values) >= 2) {
616 device = fu_engine_get_device (priv->engine, values[1], error);
617 if (device == NULL)
618 return FALSE;
619 } else {
620 device = fu_util_prompt_for_device (priv, error);
621 if (device == NULL)
622 return FALSE;
623 }
624
Mario Limonciello3f243a92019-01-21 22:05:23 -0600625 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500626 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600627 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500628
Richard Hughes98ca9932018-05-18 10:24:07 +0100629 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000630 if (priv->prepare_blob) {
631 g_autoptr(GPtrArray) devices = NULL;
632 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
633 g_ptr_array_add (devices, g_object_ref (device));
634 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
635 g_prefix_error (error, "failed to prepare composite action: ");
636 return FALSE;
637 }
638 }
Richard Hughes84af6e72019-02-01 18:19:41 +0000639 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600640 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000641 if (priv->cleanup_blob) {
642 g_autoptr(FuDevice) device_new = NULL;
643 g_autoptr(GError) error_local = NULL;
644
645 /* get the possibly new device from the old ID */
646 device_new = fu_engine_get_device (priv->engine,
647 fu_device_get_id (device),
648 &error_local);
649 if (device_new == NULL) {
650 g_debug ("failed to find new device: %s",
651 error_local->message);
652 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600653 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000654 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
655 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
656 g_prefix_error (error, "failed to cleanup composite action: ");
657 return FALSE;
658 }
659 }
660 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600661
Mario Limonciello32241f42019-01-24 10:12:41 -0600662 fu_util_display_current_message (priv);
663
Mario Limonciello3f243a92019-01-21 22:05:23 -0600664 /* success */
665 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100666}
667
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100668static gint
669fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
670{
671 FuInstallTask *task1 = *((FuInstallTask **) a);
672 FuInstallTask *task2 = *((FuInstallTask **) b);
673 return fu_install_task_compare (task1, task2);
674}
675
676static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100677fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
678{
679 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
680 { "curl", uri, "--output", fn, NULL },
681 { NULL } };
682 for (guint i = 0; argv[i][0] != NULL; i++) {
683 g_autoptr(GError) error_local = NULL;
684 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
685 g_debug ("%s", error_local->message);
686 continue;
687 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000688 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100689 }
690 g_set_error_literal (error,
691 FWUPD_ERROR,
692 FWUPD_ERROR_NOT_FOUND,
693 "no supported out-of-process downloaders found");
694 return FALSE;
695}
696
697static gchar *
698fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
699{
700 g_autofree gchar *filename = NULL;
701 g_autoptr(SoupURI) uri = NULL;
702
703 /* a local file */
704 uri = soup_uri_new (perhapsfn);
705 if (uri == NULL)
706 return g_strdup (perhapsfn);
707
708 /* download the firmware to a cachedir */
709 filename = fu_util_get_user_cache_path (perhapsfn);
710 if (!fu_common_mkdir_parent (filename, error))
711 return NULL;
712 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
713 return NULL;
714 return g_steal_pointer (&filename);
715}
716
717static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100718fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
719{
Richard Hughes3d178be2018-08-30 11:14:24 +0100720 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100721 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100722 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100723 g_autoptr(GPtrArray) devices_possible = NULL;
724 g_autoptr(GPtrArray) errors = NULL;
725 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100726 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100727
Mario Limonciello8949e892018-05-25 08:03:06 -0500728 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000729 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500730 return FALSE;
731
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100732 /* handle both forms */
733 if (g_strv_length (values) == 1) {
734 devices_possible = fu_engine_get_devices (priv->engine, error);
735 if (devices_possible == NULL)
736 return FALSE;
737 } else if (g_strv_length (values) == 2) {
738 FuDevice *device = fu_engine_get_device (priv->engine,
739 values[1],
740 error);
741 if (device == NULL)
742 return FALSE;
743 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
744 g_ptr_array_add (devices_possible, device);
745 } else {
746 g_set_error_literal (error,
747 FWUPD_ERROR,
748 FWUPD_ERROR_INVALID_ARGS,
749 "Invalid arguments");
750 return FALSE;
751 }
752
Richard Hughes3d178be2018-08-30 11:14:24 +0100753 /* download if required */
754 filename = fu_util_download_if_required (priv, values[0], error);
755 if (filename == NULL)
756 return FALSE;
757
Richard Hughes481aa2a2018-09-18 20:51:46 +0100758 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100759 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500760 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100761 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100762 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500763 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100764 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
765 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100766 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600767 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100768 if (components == NULL)
769 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100770
Richard Hughes481aa2a2018-09-18 20:51:46 +0100771 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100772 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
773 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100774 for (guint i = 0; i < components->len; i++) {
775 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100776
777 /* do any devices pass the requirements */
778 for (guint j = 0; j < devices_possible->len; j++) {
779 FuDevice *device = g_ptr_array_index (devices_possible, j);
780 g_autoptr(FuInstallTask) task = NULL;
781 g_autoptr(GError) error_local = NULL;
782
783 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100784 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100785 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100786 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100787 &error_local)) {
788 g_debug ("requirement on %s:%s failed: %s",
789 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100790 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100791 error_local->message);
792 g_ptr_array_add (errors, g_steal_pointer (&error_local));
793 continue;
794 }
795
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600796 /* if component should have an update message from CAB */
797 fu_device_incorporate_from_component (device, component);
798
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100799 /* success */
800 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
801 }
802 }
803
804 /* order the install tasks by the device priority */
805 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
806
807 /* nothing suitable */
808 if (install_tasks->len == 0) {
809 GError *error_tmp = fu_common_error_array_get_best (errors);
810 g_propagate_error (error, error_tmp);
811 return FALSE;
812 }
813
Mario Limonciello3f243a92019-01-21 22:05:23 -0600814 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500815 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600816 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500817
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100818 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100819 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
820 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100821
Mario Limonciello32241f42019-01-24 10:12:41 -0600822 fu_util_display_current_message (priv);
823
Mario Limonciello3f243a92019-01-21 22:05:23 -0600824 /* we don't want to ask anything */
825 if (priv->no_reboot_check) {
826 g_debug ("skipping reboot check");
827 return TRUE;
828 }
829
Mario Limonciello3143bad2019-02-27 07:31:00 -0600830 /* save the device state for other applications to see */
831 if (!fu_util_save_current_state (priv, error))
832 return FALSE;
833
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100834 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600835 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100836}
837
Richard Hughes98ca9932018-05-18 10:24:07 +0100838static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600839fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
840{
841 g_autoptr(GPtrArray) devices = NULL;
842
843 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000844 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello46aaee82019-01-10 12:58:00 -0600845 return FALSE;
846
Mario Limonciello3f243a92019-01-21 22:05:23 -0600847 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
848 g_signal_connect (priv->engine, "device-changed",
849 G_CALLBACK (fu_util_update_device_changed_cb), priv);
850
Mario Limonciello46aaee82019-01-10 12:58:00 -0600851 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000852 if (devices == NULL)
853 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600854 for (guint i = 0; i < devices->len; i++) {
855 FwupdDevice *dev = g_ptr_array_index (devices, i);
856 FwupdRelease *rel;
857 const gchar *remote_id;
858 const gchar *device_id;
859 const gchar *uri_tmp;
860 g_autoptr(GPtrArray) rels = NULL;
861 g_autoptr(GError) error_local = NULL;
862
863 if (!fu_util_is_interesting_device (dev))
864 continue;
865 /* only show stuff that has metadata available */
866 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
867 continue;
868
869 device_id = fu_device_get_id (dev);
870 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
871 if (rels == NULL) {
872 g_printerr ("%s\n", error_local->message);
873 continue;
874 }
875
876 rel = g_ptr_array_index (rels, 0);
877 uri_tmp = fwupd_release_get_uri (rel);
878 remote_id = fwupd_release_get_remote_id (rel);
879 if (remote_id != NULL) {
880 FwupdRemote *remote;
881 g_auto(GStrv) argv = NULL;
882
883 remote = fu_engine_get_remote_by_id (priv->engine,
884 remote_id,
885 &error_local);
886 if (remote == NULL) {
887 g_printerr ("%s\n", error_local->message);
888 continue;
889 }
890
891 argv = g_new0 (gchar *, 2);
892 /* local remotes have the firmware already */
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600893 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
Mario Limonciello46aaee82019-01-10 12:58:00 -0600894 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
895 g_autofree gchar *path = g_path_get_dirname (fn_cache);
896 argv[0] = g_build_filename (path, uri_tmp, NULL);
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600897 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
898 argv[0] = g_strdup (uri_tmp + 7);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600899 /* web remote, fu_util_install will download file */
900 } else {
901 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
902 }
903 if (!fu_util_install (priv, argv, &error_local)) {
904 g_printerr ("%s\n", error_local->message);
905 continue;
906 }
Mario Limonciello32241f42019-01-24 10:12:41 -0600907 fu_util_display_current_message (priv);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600908 }
909 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600910
911 /* we don't want to ask anything */
912 if (priv->no_reboot_check) {
913 g_debug ("skipping reboot check");
914 return TRUE;
915 }
916
Mario Limonciello3143bad2019-02-27 07:31:00 -0600917 /* save the device state for other applications to see */
918 if (!fu_util_save_current_state (priv, error))
919 return FALSE;
920
Mario Limonciello3f243a92019-01-21 22:05:23 -0600921 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600922}
923
924static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100925fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
926{
927 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100928 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100929
930 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000931 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100932 return FALSE;
933
934 /* invalid args */
935 if (g_strv_length (values) == 0) {
936 g_set_error_literal (error,
937 FWUPD_ERROR,
938 FWUPD_ERROR_INVALID_ARGS,
939 "Invalid arguments");
940 return FALSE;
941 }
942
943 /* get device */
944 if (g_strv_length (values) >= 1) {
945 device = fu_engine_get_device (priv->engine, values[0], error);
946 if (device == NULL)
947 return FALSE;
948 } else {
949 device = fu_util_prompt_for_device (priv, error);
950 if (device == NULL)
951 return FALSE;
952 }
953
954 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100955 locker = fu_device_locker_new (device, error);
956 if (locker == NULL)
957 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100958 return fu_device_detach (device, error);
959}
960
961static gboolean
962fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
963{
964 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100965 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100966
967 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000968 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100969 return FALSE;
970
971 /* invalid args */
972 if (g_strv_length (values) == 0) {
973 g_set_error_literal (error,
974 FWUPD_ERROR,
975 FWUPD_ERROR_INVALID_ARGS,
976 "Invalid arguments");
977 return FALSE;
978 }
979
980 /* get device */
981 if (g_strv_length (values) >= 1) {
982 device = fu_engine_get_device (priv->engine, values[0], error);
983 if (device == NULL)
984 return FALSE;
985 } else {
986 device = fu_util_prompt_for_device (priv, error);
987 if (device == NULL)
988 return FALSE;
989 }
990
991 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100992 locker = fu_device_locker_new (device, error);
993 if (locker == NULL)
994 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100995 return fu_device_attach (device, error);
996}
997
Richard Hughes1d894f12018-08-31 13:05:51 +0100998static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -0600999fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1000{
1001 gboolean has_pending = FALSE;
1002 g_autoptr(FuHistory) history = fu_history_new ();
1003 g_autoptr(GPtrArray) devices = NULL;
1004
1005 /* check the history database before starting the daemon */
1006 if (g_strv_length (values) == 0) {
1007 devices = fu_history_get_devices (history, error);
1008 if (devices == NULL)
1009 return FALSE;
1010 } else if (g_strv_length (values) == 1) {
1011 FuDevice *device;
1012 device = fu_history_get_device_by_id (history, values[0], error);
1013 if (device == NULL)
1014 return FALSE;
1015 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1016 g_ptr_array_add (devices, device);
1017 } else {
1018 g_set_error_literal (error,
1019 FWUPD_ERROR,
1020 FWUPD_ERROR_INVALID_ARGS,
1021 "Invalid arguments");
1022 return FALSE;
1023 }
1024
1025 /* nothing to do */
1026 for (guint i = 0; i < devices->len; i++) {
1027 FuDevice *dev = g_ptr_array_index (devices, i);
1028 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1029 fu_engine_add_plugin_filter (priv->engine,
1030 fu_device_get_plugin (dev));
1031 has_pending = TRUE;
1032 }
1033 }
1034 if (!has_pending) {
1035 g_set_error_literal (error,
1036 FWUPD_ERROR,
1037 FWUPD_ERROR_NOTHING_TO_DO,
1038 "No firmware to activate");
1039 return FALSE;
1040
1041 }
1042
1043 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001044 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001045 return FALSE;
1046
1047 /* activate anything with _NEEDS_ACTIVATION */
1048 for (guint i = 0; i < devices->len; i++) {
1049 FuDevice *device = g_ptr_array_index (devices, i);
1050 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1051 continue;
1052 /* TRANSLATORS: shown when shutting down to switch to the new version */
1053 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1054 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1055 return FALSE;
1056 }
1057
1058 return TRUE;
1059}
1060
1061static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001062fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1063{
1064 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1065 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1066 const gchar *hwid_keys[] = {
1067 FU_HWIDS_KEY_BIOS_VENDOR,
1068 FU_HWIDS_KEY_BIOS_VERSION,
1069 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1070 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1071 FU_HWIDS_KEY_MANUFACTURER,
1072 FU_HWIDS_KEY_FAMILY,
1073 FU_HWIDS_KEY_PRODUCT_NAME,
1074 FU_HWIDS_KEY_PRODUCT_SKU,
1075 FU_HWIDS_KEY_ENCLOSURE_KIND,
1076 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1077 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1078 NULL };
1079
1080 /* read DMI data */
1081 if (g_strv_length (values) == 0) {
1082 if (!fu_smbios_setup (smbios, error))
1083 return FALSE;
1084 } else if (g_strv_length (values) == 1) {
1085 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1086 return FALSE;
1087 } else {
1088 g_set_error_literal (error,
1089 FWUPD_ERROR,
1090 FWUPD_ERROR_INVALID_ARGS,
1091 "Invalid arguments");
1092 return FALSE;
1093 }
1094 if (!fu_hwids_setup (hwids, smbios, error))
1095 return FALSE;
1096
1097 /* show debug output */
1098 g_print ("Computer Information\n");
1099 g_print ("--------------------\n");
1100 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1101 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1102 if (tmp == NULL)
1103 continue;
1104 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1105 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1106 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1107 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1108 } else {
1109 g_print ("%s: %s\n", hwid_keys[i], tmp);
1110 }
1111 }
1112
1113 /* show GUIDs */
1114 g_print ("\nHardware IDs\n");
1115 g_print ("------------\n");
1116 for (guint i = 0; i < 15; i++) {
1117 const gchar *keys = NULL;
1118 g_autofree gchar *guid = NULL;
1119 g_autofree gchar *key = NULL;
1120 g_autofree gchar *keys_str = NULL;
1121 g_auto(GStrv) keysv = NULL;
1122 g_autoptr(GError) error_local = NULL;
1123
1124 /* get the GUID */
1125 key = g_strdup_printf ("HardwareID-%u", i);
1126 keys = fu_hwids_get_replace_keys (hwids, key);
1127 guid = fu_hwids_get_guid (hwids, key, &error_local);
1128 if (guid == NULL) {
1129 g_print ("%s\n", error_local->message);
1130 continue;
1131 }
1132
1133 /* show what makes up the GUID */
1134 keysv = g_strsplit (keys, "&", -1);
1135 keys_str = g_strjoinv (" + ", keysv);
1136 g_print ("{%s} <- %s\n", guid, keys_str);
1137 }
1138
1139 return TRUE;
1140}
1141
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001142static gboolean
1143fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1144{
1145 const gchar *script_fn = "startup.sh";
1146 const gchar *output_fn = "firmware.bin";
1147 g_autoptr(GBytes) archive_blob = NULL;
1148 g_autoptr(GBytes) firmware_blob = NULL;
1149 if (g_strv_length (values) < 2) {
1150 g_set_error_literal (error,
1151 FWUPD_ERROR,
1152 FWUPD_ERROR_INVALID_ARGS,
1153 "Invalid arguments");
1154 return FALSE;
1155 }
1156 archive_blob = fu_common_get_contents_bytes (values[0], error);
1157 if (archive_blob == NULL)
1158 return FALSE;
1159 if (g_strv_length (values) > 2)
1160 script_fn = values[2];
1161 if (g_strv_length (values) > 3)
1162 output_fn = values[3];
1163 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1164 if (firmware_blob == NULL)
1165 return FALSE;
1166 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1167}
1168
Mario Limonciello62f84862018-10-18 13:15:23 -05001169static void
1170fu_util_device_added_cb (FwupdClient *client,
1171 FwupdDevice *device,
1172 gpointer user_data)
1173{
1174 g_autofree gchar *tmp = fwupd_device_to_string (device);
1175 /* TRANSLATORS: this is when a device is hotplugged */
1176 g_print ("%s\n%s", _("Device added:"), tmp);
1177}
1178
1179static void
1180fu_util_device_removed_cb (FwupdClient *client,
1181 FwupdDevice *device,
1182 gpointer user_data)
1183{
1184 g_autofree gchar *tmp = fwupd_device_to_string (device);
1185 /* TRANSLATORS: this is when a device is hotplugged */
1186 g_print ("%s\n%s", _("Device removed:"), tmp);
1187}
1188
1189static void
1190fu_util_device_changed_cb (FwupdClient *client,
1191 FwupdDevice *device,
1192 gpointer user_data)
1193{
1194 g_autofree gchar *tmp = fwupd_device_to_string (device);
1195 /* TRANSLATORS: this is when a device has been updated */
1196 g_print ("%s\n%s", _("Device changed:"), tmp);
1197}
1198
1199static void
1200fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1201{
1202 /* TRANSLATORS: this is when the daemon state changes */
1203 g_print ("%s\n", _("Changed"));
1204}
1205
1206static gboolean
1207fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1208{
1209 g_autoptr(FwupdClient) client = fwupd_client_new ();
1210
1211 /* get all the devices */
1212 if (!fwupd_client_connect (client, priv->cancellable, error))
1213 return FALSE;
1214
1215 /* watch for any hotplugged device */
1216 g_signal_connect (client, "changed",
1217 G_CALLBACK (fu_util_changed_cb), priv);
1218 g_signal_connect (client, "device-added",
1219 G_CALLBACK (fu_util_device_added_cb), priv);
1220 g_signal_connect (client, "device-removed",
1221 G_CALLBACK (fu_util_device_removed_cb), priv);
1222 g_signal_connect (client, "device-changed",
1223 G_CALLBACK (fu_util_device_changed_cb), priv);
1224 g_signal_connect (priv->cancellable, "cancelled",
1225 G_CALLBACK (fu_util_cancelled_cb), priv);
1226 g_main_loop_run (priv->loop);
1227 return TRUE;
1228}
1229
Richard Hughesb5976832018-05-18 10:02:09 +01001230int
1231main (int argc, char *argv[])
1232{
Richard Hughes460226a2018-05-21 20:56:21 +01001233 gboolean allow_older = FALSE;
1234 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001235 gboolean force = FALSE;
1236 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001237 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001238 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001239 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001240 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1241 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00001242 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01001243 g_autofree gchar *cmd_descriptions = NULL;
1244 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001245 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1246 /* TRANSLATORS: command line option */
1247 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001248 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1249 /* TRANSLATORS: command line option */
1250 _("Allow re-installing existing firmware versions"), NULL },
1251 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1252 /* TRANSLATORS: command line option */
1253 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001254 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1255 /* TRANSLATORS: command line option */
1256 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001257 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1258 /* TRANSLATORS: command line option */
1259 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001260 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1261 /* TRANSLATORS: command line option */
1262 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001263 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1264 /* TRANSLATORS: command line option */
1265 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001266 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001267 /* TRANSLATORS: command line option */
1268 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001269 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001270 /* TRANSLATORS: command line option */
1271 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001272 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1273 /* TRANSLATORS: command line option */
1274 _("Save device state into a JSON file between executions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001275 { NULL}
1276 };
1277
1278 setlocale (LC_ALL, "");
1279
1280 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1281 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1282 textdomain (GETTEXT_PACKAGE);
1283
1284 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001285 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001286 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001287 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001288
1289 /* create helper object */
1290 priv->loop = g_main_loop_new (NULL, FALSE);
1291 priv->progressbar = fu_progressbar_new ();
1292
1293 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00001294 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001295 "build-firmware",
1296 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1297 /* TRANSLATORS: command description */
1298 _("Build firmware using a sandbox"),
1299 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00001300 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001301 "smbios-dump",
1302 "FILE",
1303 /* TRANSLATORS: command description */
1304 _("Dump SMBIOS data from a file"),
1305 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00001306 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001307 "get-plugins",
1308 NULL,
1309 /* TRANSLATORS: command description */
1310 _("Get all enabled plugins registered with the system"),
1311 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00001312 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001313 "get-details",
1314 NULL,
1315 /* TRANSLATORS: command description */
1316 _("Gets details about a firmware file"),
1317 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00001318 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001319 "get-updates",
1320 NULL,
1321 /* TRANSLATORS: command description */
1322 _("Gets the list of updates for connected hardware"),
1323 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00001324 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001325 "get-devices",
1326 NULL,
1327 /* TRANSLATORS: command description */
1328 _("Get all devices that support firmware updates"),
1329 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00001330 fu_util_cmd_array_add (cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001331 "get-topology",
1332 NULL,
1333 /* TRANSLATORS: command description */
1334 _("Get all devices according to the system topology"),
1335 fu_util_get_topology);
Richard Hughesc77e1112019-03-01 10:16:26 +00001336 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001337 "watch",
1338 NULL,
1339 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001340 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001341 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00001342 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001343 "install-blob",
1344 "FILENAME DEVICE-ID",
1345 /* TRANSLATORS: command description */
1346 _("Install a firmware blob on a device"),
1347 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00001348 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001349 "install",
1350 "FILE [ID]",
1351 /* TRANSLATORS: command description */
1352 _("Install a firmware file on this hardware"),
1353 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00001354 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001355 "attach",
1356 "DEVICE-ID",
1357 /* TRANSLATORS: command description */
1358 _("Attach to firmware mode"),
1359 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001360 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001361 "detach",
1362 "DEVICE-ID",
1363 /* TRANSLATORS: command description */
1364 _("Detach to bootloader mode"),
1365 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001366 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001367 "activate",
1368 "[DEVICE-ID]",
1369 /* TRANSLATORS: command description */
1370 _("Activate pending devices"),
1371 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00001372 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001373 "hwids",
1374 "[FILE]",
1375 /* TRANSLATORS: command description */
1376 _("Return all the hardware IDs for the machine"),
1377 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00001378 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05001379 "monitor",
1380 NULL,
1381 /* TRANSLATORS: command description */
1382 _("Monitor the daemon for events"),
1383 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00001384 fu_util_cmd_array_add (cmd_array,
Mario Limonciello46aaee82019-01-10 12:58:00 -06001385 "update",
1386 NULL,
1387 /* TRANSLATORS: command description */
1388 _("Update all devices that match local metadata"),
1389 fu_util_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001390
1391 /* do stuff on ctrl+c */
1392 priv->cancellable = g_cancellable_new ();
1393 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1394 SIGINT, fu_util_sigint_cb,
1395 priv, NULL);
1396 g_signal_connect (priv->cancellable, "cancelled",
1397 G_CALLBACK (fu_util_cancelled_cb), priv);
1398
1399 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00001400 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001401
Mario Limonciello3f243a92019-01-21 22:05:23 -06001402 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001403 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001404 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001405 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1406 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001407
Richard Hughesb5976832018-05-18 10:02:09 +01001408 /* get a list of the commands */
1409 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00001410 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001411 g_option_context_set_summary (priv->context, cmd_descriptions);
1412 g_option_context_set_description (priv->context,
1413 "This tool allows an administrator to use the fwupd plugins "
1414 "without being installed on the host system.");
1415
1416 /* TRANSLATORS: program name */
1417 g_set_application_name (_("Firmware Utility"));
1418 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001419 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001420 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1421 if (!ret) {
1422 /* TRANSLATORS: the user didn't read the man page */
1423 g_print ("%s: %s\n", _("Failed to parse arguments"),
1424 error->message);
1425 return EXIT_FAILURE;
1426 }
1427
Richard Hughes460226a2018-05-21 20:56:21 +01001428 /* set flags */
1429 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1430 if (allow_reinstall)
1431 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1432 if (allow_older)
1433 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1434 if (force)
1435 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1436
Richard Hughes98ca9932018-05-18 10:24:07 +01001437 /* load engine */
1438 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1439 g_signal_connect (priv->engine, "device-added",
1440 G_CALLBACK (fu_main_engine_device_added_cb),
1441 priv);
1442 g_signal_connect (priv->engine, "device-removed",
1443 G_CALLBACK (fu_main_engine_device_removed_cb),
1444 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001445 g_signal_connect (priv->engine, "status-changed",
1446 G_CALLBACK (fu_main_engine_status_changed_cb),
1447 priv);
1448 g_signal_connect (priv->engine, "percentage-changed",
1449 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1450 priv);
1451
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001452 /* just show versions and exit */
1453 if (version) {
1454 g_autofree gchar *version_str = fu_util_get_versions ();
1455 g_print ("%s\n", version_str);
1456 return EXIT_SUCCESS;
1457 }
1458
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001459 /* any plugin whitelist specified */
1460 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1461 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1462
Richard Hughesb5976832018-05-18 10:02:09 +01001463 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00001464 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01001465 if (!ret) {
1466 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1467 g_autofree gchar *tmp = NULL;
1468 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1469 g_print ("%s\n\n%s", error->message, tmp);
1470 return EXIT_FAILURE;
1471 }
1472 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1473 g_print ("%s\n", error->message);
1474 return EXIT_NOTHING_TO_DO;
1475 }
1476 g_print ("%s\n", error->message);
1477 return EXIT_FAILURE;
1478 }
1479
1480 /* success */
1481 return EXIT_SUCCESS;
1482}