]>
Commit | Line | Data |
---|---|---|
2e3d0692 LP |
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 | ||
a8fbdf54 TA |
20 | #include <dirent.h> |
21 | #include <errno.h> | |
2e3d0692 | 22 | #include <fcntl.h> |
a8fbdf54 TA |
23 | #include <limits.h> |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
cf0fbc49 | 26 | #include <string.h> |
a8fbdf54 | 27 | #include <sys/stat.h> |
cf0fbc49 | 28 | #include <unistd.h> |
2e3d0692 | 29 | |
a8fbdf54 TA |
30 | #include "sd-id128.h" |
31 | ||
b5efdb8a | 32 | #include "alloc-util.h" |
a0956174 | 33 | #include "dirent-util.h" |
3ffd4af2 LP |
34 | #include "efivars.h" |
35 | #include "fd-util.h" | |
c004493c | 36 | #include "io-util.h" |
a8fbdf54 | 37 | #include "macro.h" |
6bedfcbb | 38 | #include "parse-util.h" |
15a5e950 | 39 | #include "stdio-util.h" |
a8fbdf54 | 40 | #include "time-util.h" |
2e3d0692 | 41 | #include "utf8.h" |
3ffd4af2 | 42 | #include "util.h" |
5bdf2243 | 43 | #include "virt.h" |
2e3d0692 | 44 | |
349cc4a5 | 45 | #if ENABLE_EFI |
b872e9a0 | 46 | |
0974a682 KS |
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 | |
5bdf2243 | 55 | #define EFI_OS_INDICATIONS_BOOT_TO_FW_UI 0x0000000000000001 |
0974a682 KS |
56 | |
57 | struct boot_option { | |
58 | uint32_t attr; | |
59 | uint16_t path_len; | |
60 | uint16_t title[]; | |
885fdebc | 61 | } _packed_; |
0974a682 KS |
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; | |
885fdebc | 70 | } _packed_; |
0974a682 KS |
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 | }; | |
885fdebc | 80 | } _packed_; |
0974a682 | 81 | |
9cde64ff | 82 | bool is_efi_boot(void) { |
34e5a31e LP |
83 | return access("/sys/firmware/efi", F_OK) >= 0; |
84 | } | |
85 | ||
bc6f2e7c KS |
86 | static int read_flag(const char *varname) { |
87 | int r; | |
b47d419c | 88 | _cleanup_free_ void *v = NULL; |
bc6f2e7c KS |
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 | ||
b47d419c ZJS |
96 | if (s != 1) |
97 | return -EINVAL; | |
bc6f2e7c KS |
98 | |
99 | b = *(uint8_t *)v; | |
100 | r = b > 0; | |
bc6f2e7c KS |
101 | return r; |
102 | } | |
103 | ||
9df49b33 TG |
104 | bool is_efi_secure_boot(void) { |
105 | return read_flag("SecureBoot") > 0; | |
bc6f2e7c KS |
106 | } |
107 | ||
9df49b33 TG |
108 | bool is_efi_secure_boot_setup_mode(void) { |
109 | return read_flag("SetupMode") > 0; | |
bc6f2e7c KS |
110 | } |
111 | ||
5bdf2243 JJ |
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 | ||
75f86906 | 118 | if (!is_efi_boot() || detect_container() > 0) |
5bdf2243 JJ |
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); | |
6b62bbbc LP |
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) | |
5bdf2243 JJ |
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 | ||
9cde64ff LP |
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 | ||
2e3d0692 LP |
201 | _cleanup_close_ int fd = -1; |
202 | _cleanup_free_ char *p = NULL; | |
203 | uint32_t a; | |
204 | ssize_t n; | |
205 | struct stat st; | |
ad7bcf52 | 206 | _cleanup_free_ void *buf = NULL; |
2e3d0692 LP |
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) | |
9cde64ff | 230 | return -errno; |
2e3d0692 LP |
231 | if (n != sizeof(a)) |
232 | return -EIO; | |
233 | ||
0797f232 ZJS |
234 | buf = malloc(st.st_size - 4 + 2); |
235 | if (!buf) | |
2e3d0692 LP |
236 | return -ENOMEM; |
237 | ||
0797f232 ZJS |
238 | n = read(fd, buf, (size_t) st.st_size - 4); |
239 | if (n < 0) | |
742af54a | 240 | return -errno; |
0797f232 | 241 | if (n != (ssize_t) st.st_size - 4) |
2e3d0692 | 242 | return -EIO; |
2e3d0692 LP |
243 | |
244 | /* Always NUL terminate (2 bytes, to protect UTF-16) */ | |
0797f232 ZJS |
245 | ((char*) buf)[st.st_size - 4] = 0; |
246 | ((char*) buf)[st.st_size - 4 + 1] = 0; | |
2e3d0692 | 247 | |
0797f232 ZJS |
248 | *value = buf; |
249 | buf = NULL; | |
ff47c895 | 250 | *size = (size_t) st.st_size - 4; |
2e3d0692 LP |
251 | |
252 | if (attribute) | |
253 | *attribute = a; | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
0974a682 KS |
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[]; | |
b7749eb5 ZJS |
267 | } _packed_ * _cleanup_free_ buf = NULL; |
268 | _cleanup_free_ char *p = NULL; | |
269 | _cleanup_close_ int fd = -1; | |
0974a682 KS |
270 | |
271 | assert(name); | |
e1e26566 | 272 | assert(value || size == 0); |
0974a682 KS |
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) { | |
b7749eb5 ZJS |
280 | if (unlink(p) < 0) |
281 | return -errno; | |
282 | return 0; | |
0974a682 KS |
283 | } |
284 | ||
285 | fd = open(p, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0644); | |
b7749eb5 ZJS |
286 | if (fd < 0) |
287 | return -errno; | |
0974a682 KS |
288 | |
289 | buf = malloc(sizeof(uint32_t) + size); | |
b7749eb5 ZJS |
290 | if (!buf) |
291 | return -ENOMEM; | |
0974a682 KS |
292 | |
293 | buf->attr = EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS; | |
294 | memcpy(buf->buf, value, size); | |
295 | ||
b7749eb5 | 296 | return loop_write(fd, buf, sizeof(uint32_t) + size, false); |
0974a682 KS |
297 | } |
298 | ||
9cde64ff | 299 | int efi_get_variable_string(sd_id128_t vendor, const char *name, char **p) { |
7b4d7cc0 | 300 | _cleanup_free_ void *s = NULL; |
39883f62 | 301 | size_t ss = 0; |
9cde64ff LP |
302 | int r; |
303 | char *x; | |
7b4d7cc0 | 304 | |
9cde64ff LP |
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; | |
7b4d7cc0 KS |
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++; | |
9cde64ff | 322 | |
7b4d7cc0 KS |
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 | ||
9cde64ff LP |
346 | int efi_get_boot_option( |
347 | uint16_t id, | |
348 | char **title, | |
349 | sd_id128_t *part_uuid, | |
0974a682 KS |
350 | char **path, |
351 | bool *active) { | |
7b4d7cc0 | 352 | |
9cde64ff LP |
353 | char boot_id[9]; |
354 | _cleanup_free_ uint8_t *buf = NULL; | |
7b4d7cc0 KS |
355 | size_t l; |
356 | struct boot_option *header; | |
357 | size_t title_size; | |
b7749eb5 | 358 | _cleanup_free_ char *s = NULL, *p = NULL; |
7b4d7cc0 | 359 | sd_id128_t p_uuid = SD_ID128_NULL; |
a8436474 | 360 | int r; |
7b4d7cc0 | 361 | |
b7749eb5 | 362 | xsprintf(boot_id, "Boot%04X", id); |
a8436474 ZJS |
363 | r = efi_get_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, (void **)&buf, &l); |
364 | if (r < 0) | |
365 | return r; | |
7b4d7cc0 KS |
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 | ||
5483a186 ZJS |
374 | if (title) { |
375 | s = utf16_to_utf8(header->title, title_size); | |
b7749eb5 ZJS |
376 | if (!s) |
377 | return -ENOMEM; | |
7b4d7cc0 KS |
378 | } |
379 | ||
380 | if (header->path_len > 0) { | |
9cde64ff | 381 | uint8_t *dbuf; |
7b4d7cc0 KS |
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 */ | |
0974a682 | 394 | if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE) |
7b4d7cc0 KS |
395 | break; |
396 | ||
397 | dnext += dpath->length; | |
398 | ||
399 | /* Type 0x04 – Media Device Path */ | |
0974a682 | 400 | if (dpath->type != MEDIA_DEVICE_PATH) |
7b4d7cc0 KS |
401 | continue; |
402 | ||
403 | /* Sub-Type 1 – Hard Drive */ | |
0974a682 | 404 | if (dpath->sub_type == MEDIA_HARDDRIVE_DP) { |
7b4d7cc0 | 405 | /* 0x02 – GUID Partition Table */ |
0974a682 | 406 | if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER) |
7b4d7cc0 KS |
407 | continue; |
408 | ||
409 | /* 0x02 – GUID signature */ | |
0974a682 | 410 | if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID) |
7b4d7cc0 KS |
411 | continue; |
412 | ||
5483a186 ZJS |
413 | if (part_uuid) |
414 | efi_guid_to_id128(dpath->drive.signature, &p_uuid); | |
7b4d7cc0 KS |
415 | continue; |
416 | } | |
417 | ||
418 | /* Sub-Type 4 – File Path */ | |
0974a682 | 419 | if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && path) { |
7b4d7cc0 | 420 | p = utf16_to_utf8(dpath->path, dpath->length-4); |
0974a682 | 421 | efi_tilt_backslashes(p); |
7b4d7cc0 KS |
422 | continue; |
423 | } | |
424 | } | |
425 | } | |
426 | ||
b7749eb5 | 427 | if (title) { |
9cde64ff | 428 | *title = s; |
b7749eb5 ZJS |
429 | s = NULL; |
430 | } | |
7b4d7cc0 KS |
431 | if (part_uuid) |
432 | *part_uuid = p_uuid; | |
b7749eb5 | 433 | if (path) { |
7b4d7cc0 | 434 | *path = p; |
b7749eb5 ZJS |
435 | p = NULL; |
436 | } | |
0974a682 | 437 | if (active) |
0aa3b783 | 438 | *active = !!(header->attr & LOAD_OPTION_ACTIVE); |
9cde64ff | 439 | |
7b4d7cc0 | 440 | return 0; |
7b4d7cc0 KS |
441 | } |
442 | ||
0974a682 KS |
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]; | |
885fdebc | 456 | } _packed_; |
0974a682 KS |
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 | ||
0974a682 KS |
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]; | |
0974a682 KS |
481 | size_t size; |
482 | size_t title_len; | |
483 | size_t path_len; | |
484 | struct boot_option *option; | |
485 | struct device_path *devicep; | |
b7749eb5 | 486 | _cleanup_free_ char *buf = NULL; |
0974a682 KS |
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); | |
b7749eb5 ZJS |
494 | if (!buf) |
495 | return -ENOMEM; | |
0974a682 KS |
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; | |
920b52e4 | 513 | devicep->drive.part_size = psize; |
0974a682 KS |
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 | ||
b7749eb5 ZJS |
535 | xsprintf(boot_id, "Boot%04X", id); |
536 | return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, buf, size); | |
0974a682 KS |
537 | } |
538 | ||
539 | int efi_remove_boot_option(uint16_t id) { | |
540 | char boot_id[9]; | |
541 | ||
b7749eb5 | 542 | xsprintf(boot_id, "Boot%04X", id); |
0974a682 KS |
543 | return efi_set_variable(EFI_VENDOR_GLOBAL, boot_id, NULL, 0); |
544 | } | |
545 | ||
9cde64ff | 546 | int efi_get_boot_order(uint16_t **order) { |
0797f232 | 547 | _cleanup_free_ void *buf = NULL; |
7b4d7cc0 | 548 | size_t l; |
9cde64ff | 549 | int r; |
7b4d7cc0 | 550 | |
9cde64ff LP |
551 | r = efi_get_variable(EFI_VENDOR_GLOBAL, "BootOrder", NULL, &buf, &l); |
552 | if (r < 0) | |
553 | return r; | |
7b4d7cc0 | 554 | |
0797f232 | 555 | if (l <= 0) |
7b4d7cc0 | 556 | return -ENOENT; |
7b4d7cc0 | 557 | |
0797f232 ZJS |
558 | if (l % sizeof(uint16_t) > 0 || |
559 | l / sizeof(uint16_t) > INT_MAX) | |
7b4d7cc0 | 560 | return -EINVAL; |
7b4d7cc0 KS |
561 | |
562 | *order = buf; | |
0797f232 | 563 | buf = NULL; |
9cde64ff LP |
564 | return (int) (l / sizeof(uint16_t)); |
565 | } | |
566 | ||
0974a682 KS |
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 | ||
4d34c495 KS |
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 | |
7e8185ef | 581 | return -EINVAL; |
4d34c495 KS |
582 | |
583 | return id; | |
584 | } | |
585 | ||
9db11a99 LP |
586 | static int cmp_uint16(const void *_a, const void *_b) { |
587 | const uint16_t *a = _a, *b = _b; | |
588 | ||
02a6fc3e | 589 | return (int)*a - (int)*b; |
9db11a99 LP |
590 | } |
591 | ||
9cde64ff LP |
592 | int efi_get_boot_options(uint16_t **options) { |
593 | _cleanup_closedir_ DIR *dir = NULL; | |
594 | struct dirent *de; | |
b7749eb5 | 595 | _cleanup_free_ uint16_t *list = NULL; |
7432b24b | 596 | size_t alloc = 0; |
b7749eb5 | 597 | int count = 0; |
9cde64ff LP |
598 | |
599 | assert(options); | |
600 | ||
601 | dir = opendir("/sys/firmware/efi/efivars/"); | |
602 | if (!dir) | |
603 | return -errno; | |
604 | ||
b7749eb5 | 605 | FOREACH_DIRENT(de, dir, return -errno) { |
4d34c495 | 606 | int id; |
9cde64ff LP |
607 | |
608 | if (strncmp(de->d_name, "Boot", 4) != 0) | |
609 | continue; | |
610 | ||
4d34c495 | 611 | if (strlen(de->d_name) != 45) |
9cde64ff LP |
612 | continue; |
613 | ||
614 | if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0) | |
615 | continue; | |
616 | ||
4d34c495 KS |
617 | id = boot_id_hex(de->d_name + 4); |
618 | if (id < 0) | |
9cde64ff LP |
619 | continue; |
620 | ||
7432b24b | 621 | if (!GREEDY_REALLOC(list, alloc, count + 1)) |
b7749eb5 | 622 | return -ENOMEM; |
9cde64ff | 623 | |
7432b24b | 624 | list[count++] = id; |
9cde64ff LP |
625 | } |
626 | ||
7ff7394d | 627 | qsort_safe(list, count, sizeof(uint16_t), cmp_uint16); |
9db11a99 | 628 | |
9cde64ff | 629 | *options = list; |
b7749eb5 | 630 | list = NULL; |
9cde64ff | 631 | return count; |
7b4d7cc0 KS |
632 | } |
633 | ||
5dbe9f53 | 634 | static int read_usec(sd_id128_t vendor, const char *name, usec_t *u) { |
2e3d0692 | 635 | _cleanup_free_ char *j = NULL; |
2e3d0692 | 636 | int r; |
39883f62 | 637 | uint64_t x = 0; |
2e3d0692 LP |
638 | |
639 | assert(name); | |
640 | assert(u); | |
641 | ||
61cc634b | 642 | r = efi_get_variable_string(EFI_VENDOR_LOADER, name, &j); |
2e3d0692 LP |
643 | if (r < 0) |
644 | return r; | |
645 | ||
2e3d0692 LP |
646 | r = safe_atou64(j, &x); |
647 | if (r < 0) | |
648 | return r; | |
649 | ||
5dbe9f53 | 650 | *u = x; |
2e3d0692 LP |
651 | return 0; |
652 | } | |
653 | ||
c51d84dc | 654 | int efi_loader_get_boot_usec(usec_t *firmware, usec_t *loader) { |
2e3d0692 LP |
655 | uint64_t x, y; |
656 | int r; | |
2e3d0692 LP |
657 | |
658 | assert(firmware); | |
659 | assert(loader); | |
660 | ||
e9cea16d | 661 | r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeInitUSec", &x); |
2e3d0692 LP |
662 | if (r < 0) |
663 | return r; | |
664 | ||
e9cea16d | 665 | r = read_usec(EFI_VENDOR_LOADER, "LoaderTimeExecUSec", &y); |
2e3d0692 LP |
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 | ||
c51d84dc | 681 | int efi_loader_get_device_part_uuid(sd_id128_t *u) { |
f4ce2b3e | 682 | _cleanup_free_ char *p = NULL; |
f4ce2b3e | 683 | int r, parsed[16]; |
f4ce2b3e | 684 | |
61cc634b | 685 | r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderDevicePartUUID", &p); |
f4ce2b3e LP |
686 | if (r < 0) |
687 | return r; | |
688 | ||
f4ce2b3e LP |
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 | ||
73b80ec2 LP |
696 | if (u) { |
697 | unsigned i; | |
698 | ||
699 | for (i = 0; i < ELEMENTSOF(parsed); i++) | |
700 | u->bytes[i] = parsed[i]; | |
701 | } | |
f4ce2b3e LP |
702 | |
703 | return 0; | |
704 | } | |
b872e9a0 LP |
705 | |
706 | #endif | |
b28ce7c6 TG |
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 | } |