1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "efi-loader.h"
5 #include "parse-util.h"
13 static int read_usec(const char *variable
, usec_t
*ret
) {
14 _cleanup_free_
char *j
= NULL
;
21 r
= efi_get_variable_string(variable
, &j
);
25 r
= safe_atou64(j
, &x
);
33 int efi_loader_get_boot_usec(usec_t
*ret_firmware
, usec_t
*ret_loader
) {
43 r
= read_usec(EFI_LOADER_VARIABLE(LoaderTimeInitUSec
), &x
);
45 return log_debug_errno(r
, "Failed to read LoaderTimeInitUSec: %m");
47 r
= read_usec(EFI_LOADER_VARIABLE(LoaderTimeExecUSec
), &y
);
49 return log_debug_errno(r
, "Failed to read LoaderTimeExecUSec: %m");
51 if (y
== 0 || y
< x
|| y
- x
> USEC_PER_HOUR
)
52 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
53 "Bad LoaderTimeInitUSec=%"PRIu64
", LoaderTimeExecUSec=%" PRIu64
"; refusing.",
61 int efi_loader_get_device_part_uuid(sd_id128_t
*ret
) {
62 _cleanup_free_
char *p
= NULL
;
68 r
= efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderDevicePartUUID
), &p
);
72 if (sscanf(p
, SD_ID128_UUID_FORMAT_STR
,
73 &parsed
[0], &parsed
[1], &parsed
[2], &parsed
[3],
74 &parsed
[4], &parsed
[5], &parsed
[6], &parsed
[7],
75 &parsed
[8], &parsed
[9], &parsed
[10], &parsed
[11],
76 &parsed
[12], &parsed
[13], &parsed
[14], &parsed
[15]) != 16)
80 for (unsigned i
= 0; i
< ELEMENTSOF(parsed
); i
++)
81 ret
->bytes
[i
] = parsed
[i
];
86 int efi_loader_get_entries(char ***ret
) {
87 _cleanup_free_ char16_t
*entries
= NULL
;
88 _cleanup_strv_free_
char **l
= NULL
;
97 r
= efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntries
), NULL
, (void**) &entries
, &size
);
101 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
103 for (size_t i
= 0, start
= 0;; i
++) {
104 _cleanup_free_
char *decoded
= NULL
;
107 /* Is this the end of the variable's data? */
108 end
= i
* sizeof(char16_t
) >= size
;
110 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
111 * so, let's go to the next entry. */
112 if (!end
&& entries
[i
] != 0)
115 /* We reached the end of a string, let's decode it into UTF-8 */
116 decoded
= utf16_to_utf8(entries
+ start
, (i
- start
) * sizeof(char16_t
));
120 if (efi_loader_entry_name_valid(decoded
)) {
121 r
= strv_consume(&l
, TAKE_PTR(decoded
));
125 log_debug("Ignoring invalid loader entry '%s'.", decoded
);
127 /* We reached the end of the variable */
131 /* Continue after the NUL byte */
139 int efi_loader_get_features(uint64_t *ret
) {
140 _cleanup_free_
void *v
= NULL
;
146 if (!is_efi_boot()) {
151 r
= efi_get_variable(EFI_LOADER_VARIABLE(LoaderFeatures
), NULL
, &v
, &s
);
153 _cleanup_free_
char *info
= NULL
;
155 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
156 r
= efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderInfo
), &info
);
161 /* Variable not set, definitely means not systemd-boot */
163 } else if (first_word(info
, "systemd-boot")) {
165 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
166 * static in all its versions. */
168 *ret
= EFI_LOADER_FEATURE_CONFIG_TIMEOUT
|
169 EFI_LOADER_FEATURE_ENTRY_DEFAULT
|
170 EFI_LOADER_FEATURE_ENTRY_ONESHOT
;
175 /* No features supported */
182 if (s
!= sizeof(uint64_t))
183 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
184 "LoaderFeatures EFI variable doesn't have the right size.");
186 memcpy(ret
, v
, sizeof(uint64_t));
190 int efi_loader_get_config_timeout_one_shot(usec_t
*ret
) {
191 _cleanup_free_
char *v
= NULL
;
192 static struct stat cache_stat
= {};
193 struct stat new_stat
;
200 /* stat() the EFI variable, to see if the mtime changed. If it did, we need to cache again. */
201 if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
)), &new_stat
) < 0)
204 if (stat_inode_unmodified(&new_stat
, &cache_stat
)) {
209 r
= efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), &v
);
213 r
= safe_atou64(v
, &sec
);
216 if (sec
> USEC_INFINITY
/ USEC_PER_SEC
)
219 cache_stat
= new_stat
;
220 *ret
= cache
= sec
* USEC_PER_SEC
; /* return in µs */
224 int efi_loader_update_entry_one_shot_cache(char **cache
, struct stat
*cache_stat
) {
225 _cleanup_free_
char *v
= NULL
;
226 struct stat new_stat
;
232 /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */
233 if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntryOneShot
)), &new_stat
) < 0)
236 if (stat_inode_unmodified(&new_stat
, cache_stat
))
239 r
= efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), &v
);
243 if (!efi_loader_entry_name_valid(v
))
246 *cache_stat
= new_stat
;
247 free_and_replace(*cache
, v
);
254 bool efi_loader_entry_name_valid(const char *s
) {
255 if (!filename_is_valid(s
)) /* Make sure entry names fit in filenames */
258 return in_charset(s
, ALPHANUMERICAL
"+-_.");