blob: 8cf93c2c552f29548ef966e6780e0e205b07f4b6 [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>
Richard Hughes9e5675e2019-11-22 09:35:03 +000014#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +010015#include <glib-unix.h>
Richard Hughes9e5675e2019-11-22 09:35:03 +000016#endif
Richard Hughes7d82a092019-11-22 09:42:31 +000017#include <fcntl.h>
Richard Hughesb5976832018-05-18 10:02:09 +010018#include <locale.h>
19#include <stdlib.h>
20#include <unistd.h>
Richard Hughes3d178be2018-08-30 11:14:24 +010021#include <libsoup/soup.h>
Richard Hughesd5aab652020-02-25 12:47:50 +000022#include <jcat.h>
Richard Hughesb5976832018-05-18 10:02:09 +010023
Mario Limonciello7a3df4b2019-01-31 10:27:22 -060024#include "fu-device-private.h"
Richard Hughes98ca9932018-05-18 10:24:07 +010025#include "fu-engine.h"
Mario Limonciello96a0dd52019-02-25 13:50:03 -060026#include "fu-history.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010027#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010028#include "fu-progressbar.h"
Richard Hughesf58ac732020-05-12 15:23:44 +010029#include "fu-security-attrs-private.h"
Mario Limonciello6b0e6632019-11-22 13:04:32 -060030#include "fu-smbios-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010031#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050032#include "fu-debug.h"
Mario Limonciello1e35e4c2019-01-28 11:13:02 -060033#include "fwupd-common-private.h"
Mario Limonciello3143bad2019-02-27 07:31:00 -060034#include "fwupd-device-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010035
Richard Hughes3d005222019-05-17 14:02:41 +010036#ifdef HAVE_SYSTEMD
37#include "fu-systemd.h"
38#endif
39
Richard Hughesb5976832018-05-18 10:02:09 +010040/* custom return code */
41#define EXIT_NOTHING_TO_DO 2
42
Mario Limonciello3f243a92019-01-21 22:05:23 -060043typedef enum {
44 FU_UTIL_OPERATION_UNKNOWN,
45 FU_UTIL_OPERATION_UPDATE,
46 FU_UTIL_OPERATION_INSTALL,
Richard Hughesa58510b2019-10-30 10:03:12 +000047 FU_UTIL_OPERATION_READ,
Mario Limonciello3f243a92019-01-21 22:05:23 -060048 FU_UTIL_OPERATION_LAST
49} FuUtilOperation;
50
Richard Hughesc77e1112019-03-01 10:16:26 +000051struct FuUtilPrivate {
Richard Hughesb5976832018-05-18 10:02:09 +010052 GCancellable *cancellable;
53 GMainLoop *loop;
54 GOptionContext *context;
Richard Hughes98ca9932018-05-18 10:24:07 +010055 FuEngine *engine;
Richard Hughesdf89cd52020-06-26 20:25:18 +010056 FuEngineRequest *request;
Richard Hughesb5976832018-05-18 10:02:09 +010057 FuProgressbar *progressbar;
Mario Limonciello3f243a92019-01-21 22:05:23 -060058 gboolean no_reboot_check;
Mario Limonciello98b95162019-10-30 09:20:43 -050059 gboolean no_safety_check;
Mario Limonciello53ce25d2019-02-01 16:09:03 +000060 gboolean prepare_blob;
61 gboolean cleanup_blob;
Mario Limonciello3143bad2019-02-27 07:31:00 -060062 gboolean enable_json_state;
Richard Hughes460226a2018-05-21 20:56:21 +010063 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050064 gboolean show_all_devices;
Richard Hughes0e46b222019-09-05 12:13:35 +010065 gboolean disable_ssl_strict;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050066 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060067 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050068 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060069 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060070 FwupdDeviceFlags completion_flags;
Richard Hughes747f5702019-08-06 14:27:26 +010071 FwupdDeviceFlags filter_include;
72 FwupdDeviceFlags filter_exclude;
Richard Hughesc77e1112019-03-01 10:16:26 +000073};
Richard Hughesb5976832018-05-18 10:02:09 +010074
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050075static gboolean
Mario Limonciello3143bad2019-02-27 07:31:00 -060076fu_util_save_current_state (FuUtilPrivate *priv, GError **error)
77{
78 g_autoptr(JsonBuilder) builder = NULL;
79 g_autoptr(JsonGenerator) json_generator = NULL;
80 g_autoptr(JsonNode) json_root = NULL;
81 g_autoptr(GPtrArray) devices = NULL;
82 g_autofree gchar *state = NULL;
83 g_autofree gchar *dirname = NULL;
84 g_autofree gchar *filename = NULL;
85
86 if (!priv->enable_json_state)
87 return TRUE;
88
89 devices = fu_engine_get_devices (priv->engine, error);
90 if (devices == NULL)
91 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +000092 fwupd_device_array_ensure_parents (devices);
Mario Limonciello3143bad2019-02-27 07:31:00 -060093
94 /* create header */
95 builder = json_builder_new ();
96 json_builder_begin_object (builder);
97
98 /* add each device */
99 json_builder_set_member_name (builder, "Devices");
100 json_builder_begin_array (builder);
101 for (guint i = 0; i < devices->len; i++) {
102 FwupdDevice *dev = g_ptr_array_index (devices, i);
103 json_builder_begin_object (builder);
104 fwupd_device_to_json (dev, builder);
105 json_builder_end_object (builder);
106 }
107 json_builder_end_array (builder);
108 json_builder_end_object (builder);
109
110 /* export as a string */
111 json_root = json_builder_get_root (builder);
112 json_generator = json_generator_new ();
113 json_generator_set_pretty (json_generator, TRUE);
114 json_generator_set_root (json_generator, json_root);
115 state = json_generator_to_data (json_generator, NULL);
116 if (state == NULL)
117 return FALSE;
118 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
119 filename = g_build_filename (dirname, "state.json", NULL);
120 return g_file_set_contents (filename, state, -1, error);
121}
122
123static gboolean
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000124fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error)
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600125{
126 g_autoptr(GError) error_local = NULL;
127
Richard Hughesd92ccca2019-05-20 11:28:31 +0100128#ifdef HAVE_SYSTEMD
Richard Hughes3d005222019-05-17 14:02:41 +0100129 if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local))
Mario Limonciello8692d012019-10-12 18:01:55 -0500130 g_debug ("Failed to stop daemon: %s", error_local->message);
Richard Hughesd92ccca2019-05-20 11:28:31 +0100131#endif
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000132 if (!fu_engine_load (priv->engine, flags, error))
Richard Hughesf425d292019-01-18 17:57:39 +0000133 return FALSE;
134 if (fu_engine_get_tainted (priv->engine)) {
135 g_printerr ("WARNING: This tool has loaded 3rd party code and "
136 "is no longer supported by the upstream developers!\n");
137 }
138 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500139}
140
Richard Hughesb5976832018-05-18 10:02:09 +0100141static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500142fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
143{
144 g_autofree gchar *path = g_path_get_dirname (value);
145 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
146 g_prefix_error (error,
147 "Unable to access %s. You may need to copy %s to %s: ",
148 path, value, g_getenv ("HOME"));
149 }
150}
151
152static void
Richard Hughesb5976832018-05-18 10:02:09 +0100153fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
154{
155 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
156 /* TRANSLATORS: this is when a device ctrl+c's a watch */
157 g_print ("%s\n", _("Cancelled"));
158 g_main_loop_quit (priv->loop);
159}
160
161static gboolean
162fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
163{
164 g_autofree gchar *tmp = NULL;
165 g_autoptr(FuSmbios) smbios = NULL;
166 if (g_strv_length (values) < 1) {
167 g_set_error_literal (error,
168 FWUPD_ERROR,
169 FWUPD_ERROR_INVALID_ARGS,
170 "Invalid arguments");
171 return FALSE;
172 }
173 smbios = fu_smbios_new ();
174 if (!fu_smbios_setup_from_file (smbios, values[0], error))
175 return FALSE;
176 tmp = fu_smbios_to_string (smbios);
177 g_print ("%s\n", tmp);
178 return TRUE;
179}
180
Richard Hughes9e5675e2019-11-22 09:35:03 +0000181#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +0100182static gboolean
183fu_util_sigint_cb (gpointer user_data)
184{
185 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
186 g_debug ("Handling SIGINT");
187 g_cancellable_cancel (priv->cancellable);
188 return FALSE;
189}
Richard Hughes9e5675e2019-11-22 09:35:03 +0000190#endif
Richard Hughesb5976832018-05-18 10:02:09 +0100191
192static void
193fu_util_private_free (FuUtilPrivate *priv)
194{
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500195 if (priv->current_device != NULL)
196 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100197 if (priv->engine != NULL)
198 g_object_unref (priv->engine);
Richard Hughesdf89cd52020-06-26 20:25:18 +0100199 if (priv->request != NULL)
200 g_object_unref (priv->request);
Richard Hughesb5976832018-05-18 10:02:09 +0100201 if (priv->loop != NULL)
202 g_main_loop_unref (priv->loop);
203 if (priv->cancellable != NULL)
204 g_object_unref (priv->cancellable);
205 if (priv->progressbar != NULL)
206 g_object_unref (priv->progressbar);
207 if (priv->context != NULL)
208 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600209 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100210 g_free (priv);
211}
212
213#pragma clang diagnostic push
214#pragma clang diagnostic ignored "-Wunused-function"
215G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
216#pragma clang diagnostic pop
217
Richard Hughes98ca9932018-05-18 10:24:07 +0100218
219static void
220fu_main_engine_device_added_cb (FuEngine *engine,
221 FuDevice *device,
222 FuUtilPrivate *priv)
223{
224 g_autofree gchar *tmp = fu_device_to_string (device);
225 g_debug ("ADDED:\n%s", tmp);
226}
227
228static void
229fu_main_engine_device_removed_cb (FuEngine *engine,
230 FuDevice *device,
231 FuUtilPrivate *priv)
232{
233 g_autofree gchar *tmp = fu_device_to_string (device);
234 g_debug ("REMOVED:\n%s", tmp);
235}
236
237static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100238fu_main_engine_status_changed_cb (FuEngine *engine,
239 FwupdStatus status,
240 FuUtilPrivate *priv)
241{
242 fu_progressbar_update (priv->progressbar, status, 0);
243}
244
245static void
246fu_main_engine_percentage_changed_cb (FuEngine *engine,
247 guint percentage,
248 FuUtilPrivate *priv)
249{
250 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
251}
252
253static gboolean
254fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
255{
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000256 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100257 return FALSE;
258 g_main_loop_run (priv->loop);
259 return TRUE;
260}
261
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100262static gint
263fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
264{
265 return fu_plugin_name_compare (*item1, *item2);
266}
267
268static gboolean
269fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
270{
271 GPtrArray *plugins;
272 guint cnt = 0;
273
274 /* load engine */
275 if (!fu_engine_load_plugins (priv->engine, error))
276 return FALSE;
277
278 /* print */
279 plugins = fu_engine_get_plugins (priv->engine);
280 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
281 for (guint i = 0; i < plugins->len; i++) {
282 FuPlugin *plugin = g_ptr_array_index (plugins, i);
283 if (!fu_plugin_get_enabled (plugin))
284 continue;
285 g_print ("%s\n", fu_plugin_get_name (plugin));
286 cnt++;
287 }
288 if (cnt == 0) {
289 /* TRANSLATORS: nothing found */
290 g_print ("%s\n", _("No plugins found"));
291 return TRUE;
292 }
293
294 return TRUE;
295}
296
Richard Hughes98ca9932018-05-18 10:24:07 +0100297static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100298fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev)
299{
300 if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) {
301 if (!fwupd_device_has_flag (dev, priv->filter_include))
302 return FALSE;
303 }
304 if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) {
305 if (fwupd_device_has_flag (dev, priv->filter_exclude))
306 return FALSE;
307 }
308 return TRUE;
309}
310
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500311static gchar *
312fu_util_get_tree_title (FuUtilPrivate *priv)
313{
314 return g_strdup (fu_engine_get_host_product (priv->engine));
315}
316
Richard Hughes747f5702019-08-06 14:27:26 +0100317static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600318fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
319{
320 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500321 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600322 g_autofree gchar *title = NULL;
Mario Limoncielloeb7be162020-07-01 15:38:47 -0500323 gboolean no_updates_header = FALSE;
324 gboolean latest_header = FALSE;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600325
326 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000327 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600328 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600329 title = fu_util_get_tree_title (priv);
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600330
331 /* get devices from daemon */
332 devices = fu_engine_get_devices (priv->engine, error);
333 if (devices == NULL)
334 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +0000335 fwupd_device_array_ensure_parents (devices);
Mario Limoncielloeb7be162020-07-01 15:38:47 -0500336 g_ptr_array_sort (devices, fu_util_sort_devices_by_flags_cb);
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600337 for (guint i = 0; i < devices->len; i++) {
338 FwupdDevice *dev = g_ptr_array_index (devices, i);
339 g_autoptr(GPtrArray) rels = NULL;
340 g_autoptr(GError) error_local = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500341 GNode *child;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600342
Richard Hughes747f5702019-08-06 14:27:26 +0100343 /* not going to have results, so save a engine round-trip */
Mario Limonciello27164b72020-02-17 23:19:36 -0600344 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE))
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600345 continue;
Mario Limonciello27164b72020-02-17 23:19:36 -0600346 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -0500347 if (!no_updates_header) {
348 /* TRANSLATORS: message letting the user know no device upgrade available due to missing on LVFS */
349 g_printerr ("%s\n", _("Devices with no available firmware updates: "));
350 no_updates_header = TRUE;
351 }
352 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -0600353 continue;
354 }
Richard Hughes747f5702019-08-06 14:27:26 +0100355 if (!fu_util_filter_device (priv, dev))
356 continue;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600357
358 /* get the releases for this device and filter for validity */
359 rels = fu_engine_get_upgrades (priv->engine,
Richard Hughesdf89cd52020-06-26 20:25:18 +0100360 priv->request,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600361 fwupd_device_get_id (dev),
362 &error_local);
363 if (rels == NULL) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -0500364 if (!latest_header) {
365 /* TRANSLATORS: message letting the user know no device upgrade available */
366 g_printerr ("%s\n", _("Devices with the latest available firmware version:"));
367 latest_header = TRUE;
368 }
369 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -0600370 /* discard the actual reason from user, but leave for debugging */
371 g_debug ("%s", error_local->message);
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600372 continue;
373 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500374 child = g_node_append_data (root, dev);
375
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600376 for (guint j = 0; j < rels->len; j++) {
377 FwupdRelease *rel = g_ptr_array_index (rels, j);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500378 g_node_append_data (child, g_object_ref (rel));
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600379 }
380 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500381 if (g_node_n_nodes (root, G_TRAVERSE_ALL) > 1)
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500382 fu_util_print_tree (root, title);
Mario Limonciello3143bad2019-02-27 07:31:00 -0600383 /* save the device state for other applications to see */
384 if (!fu_util_save_current_state (priv, error))
385 return FALSE;
386
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600387 /* success */
388 return TRUE;
389}
390
391static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500392fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
393{
394 g_autoptr(GPtrArray) array = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500395 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600396 g_autofree gchar *title = NULL;
Mario Limonciello716ab272018-05-29 12:34:37 -0500397 gint fd;
398
399 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000400 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500401 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600402 title = fu_util_get_tree_title (priv);
Mario Limonciello716ab272018-05-29 12:34:37 -0500403
404 /* check args */
405 if (g_strv_length (values) != 1) {
406 g_set_error_literal (error,
407 FWUPD_ERROR,
408 FWUPD_ERROR_INVALID_ARGS,
409 "Invalid arguments");
410 return FALSE;
411 }
412
Mario Limonciellodc0608d2020-02-25 11:25:44 -0600413 /* implied, important for get-details on a device not in your system */
414 priv->show_all_devices = TRUE;
415
Mario Limonciello716ab272018-05-29 12:34:37 -0500416 /* open file */
417 fd = open (values[0], O_RDONLY);
418 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500419 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500420 g_set_error (error,
421 FWUPD_ERROR,
422 FWUPD_ERROR_INVALID_FILE,
423 "failed to open %s",
424 values[0]);
425 return FALSE;
426 }
Richard Hughesdf89cd52020-06-26 20:25:18 +0100427 array = fu_engine_get_details (priv->engine, priv->request, fd, error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500428 close (fd);
429
430 if (array == NULL)
431 return FALSE;
432 for (guint i = 0; i < array->len; i++) {
433 FwupdDevice *dev = g_ptr_array_index (array, i);
Mario Limonciello984e29c2020-02-25 11:30:28 -0600434 FwupdRelease *rel;
435 GNode *child;
Richard Hughes747f5702019-08-06 14:27:26 +0100436 if (!fu_util_filter_device (priv, dev))
437 continue;
Mario Limonciello984e29c2020-02-25 11:30:28 -0600438 child = g_node_append_data (root, dev);
439 rel = fwupd_device_get_release_default (dev);
440 if (rel != NULL)
441 g_node_append_data (child, rel);
442
Mario Limonciello716ab272018-05-29 12:34:37 -0500443 }
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500444 fu_util_print_tree (root, title);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500445
Mario Limonciello716ab272018-05-29 12:34:37 -0500446 return TRUE;
447}
448
449static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100450fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error)
451{
452 g_autoptr(GString) str = g_string_new (NULL);
453
454 for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) {
455 const gchar *tmp = fwupd_device_flag_to_string (i);
456 if (tmp == NULL)
457 break;
458 if (i != FWUPD_DEVICE_FLAG_INTERNAL)
459 g_string_append (str, " ");
460 g_string_append (str, tmp);
461 g_string_append (str, " ~");
462 g_string_append (str, tmp);
463 }
464 g_print ("%s\n", str->str);
465
466 return TRUE;
467}
468
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500469static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100470fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500471{
472 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100473 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Richard Hughes747f5702019-08-06 14:27:26 +0100474 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp)))
475 continue;
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500476 if (!priv->show_all_devices &&
477 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500478 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100479 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500480 GNode *child = g_node_append_data (root, dev_tmp);
481 fu_util_build_device_tree (priv, child, devs, dev_tmp);
482 }
483 }
484}
485
486static gboolean
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100487fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500488{
489 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600490 g_autofree gchar *title = NULL;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500491 g_autoptr(GPtrArray) devs = NULL;
492
493 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000494 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500495 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -0600496 title = fu_util_get_tree_title (priv);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500497
498 /* print */
499 devs = fu_engine_get_devices (priv->engine, error);
500 if (devs == NULL)
501 return FALSE;
502
503 /* print */
504 if (devs->len == 0) {
505 /* TRANSLATORS: nothing attached that can be upgraded */
506 g_print ("%s\n", _("No hardware detected with firmware update capability"));
507 return TRUE;
508 }
Richard Hughes0ef47202020-01-06 13:59:09 +0000509 fwupd_device_array_ensure_parents (devs);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500510 fu_util_build_device_tree (priv, root, devs, NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500511 fu_util_print_tree (root, title);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500512
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100513 /* save the device state for other applications to see */
514 return fu_util_save_current_state (priv, error);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500515}
516
Richard Hughes98ca9932018-05-18 10:24:07 +0100517static FuDevice *
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100518fu_util_prompt_for_device (FuUtilPrivate *priv, GPtrArray *devices_opt, GError **error)
Richard Hughes98ca9932018-05-18 10:24:07 +0100519{
520 FuDevice *dev;
521 guint idx;
522 g_autoptr(GPtrArray) devices = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +0100523 g_autoptr(GPtrArray) devices_filtered = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100524
525 /* get devices from daemon */
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100526 if (devices_opt != NULL) {
527 devices = g_ptr_array_ref (devices_opt);
528 } else {
529 devices = fu_engine_get_devices (priv->engine, error);
530 if (devices == NULL)
531 return NULL;
532 }
Richard Hughes0ef47202020-01-06 13:59:09 +0000533 fwupd_device_array_ensure_parents (devices);
Richard Hughes98ca9932018-05-18 10:24:07 +0100534
Richard Hughes747f5702019-08-06 14:27:26 +0100535 /* filter results */
536 devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
537 for (guint i = 0; i < devices->len; i++) {
538 dev = g_ptr_array_index (devices, i);
539 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev)))
540 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100541 g_ptr_array_add (devices_filtered, g_object_ref (dev));
542 }
543
544 /* nothing */
545 if (devices_filtered->len == 0) {
546 g_set_error_literal (error,
547 FWUPD_ERROR,
548 FWUPD_ERROR_NOTHING_TO_DO,
549 "No supported devices");
550 return NULL;
551 }
552
Richard Hughes98ca9932018-05-18 10:24:07 +0100553 /* exactly one */
Richard Hughes747f5702019-08-06 14:27:26 +0100554 if (devices_filtered->len == 1) {
555 dev = g_ptr_array_index (devices_filtered, 0);
Mario Limoncielloa61b4d82019-10-22 08:37:45 -0500556 /* TRANSLATORS: Device has been chosen by the daemon for the user */
557 g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev));
Richard Hughes98ca9932018-05-18 10:24:07 +0100558 return g_object_ref (dev);
559 }
560
561 /* TRANSLATORS: get interactive prompt */
562 g_print ("%s\n", _("Choose a device:"));
563 /* TRANSLATORS: this is to abort the interactive prompt */
564 g_print ("0.\t%s\n", _("Cancel"));
Richard Hughes747f5702019-08-06 14:27:26 +0100565 for (guint i = 0; i < devices_filtered->len; i++) {
566 dev = g_ptr_array_index (devices_filtered, i);
Richard Hughes98ca9932018-05-18 10:24:07 +0100567 g_print ("%u.\t%s (%s)\n",
568 i + 1,
569 fu_device_get_id (dev),
570 fu_device_get_name (dev));
571 }
Richard Hughes747f5702019-08-06 14:27:26 +0100572 idx = fu_util_prompt_for_number (devices_filtered->len);
Richard Hughes98ca9932018-05-18 10:24:07 +0100573 if (idx == 0) {
574 g_set_error_literal (error,
575 FWUPD_ERROR,
576 FWUPD_ERROR_NOTHING_TO_DO,
577 "Request canceled");
578 return NULL;
579 }
Richard Hughes747f5702019-08-06 14:27:26 +0100580 dev = g_ptr_array_index (devices_filtered, idx - 1);
Richard Hughes98ca9932018-05-18 10:24:07 +0100581 return g_object_ref (dev);
582}
583
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500584static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600585fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500586 FwupdDevice *device,
587 FuUtilPrivate *priv)
588{
589 g_autofree gchar *str = NULL;
590
Richard Hughes809abea2019-03-23 11:06:18 +0000591 /* allowed to set whenever the device has changed */
592 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
593 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
594 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
595 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
596
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500597 /* same as last time, so ignore */
598 if (priv->current_device != NULL &&
599 fwupd_device_compare (priv->current_device, device) == 0)
600 return;
601
Richard Hughesee562b52020-04-07 14:32:52 +0100602 /* ignore indirect devices that might have changed */
603 if (fwupd_device_get_status (device) == FWUPD_STATUS_IDLE ||
604 fwupd_device_get_status (device) == FWUPD_STATUS_UNKNOWN) {
605 g_debug ("ignoring %s with status %s",
606 fwupd_device_get_name (device),
607 fwupd_status_to_string (fwupd_device_get_status (device)));
608 return;
609 }
610
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500611 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600612 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
613 /* TRANSLATORS: %1 is a device name */
614 str = g_strdup_printf (_("Updating %s…"),
615 fwupd_device_get_name (device));
616 fu_progressbar_set_title (priv->progressbar, str);
617 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
618 /* TRANSLATORS: %1 is a device name */
619 str = g_strdup_printf (_("Installing on %s…"),
620 fwupd_device_get_name (device));
621 fu_progressbar_set_title (priv->progressbar, str);
Richard Hughesa58510b2019-10-30 10:03:12 +0000622 } else if (priv->current_operation == FU_UTIL_OPERATION_READ) {
623 /* TRANSLATORS: %1 is a device name */
624 str = g_strdup_printf (_("Reading from %s…"),
625 fwupd_device_get_name (device));
626 fu_progressbar_set_title (priv->progressbar, str);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600627 } else {
628 g_warning ("no FuUtilOperation set");
629 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500630 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600631
Mario Limonciello32241f42019-01-24 10:12:41 -0600632 if (priv->current_message == NULL) {
633 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
634 if (tmp != NULL)
635 priv->current_message = g_strdup (tmp);
636 }
637}
638
639static void
640fu_util_display_current_message (FuUtilPrivate *priv)
641{
642 if (priv->current_message == NULL)
643 return;
644 g_print ("%s\n", priv->current_message);
645 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500646}
647
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100648static FuDevice *
649fu_util_get_device (FuUtilPrivate *priv, const gchar *id, GError **error)
650{
651 if (fwupd_guid_is_valid (id)) {
652 g_autoptr(GPtrArray) devices = NULL;
653 devices = fu_engine_get_devices_by_guid (priv->engine, id, error);
654 if (devices == NULL)
655 return NULL;
656 return fu_util_prompt_for_device (priv, devices, error);
657 }
Mario Limoncielloc6009f52020-04-20 15:40:11 -0500658
659 /* did this look like a GUID? */
660 for (guint i = 0; id[i] != '\0'; i++) {
661 if (id[i] == '-') {
662 g_set_error_literal (error,
663 FWUPD_ERROR,
664 FWUPD_ERROR_INVALID_ARGS,
665 "Invalid arguments");
Richard Hughes9c09e2c2020-04-22 10:52:42 +0100666 return NULL;
Mario Limoncielloc6009f52020-04-20 15:40:11 -0500667 }
668 }
Mario Limoncielloaac9c742020-04-20 13:38:44 -0500669 return fu_engine_get_device (priv->engine, id, error);
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100670}
671
Richard Hughes98ca9932018-05-18 10:24:07 +0100672static gboolean
673fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
674{
675 g_autoptr(FuDevice) device = NULL;
676 g_autoptr(GBytes) blob_fw = NULL;
677
678 /* invalid args */
679 if (g_strv_length (values) == 0) {
680 g_set_error_literal (error,
681 FWUPD_ERROR,
682 FWUPD_ERROR_INVALID_ARGS,
683 "Invalid arguments");
684 return FALSE;
685 }
686
687 /* parse blob */
688 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500689 if (blob_fw == NULL) {
690 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100691 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500692 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100693
694 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000695 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100696 return FALSE;
697
698 /* get device */
699 if (g_strv_length (values) >= 2) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100700 device = fu_util_get_device (priv, values[1], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100701 if (device == NULL)
702 return FALSE;
703 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100704 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100705 if (device == NULL)
706 return FALSE;
707 }
708
Mario Limonciello3f243a92019-01-21 22:05:23 -0600709 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500710 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600711 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500712
Richard Hughes98ca9932018-05-18 10:24:07 +0100713 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000714 if (priv->prepare_blob) {
715 g_autoptr(GPtrArray) devices = NULL;
716 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
717 g_ptr_array_add (devices, g_object_ref (device));
718 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
719 g_prefix_error (error, "failed to prepare composite action: ");
720 return FALSE;
721 }
722 }
Richard Hughesf1fa73e2020-04-06 16:19:04 +0100723 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
Richard Hughes84af6e72019-02-01 18:19:41 +0000724 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600725 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000726 if (priv->cleanup_blob) {
727 g_autoptr(FuDevice) device_new = NULL;
728 g_autoptr(GError) error_local = NULL;
729
730 /* get the possibly new device from the old ID */
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100731 device_new = fu_util_get_device (priv,
732 fu_device_get_id (device),
733 &error_local);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000734 if (device_new == NULL) {
735 g_debug ("failed to find new device: %s",
736 error_local->message);
737 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600738 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000739 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
740 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
741 g_prefix_error (error, "failed to cleanup composite action: ");
742 return FALSE;
743 }
744 }
745 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600746
Mario Limonciello32241f42019-01-24 10:12:41 -0600747 fu_util_display_current_message (priv);
748
Mario Limonciello3f243a92019-01-21 22:05:23 -0600749 /* success */
750 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100751}
752
Richard Hughesa58510b2019-10-30 10:03:12 +0000753static gboolean
754fu_util_firmware_read (FuUtilPrivate *priv, gchar **values, GError **error)
755{
756 g_autoptr(FuDevice) device = NULL;
757 g_autoptr(GBytes) blob_empty = g_bytes_new (NULL, 0);
758 g_autoptr(GBytes) blob_fw = NULL;
759
760 /* invalid args */
761 if (g_strv_length (values) == 0) {
762 g_set_error_literal (error,
763 FWUPD_ERROR,
764 FWUPD_ERROR_INVALID_ARGS,
765 "Invalid arguments");
766 return FALSE;
767 }
768
769 /* file already exists */
770 if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0 &&
771 g_file_test (values[0], G_FILE_TEST_EXISTS)) {
772 g_set_error_literal (error,
773 FWUPD_ERROR,
774 FWUPD_ERROR_INVALID_ARGS,
775 "Filename already exists");
776 return FALSE;
777 }
778
Richard Hughes02792c02019-11-01 14:21:20 +0000779 /* write a zero length file to ensure the destination is writable to
Richard Hughesa58510b2019-10-30 10:03:12 +0000780 * avoid failing at the end of a potentially lengthy operation */
781 if (!fu_common_set_contents_bytes (values[0], blob_empty, error))
782 return FALSE;
783
784 /* load engine */
785 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
786 return FALSE;
787
788 /* get device */
789 if (g_strv_length (values) >= 2) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100790 device = fu_util_get_device (priv, values[1], error);
Richard Hughesa58510b2019-10-30 10:03:12 +0000791 if (device == NULL)
792 return FALSE;
793 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100794 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughesa58510b2019-10-30 10:03:12 +0000795 if (device == NULL)
796 return FALSE;
797 }
798 priv->current_operation = FU_UTIL_OPERATION_READ;
799 g_signal_connect (priv->engine, "device-changed",
800 G_CALLBACK (fu_util_update_device_changed_cb), priv);
801
802 /* dump firmware */
803 blob_fw = fu_engine_firmware_read (priv->engine, device, priv->flags, error);
804 if (blob_fw == NULL)
805 return FALSE;
806 return fu_common_set_contents_bytes (values[0], blob_fw, error);
807}
808
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100809static gint
810fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
811{
812 FuInstallTask *task1 = *((FuInstallTask **) a);
813 FuInstallTask *task2 = *((FuInstallTask **) b);
814 return fu_install_task_compare (task1, task2);
815}
816
817static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100818fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
819{
Filipe Laínse0914272019-09-20 10:04:43 +0100820 const gchar *argv[][5] = { { "wget", uri, "-O", fn, NULL },
Richard Hughes3d178be2018-08-30 11:14:24 +0100821 { "curl", uri, "--output", fn, NULL },
822 { NULL } };
823 for (guint i = 0; argv[i][0] != NULL; i++) {
824 g_autoptr(GError) error_local = NULL;
825 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
826 g_debug ("%s", error_local->message);
827 continue;
828 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000829 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100830 }
831 g_set_error_literal (error,
832 FWUPD_ERROR,
833 FWUPD_ERROR_NOT_FOUND,
834 "no supported out-of-process downloaders found");
835 return FALSE;
836}
837
838static gchar *
839fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
840{
841 g_autofree gchar *filename = NULL;
842 g_autoptr(SoupURI) uri = NULL;
843
844 /* a local file */
845 uri = soup_uri_new (perhapsfn);
Richard Hughes45a00732019-11-22 16:57:14 +0000846 if (g_file_test (perhapsfn, G_FILE_TEST_EXISTS))
847 return g_strdup (perhapsfn);
Richard Hughes3d178be2018-08-30 11:14:24 +0100848 if (uri == NULL)
849 return g_strdup (perhapsfn);
850
851 /* download the firmware to a cachedir */
852 filename = fu_util_get_user_cache_path (perhapsfn);
853 if (!fu_common_mkdir_parent (filename, error))
854 return NULL;
855 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
856 return NULL;
857 return g_steal_pointer (&filename);
858}
859
860static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100861fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
862{
Richard Hughes3d178be2018-08-30 11:14:24 +0100863 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100864 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100865 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100866 g_autoptr(GPtrArray) devices_possible = NULL;
867 g_autoptr(GPtrArray) errors = NULL;
868 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100869 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100870
Mario Limonciello8949e892018-05-25 08:03:06 -0500871 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000872 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500873 return FALSE;
874
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100875 /* handle both forms */
876 if (g_strv_length (values) == 1) {
877 devices_possible = fu_engine_get_devices (priv->engine, error);
878 if (devices_possible == NULL)
879 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +0000880 fwupd_device_array_ensure_parents (devices_possible);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100881 } else if (g_strv_length (values) == 2) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100882 FuDevice *device = fu_util_get_device (priv, values[1], error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100883 if (device == NULL)
884 return FALSE;
885 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
886 g_ptr_array_add (devices_possible, device);
887 } else {
888 g_set_error_literal (error,
889 FWUPD_ERROR,
890 FWUPD_ERROR_INVALID_ARGS,
891 "Invalid arguments");
892 return FALSE;
893 }
894
Richard Hughes3d178be2018-08-30 11:14:24 +0100895 /* download if required */
896 filename = fu_util_download_if_required (priv, values[0], error);
897 if (filename == NULL)
898 return FALSE;
899
Richard Hughes481aa2a2018-09-18 20:51:46 +0100900 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100901 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500902 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100903 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100904 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500905 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100906 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
907 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100908 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600909 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100910 if (components == NULL)
911 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100912
Richard Hughes481aa2a2018-09-18 20:51:46 +0100913 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100914 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
915 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100916 for (guint i = 0; i < components->len; i++) {
917 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100918
919 /* do any devices pass the requirements */
920 for (guint j = 0; j < devices_possible->len; j++) {
921 FuDevice *device = g_ptr_array_index (devices_possible, j);
922 g_autoptr(FuInstallTask) task = NULL;
923 g_autoptr(GError) error_local = NULL;
924
925 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100926 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100927 if (!fu_engine_check_requirements (priv->engine,
Richard Hughesdf89cd52020-06-26 20:25:18 +0100928 priv->request,
929 task,
930 priv->flags | FWUPD_INSTALL_FLAG_FORCE,
Mario Limonciello537da0e2020-03-09 15:38:17 -0500931 &error_local)) {
932 g_debug ("first pass requirement on %s:%s failed: %s",
933 fu_device_get_id (device),
934 xb_node_query_text (component, "id", NULL),
935 error_local->message);
936 g_ptr_array_add (errors, g_steal_pointer (&error_local));
937 continue;
938 }
939
940 /* make a second pass using possibly updated version format now */
941 fu_engine_md_refresh_device_from_component (priv->engine, device, component);
942 if (!fu_engine_check_requirements (priv->engine,
Richard Hughesdf89cd52020-06-26 20:25:18 +0100943 priv->request,
944 task,
945 priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100946 &error_local)) {
Mario Limonciello537da0e2020-03-09 15:38:17 -0500947 g_debug ("second pass requirement on %s:%s failed: %s",
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100948 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100949 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100950 error_local->message);
951 g_ptr_array_add (errors, g_steal_pointer (&error_local));
952 continue;
953 }
954
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600955 /* if component should have an update message from CAB */
956 fu_device_incorporate_from_component (device, component);
957
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100958 /* success */
959 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
960 }
961 }
962
963 /* order the install tasks by the device priority */
964 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
965
966 /* nothing suitable */
967 if (install_tasks->len == 0) {
968 GError *error_tmp = fu_common_error_array_get_best (errors);
969 g_propagate_error (error, error_tmp);
970 return FALSE;
971 }
972
Mario Limonciello3f243a92019-01-21 22:05:23 -0600973 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500974 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600975 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500976
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100977 /* install all the tasks */
Richard Hughesdf89cd52020-06-26 20:25:18 +0100978 if (!fu_engine_install_tasks (priv->engine,
979 priv->request,
980 install_tasks,
981 blob_cab,
982 priv->flags,
983 error))
Richard Hughesdbd8c762018-06-15 20:31:40 +0100984 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100985
Mario Limonciello32241f42019-01-24 10:12:41 -0600986 fu_util_display_current_message (priv);
987
Mario Limonciello3f243a92019-01-21 22:05:23 -0600988 /* we don't want to ask anything */
989 if (priv->no_reboot_check) {
990 g_debug ("skipping reboot check");
991 return TRUE;
992 }
993
Mario Limonciello3143bad2019-02-27 07:31:00 -0600994 /* save the device state for other applications to see */
995 if (!fu_util_save_current_state (priv, error))
996 return FALSE;
997
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100998 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600999 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001000}
1001
Richard Hughes98ca9932018-05-18 10:24:07 +01001002static gboolean
Mario Limonciellofd734852019-08-01 16:41:42 -05001003fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error)
Mario Limonciello46aaee82019-01-10 12:58:00 -06001004{
Mario Limonciellofd734852019-08-01 16:41:42 -05001005 FwupdRemote *remote;
1006 const gchar *remote_id;
1007 const gchar *uri_tmp;
1008 g_auto(GStrv) argv = NULL;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001009
Mario Limonciellofd734852019-08-01 16:41:42 -05001010 uri_tmp = fwupd_release_get_uri (rel);
1011 if (uri_tmp == NULL) {
1012 g_set_error_literal (error,
1013 FWUPD_ERROR,
1014 FWUPD_ERROR_INVALID_FILE,
1015 "release missing URI");
1016 return FALSE;
1017 }
1018 remote_id = fwupd_release_get_remote_id (rel);
1019 if (remote_id == NULL) {
1020 g_set_error (error,
1021 FWUPD_ERROR,
1022 FWUPD_ERROR_INVALID_FILE,
1023 "failed to find remote for %s",
1024 uri_tmp);
Richard Hughes747f5702019-08-06 14:27:26 +01001025 return FALSE;
Mario Limonciellofd734852019-08-01 16:41:42 -05001026 }
1027
1028 remote = fu_engine_get_remote_by_id (priv->engine,
1029 remote_id,
1030 error);
1031 if (remote == NULL)
Mario Limonciello46aaee82019-01-10 12:58:00 -06001032 return FALSE;
1033
Mario Limonciellofd734852019-08-01 16:41:42 -05001034 argv = g_new0 (gchar *, 2);
1035 /* local remotes have the firmware already */
1036 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
1037 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
1038 g_autofree gchar *path = g_path_get_dirname (fn_cache);
1039 argv[0] = g_build_filename (path, uri_tmp, NULL);
1040 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
1041 argv[0] = g_strdup (uri_tmp + 7);
1042 /* web remote, fu_util_install will download file */
1043 } else {
1044 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
1045 }
1046 return fu_util_install (priv, argv, error);
1047}
1048
1049static gboolean
1050fu_util_update_all (FuUtilPrivate *priv, GError **error)
1051{
1052 g_autoptr(GPtrArray) devices = NULL;
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001053 gboolean no_updates_header = FALSE;
1054 gboolean latest_header = FALSE;
Mario Limonciello3f243a92019-01-21 22:05:23 -06001055
Mario Limonciello46aaee82019-01-10 12:58:00 -06001056 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +00001057 if (devices == NULL)
1058 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +00001059 fwupd_device_array_ensure_parents (devices);
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001060 g_ptr_array_sort (devices, fu_util_sort_devices_by_flags_cb);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001061 for (guint i = 0; i < devices->len; i++) {
1062 FwupdDevice *dev = g_ptr_array_index (devices, i);
1063 FwupdRelease *rel;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001064 const gchar *device_id;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001065 g_autoptr(GPtrArray) rels = NULL;
1066 g_autoptr(GError) error_local = NULL;
1067
1068 if (!fu_util_is_interesting_device (dev))
1069 continue;
1070 /* only show stuff that has metadata available */
Mario Limonciello27164b72020-02-17 23:19:36 -06001071 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE))
Mario Limonciello46aaee82019-01-10 12:58:00 -06001072 continue;
Mario Limonciello27164b72020-02-17 23:19:36 -06001073 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001074 if (!no_updates_header) {
1075 /* TRANSLATORS: message letting the user know no device upgrade available due to missing on LVFS */
1076 g_printerr ("%s\n", _("Devices with no available firmware updates: "));
1077 no_updates_header = TRUE;
1078 }
1079 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -06001080 continue;
1081 }
Richard Hughes747f5702019-08-06 14:27:26 +01001082 if (!fu_util_filter_device (priv, dev))
1083 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001084
1085 device_id = fu_device_get_id (dev);
Richard Hughesdf89cd52020-06-26 20:25:18 +01001086 rels = fu_engine_get_upgrades (priv->engine,
1087 priv->request,
1088 device_id,
1089 &error_local);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001090 if (rels == NULL) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001091 if (!latest_header) {
1092 /* TRANSLATORS: message letting the user know no device upgrade available */
1093 g_printerr ("%s\n", _("Devices with the latest available firmware version:"));
1094 latest_header = TRUE;
1095 }
1096 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -06001097 /* discard the actual reason from user, but leave for debugging */
1098 g_debug ("%s", error_local->message);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001099 continue;
1100 }
1101
Mario Limonciello98b95162019-10-30 09:20:43 -05001102 if (!priv->no_safety_check) {
1103 if (!fu_util_prompt_warning (dev,
1104 fu_util_get_tree_title (priv),
1105 error))
1106 return FALSE;
1107 }
1108
Mario Limonciello46aaee82019-01-10 12:58:00 -06001109 rel = g_ptr_array_index (rels, 0);
Mario Limonciellofd734852019-08-01 16:41:42 -05001110 if (!fu_util_install_release (priv, rel, &error_local)) {
1111 g_printerr ("%s\n", error_local->message);
Richard Hughes747f5702019-08-06 14:27:26 +01001112 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001113 }
Mario Limonciellofd734852019-08-01 16:41:42 -05001114 fu_util_display_current_message (priv);
1115 }
1116 return TRUE;
1117}
1118
1119static gboolean
Mario Limonciello9917bb42020-04-20 13:41:27 -05001120fu_util_update_by_id (FuUtilPrivate *priv, const gchar *id, GError **error)
Mario Limonciellofd734852019-08-01 16:41:42 -05001121{
1122 FwupdRelease *rel;
1123 g_autoptr(FuDevice) dev = NULL;
1124 g_autoptr(GPtrArray) rels = NULL;
1125
Mario Limonciello9917bb42020-04-20 13:41:27 -05001126 /* do not allow a partial device-id, lookup GUIDs */
1127 dev = fu_util_get_device (priv, id, error);
Mario Limonciellofd734852019-08-01 16:41:42 -05001128 if (dev == NULL)
1129 return FALSE;
1130
1131 /* get the releases for this device and filter for validity */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001132 rels = fu_engine_get_upgrades (priv->engine,
1133 priv->request,
1134 fu_device_get_id (dev),
1135 error);
Mario Limonciellofd734852019-08-01 16:41:42 -05001136 if (rels == NULL)
1137 return FALSE;
1138 rel = g_ptr_array_index (rels, 0);
1139 if (!fu_util_install_release (priv, rel, error))
1140 return FALSE;
1141 fu_util_display_current_message (priv);
1142
1143 return TRUE;
1144}
1145
1146static gboolean
1147fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
1148{
Mario Limonciello0f109b02019-11-14 10:26:44 -06001149 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) {
1150 g_set_error_literal (error,
1151 FWUPD_ERROR,
1152 FWUPD_ERROR_INVALID_ARGS,
1153 "--allow-older is not supported for this command");
1154 return FALSE;
1155 }
1156
1157 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) {
1158 g_set_error_literal (error,
1159 FWUPD_ERROR,
1160 FWUPD_ERROR_INVALID_ARGS,
1161 "--allow-reinstall is not supported for this command");
1162 return FALSE;
1163 }
1164
Mario Limonciellofd734852019-08-01 16:41:42 -05001165 if (g_strv_length (values) > 1) {
1166 g_set_error_literal (error,
1167 FWUPD_ERROR,
1168 FWUPD_ERROR_INVALID_ARGS,
1169 "Invalid arguments");
1170 return FALSE;
1171 }
1172
1173 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1174 return FALSE;
1175
1176 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
1177 g_signal_connect (priv->engine, "device-changed",
1178 G_CALLBACK (fu_util_update_device_changed_cb), priv);
1179
1180 if (g_strv_length (values) == 1) {
1181 if (!fu_util_update_by_id (priv, values[0], error))
1182 return FALSE;
1183 } else {
1184 if (!fu_util_update_all (priv, error))
1185 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001186 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001187
1188 /* we don't want to ask anything */
1189 if (priv->no_reboot_check) {
1190 g_debug ("skipping reboot check");
1191 return TRUE;
1192 }
1193
Mario Limonciello3143bad2019-02-27 07:31:00 -06001194 /* save the device state for other applications to see */
1195 if (!fu_util_save_current_state (priv, error))
1196 return FALSE;
1197
Mario Limonciello3f243a92019-01-21 22:05:23 -06001198 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001199}
1200
1201static gboolean
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001202fu_util_reinstall (FuUtilPrivate *priv, gchar **values, GError **error)
1203{
1204 g_autoptr(FwupdRelease) rel = NULL;
1205 g_autoptr(GPtrArray) rels = NULL;
1206 g_autoptr(FuDevice) dev = NULL;
1207
1208 if (g_strv_length (values) != 1) {
1209 g_set_error_literal (error,
1210 FWUPD_ERROR,
1211 FWUPD_ERROR_INVALID_ARGS,
1212 "Invalid arguments");
1213 return FALSE;
1214 }
1215
1216 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1217 return FALSE;
1218
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001219 dev = fu_util_get_device (priv, values[0], error);
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001220 if (dev == NULL)
1221 return FALSE;
1222
1223 /* try to lookup/match release from client */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001224 rels = fu_engine_get_releases_for_device (priv->engine,
1225 priv->request,
1226 dev,
1227 error);
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001228 if (rels == NULL)
1229 return FALSE;
1230
1231 for (guint j = 0; j < rels->len; j++) {
1232 FwupdRelease *rel_tmp = g_ptr_array_index (rels, j);
1233 if (fu_common_vercmp_full (fwupd_release_get_version (rel_tmp),
1234 fu_device_get_version (dev),
1235 fu_device_get_version_format (dev)) == 0) {
1236 rel = g_object_ref (rel_tmp);
1237 break;
1238 }
1239 }
1240 if (rel == NULL) {
1241 g_set_error (error,
1242 FWUPD_ERROR,
1243 FWUPD_ERROR_NOT_SUPPORTED,
1244 "Unable to locate release for %s version %s",
1245 fu_device_get_name (dev),
1246 fu_device_get_version (dev));
1247 return FALSE;
1248 }
1249
1250 /* update the console if composite devices are also updated */
1251 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
1252 g_signal_connect (priv->engine, "device-changed",
1253 G_CALLBACK (fu_util_update_device_changed_cb), priv);
1254 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1255 if (!fu_util_install_release (priv, rel, error))
1256 return FALSE;
1257 fu_util_display_current_message (priv);
1258
1259 /* we don't want to ask anything */
1260 if (priv->no_reboot_check) {
1261 g_debug ("skipping reboot check");
1262 return TRUE;
1263 }
1264
1265 /* save the device state for other applications to see */
1266 if (!fu_util_save_current_state (priv, error))
1267 return FALSE;
1268
1269 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
1270}
1271
1272static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +01001273fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
1274{
1275 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001276 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001277
1278 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001279 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001280 return FALSE;
1281
Richard Hughes98ca9932018-05-18 10:24:07 +01001282 /* get device */
1283 if (g_strv_length (values) >= 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001284 device = fu_util_get_device (priv, values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001285 if (device == NULL)
1286 return FALSE;
1287 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001288 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001289 if (device == NULL)
1290 return FALSE;
1291 }
1292
1293 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001294 locker = fu_device_locker_new (device, error);
1295 if (locker == NULL)
1296 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001297 return fu_device_detach (device, error);
1298}
1299
1300static gboolean
1301fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1302{
1303 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001304 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001305
1306 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001307 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001308 return FALSE;
1309
Richard Hughes98ca9932018-05-18 10:24:07 +01001310 /* get device */
1311 if (g_strv_length (values) >= 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001312 device = fu_util_get_device (priv, values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001313 if (device == NULL)
1314 return FALSE;
1315 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001316 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001317 if (device == NULL)
1318 return FALSE;
1319 }
1320
1321 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001322 locker = fu_device_locker_new (device, error);
1323 if (locker == NULL)
1324 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001325 return fu_device_attach (device, error);
1326}
1327
Richard Hughes1d894f12018-08-31 13:05:51 +01001328static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001329fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1330{
1331 gboolean has_pending = FALSE;
1332 g_autoptr(FuHistory) history = fu_history_new ();
1333 g_autoptr(GPtrArray) devices = NULL;
1334
1335 /* check the history database before starting the daemon */
1336 if (g_strv_length (values) == 0) {
1337 devices = fu_history_get_devices (history, error);
1338 if (devices == NULL)
1339 return FALSE;
1340 } else if (g_strv_length (values) == 1) {
1341 FuDevice *device;
1342 device = fu_history_get_device_by_id (history, values[0], error);
1343 if (device == NULL)
1344 return FALSE;
1345 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1346 g_ptr_array_add (devices, device);
1347 } else {
1348 g_set_error_literal (error,
1349 FWUPD_ERROR,
1350 FWUPD_ERROR_INVALID_ARGS,
1351 "Invalid arguments");
1352 return FALSE;
1353 }
1354
1355 /* nothing to do */
1356 for (guint i = 0; i < devices->len; i++) {
1357 FuDevice *dev = g_ptr_array_index (devices, i);
1358 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1359 fu_engine_add_plugin_filter (priv->engine,
1360 fu_device_get_plugin (dev));
1361 has_pending = TRUE;
1362 }
1363 }
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001364
dkadioglu1db7e982019-12-04 21:13:21 +01001365 if (!has_pending) {
1366 g_printerr ("No firmware to activate\n");
1367 return TRUE;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001368 }
1369
1370 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001371 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001372 return FALSE;
1373
1374 /* activate anything with _NEEDS_ACTIVATION */
1375 for (guint i = 0; i < devices->len; i++) {
1376 FuDevice *device = g_ptr_array_index (devices, i);
Richard Hughes747f5702019-08-06 14:27:26 +01001377 if (!fu_util_filter_device (priv, FWUPD_DEVICE (device)))
1378 continue;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001379 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1380 continue;
1381 /* TRANSLATORS: shown when shutting down to switch to the new version */
1382 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1383 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1384 return FALSE;
1385 }
1386
1387 return TRUE;
1388}
1389
1390static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001391fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1392{
1393 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1394 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1395 const gchar *hwid_keys[] = {
1396 FU_HWIDS_KEY_BIOS_VENDOR,
1397 FU_HWIDS_KEY_BIOS_VERSION,
1398 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1399 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1400 FU_HWIDS_KEY_MANUFACTURER,
1401 FU_HWIDS_KEY_FAMILY,
1402 FU_HWIDS_KEY_PRODUCT_NAME,
1403 FU_HWIDS_KEY_PRODUCT_SKU,
1404 FU_HWIDS_KEY_ENCLOSURE_KIND,
1405 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1406 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1407 NULL };
1408
1409 /* read DMI data */
1410 if (g_strv_length (values) == 0) {
1411 if (!fu_smbios_setup (smbios, error))
1412 return FALSE;
1413 } else if (g_strv_length (values) == 1) {
1414 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1415 return FALSE;
1416 } else {
1417 g_set_error_literal (error,
1418 FWUPD_ERROR,
1419 FWUPD_ERROR_INVALID_ARGS,
1420 "Invalid arguments");
1421 return FALSE;
1422 }
1423 if (!fu_hwids_setup (hwids, smbios, error))
1424 return FALSE;
1425
1426 /* show debug output */
1427 g_print ("Computer Information\n");
1428 g_print ("--------------------\n");
1429 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1430 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1431 if (tmp == NULL)
1432 continue;
1433 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1434 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1435 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1436 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1437 } else {
1438 g_print ("%s: %s\n", hwid_keys[i], tmp);
1439 }
1440 }
1441
1442 /* show GUIDs */
1443 g_print ("\nHardware IDs\n");
1444 g_print ("------------\n");
1445 for (guint i = 0; i < 15; i++) {
1446 const gchar *keys = NULL;
1447 g_autofree gchar *guid = NULL;
1448 g_autofree gchar *key = NULL;
1449 g_autofree gchar *keys_str = NULL;
1450 g_auto(GStrv) keysv = NULL;
1451 g_autoptr(GError) error_local = NULL;
1452
1453 /* get the GUID */
1454 key = g_strdup_printf ("HardwareID-%u", i);
1455 keys = fu_hwids_get_replace_keys (hwids, key);
1456 guid = fu_hwids_get_guid (hwids, key, &error_local);
1457 if (guid == NULL) {
1458 g_print ("%s\n", error_local->message);
1459 continue;
1460 }
1461
1462 /* show what makes up the GUID */
1463 keysv = g_strsplit (keys, "&", -1);
1464 keys_str = g_strjoinv (" + ", keysv);
1465 g_print ("{%s} <- %s\n", guid, keys_str);
1466 }
1467
1468 return TRUE;
1469}
1470
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001471static gboolean
1472fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1473{
1474 const gchar *script_fn = "startup.sh";
1475 const gchar *output_fn = "firmware.bin";
1476 g_autoptr(GBytes) archive_blob = NULL;
1477 g_autoptr(GBytes) firmware_blob = NULL;
1478 if (g_strv_length (values) < 2) {
1479 g_set_error_literal (error,
1480 FWUPD_ERROR,
1481 FWUPD_ERROR_INVALID_ARGS,
1482 "Invalid arguments");
1483 return FALSE;
1484 }
1485 archive_blob = fu_common_get_contents_bytes (values[0], error);
1486 if (archive_blob == NULL)
1487 return FALSE;
1488 if (g_strv_length (values) > 2)
1489 script_fn = values[2];
1490 if (g_strv_length (values) > 3)
1491 output_fn = values[3];
1492 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1493 if (firmware_blob == NULL)
1494 return FALSE;
1495 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1496}
1497
Richard Hughes3d607622019-03-07 16:59:27 +00001498static gboolean
1499fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1500{
1501 g_autofree gchar *sig = NULL;
1502
1503 /* check args */
1504 if (g_strv_length (values) != 1) {
1505 g_set_error_literal (error,
1506 FWUPD_ERROR,
1507 FWUPD_ERROR_INVALID_ARGS,
1508 "Invalid arguments: value expected");
1509 return FALSE;
1510 }
1511
1512 /* start engine */
1513 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1514 return FALSE;
1515 sig = fu_engine_self_sign (priv->engine, values[0],
Richard Hughesd5aab652020-02-25 12:47:50 +00001516 JCAT_SIGN_FLAG_ADD_TIMESTAMP |
1517 JCAT_SIGN_FLAG_ADD_CERT, error);
Richard Hughes3d607622019-03-07 16:59:27 +00001518 if (sig == NULL)
1519 return FALSE;
1520 g_print ("%s\n", sig);
1521 return TRUE;
1522}
1523
Mario Limonciello62f84862018-10-18 13:15:23 -05001524static void
1525fu_util_device_added_cb (FwupdClient *client,
1526 FwupdDevice *device,
1527 gpointer user_data)
1528{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001529 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001530 /* TRANSLATORS: this is when a device is hotplugged */
1531 g_print ("%s\n%s", _("Device added:"), tmp);
1532}
1533
1534static void
1535fu_util_device_removed_cb (FwupdClient *client,
1536 FwupdDevice *device,
1537 gpointer user_data)
1538{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001539 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001540 /* TRANSLATORS: this is when a device is hotplugged */
1541 g_print ("%s\n%s", _("Device removed:"), tmp);
1542}
1543
1544static void
1545fu_util_device_changed_cb (FwupdClient *client,
1546 FwupdDevice *device,
1547 gpointer user_data)
1548{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001549 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001550 /* TRANSLATORS: this is when a device has been updated */
1551 g_print ("%s\n%s", _("Device changed:"), tmp);
1552}
1553
1554static void
1555fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1556{
1557 /* TRANSLATORS: this is when the daemon state changes */
1558 g_print ("%s\n", _("Changed"));
1559}
1560
1561static gboolean
1562fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1563{
1564 g_autoptr(FwupdClient) client = fwupd_client_new ();
1565
1566 /* get all the devices */
1567 if (!fwupd_client_connect (client, priv->cancellable, error))
1568 return FALSE;
1569
1570 /* watch for any hotplugged device */
1571 g_signal_connect (client, "changed",
1572 G_CALLBACK (fu_util_changed_cb), priv);
1573 g_signal_connect (client, "device-added",
1574 G_CALLBACK (fu_util_device_added_cb), priv);
1575 g_signal_connect (client, "device-removed",
1576 G_CALLBACK (fu_util_device_removed_cb), priv);
1577 g_signal_connect (client, "device-changed",
1578 G_CALLBACK (fu_util_device_changed_cb), priv);
1579 g_signal_connect (priv->cancellable, "cancelled",
1580 G_CALLBACK (fu_util_cancelled_cb), priv);
1581 g_main_loop_run (priv->loop);
1582 return TRUE;
1583}
1584
Richard Hughes15684492019-03-15 16:27:50 +00001585static gboolean
Richard Hughes95c98a92019-10-22 16:03:15 +01001586fu_util_get_firmware_types (FuUtilPrivate *priv, gchar **values, GError **error)
1587{
1588 g_autoptr(GPtrArray) firmware_types = NULL;
1589
1590 /* load engine */
1591 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1592 return FALSE;
1593
1594 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1595 for (guint i = 0; i < firmware_types->len; i++) {
1596 const gchar *id = g_ptr_array_index (firmware_types, i);
1597 g_print ("%s\n", id);
1598 }
1599 if (firmware_types->len == 0) {
1600 /* TRANSLATORS: nothing found */
1601 g_print ("%s\n", _("No firmware IDs found"));
1602 return TRUE;
1603 }
1604
1605 return TRUE;
1606}
1607
1608static gchar *
1609fu_util_prompt_for_firmware_type (FuUtilPrivate *priv, GError **error)
1610{
1611 g_autoptr(GPtrArray) firmware_types = NULL;
1612 guint idx;
1613 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1614
1615 /* TRANSLATORS: get interactive prompt */
1616 g_print ("%s\n", _("Choose a firmware type:"));
1617 /* TRANSLATORS: this is to abort the interactive prompt */
1618 g_print ("0.\t%s\n", _("Cancel"));
1619 for (guint i = 0; i < firmware_types->len; i++) {
1620 const gchar *id = g_ptr_array_index (firmware_types, i);
1621 g_print ("%u.\t%s\n", i + 1, id);
1622 }
1623 idx = fu_util_prompt_for_number (firmware_types->len);
1624 if (idx == 0) {
1625 g_set_error_literal (error,
1626 FWUPD_ERROR,
1627 FWUPD_ERROR_NOTHING_TO_DO,
1628 "Request canceled");
1629 return NULL;
1630 }
1631
1632 return g_strdup (g_ptr_array_index (firmware_types, idx - 1));
1633}
1634
1635static gboolean
1636fu_util_firmware_parse (FuUtilPrivate *priv, gchar **values, GError **error)
1637{
1638 GType gtype;
1639 g_autoptr(GBytes) blob = NULL;
1640 g_autoptr(FuFirmware) firmware = NULL;
1641 g_autofree gchar *firmware_type = NULL;
1642 g_autofree gchar *str = NULL;
1643
1644 /* check args */
1645 if (g_strv_length (values) == 0 || g_strv_length (values) > 2) {
1646 g_set_error_literal (error,
1647 FWUPD_ERROR,
1648 FWUPD_ERROR_INVALID_ARGS,
1649 "Invalid arguments: filename required");
1650 return FALSE;
1651 }
1652
1653 if (g_strv_length (values) == 2)
1654 firmware_type = g_strdup (values[1]);
1655
1656 /* load file */
1657 blob = fu_common_get_contents_bytes (values[0], error);
1658 if (blob == NULL)
1659 return FALSE;
1660
1661 /* load engine */
1662 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1663 return FALSE;
1664
1665 /* find the GType to use */
1666 if (firmware_type == NULL)
1667 firmware_type = fu_util_prompt_for_firmware_type (priv, error);
1668 if (firmware_type == NULL)
1669 return FALSE;
1670 gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type);
1671 if (gtype == G_TYPE_INVALID) {
1672 g_set_error (error,
1673 G_IO_ERROR,
1674 G_IO_ERROR_NOT_FOUND,
1675 "GType %s not supported", firmware_type);
1676 return FALSE;
1677 }
1678 firmware = g_object_new (gtype, NULL);
1679 if (!fu_firmware_parse (firmware, blob, priv->flags, error))
1680 return FALSE;
1681 str = fu_firmware_to_string (firmware);
1682 g_print ("%s", str);
1683 return TRUE;
1684}
1685
1686static gboolean
Richard Hughesbca63ed2020-03-09 20:20:03 +00001687fu_util_firmware_convert (FuUtilPrivate *priv, gchar **values, GError **error)
1688{
1689 GType gtype_dst;
1690 GType gtype_src;
1691 g_autofree gchar *firmware_type_dst = NULL;
1692 g_autofree gchar *firmware_type_src = NULL;
1693 g_autofree gchar *str_dst = NULL;
1694 g_autofree gchar *str_src = NULL;
1695 g_autoptr(FuFirmware) firmware_dst = NULL;
1696 g_autoptr(FuFirmware) firmware_src = NULL;
1697 g_autoptr(GBytes) blob_dst = NULL;
1698 g_autoptr(GBytes) blob_src = NULL;
1699 g_autoptr(GPtrArray) images = NULL;
1700
1701 /* check args */
1702 if (g_strv_length (values) < 2 || g_strv_length (values) > 4) {
1703 g_set_error_literal (error,
1704 FWUPD_ERROR,
1705 FWUPD_ERROR_INVALID_ARGS,
1706 "Invalid arguments: filename required");
1707 return FALSE;
1708 }
1709
1710 if (g_strv_length (values) > 2)
1711 firmware_type_src = g_strdup (values[2]);
1712 if (g_strv_length (values) > 3)
1713 firmware_type_dst = g_strdup (values[3]);
1714
1715 /* load file */
1716 blob_src = fu_common_get_contents_bytes (values[0], error);
1717 if (blob_src == NULL)
1718 return FALSE;
1719
1720 /* load engine */
1721 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1722 return FALSE;
1723
1724 /* find the GType to use */
1725 if (firmware_type_src == NULL)
1726 firmware_type_src = fu_util_prompt_for_firmware_type (priv, error);
1727 if (firmware_type_src == NULL)
1728 return FALSE;
1729 if (firmware_type_dst == NULL)
1730 firmware_type_dst = fu_util_prompt_for_firmware_type (priv, error);
1731 if (firmware_type_dst == NULL)
1732 return FALSE;
1733 gtype_src = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_src);
1734 if (gtype_src == G_TYPE_INVALID) {
1735 g_set_error (error,
1736 G_IO_ERROR,
1737 G_IO_ERROR_NOT_FOUND,
1738 "GType %s not supported", firmware_type_src);
1739 return FALSE;
1740 }
1741 gtype_dst = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_dst);
1742 if (gtype_dst == G_TYPE_INVALID) {
1743 g_set_error (error,
1744 G_IO_ERROR,
1745 G_IO_ERROR_NOT_FOUND,
1746 "GType %s not supported", firmware_type_dst);
1747 return FALSE;
1748 }
1749 firmware_src = g_object_new (gtype_src, NULL);
1750 if (!fu_firmware_parse (firmware_src, blob_src, priv->flags, error))
1751 return FALSE;
1752 str_src = fu_firmware_to_string (firmware_src);
1753 g_print ("%s", str_src);
1754
1755 /* copy images */
1756 firmware_dst = g_object_new (gtype_dst, NULL);
1757 images = fu_firmware_get_images (firmware_src);
1758 for (guint i = 0; i < images->len; i++) {
1759 FuFirmwareImage *img = g_ptr_array_index (images, i);
1760 fu_firmware_add_image (firmware_dst, img);
1761 }
1762
1763 /* write new file */
1764 blob_dst = fu_firmware_write (firmware_dst, error);
1765 if (blob_dst == NULL)
1766 return FALSE;
1767 if (!fu_common_set_contents_bytes (values[1], blob_dst, error))
1768 return FALSE;
1769 str_dst = fu_firmware_to_string (firmware_dst);
1770 g_print ("%s", str_dst);
1771
1772 /* success */
1773 return TRUE;
1774}
1775
1776static gboolean
Richard Hughes15684492019-03-15 16:27:50 +00001777fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1778{
1779 g_autofree gchar *str = NULL;
1780 g_autoptr(FuDevice) dev = NULL;
1781
1782 /* load engine */
1783 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1784 return FALSE;
1785
1786 /* get device */
1787 if (g_strv_length (values) == 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001788 dev = fu_util_get_device (priv, values[1], error);
Richard Hughes15684492019-03-15 16:27:50 +00001789 if (dev == NULL)
1790 return FALSE;
1791 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001792 dev = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes15684492019-03-15 16:27:50 +00001793 if (dev == NULL)
1794 return FALSE;
1795 }
1796
1797 /* add checksums */
1798 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1799 return FALSE;
1800
1801 /* show checksums */
1802 str = fu_device_to_string (dev);
1803 g_print ("%s\n", str);
1804 return TRUE;
1805}
1806
Mario Limonciellofe593942019-04-03 13:48:52 -05001807static gboolean
1808fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
1809{
1810 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001811 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001812 g_autofree gchar *title = NULL;
Mario Limonciellofe593942019-04-03 13:48:52 -05001813
1814 /* load engine */
1815 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1816 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001817 title = fu_util_get_tree_title (priv);
Mario Limonciellofe593942019-04-03 13:48:52 -05001818
1819 /* get all devices from the history database */
1820 devices = fu_engine_get_history (priv->engine, error);
1821 if (devices == NULL)
1822 return FALSE;
1823
1824 /* show each device */
1825 for (guint i = 0; i < devices->len; i++) {
Mario Limoncielloce94d162019-09-20 13:37:36 -05001826 g_autoptr(GPtrArray) rels = NULL;
Mario Limonciellofe593942019-04-03 13:48:52 -05001827 FwupdDevice *dev = g_ptr_array_index (devices, i);
Mario Limonciello3be596b2019-09-20 10:10:08 -05001828 FwupdRelease *rel;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001829 const gchar *remote;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001830 GNode *child;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001831
Richard Hughes747f5702019-08-06 14:27:26 +01001832 if (!fu_util_filter_device (priv, dev))
1833 continue;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001834 child = g_node_append_data (root, dev);
1835
1836 rel = fwupd_device_get_release_default (dev);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001837 if (rel == NULL)
1838 continue;
1839 remote = fwupd_release_get_remote_id (rel);
1840
1841 /* doesn't actually map to remote */
1842 if (remote == NULL) {
Mario Limonciello3be596b2019-09-20 10:10:08 -05001843 g_node_append_data (child, rel);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001844 continue;
1845 }
1846
1847 /* try to lookup releases from client */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001848 rels = fu_engine_get_releases (priv->engine,
1849 priv->request,
1850 fwupd_device_get_id (dev),
1851 error);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001852 if (rels == NULL)
1853 return FALSE;
1854
1855 /* map to a release in client */
1856 for (guint j = 0; j < rels->len; j++) {
1857 FwupdRelease *rel2 = g_ptr_array_index (rels, j);
1858 if (g_strcmp0 (remote,
1859 fwupd_release_get_remote_id (rel2)) != 0)
1860 continue;
1861 if (g_strcmp0 (fwupd_release_get_version (rel),
1862 fwupd_release_get_version (rel2)) != 0)
1863 continue;
1864 g_node_append_data (child, g_object_ref (rel2));
1865 rel = NULL;
1866 break;
1867 }
1868
1869 /* didn't match anything */
1870 if (rels->len == 0 || rel != NULL) {
1871 g_node_append_data (child, rel);
1872 continue;
1873 }
Mario Limonciello3be596b2019-09-20 10:10:08 -05001874
Mario Limonciellofe593942019-04-03 13:48:52 -05001875 }
Mario Limonciello20cc9ee2019-09-05 07:27:26 -05001876 fu_util_print_tree (root, title);
Mario Limonciellofe593942019-04-03 13:48:52 -05001877
1878 return TRUE;
1879}
1880
Richard Hughesfd7e9942020-01-17 14:09:13 +00001881static gboolean
Richard Hughes4959baa2020-01-17 14:33:00 +00001882fu_util_refresh_remote (FuUtilPrivate *priv, FwupdRemote *remote, GError **error)
1883{
Richard Hughesc5710d92020-06-26 11:08:25 +01001884 const gchar *metadata_uri = NULL;
Richard Hughes4959baa2020-01-17 14:33:00 +00001885 g_autofree gchar *fn_raw = NULL;
1886 g_autofree gchar *fn_sig = NULL;
1887 g_autoptr(GBytes) bytes_raw = NULL;
1888 g_autoptr(GBytes) bytes_sig = NULL;
1889
1890 /* payload */
Richard Hughesc5710d92020-06-26 11:08:25 +01001891 metadata_uri = fwupd_remote_get_metadata_uri (remote);
1892 if (metadata_uri == NULL) {
1893 g_set_error_literal (error,
1894 FWUPD_ERROR,
1895 FWUPD_ERROR_NOTHING_TO_DO,
1896 "no metadata URI available");
1897 return FALSE;
1898 }
1899 fn_raw = fu_util_get_user_cache_path (metadata_uri);
Richard Hughes4959baa2020-01-17 14:33:00 +00001900 if (!fu_common_mkdir_parent (fn_raw, error))
1901 return FALSE;
Richard Hughesc5710d92020-06-26 11:08:25 +01001902 if (!fu_util_download_out_of_process (metadata_uri, fn_raw, error))
Richard Hughes4959baa2020-01-17 14:33:00 +00001903 return FALSE;
1904 bytes_raw = fu_common_get_contents_bytes (fn_raw, error);
1905 if (bytes_raw == NULL)
1906 return FALSE;
1907
1908 /* signature */
Richard Hughesc5710d92020-06-26 11:08:25 +01001909 metadata_uri = fwupd_remote_get_metadata_uri_sig (remote);
1910 if (metadata_uri == NULL) {
1911 g_set_error_literal (error,
1912 FWUPD_ERROR,
1913 FWUPD_ERROR_NOTHING_TO_DO,
1914 "no metadata signature URI available");
1915 return FALSE;
1916 }
1917 fn_sig = fu_util_get_user_cache_path (metadata_uri);
1918 if (!fu_util_download_out_of_process (metadata_uri, fn_sig, error))
Richard Hughes4959baa2020-01-17 14:33:00 +00001919 return FALSE;
1920 bytes_sig = fu_common_get_contents_bytes (fn_sig, error);
1921 if (bytes_sig == NULL)
1922 return FALSE;
1923
1924 /* send to daemon */
1925 g_debug ("updating %s", fwupd_remote_get_id (remote));
1926 return fu_engine_update_metadata_bytes (priv->engine,
1927 fwupd_remote_get_id (remote),
1928 bytes_raw,
1929 bytes_sig,
1930 error);
1931
1932}
1933
1934static gboolean
1935fu_util_refresh (FuUtilPrivate *priv, gchar **values, GError **error)
1936{
1937 g_autoptr(GPtrArray) remotes = NULL;
1938
1939 /* load engine */
1940 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1941 return FALSE;
1942
1943 /* download new metadata */
1944 remotes = fu_engine_get_remotes (priv->engine, error);
1945 if (remotes == NULL)
1946 return FALSE;
1947 for (guint i = 0; i < remotes->len; i++) {
1948 FwupdRemote *remote = g_ptr_array_index (remotes, i);
1949 if (!fwupd_remote_get_enabled (remote))
1950 continue;
1951 if (fwupd_remote_get_kind (remote) != FWUPD_REMOTE_KIND_DOWNLOAD)
1952 continue;
1953 if (!fu_util_refresh_remote (priv, remote, error))
1954 return FALSE;
1955 }
1956 return TRUE;
1957}
1958
1959static gboolean
Richard Hughesfd7e9942020-01-17 14:09:13 +00001960fu_util_get_remotes (FuUtilPrivate *priv, gchar **values, GError **error)
1961{
1962 g_autoptr(GNode) root = g_node_new (NULL);
1963 g_autoptr(GPtrArray) remotes = NULL;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001964 g_autofree gchar *title = NULL;
Richard Hughesfd7e9942020-01-17 14:09:13 +00001965
1966 /* load engine */
1967 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1968 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001969 title = fu_util_get_tree_title (priv);
Richard Hughesfd7e9942020-01-17 14:09:13 +00001970
1971 /* list remotes */
1972 remotes = fu_engine_get_remotes (priv->engine, error);
1973 if (remotes == NULL)
1974 return FALSE;
1975 if (remotes->len == 0) {
1976 g_set_error_literal (error,
1977 FWUPD_ERROR,
1978 FWUPD_ERROR_NOTHING_TO_DO,
1979 "no remotes available");
1980 return FALSE;
1981 }
1982 for (guint i = 0; i < remotes->len; i++) {
1983 FwupdRemote *remote_tmp = g_ptr_array_index (remotes, i);
1984 g_node_append_data (root, remote_tmp);
1985 }
1986 fu_util_print_tree (root, title);
1987
1988 return TRUE;
1989}
1990
Richard Hughes196c6c62020-05-11 19:42:47 +01001991static gboolean
1992fu_util_security (FuUtilPrivate *priv, gchar **values, GError **error)
1993{
Richard Hughesf58ac732020-05-12 15:23:44 +01001994 g_autoptr(FuSecurityAttrs) attrs = NULL;
1995 g_autoptr(GPtrArray) items = NULL;
Richard Hughes196c6c62020-05-11 19:42:47 +01001996 g_autofree gchar *str = NULL;
1997
1998 /* not ready yet */
1999 if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
2000 g_set_error_literal (error,
2001 FWUPD_ERROR,
2002 FWUPD_ERROR_NOT_SUPPORTED,
2003 "The HSI specification is not yet complete. "
2004 "To ignore this warning, use --force");
2005 return FALSE;
2006 }
2007
2008 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
2009 return FALSE;
2010
2011 /* TRANSLATORS: this is a string like 'HSI:2-U' */
2012 g_print ("%s \033[1m%s\033[0m\n", _("Host Security ID:"),
2013 fu_engine_get_host_security_id (priv->engine));
2014
2015 /* print the "why" */
Richard Hughes56e7ae52020-05-17 21:00:23 +01002016 attrs = fu_engine_get_host_security_attrs (priv->engine);
Richard Hughesf58ac732020-05-12 15:23:44 +01002017 items = fu_security_attrs_get_all (attrs);
2018 str = fu_util_security_attrs_to_string (items);
Richard Hughes196c6c62020-05-11 19:42:47 +01002019 g_print ("%s\n", str);
2020 return TRUE;
2021}
2022
Richard Hughesa83deb42020-08-12 12:45:36 +01002023static FuVolume *
2024fu_util_prompt_for_volume (GError **error)
2025{
2026 FuVolume *volume;
2027 guint idx;
2028 g_autoptr(GPtrArray) volumes = NULL;
2029
2030 /* exactly one */
2031 volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_ESP, error);
2032 if (volumes->len == 1) {
2033 volume = g_ptr_array_index (volumes, 0);
2034 /* TRANSLATORS: Volume has been chosen by the the user */
2035 g_print ("%s: %s\n", _("Selected volume"), fu_volume_get_id (volume));
2036 return g_object_ref (volume);
2037 }
2038
2039 /* TRANSLATORS: get interactive prompt */
2040 g_print ("%s\n", _("Choose a volume:"));
2041 /* TRANSLATORS: this is to abort the interactive prompt */
2042 g_print ("0.\t%s\n", _("Cancel"));
2043 for (guint i = 0; i < volumes->len; i++) {
2044 volume = g_ptr_array_index (volumes, i);
2045 g_print ("%u.\t%s\n", i + 1, fu_volume_get_id (volume));
2046 }
2047 idx = fu_util_prompt_for_number (volumes->len);
2048 if (idx == 0) {
2049 g_set_error_literal (error,
2050 FWUPD_ERROR,
2051 FWUPD_ERROR_NOTHING_TO_DO,
2052 "Request canceled");
2053 return NULL;
2054 }
2055 volume = g_ptr_array_index (volumes, idx - 1);
2056 return g_object_ref (volume);
2057
2058}
2059
2060static gboolean
2061fu_util_esp_mount (FuUtilPrivate *priv, gchar **values, GError **error)
2062{
2063 g_autoptr(FuVolume) volume = NULL;
2064 volume = fu_util_prompt_for_volume (error);
2065 if (volume == NULL)
2066 return FALSE;
2067 return fu_volume_mount (volume, error);
2068}
2069
2070static gboolean
2071fu_util_esp_unmount (FuUtilPrivate *priv, gchar **values, GError **error)
2072{
2073 g_autoptr(FuVolume) volume = NULL;
2074 volume = fu_util_prompt_for_volume (error);
2075 if (volume == NULL)
2076 return FALSE;
2077 return fu_volume_unmount (volume, error);
2078}
2079
2080static gboolean
2081fu_util_esp_list (FuUtilPrivate *priv, gchar **values, GError **error)
2082{
2083 g_autofree gchar *mount_point = NULL;
2084 g_autoptr(FuDeviceLocker) locker = NULL;
2085 g_autoptr(FuVolume) volume = NULL;
2086 g_autoptr(GPtrArray) files = NULL;
2087
2088 volume = fu_util_prompt_for_volume (error);
2089 if (volume == NULL)
2090 return FALSE;
2091 locker = fu_volume_locker (volume, error);
2092 if (locker == NULL)
2093 return FALSE;
2094 mount_point = fu_volume_get_mount_point (volume);
2095 files = fu_common_get_files_recursive (mount_point, error);
2096 if (files == NULL)
2097 return FALSE;
2098 for (guint i = 0; i < files->len; i++) {
2099 const gchar *fn = g_ptr_array_index (files, i);
2100 g_print ("%s\n", fn);
2101 }
2102 return TRUE;
2103}
2104
Richard Hughesb5976832018-05-18 10:02:09 +01002105int
2106main (int argc, char *argv[])
2107{
Richard Hughes460226a2018-05-21 20:56:21 +01002108 gboolean allow_older = FALSE;
2109 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01002110 gboolean force = FALSE;
2111 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002112 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002113 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002114 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01002115 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
2116 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00002117 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01002118 g_autofree gchar *cmd_descriptions = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +01002119 g_autofree gchar *filter = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01002120 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002121 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
2122 /* TRANSLATORS: command line option */
2123 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01002124 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
2125 /* TRANSLATORS: command line option */
Mario Limonciello350fc4c2019-10-03 07:08:51 -05002126 _("Allow reinstalling existing firmware versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01002127 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
2128 /* TRANSLATORS: command line option */
2129 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01002130 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
2131 /* TRANSLATORS: command line option */
2132 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06002133 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
2134 /* TRANSLATORS: command line option */
2135 _("Do not check for reboot after update"), NULL },
Mario Limonciello98b95162019-10-30 09:20:43 -05002136 { "no-safety-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_safety_check,
2137 /* TRANSLATORS: command line option */
2138 _("Do not perform device safety checks"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05002139 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
2140 /* TRANSLATORS: command line option */
2141 _("Show devices that are not updatable"), NULL },
Richard Hughesf8c10c22020-07-20 21:01:39 +01002142 { "plugins", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002143 /* TRANSLATORS: command line option */
Richard Hughes85226fd2020-06-30 14:43:48 +01002144 _("Manually enable specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06002145 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00002146 /* TRANSLATORS: command line option */
2147 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06002148 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00002149 /* TRANSLATORS: command line option */
2150 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06002151 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
2152 /* TRANSLATORS: command line option */
2153 _("Save device state into a JSON file between executions"), NULL },
Richard Hughes0e46b222019-09-05 12:13:35 +01002154 { "disable-ssl-strict", '\0', 0, G_OPTION_ARG_NONE, &priv->disable_ssl_strict,
2155 /* TRANSLATORS: command line option */
2156 _("Ignore SSL strict checks when downloading files"), NULL },
Richard Hughes747f5702019-08-06 14:27:26 +01002157 { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter,
2158 /* TRANSLATORS: command line option */
2159 _("Filter with a set of device flags using a ~ prefix to "
2160 "exclude, e.g. 'internal,~needs-reboot'"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01002161 { NULL}
2162 };
2163
Richard Hughes429f72b2020-01-16 12:18:19 +00002164#ifdef _WIN32
2165 /* workaround Windows setting the codepage to 1252 */
2166 g_setenv ("LANG", "C.UTF-8", FALSE);
2167#endif
2168
Richard Hughesb5976832018-05-18 10:02:09 +01002169 setlocale (LC_ALL, "");
2170
Richard Hughes668ee212019-11-22 09:17:46 +00002171 bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR);
Richard Hughesb5976832018-05-18 10:02:09 +01002172 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2173 textdomain (GETTEXT_PACKAGE);
2174
Richard Hughes01c0bad2019-11-22 09:08:51 +00002175#ifdef HAVE_GETUID
Richard Hughesb5976832018-05-18 10:02:09 +01002176 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002177 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01002178 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05002179 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughes01c0bad2019-11-22 09:08:51 +00002180#endif
Richard Hughesb5976832018-05-18 10:02:09 +01002181
2182 /* create helper object */
2183 priv->loop = g_main_loop_new (NULL, FALSE);
2184 priv->progressbar = fu_progressbar_new ();
Richard Hughesdf89cd52020-06-26 20:25:18 +01002185 priv->request = fu_engine_request_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01002186
2187 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00002188 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05002189 "build-firmware",
2190 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
2191 /* TRANSLATORS: command description */
2192 _("Build firmware using a sandbox"),
2193 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00002194 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01002195 "smbios-dump",
2196 "FILE",
2197 /* TRANSLATORS: command description */
2198 _("Dump SMBIOS data from a file"),
2199 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00002200 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002201 "get-plugins",
2202 NULL,
2203 /* TRANSLATORS: command description */
2204 _("Get all enabled plugins registered with the system"),
2205 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00002206 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05002207 "get-details",
2208 NULL,
2209 /* TRANSLATORS: command description */
2210 _("Gets details about a firmware file"),
2211 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00002212 fu_util_cmd_array_add (cmd_array,
Mario Limonciellofe593942019-04-03 13:48:52 -05002213 "get-history",
2214 NULL,
2215 /* TRANSLATORS: command description */
2216 _("Show history of firmware updates"),
2217 fu_util_get_history);
2218 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05002219 "get-updates,get-upgrades",
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06002220 NULL,
2221 /* TRANSLATORS: command description */
2222 _("Gets the list of updates for connected hardware"),
2223 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00002224 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1a9127d2019-08-27 11:32:51 +01002225 "get-devices,get-topology",
Richard Hughes98ca9932018-05-18 10:24:07 +01002226 NULL,
2227 /* TRANSLATORS: command description */
2228 _("Get all devices that support firmware updates"),
2229 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00002230 fu_util_cmd_array_add (cmd_array,
Richard Hughes747f5702019-08-06 14:27:26 +01002231 "get-device-flags",
2232 NULL,
2233 /* TRANSLATORS: command description */
2234 _("Get all device flags supported by fwupd"),
2235 fu_util_get_device_flags);
2236 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002237 "watch",
2238 NULL,
2239 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02002240 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01002241 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00002242 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002243 "install-blob",
2244 "FILENAME DEVICE-ID",
2245 /* TRANSLATORS: command description */
2246 _("Install a firmware blob on a device"),
2247 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00002248 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01002249 "install",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002250 "FILE [DEVICE-ID|GUID]",
Richard Hughesa36c9cf2018-05-20 10:41:44 +01002251 /* TRANSLATORS: command description */
2252 _("Install a firmware file on this hardware"),
2253 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00002254 fu_util_cmd_array_add (cmd_array,
Filipe Laínsb2f377a2020-03-30 21:05:50 +01002255 "reinstall",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002256 "DEVICE-ID|GUID",
Filipe Laínsb2f377a2020-03-30 21:05:50 +01002257 /* TRANSLATORS: command description */
2258 _("Reinstall firmware on a device"),
2259 fu_util_reinstall);
2260 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002261 "attach",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002262 "DEVICE-ID|GUID",
Richard Hughes98ca9932018-05-18 10:24:07 +01002263 /* TRANSLATORS: command description */
2264 _("Attach to firmware mode"),
2265 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00002266 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002267 "detach",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002268 "DEVICE-ID|GUID",
Richard Hughes98ca9932018-05-18 10:24:07 +01002269 /* TRANSLATORS: command description */
2270 _("Detach to bootloader mode"),
2271 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00002272 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002273 "activate",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002274 "[DEVICE-ID|GUID]",
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002275 /* TRANSLATORS: command description */
2276 _("Activate pending devices"),
2277 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00002278 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01002279 "hwids",
2280 "[FILE]",
2281 /* TRANSLATORS: command description */
2282 _("Return all the hardware IDs for the machine"),
2283 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00002284 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05002285 "monitor",
2286 NULL,
2287 /* TRANSLATORS: command description */
2288 _("Monitor the daemon for events"),
2289 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00002290 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05002291 "update,upgrade",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002292 "[DEVICE-ID|GUID]",
Mario Limonciello46aaee82019-01-10 12:58:00 -06002293 /* TRANSLATORS: command description */
2294 _("Update all devices that match local metadata"),
2295 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00002296 fu_util_cmd_array_add (cmd_array,
2297 "self-sign",
2298 "TEXT",
2299 /* TRANSLATORS: command description */
Richard Hughes12a021d2019-03-27 09:23:24 +00002300 C_("command-description",
2301 "Sign data using the client certificate"),
Richard Hughes3d607622019-03-07 16:59:27 +00002302 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00002303 fu_util_cmd_array_add (cmd_array,
2304 "verify-update",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002305 "[DEVICE-ID|GUID]",
Richard Hughes15684492019-03-15 16:27:50 +00002306 /* TRANSLATORS: command description */
2307 _("Update the stored metadata with current contents"),
2308 fu_util_verify_update);
Richard Hughes95c98a92019-10-22 16:03:15 +01002309 fu_util_cmd_array_add (cmd_array,
Richard Hughesa58510b2019-10-30 10:03:12 +00002310 "firmware-read",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002311 "FILENAME [DEVICE-ID|GUID]",
Richard Hughesa58510b2019-10-30 10:03:12 +00002312 /* TRANSLATORS: command description */
2313 _("Read a firmware blob from a device"),
2314 fu_util_firmware_read);
2315 fu_util_cmd_array_add (cmd_array,
Richard Hughesbca63ed2020-03-09 20:20:03 +00002316 "firmware-convert",
2317 "FILENAME-SRC FILENAME-DST [FIRMWARE-TYPE-SRC] [FIRMWARE-TYPE-DST]",
2318 /* TRANSLATORS: command description */
2319 _("Convert a firmware file"),
2320 fu_util_firmware_convert);
2321 fu_util_cmd_array_add (cmd_array,
Richard Hughes95c98a92019-10-22 16:03:15 +01002322 "firmware-parse",
Mario Limonciello234c8642020-01-08 20:07:29 -06002323 "FILENAME [FIRMWARE-TYPE]",
Richard Hughes95c98a92019-10-22 16:03:15 +01002324 /* TRANSLATORS: command description */
2325 _("Parse and show details about a firmware file"),
2326 fu_util_firmware_parse);
2327 fu_util_cmd_array_add (cmd_array,
2328 "get-firmware-types",
2329 NULL,
2330 /* TRANSLATORS: command description */
2331 _("List the available firmware types"),
2332 fu_util_get_firmware_types);
Richard Hughesfd7e9942020-01-17 14:09:13 +00002333 fu_util_cmd_array_add (cmd_array,
2334 "get-remotes",
2335 NULL,
2336 /* TRANSLATORS: command description */
2337 _("Gets the configured remotes"),
2338 fu_util_get_remotes);
Richard Hughes4959baa2020-01-17 14:33:00 +00002339 fu_util_cmd_array_add (cmd_array,
2340 "refresh",
2341 NULL,
2342 /* TRANSLATORS: command description */
2343 _("Refresh metadata from remote server"),
2344 fu_util_refresh);
Richard Hughes196c6c62020-05-11 19:42:47 +01002345 fu_util_cmd_array_add (cmd_array,
2346 "security",
2347 NULL,
2348 /* TRANSLATORS: command description */
2349 _("Gets the host security attributes."),
2350 fu_util_security);
Richard Hughesa83deb42020-08-12 12:45:36 +01002351 fu_util_cmd_array_add (cmd_array,
2352 "esp-mount",
2353 NULL,
2354 /* TRANSLATORS: command description */
2355 _("Mounts the ESP."),
2356 fu_util_esp_mount);
2357 fu_util_cmd_array_add (cmd_array,
2358 "esp-unmount",
2359 NULL,
2360 /* TRANSLATORS: command description */
2361 _("Unmounts the ESP."),
2362 fu_util_esp_unmount);
2363 fu_util_cmd_array_add (cmd_array,
2364 "esp-list",
2365 NULL,
2366 /* TRANSLATORS: command description */
2367 _("Lists files on the ESP."),
2368 fu_util_esp_list);
Richard Hughesb5976832018-05-18 10:02:09 +01002369
2370 /* do stuff on ctrl+c */
2371 priv->cancellable = g_cancellable_new ();
Richard Hughes9e5675e2019-11-22 09:35:03 +00002372#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +01002373 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
2374 SIGINT, fu_util_sigint_cb,
2375 priv, NULL);
Richard Hughes9e5675e2019-11-22 09:35:03 +00002376#endif
Richard Hughesb5976832018-05-18 10:02:09 +01002377 g_signal_connect (priv->cancellable, "cancelled",
2378 G_CALLBACK (fu_util_cancelled_cb), priv);
2379
2380 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00002381 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01002382
Mario Limonciello3f243a92019-01-21 22:05:23 -06002383 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002384 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06002385 priv->no_reboot_check = TRUE;
Mario Limonciello98b95162019-10-30 09:20:43 -05002386 priv->no_safety_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06002387 fu_progressbar_set_interactive (priv->progressbar, FALSE);
Richard Hughesdf89cd52020-06-26 20:25:18 +01002388 } else {
2389 /* set our implemented feature set */
2390 fu_engine_request_set_feature_flags (priv->request,
2391 FWUPD_FEATURE_FLAG_DETACH_ACTION |
2392 FWUPD_FEATURE_FLAG_UPDATE_ACTION);
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06002393 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06002394
Richard Hughesb5976832018-05-18 10:02:09 +01002395 /* get a list of the commands */
2396 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00002397 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01002398 g_option_context_set_summary (priv->context, cmd_descriptions);
2399 g_option_context_set_description (priv->context,
2400 "This tool allows an administrator to use the fwupd plugins "
2401 "without being installed on the host system.");
2402
2403 /* TRANSLATORS: program name */
2404 g_set_application_name (_("Firmware Utility"));
2405 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05002406 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01002407 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
2408 if (!ret) {
2409 /* TRANSLATORS: the user didn't read the man page */
2410 g_print ("%s: %s\n", _("Failed to parse arguments"),
2411 error->message);
2412 return EXIT_FAILURE;
2413 }
2414
Richard Hughes0e46b222019-09-05 12:13:35 +01002415 /* allow disabling SSL strict mode for broken corporate proxies */
2416 if (priv->disable_ssl_strict) {
2417 /* TRANSLATORS: try to help */
2418 g_printerr ("%s\n", _("WARNING: Ignoring SSL strict checks, "
2419 "to do this automatically in the future "
2420 "export DISABLE_SSL_STRICT in your environment"));
2421 g_setenv ("DISABLE_SSL_STRICT", "1", TRUE);
2422 }
2423
Richard Hughes747f5702019-08-06 14:27:26 +01002424 /* parse filter flags */
2425 if (filter != NULL) {
2426 if (!fu_util_parse_filter_flags (filter,
2427 &priv->filter_include,
2428 &priv->filter_exclude,
2429 &error)) {
2430 /* TRANSLATORS: the user didn't read the man page */
2431 g_print ("%s: %s\n", _("Failed to parse flags for --filter"),
2432 error->message);
2433 return EXIT_FAILURE;
2434 }
2435 }
2436
2437
Richard Hughes460226a2018-05-21 20:56:21 +01002438 /* set flags */
Richard Hughes460226a2018-05-21 20:56:21 +01002439 if (allow_reinstall)
2440 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
2441 if (allow_older)
2442 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
2443 if (force)
2444 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
2445
Richard Hughes98ca9932018-05-18 10:24:07 +01002446 /* load engine */
2447 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
2448 g_signal_connect (priv->engine, "device-added",
2449 G_CALLBACK (fu_main_engine_device_added_cb),
2450 priv);
2451 g_signal_connect (priv->engine, "device-removed",
2452 G_CALLBACK (fu_main_engine_device_removed_cb),
2453 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01002454 g_signal_connect (priv->engine, "status-changed",
2455 G_CALLBACK (fu_main_engine_status_changed_cb),
2456 priv);
2457 g_signal_connect (priv->engine, "percentage-changed",
2458 G_CALLBACK (fu_main_engine_percentage_changed_cb),
2459 priv);
2460
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002461 /* just show versions and exit */
2462 if (version) {
2463 g_autofree gchar *version_str = fu_util_get_versions ();
2464 g_print ("%s\n", version_str);
2465 return EXIT_SUCCESS;
2466 }
2467
Richard Hughes85226fd2020-06-30 14:43:48 +01002468 /* any plugin allowlist specified */
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002469 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
2470 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
2471
Richard Hughesb5976832018-05-18 10:02:09 +01002472 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00002473 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01002474 if (!ret) {
Mario Limonciello89096312020-04-30 09:51:14 -05002475 g_printerr ("%s\n", error->message);
Richard Hughesb5976832018-05-18 10:02:09 +01002476 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
Mario Limonciello89096312020-04-30 09:51:14 -05002477 /* TRANSLATORS: error message explaining command to run to how to get help */
2478 g_printerr ("\n%s\n", _("Use fwupdtool --help for help"));
2479 } else if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
2480 g_debug ("%s\n", error->message);
Richard Hughesb5976832018-05-18 10:02:09 +01002481 return EXIT_NOTHING_TO_DO;
2482 }
Richard Hughesb5976832018-05-18 10:02:09 +01002483 return EXIT_FAILURE;
2484 }
2485
2486 /* success */
2487 return EXIT_SUCCESS;
2488}