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