]>
Commit | Line | Data |
---|---|---|
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" |
760877e9 | 24 | #include "sort-util.h" |
15a5e950 | 25 | #include "stdio-util.h" |
bd2865ca | 26 | #include "strv.h" |
a8fbdf54 | 27 | #include "time-util.h" |
2e3d0692 | 28 | #include "utf8.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 | 42 | |
f7cb1c79 ZJS |
43 | #define boot_option__contents { \ |
44 | uint32_t attr; \ | |
45 | uint16_t path_len; \ | |
46 | uint16_t title[]; \ | |
47 | } | |
48 | ||
49 | struct boot_option boot_option__contents; | |
50 | struct boot_option__packed boot_option__contents _packed_; | |
51 | assert_cc(offsetof(struct boot_option, title) == offsetof(struct boot_option__packed, title)); | |
52 | /* sizeof(struct boot_option) != sizeof(struct boot_option__packed), so | |
53 | * the *size* of the structure should not be used anywhere below. */ | |
0974a682 KS |
54 | |
55 | struct drive_path { | |
56 | uint32_t part_nr; | |
57 | uint64_t part_start; | |
58 | uint64_t part_size; | |
59 | char signature[16]; | |
60 | uint8_t mbr_type; | |
61 | uint8_t signature_type; | |
885fdebc | 62 | } _packed_; |
0974a682 | 63 | |
3c7dddac ZJS |
64 | #define device_path__contents { \ |
65 | uint8_t type; \ | |
66 | uint8_t sub_type; \ | |
67 | uint16_t length; \ | |
68 | union { \ | |
69 | uint16_t path[0]; \ | |
70 | struct drive_path drive; \ | |
71 | }; \ | |
72 | } | |
73 | ||
74 | struct device_path device_path__contents; | |
75 | struct device_path__packed device_path__contents _packed_; | |
76 | assert_cc(sizeof(struct device_path) == sizeof(struct device_path__packed)); | |
0974a682 | 77 | |
9cde64ff | 78 | bool is_efi_boot(void) { |
bb161cdc LP |
79 | if (detect_container() > 0) |
80 | return false; | |
81 | ||
82 | return access("/sys/firmware/efi/", F_OK) >= 0; | |
34e5a31e LP |
83 | } |
84 | ||
bc6f2e7c | 85 | static int read_flag(const char *varname) { |
b47d419c | 86 | _cleanup_free_ void *v = NULL; |
bc6f2e7c | 87 | uint8_t b; |
e22c567f LP |
88 | size_t s; |
89 | int r; | |
bc6f2e7c | 90 | |
337eed30 LP |
91 | if (!is_efi_boot()) /* If this is not an EFI boot, assume the queried flags are zero */ |
92 | return 0; | |
93 | ||
bc6f2e7c KS |
94 | r = efi_get_variable(EFI_VENDOR_GLOBAL, varname, NULL, &v, &s); |
95 | if (r < 0) | |
96 | return r; | |
97 | ||
b47d419c ZJS |
98 | if (s != 1) |
99 | return -EINVAL; | |
bc6f2e7c KS |
100 | |
101 | b = *(uint8_t *)v; | |
e78c250b | 102 | return !!b; |
bc6f2e7c KS |
103 | } |
104 | ||
9df49b33 TG |
105 | bool is_efi_secure_boot(void) { |
106 | return read_flag("SecureBoot") > 0; | |
bc6f2e7c KS |
107 | } |
108 | ||
9df49b33 TG |
109 | bool is_efi_secure_boot_setup_mode(void) { |
110 | return read_flag("SetupMode") > 0; | |
bc6f2e7c KS |
111 | } |
112 | ||
5bdf2243 | 113 | int efi_reboot_to_firmware_supported(void) { |
5bdf2243 | 114 | _cleanup_free_ void *v = NULL; |
e22c567f LP |
115 | uint64_t b; |
116 | size_t s; | |
117 | int r; | |
5bdf2243 | 118 | |
bb161cdc | 119 | if (!is_efi_boot()) |
5bdf2243 JJ |
120 | return -EOPNOTSUPP; |
121 | ||
122 | r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndicationsSupported", NULL, &v, &s); | |
846ab104 LP |
123 | if (r == -ENOENT) /* variable doesn't exist? it's not supported then */ |
124 | return -EOPNOTSUPP; | |
5bdf2243 JJ |
125 | if (r < 0) |
126 | return r; | |
e22c567f | 127 | if (s != sizeof(uint64_t)) |
5bdf2243 JJ |
128 | return -EINVAL; |
129 | ||
e22c567f LP |
130 | b = *(uint64_t*) v; |
131 | if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) | |
132 | return -EOPNOTSUPP; /* bit unset? it's not supported then */ | |
133 | ||
134 | return 0; | |
5bdf2243 JJ |
135 | } |
136 | ||
137 | static int get_os_indications(uint64_t *os_indication) { | |
5bdf2243 | 138 | _cleanup_free_ void *v = NULL; |
e22c567f LP |
139 | size_t s; |
140 | int r; | |
5bdf2243 | 141 | |
e78c250b | 142 | /* Let's verify general support first */ |
5bdf2243 JJ |
143 | r = efi_reboot_to_firmware_supported(); |
144 | if (r < 0) | |
145 | return r; | |
146 | ||
147 | r = efi_get_variable(EFI_VENDOR_GLOBAL, "OsIndications", NULL, &v, &s); | |
6b62bbbc | 148 | if (r == -ENOENT) { |
e78c250b LP |
149 | /* Some firmware implementations that do support OsIndications and report that with |
150 | * OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's pretend it's 0 | |
151 | * then, to hide this implementation detail. Note that this call will return -ENOENT then only if the | |
152 | * support for OsIndications is missing entirely, as determined by efi_reboot_to_firmware_supported() | |
153 | * above. */ | |
6b62bbbc LP |
154 | *os_indication = 0; |
155 | return 0; | |
156 | } else if (r < 0) | |
5bdf2243 JJ |
157 | return r; |
158 | else if (s != sizeof(uint64_t)) | |
159 | return -EINVAL; | |
160 | ||
161 | *os_indication = *(uint64_t *)v; | |
162 | return 0; | |
163 | } | |
164 | ||
165 | int efi_get_reboot_to_firmware(void) { | |
166 | int r; | |
167 | uint64_t b; | |
168 | ||
169 | r = get_os_indications(&b); | |
170 | if (r < 0) | |
171 | return r; | |
172 | ||
173 | return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI); | |
174 | } | |
175 | ||
176 | int efi_set_reboot_to_firmware(bool value) { | |
177 | int r; | |
178 | uint64_t b, b_new; | |
179 | ||
180 | r = get_os_indications(&b); | |
181 | if (r < 0) | |
182 | return r; | |
183 | ||
184 | if (value) | |
185 | b_new = b | EFI_OS_INDICATIONS_BOOT_TO_FW_UI; | |
186 | else | |
187 | b_new = b & ~EFI_OS_INDICATIONS_BOOT_TO_FW_UI; | |
188 | ||
189 | /* Avoid writing to efi vars store if we can due to firmware bugs. */ | |
190 | if (b != b_new) | |
191 | return efi_set_variable(EFI_VENDOR_GLOBAL, "OsIndications", &b_new, sizeof(uint64_t)); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
74b3e52b ZJS |
196 | char* efi_variable_path(sd_id128_t vendor, const char *name) { |
197 | char *p; | |
198 | ||
199 | if (asprintf(&p, | |
200 | "/sys/firmware/efi/efivars/%s-" SD_ID128_UUID_FORMAT_STR, | |
201 | name, SD_ID128_FORMAT_VAL(vendor)) < 0) | |
202 | return NULL; | |
203 | ||
204 | return p; | |
205 | } | |
206 | ||
9cde64ff LP |
207 | int efi_get_variable( |
208 | sd_id128_t vendor, | |
209 | const char *name, | |
210 | uint32_t *attribute, | |
211 | void **value, | |
212 | size_t *size) { | |
213 | ||
2e3d0692 LP |
214 | _cleanup_close_ int fd = -1; |
215 | _cleanup_free_ char *p = NULL; | |
216 | uint32_t a; | |
217 | ssize_t n; | |
218 | struct stat st; | |
ad7bcf52 | 219 | _cleanup_free_ void *buf = NULL; |
2e3d0692 LP |
220 | |
221 | assert(name); | |
222 | assert(value); | |
223 | assert(size); | |
224 | ||
74b3e52b ZJS |
225 | p = efi_variable_path(vendor, name); |
226 | if (!p) | |
2e3d0692 LP |
227 | return -ENOMEM; |
228 | ||
229 | fd = open(p, O_RDONLY|O_NOCTTY|O_CLOEXEC); | |
230 | if (fd < 0) | |
231 | return -errno; | |
232 | ||
233 | if (fstat(fd, &st) < 0) | |
234 | return -errno; | |
235 | if (st.st_size < 4) | |
1f18d942 | 236 | return -ENODATA; |
2e3d0692 LP |
237 | if (st.st_size > 4*1024*1024 + 4) |
238 | return -E2BIG; | |
239 | ||
240 | n = read(fd, &a, sizeof(a)); | |
241 | if (n < 0) | |
9cde64ff | 242 | return -errno; |
2e3d0692 LP |
243 | if (n != sizeof(a)) |
244 | return -EIO; | |
245 | ||
0797f232 ZJS |
246 | buf = malloc(st.st_size - 4 + 2); |
247 | if (!buf) | |
2e3d0692 LP |
248 | return -ENOMEM; |
249 | ||
0797f232 ZJS |
250 | n = read(fd, buf, (size_t) st.st_size - 4); |
251 | if (n < 0) | |
742af54a | 252 | return -errno; |
0797f232 | 253 | if (n != (ssize_t) st.st_size - 4) |
2e3d0692 | 254 | return -EIO; |
2e3d0692 LP |
255 | |
256 | /* Always NUL terminate (2 bytes, to protect UTF-16) */ | |
0797f232 ZJS |
257 | ((char*) buf)[st.st_size - 4] = 0; |
258 | ((char*) buf)[st.st_size - 4 + 1] = 0; | |
2e3d0692 | 259 | |
ae2a15bc | 260 | *value = TAKE_PTR(buf); |
ff47c895 | 261 | *size = (size_t) st.st_size - 4; |
2e3d0692 LP |
262 | |
263 | if (attribute) | |
264 | *attribute = a; | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
cea72d53 LP |
269 | int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { |
270 | _cleanup_free_ void *s = NULL; | |
271 | size_t ss = 0; | |
272 | int r; | |
273 | char *x; | |
274 | ||
275 | r = efi_get_variable(vendor, name, NULL, &s, &ss); | |
276 | if (r < 0) | |
277 | return r; | |
278 | ||
279 | x = utf16_to_utf8(s, ss); | |
280 | if (!x) | |
281 | return -ENOMEM; | |
282 | ||
283 | *p = x; | |
284 | return 0; | |
285 | } | |
286 | ||
0974a682 KS |
287 | int efi_set_variable( |
288 | sd_id128_t vendor, | |
289 | const char *name, | |
290 | const void *value, | |
291 | size_t size) { | |
292 | ||
293 | struct var { | |
294 | uint32_t attr; | |
295 | char buf[]; | |
b7749eb5 ZJS |
296 | } _packed_ * _cleanup_free_ buf = NULL; |
297 | _cleanup_free_ char *p = NULL; | |
298 | _cleanup_close_ int fd = -1; | |
2c3bf278 LP |
299 | bool saved_flags_valid = false; |
300 | unsigned saved_flags; | |
301 | int r; | |
0974a682 KS |
302 | |
303 | assert(name); | |
e1e26566 | 304 | assert(value || size == 0); |
0974a682 | 305 | |
74b3e52b ZJS |
306 | p = efi_variable_path(vendor, name); |
307 | if (!p) | |
0974a682 KS |
308 | return -ENOMEM; |
309 | ||
2c3bf278 LP |
310 | /* Newer efivarfs protects variables that are not in a whitelist with FS_IMMUTABLE_FL by default, to protect |
311 | * them for accidental removal and modification. We are not changing these variables accidentally however, | |
312 | * hence let's unset the bit first. */ | |
313 | ||
314 | r = chattr_path(p, 0, FS_IMMUTABLE_FL, &saved_flags); | |
315 | if (r < 0 && r != -ENOENT) | |
316 | log_debug_errno(r, "Failed to drop FS_IMMUTABLE_FL flag from '%s', ignoring: %m", p); | |
317 | ||
318 | saved_flags_valid = r >= 0; | |
319 | ||
0974a682 | 320 | if (size == 0) { |
2c3bf278 LP |
321 | if (unlink(p) < 0) { |
322 | r = -errno; | |
323 | goto finish; | |
324 | } | |
325 | ||
b7749eb5 | 326 | return 0; |
0974a682 KS |
327 | } |
328 | ||
329 | fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644); | |
2c3bf278 LP |
330 | if (fd < 0) { |
331 | r = -errno; | |
332 | goto finish; | |
333 | } | |
0974a682 KS |
334 | |
335 | buf = malloc(sizeof(uint32_t) + size); | |
2c3bf278 LP |
336 | if (!buf) { |
337 | r = -ENOMEM; | |
338 | goto finish; | |
339 | } | |
0974a682 KS |
340 | |
341 | buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS; | |
342 | memcpy(buf->buf, value, size); | |
343 | ||
2c3bf278 LP |
344 | r = loop_write(fd, buf, sizeof(uint32_t) + size, false); |
345 | if (r < 0) | |
346 | goto finish; | |
347 | ||
348 | r = 0; | |
349 | ||
350 | finish: | |
351 | if (saved_flags_valid) { | |
352 | int q; | |
353 | ||
354 | /* Restore the original flags field, just in case */ | |
355 | if (fd < 0) | |
356 | q = chattr_path(p, saved_flags, FS_IMMUTABLE_FL, NULL); | |
357 | else | |
358 | q = chattr_fd(fd, saved_flags, FS_IMMUTABLE_FL, NULL); | |
359 | if (q < 0) | |
360 | log_debug_errno(q, "Failed to restore FS_IMMUTABLE_FL on '%s', ignoring: %m", p); | |
361 | } | |
362 | ||
363 | return r; | |
0974a682 KS |
364 | } |
365 | ||
cea72d53 LP |
366 | int efi_set_variable_string(sd_id128_t vendor, const char *name, const char *v) { |
367 | _cleanup_free_ char16_t *u16 = NULL; | |
7b4d7cc0 | 368 | |
cea72d53 LP |
369 | u16 = utf8_to_utf16(v, strlen(v)); |
370 | if (!u16) | |
9cde64ff LP |
371 | return -ENOMEM; |
372 | ||
cea72d53 | 373 | return efi_set_variable(vendor, name, u16, (char16_strlen(u16) + 1) * sizeof(char16_t)); |
7b4d7cc0 KS |
374 | } |
375 | ||
f7cb1c79 | 376 | static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) { |
7b4d7cc0 KS |
377 | size_t l = 0; |
378 | ||
f7cb1c79 ZJS |
379 | /* Returns the size of the string in bytes without the terminating two zero bytes */ |
380 | ||
381 | if (buf_len_bytes % sizeof(uint16_t) != 0) | |
382 | return -EINVAL; | |
383 | ||
384 | while (l < buf_len_bytes / sizeof(uint16_t)) { | |
385 | if (s[l] == 0) | |
386 | return (l + 1) * sizeof(uint16_t); | |
7b4d7cc0 | 387 | l++; |
f7cb1c79 | 388 | } |
9cde64ff | 389 | |
f7cb1c79 | 390 | return -EINVAL; /* The terminator was not found */ |
7b4d7cc0 KS |
391 | } |
392 | ||
3c7dddac ZJS |
393 | struct guid { |
394 | uint32_t u1; | |
395 | uint16_t u2; | |
396 | uint16_t u3; | |
397 | uint8_t u4[8]; | |
398 | } _packed_; | |
399 | ||
7b4d7cc0 | 400 | static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) { |
3c7dddac ZJS |
401 | uint32_t u1; |
402 | uint16_t u2, u3; | |
403 | const struct guid *uuid = guid; | |
404 | ||
405 | memcpy(&u1, &uuid->u1, sizeof(uint32_t)); | |
406 | id128->bytes[0] = (u1 >> 24) & 0xff; | |
407 | id128->bytes[1] = (u1 >> 16) & 0xff; | |
408 | id128->bytes[2] = (u1 >> 8) & 0xff; | |
409 | id128->bytes[3] = u1 & 0xff; | |
410 | memcpy(&u2, &uuid->u2, sizeof(uint16_t)); | |
411 | id128->bytes[4] = (u2 >> 8) & 0xff; | |
412 | id128->bytes[5] = u2 & 0xff; | |
413 | memcpy(&u3, &uuid->u3, sizeof(uint16_t)); | |
414 | id128->bytes[6] = (u3 >> 8) & 0xff; | |
415 | id128->bytes[7] = u3 & 0xff; | |
7b4d7cc0 KS |
416 | memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4)); |
417 | } | |
418 | ||
9cde64ff LP |
419 | int efi_get_boot_option( |
420 | uint16_t id, | |
421 | char **title, | |
422 | sd_id128_t *part_uuid, | |
0974a682 KS |
423 | char **path, |
424 | bool *active) { | |
7b4d7cc0 | 425 | |
9cde64ff LP |
426 | char boot_id[9]; |
427 | _cleanup_free_ uint8_t *buf = NULL; | |
7b4d7cc0 KS |
428 | size_t l; |
429 | struct boot_option *header; | |
f7cb1c79 | 430 | ssize_t title_size; |
b7749eb5 | 431 | _cleanup_free_ char *s = NULL, *p = NULL; |
7b4d7cc0 | 432 | sd_id128_t p_uuid = SD_ID128_NULL; |
a8436474 | 433 | int r; |
7b4d7cc0 | 434 | |
337eed30 LP |
435 | if (!is_efi_boot()) |
436 | return -EOPNOTSUPP; | |
437 | ||
b7749eb5 | 438 | xsprintf(boot_id, "Boot%04X", id); |
a8436474 ZJS |
439 | r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l); |
440 | if (r < 0) | |
441 | return r; | |
f7cb1c79 | 442 | if (l < offsetof(struct boot_option, title)) |
7b4d7cc0 KS |
443 | return -ENOENT; |
444 | ||
445 | header = (struct boot_option *)buf; | |
f7cb1c79 ZJS |
446 | title_size = utf16_size(header->title, l - offsetof(struct boot_option, title)); |
447 | if (title_size < 0) | |
448 | return title_size; | |
7b4d7cc0 | 449 | |
5483a186 ZJS |
450 | if (title) { |
451 | s = utf16_to_utf8(header->title, title_size); | |
b7749eb5 ZJS |
452 | if (!s) |
453 | return -ENOMEM; | |
7b4d7cc0 KS |
454 | } |
455 | ||
456 | if (header->path_len > 0) { | |
9cde64ff | 457 | uint8_t *dbuf; |
e7e36b90 DT |
458 | size_t dnext, doff; |
459 | ||
460 | doff = offsetof(struct boot_option, title) + title_size; | |
461 | dbuf = buf + doff; | |
462 | if (header->path_len > l - doff) | |
463 | return -EINVAL; | |
7b4d7cc0 | 464 | |
7b4d7cc0 KS |
465 | dnext = 0; |
466 | while (dnext < header->path_len) { | |
467 | struct device_path *dpath; | |
468 | ||
469 | dpath = (struct device_path *)(dbuf + dnext); | |
470 | if (dpath->length < 4) | |
471 | break; | |
472 | ||
473 | /* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */ | |
0974a682 | 474 | if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE) |
7b4d7cc0 KS |
475 | break; |
476 | ||
477 | dnext += dpath->length; | |
478 | ||
479 | /* Type 0x04 – Media Device Path */ | |
0974a682 | 480 | if (dpath->type != MEDIA_DEVICE_PATH) |
7b4d7cc0 KS |
481 | continue; |
482 | ||
483 | /* Sub-Type 1 – Hard Drive */ | |
0974a682 | 484 | if (dpath->sub_type == MEDIA_HARDDRIVE_DP) { |
7b4d7cc0 | 485 | /* 0x02 – GUID Partition Table */ |
0974a682 | 486 | if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER) |
7b4d7cc0 KS |
487 | continue; |
488 | ||
489 | /* 0x02 – GUID signature */ | |
0974a682 | 490 | if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID) |
7b4d7cc0 KS |
491 | continue; |
492 | ||
5483a186 ZJS |
493 | if (part_uuid) |
494 | efi_guid_to_id128(dpath->drive.signature, &p_uuid); | |
7b4d7cc0 KS |
495 | continue; |
496 | } | |
497 | ||
498 | /* Sub-Type 4 – File Path */ | |
0974a682 | 499 | if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) { |
7b4d7cc0 | 500 | p = utf16_to_utf8(dpath->path, dpath->length-4); |
9db296fd LP |
501 | if (!p) |
502 | return -ENOMEM; | |
503 | ||
0974a682 | 504 | efi_tilt_backslashes(p); |
7b4d7cc0 KS |
505 | continue; |
506 | } | |
507 | } | |
508 | } | |
509 | ||
1cc6c93a YW |
510 | if (title) |
511 | *title = TAKE_PTR(s); | |
7b4d7cc0 KS |
512 | if (part_uuid) |
513 | *part_uuid = p_uuid; | |
1cc6c93a YW |
514 | if (path) |
515 | *path = TAKE_PTR(p); | |
0974a682 | 516 | if (active) |
0aa3b783 | 517 | *active = !!(header->attr & LOAD_OPTION_ACTIVE); |
9cde64ff | 518 | |
7b4d7cc0 | 519 | return 0; |
7b4d7cc0 KS |
520 | } |
521 | ||
0974a682 KS |
522 | static void to_utf16(uint16_t *dest, const char *src) { |
523 | int i; | |
524 | ||
525 | for (i = 0; src[i] != '\0'; i++) | |
526 | dest[i] = src[i]; | |
527 | dest[i] = '\0'; | |
528 | } | |
529 | ||
0974a682 | 530 | static void id128_to_efi_guid(sd_id128_t id, void *guid) { |
3c7dddac ZJS |
531 | struct guid uuid = { |
532 | .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3], | |
533 | .u2 = id.bytes[4] << 8 | id.bytes[5], | |
534 | .u3 = id.bytes[6] << 8 | id.bytes[7], | |
535 | }; | |
536 | memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4)); | |
537 | memcpy(guid, &uuid, sizeof(uuid)); | |
0974a682 KS |
538 | } |
539 | ||
540 | static uint16_t *tilt_slashes(uint16_t *s) { | |
541 | uint16_t *p; | |
542 | ||
543 | for (p = s; *p; p++) | |
544 | if (*p == '/') | |
545 | *p = '\\'; | |
546 | ||
547 | return s; | |
548 | } | |
549 | ||
337eed30 LP |
550 | int efi_add_boot_option( |
551 | uint16_t id, | |
552 | const char *title, | |
553 | uint32_t part, | |
554 | uint64_t pstart, | |
555 | uint64_t psize, | |
556 | sd_id128_t part_uuid, | |
557 | const char *path) { | |
558 | ||
559 | size_t size, title_len, path_len; | |
560 | _cleanup_free_ char *buf = NULL; | |
0974a682 KS |
561 | struct boot_option *option; |
562 | struct device_path *devicep; | |
337eed30 LP |
563 | char boot_id[9]; |
564 | ||
565 | if (!is_efi_boot()) | |
566 | return -EOPNOTSUPP; | |
0974a682 KS |
567 | |
568 | title_len = (strlen(title)+1) * 2; | |
569 | path_len = (strlen(path)+1) * 2; | |
570 | ||
f7cb1c79 | 571 | buf = malloc0(offsetof(struct boot_option, title) + title_len + |
e78c250b LP |
572 | sizeof(struct drive_path) + |
573 | sizeof(struct device_path) + path_len); | |
b7749eb5 ZJS |
574 | if (!buf) |
575 | return -ENOMEM; | |
0974a682 KS |
576 | |
577 | /* header */ | |
578 | option = (struct boot_option *)buf; | |
579 | option->attr = LOAD_OPTION_ACTIVE; | |
580 | option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) + | |
581 | offsetof(struct device_path, path) + path_len + | |
582 | offsetof(struct device_path, path); | |
583 | to_utf16(option->title, title); | |
584 | size = offsetof(struct boot_option, title) + title_len; | |
585 | ||
586 | /* partition info */ | |
587 | devicep = (struct device_path *)(buf + size); | |
588 | devicep->type = MEDIA_DEVICE_PATH; | |
589 | devicep->sub_type = MEDIA_HARDDRIVE_DP; | |
590 | devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path); | |
3c7dddac ZJS |
591 | memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t)); |
592 | memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t)); | |
593 | memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t)); | |
0974a682 | 594 | id128_to_efi_guid(part_uuid, devicep->drive.signature); |
3c7dddac ZJS |
595 | devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER; |
596 | devicep->drive.signature_type = SIGNATURE_TYPE_GUID; | |
0974a682 KS |
597 | size += devicep->length; |
598 | ||
599 | /* path to loader */ | |
600 | devicep = (struct device_path *)(buf + size); | |
601 | devicep->type = MEDIA_DEVICE_PATH; | |
602 | devicep->sub_type = MEDIA_FILEPATH_DP; | |
603 | devicep->length = offsetof(struct device_path, path) + path_len; | |
604 | to_utf16(devicep->path, path); | |
605 | tilt_slashes(devicep->path); | |
606 | size += devicep->length; | |
607 | ||
608 | /* end of path */ | |
609 | devicep = (struct device_path *)(buf + size); | |
610 | devicep->type = END_DEVICE_PATH_TYPE; | |
611 | devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE; | |
612 | devicep->length = offsetof(struct device_path, path); | |
613 | size += devicep->length; | |
614 | ||
b7749eb5 ZJS |
615 | xsprintf(boot_id, "Boot%04X", id); |
616 | return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size); | |
0974a682 KS |
617 | } |
618 | ||
619 | int efi_remove_boot_option(uint16_t id) { | |
620 | char boot_id[9]; | |
621 | ||
337eed30 LP |
622 | if (!is_efi_boot()) |
623 | return -EOPNOTSUPP; | |
624 | ||
b7749eb5 | 625 | xsprintf(boot_id, "Boot%04X", id); |
0974a682 KS |
626 | return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0); |
627 | } | |
628 | ||
9cde64ff | 629 | int efi_get_boot_order(uint16_t **order) { |
0797f232 | 630 | _cleanup_free_ void *buf = NULL; |
7b4d7cc0 | 631 | size_t l; |
9cde64ff | 632 | int r; |
7b4d7cc0 | 633 | |
337eed30 LP |
634 | if (!is_efi_boot()) |
635 | return -EOPNOTSUPP; | |
636 | ||
9cde64ff LP |
637 | r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l); |
638 | if (r < 0) | |
639 | return r; | |
7b4d7cc0 | 640 | |
0797f232 | 641 | if (l <= 0) |
7b4d7cc0 | 642 | return -ENOENT; |
7b4d7cc0 | 643 | |
0797f232 ZJS |
644 | if (l % sizeof(uint16_t) > 0 || |
645 | l / sizeof(uint16_t) > INT_MAX) | |
7b4d7cc0 | 646 | return -EINVAL; |
7b4d7cc0 | 647 | |
ae2a15bc | 648 | *order = TAKE_PTR(buf); |
9cde64ff LP |
649 | return (int) (l / sizeof(uint16_t)); |
650 | } | |
651 | ||
0974a682 | 652 | int efi_set_boot_order(uint16_t *order, size_t n) { |
337eed30 LP |
653 | |
654 | if (!is_efi_boot()) | |
655 | return -EOPNOTSUPP; | |
656 | ||
0974a682 KS |
657 | return efi_set_variable(EFI_VENDOR_GLOBAL, "BootOrder", order, n * sizeof(uint16_t)); |
658 | } | |
659 | ||
3042bbeb | 660 | static int boot_id_hex(const char s[static 4]) { |
e78c250b | 661 | int id = 0, i; |
4d34c495 KS |
662 | |
663 | for (i = 0; i < 4; i++) | |
664 | if (s[i] >= '0' && s[i] <= '9') | |
665 | id |= (s[i] - '0') << (3 - i) * 4; | |
666 | else if (s[i] >= 'A' && s[i] <= 'F') | |
667 | id |= (s[i] - 'A' + 10) << (3 - i) * 4; | |
668 | else | |
7e8185ef | 669 | return -EINVAL; |
4d34c495 KS |
670 | |
671 | return id; | |
672 | } | |
673 | ||
93bab288 YW |
674 | static int cmp_uint16(const uint16_t *a, const uint16_t *b) { |
675 | return CMP(*a, *b); | |
9db11a99 LP |
676 | } |
677 | ||
9cde64ff LP |
678 | int efi_get_boot_options(uint16_t **options) { |
679 | _cleanup_closedir_ DIR *dir = NULL; | |
b7749eb5 | 680 | _cleanup_free_ uint16_t *list = NULL; |
e78c250b | 681 | struct dirent *de; |
7432b24b | 682 | size_t alloc = 0; |
b7749eb5 | 683 | int count = 0; |
9cde64ff LP |
684 | |
685 | assert(options); | |
686 | ||
337eed30 LP |
687 | if (!is_efi_boot()) |
688 | return -EOPNOTSUPP; | |
689 | ||
9cde64ff LP |
690 | dir = opendir("/sys/firmware/efi/efivars/"); |
691 | if (!dir) | |
692 | return -errno; | |
693 | ||
b7749eb5 | 694 | FOREACH_DIRENT(de, dir, return -errno) { |
4d34c495 | 695 | int id; |
9cde64ff LP |
696 | |
697 | if (strncmp(de->d_name, "Boot", 4) != 0) | |
698 | continue; | |
699 | ||
4d34c495 | 700 | if (strlen(de->d_name) != 45) |
9cde64ff LP |
701 | continue; |
702 | ||
703 | if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0) | |
704 | continue; | |
705 | ||
4d34c495 KS |
706 | id = boot_id_hex(de->d_name + 4); |
707 | if (id < 0) | |
9cde64ff LP |
708 | continue; |
709 | ||
7432b24b | 710 | if (!GREEDY_REALLOC(list, alloc, count + 1)) |
b7749eb5 | 711 | return -ENOMEM; |
9cde64ff | 712 | |
7432b24b | 713 | list[count++] = id; |
9cde64ff LP |
714 | } |
715 | ||
93bab288 | 716 | typesafe_qsort(list, count, cmp_uint16); |
9db11a99 | 717 | |
1cc6c93a YW |
718 | *options = TAKE_PTR(list); |
719 | ||
9cde64ff | 720 | return count; |
7b4d7cc0 KS |
721 | } |
722 | ||
5dbe9f53 | 723 | static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) { |
2e3d0692 | 724 | _cleanup_free_ char *j = NULL; |
2e3d0692 | 725 | int r; |
39883f62 | 726 | uint64_t x = 0; |
2e3d0692 LP |
727 | |
728 | assert(name); | |
729 | assert(u); | |
730 | ||
61cc634b | 731 | r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j); |
2e3d0692 LP |
732 | if (r < 0) |
733 | return r; | |
734 | ||
2e3d0692 LP |
735 | r = safe_atou64(j, &x); |
736 | if (r < 0) | |
737 | return r; | |
738 | ||
5dbe9f53 | 739 | *u = x; |
2e3d0692 LP |
740 | return 0; |
741 | } | |
742 | ||
c51d84dc | 743 | int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) { |
2e3d0692 LP |
744 | uint64_t x, y; |
745 | int r; | |
2e3d0692 LP |
746 | |
747 | assert(firmware); | |
748 | assert(loader); | |
749 | ||
337eed30 LP |
750 | if (!is_efi_boot()) |
751 | return -EOPNOTSUPP; | |
752 | ||
e9cea16d | 753 | r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x); |
2e3d0692 LP |
754 | if (r < 0) |
755 | return r; | |
756 | ||
e9cea16d | 757 | r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y); |
2e3d0692 LP |
758 | if (r < 0) |
759 | return r; | |
760 | ||
761 | if (y == 0 || y < x) | |
762 | return -EIO; | |
763 | ||
764 | if (y > USEC_PER_HOUR) | |
765 | return -EIO; | |
766 | ||
767 | *firmware = x; | |
768 | *loader = y; | |
769 | ||
770 | return 0; | |
771 | } | |
772 | ||
c51d84dc | 773 | int efi_loader_get_device_part_uuid(sd_id128_t *u) { |
f4ce2b3e | 774 | _cleanup_free_ char *p = NULL; |
f4ce2b3e | 775 | int r, parsed[16]; |
f4ce2b3e | 776 | |
337eed30 LP |
777 | if (!is_efi_boot()) |
778 | return -EOPNOTSUPP; | |
779 | ||
61cc634b | 780 | r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p); |
f4ce2b3e LP |
781 | if (r < 0) |
782 | return r; | |
783 | ||
bd44566c | 784 | if (sscanf(p, SD_ID128_UUID_FORMAT_STR, |
f4ce2b3e LP |
785 | &parsed[0], &parsed[1], &parsed[2], &parsed[3], |
786 | &parsed[4], &parsed[5], &parsed[6], &parsed[7], | |
787 | &parsed[8], &parsed[9], &parsed[10], &parsed[11], | |
788 | &parsed[12], &parsed[13], &parsed[14], &parsed[15]) != 16) | |
789 | return -EIO; | |
790 | ||
73b80ec2 LP |
791 | if (u) { |
792 | unsigned i; | |
793 | ||
794 | for (i = 0; i < ELEMENTSOF(parsed); i++) | |
795 | u->bytes[i] = parsed[i]; | |
796 | } | |
f4ce2b3e LP |
797 | |
798 | return 0; | |
799 | } | |
b872e9a0 | 800 | |
bd2865ca LP |
801 | int efi_loader_get_entries(char ***ret) { |
802 | _cleanup_free_ char16_t *entries = NULL; | |
803 | _cleanup_strv_free_ char **l = NULL; | |
804 | size_t size, i, start; | |
805 | int r; | |
806 | ||
807 | assert(ret); | |
808 | ||
809 | if (!is_efi_boot()) | |
810 | return -EOPNOTSUPP; | |
811 | ||
812 | r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderEntries", NULL, (void**) &entries, &size); | |
813 | if (r < 0) | |
814 | return r; | |
815 | ||
816 | /* The variable contains a series of individually NUL terminated UTF-16 strings. */ | |
817 | ||
818 | for (i = 0, start = 0;; i++) { | |
68d7c268 | 819 | _cleanup_free_ char *decoded = NULL; |
bd2865ca LP |
820 | bool end; |
821 | ||
822 | /* Is this the end of the variable's data? */ | |
823 | end = i * sizeof(char16_t) >= size; | |
824 | ||
825 | /* Are we in the middle of a string? (i.e. not at the end of the variable, nor at a NUL terminator?) If | |
826 | * so, let's go to the next entry. */ | |
827 | if (!end && entries[i] != 0) | |
828 | continue; | |
829 | ||
830 | /* We reached the end of a string, let's decode it into UTF-8 */ | |
831 | decoded = utf16_to_utf8(entries + start, (i - start) * sizeof(char16_t)); | |
832 | if (!decoded) | |
833 | return -ENOMEM; | |
834 | ||
68d7c268 LP |
835 | if (efi_loader_entry_name_valid(decoded)) { |
836 | r = strv_consume(&l, TAKE_PTR(decoded)); | |
837 | if (r < 0) | |
838 | return r; | |
839 | } else | |
840 | log_debug("Ignoring invalid loader entry '%s'.", decoded); | |
bd2865ca LP |
841 | |
842 | /* We reached the end of the variable */ | |
843 | if (end) | |
844 | break; | |
845 | ||
846 | /* Continue after the NUL byte */ | |
847 | start = i + 1; | |
848 | } | |
849 | ||
850 | *ret = TAKE_PTR(l); | |
851 | return 0; | |
852 | } | |
853 | ||
80641a81 LP |
854 | int efi_loader_get_features(uint64_t *ret) { |
855 | _cleanup_free_ void *v = NULL; | |
856 | size_t s; | |
857 | int r; | |
858 | ||
859 | if (!is_efi_boot()) { | |
860 | *ret = 0; | |
861 | return 0; | |
862 | } | |
863 | ||
864 | r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderFeatures", NULL, &v, &s); | |
865 | if (r == -ENOENT) { | |
866 | _cleanup_free_ char *info = NULL; | |
867 | ||
868 | /* The new (v240+) LoaderFeatures variable is not supported, let's see if it's systemd-boot at all */ | |
869 | r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info); | |
870 | if (r < 0) { | |
871 | if (r != -ENOENT) | |
872 | return r; | |
873 | ||
874 | /* Variable not set, definitely means not systemd-boot */ | |
875 | ||
876 | } else if (first_word(info, "systemd-boot")) { | |
877 | ||
878 | /* An older systemd-boot version. Let's hardcode the feature set, since it was pretty | |
879 | * static in all its versions. */ | |
880 | ||
881 | *ret = EFI_LOADER_FEATURE_CONFIG_TIMEOUT | | |
882 | EFI_LOADER_FEATURE_ENTRY_DEFAULT | | |
883 | EFI_LOADER_FEATURE_ENTRY_ONESHOT; | |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
888 | /* No features supported */ | |
889 | *ret = 0; | |
890 | return 0; | |
891 | } | |
892 | if (r < 0) | |
893 | return r; | |
894 | ||
baaa35ad ZJS |
895 | if (s != sizeof(uint64_t)) |
896 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
897 | "LoaderFeatures EFI variable doesn't have the right size."); | |
80641a81 LP |
898 | |
899 | memcpy(ret, v, sizeof(uint64_t)); | |
900 | return 0; | |
901 | } | |
6917857e | 902 | |
903 | #endif | |
904 | ||
995cbd72 LP |
905 | bool efi_loader_entry_name_valid(const char *s) { |
906 | if (isempty(s)) | |
907 | return false; | |
908 | ||
909 | if (strlen(s) > FILENAME_MAX) /* Make sure entry names fit in filenames */ | |
910 | return false; | |
911 | ||
912 | return in_charset(s, ALPHANUMERICAL "-_."); | |
913 | } | |
914 | ||
6917857e | 915 | char *efi_tilt_backslashes(char *s) { |
916 | char *p; | |
917 | ||
918 | for (p = s; *p; p++) | |
919 | if (*p == '\\') | |
920 | *p = '/'; | |
921 | ||
922 | return s; | |
923 | } |