]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/efivars.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / shared / efivars.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <dirent.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <linux/fs.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include "sd-id128.h"
15
16 #include "alloc-util.h"
17 #include "chattr-util.h"
18 #include "dirent-util.h"
19 #include "efivars.h"
20 #include "fd-util.h"
21 #include "io-util.h"
22 #include "macro.h"
23 #include "parse-util.h"
24 #include "stdio-util.h"
25 #include "strv.h"
26 #include "time-util.h"
27 #include "utf8.h"
28 #include "util.h"
29 #include "virt.h"
30
31 #if ENABLE_EFI
32
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
42
43 #define boot_option__contents { \
44 uint32_t attr; \
45 uint16_t path_len; \
46 uint16_t title[]; \
47 }
48
49 struct boot_option boot_option__contents;
50 struct boot_option__packed boot_option__contents _packed_;
51 assert_cc(offsetof(struct boot_option, title) == offsetof(struct boot_option__packed, title));
52 /* sizeof(struct boot_option) != sizeof(struct boot_option__packed), so
53 * the *size* of the structure should not be used anywhere below. */
54
55 struct drive_path {
56 uint32_t part_nr;
57 uint64_t part_start;
58 uint64_t part_size;
59 char signature[16];
60 uint8_t mbr_type;
61 uint8_t signature_type;
62 } _packed_;
63
64 #define device_path__contents { \
65 uint8_t type; \
66 uint8_t sub_type; \
67 uint16_t length; \
68 union { \
69 uint16_t path[0]; \
70 struct drive_path drive; \
71 }; \
72 }
73
74 struct device_path device_path__contents;
75 struct device_path__packed device_path__contents _packed_;
76 assert_cc(sizeof(struct device_path) == sizeof(struct device_path__packed));
77
78 bool is_efi_boot(void) {
79 if (detect_container() > 0)
80 return false;
81
82 return access("/sys/firmware/efi/", F_OK) >= 0;
83 }
84
85 static int read_flag(const char *varname) {
86 _cleanup_free_ void *v = NULL;
87 uint8_t b;
88 size_t s;
89 int r;
90
91 if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */
92 return 0;
93
94 r = efi_get_variable(EFI_VENDOR_GLOBAL, varname, NULL, &v, &s);
95 if (r < 0)
96 return r;
97
98 if (s != 1)
99 return -EINVAL;
100
101 b = *(uint8_t *)v;
102 return !!b;
103 }
104
105 bool is_efi_secure_boot(void) {
106 return read_flag("SecureBoot") > 0;
107 }
108
109 bool is_efi_secure_boot_setup_mode(void) {
110 return read_flag("SetupMode") > 0;
111 }
112
113 int efi_reboot_to_firmware_supported(void) {
114 _cleanup_free_ void *v = NULL;
115 uint64_t b;
116 size_t s;
117 int r;
118
119 if (!is_efi_boot())
120 return -EOPNOTSUPP;
121
122 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndicationsSupported", NULL, &v, &s);
123 if (r == -ENOENT) /* variable doesn't exist? it's not supported then */
124 return -EOPNOTSUPP;
125 if (r < 0)
126 return r;
127 if (s != sizeof(uint64_t))
128 return -EINVAL;
129
130 b = *(uint64_t*) v;
131 if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI))
132 return -EOPNOTSUPP; /* bit unset? it's not supported then */
133
134 return 0;
135 }
136
137 static int get_os_indications(uint64_t *os_indication) {
138 _cleanup_free_ void *v = NULL;
139 size_t s;
140 int r;
141
142 /* Let's verify general support first */
143 r = efi_reboot_to_firmware_supported();
144 if (r < 0)
145 return r;
146
147 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndications", NULL, &v, &s);
148 if (r == -ENOENT) {
149 /* Some firmware implementations that do support OsIndications and report that with
150 * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's pretend it's 0
151 * then, to hide this implementation detail. Note that this call will return -ENOENT then only if the
152 * support for OsIndications is missing entirely, as determined by efi_reboot_to_firmware_supported()
153 * above. */
154 *os_indication = 0;
155 return 0;
156 } else if (r < 0)
157 return r;
158 else if (s != sizeof(uint64_t))
159 return -EINVAL;
160
161 *os_indication = *(uint64_t *)v;
162 return 0;
163 }
164
165 int efi_get_reboot_to_firmware(void) {
166 int r;
167 uint64_t b;
168
169 r = get_os_indications(&b);
170 if (r < 0)
171 return r;
172
173 return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI);
174 }
175
176 int efi_set_reboot_to_firmware(bool value) {
177 int r;
178 uint64_t b, b_new;
179
180 r = get_os_indications(&b);
181 if (r < 0)
182 return r;
183
184 if (value)
185 b_new = b | EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
186 else
187 b_new = b & ~EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
188
189 /* Avoid writing to efi vars store if we can due to firmware bugs. */
190 if (b != b_new)
191 return efi_set_variable(EFI_VENDOR_GLOBAL, "OsIndications", &b_new, sizeof(uint64_t));
192
193 return 0;
194 }
195
196 int efi_get_variable(
197 sd_id128_t vendor,
198 const char *name,
199 uint32_t *attribute,
200 void **value,
201 size_t *size) {
202
203 _cleanup_close_ int fd = -1;
204 _cleanup_free_ char *p = NULL;
205 uint32_t a;
206 ssize_t n;
207 struct stat st;
208 _cleanup_free_ void *buf = NULL;
209
210 assert(name);
211 assert(value);
212 assert(size);
213
214 if (asprintf(&p,
215 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
216 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
217 return -ENOMEM;
218
219 fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
220 if (fd < 0)
221 return -errno;
222
223 if (fstat(fd, &st) < 0)
224 return -errno;
225 if (st.st_size < 4)
226 return -EIO;
227 if (st.st_size > 4*1024*1024 + 4)
228 return -E2BIG;
229
230 n = read(fd, &a, sizeof(a));
231 if (n < 0)
232 return -errno;
233 if (n != sizeof(a))
234 return -EIO;
235
236 buf = malloc(st.st_size - 4 + 2);
237 if (!buf)
238 return -ENOMEM;
239
240 n = read(fd, buf, (size_t) st.st_size - 4);
241 if (n < 0)
242 return -errno;
243 if (n != (ssize_t) st.st_size - 4)
244 return -EIO;
245
246 /* Always NUL terminate (2 bytes, to protect UTF-16) */
247 ((char*) buf)[st.st_size - 4] = 0;
248 ((char*) buf)[st.st_size - 4 + 1] = 0;
249
250 *value = TAKE_PTR(buf);
251 *size = (size_t) st.st_size - 4;
252
253 if (attribute)
254 *attribute = a;
255
256 return 0;
257 }
258
259 int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) {
260 _cleanup_free_ void *s = NULL;
261 size_t ss = 0;
262 int r;
263 char *x;
264
265 r = efi_get_variable(vendor, name, NULL, &s, &ss);
266 if (r < 0)
267 return r;
268
269 x = utf16_to_utf8(s, ss);
270 if (!x)
271 return -ENOMEM;
272
273 *p = x;
274 return 0;
275 }
276
277 int efi_set_variable(
278 sd_id128_t vendor,
279 const char *name,
280 const void *value,
281 size_t size) {
282
283 struct var {
284 uint32_t attr;
285 char buf[];
286 } _packed_ * _cleanup_free_ buf = NULL;
287 _cleanup_free_ char *p = NULL;
288 _cleanup_close_ int fd = -1;
289 bool saved_flags_valid = false;
290 unsigned saved_flags;
291 int r;
292
293 assert(name);
294 assert(value || size == 0);
295
296 if (asprintf(&p,
297 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
298 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
299 return -ENOMEM;
300
301 /* Newer efivarfs protects variables that are not in a whitelist with FS_IMMUTABLE_FL by default, to protect
302 * them for accidental removal and modification. We are not changing these variables accidentally however,
303 * hence let's unset the bit first. */
304
305 r = chattr_path(p, 0, FS_IMMUTABLE_FL, &saved_flags);
306 if (r < 0 && r != -ENOENT)
307 log_debug_errno(r, "Failed to drop FS_IMMUTABLE_FL flag from '%s', ignoring: %m", p);
308
309 saved_flags_valid = r >= 0;
310
311 if (size == 0) {
312 if (unlink(p) < 0) {
313 r = -errno;
314 goto finish;
315 }
316
317 return 0;
318 }
319
320 fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644);
321 if (fd < 0) {
322 r = -errno;
323 goto finish;
324 }
325
326 buf = malloc(sizeof(uint32_t) + size);
327 if (!buf) {
328 r = -ENOMEM;
329 goto finish;
330 }
331
332 buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
333 memcpy(buf->buf, value, size);
334
335 r = loop_write(fd, buf, sizeof(uint32_t) + size, false);
336 if (r < 0)
337 goto finish;
338
339 r = 0;
340
341 finish:
342 if (saved_flags_valid) {
343 int q;
344
345 /* Restore the original flags field, just in case */
346 if (fd < 0)
347 q = chattr_path(p, saved_flags, FS_IMMUTABLE_FL, NULL);
348 else
349 q = chattr_fd(fd, saved_flags, FS_IMMUTABLE_FL, NULL);
350 if (q < 0)
351 log_debug_errno(q, "Failed to restore FS_IMMUTABLE_FL on '%s', ignoring: %m", p);
352 }
353
354 return r;
355 }
356
357 int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *v) {
358 _cleanup_free_ char16_t *u16 = NULL;
359
360 u16 = utf8_to_utf16(v, strlen(v));
361 if (!u16)
362 return -ENOMEM;
363
364 return efi_set_variable(vendor, name, u16, (char16_strlen(u16) + 1) * sizeof(char16_t));
365 }
366
367 static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) {
368 size_t l = 0;
369
370 /* Returns the size of the string in bytes without the terminating two zero bytes */
371
372 if (buf_len_bytes % sizeof(uint16_t) != 0)
373 return -EINVAL;
374
375 while (l < buf_len_bytes / sizeof(uint16_t)) {
376 if (s[l] == 0)
377 return (l + 1) * sizeof(uint16_t);
378 l++;
379 }
380
381 return -EINVAL; /* The terminator was not found */
382 }
383
384 struct guid {
385 uint32_t u1;
386 uint16_t u2;
387 uint16_t u3;
388 uint8_t u4[8];
389 } _packed_;
390
391 static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
392 uint32_t u1;
393 uint16_t u2, u3;
394 const struct guid *uuid = guid;
395
396 memcpy(&u1, &uuid->u1, sizeof(uint32_t));
397 id128->bytes[0] = (u1 >> 24) & 0xff;
398 id128->bytes[1] = (u1 >> 16) & 0xff;
399 id128->bytes[2] = (u1 >> 8) & 0xff;
400 id128->bytes[3] = u1 & 0xff;
401 memcpy(&u2, &uuid->u2, sizeof(uint16_t));
402 id128->bytes[4] = (u2 >> 8) & 0xff;
403 id128->bytes[5] = u2 & 0xff;
404 memcpy(&u3, &uuid->u3, sizeof(uint16_t));
405 id128->bytes[6] = (u3 >> 8) & 0xff;
406 id128->bytes[7] = u3 & 0xff;
407 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
408 }
409
410 int efi_get_boot_option(
411 uint16_t id,
412 char **title,
413 sd_id128_t *part_uuid,
414 char **path,
415 bool *active) {
416
417 char boot_id[9];
418 _cleanup_free_ uint8_t *buf = NULL;
419 size_t l;
420 struct boot_option *header;
421 ssize_t title_size;
422 _cleanup_free_ char *s = NULL, *p = NULL;
423 sd_id128_t p_uuid = SD_ID128_NULL;
424 int r;
425
426 if (!is_efi_boot())
427 return -EOPNOTSUPP;
428
429 xsprintf(boot_id, "Boot%04X", id);
430 r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l);
431 if (r < 0)
432 return r;
433 if (l < offsetof(struct boot_option, title))
434 return -ENOENT;
435
436 header = (struct boot_option *)buf;
437 title_size = utf16_size(header->title, l - offsetof(struct boot_option, title));
438 if (title_size < 0)
439 return title_size;
440
441 if (title) {
442 s = utf16_to_utf8(header->title, title_size);
443 if (!s)
444 return -ENOMEM;
445 }
446
447 if (header->path_len > 0) {
448 uint8_t *dbuf;
449 size_t dnext, doff;
450
451 doff = offsetof(struct boot_option, title) + title_size;
452 dbuf = buf + doff;
453 if (header->path_len > l - doff)
454 return -EINVAL;
455
456 dnext = 0;
457 while (dnext < header->path_len) {
458 struct device_path *dpath;
459
460 dpath = (struct device_path *)(dbuf + dnext);
461 if (dpath->length < 4)
462 break;
463
464 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
465 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
466 break;
467
468 dnext += dpath->length;
469
470 /* Type 0x04 – Media Device Path */
471 if (dpath->type != MEDIA_DEVICE_PATH)
472 continue;
473
474 /* Sub-Type 1 – Hard Drive */
475 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
476 /* 0x02 – GUID Partition Table */
477 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
478 continue;
479
480 /* 0x02 – GUID signature */
481 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
482 continue;
483
484 if (part_uuid)
485 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
486 continue;
487 }
488
489 /* Sub-Type 4 – File Path */
490 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
491 p = utf16_to_utf8(dpath->path, dpath->length-4);
492 if (!p)
493 return -ENOMEM;
494
495 efi_tilt_backslashes(p);
496 continue;
497 }
498 }
499 }
500
501 if (title)
502 *title = TAKE_PTR(s);
503 if (part_uuid)
504 *part_uuid = p_uuid;
505 if (path)
506 *path = TAKE_PTR(p);
507 if (active)
508 *active = !!(header->attr & LOAD_OPTION_ACTIVE);
509
510 return 0;
511 }
512
513 static void to_utf16(uint16_t *dest, const char *src) {
514 int i;
515
516 for (i = 0; src[i] != '\0'; i++)
517 dest[i] = src[i];
518 dest[i] = '\0';
519 }
520
521 static void id128_to_efi_guid(sd_id128_t id, void *guid) {
522 struct guid uuid = {
523 .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3],
524 .u2 = id.bytes[4] << 8 | id.bytes[5],
525 .u3 = id.bytes[6] << 8 | id.bytes[7],
526 };
527 memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4));
528 memcpy(guid, &uuid, sizeof(uuid));
529 }
530
531 static uint16_t *tilt_slashes(uint16_t *s) {
532 uint16_t *p;
533
534 for (p = s; *p; p++)
535 if (*p == '/')
536 *p = '\\';
537
538 return s;
539 }
540
541 int efi_add_boot_option(
542 uint16_t id,
543 const char *title,
544 uint32_t part,
545 uint64_t pstart,
546 uint64_t psize,
547 sd_id128_t part_uuid,
548 const char *path) {
549
550 size_t size, title_len, path_len;
551 _cleanup_free_ char *buf = NULL;
552 struct boot_option *option;
553 struct device_path *devicep;
554 char boot_id[9];
555
556 if (!is_efi_boot())
557 return -EOPNOTSUPP;
558
559 title_len = (strlen(title)+1) * 2;
560 path_len = (strlen(path)+1) * 2;
561
562 buf = malloc0(offsetof(struct boot_option, title) + title_len +
563 sizeof(struct drive_path) +
564 sizeof(struct device_path) + path_len);
565 if (!buf)
566 return -ENOMEM;
567
568 /* header */
569 option = (struct boot_option *)buf;
570 option->attr = LOAD_OPTION_ACTIVE;
571 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
572 offsetof(struct device_path, path) + path_len +
573 offsetof(struct device_path, path);
574 to_utf16(option->title, title);
575 size = offsetof(struct boot_option, title) + title_len;
576
577 /* partition info */
578 devicep = (struct device_path *)(buf + size);
579 devicep->type = MEDIA_DEVICE_PATH;
580 devicep->sub_type = MEDIA_HARDDRIVE_DP;
581 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
582 memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t));
583 memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t));
584 memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t));
585 id128_to_efi_guid(part_uuid, devicep->drive.signature);
586 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
587 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
588 size += devicep->length;
589
590 /* path to loader */
591 devicep = (struct device_path *)(buf + size);
592 devicep->type = MEDIA_DEVICE_PATH;
593 devicep->sub_type = MEDIA_FILEPATH_DP;
594 devicep->length = offsetof(struct device_path, path) + path_len;
595 to_utf16(devicep->path, path);
596 tilt_slashes(devicep->path);
597 size += devicep->length;
598
599 /* end of path */
600 devicep = (struct device_path *)(buf + size);
601 devicep->type = END_DEVICE_PATH_TYPE;
602 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
603 devicep->length = offsetof(struct device_path, path);
604 size += devicep->length;
605
606 xsprintf(boot_id, "Boot%04X", id);
607 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size);
608 }
609
610 int efi_remove_boot_option(uint16_t id) {
611 char boot_id[9];
612
613 if (!is_efi_boot())
614 return -EOPNOTSUPP;
615
616 xsprintf(boot_id, "Boot%04X", id);
617 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0);
618 }
619
620 int efi_get_boot_order(uint16_t **order) {
621 _cleanup_free_ void *buf = NULL;
622 size_t l;
623 int r;
624
625 if (!is_efi_boot())
626 return -EOPNOTSUPP;
627
628 r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l);
629 if (r < 0)
630 return r;
631
632 if (l <= 0)
633 return -ENOENT;
634
635 if (l % sizeof(uint16_t) > 0 ||
636 l / sizeof(uint16_t) > INT_MAX)
637 return -EINVAL;
638
639 *order = TAKE_PTR(buf);
640 return (int) (l / sizeof(uint16_t));
641 }
642
643 int efi_set_boot_order(uint16_t *order, size_t n) {
644
645 if (!is_efi_boot())
646 return -EOPNOTSUPP;
647
648 return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t));
649 }
650
651 static int boot_id_hex(const char s[static 4]) {
652 int id = 0, i;
653
654 for (i = 0; i < 4; i++)
655 if (s[i] >= '0' && s[i] <= '9')
656 id |= (s[i] - '0') << (3 - i) * 4;
657 else if (s[i] >= 'A' && s[i] <= 'F')
658 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
659 else
660 return -EINVAL;
661
662 return id;
663 }
664
665 static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
666 return CMP(*a, *b);
667 }
668
669 int efi_get_boot_options(uint16_t **options) {
670 _cleanup_closedir_ DIR *dir = NULL;
671 _cleanup_free_ uint16_t *list = NULL;
672 struct dirent *de;
673 size_t alloc = 0;
674 int count = 0;
675
676 assert(options);
677
678 if (!is_efi_boot())
679 return -EOPNOTSUPP;
680
681 dir = opendir("/sys/firmware/efi/efivars/");
682 if (!dir)
683 return -errno;
684
685 FOREACH_DIRENT(de, dir, return -errno) {
686 int id;
687
688 if (strncmp(de->d_name, "Boot", 4) != 0)
689 continue;
690
691 if (strlen(de->d_name) != 45)
692 continue;
693
694 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
695 continue;
696
697 id = boot_id_hex(de->d_name + 4);
698 if (id < 0)
699 continue;
700
701 if (!GREEDY_REALLOC(list, alloc, count + 1))
702 return -ENOMEM;
703
704 list[count++] = id;
705 }
706
707 typesafe_qsort(list, count, cmp_uint16);
708
709 *options = TAKE_PTR(list);
710
711 return count;
712 }
713
714 static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
715 _cleanup_free_ char *j = NULL;
716 int r;
717 uint64_t x = 0;
718
719 assert(name);
720 assert(u);
721
722 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j);
723 if (r < 0)
724 return r;
725
726 r = safe_atou64(j, &x);
727 if (r < 0)
728 return r;
729
730 *u = x;
731 return 0;
732 }
733
734 int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
735 uint64_t x, y;
736 int r;
737
738 assert(firmware);
739 assert(loader);
740
741 if (!is_efi_boot())
742 return -EOPNOTSUPP;
743
744 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
745 if (r < 0)
746 return r;
747
748 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
749 if (r < 0)
750 return r;
751
752 if (y == 0 || y < x)
753 return -EIO;
754
755 if (y > USEC_PER_HOUR)
756 return -EIO;
757
758 *firmware = x;
759 *loader = y;
760
761 return 0;
762 }
763
764 int efi_loader_get_device_part_uuid(sd_id128_t *u) {
765 _cleanup_free_ char *p = NULL;
766 int r, parsed[16];
767
768 if (!is_efi_boot())
769 return -EOPNOTSUPP;
770
771 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p);
772 if (r < 0)
773 return r;
774
775 if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
776 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
777 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
778 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
779 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
780 return -EIO;
781
782 if (u) {
783 unsigned i;
784
785 for (i = 0; i < ELEMENTSOF(parsed); i++)
786 u->bytes[i] = parsed[i];
787 }
788
789 return 0;
790 }
791
792 bool efi_loader_entry_name_valid(const char *s) {
793 if (isempty(s))
794 return false;
795
796 if (strlen(s) > FILENAME_MAX) /* Make sure entry names fit in filenames */
797 return false;
798
799 return in_charset(s, ALPHANUMERICAL "-");
800 }
801
802 int efi_loader_get_entries(char ***ret) {
803 _cleanup_free_ char16_t *entries = NULL;
804 _cleanup_strv_free_ char **l = NULL;
805 size_t size, i, start;
806 int r;
807
808 assert(ret);
809
810 if (!is_efi_boot())
811 return -EOPNOTSUPP;
812
813 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntries", NULL, (void**) &entries, &size);
814 if (r < 0)
815 return r;
816
817 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
818
819 for (i = 0, start = 0;; i++) {
820 _cleanup_free_ char *decoded = NULL;
821 bool end;
822
823 /* Is this the end of the variable's data? */
824 end = i * sizeof(char16_t) >= size;
825
826 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
827 * so, let's go to the next entry. */
828 if (!end && entries[i] != 0)
829 continue;
830
831 /* We reached the end of a string, let's decode it into UTF-8 */
832 decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t));
833 if (!decoded)
834 return -ENOMEM;
835
836 if (efi_loader_entry_name_valid(decoded)) {
837 r = strv_consume(&l, TAKE_PTR(decoded));
838 if (r < 0)
839 return r;
840 } else
841 log_debug("Ignoring invalid loader entry '%s'.", decoded);
842
843 /* We reached the end of the variable */
844 if (end)
845 break;
846
847 /* Continue after the NUL byte */
848 start = i + 1;
849 }
850
851 *ret = TAKE_PTR(l);
852 return 0;
853 }
854
855 int efi_loader_get_features(uint64_t *ret) {
856 _cleanup_free_ void *v = NULL;
857 size_t s;
858 int r;
859
860 if (!is_efi_boot()) {
861 *ret = 0;
862 return 0;
863 }
864
865 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderFeatures", NULL, &v, &s);
866 if (r == -ENOENT) {
867 _cleanup_free_ char *info = NULL;
868
869 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
870 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info);
871 if (r < 0) {
872 if (r != -ENOENT)
873 return r;
874
875 /* Variable not set, definitely means not systemd-boot */
876
877 } else if (first_word(info, "systemd-boot")) {
878
879 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
880 * static in all its versions. */
881
882 *ret = EFI_LOADER_FEATURE_CONFIG_TIMEOUT |
883 EFI_LOADER_FEATURE_ENTRY_DEFAULT |
884 EFI_LOADER_FEATURE_ENTRY_ONESHOT;
885
886 return 0;
887 }
888
889 /* No features supported */
890 *ret = 0;
891 return 0;
892 }
893 if (r < 0)
894 return r;
895
896 if (s != sizeof(uint64_t))
897 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
898 "LoaderFeatures EFI variable doesn't have the right size.");
899
900 memcpy(ret, v, sizeof(uint64_t));
901 return 0;
902 }
903
904 #endif
905
906 char *efi_tilt_backslashes(char *s) {
907 char *p;
908
909 for (p = s; *p; p++)
910 if (*p == '\\')
911 *p = '/';
912
913 return s;
914 }