]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/efi-loader.c
Define FOREACH_DIRENT through FOREACH_DIRENT_ALL
[thirdparty/systemd.git] / src / shared / efi-loader.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "dirent-util.h"
9 #include "efi-loader.h"
10 #include "efivars.h"
11 #include "fd-util.h"
12 #include "io-util.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"
18 #include "utf8.h"
19 #include "virt.h"
20
21 #if ENABLE_EFI
22
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
32
33 #define boot_option__contents \
34 { \
35 uint32_t attr; \
36 uint16_t path_len; \
37 uint16_t title[]; \
38 }
39
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. */
45
46 struct drive_path {
47 uint32_t part_nr;
48 uint64_t part_start;
49 uint64_t part_size;
50 char signature[16];
51 uint8_t mbr_type;
52 uint8_t signature_type;
53 } _packed_;
54
55 #define device_path__contents \
56 { \
57 uint8_t type; \
58 uint8_t sub_type; \
59 uint16_t length; \
60 union { \
61 uint16_t path[0]; \
62 struct drive_path drive; \
63 }; \
64 }
65
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));
69
70 int efi_reboot_to_firmware_supported(void) {
71 _cleanup_free_ void *v = NULL;
72 static int cache = -1;
73 uint64_t b;
74 size_t s;
75 int r;
76
77 if (cache > 0)
78 return 0;
79 if (cache == 0)
80 return -EOPNOTSUPP;
81
82 if (!is_efi_boot())
83 goto not_supported;
84
85 r = efi_get_variable(EFI_GLOBAL_VARIABLE(OsIndicationsSupported), NULL, &v, &s);
86 if (r == -ENOENT)
87 goto not_supported; /* variable doesn't exist? it's not supported then */
88 if (r < 0)
89 return r;
90 if (s != sizeof(uint64_t))
91 return -EINVAL;
92
93 b = *(uint64_t*) v;
94 if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI))
95 goto not_supported; /* bit unset? it's not supported then */
96
97 cache = 1;
98 return 0;
99
100 not_supported:
101 cache = 0;
102 return -EOPNOTSUPP;
103 }
104
105 static int get_os_indications(uint64_t *ret) {
106 static struct stat cache_stat = {};
107 _cleanup_free_ void *v = NULL;
108 static uint64_t cache;
109 struct stat new_stat;
110 size_t s;
111 int r;
112
113 assert(ret);
114
115 /* Let's verify general support first */
116 r = efi_reboot_to_firmware_supported();
117 if (r < 0)
118 return r;
119
120 /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */
121 if (stat(EFIVAR_PATH(EFI_GLOBAL_VARIABLE(OsIndications)), &new_stat) < 0) {
122 if (errno != ENOENT)
123 return -errno;
124
125 /* Doesn't exist? Then we can exit early (also see below) */
126 *ret = 0;
127 return 0;
128
129 } else if (stat_inode_unmodified(&new_stat, &cache_stat)) {
130 /* inode didn't change, we can return the cached value */
131 *ret = cache;
132 return 0;
133 }
134
135 r = efi_get_variable(EFI_GLOBAL_VARIABLE(OsIndications), NULL, &v, &s);
136 if (r == -ENOENT) {
137 /* Some firmware implementations that do support OsIndications and report that with
138 * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's
139 * pretend it's 0 then, to hide this implementation detail. Note that this call will return
140 * -ENOENT then only if the support for OsIndications is missing entirely, as determined by
141 * efi_reboot_to_firmware_supported() above. */
142 *ret = 0;
143 return 0;
144 }
145 if (r < 0)
146 return r;
147 if (s != sizeof(uint64_t))
148 return -EINVAL;
149
150 cache_stat = new_stat;
151 *ret = cache = *(uint64_t *)v;
152 return 0;
153 }
154
155 int efi_get_reboot_to_firmware(void) {
156 int r;
157 uint64_t b;
158
159 r = get_os_indications(&b);
160 if (r < 0)
161 return r;
162
163 return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI);
164 }
165
166 int efi_set_reboot_to_firmware(bool value) {
167 int r;
168 uint64_t b, b_new;
169
170 r = get_os_indications(&b);
171 if (r < 0)
172 return r;
173
174 b_new = UPDATE_FLAG(b, EFI_OS_INDICATIONS_BOOT_TO_FW_UI, value);
175
176 /* Avoid writing to efi vars store if we can due to firmware bugs. */
177 if (b != b_new)
178 return efi_set_variable(EFI_GLOBAL_VARIABLE(OsIndications), &b_new, sizeof(uint64_t));
179
180 return 0;
181 }
182
183 static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) {
184 size_t l = 0;
185
186 /* Returns the size of the string in bytes without the terminating two zero bytes */
187
188 if (buf_len_bytes % sizeof(uint16_t) != 0)
189 return -EINVAL;
190
191 while (l < buf_len_bytes / sizeof(uint16_t)) {
192 if (s[l] == 0)
193 return (l + 1) * sizeof(uint16_t);
194 l++;
195 }
196
197 return -EINVAL; /* The terminator was not found */
198 }
199
200 struct guid {
201 uint32_t u1;
202 uint16_t u2;
203 uint16_t u3;
204 uint8_t u4[8];
205 } _packed_;
206
207 static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
208 uint32_t u1;
209 uint16_t u2, u3;
210 const struct guid *uuid = guid;
211
212 memcpy(&u1, &uuid->u1, sizeof(uint32_t));
213 id128->bytes[0] = (u1 >> 24) & 0xff;
214 id128->bytes[1] = (u1 >> 16) & 0xff;
215 id128->bytes[2] = (u1 >> 8) & 0xff;
216 id128->bytes[3] = u1 & 0xff;
217 memcpy(&u2, &uuid->u2, sizeof(uint16_t));
218 id128->bytes[4] = (u2 >> 8) & 0xff;
219 id128->bytes[5] = u2 & 0xff;
220 memcpy(&u3, &uuid->u3, sizeof(uint16_t));
221 id128->bytes[6] = (u3 >> 8) & 0xff;
222 id128->bytes[7] = u3 & 0xff;
223 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
224 }
225
226 int efi_get_boot_option(
227 uint16_t id,
228 char **title,
229 sd_id128_t *part_uuid,
230 char **path,
231 bool *active) {
232
233 char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1];
234 _cleanup_free_ uint8_t *buf = NULL;
235 size_t l;
236 struct boot_option *header;
237 ssize_t title_size;
238 _cleanup_free_ char *s = NULL, *p = NULL;
239 sd_id128_t p_uuid = SD_ID128_NULL;
240 int r;
241
242 if (!is_efi_boot())
243 return -EOPNOTSUPP;
244
245 xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id);
246 r = efi_get_variable(variable, NULL, (void **)&buf, &l);
247 if (r < 0)
248 return r;
249 if (l < offsetof(struct boot_option, title))
250 return -ENOENT;
251
252 header = (struct boot_option *)buf;
253 title_size = utf16_size(header->title, l - offsetof(struct boot_option, title));
254 if (title_size < 0)
255 return title_size;
256
257 if (title) {
258 s = utf16_to_utf8(header->title, title_size);
259 if (!s)
260 return -ENOMEM;
261 }
262
263 if (header->path_len > 0) {
264 uint8_t *dbuf;
265 size_t dnext, doff;
266
267 doff = offsetof(struct boot_option, title) + title_size;
268 dbuf = buf + doff;
269 if (header->path_len > l - doff)
270 return -EINVAL;
271
272 dnext = 0;
273 while (dnext < header->path_len) {
274 struct device_path *dpath;
275
276 dpath = (struct device_path *)(dbuf + dnext);
277 if (dpath->length < 4)
278 break;
279
280 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
281 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
282 break;
283
284 dnext += dpath->length;
285
286 /* Type 0x04 – Media Device Path */
287 if (dpath->type != MEDIA_DEVICE_PATH)
288 continue;
289
290 /* Sub-Type 1 – Hard Drive */
291 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
292 /* 0x02 – GUID Partition Table */
293 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
294 continue;
295
296 /* 0x02 – GUID signature */
297 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
298 continue;
299
300 if (part_uuid)
301 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
302 continue;
303 }
304
305 /* Sub-Type 4 – File Path */
306 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
307 p = utf16_to_utf8(dpath->path, dpath->length-4);
308 if (!p)
309 return -ENOMEM;
310
311 efi_tilt_backslashes(p);
312 continue;
313 }
314 }
315 }
316
317 if (title)
318 *title = TAKE_PTR(s);
319 if (part_uuid)
320 *part_uuid = p_uuid;
321 if (path)
322 *path = TAKE_PTR(p);
323 if (active)
324 *active = header->attr & LOAD_OPTION_ACTIVE;
325
326 return 0;
327 }
328
329 static void to_utf16(uint16_t *dest, const char *src) {
330 int i;
331
332 for (i = 0; src[i] != '\0'; i++)
333 dest[i] = src[i];
334 dest[i] = '\0';
335 }
336
337 static void id128_to_efi_guid(sd_id128_t id, void *guid) {
338 struct guid uuid = {
339 .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3],
340 .u2 = id.bytes[4] << 8 | id.bytes[5],
341 .u3 = id.bytes[6] << 8 | id.bytes[7],
342 };
343 memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4));
344 memcpy(guid, &uuid, sizeof(uuid));
345 }
346
347 static uint16_t *tilt_slashes(uint16_t *s) {
348 for (uint16_t *p = s; *p; p++)
349 if (*p == '/')
350 *p = '\\';
351
352 return s;
353 }
354
355 int efi_add_boot_option(
356 uint16_t id,
357 const char *title,
358 uint32_t part,
359 uint64_t pstart,
360 uint64_t psize,
361 sd_id128_t part_uuid,
362 const char *path) {
363
364 size_t size, title_len, path_len;
365 _cleanup_free_ char *buf = NULL;
366 struct boot_option *option;
367 struct device_path *devicep;
368 char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1];
369
370 if (!is_efi_boot())
371 return -EOPNOTSUPP;
372
373 title_len = (strlen(title)+1) * 2;
374 path_len = (strlen(path)+1) * 2;
375
376 buf = malloc0(offsetof(struct boot_option, title) + title_len +
377 sizeof(struct drive_path) +
378 sizeof(struct device_path) + path_len);
379 if (!buf)
380 return -ENOMEM;
381
382 /* header */
383 option = (struct boot_option *)buf;
384 option->attr = LOAD_OPTION_ACTIVE;
385 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
386 offsetof(struct device_path, path) + path_len +
387 offsetof(struct device_path, path);
388 to_utf16(option->title, title);
389 size = offsetof(struct boot_option, title) + title_len;
390
391 /* partition info */
392 devicep = (struct device_path *)(buf + size);
393 devicep->type = MEDIA_DEVICE_PATH;
394 devicep->sub_type = MEDIA_HARDDRIVE_DP;
395 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
396 memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t));
397 memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t));
398 memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t));
399 id128_to_efi_guid(part_uuid, devicep->drive.signature);
400 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
401 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
402 size += devicep->length;
403
404 /* path to loader */
405 devicep = (struct device_path *)(buf + size);
406 devicep->type = MEDIA_DEVICE_PATH;
407 devicep->sub_type = MEDIA_FILEPATH_DP;
408 devicep->length = offsetof(struct device_path, path) + path_len;
409 to_utf16(devicep->path, path);
410 tilt_slashes(devicep->path);
411 size += devicep->length;
412
413 /* end of path */
414 devicep = (struct device_path *)(buf + size);
415 devicep->type = END_DEVICE_PATH_TYPE;
416 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
417 devicep->length = offsetof(struct device_path, path);
418 size += devicep->length;
419
420 xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id);
421 return efi_set_variable(variable, buf, size);
422 }
423
424 int efi_remove_boot_option(uint16_t id) {
425 char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1];
426
427 if (!is_efi_boot())
428 return -EOPNOTSUPP;
429
430 xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id);
431 return efi_set_variable(variable, NULL, 0);
432 }
433
434 int efi_get_boot_order(uint16_t **order) {
435 _cleanup_free_ void *buf = NULL;
436 size_t l;
437 int r;
438
439 if (!is_efi_boot())
440 return -EOPNOTSUPP;
441
442 r = efi_get_variable(EFI_GLOBAL_VARIABLE(BootOrder), NULL, &buf, &l);
443 if (r < 0)
444 return r;
445
446 if (l <= 0)
447 return -ENOENT;
448
449 if (l % sizeof(uint16_t) > 0 ||
450 l / sizeof(uint16_t) > INT_MAX)
451 return -EINVAL;
452
453 *order = TAKE_PTR(buf);
454 return (int) (l / sizeof(uint16_t));
455 }
456
457 int efi_set_boot_order(uint16_t *order, size_t n) {
458
459 if (!is_efi_boot())
460 return -EOPNOTSUPP;
461
462 return efi_set_variable(EFI_GLOBAL_VARIABLE(BootOrder), order, n * sizeof(uint16_t));
463 }
464
465 static int boot_id_hex(const char s[static 4]) {
466 int id = 0;
467
468 assert(s);
469
470 for (int i = 0; i < 4; i++)
471 if (s[i] >= '0' && s[i] <= '9')
472 id |= (s[i] - '0') << (3 - i) * 4;
473 else if (s[i] >= 'A' && s[i] <= 'F')
474 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
475 else
476 return -EINVAL;
477
478 return id;
479 }
480
481 static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
482 return CMP(*a, *b);
483 }
484
485 int efi_get_boot_options(uint16_t **options) {
486 _cleanup_closedir_ DIR *dir = NULL;
487 _cleanup_free_ uint16_t *list = NULL;
488 int count = 0;
489
490 assert(options);
491
492 if (!is_efi_boot())
493 return -EOPNOTSUPP;
494
495 dir = opendir(EFIVAR_PATH("."));
496 if (!dir)
497 return -errno;
498
499 FOREACH_DIRENT(de, dir, return -errno) {
500 int id;
501
502 if (strncmp(de->d_name, "Boot", 4) != 0)
503 continue;
504
505 if (strlen(de->d_name) != 45)
506 continue;
507
508 if (strcmp(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR("")) != 0) /* generate variable suffix using macro */
509 continue;
510
511 id = boot_id_hex(de->d_name + 4);
512 if (id < 0)
513 continue;
514
515 if (!GREEDY_REALLOC(list, count + 1))
516 return -ENOMEM;
517
518 list[count++] = id;
519 }
520
521 typesafe_qsort(list, count, cmp_uint16);
522
523 *options = TAKE_PTR(list);
524
525 return count;
526 }
527
528 static int read_usec(const char *variable, usec_t *u) {
529 _cleanup_free_ char *j = NULL;
530 int r;
531 uint64_t x = 0;
532
533 assert(variable);
534 assert(u);
535
536 r = efi_get_variable_string(variable, &j);
537 if (r < 0)
538 return r;
539
540 r = safe_atou64(j, &x);
541 if (r < 0)
542 return r;
543
544 *u = x;
545 return 0;
546 }
547
548 int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
549 uint64_t x, y;
550 int r;
551
552 assert(firmware);
553 assert(loader);
554
555 if (!is_efi_boot())
556 return -EOPNOTSUPP;
557
558 r = read_usec(EFI_LOADER_VARIABLE(LoaderTimeInitUSec), &x);
559 if (r < 0)
560 return log_debug_errno(r, "Failed to read LoaderTimeInitUSec: %m");
561
562 r = read_usec(EFI_LOADER_VARIABLE(LoaderTimeExecUSec), &y);
563 if (r < 0)
564 return log_debug_errno(r, "Failed to read LoaderTimeExecUSec: %m");
565
566 if (y == 0 || y < x || y - x > USEC_PER_HOUR)
567 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
568 "Bad LoaderTimeInitUSec=%"PRIu64", LoaderTimeExecUSec=%" PRIu64"; refusing.",
569 x, y);
570
571 *firmware = x;
572 *loader = y;
573
574 return 0;
575 }
576
577 int efi_loader_get_device_part_uuid(sd_id128_t *u) {
578 _cleanup_free_ char *p = NULL;
579 int r, parsed[16];
580
581 if (!is_efi_boot())
582 return -EOPNOTSUPP;
583
584 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderDevicePartUUID), &p);
585 if (r < 0)
586 return r;
587
588 if (sscanf(p, SD_ID128_UUID_FORMAT_STR,
589 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
590 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
591 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
592 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
593 return -EIO;
594
595 if (u)
596 for (unsigned i = 0; i < ELEMENTSOF(parsed); i++)
597 u->bytes[i] = parsed[i];
598
599 return 0;
600 }
601
602 int efi_loader_get_entries(char ***ret) {
603 _cleanup_free_ char16_t *entries = NULL;
604 _cleanup_strv_free_ char **l = NULL;
605 size_t size;
606 int r;
607
608 assert(ret);
609
610 if (!is_efi_boot())
611 return -EOPNOTSUPP;
612
613 r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderEntries), NULL, (void**) &entries, &size);
614 if (r < 0)
615 return r;
616
617 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
618
619 for (size_t i = 0, start = 0;; i++) {
620 _cleanup_free_ char *decoded = NULL;
621 bool end;
622
623 /* Is this the end of the variable's data? */
624 end = i * sizeof(char16_t) >= size;
625
626 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
627 * so, let's go to the next entry. */
628 if (!end && entries[i] != 0)
629 continue;
630
631 /* We reached the end of a string, let's decode it into UTF-8 */
632 decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t));
633 if (!decoded)
634 return -ENOMEM;
635
636 if (efi_loader_entry_name_valid(decoded)) {
637 r = strv_consume(&l, TAKE_PTR(decoded));
638 if (r < 0)
639 return r;
640 } else
641 log_debug("Ignoring invalid loader entry '%s'.", decoded);
642
643 /* We reached the end of the variable */
644 if (end)
645 break;
646
647 /* Continue after the NUL byte */
648 start = i + 1;
649 }
650
651 *ret = TAKE_PTR(l);
652 return 0;
653 }
654
655 int efi_loader_get_features(uint64_t *ret) {
656 _cleanup_free_ void *v = NULL;
657 size_t s;
658 int r;
659
660 if (!is_efi_boot()) {
661 *ret = 0;
662 return 0;
663 }
664
665 r = efi_get_variable(EFI_LOADER_VARIABLE(LoaderFeatures), NULL, &v, &s);
666 if (r == -ENOENT) {
667 _cleanup_free_ char *info = NULL;
668
669 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
670 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderInfo), &info);
671 if (r < 0) {
672 if (r != -ENOENT)
673 return r;
674
675 /* Variable not set, definitely means not systemd-boot */
676
677 } else if (first_word(info, "systemd-boot")) {
678
679 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
680 * static in all its versions. */
681
682 *ret = EFI_LOADER_FEATURE_CONFIG_TIMEOUT |
683 EFI_LOADER_FEATURE_ENTRY_DEFAULT |
684 EFI_LOADER_FEATURE_ENTRY_ONESHOT;
685
686 return 0;
687 }
688
689 /* No features supported */
690 *ret = 0;
691 return 0;
692 }
693 if (r < 0)
694 return r;
695
696 if (s != sizeof(uint64_t))
697 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
698 "LoaderFeatures EFI variable doesn't have the right size.");
699
700 memcpy(ret, v, sizeof(uint64_t));
701 return 0;
702 }
703
704 int efi_loader_get_config_timeout_one_shot(usec_t *ret) {
705 _cleanup_free_ char *v = NULL;
706 static struct stat cache_stat = {};
707 struct stat new_stat;
708 static usec_t cache;
709 uint64_t sec;
710 int r;
711
712 assert(ret);
713
714 /* stat() the EFI variable, to see if the mtime changed. If it did, we need to cache again. */
715 if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot)), &new_stat) < 0)
716 return -errno;
717
718 if (stat_inode_unmodified(&new_stat, &cache_stat)) {
719 *ret = cache;
720 return 0;
721 }
722
723 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot), &v);
724 if (r < 0)
725 return r;
726
727 r = safe_atou64(v, &sec);
728 if (r < 0)
729 return r;
730 if (sec > USEC_INFINITY / USEC_PER_SEC)
731 return -ERANGE;
732
733 cache_stat = new_stat;
734 *ret = cache = sec * USEC_PER_SEC; /* return in µs */
735 return 0;
736 }
737
738 int efi_loader_update_entry_one_shot_cache(char **cache, struct stat *cache_stat) {
739 _cleanup_free_ char *v = NULL;
740 struct stat new_stat;
741 int r;
742
743 assert(cache);
744 assert(cache_stat);
745
746 /* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */
747 if (stat(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntryOneShot)), &new_stat) < 0)
748 return -errno;
749
750 if (stat_inode_unmodified(&new_stat, cache_stat))
751 return 0;
752
753 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), &v);
754 if (r < 0)
755 return r;
756
757 if (!efi_loader_entry_name_valid(v))
758 return -EINVAL;
759
760 *cache_stat = new_stat;
761 free_and_replace(*cache, v);
762
763 return 0;
764 }
765
766 bool efi_has_tpm2(void) {
767 static int cache = -1;
768
769 /* Returns whether the system has a TPM2 chip which is known to the EFI firmware. */
770
771 if (cache < 0) {
772
773 /* First, check if we are on an EFI boot at all. */
774 if (!is_efi_boot())
775 cache = false;
776 else {
777 /* Then, check if the ACPI table "TPM2" exists, which is the TPM2 event log table, see:
778 * https://trustedcomputinggroup.org/wp-content/uploads/TCG_ACPIGeneralSpecification_v1.20_r8.pdf
779 * This table exists whenever the firmware is hooked up to TPM2. */
780 cache = access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0;
781 if (!cache && errno != ENOENT)
782 log_debug_errno(errno, "Unable to test whether /sys/firmware/acpi/tables/TPM2 exists, assuming it doesn't: %m");
783 }
784 }
785
786 return cache;
787 }
788
789 #endif
790
791 bool efi_loader_entry_name_valid(const char *s) {
792 if (!filename_is_valid(s)) /* Make sure entry names fit in filenames */
793 return false;
794
795 return in_charset(s, ALPHANUMERICAL "+-_.");
796 }
797
798 char *efi_tilt_backslashes(char *s) {
799 for (char *p = s; *p; p++)
800 if (*p == '\\')
801 *p = '/';
802
803 return s;
804 }