blob: 73f538cd42b13dcf72632f8cc0219494245419c2 [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 }
Mario Limonciello035818b2019-03-26 11:12:16 -0500640 priv->flags = FWUPD_INSTALL_FLAG_NO_HISTORY;
Richard Hughes84af6e72019-02-01 18:19:41 +0000641 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600642 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000643 if (priv->cleanup_blob) {
644 g_autoptr(FuDevice) device_new = NULL;
645 g_autoptr(GError) error_local = NULL;
646
647 /* get the possibly new device from the old ID */
648 device_new = fu_engine_get_device (priv->engine,
649 fu_device_get_id (device),
650 &error_local);
651 if (device_new == NULL) {
652 g_debug ("failed to find new device: %s",
653 error_local->message);
654 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600655 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000656 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
657 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
658 g_prefix_error (error, "failed to cleanup composite action: ");
659 return FALSE;
660 }
661 }
662 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600663
Mario Limonciello32241f42019-01-24 10:12:41 -0600664 fu_util_display_current_message (priv);
665
Mario Limonciello3f243a92019-01-21 22:05:23 -0600666 /* success */
667 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100668}
669
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100670static gint
671fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
672{
673 FuInstallTask *task1 = *((FuInstallTask **) a);
674 FuInstallTask *task2 = *((FuInstallTask **) b);
675 return fu_install_task_compare (task1, task2);
676}
677
678static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100679fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
680{
681 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
682 { "curl", uri, "--output", fn, NULL },
683 { NULL } };
684 for (guint i = 0; argv[i][0] != NULL; i++) {
685 g_autoptr(GError) error_local = NULL;
686 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
687 g_debug ("%s", error_local->message);
688 continue;
689 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000690 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100691 }
692 g_set_error_literal (error,
693 FWUPD_ERROR,
694 FWUPD_ERROR_NOT_FOUND,
695 "no supported out-of-process downloaders found");
696 return FALSE;
697}
698
699static gchar *
700fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
701{
702 g_autofree gchar *filename = NULL;
703 g_autoptr(SoupURI) uri = NULL;
704
705 /* a local file */
706 uri = soup_uri_new (perhapsfn);
707 if (uri == NULL)
708 return g_strdup (perhapsfn);
709
710 /* download the firmware to a cachedir */
711 filename = fu_util_get_user_cache_path (perhapsfn);
712 if (!fu_common_mkdir_parent (filename, error))
713 return NULL;
714 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
715 return NULL;
716 return g_steal_pointer (&filename);
717}
718
719static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100720fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
721{
Richard Hughes3d178be2018-08-30 11:14:24 +0100722 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100723 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100724 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100725 g_autoptr(GPtrArray) devices_possible = NULL;
726 g_autoptr(GPtrArray) errors = NULL;
727 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100728 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100729
Mario Limonciello8949e892018-05-25 08:03:06 -0500730 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000731 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500732 return FALSE;
733
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100734 /* handle both forms */
735 if (g_strv_length (values) == 1) {
736 devices_possible = fu_engine_get_devices (priv->engine, error);
737 if (devices_possible == NULL)
738 return FALSE;
739 } else if (g_strv_length (values) == 2) {
740 FuDevice *device = fu_engine_get_device (priv->engine,
741 values[1],
742 error);
743 if (device == NULL)
744 return FALSE;
745 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
746 g_ptr_array_add (devices_possible, device);
747 } else {
748 g_set_error_literal (error,
749 FWUPD_ERROR,
750 FWUPD_ERROR_INVALID_ARGS,
751 "Invalid arguments");
752 return FALSE;
753 }
754
Richard Hughes3d178be2018-08-30 11:14:24 +0100755 /* download if required */
756 filename = fu_util_download_if_required (priv, values[0], error);
757 if (filename == NULL)
758 return FALSE;
759
Richard Hughes481aa2a2018-09-18 20:51:46 +0100760 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100761 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500762 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100763 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100764 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500765 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100766 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
767 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100768 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600769 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100770 if (components == NULL)
771 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100772
Richard Hughes481aa2a2018-09-18 20:51:46 +0100773 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100774 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
775 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100776 for (guint i = 0; i < components->len; i++) {
777 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100778
779 /* do any devices pass the requirements */
780 for (guint j = 0; j < devices_possible->len; j++) {
781 FuDevice *device = g_ptr_array_index (devices_possible, j);
782 g_autoptr(FuInstallTask) task = NULL;
783 g_autoptr(GError) error_local = NULL;
784
785 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100786 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100787 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100788 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100789 &error_local)) {
790 g_debug ("requirement on %s:%s failed: %s",
791 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100792 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100793 error_local->message);
794 g_ptr_array_add (errors, g_steal_pointer (&error_local));
795 continue;
796 }
797
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600798 /* if component should have an update message from CAB */
799 fu_device_incorporate_from_component (device, component);
800
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100801 /* success */
802 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
803 }
804 }
805
806 /* order the install tasks by the device priority */
807 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
808
809 /* nothing suitable */
810 if (install_tasks->len == 0) {
811 GError *error_tmp = fu_common_error_array_get_best (errors);
812 g_propagate_error (error, error_tmp);
813 return FALSE;
814 }
815
Mario Limonciello3f243a92019-01-21 22:05:23 -0600816 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500817 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600818 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500819
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100820 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100821 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
822 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100823
Mario Limonciello32241f42019-01-24 10:12:41 -0600824 fu_util_display_current_message (priv);
825
Mario Limonciello3f243a92019-01-21 22:05:23 -0600826 /* we don't want to ask anything */
827 if (priv->no_reboot_check) {
828 g_debug ("skipping reboot check");
829 return TRUE;
830 }
831
Mario Limonciello3143bad2019-02-27 07:31:00 -0600832 /* save the device state for other applications to see */
833 if (!fu_util_save_current_state (priv, error))
834 return FALSE;
835
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100836 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600837 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100838}
839
Richard Hughes98ca9932018-05-18 10:24:07 +0100840static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600841fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
842{
843 g_autoptr(GPtrArray) devices = NULL;
844
845 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000846 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello46aaee82019-01-10 12:58:00 -0600847 return FALSE;
848
Mario Limonciello3f243a92019-01-21 22:05:23 -0600849 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
850 g_signal_connect (priv->engine, "device-changed",
851 G_CALLBACK (fu_util_update_device_changed_cb), priv);
852
Mario Limonciello46aaee82019-01-10 12:58:00 -0600853 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000854 if (devices == NULL)
855 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600856 for (guint i = 0; i < devices->len; i++) {
857 FwupdDevice *dev = g_ptr_array_index (devices, i);
858 FwupdRelease *rel;
859 const gchar *remote_id;
860 const gchar *device_id;
861 const gchar *uri_tmp;
862 g_autoptr(GPtrArray) rels = NULL;
863 g_autoptr(GError) error_local = NULL;
864
865 if (!fu_util_is_interesting_device (dev))
866 continue;
867 /* only show stuff that has metadata available */
868 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
869 continue;
870
871 device_id = fu_device_get_id (dev);
872 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
873 if (rels == NULL) {
874 g_printerr ("%s\n", error_local->message);
875 continue;
876 }
877
878 rel = g_ptr_array_index (rels, 0);
879 uri_tmp = fwupd_release_get_uri (rel);
880 remote_id = fwupd_release_get_remote_id (rel);
881 if (remote_id != NULL) {
882 FwupdRemote *remote;
883 g_auto(GStrv) argv = NULL;
884
885 remote = fu_engine_get_remote_by_id (priv->engine,
886 remote_id,
887 &error_local);
888 if (remote == NULL) {
889 g_printerr ("%s\n", error_local->message);
890 continue;
891 }
892
893 argv = g_new0 (gchar *, 2);
894 /* local remotes have the firmware already */
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600895 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
Mario Limonciello46aaee82019-01-10 12:58:00 -0600896 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
897 g_autofree gchar *path = g_path_get_dirname (fn_cache);
898 argv[0] = g_build_filename (path, uri_tmp, NULL);
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600899 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
900 argv[0] = g_strdup (uri_tmp + 7);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600901 /* web remote, fu_util_install will download file */
902 } else {
903 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
904 }
905 if (!fu_util_install (priv, argv, &error_local)) {
906 g_printerr ("%s\n", error_local->message);
907 continue;
908 }
Mario Limonciello32241f42019-01-24 10:12:41 -0600909 fu_util_display_current_message (priv);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600910 }
911 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600912
913 /* we don't want to ask anything */
914 if (priv->no_reboot_check) {
915 g_debug ("skipping reboot check");
916 return TRUE;
917 }
918
Mario Limonciello3143bad2019-02-27 07:31:00 -0600919 /* save the device state for other applications to see */
920 if (!fu_util_save_current_state (priv, error))
921 return FALSE;
922
Mario Limonciello3f243a92019-01-21 22:05:23 -0600923 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600924}
925
926static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100927fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
928{
929 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100930 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100931
932 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000933 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100934 return FALSE;
935
936 /* invalid args */
937 if (g_strv_length (values) == 0) {
938 g_set_error_literal (error,
939 FWUPD_ERROR,
940 FWUPD_ERROR_INVALID_ARGS,
941 "Invalid arguments");
942 return FALSE;
943 }
944
945 /* get device */
946 if (g_strv_length (values) >= 1) {
947 device = fu_engine_get_device (priv->engine, values[0], error);
948 if (device == NULL)
949 return FALSE;
950 } else {
951 device = fu_util_prompt_for_device (priv, error);
952 if (device == NULL)
953 return FALSE;
954 }
955
956 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100957 locker = fu_device_locker_new (device, error);
958 if (locker == NULL)
959 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100960 return fu_device_detach (device, error);
961}
962
963static gboolean
964fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
965{
966 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100967 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100968
969 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000970 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100971 return FALSE;
972
973 /* invalid args */
974 if (g_strv_length (values) == 0) {
975 g_set_error_literal (error,
976 FWUPD_ERROR,
977 FWUPD_ERROR_INVALID_ARGS,
978 "Invalid arguments");
979 return FALSE;
980 }
981
982 /* get device */
983 if (g_strv_length (values) >= 1) {
984 device = fu_engine_get_device (priv->engine, values[0], error);
985 if (device == NULL)
986 return FALSE;
987 } else {
988 device = fu_util_prompt_for_device (priv, error);
989 if (device == NULL)
990 return FALSE;
991 }
992
993 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100994 locker = fu_device_locker_new (device, error);
995 if (locker == NULL)
996 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100997 return fu_device_attach (device, error);
998}
999
Richard Hughes1d894f12018-08-31 13:05:51 +01001000static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001001fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1002{
1003 gboolean has_pending = FALSE;
1004 g_autoptr(FuHistory) history = fu_history_new ();
1005 g_autoptr(GPtrArray) devices = NULL;
1006
1007 /* check the history database before starting the daemon */
1008 if (g_strv_length (values) == 0) {
1009 devices = fu_history_get_devices (history, error);
1010 if (devices == NULL)
1011 return FALSE;
1012 } else if (g_strv_length (values) == 1) {
1013 FuDevice *device;
1014 device = fu_history_get_device_by_id (history, values[0], error);
1015 if (device == NULL)
1016 return FALSE;
1017 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1018 g_ptr_array_add (devices, device);
1019 } else {
1020 g_set_error_literal (error,
1021 FWUPD_ERROR,
1022 FWUPD_ERROR_INVALID_ARGS,
1023 "Invalid arguments");
1024 return FALSE;
1025 }
1026
1027 /* nothing to do */
1028 for (guint i = 0; i < devices->len; i++) {
1029 FuDevice *dev = g_ptr_array_index (devices, i);
1030 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1031 fu_engine_add_plugin_filter (priv->engine,
1032 fu_device_get_plugin (dev));
1033 has_pending = TRUE;
1034 }
1035 }
1036 if (!has_pending) {
1037 g_set_error_literal (error,
1038 FWUPD_ERROR,
1039 FWUPD_ERROR_NOTHING_TO_DO,
1040 "No firmware to activate");
1041 return FALSE;
1042
1043 }
1044
1045 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001046 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001047 return FALSE;
1048
1049 /* activate anything with _NEEDS_ACTIVATION */
1050 for (guint i = 0; i < devices->len; i++) {
1051 FuDevice *device = g_ptr_array_index (devices, i);
1052 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1053 continue;
1054 /* TRANSLATORS: shown when shutting down to switch to the new version */
1055 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1056 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1057 return FALSE;
1058 }
1059
1060 return TRUE;
1061}
1062
1063static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001064fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1065{
1066 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1067 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1068 const gchar *hwid_keys[] = {
1069 FU_HWIDS_KEY_BIOS_VENDOR,
1070 FU_HWIDS_KEY_BIOS_VERSION,
1071 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1072 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1073 FU_HWIDS_KEY_MANUFACTURER,
1074 FU_HWIDS_KEY_FAMILY,
1075 FU_HWIDS_KEY_PRODUCT_NAME,
1076 FU_HWIDS_KEY_PRODUCT_SKU,
1077 FU_HWIDS_KEY_ENCLOSURE_KIND,
1078 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1079 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1080 NULL };
1081
1082 /* read DMI data */
1083 if (g_strv_length (values) == 0) {
1084 if (!fu_smbios_setup (smbios, error))
1085 return FALSE;
1086 } else if (g_strv_length (values) == 1) {
1087 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1088 return FALSE;
1089 } else {
1090 g_set_error_literal (error,
1091 FWUPD_ERROR,
1092 FWUPD_ERROR_INVALID_ARGS,
1093 "Invalid arguments");
1094 return FALSE;
1095 }
1096 if (!fu_hwids_setup (hwids, smbios, error))
1097 return FALSE;
1098
1099 /* show debug output */
1100 g_print ("Computer Information\n");
1101 g_print ("--------------------\n");
1102 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1103 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1104 if (tmp == NULL)
1105 continue;
1106 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1107 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1108 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1109 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1110 } else {
1111 g_print ("%s: %s\n", hwid_keys[i], tmp);
1112 }
1113 }
1114
1115 /* show GUIDs */
1116 g_print ("\nHardware IDs\n");
1117 g_print ("------------\n");
1118 for (guint i = 0; i < 15; i++) {
1119 const gchar *keys = NULL;
1120 g_autofree gchar *guid = NULL;
1121 g_autofree gchar *key = NULL;
1122 g_autofree gchar *keys_str = NULL;
1123 g_auto(GStrv) keysv = NULL;
1124 g_autoptr(GError) error_local = NULL;
1125
1126 /* get the GUID */
1127 key = g_strdup_printf ("HardwareID-%u", i);
1128 keys = fu_hwids_get_replace_keys (hwids, key);
1129 guid = fu_hwids_get_guid (hwids, key, &error_local);
1130 if (guid == NULL) {
1131 g_print ("%s\n", error_local->message);
1132 continue;
1133 }
1134
1135 /* show what makes up the GUID */
1136 keysv = g_strsplit (keys, "&", -1);
1137 keys_str = g_strjoinv (" + ", keysv);
1138 g_print ("{%s} <- %s\n", guid, keys_str);
1139 }
1140
1141 return TRUE;
1142}
1143
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001144static gboolean
1145fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1146{
1147 const gchar *script_fn = "startup.sh";
1148 const gchar *output_fn = "firmware.bin";
1149 g_autoptr(GBytes) archive_blob = NULL;
1150 g_autoptr(GBytes) firmware_blob = NULL;
1151 if (g_strv_length (values) < 2) {
1152 g_set_error_literal (error,
1153 FWUPD_ERROR,
1154 FWUPD_ERROR_INVALID_ARGS,
1155 "Invalid arguments");
1156 return FALSE;
1157 }
1158 archive_blob = fu_common_get_contents_bytes (values[0], error);
1159 if (archive_blob == NULL)
1160 return FALSE;
1161 if (g_strv_length (values) > 2)
1162 script_fn = values[2];
1163 if (g_strv_length (values) > 3)
1164 output_fn = values[3];
1165 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1166 if (firmware_blob == NULL)
1167 return FALSE;
1168 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1169}
1170
Richard Hughes3d607622019-03-07 16:59:27 +00001171static gboolean
1172fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1173{
1174 g_autofree gchar *sig = NULL;
1175
1176 /* check args */
1177 if (g_strv_length (values) != 1) {
1178 g_set_error_literal (error,
1179 FWUPD_ERROR,
1180 FWUPD_ERROR_INVALID_ARGS,
1181 "Invalid arguments: value expected");
1182 return FALSE;
1183 }
1184
1185 /* start engine */
1186 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1187 return FALSE;
1188 sig = fu_engine_self_sign (priv->engine, values[0],
1189 FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP |
1190 FU_KEYRING_SIGN_FLAG_ADD_CERT, error);
1191 if (sig == NULL)
1192 return FALSE;
1193 g_print ("%s\n", sig);
1194 return TRUE;
1195}
1196
Mario Limonciello62f84862018-10-18 13:15:23 -05001197static void
1198fu_util_device_added_cb (FwupdClient *client,
1199 FwupdDevice *device,
1200 gpointer user_data)
1201{
1202 g_autofree gchar *tmp = fwupd_device_to_string (device);
1203 /* TRANSLATORS: this is when a device is hotplugged */
1204 g_print ("%s\n%s", _("Device added:"), tmp);
1205}
1206
1207static void
1208fu_util_device_removed_cb (FwupdClient *client,
1209 FwupdDevice *device,
1210 gpointer user_data)
1211{
1212 g_autofree gchar *tmp = fwupd_device_to_string (device);
1213 /* TRANSLATORS: this is when a device is hotplugged */
1214 g_print ("%s\n%s", _("Device removed:"), tmp);
1215}
1216
1217static void
1218fu_util_device_changed_cb (FwupdClient *client,
1219 FwupdDevice *device,
1220 gpointer user_data)
1221{
1222 g_autofree gchar *tmp = fwupd_device_to_string (device);
1223 /* TRANSLATORS: this is when a device has been updated */
1224 g_print ("%s\n%s", _("Device changed:"), tmp);
1225}
1226
1227static void
1228fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1229{
1230 /* TRANSLATORS: this is when the daemon state changes */
1231 g_print ("%s\n", _("Changed"));
1232}
1233
1234static gboolean
1235fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1236{
1237 g_autoptr(FwupdClient) client = fwupd_client_new ();
1238
1239 /* get all the devices */
1240 if (!fwupd_client_connect (client, priv->cancellable, error))
1241 return FALSE;
1242
1243 /* watch for any hotplugged device */
1244 g_signal_connect (client, "changed",
1245 G_CALLBACK (fu_util_changed_cb), priv);
1246 g_signal_connect (client, "device-added",
1247 G_CALLBACK (fu_util_device_added_cb), priv);
1248 g_signal_connect (client, "device-removed",
1249 G_CALLBACK (fu_util_device_removed_cb), priv);
1250 g_signal_connect (client, "device-changed",
1251 G_CALLBACK (fu_util_device_changed_cb), priv);
1252 g_signal_connect (priv->cancellable, "cancelled",
1253 G_CALLBACK (fu_util_cancelled_cb), priv);
1254 g_main_loop_run (priv->loop);
1255 return TRUE;
1256}
1257
Richard Hughes15684492019-03-15 16:27:50 +00001258static gboolean
1259fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1260{
1261 g_autofree gchar *str = NULL;
1262 g_autoptr(FuDevice) dev = NULL;
1263
1264 /* load engine */
1265 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1266 return FALSE;
1267
1268 /* get device */
1269 if (g_strv_length (values) == 1) {
1270 dev = fu_engine_get_device (priv->engine, values[1], error);
1271 if (dev == NULL)
1272 return FALSE;
1273 } else {
1274 dev = fu_util_prompt_for_device (priv, error);
1275 if (dev == NULL)
1276 return FALSE;
1277 }
1278
1279 /* add checksums */
1280 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1281 return FALSE;
1282
1283 /* show checksums */
1284 str = fu_device_to_string (dev);
1285 g_print ("%s\n", str);
1286 return TRUE;
1287}
1288
Mario Limonciellofe593942019-04-03 13:48:52 -05001289static gboolean
1290fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
1291{
1292 g_autoptr(GPtrArray) devices = NULL;
1293
1294 /* load engine */
1295 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1296 return FALSE;
1297
1298 /* get all devices from the history database */
1299 devices = fu_engine_get_history (priv->engine, error);
1300 if (devices == NULL)
1301 return FALSE;
1302
1303 /* show each device */
1304 for (guint i = 0; i < devices->len; i++) {
1305 FwupdDevice *dev = g_ptr_array_index (devices, i);
1306 g_autofree gchar *str = fwupd_device_to_string (dev);
1307 g_print ("%s\n", str);
1308 }
1309
1310 return TRUE;
1311}
1312
Richard Hughesb5976832018-05-18 10:02:09 +01001313int
1314main (int argc, char *argv[])
1315{
Richard Hughes460226a2018-05-21 20:56:21 +01001316 gboolean allow_older = FALSE;
1317 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001318 gboolean force = FALSE;
1319 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001320 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001321 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001322 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001323 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1324 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00001325 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01001326 g_autofree gchar *cmd_descriptions = NULL;
1327 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001328 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1329 /* TRANSLATORS: command line option */
1330 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001331 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1332 /* TRANSLATORS: command line option */
1333 _("Allow re-installing existing firmware versions"), NULL },
1334 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1335 /* TRANSLATORS: command line option */
1336 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001337 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1338 /* TRANSLATORS: command line option */
1339 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001340 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1341 /* TRANSLATORS: command line option */
1342 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001343 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1344 /* TRANSLATORS: command line option */
1345 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001346 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1347 /* TRANSLATORS: command line option */
1348 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001349 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001350 /* TRANSLATORS: command line option */
1351 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001352 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001353 /* TRANSLATORS: command line option */
1354 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001355 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1356 /* TRANSLATORS: command line option */
1357 _("Save device state into a JSON file between executions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001358 { NULL}
1359 };
1360
1361 setlocale (LC_ALL, "");
1362
1363 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1364 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1365 textdomain (GETTEXT_PACKAGE);
1366
1367 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001368 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001369 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001370 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001371
1372 /* create helper object */
1373 priv->loop = g_main_loop_new (NULL, FALSE);
1374 priv->progressbar = fu_progressbar_new ();
1375
1376 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00001377 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001378 "build-firmware",
1379 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1380 /* TRANSLATORS: command description */
1381 _("Build firmware using a sandbox"),
1382 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00001383 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001384 "smbios-dump",
1385 "FILE",
1386 /* TRANSLATORS: command description */
1387 _("Dump SMBIOS data from a file"),
1388 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00001389 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001390 "get-plugins",
1391 NULL,
1392 /* TRANSLATORS: command description */
1393 _("Get all enabled plugins registered with the system"),
1394 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00001395 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001396 "get-details",
1397 NULL,
1398 /* TRANSLATORS: command description */
1399 _("Gets details about a firmware file"),
1400 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00001401 fu_util_cmd_array_add (cmd_array,
Mario Limonciellofe593942019-04-03 13:48:52 -05001402 "get-history",
1403 NULL,
1404 /* TRANSLATORS: command description */
1405 _("Show history of firmware updates"),
1406 fu_util_get_history);
1407 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001408 "get-updates",
1409 NULL,
1410 /* TRANSLATORS: command description */
1411 _("Gets the list of updates for connected hardware"),
1412 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00001413 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001414 "get-devices",
1415 NULL,
1416 /* TRANSLATORS: command description */
1417 _("Get all devices that support firmware updates"),
1418 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00001419 fu_util_cmd_array_add (cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001420 "get-topology",
1421 NULL,
1422 /* TRANSLATORS: command description */
1423 _("Get all devices according to the system topology"),
1424 fu_util_get_topology);
Richard Hughesc77e1112019-03-01 10:16:26 +00001425 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001426 "watch",
1427 NULL,
1428 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001429 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001430 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00001431 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001432 "install-blob",
1433 "FILENAME DEVICE-ID",
1434 /* TRANSLATORS: command description */
1435 _("Install a firmware blob on a device"),
1436 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00001437 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001438 "install",
1439 "FILE [ID]",
1440 /* TRANSLATORS: command description */
1441 _("Install a firmware file on this hardware"),
1442 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00001443 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001444 "attach",
1445 "DEVICE-ID",
1446 /* TRANSLATORS: command description */
1447 _("Attach to firmware mode"),
1448 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001449 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001450 "detach",
1451 "DEVICE-ID",
1452 /* TRANSLATORS: command description */
1453 _("Detach to bootloader mode"),
1454 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001455 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001456 "activate",
1457 "[DEVICE-ID]",
1458 /* TRANSLATORS: command description */
1459 _("Activate pending devices"),
1460 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00001461 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001462 "hwids",
1463 "[FILE]",
1464 /* TRANSLATORS: command description */
1465 _("Return all the hardware IDs for the machine"),
1466 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00001467 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05001468 "monitor",
1469 NULL,
1470 /* TRANSLATORS: command description */
1471 _("Monitor the daemon for events"),
1472 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00001473 fu_util_cmd_array_add (cmd_array,
Mario Limonciello46aaee82019-01-10 12:58:00 -06001474 "update",
1475 NULL,
1476 /* TRANSLATORS: command description */
1477 _("Update all devices that match local metadata"),
1478 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00001479 fu_util_cmd_array_add (cmd_array,
1480 "self-sign",
1481 "TEXT",
1482 /* TRANSLATORS: command description */
Richard Hughes12a021d2019-03-27 09:23:24 +00001483 C_("command-description",
1484 "Sign data using the client certificate"),
Richard Hughes3d607622019-03-07 16:59:27 +00001485 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00001486 fu_util_cmd_array_add (cmd_array,
1487 "verify-update",
1488 "[DEVICE_ID]",
1489 /* TRANSLATORS: command description */
1490 _("Update the stored metadata with current contents"),
1491 fu_util_verify_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001492
1493 /* do stuff on ctrl+c */
1494 priv->cancellable = g_cancellable_new ();
1495 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1496 SIGINT, fu_util_sigint_cb,
1497 priv, NULL);
1498 g_signal_connect (priv->cancellable, "cancelled",
1499 G_CALLBACK (fu_util_cancelled_cb), priv);
1500
1501 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00001502 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001503
Mario Limonciello3f243a92019-01-21 22:05:23 -06001504 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001505 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001506 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001507 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1508 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001509
Richard Hughesb5976832018-05-18 10:02:09 +01001510 /* get a list of the commands */
1511 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00001512 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001513 g_option_context_set_summary (priv->context, cmd_descriptions);
1514 g_option_context_set_description (priv->context,
1515 "This tool allows an administrator to use the fwupd plugins "
1516 "without being installed on the host system.");
1517
1518 /* TRANSLATORS: program name */
1519 g_set_application_name (_("Firmware Utility"));
1520 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001521 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001522 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1523 if (!ret) {
1524 /* TRANSLATORS: the user didn't read the man page */
1525 g_print ("%s: %s\n", _("Failed to parse arguments"),
1526 error->message);
1527 return EXIT_FAILURE;
1528 }
1529
Richard Hughes460226a2018-05-21 20:56:21 +01001530 /* set flags */
Richard Hughes460226a2018-05-21 20:56:21 +01001531 if (allow_reinstall)
1532 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1533 if (allow_older)
1534 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1535 if (force)
1536 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1537
Richard Hughes98ca9932018-05-18 10:24:07 +01001538 /* load engine */
1539 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1540 g_signal_connect (priv->engine, "device-added",
1541 G_CALLBACK (fu_main_engine_device_added_cb),
1542 priv);
1543 g_signal_connect (priv->engine, "device-removed",
1544 G_CALLBACK (fu_main_engine_device_removed_cb),
1545 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001546 g_signal_connect (priv->engine, "status-changed",
1547 G_CALLBACK (fu_main_engine_status_changed_cb),
1548 priv);
1549 g_signal_connect (priv->engine, "percentage-changed",
1550 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1551 priv);
1552
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001553 /* just show versions and exit */
1554 if (version) {
1555 g_autofree gchar *version_str = fu_util_get_versions ();
1556 g_print ("%s\n", version_str);
1557 return EXIT_SUCCESS;
1558 }
1559
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001560 /* any plugin whitelist specified */
1561 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1562 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1563
Richard Hughesb5976832018-05-18 10:02:09 +01001564 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00001565 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01001566 if (!ret) {
1567 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1568 g_autofree gchar *tmp = NULL;
1569 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1570 g_print ("%s\n\n%s", error->message, tmp);
1571 return EXIT_FAILURE;
1572 }
1573 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1574 g_print ("%s\n", error->message);
1575 return EXIT_NOTHING_TO_DO;
1576 }
1577 g_print ("%s\n", error->message);
1578 return EXIT_FAILURE;
1579 }
1580
1581 /* success */
1582 return EXIT_SUCCESS;
1583}