blob: c40e992dba734c45402933557354c89e0dc77087 [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>
12#include <glib/gi18n.h>
13#include <glib-unix.h>
14#include <locale.h>
15#include <stdlib.h>
16#include <unistd.h>
Richard Hughes3d178be2018-08-30 11:14:24 +010017#include <libsoup/soup.h>
Richard Hughesb5976832018-05-18 10:02:09 +010018
Richard Hughes98ca9932018-05-18 10:24:07 +010019#include "fu-engine.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010020#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010021#include "fu-progressbar.h"
22#include "fu-smbios.h"
23#include "fu-util-common.h"
Mario Limonciellofde47732018-09-11 12:20:58 -050024#include "fu-debug.h"
Richard Hughesb5976832018-05-18 10:02:09 +010025
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050026#define SYSTEMD_SERVICE "org.freedesktop.systemd1"
27#define SYSTEMD_OBJECT_PATH "/org/freedesktop/systemd1"
28#define SYSTEMD_MANAGER_INTERFACE "org.freedesktop.systemd1.Manager"
29#define SYSTEMD_FWUPD_UNIT "fwupd.service"
30
Richard Hughesb5976832018-05-18 10:02:09 +010031/* this is only valid in this file */
32#define FWUPD_ERROR_INVALID_ARGS (FWUPD_ERROR_LAST+1)
33
34/* custom return code */
35#define EXIT_NOTHING_TO_DO 2
36
37typedef struct {
38 GCancellable *cancellable;
39 GMainLoop *loop;
40 GOptionContext *context;
41 GPtrArray *cmd_array;
Richard Hughes98ca9932018-05-18 10:24:07 +010042 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010043 FuProgressbar *progressbar;
Richard Hughes460226a2018-05-21 20:56:21 +010044 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050045 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050046 /* only valid in update and downgrade */
47 FwupdDevice *current_device;
Richard Hughesb5976832018-05-18 10:02:09 +010048} FuUtilPrivate;
49
50typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
51 gchar **values,
52 GError **error);
53
54typedef struct {
55 gchar *name;
56 gchar *arguments;
57 gchar *description;
58 FuUtilPrivateCb callback;
59} FuUtilItem;
60
61static void
62fu_util_item_free (FuUtilItem *item)
63{
64 g_free (item->name);
65 g_free (item->arguments);
66 g_free (item->description);
67 g_free (item);
68}
69
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050070static gboolean
71fu_util_start_engine (FuUtilPrivate *priv, GError **error)
72{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050073 g_autoptr(GDBusConnection) connection = NULL;
74 g_autoptr(GDBusProxy) proxy = NULL;
75 g_autoptr(GVariant) val = NULL;
76 g_autoptr(GError) error_local = NULL;
77
78 /* try to stop any already running daemon */
79 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
80 if (connection == NULL)
81 return FALSE;
82 proxy = g_dbus_proxy_new_sync (connection,
83 G_DBUS_PROXY_FLAGS_NONE,
84 NULL,
85 SYSTEMD_SERVICE,
86 SYSTEMD_OBJECT_PATH,
87 SYSTEMD_MANAGER_INTERFACE,
88 NULL,
Mario Limonciello67b82af2018-11-01 13:37:00 -050089 &error_local);
90 if (proxy == NULL) {
91 g_debug ("Failed to find %s: %s",
92 SYSTEMD_SERVICE,
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050093 error_local->message);
94 } else {
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050095 val = g_dbus_proxy_call_sync (proxy,
Mario Limonciello67b82af2018-11-01 13:37:00 -050096 "GetUnit",
97 g_variant_new ("(s)",
98 SYSTEMD_FWUPD_UNIT),
Mario Limoncielloe61c94d2018-10-11 10:49:55 -050099 G_DBUS_CALL_FLAGS_NONE,
100 -1,
101 NULL,
Mario Limonciello67b82af2018-11-01 13:37:00 -0500102 &error_local);
103 if (val == NULL) {
104 g_debug ("Unable to find %s: %s",
105 SYSTEMD_FWUPD_UNIT,
106 error_local->message);
107 } else {
108 g_variant_unref (val);
109 val = g_dbus_proxy_call_sync (proxy,
110 "StopUnit",
111 g_variant_new ("(ss)",
112 SYSTEMD_FWUPD_UNIT,
113 "replace"),
114 G_DBUS_CALL_FLAGS_NONE,
115 -1,
116 NULL,
117 error);
118 if (val == NULL)
119 return FALSE;
120 }
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500121 }
122
Richard Hughesf425d292019-01-18 17:57:39 +0000123 if (!fu_engine_load (priv->engine, error))
124 return FALSE;
125 if (fu_engine_get_tainted (priv->engine)) {
126 g_printerr ("WARNING: This tool has loaded 3rd party code and "
127 "is no longer supported by the upstream developers!\n");
128 }
129 return TRUE;
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500130}
131
Richard Hughesb5976832018-05-18 10:02:09 +0100132static gint
133fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
134{
135 return g_strcmp0 ((*item1)->name, (*item2)->name);
136}
137
138static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500139fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
140{
141 g_autofree gchar *path = g_path_get_dirname (value);
142 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
143 g_prefix_error (error,
144 "Unable to access %s. You may need to copy %s to %s: ",
145 path, value, g_getenv ("HOME"));
146 }
147}
148
149static void
Richard Hughesb5976832018-05-18 10:02:09 +0100150fu_util_add (GPtrArray *array,
151 const gchar *name,
152 const gchar *arguments,
153 const gchar *description,
154 FuUtilPrivateCb callback)
155{
156 g_auto(GStrv) names = NULL;
157
158 g_return_if_fail (name != NULL);
159 g_return_if_fail (description != NULL);
160 g_return_if_fail (callback != NULL);
161
162 /* add each one */
163 names = g_strsplit (name, ",", -1);
164 for (guint i = 0; names[i] != NULL; i++) {
165 FuUtilItem *item = g_new0 (FuUtilItem, 1);
166 item->name = g_strdup (names[i]);
167 if (i == 0) {
168 item->description = g_strdup (description);
169 } else {
170 /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */
171 item->description = g_strdup_printf (_("Alias to %s"),
172 names[0]);
173 }
174 item->arguments = g_strdup (arguments);
175 item->callback = callback;
176 g_ptr_array_add (array, item);
177 }
178}
179
180static gchar *
181fu_util_get_descriptions (GPtrArray *array)
182{
183 gsize len;
184 const gsize max_len = 35;
185 GString *string;
186
187 /* print each command */
188 string = g_string_new ("");
189 for (guint i = 0; i < array->len; i++) {
190 FuUtilItem *item = g_ptr_array_index (array, i);
191 g_string_append (string, " ");
192 g_string_append (string, item->name);
193 len = strlen (item->name) + 2;
194 if (item->arguments != NULL) {
195 g_string_append (string, " ");
196 g_string_append (string, item->arguments);
197 len += strlen (item->arguments) + 1;
198 }
199 if (len < max_len) {
200 for (gsize j = len; j < max_len + 1; j++)
201 g_string_append_c (string, ' ');
202 g_string_append (string, item->description);
203 g_string_append_c (string, '\n');
204 } else {
205 g_string_append_c (string, '\n');
206 for (gsize j = 0; j < max_len + 1; j++)
207 g_string_append_c (string, ' ');
208 g_string_append (string, item->description);
209 g_string_append_c (string, '\n');
210 }
211 }
212
213 /* remove trailing newline */
214 if (string->len > 0)
215 g_string_set_size (string, string->len - 1);
216
217 return g_string_free (string, FALSE);
218}
219
220static gboolean
221fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error)
222{
223 /* find command */
224 for (guint i = 0; i < priv->cmd_array->len; i++) {
225 FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i);
226 if (g_strcmp0 (item->name, command) == 0)
227 return item->callback (priv, values, error);
228 }
229
230 /* not found */
231 g_set_error_literal (error,
232 FWUPD_ERROR,
233 FWUPD_ERROR_INVALID_ARGS,
234 /* TRANSLATORS: error message */
235 _("Command not found"));
236 return FALSE;
237}
238
239static void
240fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
241{
242 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
243 /* TRANSLATORS: this is when a device ctrl+c's a watch */
244 g_print ("%s\n", _("Cancelled"));
245 g_main_loop_quit (priv->loop);
246}
247
248static gboolean
249fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
250{
251 g_autofree gchar *tmp = NULL;
252 g_autoptr(FuSmbios) smbios = NULL;
253 if (g_strv_length (values) < 1) {
254 g_set_error_literal (error,
255 FWUPD_ERROR,
256 FWUPD_ERROR_INVALID_ARGS,
257 "Invalid arguments");
258 return FALSE;
259 }
260 smbios = fu_smbios_new ();
261 if (!fu_smbios_setup_from_file (smbios, values[0], error))
262 return FALSE;
263 tmp = fu_smbios_to_string (smbios);
264 g_print ("%s\n", tmp);
265 return TRUE;
266}
267
Richard Hughesb5976832018-05-18 10:02:09 +0100268static gboolean
269fu_util_sigint_cb (gpointer user_data)
270{
271 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
272 g_debug ("Handling SIGINT");
273 g_cancellable_cancel (priv->cancellable);
274 return FALSE;
275}
276
277static void
278fu_util_private_free (FuUtilPrivate *priv)
279{
280 if (priv->cmd_array != NULL)
281 g_ptr_array_unref (priv->cmd_array);
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500282 if (priv->current_device != NULL)
283 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100284 if (priv->engine != NULL)
285 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100286 if (priv->loop != NULL)
287 g_main_loop_unref (priv->loop);
288 if (priv->cancellable != NULL)
289 g_object_unref (priv->cancellable);
290 if (priv->progressbar != NULL)
291 g_object_unref (priv->progressbar);
292 if (priv->context != NULL)
293 g_option_context_free (priv->context);
294 g_free (priv);
295}
296
297#pragma clang diagnostic push
298#pragma clang diagnostic ignored "-Wunused-function"
299G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
300#pragma clang diagnostic pop
301
Richard Hughes98ca9932018-05-18 10:24:07 +0100302
303static void
304fu_main_engine_device_added_cb (FuEngine *engine,
305 FuDevice *device,
306 FuUtilPrivate *priv)
307{
308 g_autofree gchar *tmp = fu_device_to_string (device);
309 g_debug ("ADDED:\n%s", tmp);
310}
311
312static void
313fu_main_engine_device_removed_cb (FuEngine *engine,
314 FuDevice *device,
315 FuUtilPrivate *priv)
316{
317 g_autofree gchar *tmp = fu_device_to_string (device);
318 g_debug ("REMOVED:\n%s", tmp);
319}
320
321static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100322fu_main_engine_status_changed_cb (FuEngine *engine,
323 FwupdStatus status,
324 FuUtilPrivate *priv)
325{
326 fu_progressbar_update (priv->progressbar, status, 0);
327}
328
329static void
330fu_main_engine_percentage_changed_cb (FuEngine *engine,
331 guint percentage,
332 FuUtilPrivate *priv)
333{
334 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
335}
336
337static gboolean
338fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
339{
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500340 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100341 return FALSE;
342 g_main_loop_run (priv->loop);
343 return TRUE;
344}
345
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100346static gint
347fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
348{
349 return fu_plugin_name_compare (*item1, *item2);
350}
351
352static gboolean
353fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
354{
355 GPtrArray *plugins;
356 guint cnt = 0;
357
358 /* load engine */
359 if (!fu_engine_load_plugins (priv->engine, error))
360 return FALSE;
361
362 /* print */
363 plugins = fu_engine_get_plugins (priv->engine);
364 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
365 for (guint i = 0; i < plugins->len; i++) {
366 FuPlugin *plugin = g_ptr_array_index (plugins, i);
367 if (!fu_plugin_get_enabled (plugin))
368 continue;
369 g_print ("%s\n", fu_plugin_get_name (plugin));
370 cnt++;
371 }
372 if (cnt == 0) {
373 /* TRANSLATORS: nothing found */
374 g_print ("%s\n", _("No plugins found"));
375 return TRUE;
376 }
377
378 return TRUE;
379}
380
Richard Hughes98ca9932018-05-18 10:24:07 +0100381static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500382fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
383{
384 g_autoptr(GPtrArray) array = NULL;
385 gint fd;
386
387 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500388 if (!fu_util_start_engine (priv, error))
Mario Limonciello716ab272018-05-29 12:34:37 -0500389 return FALSE;
390
391 /* check args */
392 if (g_strv_length (values) != 1) {
393 g_set_error_literal (error,
394 FWUPD_ERROR,
395 FWUPD_ERROR_INVALID_ARGS,
396 "Invalid arguments");
397 return FALSE;
398 }
399
400 /* open file */
401 fd = open (values[0], O_RDONLY);
402 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500403 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500404 g_set_error (error,
405 FWUPD_ERROR,
406 FWUPD_ERROR_INVALID_FILE,
407 "failed to open %s",
408 values[0]);
409 return FALSE;
410 }
411 array = fu_engine_get_details (priv->engine, fd, error);
412 close (fd);
413
414 if (array == NULL)
415 return FALSE;
416 for (guint i = 0; i < array->len; i++) {
417 FwupdDevice *dev = g_ptr_array_index (array, i);
418 g_autofree gchar *tmp = NULL;
419 tmp = fwupd_device_to_string (dev);
Mario Limoncielloece90a42018-07-25 17:35:55 -0500420 g_print ("%s\n", tmp);
Mario Limonciello716ab272018-05-29 12:34:37 -0500421 }
422 return TRUE;
423}
424
425static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100426fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
427{
428 g_autoptr(GPtrArray) devs = NULL;
429
430 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500431 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100432 return FALSE;
433
434 /* print */
435 devs = fu_engine_get_devices (priv->engine, error);
436 if (devs == NULL)
437 return FALSE;
438 if (devs->len == 0) {
439 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500440 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100441 return TRUE;
442 }
443 for (guint i = 0; i < devs->len; i++) {
444 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500445 if (priv->show_all_devices || fu_util_is_interesting_device (dev)) {
446 g_autofree gchar *tmp = fwupd_device_to_string (dev);
447 g_print ("%s\n", tmp);
448 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100449 }
450
451 return TRUE;
452}
453
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500454static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100455fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500456{
457 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100458 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500459 if (!priv->show_all_devices &&
460 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500461 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100462 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500463 GNode *child = g_node_append_data (root, dev_tmp);
464 fu_util_build_device_tree (priv, child, devs, dev_tmp);
465 }
466 }
467}
468
469static gboolean
470fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
471{
472 g_autoptr(GNode) root = g_node_new (NULL);
473 g_autoptr(GPtrArray) devs = NULL;
474
475 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500476 if (!fu_util_start_engine (priv, error))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500477 return FALSE;
478
479 /* print */
480 devs = fu_engine_get_devices (priv->engine, error);
481 if (devs == NULL)
482 return FALSE;
483
484 /* print */
485 if (devs->len == 0) {
486 /* TRANSLATORS: nothing attached that can be upgraded */
487 g_print ("%s\n", _("No hardware detected with firmware update capability"));
488 return TRUE;
489 }
490 fu_util_build_device_tree (priv, root, devs, NULL);
491 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
492 fu_util_print_device_tree, priv);
493
494 return TRUE;
495}
496
497
Richard Hughes98ca9932018-05-18 10:24:07 +0100498static FuDevice *
499fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
500{
501 FuDevice *dev;
502 guint idx;
503 g_autoptr(GPtrArray) devices = NULL;
504
505 /* get devices from daemon */
506 devices = fu_engine_get_devices (priv->engine, error);
507 if (devices == NULL)
508 return NULL;
509
510 /* exactly one */
511 if (devices->len == 1) {
512 dev = g_ptr_array_index (devices, 0);
513 return g_object_ref (dev);
514 }
515
516 /* TRANSLATORS: get interactive prompt */
517 g_print ("%s\n", _("Choose a device:"));
518 /* TRANSLATORS: this is to abort the interactive prompt */
519 g_print ("0.\t%s\n", _("Cancel"));
520 for (guint i = 0; i < devices->len; i++) {
521 dev = g_ptr_array_index (devices, i);
522 g_print ("%u.\t%s (%s)\n",
523 i + 1,
524 fu_device_get_id (dev),
525 fu_device_get_name (dev));
526 }
527 idx = fu_util_prompt_for_number (devices->len);
528 if (idx == 0) {
529 g_set_error_literal (error,
530 FWUPD_ERROR,
531 FWUPD_ERROR_NOTHING_TO_DO,
532 "Request canceled");
533 return NULL;
534 }
535 dev = g_ptr_array_index (devices, idx - 1);
536 return g_object_ref (dev);
537}
538
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500539static void
540fu_util_install_device_changed_cb (FwupdClient *client,
541 FwupdDevice *device,
542 FuUtilPrivate *priv)
543{
544 g_autofree gchar *str = NULL;
545
546 /* same as last time, so ignore */
547 if (priv->current_device != NULL &&
548 fwupd_device_compare (priv->current_device, device) == 0)
549 return;
550
551 /* show message in progressbar */
552 /* TRANSLATORS: %1 is a device name */
553 str = g_strdup_printf (_("Installing %s"),
554 fwupd_device_get_name (device));
555 fu_progressbar_set_title (priv->progressbar, str);
556 g_set_object (&priv->current_device, device);
557}
558
Richard Hughes98ca9932018-05-18 10:24:07 +0100559static gboolean
560fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
561{
562 g_autoptr(FuDevice) device = NULL;
563 g_autoptr(GBytes) blob_fw = NULL;
564
565 /* invalid args */
566 if (g_strv_length (values) == 0) {
567 g_set_error_literal (error,
568 FWUPD_ERROR,
569 FWUPD_ERROR_INVALID_ARGS,
570 "Invalid arguments");
571 return FALSE;
572 }
573
574 /* parse blob */
575 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500576 if (blob_fw == NULL) {
577 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100578 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500579 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100580
581 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500582 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100583 return FALSE;
584
585 /* get device */
586 if (g_strv_length (values) >= 2) {
587 device = fu_engine_get_device (priv->engine, values[1], error);
588 if (device == NULL)
589 return FALSE;
590 } else {
591 device = fu_util_prompt_for_device (priv, error);
592 if (device == NULL)
593 return FALSE;
594 }
595
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500596 g_signal_connect (priv->engine, "device-changed",
597 G_CALLBACK (fu_util_install_device_changed_cb), priv);
598
Richard Hughes98ca9932018-05-18 10:24:07 +0100599 /* write bare firmware */
600 return fu_engine_install_blob (priv->engine, device,
601 NULL, /* blob_cab */
602 blob_fw,
603 NULL, /* version */
Richard Hughes460226a2018-05-21 20:56:21 +0100604 priv->flags,
Richard Hughes98ca9932018-05-18 10:24:07 +0100605 error);
606}
607
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100608static gint
609fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
610{
611 FuInstallTask *task1 = *((FuInstallTask **) a);
612 FuInstallTask *task2 = *((FuInstallTask **) b);
613 return fu_install_task_compare (task1, task2);
614}
615
616static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100617fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
618{
619 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
620 { "curl", uri, "--output", fn, NULL },
621 { NULL } };
622 for (guint i = 0; argv[i][0] != NULL; i++) {
623 g_autoptr(GError) error_local = NULL;
624 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
625 g_debug ("%s", error_local->message);
626 continue;
627 }
628 return fu_common_spawn_sync (argv[i], NULL, NULL, NULL, error);
629 }
630 g_set_error_literal (error,
631 FWUPD_ERROR,
632 FWUPD_ERROR_NOT_FOUND,
633 "no supported out-of-process downloaders found");
634 return FALSE;
635}
636
637static gchar *
638fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
639{
640 g_autofree gchar *filename = NULL;
641 g_autoptr(SoupURI) uri = NULL;
642
643 /* a local file */
644 uri = soup_uri_new (perhapsfn);
645 if (uri == NULL)
646 return g_strdup (perhapsfn);
647
648 /* download the firmware to a cachedir */
649 filename = fu_util_get_user_cache_path (perhapsfn);
650 if (!fu_common_mkdir_parent (filename, error))
651 return NULL;
652 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
653 return NULL;
654 return g_steal_pointer (&filename);
655}
656
657static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100658fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
659{
Richard Hughes3d178be2018-08-30 11:14:24 +0100660 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100661 g_autoptr(GBytes) blob_cab = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100662 g_autoptr(GPtrArray) components = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100663 g_autoptr(GPtrArray) devices_possible = NULL;
664 g_autoptr(GPtrArray) errors = NULL;
665 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100666 g_autoptr(XbSilo) silo = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100667
Mario Limonciello8949e892018-05-25 08:03:06 -0500668 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500669 if (!fu_util_start_engine (priv, error))
Mario Limonciello8949e892018-05-25 08:03:06 -0500670 return FALSE;
671
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100672 /* handle both forms */
673 if (g_strv_length (values) == 1) {
674 devices_possible = fu_engine_get_devices (priv->engine, error);
675 if (devices_possible == NULL)
676 return FALSE;
677 } else if (g_strv_length (values) == 2) {
678 FuDevice *device = fu_engine_get_device (priv->engine,
679 values[1],
680 error);
681 if (device == NULL)
682 return FALSE;
683 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
684 g_ptr_array_add (devices_possible, device);
685 } else {
686 g_set_error_literal (error,
687 FWUPD_ERROR,
688 FWUPD_ERROR_INVALID_ARGS,
689 "Invalid arguments");
690 return FALSE;
691 }
692
Richard Hughes3d178be2018-08-30 11:14:24 +0100693 /* download if required */
694 filename = fu_util_download_if_required (priv, values[0], error);
695 if (filename == NULL)
696 return FALSE;
697
Richard Hughes481aa2a2018-09-18 20:51:46 +0100698 /* parse silo */
Richard Hughes3d178be2018-08-30 11:14:24 +0100699 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500700 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100701 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100702 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500703 }
Richard Hughes481aa2a2018-09-18 20:51:46 +0100704 silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error);
705 if (silo == NULL)
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100706 return FALSE;
Richard Hughes481aa2a2018-09-18 20:51:46 +0100707 components = xb_silo_query (silo, "component", 0, error);
708 if (components == NULL)
709 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100710
Richard Hughes481aa2a2018-09-18 20:51:46 +0100711 /* for each component in the silo */
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100712 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
713 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
Richard Hughes481aa2a2018-09-18 20:51:46 +0100714 for (guint i = 0; i < components->len; i++) {
715 XbNode *component = g_ptr_array_index (components, i);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100716
717 /* do any devices pass the requirements */
718 for (guint j = 0; j < devices_possible->len; j++) {
719 FuDevice *device = g_ptr_array_index (devices_possible, j);
720 g_autoptr(FuInstallTask) task = NULL;
721 g_autoptr(GError) error_local = NULL;
722
723 /* is this component valid for the device */
Richard Hughes481aa2a2018-09-18 20:51:46 +0100724 task = fu_install_task_new (device, component);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100725 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100726 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100727 &error_local)) {
728 g_debug ("requirement on %s:%s failed: %s",
729 fu_device_get_id (device),
Richard Hughes481aa2a2018-09-18 20:51:46 +0100730 xb_node_query_text (component, "id", NULL),
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100731 error_local->message);
732 g_ptr_array_add (errors, g_steal_pointer (&error_local));
733 continue;
734 }
735
736 /* success */
737 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
738 }
739 }
740
741 /* order the install tasks by the device priority */
742 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
743
744 /* nothing suitable */
745 if (install_tasks->len == 0) {
746 GError *error_tmp = fu_common_error_array_get_best (errors);
747 g_propagate_error (error, error_tmp);
748 return FALSE;
749 }
750
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500751 g_signal_connect (priv->engine, "device-changed",
752 G_CALLBACK (fu_util_install_device_changed_cb), priv);
753
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100754 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100755 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
756 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100757
758 /* success */
759 return TRUE;
760}
761
Richard Hughes98ca9932018-05-18 10:24:07 +0100762static gboolean
Mario Limonciello46aaee82019-01-10 12:58:00 -0600763fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error)
764{
765 g_autoptr(GPtrArray) devices = NULL;
766
767 /* load engine */
768 if (!fu_util_start_engine (priv, error))
769 return FALSE;
770
771 devices = fu_engine_get_devices (priv->engine, error);
772 for (guint i = 0; i < devices->len; i++) {
773 FwupdDevice *dev = g_ptr_array_index (devices, i);
774 FwupdRelease *rel;
775 const gchar *remote_id;
776 const gchar *device_id;
777 const gchar *uri_tmp;
778 g_autoptr(GPtrArray) rels = NULL;
779 g_autoptr(GError) error_local = NULL;
780
781 if (!fu_util_is_interesting_device (dev))
782 continue;
783 /* only show stuff that has metadata available */
784 if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
785 continue;
786
787 device_id = fu_device_get_id (dev);
788 rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local);
789 if (rels == NULL) {
790 g_printerr ("%s\n", error_local->message);
791 continue;
792 }
793
794 rel = g_ptr_array_index (rels, 0);
795 uri_tmp = fwupd_release_get_uri (rel);
796 remote_id = fwupd_release_get_remote_id (rel);
797 if (remote_id != NULL) {
798 FwupdRemote *remote;
799 g_auto(GStrv) argv = NULL;
800
801 remote = fu_engine_get_remote_by_id (priv->engine,
802 remote_id,
803 &error_local);
804 if (remote == NULL) {
805 g_printerr ("%s\n", error_local->message);
806 continue;
807 }
808
809 argv = g_new0 (gchar *, 2);
810 /* local remotes have the firmware already */
811 if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) {
812 const gchar *fn_cache = fwupd_remote_get_filename_cache (remote);
813 g_autofree gchar *path = g_path_get_dirname (fn_cache);
814 argv[0] = g_build_filename (path, uri_tmp, NULL);
815 /* web remote, fu_util_install will download file */
816 } else {
817 argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error);
818 }
819 if (!fu_util_install (priv, argv, &error_local)) {
820 g_printerr ("%s\n", error_local->message);
821 continue;
822 }
823 }
824 }
825 return TRUE;
826}
827
828static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100829fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
830{
831 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100832 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100833
834 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500835 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100836 return FALSE;
837
838 /* invalid args */
839 if (g_strv_length (values) == 0) {
840 g_set_error_literal (error,
841 FWUPD_ERROR,
842 FWUPD_ERROR_INVALID_ARGS,
843 "Invalid arguments");
844 return FALSE;
845 }
846
847 /* get device */
848 if (g_strv_length (values) >= 1) {
849 device = fu_engine_get_device (priv->engine, values[0], error);
850 if (device == NULL)
851 return FALSE;
852 } else {
853 device = fu_util_prompt_for_device (priv, error);
854 if (device == NULL)
855 return FALSE;
856 }
857
858 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100859 locker = fu_device_locker_new (device, error);
860 if (locker == NULL)
861 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100862 return fu_device_detach (device, error);
863}
864
865static gboolean
866fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
867{
868 g_autoptr(FuDevice) device = NULL;
Richard Hughes2a679cd2018-09-04 21:13:23 +0100869 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100870
871 /* load engine */
Mario Limoncielloe61c94d2018-10-11 10:49:55 -0500872 if (!fu_util_start_engine (priv, error))
Richard Hughes98ca9932018-05-18 10:24:07 +0100873 return FALSE;
874
875 /* invalid args */
876 if (g_strv_length (values) == 0) {
877 g_set_error_literal (error,
878 FWUPD_ERROR,
879 FWUPD_ERROR_INVALID_ARGS,
880 "Invalid arguments");
881 return FALSE;
882 }
883
884 /* get device */
885 if (g_strv_length (values) >= 1) {
886 device = fu_engine_get_device (priv->engine, values[0], error);
887 if (device == NULL)
888 return FALSE;
889 } else {
890 device = fu_util_prompt_for_device (priv, error);
891 if (device == NULL)
892 return FALSE;
893 }
894
895 /* run vfunc */
Richard Hughes2a679cd2018-09-04 21:13:23 +0100896 locker = fu_device_locker_new (device, error);
897 if (locker == NULL)
898 return FALSE;
Richard Hughes98ca9932018-05-18 10:24:07 +0100899 return fu_device_attach (device, error);
900}
901
Richard Hughes1d894f12018-08-31 13:05:51 +0100902static gboolean
903fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
904{
905 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
906 g_autoptr(FuHwids) hwids = fu_hwids_new ();
907 const gchar *hwid_keys[] = {
908 FU_HWIDS_KEY_BIOS_VENDOR,
909 FU_HWIDS_KEY_BIOS_VERSION,
910 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
911 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
912 FU_HWIDS_KEY_MANUFACTURER,
913 FU_HWIDS_KEY_FAMILY,
914 FU_HWIDS_KEY_PRODUCT_NAME,
915 FU_HWIDS_KEY_PRODUCT_SKU,
916 FU_HWIDS_KEY_ENCLOSURE_KIND,
917 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
918 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
919 NULL };
920
921 /* read DMI data */
922 if (g_strv_length (values) == 0) {
923 if (!fu_smbios_setup (smbios, error))
924 return FALSE;
925 } else if (g_strv_length (values) == 1) {
926 if (!fu_smbios_setup_from_file (smbios, values[0], error))
927 return FALSE;
928 } else {
929 g_set_error_literal (error,
930 FWUPD_ERROR,
931 FWUPD_ERROR_INVALID_ARGS,
932 "Invalid arguments");
933 return FALSE;
934 }
935 if (!fu_hwids_setup (hwids, smbios, error))
936 return FALSE;
937
938 /* show debug output */
939 g_print ("Computer Information\n");
940 g_print ("--------------------\n");
941 for (guint i = 0; hwid_keys[i] != NULL; i++) {
942 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
943 if (tmp == NULL)
944 continue;
945 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
946 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
947 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
948 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
949 } else {
950 g_print ("%s: %s\n", hwid_keys[i], tmp);
951 }
952 }
953
954 /* show GUIDs */
955 g_print ("\nHardware IDs\n");
956 g_print ("------------\n");
957 for (guint i = 0; i < 15; i++) {
958 const gchar *keys = NULL;
959 g_autofree gchar *guid = NULL;
960 g_autofree gchar *key = NULL;
961 g_autofree gchar *keys_str = NULL;
962 g_auto(GStrv) keysv = NULL;
963 g_autoptr(GError) error_local = NULL;
964
965 /* get the GUID */
966 key = g_strdup_printf ("HardwareID-%u", i);
967 keys = fu_hwids_get_replace_keys (hwids, key);
968 guid = fu_hwids_get_guid (hwids, key, &error_local);
969 if (guid == NULL) {
970 g_print ("%s\n", error_local->message);
971 continue;
972 }
973
974 /* show what makes up the GUID */
975 keysv = g_strsplit (keys, "&", -1);
976 keys_str = g_strjoinv (" + ", keysv);
977 g_print ("{%s} <- %s\n", guid, keys_str);
978 }
979
980 return TRUE;
981}
982
Mario Limonciellof6d01b12018-10-18 12:57:10 -0500983static gboolean
984fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error)
985{
986 const gchar *script_fn = "startup.sh";
987 const gchar *output_fn = "firmware.bin";
988 g_autoptr(GBytes) archive_blob = NULL;
989 g_autoptr(GBytes) firmware_blob = NULL;
990 if (g_strv_length (values) < 2) {
991 g_set_error_literal (error,
992 FWUPD_ERROR,
993 FWUPD_ERROR_INVALID_ARGS,
994 "Invalid arguments");
995 return FALSE;
996 }
997 archive_blob = fu_common_get_contents_bytes (values[0], error);
998 if (archive_blob == NULL)
999 return FALSE;
1000 if (g_strv_length (values) > 2)
1001 script_fn = values[2];
1002 if (g_strv_length (values) > 3)
1003 output_fn = values[3];
1004 firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error);
1005 if (firmware_blob == NULL)
1006 return FALSE;
1007 return fu_common_set_contents_bytes (values[1], firmware_blob, error);
1008}
1009
Mario Limonciello62f84862018-10-18 13:15:23 -05001010static void
1011fu_util_device_added_cb (FwupdClient *client,
1012 FwupdDevice *device,
1013 gpointer user_data)
1014{
1015 g_autofree gchar *tmp = fwupd_device_to_string (device);
1016 /* TRANSLATORS: this is when a device is hotplugged */
1017 g_print ("%s\n%s", _("Device added:"), tmp);
1018}
1019
1020static void
1021fu_util_device_removed_cb (FwupdClient *client,
1022 FwupdDevice *device,
1023 gpointer user_data)
1024{
1025 g_autofree gchar *tmp = fwupd_device_to_string (device);
1026 /* TRANSLATORS: this is when a device is hotplugged */
1027 g_print ("%s\n%s", _("Device removed:"), tmp);
1028}
1029
1030static void
1031fu_util_device_changed_cb (FwupdClient *client,
1032 FwupdDevice *device,
1033 gpointer user_data)
1034{
1035 g_autofree gchar *tmp = fwupd_device_to_string (device);
1036 /* TRANSLATORS: this is when a device has been updated */
1037 g_print ("%s\n%s", _("Device changed:"), tmp);
1038}
1039
1040static void
1041fu_util_changed_cb (FwupdClient *client, gpointer user_data)
1042{
1043 /* TRANSLATORS: this is when the daemon state changes */
1044 g_print ("%s\n", _("Changed"));
1045}
1046
1047static gboolean
1048fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error)
1049{
1050 g_autoptr(FwupdClient) client = fwupd_client_new ();
1051
1052 /* get all the devices */
1053 if (!fwupd_client_connect (client, priv->cancellable, error))
1054 return FALSE;
1055
1056 /* watch for any hotplugged device */
1057 g_signal_connect (client, "changed",
1058 G_CALLBACK (fu_util_changed_cb), priv);
1059 g_signal_connect (client, "device-added",
1060 G_CALLBACK (fu_util_device_added_cb), priv);
1061 g_signal_connect (client, "device-removed",
1062 G_CALLBACK (fu_util_device_removed_cb), priv);
1063 g_signal_connect (client, "device-changed",
1064 G_CALLBACK (fu_util_device_changed_cb), priv);
1065 g_signal_connect (priv->cancellable, "cancelled",
1066 G_CALLBACK (fu_util_cancelled_cb), priv);
1067 g_main_loop_run (priv->loop);
1068 return TRUE;
1069}
1070
Richard Hughesb5976832018-05-18 10:02:09 +01001071int
1072main (int argc, char *argv[])
1073{
Richard Hughes460226a2018-05-21 20:56:21 +01001074 gboolean allow_older = FALSE;
1075 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +01001076 gboolean force = FALSE;
1077 gboolean ret;
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001078 gboolean version = FALSE;
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001079 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +01001080 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
1081 g_autoptr(GError) error = NULL;
1082 g_autofree gchar *cmd_descriptions = NULL;
1083 const GOptionEntry options[] = {
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001084 { "version", '\0', 0, G_OPTION_ARG_NONE, &version,
1085 /* TRANSLATORS: command line option */
1086 _("Show client and daemon versions"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +01001087 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
1088 /* TRANSLATORS: command line option */
1089 _("Allow re-installing existing firmware versions"), NULL },
1090 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
1091 /* TRANSLATORS: command line option */
1092 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001093 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
1094 /* TRANSLATORS: command line option */
1095 _("Override plugin warning"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001096 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
1097 /* TRANSLATORS: command line option */
1098 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001099 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
1100 /* TRANSLATORS: command line option */
1101 _("Manually whitelist specific plugins"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +01001102 { NULL}
1103 };
1104
1105 setlocale (LC_ALL, "");
1106
1107 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1108 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1109 textdomain (GETTEXT_PACKAGE);
1110
1111 /* ensure root user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001112 if (getuid () != 0 || geteuid () != 0)
Richard Hughesb5976832018-05-18 10:02:09 +01001113 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -05001114 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +01001115
1116 /* create helper object */
1117 priv->loop = g_main_loop_new (NULL, FALSE);
1118 priv->progressbar = fu_progressbar_new ();
1119
1120 /* add commands */
1121 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
1122 fu_util_add (priv->cmd_array,
Mario Limonciellof6d01b12018-10-18 12:57:10 -05001123 "build-firmware",
1124 "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]",
1125 /* TRANSLATORS: command description */
1126 _("Build firmware using a sandbox"),
1127 fu_util_firmware_builder);
1128 fu_util_add (priv->cmd_array,
Richard Hughesb5976832018-05-18 10:02:09 +01001129 "smbios-dump",
1130 "FILE",
1131 /* TRANSLATORS: command description */
1132 _("Dump SMBIOS data from a file"),
1133 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +01001134 fu_util_add (priv->cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001135 "get-plugins",
1136 NULL,
1137 /* TRANSLATORS: command description */
1138 _("Get all enabled plugins registered with the system"),
1139 fu_util_get_plugins);
1140 fu_util_add (priv->cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -05001141 "get-details",
1142 NULL,
1143 /* TRANSLATORS: command description */
1144 _("Gets details about a firmware file"),
1145 fu_util_get_details);
1146 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001147 "get-devices",
1148 NULL,
1149 /* TRANSLATORS: command description */
1150 _("Get all devices that support firmware updates"),
1151 fu_util_get_devices);
1152 fu_util_add (priv->cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -05001153 "get-topology",
1154 NULL,
1155 /* TRANSLATORS: command description */
1156 _("Get all devices according to the system topology"),
1157 fu_util_get_topology);
1158 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001159 "watch",
1160 NULL,
1161 /* TRANSLATORS: command description */
Piotr Drąg472fa592018-06-06 14:53:48 +02001162 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +01001163 fu_util_watch);
1164 fu_util_add (priv->cmd_array,
1165 "install-blob",
1166 "FILENAME DEVICE-ID",
1167 /* TRANSLATORS: command description */
1168 _("Install a firmware blob on a device"),
1169 fu_util_install_blob);
1170 fu_util_add (priv->cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +01001171 "install",
1172 "FILE [ID]",
1173 /* TRANSLATORS: command description */
1174 _("Install a firmware file on this hardware"),
1175 fu_util_install);
1176 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +01001177 "attach",
1178 "DEVICE-ID",
1179 /* TRANSLATORS: command description */
1180 _("Attach to firmware mode"),
1181 fu_util_attach);
1182 fu_util_add (priv->cmd_array,
1183 "detach",
1184 "DEVICE-ID",
1185 /* TRANSLATORS: command description */
1186 _("Detach to bootloader mode"),
1187 fu_util_detach);
Richard Hughes1d894f12018-08-31 13:05:51 +01001188 fu_util_add (priv->cmd_array,
1189 "hwids",
1190 "[FILE]",
1191 /* TRANSLATORS: command description */
1192 _("Return all the hardware IDs for the machine"),
1193 fu_util_hwids);
Mario Limonciello62f84862018-10-18 13:15:23 -05001194 fu_util_add (priv->cmd_array,
1195 "monitor",
1196 NULL,
1197 /* TRANSLATORS: command description */
1198 _("Monitor the daemon for events"),
1199 fu_util_monitor);
Mario Limonciello46aaee82019-01-10 12:58:00 -06001200 fu_util_add (priv->cmd_array,
1201 "update",
1202 NULL,
1203 /* TRANSLATORS: command description */
1204 _("Update all devices that match local metadata"),
1205 fu_util_update);
Richard Hughesb5976832018-05-18 10:02:09 +01001206
1207 /* do stuff on ctrl+c */
1208 priv->cancellable = g_cancellable_new ();
1209 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
1210 SIGINT, fu_util_sigint_cb,
1211 priv, NULL);
1212 g_signal_connect (priv->cancellable, "cancelled",
1213 G_CALLBACK (fu_util_cancelled_cb), priv);
1214
1215 /* sort by command name */
1216 g_ptr_array_sort (priv->cmd_array,
1217 (GCompareFunc) fu_sort_command_name_cb);
1218
1219 /* get a list of the commands */
1220 priv->context = g_option_context_new (NULL);
1221 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
1222 g_option_context_set_summary (priv->context, cmd_descriptions);
1223 g_option_context_set_description (priv->context,
1224 "This tool allows an administrator to use the fwupd plugins "
1225 "without being installed on the host system.");
1226
1227 /* TRANSLATORS: program name */
1228 g_set_application_name (_("Firmware Utility"));
1229 g_option_context_add_main_entries (priv->context, options, NULL);
Mario Limonciellofde47732018-09-11 12:20:58 -05001230 g_option_context_add_group (priv->context, fu_debug_get_option_group ());
Richard Hughesb5976832018-05-18 10:02:09 +01001231 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
1232 if (!ret) {
1233 /* TRANSLATORS: the user didn't read the man page */
1234 g_print ("%s: %s\n", _("Failed to parse arguments"),
1235 error->message);
1236 return EXIT_FAILURE;
1237 }
1238
Richard Hughes460226a2018-05-21 20:56:21 +01001239 /* set flags */
1240 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1241 if (allow_reinstall)
1242 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1243 if (allow_older)
1244 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1245 if (force)
1246 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1247
Richard Hughes98ca9932018-05-18 10:24:07 +01001248 /* load engine */
1249 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1250 g_signal_connect (priv->engine, "device-added",
1251 G_CALLBACK (fu_main_engine_device_added_cb),
1252 priv);
1253 g_signal_connect (priv->engine, "device-removed",
1254 G_CALLBACK (fu_main_engine_device_removed_cb),
1255 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001256 g_signal_connect (priv->engine, "status-changed",
1257 G_CALLBACK (fu_main_engine_status_changed_cb),
1258 priv);
1259 g_signal_connect (priv->engine, "percentage-changed",
1260 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1261 priv);
1262
Mario Limonciello2d4b7a52018-09-12 22:08:04 -05001263 /* just show versions and exit */
1264 if (version) {
1265 g_autofree gchar *version_str = fu_util_get_versions ();
1266 g_print ("%s\n", version_str);
1267 return EXIT_SUCCESS;
1268 }
1269
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001270 /* any plugin whitelist specified */
1271 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1272 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1273
Richard Hughesb5976832018-05-18 10:02:09 +01001274 /* run the specified command */
1275 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
1276 if (!ret) {
1277 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1278 g_autofree gchar *tmp = NULL;
1279 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1280 g_print ("%s\n\n%s", error->message, tmp);
1281 return EXIT_FAILURE;
1282 }
1283 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1284 g_print ("%s\n", error->message);
1285 return EXIT_NOTHING_TO_DO;
1286 }
1287 g_print ("%s\n", error->message);
1288 return EXIT_FAILURE;
1289 }
1290
1291 /* success */
1292 return EXIT_SUCCESS;
1293}