blob: 36912e4a8afdce2637ff81ac449923a17ed0ced4 [file] [log] [blame]
Richard Hughesb5976832018-05-18 10:02:09 +01001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com>
4 *
Mario Limonciello51308e62018-05-28 20:05:46 -05005 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesb5976832018-05-18 10:02:09 +01006 */
7
8#include "config.h"
9
10#include <fwupd.h>
11#include <glib/gi18n.h>
12#include <glib-unix.h>
13#include <locale.h>
14#include <stdlib.h>
15#include <unistd.h>
16
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;
Richard Hughesb5976832018-05-18 10:02:09 +010038} FuUtilPrivate;
39
40typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
41 gchar **values,
42 GError **error);
43
44typedef struct {
45 gchar *name;
46 gchar *arguments;
47 gchar *description;
48 FuUtilPrivateCb callback;
49} FuUtilItem;
50
51static void
52fu_util_item_free (FuUtilItem *item)
53{
54 g_free (item->name);
55 g_free (item->arguments);
56 g_free (item->description);
57 g_free (item);
58}
59
60static gint
61fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
62{
63 return g_strcmp0 ((*item1)->name, (*item2)->name);
64}
65
66static void
Mario Limonciellob72aa8c2018-06-08 09:24:36 -050067fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error)
68{
69 g_autofree gchar *path = g_path_get_dirname (value);
70 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
71 g_prefix_error (error,
72 "Unable to access %s. You may need to copy %s to %s: ",
73 path, value, g_getenv ("HOME"));
74 }
75}
76
77static void
Richard Hughesb5976832018-05-18 10:02:09 +010078fu_util_add (GPtrArray *array,
79 const gchar *name,
80 const gchar *arguments,
81 const gchar *description,
82 FuUtilPrivateCb callback)
83{
84 g_auto(GStrv) names = NULL;
85
86 g_return_if_fail (name != NULL);
87 g_return_if_fail (description != NULL);
88 g_return_if_fail (callback != NULL);
89
90 /* add each one */
91 names = g_strsplit (name, ",", -1);
92 for (guint i = 0; names[i] != NULL; i++) {
93 FuUtilItem *item = g_new0 (FuUtilItem, 1);
94 item->name = g_strdup (names[i]);
95 if (i == 0) {
96 item->description = g_strdup (description);
97 } else {
98 /* TRANSLATORS: this is a command alias, e.g. 'get-devices' */
99 item->description = g_strdup_printf (_("Alias to %s"),
100 names[0]);
101 }
102 item->arguments = g_strdup (arguments);
103 item->callback = callback;
104 g_ptr_array_add (array, item);
105 }
106}
107
108static gchar *
109fu_util_get_descriptions (GPtrArray *array)
110{
111 gsize len;
112 const gsize max_len = 35;
113 GString *string;
114
115 /* print each command */
116 string = g_string_new ("");
117 for (guint i = 0; i < array->len; i++) {
118 FuUtilItem *item = g_ptr_array_index (array, i);
119 g_string_append (string, " ");
120 g_string_append (string, item->name);
121 len = strlen (item->name) + 2;
122 if (item->arguments != NULL) {
123 g_string_append (string, " ");
124 g_string_append (string, item->arguments);
125 len += strlen (item->arguments) + 1;
126 }
127 if (len < max_len) {
128 for (gsize j = len; j < max_len + 1; j++)
129 g_string_append_c (string, ' ');
130 g_string_append (string, item->description);
131 g_string_append_c (string, '\n');
132 } else {
133 g_string_append_c (string, '\n');
134 for (gsize j = 0; j < max_len + 1; j++)
135 g_string_append_c (string, ' ');
136 g_string_append (string, item->description);
137 g_string_append_c (string, '\n');
138 }
139 }
140
141 /* remove trailing newline */
142 if (string->len > 0)
143 g_string_set_size (string, string->len - 1);
144
145 return g_string_free (string, FALSE);
146}
147
148static gboolean
149fu_util_run (FuUtilPrivate *priv, const gchar *command, gchar **values, GError **error)
150{
151 /* find command */
152 for (guint i = 0; i < priv->cmd_array->len; i++) {
153 FuUtilItem *item = g_ptr_array_index (priv->cmd_array, i);
154 if (g_strcmp0 (item->name, command) == 0)
155 return item->callback (priv, values, error);
156 }
157
158 /* not found */
159 g_set_error_literal (error,
160 FWUPD_ERROR,
161 FWUPD_ERROR_INVALID_ARGS,
162 /* TRANSLATORS: error message */
163 _("Command not found"));
164 return FALSE;
165}
166
167static void
168fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data)
169{
170 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
171 /* TRANSLATORS: this is when a device ctrl+c's a watch */
172 g_print ("%s\n", _("Cancelled"));
173 g_main_loop_quit (priv->loop);
174}
175
176static gboolean
177fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error)
178{
179 g_autofree gchar *tmp = NULL;
180 g_autoptr(FuSmbios) smbios = NULL;
181 if (g_strv_length (values) < 1) {
182 g_set_error_literal (error,
183 FWUPD_ERROR,
184 FWUPD_ERROR_INVALID_ARGS,
185 "Invalid arguments");
186 return FALSE;
187 }
188 smbios = fu_smbios_new ();
189 if (!fu_smbios_setup_from_file (smbios, values[0], error))
190 return FALSE;
191 tmp = fu_smbios_to_string (smbios);
192 g_print ("%s\n", tmp);
193 return TRUE;
194}
195
196static void
197fu_util_ignore_cb (const gchar *log_domain, GLogLevelFlags log_level,
198 const gchar *message, gpointer user_data)
199{
200}
201
202static gboolean
203fu_util_sigint_cb (gpointer user_data)
204{
205 FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
206 g_debug ("Handling SIGINT");
207 g_cancellable_cancel (priv->cancellable);
208 return FALSE;
209}
210
211static void
212fu_util_private_free (FuUtilPrivate *priv)
213{
214 if (priv->cmd_array != NULL)
215 g_ptr_array_unref (priv->cmd_array);
Richard Hughes98ca9932018-05-18 10:24:07 +0100216 if (priv->engine != NULL)
217 g_object_unref (priv->engine);
Richard Hughesb5976832018-05-18 10:02:09 +0100218 if (priv->loop != NULL)
219 g_main_loop_unref (priv->loop);
220 if (priv->cancellable != NULL)
221 g_object_unref (priv->cancellable);
222 if (priv->progressbar != NULL)
223 g_object_unref (priv->progressbar);
224 if (priv->context != NULL)
225 g_option_context_free (priv->context);
226 g_free (priv);
227}
228
229#pragma clang diagnostic push
230#pragma clang diagnostic ignored "-Wunused-function"
231G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free)
232#pragma clang diagnostic pop
233
Richard Hughes98ca9932018-05-18 10:24:07 +0100234
235static void
236fu_main_engine_device_added_cb (FuEngine *engine,
237 FuDevice *device,
238 FuUtilPrivate *priv)
239{
240 g_autofree gchar *tmp = fu_device_to_string (device);
241 g_debug ("ADDED:\n%s", tmp);
242}
243
244static void
245fu_main_engine_device_removed_cb (FuEngine *engine,
246 FuDevice *device,
247 FuUtilPrivate *priv)
248{
249 g_autofree gchar *tmp = fu_device_to_string (device);
250 g_debug ("REMOVED:\n%s", tmp);
251}
252
253static void
254fu_main_engine_device_changed_cb (FuEngine *engine,
255 FuDevice *device,
256 FuUtilPrivate *priv)
257{
258 g_autofree gchar *tmp = fu_device_to_string (device);
259 g_debug ("CHANGED:\n%s", tmp);
260}
261
262static void
263fu_main_engine_status_changed_cb (FuEngine *engine,
264 FwupdStatus status,
265 FuUtilPrivate *priv)
266{
267 fu_progressbar_update (priv->progressbar, status, 0);
268}
269
270static void
271fu_main_engine_percentage_changed_cb (FuEngine *engine,
272 guint percentage,
273 FuUtilPrivate *priv)
274{
275 fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage);
276}
277
278static gboolean
279fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error)
280{
281 if (!fu_engine_load (priv->engine, error))
282 return FALSE;
283 g_main_loop_run (priv->loop);
284 return TRUE;
285}
286
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100287static gint
288fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2)
289{
290 return fu_plugin_name_compare (*item1, *item2);
291}
292
293static gboolean
294fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error)
295{
296 GPtrArray *plugins;
297 guint cnt = 0;
298
299 /* load engine */
300 if (!fu_engine_load_plugins (priv->engine, error))
301 return FALSE;
302
303 /* print */
304 plugins = fu_engine_get_plugins (priv->engine);
305 g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb);
306 for (guint i = 0; i < plugins->len; i++) {
307 FuPlugin *plugin = g_ptr_array_index (plugins, i);
308 if (!fu_plugin_get_enabled (plugin))
309 continue;
310 g_print ("%s\n", fu_plugin_get_name (plugin));
311 cnt++;
312 }
313 if (cnt == 0) {
314 /* TRANSLATORS: nothing found */
315 g_print ("%s\n", _("No plugins found"));
316 return TRUE;
317 }
318
319 return TRUE;
320}
321
Richard Hughes98ca9932018-05-18 10:24:07 +0100322static gboolean
Mario Limonciello716ab272018-05-29 12:34:37 -0500323fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
324{
325 g_autoptr(GPtrArray) array = NULL;
326 gint fd;
327
328 /* load engine */
329 if (!fu_engine_load (priv->engine, error))
330 return FALSE;
331
332 /* check args */
333 if (g_strv_length (values) != 1) {
334 g_set_error_literal (error,
335 FWUPD_ERROR,
336 FWUPD_ERROR_INVALID_ARGS,
337 "Invalid arguments");
338 return FALSE;
339 }
340
341 /* open file */
342 fd = open (values[0], O_RDONLY);
343 if (fd < 0) {
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500344 fu_util_maybe_prefix_sandbox_error (values[0], error);
Mario Limonciello716ab272018-05-29 12:34:37 -0500345 g_set_error (error,
346 FWUPD_ERROR,
347 FWUPD_ERROR_INVALID_FILE,
348 "failed to open %s",
349 values[0]);
350 return FALSE;
351 }
352 array = fu_engine_get_details (priv->engine, fd, error);
353 close (fd);
354
355 if (array == NULL)
356 return FALSE;
357 for (guint i = 0; i < array->len; i++) {
358 FwupdDevice *dev = g_ptr_array_index (array, i);
359 g_autofree gchar *tmp = NULL;
360 tmp = fwupd_device_to_string (dev);
361 g_print ("%s", tmp);
362 }
363 return TRUE;
364}
365
366static gboolean
Richard Hughes98ca9932018-05-18 10:24:07 +0100367fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
368{
369 g_autoptr(GPtrArray) devs = NULL;
370
371 /* load engine */
372 if (!fu_engine_load (priv->engine, error))
373 return FALSE;
374
375 /* print */
376 devs = fu_engine_get_devices (priv->engine, error);
377 if (devs == NULL)
378 return FALSE;
379 if (devs->len == 0) {
380 /* TRANSLATORS: nothing attached */
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500381 g_print ("%s\n", _("No hardware detected with firmware update capability"));
Richard Hughes98ca9932018-05-18 10:24:07 +0100382 return TRUE;
383 }
384 for (guint i = 0; i < devs->len; i++) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500385 g_autofree gchar *tmp = NULL;
Richard Hughes98ca9932018-05-18 10:24:07 +0100386 FwupdDevice *dev = g_ptr_array_index (devs, i);
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500387 if (!(fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE) || priv->show_all_devices))
388 continue;
389 tmp = fwupd_device_to_string (dev);
Richard Hughes98ca9932018-05-18 10:24:07 +0100390 g_print ("%s\n", tmp);
391 }
392
393 return TRUE;
394}
395
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500396static void
Richard Hughes0d1577e2018-05-29 13:59:06 +0100397fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev)
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500398{
399 for (guint i = 0; i < devs->len; i++) {
Richard Hughes0d1577e2018-05-29 13:59:06 +0100400 FuDevice *dev_tmp = g_ptr_array_index (devs, i);
401 if (!(fu_device_has_flag (dev_tmp, FWUPD_DEVICE_FLAG_UPDATABLE) || priv->show_all_devices))
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500402 continue;
Richard Hughes0d1577e2018-05-29 13:59:06 +0100403 if (fu_device_get_parent (dev_tmp) == dev) {
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500404 GNode *child = g_node_append_data (root, dev_tmp);
405 fu_util_build_device_tree (priv, child, devs, dev_tmp);
406 }
407 }
408}
409
410static gboolean
411fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
412{
413 g_autoptr(GNode) root = g_node_new (NULL);
414 g_autoptr(GPtrArray) devs = NULL;
415
416 /* load engine */
417 if (!fu_engine_load (priv->engine, error))
418 return FALSE;
419
420 /* print */
421 devs = fu_engine_get_devices (priv->engine, error);
422 if (devs == NULL)
423 return FALSE;
424
425 /* print */
426 if (devs->len == 0) {
427 /* TRANSLATORS: nothing attached that can be upgraded */
428 g_print ("%s\n", _("No hardware detected with firmware update capability"));
429 return TRUE;
430 }
431 fu_util_build_device_tree (priv, root, devs, NULL);
432 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
433 fu_util_print_device_tree, priv);
434
435 return TRUE;
436}
437
438
Richard Hughes98ca9932018-05-18 10:24:07 +0100439static FuDevice *
440fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
441{
442 FuDevice *dev;
443 guint idx;
444 g_autoptr(GPtrArray) devices = NULL;
445
446 /* get devices from daemon */
447 devices = fu_engine_get_devices (priv->engine, error);
448 if (devices == NULL)
449 return NULL;
450
451 /* exactly one */
452 if (devices->len == 1) {
453 dev = g_ptr_array_index (devices, 0);
454 return g_object_ref (dev);
455 }
456
457 /* TRANSLATORS: get interactive prompt */
458 g_print ("%s\n", _("Choose a device:"));
459 /* TRANSLATORS: this is to abort the interactive prompt */
460 g_print ("0.\t%s\n", _("Cancel"));
461 for (guint i = 0; i < devices->len; i++) {
462 dev = g_ptr_array_index (devices, i);
463 g_print ("%u.\t%s (%s)\n",
464 i + 1,
465 fu_device_get_id (dev),
466 fu_device_get_name (dev));
467 }
468 idx = fu_util_prompt_for_number (devices->len);
469 if (idx == 0) {
470 g_set_error_literal (error,
471 FWUPD_ERROR,
472 FWUPD_ERROR_NOTHING_TO_DO,
473 "Request canceled");
474 return NULL;
475 }
476 dev = g_ptr_array_index (devices, idx - 1);
477 return g_object_ref (dev);
478}
479
480static gboolean
481fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
482{
483 g_autoptr(FuDevice) device = NULL;
484 g_autoptr(GBytes) blob_fw = NULL;
485
486 /* invalid args */
487 if (g_strv_length (values) == 0) {
488 g_set_error_literal (error,
489 FWUPD_ERROR,
490 FWUPD_ERROR_INVALID_ARGS,
491 "Invalid arguments");
492 return FALSE;
493 }
494
495 /* parse blob */
496 blob_fw = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500497 if (blob_fw == NULL) {
498 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughes98ca9932018-05-18 10:24:07 +0100499 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500500 }
Richard Hughes98ca9932018-05-18 10:24:07 +0100501
502 /* load engine */
503 if (!fu_engine_load (priv->engine, error))
504 return FALSE;
505
506 /* get device */
507 if (g_strv_length (values) >= 2) {
508 device = fu_engine_get_device (priv->engine, values[1], error);
509 if (device == NULL)
510 return FALSE;
511 } else {
512 device = fu_util_prompt_for_device (priv, error);
513 if (device == NULL)
514 return FALSE;
515 }
516
517 /* write bare firmware */
518 return fu_engine_install_blob (priv->engine, device,
519 NULL, /* blob_cab */
520 blob_fw,
521 NULL, /* version */
Richard Hughes460226a2018-05-21 20:56:21 +0100522 priv->flags,
Richard Hughes98ca9932018-05-18 10:24:07 +0100523 error);
524}
525
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100526static gint
527fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b)
528{
529 FuInstallTask *task1 = *((FuInstallTask **) a);
530 FuInstallTask *task2 = *((FuInstallTask **) b);
531 return fu_install_task_compare (task1, task2);
532}
533
534static gboolean
535fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
536{
537 GPtrArray *apps;
538 g_autoptr(AsStore) store = NULL;
539 g_autoptr(GBytes) blob_cab = NULL;
540 g_autoptr(GPtrArray) devices_possible = NULL;
541 g_autoptr(GPtrArray) errors = NULL;
542 g_autoptr(GPtrArray) install_tasks = NULL;
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100543
Mario Limonciello8949e892018-05-25 08:03:06 -0500544 /* load engine */
545 if (!fu_engine_load (priv->engine, error))
546 return FALSE;
547
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100548 /* handle both forms */
549 if (g_strv_length (values) == 1) {
550 devices_possible = fu_engine_get_devices (priv->engine, error);
551 if (devices_possible == NULL)
552 return FALSE;
553 } else if (g_strv_length (values) == 2) {
554 FuDevice *device = fu_engine_get_device (priv->engine,
555 values[1],
556 error);
557 if (device == NULL)
558 return FALSE;
559 devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
560 g_ptr_array_add (devices_possible, device);
561 } else {
562 g_set_error_literal (error,
563 FWUPD_ERROR,
564 FWUPD_ERROR_INVALID_ARGS,
565 "Invalid arguments");
566 return FALSE;
567 }
568
569 /* parse store */
570 blob_cab = fu_common_get_contents_bytes (values[0], error);
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500571 if (blob_cab == NULL) {
572 fu_util_maybe_prefix_sandbox_error (values[0], error);
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100573 return FALSE;
Mario Limonciellob72aa8c2018-06-08 09:24:36 -0500574 }
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100575 store = fu_engine_get_store_from_blob (priv->engine, blob_cab, error);
576 if (store == NULL)
577 return FALSE;
578 apps = as_store_get_apps (store);
579
580 /* for each component in the store */
581 errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free);
582 install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
583 for (guint i = 0; i < apps->len; i++) {
584 AsApp *app = g_ptr_array_index (apps, i);
585
586 /* do any devices pass the requirements */
587 for (guint j = 0; j < devices_possible->len; j++) {
588 FuDevice *device = g_ptr_array_index (devices_possible, j);
589 g_autoptr(FuInstallTask) task = NULL;
590 g_autoptr(GError) error_local = NULL;
591
592 /* is this component valid for the device */
593 task = fu_install_task_new (device, app);
594 if (!fu_engine_check_requirements (priv->engine,
Richard Hughes460226a2018-05-21 20:56:21 +0100595 task, priv->flags,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100596 &error_local)) {
597 g_debug ("requirement on %s:%s failed: %s",
598 fu_device_get_id (device),
599 as_app_get_id (app),
600 error_local->message);
601 g_ptr_array_add (errors, g_steal_pointer (&error_local));
602 continue;
603 }
604
605 /* success */
606 g_ptr_array_add (install_tasks, g_steal_pointer (&task));
607 }
608 }
609
610 /* order the install tasks by the device priority */
611 g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb);
612
613 /* nothing suitable */
614 if (install_tasks->len == 0) {
615 GError *error_tmp = fu_common_error_array_get_best (errors);
616 g_propagate_error (error, error_tmp);
617 return FALSE;
618 }
619
620 /* install all the tasks */
621 for (guint i = 0; i < install_tasks->len; i++) {
622 FuInstallTask *task = g_ptr_array_index (install_tasks, i);
Richard Hughes460226a2018-05-21 20:56:21 +0100623 if (!fu_engine_install (priv->engine, task, blob_cab, priv->flags, error))
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100624 return FALSE;
625 }
626
627 /* success */
628 return TRUE;
629}
630
Richard Hughes98ca9932018-05-18 10:24:07 +0100631static gboolean
632fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
633{
634 g_autoptr(FuDevice) device = NULL;
635
636 /* load engine */
637 if (!fu_engine_load (priv->engine, error))
638 return FALSE;
639
640 /* invalid args */
641 if (g_strv_length (values) == 0) {
642 g_set_error_literal (error,
643 FWUPD_ERROR,
644 FWUPD_ERROR_INVALID_ARGS,
645 "Invalid arguments");
646 return FALSE;
647 }
648
649 /* get device */
650 if (g_strv_length (values) >= 1) {
651 device = fu_engine_get_device (priv->engine, values[0], error);
652 if (device == NULL)
653 return FALSE;
654 } else {
655 device = fu_util_prompt_for_device (priv, error);
656 if (device == NULL)
657 return FALSE;
658 }
659
660 /* run vfunc */
661 return fu_device_detach (device, error);
662}
663
664static gboolean
665fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
666{
667 g_autoptr(FuDevice) device = NULL;
668
669 /* load engine */
670 if (!fu_engine_load (priv->engine, error))
671 return FALSE;
672
673 /* invalid args */
674 if (g_strv_length (values) == 0) {
675 g_set_error_literal (error,
676 FWUPD_ERROR,
677 FWUPD_ERROR_INVALID_ARGS,
678 "Invalid arguments");
679 return FALSE;
680 }
681
682 /* get device */
683 if (g_strv_length (values) >= 1) {
684 device = fu_engine_get_device (priv->engine, values[0], error);
685 if (device == NULL)
686 return FALSE;
687 } else {
688 device = fu_util_prompt_for_device (priv, error);
689 if (device == NULL)
690 return FALSE;
691 }
692
693 /* run vfunc */
694 return fu_device_attach (device, error);
695}
696
Richard Hughesb5976832018-05-18 10:02:09 +0100697int
698main (int argc, char *argv[])
699{
Richard Hughes460226a2018-05-21 20:56:21 +0100700 gboolean allow_older = FALSE;
701 gboolean allow_reinstall = FALSE;
Richard Hughesb5976832018-05-18 10:02:09 +0100702 gboolean force = FALSE;
703 gboolean ret;
704 gboolean verbose = FALSE;
Richard Hughesc02ee4d2018-05-22 15:46:03 +0100705 g_auto(GStrv) plugin_glob = NULL;
Richard Hughesb5976832018-05-18 10:02:09 +0100706 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
707 g_autoptr(GError) error = NULL;
708 g_autofree gchar *cmd_descriptions = NULL;
709 const GOptionEntry options[] = {
710 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
711 /* TRANSLATORS: command line option */
712 _("Show extra debugging information"), NULL },
Richard Hughes460226a2018-05-21 20:56:21 +0100713 { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall,
714 /* TRANSLATORS: command line option */
715 _("Allow re-installing existing firmware versions"), NULL },
716 { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older,
717 /* TRANSLATORS: command line option */
718 _("Allow downgrading firmware versions"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +0100719 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
720 /* TRANSLATORS: command line option */
721 _("Override plugin warning"), NULL },
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500722 { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices,
723 /* TRANSLATORS: command line option */
724 _("Show devices that are not updatable"), NULL },
Richard Hughesc02ee4d2018-05-22 15:46:03 +0100725 { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob,
726 /* TRANSLATORS: command line option */
727 _("Manually whitelist specific plugins"), NULL },
Richard Hughesb5976832018-05-18 10:02:09 +0100728 { NULL}
729 };
730
731 setlocale (LC_ALL, "");
732
733 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
734 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
735 textdomain (GETTEXT_PACKAGE);
736
737 /* ensure root user */
Mario Limonciellob900c092018-05-22 14:22:21 -0500738 if (getuid () != 0 || geteuid () != 0)
Richard Hughesb5976832018-05-18 10:02:09 +0100739 /* TRANSLATORS: we're poking around as a power user */
Mario Limonciellob900c092018-05-22 14:22:21 -0500740 g_printerr ("%s\n", _("This program may only work correctly as root"));
Richard Hughesb5976832018-05-18 10:02:09 +0100741
742 /* create helper object */
743 priv->loop = g_main_loop_new (NULL, FALSE);
744 priv->progressbar = fu_progressbar_new ();
745
746 /* add commands */
747 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
748 fu_util_add (priv->cmd_array,
749 "smbios-dump",
750 "FILE",
751 /* TRANSLATORS: command description */
752 _("Dump SMBIOS data from a file"),
753 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +0100754 fu_util_add (priv->cmd_array,
Richard Hughes8c71a3f2018-05-22 19:19:52 +0100755 "get-plugins",
756 NULL,
757 /* TRANSLATORS: command description */
758 _("Get all enabled plugins registered with the system"),
759 fu_util_get_plugins);
760 fu_util_add (priv->cmd_array,
Mario Limonciello716ab272018-05-29 12:34:37 -0500761 "get-details",
762 NULL,
763 /* TRANSLATORS: command description */
764 _("Gets details about a firmware file"),
765 fu_util_get_details);
766 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100767 "get-devices",
768 NULL,
769 /* TRANSLATORS: command description */
770 _("Get all devices that support firmware updates"),
771 fu_util_get_devices);
772 fu_util_add (priv->cmd_array,
Mario Limoncielloba9e5b92018-05-21 16:02:32 -0500773 "get-topology",
774 NULL,
775 /* TRANSLATORS: command description */
776 _("Get all devices according to the system topology"),
777 fu_util_get_topology);
778 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100779 "watch",
780 NULL,
781 /* TRANSLATORS: command description */
Piotr Drąg472fa592018-06-06 14:53:48 +0200782 _("Watch for hardware changes"),
Richard Hughes98ca9932018-05-18 10:24:07 +0100783 fu_util_watch);
784 fu_util_add (priv->cmd_array,
785 "install-blob",
786 "FILENAME DEVICE-ID",
787 /* TRANSLATORS: command description */
788 _("Install a firmware blob on a device"),
789 fu_util_install_blob);
790 fu_util_add (priv->cmd_array,
Richard Hughesa36c9cf2018-05-20 10:41:44 +0100791 "install",
792 "FILE [ID]",
793 /* TRANSLATORS: command description */
794 _("Install a firmware file on this hardware"),
795 fu_util_install);
796 fu_util_add (priv->cmd_array,
Richard Hughes98ca9932018-05-18 10:24:07 +0100797 "attach",
798 "DEVICE-ID",
799 /* TRANSLATORS: command description */
800 _("Attach to firmware mode"),
801 fu_util_attach);
802 fu_util_add (priv->cmd_array,
803 "detach",
804 "DEVICE-ID",
805 /* TRANSLATORS: command description */
806 _("Detach to bootloader mode"),
807 fu_util_detach);
Richard Hughesb5976832018-05-18 10:02:09 +0100808
809 /* do stuff on ctrl+c */
810 priv->cancellable = g_cancellable_new ();
811 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
812 SIGINT, fu_util_sigint_cb,
813 priv, NULL);
814 g_signal_connect (priv->cancellable, "cancelled",
815 G_CALLBACK (fu_util_cancelled_cb), priv);
816
817 /* sort by command name */
818 g_ptr_array_sort (priv->cmd_array,
819 (GCompareFunc) fu_sort_command_name_cb);
820
821 /* get a list of the commands */
822 priv->context = g_option_context_new (NULL);
823 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
824 g_option_context_set_summary (priv->context, cmd_descriptions);
825 g_option_context_set_description (priv->context,
826 "This tool allows an administrator to use the fwupd plugins "
827 "without being installed on the host system.");
828
829 /* TRANSLATORS: program name */
830 g_set_application_name (_("Firmware Utility"));
831 g_option_context_add_main_entries (priv->context, options, NULL);
832 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
833 if (!ret) {
834 /* TRANSLATORS: the user didn't read the man page */
835 g_print ("%s: %s\n", _("Failed to parse arguments"),
836 error->message);
837 return EXIT_FAILURE;
838 }
839
840 /* set verbose? */
841 if (verbose) {
842 g_setenv ("G_MESSAGES_DEBUG", "all", FALSE);
843 } else {
844 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
845 fu_util_ignore_cb, NULL);
846 }
847
Richard Hughes460226a2018-05-21 20:56:21 +0100848 /* set flags */
849 priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY;
850 if (allow_reinstall)
851 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL;
852 if (allow_older)
853 priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER;
854 if (force)
855 priv->flags |= FWUPD_INSTALL_FLAG_FORCE;
856
Richard Hughes98ca9932018-05-18 10:24:07 +0100857 /* load engine */
858 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
859 g_signal_connect (priv->engine, "device-added",
860 G_CALLBACK (fu_main_engine_device_added_cb),
861 priv);
862 g_signal_connect (priv->engine, "device-removed",
863 G_CALLBACK (fu_main_engine_device_removed_cb),
864 priv);
865 g_signal_connect (priv->engine, "device-changed",
866 G_CALLBACK (fu_main_engine_device_changed_cb),
867 priv);
868 g_signal_connect (priv->engine, "status-changed",
869 G_CALLBACK (fu_main_engine_status_changed_cb),
870 priv);
871 g_signal_connect (priv->engine, "percentage-changed",
872 G_CALLBACK (fu_main_engine_percentage_changed_cb),
873 priv);
874
Richard Hughesc02ee4d2018-05-22 15:46:03 +0100875 /* any plugin whitelist specified */
876 for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++)
877 fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]);
878
Richard Hughesb5976832018-05-18 10:02:09 +0100879 /* run the specified command */
880 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
881 if (!ret) {
882 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
883 g_autofree gchar *tmp = NULL;
884 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
885 g_print ("%s\n\n%s", error->message, tmp);
886 return EXIT_FAILURE;
887 }
888 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
889 g_print ("%s\n", error->message);
890 return EXIT_NOTHING_TO_DO;
891 }
892 g_print ("%s\n", error->message);
893 return EXIT_FAILURE;
894 }
895
896 /* success */
897 return EXIT_SUCCESS;
898}