/
fu-util.c
2453 lines (2190 loc) · 71.9 KB
/
fu-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#define G_LOG_DOMAIN "FuMain"
#include "config.h"
#include <fwupd.h>
#include <xmlb.h>
#include <fcntl.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <glib/gi18n.h>
#include <glib-unix.h>
#include <gudev/gudev.h>
#include <json-glib/json-glib.h>
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include "fu-history.h"
#include "fu-plugin-private.h"
#include "fu-progressbar.h"
#include "fu-util-common.h"
#include "fwupd-common-private.h"
#ifdef HAVE_SYSTEMD
#include "fu-systemd.h"
#endif
/* custom return code */
#define EXIT_NOTHING_TO_DO 2
typedef enum {
FU_UTIL_OPERATION_UNKNOWN,
FU_UTIL_OPERATION_UPDATE,
FU_UTIL_OPERATION_DOWNGRADE,
FU_UTIL_OPERATION_INSTALL,
FU_UTIL_OPERATION_LAST
} FuUtilOperation;
struct FuUtilPrivate {
GCancellable *cancellable;
GMainLoop *loop;
GOptionContext *context;
SoupSession *soup_session;
FwupdInstallFlags flags;
FwupdClient *client;
FuProgressbar *progressbar;
gboolean no_metadata_check;
gboolean no_reboot_check;
gboolean no_unreported_check;
gboolean assume_yes;
gboolean sign;
gboolean show_all_devices;
gboolean disable_ssl_strict;
/* only valid in update and downgrade */
FuUtilOperation current_operation;
FwupdDevice *current_device;
gchar *current_message;
FwupdDeviceFlags completion_flags;
FwupdDeviceFlags filter_include;
FwupdDeviceFlags filter_exclude;
};
static GFileOutputStream *log_output = NULL;
static gboolean fu_util_report_history (FuUtilPrivate *priv, gchar **values, GError **error);
static gboolean fu_util_download_file (FuUtilPrivate *priv,
SoupURI *uri,
const gchar *fn,
const gchar *checksum_expected,
GError **error);
static void
fu_util_client_notify_cb (GObject *object,
GParamSpec *pspec,
FuUtilPrivate *priv)
{
fu_progressbar_update (priv->progressbar,
fwupd_client_get_status (priv->client),
fwupd_client_get_percentage (priv->client));
}
static void
fu_util_update_device_changed_cb (FwupdClient *client,
FwupdDevice *device,
FuUtilPrivate *priv)
{
g_autofree gchar *str = NULL;
/* allowed to set whenever the device has changed */
if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN))
priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN;
if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT))
priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT;
/* same as last time, so ignore */
if (priv->current_device != NULL &&
fwupd_device_compare (priv->current_device, device) == 0)
return;
/* show message in progressbar */
if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) {
/* TRANSLATORS: %1 is a device name */
str = g_strdup_printf (_("Updating %s…"),
fwupd_device_get_name (device));
fu_progressbar_set_title (priv->progressbar, str);
} else if (priv->current_operation == FU_UTIL_OPERATION_DOWNGRADE) {
/* TRANSLATORS: %1 is a device name */
str = g_strdup_printf (_("Downgrading %s…"),
fwupd_device_get_name (device));
fu_progressbar_set_title (priv->progressbar, str);
} else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) {
/* TRANSLATORS: %1 is a device name */
str = g_strdup_printf (_("Installing on %s…"),
fwupd_device_get_name (device));
fu_progressbar_set_title (priv->progressbar, str);
} else {
g_warning ("no FuUtilOperation set");
}
g_set_object (&priv->current_device, device);
if (priv->current_message == NULL) {
const gchar *tmp = fwupd_device_get_update_message (priv->current_device);
if (tmp != NULL)
priv->current_message = g_strdup (tmp);
}
}
static gboolean
fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev)
{
if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) {
if (!fwupd_device_has_flag (dev, priv->filter_include))
return FALSE;
}
if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) {
if (fwupd_device_has_flag (dev, priv->filter_exclude))
return FALSE;
}
return TRUE;
}
static FwupdDevice *
fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error)
{
FwupdDevice *dev;
guint idx;
g_autoptr(GPtrArray) devices = NULL;
g_autoptr(GPtrArray) devices_filtered = NULL;
/* get devices from daemon */
devices = fwupd_client_get_devices (priv->client, NULL, error);
if (devices == NULL)
return NULL;
/* filter results */
devices_filtered = g_ptr_array_new ();
for (guint i = 0; i < devices->len; i++) {
dev = g_ptr_array_index (devices, i);
if (!fu_util_filter_device (priv, dev))
continue;
if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
continue;
g_ptr_array_add (devices_filtered, dev);
}
/* nothing */
if (devices_filtered->len == 0) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOTHING_TO_DO,
"No supported devices");
return NULL;
}
/* exactly one */
if (devices_filtered->len == 1) {
dev = g_ptr_array_index (devices_filtered, 0);
return g_object_ref (dev);
}
/* TRANSLATORS: get interactive prompt */
g_print ("%s\n", _("Choose a device:"));
/* TRANSLATORS: this is to abort the interactive prompt */
g_print ("0.\t%s\n", _("Cancel"));
for (guint i = 0; i < devices_filtered->len; i++) {
dev = g_ptr_array_index (devices_filtered, i);
g_print ("%u.\t%s (%s)\n",
i + 1,
fwupd_device_get_id (dev),
fwupd_device_get_name (dev));
}
idx = fu_util_prompt_for_number (devices_filtered->len);
if (idx == 0) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOTHING_TO_DO,
"Request canceled");
return NULL;
}
dev = g_ptr_array_index (devices_filtered, idx - 1);
return g_object_ref (dev);
}
static gboolean
fu_util_perhaps_show_unreported (FuUtilPrivate *priv, GError **error)
{
g_autoptr(GError) error_local = NULL;
g_autoptr(GPtrArray) devices = NULL;
g_autoptr(GPtrArray) devices_failed = g_ptr_array_new ();
g_autoptr(GPtrArray) devices_success = g_ptr_array_new ();
/* we don't want to ask anything */
if (priv->no_unreported_check) {
g_debug ("skipping unreported check");
return TRUE;
}
/* get all devices from the history database */
devices = fwupd_client_get_history (priv->client, NULL, &error_local);
if (devices == NULL) {
if (g_error_matches (error_local, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO))
return TRUE;
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
for (guint i = 0; i < devices->len; i++) {
FwupdDevice *dev = g_ptr_array_index (devices, i);
if (!fu_util_filter_device (priv, dev))
continue;
if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_REPORTED))
continue;
if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
continue;
switch (fwupd_device_get_update_state (dev)) {
case FWUPD_UPDATE_STATE_FAILED:
g_ptr_array_add (devices_failed, dev);
break;
case FWUPD_UPDATE_STATE_SUCCESS:
g_ptr_array_add (devices_success, dev);
break;
default:
break;
}
}
/* nothing to do */
if (devices_failed->len == 0 && devices_success->len == 0) {
g_debug ("no unreported devices");
return TRUE;
}
/* show the success and failures */
if (!priv->assume_yes) {
/* delimit */
g_print ("________________________________________________\n");
/* failures */
if (devices_failed->len > 0) {
/* TRANSLATORS: a list of failed updates */
g_print ("\n%s\n\n", _("Devices that were not updated correctly:"));
for (guint i = 0; i < devices_failed->len; i++) {
FwupdDevice *dev = g_ptr_array_index (devices_failed, i);
FwupdRelease *rel = fwupd_device_get_release_default (dev);
g_print (" • %s (%s → %s)\n",
fwupd_device_get_name (dev),
fwupd_device_get_version (dev),
fwupd_release_get_version (rel));
}
}
/* success */
if (devices_success->len > 0) {
/* TRANSLATORS: a list of successful updates */
g_print ("\n%s\n\n", _("Devices that have been updated successfully:"));
for (guint i = 0; i < devices_success->len; i++) {
FwupdDevice *dev = g_ptr_array_index (devices_success, i);
FwupdRelease *rel = fwupd_device_get_release_default (dev);
g_print (" • %s (%s → %s)\n",
fwupd_device_get_name (dev),
fwupd_device_get_version (dev),
fwupd_release_get_version (rel));
}
}
/* ask for permission */
g_print ("\n%s\n%s (%s) [Y|n]: ",
/* TRANSLATORS: explain why we want to upload */
_("Uploading firmware reports helps hardware vendors"
" to quickly identify failing and successful updates"
" on real devices."),
/* TRANSLATORS: ask the user to upload */
_("Upload report now?"),
/* TRANSLATORS: metadata is downloaded from the Internet */
_("Requires internet connection"));
if (!fu_util_prompt_for_boolean (TRUE))
return TRUE;
}
/* success */
return fu_util_report_history (priv, NULL, error);
}
static gboolean
fu_util_modify_remote_warning (FuUtilPrivate *priv, FwupdRemote *remote, GError **error)
{
const gchar *warning_markup = NULL;
g_autofree gchar *warning_plain = NULL;
/* get formatted text */
warning_markup = fwupd_remote_get_agreement (remote);
if (warning_markup == NULL)
return TRUE;
warning_plain = fu_util_convert_description (warning_markup, error);
if (warning_plain == NULL)
return FALSE;
/* show and ask user to confirm */
fu_util_warning_box (warning_plain, 80);
if (!priv->assume_yes) {
/* ask for permission */
g_print ("\n%s [Y|n]: ",
/* TRANSLATORS: should the remote still be enabled */
_("Agree and enable the remote?"));
if (!fu_util_prompt_for_boolean (TRUE)) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOTHING_TO_DO,
"Declined agreement");
return FALSE;
}
}
return TRUE;
}
static gboolean
fu_util_modify_remote (FuUtilPrivate *priv,
const gchar *remote_id,
const gchar *key,
const gchar *value,
GError **error)
{
g_autoptr(FwupdRemote) remote = NULL;
/* ensure the remote exists */
remote = fwupd_client_get_remote_by_id (priv->client, remote_id, NULL, error);
if (remote == NULL)
return FALSE;
/* show some kind of warning when enabling download-type remotes */
if (g_strcmp0 (key, "Enabled") == 0 && g_strcmp0 (value, "true") == 0) {
if (!fu_util_modify_remote_warning (priv, remote, error))
return FALSE;
}
return fwupd_client_modify_remote (priv->client,
remote_id, key, value,
NULL, error);
}
static void
fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FwupdDevice *dev)
{
for (guint i = 0; i < devs->len; i++) {
FwupdDevice *dev_tmp = g_ptr_array_index (devs, i);
if (!fu_util_filter_device (priv, dev_tmp))
continue;
if (!priv->show_all_devices &&
!fu_util_is_interesting_device (dev_tmp))
continue;
if (fwupd_device_get_parent (dev_tmp) == dev) {
FwupdRelease *rel = fwupd_device_get_release_default (dev_tmp);
GNode *child = g_node_append_data (root, dev_tmp);
if (rel != NULL)
g_node_append_data (child, rel);
fu_util_build_device_tree (priv, child, devs, dev_tmp);
}
}
}
static gchar *
fu_util_get_tree_title (FuUtilPrivate *priv)
{
return g_strdup (fwupd_client_get_host_product (priv->client));
}
static gboolean
fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(GNode) root = g_node_new (NULL);
g_autoptr(GPtrArray) devs = NULL;
g_autofree gchar *title = fu_util_get_tree_title (priv);
/* get results from daemon */
devs = fwupd_client_get_devices (priv->client, NULL, error);
if (devs == NULL)
return FALSE;
/* print */
if (devs->len == 0) {
/* TRANSLATORS: nothing attached that can be upgraded */
g_print ("%s\n", _("No hardware detected with firmware update capability"));
return TRUE;
}
fu_util_build_device_tree (priv, root, devs, NULL);
fu_util_print_tree (root, title);
/* nag? */
if (!fu_util_perhaps_show_unreported (priv, error))
return FALSE;
return TRUE;
}
static gchar *
fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error)
{
g_autofree gchar *filename = NULL;
g_autoptr(SoupURI) uri = NULL;
/* a local file */
uri = soup_uri_new (perhapsfn);
if (uri == NULL)
return g_strdup (perhapsfn);
/* download the firmware to a cachedir */
filename = fu_util_get_user_cache_path (perhapsfn);
if (!fu_common_mkdir_parent (filename, error))
return NULL;
if (!fu_util_download_file (priv, uri, filename, NULL, error))
return NULL;
return g_steal_pointer (&filename);
}
static void
fu_util_display_current_message (FuUtilPrivate *priv)
{
if (priv->current_message == NULL)
return;
g_print ("%s\n", priv->current_message);
g_clear_pointer (&priv->current_message, g_free);
}
static gboolean
fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error)
{
const gchar *id;
g_autofree gchar *filename = NULL;
/* handle both forms */
if (g_strv_length (values) == 1) {
id = FWUPD_DEVICE_ID_ANY;
} else if (g_strv_length (values) == 2) {
id = values[1];
} else {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_ARGS,
"Invalid arguments");
return FALSE;
}
priv->current_operation = FU_UTIL_OPERATION_INSTALL;
g_signal_connect (priv->client, "device-changed",
G_CALLBACK (fu_util_update_device_changed_cb), priv);
/* install with flags chosen by the user */
filename = fu_util_download_if_required (priv, values[0], error);
if (filename == NULL)
return FALSE;
if (!fwupd_client_install (priv->client, id, filename, priv->flags, NULL, error))
return FALSE;
fu_util_display_current_message (priv);
/* we don't want to ask anything */
if (priv->no_reboot_check) {
g_debug ("skipping reboot check");
return TRUE;
}
/* show reboot if needed */
return fu_util_prompt_complete (priv->completion_flags, TRUE, error);
}
static gboolean
fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(GPtrArray) array = NULL;
g_autoptr(GNode) root = g_node_new (NULL);
g_autofree gchar *title = fu_util_get_tree_title (priv);
/* check args */
if (g_strv_length (values) != 1) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_ARGS,
"Invalid arguments");
return FALSE;
}
array = fwupd_client_get_details (priv->client, values[0], NULL, error);
if (array == NULL)
return FALSE;
fu_util_build_device_tree (priv, root, array, NULL);
fu_util_print_tree (root, title);
return TRUE;
}
static gboolean
fu_util_clear_history (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(FuHistory) history = fu_history_new ();
return fu_history_remove_all (history, error);
}
static gboolean
fu_util_report_history_for_uri (FuUtilPrivate *priv,
const gchar *report_uri,
GPtrArray *devices,
GError **error)
{
JsonNode *json_root;
JsonObject *json_object;
const gchar *server_msg = NULL;
guint status_code;
g_autofree gchar *data = NULL;
g_autofree gchar *sig = NULL;
g_autoptr(JsonParser) json_parser = NULL;
g_autoptr(SoupMessage) msg = NULL;
/* convert to JSON */
data = fwupd_build_history_report_json (devices, error);
if (data == NULL)
return FALSE;
/* self sign data */
if (priv->sign) {
sig = fwupd_client_self_sign (priv->client, data,
FWUPD_SELF_SIGN_FLAG_ADD_TIMESTAMP,
priv->cancellable, error);
if (sig == NULL)
return FALSE;
}
/* ask for permission */
if (!priv->assume_yes) {
fu_util_print_data (_("Target"), report_uri);
fu_util_print_data (_("Payload"), data);
if (sig != NULL)
fu_util_print_data (_("Signature"), sig);
g_print ("%s [Y|n]: ", _("Proceed with upload?"));
if (!fu_util_prompt_for_boolean (TRUE)) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_PERMISSION_DENIED,
"User declined action");
return FALSE;
}
}
/* POST request */
if (sig != NULL) {
g_autoptr(SoupMultipart) mp = NULL;
mp = soup_multipart_new (SOUP_FORM_MIME_TYPE_MULTIPART);
soup_multipart_append_form_string (mp, "payload", data);
soup_multipart_append_form_string (mp, "signature", sig);
msg = soup_form_request_new_from_multipart (report_uri, mp);
} else {
msg = soup_message_new (SOUP_METHOD_POST, report_uri);
soup_message_set_request (msg, "application/json; charset=utf-8",
SOUP_MEMORY_COPY, data, strlen (data));
}
status_code = soup_session_send_message (priv->soup_session, msg);
g_debug ("server returned: %s", msg->response_body->data);
/* server returned nothing, and probably exploded in a ball of flames */
if (msg->response_body->length == 0) {
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Failed to upload to %s: %s",
report_uri, soup_status_get_phrase (status_code));
return FALSE;
}
/* parse JSON reply */
json_parser = json_parser_new ();
if (!json_parser_load_from_data (json_parser,
msg->response_body->data,
msg->response_body->length,
error)) {
g_autofree gchar *str = g_strndup (msg->response_body->data,
msg->response_body->length);
g_prefix_error (error, "Failed to parse JSON response from '%s': ", str);
return FALSE;
}
json_root = json_parser_get_root (json_parser);
if (json_root == NULL) {
g_autofree gchar *str = g_strndup (msg->response_body->data,
msg->response_body->length);
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_PERMISSION_DENIED,
"JSON response was malformed: '%s'", str);
return FALSE;
}
json_object = json_node_get_object (json_root);
if (json_object == NULL) {
g_autofree gchar *str = g_strndup (msg->response_body->data,
msg->response_body->length);
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_PERMISSION_DENIED,
"JSON response object was malformed: '%s'", str);
return FALSE;
}
/* get any optional server message */
if (json_object_has_member (json_object, "msg"))
server_msg = json_object_get_string_member (json_object, "msg");
/* server reported failed */
if (!json_object_get_boolean_member (json_object, "success")) {
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_PERMISSION_DENIED,
"Server rejected report: %s",
server_msg != NULL ? server_msg : "unspecified");
return FALSE;
}
/* server wanted us to see the message */
if (server_msg != NULL) {
if (g_strstr_len (server_msg, -1, "known issue") != NULL &&
json_object_has_member (json_object, "uri")) {
g_print ("%s %s\n",
/* TRANSLATORS: the server sent the user a small message */
_("Update failure is a known issue, visit this URL for more information:"),
json_object_get_string_member (json_object, "uri"));
} else {
/* TRANSLATORS: the server sent the user a small message */
g_print ("%s %s\n", _("Upload message:"), server_msg);
}
}
/* fall back to HTTP status codes in case the server is offline */
if (!SOUP_STATUS_IS_SUCCESSFUL (status_code)) {
g_set_error (error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Failed to upload to %s: %s",
report_uri, soup_status_get_phrase (status_code));
return FALSE;
}
return TRUE;
}
static gboolean
fu_util_report_history (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(GHashTable) remote_id_uri_map = NULL;
g_autoptr(GHashTable) report_map = NULL;
g_autoptr(GList) uris = NULL;
g_autoptr(GPtrArray) devices = NULL;
g_autoptr(GPtrArray) remotes = NULL;
/* set up networking */
if (priv->soup_session == NULL) {
priv->soup_session = fu_util_setup_networking (error);
if (priv->soup_session == NULL)
return FALSE;
}
/* create a map of RemoteID to RemoteURI */
remotes = fwupd_client_get_remotes (priv->client, NULL, error);
if (remotes == NULL)
return FALSE;
remote_id_uri_map = g_hash_table_new (g_str_hash, g_str_equal);
for (guint i = 0; i < remotes->len; i++) {
FwupdRemote *remote = g_ptr_array_index (remotes, i);
if (fwupd_remote_get_id (remote) == NULL)
continue;
if (fwupd_remote_get_report_uri (remote) == NULL)
continue;
g_debug ("adding %s for %s",
fwupd_remote_get_report_uri (remote),
fwupd_remote_get_id (remote));
g_hash_table_insert (remote_id_uri_map,
(gpointer) fwupd_remote_get_id (remote),
(gpointer) fwupd_remote_get_report_uri (remote));
}
/* get all devices from the history database, then filter them,
* adding to a hash map of report-ids */
devices = fwupd_client_get_history (priv->client, NULL, error);
if (devices == NULL)
return FALSE;
report_map = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify) g_ptr_array_unref);
for (guint i = 0; i < devices->len; i++) {
FwupdDevice *dev = g_ptr_array_index (devices, i);
FwupdRelease *rel = fwupd_device_get_release_default (dev);
const gchar *remote_id;
const gchar *remote_uri;
GPtrArray *devices_tmp;
/* filter, if not forcing */
if (!fu_util_filter_device (priv, dev))
continue;
if ((priv->flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_REPORTED))
continue;
if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
continue;
}
/* only send success and failure */
if (fwupd_device_get_update_state (dev) != FWUPD_UPDATE_STATE_FAILED &&
fwupd_device_get_update_state (dev) != FWUPD_UPDATE_STATE_SUCCESS) {
g_debug ("ignoring %s with UpdateState %s",
fwupd_device_get_id (dev),
fwupd_update_state_to_string (fwupd_device_get_update_state (dev)));
continue;
}
/* find the RemoteURI to use for the device */
remote_id = fwupd_release_get_remote_id (rel);
if (remote_id == NULL) {
g_debug ("%s has no RemoteID", fwupd_device_get_id (dev));
continue;
}
remote_uri = g_hash_table_lookup (remote_id_uri_map, remote_id);
if (remote_uri == NULL) {
g_debug ("%s has no RemoteURI", remote_id);
continue;
}
/* add this to the hash map */
devices_tmp = g_hash_table_lookup (report_map, remote_uri);
if (devices_tmp == NULL) {
devices_tmp = g_ptr_array_new ();
g_hash_table_insert (report_map, g_strdup (remote_uri), devices_tmp);
}
g_debug ("using %s for %s", remote_uri, fwupd_device_get_id (dev));
g_ptr_array_add (devices_tmp, dev);
}
/* nothing to report */
if (g_hash_table_size (report_map) == 0) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"No reports require uploading");
return FALSE;
}
/* process each uri */
uris = g_hash_table_get_keys (report_map);
for (GList *l = uris; l != NULL; l = l->next) {
const gchar *uri = l->data;
GPtrArray *devices_tmp = g_hash_table_lookup (report_map, uri);
if (!fu_util_report_history_for_uri (priv, uri, devices_tmp, error))
return FALSE;
}
/* mark each device as reported */
for (guint i = 0; i < devices->len; i++) {
FwupdDevice *dev = g_ptr_array_index (devices, i);
if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_REPORTED))
continue;
if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED))
continue;
g_debug ("setting flag on %s", fwupd_device_get_id (dev));
if (!fwupd_client_modify_device (priv->client,
fwupd_device_get_id (dev),
"Flags", "reported",
NULL, error))
return FALSE;
}
return TRUE;
}
static gboolean
fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(GPtrArray) devices = NULL;
g_autoptr(GNode) root = g_node_new (NULL);
g_autofree gchar *title = fu_util_get_tree_title (priv);
/* get all devices from the history database */
devices = fwupd_client_get_history (priv->client, NULL, error);
if (devices == NULL)
return FALSE;
/* show each device */
for (guint i = 0; i < devices->len; i++) {
g_autoptr(GPtrArray) rels = NULL;
FwupdDevice *dev = g_ptr_array_index (devices, i);
FwupdRelease *rel;
const gchar *remote;
GNode *child;
if (!fu_util_filter_device (priv, dev))
continue;
child = g_node_append_data (root, dev);
rel = fwupd_device_get_release_default (dev);
if (rel == NULL)
continue;
remote = fwupd_release_get_remote_id (rel);
/* doesn't actually map to remote */
if (remote == NULL) {
g_node_append_data (child, rel);
continue;
}
/* try to lookup releases from client */
rels = fwupd_client_get_releases (priv->client, fwupd_device_get_id (dev), NULL, error);
if (rels == NULL)
return FALSE;
/* map to a release in client */
for (guint j = 0; j < rels->len; j++) {
FwupdRelease *rel2 = g_ptr_array_index (rels, j);
if (g_strcmp0 (remote,
fwupd_release_get_remote_id (rel2)) != 0)
continue;
if (g_strcmp0 (fwupd_release_get_version (rel),
fwupd_release_get_version (rel2)) != 0)
continue;
g_node_append_data (child, g_object_ref (rel2));
rel = NULL;
break;
}
/* didn't match anything */
if (rels->len == 0 || rel != NULL) {
g_node_append_data (child, rel);
continue;
}
}
fu_util_print_tree (root, title);
return TRUE;
}
static FwupdDevice*
fu_util_get_device_or_prompt (FuUtilPrivate *priv, gchar **values, GError **error)
{
FwupdDevice *dev = NULL;
/* get device to use */
if (g_strv_length (values) >= 1) {
g_autoptr(GError) error_local = NULL;
if (g_strv_length (values) > 1) {
for (guint i = 1; i < g_strv_length (values); i++)
g_debug ("Ignoring extra input %s", values[i]);
}
dev = fwupd_client_get_device_by_id (priv->client, values[0],
NULL, &error_local);
if (dev != NULL)
return dev;
g_print ("%s\n", error_local->message);
}
return fu_util_prompt_for_device (priv, error);
}
static gboolean
fu_util_clear_results (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(FwupdDevice) dev = NULL;
dev = fu_util_get_device_or_prompt (priv, values, error);
if (dev == NULL)
return FALSE;
return fwupd_client_clear_results (priv->client, fwupd_device_get_id (dev), NULL, error);
}
static gboolean
fu_util_clear_offline (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(FuHistory) history = fu_history_new ();
return fu_history_remove_all_with_state (history, FWUPD_UPDATE_STATE_PENDING, error);
}
static gboolean
fu_util_verify_update_all (FuUtilPrivate *priv, GError **error)
{
g_autoptr(GPtrArray) devs = NULL;
/* get devices from daemon */
devs = fwupd_client_get_devices (priv->client, NULL, error);
if (devs == NULL)
return FALSE;
/* get results */
for (guint i = 0; i < devs->len; i++) {
g_autoptr(GError) error_local = NULL;
FwupdDevice *dev = g_ptr_array_index (devs, i);
if (!fu_util_filter_device (priv, dev))
continue;
if (!fwupd_client_verify_update (priv->client,
fwupd_device_get_id (dev),
NULL,
&error_local)) {
g_print ("%s\tFAILED: %s\n",
fwupd_device_get_guid_default (dev),
error_local->message);
continue;
}
g_print ("%s\t%s\n",
fwupd_device_get_guid_default (dev),
_("OK"));
}
return TRUE;
}
static gboolean
fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(FwupdDevice) dev = NULL;
if (g_strv_length (values) == 0)
return fu_util_verify_update_all (priv, error);
dev = fu_util_get_device_or_prompt (priv, values, error);
if (dev == NULL)
return FALSE;
return fwupd_client_verify_update (priv->client, fwupd_device_get_id (dev), NULL, error);
}
static gboolean
fu_util_file_exists_with_checksum (const gchar *fn,
const gchar *checksum_expected,
GChecksumType checksum_type)
{
gsize len = 0;
g_autofree gchar *checksum_actual = NULL;
g_autofree gchar *data = NULL;
if (!g_file_get_contents (fn, &data, &len, NULL))
return FALSE;
checksum_actual = g_compute_checksum_for_data (checksum_type,
(guchar *) data, len);
return g_strcmp0 (checksum_expected, checksum_actual) == 0;
}
static void
fu_util_download_chunk_cb (SoupMessage *msg, SoupBuffer *chunk, gpointer user_data)
{
guint percentage;
goffset header_size;
goffset body_length;
FuUtilPrivate *priv = (FuUtilPrivate *) user_data;
/* if it's returning "Found" or an error, ignore the percentage */
if (msg->status_code != SOUP_STATUS_OK) {
g_debug ("ignoring status code %u (%s)",
msg->status_code, msg->reason_phrase);
return;
}
/* get data */
body_length = msg->response_body->length;
header_size = soup_message_headers_get_content_length (msg->response_headers);
/* size is not known */
if (header_size < body_length)
return;
/* calculate percentage */
percentage = (guint) ((100 * body_length) / header_size);
g_debug ("progress: %u%%", percentage);
fu_progressbar_update (priv->progressbar, FWUPD_STATUS_DOWNLOADING, percentage);
}
static gboolean
fu_util_download_file (FuUtilPrivate *priv,
SoupURI *uri,
const gchar *fn,
const gchar *checksum_expected,
GError **error)
{
GChecksumType checksum_type;
guint status_code;