]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/efivars.c
tree-wide: use typesafe_qsort()
[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 <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include "sd-id128.h"
14
15 #include "alloc-util.h"
16 #include "dirent-util.h"
17 #include "efivars.h"
18 #include "fd-util.h"
19 #include "io-util.h"
20 #include "macro.h"
21 #include "parse-util.h"
22 #include "stdio-util.h"
23 #include "time-util.h"
24 #include "utf8.h"
25 #include "util.h"
26 #include "virt.h"
27
28 #if ENABLE_EFI
29
30 #define LOAD_OPTION_ACTIVE 0x00000001
31 #define MEDIA_DEVICE_PATH 0x04
32 #define MEDIA_HARDDRIVE_DP 0x01
33 #define MEDIA_FILEPATH_DP 0x04
34 #define SIGNATURE_TYPE_GUID 0x02
35 #define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
36 #define END_DEVICE_PATH_TYPE 0x7f
37 #define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff
38 #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001
39
40 struct boot_option {
41 uint32_t attr;
42 uint16_t path_len;
43 uint16_t title[];
44 } _packed_;
45
46 struct drive_path {
47 uint32_t part_nr;
48 uint64_t part_start;
49 uint64_t part_size;
50 char signature[16];
51 uint8_t mbr_type;
52 uint8_t signature_type;
53 } _packed_;
54
55 struct device_path {
56 uint8_t type;
57 uint8_t sub_type;
58 uint16_t length;
59 union {
60 uint16_t path[0];
61 struct drive_path drive;
62 };
63 } _packed_;
64
65 bool is_efi_boot(void) {
66 return access("/sys/firmware/efi", F_OK) >= 0;
67 }
68
69 static int read_flag(const char *varname) {
70 _cleanup_free_ void *v = NULL;
71 uint8_t b;
72 size_t s;
73 int r;
74
75 r = efi_get_variable(EFI_VENDOR_GLOBAL, varname, NULL, &v, &s);
76 if (r < 0)
77 return r;
78
79 if (s != 1)
80 return -EINVAL;
81
82 b = *(uint8_t *)v;
83 return b > 0;
84 }
85
86 bool is_efi_secure_boot(void) {
87 return read_flag("SecureBoot") > 0;
88 }
89
90 bool is_efi_secure_boot_setup_mode(void) {
91 return read_flag("SetupMode") > 0;
92 }
93
94 int efi_reboot_to_firmware_supported(void) {
95 _cleanup_free_ void *v = NULL;
96 uint64_t b;
97 size_t s;
98 int r;
99
100 if (!is_efi_boot() || detect_container() > 0)
101 return -EOPNOTSUPP;
102
103 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndicationsSupported", NULL, &v, &s);
104 if (r == -ENOENT) /* variable doesn't exist? it's not supported then */
105 return -EOPNOTSUPP;
106 if (r < 0)
107 return r;
108 if (s != sizeof(uint64_t))
109 return -EINVAL;
110
111 b = *(uint64_t*) v;
112 if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI))
113 return -EOPNOTSUPP; /* bit unset? it's not supported then */
114
115 return 0;
116 }
117
118 static int get_os_indications(uint64_t *os_indication) {
119 _cleanup_free_ void *v = NULL;
120 size_t s;
121 int r;
122
123 r = efi_reboot_to_firmware_supported();
124 if (r < 0)
125 return r;
126
127 r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndications", NULL, &v, &s);
128 if (r == -ENOENT) {
129 /* Some firmware implementations that do support
130 * OsIndications and report that with
131 * OsIndicationsSupported will remove the
132 * OsIndications variable when it is unset. Let's
133 * pretend it's 0 then, to hide this implementation
134 * detail. Note that this call will return -ENOENT
135 * then only if the support for OsIndications is
136 * missing entirely, as determined by
137 * efi_reboot_to_firmware_supported() above. */
138 *os_indication = 0;
139 return 0;
140 } else if (r < 0)
141 return r;
142 else if (s != sizeof(uint64_t))
143 return -EINVAL;
144
145 *os_indication = *(uint64_t *)v;
146 return 0;
147 }
148
149 int efi_get_reboot_to_firmware(void) {
150 int r;
151 uint64_t b;
152
153 r = get_os_indications(&b);
154 if (r < 0)
155 return r;
156
157 return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI);
158 }
159
160 int efi_set_reboot_to_firmware(bool value) {
161 int r;
162 uint64_t b, b_new;
163
164 r = get_os_indications(&b);
165 if (r < 0)
166 return r;
167
168 if (value)
169 b_new = b | EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
170 else
171 b_new = b & ~EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
172
173 /* Avoid writing to efi vars store if we can due to firmware bugs. */
174 if (b != b_new)
175 return efi_set_variable(EFI_VENDOR_GLOBAL, "OsIndications", &b_new, sizeof(uint64_t));
176
177 return 0;
178 }
179
180 int efi_get_variable(
181 sd_id128_t vendor,
182 const char *name,
183 uint32_t *attribute,
184 void **value,
185 size_t *size) {
186
187 _cleanup_close_ int fd = -1;
188 _cleanup_free_ char *p = NULL;
189 uint32_t a;
190 ssize_t n;
191 struct stat st;
192 _cleanup_free_ void *buf = NULL;
193
194 assert(name);
195 assert(value);
196 assert(size);
197
198 if (asprintf(&p,
199 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
200 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
201 return -ENOMEM;
202
203 fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC);
204 if (fd < 0)
205 return -errno;
206
207 if (fstat(fd, &st) < 0)
208 return -errno;
209 if (st.st_size < 4)
210 return -EIO;
211 if (st.st_size > 4*1024*1024 + 4)
212 return -E2BIG;
213
214 n = read(fd, &a, sizeof(a));
215 if (n < 0)
216 return -errno;
217 if (n != sizeof(a))
218 return -EIO;
219
220 buf = malloc(st.st_size - 4 + 2);
221 if (!buf)
222 return -ENOMEM;
223
224 n = read(fd, buf, (size_t) st.st_size - 4);
225 if (n < 0)
226 return -errno;
227 if (n != (ssize_t) st.st_size - 4)
228 return -EIO;
229
230 /* Always NUL terminate (2 bytes, to protect UTF-16) */
231 ((char*) buf)[st.st_size - 4] = 0;
232 ((char*) buf)[st.st_size - 4 + 1] = 0;
233
234 *value = TAKE_PTR(buf);
235 *size = (size_t) st.st_size - 4;
236
237 if (attribute)
238 *attribute = a;
239
240 return 0;
241 }
242
243 int efi_set_variable(
244 sd_id128_t vendor,
245 const char *name,
246 const void *value,
247 size_t size) {
248
249 struct var {
250 uint32_t attr;
251 char buf[];
252 } _packed_ * _cleanup_free_ buf = NULL;
253 _cleanup_free_ char *p = NULL;
254 _cleanup_close_ int fd = -1;
255
256 assert(name);
257 assert(value || size == 0);
258
259 if (asprintf(&p,
260 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
261 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
262 return -ENOMEM;
263
264 if (size == 0) {
265 if (unlink(p) < 0)
266 return -errno;
267 return 0;
268 }
269
270 fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644);
271 if (fd < 0)
272 return -errno;
273
274 buf = malloc(sizeof(uint32_t) + size);
275 if (!buf)
276 return -ENOMEM;
277
278 buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
279 memcpy(buf->buf, value, size);
280
281 return loop_write(fd, buf, sizeof(uint32_t) + size, false);
282 }
283
284 int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) {
285 _cleanup_free_ void *s = NULL;
286 size_t ss = 0;
287 int r;
288 char *x;
289
290 r = efi_get_variable(vendor, name, NULL, &s, &ss);
291 if (r < 0)
292 return r;
293
294 x = utf16_to_utf8(s, ss);
295 if (!x)
296 return -ENOMEM;
297
298 *p = x;
299 return 0;
300 }
301
302 static size_t utf16_size(const uint16_t *s) {
303 size_t l = 0;
304
305 while (s[l] > 0)
306 l++;
307
308 return (l+1) * sizeof(uint16_t);
309 }
310
311 static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
312 struct uuid {
313 uint32_t u1;
314 uint16_t u2;
315 uint16_t u3;
316 uint8_t u4[8];
317 } _packed_;
318 const struct uuid *uuid = guid;
319
320 id128->bytes[0] = (uuid->u1 >> 24) & 0xff;
321 id128->bytes[1] = (uuid->u1 >> 16) & 0xff;
322 id128->bytes[2] = (uuid->u1 >> 8) & 0xff;
323 id128->bytes[3] = (uuid->u1) & 0xff;
324 id128->bytes[4] = (uuid->u2 >> 8) & 0xff;
325 id128->bytes[5] = (uuid->u2) & 0xff;
326 id128->bytes[6] = (uuid->u3 >> 8) & 0xff;
327 id128->bytes[7] = (uuid->u3) & 0xff;
328 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
329 }
330
331 int efi_get_boot_option(
332 uint16_t id,
333 char **title,
334 sd_id128_t *part_uuid,
335 char **path,
336 bool *active) {
337
338 char boot_id[9];
339 _cleanup_free_ uint8_t *buf = NULL;
340 size_t l;
341 struct boot_option *header;
342 size_t title_size;
343 _cleanup_free_ char *s = NULL, *p = NULL;
344 sd_id128_t p_uuid = SD_ID128_NULL;
345 int r;
346
347 xsprintf(boot_id, "Boot%04X", id);
348 r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l);
349 if (r < 0)
350 return r;
351 if (l < sizeof(struct boot_option))
352 return -ENOENT;
353
354 header = (struct boot_option *)buf;
355 title_size = utf16_size(header->title);
356 if (title_size > l - offsetof(struct boot_option, title))
357 return -EINVAL;
358
359 if (title) {
360 s = utf16_to_utf8(header->title, title_size);
361 if (!s)
362 return -ENOMEM;
363 }
364
365 if (header->path_len > 0) {
366 uint8_t *dbuf;
367 size_t dnext;
368
369 dbuf = buf + offsetof(struct boot_option, title) + title_size;
370 dnext = 0;
371 while (dnext < header->path_len) {
372 struct device_path *dpath;
373
374 dpath = (struct device_path *)(dbuf + dnext);
375 if (dpath->length < 4)
376 break;
377
378 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
379 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
380 break;
381
382 dnext += dpath->length;
383
384 /* Type 0x04 – Media Device Path */
385 if (dpath->type != MEDIA_DEVICE_PATH)
386 continue;
387
388 /* Sub-Type 1 – Hard Drive */
389 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
390 /* 0x02 – GUID Partition Table */
391 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
392 continue;
393
394 /* 0x02 – GUID signature */
395 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
396 continue;
397
398 if (part_uuid)
399 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
400 continue;
401 }
402
403 /* Sub-Type 4 – File Path */
404 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
405 p = utf16_to_utf8(dpath->path, dpath->length-4);
406 efi_tilt_backslashes(p);
407 continue;
408 }
409 }
410 }
411
412 if (title)
413 *title = TAKE_PTR(s);
414 if (part_uuid)
415 *part_uuid = p_uuid;
416 if (path)
417 *path = TAKE_PTR(p);
418 if (active)
419 *active = !!(header->attr & LOAD_OPTION_ACTIVE);
420
421 return 0;
422 }
423
424 static void to_utf16(uint16_t *dest, const char *src) {
425 int i;
426
427 for (i = 0; src[i] != '\0'; i++)
428 dest[i] = src[i];
429 dest[i] = '\0';
430 }
431
432 struct guid {
433 uint32_t u1;
434 uint16_t u2;
435 uint16_t u3;
436 uint8_t u4[8];
437 } _packed_;
438
439 static void id128_to_efi_guid(sd_id128_t id, void *guid) {
440 struct guid *uuid = guid;
441
442 uuid->u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3];
443 uuid->u2 = id.bytes[4] << 8 | id.bytes[5];
444 uuid->u3 = id.bytes[6] << 8 | id.bytes[7];
445 memcpy(uuid->u4, id.bytes+8, sizeof(uuid->u4));
446 }
447
448 static uint16_t *tilt_slashes(uint16_t *s) {
449 uint16_t *p;
450
451 for (p = s; *p; p++)
452 if (*p == '/')
453 *p = '\\';
454
455 return s;
456 }
457
458 int efi_add_boot_option(uint16_t id, const char *title,
459 uint32_t part, uint64_t pstart, uint64_t psize,
460 sd_id128_t part_uuid, const char *path) {
461 char boot_id[9];
462 size_t size;
463 size_t title_len;
464 size_t path_len;
465 struct boot_option *option;
466 struct device_path *devicep;
467 _cleanup_free_ char *buf = NULL;
468
469 title_len = (strlen(title)+1) * 2;
470 path_len = (strlen(path)+1) * 2;
471
472 buf = calloc(sizeof(struct boot_option) + title_len +
473 sizeof(struct drive_path) +
474 sizeof(struct device_path) + path_len, 1);
475 if (!buf)
476 return -ENOMEM;
477
478 /* header */
479 option = (struct boot_option *)buf;
480 option->attr = LOAD_OPTION_ACTIVE;
481 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
482 offsetof(struct device_path, path) + path_len +
483 offsetof(struct device_path, path);
484 to_utf16(option->title, title);
485 size = offsetof(struct boot_option, title) + title_len;
486
487 /* partition info */
488 devicep = (struct device_path *)(buf + size);
489 devicep->type = MEDIA_DEVICE_PATH;
490 devicep->sub_type = MEDIA_HARDDRIVE_DP;
491 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
492 devicep->drive.part_nr = part;
493 devicep->drive.part_start = pstart;
494 devicep->drive.part_size = psize;
495 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
496 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
497 id128_to_efi_guid(part_uuid, devicep->drive.signature);
498 size += devicep->length;
499
500 /* path to loader */
501 devicep = (struct device_path *)(buf + size);
502 devicep->type = MEDIA_DEVICE_PATH;
503 devicep->sub_type = MEDIA_FILEPATH_DP;
504 devicep->length = offsetof(struct device_path, path) + path_len;
505 to_utf16(devicep->path, path);
506 tilt_slashes(devicep->path);
507 size += devicep->length;
508
509 /* end of path */
510 devicep = (struct device_path *)(buf + size);
511 devicep->type = END_DEVICE_PATH_TYPE;
512 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
513 devicep->length = offsetof(struct device_path, path);
514 size += devicep->length;
515
516 xsprintf(boot_id, "Boot%04X", id);
517 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size);
518 }
519
520 int efi_remove_boot_option(uint16_t id) {
521 char boot_id[9];
522
523 xsprintf(boot_id, "Boot%04X", id);
524 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0);
525 }
526
527 int efi_get_boot_order(uint16_t **order) {
528 _cleanup_free_ void *buf = NULL;
529 size_t l;
530 int r;
531
532 r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l);
533 if (r < 0)
534 return r;
535
536 if (l <= 0)
537 return -ENOENT;
538
539 if (l % sizeof(uint16_t) > 0 ||
540 l / sizeof(uint16_t) > INT_MAX)
541 return -EINVAL;
542
543 *order = TAKE_PTR(buf);
544 return (int) (l / sizeof(uint16_t));
545 }
546
547 int efi_set_boot_order(uint16_t *order, size_t n) {
548 return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t));
549 }
550
551 static int boot_id_hex(const char s[4]) {
552 int i;
553 int id = 0;
554
555 for (i = 0; i < 4; i++)
556 if (s[i] >= '0' && s[i] <= '9')
557 id |= (s[i] - '0') << (3 - i) * 4;
558 else if (s[i] >= 'A' && s[i] <= 'F')
559 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
560 else
561 return -EINVAL;
562
563 return id;
564 }
565
566 static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
567 return CMP(*a, *b);
568 }
569
570 int efi_get_boot_options(uint16_t **options) {
571 _cleanup_closedir_ DIR *dir = NULL;
572 struct dirent *de;
573 _cleanup_free_ uint16_t *list = NULL;
574 size_t alloc = 0;
575 int count = 0;
576
577 assert(options);
578
579 dir = opendir("/sys/firmware/efi/efivars/");
580 if (!dir)
581 return -errno;
582
583 FOREACH_DIRENT(de, dir, return -errno) {
584 int id;
585
586 if (strncmp(de->d_name, "Boot", 4) != 0)
587 continue;
588
589 if (strlen(de->d_name) != 45)
590 continue;
591
592 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
593 continue;
594
595 id = boot_id_hex(de->d_name + 4);
596 if (id < 0)
597 continue;
598
599 if (!GREEDY_REALLOC(list, alloc, count + 1))
600 return -ENOMEM;
601
602 list[count++] = id;
603 }
604
605 typesafe_qsort(list, count, cmp_uint16);
606
607 *options = TAKE_PTR(list);
608
609 return count;
610 }
611
612 static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
613 _cleanup_free_ char *j = NULL;
614 int r;
615 uint64_t x = 0;
616
617 assert(name);
618 assert(u);
619
620 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j);
621 if (r < 0)
622 return r;
623
624 r = safe_atou64(j, &x);
625 if (r < 0)
626 return r;
627
628 *u = x;
629 return 0;
630 }
631
632 int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
633 uint64_t x, y;
634 int r;
635
636 assert(firmware);
637 assert(loader);
638
639 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
640 if (r < 0)
641 return r;
642
643 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
644 if (r < 0)
645 return r;
646
647 if (y == 0 || y < x)
648 return -EIO;
649
650 if (y > USEC_PER_HOUR)
651 return -EIO;
652
653 *firmware = x;
654 *loader = y;
655
656 return 0;
657 }
658
659 int efi_loader_get_device_part_uuid(sd_id128_t *u) {
660 _cleanup_free_ char *p = NULL;
661 int r, parsed[16];
662
663 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p);
664 if (r < 0)
665 return r;
666
667 if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
668 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
669 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
670 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
671 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
672 return -EIO;
673
674 if (u) {
675 unsigned i;
676
677 for (i = 0; i < ELEMENTSOF(parsed); i++)
678 u->bytes[i] = parsed[i];
679 }
680
681 return 0;
682 }
683
684 #endif
685
686 char *efi_tilt_backslashes(char *s) {
687 char *p;
688
689 for (p = s; *p; p++)
690 if (*p == '\\')
691 *p = '/';
692
693 return s;
694 }