blob: 039abe6fcf45942a844fbf0c07dd27773572a726 [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 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "config.h"
23
24#include <fwupd.h>
25#include <glib/gi18n.h>
26#include <glib-unix.h>
27#include <locale.h>
28#include <stdlib.h>
29#include <unistd.h>
30
Richard Hughes98ca9932018-05-18 10:24:07 +010031#include "fu-engine.h"
Richard Hughesb5976832018-05-18 10:02:09 +010032#include "fu-progressbar.h"
33#include "fu-smbios.h"
34#include "fu-util-common.h"
35
36/* this is only valid in this file */
37#define FWUPD_ERROR_INVALID_ARGS (FWUPD_ERROR_LAST+1)
38
39/* custom return code */
40#define EXIT_NOTHING_TO_DO 2
41
42typedef struct {
43 GCancellable *cancellable;
44 GMainLoop *loop;
45 GOptionContext *context;
46 GPtrArray *cmd_array;
Richard Hughes98ca9932018-05-18 10:24:07 +010047 FuEngine *engine;
Richard Hughesb5976832018-05-18 10:02:09 +010048 FuProgressbar *progressbar;
49} FuUtilPrivate;
50
51typedef gboolean (*FuUtilPrivateCb) (FuUtilPrivate *util,
52 gchar **values,
53 GError **error);
54
55typedef struct {
56 gchar *name;
57 gchar *arguments;
58 gchar *description;
59 FuUtilPrivateCb callback;
60} FuUtilItem;
61
62static void
63fu_util_item_free (FuUtilItem *item)
64{
65 g_free (item->name);
66 g_free (item->arguments);
67 g_free (item->description);
68 g_free (item);
69}
70
71static gint
72fu_sort_command_name_cb (FuUtilItem **item1, FuUtilItem **item2)
73{
74 return g_strcmp0 ((*item1)->name, (*item2)->name);
75}
76
77static void
78fu_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
287static gboolean
288fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
289{
290 g_autoptr(GPtrArray) devs = NULL;
291
292 /* load engine */
293 if (!fu_engine_load (priv->engine, error))
294 return FALSE;
295
296 /* print */
297 devs = fu_engine_get_devices (priv->engine, error);
298 if (devs == NULL)
299 return FALSE;
300 if (devs->len == 0) {
301 /* TRANSLATORS: nothing attached */
302 g_print ("%s\n", _("No hardware detected"));
303 return TRUE;
304 }
305 for (guint i = 0; i < devs->len; i++) {
306 FwupdDevice *dev = g_ptr_array_index (devs, i);
307 g_autofree gchar *tmp = fwupd_device_to_string (dev);
308 g_print ("%s\n", tmp);
309 }
310
311 return TRUE;
312}
313
314static FuDevice *
315fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
316{
317 FuDevice *dev;
318 guint idx;
319 g_autoptr(GPtrArray) devices = NULL;
320
321 /* get devices from daemon */
322 devices = fu_engine_get_devices (priv->engine, error);
323 if (devices == NULL)
324 return NULL;
325
326 /* exactly one */
327 if (devices->len == 1) {
328 dev = g_ptr_array_index (devices, 0);
329 return g_object_ref (dev);
330 }
331
332 /* TRANSLATORS: get interactive prompt */
333 g_print ("%s\n", _("Choose a device:"));
334 /* TRANSLATORS: this is to abort the interactive prompt */
335 g_print ("0.\t%s\n", _("Cancel"));
336 for (guint i = 0; i < devices->len; i++) {
337 dev = g_ptr_array_index (devices, i);
338 g_print ("%u.\t%s (%s)\n",
339 i + 1,
340 fu_device_get_id (dev),
341 fu_device_get_name (dev));
342 }
343 idx = fu_util_prompt_for_number (devices->len);
344 if (idx == 0) {
345 g_set_error_literal (error,
346 FWUPD_ERROR,
347 FWUPD_ERROR_NOTHING_TO_DO,
348 "Request canceled");
349 return NULL;
350 }
351 dev = g_ptr_array_index (devices, idx - 1);
352 return g_object_ref (dev);
353}
354
355static gboolean
356fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error)
357{
358 g_autoptr(FuDevice) device = NULL;
359 g_autoptr(GBytes) blob_fw = NULL;
360
361 /* invalid args */
362 if (g_strv_length (values) == 0) {
363 g_set_error_literal (error,
364 FWUPD_ERROR,
365 FWUPD_ERROR_INVALID_ARGS,
366 "Invalid arguments");
367 return FALSE;
368 }
369
370 /* parse blob */
371 blob_fw = fu_common_get_contents_bytes (values[0], error);
372 if (blob_fw == NULL)
373 return FALSE;
374
375 /* load engine */
376 if (!fu_engine_load (priv->engine, error))
377 return FALSE;
378
379 /* get device */
380 if (g_strv_length (values) >= 2) {
381 device = fu_engine_get_device (priv->engine, values[1], error);
382 if (device == NULL)
383 return FALSE;
384 } else {
385 device = fu_util_prompt_for_device (priv, error);
386 if (device == NULL)
387 return FALSE;
388 }
389
390 /* write bare firmware */
391 return fu_engine_install_blob (priv->engine, device,
392 NULL, /* blob_cab */
393 blob_fw,
394 NULL, /* version */
395 FWUPD_INSTALL_FLAG_ALLOW_OLDER |
396 FWUPD_INSTALL_FLAG_ALLOW_REINSTALL |
397 FWUPD_INSTALL_FLAG_NO_HISTORY,
398 error);
399}
400
401static gboolean
402fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error)
403{
404 g_autoptr(FuDevice) device = NULL;
405
406 /* load engine */
407 if (!fu_engine_load (priv->engine, error))
408 return FALSE;
409
410 /* invalid args */
411 if (g_strv_length (values) == 0) {
412 g_set_error_literal (error,
413 FWUPD_ERROR,
414 FWUPD_ERROR_INVALID_ARGS,
415 "Invalid arguments");
416 return FALSE;
417 }
418
419 /* get device */
420 if (g_strv_length (values) >= 1) {
421 device = fu_engine_get_device (priv->engine, values[0], error);
422 if (device == NULL)
423 return FALSE;
424 } else {
425 device = fu_util_prompt_for_device (priv, error);
426 if (device == NULL)
427 return FALSE;
428 }
429
430 /* run vfunc */
431 return fu_device_detach (device, error);
432}
433
434static gboolean
435fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error)
436{
437 g_autoptr(FuDevice) device = NULL;
438
439 /* load engine */
440 if (!fu_engine_load (priv->engine, error))
441 return FALSE;
442
443 /* invalid args */
444 if (g_strv_length (values) == 0) {
445 g_set_error_literal (error,
446 FWUPD_ERROR,
447 FWUPD_ERROR_INVALID_ARGS,
448 "Invalid arguments");
449 return FALSE;
450 }
451
452 /* get device */
453 if (g_strv_length (values) >= 1) {
454 device = fu_engine_get_device (priv->engine, values[0], error);
455 if (device == NULL)
456 return FALSE;
457 } else {
458 device = fu_util_prompt_for_device (priv, error);
459 if (device == NULL)
460 return FALSE;
461 }
462
463 /* run vfunc */
464 return fu_device_attach (device, error);
465}
466
Richard Hughesb5976832018-05-18 10:02:09 +0100467int
468main (int argc, char *argv[])
469{
470 gboolean force = FALSE;
471 gboolean ret;
472 gboolean verbose = FALSE;
473 g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1);
474 g_autoptr(GError) error = NULL;
475 g_autofree gchar *cmd_descriptions = NULL;
476 const GOptionEntry options[] = {
477 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
478 /* TRANSLATORS: command line option */
479 _("Show extra debugging information"), NULL },
480 { "force", '\0', 0, G_OPTION_ARG_NONE, &force,
481 /* TRANSLATORS: command line option */
482 _("Override plugin warning"), NULL },
483 { NULL}
484 };
485
486 setlocale (LC_ALL, "");
487
488 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
489 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
490 textdomain (GETTEXT_PACKAGE);
491
492 /* ensure root user */
493 if (getuid () != 0 || geteuid () != 0) {
494 /* TRANSLATORS: we're poking around as a power user */
495 g_print ("%s\n", _("This program can only be used when root"));
496 return EXIT_FAILURE;
497 }
498
499 /* create helper object */
500 priv->loop = g_main_loop_new (NULL, FALSE);
501 priv->progressbar = fu_progressbar_new ();
502
503 /* add commands */
504 priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_util_item_free);
505 fu_util_add (priv->cmd_array,
506 "smbios-dump",
507 "FILE",
508 /* TRANSLATORS: command description */
509 _("Dump SMBIOS data from a file"),
510 fu_util_smbios_dump);
Richard Hughes98ca9932018-05-18 10:24:07 +0100511 fu_util_add (priv->cmd_array,
512 "get-devices",
513 NULL,
514 /* TRANSLATORS: command description */
515 _("Get all devices that support firmware updates"),
516 fu_util_get_devices);
517 fu_util_add (priv->cmd_array,
518 "watch",
519 NULL,
520 /* TRANSLATORS: command description */
521 _("Watch for hardare changes"),
522 fu_util_watch);
523 fu_util_add (priv->cmd_array,
524 "install-blob",
525 "FILENAME DEVICE-ID",
526 /* TRANSLATORS: command description */
527 _("Install a firmware blob on a device"),
528 fu_util_install_blob);
529 fu_util_add (priv->cmd_array,
530 "attach",
531 "DEVICE-ID",
532 /* TRANSLATORS: command description */
533 _("Attach to firmware mode"),
534 fu_util_attach);
535 fu_util_add (priv->cmd_array,
536 "detach",
537 "DEVICE-ID",
538 /* TRANSLATORS: command description */
539 _("Detach to bootloader mode"),
540 fu_util_detach);
Richard Hughesb5976832018-05-18 10:02:09 +0100541
542 /* do stuff on ctrl+c */
543 priv->cancellable = g_cancellable_new ();
544 g_unix_signal_add_full (G_PRIORITY_DEFAULT,
545 SIGINT, fu_util_sigint_cb,
546 priv, NULL);
547 g_signal_connect (priv->cancellable, "cancelled",
548 G_CALLBACK (fu_util_cancelled_cb), priv);
549
550 /* sort by command name */
551 g_ptr_array_sort (priv->cmd_array,
552 (GCompareFunc) fu_sort_command_name_cb);
553
554 /* get a list of the commands */
555 priv->context = g_option_context_new (NULL);
556 cmd_descriptions = fu_util_get_descriptions (priv->cmd_array);
557 g_option_context_set_summary (priv->context, cmd_descriptions);
558 g_option_context_set_description (priv->context,
559 "This tool allows an administrator to use the fwupd plugins "
560 "without being installed on the host system.");
561
562 /* TRANSLATORS: program name */
563 g_set_application_name (_("Firmware Utility"));
564 g_option_context_add_main_entries (priv->context, options, NULL);
565 ret = g_option_context_parse (priv->context, &argc, &argv, &error);
566 if (!ret) {
567 /* TRANSLATORS: the user didn't read the man page */
568 g_print ("%s: %s\n", _("Failed to parse arguments"),
569 error->message);
570 return EXIT_FAILURE;
571 }
572
573 /* set verbose? */
574 if (verbose) {
575 g_setenv ("G_MESSAGES_DEBUG", "all", FALSE);
576 } else {
577 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
578 fu_util_ignore_cb, NULL);
579 }
580
Richard Hughes98ca9932018-05-18 10:24:07 +0100581 /* load engine */
582 priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES);
583 g_signal_connect (priv->engine, "device-added",
584 G_CALLBACK (fu_main_engine_device_added_cb),
585 priv);
586 g_signal_connect (priv->engine, "device-removed",
587 G_CALLBACK (fu_main_engine_device_removed_cb),
588 priv);
589 g_signal_connect (priv->engine, "device-changed",
590 G_CALLBACK (fu_main_engine_device_changed_cb),
591 priv);
592 g_signal_connect (priv->engine, "status-changed",
593 G_CALLBACK (fu_main_engine_status_changed_cb),
594 priv);
595 g_signal_connect (priv->engine, "percentage-changed",
596 G_CALLBACK (fu_main_engine_percentage_changed_cb),
597 priv);
598
Richard Hughesb5976832018-05-18 10:02:09 +0100599 /* run the specified command */
600 ret = fu_util_run (priv, argv[1], (gchar**) &argv[2], &error);
601 if (!ret) {
602 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) {
603 g_autofree gchar *tmp = NULL;
604 tmp = g_option_context_get_help (priv->context, TRUE, NULL);
605 g_print ("%s\n\n%s", error->message, tmp);
606 return EXIT_FAILURE;
607 }
608 if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
609 g_print ("%s\n", error->message);
610 return EXIT_NOTHING_TO_DO;
611 }
612 g_print ("%s\n", error->message);
613 return EXIT_FAILURE;
614 }
615
616 /* success */
617 return EXIT_SUCCESS;
618}