blob: 7a7e6429662754eb8975b4b801b26fbc42d594d6 [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
Richard Hughes809abea2019-03-23 11:06:18 +0000546 /* allowed to set whenever the device has changed */
547 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
548 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
549 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
550 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
551
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500552 /* same as last time, so ignore */
553 if (priv->current_device != NULL &&
554 fwupd_device_compare (priv->current_device, device) == 0)
555 return;
556
557 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600558 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
559 /* TRANSLATORS: %1 is a device name */
560 str = g_strdup_printf (_("Updating %s…"),
561 fwupd_device_get_name (device));
562 fu_progressbar_set_title (priv->progressbar, str);
563 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
564 /* TRANSLATORS: %1 is a device name */
565 str = g_strdup_printf (_("Installing on %s…"),
566 fwupd_device_get_name (device));
567 fu_progressbar_set_title (priv->progressbar, str);
568 } else {
569 g_warning ("no FuUtilOperation set");
570 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500571 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600572
Mario Limonciello32241f42019-01-24 10:12:41 -0600573 if (priv->current_message == NULL) {
574 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
575 if (tmp != NULL)
576 priv->current_message = g_strdup (tmp);
577 }
578}
579
580static void
581fu_util_display_current_message (FuUtilPrivate *priv)
582{
583 if (priv->current_message == NULL)
584 return;
585 g_print ("%s\n", priv->current_message);
586 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500587}
588
Richard Hughes98ca9932018-05-18 10:24:07 +0100589static gboolean
590fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
591{
592 g_autoptr(FuDevice) device = NULL;
593 g_autoptr(GBytes) blob_fw = NULL;
594
595 /* invalid args */
596 if (g_strv_length (values) == 0) {
597 g_set_error_literal (error,
598 FWUPD_ERROR,
599 FWUPD_ERROR_INVALID_ARGS,
600 "Invalid arguments");
601 return FALSE;
602 }
603
604 /* parse blob */
605 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500606 if (blob_fw == NULL) {
607 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100608 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500609 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100610
611 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000612 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100613 return FALSE;
614
615 /* get device */
616 if (g_strv_length (values) >= 2) {
617 device = fu_engine_get_device (priv->engine, values[1], error);
618 if (device == NULL)
619 return FALSE;
620 } else {
621 device = fu_util_prompt_for_device (priv, error);
622 if (device == NULL)
623 return FALSE;
624 }
625
Mario Limonciello3f243a92019-01-21 22:05:23 -0600626 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500627 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600628 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500629
Richard Hughes98ca9932018-05-18 10:24:07 +0100630 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000631 if (priv->prepare_blob) {
632 g_autoptr(GPtrArray) devices = NULL;
633 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
634 g_ptr_array_add (devices, g_object_ref (device));
635 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
636 g_prefix_error (error, "failed to prepare composite action: ");
637 return FALSE;
638 }
639 }
Richard Hughes84af6e72019-02-01 18:19:41 +0000640 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600641 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000642 if (priv->cleanup_blob) {
643 g_autoptr(FuDevice) device_new = NULL;
644 g_autoptr(GError) error_local = NULL;
645
646 /* get the possibly new device from the old ID */
647 device_new = fu_engine_get_device (priv->engine,
648 fu_device_get_id (device),
649 &error_local);
650 if (device_new == NULL) {
651 g_debug ("failed to find new device: %s",
652 error_local->message);
653 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600654 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000655 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
656 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
657 g_prefix_error (error, "failed to cleanup composite action: ");
658 return FALSE;
659 }
660 }
661 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600662
Mario Limonciello32241f42019-01-24 10:12:41 -0600663 fu_util_display_current_message (priv);
664
Mario Limonciello3f243a92019-01-21 22:05:23 -0600665 /* success */
666 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100667}
668
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100669static gint
670fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
671{
672 FuInstallTask *task1 = *((FuInstallTask **) a);
673 FuInstallTask *task2 = *((FuInstallTask **) b);
674 return fu_install_task_compare (task1, task2);
675}
676
677static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100678fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
679{
680 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
681 { "curl", uri, "--output", fn, NULL },
682 { NULL } };
683 for (guint i = 0; argv[i][0] != NULL; i++) {
684 g_autoptr(GError) error_local = NULL;
685 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
686 g_debug ("%s", error_local->message);
687 continue;
688 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000689 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100690 }
691 g_set_error_literal (error,
692 FWUPD_ERROR,
693 FWUPD_ERROR_NOT_FOUND,
694 "no supported out-of-process downloaders found");
695 return FALSE;
696}
697
698static gchar *
699fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
700{
701 g_autofree gchar *filename = NULL;
702 g_autoptr(SoupURI) uri = NULL;
703
704 /* a local file */
705 uri = soup_uri_new (perhapsfn);
706 if (uri == NULL)
707 return g_strdup (perhapsfn);
708
709 /* download the firmware to a cachedir */
710 filename = fu_util_get_user_cache_path (perhapsfn);
711 if (!fu_common_mkdir_parent (filename, error))
712 return NULL;
713 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
714 return NULL;
715 return g_steal_pointer (&filename);
716}
717
718static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100719fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
720{
Richard Hughes3d178be2018-08-30 11:14:24 +0100721 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100722 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100723 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100724 g_autoptr(GPtrArray) devices_possible = NULL;
725 g_autoptr(GPtrArray) errors = NULL;
726 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100727 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100728
Mario Limonciello8949e892018-05-25 08:03:06 -0500729 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000730 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500731 return FALSE;
732
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100733 /* handle both forms */
734 if (g_strv_length (values) == 1) {
735 devices_possible = fu_engine_get_devices (priv->engine, error);
736 if (devices_possible == NULL)
737 return FALSE;
738 } else if (g_strv_length (values) == 2) {
739 FuDevice *device = fu_engine_get_device (priv->engine,
740 values[1],
741 error);
742 if (device == NULL)
743 return FALSE;
744 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
745 g_ptr_array_add (devices_possible, device);
746 } else {
747 g_set_error_literal (error,
748 FWUPD_ERROR,
749 FWUPD_ERROR_INVALID_ARGS,
750 "Invalid arguments");
751 return FALSE;
752 }
753
Richard Hughes3d178be2018-08-30 11:14:24 +0100754 /* download if required */
755 filename = fu_util_download_if_required (priv, values[0], error);
756 if (filename == NULL)
757 return FALSE;
758
Richard Hughes481aa2a2018-09-18 20:51:46 +0100759 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100760 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500761 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100762 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100763 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500764 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100765 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
766 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100767 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600768 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100769 if (components == NULL)
770 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100771
Richard Hughes481aa2a2018-09-18 20:51:46 +0100772 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100773 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
774 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100775 for (guint i = 0; i < components->len; i++) {
776 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100777
778 /* do any devices pass the requirements */
779 for (guint j = 0; j < devices_possible->len; j++) {
780 FuDevice *device = g_ptr_array_index (devices_possible, j);
781 g_autoptr(FuInstallTask) task = NULL;
782 g_autoptr(GError) error_local = NULL;
783
784 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100785 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100786 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100787 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100788 &error_local)) {
789 g_debug ("requirement on %s:%s failed: %s",
790 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100791 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100792 error_local->message);
793 g_ptr_array_add (errors, g_steal_pointer (&error_local));
794 continue;
795 }
796
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600797 /* if component should have an update message from CAB */
798 fu_device_incorporate_from_component (device, component);
799
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100800 /* success */
801 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
802 }
803 }
804
805 /* order the install tasks by the device priority */
806 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
807
808 /* nothing suitable */
809 if (install_tasks->len == 0) {
810 GError *error_tmp = fu_common_error_array_get_best (errors);
811 g_propagate_error (error, error_tmp);
812 return FALSE;
813 }
814
Mario Limonciello3f243a92019-01-21 22:05:23 -0600815 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500816 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600817 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500818
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100819 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100820 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
821 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100822
Mario Limonciello32241f42019-01-24 10:12:41 -0600823 fu_util_display_current_message (priv);
824
Mario Limonciello3f243a92019-01-21 22:05:23 -0600825 /* we don't want to ask anything */
826 if (priv->no_reboot_check) {
827 g_debug ("skipping reboot check");
828 return TRUE;
829 }
830
Mario Limonciello3143bad2019-02-27 07:31:00 -0600831 /* save the device state for other applications to see */
832 if (!fu_util_save_current_state (priv, error))
833 return FALSE;
834
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100835 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600836 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100837}
838
Richard Hughes98ca9932018-05-18 10:24:07 +0100839static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600840fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
841{
842 g_autoptr(GPtrArray) devices = NULL;
843
844 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000845 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello46aaee82019-01-10 12:58:00 -0600846 return FALSE;
847
Mario Limonciello3f243a92019-01-21 22:05:23 -0600848 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
849 g_signal_connect (priv->engine, "device-changed",
850 G_CALLBACK (fu_util_update_device_changed_cb), priv);
851
Mario Limonciello46aaee82019-01-10 12:58:00 -0600852 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000853 if (devices == NULL)
854 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600855 for (guint i = 0; i < devices->len; i++) {
856 FwupdDevice *dev = g_ptr_array_index (devices, i);
857 FwupdRelease *rel;
858 const gchar *remote_id;
859 const gchar *device_id;
860 const gchar *uri_tmp;
861 g_autoptr(GPtrArray) rels = NULL;
862 g_autoptr(GError) error_local = NULL;
863
864 if (!fu_util_is_interesting_device (dev))
865 continue;
866 /* only show stuff that has metadata available */
867 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
868 continue;
869
870 device_id = fu_device_get_id (dev);
871 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
872 if (rels == NULL) {
873 g_printerr ("%s\n", error_local->message);
874 continue;
875 }
876
877 rel = g_ptr_array_index (rels, 0);
878 uri_tmp = fwupd_release_get_uri (rel);
879 remote_id = fwupd_release_get_remote_id (rel);
880 if (remote_id != NULL) {
881 FwupdRemote *remote;
882 g_auto(GStrv) argv = NULL;
883
884 remote = fu_engine_get_remote_by_id (priv->engine,
885 remote_id,
886 &error_local);
887 if (remote == NULL) {
888 g_printerr ("%s\n", error_local->message);
889 continue;
890 }
891
892 argv = g_new0 (gchar *, 2);
893 /* local remotes have the firmware already */
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600894 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
Mario Limonciello46aaee82019-01-10 12:58:00 -0600895 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
896 g_autofree gchar *path = g_path_get_dirname (fn_cache);
897 argv[0] = g_build_filename (path, uri_tmp, NULL);
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600898 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
899 argv[0] = g_strdup (uri_tmp + 7);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600900 /* web remote, fu_util_install will download file */
901 } else {
902 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
903 }
904 if (!fu_util_install (priv, argv, &error_local)) {
905 g_printerr ("%s\n", error_local->message);
906 continue;
907 }
Mario Limonciello32241f42019-01-24 10:12:41 -0600908 fu_util_display_current_message (priv);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600909 }
910 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600911
912 /* we don't want to ask anything */
913 if (priv->no_reboot_check) {
914 g_debug ("skipping reboot check");
915 return TRUE;
916 }
917
Mario Limonciello3143bad2019-02-27 07:31:00 -0600918 /* save the device state for other applications to see */
919 if (!fu_util_save_current_state (priv, error))
920 return FALSE;
921
Mario Limonciello3f243a92019-01-21 22:05:23 -0600922 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600923}
924
925static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100926fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
927{
928 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100929 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100930
931 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000932 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100933 return FALSE;
934
935 /* invalid args */
936 if (g_strv_length (values) == 0) {
937 g_set_error_literal (error,
938 FWUPD_ERROR,
939 FWUPD_ERROR_INVALID_ARGS,
940 "Invalid arguments");
941 return FALSE;
942 }
943
944 /* get device */
945 if (g_strv_length (values) >= 1) {
946 device = fu_engine_get_device (priv->engine, values[0], error);
947 if (device == NULL)
948 return FALSE;
949 } else {
950 device = fu_util_prompt_for_device (priv, error);
951 if (device == NULL)
952 return FALSE;
953 }
954
955 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100956 locker = fu_device_locker_new (device, error);
957 if (locker == NULL)
958 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100959 return fu_device_detach (device, error);
960}
961
962static gboolean
963fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
964{
965 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100966 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100967
968 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000969 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100970 return FALSE;
971
972 /* invalid args */
973 if (g_strv_length (values) == 0) {
974 g_set_error_literal (error,
975 FWUPD_ERROR,
976 FWUPD_ERROR_INVALID_ARGS,
977 "Invalid arguments");
978 return FALSE;
979 }
980
981 /* get device */
982 if (g_strv_length (values) >= 1) {
983 device = fu_engine_get_device (priv->engine, values[0], error);
984 if (device == NULL)
985 return FALSE;
986 } else {
987 device = fu_util_prompt_for_device (priv, error);
988 if (device == NULL)
989 return FALSE;
990 }
991
992 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100993 locker = fu_device_locker_new (device, error);
994 if (locker == NULL)
995 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100996 return fu_device_attach (device, error);
997}
998
Richard Hughes1d894f12018-08-31 13:05:51 +0100999static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001000fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1001{
1002 gboolean has_pending = FALSE;
1003 g_autoptr(FuHistory) history = fu_history_new ();
1004 g_autoptr(GPtrArray) devices = NULL;
1005
1006 /* check the history database before starting the daemon */
1007 if (g_strv_length (values) == 0) {
1008 devices = fu_history_get_devices (history, error);
1009 if (devices == NULL)
1010 return FALSE;
1011 } else if (g_strv_length (values) == 1) {
1012 FuDevice *device;
1013 device = fu_history_get_device_by_id (history, values[0], error);
1014 if (device == NULL)
1015 return FALSE;
1016 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1017 g_ptr_array_add (devices, device);
1018 } else {
1019 g_set_error_literal (error,
1020 FWUPD_ERROR,
1021 FWUPD_ERROR_INVALID_ARGS,
1022 "Invalid arguments");
1023 return FALSE;
1024 }
1025
1026 /* nothing to do */
1027 for (guint i = 0; i < devices->len; i++) {
1028 FuDevice *dev = g_ptr_array_index (devices, i);
1029 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1030 fu_engine_add_plugin_filter (priv->engine,
1031 fu_device_get_plugin (dev));
1032 has_pending = TRUE;
1033 }
1034 }
1035 if (!has_pending) {
1036 g_set_error_literal (error,
1037 FWUPD_ERROR,
1038 FWUPD_ERROR_NOTHING_TO_DO,
1039 "No firmware to activate");
1040 return FALSE;
1041
1042 }
1043
1044 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001045 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001046 return FALSE;
1047
1048 /* activate anything with _NEEDS_ACTIVATION */
1049 for (guint i = 0; i < devices->len; i++) {
1050 FuDevice *device = g_ptr_array_index (devices, i);
1051 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1052 continue;
1053 /* TRANSLATORS: shown when shutting down to switch to the new version */
1054 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1055 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1056 return FALSE;
1057 }
1058
1059 return TRUE;
1060}
1061
1062static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001063fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1064{
1065 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1066 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1067 const gchar *hwid_keys[] = {
1068 FU_HWIDS_KEY_BIOS_VENDOR,
1069 FU_HWIDS_KEY_BIOS_VERSION,
1070 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1071 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1072 FU_HWIDS_KEY_MANUFACTURER,
1073 FU_HWIDS_KEY_FAMILY,
1074 FU_HWIDS_KEY_PRODUCT_NAME,
1075 FU_HWIDS_KEY_PRODUCT_SKU,
1076 FU_HWIDS_KEY_ENCLOSURE_KIND,
1077 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1078 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1079 NULL };
1080
1081 /* read DMI data */
1082 if (g_strv_length (values) == 0) {
1083 if (!fu_smbios_setup (smbios, error))
1084 return FALSE;
1085 } else if (g_strv_length (values) == 1) {
1086 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1087 return FALSE;
1088 } else {
1089 g_set_error_literal (error,
1090 FWUPD_ERROR,
1091 FWUPD_ERROR_INVALID_ARGS,
1092 "Invalid arguments");
1093 return FALSE;
1094 }
1095 if (!fu_hwids_setup (hwids, smbios, error))
1096 return FALSE;
1097
1098 /* show debug output */
1099 g_print ("Computer Information\n");
1100 g_print ("--------------------\n");
1101 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1102 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1103 if (tmp == NULL)
1104 continue;
1105 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1106 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1107 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1108 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1109 } else {
1110 g_print ("%s: %s\n", hwid_keys[i], tmp);
1111 }
1112 }
1113
1114 /* show GUIDs */
1115 g_print ("\nHardware IDs\n");
1116 g_print ("------------\n");
1117 for (guint i = 0; i < 15; i++) {
1118 const gchar *keys = NULL;
1119 g_autofree gchar *guid = NULL;
1120 g_autofree gchar *key = NULL;
1121 g_autofree gchar *keys_str = NULL;
1122 g_auto(GStrv) keysv = NULL;
1123 g_autoptr(GError) error_local = NULL;
1124
1125 /* get the GUID */
1126 key = g_strdup_printf ("HardwareID-%u", i);
1127 keys = fu_hwids_get_replace_keys (hwids, key);
1128 guid = fu_hwids_get_guid (hwids, key, &error_local);
1129 if (guid == NULL) {
1130 g_print ("%s\n", error_local->message);
1131 continue;
1132 }
1133
1134 /* show what makes up the GUID */
1135 keysv = g_strsplit (keys, "&", -1);
1136 keys_str = g_strjoinv (" + ", keysv);
1137 g_print ("{%s} <- %s\n", guid, keys_str);
1138 }
1139
1140 return TRUE;
1141}
1142
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001143static gboolean
1144fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1145{
1146 const gchar *script_fn = "startup.sh";
1147 const gchar *output_fn = "firmware.bin";
1148 g_autoptr(GBytes) archive_blob = NULL;
1149 g_autoptr(GBytes) firmware_blob = NULL;
1150 if (g_strv_length (values) < 2) {
1151 g_set_error_literal (error,
1152 FWUPD_ERROR,
1153 FWUPD_ERROR_INVALID_ARGS,
1154 "Invalid arguments");
1155 return FALSE;
1156 }
1157 archive_blob = fu_common_get_contents_bytes (values[0], error);
1158 if (archive_blob == NULL)
1159 return FALSE;
1160 if (g_strv_length (values) > 2)
1161 script_fn = values[2];
1162 if (g_strv_length (values) > 3)
1163 output_fn = values[3];
1164 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1165 if (firmware_blob == NULL)
1166 return FALSE;
1167 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1168}
1169
Richard Hughes3d607622019-03-07 16:59:27 +00001170static gboolean
1171fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1172{
1173 g_autofree gchar *sig = NULL;
1174
1175 /* check args */
1176 if (g_strv_length (values) != 1) {
1177 g_set_error_literal (error,
1178 FWUPD_ERROR,
1179 FWUPD_ERROR_INVALID_ARGS,
1180 "Invalid arguments: value expected");
1181 return FALSE;
1182 }
1183
1184 /* start engine */
1185 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1186 return FALSE;
1187 sig = fu_engine_self_sign (priv->engine, values[0],
1188 FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP |
1189 FU_KEYRING_SIGN_FLAG_ADD_CERT, error);
1190 if (sig == NULL)
1191 return FALSE;
1192 g_print ("%s\n", sig);
1193 return TRUE;
1194}
1195
Mario Limonciello62f84862018-10-18 13:15:23 -05001196static void
1197fu_util_device_added_cb (FwupdClient *client,
1198 FwupdDevice *device,
1199 gpointer user_data)
1200{
1201 g_autofree gchar *tmp = fwupd_device_to_string (device);
1202 /* TRANSLATORS: this is when a device is hotplugged */
1203 g_print ("%s\n%s", _("Device added:"), tmp);
1204}
1205
1206static void
1207fu_util_device_removed_cb (FwupdClient *client,
1208 FwupdDevice *device,
1209 gpointer user_data)
1210{
1211 g_autofree gchar *tmp = fwupd_device_to_string (device);
1212 /* TRANSLATORS: this is when a device is hotplugged */
1213 g_print ("%s\n%s", _("Device removed:"), tmp);
1214}
1215
1216static void
1217fu_util_device_changed_cb (FwupdClient *client,
1218 FwupdDevice *device,
1219 gpointer user_data)
1220{
1221 g_autofree gchar *tmp = fwupd_device_to_string (device);
1222 /* TRANSLATORS: this is when a device has been updated */
1223 g_print ("%s\n%s", _("Device changed:"), tmp);
1224}
1225
1226static void
1227fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1228{
1229 /* TRANSLATORS: this is when the daemon state changes */
1230 g_print ("%s\n", _("Changed"));
1231}
1232
1233static gboolean
1234fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1235{
1236 g_autoptr(FwupdClient) client = fwupd_client_new ();
1237
1238 /* get all the devices */
1239 if (!fwupd_client_connect (client, priv->cancellable, error))
1240 return FALSE;
1241
1242 /* watch for any hotplugged device */
1243 g_signal_connect (client, "changed",
1244 G_CALLBACK (fu_util_changed_cb), priv);
1245 g_signal_connect (client, "device-added",
1246 G_CALLBACK (fu_util_device_added_cb), priv);
1247 g_signal_connect (client, "device-removed",
1248 G_CALLBACK (fu_util_device_removed_cb), priv);
1249 g_signal_connect (client, "device-changed",
1250 G_CALLBACK (fu_util_device_changed_cb), priv);
1251 g_signal_connect (priv->cancellable, "cancelled",
1252 G_CALLBACK (fu_util_cancelled_cb), priv);
1253 g_main_loop_run (priv->loop);
1254 return TRUE;
1255}
1256
Richard Hughes15684492019-03-15 16:27:50 +00001257static gboolean
1258fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1259{
1260 g_autofree gchar *str = NULL;
1261 g_autoptr(FuDevice) dev = NULL;
1262
1263 /* load engine */
1264 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1265 return FALSE;
1266
1267 /* get device */
1268 if (g_strv_length (values) == 1) {
1269 dev = fu_engine_get_device (priv->engine, values[1], error);
1270 if (dev == NULL)
1271 return FALSE;
1272 } else {
1273 dev = fu_util_prompt_for_device (priv, error);
1274 if (dev == NULL)
1275 return FALSE;
1276 }
1277
1278 /* add checksums */
1279 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1280 return FALSE;
1281
1282 /* show checksums */
1283 str = fu_device_to_string (dev);
1284 g_print ("%s\n", str);
1285 return TRUE;
1286}
1287
Richard Hughesb5976832018-05-18 10:02:09 +01001288int
1289main (int argc, char *argv[])
1290{
Richard Hughes460226a2018-05-21 20:56:21 +01001291 gboolean allow_older = FALSE;
1292 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001293 gboolean force = FALSE;
1294 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001295 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001296 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001297 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001298 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1299 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00001300 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01001301 g_autofree gchar *cmd_descriptions = NULL;
1302 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001303 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1304 /* TRANSLATORS: command line option */
1305 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001306 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1307 /* TRANSLATORS: command line option */
1308 _("Allow re-installing existing firmware versions"), NULL },
1309 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1310 /* TRANSLATORS: command line option */
1311 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001312 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1313 /* TRANSLATORS: command line option */
1314 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001315 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1316 /* TRANSLATORS: command line option */
1317 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001318 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1319 /* TRANSLATORS: command line option */
1320 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001321 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1322 /* TRANSLATORS: command line option */
1323 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001324 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001325 /* TRANSLATORS: command line option */
1326 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001327 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001328 /* TRANSLATORS: command line option */
1329 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001330 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1331 /* TRANSLATORS: command line option */
1332 _("Save device state into a JSON file between executions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001333 { NULL}
1334 };
1335
1336 setlocale (LC_ALL, "");
1337
1338 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1339 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1340 textdomain (GETTEXT_PACKAGE);
1341
1342 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001343 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001344 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001345 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001346
1347 /* create helper object */
1348 priv->loop = g_main_loop_new (NULL, FALSE);
1349 priv->progressbar = fu_progressbar_new ();
1350
1351 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00001352 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001353 "build-firmware",
1354 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1355 /* TRANSLATORS: command description */
1356 _("Build firmware using a sandbox"),
1357 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00001358 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001359 "smbios-dump",
1360 "FILE",
1361 /* TRANSLATORS: command description */
1362 _("Dump SMBIOS data from a file"),
1363 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00001364 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001365 "get-plugins",
1366 NULL,
1367 /* TRANSLATORS: command description */
1368 _("Get all enabled plugins registered with the system"),
1369 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00001370 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001371 "get-details",
1372 NULL,
1373 /* TRANSLATORS: command description */
1374 _("Gets details about a firmware file"),
1375 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00001376 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001377 "get-updates",
1378 NULL,
1379 /* TRANSLATORS: command description */
1380 _("Gets the list of updates for connected hardware"),
1381 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00001382 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001383 "get-devices",
1384 NULL,
1385 /* TRANSLATORS: command description */
1386 _("Get all devices that support firmware updates"),
1387 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00001388 fu_util_cmd_array_add (cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001389 "get-topology",
1390 NULL,
1391 /* TRANSLATORS: command description */
1392 _("Get all devices according to the system topology"),
1393 fu_util_get_topology);
Richard Hughesc77e1112019-03-01 10:16:26 +00001394 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001395 "watch",
1396 NULL,
1397 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001398 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001399 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00001400 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001401 "install-blob",
1402 "FILENAME DEVICE-ID",
1403 /* TRANSLATORS: command description */
1404 _("Install a firmware blob on a device"),
1405 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00001406 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001407 "install",
1408 "FILE [ID]",
1409 /* TRANSLATORS: command description */
1410 _("Install a firmware file on this hardware"),
1411 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00001412 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001413 "attach",
1414 "DEVICE-ID",
1415 /* TRANSLATORS: command description */
1416 _("Attach to firmware mode"),
1417 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001418 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001419 "detach",
1420 "DEVICE-ID",
1421 /* TRANSLATORS: command description */
1422 _("Detach to bootloader mode"),
1423 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001424 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001425 "activate",
1426 "[DEVICE-ID]",
1427 /* TRANSLATORS: command description */
1428 _("Activate pending devices"),
1429 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00001430 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001431 "hwids",
1432 "[FILE]",
1433 /* TRANSLATORS: command description */
1434 _("Return all the hardware IDs for the machine"),
1435 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00001436 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05001437 "monitor",
1438 NULL,
1439 /* TRANSLATORS: command description */
1440 _("Monitor the daemon for events"),
1441 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00001442 fu_util_cmd_array_add (cmd_array,
Mario Limonciello46aaee82019-01-10 12:58:00 -06001443 "update",
1444 NULL,
1445 /* TRANSLATORS: command description */
1446 _("Update all devices that match local metadata"),
1447 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00001448 fu_util_cmd_array_add (cmd_array,
1449 "self-sign",
1450 "TEXT",
1451 /* TRANSLATORS: command description */
1452 _("Sign data using the client certificate"),
1453 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00001454 fu_util_cmd_array_add (cmd_array,
1455 "verify-update",
1456 "[DEVICE_ID]",
1457 /* TRANSLATORS: command description */
1458 _("Update the stored metadata with current contents"),
1459 fu_util_verify_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001460
1461 /* do stuff on ctrl+c */
1462 priv->cancellable = g_cancellable_new ();
1463 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1464 SIGINT, fu_util_sigint_cb,
1465 priv, NULL);
1466 g_signal_connect (priv->cancellable, "cancelled",
1467 G_CALLBACK (fu_util_cancelled_cb), priv);
1468
1469 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00001470 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001471
Mario Limonciello3f243a92019-01-21 22:05:23 -06001472 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001473 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001474 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001475 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1476 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001477
Richard Hughesb5976832018-05-18 10:02:09 +01001478 /* get a list of the commands */
1479 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00001480 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001481 g_option_context_set_summary (priv->context, cmd_descriptions);
1482 g_option_context_set_description (priv->context,
1483 "This tool allows an administrator to use the fwupd plugins "
1484 "without being installed on the host system.");
1485
1486 /* TRANSLATORS: program name */
1487 g_set_application_name (_("Firmware Utility"));
1488 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001489 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001490 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1491 if (!ret) {
1492 /* TRANSLATORS: the user didn't read the man page */
1493 g_print ("%s: %s\n", _("Failed to parse arguments"),
1494 error->message);
1495 return EXIT_FAILURE;
1496 }
1497
Richard Hughes460226a2018-05-21 20:56:21 +01001498 /* set flags */
1499 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1500 if (allow_reinstall)
1501 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1502 if (allow_older)
1503 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1504 if (force)
1505 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1506
Richard Hughes98ca9932018-05-18 10:24:07 +01001507 /* load engine */
1508 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1509 g_signal_connect (priv->engine, "device-added",
1510 G_CALLBACK (fu_main_engine_device_added_cb),
1511 priv);
1512 g_signal_connect (priv->engine, "device-removed",
1513 G_CALLBACK (fu_main_engine_device_removed_cb),
1514 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001515 g_signal_connect (priv->engine, "status-changed",
1516 G_CALLBACK (fu_main_engine_status_changed_cb),
1517 priv);
1518 g_signal_connect (priv->engine, "percentage-changed",
1519 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1520 priv);
1521
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001522 /* just show versions and exit */
1523 if (version) {
1524 g_autofree gchar *version_str = fu_util_get_versions ();
1525 g_print ("%s\n", version_str);
1526 return EXIT_SUCCESS;
1527 }
1528
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001529 /* any plugin whitelist specified */
1530 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1531 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1532
Richard Hughesb5976832018-05-18 10:02:09 +01001533 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00001534 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01001535 if (!ret) {
1536 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1537 g_autofree gchar *tmp = NULL;
1538 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1539 g_print ("%s\n\n%s", error->message, tmp);
1540 return EXIT_FAILURE;
1541 }
1542 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1543 g_print ("%s\n", error->message);
1544 return EXIT_NOTHING_TO_DO;
1545 }
1546 g_print ("%s\n", error->message);
1547 return EXIT_FAILURE;
1548 }
1549
1550 /* success */
1551 return EXIT_SUCCESS;
1552}