blob: 69752997d89cfff1bdf1dcc5350e9d1dbaf1c546 [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
7#include "config.h"
8
9#include <fwupd.h>
10#include <glib/gi18n.h>
11#include <glib-unix.h>
12#include <locale.h>
13#include <stdlib.h>
14#include <unistd.h>
Richard Hughes3d178be2018-08-30 11:14:24 +010015#include <libsoup/soup.h>
Richard Hughesb5976832018-05-18 10:02:09 +010016
Richard Hughes98ca9932018-05-18 10:24:07 +010017#include "fu-engine.h"
Richard Hughes8c71a3f2018-05-22 19:19:52 +010018#include "fu-plugin-private.h"
Richard Hughesb5976832018-05-18 10:02:09 +010019#include "fu-progressbar.h"
20#include "fu-smbios.h"
21#include "fu-util-common.h"
22
23/* this is only valid in this file */
24#define FWUPD_ERROR_INVALID_ARGS (FWUPD_ERROR_LAST+1)
25
26/* custom return code */
27#define EXIT_NOTHING_TO_DO 2
28
29typedef struct {
30 GCancellable *cancellable;
31 GMainLoop *loop;
32 GOptionContext *context;
33 GPtrArray *cmd_array;
Richard Hughes98ca9932018-05-18 10:24:07 +010034 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010035 FuProgressbar *progressbar;
Richard Hughes460226a2018-05-21 20:56:21 +010036 FwupdInstallFlags flags;
Mario Limoncielloba9e5b92018-05-21 16:02:32 -050037 gboolean show_all_devices;
Mario Limonciello9eb66fe2018-08-10 11:32:44 -050038 /* only valid in update and downgrade */
39 FwupdDevice *current_device;
Richard Hughesb5976832018-05-18 10:02:09 +010040} FuUtilPrivate;
41
42typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
43 gchar **values,
44 GError **error);
45
46typedef struct {
47 gchar *name;
48 gchar *arguments;
49 gchar *description;
50 FuUtilPrivateCb callback;
51} FuUtilItem;
52
53static void
54fu_util_item_free (FuUtilItem *item)
55{
56 g_free (item->name);
57 g_free (item->arguments);
58 g_free (item->description);
59 g_free (item);
60}
61
62static gint
63fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
64{
65 return g_strcmp0 ((*item1)->name, (*item2)->name);
66}
67
68static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -050069fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
70{
71 g_autofree gchar *path = g_path_get_dirname (value);
72 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
73 g_prefix_error (error,
74 "Unable to access %s. You may need to copy %s to %s: ",
75 path, value, g_getenv ("HOME"));
76 }
77}
78
79static void
Richard Hughesb5976832018-05-18 10:02:09 +010080fu_util_add (GPtrArray *array,
81 const gchar *name,
82 const gchar *arguments,
83 const gchar *description,
84 FuUtilPrivateCb callback)
85{
86 g_auto(GStrv) names = NULL;
87
88 g_return_if_fail (name != NULL);
89 g_return_if_fail (description != NULL);
90 g_return_if_fail (callback != NULL);
91
92 /* add each one */
93 names = g_strsplit (name, ",", -1);
94 for (guint i = 0; names[i] != NULL; i++) {
95 FuUtilItem *item = g_new0 (FuUtilItem, 1);
96 item->name = g_strdup (names[i]);
97 if (i == 0) {
98 item->description = g_strdup (description);
99 } else {
100 /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */
101 item->description = g_strdup_printf (_("Alias to %s"),
102 names[0]);
103 }
104 item->arguments = g_strdup (arguments);
105 item->callback = callback;
106 g_ptr_array_add (array, item);
107 }
108}
109
110static gchar *
111fu_util_get_descriptions (GPtrArray *array)
112{
113 gsize len;
114 const gsize max_len = 35;
115 GString *string;
116
117 /* print each command */
118 string = g_string_new ("");
119 for (guint i = 0; i < array->len; i++) {
120 FuUtilItem *item = g_ptr_array_index (array, i);
121 g_string_append (string, " ");
122 g_string_append (string, item->name);
123 len = strlen (item->name) + 2;
124 if (item->arguments != NULL) {
125 g_string_append (string, " ");
126 g_string_append (string, item->arguments);
127 len += strlen (item->arguments) + 1;
128 }
129 if (len < max_len) {
130 for (gsize j = len; j < max_len + 1; j++)
131 g_string_append_c (string, ' ');
132 g_string_append (string, item->description);
133 g_string_append_c (string, '\n');
134 } else {
135 g_string_append_c (string, '\n');
136 for (gsize j = 0; j < max_len + 1; j++)
137 g_string_append_c (string, ' ');
138 g_string_append (string, item->description);
139 g_string_append_c (string, '\n');
140 }
141 }
142
143 /* remove trailing newline */
144 if (string->len > 0)
145 g_string_set_size (string, string->len - 1);
146
147 return g_string_free (string, FALSE);
148}
149
150static gboolean
151fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error)
152{
153 /* find command */
154 for (guint i = 0; i < priv->cmd_array->len; i++) {
155 FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i);
156 if (g_strcmp0 (item->name, command) == 0)
157 return item->callback (priv, values, error);
158 }
159
160 /* not found */
161 g_set_error_literal (error,
162 FWUPD_ERROR,
163 FWUPD_ERROR_INVALID_ARGS,
164 /* TRANSLATORS: error message */
165 _("Command not found"));
166 return FALSE;
167}
168
169static void
170fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
171{
172 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
173 /* TRANSLATORS: this is when a device ctrl+c's a watch */
174 g_print ("%s\n", _("Cancelled"));
175 g_main_loop_quit (priv->loop);
176}
177
178static gboolean
179fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
180{
181 g_autofree gchar *tmp = NULL;
182 g_autoptr(FuSmbios) smbios = NULL;
183 if (g_strv_length (values) < 1) {
184 g_set_error_literal (error,
185 FWUPD_ERROR,
186 FWUPD_ERROR_INVALID_ARGS,
187 "Invalid arguments");
188 return FALSE;
189 }
190 smbios = fu_smbios_new ();
191 if (!fu_smbios_setup_from_file (smbios, values[0], error))
192 return FALSE;
193 tmp = fu_smbios_to_string (smbios);
194 g_print ("%s\n", tmp);
195 return TRUE;
196}
197
198static void
199fu_util_ignore_cb (const gchar *log_domain, GLogLevelFlags log_level,
200 const gchar *message, gpointer user_data)
201{
202}
203
204static gboolean
205fu_util_sigint_cb (gpointer user_data)
206{
207 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
208 g_debug ("Handling SIGINT");
209 g_cancellable_cancel (priv->cancellable);
210 return FALSE;
211}
212
213static void
214fu_util_private_free (FuUtilPrivate *priv)
215{
216 if (priv->cmd_array != NULL)
217 g_ptr_array_unref (priv->cmd_array);
Mario Limonciellocc50e1a2018-08-14 17:45:24 -0500218 if (priv->current_device != NULL)
219 g_object_unref (priv->current_device);
Richard Hughes98ca9932018-05-18 10:24:07 +0100220 if (priv->engine != NULL)
221 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100222 if (priv->loop != NULL)
223 g_main_loop_unref (priv->loop);
224 if (priv->cancellable != NULL)
225 g_object_unref (priv->cancellable);
226 if (priv->progressbar != NULL)
227 g_object_unref (priv->progressbar);
228 if (priv->context != NULL)
229 g_option_context_free (priv->context);
230 g_free (priv);
231}
232
233#pragma clang diagnostic push
234#pragma clang diagnostic ignored "-Wunused-function"
235G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
236#pragma clang diagnostic pop
237
Richard Hughes98ca9932018-05-18 10:24:07 +0100238
239static void
240fu_main_engine_device_added_cb (FuEngine *engine,
241 FuDevice *device,
242 FuUtilPrivate *priv)
243{
244 g_autofree gchar *tmp = fu_device_to_string (device);
245 g_debug ("ADDED:\n%s", tmp);
246}
247
248static void
249fu_main_engine_device_removed_cb (FuEngine *engine,
250 FuDevice *device,
251 FuUtilPrivate *priv)
252{
253 g_autofree gchar *tmp = fu_device_to_string (device);
254 g_debug ("REMOVED:\n%s", tmp);
255}
256
257static void
Richard Hughes98ca9932018-05-18 10:24:07 +0100258fu_main_engine_status_changed_cb (FuEngine *engine,
259 FwupdStatus status,
260 FuUtilPrivate *priv)
261{
262 fu_progressbar_update (priv->progressbar, status, 0);
263}
264
265static void
266fu_main_engine_percentage_changed_cb (FuEngine *engine,
267 guint percentage,
268 FuUtilPrivate *priv)
269{
270 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
271}
272
273static gboolean
274fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
275{
276 if (!fu_engine_load (priv->engine, error))
277 return FALSE;
278 g_main_loop_run (priv->loop);
279 return TRUE;
280}
281
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100282static gint
283fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
284{
285 return fu_plugin_name_compare (*item1, *item2);
286}
287
288static gboolean
289fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
290{
291 GPtrArray *plugins;
292 guint cnt = 0;
293
294 /* load engine */
295 if (!fu_engine_load_plugins (priv->engine, error))
296 return FALSE;
297
298 /* print */
299 plugins = fu_engine_get_plugins (priv->engine);
300 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
301 for (guint i = 0; i < plugins->len; i++) {
302 FuPlugin *plugin = g_ptr_array_index (plugins, i);
303 if (!fu_plugin_get_enabled (plugin))
304 continue;
305 g_print ("%s\n", fu_plugin_get_name (plugin));
306 cnt++;
307 }
308 if (cnt == 0) {
309 /* TRANSLATORS: nothing found */
310 g_print ("%s\n", _("No plugins found"));
311 return TRUE;
312 }
313
314 return TRUE;
315}
316
Richard Hughes98ca9932018-05-18 10:24:07 +0100317static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500318fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
319{
320 g_autoptr(GPtrArray) array = NULL;
321 gint fd;
322
323 /* load engine */
324 if (!fu_engine_load (priv->engine, error))
325 return FALSE;
326
327 /* check args */
328 if (g_strv_length (values) != 1) {
329 g_set_error_literal (error,
330 FWUPD_ERROR,
331 FWUPD_ERROR_INVALID_ARGS,
332 "Invalid arguments");
333 return FALSE;
334 }
335
336 /* open file */
337 fd = open (values[0], O_RDONLY);
338 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500339 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500340 g_set_error (error,
341 FWUPD_ERROR,
342 FWUPD_ERROR_INVALID_FILE,
343 "failed to open %s",
344 values[0]);
345 return FALSE;
346 }
347 array = fu_engine_get_details (priv->engine, fd, error);
348 close (fd);
349
350 if (array == NULL)
351 return FALSE;
352 for (guint i = 0; i < array->len; i++) {
353 FwupdDevice *dev = g_ptr_array_index (array, i);
354 g_autofree gchar *tmp = NULL;
355 tmp = fwupd_device_to_string (dev);
Mario Limoncielloece90a42018-07-25 17:35:55 -0500356 g_print ("%s\n", tmp);
Mario Limonciello716ab272018-05-29 12:34:37 -0500357 }
358 return TRUE;
359}
360
361static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100362fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
363{
364 g_autoptr(GPtrArray) devs = NULL;
365
366 /* load engine */
367 if (!fu_engine_load (priv->engine, error))
368 return FALSE;
369
370 /* print */
371 devs = fu_engine_get_devices (priv->engine, error);
372 if (devs == NULL)
373 return FALSE;
374 if (devs->len == 0) {
375 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500376 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100377 return TRUE;
378 }
379 for (guint i = 0; i < devs->len; i++) {
380 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500381 if (priv->show_all_devices || fu_util_is_interesting_device (dev)) {
382 g_autofree gchar *tmp = fwupd_device_to_string (dev);
383 g_print ("%s\n", tmp);
384 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100385 }
386
387 return TRUE;
388}
389
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500390static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100391fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500392{
393 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100394 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
Mario Limonciellod1775bc2018-07-17 00:28:52 -0500395 if (!priv->show_all_devices &&
396 !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp)))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500397 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100398 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500399 GNode *child = g_node_append_data (root, dev_tmp);
400 fu_util_build_device_tree (priv, child, devs, dev_tmp);
401 }
402 }
403}
404
405static gboolean
406fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
407{
408 g_autoptr(GNode) root = g_node_new (NULL);
409 g_autoptr(GPtrArray) devs = NULL;
410
411 /* load engine */
412 if (!fu_engine_load (priv->engine, error))
413 return FALSE;
414
415 /* print */
416 devs = fu_engine_get_devices (priv->engine, error);
417 if (devs == NULL)
418 return FALSE;
419
420 /* print */
421 if (devs->len == 0) {
422 /* TRANSLATORS: nothing attached that can be upgraded */
423 g_print ("%s\n", _("No hardware detected with firmware update capability"));
424 return TRUE;
425 }
426 fu_util_build_device_tree (priv, root, devs, NULL);
427 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
428 fu_util_print_device_tree, priv);
429
430 return TRUE;
431}
432
433
Richard Hughes98ca9932018-05-18 10:24:07 +0100434static FuDevice *
435fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
436{
437 FuDevice *dev;
438 guint idx;
439 g_autoptr(GPtrArray) devices = NULL;
440
441 /* get devices from daemon */
442 devices = fu_engine_get_devices (priv->engine, error);
443 if (devices == NULL)
444 return NULL;
445
446 /* exactly one */
447 if (devices->len == 1) {
448 dev = g_ptr_array_index (devices, 0);
449 return g_object_ref (dev);
450 }
451
452 /* TRANSLATORS: get interactive prompt */
453 g_print ("%s\n", _("Choose a device:"));
454 /* TRANSLATORS: this is to abort the interactive prompt */
455 g_print ("0.\t%s\n", _("Cancel"));
456 for (guint i = 0; i < devices->len; i++) {
457 dev = g_ptr_array_index (devices, i);
458 g_print ("%u.\t%s (%s)\n",
459 i + 1,
460 fu_device_get_id (dev),
461 fu_device_get_name (dev));
462 }
463 idx = fu_util_prompt_for_number (devices->len);
464 if (idx == 0) {
465 g_set_error_literal (error,
466 FWUPD_ERROR,
467 FWUPD_ERROR_NOTHING_TO_DO,
468 "Request canceled");
469 return NULL;
470 }
471 dev = g_ptr_array_index (devices, idx - 1);
472 return g_object_ref (dev);
473}
474
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500475static void
476fu_util_install_device_changed_cb (FwupdClient *client,
477 FwupdDevice *device,
478 FuUtilPrivate *priv)
479{
480 g_autofree gchar *str = NULL;
481
482 /* same as last time, so ignore */
483 if (priv->current_device != NULL &&
484 fwupd_device_compare (priv->current_device, device) == 0)
485 return;
486
487 /* show message in progressbar */
488 /* TRANSLATORS: %1 is a device name */
489 str = g_strdup_printf (_("Installing %s"),
490 fwupd_device_get_name (device));
491 fu_progressbar_set_title (priv->progressbar, str);
492 g_set_object (&priv->current_device, device);
493}
494
Richard Hughes98ca9932018-05-18 10:24:07 +0100495static gboolean
496fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
497{
498 g_autoptr(FuDevice) device = NULL;
499 g_autoptr(GBytes) blob_fw = NULL;
500
501 /* invalid args */
502 if (g_strv_length (values) == 0) {
503 g_set_error_literal (error,
504 FWUPD_ERROR,
505 FWUPD_ERROR_INVALID_ARGS,
506 "Invalid arguments");
507 return FALSE;
508 }
509
510 /* parse blob */
511 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500512 if (blob_fw == NULL) {
513 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100514 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500515 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100516
517 /* load engine */
518 if (!fu_engine_load (priv->engine, error))
519 return FALSE;
520
521 /* get device */
522 if (g_strv_length (values) >= 2) {
523 device = fu_engine_get_device (priv->engine, values[1], error);
524 if (device == NULL)
525 return FALSE;
526 } else {
527 device = fu_util_prompt_for_device (priv, error);
528 if (device == NULL)
529 return FALSE;
530 }
531
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500532 g_signal_connect (priv->engine, "device-changed",
533 G_CALLBACK (fu_util_install_device_changed_cb), priv);
534
Richard Hughes98ca9932018-05-18 10:24:07 +0100535 /* write bare firmware */
536 return fu_engine_install_blob (priv->engine, device,
537 NULL, /* blob_cab */
538 blob_fw,
539 NULL, /* version */
Richard Hughes460226a2018-05-21 20:56:21 +0100540 priv->flags,
Richard Hughes98ca9932018-05-18 10:24:07 +0100541 error);
542}
543
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100544static gint
545fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
546{
547 FuInstallTask *task1 = *((FuInstallTask **) a);
548 FuInstallTask *task2 = *((FuInstallTask **) b);
549 return fu_install_task_compare (task1, task2);
550}
551
552static gboolean
Richard Hughes3d178be2018-08-30 11:14:24 +0100553fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error)
554{
555 const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL },
556 { "curl", uri, "--output", fn, NULL },
557 { NULL } };
558 for (guint i = 0; argv[i][0] != NULL; i++) {
559 g_autoptr(GError) error_local = NULL;
560 if (!fu_common_find_program_in_path (argv[i][0], &error_local)) {
561 g_debug ("%s", error_local->message);
562 continue;
563 }
564 return fu_common_spawn_sync (argv[i], NULL, NULL, NULL, error);
565 }
566 g_set_error_literal (error,
567 FWUPD_ERROR,
568 FWUPD_ERROR_NOT_FOUND,
569 "no supported out-of-process downloaders found");
570 return FALSE;
571}
572
573static gchar *
574fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
575{
576 g_autofree gchar *filename = NULL;
577 g_autoptr(SoupURI) uri = NULL;
578
579 /* a local file */
580 uri = soup_uri_new (perhapsfn);
581 if (uri == NULL)
582 return g_strdup (perhapsfn);
583
584 /* download the firmware to a cachedir */
585 filename = fu_util_get_user_cache_path (perhapsfn);
586 if (!fu_common_mkdir_parent (filename, error))
587 return NULL;
588 if (!fu_util_download_out_of_process (perhapsfn, filename, error))
589 return NULL;
590 return g_steal_pointer (&filename);
591}
592
593static gboolean
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100594fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
595{
596 GPtrArray *apps;
Richard Hughes3d178be2018-08-30 11:14:24 +0100597 g_autofree gchar *filename = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100598 g_autoptr(AsStore) store = NULL;
599 g_autoptr(GBytes) blob_cab = NULL;
600 g_autoptr(GPtrArray) devices_possible = NULL;
601 g_autoptr(GPtrArray) errors = NULL;
602 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100603
Mario Limonciello8949e892018-05-25 08:03:06 -0500604 /* load engine */
605 if (!fu_engine_load (priv->engine, error))
606 return FALSE;
607
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100608 /* handle both forms */
609 if (g_strv_length (values) == 1) {
610 devices_possible = fu_engine_get_devices (priv->engine, error);
611 if (devices_possible == NULL)
612 return FALSE;
613 } else if (g_strv_length (values) == 2) {
614 FuDevice *device = fu_engine_get_device (priv->engine,
615 values[1],
616 error);
617 if (device == NULL)
618 return FALSE;
619 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
620 g_ptr_array_add (devices_possible, device);
621 } else {
622 g_set_error_literal (error,
623 FWUPD_ERROR,
624 FWUPD_ERROR_INVALID_ARGS,
625 "Invalid arguments");
626 return FALSE;
627 }
628
Richard Hughes3d178be2018-08-30 11:14:24 +0100629 /* download if required */
630 filename = fu_util_download_if_required (priv, values[0], error);
631 if (filename == NULL)
632 return FALSE;
633
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100634 /* parse store */
Richard Hughes3d178be2018-08-30 11:14:24 +0100635 blob_cab = fu_common_get_contents_bytes (filename, error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500636 if (blob_cab == NULL) {
Richard Hughes3d178be2018-08-30 11:14:24 +0100637 fu_util_maybe_prefix_sandbox_error (filename, error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100638 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500639 }
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100640 store = fu_engine_get_store_from_blob (priv->engine, blob_cab, error);
641 if (store == NULL)
642 return FALSE;
643 apps = as_store_get_apps (store);
644
645 /* for each component in the store */
646 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
647 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
648 for (guint i = 0; i < apps->len; i++) {
649 AsApp *app = g_ptr_array_index (apps, i);
650
651 /* do any devices pass the requirements */
652 for (guint j = 0; j < devices_possible->len; j++) {
653 FuDevice *device = g_ptr_array_index (devices_possible, j);
654 g_autoptr(FuInstallTask) task = NULL;
655 g_autoptr(GError) error_local = NULL;
656
657 /* is this component valid for the device */
658 task = fu_install_task_new (device, app);
659 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100660 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100661 &error_local)) {
662 g_debug ("requirement on %s:%s failed: %s",
663 fu_device_get_id (device),
664 as_app_get_id (app),
665 error_local->message);
666 g_ptr_array_add (errors, g_steal_pointer (&error_local));
667 continue;
668 }
669
670 /* success */
671 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
672 }
673 }
674
675 /* order the install tasks by the device priority */
676 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
677
678 /* nothing suitable */
679 if (install_tasks->len == 0) {
680 GError *error_tmp = fu_common_error_array_get_best (errors);
681 g_propagate_error (error, error_tmp);
682 return FALSE;
683 }
684
Mario Limonciello9eb66fe2018-08-10 11:32:44 -0500685 g_signal_connect (priv->engine, "device-changed",
686 G_CALLBACK (fu_util_install_device_changed_cb), priv);
687
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100688 /* install all the tasks */
Richard Hughesdbd8c762018-06-15 20:31:40 +0100689 if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error))
690 return FALSE;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100691
692 /* success */
693 return TRUE;
694}
695
Richard Hughes98ca9932018-05-18 10:24:07 +0100696static gboolean
697fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
698{
699 g_autoptr(FuDevice) device = NULL;
700
701 /* load engine */
702 if (!fu_engine_load (priv->engine, error))
703 return FALSE;
704
705 /* invalid args */
706 if (g_strv_length (values) == 0) {
707 g_set_error_literal (error,
708 FWUPD_ERROR,
709 FWUPD_ERROR_INVALID_ARGS,
710 "Invalid arguments");
711 return FALSE;
712 }
713
714 /* get device */
715 if (g_strv_length (values) >= 1) {
716 device = fu_engine_get_device (priv->engine, values[0], error);
717 if (device == NULL)
718 return FALSE;
719 } else {
720 device = fu_util_prompt_for_device (priv, error);
721 if (device == NULL)
722 return FALSE;
723 }
724
725 /* run vfunc */
726 return fu_device_detach (device, error);
727}
728
729static gboolean
730fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
731{
732 g_autoptr(FuDevice) device = NULL;
733
734 /* load engine */
735 if (!fu_engine_load (priv->engine, error))
736 return FALSE;
737
738 /* invalid args */
739 if (g_strv_length (values) == 0) {
740 g_set_error_literal (error,
741 FWUPD_ERROR,
742 FWUPD_ERROR_INVALID_ARGS,
743 "Invalid arguments");
744 return FALSE;
745 }
746
747 /* get device */
748 if (g_strv_length (values) >= 1) {
749 device = fu_engine_get_device (priv->engine, values[0], error);
750 if (device == NULL)
751 return FALSE;
752 } else {
753 device = fu_util_prompt_for_device (priv, error);
754 if (device == NULL)
755 return FALSE;
756 }
757
758 /* run vfunc */
759 return fu_device_attach (device, error);
760}
761
Richard Hughes1d894f12018-08-31 13:05:51 +0100762static gboolean
763fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error)
764{
765 g_autoptr(FuSmbios) smbios = fu_smbios_new ();
766 g_autoptr(FuHwids) hwids = fu_hwids_new ();
767 const gchar *hwid_keys[] = {
768 FU_HWIDS_KEY_BIOS_VENDOR,
769 FU_HWIDS_KEY_BIOS_VERSION,
770 FU_HWIDS_KEY_BIOS_MAJOR_RELEASE,
771 FU_HWIDS_KEY_BIOS_MINOR_RELEASE,
772 FU_HWIDS_KEY_MANUFACTURER,
773 FU_HWIDS_KEY_FAMILY,
774 FU_HWIDS_KEY_PRODUCT_NAME,
775 FU_HWIDS_KEY_PRODUCT_SKU,
776 FU_HWIDS_KEY_ENCLOSURE_KIND,
777 FU_HWIDS_KEY_BASEBOARD_MANUFACTURER,
778 FU_HWIDS_KEY_BASEBOARD_PRODUCT,
779 NULL };
780
781 /* read DMI data */
782 if (g_strv_length (values) == 0) {
783 if (!fu_smbios_setup (smbios, error))
784 return FALSE;
785 } else if (g_strv_length (values) == 1) {
786 if (!fu_smbios_setup_from_file (smbios, values[0], error))
787 return FALSE;
788 } else {
789 g_set_error_literal (error,
790 FWUPD_ERROR,
791 FWUPD_ERROR_INVALID_ARGS,
792 "Invalid arguments");
793 return FALSE;
794 }
795 if (!fu_hwids_setup (hwids, smbios, error))
796 return FALSE;
797
798 /* show debug output */
799 g_print ("Computer Information\n");
800 g_print ("--------------------\n");
801 for (guint i = 0; hwid_keys[i] != NULL; i++) {
802 const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]);
803 if (tmp == NULL)
804 continue;
805 if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 ||
806 g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) {
807 guint64 val = g_ascii_strtoull (tmp, NULL, 16);
808 g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val);
809 } else {
810 g_print ("%s: %s\n", hwid_keys[i], tmp);
811 }
812 }
813
814 /* show GUIDs */
815 g_print ("\nHardware IDs\n");
816 g_print ("------------\n");
817 for (guint i = 0; i < 15; i++) {
818 const gchar *keys = NULL;
819 g_autofree gchar *guid = NULL;
820 g_autofree gchar *key = NULL;
821 g_autofree gchar *keys_str = NULL;
822 g_auto(GStrv) keysv = NULL;
823 g_autoptr(GError) error_local = NULL;
824
825 /* get the GUID */
826 key = g_strdup_printf ("HardwareID-%u", i);
827 keys = fu_hwids_get_replace_keys (hwids, key);
828 guid = fu_hwids_get_guid (hwids, key, &error_local);
829 if (guid == NULL) {
830 g_print ("%s\n", error_local->message);
831 continue;
832 }
833
834 /* show what makes up the GUID */
835 keysv = g_strsplit (keys, "&", -1);
836 keys_str = g_strjoinv (" + ", keysv);
837 g_print ("{%s} <- %s\n", guid, keys_str);
838 }
839
840 return TRUE;
841}
842
Richard Hughesb5976832018-05-18 10:02:09 +0100843int
844main (int argc, char *argv[])
845{
Richard Hughes460226a2018-05-21 20:56:21 +0100846 gboolean allow_older = FALSE;
847 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +0100848 gboolean force = FALSE;
849 gboolean ret;
850 gboolean verbose = FALSE;
Richard Hughesc02ee4d2018-05-22 15:46:03 +0100851 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +0100852 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
853 g_autoptr(GError) error = NULL;
854 g_autofree gchar *cmd_descriptions = NULL;
855 const GOptionEntry options[] = {
856 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
857 /* TRANSLATORS: command line option */
858 _("Show extra debugging information"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +0100859 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
860 /* TRANSLATORS: command line option */
861 _("Allow re-installing existing firmware versions"), NULL },
862 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
863 /* TRANSLATORS: command line option */
864 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +0100865 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
866 /* TRANSLATORS: command line option */
867 _("Override plugin warning"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500868 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
869 /* TRANSLATORS: command line option */
870 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +0100871 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
872 /* TRANSLATORS: command line option */
873 _("Manually whitelist specific plugins"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +0100874 { NULL}
875 };
876
877 setlocale (LC_ALL, "");
878
879 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
880 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
881 textdomain (GETTEXT_PACKAGE);
882
883 /* ensure root user */
Mario Limonciellob900c092018-05-22 14:22:21 -0500884 if (getuid () != 0 || geteuid () != 0)
Richard Hughesb5976832018-05-18 10:02:09 +0100885 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -0500886 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +0100887
888 /* create helper object */
889 priv->loop = g_main_loop_new (NULL, FALSE);
890 priv->progressbar = fu_progressbar_new ();
891
892 /* add commands */
893 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
894 fu_util_add (priv->cmd_array,
895 "smbios-dump",
896 "FILE",
897 /* TRANSLATORS: command description */
898 _("Dump SMBIOS data from a file"),
899 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +0100900 fu_util_add (priv->cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100901 "get-plugins",
902 NULL,
903 /* TRANSLATORS: command description */
904 _("Get all enabled plugins registered with the system"),
905 fu_util_get_plugins);
906 fu_util_add (priv->cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -0500907 "get-details",
908 NULL,
909 /* TRANSLATORS: command description */
910 _("Gets details about a firmware file"),
911 fu_util_get_details);
912 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100913 "get-devices",
914 NULL,
915 /* TRANSLATORS: command description */
916 _("Get all devices that support firmware updates"),
917 fu_util_get_devices);
918 fu_util_add (priv->cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500919 "get-topology",
920 NULL,
921 /* TRANSLATORS: command description */
922 _("Get all devices according to the system topology"),
923 fu_util_get_topology);
924 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100925 "watch",
926 NULL,
927 /* TRANSLATORS: command description */
Piotr Drąg472fa592018-06-06 14:53:48 +0200928 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +0100929 fu_util_watch);
930 fu_util_add (priv->cmd_array,
931 "install-blob",
932 "FILENAME DEVICE-ID",
933 /* TRANSLATORS: command description */
934 _("Install a firmware blob on a device"),
935 fu_util_install_blob);
936 fu_util_add (priv->cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100937 "install",
938 "FILE [ID]",
939 /* TRANSLATORS: command description */
940 _("Install a firmware file on this hardware"),
941 fu_util_install);
942 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100943 "attach",
944 "DEVICE-ID",
945 /* TRANSLATORS: command description */
946 _("Attach to firmware mode"),
947 fu_util_attach);
948 fu_util_add (priv->cmd_array,
949 "detach",
950 "DEVICE-ID",
951 /* TRANSLATORS: command description */
952 _("Detach to bootloader mode"),
953 fu_util_detach);
Richard Hughes1d894f12018-08-31 13:05:51 +0100954 fu_util_add (priv->cmd_array,
955 "hwids",
956 "[FILE]",
957 /* TRANSLATORS: command description */
958 _("Return all the hardware IDs for the machine"),
959 fu_util_hwids);
Richard Hughesb5976832018-05-18 10:02:09 +0100960
961 /* do stuff on ctrl+c */
962 priv->cancellable = g_cancellable_new ();
963 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
964 SIGINT, fu_util_sigint_cb,
965 priv, NULL);
966 g_signal_connect (priv->cancellable, "cancelled",
967 G_CALLBACK (fu_util_cancelled_cb), priv);
968
969 /* sort by command name */
970 g_ptr_array_sort (priv->cmd_array,
971 (GCompareFunc) fu_sort_command_name_cb);
972
973 /* get a list of the commands */
974 priv->context = g_option_context_new (NULL);
975 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
976 g_option_context_set_summary (priv->context, cmd_descriptions);
977 g_option_context_set_description (priv->context,
978 "This tool allows an administrator to use the fwupd plugins "
979 "without being installed on the host system.");
980
981 /* TRANSLATORS: program name */
982 g_set_application_name (_("Firmware Utility"));
983 g_option_context_add_main_entries (priv->context, options, NULL);
984 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
985 if (!ret) {
986 /* TRANSLATORS: the user didn't read the man page */
987 g_print ("%s: %s\n", _("Failed to parse arguments"),
988 error->message);
989 return EXIT_FAILURE;
990 }
991
992 /* set verbose? */
993 if (verbose) {
994 g_setenv ("G_MESSAGES_DEBUG", "all", FALSE);
995 } else {
996 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
997 fu_util_ignore_cb, NULL);
998 }
999
Richard Hughes460226a2018-05-21 20:56:21 +01001000 /* set flags */
1001 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
1002 if (allow_reinstall)
1003 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
1004 if (allow_older)
1005 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
1006 if (force)
1007 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
1008
Richard Hughes98ca9932018-05-18 10:24:07 +01001009 /* load engine */
1010 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
1011 g_signal_connect (priv->engine, "device-added",
1012 G_CALLBACK (fu_main_engine_device_added_cb),
1013 priv);
1014 g_signal_connect (priv->engine, "device-removed",
1015 G_CALLBACK (fu_main_engine_device_removed_cb),
1016 priv);
Richard Hughes98ca9932018-05-18 10:24:07 +01001017 g_signal_connect (priv->engine, "status-changed",
1018 G_CALLBACK (fu_main_engine_status_changed_cb),
1019 priv);
1020 g_signal_connect (priv->engine, "percentage-changed",
1021 G_CALLBACK (fu_main_engine_percentage_changed_cb),
1022 priv);
1023
Richard Hughesc02ee4d2018-05-22 15:46:03 +01001024 /* any plugin whitelist specified */
1025 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
1026 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
1027
Richard Hughesb5976832018-05-18 10:02:09 +01001028 /* run the specified command */
1029 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
1030 if (!ret) {
1031 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
1032 g_autofree gchar *tmp = NULL;
1033 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
1034 g_print ("%s\n\n%s", error->message, tmp);
1035 return EXIT_FAILURE;
1036 }
1037 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
1038 g_print ("%s\n", error->message);
1039 return EXIT_NOTHING_TO_DO;
1040 }
1041 g_print ("%s\n", error->message);
1042 return EXIT_FAILURE;
1043 }
1044
1045 /* success */
1046 return EXIT_SUCCESS;
1047}