blob: 8028d13b305b2709627c6257be60c1254df89159 [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 Hughesb5976832018-05-18 10:02:09 +010022
Mario Limonciello7a3df4b2019-01-31 10:27:22 -060023#include "fu-device-private.h"
Richard Hughes98ca9932018-05-18 10:24:07 +010024#include "fu-engine.h"
Mario Limonciello96a0dd52019-02-25 13:50:03 -060025#include "fu-history.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010026#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010027#include "fu-progressbar.h"
Mario Limonciello6b0e6632019-11-22 13:04:32 -060028#include "fu-smbios-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010029#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050030#include "fu-debug.h"
Mario Limonciello1e35e4c2019-01-28 11:13:02 -060031#include "fwupd-common-private.h"
Mario Limonciello3143bad2019-02-27 07:31:00 -060032#include "fwupd-device-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010033
Richard Hughes3d005222019-05-17 14:02:41 +010034#ifdef HAVE_SYSTEMD
35#include "fu-systemd.h"
36#endif
37
Richard Hughesb5976832018-05-18 10:02:09 +010038/* custom return code */
39#define EXIT_NOTHING_TO_DO 2
40
Mario Limonciello3f243a92019-01-21 22:05:23 -060041typedef enum {
42 FU_UTIL_OPERATION_UNKNOWN,
43 FU_UTIL_OPERATION_UPDATE,
44 FU_UTIL_OPERATION_INSTALL,
Richard Hughesa58510b2019-10-30 10:03:12 +000045 FU_UTIL_OPERATION_READ,
Mario Limonciello3f243a92019-01-21 22:05:23 -060046 FU_UTIL_OPERATION_LAST
47} FuUtilOperation;
48
Richard Hughesc77e1112019-03-01 10:16:26 +000049struct FuUtilPrivate {
Richard Hughesb5976832018-05-18 10:02:09 +010050 GCancellable *cancellable;
51 GMainLoop *loop;
52 GOptionContext *context;
Richard Hughes98ca9932018-05-18 10:24:07 +010053 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010054 FuProgressbar *progressbar;
Mario Limonciello3f243a92019-01-21 22:05:23 -060055 gboolean no_reboot_check;
Mario Limonciello98b95162019-10-30 09:20:43 -050056 gboolean no_safety_check;
Mario Limonciello53ce25d2019-02-01 16:09:03 +000057 gboolean prepare_blob;
58 gboolean cleanup_blob;
Mario Limonciello3143bad2019-02-27 07:31:00 -060059 gboolean enable_json_state;
Richard Hughes460226a2018-05-21 20:56:21 +010060 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050061 gboolean show_all_devices;
Richard Hughes0e46b222019-09-05 12:13:35 +010062 gboolean disable_ssl_strict;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050063 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060064 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050065 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060066 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060067 FwupdDeviceFlags completion_flags;
Richard Hughes747f5702019-08-06 14:27:26 +010068 FwupdDeviceFlags filter_include;
69 FwupdDeviceFlags filter_exclude;
Richard Hughesc77e1112019-03-01 10:16:26 +000070};
Richard Hughesb5976832018-05-18 10:02:09 +010071
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050072static gboolean
Mario Limonciello3143bad2019-02-27 07:31:00 -060073fu_util_save_current_state (FuUtilPrivate *priv, GError **error)
74{
75 g_autoptr(JsonBuilder) builder = NULL;
76 g_autoptr(JsonGenerator) json_generator = NULL;
77 g_autoptr(JsonNode) json_root = NULL;
78 g_autoptr(GPtrArray) devices = NULL;
79 g_autofree gchar *state = NULL;
80 g_autofree gchar *dirname = NULL;
81 g_autofree gchar *filename = NULL;
82
83 if (!priv->enable_json_state)
84 return TRUE;
85
86 devices = fu_engine_get_devices (priv->engine, error);
87 if (devices == NULL)
88 return FALSE;
89
90 /* create header */
91 builder = json_builder_new ();
92 json_builder_begin_object (builder);
93
94 /* add each device */
95 json_builder_set_member_name (builder, "Devices");
96 json_builder_begin_array (builder);
97 for (guint i = 0; i < devices->len; i++) {
98 FwupdDevice *dev = g_ptr_array_index (devices, i);
99 json_builder_begin_object (builder);
100 fwupd_device_to_json (dev, builder);
101 json_builder_end_object (builder);
102 }
103 json_builder_end_array (builder);
104 json_builder_end_object (builder);
105
106 /* export as a string */
107 json_root = json_builder_get_root (builder);
108 json_generator = json_generator_new ();
109 json_generator_set_pretty (json_generator, TRUE);
110 json_generator_set_root (json_generator, json_root);
111 state = json_generator_to_data (json_generator, NULL);
112 if (state == NULL)
113 return FALSE;
114 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
115 filename = g_build_filename (dirname, "state.json", NULL);
116 return g_file_set_contents (filename, state, -1, error);
117}
118
119static gboolean
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000120fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error)
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600121{
122 g_autoptr(GError) error_local = NULL;
123
Richard Hughesd92ccca2019-05-20 11:28:31 +0100124#ifdef HAVE_SYSTEMD
Richard Hughes3d005222019-05-17 14:02:41 +0100125 if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local))
Mario Limonciello8692d012019-10-12 18:01:55 -0500126 g_debug ("Failed to stop daemon: %s", error_local->message);
Richard Hughesd92ccca2019-05-20 11:28:31 +0100127#endif
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000128 if (!fu_engine_load (priv->engine, flags, error))
Richard Hughesf425d292019-01-18 17:57:39 +0000129 return FALSE;
130 if (fu_engine_get_tainted (priv->engine)) {
131 g_printerr ("WARNING: This tool has loaded 3rd party code and "
132 "is no longer supported by the upstream developers!\n");
133 }
134 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500135}
136
Richard Hughesb5976832018-05-18 10:02:09 +0100137static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500138fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
139{
140 g_autofree gchar *path = g_path_get_dirname (value);
141 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
142 g_prefix_error (error,
143 "Unable to access %s. You may need to copy %s to %s: ",
144 path, value, g_getenv ("HOME"));
145 }
146}
147
148static void
Richard Hughesb5976832018-05-18 10:02:09 +0100149fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
150{
151 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
152 /* TRANSLATORS: this is when a device ctrl+c's a watch */
153 g_print ("%s\n", _("Cancelled"));
154 g_main_loop_quit (priv->loop);
155}
156
157static gboolean
158fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
159{
160 g_autofree gchar *tmp = NULL;
161 g_autoptr(FuSmbios) smbios = NULL;
162 if (g_strv_length (values) < 1) {
163 g_set_error_literal (error,
164 FWUPD_ERROR,
165 FWUPD_ERROR_INVALID_ARGS,
166 "Invalid arguments");
167 return FALSE;
168 }
169 smbios = fu_smbios_new ();
170 if (!fu_smbios_setup_from_file (smbios, values[0], error))
171 return FALSE;
172 tmp = fu_smbios_to_string (smbios);
173 g_print ("%s\n", tmp);
174 return TRUE;
175}
176
Richard Hughes9e5675e2019-11-22 09:35:03 +0000177#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +0100178static gboolean
179fu_util_sigint_cb (gpointer user_data)
180{
181 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
182 g_debug ("Handling SIGINT");
183 g_cancellable_cancel (priv->cancellable);
184 return FALSE;
185}
Richard Hughes9e5675e2019-11-22 09:35:03 +0000186#endif
Richard Hughesb5976832018-05-18 10:02:09 +0100187
188static void
189fu_util_private_free (FuUtilPrivate *priv)
190{
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500191 if (priv->current_device != NULL)
192 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100193 if (priv->engine != NULL)
194 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100195 if (priv->loop != NULL)
196 g_main_loop_unref (priv->loop);
197 if (priv->cancellable != NULL)
198 g_object_unref (priv->cancellable);
199 if (priv->progressbar != NULL)
200 g_object_unref (priv->progressbar);
201 if (priv->context != NULL)
202 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600203 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100204 g_free (priv);
205}
206
207#pragma clang diagnostic push
208#pragma clang diagnostic ignored "-Wunused-function"
209G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
210#pragma clang diagnostic pop
211
Richard Hughes98ca9932018-05-18 10:24:07 +0100212
213static void
214fu_main_engine_device_added_cb (FuEngine *engine,
215 FuDevice *device,
216 FuUtilPrivate *priv)
217{
218 g_autofree gchar *tmp = fu_device_to_string (device);
219 g_debug ("ADDED:\n%s", tmp);
220}
221
222static void
223fu_main_engine_device_removed_cb (FuEngine *engine,
224 FuDevice *device,
225 FuUtilPrivate *priv)
226{
227 g_autofree gchar *tmp = fu_device_to_string (device);
228 g_debug ("REMOVED:\n%s", tmp);
229}
230
231static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100232fu_main_engine_status_changed_cb (FuEngine *engine,
233 FwupdStatus status,
234 FuUtilPrivate *priv)
235{
236 fu_progressbar_update (priv->progressbar, status, 0);
237}
238
239static void
240fu_main_engine_percentage_changed_cb (FuEngine *engine,
241 guint percentage,
242 FuUtilPrivate *priv)
243{
244 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
245}
246
247static gboolean
248fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
249{
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000250 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100251 return FALSE;
252 g_main_loop_run (priv->loop);
253 return TRUE;
254}
255
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100256static gint
257fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
258{
259 return fu_plugin_name_compare (*item1, *item2);
260}
261
262static gboolean
263fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
264{
265 GPtrArray *plugins;
266 guint cnt = 0;
267
268 /* load engine */
269 if (!fu_engine_load_plugins (priv->engine, error))
270 return FALSE;
271
272 /* print */
273 plugins = fu_engine_get_plugins (priv->engine);
274 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
275 for (guint i = 0; i < plugins->len; i++) {
276 FuPlugin *plugin = g_ptr_array_index (plugins, i);
277 if (!fu_plugin_get_enabled (plugin))
278 continue;
279 g_print ("%s\n", fu_plugin_get_name (plugin));
280 cnt++;
281 }
282 if (cnt == 0) {
283 /* TRANSLATORS: nothing found */
284 g_print ("%s\n", _("No plugins found"));
285 return TRUE;
286 }
287
288 return TRUE;
289}
290
Richard Hughes98ca9932018-05-18 10:24:07 +0100291static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100292fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev)
293{
294 if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) {
295 if (!fwupd_device_has_flag (dev, priv->filter_include))
296 return FALSE;
297 }
298 if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) {
299 if (fwupd_device_has_flag (dev, priv->filter_exclude))
300 return FALSE;
301 }
302 return TRUE;
303}
304
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500305static gchar *
306fu_util_get_tree_title (FuUtilPrivate *priv)
307{
308 return g_strdup (fu_engine_get_host_product (priv->engine));
309}
310
Richard Hughes747f5702019-08-06 14:27:26 +0100311static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600312fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
313{
314 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500315 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500316 g_autofree gchar *title = fu_util_get_tree_title (priv);
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600317
318 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000319 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600320 return FALSE;
321
322 /* get devices from daemon */
323 devices = fu_engine_get_devices (priv->engine, error);
324 if (devices == NULL)
325 return FALSE;
326 for (guint i = 0; i < devices->len; i++) {
327 FwupdDevice *dev = g_ptr_array_index (devices, i);
328 g_autoptr(GPtrArray) rels = NULL;
329 g_autoptr(GError) error_local = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500330 GNode *child;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600331
Richard Hughes747f5702019-08-06 14:27:26 +0100332 /* not going to have results, so save a engine round-trip */
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600333 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
334 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100335 if (!fu_util_filter_device (priv, dev))
336 continue;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600337
338 /* get the releases for this device and filter for validity */
339 rels = fu_engine_get_upgrades (priv->engine,
340 fwupd_device_get_id (dev),
341 &error_local);
342 if (rels == NULL) {
343 g_printerr ("%s\n", error_local->message);
344 continue;
345 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500346 child = g_node_append_data (root, dev);
347
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600348 for (guint j = 0; j < rels->len; j++) {
349 FwupdRelease *rel = g_ptr_array_index (rels, j);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500350 g_node_append_data (child, g_object_ref (rel));
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600351 }
352 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500353 if (g_node_n_nodes (root, G_TRAVERSE_ALL) > 1)
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500354 fu_util_print_tree (root, title);
Mario Limonciello3143bad2019-02-27 07:31:00 -0600355 /* save the device state for other applications to see */
356 if (!fu_util_save_current_state (priv, error))
357 return FALSE;
358
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600359 /* success */
360 return TRUE;
361}
362
363static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500364fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
365{
366 g_autoptr(GPtrArray) array = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500367 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500368 g_autofree gchar *title = fu_util_get_tree_title (priv);
Mario Limonciello716ab272018-05-29 12:34:37 -0500369 gint fd;
370
371 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000372 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500373 return FALSE;
374
375 /* check args */
376 if (g_strv_length (values) != 1) {
377 g_set_error_literal (error,
378 FWUPD_ERROR,
379 FWUPD_ERROR_INVALID_ARGS,
380 "Invalid arguments");
381 return FALSE;
382 }
383
384 /* open file */
385 fd = open (values[0], O_RDONLY);
386 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500387 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500388 g_set_error (error,
389 FWUPD_ERROR,
390 FWUPD_ERROR_INVALID_FILE,
391 "failed to open %s",
392 values[0]);
393 return FALSE;
394 }
395 array = fu_engine_get_details (priv->engine, fd, error);
396 close (fd);
397
398 if (array == NULL)
399 return FALSE;
400 for (guint i = 0; i < array->len; i++) {
401 FwupdDevice *dev = g_ptr_array_index (array, i);
Richard Hughes747f5702019-08-06 14:27:26 +0100402 if (!fu_util_filter_device (priv, dev))
403 continue;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500404 g_node_append_data (root, dev);
Mario Limonciello716ab272018-05-29 12:34:37 -0500405 }
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500406 fu_util_print_tree (root, title);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500407
Mario Limonciello716ab272018-05-29 12:34:37 -0500408 return TRUE;
409}
410
411static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100412fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error)
413{
414 g_autoptr(GString) str = g_string_new (NULL);
415
416 for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) {
417 const gchar *tmp = fwupd_device_flag_to_string (i);
418 if (tmp == NULL)
419 break;
420 if (i != FWUPD_DEVICE_FLAG_INTERNAL)
421 g_string_append (str, " ");
422 g_string_append (str, tmp);
423 g_string_append (str, " ~");
424 g_string_append (str, tmp);
425 }
426 g_print ("%s\n", str->str);
427
428 return TRUE;
429}
430
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500431static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100432fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500433{
434 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100435 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Richard Hughes747f5702019-08-06 14:27:26 +0100436 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp)))
437 continue;
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500438 if (!priv->show_all_devices &&
439 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500440 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100441 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500442 GNode *child = g_node_append_data (root, dev_tmp);
443 fu_util_build_device_tree (priv, child, devs, dev_tmp);
444 }
445 }
446}
447
448static gboolean
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100449fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500450{
451 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500452 g_autofree gchar *title = fu_util_get_tree_title (priv);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500453 g_autoptr(GPtrArray) devs = NULL;
454
455 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000456 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500457 return FALSE;
458
459 /* print */
460 devs = fu_engine_get_devices (priv->engine, error);
461 if (devs == NULL)
462 return FALSE;
463
464 /* print */
465 if (devs->len == 0) {
466 /* TRANSLATORS: nothing attached that can be upgraded */
467 g_print ("%s\n", _("No hardware detected with firmware update capability"));
468 return TRUE;
469 }
470 fu_util_build_device_tree (priv, root, devs, NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -0500471 fu_util_print_tree (root, title);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500472
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100473 /* save the device state for other applications to see */
474 return fu_util_save_current_state (priv, error);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500475}
476
Richard Hughes98ca9932018-05-18 10:24:07 +0100477static FuDevice *
478fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
479{
480 FuDevice *dev;
481 guint idx;
482 g_autoptr(GPtrArray) devices = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +0100483 g_autoptr(GPtrArray) devices_filtered = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100484
485 /* get devices from daemon */
486 devices = fu_engine_get_devices (priv->engine, error);
487 if (devices == NULL)
488 return NULL;
489
Richard Hughes747f5702019-08-06 14:27:26 +0100490 /* filter results */
491 devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
492 for (guint i = 0; i < devices->len; i++) {
493 dev = g_ptr_array_index (devices, i);
494 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev)))
495 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100496 g_ptr_array_add (devices_filtered, g_object_ref (dev));
497 }
498
499 /* nothing */
500 if (devices_filtered->len == 0) {
501 g_set_error_literal (error,
502 FWUPD_ERROR,
503 FWUPD_ERROR_NOTHING_TO_DO,
504 "No supported devices");
505 return NULL;
506 }
507
Richard Hughes98ca9932018-05-18 10:24:07 +0100508 /* exactly one */
Richard Hughes747f5702019-08-06 14:27:26 +0100509 if (devices_filtered->len == 1) {
510 dev = g_ptr_array_index (devices_filtered, 0);
Mario Limoncielloa61b4d82019-10-22 08:37:45 -0500511 /* TRANSLATORS: Device has been chosen by the daemon for the user */
512 g_print ("%s: %s\n", _("Selected device"), fu_device_get_name (dev));
Richard Hughes98ca9932018-05-18 10:24:07 +0100513 return g_object_ref (dev);
514 }
515
516 /* TRANSLATORS: get interactive prompt */
517 g_print ("%s\n", _("Choose a device:"));
518 /* TRANSLATORS: this is to abort the interactive prompt */
519 g_print ("0.\t%s\n", _("Cancel"));
Richard Hughes747f5702019-08-06 14:27:26 +0100520 for (guint i = 0; i < devices_filtered->len; i++) {
521 dev = g_ptr_array_index (devices_filtered, i);
Richard Hughes98ca9932018-05-18 10:24:07 +0100522 g_print ("%u.\t%s (%s)\n",
523 i + 1,
524 fu_device_get_id (dev),
525 fu_device_get_name (dev));
526 }
Richard Hughes747f5702019-08-06 14:27:26 +0100527 idx = fu_util_prompt_for_number (devices_filtered->len);
Richard Hughes98ca9932018-05-18 10:24:07 +0100528 if (idx == 0) {
529 g_set_error_literal (error,
530 FWUPD_ERROR,
531 FWUPD_ERROR_NOTHING_TO_DO,
532 "Request canceled");
533 return NULL;
534 }
Richard Hughes747f5702019-08-06 14:27:26 +0100535 dev = g_ptr_array_index (devices_filtered, idx - 1);
Richard Hughes98ca9932018-05-18 10:24:07 +0100536 return g_object_ref (dev);
537}
538
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500539static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600540fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500541 FwupdDevice *device,
542 FuUtilPrivate *priv)
543{
544 g_autofree gchar *str = NULL;
545
Richard Hughes809abea2019-03-23 11:06:18 +0000546 /* allowed to set whenever the device has changed */
547 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
548 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
549 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
550 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
551
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500552 /* same as last time, so ignore */
553 if (priv->current_device != NULL &&
554 fwupd_device_compare (priv->current_device, device) == 0)
555 return;
556
557 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600558 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
559 /* TRANSLATORS: %1 is a device name */
560 str = g_strdup_printf (_("Updating %s…"),
561 fwupd_device_get_name (device));
562 fu_progressbar_set_title (priv->progressbar, str);
563 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
564 /* TRANSLATORS: %1 is a device name */
565 str = g_strdup_printf (_("Installing on %s…"),
566 fwupd_device_get_name (device));
567 fu_progressbar_set_title (priv->progressbar, str);
Richard Hughesa58510b2019-10-30 10:03:12 +0000568 } else if (priv->current_operation == FU_UTIL_OPERATION_READ) {
569 /* TRANSLATORS: %1 is a device name */
570 str = g_strdup_printf (_("Reading from %s…"),
571 fwupd_device_get_name (device));
572 fu_progressbar_set_title (priv->progressbar, str);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600573 } else {
574 g_warning ("no FuUtilOperation set");
575 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500576 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600577
Mario Limonciello32241f42019-01-24 10:12:41 -0600578 if (priv->current_message == NULL) {
579 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
580 if (tmp != NULL)
581 priv->current_message = g_strdup (tmp);
582 }
583}
584
585static void
586fu_util_display_current_message (FuUtilPrivate *priv)
587{
588 if (priv->current_message == NULL)
589 return;
590 g_print ("%s\n", priv->current_message);
591 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500592}
593
Richard Hughes98ca9932018-05-18 10:24:07 +0100594static gboolean
595fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
596{
597 g_autoptr(FuDevice) device = NULL;
598 g_autoptr(GBytes) blob_fw = NULL;
599
600 /* invalid args */
601 if (g_strv_length (values) == 0) {
602 g_set_error_literal (error,
603 FWUPD_ERROR,
604 FWUPD_ERROR_INVALID_ARGS,
605 "Invalid arguments");
606 return FALSE;
607 }
608
609 /* parse blob */
610 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500611 if (blob_fw == NULL) {
612 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100613 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500614 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100615
616 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000617 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100618 return FALSE;
619
620 /* get device */
621 if (g_strv_length (values) >= 2) {
622 device = fu_engine_get_device (priv->engine, values[1], error);
623 if (device == NULL)
624 return FALSE;
625 } else {
626 device = fu_util_prompt_for_device (priv, error);
627 if (device == NULL)
628 return FALSE;
629 }
630
Mario Limonciello3f243a92019-01-21 22:05:23 -0600631 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500632 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600633 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500634
Richard Hughes98ca9932018-05-18 10:24:07 +0100635 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000636 if (priv->prepare_blob) {
637 g_autoptr(GPtrArray) devices = NULL;
638 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
639 g_ptr_array_add (devices, g_object_ref (device));
640 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
641 g_prefix_error (error, "failed to prepare composite action: ");
642 return FALSE;
643 }
644 }
Mario Limonciello035818b2019-03-26 11:12:16 -0500645 priv->flags = FWUPD_INSTALL_FLAG_NO_HISTORY;
Richard Hughes84af6e72019-02-01 18:19:41 +0000646 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600647 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000648 if (priv->cleanup_blob) {
649 g_autoptr(FuDevice) device_new = NULL;
650 g_autoptr(GError) error_local = NULL;
651
652 /* get the possibly new device from the old ID */
653 device_new = fu_engine_get_device (priv->engine,
654 fu_device_get_id (device),
655 &error_local);
656 if (device_new == NULL) {
657 g_debug ("failed to find new device: %s",
658 error_local->message);
659 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600660 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000661 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
662 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
663 g_prefix_error (error, "failed to cleanup composite action: ");
664 return FALSE;
665 }
666 }
667 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600668
Mario Limonciello32241f42019-01-24 10:12:41 -0600669 fu_util_display_current_message (priv);
670
Mario Limonciello3f243a92019-01-21 22:05:23 -0600671 /* success */
672 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100673}
674
Richard Hughesa58510b2019-10-30 10:03:12 +0000675static gboolean
676fu_util_firmware_read (FuUtilPrivate *priv, gchar **values, GError **error)
677{
678 g_autoptr(FuDevice) device = NULL;
679 g_autoptr(GBytes) blob_empty = g_bytes_new (NULL, 0);
680 g_autoptr(GBytes) blob_fw = NULL;
681
682 /* invalid args */
683 if (g_strv_length (values) == 0) {
684 g_set_error_literal (error,
685 FWUPD_ERROR,
686 FWUPD_ERROR_INVALID_ARGS,
687 "Invalid arguments");
688 return FALSE;
689 }
690
691 /* file already exists */
692 if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0 &&
693 g_file_test (values[0], G_FILE_TEST_EXISTS)) {
694 g_set_error_literal (error,
695 FWUPD_ERROR,
696 FWUPD_ERROR_INVALID_ARGS,
697 "Filename already exists");
698 return FALSE;
699 }
700
Richard Hughes02792c02019-11-01 14:21:20 +0000701 /* write a zero length file to ensure the destination is writable to
Richard Hughesa58510b2019-10-30 10:03:12 +0000702 * avoid failing at the end of a potentially lengthy operation */
703 if (!fu_common_set_contents_bytes (values[0], blob_empty, error))
704 return FALSE;
705
706 /* load engine */
707 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
708 return FALSE;
709
710 /* get device */
711 if (g_strv_length (values) >= 2) {
712 device = fu_engine_get_device (priv->engine, values[1], error);
713 if (device == NULL)
714 return FALSE;
715 } else {
716 device = fu_util_prompt_for_device (priv, error);
717 if (device == NULL)
718 return FALSE;
719 }
720 priv->current_operation = FU_UTIL_OPERATION_READ;
721 g_signal_connect (priv->engine, "device-changed",
722 G_CALLBACK (fu_util_update_device_changed_cb), priv);
723
724 /* dump firmware */
725 blob_fw = fu_engine_firmware_read (priv->engine, device, priv->flags, error);
726 if (blob_fw == NULL)
727 return FALSE;
728 return fu_common_set_contents_bytes (values[0], blob_fw, error);
729}
730
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100731static gint
732fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
733{
734 FuInstallTask *task1 = *((FuInstallTask **) a);
735 FuInstallTask *task2 = *((FuInstallTask **) b);
736 return fu_install_task_compare (task1, task2);
737}
738
739static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100740fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
741{
Filipe Laínse0914272019-09-20 10:04:43 +0100742 const gchar *argv[][5] = { { "wget", uri, "-O", fn, NULL },
Richard Hughes3d178be2018-08-30 11:14:24 +0100743 { "curl", uri, "--output", fn, NULL },
744 { NULL } };
745 for (guint i = 0; argv[i][0] != NULL; i++) {
746 g_autoptr(GError) error_local = NULL;
747 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
748 g_debug ("%s", error_local->message);
749 continue;
750 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000751 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100752 }
753 g_set_error_literal (error,
754 FWUPD_ERROR,
755 FWUPD_ERROR_NOT_FOUND,
756 "no supported out-of-process downloaders found");
757 return FALSE;
758}
759
760static gchar *
761fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
762{
763 g_autofree gchar *filename = NULL;
764 g_autoptr(SoupURI) uri = NULL;
765
766 /* a local file */
767 uri = soup_uri_new (perhapsfn);
768 if (uri == NULL)
769 return g_strdup (perhapsfn);
770
771 /* download the firmware to a cachedir */
772 filename = fu_util_get_user_cache_path (perhapsfn);
773 if (!fu_common_mkdir_parent (filename, error))
774 return NULL;
775 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
776 return NULL;
777 return g_steal_pointer (&filename);
778}
779
780static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100781fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
782{
Richard Hughes3d178be2018-08-30 11:14:24 +0100783 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100784 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100785 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100786 g_autoptr(GPtrArray) devices_possible = NULL;
787 g_autoptr(GPtrArray) errors = NULL;
788 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100789 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100790
Mario Limonciello8949e892018-05-25 08:03:06 -0500791 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000792 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500793 return FALSE;
794
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100795 /* handle both forms */
796 if (g_strv_length (values) == 1) {
797 devices_possible = fu_engine_get_devices (priv->engine, error);
798 if (devices_possible == NULL)
799 return FALSE;
800 } else if (g_strv_length (values) == 2) {
801 FuDevice *device = fu_engine_get_device (priv->engine,
802 values[1],
803 error);
804 if (device == NULL)
805 return FALSE;
806 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
807 g_ptr_array_add (devices_possible, device);
808 } else {
809 g_set_error_literal (error,
810 FWUPD_ERROR,
811 FWUPD_ERROR_INVALID_ARGS,
812 "Invalid arguments");
813 return FALSE;
814 }
815
Richard Hughes3d178be2018-08-30 11:14:24 +0100816 /* download if required */
817 filename = fu_util_download_if_required (priv, values[0], error);
818 if (filename == NULL)
819 return FALSE;
820
Richard Hughes481aa2a2018-09-18 20:51:46 +0100821 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100822 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500823 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100824 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100825 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500826 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100827 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
828 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100829 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600830 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100831 if (components == NULL)
832 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100833
Richard Hughes481aa2a2018-09-18 20:51:46 +0100834 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100835 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
836 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100837 for (guint i = 0; i < components->len; i++) {
838 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100839
840 /* do any devices pass the requirements */
841 for (guint j = 0; j < devices_possible->len; j++) {
842 FuDevice *device = g_ptr_array_index (devices_possible, j);
843 g_autoptr(FuInstallTask) task = NULL;
844 g_autoptr(GError) error_local = NULL;
845
846 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100847 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100848 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100849 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100850 &error_local)) {
851 g_debug ("requirement on %s:%s failed: %s",
852 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100853 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100854 error_local->message);
855 g_ptr_array_add (errors, g_steal_pointer (&error_local));
856 continue;
857 }
858
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600859 /* if component should have an update message from CAB */
860 fu_device_incorporate_from_component (device, component);
861
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100862 /* success */
863 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
864 }
865 }
866
867 /* order the install tasks by the device priority */
868 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
869
870 /* nothing suitable */
871 if (install_tasks->len == 0) {
872 GError *error_tmp = fu_common_error_array_get_best (errors);
873 g_propagate_error (error, error_tmp);
874 return FALSE;
875 }
876
Mario Limonciello3f243a92019-01-21 22:05:23 -0600877 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500878 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600879 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500880
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100881 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100882 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
883 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100884
Mario Limonciello32241f42019-01-24 10:12:41 -0600885 fu_util_display_current_message (priv);
886
Mario Limonciello3f243a92019-01-21 22:05:23 -0600887 /* we don't want to ask anything */
888 if (priv->no_reboot_check) {
889 g_debug ("skipping reboot check");
890 return TRUE;
891 }
892
Mario Limonciello3143bad2019-02-27 07:31:00 -0600893 /* save the device state for other applications to see */
894 if (!fu_util_save_current_state (priv, error))
895 return FALSE;
896
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100897 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600898 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100899}
900
Richard Hughes98ca9932018-05-18 10:24:07 +0100901static gboolean
Mario Limonciellofd734852019-08-01 16:41:42 -0500902fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error)
Mario Limonciello46aaee82019-01-10 12:58:00 -0600903{
Mario Limonciellofd734852019-08-01 16:41:42 -0500904 FwupdRemote *remote;
905 const gchar *remote_id;
906 const gchar *uri_tmp;
907 g_auto(GStrv) argv = NULL;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600908
Mario Limonciellofd734852019-08-01 16:41:42 -0500909 uri_tmp = fwupd_release_get_uri (rel);
910 if (uri_tmp == NULL) {
911 g_set_error_literal (error,
912 FWUPD_ERROR,
913 FWUPD_ERROR_INVALID_FILE,
914 "release missing URI");
915 return FALSE;
916 }
917 remote_id = fwupd_release_get_remote_id (rel);
918 if (remote_id == NULL) {
919 g_set_error (error,
920 FWUPD_ERROR,
921 FWUPD_ERROR_INVALID_FILE,
922 "failed to find remote for %s",
923 uri_tmp);
Richard Hughes747f5702019-08-06 14:27:26 +0100924 return FALSE;
Mario Limonciellofd734852019-08-01 16:41:42 -0500925 }
926
927 remote = fu_engine_get_remote_by_id (priv->engine,
928 remote_id,
929 error);
930 if (remote == NULL)
Mario Limonciello46aaee82019-01-10 12:58:00 -0600931 return FALSE;
932
Mario Limonciellofd734852019-08-01 16:41:42 -0500933 argv = g_new0 (gchar *, 2);
934 /* local remotes have the firmware already */
935 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
936 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
937 g_autofree gchar *path = g_path_get_dirname (fn_cache);
938 argv[0] = g_build_filename (path, uri_tmp, NULL);
939 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
940 argv[0] = g_strdup (uri_tmp + 7);
941 /* web remote, fu_util_install will download file */
942 } else {
943 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
944 }
945 return fu_util_install (priv, argv, error);
946}
947
948static gboolean
949fu_util_update_all (FuUtilPrivate *priv, GError **error)
950{
951 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello3f243a92019-01-21 22:05:23 -0600952
Mario Limonciello46aaee82019-01-10 12:58:00 -0600953 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000954 if (devices == NULL)
955 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600956 for (guint i = 0; i < devices->len; i++) {
957 FwupdDevice *dev = g_ptr_array_index (devices, i);
958 FwupdRelease *rel;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600959 const gchar *device_id;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600960 g_autoptr(GPtrArray) rels = NULL;
961 g_autoptr(GError) error_local = NULL;
962
963 if (!fu_util_is_interesting_device (dev))
964 continue;
965 /* only show stuff that has metadata available */
966 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
967 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100968 if (!fu_util_filter_device (priv, dev))
969 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600970
971 device_id = fu_device_get_id (dev);
972 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
973 if (rels == NULL) {
974 g_printerr ("%s\n", error_local->message);
975 continue;
976 }
977
Mario Limonciello98b95162019-10-30 09:20:43 -0500978 if (!priv->no_safety_check) {
979 if (!fu_util_prompt_warning (dev,
980 fu_util_get_tree_title (priv),
981 error))
982 return FALSE;
983 }
984
Mario Limonciello46aaee82019-01-10 12:58:00 -0600985 rel = g_ptr_array_index (rels, 0);
Mario Limonciellofd734852019-08-01 16:41:42 -0500986 if (!fu_util_install_release (priv, rel, &error_local)) {
987 g_printerr ("%s\n", error_local->message);
Richard Hughes747f5702019-08-06 14:27:26 +0100988 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600989 }
Mario Limonciellofd734852019-08-01 16:41:42 -0500990 fu_util_display_current_message (priv);
991 }
992 return TRUE;
993}
994
995static gboolean
996fu_util_update_by_id (FuUtilPrivate *priv, const gchar *device_id, GError **error)
997{
998 FwupdRelease *rel;
999 g_autoptr(FuDevice) dev = NULL;
1000 g_autoptr(GPtrArray) rels = NULL;
1001
1002 /* do not allow a partial device-id */
1003 dev = fu_engine_get_device (priv->engine, device_id, error);
1004 if (dev == NULL)
1005 return FALSE;
1006
1007 /* get the releases for this device and filter for validity */
1008 rels = fu_engine_get_upgrades (priv->engine, device_id, error);
1009 if (rels == NULL)
1010 return FALSE;
1011 rel = g_ptr_array_index (rels, 0);
1012 if (!fu_util_install_release (priv, rel, error))
1013 return FALSE;
1014 fu_util_display_current_message (priv);
1015
1016 return TRUE;
1017}
1018
1019static gboolean
1020fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
1021{
Mario Limonciello0f109b02019-11-14 10:26:44 -06001022 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) {
1023 g_set_error_literal (error,
1024 FWUPD_ERROR,
1025 FWUPD_ERROR_INVALID_ARGS,
1026 "--allow-older is not supported for this command");
1027 return FALSE;
1028 }
1029
1030 if (priv->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) {
1031 g_set_error_literal (error,
1032 FWUPD_ERROR,
1033 FWUPD_ERROR_INVALID_ARGS,
1034 "--allow-reinstall is not supported for this command");
1035 return FALSE;
1036 }
1037
Mario Limonciellofd734852019-08-01 16:41:42 -05001038 if (g_strv_length (values) > 1) {
1039 g_set_error_literal (error,
1040 FWUPD_ERROR,
1041 FWUPD_ERROR_INVALID_ARGS,
1042 "Invalid arguments");
1043 return FALSE;
1044 }
1045
1046 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1047 return FALSE;
1048
1049 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
1050 g_signal_connect (priv->engine, "device-changed",
1051 G_CALLBACK (fu_util_update_device_changed_cb), priv);
1052
1053 if (g_strv_length (values) == 1) {
1054 if (!fu_util_update_by_id (priv, values[0], error))
1055 return FALSE;
1056 } else {
1057 if (!fu_util_update_all (priv, error))
1058 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -06001059 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001060
1061 /* we don't want to ask anything */
1062 if (priv->no_reboot_check) {
1063 g_debug ("skipping reboot check");
1064 return TRUE;
1065 }
1066
Mario Limonciello3143bad2019-02-27 07:31:00 -06001067 /* save the device state for other applications to see */
1068 if (!fu_util_save_current_state (priv, error))
1069 return FALSE;
1070
Mario Limonciello3f243a92019-01-21 22:05:23 -06001071 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001072}
1073
1074static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +01001075fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
1076{
1077 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001078 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001079
1080 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001081 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001082 return FALSE;
1083
Richard Hughes98ca9932018-05-18 10:24:07 +01001084 /* get device */
1085 if (g_strv_length (values) >= 1) {
1086 device = fu_engine_get_device (priv->engine, values[0], error);
1087 if (device == NULL)
1088 return FALSE;
1089 } else {
1090 device = fu_util_prompt_for_device (priv, error);
1091 if (device == NULL)
1092 return FALSE;
1093 }
1094
1095 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001096 locker = fu_device_locker_new (device, error);
1097 if (locker == NULL)
1098 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001099 return fu_device_detach (device, error);
1100}
1101
1102static gboolean
1103fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1104{
1105 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001106 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001107
1108 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001109 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001110 return FALSE;
1111
Richard Hughes98ca9932018-05-18 10:24:07 +01001112 /* get device */
1113 if (g_strv_length (values) >= 1) {
1114 device = fu_engine_get_device (priv->engine, values[0], error);
1115 if (device == NULL)
1116 return FALSE;
1117 } else {
1118 device = fu_util_prompt_for_device (priv, error);
1119 if (device == NULL)
1120 return FALSE;
1121 }
1122
1123 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001124 locker = fu_device_locker_new (device, error);
1125 if (locker == NULL)
1126 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001127 return fu_device_attach (device, error);
1128}
1129
Richard Hughes1d894f12018-08-31 13:05:51 +01001130static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001131fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1132{
1133 gboolean has_pending = FALSE;
1134 g_autoptr(FuHistory) history = fu_history_new ();
1135 g_autoptr(GPtrArray) devices = NULL;
1136
1137 /* check the history database before starting the daemon */
1138 if (g_strv_length (values) == 0) {
1139 devices = fu_history_get_devices (history, error);
1140 if (devices == NULL)
1141 return FALSE;
1142 } else if (g_strv_length (values) == 1) {
1143 FuDevice *device;
1144 device = fu_history_get_device_by_id (history, values[0], error);
1145 if (device == NULL)
1146 return FALSE;
1147 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1148 g_ptr_array_add (devices, device);
1149 } else {
1150 g_set_error_literal (error,
1151 FWUPD_ERROR,
1152 FWUPD_ERROR_INVALID_ARGS,
1153 "Invalid arguments");
1154 return FALSE;
1155 }
1156
1157 /* nothing to do */
1158 for (guint i = 0; i < devices->len; i++) {
1159 FuDevice *dev = g_ptr_array_index (devices, i);
1160 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1161 fu_engine_add_plugin_filter (priv->engine,
1162 fu_device_get_plugin (dev));
1163 has_pending = TRUE;
1164 }
1165 }
1166 if (!has_pending) {
1167 g_set_error_literal (error,
1168 FWUPD_ERROR,
1169 FWUPD_ERROR_NOTHING_TO_DO,
1170 "No firmware to activate");
1171 return FALSE;
1172
1173 }
1174
1175 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001176 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001177 return FALSE;
1178
1179 /* activate anything with _NEEDS_ACTIVATION */
1180 for (guint i = 0; i < devices->len; i++) {
1181 FuDevice *device = g_ptr_array_index (devices, i);
Richard Hughes747f5702019-08-06 14:27:26 +01001182 if (!fu_util_filter_device (priv, FWUPD_DEVICE (device)))
1183 continue;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001184 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1185 continue;
1186 /* TRANSLATORS: shown when shutting down to switch to the new version */
1187 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1188 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1189 return FALSE;
1190 }
1191
1192 return TRUE;
1193}
1194
1195static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001196fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1197{
1198 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1199 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1200 const gchar *hwid_keys[] = {
1201 FU_HWIDS_KEY_BIOS_VENDOR,
1202 FU_HWIDS_KEY_BIOS_VERSION,
1203 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1204 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1205 FU_HWIDS_KEY_MANUFACTURER,
1206 FU_HWIDS_KEY_FAMILY,
1207 FU_HWIDS_KEY_PRODUCT_NAME,
1208 FU_HWIDS_KEY_PRODUCT_SKU,
1209 FU_HWIDS_KEY_ENCLOSURE_KIND,
1210 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1211 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1212 NULL };
1213
1214 /* read DMI data */
1215 if (g_strv_length (values) == 0) {
1216 if (!fu_smbios_setup (smbios, error))
1217 return FALSE;
1218 } else if (g_strv_length (values) == 1) {
1219 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1220 return FALSE;
1221 } else {
1222 g_set_error_literal (error,
1223 FWUPD_ERROR,
1224 FWUPD_ERROR_INVALID_ARGS,
1225 "Invalid arguments");
1226 return FALSE;
1227 }
1228 if (!fu_hwids_setup (hwids, smbios, error))
1229 return FALSE;
1230
1231 /* show debug output */
1232 g_print ("Computer Information\n");
1233 g_print ("--------------------\n");
1234 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1235 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1236 if (tmp == NULL)
1237 continue;
1238 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1239 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1240 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1241 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1242 } else {
1243 g_print ("%s: %s\n", hwid_keys[i], tmp);
1244 }
1245 }
1246
1247 /* show GUIDs */
1248 g_print ("\nHardware IDs\n");
1249 g_print ("------------\n");
1250 for (guint i = 0; i < 15; i++) {
1251 const gchar *keys = NULL;
1252 g_autofree gchar *guid = NULL;
1253 g_autofree gchar *key = NULL;
1254 g_autofree gchar *keys_str = NULL;
1255 g_auto(GStrv) keysv = NULL;
1256 g_autoptr(GError) error_local = NULL;
1257
1258 /* get the GUID */
1259 key = g_strdup_printf ("HardwareID-%u", i);
1260 keys = fu_hwids_get_replace_keys (hwids, key);
1261 guid = fu_hwids_get_guid (hwids, key, &error_local);
1262 if (guid == NULL) {
1263 g_print ("%s\n", error_local->message);
1264 continue;
1265 }
1266
1267 /* show what makes up the GUID */
1268 keysv = g_strsplit (keys, "&", -1);
1269 keys_str = g_strjoinv (" + ", keysv);
1270 g_print ("{%s} <- %s\n", guid, keys_str);
1271 }
1272
1273 return TRUE;
1274}
1275
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001276static gboolean
1277fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1278{
1279 const gchar *script_fn = "startup.sh";
1280 const gchar *output_fn = "firmware.bin";
1281 g_autoptr(GBytes) archive_blob = NULL;
1282 g_autoptr(GBytes) firmware_blob = NULL;
1283 if (g_strv_length (values) < 2) {
1284 g_set_error_literal (error,
1285 FWUPD_ERROR,
1286 FWUPD_ERROR_INVALID_ARGS,
1287 "Invalid arguments");
1288 return FALSE;
1289 }
1290 archive_blob = fu_common_get_contents_bytes (values[0], error);
1291 if (archive_blob == NULL)
1292 return FALSE;
1293 if (g_strv_length (values) > 2)
1294 script_fn = values[2];
1295 if (g_strv_length (values) > 3)
1296 output_fn = values[3];
1297 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1298 if (firmware_blob == NULL)
1299 return FALSE;
1300 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1301}
1302
Richard Hughes3d607622019-03-07 16:59:27 +00001303static gboolean
1304fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1305{
1306 g_autofree gchar *sig = NULL;
1307
1308 /* check args */
1309 if (g_strv_length (values) != 1) {
1310 g_set_error_literal (error,
1311 FWUPD_ERROR,
1312 FWUPD_ERROR_INVALID_ARGS,
1313 "Invalid arguments: value expected");
1314 return FALSE;
1315 }
1316
1317 /* start engine */
1318 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1319 return FALSE;
1320 sig = fu_engine_self_sign (priv->engine, values[0],
1321 FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP |
1322 FU_KEYRING_SIGN_FLAG_ADD_CERT, error);
1323 if (sig == NULL)
1324 return FALSE;
1325 g_print ("%s\n", sig);
1326 return TRUE;
1327}
1328
Mario Limonciello62f84862018-10-18 13:15:23 -05001329static void
1330fu_util_device_added_cb (FwupdClient *client,
1331 FwupdDevice *device,
1332 gpointer user_data)
1333{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001334 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001335 /* TRANSLATORS: this is when a device is hotplugged */
1336 g_print ("%s\n%s", _("Device added:"), tmp);
1337}
1338
1339static void
1340fu_util_device_removed_cb (FwupdClient *client,
1341 FwupdDevice *device,
1342 gpointer user_data)
1343{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001344 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001345 /* TRANSLATORS: this is when a device is hotplugged */
1346 g_print ("%s\n%s", _("Device removed:"), tmp);
1347}
1348
1349static void
1350fu_util_device_changed_cb (FwupdClient *client,
1351 FwupdDevice *device,
1352 gpointer user_data)
1353{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001354 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001355 /* TRANSLATORS: this is when a device has been updated */
1356 g_print ("%s\n%s", _("Device changed:"), tmp);
1357}
1358
1359static void
1360fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1361{
1362 /* TRANSLATORS: this is when the daemon state changes */
1363 g_print ("%s\n", _("Changed"));
1364}
1365
1366static gboolean
1367fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1368{
1369 g_autoptr(FwupdClient) client = fwupd_client_new ();
1370
1371 /* get all the devices */
1372 if (!fwupd_client_connect (client, priv->cancellable, error))
1373 return FALSE;
1374
1375 /* watch for any hotplugged device */
1376 g_signal_connect (client, "changed",
1377 G_CALLBACK (fu_util_changed_cb), priv);
1378 g_signal_connect (client, "device-added",
1379 G_CALLBACK (fu_util_device_added_cb), priv);
1380 g_signal_connect (client, "device-removed",
1381 G_CALLBACK (fu_util_device_removed_cb), priv);
1382 g_signal_connect (client, "device-changed",
1383 G_CALLBACK (fu_util_device_changed_cb), priv);
1384 g_signal_connect (priv->cancellable, "cancelled",
1385 G_CALLBACK (fu_util_cancelled_cb), priv);
1386 g_main_loop_run (priv->loop);
1387 return TRUE;
1388}
1389
Richard Hughes15684492019-03-15 16:27:50 +00001390static gboolean
Richard Hughes95c98a92019-10-22 16:03:15 +01001391fu_util_get_firmware_types (FuUtilPrivate *priv, gchar **values, GError **error)
1392{
1393 g_autoptr(GPtrArray) firmware_types = NULL;
1394
1395 /* load engine */
1396 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1397 return FALSE;
1398
1399 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1400 for (guint i = 0; i < firmware_types->len; i++) {
1401 const gchar *id = g_ptr_array_index (firmware_types, i);
1402 g_print ("%s\n", id);
1403 }
1404 if (firmware_types->len == 0) {
1405 /* TRANSLATORS: nothing found */
1406 g_print ("%s\n", _("No firmware IDs found"));
1407 return TRUE;
1408 }
1409
1410 return TRUE;
1411}
1412
1413static gchar *
1414fu_util_prompt_for_firmware_type (FuUtilPrivate *priv, GError **error)
1415{
1416 g_autoptr(GPtrArray) firmware_types = NULL;
1417 guint idx;
1418 firmware_types = fu_engine_get_firmware_gtype_ids (priv->engine);
1419
1420 /* TRANSLATORS: get interactive prompt */
1421 g_print ("%s\n", _("Choose a firmware type:"));
1422 /* TRANSLATORS: this is to abort the interactive prompt */
1423 g_print ("0.\t%s\n", _("Cancel"));
1424 for (guint i = 0; i < firmware_types->len; i++) {
1425 const gchar *id = g_ptr_array_index (firmware_types, i);
1426 g_print ("%u.\t%s\n", i + 1, id);
1427 }
1428 idx = fu_util_prompt_for_number (firmware_types->len);
1429 if (idx == 0) {
1430 g_set_error_literal (error,
1431 FWUPD_ERROR,
1432 FWUPD_ERROR_NOTHING_TO_DO,
1433 "Request canceled");
1434 return NULL;
1435 }
1436
1437 return g_strdup (g_ptr_array_index (firmware_types, idx - 1));
1438}
1439
1440static gboolean
1441fu_util_firmware_parse (FuUtilPrivate *priv, gchar **values, GError **error)
1442{
1443 GType gtype;
1444 g_autoptr(GBytes) blob = NULL;
1445 g_autoptr(FuFirmware) firmware = NULL;
1446 g_autofree gchar *firmware_type = NULL;
1447 g_autofree gchar *str = NULL;
1448
1449 /* check args */
1450 if (g_strv_length (values) == 0 || g_strv_length (values) > 2) {
1451 g_set_error_literal (error,
1452 FWUPD_ERROR,
1453 FWUPD_ERROR_INVALID_ARGS,
1454 "Invalid arguments: filename required");
1455 return FALSE;
1456 }
1457
1458 if (g_strv_length (values) == 2)
1459 firmware_type = g_strdup (values[1]);
1460
1461 /* load file */
1462 blob = fu_common_get_contents_bytes (values[0], error);
1463 if (blob == NULL)
1464 return FALSE;
1465
1466 /* load engine */
1467 if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NO_ENUMERATE, error))
1468 return FALSE;
1469
1470 /* find the GType to use */
1471 if (firmware_type == NULL)
1472 firmware_type = fu_util_prompt_for_firmware_type (priv, error);
1473 if (firmware_type == NULL)
1474 return FALSE;
1475 gtype = fu_engine_get_firmware_gtype_by_id (priv->engine, firmware_type);
1476 if (gtype == G_TYPE_INVALID) {
1477 g_set_error (error,
1478 G_IO_ERROR,
1479 G_IO_ERROR_NOT_FOUND,
1480 "GType %s not supported", firmware_type);
1481 return FALSE;
1482 }
1483 firmware = g_object_new (gtype, NULL);
1484 if (!fu_firmware_parse (firmware, blob, priv->flags, error))
1485 return FALSE;
1486 str = fu_firmware_to_string (firmware);
1487 g_print ("%s", str);
1488 return TRUE;
1489}
1490
1491static gboolean
Richard Hughes15684492019-03-15 16:27:50 +00001492fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1493{
1494 g_autofree gchar *str = NULL;
1495 g_autoptr(FuDevice) dev = NULL;
1496
1497 /* load engine */
1498 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1499 return FALSE;
1500
1501 /* get device */
1502 if (g_strv_length (values) == 1) {
1503 dev = fu_engine_get_device (priv->engine, values[1], error);
1504 if (dev == NULL)
1505 return FALSE;
1506 } else {
1507 dev = fu_util_prompt_for_device (priv, error);
1508 if (dev == NULL)
1509 return FALSE;
1510 }
1511
1512 /* add checksums */
1513 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1514 return FALSE;
1515
1516 /* show checksums */
1517 str = fu_device_to_string (dev);
1518 g_print ("%s\n", str);
1519 return TRUE;
1520}
1521
Mario Limonciellofe593942019-04-03 13:48:52 -05001522static gboolean
1523fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
1524{
1525 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001526 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello20cc9ee2019-09-05 07:27:26 -05001527 g_autofree gchar *title = fu_util_get_tree_title (priv);
Mario Limonciellofe593942019-04-03 13:48:52 -05001528
1529 /* load engine */
1530 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1531 return FALSE;
1532
1533 /* get all devices from the history database */
1534 devices = fu_engine_get_history (priv->engine, error);
1535 if (devices == NULL)
1536 return FALSE;
1537
1538 /* show each device */
1539 for (guint i = 0; i < devices->len; i++) {
Mario Limoncielloce94d162019-09-20 13:37:36 -05001540 g_autoptr(GPtrArray) rels = NULL;
Mario Limonciellofe593942019-04-03 13:48:52 -05001541 FwupdDevice *dev = g_ptr_array_index (devices, i);
Mario Limonciello3be596b2019-09-20 10:10:08 -05001542 FwupdRelease *rel;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001543 const gchar *remote;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001544 GNode *child;
Mario Limoncielloce94d162019-09-20 13:37:36 -05001545
Richard Hughes747f5702019-08-06 14:27:26 +01001546 if (!fu_util_filter_device (priv, dev))
1547 continue;
Mario Limonciello3be596b2019-09-20 10:10:08 -05001548 child = g_node_append_data (root, dev);
1549
1550 rel = fwupd_device_get_release_default (dev);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001551 if (rel == NULL)
1552 continue;
1553 remote = fwupd_release_get_remote_id (rel);
1554
1555 /* doesn't actually map to remote */
1556 if (remote == NULL) {
Mario Limonciello3be596b2019-09-20 10:10:08 -05001557 g_node_append_data (child, rel);
Mario Limoncielloce94d162019-09-20 13:37:36 -05001558 continue;
1559 }
1560
1561 /* try to lookup releases from client */
1562 rels = fu_engine_get_releases (priv->engine, fwupd_device_get_id (dev), error);
1563 if (rels == NULL)
1564 return FALSE;
1565
1566 /* map to a release in client */
1567 for (guint j = 0; j < rels->len; j++) {
1568 FwupdRelease *rel2 = g_ptr_array_index (rels, j);
1569 if (g_strcmp0 (remote,
1570 fwupd_release_get_remote_id (rel2)) != 0)
1571 continue;
1572 if (g_strcmp0 (fwupd_release_get_version (rel),
1573 fwupd_release_get_version (rel2)) != 0)
1574 continue;
1575 g_node_append_data (child, g_object_ref (rel2));
1576 rel = NULL;
1577 break;
1578 }
1579
1580 /* didn't match anything */
1581 if (rels->len == 0 || rel != NULL) {
1582 g_node_append_data (child, rel);
1583 continue;
1584 }
Mario Limonciello3be596b2019-09-20 10:10:08 -05001585
Mario Limonciellofe593942019-04-03 13:48:52 -05001586 }
Mario Limonciello20cc9ee2019-09-05 07:27:26 -05001587 fu_util_print_tree (root, title);
Mario Limonciellofe593942019-04-03 13:48:52 -05001588
1589 return TRUE;
1590}
1591
Richard Hughesb5976832018-05-18 10:02:09 +01001592int
1593main (int argc, char *argv[])
1594{
Richard Hughes460226a2018-05-21 20:56:21 +01001595 gboolean allow_older = FALSE;
1596 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001597 gboolean force = FALSE;
1598 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001599 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001600 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001601 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001602 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1603 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00001604 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01001605 g_autofree gchar *cmd_descriptions = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +01001606 g_autofree gchar *filter = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001607 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001608 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1609 /* TRANSLATORS: command line option */
1610 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001611 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1612 /* TRANSLATORS: command line option */
Mario Limonciello350fc4c2019-10-03 07:08:51 -05001613 _("Allow reinstalling existing firmware versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001614 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1615 /* TRANSLATORS: command line option */
1616 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001617 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1618 /* TRANSLATORS: command line option */
1619 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001620 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1621 /* TRANSLATORS: command line option */
1622 _("Do not check for reboot after update"), NULL },
Mario Limonciello98b95162019-10-30 09:20:43 -05001623 { "no-safety-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_safety_check,
1624 /* TRANSLATORS: command line option */
1625 _("Do not perform device safety checks"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001626 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1627 /* TRANSLATORS: command line option */
1628 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001629 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1630 /* TRANSLATORS: command line option */
1631 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001632 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001633 /* TRANSLATORS: command line option */
1634 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001635 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001636 /* TRANSLATORS: command line option */
1637 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001638 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1639 /* TRANSLATORS: command line option */
1640 _("Save device state into a JSON file between executions"), NULL },
Richard Hughes0e46b222019-09-05 12:13:35 +01001641 { "disable-ssl-strict", '\0', 0, G_OPTION_ARG_NONE, &priv->disable_ssl_strict,
1642 /* TRANSLATORS: command line option */
1643 _("Ignore SSL strict checks when downloading files"), NULL },
Richard Hughes747f5702019-08-06 14:27:26 +01001644 { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter,
1645 /* TRANSLATORS: command line option */
1646 _("Filter with a set of device flags using a ~ prefix to "
1647 "exclude, e.g. 'internal,~needs-reboot'"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001648 { NULL}
1649 };
1650
1651 setlocale (LC_ALL, "");
1652
Richard Hughes668ee212019-11-22 09:17:46 +00001653 bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR);
Richard Hughesb5976832018-05-18 10:02:09 +01001654 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1655 textdomain (GETTEXT_PACKAGE);
1656
Richard Hughes01c0bad2019-11-22 09:08:51 +00001657#ifdef HAVE_GETUID
Richard Hughesb5976832018-05-18 10:02:09 +01001658 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001659 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001660 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001661 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughes01c0bad2019-11-22 09:08:51 +00001662#endif
Richard Hughesb5976832018-05-18 10:02:09 +01001663
1664 /* create helper object */
1665 priv->loop = g_main_loop_new (NULL, FALSE);
1666 priv->progressbar = fu_progressbar_new ();
1667
1668 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00001669 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001670 "build-firmware",
1671 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1672 /* TRANSLATORS: command description */
1673 _("Build firmware using a sandbox"),
1674 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00001675 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001676 "smbios-dump",
1677 "FILE",
1678 /* TRANSLATORS: command description */
1679 _("Dump SMBIOS data from a file"),
1680 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00001681 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001682 "get-plugins",
1683 NULL,
1684 /* TRANSLATORS: command description */
1685 _("Get all enabled plugins registered with the system"),
1686 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00001687 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001688 "get-details",
1689 NULL,
1690 /* TRANSLATORS: command description */
1691 _("Gets details about a firmware file"),
1692 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00001693 fu_util_cmd_array_add (cmd_array,
Mario Limonciellofe593942019-04-03 13:48:52 -05001694 "get-history",
1695 NULL,
1696 /* TRANSLATORS: command description */
1697 _("Show history of firmware updates"),
1698 fu_util_get_history);
1699 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05001700 "get-updates,get-upgrades",
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001701 NULL,
1702 /* TRANSLATORS: command description */
1703 _("Gets the list of updates for connected hardware"),
1704 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00001705 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1a9127d2019-08-27 11:32:51 +01001706 "get-devices,get-topology",
Richard Hughes98ca9932018-05-18 10:24:07 +01001707 NULL,
1708 /* TRANSLATORS: command description */
1709 _("Get all devices that support firmware updates"),
1710 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00001711 fu_util_cmd_array_add (cmd_array,
Richard Hughes747f5702019-08-06 14:27:26 +01001712 "get-device-flags",
1713 NULL,
1714 /* TRANSLATORS: command description */
1715 _("Get all device flags supported by fwupd"),
1716 fu_util_get_device_flags);
1717 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001718 "watch",
1719 NULL,
1720 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001721 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001722 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00001723 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001724 "install-blob",
1725 "FILENAME DEVICE-ID",
1726 /* TRANSLATORS: command description */
1727 _("Install a firmware blob on a device"),
1728 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00001729 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001730 "install",
1731 "FILE [ID]",
1732 /* TRANSLATORS: command description */
1733 _("Install a firmware file on this hardware"),
1734 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00001735 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001736 "attach",
1737 "DEVICE-ID",
1738 /* TRANSLATORS: command description */
1739 _("Attach to firmware mode"),
1740 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001741 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001742 "detach",
1743 "DEVICE-ID",
1744 /* TRANSLATORS: command description */
1745 _("Detach to bootloader mode"),
1746 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001747 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001748 "activate",
1749 "[DEVICE-ID]",
1750 /* TRANSLATORS: command description */
1751 _("Activate pending devices"),
1752 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00001753 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001754 "hwids",
1755 "[FILE]",
1756 /* TRANSLATORS: command description */
1757 _("Return all the hardware IDs for the machine"),
1758 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00001759 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05001760 "monitor",
1761 NULL,
1762 /* TRANSLATORS: command description */
1763 _("Monitor the daemon for events"),
1764 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00001765 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05001766 "update,upgrade",
Mario Limonciello46aaee82019-01-10 12:58:00 -06001767 NULL,
1768 /* TRANSLATORS: command description */
1769 _("Update all devices that match local metadata"),
1770 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00001771 fu_util_cmd_array_add (cmd_array,
1772 "self-sign",
1773 "TEXT",
1774 /* TRANSLATORS: command description */
Richard Hughes12a021d2019-03-27 09:23:24 +00001775 C_("command-description",
1776 "Sign data using the client certificate"),
Richard Hughes3d607622019-03-07 16:59:27 +00001777 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00001778 fu_util_cmd_array_add (cmd_array,
1779 "verify-update",
1780 "[DEVICE_ID]",
1781 /* TRANSLATORS: command description */
1782 _("Update the stored metadata with current contents"),
1783 fu_util_verify_update);
Richard Hughes95c98a92019-10-22 16:03:15 +01001784 fu_util_cmd_array_add (cmd_array,
Richard Hughesa58510b2019-10-30 10:03:12 +00001785 "firmware-read",
1786 "FILENAME [DEVICE-ID]",
1787 /* TRANSLATORS: command description */
1788 _("Read a firmware blob from a device"),
1789 fu_util_firmware_read);
1790 fu_util_cmd_array_add (cmd_array,
Richard Hughes95c98a92019-10-22 16:03:15 +01001791 "firmware-parse",
1792 "FILENAME [FIRMWARE_TYPE]",
1793 /* TRANSLATORS: command description */
1794 _("Parse and show details about a firmware file"),
1795 fu_util_firmware_parse);
1796 fu_util_cmd_array_add (cmd_array,
1797 "get-firmware-types",
1798 NULL,
1799 /* TRANSLATORS: command description */
1800 _("List the available firmware types"),
1801 fu_util_get_firmware_types);
Richard Hughesb5976832018-05-18 10:02:09 +01001802
1803 /* do stuff on ctrl+c */
1804 priv->cancellable = g_cancellable_new ();
Richard Hughes9e5675e2019-11-22 09:35:03 +00001805#ifdef HAVE_GIO_UNIX
Richard Hughesb5976832018-05-18 10:02:09 +01001806 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1807 SIGINT, fu_util_sigint_cb,
1808 priv, NULL);
Richard Hughes9e5675e2019-11-22 09:35:03 +00001809#endif
Richard Hughesb5976832018-05-18 10:02:09 +01001810 g_signal_connect (priv->cancellable, "cancelled",
1811 G_CALLBACK (fu_util_cancelled_cb), priv);
1812
1813 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00001814 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001815
Mario Limonciello3f243a92019-01-21 22:05:23 -06001816 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001817 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001818 priv->no_reboot_check = TRUE;
Mario Limonciello98b95162019-10-30 09:20:43 -05001819 priv->no_safety_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001820 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1821 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001822
Richard Hughesb5976832018-05-18 10:02:09 +01001823 /* get a list of the commands */
1824 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00001825 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001826 g_option_context_set_summary (priv->context, cmd_descriptions);
1827 g_option_context_set_description (priv->context,
1828 "This tool allows an administrator to use the fwupd plugins "
1829 "without being installed on the host system.");
1830
1831 /* TRANSLATORS: program name */
1832 g_set_application_name (_("Firmware Utility"));
1833 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001834 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001835 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1836 if (!ret) {
1837 /* TRANSLATORS: the user didn't read the man page */
1838 g_print ("%s: %s\n", _("Failed to parse arguments"),
1839 error->message);
1840 return EXIT_FAILURE;
1841 }
1842
Richard Hughes0e46b222019-09-05 12:13:35 +01001843 /* allow disabling SSL strict mode for broken corporate proxies */
1844 if (priv->disable_ssl_strict) {
1845 /* TRANSLATORS: try to help */
1846 g_printerr ("%s\n", _("WARNING: Ignoring SSL strict checks, "
1847 "to do this automatically in the future "
1848 "export DISABLE_SSL_STRICT in your environment"));
1849 g_setenv ("DISABLE_SSL_STRICT", "1", TRUE);
1850 }
1851
Richard Hughes747f5702019-08-06 14:27:26 +01001852 /* parse filter flags */
1853 if (filter != NULL) {
1854 if (!fu_util_parse_filter_flags (filter,
1855 &priv->filter_include,
1856 &priv->filter_exclude,
1857 &error)) {
1858 /* TRANSLATORS: the user didn't read the man page */
1859 g_print ("%s: %s\n", _("Failed to parse flags for --filter"),
1860 error->message);
1861 return EXIT_FAILURE;
1862 }
1863 }
1864
1865
Richard Hughes460226a2018-05-21 20:56:21 +01001866 /* set flags */
Richard Hughes460226a2018-05-21 20:56:21 +01001867 if (allow_reinstall)
1868 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1869 if (allow_older)
1870 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1871 if (force)
1872 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1873
Richard Hughes98ca9932018-05-18 10:24:07 +01001874 /* load engine */
1875 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1876 g_signal_connect (priv->engine, "device-added",
1877 G_CALLBACK (fu_main_engine_device_added_cb),
1878 priv);
1879 g_signal_connect (priv->engine, "device-removed",
1880 G_CALLBACK (fu_main_engine_device_removed_cb),
1881 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001882 g_signal_connect (priv->engine, "status-changed",
1883 G_CALLBACK (fu_main_engine_status_changed_cb),
1884 priv);
1885 g_signal_connect (priv->engine, "percentage-changed",
1886 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1887 priv);
1888
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001889 /* just show versions and exit */
1890 if (version) {
1891 g_autofree gchar *version_str = fu_util_get_versions ();
1892 g_print ("%s\n", version_str);
1893 return EXIT_SUCCESS;
1894 }
1895
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001896 /* any plugin whitelist specified */
1897 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1898 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1899
Richard Hughesb5976832018-05-18 10:02:09 +01001900 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00001901 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01001902 if (!ret) {
1903 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1904 g_autofree gchar *tmp = NULL;
1905 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1906 g_print ("%s\n\n%s", error->message, tmp);
1907 return EXIT_FAILURE;
1908 }
1909 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1910 g_print ("%s\n", error->message);
1911 return EXIT_NOTHING_TO_DO;
1912 }
1913 g_print ("%s\n", error->message);
1914 return EXIT_FAILURE;
1915 }
1916
1917 /* success */
1918 return EXIT_SUCCESS;
1919}