]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/efivars.c
util: force time multiplications to happen in longs
[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
0974a682
KS
249int efi_set_variable(
250 sd_id128_t vendor,
251 const char *name,
252 const void *value,
253 size_t size) {
254
255 struct var {
256 uint32_t attr;
257 char buf[];
b7749eb5
ZJS
258 } _packed_ * _cleanup_free_ buf = NULL;
259 _cleanup_free_ char *p = NULL;
260 _cleanup_close_ int fd = -1;
2c3bf278
LP
261 bool saved_flags_valid = false;
262 unsigned saved_flags;
263 int r;
0974a682
KS
264
265 assert(name);
e1e26566 266 assert(value || size == 0);
0974a682
KS
267
268 if (asprintf(&p,
269 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
270 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
271 return -ENOMEM;
272
2c3bf278
LP
273 /* Newer efivarfs protects variables that are not in a whitelist with FS_IMMUTABLE_FL by default, to protect
274 * them for accidental removal and modification. We are not changing these variables accidentally however,
275 * hence let's unset the bit first. */
276
277 r = chattr_path(p, 0, FS_IMMUTABLE_FL, &saved_flags);
278 if (r < 0 && r != -ENOENT)
279 log_debug_errno(r, "Failed to drop FS_IMMUTABLE_FL flag from '%s', ignoring: %m", p);
280
281 saved_flags_valid = r >= 0;
282
0974a682 283 if (size == 0) {
2c3bf278
LP
284 if (unlink(p) < 0) {
285 r = -errno;
286 goto finish;
287 }
288
b7749eb5 289 return 0;
0974a682
KS
290 }
291
292 fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644);
2c3bf278
LP
293 if (fd < 0) {
294 r = -errno;
295 goto finish;
296 }
0974a682
KS
297
298 buf = malloc(sizeof(uint32_t) + size);
2c3bf278
LP
299 if (!buf) {
300 r = -ENOMEM;
301 goto finish;
302 }
0974a682
KS
303
304 buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
305 memcpy(buf->buf, value, size);
306
2c3bf278
LP
307 r = loop_write(fd, buf, sizeof(uint32_t) + size, false);
308 if (r < 0)
309 goto finish;
310
311 r = 0;
312
313finish:
314 if (saved_flags_valid) {
315 int q;
316
317 /* Restore the original flags field, just in case */
318 if (fd < 0)
319 q = chattr_path(p, saved_flags, FS_IMMUTABLE_FL, NULL);
320 else
321 q = chattr_fd(fd, saved_flags, FS_IMMUTABLE_FL, NULL);
322 if (q < 0)
323 log_debug_errno(q, "Failed to restore FS_IMMUTABLE_FL on '%s', ignoring: %m", p);
324 }
325
326 return r;
0974a682
KS
327}
328
9cde64ff 329int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) {
7b4d7cc0 330 _cleanup_free_ void *s = NULL;
39883f62 331 size_t ss = 0;
9cde64ff
LP
332 int r;
333 char *x;
7b4d7cc0 334
9cde64ff
LP
335 r = efi_get_variable(vendor, name, NULL, &s, &ss);
336 if (r < 0)
337 return r;
338
339 x = utf16_to_utf8(s, ss);
340 if (!x)
341 return -ENOMEM;
342
343 *p = x;
344 return 0;
7b4d7cc0
KS
345}
346
347static size_t utf16_size(const uint16_t *s) {
348 size_t l = 0;
349
350 while (s[l] > 0)
351 l++;
9cde64ff 352
7b4d7cc0
KS
353 return (l+1) * sizeof(uint16_t);
354}
355
356static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
357 struct uuid {
358 uint32_t u1;
359 uint16_t u2;
360 uint16_t u3;
361 uint8_t u4[8];
362 } _packed_;
363 const struct uuid *uuid = guid;
364
365 id128->bytes[0] = (uuid->u1 >> 24) & 0xff;
366 id128->bytes[1] = (uuid->u1 >> 16) & 0xff;
367 id128->bytes[2] = (uuid->u1 >> 8) & 0xff;
368 id128->bytes[3] = (uuid->u1) & 0xff;
369 id128->bytes[4] = (uuid->u2 >> 8) & 0xff;
370 id128->bytes[5] = (uuid->u2) & 0xff;
371 id128->bytes[6] = (uuid->u3 >> 8) & 0xff;
372 id128->bytes[7] = (uuid->u3) & 0xff;
373 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
374}
375
9cde64ff
LP
376int efi_get_boot_option(
377 uint16_t id,
378 char **title,
379 sd_id128_t *part_uuid,
0974a682
KS
380 char **path,
381 bool *active) {
7b4d7cc0 382
9cde64ff
LP
383 char boot_id[9];
384 _cleanup_free_ uint8_t *buf = NULL;
7b4d7cc0
KS
385 size_t l;
386 struct boot_option *header;
387 size_t title_size;
b7749eb5 388 _cleanup_free_ char *s = NULL, *p = NULL;
7b4d7cc0 389 sd_id128_t p_uuid = SD_ID128_NULL;
a8436474 390 int r;
7b4d7cc0 391
337eed30
LP
392 if (!is_efi_boot())
393 return -EOPNOTSUPP;
394
b7749eb5 395 xsprintf(boot_id, "Boot%04X", id);
a8436474
ZJS
396 r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l);
397 if (r < 0)
398 return r;
7b4d7cc0
KS
399 if (l < sizeof(struct boot_option))
400 return -ENOENT;
401
402 header = (struct boot_option *)buf;
403 title_size = utf16_size(header->title);
404 if (title_size > l - offsetof(struct boot_option, title))
405 return -EINVAL;
406
5483a186
ZJS
407 if (title) {
408 s = utf16_to_utf8(header->title, title_size);
b7749eb5
ZJS
409 if (!s)
410 return -ENOMEM;
7b4d7cc0
KS
411 }
412
413 if (header->path_len > 0) {
9cde64ff 414 uint8_t *dbuf;
e7e36b90
DT
415 size_t dnext, doff;
416
417 doff = offsetof(struct boot_option, title) + title_size;
418 dbuf = buf + doff;
419 if (header->path_len > l - doff)
420 return -EINVAL;
7b4d7cc0 421
7b4d7cc0
KS
422 dnext = 0;
423 while (dnext < header->path_len) {
424 struct device_path *dpath;
425
426 dpath = (struct device_path *)(dbuf + dnext);
427 if (dpath->length < 4)
428 break;
429
430 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
0974a682 431 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
7b4d7cc0
KS
432 break;
433
434 dnext += dpath->length;
435
436 /* Type 0x04 – Media Device Path */
0974a682 437 if (dpath->type != MEDIA_DEVICE_PATH)
7b4d7cc0
KS
438 continue;
439
440 /* Sub-Type 1 – Hard Drive */
0974a682 441 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
7b4d7cc0 442 /* 0x02 – GUID Partition Table */
0974a682 443 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
7b4d7cc0
KS
444 continue;
445
446 /* 0x02 – GUID signature */
0974a682 447 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
7b4d7cc0
KS
448 continue;
449
5483a186
ZJS
450 if (part_uuid)
451 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
7b4d7cc0
KS
452 continue;
453 }
454
455 /* Sub-Type 4 – File Path */
0974a682 456 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
7b4d7cc0 457 p = utf16_to_utf8(dpath->path, dpath->length-4);
9db296fd
LP
458 if (!p)
459 return -ENOMEM;
460
0974a682 461 efi_tilt_backslashes(p);
7b4d7cc0
KS
462 continue;
463 }
464 }
465 }
466
1cc6c93a
YW
467 if (title)
468 *title = TAKE_PTR(s);
7b4d7cc0
KS
469 if (part_uuid)
470 *part_uuid = p_uuid;
1cc6c93a
YW
471 if (path)
472 *path = TAKE_PTR(p);
0974a682 473 if (active)
0aa3b783 474 *active = !!(header->attr & LOAD_OPTION_ACTIVE);
9cde64ff 475
7b4d7cc0 476 return 0;
7b4d7cc0
KS
477}
478
0974a682
KS
479static void to_utf16(uint16_t *dest, const char *src) {
480 int i;
481
482 for (i = 0; src[i] != '\0'; i++)
483 dest[i] = src[i];
484 dest[i] = '\0';
485}
486
487struct guid {
488 uint32_t u1;
489 uint16_t u2;
490 uint16_t u3;
491 uint8_t u4[8];
885fdebc 492} _packed_;
0974a682
KS
493
494static void id128_to_efi_guid(sd_id128_t id, void *guid) {
495 struct guid *uuid = guid;
496
497 uuid->u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3];
498 uuid->u2 = id.bytes[4] << 8 | id.bytes[5];
499 uuid->u3 = id.bytes[6] << 8 | id.bytes[7];
500 memcpy(uuid->u4, id.bytes+8, sizeof(uuid->u4));
501}
502
503static uint16_t *tilt_slashes(uint16_t *s) {
504 uint16_t *p;
505
506 for (p = s; *p; p++)
507 if (*p == '/')
508 *p = '\\';
509
510 return s;
511}
512
337eed30
LP
513int efi_add_boot_option(
514 uint16_t id,
515 const char *title,
516 uint32_t part,
517 uint64_t pstart,
518 uint64_t psize,
519 sd_id128_t part_uuid,
520 const char *path) {
521
522 size_t size, title_len, path_len;
523 _cleanup_free_ char *buf = NULL;
0974a682
KS
524 struct boot_option *option;
525 struct device_path *devicep;
337eed30
LP
526 char boot_id[9];
527
528 if (!is_efi_boot())
529 return -EOPNOTSUPP;
0974a682
KS
530
531 title_len = (strlen(title)+1) * 2;
532 path_len = (strlen(path)+1) * 2;
533
e78c250b
LP
534 buf = malloc0(sizeof(struct boot_option) + title_len +
535 sizeof(struct drive_path) +
536 sizeof(struct device_path) + path_len);
b7749eb5
ZJS
537 if (!buf)
538 return -ENOMEM;
0974a682
KS
539
540 /* header */
541 option = (struct boot_option *)buf;
542 option->attr = LOAD_OPTION_ACTIVE;
543 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
544 offsetof(struct device_path, path) + path_len +
545 offsetof(struct device_path, path);
546 to_utf16(option->title, title);
547 size = offsetof(struct boot_option, title) + title_len;
548
549 /* partition info */
550 devicep = (struct device_path *)(buf + size);
551 devicep->type = MEDIA_DEVICE_PATH;
552 devicep->sub_type = MEDIA_HARDDRIVE_DP;
553 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
554 devicep->drive.part_nr = part;
555 devicep->drive.part_start = pstart;
920b52e4 556 devicep->drive.part_size = psize;
0974a682
KS
557 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
558 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
559 id128_to_efi_guid(part_uuid, devicep->drive.signature);
560 size += devicep->length;
561
562 /* path to loader */
563 devicep = (struct device_path *)(buf + size);
564 devicep->type = MEDIA_DEVICE_PATH;
565 devicep->sub_type = MEDIA_FILEPATH_DP;
566 devicep->length = offsetof(struct device_path, path) + path_len;
567 to_utf16(devicep->path, path);
568 tilt_slashes(devicep->path);
569 size += devicep->length;
570
571 /* end of path */
572 devicep = (struct device_path *)(buf + size);
573 devicep->type = END_DEVICE_PATH_TYPE;
574 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
575 devicep->length = offsetof(struct device_path, path);
576 size += devicep->length;
577
b7749eb5
ZJS
578 xsprintf(boot_id, "Boot%04X", id);
579 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size);
0974a682
KS
580}
581
582int efi_remove_boot_option(uint16_t id) {
583 char boot_id[9];
584
337eed30
LP
585 if (!is_efi_boot())
586 return -EOPNOTSUPP;
587
b7749eb5 588 xsprintf(boot_id, "Boot%04X", id);
0974a682
KS
589 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0);
590}
591
9cde64ff 592int efi_get_boot_order(uint16_t **order) {
0797f232 593 _cleanup_free_ void *buf = NULL;
7b4d7cc0 594 size_t l;
9cde64ff 595 int r;
7b4d7cc0 596
337eed30
LP
597 if (!is_efi_boot())
598 return -EOPNOTSUPP;
599
9cde64ff
LP
600 r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l);
601 if (r < 0)
602 return r;
7b4d7cc0 603
0797f232 604 if (l <= 0)
7b4d7cc0 605 return -ENOENT;
7b4d7cc0 606
0797f232
ZJS
607 if (l % sizeof(uint16_t) > 0 ||
608 l / sizeof(uint16_t) > INT_MAX)
7b4d7cc0 609 return -EINVAL;
7b4d7cc0 610
ae2a15bc 611 *order = TAKE_PTR(buf);
9cde64ff
LP
612 return (int) (l / sizeof(uint16_t));
613}
614
0974a682 615int efi_set_boot_order(uint16_t *order, size_t n) {
337eed30
LP
616
617 if (!is_efi_boot())
618 return -EOPNOTSUPP;
619
0974a682
KS
620 return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t));
621}
622
4d34c495 623static int boot_id_hex(const char s[4]) {
e78c250b 624 int id = 0, i;
4d34c495
KS
625
626 for (i = 0; i < 4; i++)
627 if (s[i] >= '0' && s[i] <= '9')
628 id |= (s[i] - '0') << (3 - i) * 4;
629 else if (s[i] >= 'A' && s[i] <= 'F')
630 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
631 else
7e8185ef 632 return -EINVAL;
4d34c495
KS
633
634 return id;
635}
636
93bab288
YW
637static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
638 return CMP(*a, *b);
9db11a99
LP
639}
640
9cde64ff
LP
641int efi_get_boot_options(uint16_t **options) {
642 _cleanup_closedir_ DIR *dir = NULL;
b7749eb5 643 _cleanup_free_ uint16_t *list = NULL;
e78c250b 644 struct dirent *de;
7432b24b 645 size_t alloc = 0;
b7749eb5 646 int count = 0;
9cde64ff
LP
647
648 assert(options);
649
337eed30
LP
650 if (!is_efi_boot())
651 return -EOPNOTSUPP;
652
9cde64ff
LP
653 dir = opendir("/sys/firmware/efi/efivars/");
654 if (!dir)
655 return -errno;
656
b7749eb5 657 FOREACH_DIRENT(de, dir, return -errno) {
4d34c495 658 int id;
9cde64ff
LP
659
660 if (strncmp(de->d_name, "Boot", 4) != 0)
661 continue;
662
4d34c495 663 if (strlen(de->d_name) != 45)
9cde64ff
LP
664 continue;
665
666 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
667 continue;
668
4d34c495
KS
669 id = boot_id_hex(de->d_name + 4);
670 if (id < 0)
9cde64ff
LP
671 continue;
672
7432b24b 673 if (!GREEDY_REALLOC(list, alloc, count + 1))
b7749eb5 674 return -ENOMEM;
9cde64ff 675
7432b24b 676 list[count++] = id;
9cde64ff
LP
677 }
678
93bab288 679 typesafe_qsort(list, count, cmp_uint16);
9db11a99 680
1cc6c93a
YW
681 *options = TAKE_PTR(list);
682
9cde64ff 683 return count;
7b4d7cc0
KS
684}
685
5dbe9f53 686static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
2e3d0692 687 _cleanup_free_ char *j = NULL;
2e3d0692 688 int r;
39883f62 689 uint64_t x = 0;
2e3d0692
LP
690
691 assert(name);
692 assert(u);
693
61cc634b 694 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j);
2e3d0692
LP
695 if (r < 0)
696 return r;
697
2e3d0692
LP
698 r = safe_atou64(j, &x);
699 if (r < 0)
700 return r;
701
5dbe9f53 702 *u = x;
2e3d0692
LP
703 return 0;
704}
705
c51d84dc 706int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
2e3d0692
LP
707 uint64_t x, y;
708 int r;
2e3d0692
LP
709
710 assert(firmware);
711 assert(loader);
712
337eed30
LP
713 if (!is_efi_boot())
714 return -EOPNOTSUPP;
715
e9cea16d 716 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
2e3d0692
LP
717 if (r < 0)
718 return r;
719
e9cea16d 720 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
2e3d0692
LP
721 if (r < 0)
722 return r;
723
724 if (y == 0 || y < x)
725 return -EIO;
726
727 if (y > USEC_PER_HOUR)
728 return -EIO;
729
730 *firmware = x;
731 *loader = y;
732
733 return 0;
734}
735
c51d84dc 736int efi_loader_get_device_part_uuid(sd_id128_t *u) {
f4ce2b3e 737 _cleanup_free_ char *p = NULL;
f4ce2b3e 738 int r, parsed[16];
f4ce2b3e 739
337eed30
LP
740 if (!is_efi_boot())
741 return -EOPNOTSUPP;
742
61cc634b 743 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p);
f4ce2b3e
LP
744 if (r < 0)
745 return r;
746
f4ce2b3e
LP
747 if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
748 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
749 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
750 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
751 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
752 return -EIO;
753
73b80ec2
LP
754 if (u) {
755 unsigned i;
756
757 for (i = 0; i < ELEMENTSOF(parsed); i++)
758 u->bytes[i] = parsed[i];
759 }
f4ce2b3e
LP
760
761 return 0;
762}
b872e9a0 763
bd2865ca
LP
764int efi_loader_get_entries(char ***ret) {
765 _cleanup_free_ char16_t *entries = NULL;
766 _cleanup_strv_free_ char **l = NULL;
767 size_t size, i, start;
768 int r;
769
770 assert(ret);
771
772 if (!is_efi_boot())
773 return -EOPNOTSUPP;
774
775 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntries", NULL, (void**) &entries, &size);
776 if (r < 0)
777 return r;
778
779 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
780
781 for (i = 0, start = 0;; i++) {
782 char *decoded;
783 bool end;
784
785 /* Is this the end of the variable's data? */
786 end = i * sizeof(char16_t) >= size;
787
788 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
789 * so, let's go to the next entry. */
790 if (!end && entries[i] != 0)
791 continue;
792
793 /* We reached the end of a string, let's decode it into UTF-8 */
794 decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t));
795 if (!decoded)
796 return -ENOMEM;
797
798 r = strv_consume(&l, decoded);
799 if (r < 0)
800 return r;
801
802 /* We reached the end of the variable */
803 if (end)
804 break;
805
806 /* Continue after the NUL byte */
807 start = i + 1;
808 }
809
810 *ret = TAKE_PTR(l);
811 return 0;
812}
813
b872e9a0 814#endif
b28ce7c6
TG
815
816char *efi_tilt_backslashes(char *s) {
817 char *p;
818
819 for (p = s; *p; p++)
820 if (*p == '\\')
821 *p = '/';
822
823 return s;
824}