blob: 1ecf8ddd5fba25cf04d20482a6a0f60596b46e6c [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"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010022#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010023#include "fu-progressbar.h"
24#include "fu-smbios.h"
25#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050026#include "fu-debug.h"
Mario Limonciello1e35e4c2019-01-28 11:13:02 -060027#include "fwupd-common-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010028
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050029#define SYSTEMD_SERVICE "org.freedesktop.systemd1"
30#define SYSTEMD_OBJECT_PATH "/org/freedesktop/systemd1"
31#define SYSTEMD_MANAGER_INTERFACE "org.freedesktop.systemd1.Manager"
32#define SYSTEMD_FWUPD_UNIT "fwupd.service"
33
Richard Hughesb5976832018-05-18 10:02:09 +010034/* custom return code */
35#define EXIT_NOTHING_TO_DO 2
36
Mario Limonciello3f243a92019-01-21 22:05:23 -060037typedef enum {
38 FU_UTIL_OPERATION_UNKNOWN,
39 FU_UTIL_OPERATION_UPDATE,
40 FU_UTIL_OPERATION_INSTALL,
41 FU_UTIL_OPERATION_LAST
42} FuUtilOperation;
43
Richard Hughesb5976832018-05-18 10:02:09 +010044typedef struct {
45 GCancellable *cancellable;
46 GMainLoop *loop;
47 GOptionContext *context;
48 GPtrArray *cmd_array;
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;
Richard Hughes460226a2018-05-21 20:56:21 +010054 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050055 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050056 /* only valid in update and downgrade */
Mario Limonciello3f243a92019-01-21 22:05:23 -060057 FuUtilOperation current_operation;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050058 FwupdDevice *current_device;
Mario Limonciello32241f42019-01-24 10:12:41 -060059 gchar *current_message;
Mario Limonciello3f243a92019-01-21 22:05:23 -060060 FwupdDeviceFlags completion_flags;
Richard Hughesb5976832018-05-18 10:02:09 +010061} FuUtilPrivate;
62
63typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
64 gchar **values,
65 GError **error);
66
67typedef struct {
68 gchar *name;
69 gchar *arguments;
70 gchar *description;
71 FuUtilPrivateCb callback;
72} FuUtilItem;
73
74static void
75fu_util_item_free (FuUtilItem *item)
76{
77 g_free (item->name);
78 g_free (item->arguments);
79 g_free (item->description);
80 g_free (item);
81}
82
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050083static gboolean
84fu_util_start_engine (FuUtilPrivate *priv, GError **error)
85{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050086 g_autoptr(GDBusConnection) connection = NULL;
87 g_autoptr(GDBusProxy) proxy = NULL;
88 g_autoptr(GVariant) val = NULL;
89 g_autoptr(GError) error_local = NULL;
90
91 /* try to stop any already running daemon */
Mario Limonciello8101bfc2019-02-07 13:47:44 +000092 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error_local);
93 if (connection == NULL) {
94 g_debug ("Failed to get bus: %s", error_local->message);
95 return TRUE;
96 }
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050097 proxy = g_dbus_proxy_new_sync (connection,
98 G_DBUS_PROXY_FLAGS_NONE,
99 NULL,
100 SYSTEMD_SERVICE,
101 SYSTEMD_OBJECT_PATH,
102 SYSTEMD_MANAGER_INTERFACE,
103 NULL,
Mario Limonciello67b82af2018-11-01 13:37:00 -0500104 &error_local);
105 if (proxy == NULL) {
106 g_debug ("Failed to find %s: %s",
107 SYSTEMD_SERVICE,
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500108 error_local->message);
109 } else {
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500110 val = g_dbus_proxy_call_sync (proxy,
Mario Limonciello67b82af2018-11-01 13:37:00 -0500111 "GetUnit",
112 g_variant_new ("(s)",
113 SYSTEMD_FWUPD_UNIT),
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500114 G_DBUS_CALL_FLAGS_NONE,
115 -1,
116 NULL,
Mario Limonciello67b82af2018-11-01 13:37:00 -0500117 &error_local);
118 if (val == NULL) {
119 g_debug ("Unable to find %s: %s",
120 SYSTEMD_FWUPD_UNIT,
121 error_local->message);
122 } else {
123 g_variant_unref (val);
124 val = g_dbus_proxy_call_sync (proxy,
125 "StopUnit",
126 g_variant_new ("(ss)",
127 SYSTEMD_FWUPD_UNIT,
128 "replace"),
129 G_DBUS_CALL_FLAGS_NONE,
130 -1,
131 NULL,
132 error);
133 if (val == NULL)
134 return FALSE;
135 }
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500136 }
137
Richard Hughesf425d292019-01-18 17:57:39 +0000138 if (!fu_engine_load (priv->engine, error))
139 return FALSE;
140 if (fu_engine_get_tainted (priv->engine)) {
141 g_printerr ("WARNING: This tool has loaded 3rd party code and "
142 "is no longer supported by the upstream developers!\n");
143 }
144 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500145}
146
Richard Hughesb5976832018-05-18 10:02:09 +0100147static gint
148fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
149{
150 return g_strcmp0 ((*item1)->name, (*item2)->name);
151}
152
153static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500154fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
155{
156 g_autofree gchar *path = g_path_get_dirname (value);
157 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
158 g_prefix_error (error,
159 "Unable to access %s. You may need to copy %s to %s: ",
160 path, value, g_getenv ("HOME"));
161 }
162}
163
164static void
Richard Hughesb5976832018-05-18 10:02:09 +0100165fu_util_add (GPtrArray *array,
166 const gchar *name,
167 const gchar *arguments,
168 const gchar *description,
169 FuUtilPrivateCb callback)
170{
171 g_auto(GStrv) names = NULL;
172
173 g_return_if_fail (name != NULL);
174 g_return_if_fail (description != NULL);
175 g_return_if_fail (callback != NULL);
176
177 /* add each one */
178 names = g_strsplit (name, ",", -1);
179 for (guint i = 0; names[i] != NULL; i++) {
180 FuUtilItem *item = g_new0 (FuUtilItem, 1);
181 item->name = g_strdup (names[i]);
182 if (i == 0) {
183 item->description = g_strdup (description);
184 } else {
185 /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */
186 item->description = g_strdup_printf (_("Alias to %s"),
187 names[0]);
188 }
189 item->arguments = g_strdup (arguments);
190 item->callback = callback;
191 g_ptr_array_add (array, item);
192 }
193}
194
195static gchar *
196fu_util_get_descriptions (GPtrArray *array)
197{
198 gsize len;
199 const gsize max_len = 35;
200 GString *string;
201
202 /* print each command */
203 string = g_string_new ("");
204 for (guint i = 0; i < array->len; i++) {
205 FuUtilItem *item = g_ptr_array_index (array, i);
206 g_string_append (string, " ");
207 g_string_append (string, item->name);
208 len = strlen (item->name) + 2;
209 if (item->arguments != NULL) {
210 g_string_append (string, " ");
211 g_string_append (string, item->arguments);
212 len += strlen (item->arguments) + 1;
213 }
214 if (len < max_len) {
215 for (gsize j = len; j < max_len + 1; j++)
216 g_string_append_c (string, ' ');
217 g_string_append (string, item->description);
218 g_string_append_c (string, '\n');
219 } else {
220 g_string_append_c (string, '\n');
221 for (gsize j = 0; j < max_len + 1; j++)
222 g_string_append_c (string, ' ');
223 g_string_append (string, item->description);
224 g_string_append_c (string, '\n');
225 }
226 }
227
228 /* remove trailing newline */
229 if (string->len > 0)
230 g_string_set_size (string, string->len - 1);
231
232 return g_string_free (string, FALSE);
233}
234
235static gboolean
236fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error)
237{
238 /* find command */
239 for (guint i = 0; i < priv->cmd_array->len; i++) {
240 FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i);
241 if (g_strcmp0 (item->name, command) == 0)
242 return item->callback (priv, values, error);
243 }
244
245 /* not found */
246 g_set_error_literal (error,
247 FWUPD_ERROR,
248 FWUPD_ERROR_INVALID_ARGS,
249 /* TRANSLATORS: error message */
250 _("Command not found"));
251 return FALSE;
252}
253
254static void
255fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
256{
257 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
258 /* TRANSLATORS: this is when a device ctrl+c's a watch */
259 g_print ("%s\n", _("Cancelled"));
260 g_main_loop_quit (priv->loop);
261}
262
263static gboolean
264fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
265{
266 g_autofree gchar *tmp = NULL;
267 g_autoptr(FuSmbios) smbios = NULL;
268 if (g_strv_length (values) < 1) {
269 g_set_error_literal (error,
270 FWUPD_ERROR,
271 FWUPD_ERROR_INVALID_ARGS,
272 "Invalid arguments");
273 return FALSE;
274 }
275 smbios = fu_smbios_new ();
276 if (!fu_smbios_setup_from_file (smbios, values[0], error))
277 return FALSE;
278 tmp = fu_smbios_to_string (smbios);
279 g_print ("%s\n", tmp);
280 return TRUE;
281}
282
Richard Hughesb5976832018-05-18 10:02:09 +0100283static gboolean
284fu_util_sigint_cb (gpointer user_data)
285{
286 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
287 g_debug ("Handling SIGINT");
288 g_cancellable_cancel (priv->cancellable);
289 return FALSE;
290}
291
292static void
293fu_util_private_free (FuUtilPrivate *priv)
294{
295 if (priv->cmd_array != NULL)
296 g_ptr_array_unref (priv->cmd_array);
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500297 if (priv->current_device != NULL)
298 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100299 if (priv->engine != NULL)
300 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100301 if (priv->loop != NULL)
302 g_main_loop_unref (priv->loop);
303 if (priv->cancellable != NULL)
304 g_object_unref (priv->cancellable);
305 if (priv->progressbar != NULL)
306 g_object_unref (priv->progressbar);
307 if (priv->context != NULL)
308 g_option_context_free (priv->context);
Mario Limonciello32241f42019-01-24 10:12:41 -0600309 g_free (priv->current_message);
Richard Hughesb5976832018-05-18 10:02:09 +0100310 g_free (priv);
311}
312
313#pragma clang diagnostic push
314#pragma clang diagnostic ignored "-Wunused-function"
315G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
316#pragma clang diagnostic pop
317
Richard Hughes98ca9932018-05-18 10:24:07 +0100318
319static void
320fu_main_engine_device_added_cb (FuEngine *engine,
321 FuDevice *device,
322 FuUtilPrivate *priv)
323{
324 g_autofree gchar *tmp = fu_device_to_string (device);
325 g_debug ("ADDED:\n%s", tmp);
326}
327
328static void
329fu_main_engine_device_removed_cb (FuEngine *engine,
330 FuDevice *device,
331 FuUtilPrivate *priv)
332{
333 g_autofree gchar *tmp = fu_device_to_string (device);
334 g_debug ("REMOVED:\n%s", tmp);
335}
336
337static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100338fu_main_engine_status_changed_cb (FuEngine *engine,
339 FwupdStatus status,
340 FuUtilPrivate *priv)
341{
342 fu_progressbar_update (priv->progressbar, status, 0);
343}
344
345static void
346fu_main_engine_percentage_changed_cb (FuEngine *engine,
347 guint percentage,
348 FuUtilPrivate *priv)
349{
350 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
351}
352
353static gboolean
354fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
355{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500356 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100357 return FALSE;
358 g_main_loop_run (priv->loop);
359 return TRUE;
360}
361
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100362static gint
363fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
364{
365 return fu_plugin_name_compare (*item1, *item2);
366}
367
368static gboolean
369fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
370{
371 GPtrArray *plugins;
372 guint cnt = 0;
373
374 /* load engine */
375 if (!fu_engine_load_plugins (priv->engine, error))
376 return FALSE;
377
378 /* print */
379 plugins = fu_engine_get_plugins (priv->engine);
380 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
381 for (guint i = 0; i < plugins->len; i++) {
382 FuPlugin *plugin = g_ptr_array_index (plugins, i);
383 if (!fu_plugin_get_enabled (plugin))
384 continue;
385 g_print ("%s\n", fu_plugin_get_name (plugin));
386 cnt++;
387 }
388 if (cnt == 0) {
389 /* TRANSLATORS: nothing found */
390 g_print ("%s\n", _("No plugins found"));
391 return TRUE;
392 }
393
394 return TRUE;
395}
396
Richard Hughes98ca9932018-05-18 10:24:07 +0100397static gboolean
Mario Limonciello1e35e4c2019-01-28 11:13:02 -0600398fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error)
399{
400 g_autoptr(GPtrArray) devices = NULL;
401
402 /* load engine */
403 if (!fu_util_start_engine (priv, error))
404 return FALSE;
405
406 /* get devices from daemon */
407 devices = fu_engine_get_devices (priv->engine, error);
408 if (devices == NULL)
409 return FALSE;
410 for (guint i = 0; i < devices->len; i++) {
411 FwupdDevice *dev = g_ptr_array_index (devices, i);
412 g_autoptr(GPtrArray) rels = NULL;
413 g_autoptr(GError) error_local = NULL;
414
415 /* not going to have results, so save a D-Bus round-trip */
416 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
417 continue;
418
419 /* get the releases for this device and filter for validity */
420 rels = fu_engine_get_upgrades (priv->engine,
421 fwupd_device_get_id (dev),
422 &error_local);
423 if (rels == NULL) {
424 g_printerr ("%s\n", error_local->message);
425 continue;
426 }
427 g_print ("%s", fwupd_device_to_string (dev));
428 g_print (" Release information:\n");
429 /* print all releases */
430 for (guint j = 0; j < rels->len; j++) {
431 FwupdRelease *rel = g_ptr_array_index (rels, j);
432 g_print ("%s\n", fwupd_release_to_string (rel));
433 }
434 }
435
436 /* success */
437 return TRUE;
438}
439
440static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500441fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
442{
443 g_autoptr(GPtrArray) array = NULL;
444 gint fd;
445
446 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500447 if (!fu_util_start_engine (priv, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500448 return FALSE;
449
450 /* check args */
451 if (g_strv_length (values) != 1) {
452 g_set_error_literal (error,
453 FWUPD_ERROR,
454 FWUPD_ERROR_INVALID_ARGS,
455 "Invalid arguments");
456 return FALSE;
457 }
458
459 /* open file */
460 fd = open (values[0], O_RDONLY);
461 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500462 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500463 g_set_error (error,
464 FWUPD_ERROR,
465 FWUPD_ERROR_INVALID_FILE,
466 "failed to open %s",
467 values[0]);
468 return FALSE;
469 }
470 array = fu_engine_get_details (priv->engine, fd, error);
471 close (fd);
472
473 if (array == NULL)
474 return FALSE;
475 for (guint i = 0; i < array->len; i++) {
476 FwupdDevice *dev = g_ptr_array_index (array, i);
477 g_autofree gchar *tmp = NULL;
478 tmp = fwupd_device_to_string (dev);
Mario Limoncielloece90a42018-07-25 17:35:55 -0500479 g_print ("%s\n", tmp);
Mario Limonciello716ab272018-05-29 12:34:37 -0500480 }
481 return TRUE;
482}
483
484static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100485fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
486{
487 g_autoptr(GPtrArray) devs = NULL;
488
489 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500490 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100491 return FALSE;
492
493 /* print */
494 devs = fu_engine_get_devices (priv->engine, error);
495 if (devs == NULL)
496 return FALSE;
497 if (devs->len == 0) {
498 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500499 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100500 return TRUE;
501 }
502 for (guint i = 0; i < devs->len; i++) {
503 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500504 if (priv->show_all_devices || fu_util_is_interesting_device (dev)) {
505 g_autofree gchar *tmp = fwupd_device_to_string (dev);
506 g_print ("%s\n", tmp);
507 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100508 }
509
510 return TRUE;
511}
512
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500513static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100514fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500515{
516 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100517 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500518 if (!priv->show_all_devices &&
519 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500520 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100521 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500522 GNode *child = g_node_append_data (root, dev_tmp);
523 fu_util_build_device_tree (priv, child, devs, dev_tmp);
524 }
525 }
526}
527
528static gboolean
529fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
530{
531 g_autoptr(GNode) root = g_node_new (NULL);
532 g_autoptr(GPtrArray) devs = NULL;
533
534 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500535 if (!fu_util_start_engine (priv, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500536 return FALSE;
537
538 /* print */
539 devs = fu_engine_get_devices (priv->engine, error);
540 if (devs == NULL)
541 return FALSE;
542
543 /* print */
544 if (devs->len == 0) {
545 /* TRANSLATORS: nothing attached that can be upgraded */
546 g_print ("%s\n", _("No hardware detected with firmware update capability"));
547 return TRUE;
548 }
549 fu_util_build_device_tree (priv, root, devs, NULL);
550 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
551 fu_util_print_device_tree, priv);
552
553 return TRUE;
554}
555
556
Richard Hughes98ca9932018-05-18 10:24:07 +0100557static FuDevice *
558fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
559{
560 FuDevice *dev;
561 guint idx;
562 g_autoptr(GPtrArray) devices = NULL;
563
564 /* get devices from daemon */
565 devices = fu_engine_get_devices (priv->engine, error);
566 if (devices == NULL)
567 return NULL;
568
569 /* exactly one */
570 if (devices->len == 1) {
571 dev = g_ptr_array_index (devices, 0);
572 return g_object_ref (dev);
573 }
574
575 /* TRANSLATORS: get interactive prompt */
576 g_print ("%s\n", _("Choose a device:"));
577 /* TRANSLATORS: this is to abort the interactive prompt */
578 g_print ("0.\t%s\n", _("Cancel"));
579 for (guint i = 0; i < devices->len; i++) {
580 dev = g_ptr_array_index (devices, i);
581 g_print ("%u.\t%s (%s)\n",
582 i + 1,
583 fu_device_get_id (dev),
584 fu_device_get_name (dev));
585 }
586 idx = fu_util_prompt_for_number (devices->len);
587 if (idx == 0) {
588 g_set_error_literal (error,
589 FWUPD_ERROR,
590 FWUPD_ERROR_NOTHING_TO_DO,
591 "Request canceled");
592 return NULL;
593 }
594 dev = g_ptr_array_index (devices, idx - 1);
595 return g_object_ref (dev);
596}
597
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500598static void
Mario Limonciello3f243a92019-01-21 22:05:23 -0600599fu_util_update_device_changed_cb (FwupdClient *client,
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500600 FwupdDevice *device,
601 FuUtilPrivate *priv)
602{
603 g_autofree gchar *str = NULL;
604
605 /* same as last time, so ignore */
606 if (priv->current_device != NULL &&
607 fwupd_device_compare (priv->current_device, device) == 0)
608 return;
609
610 /* show message in progressbar */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600611 if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
612 /* TRANSLATORS: %1 is a device name */
613 str = g_strdup_printf (_("Updating %s…"),
614 fwupd_device_get_name (device));
615 fu_progressbar_set_title (priv->progressbar, str);
616 } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
617 /* TRANSLATORS: %1 is a device name */
618 str = g_strdup_printf (_("Installing on %s…"),
619 fwupd_device_get_name (device));
620 fu_progressbar_set_title (priv->progressbar, str);
621 } else {
622 g_warning ("no FuUtilOperation set");
623 }
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500624 g_set_object (&priv->current_device, device);
Mario Limonciello3f243a92019-01-21 22:05:23 -0600625
626 if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
627 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
628 else if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
629 priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
Mario Limonciello32241f42019-01-24 10:12:41 -0600630
631 if (priv->current_message == NULL) {
632 const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
633 if (tmp != NULL)
634 priv->current_message = g_strdup (tmp);
635 }
636}
637
638static void
639fu_util_display_current_message (FuUtilPrivate *priv)
640{
641 if (priv->current_message == NULL)
642 return;
643 g_print ("%s\n", priv->current_message);
644 g_clear_pointer (&priv->current_message, g_free);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500645}
646
Richard Hughes98ca9932018-05-18 10:24:07 +0100647static gboolean
648fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
649{
650 g_autoptr(FuDevice) device = NULL;
651 g_autoptr(GBytes) blob_fw = NULL;
652
653 /* invalid args */
654 if (g_strv_length (values) == 0) {
655 g_set_error_literal (error,
656 FWUPD_ERROR,
657 FWUPD_ERROR_INVALID_ARGS,
658 "Invalid arguments");
659 return FALSE;
660 }
661
662 /* parse blob */
663 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500664 if (blob_fw == NULL) {
665 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100666 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500667 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100668
669 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500670 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100671 return FALSE;
672
673 /* get device */
674 if (g_strv_length (values) >= 2) {
675 device = fu_engine_get_device (priv->engine, values[1], error);
676 if (device == NULL)
677 return FALSE;
678 } else {
679 device = fu_util_prompt_for_device (priv, error);
680 if (device == NULL)
681 return FALSE;
682 }
683
Mario Limonciello3f243a92019-01-21 22:05:23 -0600684 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500685 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600686 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500687
Richard Hughes98ca9932018-05-18 10:24:07 +0100688 /* write bare firmware */
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000689 if (priv->prepare_blob) {
690 g_autoptr(GPtrArray) devices = NULL;
691 devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
692 g_ptr_array_add (devices, g_object_ref (device));
693 if (!fu_engine_composite_prepare (priv->engine, devices, error)) {
694 g_prefix_error (error, "failed to prepare composite action: ");
695 return FALSE;
696 }
697 }
Richard Hughes84af6e72019-02-01 18:19:41 +0000698 if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error))
Mario Limonciello3f243a92019-01-21 22:05:23 -0600699 return FALSE;
Mario Limonciello53ce25d2019-02-01 16:09:03 +0000700 if (priv->cleanup_blob) {
701 g_autoptr(FuDevice) device_new = NULL;
702 g_autoptr(GError) error_local = NULL;
703
704 /* get the possibly new device from the old ID */
705 device_new = fu_engine_get_device (priv->engine,
706 fu_device_get_id (device),
707 &error_local);
708 if (device_new == NULL) {
709 g_debug ("failed to find new device: %s",
710 error_local->message);
711 } else {
712 g_autoptr(GPtrArray) devices_new = NULL;
713 g_ptr_array_add (devices_new, g_steal_pointer (&device_new));
714 if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) {
715 g_prefix_error (error, "failed to cleanup composite action: ");
716 return FALSE;
717 }
718 }
719 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600720
Mario Limonciello32241f42019-01-24 10:12:41 -0600721 fu_util_display_current_message (priv);
722
Mario Limonciello3f243a92019-01-21 22:05:23 -0600723 /* success */
724 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100725}
726
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100727static gint
728fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
729{
730 FuInstallTask *task1 = *((FuInstallTask **) a);
731 FuInstallTask *task2 = *((FuInstallTask **) b);
732 return fu_install_task_compare (task1, task2);
733}
734
735static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100736fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
737{
738 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
739 { "curl", uri, "--output", fn, NULL },
740 { NULL } };
741 for (guint i = 0; argv[i][0] != NULL; i++) {
742 g_autoptr(GError) error_local = NULL;
743 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
744 g_debug ("%s", error_local->message);
745 continue;
746 }
747 return fu_common_spawn_sync (argv[i], NULL, NULL, NULL, error);
748 }
749 g_set_error_literal (error,
750 FWUPD_ERROR,
751 FWUPD_ERROR_NOT_FOUND,
752 "no supported out-of-process downloaders found");
753 return FALSE;
754}
755
756static gchar *
757fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
758{
759 g_autofree gchar *filename = NULL;
760 g_autoptr(SoupURI) uri = NULL;
761
762 /* a local file */
763 uri = soup_uri_new (perhapsfn);
764 if (uri == NULL)
765 return g_strdup (perhapsfn);
766
767 /* download the firmware to a cachedir */
768 filename = fu_util_get_user_cache_path (perhapsfn);
769 if (!fu_common_mkdir_parent (filename, error))
770 return NULL;
771 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
772 return NULL;
773 return g_steal_pointer (&filename);
774}
775
776static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100777fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
778{
Richard Hughes3d178be2018-08-30 11:14:24 +0100779 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100780 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100781 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100782 g_autoptr(GPtrArray) devices_possible = NULL;
783 g_autoptr(GPtrArray) errors = NULL;
784 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100785 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100786
Mario Limonciello8949e892018-05-25 08:03:06 -0500787 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500788 if (!fu_util_start_engine (priv, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500789 return FALSE;
790
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100791 /* handle both forms */
792 if (g_strv_length (values) == 1) {
793 devices_possible = fu_engine_get_devices (priv->engine, error);
794 if (devices_possible == NULL)
795 return FALSE;
796 } else if (g_strv_length (values) == 2) {
797 FuDevice *device = fu_engine_get_device (priv->engine,
798 values[1],
799 error);
800 if (device == NULL)
801 return FALSE;
802 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
803 g_ptr_array_add (devices_possible, device);
804 } else {
805 g_set_error_literal (error,
806 FWUPD_ERROR,
807 FWUPD_ERROR_INVALID_ARGS,
808 "Invalid arguments");
809 return FALSE;
810 }
811
Richard Hughes3d178be2018-08-30 11:14:24 +0100812 /* download if required */
813 filename = fu_util_download_if_required (priv, values[0], error);
814 if (filename == NULL)
815 return FALSE;
816
Richard Hughes481aa2a2018-09-18 20:51:46 +0100817 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100818 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500819 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100820 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100821 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500822 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100823 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
824 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100825 return FALSE;
Mario Limonciello51ddf182019-01-26 00:31:58 -0600826 components = xb_silo_query (silo, "components/component", 0, error);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100827 if (components == NULL)
828 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100829
Richard Hughes481aa2a2018-09-18 20:51:46 +0100830 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100831 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
832 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100833 for (guint i = 0; i < components->len; i++) {
834 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100835
836 /* do any devices pass the requirements */
837 for (guint j = 0; j < devices_possible->len; j++) {
838 FuDevice *device = g_ptr_array_index (devices_possible, j);
839 g_autoptr(FuInstallTask) task = NULL;
840 g_autoptr(GError) error_local = NULL;
841
842 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100843 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100844 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100845 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100846 &error_local)) {
847 g_debug ("requirement on %s:%s failed: %s",
848 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100849 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100850 error_local->message);
851 g_ptr_array_add (errors, g_steal_pointer (&error_local));
852 continue;
853 }
854
Mario Limonciello7a3df4b2019-01-31 10:27:22 -0600855 /* if component should have an update message from CAB */
856 fu_device_incorporate_from_component (device, component);
857
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100858 /* success */
859 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
860 }
861 }
862
863 /* order the install tasks by the device priority */
864 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
865
866 /* nothing suitable */
867 if (install_tasks->len == 0) {
868 GError *error_tmp = fu_common_error_array_get_best (errors);
869 g_propagate_error (error, error_tmp);
870 return FALSE;
871 }
872
Mario Limonciello3f243a92019-01-21 22:05:23 -0600873 priv->current_operation = FU_UTIL_OPERATION_INSTALL;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500874 g_signal_connect (priv->engine, "device-changed",
Mario Limonciello3f243a92019-01-21 22:05:23 -0600875 G_CALLBACK (fu_util_update_device_changed_cb), priv);
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500876
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100877 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100878 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
879 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100880
Mario Limonciello32241f42019-01-24 10:12:41 -0600881 fu_util_display_current_message (priv);
882
Mario Limonciello3f243a92019-01-21 22:05:23 -0600883 /* we don't want to ask anything */
884 if (priv->no_reboot_check) {
885 g_debug ("skipping reboot check");
886 return TRUE;
887 }
888
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100889 /* success */
Mario Limonciello3f243a92019-01-21 22:05:23 -0600890 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100891}
892
Richard Hughes98ca9932018-05-18 10:24:07 +0100893static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600894fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
895{
896 g_autoptr(GPtrArray) devices = NULL;
897
898 /* load engine */
899 if (!fu_util_start_engine (priv, error))
900 return FALSE;
901
Mario Limonciello3f243a92019-01-21 22:05:23 -0600902 priv->current_operation = FU_UTIL_OPERATION_UPDATE;
903 g_signal_connect (priv->engine, "device-changed",
904 G_CALLBACK (fu_util_update_device_changed_cb), priv);
905
Mario Limonciello46aaee82019-01-10 12:58:00 -0600906 devices = fu_engine_get_devices (priv->engine, error);
Mario Limonciello387bda42019-02-07 14:20:22 +0000907 if (devices == NULL)
908 return FALSE;
Mario Limonciello46aaee82019-01-10 12:58:00 -0600909 for (guint i = 0; i < devices->len; i++) {
910 FwupdDevice *dev = g_ptr_array_index (devices, i);
911 FwupdRelease *rel;
912 const gchar *remote_id;
913 const gchar *device_id;
914 const gchar *uri_tmp;
915 g_autoptr(GPtrArray) rels = NULL;
916 g_autoptr(GError) error_local = NULL;
917
918 if (!fu_util_is_interesting_device (dev))
919 continue;
920 /* only show stuff that has metadata available */
921 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
922 continue;
923
924 device_id = fu_device_get_id (dev);
925 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
926 if (rels == NULL) {
927 g_printerr ("%s\n", error_local->message);
928 continue;
929 }
930
931 rel = g_ptr_array_index (rels, 0);
932 uri_tmp = fwupd_release_get_uri (rel);
933 remote_id = fwupd_release_get_remote_id (rel);
934 if (remote_id != NULL) {
935 FwupdRemote *remote;
936 g_auto(GStrv) argv = NULL;
937
938 remote = fu_engine_get_remote_by_id (priv->engine,
939 remote_id,
940 &error_local);
941 if (remote == NULL) {
942 g_printerr ("%s\n", error_local->message);
943 continue;
944 }
945
946 argv = g_new0 (gchar *, 2);
947 /* local remotes have the firmware already */
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600948 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
Mario Limonciello46aaee82019-01-10 12:58:00 -0600949 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
950 g_autofree gchar *path = g_path_get_dirname (fn_cache);
951 argv[0] = g_build_filename (path, uri_tmp, NULL);
Mario Limonciello4f24d0b2019-01-26 01:19:59 -0600952 } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) {
953 argv[0] = g_strdup (uri_tmp + 7);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600954 /* web remote, fu_util_install will download file */
955 } else {
956 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
957 }
958 if (!fu_util_install (priv, argv, &error_local)) {
959 g_printerr ("%s\n", error_local->message);
960 continue;
961 }
Mario Limonciello32241f42019-01-24 10:12:41 -0600962 fu_util_display_current_message (priv);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600963 }
964 }
Mario Limonciello3f243a92019-01-21 22:05:23 -0600965
966 /* we don't want to ask anything */
967 if (priv->no_reboot_check) {
968 g_debug ("skipping reboot check");
969 return TRUE;
970 }
971
972 return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
Mario Limonciello46aaee82019-01-10 12:58:00 -0600973}
974
975static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100976fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
977{
978 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100979 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100980
981 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500982 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100983 return FALSE;
984
985 /* invalid args */
986 if (g_strv_length (values) == 0) {
987 g_set_error_literal (error,
988 FWUPD_ERROR,
989 FWUPD_ERROR_INVALID_ARGS,
990 "Invalid arguments");
991 return FALSE;
992 }
993
994 /* get device */
995 if (g_strv_length (values) >= 1) {
996 device = fu_engine_get_device (priv->engine, values[0], error);
997 if (device == NULL)
998 return FALSE;
999 } else {
1000 device = fu_util_prompt_for_device (priv, error);
1001 if (device == NULL)
1002 return FALSE;
1003 }
1004
1005 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001006 locker = fu_device_locker_new (device, error);
1007 if (locker == NULL)
1008 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001009 return fu_device_detach (device, error);
1010}
1011
1012static gboolean
1013fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
1014{
1015 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +01001016 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +01001017
1018 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -05001019 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +01001020 return FALSE;
1021
1022 /* invalid args */
1023 if (g_strv_length (values) == 0) {
1024 g_set_error_literal (error,
1025 FWUPD_ERROR,
1026 FWUPD_ERROR_INVALID_ARGS,
1027 "Invalid arguments");
1028 return FALSE;
1029 }
1030
1031 /* get device */
1032 if (g_strv_length (values) >= 1) {
1033 device = fu_engine_get_device (priv->engine, values[0], error);
1034 if (device == NULL)
1035 return FALSE;
1036 } else {
1037 device = fu_util_prompt_for_device (priv, error);
1038 if (device == NULL)
1039 return FALSE;
1040 }
1041
1042 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +01001043 locker = fu_device_locker_new (device, error);
1044 if (locker == NULL)
1045 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +01001046 return fu_device_attach (device, error);
1047}
1048
Richard Hughes1d894f12018-08-31 13:05:51 +01001049static gboolean
1050fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
1051{
1052 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
1053 g_autoptr(FuHwids) hwids = fu_hwids_new ();
1054 const gchar *hwid_keys[] = {
1055 FU_HWIDS_KEY_BIOS_VENDOR,
1056 FU_HWIDS_KEY_BIOS_VERSION,
1057 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
1058 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
1059 FU_HWIDS_KEY_MANUFACTURER,
1060 FU_HWIDS_KEY_FAMILY,
1061 FU_HWIDS_KEY_PRODUCT_NAME,
1062 FU_HWIDS_KEY_PRODUCT_SKU,
1063 FU_HWIDS_KEY_ENCLOSURE_KIND,
1064 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
1065 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
1066 NULL };
1067
1068 /* read DMI data */
1069 if (g_strv_length (values) == 0) {
1070 if (!fu_smbios_setup (smbios, error))
1071 return FALSE;
1072 } else if (g_strv_length (values) == 1) {
1073 if (!fu_smbios_setup_from_file (smbios, values[0], error))
1074 return FALSE;
1075 } else {
1076 g_set_error_literal (error,
1077 FWUPD_ERROR,
1078 FWUPD_ERROR_INVALID_ARGS,
1079 "Invalid arguments");
1080 return FALSE;
1081 }
1082 if (!fu_hwids_setup (hwids, smbios, error))
1083 return FALSE;
1084
1085 /* show debug output */
1086 g_print ("Computer Information\n");
1087 g_print ("--------------------\n");
1088 for (guint i = 0; hwid_keys[i] != NULL; i++) {
1089 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
1090 if (tmp == NULL)
1091 continue;
1092 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
1093 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
1094 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
1095 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
1096 } else {
1097 g_print ("%s: %s\n", hwid_keys[i], tmp);
1098 }
1099 }
1100
1101 /* show GUIDs */
1102 g_print ("\nHardware IDs\n");
1103 g_print ("------------\n");
1104 for (guint i = 0; i < 15; i++) {
1105 const gchar *keys = NULL;
1106 g_autofree gchar *guid = NULL;
1107 g_autofree gchar *key = NULL;
1108 g_autofree gchar *keys_str = NULL;
1109 g_auto(GStrv) keysv = NULL;
1110 g_autoptr(GError) error_local = NULL;
1111
1112 /* get the GUID */
1113 key = g_strdup_printf ("HardwareID-%u", i);
1114 keys = fu_hwids_get_replace_keys (hwids, key);
1115 guid = fu_hwids_get_guid (hwids, key, &error_local);
1116 if (guid == NULL) {
1117 g_print ("%s\n", error_local->message);
1118 continue;
1119 }
1120
1121 /* show what makes up the GUID */
1122 keysv = g_strsplit (keys, "&", -1);
1123 keys_str = g_strjoinv (" + ", keysv);
1124 g_print ("{%s} <- %s\n", guid, keys_str);
1125 }
1126
1127 return TRUE;
1128}
1129
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001130static gboolean
1131fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
1132{
1133 const gchar *script_fn = "startup.sh";
1134 const gchar *output_fn = "firmware.bin";
1135 g_autoptr(GBytes) archive_blob = NULL;
1136 g_autoptr(GBytes) firmware_blob = NULL;
1137 if (g_strv_length (values) < 2) {
1138 g_set_error_literal (error,
1139 FWUPD_ERROR,
1140 FWUPD_ERROR_INVALID_ARGS,
1141 "Invalid arguments");
1142 return FALSE;
1143 }
1144 archive_blob = fu_common_get_contents_bytes (values[0], error);
1145 if (archive_blob == NULL)
1146 return FALSE;
1147 if (g_strv_length (values) > 2)
1148 script_fn = values[2];
1149 if (g_strv_length (values) > 3)
1150 output_fn = values[3];
1151 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1152 if (firmware_blob == NULL)
1153 return FALSE;
1154 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1155}
1156
Mario Limonciello62f84862018-10-18 13:15:23 -05001157static void
1158fu_util_device_added_cb (FwupdClient *client,
1159 FwupdDevice *device,
1160 gpointer user_data)
1161{
1162 g_autofree gchar *tmp = fwupd_device_to_string (device);
1163 /* TRANSLATORS: this is when a device is hotplugged */
1164 g_print ("%s\n%s", _("Device added:"), tmp);
1165}
1166
1167static void
1168fu_util_device_removed_cb (FwupdClient *client,
1169 FwupdDevice *device,
1170 gpointer user_data)
1171{
1172 g_autofree gchar *tmp = fwupd_device_to_string (device);
1173 /* TRANSLATORS: this is when a device is hotplugged */
1174 g_print ("%s\n%s", _("Device removed:"), tmp);
1175}
1176
1177static void
1178fu_util_device_changed_cb (FwupdClient *client,
1179 FwupdDevice *device,
1180 gpointer user_data)
1181{
1182 g_autofree gchar *tmp = fwupd_device_to_string (device);
1183 /* TRANSLATORS: this is when a device has been updated */
1184 g_print ("%s\n%s", _("Device changed:"), tmp);
1185}
1186
1187static void
1188fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1189{
1190 /* TRANSLATORS: this is when the daemon state changes */
1191 g_print ("%s\n", _("Changed"));
1192}
1193
1194static gboolean
1195fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1196{
1197 g_autoptr(FwupdClient) client = fwupd_client_new ();
1198
1199 /* get all the devices */
1200 if (!fwupd_client_connect (client, priv->cancellable, error))
1201 return FALSE;
1202
1203 /* watch for any hotplugged device */
1204 g_signal_connect (client, "changed",
1205 G_CALLBACK (fu_util_changed_cb), priv);
1206 g_signal_connect (client, "device-added",
1207 G_CALLBACK (fu_util_device_added_cb), priv);
1208 g_signal_connect (client, "device-removed",
1209 G_CALLBACK (fu_util_device_removed_cb), priv);
1210 g_signal_connect (client, "device-changed",
1211 G_CALLBACK (fu_util_device_changed_cb), priv);
1212 g_signal_connect (priv->cancellable, "cancelled",
1213 G_CALLBACK (fu_util_cancelled_cb), priv);
1214 g_main_loop_run (priv->loop);
1215 return TRUE;
1216}
1217
Richard Hughesb5976832018-05-18 10:02:09 +01001218int
1219main (int argc, char *argv[])
1220{
Richard Hughes460226a2018-05-21 20:56:21 +01001221 gboolean allow_older = FALSE;
1222 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001223 gboolean force = FALSE;
1224 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001225 gboolean version = FALSE;
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001226 gboolean interactive = isatty (fileno (stdout)) != 0;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001227 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001228 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1229 g_autoptr(GError) error = NULL;
1230 g_autofree gchar *cmd_descriptions = NULL;
1231 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001232 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1233 /* TRANSLATORS: command line option */
1234 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001235 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1236 /* TRANSLATORS: command line option */
1237 _("Allow re-installing existing firmware versions"), NULL },
1238 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1239 /* TRANSLATORS: command line option */
1240 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001241 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1242 /* TRANSLATORS: command line option */
1243 _("Override plugin warning"), NULL },
Mario Limonciello3f243a92019-01-21 22:05:23 -06001244 { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check,
1245 /* TRANSLATORS: command line option */
1246 _("Do not check for reboot after update"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001247 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1248 /* TRANSLATORS: command line option */
1249 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001250 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1251 /* TRANSLATORS: command line option */
1252 _("Manually whitelist specific plugins"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001253 { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001254 /* TRANSLATORS: command line option */
1255 _("Run the plugin composite prepare routine when using install-blob"), NULL },
Mario Limonciello8402cea2019-02-07 20:25:31 -06001256 { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob,
Mario Limonciello53ce25d2019-02-01 16:09:03 +00001257 /* TRANSLATORS: command line option */
1258 _("Run the plugin composite cleanup routine when using install-blob"), NULL },
1259
Richard Hughesb5976832018-05-18 10:02:09 +01001260 { NULL}
1261 };
1262
1263 setlocale (LC_ALL, "");
1264
1265 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1266 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1267 textdomain (GETTEXT_PACKAGE);
1268
1269 /* ensure root user */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001270 if (interactive && (getuid () != 0 || geteuid () != 0))
Richard Hughesb5976832018-05-18 10:02:09 +01001271 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001272 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001273
1274 /* create helper object */
1275 priv->loop = g_main_loop_new (NULL, FALSE);
1276 priv->progressbar = fu_progressbar_new ();
1277
1278 /* add commands */
1279 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
1280 fu_util_add (priv->cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001281 "build-firmware",
1282 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1283 /* TRANSLATORS: command description */
1284 _("Build firmware using a sandbox"),
1285 fu_util_firmware_builder);
1286 fu_util_add (priv->cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001287 "smbios-dump",
1288 "FILE",
1289 /* TRANSLATORS: command description */
1290 _("Dump SMBIOS data from a file"),
1291 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +01001292 fu_util_add (priv->cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001293 "get-plugins",
1294 NULL,
1295 /* TRANSLATORS: command description */
1296 _("Get all enabled plugins registered with the system"),
1297 fu_util_get_plugins);
1298 fu_util_add (priv->cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001299 "get-details",
1300 NULL,
1301 /* TRANSLATORS: command description */
1302 _("Gets details about a firmware file"),
1303 fu_util_get_details);
1304 fu_util_add (priv->cmd_array,
Mario Limonciello1e35e4c2019-01-28 11:13:02 -06001305 "get-updates",
1306 NULL,
1307 /* TRANSLATORS: command description */
1308 _("Gets the list of updates for connected hardware"),
1309 fu_util_get_updates);
1310 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001311 "get-devices",
1312 NULL,
1313 /* TRANSLATORS: command description */
1314 _("Get all devices that support firmware updates"),
1315 fu_util_get_devices);
1316 fu_util_add (priv->cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001317 "get-topology",
1318 NULL,
1319 /* TRANSLATORS: command description */
1320 _("Get all devices according to the system topology"),
1321 fu_util_get_topology);
1322 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001323 "watch",
1324 NULL,
1325 /* TRANSLATORS: command description */
Piotr Drąg472fa592018-06-06 14:53:48 +02001326 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001327 fu_util_watch);
1328 fu_util_add (priv->cmd_array,
1329 "install-blob",
1330 "FILENAME DEVICE-ID",
1331 /* TRANSLATORS: command description */
1332 _("Install a firmware blob on a device"),
1333 fu_util_install_blob);
1334 fu_util_add (priv->cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001335 "install",
1336 "FILE [ID]",
1337 /* TRANSLATORS: command description */
1338 _("Install a firmware file on this hardware"),
1339 fu_util_install);
1340 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001341 "attach",
1342 "DEVICE-ID",
1343 /* TRANSLATORS: command description */
1344 _("Attach to firmware mode"),
1345 fu_util_attach);
1346 fu_util_add (priv->cmd_array,
1347 "detach",
1348 "DEVICE-ID",
1349 /* TRANSLATORS: command description */
1350 _("Detach to bootloader mode"),
1351 fu_util_detach);
Richard Hughes1d894f12018-08-31 13:05:51 +01001352 fu_util_add (priv->cmd_array,
1353 "hwids",
1354 "[FILE]",
1355 /* TRANSLATORS: command description */
1356 _("Return all the hardware IDs for the machine"),
1357 fu_util_hwids);
Mario Limonciello62f84862018-10-18 13:15:23 -05001358 fu_util_add (priv->cmd_array,
1359 "monitor",
1360 NULL,
1361 /* TRANSLATORS: command description */
1362 _("Monitor the daemon for events"),
1363 fu_util_monitor);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001364 fu_util_add (priv->cmd_array,
1365 "update",
1366 NULL,
1367 /* TRANSLATORS: command description */
1368 _("Update all devices that match local metadata"),
1369 fu_util_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001370
1371 /* do stuff on ctrl+c */
1372 priv->cancellable = g_cancellable_new ();
1373 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1374 SIGINT, fu_util_sigint_cb,
1375 priv, NULL);
1376 g_signal_connect (priv->cancellable, "cancelled",
1377 G_CALLBACK (fu_util_cancelled_cb), priv);
1378
1379 /* sort by command name */
1380 g_ptr_array_sort (priv->cmd_array,
1381 (GCompareFunc) fu_sort_command_name_cb);
1382
Mario Limonciello3f243a92019-01-21 22:05:23 -06001383 /* non-TTY consoles cannot answer questions */
Mario Limonciello5d7aa402019-02-04 09:35:58 -06001384 if (!interactive) {
Mario Limonciello3f243a92019-01-21 22:05:23 -06001385 priv->no_reboot_check = TRUE;
Mario Limonciello9b31e6f2019-01-31 15:42:19 -06001386 fu_progressbar_set_interactive (priv->progressbar, FALSE);
1387 }
Mario Limonciello3f243a92019-01-21 22:05:23 -06001388
Richard Hughesb5976832018-05-18 10:02:09 +01001389 /* get a list of the commands */
1390 priv->context = g_option_context_new (NULL);
1391 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
1392 g_option_context_set_summary (priv->context, cmd_descriptions);
1393 g_option_context_set_description (priv->context,
1394 "This tool allows an administrator to use the fwupd plugins "
1395 "without being installed on the host system.");
1396
1397 /* TRANSLATORS: program name */
1398 g_set_application_name (_("Firmware Utility"));
1399 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001400 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001401 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1402 if (!ret) {
1403 /* TRANSLATORS: the user didn't read the man page */
1404 g_print ("%s: %s\n", _("Failed to parse arguments"),
1405 error->message);
1406 return EXIT_FAILURE;
1407 }
1408
Richard Hughes460226a2018-05-21 20:56:21 +01001409 /* set flags */
1410 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1411 if (allow_reinstall)
1412 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1413 if (allow_older)
1414 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1415 if (force)
1416 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1417
Richard Hughes98ca9932018-05-18 10:24:07 +01001418 /* load engine */
1419 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1420 g_signal_connect (priv->engine, "device-added",
1421 G_CALLBACK (fu_main_engine_device_added_cb),
1422 priv);
1423 g_signal_connect (priv->engine, "device-removed",
1424 G_CALLBACK (fu_main_engine_device_removed_cb),
1425 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001426 g_signal_connect (priv->engine, "status-changed",
1427 G_CALLBACK (fu_main_engine_status_changed_cb),
1428 priv);
1429 g_signal_connect (priv->engine, "percentage-changed",
1430 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1431 priv);
1432
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001433 /* just show versions and exit */
1434 if (version) {
1435 g_autofree gchar *version_str = fu_util_get_versions ();
1436 g_print ("%s\n", version_str);
1437 return EXIT_SUCCESS;
1438 }
1439
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001440 /* any plugin whitelist specified */
1441 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1442 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1443
Richard Hughesb5976832018-05-18 10:02:09 +01001444 /* run the specified command */
1445 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
1446 if (!ret) {
1447 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1448 g_autofree gchar *tmp = NULL;
1449 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1450 g_print ("%s\n\n%s", error->message, tmp);
1451 return EXIT_FAILURE;
1452 }
1453 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1454 g_print ("%s\n", error->message);
1455 return EXIT_NOTHING_TO_DO;
1456 }
1457 g_print ("%s\n", error->message);
1458 return EXIT_FAILURE;
1459 }
1460
1461 /* success */
1462 return EXIT_SUCCESS;
1463}