blob: fb7de426c2b001702939cdd603bb6a46c1c3c7c0 [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;
Richard Hughes5c82b942020-09-14 12:24:06 +010064 gboolean show_all;
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 */
Richard Hughes5c82b942020-09-14 12:24:06 +0100414 priv->show_all = TRUE;
Mario Limonciellodc0608d2020-02-25 11:25:44 -0600415
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;
Richard Hughes5c82b942020-09-14 12:24:06 +0100476 if (!priv->show_all &&
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500477 !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;
Richard Hughesdb344d52020-09-09 19:42:27 +0100825 g_autofree gchar *fn_tmp = NULL;
826 fn_tmp = fu_common_find_program_in_path (argv[i][0], &error_local);
827 if (fn_tmp == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100828 g_debug ("%s", error_local->message);
829 continue;
830 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000831 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100832 }
833 g_set_error_literal (error,
834 FWUPD_ERROR,
835 FWUPD_ERROR_NOT_FOUND,
836 "no supported out-of-process downloaders found");
837 return FALSE;
838}
839
840static gchar *
841fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
842{
843 g_autofree gchar *filename = NULL;
844 g_autoptr(SoupURI) uri = NULL;
845
846 /* a local file */
847 uri = soup_uri_new (perhapsfn);
Richard Hughes45a00732019-11-22 16:57:14 +0000848 if (g_file_test (perhapsfn, G_FILE_TEST_EXISTS))
849 return g_strdup (perhapsfn);
Richard Hughes3d178be2018-08-30 11:14:24 +0100850 if (uri == NULL)
851 return g_strdup (perhapsfn);
852
853 /* download the firmware to a cachedir */
854 filename = fu_util_get_user_cache_path (perhapsfn);
855 if (!fu_common_mkdir_parent (filename, error))
856 return NULL;
857 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
858 return NULL;
859 return g_steal_pointer (&filename);
860}
861
862static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100863fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
864{
Richard Hughes3d178be2018-08-30 11:14:24 +0100865 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100866 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100867 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100868 g_autoptr(GPtrArray) devices_possible = NULL;
869 g_autoptr(GPtrArray) errors = NULL;
870 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100871 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100872
Mario Limonciello8949e892018-05-25 08:03:06 -0500873 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000874 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500875 return FALSE;
876
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100877 /* handle both forms */
878 if (g_strv_length (values) == 1) {
879 devices_possible = fu_engine_get_devices (priv->engine, error);
880 if (devices_possible == NULL)
881 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +0000882 fwupd_device_array_ensure_parents (devices_possible);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100883 } else if (g_strv_length (values) == 2) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +0100884 FuDevice *device = fu_util_get_device (priv, values[1], error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100885 if (device == NULL)
886 return FALSE;
887 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
888 g_ptr_array_add (devices_possible, device);
889 } else {
890 g_set_error_literal (error,
891 FWUPD_ERROR,
892 FWUPD_ERROR_INVALID_ARGS,
893 "Invalid arguments");
894 return FALSE;
895 }
896
Richard Hughes3d178be2018-08-30 11:14:24 +0100897 /* download if required */
898 filename = fu_util_download_if_required (priv, values[0], error);
899 if (filename == NULL)
900 return FALSE;
901
Richard Hughes481aa2a2018-09-18 20:51:46 +0100902 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100903 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500904 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100905 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100906 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500907 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100908 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
909 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100910 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600911 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100912 if (components == NULL)
913 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100914
Richard Hughes481aa2a2018-09-18 20:51:46 +0100915 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100916 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
917 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100918 for (guint i = 0; i < components->len; i++) {
919 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100920
921 /* do any devices pass the requirements */
922 for (guint j = 0; j < devices_possible->len; j++) {
923 FuDevice *device = g_ptr_array_index (devices_possible, j);
924 g_autoptr(FuInstallTask) task = NULL;
925 g_autoptr(GError) error_local = NULL;
926
927 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100928 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100929 if (!fu_engine_check_requirements (priv->engine,
Richard Hughesdf89cd52020-06-26 20:25:18 +0100930 priv->request,
931 task,
932 priv->flags | FWUPD_INSTALL_FLAG_FORCE,
Mario Limonciello537da0e2020-03-09 15:38:17 -0500933 &error_local)) {
934 g_debug ("first pass requirement on %s:%s failed: %s",
935 fu_device_get_id (device),
936 xb_node_query_text (component, "id", NULL),
937 error_local->message);
938 g_ptr_array_add (errors, g_steal_pointer (&error_local));
939 continue;
940 }
941
942 /* make a second pass using possibly updated version format now */
943 fu_engine_md_refresh_device_from_component (priv->engine, device, component);
944 if (!fu_engine_check_requirements (priv->engine,
Richard Hughesdf89cd52020-06-26 20:25:18 +0100945 priv->request,
946 task,
947 priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100948 &error_local)) {
Mario Limonciello537da0e2020-03-09 15:38:17 -0500949 g_debug ("second pass requirement on %s:%s failed: %s",
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100950 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100951 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100952 error_local->message);
953 g_ptr_array_add (errors, g_steal_pointer (&error_local));
954 continue;
955 }
956
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600957 /* if component should have an update message from CAB */
958 fu_device_incorporate_from_component (device, component);
959
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100960 /* success */
961 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
962 }
963 }
964
965 /* order the install tasks by the device priority */
966 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
967
968 /* nothing suitable */
969 if (install_tasks->len == 0) {
970 GError *error_tmp = fu_common_error_array_get_best (errors);
971 g_propagate_error (error, error_tmp);
972 return FALSE;
973 }
974
Mario Limonciello3f243a92019-01-21 22:05:23 -0600975 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500976 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600977 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500978
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100979 /* install all the tasks */
Richard Hughesdf89cd52020-06-26 20:25:18 +0100980 if (!fu_engine_install_tasks (priv->engine,
981 priv->request,
982 install_tasks,
983 blob_cab,
984 priv->flags,
985 error))
Richard Hughesdbd8c762018-06-15 20:31:40 +0100986 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100987
Mario Limonciello32241f42019-01-24 10:12:41 -0600988 fu_util_display_current_message (priv);
989
Mario Limonciello3f243a92019-01-21 22:05:23 -0600990 /* we don't want to ask anything */
991 if (priv->no_reboot_check) {
992 g_debug ("skipping reboot check");
993 return TRUE;
994 }
995
Mario Limonciello3143bad2019-02-27 07:31:00 -0600996 /* save the device state for other applications to see */
997 if (!fu_util_save_current_state (priv, error))
998 return FALSE;
999
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001000 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -06001001 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001002}
1003
Richard Hughes98ca9932018-05-18 10:24:07 +01001004static gboolean
Mario Limonciellofd734852019-08-01 16:41:42 -05001005fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error)
Mario Limonciello46aaee82019-01-10 12:58:00 -06001006{
Mario Limonciellofd734852019-08-01 16:41:42 -05001007 FwupdRemote *remote;
1008 const gchar *remote_id;
1009 const gchar *uri_tmp;
1010 g_auto(GStrv) argv = NULL;
Daniel Campello722f5322020-08-12 11:27:38 -06001011 g_autoptr(SoupURI) uri = NULL;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001012
Mario Limonciellofd734852019-08-01 16:41:42 -05001013 uri_tmp = fwupd_release_get_uri (rel);
1014 if (uri_tmp == NULL) {
1015 g_set_error_literal (error,
1016 FWUPD_ERROR,
1017 FWUPD_ERROR_INVALID_FILE,
1018 "release missing URI");
1019 return FALSE;
1020 }
1021 remote_id = fwupd_release_get_remote_id (rel);
1022 if (remote_id == NULL) {
1023 g_set_error (error,
1024 FWUPD_ERROR,
1025 FWUPD_ERROR_INVALID_FILE,
1026 "failed to find remote for %s",
1027 uri_tmp);
Richard Hughes747f5702019-08-06 14:27:26 +01001028 return FALSE;
Mario Limonciellofd734852019-08-01 16:41:42 -05001029 }
1030
1031 remote = fu_engine_get_remote_by_id (priv->engine,
1032 remote_id,
1033 error);
1034 if (remote == NULL)
Mario Limonciello46aaee82019-01-10 12:58:00 -06001035 return FALSE;
1036
Mario Limonciellofd734852019-08-01 16:41:42 -05001037 argv = g_new0 (gchar *, 2);
Daniel Campello722f5322020-08-12 11:27:38 -06001038 /* local remotes may have the firmware already */
1039 uri = soup_uri_new (uri_tmp);
1040 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL && uri == NULL) {
Mario Limonciellofd734852019-08-01 16:41:42 -05001041 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
1042 g_autofree gchar *path = g_path_get_dirname (fn_cache);
1043 argv[0] = g_build_filename (path, uri_tmp, NULL);
1044 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
1045 argv[0] = g_strdup (uri_tmp + 7);
1046 /* web remote, fu_util_install will download file */
1047 } else {
1048 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
1049 }
1050 return fu_util_install (priv, argv, error);
1051}
1052
1053static gboolean
1054fu_util_update_all (FuUtilPrivate *priv, GError **error)
1055{
1056 g_autoptr(GPtrArray) devices = NULL;
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001057 gboolean no_updates_header = FALSE;
1058 gboolean latest_header = FALSE;
Mario Limonciello3f243a92019-01-21 22:05:23 -06001059
Mario Limonciello46aaee82019-01-10 12:58:00 -06001060 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +00001061 if (devices == NULL)
1062 return FALSE;
Richard Hughes0ef47202020-01-06 13:59:09 +00001063 fwupd_device_array_ensure_parents (devices);
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001064 g_ptr_array_sort (devices, fu_util_sort_devices_by_flags_cb);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001065 for (guint i = 0; i < devices->len; i++) {
1066 FwupdDevice *dev = g_ptr_array_index (devices, i);
1067 FwupdRelease *rel;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001068 const gchar *device_id;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001069 g_autoptr(GPtrArray) rels = NULL;
1070 g_autoptr(GError) error_local = NULL;
1071
1072 if (!fu_util_is_interesting_device (dev))
1073 continue;
1074 /* only show stuff that has metadata available */
Mario Limonciello27164b72020-02-17 23:19:36 -06001075 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE))
Mario Limonciello46aaee82019-01-10 12:58:00 -06001076 continue;
Mario Limonciello27164b72020-02-17 23:19:36 -06001077 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001078 if (!no_updates_header) {
1079 /* TRANSLATORS: message letting the user know no device upgrade available due to missing on LVFS */
1080 g_printerr ("%s\n", _("Devices with no available firmware updates: "));
1081 no_updates_header = TRUE;
1082 }
1083 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -06001084 continue;
1085 }
Richard Hughes747f5702019-08-06 14:27:26 +01001086 if (!fu_util_filter_device (priv, dev))
1087 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001088
1089 device_id = fu_device_get_id (dev);
Richard Hughesdf89cd52020-06-26 20:25:18 +01001090 rels = fu_engine_get_upgrades (priv->engine,
1091 priv->request,
1092 device_id,
1093 &error_local);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001094 if (rels == NULL) {
Mario Limoncielloeb7be162020-07-01 15:38:47 -05001095 if (!latest_header) {
1096 /* TRANSLATORS: message letting the user know no device upgrade available */
1097 g_printerr ("%s\n", _("Devices with the latest available firmware version:"));
1098 latest_header = TRUE;
1099 }
1100 g_printerr (" • %s\n", fwupd_device_get_name (dev));
Mario Limonciello27164b72020-02-17 23:19:36 -06001101 /* discard the actual reason from user, but leave for debugging */
1102 g_debug ("%s", error_local->message);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001103 continue;
1104 }
1105
Mario Limonciello98b95162019-10-30 09:20:43 -05001106 if (!priv->no_safety_check) {
1107 if (!fu_util_prompt_warning (dev,
1108 fu_util_get_tree_title (priv),
1109 error))
1110 return FALSE;
1111 }
1112
Mario Limonciello46aaee82019-01-10 12:58:00 -06001113 rel = g_ptr_array_index (rels, 0);
Mario Limonciellofd734852019-08-01 16:41:42 -05001114 if (!fu_util_install_release (priv, rel, &error_local)) {
1115 g_printerr ("%s\n", error_local->message);
Richard Hughes747f5702019-08-06 14:27:26 +01001116 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001117 }
Mario Limonciellofd734852019-08-01 16:41:42 -05001118 fu_util_display_current_message (priv);
1119 }
1120 return TRUE;
1121}
1122
1123static gboolean
Mario Limonciello9917bb42020-04-20 13:41:27 -05001124fu_util_update_by_id (FuUtilPrivate *priv, const gchar *id, GError **error)
Mario Limonciellofd734852019-08-01 16:41:42 -05001125{
1126 FwupdRelease *rel;
1127 g_autoptr(FuDevice) dev = NULL;
1128 g_autoptr(GPtrArray) rels = NULL;
1129
Mario Limonciello9917bb42020-04-20 13:41:27 -05001130 /* do not allow a partial device-id, lookup GUIDs */
1131 dev = fu_util_get_device (priv, id, error);
Mario Limonciellofd734852019-08-01 16:41:42 -05001132 if (dev == NULL)
1133 return FALSE;
1134
1135 /* get the releases for this device and filter for validity */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001136 rels = fu_engine_get_upgrades (priv->engine,
1137 priv->request,
1138 fu_device_get_id (dev),
1139 error);
Mario Limonciellofd734852019-08-01 16:41:42 -05001140 if (rels == NULL)
1141 return FALSE;
1142 rel = g_ptr_array_index (rels, 0);
1143 if (!fu_util_install_release (priv, rel, error))
1144 return FALSE;
1145 fu_util_display_current_message (priv);
1146
1147 return TRUE;
1148}
1149
1150static gboolean
1151fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
1152{
Mario Limonciello0f109b02019-11-14 10:26:44 -06001153 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) {
1154 g_set_error_literal (error,
1155 FWUPD_ERROR,
1156 FWUPD_ERROR_INVALID_ARGS,
1157 "--allow-older is not supported for this command");
1158 return FALSE;
1159 }
1160
1161 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) {
1162 g_set_error_literal (error,
1163 FWUPD_ERROR,
1164 FWUPD_ERROR_INVALID_ARGS,
1165 "--allow-reinstall is not supported for this command");
1166 return FALSE;
1167 }
1168
Mario Limonciellofd734852019-08-01 16:41:42 -05001169 if (g_strv_length (values) > 1) {
1170 g_set_error_literal (error,
1171 FWUPD_ERROR,
1172 FWUPD_ERROR_INVALID_ARGS,
1173 "Invalid arguments");
1174 return FALSE;
1175 }
1176
1177 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1178 return FALSE;
1179
1180 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
1181 g_signal_connect (priv->engine, "device-changed",
1182 G_CALLBACK (fu_util_update_device_changed_cb), priv);
1183
1184 if (g_strv_length (values) == 1) {
1185 if (!fu_util_update_by_id (priv, values[0], error))
1186 return FALSE;
1187 } else {
1188 if (!fu_util_update_all (priv, error))
1189 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001190 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001191
1192 /* we don't want to ask anything */
1193 if (priv->no_reboot_check) {
1194 g_debug ("skipping reboot check");
1195 return TRUE;
1196 }
1197
Mario Limonciello3143bad2019-02-27 07:31:00 -06001198 /* save the device state for other applications to see */
1199 if (!fu_util_save_current_state (priv, error))
1200 return FALSE;
1201
Mario Limonciello3f243a92019-01-21 22:05:23 -06001202 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001203}
1204
1205static gboolean
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001206fu_util_reinstall (FuUtilPrivate *priv, gchar **values, GError **error)
1207{
1208 g_autoptr(FwupdRelease) rel = NULL;
1209 g_autoptr(GPtrArray) rels = NULL;
1210 g_autoptr(FuDevice) dev = NULL;
1211
1212 if (g_strv_length (values) != 1) {
1213 g_set_error_literal (error,
1214 FWUPD_ERROR,
1215 FWUPD_ERROR_INVALID_ARGS,
1216 "Invalid arguments");
1217 return FALSE;
1218 }
1219
1220 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1221 return FALSE;
1222
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001223 dev = fu_util_get_device (priv, values[0], error);
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001224 if (dev == NULL)
1225 return FALSE;
1226
1227 /* try to lookup/match release from client */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001228 rels = fu_engine_get_releases_for_device (priv->engine,
1229 priv->request,
1230 dev,
1231 error);
Filipe Laínsb2f377a2020-03-30 21:05:50 +01001232 if (rels == NULL)
1233 return FALSE;
1234
1235 for (guint j = 0; j < rels->len; j++) {
1236 FwupdRelease *rel_tmp = g_ptr_array_index (rels, j);
1237 if (fu_common_vercmp_full (fwupd_release_get_version (rel_tmp),
1238 fu_device_get_version (dev),
1239 fu_device_get_version_format (dev)) == 0) {
1240 rel = g_object_ref (rel_tmp);
1241 break;
1242 }
1243 }
1244 if (rel == NULL) {
1245 g_set_error (error,
1246 FWUPD_ERROR,
1247 FWUPD_ERROR_NOT_SUPPORTED,
1248 "Unable to locate release for %s version %s",
1249 fu_device_get_name (dev),
1250 fu_device_get_version (dev));
1251 return FALSE;
1252 }
1253
1254 /* update the console if composite devices are also updated */
1255 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
1256 g_signal_connect (priv->engine, "device-changed",
1257 G_CALLBACK (fu_util_update_device_changed_cb), priv);
1258 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1259 if (!fu_util_install_release (priv, rel, error))
1260 return FALSE;
1261 fu_util_display_current_message (priv);
1262
1263 /* we don't want to ask anything */
1264 if (priv->no_reboot_check) {
1265 g_debug ("skipping reboot check");
1266 return TRUE;
1267 }
1268
1269 /* save the device state for other applications to see */
1270 if (!fu_util_save_current_state (priv, error))
1271 return FALSE;
1272
1273 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
1274}
1275
1276static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +01001277fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
1278{
1279 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001280 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001281
1282 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001283 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001284 return FALSE;
1285
Richard Hughes98ca9932018-05-18 10:24:07 +01001286 /* get device */
1287 if (g_strv_length (values) >= 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001288 device = fu_util_get_device (priv, values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001289 if (device == NULL)
1290 return FALSE;
1291 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001292 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001293 if (device == NULL)
1294 return FALSE;
1295 }
1296
1297 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001298 locker = fu_device_locker_new (device, error);
1299 if (locker == NULL)
1300 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001301 return fu_device_detach (device, error);
1302}
1303
1304static gboolean
1305fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1306{
1307 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001308 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001309
1310 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001311 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001312 return FALSE;
1313
Richard Hughes98ca9932018-05-18 10:24:07 +01001314 /* get device */
1315 if (g_strv_length (values) >= 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001316 device = fu_util_get_device (priv, values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001317 if (device == NULL)
1318 return FALSE;
1319 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001320 device = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes98ca9932018-05-18 10:24:07 +01001321 if (device == NULL)
1322 return FALSE;
1323 }
1324
1325 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001326 locker = fu_device_locker_new (device, error);
1327 if (locker == NULL)
1328 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001329 return fu_device_attach (device, error);
1330}
1331
Richard Hughes1d894f12018-08-31 13:05:51 +01001332static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001333fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1334{
1335 gboolean has_pending = FALSE;
1336 g_autoptr(FuHistory) history = fu_history_new ();
1337 g_autoptr(GPtrArray) devices = NULL;
1338
1339 /* check the history database before starting the daemon */
1340 if (g_strv_length (values) == 0) {
1341 devices = fu_history_get_devices (history, error);
1342 if (devices == NULL)
1343 return FALSE;
1344 } else if (g_strv_length (values) == 1) {
1345 FuDevice *device;
1346 device = fu_history_get_device_by_id (history, values[0], error);
1347 if (device == NULL)
1348 return FALSE;
1349 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1350 g_ptr_array_add (devices, device);
1351 } else {
1352 g_set_error_literal (error,
1353 FWUPD_ERROR,
1354 FWUPD_ERROR_INVALID_ARGS,
1355 "Invalid arguments");
1356 return FALSE;
1357 }
1358
1359 /* nothing to do */
1360 for (guint i = 0; i < devices->len; i++) {
1361 FuDevice *dev = g_ptr_array_index (devices, i);
1362 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1363 fu_engine_add_plugin_filter (priv->engine,
1364 fu_device_get_plugin (dev));
1365 has_pending = TRUE;
1366 }
1367 }
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001368
dkadioglu1db7e982019-12-04 21:13:21 +01001369 if (!has_pending) {
1370 g_printerr ("No firmware to activate\n");
1371 return TRUE;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001372 }
1373
1374 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001375 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001376 return FALSE;
1377
1378 /* activate anything with _NEEDS_ACTIVATION */
1379 for (guint i = 0; i < devices->len; i++) {
1380 FuDevice *device = g_ptr_array_index (devices, i);
Richard Hughes747f5702019-08-06 14:27:26 +01001381 if (!fu_util_filter_device (priv, FWUPD_DEVICE (device)))
1382 continue;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001383 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1384 continue;
1385 /* TRANSLATORS: shown when shutting down to switch to the new version */
1386 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1387 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1388 return FALSE;
1389 }
1390
1391 return TRUE;
1392}
1393
1394static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001395fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1396{
1397 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1398 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1399 const gchar *hwid_keys[] = {
1400 FU_HWIDS_KEY_BIOS_VENDOR,
1401 FU_HWIDS_KEY_BIOS_VERSION,
1402 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1403 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1404 FU_HWIDS_KEY_MANUFACTURER,
1405 FU_HWIDS_KEY_FAMILY,
1406 FU_HWIDS_KEY_PRODUCT_NAME,
1407 FU_HWIDS_KEY_PRODUCT_SKU,
1408 FU_HWIDS_KEY_ENCLOSURE_KIND,
1409 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1410 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1411 NULL };
1412
1413 /* read DMI data */
1414 if (g_strv_length (values) == 0) {
1415 if (!fu_smbios_setup (smbios, error))
1416 return FALSE;
1417 } else if (g_strv_length (values) == 1) {
1418 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1419 return FALSE;
1420 } else {
1421 g_set_error_literal (error,
1422 FWUPD_ERROR,
1423 FWUPD_ERROR_INVALID_ARGS,
1424 "Invalid arguments");
1425 return FALSE;
1426 }
1427 if (!fu_hwids_setup (hwids, smbios, error))
1428 return FALSE;
1429
1430 /* show debug output */
1431 g_print ("Computer Information\n");
1432 g_print ("--------------------\n");
1433 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1434 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1435 if (tmp == NULL)
1436 continue;
1437 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1438 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1439 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1440 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1441 } else {
1442 g_print ("%s: %s\n", hwid_keys[i], tmp);
1443 }
1444 }
1445
1446 /* show GUIDs */
1447 g_print ("\nHardware IDs\n");
1448 g_print ("------------\n");
1449 for (guint i = 0; i < 15; i++) {
1450 const gchar *keys = NULL;
1451 g_autofree gchar *guid = NULL;
1452 g_autofree gchar *key = NULL;
1453 g_autofree gchar *keys_str = NULL;
1454 g_auto(GStrv) keysv = NULL;
1455 g_autoptr(GError) error_local = NULL;
1456
1457 /* get the GUID */
1458 key = g_strdup_printf ("HardwareID-%u", i);
1459 keys = fu_hwids_get_replace_keys (hwids, key);
1460 guid = fu_hwids_get_guid (hwids, key, &error_local);
1461 if (guid == NULL) {
1462 g_print ("%s\n", error_local->message);
1463 continue;
1464 }
1465
1466 /* show what makes up the GUID */
1467 keysv = g_strsplit (keys, "&", -1);
1468 keys_str = g_strjoinv (" + ", keysv);
1469 g_print ("{%s} <- %s\n", guid, keys_str);
1470 }
1471
1472 return TRUE;
1473}
1474
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001475static gboolean
1476fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1477{
1478 const gchar *script_fn = "startup.sh";
1479 const gchar *output_fn = "firmware.bin";
1480 g_autoptr(GBytes) archive_blob = NULL;
1481 g_autoptr(GBytes) firmware_blob = NULL;
1482 if (g_strv_length (values) < 2) {
1483 g_set_error_literal (error,
1484 FWUPD_ERROR,
1485 FWUPD_ERROR_INVALID_ARGS,
1486 "Invalid arguments");
1487 return FALSE;
1488 }
1489 archive_blob = fu_common_get_contents_bytes (values[0], error);
1490 if (archive_blob == NULL)
1491 return FALSE;
1492 if (g_strv_length (values) > 2)
1493 script_fn = values[2];
1494 if (g_strv_length (values) > 3)
1495 output_fn = values[3];
1496 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1497 if (firmware_blob == NULL)
1498 return FALSE;
1499 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1500}
1501
Richard Hughes3d607622019-03-07 16:59:27 +00001502static gboolean
1503fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1504{
1505 g_autofree gchar *sig = NULL;
1506
1507 /* check args */
1508 if (g_strv_length (values) != 1) {
1509 g_set_error_literal (error,
1510 FWUPD_ERROR,
1511 FWUPD_ERROR_INVALID_ARGS,
1512 "Invalid arguments: value expected");
1513 return FALSE;
1514 }
1515
1516 /* start engine */
1517 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1518 return FALSE;
1519 sig = fu_engine_self_sign (priv->engine, values[0],
Richard Hughesd5aab652020-02-25 12:47:50 +00001520 JCAT_SIGN_FLAG_ADD_TIMESTAMP |
1521 JCAT_SIGN_FLAG_ADD_CERT, error);
Richard Hughes3d607622019-03-07 16:59:27 +00001522 if (sig == NULL)
1523 return FALSE;
1524 g_print ("%s\n", sig);
1525 return TRUE;
1526}
1527
Mario Limonciello62f84862018-10-18 13:15:23 -05001528static void
1529fu_util_device_added_cb (FwupdClient *client,
1530 FwupdDevice *device,
1531 gpointer user_data)
1532{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001533 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001534 /* TRANSLATORS: this is when a device is hotplugged */
1535 g_print ("%s\n%s", _("Device added:"), tmp);
1536}
1537
1538static void
1539fu_util_device_removed_cb (FwupdClient *client,
1540 FwupdDevice *device,
1541 gpointer user_data)
1542{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001543 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001544 /* TRANSLATORS: this is when a device is hotplugged */
1545 g_print ("%s\n%s", _("Device removed:"), tmp);
1546}
1547
1548static void
1549fu_util_device_changed_cb (FwupdClient *client,
1550 FwupdDevice *device,
1551 gpointer user_data)
1552{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001553 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001554 /* TRANSLATORS: this is when a device has been updated */
1555 g_print ("%s\n%s", _("Device changed:"), tmp);
1556}
1557
1558static void
1559fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1560{
1561 /* TRANSLATORS: this is when the daemon state changes */
1562 g_print ("%s\n", _("Changed"));
1563}
1564
1565static gboolean
1566fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1567{
1568 g_autoptr(FwupdClient) client = fwupd_client_new ();
1569
1570 /* get all the devices */
1571 if (!fwupd_client_connect (client, priv->cancellable, error))
1572 return FALSE;
1573
1574 /* watch for any hotplugged device */
1575 g_signal_connect (client, "changed",
1576 G_CALLBACK (fu_util_changed_cb), priv);
1577 g_signal_connect (client, "device-added",
1578 G_CALLBACK (fu_util_device_added_cb), priv);
1579 g_signal_connect (client, "device-removed",
1580 G_CALLBACK (fu_util_device_removed_cb), priv);
1581 g_signal_connect (client, "device-changed",
1582 G_CALLBACK (fu_util_device_changed_cb), priv);
1583 g_signal_connect (priv->cancellable, "cancelled",
1584 G_CALLBACK (fu_util_cancelled_cb), priv);
1585 g_main_loop_run (priv->loop);
1586 return TRUE;
1587}
1588
Richard Hughes15684492019-03-15 16:27:50 +00001589static gboolean
Richard Hughes95c98a92019-10-22 16:03:15 +01001590fu_util_get_firmware_types (FuUtilPrivate *priv, gchar **values, GError **error)
1591{
1592 g_autoptr(GPtrArray) firmware_types = NULL;
1593
1594 /* load engine */
1595 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1596 return FALSE;
1597
1598 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1599 for (guint i = 0; i < firmware_types->len; i++) {
1600 const gchar *id = g_ptr_array_index (firmware_types, i);
1601 g_print ("%s\n", id);
1602 }
1603 if (firmware_types->len == 0) {
1604 /* TRANSLATORS: nothing found */
1605 g_print ("%s\n", _("No firmware IDs found"));
1606 return TRUE;
1607 }
1608
1609 return TRUE;
1610}
1611
1612static gchar *
1613fu_util_prompt_for_firmware_type (FuUtilPrivate *priv, GError **error)
1614{
1615 g_autoptr(GPtrArray) firmware_types = NULL;
1616 guint idx;
1617 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1618
1619 /* TRANSLATORS: get interactive prompt */
1620 g_print ("%s\n", _("Choose a firmware type:"));
1621 /* TRANSLATORS: this is to abort the interactive prompt */
1622 g_print ("0.\t%s\n", _("Cancel"));
1623 for (guint i = 0; i < firmware_types->len; i++) {
1624 const gchar *id = g_ptr_array_index (firmware_types, i);
1625 g_print ("%u.\t%s\n", i + 1, id);
1626 }
1627 idx = fu_util_prompt_for_number (firmware_types->len);
1628 if (idx == 0) {
1629 g_set_error_literal (error,
1630 FWUPD_ERROR,
1631 FWUPD_ERROR_NOTHING_TO_DO,
1632 "Request canceled");
1633 return NULL;
1634 }
1635
1636 return g_strdup (g_ptr_array_index (firmware_types, idx - 1));
1637}
1638
1639static gboolean
1640fu_util_firmware_parse (FuUtilPrivate *priv, gchar **values, GError **error)
1641{
1642 GType gtype;
1643 g_autoptr(GBytes) blob = NULL;
1644 g_autoptr(FuFirmware) firmware = NULL;
1645 g_autofree gchar *firmware_type = NULL;
1646 g_autofree gchar *str = NULL;
1647
1648 /* check args */
1649 if (g_strv_length (values) == 0 || g_strv_length (values) > 2) {
1650 g_set_error_literal (error,
1651 FWUPD_ERROR,
1652 FWUPD_ERROR_INVALID_ARGS,
1653 "Invalid arguments: filename required");
1654 return FALSE;
1655 }
1656
1657 if (g_strv_length (values) == 2)
1658 firmware_type = g_strdup (values[1]);
1659
1660 /* load file */
1661 blob = fu_common_get_contents_bytes (values[0], error);
1662 if (blob == NULL)
1663 return FALSE;
1664
1665 /* load engine */
1666 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1667 return FALSE;
1668
1669 /* find the GType to use */
1670 if (firmware_type == NULL)
1671 firmware_type = fu_util_prompt_for_firmware_type (priv, error);
1672 if (firmware_type == NULL)
1673 return FALSE;
1674 gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type);
1675 if (gtype == G_TYPE_INVALID) {
1676 g_set_error (error,
1677 G_IO_ERROR,
1678 G_IO_ERROR_NOT_FOUND,
1679 "GType %s not supported", firmware_type);
1680 return FALSE;
1681 }
1682 firmware = g_object_new (gtype, NULL);
1683 if (!fu_firmware_parse (firmware, blob, priv->flags, error))
1684 return FALSE;
1685 str = fu_firmware_to_string (firmware);
1686 g_print ("%s", str);
1687 return TRUE;
1688}
1689
1690static gboolean
Richard Hughesbca63ed2020-03-09 20:20:03 +00001691fu_util_firmware_convert (FuUtilPrivate *priv, gchar **values, GError **error)
1692{
1693 GType gtype_dst;
1694 GType gtype_src;
1695 g_autofree gchar *firmware_type_dst = NULL;
1696 g_autofree gchar *firmware_type_src = NULL;
1697 g_autofree gchar *str_dst = NULL;
1698 g_autofree gchar *str_src = NULL;
1699 g_autoptr(FuFirmware) firmware_dst = NULL;
1700 g_autoptr(FuFirmware) firmware_src = NULL;
1701 g_autoptr(GBytes) blob_dst = NULL;
1702 g_autoptr(GBytes) blob_src = NULL;
1703 g_autoptr(GPtrArray) images = NULL;
1704
1705 /* check args */
1706 if (g_strv_length (values) < 2 || g_strv_length (values) > 4) {
1707 g_set_error_literal (error,
1708 FWUPD_ERROR,
1709 FWUPD_ERROR_INVALID_ARGS,
1710 "Invalid arguments: filename required");
1711 return FALSE;
1712 }
1713
1714 if (g_strv_length (values) > 2)
1715 firmware_type_src = g_strdup (values[2]);
1716 if (g_strv_length (values) > 3)
1717 firmware_type_dst = g_strdup (values[3]);
1718
1719 /* load file */
1720 blob_src = fu_common_get_contents_bytes (values[0], error);
1721 if (blob_src == NULL)
1722 return FALSE;
1723
1724 /* load engine */
1725 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1726 return FALSE;
1727
1728 /* find the GType to use */
1729 if (firmware_type_src == NULL)
1730 firmware_type_src = fu_util_prompt_for_firmware_type (priv, error);
1731 if (firmware_type_src == NULL)
1732 return FALSE;
1733 if (firmware_type_dst == NULL)
1734 firmware_type_dst = fu_util_prompt_for_firmware_type (priv, error);
1735 if (firmware_type_dst == NULL)
1736 return FALSE;
1737 gtype_src = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_src);
1738 if (gtype_src == G_TYPE_INVALID) {
1739 g_set_error (error,
1740 G_IO_ERROR,
1741 G_IO_ERROR_NOT_FOUND,
1742 "GType %s not supported", firmware_type_src);
1743 return FALSE;
1744 }
1745 gtype_dst = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type_dst);
1746 if (gtype_dst == G_TYPE_INVALID) {
1747 g_set_error (error,
1748 G_IO_ERROR,
1749 G_IO_ERROR_NOT_FOUND,
1750 "GType %s not supported", firmware_type_dst);
1751 return FALSE;
1752 }
1753 firmware_src = g_object_new (gtype_src, NULL);
1754 if (!fu_firmware_parse (firmware_src, blob_src, priv->flags, error))
1755 return FALSE;
1756 str_src = fu_firmware_to_string (firmware_src);
1757 g_print ("%s", str_src);
1758
1759 /* copy images */
1760 firmware_dst = g_object_new (gtype_dst, NULL);
1761 images = fu_firmware_get_images (firmware_src);
1762 for (guint i = 0; i < images->len; i++) {
1763 FuFirmwareImage *img = g_ptr_array_index (images, i);
1764 fu_firmware_add_image (firmware_dst, img);
1765 }
1766
1767 /* write new file */
1768 blob_dst = fu_firmware_write (firmware_dst, error);
1769 if (blob_dst == NULL)
1770 return FALSE;
1771 if (!fu_common_set_contents_bytes (values[1], blob_dst, error))
1772 return FALSE;
1773 str_dst = fu_firmware_to_string (firmware_dst);
1774 g_print ("%s", str_dst);
1775
1776 /* success */
1777 return TRUE;
1778}
1779
1780static gboolean
Richard Hughes15684492019-03-15 16:27:50 +00001781fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1782{
1783 g_autofree gchar *str = NULL;
1784 g_autoptr(FuDevice) dev = NULL;
1785
1786 /* load engine */
1787 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1788 return FALSE;
1789
1790 /* get device */
1791 if (g_strv_length (values) == 1) {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001792 dev = fu_util_get_device (priv, values[1], error);
Richard Hughes15684492019-03-15 16:27:50 +00001793 if (dev == NULL)
1794 return FALSE;
1795 } else {
Richard Hughes3aaf53c2020-04-20 09:39:46 +01001796 dev = fu_util_prompt_for_device (priv, NULL, error);
Richard Hughes15684492019-03-15 16:27:50 +00001797 if (dev == NULL)
1798 return FALSE;
1799 }
1800
1801 /* add checksums */
1802 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1803 return FALSE;
1804
1805 /* show checksums */
1806 str = fu_device_to_string (dev);
1807 g_print ("%s\n", str);
1808 return TRUE;
1809}
1810
Mario Limonciellofe593942019-04-03 13:48:52 -05001811static gboolean
1812fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
1813{
1814 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001815 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001816 g_autofree gchar *title = NULL;
Mario Limonciellofe593942019-04-03 13:48:52 -05001817
1818 /* load engine */
1819 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1820 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001821 title = fu_util_get_tree_title (priv);
Mario Limonciellofe593942019-04-03 13:48:52 -05001822
1823 /* get all devices from the history database */
1824 devices = fu_engine_get_history (priv->engine, error);
1825 if (devices == NULL)
1826 return FALSE;
1827
1828 /* show each device */
1829 for (guint i = 0; i < devices->len; i++) {
Mario Limoncielloce94d162019-09-20 13:37:36 -05001830 g_autoptr(GPtrArray) rels = NULL;
Mario Limonciellofe593942019-04-03 13:48:52 -05001831 FwupdDevice *dev = g_ptr_array_index (devices, i);
Mario Limonciello3be596b2019-09-20 10:10:08 -05001832 FwupdRelease *rel;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001833 const gchar *remote;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001834 GNode *child;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001835
Richard Hughes747f5702019-08-06 14:27:26 +01001836 if (!fu_util_filter_device (priv, dev))
1837 continue;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001838 child = g_node_append_data (root, dev);
1839
1840 rel = fwupd_device_get_release_default (dev);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001841 if (rel == NULL)
1842 continue;
1843 remote = fwupd_release_get_remote_id (rel);
1844
1845 /* doesn't actually map to remote */
1846 if (remote == NULL) {
Mario Limonciello3be596b2019-09-20 10:10:08 -05001847 g_node_append_data (child, rel);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001848 continue;
1849 }
1850
1851 /* try to lookup releases from client */
Richard Hughesdf89cd52020-06-26 20:25:18 +01001852 rels = fu_engine_get_releases (priv->engine,
1853 priv->request,
1854 fwupd_device_get_id (dev),
1855 error);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001856 if (rels == NULL)
1857 return FALSE;
1858
1859 /* map to a release in client */
1860 for (guint j = 0; j < rels->len; j++) {
1861 FwupdRelease *rel2 = g_ptr_array_index (rels, j);
1862 if (g_strcmp0 (remote,
1863 fwupd_release_get_remote_id (rel2)) != 0)
1864 continue;
1865 if (g_strcmp0 (fwupd_release_get_version (rel),
1866 fwupd_release_get_version (rel2)) != 0)
1867 continue;
1868 g_node_append_data (child, g_object_ref (rel2));
1869 rel = NULL;
1870 break;
1871 }
1872
1873 /* didn't match anything */
1874 if (rels->len == 0 || rel != NULL) {
1875 g_node_append_data (child, rel);
1876 continue;
1877 }
Mario Limonciello3be596b2019-09-20 10:10:08 -05001878
Mario Limonciellofe593942019-04-03 13:48:52 -05001879 }
Mario Limonciello20cc9ee2019-09-05 07:27:26 -05001880 fu_util_print_tree (root, title);
Mario Limonciellofe593942019-04-03 13:48:52 -05001881
1882 return TRUE;
1883}
1884
Richard Hughesfd7e9942020-01-17 14:09:13 +00001885static gboolean
Richard Hughes4959baa2020-01-17 14:33:00 +00001886fu_util_refresh_remote (FuUtilPrivate *priv, FwupdRemote *remote, GError **error)
1887{
Richard Hughesc5710d92020-06-26 11:08:25 +01001888 const gchar *metadata_uri = NULL;
Richard Hughes4959baa2020-01-17 14:33:00 +00001889 g_autofree gchar *fn_raw = NULL;
1890 g_autofree gchar *fn_sig = NULL;
1891 g_autoptr(GBytes) bytes_raw = NULL;
1892 g_autoptr(GBytes) bytes_sig = NULL;
1893
1894 /* payload */
Richard Hughesc5710d92020-06-26 11:08:25 +01001895 metadata_uri = fwupd_remote_get_metadata_uri (remote);
1896 if (metadata_uri == NULL) {
Richard Hughesfb6315f2020-09-09 17:05:20 +01001897 g_set_error (error,
1898 FWUPD_ERROR,
1899 FWUPD_ERROR_NOTHING_TO_DO,
1900 "no metadata URI available for %s",
1901 fwupd_remote_get_id (remote));
Richard Hughesc5710d92020-06-26 11:08:25 +01001902 return FALSE;
1903 }
1904 fn_raw = fu_util_get_user_cache_path (metadata_uri);
Richard Hughes4959baa2020-01-17 14:33:00 +00001905 if (!fu_common_mkdir_parent (fn_raw, error))
1906 return FALSE;
Richard Hughesc5710d92020-06-26 11:08:25 +01001907 if (!fu_util_download_out_of_process (metadata_uri, fn_raw, error))
Richard Hughes4959baa2020-01-17 14:33:00 +00001908 return FALSE;
1909 bytes_raw = fu_common_get_contents_bytes (fn_raw, error);
1910 if (bytes_raw == NULL)
1911 return FALSE;
1912
1913 /* signature */
Richard Hughesc5710d92020-06-26 11:08:25 +01001914 metadata_uri = fwupd_remote_get_metadata_uri_sig (remote);
1915 if (metadata_uri == NULL) {
Richard Hughesfb6315f2020-09-09 17:05:20 +01001916 g_set_error (error,
1917 FWUPD_ERROR,
1918 FWUPD_ERROR_NOTHING_TO_DO,
1919 "no metadata signature URI available for %s",
1920 fwupd_remote_get_id (remote));
Richard Hughesc5710d92020-06-26 11:08:25 +01001921 return FALSE;
1922 }
1923 fn_sig = fu_util_get_user_cache_path (metadata_uri);
1924 if (!fu_util_download_out_of_process (metadata_uri, fn_sig, error))
Richard Hughes4959baa2020-01-17 14:33:00 +00001925 return FALSE;
1926 bytes_sig = fu_common_get_contents_bytes (fn_sig, error);
1927 if (bytes_sig == NULL)
1928 return FALSE;
1929
1930 /* send to daemon */
1931 g_debug ("updating %s", fwupd_remote_get_id (remote));
1932 return fu_engine_update_metadata_bytes (priv->engine,
1933 fwupd_remote_get_id (remote),
1934 bytes_raw,
1935 bytes_sig,
1936 error);
1937
1938}
1939
1940static gboolean
1941fu_util_refresh (FuUtilPrivate *priv, gchar **values, GError **error)
1942{
1943 g_autoptr(GPtrArray) remotes = NULL;
1944
1945 /* load engine */
1946 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1947 return FALSE;
1948
1949 /* download new metadata */
1950 remotes = fu_engine_get_remotes (priv->engine, error);
1951 if (remotes == NULL)
1952 return FALSE;
1953 for (guint i = 0; i < remotes->len; i++) {
1954 FwupdRemote *remote = g_ptr_array_index (remotes, i);
1955 if (!fwupd_remote_get_enabled (remote))
1956 continue;
1957 if (fwupd_remote_get_kind (remote) != FWUPD_REMOTE_KIND_DOWNLOAD)
1958 continue;
1959 if (!fu_util_refresh_remote (priv, remote, error))
1960 return FALSE;
1961 }
1962 return TRUE;
1963}
1964
1965static gboolean
Richard Hughesfd7e9942020-01-17 14:09:13 +00001966fu_util_get_remotes (FuUtilPrivate *priv, gchar **values, GError **error)
1967{
1968 g_autoptr(GNode) root = g_node_new (NULL);
1969 g_autoptr(GPtrArray) remotes = NULL;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001970 g_autofree gchar *title = NULL;
Richard Hughesfd7e9942020-01-17 14:09:13 +00001971
1972 /* load engine */
1973 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1974 return FALSE;
Mario Limonciellodc9a1a82020-02-20 14:20:20 -06001975 title = fu_util_get_tree_title (priv);
Richard Hughesfd7e9942020-01-17 14:09:13 +00001976
1977 /* list remotes */
1978 remotes = fu_engine_get_remotes (priv->engine, error);
1979 if (remotes == NULL)
1980 return FALSE;
1981 if (remotes->len == 0) {
1982 g_set_error_literal (error,
1983 FWUPD_ERROR,
1984 FWUPD_ERROR_NOTHING_TO_DO,
1985 "no remotes available");
1986 return FALSE;
1987 }
1988 for (guint i = 0; i < remotes->len; i++) {
1989 FwupdRemote *remote_tmp = g_ptr_array_index (remotes, i);
1990 g_node_append_data (root, remote_tmp);
1991 }
1992 fu_util_print_tree (root, title);
1993
1994 return TRUE;
1995}
1996
Richard Hughes196c6c62020-05-11 19:42:47 +01001997static gboolean
1998fu_util_security (FuUtilPrivate *priv, gchar **values, GError **error)
1999{
Richard Hughes5c82b942020-09-14 12:24:06 +01002000 FuSecurityAttrToStringFlags flags = FU_SECURITY_ATTR_TO_STRING_FLAG_NONE;
Richard Hughesf58ac732020-05-12 15:23:44 +01002001 g_autoptr(FuSecurityAttrs) attrs = NULL;
2002 g_autoptr(GPtrArray) items = NULL;
Richard Hughes196c6c62020-05-11 19:42:47 +01002003 g_autofree gchar *str = NULL;
2004
2005 /* not ready yet */
2006 if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
2007 g_set_error_literal (error,
2008 FWUPD_ERROR,
2009 FWUPD_ERROR_NOT_SUPPORTED,
2010 "The HSI specification is not yet complete. "
2011 "To ignore this warning, use --force");
2012 return FALSE;
2013 }
2014
2015 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
2016 return FALSE;
2017
2018 /* TRANSLATORS: this is a string like 'HSI:2-U' */
2019 g_print ("%s \033[1m%s\033[0m\n", _("Host Security ID:"),
2020 fu_engine_get_host_security_id (priv->engine));
2021
Richard Hughes5c82b942020-09-14 12:24:06 +01002022 /* show or hide different elements */
2023 if (priv->show_all) {
2024 flags |= FU_SECURITY_ATTR_TO_STRING_FLAG_SHOW_OBSOLETES;
2025 flags |= FU_SECURITY_ATTR_TO_STRING_FLAG_SHOW_URLS;
2026 }
2027
Richard Hughes196c6c62020-05-11 19:42:47 +01002028 /* print the "why" */
Richard Hughes56e7ae52020-05-17 21:00:23 +01002029 attrs = fu_engine_get_host_security_attrs (priv->engine);
Richard Hughesf58ac732020-05-12 15:23:44 +01002030 items = fu_security_attrs_get_all (attrs);
Richard Hughes5c82b942020-09-14 12:24:06 +01002031 str = fu_util_security_attrs_to_string (items, flags);
Richard Hughes196c6c62020-05-11 19:42:47 +01002032 g_print ("%s\n", str);
2033 return TRUE;
2034}
2035
Richard Hughesa83deb42020-08-12 12:45:36 +01002036static FuVolume *
2037fu_util_prompt_for_volume (GError **error)
2038{
2039 FuVolume *volume;
2040 guint idx;
2041 g_autoptr(GPtrArray) volumes = NULL;
2042
2043 /* exactly one */
2044 volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_ESP, error);
Jochen Sprickerhof25b78a62020-09-10 23:57:02 +02002045 if (volumes == NULL)
2046 return NULL;
Richard Hughesa83deb42020-08-12 12:45:36 +01002047 if (volumes->len == 1) {
2048 volume = g_ptr_array_index (volumes, 0);
Richard Hughes59761252020-08-20 07:51:36 +01002049 /* TRANSLATORS: Volume has been chosen by the user */
Richard Hughesa83deb42020-08-12 12:45:36 +01002050 g_print ("%s: %s\n", _("Selected volume"), fu_volume_get_id (volume));
2051 return g_object_ref (volume);
2052 }
2053
2054 /* TRANSLATORS: get interactive prompt */
2055 g_print ("%s\n", _("Choose a volume:"));
2056 /* TRANSLATORS: this is to abort the interactive prompt */
2057 g_print ("0.\t%s\n", _("Cancel"));
2058 for (guint i = 0; i < volumes->len; i++) {
2059 volume = g_ptr_array_index (volumes, i);
2060 g_print ("%u.\t%s\n", i + 1, fu_volume_get_id (volume));
2061 }
2062 idx = fu_util_prompt_for_number (volumes->len);
2063 if (idx == 0) {
2064 g_set_error_literal (error,
2065 FWUPD_ERROR,
2066 FWUPD_ERROR_NOTHING_TO_DO,
2067 "Request canceled");
2068 return NULL;
2069 }
2070 volume = g_ptr_array_index (volumes, idx - 1);
2071 return g_object_ref (volume);
2072
2073}
2074
2075static gboolean
2076fu_util_esp_mount (FuUtilPrivate *priv, gchar **values, GError **error)
2077{
2078 g_autoptr(FuVolume) volume = NULL;
2079 volume = fu_util_prompt_for_volume (error);
2080 if (volume == NULL)
2081 return FALSE;
2082 return fu_volume_mount (volume, error);
2083}
2084
2085static gboolean
2086fu_util_esp_unmount (FuUtilPrivate *priv, gchar **values, GError **error)
2087{
2088 g_autoptr(FuVolume) volume = NULL;
2089 volume = fu_util_prompt_for_volume (error);
2090 if (volume == NULL)
2091 return FALSE;
2092 return fu_volume_unmount (volume, error);
2093}
2094
2095static gboolean
2096fu_util_esp_list (FuUtilPrivate *priv, gchar **values, GError **error)
2097{
2098 g_autofree gchar *mount_point = NULL;
2099 g_autoptr(FuDeviceLocker) locker = NULL;
2100 g_autoptr(FuVolume) volume = NULL;
2101 g_autoptr(GPtrArray) files = NULL;
2102
2103 volume = fu_util_prompt_for_volume (error);
2104 if (volume == NULL)
2105 return FALSE;
2106 locker = fu_volume_locker (volume, error);
2107 if (locker == NULL)
2108 return FALSE;
2109 mount_point = fu_volume_get_mount_point (volume);
2110 files = fu_common_get_files_recursive (mount_point, error);
2111 if (files == NULL)
2112 return FALSE;
2113 for (guint i = 0; i < files->len; i++) {
2114 const gchar *fn = g_ptr_array_index (files, i);
2115 g_print ("%s\n", fn);
2116 }
2117 return TRUE;
2118}
2119
Richard Hughesb5976832018-05-18 10:02:09 +01002120int
2121main (int argc, char *argv[])
2122{
Richard Hughes460226a2018-05-21 20:56:21 +01002123 gboolean allow_older = FALSE;
2124 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01002125 gboolean force = FALSE;
2126 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002127 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002128 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002129 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01002130 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
2131 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00002132 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01002133 g_autofree gchar *cmd_descriptions = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +01002134 g_autofree gchar *filter = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01002135 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002136 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
2137 /* TRANSLATORS: command line option */
2138 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01002139 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
2140 /* TRANSLATORS: command line option */
Mario Limonciello350fc4c2019-10-03 07:08:51 -05002141 _("Allow reinstalling existing firmware versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01002142 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
2143 /* TRANSLATORS: command line option */
2144 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01002145 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
2146 /* TRANSLATORS: command line option */
2147 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06002148 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
2149 /* TRANSLATORS: command line option */
2150 _("Do not check for reboot after update"), NULL },
Mario Limonciello98b95162019-10-30 09:20:43 -05002151 { "no-safety-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_safety_check,
2152 /* TRANSLATORS: command line option */
2153 _("Do not perform device safety checks"), NULL },
Richard Hughes5c82b942020-09-14 12:24:06 +01002154 { "show-all", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all,
2155 /* TRANSLATORS: command line option */
2156 _("Show all results"), NULL },
2157 { "show-all-devices", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &priv->show_all,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05002158 /* TRANSLATORS: command line option */
2159 _("Show devices that are not updatable"), NULL },
Richard Hughesf8c10c22020-07-20 21:01:39 +01002160 { "plugins", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002161 /* TRANSLATORS: command line option */
Richard Hughes85226fd2020-06-30 14:43:48 +01002162 _("Manually enable specific plugins"), NULL },
Richard Hughesa1ebcbf2020-09-14 15:27:43 +01002163 { "plugin-whitelist", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
2164 /* TRANSLATORS: command line option */
2165 _("Manually enable specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06002166 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00002167 /* TRANSLATORS: command line option */
2168 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06002169 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00002170 /* TRANSLATORS: command line option */
2171 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06002172 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
2173 /* TRANSLATORS: command line option */
2174 _("Save device state into a JSON file between executions"), NULL },
Richard Hughes0e46b222019-09-05 12:13:35 +01002175 { "disable-ssl-strict", '\0', 0, G_OPTION_ARG_NONE, &priv->disable_ssl_strict,
2176 /* TRANSLATORS: command line option */
2177 _("Ignore SSL strict checks when downloading files"), NULL },
Richard Hughes747f5702019-08-06 14:27:26 +01002178 { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter,
2179 /* TRANSLATORS: command line option */
2180 _("Filter with a set of device flags using a ~ prefix to "
2181 "exclude, e.g. 'internal,~needs-reboot'"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01002182 { NULL}
2183 };
2184
Richard Hughes429f72b2020-01-16 12:18:19 +00002185#ifdef _WIN32
2186 /* workaround Windows setting the codepage to 1252 */
2187 g_setenv ("LANG", "C.UTF-8", FALSE);
2188#endif
2189
Richard Hughesb5976832018-05-18 10:02:09 +01002190 setlocale (LC_ALL, "");
2191
Richard Hughes668ee212019-11-22 09:17:46 +00002192 bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR);
Richard Hughesb5976832018-05-18 10:02:09 +01002193 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2194 textdomain (GETTEXT_PACKAGE);
2195
Richard Hughes01c0bad2019-11-22 09:08:51 +00002196#ifdef HAVE_GETUID
Richard Hughesb5976832018-05-18 10:02:09 +01002197 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002198 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01002199 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05002200 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughes01c0bad2019-11-22 09:08:51 +00002201#endif
Richard Hughesb5976832018-05-18 10:02:09 +01002202
2203 /* create helper object */
2204 priv->loop = g_main_loop_new (NULL, FALSE);
2205 priv->progressbar = fu_progressbar_new ();
Richard Hughesdf89cd52020-06-26 20:25:18 +01002206 priv->request = fu_engine_request_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01002207
2208 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00002209 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05002210 "build-firmware",
2211 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
2212 /* TRANSLATORS: command description */
2213 _("Build firmware using a sandbox"),
2214 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00002215 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01002216 "smbios-dump",
2217 "FILE",
2218 /* TRANSLATORS: command description */
2219 _("Dump SMBIOS data from a file"),
2220 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00002221 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002222 "get-plugins",
2223 NULL,
2224 /* TRANSLATORS: command description */
2225 _("Get all enabled plugins registered with the system"),
2226 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00002227 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05002228 "get-details",
2229 NULL,
2230 /* TRANSLATORS: command description */
2231 _("Gets details about a firmware file"),
2232 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00002233 fu_util_cmd_array_add (cmd_array,
Mario Limonciellofe593942019-04-03 13:48:52 -05002234 "get-history",
2235 NULL,
2236 /* TRANSLATORS: command description */
2237 _("Show history of firmware updates"),
2238 fu_util_get_history);
2239 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05002240 "get-updates,get-upgrades",
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06002241 NULL,
2242 /* TRANSLATORS: command description */
2243 _("Gets the list of updates for connected hardware"),
2244 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00002245 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1a9127d2019-08-27 11:32:51 +01002246 "get-devices,get-topology",
Richard Hughes98ca9932018-05-18 10:24:07 +01002247 NULL,
2248 /* TRANSLATORS: command description */
2249 _("Get all devices that support firmware updates"),
2250 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00002251 fu_util_cmd_array_add (cmd_array,
Richard Hughes747f5702019-08-06 14:27:26 +01002252 "get-device-flags",
2253 NULL,
2254 /* TRANSLATORS: command description */
2255 _("Get all device flags supported by fwupd"),
2256 fu_util_get_device_flags);
2257 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002258 "watch",
2259 NULL,
2260 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02002261 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01002262 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00002263 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002264 "install-blob",
2265 "FILENAME DEVICE-ID",
2266 /* TRANSLATORS: command description */
2267 _("Install a firmware blob on a device"),
2268 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00002269 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01002270 "install",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002271 "FILE [DEVICE-ID|GUID]",
Richard Hughesa36c9cf2018-05-20 10:41:44 +01002272 /* TRANSLATORS: command description */
2273 _("Install a firmware file on this hardware"),
2274 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00002275 fu_util_cmd_array_add (cmd_array,
Filipe Laínsb2f377a2020-03-30 21:05:50 +01002276 "reinstall",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002277 "DEVICE-ID|GUID",
Filipe Laínsb2f377a2020-03-30 21:05:50 +01002278 /* TRANSLATORS: command description */
2279 _("Reinstall firmware on a device"),
2280 fu_util_reinstall);
2281 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002282 "attach",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002283 "DEVICE-ID|GUID",
Richard Hughes98ca9932018-05-18 10:24:07 +01002284 /* TRANSLATORS: command description */
2285 _("Attach to firmware mode"),
2286 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00002287 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01002288 "detach",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002289 "DEVICE-ID|GUID",
Richard Hughes98ca9932018-05-18 10:24:07 +01002290 /* TRANSLATORS: command description */
2291 _("Detach to bootloader mode"),
2292 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00002293 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002294 "activate",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002295 "[DEVICE-ID|GUID]",
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002296 /* TRANSLATORS: command description */
2297 _("Activate pending devices"),
2298 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00002299 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01002300 "hwids",
2301 "[FILE]",
2302 /* TRANSLATORS: command description */
2303 _("Return all the hardware IDs for the machine"),
2304 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00002305 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05002306 "monitor",
2307 NULL,
2308 /* TRANSLATORS: command description */
2309 _("Monitor the daemon for events"),
2310 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00002311 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05002312 "update,upgrade",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002313 "[DEVICE-ID|GUID]",
Mario Limonciello46aaee82019-01-10 12:58:00 -06002314 /* TRANSLATORS: command description */
2315 _("Update all devices that match local metadata"),
2316 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00002317 fu_util_cmd_array_add (cmd_array,
2318 "self-sign",
2319 "TEXT",
2320 /* TRANSLATORS: command description */
Richard Hughes12a021d2019-03-27 09:23:24 +00002321 C_("command-description",
2322 "Sign data using the client certificate"),
Richard Hughes3d607622019-03-07 16:59:27 +00002323 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00002324 fu_util_cmd_array_add (cmd_array,
2325 "verify-update",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002326 "[DEVICE-ID|GUID]",
Richard Hughes15684492019-03-15 16:27:50 +00002327 /* TRANSLATORS: command description */
2328 _("Update the stored metadata with current contents"),
2329 fu_util_verify_update);
Richard Hughes95c98a92019-10-22 16:03:15 +01002330 fu_util_cmd_array_add (cmd_array,
Richard Hughesa58510b2019-10-30 10:03:12 +00002331 "firmware-read",
Richard Hughes3aaf53c2020-04-20 09:39:46 +01002332 "FILENAME [DEVICE-ID|GUID]",
Richard Hughesa58510b2019-10-30 10:03:12 +00002333 /* TRANSLATORS: command description */
2334 _("Read a firmware blob from a device"),
2335 fu_util_firmware_read);
2336 fu_util_cmd_array_add (cmd_array,
Richard Hughesbca63ed2020-03-09 20:20:03 +00002337 "firmware-convert",
2338 "FILENAME-SRC FILENAME-DST [FIRMWARE-TYPE-SRC] [FIRMWARE-TYPE-DST]",
2339 /* TRANSLATORS: command description */
2340 _("Convert a firmware file"),
2341 fu_util_firmware_convert);
2342 fu_util_cmd_array_add (cmd_array,
Richard Hughes95c98a92019-10-22 16:03:15 +01002343 "firmware-parse",
Mario Limonciello234c8642020-01-08 20:07:29 -06002344 "FILENAME [FIRMWARE-TYPE]",
Richard Hughes95c98a92019-10-22 16:03:15 +01002345 /* TRANSLATORS: command description */
2346 _("Parse and show details about a firmware file"),
2347 fu_util_firmware_parse);
2348 fu_util_cmd_array_add (cmd_array,
2349 "get-firmware-types",
2350 NULL,
2351 /* TRANSLATORS: command description */
2352 _("List the available firmware types"),
2353 fu_util_get_firmware_types);
Richard Hughesfd7e9942020-01-17 14:09:13 +00002354 fu_util_cmd_array_add (cmd_array,
2355 "get-remotes",
2356 NULL,
2357 /* TRANSLATORS: command description */
2358 _("Gets the configured remotes"),
2359 fu_util_get_remotes);
Richard Hughes4959baa2020-01-17 14:33:00 +00002360 fu_util_cmd_array_add (cmd_array,
2361 "refresh",
2362 NULL,
2363 /* TRANSLATORS: command description */
2364 _("Refresh metadata from remote server"),
2365 fu_util_refresh);
Richard Hughes196c6c62020-05-11 19:42:47 +01002366 fu_util_cmd_array_add (cmd_array,
2367 "security",
2368 NULL,
2369 /* TRANSLATORS: command description */
2370 _("Gets the host security attributes."),
2371 fu_util_security);
Richard Hughesa83deb42020-08-12 12:45:36 +01002372 fu_util_cmd_array_add (cmd_array,
2373 "esp-mount",
2374 NULL,
2375 /* TRANSLATORS: command description */
2376 _("Mounts the ESP."),
2377 fu_util_esp_mount);
2378 fu_util_cmd_array_add (cmd_array,
2379 "esp-unmount",
2380 NULL,
2381 /* TRANSLATORS: command description */
2382 _("Unmounts the ESP."),
2383 fu_util_esp_unmount);
2384 fu_util_cmd_array_add (cmd_array,
2385 "esp-list",
2386 NULL,
2387 /* TRANSLATORS: command description */
2388 _("Lists files on the ESP."),
2389 fu_util_esp_list);
Richard Hughesb5976832018-05-18 10:02:09 +01002390
2391 /* do stuff on ctrl+c */
2392 priv->cancellable = g_cancellable_new ();
Richard Hughes9e5675e2019-11-22 09:35:03 +00002393#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +01002394 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
2395 SIGINT, fu_util_sigint_cb,
2396 priv, NULL);
Richard Hughes9e5675e2019-11-22 09:35:03 +00002397#endif
Richard Hughesb5976832018-05-18 10:02:09 +01002398 g_signal_connect (priv->cancellable, "cancelled",
2399 G_CALLBACK (fu_util_cancelled_cb), priv);
2400
2401 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00002402 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01002403
Mario Limonciello3f243a92019-01-21 22:05:23 -06002404 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06002405 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06002406 priv->no_reboot_check = TRUE;
Mario Limonciello98b95162019-10-30 09:20:43 -05002407 priv->no_safety_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06002408 fu_progressbar_set_interactive (priv->progressbar, FALSE);
Richard Hughesdf89cd52020-06-26 20:25:18 +01002409 } else {
2410 /* set our implemented feature set */
2411 fu_engine_request_set_feature_flags (priv->request,
2412 FWUPD_FEATURE_FLAG_DETACH_ACTION |
2413 FWUPD_FEATURE_FLAG_UPDATE_ACTION);
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06002414 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06002415
Richard Hughesb5976832018-05-18 10:02:09 +01002416 /* get a list of the commands */
2417 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00002418 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01002419 g_option_context_set_summary (priv->context, cmd_descriptions);
2420 g_option_context_set_description (priv->context,
2421 "This tool allows an administrator to use the fwupd plugins "
2422 "without being installed on the host system.");
2423
2424 /* TRANSLATORS: program name */
2425 g_set_application_name (_("Firmware Utility"));
2426 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05002427 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01002428 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
2429 if (!ret) {
2430 /* TRANSLATORS: the user didn't read the man page */
2431 g_print ("%s: %s\n", _("Failed to parse arguments"),
2432 error->message);
2433 return EXIT_FAILURE;
2434 }
2435
Richard Hughes0e46b222019-09-05 12:13:35 +01002436 /* allow disabling SSL strict mode for broken corporate proxies */
2437 if (priv->disable_ssl_strict) {
2438 /* TRANSLATORS: try to help */
2439 g_printerr ("%s\n", _("WARNING: Ignoring SSL strict checks, "
2440 "to do this automatically in the future "
2441 "export DISABLE_SSL_STRICT in your environment"));
2442 g_setenv ("DISABLE_SSL_STRICT", "1", TRUE);
2443 }
2444
Richard Hughes747f5702019-08-06 14:27:26 +01002445 /* parse filter flags */
2446 if (filter != NULL) {
2447 if (!fu_util_parse_filter_flags (filter,
2448 &priv->filter_include,
2449 &priv->filter_exclude,
2450 &error)) {
2451 /* TRANSLATORS: the user didn't read the man page */
2452 g_print ("%s: %s\n", _("Failed to parse flags for --filter"),
2453 error->message);
2454 return EXIT_FAILURE;
2455 }
2456 }
2457
2458
Richard Hughes460226a2018-05-21 20:56:21 +01002459 /* set flags */
Richard Hughes460226a2018-05-21 20:56:21 +01002460 if (allow_reinstall)
2461 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
2462 if (allow_older)
2463 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
2464 if (force)
2465 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
2466
Richard Hughes98ca9932018-05-18 10:24:07 +01002467 /* load engine */
2468 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
2469 g_signal_connect (priv->engine, "device-added",
2470 G_CALLBACK (fu_main_engine_device_added_cb),
2471 priv);
2472 g_signal_connect (priv->engine, "device-removed",
2473 G_CALLBACK (fu_main_engine_device_removed_cb),
2474 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01002475 g_signal_connect (priv->engine, "status-changed",
2476 G_CALLBACK (fu_main_engine_status_changed_cb),
2477 priv);
2478 g_signal_connect (priv->engine, "percentage-changed",
2479 G_CALLBACK (fu_main_engine_percentage_changed_cb),
2480 priv);
2481
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05002482 /* just show versions and exit */
2483 if (version) {
2484 g_autofree gchar *version_str = fu_util_get_versions ();
2485 g_print ("%s\n", version_str);
2486 return EXIT_SUCCESS;
2487 }
2488
Richard Hughes85226fd2020-06-30 14:43:48 +01002489 /* any plugin allowlist specified */
Richard Hughesc02ee4d2018-05-22 15:46:03 +01002490 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
2491 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
2492
Richard Hughesb5976832018-05-18 10:02:09 +01002493 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00002494 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01002495 if (!ret) {
Mario Limonciello89096312020-04-30 09:51:14 -05002496 g_printerr ("%s\n", error->message);
Richard Hughesb5976832018-05-18 10:02:09 +01002497 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
Mario Limonciello89096312020-04-30 09:51:14 -05002498 /* TRANSLATORS: error message explaining command to run to how to get help */
2499 g_printerr ("\n%s\n", _("Use fwupdtool --help for help"));
2500 } else if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
2501 g_debug ("%s\n", error->message);
Richard Hughesb5976832018-05-18 10:02:09 +01002502 return EXIT_NOTHING_TO_DO;
2503 }
Richard Hughesb5976832018-05-18 10:02:09 +01002504 return EXIT_FAILURE;
2505 }
2506
2507 /* success */
2508 return EXIT_SUCCESS;
2509}