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