1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "dirent-util.h"
9 #include "efi-loader.h"
13 #include "parse-util.h"
14 #include "sort-util.h"
15 #include "stat-util.h"
16 #include "stdio-util.h"
17 #include "string-util.h"
23 #define LOAD_OPTION_ACTIVE 0x00000001
24 #define MEDIA_DEVICE_PATH 0x04
25 #define MEDIA_HARDDRIVE_DP 0x01
26 #define MEDIA_FILEPATH_DP 0x04
27 #define SIGNATURE_TYPE_GUID 0x02
28 #define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
29 #define END_DEVICE_PATH_TYPE 0x7f
30 #define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff
31 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001
33 #define boot_option__contents \
40 struct boot_option boot_option__contents
;
41 struct boot_option__packed boot_option__contents _packed_
;
42 assert_cc(offsetof(struct boot_option
, title
) == offsetof(struct boot_option__packed
, title
));
43 /* sizeof(struct boot_option) != sizeof(struct boot_option__packed), so
44 * the *size* of the structure should not be used anywhere below. */
52 uint8_t signature_type
;
55 #define device_path__contents \
62 struct drive_path drive; \
66 struct device_path device_path__contents
;
67 struct device_path__packed device_path__contents _packed_
;
68 assert_cc(sizeof(struct device_path
) == sizeof(struct device_path__packed
));
70 int efi_reboot_to_firmware_supported(void) {
71 _cleanup_free_
void *v
= NULL
;
72 static int cache
= -1;
85 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "OsIndicationsSupported", NULL
, &v
, &s
);
87 goto not_supported
; /* variable doesn't exist? it's not supported then */
90 if (s
!= sizeof(uint64_t))
94 if (!(b
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
))
95 goto not_supported
; /* bit unset? it's not supported then */
105 static int get_os_indications(uint64_t *ret
) {
106 static struct stat cache_stat
= {};
107 _cleanup_free_
void *v
= NULL
;
108 _cleanup_free_
char *fn
= NULL
;
109 static uint64_t cache
;
110 struct stat new_stat
;
116 /* Let's verify general support first */
117 r
= efi_reboot_to_firmware_supported();
121 fn
= efi_variable_path(EFI_VENDOR_GLOBAL
, "OsIndications");
125 /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */
126 if (stat(fn
, &new_stat
) < 0) {
130 /* Doesn't exist? Then we can exit early (also see below) */
134 } else if (stat_inode_unmodified(&new_stat
, &cache_stat
)) {
135 /* inode didn't change, we can return the cached value */
140 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "OsIndications", NULL
, &v
, &s
);
142 /* Some firmware implementations that do support OsIndications and report that with
143 * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's
144 * pretend it's 0 then, to hide this implementation detail. Note that this call will return
145 * -ENOENT then only if the support for OsIndications is missing entirely, as determined by
146 * efi_reboot_to_firmware_supported() above. */
152 if (s
!= sizeof(uint64_t))
155 cache_stat
= new_stat
;
156 *ret
= cache
= *(uint64_t *)v
;
160 int efi_get_reboot_to_firmware(void) {
164 r
= get_os_indications(&b
);
168 return !!(b
& EFI_OS_INDICATIONS_BOOT_TO_FW_UI
);
171 int efi_set_reboot_to_firmware(bool value
) {
175 r
= get_os_indications(&b
);
179 b_new
= UPDATE_FLAG(b
, EFI_OS_INDICATIONS_BOOT_TO_FW_UI
, value
);
181 /* Avoid writing to efi vars store if we can due to firmware bugs. */
183 return efi_set_variable(EFI_VENDOR_GLOBAL
, "OsIndications", &b_new
, sizeof(uint64_t));
188 static ssize_t
utf16_size(const uint16_t *s
, size_t buf_len_bytes
) {
191 /* Returns the size of the string in bytes without the terminating two zero bytes */
193 if (buf_len_bytes
% sizeof(uint16_t) != 0)
196 while (l
< buf_len_bytes
/ sizeof(uint16_t)) {
198 return (l
+ 1) * sizeof(uint16_t);
202 return -EINVAL
; /* The terminator was not found */
212 static void efi_guid_to_id128(const void *guid
, sd_id128_t
*id128
) {
215 const struct guid
*uuid
= guid
;
217 memcpy(&u1
, &uuid
->u1
, sizeof(uint32_t));
218 id128
->bytes
[0] = (u1
>> 24) & 0xff;
219 id128
->bytes
[1] = (u1
>> 16) & 0xff;
220 id128
->bytes
[2] = (u1
>> 8) & 0xff;
221 id128
->bytes
[3] = u1
& 0xff;
222 memcpy(&u2
, &uuid
->u2
, sizeof(uint16_t));
223 id128
->bytes
[4] = (u2
>> 8) & 0xff;
224 id128
->bytes
[5] = u2
& 0xff;
225 memcpy(&u3
, &uuid
->u3
, sizeof(uint16_t));
226 id128
->bytes
[6] = (u3
>> 8) & 0xff;
227 id128
->bytes
[7] = u3
& 0xff;
228 memcpy(&id128
->bytes
[8], uuid
->u4
, sizeof(uuid
->u4
));
231 int efi_get_boot_option(
234 sd_id128_t
*part_uuid
,
239 _cleanup_free_
uint8_t *buf
= NULL
;
241 struct boot_option
*header
;
243 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
244 sd_id128_t p_uuid
= SD_ID128_NULL
;
250 xsprintf(boot_id
, "Boot%04X", id
);
251 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, boot_id
, NULL
, (void **)&buf
, &l
);
254 if (l
< offsetof(struct boot_option
, title
))
257 header
= (struct boot_option
*)buf
;
258 title_size
= utf16_size(header
->title
, l
- offsetof(struct boot_option
, title
));
263 s
= utf16_to_utf8(header
->title
, title_size
);
268 if (header
->path_len
> 0) {
272 doff
= offsetof(struct boot_option
, title
) + title_size
;
274 if (header
->path_len
> l
- doff
)
278 while (dnext
< header
->path_len
) {
279 struct device_path
*dpath
;
281 dpath
= (struct device_path
*)(dbuf
+ dnext
);
282 if (dpath
->length
< 4)
285 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
286 if (dpath
->type
== END_DEVICE_PATH_TYPE
&& dpath
->sub_type
== END_ENTIRE_DEVICE_PATH_SUBTYPE
)
289 dnext
+= dpath
->length
;
291 /* Type 0x04 – Media Device Path */
292 if (dpath
->type
!= MEDIA_DEVICE_PATH
)
295 /* Sub-Type 1 – Hard Drive */
296 if (dpath
->sub_type
== MEDIA_HARDDRIVE_DP
) {
297 /* 0x02 – GUID Partition Table */
298 if (dpath
->drive
.mbr_type
!= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
)
301 /* 0x02 – GUID signature */
302 if (dpath
->drive
.signature_type
!= SIGNATURE_TYPE_GUID
)
306 efi_guid_to_id128(dpath
->drive
.signature
, &p_uuid
);
310 /* Sub-Type 4 – File Path */
311 if (dpath
->sub_type
== MEDIA_FILEPATH_DP
&& !p
&& path
) {
312 p
= utf16_to_utf8(dpath
->path
, dpath
->length
-4);
316 efi_tilt_backslashes(p
);
323 *title
= TAKE_PTR(s
);
329 *active
= header
->attr
& LOAD_OPTION_ACTIVE
;
334 static void to_utf16(uint16_t *dest
, const char *src
) {
337 for (i
= 0; src
[i
] != '\0'; i
++)
342 static void id128_to_efi_guid(sd_id128_t id
, void *guid
) {
344 .u1
= id
.bytes
[0] << 24 | id
.bytes
[1] << 16 | id
.bytes
[2] << 8 | id
.bytes
[3],
345 .u2
= id
.bytes
[4] << 8 | id
.bytes
[5],
346 .u3
= id
.bytes
[6] << 8 | id
.bytes
[7],
348 memcpy(uuid
.u4
, id
.bytes
+8, sizeof(uuid
.u4
));
349 memcpy(guid
, &uuid
, sizeof(uuid
));
352 static uint16_t *tilt_slashes(uint16_t *s
) {
362 int efi_add_boot_option(
368 sd_id128_t part_uuid
,
371 size_t size
, title_len
, path_len
;
372 _cleanup_free_
char *buf
= NULL
;
373 struct boot_option
*option
;
374 struct device_path
*devicep
;
380 title_len
= (strlen(title
)+1) * 2;
381 path_len
= (strlen(path
)+1) * 2;
383 buf
= malloc0(offsetof(struct boot_option
, title
) + title_len
+
384 sizeof(struct drive_path
) +
385 sizeof(struct device_path
) + path_len
);
390 option
= (struct boot_option
*)buf
;
391 option
->attr
= LOAD_OPTION_ACTIVE
;
392 option
->path_len
= offsetof(struct device_path
, drive
) + sizeof(struct drive_path
) +
393 offsetof(struct device_path
, path
) + path_len
+
394 offsetof(struct device_path
, path
);
395 to_utf16(option
->title
, title
);
396 size
= offsetof(struct boot_option
, title
) + title_len
;
399 devicep
= (struct device_path
*)(buf
+ size
);
400 devicep
->type
= MEDIA_DEVICE_PATH
;
401 devicep
->sub_type
= MEDIA_HARDDRIVE_DP
;
402 devicep
->length
= offsetof(struct device_path
, drive
) + sizeof(struct drive_path
);
403 memcpy(&devicep
->drive
.part_nr
, &part
, sizeof(uint32_t));
404 memcpy(&devicep
->drive
.part_start
, &pstart
, sizeof(uint64_t));
405 memcpy(&devicep
->drive
.part_size
, &psize
, sizeof(uint64_t));
406 id128_to_efi_guid(part_uuid
, devicep
->drive
.signature
);
407 devicep
->drive
.mbr_type
= MBR_TYPE_EFI_PARTITION_TABLE_HEADER
;
408 devicep
->drive
.signature_type
= SIGNATURE_TYPE_GUID
;
409 size
+= devicep
->length
;
412 devicep
= (struct device_path
*)(buf
+ size
);
413 devicep
->type
= MEDIA_DEVICE_PATH
;
414 devicep
->sub_type
= MEDIA_FILEPATH_DP
;
415 devicep
->length
= offsetof(struct device_path
, path
) + path_len
;
416 to_utf16(devicep
->path
, path
);
417 tilt_slashes(devicep
->path
);
418 size
+= devicep
->length
;
421 devicep
= (struct device_path
*)(buf
+ size
);
422 devicep
->type
= END_DEVICE_PATH_TYPE
;
423 devicep
->sub_type
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
424 devicep
->length
= offsetof(struct device_path
, path
);
425 size
+= devicep
->length
;
427 xsprintf(boot_id
, "Boot%04X", id
);
428 return efi_set_variable(EFI_VENDOR_GLOBAL
, boot_id
, buf
, size
);
431 int efi_remove_boot_option(uint16_t id
) {
437 xsprintf(boot_id
, "Boot%04X", id
);
438 return efi_set_variable(EFI_VENDOR_GLOBAL
, boot_id
, NULL
, 0);
441 int efi_get_boot_order(uint16_t **order
) {
442 _cleanup_free_
void *buf
= NULL
;
449 r
= efi_get_variable(EFI_VENDOR_GLOBAL
, "BootOrder", NULL
, &buf
, &l
);
456 if (l
% sizeof(uint16_t) > 0 ||
457 l
/ sizeof(uint16_t) > INT_MAX
)
460 *order
= TAKE_PTR(buf
);
461 return (int) (l
/ sizeof(uint16_t));
464 int efi_set_boot_order(uint16_t *order
, size_t n
) {
469 return efi_set_variable(EFI_VENDOR_GLOBAL
, "BootOrder", order
, n
* sizeof(uint16_t));
472 static int boot_id_hex(const char s
[static 4]) {
477 for (i
= 0; i
< 4; i
++)
478 if (s
[i
] >= '0' && s
[i
] <= '9')
479 id
|= (s
[i
] - '0') << (3 - i
) * 4;
480 else if (s
[i
] >= 'A' && s
[i
] <= 'F')
481 id
|= (s
[i
] - 'A' + 10) << (3 - i
) * 4;
488 static int cmp_uint16(const uint16_t *a
, const uint16_t *b
) {
492 int efi_get_boot_options(uint16_t **options
) {
493 _cleanup_closedir_
DIR *dir
= NULL
;
494 _cleanup_free_
uint16_t *list
= NULL
;
504 dir
= opendir("/sys/firmware/efi/efivars/");
508 FOREACH_DIRENT(de
, dir
, return -errno
) {
511 if (strncmp(de
->d_name
, "Boot", 4) != 0)
514 if (strlen(de
->d_name
) != 45)
517 if (strcmp(de
->d_name
+ 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
520 id
= boot_id_hex(de
->d_name
+ 4);
524 if (!GREEDY_REALLOC(list
, alloc
, count
+ 1))
530 typesafe_qsort(list
, count
, cmp_uint16
);
532 *options
= TAKE_PTR(list
);
537 static int read_usec(sd_id128_t vendor
, const char *name
, usec_t
*u
) {
538 _cleanup_free_
char *j
= NULL
;
545 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, name
, &j
);
549 r
= safe_atou64(j
, &x
);
557 int efi_loader_get_boot_usec(usec_t
*firmware
, usec_t
*loader
) {
567 r
= read_usec(EFI_VENDOR_LOADER
, "LoaderTimeInitUSec", &x
);
569 return log_debug_errno(r
, "Failed to read LoaderTimeInitUSec: %m");
571 r
= read_usec(EFI_VENDOR_LOADER
, "LoaderTimeExecUSec", &y
);
573 return log_debug_errno(r
, "Failed to read LoaderTimeExecUSec: %m");
575 if (y
== 0 || y
< x
|| y
- x
> USEC_PER_HOUR
)
576 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
577 "Bad LoaderTimeInitUSec=%"PRIu64
", LoaderTimeExecUSec=%" PRIu64
"; refusing.",
586 int efi_loader_get_device_part_uuid(sd_id128_t
*u
) {
587 _cleanup_free_
char *p
= NULL
;
593 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderDevicePartUUID", &p
);
597 if (sscanf(p
, SD_ID128_UUID_FORMAT_STR
,
598 &parsed
[0], &parsed
[1], &parsed
[2], &parsed
[3],
599 &parsed
[4], &parsed
[5], &parsed
[6], &parsed
[7],
600 &parsed
[8], &parsed
[9], &parsed
[10], &parsed
[11],
601 &parsed
[12], &parsed
[13], &parsed
[14], &parsed
[15]) != 16)
607 for (i
= 0; i
< ELEMENTSOF(parsed
); i
++)
608 u
->bytes
[i
] = parsed
[i
];
614 int efi_loader_get_entries(char ***ret
) {
615 _cleanup_free_ char16_t
*entries
= NULL
;
616 _cleanup_strv_free_
char **l
= NULL
;
617 size_t size
, i
, start
;
625 r
= efi_get_variable(EFI_VENDOR_LOADER
, "LoaderEntries", NULL
, (void**) &entries
, &size
);
629 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
631 for (i
= 0, start
= 0;; i
++) {
632 _cleanup_free_
char *decoded
= NULL
;
635 /* Is this the end of the variable's data? */
636 end
= i
* sizeof(char16_t
) >= size
;
638 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
639 * so, let's go to the next entry. */
640 if (!end
&& entries
[i
] != 0)
643 /* We reached the end of a string, let's decode it into UTF-8 */
644 decoded
= utf16_to_utf8(entries
+ start
, (i
- start
) * sizeof(char16_t
));
648 if (efi_loader_entry_name_valid(decoded
)) {
649 r
= strv_consume(&l
, TAKE_PTR(decoded
));
653 log_debug("Ignoring invalid loader entry '%s'.", decoded
);
655 /* We reached the end of the variable */
659 /* Continue after the NUL byte */
667 int efi_loader_get_features(uint64_t *ret
) {
668 _cleanup_free_
void *v
= NULL
;
672 if (!is_efi_boot()) {
677 r
= efi_get_variable(EFI_VENDOR_LOADER
, "LoaderFeatures", NULL
, &v
, &s
);
679 _cleanup_free_
char *info
= NULL
;
681 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
682 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderInfo", &info
);
687 /* Variable not set, definitely means not systemd-boot */
689 } else if (first_word(info
, "systemd-boot")) {
691 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
692 * static in all its versions. */
694 *ret
= EFI_LOADER_FEATURE_CONFIG_TIMEOUT
|
695 EFI_LOADER_FEATURE_ENTRY_DEFAULT
|
696 EFI_LOADER_FEATURE_ENTRY_ONESHOT
;
701 /* No features supported */
708 if (s
!= sizeof(uint64_t))
709 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
710 "LoaderFeatures EFI variable doesn't have the right size.");
712 memcpy(ret
, v
, sizeof(uint64_t));
718 bool efi_loader_entry_name_valid(const char *s
) {
722 if (strlen(s
) > FILENAME_MAX
) /* Make sure entry names fit in filenames */
725 return in_charset(s
, ALPHANUMERICAL
"+-_.");
728 char *efi_tilt_backslashes(char *s
) {