]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/efi-loader.c
tree-wide: drop double newline
[thirdparty/systemd.git] / src / shared / efi-loader.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2e3d0692 2
a8fbdf54 3#include <stdlib.h>
cf0fbc49 4#include <unistd.h>
2e3d0692 5
b5efdb8a 6#include "alloc-util.h"
a0956174 7#include "dirent-util.h"
0bb2f0f1 8#include "efi-loader.h"
3ffd4af2
LP
9#include "efivars.h"
10#include "fd-util.h"
c004493c 11#include "io-util.h"
6bedfcbb 12#include "parse-util.h"
760877e9 13#include "sort-util.h"
15a5e950 14#include "stdio-util.h"
0bb2f0f1 15#include "string-util.h"
2e3d0692 16#include "utf8.h"
5bdf2243 17#include "virt.h"
2e3d0692 18
349cc4a5 19#if ENABLE_EFI
b872e9a0 20
0974a682
KS
21#define LOAD_OPTION_ACTIVE 0x00000001
22#define MEDIA_DEVICE_PATH 0x04
23#define MEDIA_HARDDRIVE_DP 0x01
24#define MEDIA_FILEPATH_DP 0x04
25#define SIGNATURE_TYPE_GUID 0x02
26#define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
27#define END_DEVICE_PATH_TYPE 0x7f
28#define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff
5bdf2243 29#define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001
0974a682 30
f7cb1c79
ZJS
31#define boot_option__contents { \
32 uint32_t attr; \
33 uint16_t path_len; \
34 uint16_t title[]; \
35 }
36
37struct boot_option boot_option__contents;
38struct boot_option__packed boot_option__contents _packed_;
39assert_cc(offsetof(struct boot_option, title) == offsetof(struct boot_option__packed, title));
40/* sizeof(struct boot_option) != sizeof(struct boot_option__packed), so
41 * the *size* of the structure should not be used anywhere below. */
0974a682
KS
42
43struct drive_path {
44 uint32_t part_nr;
45 uint64_t part_start;
46 uint64_t part_size;
47 char signature[16];
48 uint8_t mbr_type;
49 uint8_t signature_type;
885fdebc 50} _packed_;
0974a682 51
3c7dddac
ZJS
52#define device_path__contents { \
53 uint8_t type; \
54 uint8_t sub_type; \
55 uint16_t length; \
56 union { \
57 uint16_t path[0]; \
58 struct drive_path drive; \
59 }; \
60 }
61
62struct device_path device_path__contents;
63struct device_path__packed device_path__contents _packed_;
64assert_cc(sizeof(struct device_path) == sizeof(struct device_path__packed));
0974a682 65
9cde64ff 66bool is_efi_boot(void) {
bb161cdc
LP
67 if (detect_container() > 0)
68 return false;
69
70 return access("/sys/firmware/efi/", F_OK) >= 0;
34e5a31e
LP
71}
72
bc6f2e7c 73static int read_flag(const char *varname) {
b47d419c 74 _cleanup_free_ void *v = NULL;
bc6f2e7c 75 uint8_t b;
e22c567f
LP
76 size_t s;
77 int r;
bc6f2e7c 78
337eed30
LP
79 if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */
80 return 0;
81
bc6f2e7c
KS
82 r = efi_get_variable(EFI_VENDOR_GLOBAL, varname, NULL, &v, &s);
83 if (r < 0)
84 return r;
85
b47d419c
ZJS
86 if (s != 1)
87 return -EINVAL;
bc6f2e7c
KS
88
89 b = *(uint8_t *)v;
e78c250b 90 return !!b;
bc6f2e7c
KS
91}
92
9df49b33
TG
93bool is_efi_secure_boot(void) {
94 return read_flag("SecureBoot") > 0;
bc6f2e7c
KS
95}
96
9df49b33
TG
97bool is_efi_secure_boot_setup_mode(void) {
98 return read_flag("SetupMode") > 0;
bc6f2e7c
KS
99}
100
5bdf2243 101int efi_reboot_to_firmware_supported(void) {
5bdf2243 102 _cleanup_free_ void *v = NULL;
e22c567f
LP
103 uint64_t b;
104 size_t s;
105 int r;
5bdf2243 106
bb161cdc 107 if (!is_efi_boot())
5bdf2243
JJ
108 return -EOPNOTSUPP;
109
110 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndicationsSupported", NULL, &v, &s);
846ab104
LP
111 if (r == -ENOENT) /* variable doesn't exist? it's not supported then */
112 return -EOPNOTSUPP;
5bdf2243
JJ
113 if (r < 0)
114 return r;
e22c567f 115 if (s != sizeof(uint64_t))
5bdf2243
JJ
116 return -EINVAL;
117
e22c567f
LP
118 b = *(uint64_t*) v;
119 if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI))
120 return -EOPNOTSUPP; /* bit unset? it's not supported then */
121
122 return 0;
5bdf2243
JJ
123}
124
125static int get_os_indications(uint64_t *os_indication) {
5bdf2243 126 _cleanup_free_ void *v = NULL;
e22c567f
LP
127 size_t s;
128 int r;
5bdf2243 129
e78c250b 130 /* Let's verify general support first */
5bdf2243
JJ
131 r = efi_reboot_to_firmware_supported();
132 if (r < 0)
133 return r;
134
135 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndications", NULL, &v, &s);
6b62bbbc 136 if (r == -ENOENT) {
e78c250b
LP
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 pretend it's 0
139 * then, to hide this implementation detail. Note that this call will return -ENOENT then only if the
140 * support for OsIndications is missing entirely, as determined by efi_reboot_to_firmware_supported()
141 * above. */
6b62bbbc
LP
142 *os_indication = 0;
143 return 0;
144 } else if (r < 0)
5bdf2243
JJ
145 return r;
146 else if (s != sizeof(uint64_t))
147 return -EINVAL;
148
149 *os_indication = *(uint64_t *)v;
150 return 0;
151}
152
153int efi_get_reboot_to_firmware(void) {
154 int r;
155 uint64_t b;
156
157 r = get_os_indications(&b);
158 if (r < 0)
159 return r;
160
161 return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI);
162}
163
164int efi_set_reboot_to_firmware(bool value) {
165 int r;
166 uint64_t b, b_new;
167
168 r = get_os_indications(&b);
169 if (r < 0)
170 return r;
171
172 if (value)
173 b_new = b | EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
174 else
175 b_new = b & ~EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
176
177 /* Avoid writing to efi vars store if we can due to firmware bugs. */
178 if (b != b_new)
179 return efi_set_variable(EFI_VENDOR_GLOBAL, "OsIndications", &b_new, sizeof(uint64_t));
180
181 return 0;
182}
183
f7cb1c79 184static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) {
7b4d7cc0
KS
185 size_t l = 0;
186
f7cb1c79
ZJS
187 /* Returns the size of the string in bytes without the terminating two zero bytes */
188
189 if (buf_len_bytes % sizeof(uint16_t) != 0)
190 return -EINVAL;
191
192 while (l < buf_len_bytes / sizeof(uint16_t)) {
193 if (s[l] == 0)
194 return (l + 1) * sizeof(uint16_t);
7b4d7cc0 195 l++;
f7cb1c79 196 }
9cde64ff 197
f7cb1c79 198 return -EINVAL; /* The terminator was not found */
7b4d7cc0
KS
199}
200
3c7dddac
ZJS
201struct guid {
202 uint32_t u1;
203 uint16_t u2;
204 uint16_t u3;
205 uint8_t u4[8];
206} _packed_;
207
7b4d7cc0 208static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
3c7dddac
ZJS
209 uint32_t u1;
210 uint16_t u2, u3;
211 const struct guid *uuid = guid;
212
213 memcpy(&u1, &uuid->u1, sizeof(uint32_t));
214 id128->bytes[0] = (u1 >> 24) & 0xff;
215 id128->bytes[1] = (u1 >> 16) & 0xff;
216 id128->bytes[2] = (u1 >> 8) & 0xff;
217 id128->bytes[3] = u1 & 0xff;
218 memcpy(&u2, &uuid->u2, sizeof(uint16_t));
219 id128->bytes[4] = (u2 >> 8) & 0xff;
220 id128->bytes[5] = u2 & 0xff;
221 memcpy(&u3, &uuid->u3, sizeof(uint16_t));
222 id128->bytes[6] = (u3 >> 8) & 0xff;
223 id128->bytes[7] = u3 & 0xff;
7b4d7cc0
KS
224 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
225}
226
9cde64ff
LP
227int efi_get_boot_option(
228 uint16_t id,
229 char **title,
230 sd_id128_t *part_uuid,
0974a682
KS
231 char **path,
232 bool *active) {
7b4d7cc0 233
9cde64ff
LP
234 char boot_id[9];
235 _cleanup_free_ uint8_t *buf = NULL;
7b4d7cc0
KS
236 size_t l;
237 struct boot_option *header;
f7cb1c79 238 ssize_t title_size;
b7749eb5 239 _cleanup_free_ char *s = NULL, *p = NULL;
7b4d7cc0 240 sd_id128_t p_uuid = SD_ID128_NULL;
a8436474 241 int r;
7b4d7cc0 242
337eed30
LP
243 if (!is_efi_boot())
244 return -EOPNOTSUPP;
245
b7749eb5 246 xsprintf(boot_id, "Boot%04X", id);
a8436474
ZJS
247 r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l);
248 if (r < 0)
249 return r;
f7cb1c79 250 if (l < offsetof(struct boot_option, title))
7b4d7cc0
KS
251 return -ENOENT;
252
253 header = (struct boot_option *)buf;
f7cb1c79
ZJS
254 title_size = utf16_size(header->title, l - offsetof(struct boot_option, title));
255 if (title_size < 0)
256 return title_size;
7b4d7cc0 257
5483a186
ZJS
258 if (title) {
259 s = utf16_to_utf8(header->title, title_size);
b7749eb5
ZJS
260 if (!s)
261 return -ENOMEM;
7b4d7cc0
KS
262 }
263
264 if (header->path_len > 0) {
9cde64ff 265 uint8_t *dbuf;
e7e36b90
DT
266 size_t dnext, doff;
267
268 doff = offsetof(struct boot_option, title) + title_size;
269 dbuf = buf + doff;
270 if (header->path_len > l - doff)
271 return -EINVAL;
7b4d7cc0 272
7b4d7cc0
KS
273 dnext = 0;
274 while (dnext < header->path_len) {
275 struct device_path *dpath;
276
277 dpath = (struct device_path *)(dbuf + dnext);
278 if (dpath->length < 4)
279 break;
280
281 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
0974a682 282 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
7b4d7cc0
KS
283 break;
284
285 dnext += dpath->length;
286
287 /* Type 0x04 – Media Device Path */
0974a682 288 if (dpath->type != MEDIA_DEVICE_PATH)
7b4d7cc0
KS
289 continue;
290
291 /* Sub-Type 1 – Hard Drive */
0974a682 292 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
7b4d7cc0 293 /* 0x02 – GUID Partition Table */
0974a682 294 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
7b4d7cc0
KS
295 continue;
296
297 /* 0x02 – GUID signature */
0974a682 298 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
7b4d7cc0
KS
299 continue;
300
5483a186
ZJS
301 if (part_uuid)
302 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
7b4d7cc0
KS
303 continue;
304 }
305
306 /* Sub-Type 4 – File Path */
0974a682 307 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
7b4d7cc0 308 p = utf16_to_utf8(dpath->path, dpath->length-4);
9db296fd
LP
309 if (!p)
310 return -ENOMEM;
311
0974a682 312 efi_tilt_backslashes(p);
7b4d7cc0
KS
313 continue;
314 }
315 }
316 }
317
1cc6c93a
YW
318 if (title)
319 *title = TAKE_PTR(s);
7b4d7cc0
KS
320 if (part_uuid)
321 *part_uuid = p_uuid;
1cc6c93a
YW
322 if (path)
323 *path = TAKE_PTR(p);
0974a682 324 if (active)
55033662 325 *active = header->attr & LOAD_OPTION_ACTIVE;
9cde64ff 326
7b4d7cc0 327 return 0;
7b4d7cc0
KS
328}
329
0974a682
KS
330static void to_utf16(uint16_t *dest, const char *src) {
331 int i;
332
333 for (i = 0; src[i] != '\0'; i++)
334 dest[i] = src[i];
335 dest[i] = '\0';
336}
337
0974a682 338static void id128_to_efi_guid(sd_id128_t id, void *guid) {
3c7dddac
ZJS
339 struct guid uuid = {
340 .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3],
341 .u2 = id.bytes[4] << 8 | id.bytes[5],
342 .u3 = id.bytes[6] << 8 | id.bytes[7],
343 };
344 memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4));
345 memcpy(guid, &uuid, sizeof(uuid));
0974a682
KS
346}
347
348static uint16_t *tilt_slashes(uint16_t *s) {
349 uint16_t *p;
350
351 for (p = s; *p; p++)
352 if (*p == '/')
353 *p = '\\';
354
355 return s;
356}
357
337eed30
LP
358int efi_add_boot_option(
359 uint16_t id,
360 const char *title,
361 uint32_t part,
362 uint64_t pstart,
363 uint64_t psize,
364 sd_id128_t part_uuid,
365 const char *path) {
366
367 size_t size, title_len, path_len;
368 _cleanup_free_ char *buf = NULL;
0974a682
KS
369 struct boot_option *option;
370 struct device_path *devicep;
337eed30
LP
371 char boot_id[9];
372
373 if (!is_efi_boot())
374 return -EOPNOTSUPP;
0974a682
KS
375
376 title_len = (strlen(title)+1) * 2;
377 path_len = (strlen(path)+1) * 2;
378
f7cb1c79 379 buf = malloc0(offsetof(struct boot_option, title) + title_len +
e78c250b
LP
380 sizeof(struct drive_path) +
381 sizeof(struct device_path) + path_len);
b7749eb5
ZJS
382 if (!buf)
383 return -ENOMEM;
0974a682
KS
384
385 /* header */
386 option = (struct boot_option *)buf;
387 option->attr = LOAD_OPTION_ACTIVE;
388 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
389 offsetof(struct device_path, path) + path_len +
390 offsetof(struct device_path, path);
391 to_utf16(option->title, title);
392 size = offsetof(struct boot_option, title) + title_len;
393
394 /* partition info */
395 devicep = (struct device_path *)(buf + size);
396 devicep->type = MEDIA_DEVICE_PATH;
397 devicep->sub_type = MEDIA_HARDDRIVE_DP;
398 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
3c7dddac
ZJS
399 memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t));
400 memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t));
401 memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t));
0974a682 402 id128_to_efi_guid(part_uuid, devicep->drive.signature);
3c7dddac
ZJS
403 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
404 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
0974a682
KS
405 size += devicep->length;
406
407 /* path to loader */
408 devicep = (struct device_path *)(buf + size);
409 devicep->type = MEDIA_DEVICE_PATH;
410 devicep->sub_type = MEDIA_FILEPATH_DP;
411 devicep->length = offsetof(struct device_path, path) + path_len;
412 to_utf16(devicep->path, path);
413 tilt_slashes(devicep->path);
414 size += devicep->length;
415
416 /* end of path */
417 devicep = (struct device_path *)(buf + size);
418 devicep->type = END_DEVICE_PATH_TYPE;
419 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
420 devicep->length = offsetof(struct device_path, path);
421 size += devicep->length;
422
b7749eb5
ZJS
423 xsprintf(boot_id, "Boot%04X", id);
424 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size);
0974a682
KS
425}
426
427int efi_remove_boot_option(uint16_t id) {
428 char boot_id[9];
429
337eed30
LP
430 if (!is_efi_boot())
431 return -EOPNOTSUPP;
432
b7749eb5 433 xsprintf(boot_id, "Boot%04X", id);
0974a682
KS
434 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0);
435}
436
9cde64ff 437int efi_get_boot_order(uint16_t **order) {
0797f232 438 _cleanup_free_ void *buf = NULL;
7b4d7cc0 439 size_t l;
9cde64ff 440 int r;
7b4d7cc0 441
337eed30
LP
442 if (!is_efi_boot())
443 return -EOPNOTSUPP;
444
9cde64ff
LP
445 r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l);
446 if (r < 0)
447 return r;
7b4d7cc0 448
0797f232 449 if (l <= 0)
7b4d7cc0 450 return -ENOENT;
7b4d7cc0 451
0797f232
ZJS
452 if (l % sizeof(uint16_t) > 0 ||
453 l / sizeof(uint16_t) > INT_MAX)
7b4d7cc0 454 return -EINVAL;
7b4d7cc0 455
ae2a15bc 456 *order = TAKE_PTR(buf);
9cde64ff
LP
457 return (int) (l / sizeof(uint16_t));
458}
459
0974a682 460int efi_set_boot_order(uint16_t *order, size_t n) {
337eed30
LP
461
462 if (!is_efi_boot())
463 return -EOPNOTSUPP;
464
0974a682
KS
465 return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t));
466}
467
3042bbeb 468static int boot_id_hex(const char s[static 4]) {
e78c250b 469 int id = 0, i;
4d34c495 470
2caa38e9
LP
471 assert(s);
472
4d34c495
KS
473 for (i = 0; i < 4; i++)
474 if (s[i] >= '0' && s[i] <= '9')
475 id |= (s[i] - '0') << (3 - i) * 4;
476 else if (s[i] >= 'A' && s[i] <= 'F')
477 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
478 else
7e8185ef 479 return -EINVAL;
4d34c495
KS
480
481 return id;
482}
483
93bab288
YW
484static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
485 return CMP(*a, *b);
9db11a99
LP
486}
487
9cde64ff
LP
488int efi_get_boot_options(uint16_t **options) {
489 _cleanup_closedir_ DIR *dir = NULL;
b7749eb5 490 _cleanup_free_ uint16_t *list = NULL;
e78c250b 491 struct dirent *de;
7432b24b 492 size_t alloc = 0;
b7749eb5 493 int count = 0;
9cde64ff
LP
494
495 assert(options);
496
337eed30
LP
497 if (!is_efi_boot())
498 return -EOPNOTSUPP;
499
9cde64ff
LP
500 dir = opendir("/sys/firmware/efi/efivars/");
501 if (!dir)
502 return -errno;
503
b7749eb5 504 FOREACH_DIRENT(de, dir, return -errno) {
4d34c495 505 int id;
9cde64ff
LP
506
507 if (strncmp(de->d_name, "Boot", 4) != 0)
508 continue;
509
4d34c495 510 if (strlen(de->d_name) != 45)
9cde64ff
LP
511 continue;
512
513 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
514 continue;
515
4d34c495
KS
516 id = boot_id_hex(de->d_name + 4);
517 if (id < 0)
9cde64ff
LP
518 continue;
519
7432b24b 520 if (!GREEDY_REALLOC(list, alloc, count + 1))
b7749eb5 521 return -ENOMEM;
9cde64ff 522
7432b24b 523 list[count++] = id;
9cde64ff
LP
524 }
525
93bab288 526 typesafe_qsort(list, count, cmp_uint16);
9db11a99 527
1cc6c93a
YW
528 *options = TAKE_PTR(list);
529
9cde64ff 530 return count;
7b4d7cc0
KS
531}
532
5dbe9f53 533static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
2e3d0692 534 _cleanup_free_ char *j = NULL;
2e3d0692 535 int r;
39883f62 536 uint64_t x = 0;
2e3d0692
LP
537
538 assert(name);
539 assert(u);
540
61cc634b 541 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j);
2e3d0692
LP
542 if (r < 0)
543 return r;
544
2e3d0692
LP
545 r = safe_atou64(j, &x);
546 if (r < 0)
547 return r;
548
5dbe9f53 549 *u = x;
2e3d0692
LP
550 return 0;
551}
552
c51d84dc 553int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
2e3d0692
LP
554 uint64_t x, y;
555 int r;
2e3d0692
LP
556
557 assert(firmware);
558 assert(loader);
559
337eed30
LP
560 if (!is_efi_boot())
561 return -EOPNOTSUPP;
562
e9cea16d 563 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
2e3d0692
LP
564 if (r < 0)
565 return r;
566
e9cea16d 567 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
2e3d0692
LP
568 if (r < 0)
569 return r;
570
571 if (y == 0 || y < x)
572 return -EIO;
573
574 if (y > USEC_PER_HOUR)
575 return -EIO;
576
577 *firmware = x;
578 *loader = y;
579
580 return 0;
581}
582
c51d84dc 583int efi_loader_get_device_part_uuid(sd_id128_t *u) {
f4ce2b3e 584 _cleanup_free_ char *p = NULL;
f4ce2b3e 585 int r, parsed[16];
f4ce2b3e 586
337eed30
LP
587 if (!is_efi_boot())
588 return -EOPNOTSUPP;
589
61cc634b 590 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p);
f4ce2b3e
LP
591 if (r < 0)
592 return r;
593
bd44566c 594 if (sscanf(p, SD_ID128_UUID_FORMAT_STR,
f4ce2b3e
LP
595 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
596 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
597 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
598 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
599 return -EIO;
600
73b80ec2
LP
601 if (u) {
602 unsigned i;
603
604 for (i = 0; i < ELEMENTSOF(parsed); i++)
605 u->bytes[i] = parsed[i];
606 }
f4ce2b3e
LP
607
608 return 0;
609}
b872e9a0 610
bd2865ca
LP
611int efi_loader_get_entries(char ***ret) {
612 _cleanup_free_ char16_t *entries = NULL;
613 _cleanup_strv_free_ char **l = NULL;
614 size_t size, i, start;
615 int r;
616
617 assert(ret);
618
619 if (!is_efi_boot())
620 return -EOPNOTSUPP;
621
622 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntries", NULL, (void**) &entries, &size);
623 if (r < 0)
624 return r;
625
626 /* The variable contains a series of individually NUL terminated UTF-16 strings. */
627
628 for (i = 0, start = 0;; i++) {
68d7c268 629 _cleanup_free_ char *decoded = NULL;
bd2865ca
LP
630 bool end;
631
632 /* Is this the end of the variable's data? */
633 end = i * sizeof(char16_t) >= size;
634
635 /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If
636 * so, let's go to the next entry. */
637 if (!end && entries[i] != 0)
638 continue;
639
640 /* We reached the end of a string, let's decode it into UTF-8 */
641 decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t));
642 if (!decoded)
643 return -ENOMEM;
644
68d7c268
LP
645 if (efi_loader_entry_name_valid(decoded)) {
646 r = strv_consume(&l, TAKE_PTR(decoded));
647 if (r < 0)
648 return r;
649 } else
650 log_debug("Ignoring invalid loader entry '%s'.", decoded);
bd2865ca
LP
651
652 /* We reached the end of the variable */
653 if (end)
654 break;
655
656 /* Continue after the NUL byte */
657 start = i + 1;
658 }
659
660 *ret = TAKE_PTR(l);
661 return 0;
662}
663
80641a81
LP
664int efi_loader_get_features(uint64_t *ret) {
665 _cleanup_free_ void *v = NULL;
666 size_t s;
667 int r;
668
669 if (!is_efi_boot()) {
670 *ret = 0;
671 return 0;
672 }
673
674 r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderFeatures", NULL, &v, &s);
675 if (r == -ENOENT) {
676 _cleanup_free_ char *info = NULL;
677
678 /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */
679 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info);
680 if (r < 0) {
681 if (r != -ENOENT)
682 return r;
683
684 /* Variable not set, definitely means not systemd-boot */
685
686 } else if (first_word(info, "systemd-boot")) {
687
688 /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty
689 * static in all its versions. */
690
691 *ret = EFI_LOADER_FEATURE_CONFIG_TIMEOUT |
692 EFI_LOADER_FEATURE_ENTRY_DEFAULT |
693 EFI_LOADER_FEATURE_ENTRY_ONESHOT;
694
695 return 0;
696 }
697
698 /* No features supported */
699 *ret = 0;
700 return 0;
701 }
702 if (r < 0)
703 return r;
704
baaa35ad
ZJS
705 if (s != sizeof(uint64_t))
706 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
707 "LoaderFeatures EFI variable doesn't have the right size.");
80641a81
LP
708
709 memcpy(ret, v, sizeof(uint64_t));
710 return 0;
711}
6917857e 712
713#endif
714
995cbd72
LP
715bool efi_loader_entry_name_valid(const char *s) {
716 if (isempty(s))
717 return false;
718
719 if (strlen(s) > FILENAME_MAX) /* Make sure entry names fit in filenames */
720 return false;
721
030a5d78 722 return in_charset(s, ALPHANUMERICAL "+-_.");
995cbd72
LP
723}
724
6917857e 725char *efi_tilt_backslashes(char *s) {
726 char *p;
727
728 for (p = s; *p; p++)
729 if (*p == '\\')
730 *p = '/';
731
732 return s;
733}