1 /* SPDX-License-Identifier: LGPL-2.1+ */
16 #include "alloc-util.h"
17 #include "chattr-util.h"
18 #include "dirent-util.h"
23 #include "parse-util.h"
24 #include "stdio-util.h"
26 #include "time-util.h"
33 #define LOAD_OPTION_ACTIVE 0x00000001
34 #define MEDIA_DEVICE_PATH 0x04
35 #define MEDIA_HARDDRIVE_DP 0x01
36 #define MEDIA_FILEPATH_DP 0x04
37 #define SIGNATURE_TYPE_GUID 0x02
38 #define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
39 #define END_DEVICE_PATH_TYPE 0x7f
40 #define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff
41 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001
55 uint8_t signature_type
;
64 struct drive_path drive
;
68 bool is_efi_boot(void) {
69 if (detect_container() > 0)
72 return access("/sys/firmware/efi/", F_OK
) >= 0;
75 static int read_flag(const char *varname
) {
76 _cleanup_free_
void *v
= NULL
;
81 if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */
84 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, varname
, NULL
, &v
, &s
);
95 bool is_efi_secure_boot(void) {
96 return read_flag("SecureBoot") > 0;
99 bool is_efi_secure_boot_setup_mode(void) {
100 return read_flag("SetupMode") > 0;
103 int efi_reboot_to_firmware_supported(void) {
104 _cleanup_free_
void *v
= NULL
;
112 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "OsIndicationsSupported", NULL
, &v
, &s
);
113 if (r
== -ENOENT
) /* variable doesn't exist? it's not supported then */
117 if (s
!= sizeof(uint64_t))
121 if (!(b
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
))
122 return -EOPNOTSUPP
; /* bit unset? it's not supported then */
127 static int get_os_indications(uint64_t *os_indication
) {
128 _cleanup_free_
void *v
= NULL
;
132 /* Let's verify general support first */
133 r
= efi_reboot_to_firmware_supported();
137 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "OsIndications", NULL
, &v
, &s
);
139 /* Some firmware implementations that do support OsIndications and report that with
140 * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's pretend it's 0
141 * then, to hide this implementation detail. Note that this call will return -ENOENT then only if the
142 * support for OsIndications is missing entirely, as determined by efi_reboot_to_firmware_supported()
148 else if (s
!= sizeof(uint64_t))
151 *os_indication
= *(uint64_t *)v
;
155 int efi_get_reboot_to_firmware(void) {
159 r
= get_os_indications(&b
);
163 return !!(b
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
);
166 int efi_set_reboot_to_firmware(bool value
) {
170 r
= get_os_indications(&b
);
175 b_new
= b
| EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
177 b_new
= b
& ~EFI_OS_INDICATIONS_BOOT_TO_FW_UI
;
179 /* Avoid writing to efi vars store if we can due to firmware bugs. */
181 return efi_set_variable(EFI_VENDOR_GLOBAL
, "OsIndications", &b_new
, sizeof(uint64_t));
186 int efi_get_variable(
193 _cleanup_close_
int fd
= -1;
194 _cleanup_free_
char *p
= NULL
;
198 _cleanup_free_
void *buf
= NULL
;
205 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
206 name
, SD_ID128_FORMAT_VAL(vendor
)) < 0)
209 fd
= open(p
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
213 if (fstat(fd
, &st
) < 0)
217 if (st
.st_size
> 4*1024*1024 + 4)
220 n
= read(fd
, &a
, sizeof(a
));
226 buf
= malloc(st
.st_size
- 4 + 2);
230 n
= read(fd
, buf
, (size_t) st
.st_size
- 4);
233 if (n
!= (ssize_t
) st
.st_size
- 4)
236 /* Always NUL terminate (2 bytes, to protect UTF-16) */
237 ((char*) buf
)[st
.st_size
- 4] = 0;
238 ((char*) buf
)[st
.st_size
- 4 + 1] = 0;
240 *value
= TAKE_PTR(buf
);
241 *size
= (size_t) st
.st_size
- 4;
249 int efi_get_variable_string(sd_id128_t vendor
, const char *name
, char **p
) {
250 _cleanup_free_
void *s
= NULL
;
255 r
= efi_get_variable(vendor
, name
, NULL
, &s
, &ss
);
259 x
= utf16_to_utf8(s
, ss
);
267 int efi_set_variable(
276 } _packed_
* _cleanup_free_ buf
= NULL
;
277 _cleanup_free_
char *p
= NULL
;
278 _cleanup_close_
int fd
= -1;
279 bool saved_flags_valid
= false;
280 unsigned saved_flags
;
284 assert(value
|| size
== 0);
287 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
288 name
, SD_ID128_FORMAT_VAL(vendor
)) < 0)
291 /* Newer efivarfs protects variables that are not in a whitelist with FS_IMMUTABLE_FL by default, to protect
292 * them for accidental removal and modification. We are not changing these variables accidentally however,
293 * hence let's unset the bit first. */
295 r
= chattr_path(p
, 0, FS_IMMUTABLE_FL
, &saved_flags
);
296 if (r
< 0 && r
!= -ENOENT
)
297 log_debug_errno(r
, "Failed to drop FS_IMMUTABLE_FL flag from '%s', ignoring: %m", p
);
299 saved_flags_valid
= r
>= 0;
310 fd
= open(p
, O_WRONLY
|O_CREAT
|O_NOCTTY
|O_CLOEXEC
, 0644);
316 buf
= malloc(sizeof(uint32_t) + size
);
322 buf
->attr
= EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
;
323 memcpy(buf
->buf
, value
, size
);
325 r
= loop_write(fd
, buf
, sizeof(uint32_t) + size
, false);
332 if (saved_flags_valid
) {
335 /* Restore the original flags field, just in case */
337 q
= chattr_path(p
, saved_flags
, FS_IMMUTABLE_FL
, NULL
);
339 q
= chattr_fd(fd
, saved_flags
, FS_IMMUTABLE_FL
, NULL
);
341 log_debug_errno(q
, "Failed to restore FS_IMMUTABLE_FL on '%s', ignoring: %m", p
);
347 int efi_set_variable_string(sd_id128_t vendor
, const char *name
, const char *v
) {
348 _cleanup_free_ char16_t
*u16
= NULL
;
350 u16
= utf8_to_utf16(v
, strlen(v
));
354 return efi_set_variable(vendor
, name
, u16
, (char16_strlen(u16
) + 1) * sizeof(char16_t
));
357 static size_t utf16_size(const uint16_t *s
) {
363 return (l
+1) * sizeof(uint16_t);
366 static void efi_guid_to_id128(const void *guid
, sd_id128_t
*id128
) {
373 const struct uuid
*uuid
= guid
;
375 id128
->bytes
[0] = (uuid
->u1
>> 24) & 0xff;
376 id128
->bytes
[1] = (uuid
->u1
>> 16) & 0xff;
377 id128
->bytes
[2] = (uuid
->u1
>> 8) & 0xff;
378 id128
->bytes
[3] = (uuid
->u1
) & 0xff;
379 id128
->bytes
[4] = (uuid
->u2
>> 8) & 0xff;
380 id128
->bytes
[5] = (uuid
->u2
) & 0xff;
381 id128
->bytes
[6] = (uuid
->u3
>> 8) & 0xff;
382 id128
->bytes
[7] = (uuid
->u3
) & 0xff;
383 memcpy(&id128
->bytes
[8], uuid
->u4
, sizeof(uuid
->u4
));
386 int efi_get_boot_option(
389 sd_id128_t
*part_uuid
,
394 _cleanup_free_
uint8_t *buf
= NULL
;
396 struct boot_option
*header
;
398 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
399 sd_id128_t p_uuid
= SD_ID128_NULL
;
405 xsprintf(boot_id
, "Boot%04X", id
);
406 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, boot_id
, NULL
, (void **)&buf
, &l
);
409 if (l
< sizeof(struct boot_option
))
412 header
= (struct boot_option
*)buf
;
413 title_size
= utf16_size(header
->title
);
414 if (title_size
> l
- offsetof(struct boot_option
, title
))
418 s
= utf16_to_utf8(header
->title
, title_size
);
423 if (header
->path_len
> 0) {
427 doff
= offsetof(struct boot_option
, title
) + title_size
;
429 if (header
->path_len
> l
- doff
)
433 while (dnext
< header
->path_len
) {
434 struct device_path
*dpath
;
436 dpath
= (struct device_path
*)(dbuf
+ dnext
);
437 if (dpath
->length
< 4)
440 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
441 if (dpath
->type
== END_DEVICE_PATH_TYPE
&& dpath
->sub_type
== END_ENTIRE_DEVICE_PATH_SUBTYPE
)
444 dnext
+= dpath
->length
;
446 /* Type 0x04 – Media Device Path */
447 if (dpath
->type
!= MEDIA_DEVICE_PATH
)
450 /* Sub-Type 1 – Hard Drive */
451 if (dpath
->sub_type
== MEDIA_HARDDRIVE_DP
) {
452 /* 0x02 – GUID Partition Table */
453 if (dpath
->drive
.mbr_type
!= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
)
456 /* 0x02 – GUID signature */
457 if (dpath
->drive
.signature_type
!= SIGNATURE_TYPE_GUID
)
461 efi_guid_to_id128(dpath
->drive
.signature
, &p_uuid
);
465 /* Sub-Type 4 – File Path */
466 if (dpath
->sub_type
== MEDIA_FILEPATH_DP
&& !p
&& path
) {
467 p
= utf16_to_utf8(dpath
->path
, dpath
->length
-4);
471 efi_tilt_backslashes(p
);
478 *title
= TAKE_PTR(s
);
484 *active
= !!(header
->attr
& LOAD_OPTION_ACTIVE
);
489 static void to_utf16(uint16_t *dest
, const char *src
) {
492 for (i
= 0; src
[i
] != '\0'; i
++)
504 static void id128_to_efi_guid(sd_id128_t id
, void *guid
) {
505 struct guid
*uuid
= guid
;
507 uuid
->u1
= id
.bytes
[0] << 24 | id
.bytes
[1] << 16 | id
.bytes
[2] << 8 | id
.bytes
[3];
508 uuid
->u2
= id
.bytes
[4] << 8 | id
.bytes
[5];
509 uuid
->u3
= id
.bytes
[6] << 8 | id
.bytes
[7];
510 memcpy(uuid
->u4
, id
.bytes
+8, sizeof(uuid
->u4
));
513 static uint16_t *tilt_slashes(uint16_t *s
) {
523 int efi_add_boot_option(
529 sd_id128_t part_uuid
,
532 size_t size
, title_len
, path_len
;
533 _cleanup_free_
char *buf
= NULL
;
534 struct boot_option
*option
;
535 struct device_path
*devicep
;
541 title_len
= (strlen(title
)+1) * 2;
542 path_len
= (strlen(path
)+1) * 2;
544 buf
= malloc0(sizeof(struct boot_option
) + title_len
+
545 sizeof(struct drive_path
) +
546 sizeof(struct device_path
) + path_len
);
551 option
= (struct boot_option
*)buf
;
552 option
->attr
= LOAD_OPTION_ACTIVE
;
553 option
->path_len
= offsetof(struct device_path
, drive
) + sizeof(struct drive_path
) +
554 offsetof(struct device_path
, path
) + path_len
+
555 offsetof(struct device_path
, path
);
556 to_utf16(option
->title
, title
);
557 size
= offsetof(struct boot_option
, title
) + title_len
;
560 devicep
= (struct device_path
*)(buf
+ size
);
561 devicep
->type
= MEDIA_DEVICE_PATH
;
562 devicep
->sub_type
= MEDIA_HARDDRIVE_DP
;
563 devicep
->length
= offsetof(struct device_path
, drive
) + sizeof(struct drive_path
);
564 devicep
->drive
.part_nr
= part
;
565 devicep
->drive
.part_start
= pstart
;
566 devicep
->drive
.part_size
= psize
;
567 devicep
->drive
.signature_type
= SIGNATURE_TYPE_GUID
;
568 devicep
->drive
.mbr_type
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
569 id128_to_efi_guid(part_uuid
, devicep
->drive
.signature
);
570 size
+= devicep
->length
;
573 devicep
= (struct device_path
*)(buf
+ size
);
574 devicep
->type
= MEDIA_DEVICE_PATH
;
575 devicep
->sub_type
= MEDIA_FILEPATH_DP
;
576 devicep
->length
= offsetof(struct device_path
, path
) + path_len
;
577 to_utf16(devicep
->path
, path
);
578 tilt_slashes(devicep
->path
);
579 size
+= devicep
->length
;
582 devicep
= (struct device_path
*)(buf
+ size
);
583 devicep
->type
= END_DEVICE_PATH_TYPE
;
584 devicep
->sub_type
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
585 devicep
->length
= offsetof(struct device_path
, path
);
586 size
+= devicep
->length
;
588 xsprintf(boot_id
, "Boot%04X", id
);
589 return efi_set_variable(EFI_VENDOR_GLOBAL
, boot_id
, buf
, size
);
592 int efi_remove_boot_option(uint16_t id
) {
598 xsprintf(boot_id
, "Boot%04X", id
);
599 return efi_set_variable(EFI_VENDOR_GLOBAL
, boot_id
, NULL
, 0);
602 int efi_get_boot_order(uint16_t **order
) {
603 _cleanup_free_
void *buf
= NULL
;
610 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "BootOrder", NULL
, &buf
, &l
);
617 if (l
% sizeof(uint16_t) > 0 ||
618 l
/ sizeof(uint16_t) > INT_MAX
)
621 *order
= TAKE_PTR(buf
);
622 return (int) (l
/ sizeof(uint16_t));
625 int efi_set_boot_order(uint16_t *order
, size_t n
) {
630 return efi_set_variable(EFI_VENDOR_GLOBAL
, "BootOrder", order
, n
* sizeof(uint16_t));
633 static int boot_id_hex(const char s
[4]) {
636 for (i
= 0; i
< 4; i
++)
637 if (s
[i
] >= '0' && s
[i
] <= '9')
638 id
|= (s
[i
] - '0') << (3 - i
) * 4;
639 else if (s
[i
] >= 'A' && s
[i
] <= 'F')
640 id
|= (s
[i
] - 'A' + 10) << (3 - i
) * 4;
647 static int cmp_uint16(const uint16_t *a
, const uint16_t *b
) {
651 int efi_get_boot_options(uint16_t **options
) {
652 _cleanup_closedir_
DIR *dir
= NULL
;
653 _cleanup_free_
uint16_t *list
= NULL
;
663 dir
= opendir("/sys/firmware/efi/efivars/");
667 FOREACH_DIRENT(de
, dir
, return -errno
) {
670 if (strncmp(de
->d_name
, "Boot", 4) != 0)
673 if (strlen(de
->d_name
) != 45)
676 if (strcmp(de
->d_name
+ 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
679 id
= boot_id_hex(de
->d_name
+ 4);
683 if (!GREEDY_REALLOC(list
, alloc
, count
+ 1))
689 typesafe_qsort(list
, count
, cmp_uint16
);
691 *options
= TAKE_PTR(list
);
696 static int read_usec(sd_id128_t vendor
, const char *name
, usec_t
*u
) {
697 _cleanup_free_
char *j
= NULL
;
704 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, name
, &j
);
708 r
= safe_atou64(j
, &x
);
716 int efi_loader_get_boot_usec(usec_t
*firmware
, usec_t
*loader
) {
726 r
= read_usec(EFI_VENDOR_LOADER
, "LoaderTimeInitUSec", &x
);
730 r
= read_usec(EFI_VENDOR_LOADER
, "LoaderTimeExecUSec", &y
);
737 if (y
> USEC_PER_HOUR
)
746 int efi_loader_get_device_part_uuid(sd_id128_t
*u
) {
747 _cleanup_free_
char *p
= NULL
;
753 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderDevicePartUUID", &p
);
757 if (sscanf(p
, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
758 &parsed
[0], &parsed
[1], &parsed
[2], &parsed
[3],
759 &parsed
[4], &parsed
[5], &parsed
[6], &parsed
[7],
760 &parsed
[8], &parsed
[9], &parsed
[10], &parsed
[11],
761 &parsed
[12], &parsed
[13], &parsed
[14], &parsed
[15]) != 16)
767 for (i
= 0; i
< ELEMENTSOF(parsed
); i
++)
768 u
->bytes
[i
] = parsed
[i
];
774 bool efi_loader_entry_name_valid(const char *s
) {
778 if (strlen(s
) > FILENAME_MAX
) /* Make sure entry names fit in filenames */
781 return in_charset(s
, ALPHANUMERICAL
"-");
784 int efi_loader_get_entries(char ***ret
) {
785 _cleanup_free_ char16_t
*entries
= NULL
;
786 _cleanup_strv_free_
char **l
= NULL
;
787 size_t size
, i
, start
;
795 r
= efi_get_variable(EFI_VENDOR_LOADER
, "LoaderEntries", NULL
, (void**) &entries
, &size
);
799 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
801 for (i
= 0, start
= 0;; i
++) {
802 _cleanup_free_
char *decoded
= NULL
;
805 /* Is this the end of the variable's data? */
806 end
= i
* sizeof(char16_t
) >= size
;
808 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
809 * so, let's go to the next entry. */
810 if (!end
&& entries
[i
] != 0)
813 /* We reached the end of a string, let's decode it into UTF-8 */
814 decoded
= utf16_to_utf8(entries
+ start
, (i
- start
) * sizeof(char16_t
));
818 if (efi_loader_entry_name_valid(decoded
)) {
819 r
= strv_consume(&l
, TAKE_PTR(decoded
));
823 log_debug("Ignoring invalid loader entry '%s'.", decoded
);
825 /* We reached the end of the variable */
829 /* Continue after the NUL byte */
837 int efi_loader_get_features(uint64_t *ret
) {
838 _cleanup_free_
void *v
= NULL
;
842 if (!is_efi_boot()) {
847 r
= efi_get_variable(EFI_VENDOR_LOADER
, "LoaderFeatures", NULL
, &v
, &s
);
849 _cleanup_free_
char *info
= NULL
;
851 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
852 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderInfo", &info
);
857 /* Variable not set, definitely means not systemd-boot */
859 } else if (first_word(info
, "systemd-boot")) {
861 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
862 * static in all its versions. */
864 *ret
= EFI_LOADER_FEATURE_CONFIG_TIMEOUT
|
865 EFI_LOADER_FEATURE_ENTRY_DEFAULT
|
866 EFI_LOADER_FEATURE_ENTRY_ONESHOT
;
871 /* No features supported */
878 if (s
!= sizeof(uint64_t)) {
879 log_debug("LoaderFeatures EFI variable doesn't have the right size.");
883 memcpy(ret
, v
, sizeof(uint64_t));
889 char *efi_tilt_backslashes(char *s
) {