]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/efivars.c
Merge pull request #653 from dvdhrm/bus-gold
[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
273 if (asprintf(&p,
274 "/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
275 name, SD_ID128_FORMAT_VAL(vendor)) < 0)
276 return -ENOMEM;
277
278 if (size == 0) {
279 if (unlink(p) < 0)
280 return -errno;
281 return 0;
282 }
283
284 fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644);
285 if (fd < 0)
286 return -errno;
287
288 buf = malloc(sizeof(uint32_t) + size);
289 if (!buf)
290 return -ENOMEM;
291
292 buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS;
293 memcpy(buf->buf, value, size);
294
295 return loop_write(fd, buf, sizeof(uint32_t) + size, false);
296 }
297
298 int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) {
299 _cleanup_free_ void *s = NULL;
300 size_t ss = 0;
301 int r;
302 char *x;
303
304 r = efi_get_variable(vendor, name, NULL, &s, &ss);
305 if (r < 0)
306 return r;
307
308 x = utf16_to_utf8(s, ss);
309 if (!x)
310 return -ENOMEM;
311
312 *p = x;
313 return 0;
314 }
315
316 static size_t utf16_size(const uint16_t *s) {
317 size_t l = 0;
318
319 while (s[l] > 0)
320 l++;
321
322 return (l+1) * sizeof(uint16_t);
323 }
324
325 static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
326 struct uuid {
327 uint32_t u1;
328 uint16_t u2;
329 uint16_t u3;
330 uint8_t u4[8];
331 } _packed_;
332 const struct uuid *uuid = guid;
333
334 id128->bytes[0] = (uuid->u1 >> 24) & 0xff;
335 id128->bytes[1] = (uuid->u1 >> 16) & 0xff;
336 id128->bytes[2] = (uuid->u1 >> 8) & 0xff;
337 id128->bytes[3] = (uuid->u1) & 0xff;
338 id128->bytes[4] = (uuid->u2 >> 8) & 0xff;
339 id128->bytes[5] = (uuid->u2) & 0xff;
340 id128->bytes[6] = (uuid->u3 >> 8) & 0xff;
341 id128->bytes[7] = (uuid->u3) & 0xff;
342 memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
343 }
344
345 int efi_get_boot_option(
346 uint16_t id,
347 char **title,
348 sd_id128_t *part_uuid,
349 char **path,
350 bool *active) {
351
352 char boot_id[9];
353 _cleanup_free_ uint8_t *buf = NULL;
354 size_t l;
355 struct boot_option *header;
356 size_t title_size;
357 _cleanup_free_ char *s = NULL, *p = NULL;
358 sd_id128_t p_uuid = SD_ID128_NULL;
359 int r;
360
361 xsprintf(boot_id, "Boot%04X", id);
362 r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l);
363 if (r < 0)
364 return r;
365 if (l < sizeof(struct boot_option))
366 return -ENOENT;
367
368 header = (struct boot_option *)buf;
369 title_size = utf16_size(header->title);
370 if (title_size > l - offsetof(struct boot_option, title))
371 return -EINVAL;
372
373 if (title) {
374 s = utf16_to_utf8(header->title, title_size);
375 if (!s)
376 return -ENOMEM;
377 }
378
379 if (header->path_len > 0) {
380 uint8_t *dbuf;
381 size_t dnext;
382
383 dbuf = buf + offsetof(struct boot_option, title) + title_size;
384 dnext = 0;
385 while (dnext < header->path_len) {
386 struct device_path *dpath;
387
388 dpath = (struct device_path *)(dbuf + dnext);
389 if (dpath->length < 4)
390 break;
391
392 /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
393 if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE)
394 break;
395
396 dnext += dpath->length;
397
398 /* Type 0x04 – Media Device Path */
399 if (dpath->type != MEDIA_DEVICE_PATH)
400 continue;
401
402 /* Sub-Type 1 – Hard Drive */
403 if (dpath->sub_type == MEDIA_HARDDRIVE_DP) {
404 /* 0x02 – GUID Partition Table */
405 if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER)
406 continue;
407
408 /* 0x02 – GUID signature */
409 if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID)
410 continue;
411
412 if (part_uuid)
413 efi_guid_to_id128(dpath->drive.signature, &p_uuid);
414 continue;
415 }
416
417 /* Sub-Type 4 – File Path */
418 if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) {
419 p = utf16_to_utf8(dpath->path, dpath->length-4);
420 efi_tilt_backslashes(p);
421 continue;
422 }
423 }
424 }
425
426 if (title) {
427 *title = s;
428 s = NULL;
429 }
430 if (part_uuid)
431 *part_uuid = p_uuid;
432 if (path) {
433 *path = p;
434 p = NULL;
435 }
436 if (active)
437 *active = !!(header->attr & LOAD_OPTION_ACTIVE);
438
439 return 0;
440 }
441
442 static void to_utf16(uint16_t *dest, const char *src) {
443 int i;
444
445 for (i = 0; src[i] != '\0'; i++)
446 dest[i] = src[i];
447 dest[i] = '\0';
448 }
449
450 struct guid {
451 uint32_t u1;
452 uint16_t u2;
453 uint16_t u3;
454 uint8_t u4[8];
455 } _packed_;
456
457 static void id128_to_efi_guid(sd_id128_t id, void *guid) {
458 struct guid *uuid = guid;
459
460 uuid->u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3];
461 uuid->u2 = id.bytes[4] << 8 | id.bytes[5];
462 uuid->u3 = id.bytes[6] << 8 | id.bytes[7];
463 memcpy(uuid->u4, id.bytes+8, sizeof(uuid->u4));
464 }
465
466 static uint16_t *tilt_slashes(uint16_t *s) {
467 uint16_t *p;
468
469 for (p = s; *p; p++)
470 if (*p == '/')
471 *p = '\\';
472
473 return s;
474 }
475
476 int efi_add_boot_option(uint16_t id, const char *title,
477 uint32_t part, uint64_t pstart, uint64_t psize,
478 sd_id128_t part_uuid, const char *path) {
479 char boot_id[9];
480 size_t size;
481 size_t title_len;
482 size_t path_len;
483 struct boot_option *option;
484 struct device_path *devicep;
485 _cleanup_free_ char *buf = NULL;
486
487 title_len = (strlen(title)+1) * 2;
488 path_len = (strlen(path)+1) * 2;
489
490 buf = calloc(sizeof(struct boot_option) + title_len +
491 sizeof(struct drive_path) +
492 sizeof(struct device_path) + path_len, 1);
493 if (!buf)
494 return -ENOMEM;
495
496 /* header */
497 option = (struct boot_option *)buf;
498 option->attr = LOAD_OPTION_ACTIVE;
499 option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) +
500 offsetof(struct device_path, path) + path_len +
501 offsetof(struct device_path, path);
502 to_utf16(option->title, title);
503 size = offsetof(struct boot_option, title) + title_len;
504
505 /* partition info */
506 devicep = (struct device_path *)(buf + size);
507 devicep->type = MEDIA_DEVICE_PATH;
508 devicep->sub_type = MEDIA_HARDDRIVE_DP;
509 devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path);
510 devicep->drive.part_nr = part;
511 devicep->drive.part_start = pstart;
512 devicep->drive.part_size = psize;
513 devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
514 devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
515 id128_to_efi_guid(part_uuid, devicep->drive.signature);
516 size += devicep->length;
517
518 /* path to loader */
519 devicep = (struct device_path *)(buf + size);
520 devicep->type = MEDIA_DEVICE_PATH;
521 devicep->sub_type = MEDIA_FILEPATH_DP;
522 devicep->length = offsetof(struct device_path, path) + path_len;
523 to_utf16(devicep->path, path);
524 tilt_slashes(devicep->path);
525 size += devicep->length;
526
527 /* end of path */
528 devicep = (struct device_path *)(buf + size);
529 devicep->type = END_DEVICE_PATH_TYPE;
530 devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE;
531 devicep->length = offsetof(struct device_path, path);
532 size += devicep->length;
533
534 xsprintf(boot_id, "Boot%04X", id);
535 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size);
536 }
537
538 int efi_remove_boot_option(uint16_t id) {
539 char boot_id[9];
540
541 xsprintf(boot_id, "Boot%04X", id);
542 return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0);
543 }
544
545 int efi_get_boot_order(uint16_t **order) {
546 _cleanup_free_ void *buf = NULL;
547 size_t l;
548 int r;
549
550 r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l);
551 if (r < 0)
552 return r;
553
554 if (l <= 0)
555 return -ENOENT;
556
557 if (l % sizeof(uint16_t) > 0 ||
558 l / sizeof(uint16_t) > INT_MAX)
559 return -EINVAL;
560
561 *order = buf;
562 buf = NULL;
563 return (int) (l / sizeof(uint16_t));
564 }
565
566 int efi_set_boot_order(uint16_t *order, size_t n) {
567 return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t));
568 }
569
570 static int boot_id_hex(const char s[4]) {
571 int i;
572 int id = 0;
573
574 for (i = 0; i < 4; i++)
575 if (s[i] >= '0' && s[i] <= '9')
576 id |= (s[i] - '0') << (3 - i) * 4;
577 else if (s[i] >= 'A' && s[i] <= 'F')
578 id |= (s[i] - 'A' + 10) << (3 - i) * 4;
579 else
580 return -EINVAL;
581
582 return id;
583 }
584
585 static int cmp_uint16(const void *_a, const void *_b) {
586 const uint16_t *a = _a, *b = _b;
587
588 return (int)*a - (int)*b;
589 }
590
591 int efi_get_boot_options(uint16_t **options) {
592 _cleanup_closedir_ DIR *dir = NULL;
593 struct dirent *de;
594 _cleanup_free_ uint16_t *list = NULL;
595 size_t alloc = 0;
596 int count = 0;
597
598 assert(options);
599
600 dir = opendir("/sys/firmware/efi/efivars/");
601 if (!dir)
602 return -errno;
603
604 FOREACH_DIRENT(de, dir, return -errno) {
605 int id;
606
607 if (strncmp(de->d_name, "Boot", 4) != 0)
608 continue;
609
610 if (strlen(de->d_name) != 45)
611 continue;
612
613 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
614 continue;
615
616 id = boot_id_hex(de->d_name + 4);
617 if (id < 0)
618 continue;
619
620 if (!GREEDY_REALLOC(list, alloc, count + 1))
621 return -ENOMEM;
622
623 list[count++] = id;
624 }
625
626 qsort_safe(list, count, sizeof(uint16_t), cmp_uint16);
627
628 *options = list;
629 list = NULL;
630 return count;
631 }
632
633 static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) {
634 _cleanup_free_ char *j = NULL;
635 int r;
636 uint64_t x = 0;
637
638 assert(name);
639 assert(u);
640
641 r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j);
642 if (r < 0)
643 return r;
644
645 r = safe_atou64(j, &x);
646 if (r < 0)
647 return r;
648
649 *u = x;
650 return 0;
651 }
652
653 int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) {
654 uint64_t x, y;
655 int r;
656
657 assert(firmware);
658 assert(loader);
659
660 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x);
661 if (r < 0)
662 return r;
663
664 r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y);
665 if (r < 0)
666 return r;
667
668 if (y == 0 || y < x)
669 return -EIO;
670
671 if (y > USEC_PER_HOUR)
672 return -EIO;
673
674 *firmware = x;
675 *loader = y;
676
677 return 0;
678 }
679
680 int efi_loader_get_device_part_uuid(sd_id128_t *u) {
681 _cleanup_free_ char *p = NULL;
682 int r, parsed[16];
683
684 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p);
685 if (r < 0)
686 return r;
687
688 if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
689 &parsed[0], &parsed[1], &parsed[2], &parsed[3],
690 &parsed[4], &parsed[5], &parsed[6], &parsed[7],
691 &parsed[8], &parsed[9], &parsed[10], &parsed[11],
692 &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16)
693 return -EIO;
694
695 if (u) {
696 unsigned i;
697
698 for (i = 0; i < ELEMENTSOF(parsed); i++)
699 u->bytes[i] = parsed[i];
700 }
701
702 return 0;
703 }
704
705 #endif
706
707 char *efi_tilt_backslashes(char *s) {
708 char *p;
709
710 for (p = s; *p; p++)
711 if (*p == '\\')
712 *p = '/';
713
714 return s;
715 }