-
Notifications
You must be signed in to change notification settings - Fork 411
/
fu-uefi-device.c
878 lines (777 loc) · 25.8 KB
/
fu-uefi-device.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
/*
* Copyright 2018 Richard Hughes <richard@hughsie.com>
* Copyright 2015 Peter Jones <pjones@redhat.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <string.h>
#include "fu-uefi-common.h"
#include "fu-uefi-device.h"
#include "fu-uefi-struct.h"
typedef struct {
FuVolume *esp;
FuDeviceLocker *esp_locker;
gchar *fw_class;
FuUefiDeviceKind kind;
guint32 capsule_flags;
guint32 fw_version;
guint32 fw_version_lowest;
FuUefiDeviceStatus last_attempt_status;
guint32 last_attempt_version;
guint64 fmp_hardware_instance;
gboolean missing_header;
gboolean automounted_esp;
gsize require_esp_free_space;
} FuUefiDevicePrivate;
G_DEFINE_TYPE_WITH_PRIVATE(FuUefiDevice, fu_uefi_device, FU_TYPE_DEVICE)
#define GET_PRIVATE(o) (fu_uefi_device_get_instance_private(o))
#define FU_EFI_FMP_CAPSULE_GUID "6dcbd5ed-e82d-4c44-bda1-7194199ad92a"
enum {
PROP_0,
PROP_FW_CLASS,
PROP_KIND,
PROP_CAPSULE_FLAGS,
PROP_FW_VERSION,
PROP_FW_VERSION_LOWEST,
PROP_LAST_ATTEMPT_STATUS,
PROP_LAST_ATTEMPT_VERSION,
PROP_FMP_HARDWARE_INSTANCE,
PROP_LAST
};
void
fu_uefi_device_set_esp(FuUefiDevice *self, FuVolume *esp)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_UEFI_DEVICE(self));
g_return_if_fail(FU_IS_VOLUME(esp));
g_set_object(&priv->esp, esp);
}
static void
fu_uefi_device_to_string(FuDevice *device, guint idt, GString *str)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
fu_string_append(str, idt, "Kind", fu_uefi_device_kind_to_string(priv->kind));
fu_string_append(str, idt, "FwClass", priv->fw_class);
fu_string_append_kx(str, idt, "CapsuleFlags", priv->capsule_flags);
fu_string_append_kx(str, idt, "FwVersion", priv->fw_version);
fu_string_append_kx(str, idt, "FwVersionLowest", priv->fw_version_lowest);
fu_string_append(str,
idt,
"LastAttemptStatus",
fu_uefi_device_status_to_string(priv->last_attempt_status));
fu_string_append_kx(str, idt, "LastAttemptVersion", priv->last_attempt_version);
if (priv->esp != NULL) {
g_autofree gchar *kind = fu_volume_get_partition_kind(priv->esp);
g_autofree gchar *mount_point = fu_volume_get_mount_point(priv->esp);
fu_string_append(str, idt, "EspId", fu_volume_get_id(priv->esp));
if (mount_point != NULL)
fu_string_append(str, idt, "EspPath", mount_point);
if (kind != NULL) {
const gchar *guid = fu_volume_kind_convert_to_gpt(kind);
fu_string_append(str, idt, "EspKind", kind);
if (g_strcmp0(kind, guid) != 0)
fu_string_append(str, idt, "EspGuid", guid);
}
}
fu_string_append_ku(str, idt, "RequireESPFreeSpace", priv->require_esp_free_space);
}
static void
fu_uefi_device_report_metadata_pre(FuDevice *device, GHashTable *metadata)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_autofree gchar *kind = fu_volume_get_partition_kind(priv->esp);
g_autofree gchar *mount_point = fu_volume_get_mount_point(priv->esp);
/* record if we had an invalid header during update */
g_hash_table_insert(metadata,
g_strdup("MissingCapsuleHeader"),
g_strdup(priv->missing_header ? "True" : "False"));
/* where and how the ESP was mounted during installation */
g_hash_table_insert(metadata, g_strdup("EspPath"), g_steal_pointer(&mount_point));
if (kind != NULL) {
g_hash_table_insert(metadata, g_strdup("EspKind"), g_steal_pointer(&kind));
}
}
static void
fu_uefi_device_report_metadata_post(FuDevice *device, GHashTable *metadata)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
/* the actual last_attempt values */
g_hash_table_insert(metadata,
g_strdup("LastAttemptStatus"),
g_strdup_printf("0x%x", priv->last_attempt_status));
g_hash_table_insert(metadata,
g_strdup("LastAttemptVersion"),
g_strdup_printf("0x%x", priv->last_attempt_version));
}
FuUefiDeviceKind
fu_uefi_device_get_kind(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0);
return priv->kind;
}
guint32
fu_uefi_device_get_version(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0x0);
return priv->fw_version;
}
guint32
fu_uefi_device_get_version_lowest(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0x0);
return priv->fw_version_lowest;
}
guint32
fu_uefi_device_get_version_error(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0x0);
return priv->last_attempt_version;
}
guint64
fu_uefi_device_get_hardware_instance(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0x0);
return priv->fmp_hardware_instance;
}
FuUefiDeviceStatus
fu_uefi_device_get_status(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0);
return priv->last_attempt_status;
}
void
fu_uefi_device_set_status(FuUefiDevice *self, FuUefiDeviceStatus status)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
const gchar *tmp;
g_autofree gchar *err_msg = NULL;
g_autofree gchar *version_str = NULL;
g_return_if_fail(FU_IS_UEFI_DEVICE(self));
/* cache for later */
priv->last_attempt_status = status;
/* all good */
if (status == FU_UEFI_DEVICE_STATUS_SUCCESS) {
fu_device_set_update_state(FU_DEVICE(self), FWUPD_UPDATE_STATE_SUCCESS);
return;
}
/* something went wrong */
if (status == FU_UEFI_DEVICE_STATUS_ERROR_PWR_EVT_AC ||
status == FU_UEFI_DEVICE_STATUS_ERROR_PWR_EVT_BATT) {
fu_device_set_update_state(FU_DEVICE(self), FWUPD_UPDATE_STATE_FAILED_TRANSIENT);
} else {
fu_device_set_update_state(FU_DEVICE(self), FWUPD_UPDATE_STATE_FAILED);
}
version_str = g_strdup_printf("%u", priv->last_attempt_version);
tmp = fu_uefi_device_status_to_string(status);
if (tmp == NULL) {
err_msg = g_strdup_printf("failed to update to %s", version_str);
} else {
err_msg = g_strdup_printf("failed to update to %s: %s", version_str, tmp);
}
fu_device_set_update_error(FU_DEVICE(self), err_msg);
}
void
fu_uefi_device_set_require_esp_free_space(FuUefiDevice *self, gsize require_esp_free_space)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_UEFI_DEVICE(self));
priv->require_esp_free_space = require_esp_free_space;
}
guint32
fu_uefi_device_get_capsule_flags(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), 0x0);
return priv->capsule_flags;
}
const gchar *
fu_uefi_device_get_guid(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), NULL);
return priv->fw_class;
}
gchar *
fu_uefi_device_build_varname(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
return g_strdup_printf("fwupd-%s-%" G_GUINT64_FORMAT,
priv->fw_class,
priv->fmp_hardware_instance);
}
FuUefiUpdateInfo *
fu_uefi_device_load_update_info(FuUefiDevice *self, GError **error)
{
g_autofree gchar *varname = fu_uefi_device_build_varname(self);
g_autoptr(FuUefiUpdateInfo) info = fu_uefi_update_info_new();
g_autoptr(GBytes) fw = NULL;
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
/* get the existing status */
fw = fu_efivar_get_data_bytes(FU_EFIVAR_GUID_FWUPDATE, varname, NULL, error);
if (fw == NULL)
return NULL;
if (!fu_firmware_parse(FU_FIRMWARE(info), fw, FWUPD_INSTALL_FLAG_NONE, error))
return NULL;
return g_steal_pointer(&info);
}
gboolean
fu_uefi_device_clear_status(FuUefiDevice *self, GError **error)
{
gsize datasz = 0;
g_autofree gchar *varname = fu_uefi_device_build_varname(self);
g_autofree guint8 *data = NULL;
g_autoptr(GByteArray) st_inf = NULL;
g_return_val_if_fail(FU_IS_UEFI_DEVICE(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* get the existing status */
if (!fu_efivar_get_data(FU_EFIVAR_GUID_FWUPDATE, varname, &data, &datasz, NULL, error))
return FALSE;
st_inf = fu_struct_efi_update_info_parse(data, datasz, 0x0, error);
if (st_inf == NULL) {
g_prefix_error(error, "EFI variable is corrupt: ");
return FALSE;
}
/* just copy the new EfiUpdateInfo and save it back */
fu_struct_efi_update_info_set_status(st_inf, FU_UEFI_UPDATE_INFO_STATUS_UNKNOWN);
memcpy(data, st_inf->data, st_inf->len);
if (!fu_efivar_set_data(FU_EFIVAR_GUID_FWUPDATE,
varname,
data,
datasz,
FU_EFIVAR_ATTR_NON_VOLATILE | FU_EFIVAR_ATTR_BOOTSERVICE_ACCESS |
FU_EFIVAR_ATTR_RUNTIME_ACCESS,
error)) {
g_prefix_error(error, "could not set EfiUpdateInfo: ");
return FALSE;
}
/* success */
return TRUE;
}
FuEfiDevicePathList *
fu_uefi_device_build_dp_buf(FuVolume *esp, const gchar *capsule_path, GError **error)
{
g_autoptr(FuEfiDevicePathList) dp_buf = fu_efi_device_path_list_new();
g_autoptr(FuEfiFilePathDevicePath) dp_file = fu_efi_file_path_device_path_new();
g_autoptr(FuEfiHardDriveDevicePath) dp_hd = NULL;
g_autofree gchar *name_with_root = NULL;
dp_hd = fu_efi_hard_drive_device_path_new_from_volume(esp, error);
if (dp_hd == NULL)
return NULL;
name_with_root = g_strdup_printf("/%s", capsule_path);
if (!fu_efi_file_path_device_path_set_name(dp_file, name_with_root, error))
return NULL;
fu_firmware_add_image(FU_FIRMWARE(dp_buf), FU_FIRMWARE(dp_hd));
fu_firmware_add_image(FU_FIRMWARE(dp_buf), FU_FIRMWARE(dp_file));
return g_steal_pointer(&dp_buf);
}
GBytes *
fu_uefi_device_fixup_firmware(FuUefiDevice *self, GBytes *fw, GError **error)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
fwupd_guid_t esrt_guid = {0x0};
guint hdrsize = getpagesize();
gsize bufsz;
const guint8 *buf = g_bytes_get_data(fw, &bufsz);
g_autofree gchar *guid_new = NULL;
g_autoptr(GByteArray) st_cap = fu_struct_efi_capsule_header_new();
priv->missing_header = FALSE;
/* GUID is the first 16 bytes */
if (bufsz < sizeof(fwupd_guid_t)) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"Invalid payload");
return NULL;
}
guid_new = fwupd_guid_to_string((fwupd_guid_t *)buf, FWUPD_GUID_FLAG_MIXED_ENDIAN);
/* ESRT header matches payload */
if (g_strcmp0(fu_uefi_device_get_guid(self), guid_new) == 0) {
g_debug("ESRT matches payload GUID");
return g_bytes_ref(fw);
}
if (g_strcmp0(guid_new, FU_EFI_FMP_CAPSULE_GUID) == 0 ||
fu_device_has_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_NO_CAPSULE_HEADER_FIXUP)) {
return g_bytes_ref(fw);
}
/* create a fake header with plausible contents */
g_info("missing or invalid embedded capsule header");
priv->missing_header = TRUE;
fu_struct_efi_capsule_header_set_flags(st_cap, priv->capsule_flags);
fu_struct_efi_capsule_header_set_header_size(st_cap, hdrsize);
fu_struct_efi_capsule_header_set_image_size(st_cap, bufsz + hdrsize);
if (!fwupd_guid_from_string(fu_uefi_device_get_guid(self),
&esrt_guid,
FWUPD_GUID_FLAG_MIXED_ENDIAN,
error)) {
g_prefix_error(error, "Invalid ESRT GUID: ");
return NULL;
}
fu_struct_efi_capsule_header_set_guid(st_cap, &esrt_guid);
/* pad to the headersize then add the payload */
fu_byte_array_set_size(st_cap, hdrsize, 0x00);
g_byte_array_append(st_cap, buf, bufsz);
return g_bytes_new(st_cap->data, st_cap->len);
}
gboolean
fu_uefi_device_write_update_info(FuUefiDevice *self,
const gchar *capsule_path,
const gchar *varname,
const gchar *guid_str,
GError **error)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
fwupd_guid_t guid = {0x0};
g_autoptr(FuEfiDevicePathList) dp_buf = NULL;
g_autoptr(GBytes) dp_blob = NULL;
g_autoptr(GByteArray) st_inf = fu_struct_efi_update_info_new();
/* set the body as the device path */
if (g_getenv("FWUPD_UEFI_TEST") != NULL) {
g_debug("not building device path, in tests....");
return TRUE;
}
/* convert to EFI device path */
dp_buf = fu_uefi_device_build_dp_buf(priv->esp, capsule_path, error);
if (dp_buf == NULL)
return FALSE;
dp_blob = fu_firmware_write(FU_FIRMWARE(dp_buf), error);
if (dp_blob == NULL)
return FALSE;
/* save this header and body to the hardware */
if (!fwupd_guid_from_string(guid_str, &guid, FWUPD_GUID_FLAG_MIXED_ENDIAN, error))
return FALSE;
fu_struct_efi_update_info_set_flags(st_inf, priv->capsule_flags);
fu_struct_efi_update_info_set_hw_inst(st_inf, priv->fmp_hardware_instance);
fu_struct_efi_update_info_set_status(st_inf, FU_UEFI_UPDATE_INFO_STATUS_ATTEMPT_UPDATE);
fu_struct_efi_update_info_set_guid(st_inf, &guid);
fu_byte_array_append_bytes(st_inf, dp_blob);
if (!fu_efivar_set_data(FU_EFIVAR_GUID_FWUPDATE,
varname,
st_inf->data,
st_inf->len,
FU_EFIVAR_ATTR_NON_VOLATILE | FU_EFIVAR_ATTR_BOOTSERVICE_ACCESS |
FU_EFIVAR_ATTR_RUNTIME_ACCESS,
error)) {
g_prefix_error(error, "could not set DP_BUF with %s: ", capsule_path);
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_uefi_check_asset(FuDevice *device, GError **error)
{
g_autofree gchar *source_app = fu_uefi_get_built_app_path("fwupd", error);
if (source_app == NULL) {
if (fu_efivar_secure_boot_enabled(NULL))
g_prefix_error(error, "missing signed bootloader for secure boot: ");
return FALSE;
}
return TRUE;
}
static gboolean
fu_uefi_device_prepare(FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
/* mount if required */
priv->esp_locker = fu_volume_locker(priv->esp, error);
if (priv->esp_locker == NULL)
return FALSE;
/* sanity checks */
if (!fu_uefi_check_asset(device, error))
return FALSE;
return TRUE;
}
static gboolean
fu_uefi_device_cleanup(FuDevice *device,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
/* unmount ESP if we opened it */
if (!fu_device_locker_close(priv->esp_locker, error))
return FALSE;
g_clear_object(&priv->esp_locker);
return TRUE;
}
static gboolean
fu_uefi_device_probe(FuDevice *device, GError **error)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
/* broken sysfs? */
if (priv->fw_class == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"failed to read fw_class");
return FALSE;
}
/* this is invalid */
if (!fwupd_guid_is_valid(priv->fw_class)) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"ESRT GUID '%s' was not valid",
priv->fw_class);
return FALSE;
}
/* add GUID first, as quirks may set the version format */
fu_device_add_guid(device, priv->fw_class);
/* set versions */
fu_device_set_version_raw(device, priv->fw_version);
if (priv->fw_version_lowest != 0) {
g_autofree gchar *version_lowest =
fu_version_from_uint32(priv->fw_version_lowest,
fu_device_get_version_format(self));
fu_device_set_version_lowest_raw(device, priv->fw_version_lowest);
fu_device_set_version_lowest(device, version_lowest);
}
/* set flags */
fu_device_add_flag(device, FWUPD_DEVICE_FLAG_INTERNAL);
fu_device_add_flag(device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
fu_device_add_flag(device, FWUPD_DEVICE_FLAG_REQUIRE_AC);
fu_device_add_internal_flag(device, FU_DEVICE_INTERNAL_FLAG_MD_SET_VERFMT);
fu_device_add_internal_flag(device, FU_DEVICE_INTERNAL_FLAG_MD_SET_ICON);
fu_device_add_internal_flag(device, FU_DEVICE_INTERNAL_FLAG_MD_SET_VENDOR);
/* add icons */
if (priv->kind == FU_UEFI_DEVICE_KIND_SYSTEM_FIRMWARE) {
fu_device_add_icon(device, "computer");
fu_device_add_internal_flag(device, FU_DEVICE_INTERNAL_FLAG_HOST_FIRMWARE);
}
/* whether to create a missing header */
if (priv->kind == FU_UEFI_DEVICE_KIND_FMP ||
priv->kind == FU_UEFI_DEVICE_KIND_DELL_TPM_FIRMWARE)
fu_device_add_private_flag(device, FU_UEFI_DEVICE_FLAG_NO_CAPSULE_HEADER_FIXUP);
/* success */
return TRUE;
}
static void
fu_uefi_device_capture_efi_debugging(FuDevice *device)
{
g_autofree gchar *str = NULL;
g_autoptr(GBytes) buf = NULL;
g_autoptr(GError) error_local = NULL;
/* get the EFI variable contents */
buf = fu_efivar_get_data_bytes(FU_EFIVAR_GUID_FWUPDATE,
"FWUPDATE_DEBUG_LOG",
NULL,
&error_local);
if (buf == NULL) {
g_warning("failed to capture EFI debugging: %s", error_local->message);
return;
}
/* convert from UCS-2 to UTF-8 */
str = fu_utf16_to_utf8_bytes(buf, G_LITTLE_ENDIAN, &error_local);
if (str == NULL) {
g_warning("failed to capture EFI debugging: %s", error_local->message);
return;
}
/* success, dump into journal */
g_info("EFI debugging: %s", str);
}
gboolean
fu_uefi_device_perhaps_enable_debugging(FuUefiDevice *self, GError **error)
{
if (fu_device_has_private_flag(FU_DEVICE(self), FU_UEFI_DEVICE_FLAG_ENABLE_EFI_DEBUGGING)) {
const guint8 data = 1;
if (!fu_efivar_set_data(FU_EFIVAR_GUID_FWUPDATE,
"FWUPDATE_VERBOSE",
&data,
sizeof(data),
FU_EFIVAR_ATTR_NON_VOLATILE |
FU_EFIVAR_ATTR_BOOTSERVICE_ACCESS |
FU_EFIVAR_ATTR_RUNTIME_ACCESS,
error)) {
g_prefix_error(error, "failed to enable debugging: ");
return FALSE;
}
return TRUE;
}
/* unset this */
if (fu_efivar_exists(FU_EFIVAR_GUID_FWUPDATE, "FWUPDATE_VERBOSE")) {
if (!fu_efivar_delete(FU_EFIVAR_GUID_FWUPDATE, "FWUPDATE_VERBOSE", error))
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_uefi_device_get_results(FuDevice *device, GError **error)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
/* capture EFI binary debug output */
if (fu_device_has_private_flag(device, FU_UEFI_DEVICE_FLAG_ENABLE_EFI_DEBUGGING))
fu_uefi_device_capture_efi_debugging(device);
/* just set the update error */
fu_uefi_device_set_status(self, priv->last_attempt_status);
return TRUE;
}
FuVolume *
fu_uefi_device_get_esp(FuUefiDevice *self)
{
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
return priv->esp;
}
static FuFirmware *
fu_uefi_device_prepare_firmware(FuDevice *device,
GInputStream *stream,
FwupdInstallFlags flags,
GError **error)
{
FuUefiDevice *self = FU_UEFI_DEVICE(device);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
gsize sz_reqd = priv->require_esp_free_space;
g_autoptr(FuFirmware) firmware = fu_firmware_new();
/* check there is enough space in the ESP */
if (!fu_firmware_parse_stream(firmware, stream, 0x0, flags, error))
return NULL;
if (sz_reqd == 0) {
g_info("required ESP free space is not configured, using 2 x %uMB + 20MB",
(guint)fu_firmware_get_size(firmware) / (1024 * 1024));
sz_reqd = fu_firmware_get_size(firmware) * 2 + (20u * 1024 * 1024);
}
if (!fu_volume_check_free_space(priv->esp, sz_reqd, error))
return NULL;
/* success */
return g_steal_pointer(&firmware);
}
static void
fu_uefi_device_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
FuUefiDevice *self = FU_UEFI_DEVICE(object);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
switch (prop_id) {
case PROP_FW_CLASS:
priv->fw_class = g_value_dup_string(value);
break;
case PROP_KIND:
priv->kind = g_value_get_uint(value);
break;
case PROP_CAPSULE_FLAGS:
priv->capsule_flags = g_value_get_uint(value);
break;
case PROP_FW_VERSION:
priv->fw_version = g_value_get_uint(value);
break;
case PROP_FW_VERSION_LOWEST:
priv->fw_version_lowest = g_value_get_uint(value);
break;
case PROP_LAST_ATTEMPT_STATUS:
fu_uefi_device_set_status(self, g_value_get_uint(value));
break;
case PROP_LAST_ATTEMPT_VERSION:
priv->last_attempt_version = g_value_get_uint(value);
break;
case PROP_FMP_HARDWARE_INSTANCE:
priv->fmp_hardware_instance = g_value_get_uint64(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
fu_uefi_device_set_progress(FuDevice *self, FuProgress *progress)
{
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, "detach");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 100, "write");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, "attach");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 0, "reload");
}
static void
fu_uefi_device_init(FuUefiDevice *self)
{
fu_device_add_protocol(FU_DEVICE(self), "org.uefi.capsule");
fu_device_add_internal_flag(FU_DEVICE(self), FU_DEVICE_INTERNAL_FLAG_MD_SET_SIGNED);
fu_device_add_internal_flag(FU_DEVICE(self), FU_DEVICE_INTERNAL_FLAG_MD_SET_FLAGS);
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_NO_UX_CAPSULE,
"no-ux-capsule");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_USE_SHIM_UNIQUE,
"use-shim-unique");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_USE_LEGACY_BOOTMGR_DESC,
"use-legacy-bootmgr-desc");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_SUPPORTS_BOOT_ORDER_LOCK,
"supports-boot-order-lock");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_USE_SHIM_FOR_SB,
"use-shim-for-sb");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_NO_RT_SET_VARIABLE,
"no-rt-set-variable");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_NO_CAPSULE_HEADER_FIXUP,
"no-capsule-header-fixup");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_ENABLE_EFI_DEBUGGING,
"enable-debugging");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_COD_INDEXED_FILENAME,
"cod-indexed-filename");
fu_device_register_private_flag(FU_DEVICE(self),
FU_UEFI_DEVICE_FLAG_MODIFY_BOOTORDER,
"modify-bootorder");
}
static void
fu_uefi_device_finalize(GObject *object)
{
FuUefiDevice *self = FU_UEFI_DEVICE(object);
FuUefiDevicePrivate *priv = GET_PRIVATE(self);
g_free(priv->fw_class);
if (priv->esp != NULL)
g_object_unref(priv->esp);
if (priv->esp_locker != NULL)
g_object_unref(priv->esp_locker);
G_OBJECT_CLASS(fu_uefi_device_parent_class)->finalize(object);
}
static gchar *
fu_uefi_device_convert_version(FuDevice *device, guint64 version_raw)
{
return fu_version_from_uint32(version_raw, fu_device_get_version_format(device));
}
static void
fu_uefi_device_class_init(FuUefiDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
GParamSpec *pspec;
FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
object_class->set_property = fu_uefi_device_set_property;
object_class->finalize = fu_uefi_device_finalize;
device_class->to_string = fu_uefi_device_to_string;
device_class->probe = fu_uefi_device_probe;
device_class->prepare_firmware = fu_uefi_device_prepare_firmware;
device_class->prepare = fu_uefi_device_prepare;
device_class->cleanup = fu_uefi_device_cleanup;
device_class->report_metadata_pre = fu_uefi_device_report_metadata_pre;
device_class->report_metadata_post = fu_uefi_device_report_metadata_post;
device_class->get_results = fu_uefi_device_get_results;
device_class->set_progress = fu_uefi_device_set_progress;
device_class->convert_version = fu_uefi_device_convert_version;
/**
* FuUefiDevice:fw-class:
*
* The firmware class, i.e. the ESRT GUID.
*/
pspec =
g_param_spec_string("fw-class",
NULL,
NULL,
NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_FW_CLASS, pspec);
/**
* FuUefiDevice:kind:
*
* The device kind.
*/
pspec = g_param_spec_uint("kind",
NULL,
NULL,
FU_UEFI_DEVICE_KIND_UNKNOWN,
FU_UEFI_DEVICE_KIND_LAST - 1,
FU_UEFI_DEVICE_KIND_UNKNOWN,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_KIND, pspec);
/**
* FuUefiDevice:capsule-flags:
*
* The capsule flags to use for the update.
*/
pspec = g_param_spec_uint("capsule-flags",
NULL,
NULL,
0,
G_MAXUINT32,
0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_CAPSULE_FLAGS, pspec);
/**
* FuUefiDevice:fw-version:
*
* The current firmware version.
*/
pspec = g_param_spec_uint("fw-version",
NULL,
NULL,
0,
G_MAXUINT32,
0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_FW_VERSION, pspec);
/**
* FuUefiDevice:fw-version-lowest:
*
* The lowest possible installable version.
*/
pspec = g_param_spec_uint("fw-version-lowest",
NULL,
NULL,
0,
G_MAXUINT32,
0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_FW_VERSION_LOWEST, pspec);
/**
* FuUefiDevice:last-attempt-status:
*
* The last attempt status value.
*/
pspec = g_param_spec_uint("last-attempt-status",
NULL,
NULL,
FU_UEFI_DEVICE_STATUS_SUCCESS,
FU_UEFI_DEVICE_STATUS_LAST - 1,
FU_UEFI_DEVICE_STATUS_SUCCESS,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_LAST_ATTEMPT_STATUS, pspec);
/**
* FuUefiDevice:last-attempt-version:
*
* The last attempt firmware version.
*/
pspec = g_param_spec_uint("last-attempt-version",
NULL,
NULL,
0,
G_MAXUINT32,
0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_LAST_ATTEMPT_VERSION, pspec);
/**
* FuUefiDevice:fmp-hardware-instance:
*
* The FMP hardware instance.
*/
pspec =
g_param_spec_uint64("fmp-hardware-instance",
NULL,
NULL,
0,
G_MAXUINT64,
0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME);
g_object_class_install_property(object_class, PROP_FMP_HARDWARE_INSTANCE, pspec);
}