blob: b5f21c7954859ffb8f8dda58e45583136405487c [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughesb5976832018-05-18 10:02:09 +01002 * Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com>
3 *
Mario Limonciello51308e62018-05-28 20:05:46 -05004 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesb5976832018-05-18 10:02:09 +01005 */
6
Richard Hughesb08e7bc2018-09-11 10:51:13 +01007#define G_LOG_DOMAIN "FuMain"
8
Richard Hughesb5976832018-05-18 10:02:09 +01009#include "config.h"
10
11#include <fwupd.h>
Mario Limonciello3f243a92019-01-21 22:05:23 -060012#include <glib/gstdio.h>
Richard Hughesb5976832018-05-18 10:02:09 +010013#include <glib/gi18n.h>
14#include <glib-unix.h>
15#include <locale.h>
16#include <stdlib.h>
17#include <unistd.h>
Richard Hughes3d178be2018-08-30 11:14:24 +010018#include <libsoup/soup.h>
Richard Hughesb5976832018-05-18 10:02:09 +010019
Mario Limonciello7a3df4b2019-01-31 10:27:22 -060020#include "fu-device-private.h"
Richard Hughes98ca9932018-05-18 10:24:07 +010021#include "fu-engine.h"
Mario Limonciello96a0dd52019-02-25 13:50:03 -060022#include "fu-history.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010023#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010024#include "fu-progressbar.h"
25#include "fu-smbios.h"
26#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050027#include "fu-debug.h"
Mario Limonciello1e35e4c2019-01-28 11:13:02 -060028#include "fwupd-common-private.h"
Mario Limonciello3143bad2019-02-27 07:31:00 -060029#include "fwupd-device-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010030
Richard Hughes3d005222019-05-17 14:02:41 +010031#ifdef HAVE_SYSTEMD
32#include "fu-systemd.h"
33#endif
34
Richard Hughesb5976832018-05-18 10:02:09 +010035/* custom return code */
36#define EXIT_NOTHING_TO_DO 2
37
Mario Limonciello3f243a92019-01-21 22:05:23 -060038typedef enum {
39 FU_UTIL_OPERATION_UNKNOWN,
40 FU_UTIL_OPERATION_UPDATE,
41 FU_UTIL_OPERATION_INSTALL,
42 FU_UTIL_OPERATION_LAST
43} FuUtilOperation;
44
Richard Hughesc77e1112019-03-01 10:16:26 +000045struct FuUtilPrivate {
Richard Hughesb5976832018-05-18 10:02:09 +010046 GCancellable *cancellable;
47 GMainLoop *loop;
48 GOptionContext *context;
Richard Hughes98ca9932018-05-18 10:24:07 +010049 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010050 FuProgressbar *progressbar;
Mario Limonciello3f243a92019-01-21 22:05:23 -060051 gboolean no_reboot_check;
Mario Limonciello53ce25d2019-02-01 16:09:03 +000052 gboolean prepare_blob;
53 gboolean cleanup_blob;
Mario Limonciello3143bad2019-02-27 07:31:00 -060054 gboolean enable_json_state;
Richard Hughes460226a2018-05-21 20:56:21 +010055 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050056 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050057 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060058 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050059 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060060 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060061 FwupdDeviceFlags completion_flags;
Richard Hughes747f5702019-08-06 14:27:26 +010062 FwupdDeviceFlags filter_include;
63 FwupdDeviceFlags filter_exclude;
Richard Hughesc77e1112019-03-01 10:16:26 +000064};
Richard Hughesb5976832018-05-18 10:02:09 +010065
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050066static gboolean
Mario Limonciello3143bad2019-02-27 07:31:00 -060067fu_util_save_current_state (FuUtilPrivate *priv, GError **error)
68{
69 g_autoptr(JsonBuilder) builder = NULL;
70 g_autoptr(JsonGenerator) json_generator = NULL;
71 g_autoptr(JsonNode) json_root = NULL;
72 g_autoptr(GPtrArray) devices = NULL;
73 g_autofree gchar *state = NULL;
74 g_autofree gchar *dirname = NULL;
75 g_autofree gchar *filename = NULL;
76
77 if (!priv->enable_json_state)
78 return TRUE;
79
80 devices = fu_engine_get_devices (priv->engine, error);
81 if (devices == NULL)
82 return FALSE;
83
84 /* create header */
85 builder = json_builder_new ();
86 json_builder_begin_object (builder);
87
88 /* add each device */
89 json_builder_set_member_name (builder, "Devices");
90 json_builder_begin_array (builder);
91 for (guint i = 0; i < devices->len; i++) {
92 FwupdDevice *dev = g_ptr_array_index (devices, i);
93 json_builder_begin_object (builder);
94 fwupd_device_to_json (dev, builder);
95 json_builder_end_object (builder);
96 }
97 json_builder_end_array (builder);
98 json_builder_end_object (builder);
99
100 /* export as a string */
101 json_root = json_builder_get_root (builder);
102 json_generator = json_generator_new ();
103 json_generator_set_pretty (json_generator, TRUE);
104 json_generator_set_root (json_generator, json_root);
105 state = json_generator_to_data (json_generator, NULL);
106 if (state == NULL)
107 return FALSE;
108 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
109 filename = g_build_filename (dirname, "state.json", NULL);
110 return g_file_set_contents (filename, state, -1, error);
111}
112
113static gboolean
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000114fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error)
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600115{
116 g_autoptr(GError) error_local = NULL;
117
Richard Hughesd92ccca2019-05-20 11:28:31 +0100118#ifdef HAVE_SYSTEMD
Richard Hughes3d005222019-05-17 14:02:41 +0100119 if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local))
Mario Limoncielloe8dd4d72019-02-26 15:28:04 -0600120 g_debug ("Failed top stop daemon: %s", error_local->message);
Richard Hughesd92ccca2019-05-20 11:28:31 +0100121#endif
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000122 if (!fu_engine_load (priv->engine, flags, error))
Richard Hughesf425d292019-01-18 17:57:39 +0000123 return FALSE;
124 if (fu_engine_get_tainted (priv->engine)) {
125 g_printerr ("WARNING: This tool has loaded 3rd party code and "
126 "is no longer supported by the upstream developers!\n");
127 }
128 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500129}
130
Richard Hughesb5976832018-05-18 10:02:09 +0100131static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500132fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
133{
134 g_autofree gchar *path = g_path_get_dirname (value);
135 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
136 g_prefix_error (error,
137 "Unable to access %s. You may need to copy %s to %s: ",
138 path, value, g_getenv ("HOME"));
139 }
140}
141
142static void
Richard Hughesb5976832018-05-18 10:02:09 +0100143fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
144{
145 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
146 /* TRANSLATORS: this is when a device ctrl+c's a watch */
147 g_print ("%s\n", _("Cancelled"));
148 g_main_loop_quit (priv->loop);
149}
150
151static gboolean
152fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
153{
154 g_autofree gchar *tmp = NULL;
155 g_autoptr(FuSmbios) smbios = NULL;
156 if (g_strv_length (values) < 1) {
157 g_set_error_literal (error,
158 FWUPD_ERROR,
159 FWUPD_ERROR_INVALID_ARGS,
160 "Invalid arguments");
161 return FALSE;
162 }
163 smbios = fu_smbios_new ();
164 if (!fu_smbios_setup_from_file (smbios, values[0], error))
165 return FALSE;
166 tmp = fu_smbios_to_string (smbios);
167 g_print ("%s\n", tmp);
168 return TRUE;
169}
170
Richard Hughesb5976832018-05-18 10:02:09 +0100171static gboolean
172fu_util_sigint_cb (gpointer user_data)
173{
174 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
175 g_debug ("Handling SIGINT");
176 g_cancellable_cancel (priv->cancellable);
177 return FALSE;
178}
179
180static void
181fu_util_private_free (FuUtilPrivate *priv)
182{
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500183 if (priv->current_device != NULL)
184 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100185 if (priv->engine != NULL)
186 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100187 if (priv->loop != NULL)
188 g_main_loop_unref (priv->loop);
189 if (priv->cancellable != NULL)
190 g_object_unref (priv->cancellable);
191 if (priv->progressbar != NULL)
192 g_object_unref (priv->progressbar);
193 if (priv->context != NULL)
194 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600195 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100196 g_free (priv);
197}
198
199#pragma clang diagnostic push
200#pragma clang diagnostic ignored "-Wunused-function"
201G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
202#pragma clang diagnostic pop
203
Richard Hughes98ca9932018-05-18 10:24:07 +0100204
205static void
206fu_main_engine_device_added_cb (FuEngine *engine,
207 FuDevice *device,
208 FuUtilPrivate *priv)
209{
210 g_autofree gchar *tmp = fu_device_to_string (device);
211 g_debug ("ADDED:\n%s", tmp);
212}
213
214static void
215fu_main_engine_device_removed_cb (FuEngine *engine,
216 FuDevice *device,
217 FuUtilPrivate *priv)
218{
219 g_autofree gchar *tmp = fu_device_to_string (device);
220 g_debug ("REMOVED:\n%s", tmp);
221}
222
223static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100224fu_main_engine_status_changed_cb (FuEngine *engine,
225 FwupdStatus status,
226 FuUtilPrivate *priv)
227{
228 fu_progressbar_update (priv->progressbar, status, 0);
229}
230
231static void
232fu_main_engine_percentage_changed_cb (FuEngine *engine,
233 guint percentage,
234 FuUtilPrivate *priv)
235{
236 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
237}
238
239static gboolean
240fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
241{
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000242 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100243 return FALSE;
244 g_main_loop_run (priv->loop);
245 return TRUE;
246}
247
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100248static gint
249fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
250{
251 return fu_plugin_name_compare (*item1, *item2);
252}
253
254static gboolean
255fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
256{
257 GPtrArray *plugins;
258 guint cnt = 0;
259
260 /* load engine */
261 if (!fu_engine_load_plugins (priv->engine, error))
262 return FALSE;
263
264 /* print */
265 plugins = fu_engine_get_plugins (priv->engine);
266 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
267 for (guint i = 0; i < plugins->len; i++) {
268 FuPlugin *plugin = g_ptr_array_index (plugins, i);
269 if (!fu_plugin_get_enabled (plugin))
270 continue;
271 g_print ("%s\n", fu_plugin_get_name (plugin));
272 cnt++;
273 }
274 if (cnt == 0) {
275 /* TRANSLATORS: nothing found */
276 g_print ("%s\n", _("No plugins found"));
277 return TRUE;
278 }
279
280 return TRUE;
281}
282
Richard Hughes98ca9932018-05-18 10:24:07 +0100283static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100284fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev)
285{
286 if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) {
287 if (!fwupd_device_has_flag (dev, priv->filter_include))
288 return FALSE;
289 }
290 if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) {
291 if (fwupd_device_has_flag (dev, priv->filter_exclude))
292 return FALSE;
293 }
294 return TRUE;
295}
296
297static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600298fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
299{
300 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500301 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600302
303 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000304 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600305 return FALSE;
306
307 /* get devices from daemon */
308 devices = fu_engine_get_devices (priv->engine, error);
309 if (devices == NULL)
310 return FALSE;
311 for (guint i = 0; i < devices->len; i++) {
312 FwupdDevice *dev = g_ptr_array_index (devices, i);
313 g_autoptr(GPtrArray) rels = NULL;
314 g_autoptr(GError) error_local = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500315 GNode *child;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600316
Richard Hughes747f5702019-08-06 14:27:26 +0100317 /* not going to have results, so save a engine round-trip */
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600318 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
319 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100320 if (!fu_util_filter_device (priv, dev))
321 continue;
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600322
323 /* get the releases for this device and filter for validity */
324 rels = fu_engine_get_upgrades (priv->engine,
325 fwupd_device_get_id (dev),
326 &error_local);
327 if (rels == NULL) {
328 g_printerr ("%s\n", error_local->message);
329 continue;
330 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500331 child = g_node_append_data (root, dev);
332
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600333 for (guint j = 0; j < rels->len; j++) {
334 FwupdRelease *rel = g_ptr_array_index (rels, j);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500335 g_node_append_data (child, g_object_ref (rel));
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600336 }
337 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500338 if (g_node_n_nodes (root, G_TRAVERSE_ALL) > 1)
339 fu_util_print_tree (root, priv);
Mario Limonciello3143bad2019-02-27 07:31:00 -0600340 /* save the device state for other applications to see */
341 if (!fu_util_save_current_state (priv, error))
342 return FALSE;
343
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600344 /* success */
345 return TRUE;
346}
347
348static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500349fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
350{
351 g_autoptr(GPtrArray) array = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500352 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciello716ab272018-05-29 12:34:37 -0500353 gint fd;
354
355 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000356 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500357 return FALSE;
358
359 /* check args */
360 if (g_strv_length (values) != 1) {
361 g_set_error_literal (error,
362 FWUPD_ERROR,
363 FWUPD_ERROR_INVALID_ARGS,
364 "Invalid arguments");
365 return FALSE;
366 }
367
368 /* open file */
369 fd = open (values[0], O_RDONLY);
370 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500371 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500372 g_set_error (error,
373 FWUPD_ERROR,
374 FWUPD_ERROR_INVALID_FILE,
375 "failed to open %s",
376 values[0]);
377 return FALSE;
378 }
379 array = fu_engine_get_details (priv->engine, fd, error);
380 close (fd);
381
382 if (array == NULL)
383 return FALSE;
384 for (guint i = 0; i < array->len; i++) {
385 FwupdDevice *dev = g_ptr_array_index (array, i);
Richard Hughes747f5702019-08-06 14:27:26 +0100386 if (!fu_util_filter_device (priv, dev))
387 continue;
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500388 g_node_append_data (root, dev);
Mario Limonciello716ab272018-05-29 12:34:37 -0500389 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500390 fu_util_print_tree (root, priv);
391
Mario Limonciello716ab272018-05-29 12:34:37 -0500392 return TRUE;
393}
394
395static gboolean
Richard Hughes747f5702019-08-06 14:27:26 +0100396fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error)
397{
398 g_autoptr(GString) str = g_string_new (NULL);
399
400 for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) {
401 const gchar *tmp = fwupd_device_flag_to_string (i);
402 if (tmp == NULL)
403 break;
404 if (i != FWUPD_DEVICE_FLAG_INTERNAL)
405 g_string_append (str, " ");
406 g_string_append (str, tmp);
407 g_string_append (str, " ~");
408 g_string_append (str, tmp);
409 }
410 g_print ("%s\n", str->str);
411
412 return TRUE;
413}
414
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500415static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100416fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500417{
418 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100419 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Richard Hughes747f5702019-08-06 14:27:26 +0100420 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp)))
421 continue;
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500422 if (!priv->show_all_devices &&
423 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500424 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100425 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500426 GNode *child = g_node_append_data (root, dev_tmp);
427 fu_util_build_device_tree (priv, child, devs, dev_tmp);
428 }
429 }
430}
431
432static gboolean
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100433fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500434{
435 g_autoptr(GNode) root = g_node_new (NULL);
436 g_autoptr(GPtrArray) devs = NULL;
437
438 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000439 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500440 return FALSE;
441
442 /* print */
443 devs = fu_engine_get_devices (priv->engine, error);
444 if (devs == NULL)
445 return FALSE;
446
447 /* print */
448 if (devs->len == 0) {
449 /* TRANSLATORS: nothing attached that can be upgraded */
450 g_print ("%s\n", _("No hardware detected with firmware update capability"));
451 return TRUE;
452 }
453 fu_util_build_device_tree (priv, root, devs, NULL);
Mario Limonciello4250d9d2019-08-29 09:53:44 -0500454 fu_util_print_tree (root, priv);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500455
Mario Limonciello1a9127d2019-08-27 11:32:51 +0100456 /* save the device state for other applications to see */
457 return fu_util_save_current_state (priv, error);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500458}
459
Richard Hughes98ca9932018-05-18 10:24:07 +0100460static FuDevice *
461fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
462{
463 FuDevice *dev;
464 guint idx;
465 g_autoptr(GPtrArray) devices = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +0100466 g_autoptr(GPtrArray) devices_filtered = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100467
468 /* get devices from daemon */
469 devices = fu_engine_get_devices (priv->engine, error);
470 if (devices == NULL)
471 return NULL;
472
Richard Hughes747f5702019-08-06 14:27:26 +0100473 /* filter results */
474 devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
475 for (guint i = 0; i < devices->len; i++) {
476 dev = g_ptr_array_index (devices, i);
477 if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev)))
478 continue;
479 if (!fwupd_device_has_flag (FWUPD_DEVICE (dev), FWUPD_DEVICE_FLAG_SUPPORTED))
480 continue;
481 g_ptr_array_add (devices_filtered, g_object_ref (dev));
482 }
483
484 /* nothing */
485 if (devices_filtered->len == 0) {
486 g_set_error_literal (error,
487 FWUPD_ERROR,
488 FWUPD_ERROR_NOTHING_TO_DO,
489 "No supported devices");
490 return NULL;
491 }
492
Richard Hughes98ca9932018-05-18 10:24:07 +0100493 /* exactly one */
Richard Hughes747f5702019-08-06 14:27:26 +0100494 if (devices_filtered->len == 1) {
495 dev = g_ptr_array_index (devices_filtered, 0);
Richard Hughes98ca9932018-05-18 10:24:07 +0100496 return g_object_ref (dev);
497 }
498
499 /* TRANSLATORS: get interactive prompt */
500 g_print ("%s\n", _("Choose a device:"));
501 /* TRANSLATORS: this is to abort the interactive prompt */
502 g_print ("0.\t%s\n", _("Cancel"));
Richard Hughes747f5702019-08-06 14:27:26 +0100503 for (guint i = 0; i < devices_filtered->len; i++) {
504 dev = g_ptr_array_index (devices_filtered, i);
Richard Hughes98ca9932018-05-18 10:24:07 +0100505 g_print ("%u.\t%s (%s)\n",
506 i + 1,
507 fu_device_get_id (dev),
508 fu_device_get_name (dev));
509 }
Richard Hughes747f5702019-08-06 14:27:26 +0100510 idx = fu_util_prompt_for_number (devices_filtered->len);
Richard Hughes98ca9932018-05-18 10:24:07 +0100511 if (idx == 0) {
512 g_set_error_literal (error,
513 FWUPD_ERROR,
514 FWUPD_ERROR_NOTHING_TO_DO,
515 "Request canceled");
516 return NULL;
517 }
Richard Hughes747f5702019-08-06 14:27:26 +0100518 dev = g_ptr_array_index (devices_filtered, idx - 1);
Richard Hughes98ca9932018-05-18 10:24:07 +0100519 return g_object_ref (dev);
520}
521
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500522static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600523fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500524 FwupdDevice *device,
525 FuUtilPrivate *priv)
526{
527 g_autofree gchar *str = NULL;
528
Richard Hughes809abea2019-03-23 11:06:18 +0000529 /* allowed to set whenever the device has changed */
530 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
531 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
532 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
533 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
534
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500535 /* same as last time, so ignore */
536 if (priv->current_device != NULL &&
537 fwupd_device_compare (priv->current_device, device) == 0)
538 return;
539
540 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600541 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
542 /* TRANSLATORS: %1 is a device name */
543 str = g_strdup_printf (_("Updating %s…"),
544 fwupd_device_get_name (device));
545 fu_progressbar_set_title (priv->progressbar, str);
546 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
547 /* TRANSLATORS: %1 is a device name */
548 str = g_strdup_printf (_("Installing on %s…"),
549 fwupd_device_get_name (device));
550 fu_progressbar_set_title (priv->progressbar, str);
551 } else {
552 g_warning ("no FuUtilOperation set");
553 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500554 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600555
Mario Limonciello32241f42019-01-24 10:12:41 -0600556 if (priv->current_message == NULL) {
557 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
558 if (tmp != NULL)
559 priv->current_message = g_strdup (tmp);
560 }
561}
562
563static void
564fu_util_display_current_message (FuUtilPrivate *priv)
565{
566 if (priv->current_message == NULL)
567 return;
568 g_print ("%s\n", priv->current_message);
569 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500570}
571
Richard Hughes98ca9932018-05-18 10:24:07 +0100572static gboolean
573fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
574{
575 g_autoptr(FuDevice) device = NULL;
576 g_autoptr(GBytes) blob_fw = NULL;
577
578 /* invalid args */
579 if (g_strv_length (values) == 0) {
580 g_set_error_literal (error,
581 FWUPD_ERROR,
582 FWUPD_ERROR_INVALID_ARGS,
583 "Invalid arguments");
584 return FALSE;
585 }
586
587 /* parse blob */
588 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500589 if (blob_fw == NULL) {
590 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100591 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500592 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100593
594 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000595 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100596 return FALSE;
597
598 /* get device */
599 if (g_strv_length (values) >= 2) {
600 device = fu_engine_get_device (priv->engine, values[1], error);
601 if (device == NULL)
602 return FALSE;
603 } else {
604 device = fu_util_prompt_for_device (priv, error);
605 if (device == NULL)
606 return FALSE;
607 }
608
Mario Limonciello3f243a92019-01-21 22:05:23 -0600609 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500610 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600611 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500612
Richard Hughes98ca9932018-05-18 10:24:07 +0100613 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000614 if (priv->prepare_blob) {
615 g_autoptr(GPtrArray) devices = NULL;
616 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
617 g_ptr_array_add (devices, g_object_ref (device));
618 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
619 g_prefix_error (error, "failed to prepare composite action: ");
620 return FALSE;
621 }
622 }
Mario Limonciello035818b2019-03-26 11:12:16 -0500623 priv->flags = FWUPD_INSTALL_FLAG_NO_HISTORY;
Richard Hughes84af6e72019-02-01 18:19:41 +0000624 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600625 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000626 if (priv->cleanup_blob) {
627 g_autoptr(FuDevice) device_new = NULL;
628 g_autoptr(GError) error_local = NULL;
629
630 /* get the possibly new device from the old ID */
631 device_new = fu_engine_get_device (priv->engine,
632 fu_device_get_id (device),
633 &error_local);
634 if (device_new == NULL) {
635 g_debug ("failed to find new device: %s",
636 error_local->message);
637 } else {
Mario Limonciellobb3fa5e2019-02-07 21:13:02 -0600638 g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000639 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
640 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
641 g_prefix_error (error, "failed to cleanup composite action: ");
642 return FALSE;
643 }
644 }
645 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600646
Mario Limonciello32241f42019-01-24 10:12:41 -0600647 fu_util_display_current_message (priv);
648
Mario Limonciello3f243a92019-01-21 22:05:23 -0600649 /* success */
650 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100651}
652
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100653static gint
654fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
655{
656 FuInstallTask *task1 = *((FuInstallTask **) a);
657 FuInstallTask *task2 = *((FuInstallTask **) b);
658 return fu_install_task_compare (task1, task2);
659}
660
661static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100662fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
663{
664 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
665 { "curl", uri, "--output", fn, NULL },
666 { NULL } };
667 for (guint i = 0; argv[i][0] != NULL; i++) {
668 g_autoptr(GError) error_local = NULL;
669 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
670 g_debug ("%s", error_local->message);
671 continue;
672 }
Richard Hughesb768e4d2019-02-26 13:55:18 +0000673 return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error);
Richard Hughes3d178be2018-08-30 11:14:24 +0100674 }
675 g_set_error_literal (error,
676 FWUPD_ERROR,
677 FWUPD_ERROR_NOT_FOUND,
678 "no supported out-of-process downloaders found");
679 return FALSE;
680}
681
682static gchar *
683fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
684{
685 g_autofree gchar *filename = NULL;
686 g_autoptr(SoupURI) uri = NULL;
687
688 /* a local file */
689 uri = soup_uri_new (perhapsfn);
690 if (uri == NULL)
691 return g_strdup (perhapsfn);
692
693 /* download the firmware to a cachedir */
694 filename = fu_util_get_user_cache_path (perhapsfn);
695 if (!fu_common_mkdir_parent (filename, error))
696 return NULL;
697 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
698 return NULL;
699 return g_steal_pointer (&filename);
700}
701
702static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100703fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
704{
Richard Hughes3d178be2018-08-30 11:14:24 +0100705 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100706 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100707 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100708 g_autoptr(GPtrArray) devices_possible = NULL;
709 g_autoptr(GPtrArray) errors = NULL;
710 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100711 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100712
Mario Limonciello8949e892018-05-25 08:03:06 -0500713 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000714 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500715 return FALSE;
716
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100717 /* handle both forms */
718 if (g_strv_length (values) == 1) {
719 devices_possible = fu_engine_get_devices (priv->engine, error);
720 if (devices_possible == NULL)
721 return FALSE;
722 } else if (g_strv_length (values) == 2) {
723 FuDevice *device = fu_engine_get_device (priv->engine,
724 values[1],
725 error);
726 if (device == NULL)
727 return FALSE;
728 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
729 g_ptr_array_add (devices_possible, device);
730 } else {
731 g_set_error_literal (error,
732 FWUPD_ERROR,
733 FWUPD_ERROR_INVALID_ARGS,
734 "Invalid arguments");
735 return FALSE;
736 }
737
Richard Hughes3d178be2018-08-30 11:14:24 +0100738 /* download if required */
739 filename = fu_util_download_if_required (priv, values[0], error);
740 if (filename == NULL)
741 return FALSE;
742
Richard Hughes481aa2a2018-09-18 20:51:46 +0100743 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100744 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500745 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100746 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100747 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500748 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100749 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
750 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100751 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600752 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100753 if (components == NULL)
754 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100755
Richard Hughes481aa2a2018-09-18 20:51:46 +0100756 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100757 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
758 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100759 for (guint i = 0; i < components->len; i++) {
760 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100761
762 /* do any devices pass the requirements */
763 for (guint j = 0; j < devices_possible->len; j++) {
764 FuDevice *device = g_ptr_array_index (devices_possible, j);
765 g_autoptr(FuInstallTask) task = NULL;
766 g_autoptr(GError) error_local = NULL;
767
768 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100769 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100770 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100771 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100772 &error_local)) {
773 g_debug ("requirement on %s:%s failed: %s",
774 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100775 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100776 error_local->message);
777 g_ptr_array_add (errors, g_steal_pointer (&error_local));
778 continue;
779 }
780
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600781 /* if component should have an update message from CAB */
782 fu_device_incorporate_from_component (device, component);
783
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100784 /* success */
785 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
786 }
787 }
788
789 /* order the install tasks by the device priority */
790 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
791
792 /* nothing suitable */
793 if (install_tasks->len == 0) {
794 GError *error_tmp = fu_common_error_array_get_best (errors);
795 g_propagate_error (error, error_tmp);
796 return FALSE;
797 }
798
Mario Limonciello3f243a92019-01-21 22:05:23 -0600799 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500800 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600801 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500802
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100803 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100804 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
805 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100806
Mario Limonciello32241f42019-01-24 10:12:41 -0600807 fu_util_display_current_message (priv);
808
Mario Limonciello3f243a92019-01-21 22:05:23 -0600809 /* we don't want to ask anything */
810 if (priv->no_reboot_check) {
811 g_debug ("skipping reboot check");
812 return TRUE;
813 }
814
Mario Limonciello3143bad2019-02-27 07:31:00 -0600815 /* save the device state for other applications to see */
816 if (!fu_util_save_current_state (priv, error))
817 return FALSE;
818
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100819 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600820 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100821}
822
Richard Hughes98ca9932018-05-18 10:24:07 +0100823static gboolean
Mario Limonciellofd734852019-08-01 16:41:42 -0500824fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error)
Mario Limonciello46aaee82019-01-10 12:58:00 -0600825{
Mario Limonciellofd734852019-08-01 16:41:42 -0500826 FwupdRemote *remote;
827 const gchar *remote_id;
828 const gchar *uri_tmp;
829 g_auto(GStrv) argv = NULL;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600830
Mario Limonciellofd734852019-08-01 16:41:42 -0500831 uri_tmp = fwupd_release_get_uri (rel);
832 if (uri_tmp == NULL) {
833 g_set_error_literal (error,
834 FWUPD_ERROR,
835 FWUPD_ERROR_INVALID_FILE,
836 "release missing URI");
837 return FALSE;
838 }
839 remote_id = fwupd_release_get_remote_id (rel);
840 if (remote_id == NULL) {
841 g_set_error (error,
842 FWUPD_ERROR,
843 FWUPD_ERROR_INVALID_FILE,
844 "failed to find remote for %s",
845 uri_tmp);
Richard Hughes747f5702019-08-06 14:27:26 +0100846 return FALSE;
Mario Limonciellofd734852019-08-01 16:41:42 -0500847 }
848
849 remote = fu_engine_get_remote_by_id (priv->engine,
850 remote_id,
851 error);
852 if (remote == NULL)
Mario Limonciello46aaee82019-01-10 12:58:00 -0600853 return FALSE;
854
Mario Limonciellofd734852019-08-01 16:41:42 -0500855 argv = g_new0 (gchar *, 2);
856 /* local remotes have the firmware already */
857 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
858 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
859 g_autofree gchar *path = g_path_get_dirname (fn_cache);
860 argv[0] = g_build_filename (path, uri_tmp, NULL);
861 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
862 argv[0] = g_strdup (uri_tmp + 7);
863 /* web remote, fu_util_install will download file */
864 } else {
865 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
866 }
867 return fu_util_install (priv, argv, error);
868}
869
870static gboolean
871fu_util_update_all (FuUtilPrivate *priv, GError **error)
872{
873 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello3f243a92019-01-21 22:05:23 -0600874
Mario Limonciello46aaee82019-01-10 12:58:00 -0600875 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000876 if (devices == NULL)
877 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600878 for (guint i = 0; i < devices->len; i++) {
879 FwupdDevice *dev = g_ptr_array_index (devices, i);
880 FwupdRelease *rel;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600881 const gchar *device_id;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600882 g_autoptr(GPtrArray) rels = NULL;
883 g_autoptr(GError) error_local = NULL;
884
885 if (!fu_util_is_interesting_device (dev))
886 continue;
887 /* only show stuff that has metadata available */
888 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
889 continue;
Richard Hughes747f5702019-08-06 14:27:26 +0100890 if (!fu_util_filter_device (priv, dev))
891 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600892
893 device_id = fu_device_get_id (dev);
894 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
895 if (rels == NULL) {
896 g_printerr ("%s\n", error_local->message);
897 continue;
898 }
899
900 rel = g_ptr_array_index (rels, 0);
Mario Limonciellofd734852019-08-01 16:41:42 -0500901 if (!fu_util_install_release (priv, rel, &error_local)) {
902 g_printerr ("%s\n", error_local->message);
Richard Hughes747f5702019-08-06 14:27:26 +0100903 continue;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600904 }
Mario Limonciellofd734852019-08-01 16:41:42 -0500905 fu_util_display_current_message (priv);
906 }
907 return TRUE;
908}
909
910static gboolean
911fu_util_update_by_id (FuUtilPrivate *priv, const gchar *device_id, GError **error)
912{
913 FwupdRelease *rel;
914 g_autoptr(FuDevice) dev = NULL;
915 g_autoptr(GPtrArray) rels = NULL;
916
917 /* do not allow a partial device-id */
918 dev = fu_engine_get_device (priv->engine, device_id, error);
919 if (dev == NULL)
920 return FALSE;
921
922 /* get the releases for this device and filter for validity */
923 rels = fu_engine_get_upgrades (priv->engine, device_id, error);
924 if (rels == NULL)
925 return FALSE;
926 rel = g_ptr_array_index (rels, 0);
927 if (!fu_util_install_release (priv, rel, error))
928 return FALSE;
929 fu_util_display_current_message (priv);
930
931 return TRUE;
932}
933
934static gboolean
935fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
936{
937 if (g_strv_length (values) > 1) {
938 g_set_error_literal (error,
939 FWUPD_ERROR,
940 FWUPD_ERROR_INVALID_ARGS,
941 "Invalid arguments");
942 return FALSE;
943 }
944
945 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
946 return FALSE;
947
948 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
949 g_signal_connect (priv->engine, "device-changed",
950 G_CALLBACK (fu_util_update_device_changed_cb), priv);
951
952 if (g_strv_length (values) == 1) {
953 if (!fu_util_update_by_id (priv, values[0], error))
954 return FALSE;
955 } else {
956 if (!fu_util_update_all (priv, error))
957 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600958 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600959
960 /* we don't want to ask anything */
961 if (priv->no_reboot_check) {
962 g_debug ("skipping reboot check");
963 return TRUE;
964 }
965
Mario Limonciello3143bad2019-02-27 07:31:00 -0600966 /* save the device state for other applications to see */
967 if (!fu_util_save_current_state (priv, error))
968 return FALSE;
969
Mario Limonciello3f243a92019-01-21 22:05:23 -0600970 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600971}
972
973static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100974fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
975{
976 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100977 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100978
979 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +0000980 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100981 return FALSE;
982
983 /* invalid args */
984 if (g_strv_length (values) == 0) {
985 g_set_error_literal (error,
986 FWUPD_ERROR,
987 FWUPD_ERROR_INVALID_ARGS,
988 "Invalid arguments");
989 return FALSE;
990 }
991
992 /* get device */
993 if (g_strv_length (values) >= 1) {
994 device = fu_engine_get_device (priv->engine, values[0], error);
995 if (device == NULL)
996 return FALSE;
997 } else {
998 device = fu_util_prompt_for_device (priv, error);
999 if (device == NULL)
1000 return FALSE;
1001 }
1002
1003 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001004 locker = fu_device_locker_new (device, error);
1005 if (locker == NULL)
1006 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001007 return fu_device_detach (device, error);
1008}
1009
1010static gboolean
1011fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1012{
1013 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001014 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001015
1016 /* load engine */
Richard Hughesc8cc77c2019-03-07 11:57:24 +00001017 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001018 return FALSE;
1019
1020 /* invalid args */
1021 if (g_strv_length (values) == 0) {
1022 g_set_error_literal (error,
1023 FWUPD_ERROR,
1024 FWUPD_ERROR_INVALID_ARGS,
1025 "Invalid arguments");
1026 return FALSE;
1027 }
1028
1029 /* get device */
1030 if (g_strv_length (values) >= 1) {
1031 device = fu_engine_get_device (priv->engine, values[0], error);
1032 if (device == NULL)
1033 return FALSE;
1034 } else {
1035 device = fu_util_prompt_for_device (priv, error);
1036 if (device == NULL)
1037 return FALSE;
1038 }
1039
1040 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001041 locker = fu_device_locker_new (device, error);
1042 if (locker == NULL)
1043 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001044 return fu_device_attach (device, error);
1045}
1046
Richard Hughes1d894f12018-08-31 13:05:51 +01001047static gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001048fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error)
1049{
1050 gboolean has_pending = FALSE;
1051 g_autoptr(FuHistory) history = fu_history_new ();
1052 g_autoptr(GPtrArray) devices = NULL;
1053
1054 /* check the history database before starting the daemon */
1055 if (g_strv_length (values) == 0) {
1056 devices = fu_history_get_devices (history, error);
1057 if (devices == NULL)
1058 return FALSE;
1059 } else if (g_strv_length (values) == 1) {
1060 FuDevice *device;
1061 device = fu_history_get_device_by_id (history, values[0], error);
1062 if (device == NULL)
1063 return FALSE;
1064 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1065 g_ptr_array_add (devices, device);
1066 } else {
1067 g_set_error_literal (error,
1068 FWUPD_ERROR,
1069 FWUPD_ERROR_INVALID_ARGS,
1070 "Invalid arguments");
1071 return FALSE;
1072 }
1073
1074 /* nothing to do */
1075 for (guint i = 0; i < devices->len; i++) {
1076 FuDevice *dev = g_ptr_array_index (devices, i);
1077 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) {
1078 fu_engine_add_plugin_filter (priv->engine,
1079 fu_device_get_plugin (dev));
1080 has_pending = TRUE;
1081 }
1082 }
1083 if (!has_pending) {
1084 g_set_error_literal (error,
1085 FWUPD_ERROR,
1086 FWUPD_ERROR_NOTHING_TO_DO,
1087 "No firmware to activate");
1088 return FALSE;
1089
1090 }
1091
1092 /* load engine */
Richard Hughes88dc0f42019-03-07 11:58:11 +00001093 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001094 return FALSE;
1095
1096 /* activate anything with _NEEDS_ACTIVATION */
1097 for (guint i = 0; i < devices->len; i++) {
1098 FuDevice *device = g_ptr_array_index (devices, i);
Richard Hughes747f5702019-08-06 14:27:26 +01001099 if (!fu_util_filter_device (priv, FWUPD_DEVICE (device)))
1100 continue;
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001101 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION))
1102 continue;
1103 /* TRANSLATORS: shown when shutting down to switch to the new version */
1104 g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device));
1105 if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error))
1106 return FALSE;
1107 }
1108
1109 return TRUE;
1110}
1111
1112static gboolean
Richard Hughes1d894f12018-08-31 13:05:51 +01001113fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1114{
1115 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1116 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1117 const gchar *hwid_keys[] = {
1118 FU_HWIDS_KEY_BIOS_VENDOR,
1119 FU_HWIDS_KEY_BIOS_VERSION,
1120 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1121 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1122 FU_HWIDS_KEY_MANUFACTURER,
1123 FU_HWIDS_KEY_FAMILY,
1124 FU_HWIDS_KEY_PRODUCT_NAME,
1125 FU_HWIDS_KEY_PRODUCT_SKU,
1126 FU_HWIDS_KEY_ENCLOSURE_KIND,
1127 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1128 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1129 NULL };
1130
1131 /* read DMI data */
1132 if (g_strv_length (values) == 0) {
1133 if (!fu_smbios_setup (smbios, error))
1134 return FALSE;
1135 } else if (g_strv_length (values) == 1) {
1136 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1137 return FALSE;
1138 } else {
1139 g_set_error_literal (error,
1140 FWUPD_ERROR,
1141 FWUPD_ERROR_INVALID_ARGS,
1142 "Invalid arguments");
1143 return FALSE;
1144 }
1145 if (!fu_hwids_setup (hwids, smbios, error))
1146 return FALSE;
1147
1148 /* show debug output */
1149 g_print ("Computer Information\n");
1150 g_print ("--------------------\n");
1151 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1152 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1153 if (tmp == NULL)
1154 continue;
1155 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1156 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1157 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1158 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1159 } else {
1160 g_print ("%s: %s\n", hwid_keys[i], tmp);
1161 }
1162 }
1163
1164 /* show GUIDs */
1165 g_print ("\nHardware IDs\n");
1166 g_print ("------------\n");
1167 for (guint i = 0; i < 15; i++) {
1168 const gchar *keys = NULL;
1169 g_autofree gchar *guid = NULL;
1170 g_autofree gchar *key = NULL;
1171 g_autofree gchar *keys_str = NULL;
1172 g_auto(GStrv) keysv = NULL;
1173 g_autoptr(GError) error_local = NULL;
1174
1175 /* get the GUID */
1176 key = g_strdup_printf ("HardwareID-%u", i);
1177 keys = fu_hwids_get_replace_keys (hwids, key);
1178 guid = fu_hwids_get_guid (hwids, key, &error_local);
1179 if (guid == NULL) {
1180 g_print ("%s\n", error_local->message);
1181 continue;
1182 }
1183
1184 /* show what makes up the GUID */
1185 keysv = g_strsplit (keys, "&", -1);
1186 keys_str = g_strjoinv (" + ", keysv);
1187 g_print ("{%s} <- %s\n", guid, keys_str);
1188 }
1189
1190 return TRUE;
1191}
1192
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001193static gboolean
1194fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1195{
1196 const gchar *script_fn = "startup.sh";
1197 const gchar *output_fn = "firmware.bin";
1198 g_autoptr(GBytes) archive_blob = NULL;
1199 g_autoptr(GBytes) firmware_blob = NULL;
1200 if (g_strv_length (values) < 2) {
1201 g_set_error_literal (error,
1202 FWUPD_ERROR,
1203 FWUPD_ERROR_INVALID_ARGS,
1204 "Invalid arguments");
1205 return FALSE;
1206 }
1207 archive_blob = fu_common_get_contents_bytes (values[0], error);
1208 if (archive_blob == NULL)
1209 return FALSE;
1210 if (g_strv_length (values) > 2)
1211 script_fn = values[2];
1212 if (g_strv_length (values) > 3)
1213 output_fn = values[3];
1214 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1215 if (firmware_blob == NULL)
1216 return FALSE;
1217 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1218}
1219
Richard Hughes3d607622019-03-07 16:59:27 +00001220static gboolean
1221fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error)
1222{
1223 g_autofree gchar *sig = NULL;
1224
1225 /* check args */
1226 if (g_strv_length (values) != 1) {
1227 g_set_error_literal (error,
1228 FWUPD_ERROR,
1229 FWUPD_ERROR_INVALID_ARGS,
1230 "Invalid arguments: value expected");
1231 return FALSE;
1232 }
1233
1234 /* start engine */
1235 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1236 return FALSE;
1237 sig = fu_engine_self_sign (priv->engine, values[0],
1238 FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP |
1239 FU_KEYRING_SIGN_FLAG_ADD_CERT, error);
1240 if (sig == NULL)
1241 return FALSE;
1242 g_print ("%s\n", sig);
1243 return TRUE;
1244}
1245
Mario Limonciello62f84862018-10-18 13:15:23 -05001246static void
1247fu_util_device_added_cb (FwupdClient *client,
1248 FwupdDevice *device,
1249 gpointer user_data)
1250{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001251 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001252 /* TRANSLATORS: this is when a device is hotplugged */
1253 g_print ("%s\n%s", _("Device added:"), tmp);
1254}
1255
1256static void
1257fu_util_device_removed_cb (FwupdClient *client,
1258 FwupdDevice *device,
1259 gpointer user_data)
1260{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001261 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001262 /* TRANSLATORS: this is when a device is hotplugged */
1263 g_print ("%s\n%s", _("Device removed:"), tmp);
1264}
1265
1266static void
1267fu_util_device_changed_cb (FwupdClient *client,
1268 FwupdDevice *device,
1269 gpointer user_data)
1270{
Mario Limonciellofee8f492019-08-18 12:16:07 -05001271 g_autofree gchar *tmp = fu_util_device_to_string (device, 0);
Mario Limonciello62f84862018-10-18 13:15:23 -05001272 /* TRANSLATORS: this is when a device has been updated */
1273 g_print ("%s\n%s", _("Device changed:"), tmp);
1274}
1275
1276static void
1277fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1278{
1279 /* TRANSLATORS: this is when the daemon state changes */
1280 g_print ("%s\n", _("Changed"));
1281}
1282
1283static gboolean
1284fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1285{
1286 g_autoptr(FwupdClient) client = fwupd_client_new ();
1287
1288 /* get all the devices */
1289 if (!fwupd_client_connect (client, priv->cancellable, error))
1290 return FALSE;
1291
1292 /* watch for any hotplugged device */
1293 g_signal_connect (client, "changed",
1294 G_CALLBACK (fu_util_changed_cb), priv);
1295 g_signal_connect (client, "device-added",
1296 G_CALLBACK (fu_util_device_added_cb), priv);
1297 g_signal_connect (client, "device-removed",
1298 G_CALLBACK (fu_util_device_removed_cb), priv);
1299 g_signal_connect (client, "device-changed",
1300 G_CALLBACK (fu_util_device_changed_cb), priv);
1301 g_signal_connect (priv->cancellable, "cancelled",
1302 G_CALLBACK (fu_util_cancelled_cb), priv);
1303 g_main_loop_run (priv->loop);
1304 return TRUE;
1305}
1306
Richard Hughes15684492019-03-15 16:27:50 +00001307static gboolean
1308fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
1309{
1310 g_autofree gchar *str = NULL;
1311 g_autoptr(FuDevice) dev = NULL;
1312
1313 /* load engine */
1314 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1315 return FALSE;
1316
1317 /* get device */
1318 if (g_strv_length (values) == 1) {
1319 dev = fu_engine_get_device (priv->engine, values[1], error);
1320 if (dev == NULL)
1321 return FALSE;
1322 } else {
1323 dev = fu_util_prompt_for_device (priv, error);
1324 if (dev == NULL)
1325 return FALSE;
1326 }
1327
1328 /* add checksums */
1329 if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error))
1330 return FALSE;
1331
1332 /* show checksums */
1333 str = fu_device_to_string (dev);
1334 g_print ("%s\n", str);
1335 return TRUE;
1336}
1337
Mario Limonciellofe593942019-04-03 13:48:52 -05001338static gboolean
1339fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
1340{
1341 g_autoptr(GPtrArray) devices = NULL;
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001342 g_autoptr(GNode) root = g_node_new (NULL);
Mario Limonciellofe593942019-04-03 13:48:52 -05001343
1344 /* load engine */
1345 if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error))
1346 return FALSE;
1347
1348 /* get all devices from the history database */
1349 devices = fu_engine_get_history (priv->engine, error);
1350 if (devices == NULL)
1351 return FALSE;
1352
1353 /* show each device */
1354 for (guint i = 0; i < devices->len; i++) {
1355 FwupdDevice *dev = g_ptr_array_index (devices, i);
Richard Hughes747f5702019-08-06 14:27:26 +01001356 if (!fu_util_filter_device (priv, dev))
1357 continue;
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001358 g_node_append_data (root, dev);
Mario Limonciellofe593942019-04-03 13:48:52 -05001359 }
Mario Limonciello4250d9d2019-08-29 09:53:44 -05001360 fu_util_print_tree (root, priv);
Mario Limonciellofe593942019-04-03 13:48:52 -05001361
1362 return TRUE;
1363}
1364
Richard Hughesb5976832018-05-18 10:02:09 +01001365int
1366main (int argc, char *argv[])
1367{
Richard Hughes460226a2018-05-21 20:56:21 +01001368 gboolean allow_older = FALSE;
1369 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001370 gboolean force = FALSE;
1371 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001372 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001373 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001374 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001375 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1376 g_autoptr(GError) error = NULL;
Richard Hughesc77e1112019-03-01 10:16:26 +00001377 g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new ();
Richard Hughesb5976832018-05-18 10:02:09 +01001378 g_autofree gchar *cmd_descriptions = NULL;
Richard Hughes747f5702019-08-06 14:27:26 +01001379 g_autofree gchar *filter = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001380 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001381 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1382 /* TRANSLATORS: command line option */
1383 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001384 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1385 /* TRANSLATORS: command line option */
1386 _("Allow re-installing existing firmware versions"), NULL },
1387 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1388 /* TRANSLATORS: command line option */
1389 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001390 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1391 /* TRANSLATORS: command line option */
1392 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001393 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1394 /* TRANSLATORS: command line option */
1395 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001396 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1397 /* TRANSLATORS: command line option */
1398 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001399 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1400 /* TRANSLATORS: command line option */
1401 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001402 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001403 /* TRANSLATORS: command line option */
1404 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001405 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001406 /* TRANSLATORS: command line option */
1407 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
Mario Limonciello3143bad2019-02-27 07:31:00 -06001408 { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state,
1409 /* TRANSLATORS: command line option */
1410 _("Save device state into a JSON file between executions"), NULL },
Richard Hughes747f5702019-08-06 14:27:26 +01001411 { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter,
1412 /* TRANSLATORS: command line option */
1413 _("Filter with a set of device flags using a ~ prefix to "
1414 "exclude, e.g. 'internal,~needs-reboot'"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001415 { NULL}
1416 };
1417
1418 setlocale (LC_ALL, "");
1419
1420 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1421 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1422 textdomain (GETTEXT_PACKAGE);
1423
1424 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001425 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001426 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001427 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001428
1429 /* create helper object */
1430 priv->loop = g_main_loop_new (NULL, FALSE);
1431 priv->progressbar = fu_progressbar_new ();
1432
1433 /* add commands */
Richard Hughesc77e1112019-03-01 10:16:26 +00001434 fu_util_cmd_array_add (cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001435 "build-firmware",
1436 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1437 /* TRANSLATORS: command description */
1438 _("Build firmware using a sandbox"),
1439 fu_util_firmware_builder);
Richard Hughesc77e1112019-03-01 10:16:26 +00001440 fu_util_cmd_array_add (cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001441 "smbios-dump",
1442 "FILE",
1443 /* TRANSLATORS: command description */
1444 _("Dump SMBIOS data from a file"),
1445 fu_util_smbios_dump);
Richard Hughesc77e1112019-03-01 10:16:26 +00001446 fu_util_cmd_array_add (cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001447 "get-plugins",
1448 NULL,
1449 /* TRANSLATORS: command description */
1450 _("Get all enabled plugins registered with the system"),
1451 fu_util_get_plugins);
Richard Hughesc77e1112019-03-01 10:16:26 +00001452 fu_util_cmd_array_add (cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001453 "get-details",
1454 NULL,
1455 /* TRANSLATORS: command description */
1456 _("Gets details about a firmware file"),
1457 fu_util_get_details);
Richard Hughesc77e1112019-03-01 10:16:26 +00001458 fu_util_cmd_array_add (cmd_array,
Mario Limonciellofe593942019-04-03 13:48:52 -05001459 "get-history",
1460 NULL,
1461 /* TRANSLATORS: command description */
1462 _("Show history of firmware updates"),
1463 fu_util_get_history);
1464 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05001465 "get-updates,get-upgrades",
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001466 NULL,
1467 /* TRANSLATORS: command description */
1468 _("Gets the list of updates for connected hardware"),
1469 fu_util_get_updates);
Richard Hughesc77e1112019-03-01 10:16:26 +00001470 fu_util_cmd_array_add (cmd_array,
Mario Limonciello1a9127d2019-08-27 11:32:51 +01001471 "get-devices,get-topology",
Richard Hughes98ca9932018-05-18 10:24:07 +01001472 NULL,
1473 /* TRANSLATORS: command description */
1474 _("Get all devices that support firmware updates"),
1475 fu_util_get_devices);
Richard Hughesc77e1112019-03-01 10:16:26 +00001476 fu_util_cmd_array_add (cmd_array,
Richard Hughes747f5702019-08-06 14:27:26 +01001477 "get-device-flags",
1478 NULL,
1479 /* TRANSLATORS: command description */
1480 _("Get all device flags supported by fwupd"),
1481 fu_util_get_device_flags);
1482 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001483 "watch",
1484 NULL,
1485 /* TRANSLATORS: command description */
Piotr DrÄ…g472fa592018-06-06 14:53:48 +02001486 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001487 fu_util_watch);
Richard Hughesc77e1112019-03-01 10:16:26 +00001488 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001489 "install-blob",
1490 "FILENAME DEVICE-ID",
1491 /* TRANSLATORS: command description */
1492 _("Install a firmware blob on a device"),
1493 fu_util_install_blob);
Richard Hughesc77e1112019-03-01 10:16:26 +00001494 fu_util_cmd_array_add (cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001495 "install",
1496 "FILE [ID]",
1497 /* TRANSLATORS: command description */
1498 _("Install a firmware file on this hardware"),
1499 fu_util_install);
Richard Hughesc77e1112019-03-01 10:16:26 +00001500 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001501 "attach",
1502 "DEVICE-ID",
1503 /* TRANSLATORS: command description */
1504 _("Attach to firmware mode"),
1505 fu_util_attach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001506 fu_util_cmd_array_add (cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001507 "detach",
1508 "DEVICE-ID",
1509 /* TRANSLATORS: command description */
1510 _("Detach to bootloader mode"),
1511 fu_util_detach);
Richard Hughesc77e1112019-03-01 10:16:26 +00001512 fu_util_cmd_array_add (cmd_array,
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001513 "activate",
1514 "[DEVICE-ID]",
1515 /* TRANSLATORS: command description */
1516 _("Activate pending devices"),
1517 fu_util_activate);
Richard Hughesc77e1112019-03-01 10:16:26 +00001518 fu_util_cmd_array_add (cmd_array,
Richard Hughes1d894f12018-08-31 13:05:51 +01001519 "hwids",
1520 "[FILE]",
1521 /* TRANSLATORS: command description */
1522 _("Return all the hardware IDs for the machine"),
1523 fu_util_hwids);
Richard Hughesc77e1112019-03-01 10:16:26 +00001524 fu_util_cmd_array_add (cmd_array,
Mario Limonciello62f84862018-10-18 13:15:23 -05001525 "monitor",
1526 NULL,
1527 /* TRANSLATORS: command description */
1528 _("Monitor the daemon for events"),
1529 fu_util_monitor);
Richard Hughesc77e1112019-03-01 10:16:26 +00001530 fu_util_cmd_array_add (cmd_array,
Mario Limonciellodfff18e2019-08-29 11:51:41 -05001531 "update,upgrade",
Mario Limonciello46aaee82019-01-10 12:58:00 -06001532 NULL,
1533 /* TRANSLATORS: command description */
1534 _("Update all devices that match local metadata"),
1535 fu_util_update);
Richard Hughes3d607622019-03-07 16:59:27 +00001536 fu_util_cmd_array_add (cmd_array,
1537 "self-sign",
1538 "TEXT",
1539 /* TRANSLATORS: command description */
Richard Hughes12a021d2019-03-27 09:23:24 +00001540 C_("command-description",
1541 "Sign data using the client certificate"),
Richard Hughes3d607622019-03-07 16:59:27 +00001542 fu_util_self_sign);
Richard Hughes15684492019-03-15 16:27:50 +00001543 fu_util_cmd_array_add (cmd_array,
1544 "verify-update",
1545 "[DEVICE_ID]",
1546 /* TRANSLATORS: command description */
1547 _("Update the stored metadata with current contents"),
1548 fu_util_verify_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001549
1550 /* do stuff on ctrl+c */
1551 priv->cancellable = g_cancellable_new ();
1552 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1553 SIGINT, fu_util_sigint_cb,
1554 priv, NULL);
1555 g_signal_connect (priv->cancellable, "cancelled",
1556 G_CALLBACK (fu_util_cancelled_cb), priv);
1557
1558 /* sort by command name */
Richard Hughesc77e1112019-03-01 10:16:26 +00001559 fu_util_cmd_array_sort (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001560
Mario Limonciello3f243a92019-01-21 22:05:23 -06001561 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001562 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001563 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001564 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1565 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001566
Richard Hughesb5976832018-05-18 10:02:09 +01001567 /* get a list of the commands */
1568 priv->context = g_option_context_new (NULL);
Richard Hughesc77e1112019-03-01 10:16:26 +00001569 cmd_descriptions = fu_util_cmd_array_to_string (cmd_array);
Richard Hughesb5976832018-05-18 10:02:09 +01001570 g_option_context_set_summary (priv->context, cmd_descriptions);
1571 g_option_context_set_description (priv->context,
1572 "This tool allows an administrator to use the fwupd plugins "
1573 "without being installed on the host system.");
1574
1575 /* TRANSLATORS: program name */
1576 g_set_application_name (_("Firmware Utility"));
1577 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001578 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001579 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1580 if (!ret) {
1581 /* TRANSLATORS: the user didn't read the man page */
1582 g_print ("%s: %s\n", _("Failed to parse arguments"),
1583 error->message);
1584 return EXIT_FAILURE;
1585 }
1586
Richard Hughes747f5702019-08-06 14:27:26 +01001587 /* parse filter flags */
1588 if (filter != NULL) {
1589 if (!fu_util_parse_filter_flags (filter,
1590 &priv->filter_include,
1591 &priv->filter_exclude,
1592 &error)) {
1593 /* TRANSLATORS: the user didn't read the man page */
1594 g_print ("%s: %s\n", _("Failed to parse flags for --filter"),
1595 error->message);
1596 return EXIT_FAILURE;
1597 }
1598 }
1599
1600
Richard Hughes460226a2018-05-21 20:56:21 +01001601 /* set flags */
Richard Hughes460226a2018-05-21 20:56:21 +01001602 if (allow_reinstall)
1603 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1604 if (allow_older)
1605 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1606 if (force)
1607 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1608
Richard Hughes98ca9932018-05-18 10:24:07 +01001609 /* load engine */
1610 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1611 g_signal_connect (priv->engine, "device-added",
1612 G_CALLBACK (fu_main_engine_device_added_cb),
1613 priv);
1614 g_signal_connect (priv->engine, "device-removed",
1615 G_CALLBACK (fu_main_engine_device_removed_cb),
1616 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001617 g_signal_connect (priv->engine, "status-changed",
1618 G_CALLBACK (fu_main_engine_status_changed_cb),
1619 priv);
1620 g_signal_connect (priv->engine, "percentage-changed",
1621 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1622 priv);
1623
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001624 /* just show versions and exit */
1625 if (version) {
1626 g_autofree gchar *version_str = fu_util_get_versions ();
1627 g_print ("%s\n", version_str);
1628 return EXIT_SUCCESS;
1629 }
1630
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001631 /* any plugin whitelist specified */
1632 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1633 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1634
Richard Hughesb5976832018-05-18 10:02:09 +01001635 /* run the specified command */
Richard Hughesc77e1112019-03-01 10:16:26 +00001636 ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error);
Richard Hughesb5976832018-05-18 10:02:09 +01001637 if (!ret) {
1638 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1639 g_autofree gchar *tmp = NULL;
1640 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1641 g_print ("%s\n\n%s", error->message, tmp);
1642 return EXIT_FAILURE;
1643 }
1644 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1645 g_print ("%s\n", error->message);
1646 return EXIT_NOTHING_TO_DO;
1647 }
1648 g_print ("%s\n", error->message);
1649 return EXIT_FAILURE;
1650 }
1651
1652 /* success */
1653 return EXIT_SUCCESS;
1654}