]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7b4d7cc0 | 2 | |
0974a682 | 3 | #include <ctype.h> |
3f6fd1ba | 4 | #include <errno.h> |
0974a682 | 5 | #include <ftw.h> |
3f6fd1ba LP |
6 | #include <getopt.h> |
7 | #include <limits.h> | |
5fa6c13c | 8 | #include <linux/magic.h> |
0974a682 | 9 | #include <stdbool.h> |
3f6fd1ba | 10 | #include <stdlib.h> |
3f6fd1ba | 11 | #include <sys/mman.h> |
3f6fd1ba | 12 | #include <unistd.h> |
7b4d7cc0 | 13 | |
dccca82b LP |
14 | #include "sd-id128.h" |
15 | ||
b5efdb8a | 16 | #include "alloc-util.h" |
3f6fd1ba | 17 | #include "blkid-util.h" |
7e87c7d9 | 18 | #include "bootspec.h" |
175d308c | 19 | #include "copy.h" |
e41256dc | 20 | #include "dirent-util.h" |
0bb2f0f1 | 21 | #include "efi-loader.h" |
0974a682 | 22 | #include "efivars.h" |
e44c3229 | 23 | #include "env-util.h" |
9dae4c8a | 24 | #include "escape.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
0d39fa9c | 26 | #include "fileio.h" |
175d308c | 27 | #include "fs-util.h" |
8752c575 | 28 | #include "locale-util.h" |
608f8ec9 | 29 | #include "main-func.h" |
a4a55e9a | 30 | #include "mkdir.h" |
57db6f18 | 31 | #include "pager.h" |
2f2c539c | 32 | #include "parse-util.h" |
294bf0c3 | 33 | #include "pretty-print.h" |
e44c3229 | 34 | #include "random-util.h" |
c6878637 | 35 | #include "rm-rf.h" |
175d308c | 36 | #include "stat-util.h" |
341890de | 37 | #include "stdio-util.h" |
07630cea | 38 | #include "string-util.h" |
2f2c539c | 39 | #include "strv.h" |
7e87c7d9 | 40 | #include "terminal-util.h" |
e4de7287 | 41 | #include "tmpfile-util.h" |
2f2c539c | 42 | #include "umask-util.h" |
d88c96ff | 43 | #include "utf8.h" |
3f6fd1ba | 44 | #include "util.h" |
2f2c539c LP |
45 | #include "verbs.h" |
46 | #include "virt.h" | |
7b4d7cc0 | 47 | |
fbf45d22 LP |
48 | static char *arg_esp_path = NULL; |
49 | static char *arg_xbootldr_path = NULL; | |
50 | static bool arg_print_esp_path = false; | |
51 | static bool arg_print_dollar_boot_path = false; | |
25579a43 | 52 | static bool arg_touch_variables = true; |
0221d68a | 53 | static PagerFlags arg_pager_flags = 0; |
25579a43 | 54 | |
fbf45d22 LP |
55 | STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep); |
56 | STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep); | |
57 | ||
58 | static const char *arg_dollar_boot_path(void) { | |
59 | /* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */ | |
60 | return arg_xbootldr_path ?: arg_esp_path; | |
61 | } | |
608f8ec9 | 62 | |
5caa3167 LP |
63 | static int acquire_esp( |
64 | bool unprivileged_mode, | |
65 | uint32_t *ret_part, | |
66 | uint64_t *ret_pstart, | |
67 | uint64_t *ret_psize, | |
68 | sd_id128_t *ret_uuid) { | |
69 | ||
70 | char *np; | |
2f2c539c LP |
71 | int r; |
72 | ||
fbf45d22 LP |
73 | /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on |
74 | * its own, except for ENOKEY (which is good, we want to show our own message in that case, | |
75 | * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case | |
76 | * we simply eat up the error here, so that --list and --status work too, without noise about | |
77 | * this). */ | |
5caa3167 | 78 | |
fbf45d22 | 79 | r = find_esp_and_warn(arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid); |
5caa3167 | 80 | if (r == -ENOKEY) |
af918182 | 81 | return log_error_errno(r, |
5caa3167 | 82 | "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n" |
fbf45d22 | 83 | "Alternatively, use --esp-path= to specify path to mount point."); |
5caa3167 LP |
84 | if (r < 0) |
85 | return r; | |
86 | ||
fbf45d22 LP |
87 | free_and_replace(arg_esp_path, np); |
88 | log_debug("Using EFI System Partition at %s.", arg_esp_path); | |
89 | ||
90 | return 1; | |
91 | } | |
0974a682 | 92 | |
fbf45d22 LP |
93 | static int acquire_xbootldr(bool unprivileged_mode, sd_id128_t *ret_uuid) { |
94 | char *np; | |
95 | int r; | |
5caa3167 | 96 | |
fbf45d22 LP |
97 | r = find_xbootldr_and_warn(arg_xbootldr_path, unprivileged_mode, &np, ret_uuid); |
98 | if (r == -ENOKEY) { | |
99 | log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT."); | |
100 | if (ret_uuid) | |
101 | *ret_uuid = SD_ID128_NULL; | |
a2ae0d49 | 102 | arg_xbootldr_path = mfree(arg_xbootldr_path); |
fbf45d22 LP |
103 | return 0; |
104 | } | |
105 | if (r < 0) | |
106 | return r; | |
107 | ||
108 | free_and_replace(arg_xbootldr_path, np); | |
109 | log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path); | |
110 | ||
111 | return 1; | |
7b4d7cc0 KS |
112 | } |
113 | ||
e7dd673d | 114 | /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */ |
d3226d77 | 115 | static int get_file_version(int fd, char **v) { |
0974a682 KS |
116 | struct stat st; |
117 | char *buf; | |
118 | const char *s, *e; | |
119 | char *x = NULL; | |
120 | int r = 0; | |
7b4d7cc0 | 121 | |
d3226d77 | 122 | assert(fd >= 0); |
0974a682 | 123 | assert(v); |
7b4d7cc0 | 124 | |
d3226d77 | 125 | if (fstat(fd, &st) < 0) |
db6d9fae | 126 | return log_error_errno(errno, "Failed to stat EFI binary: %m"); |
7b4d7cc0 | 127 | |
c4ba5b51 LP |
128 | r = stat_verify_regular(&st); |
129 | if (r < 0) | |
d90f2add | 130 | return log_error_errno(r, "EFI binary is not a regular file: %m"); |
c4ba5b51 | 131 | |
db6d9fae LP |
132 | if (st.st_size < 27) { |
133 | *v = NULL; | |
0974a682 | 134 | return 0; |
db6d9fae | 135 | } |
eb9da376 | 136 | |
d3226d77 | 137 | buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
0974a682 | 138 | if (buf == MAP_FAILED) |
db6d9fae | 139 | return log_error_errno(errno, "Failed to memory map EFI binary: %m"); |
7b4d7cc0 | 140 | |
0974a682 KS |
141 | s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17); |
142 | if (!s) | |
143 | goto finish; | |
144 | s += 17; | |
7b4d7cc0 | 145 | |
0974a682 KS |
146 | e = memmem(s, st.st_size - (s - buf), " ####", 5); |
147 | if (!e || e - s < 3) { | |
78d5d4ed | 148 | r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string."); |
0974a682 KS |
149 | goto finish; |
150 | } | |
7b4d7cc0 | 151 | |
0974a682 KS |
152 | x = strndup(s, e - s); |
153 | if (!x) { | |
d3226d77 | 154 | r = log_oom(); |
0974a682 KS |
155 | goto finish; |
156 | } | |
157 | r = 1; | |
7b4d7cc0 | 158 | |
0974a682 | 159 | finish: |
db6d9fae | 160 | (void) munmap(buf, st.st_size); |
0974a682 KS |
161 | *v = x; |
162 | return r; | |
163 | } | |
7b4d7cc0 | 164 | |
0974a682 | 165 | static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { |
d3226d77 | 166 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 | 167 | struct dirent *de; |
270384b2 | 168 | const char *p; |
fbf45d22 | 169 | int c = 0, r; |
fbf45d22 LP |
170 | |
171 | assert(esp_path); | |
172 | assert(path); | |
0974a682 | 173 | |
270384b2 | 174 | p = prefix_roota(esp_path, path); |
0974a682 KS |
175 | d = opendir(p); |
176 | if (!d) { | |
d3226d77 ZJS |
177 | if (errno == ENOENT) |
178 | return 0; | |
7b4d7cc0 | 179 | |
d3226d77 | 180 | return log_error_errno(errno, "Failed to read \"%s\": %m", p); |
0974a682 KS |
181 | } |
182 | ||
e41256dc | 183 | FOREACH_DIRENT(de, d, break) { |
d3226d77 | 184 | _cleanup_free_ char *v = NULL; |
fbf45d22 | 185 | _cleanup_close_ int fd = -1; |
0974a682 | 186 | |
d3226d77 | 187 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
188 | continue; |
189 | ||
d3226d77 | 190 | if (prefix && !startswith_no_case(de->d_name, prefix)) |
0974a682 KS |
191 | continue; |
192 | ||
d3226d77 ZJS |
193 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
194 | if (fd < 0) | |
195 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); | |
0974a682 | 196 | |
d3226d77 | 197 | r = get_file_version(fd, &v); |
0974a682 | 198 | if (r < 0) |
d3226d77 | 199 | return r; |
0974a682 | 200 | if (r > 0) |
9a6f746f | 201 | printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name, ansi_highlight(), v, ansi_normal()); |
0974a682 | 202 | else |
9a6f746f | 203 | printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name); |
fbf45d22 | 204 | |
0974a682 | 205 | c++; |
0974a682 KS |
206 | } |
207 | ||
d3226d77 | 208 | return c; |
7b4d7cc0 KS |
209 | } |
210 | ||
0974a682 KS |
211 | static int status_binaries(const char *esp_path, sd_id128_t partition) { |
212 | int r; | |
213 | ||
7fd66f3c | 214 | printf("Available Boot Loaders on ESP:\n"); |
0974a682 | 215 | |
cd2d4c7f YW |
216 | if (!esp_path) { |
217 | printf(" ESP: Cannot find or access mount point of ESP.\n\n"); | |
218 | return -ENOENT; | |
219 | } | |
220 | ||
221 | printf(" ESP: %s", esp_path); | |
222 | if (!sd_id128_is_null(partition)) | |
bd44566c | 223 | printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition)); |
cd2d4c7f | 224 | printf("\n"); |
0974a682 KS |
225 | |
226 | r = enumerate_binaries(esp_path, "EFI/systemd", NULL); | |
fbf45d22 | 227 | if (r < 0) |
882b3bd6 | 228 | goto finish; |
0974a682 | 229 | if (r == 0) |
48184e43 | 230 | log_info("systemd-boot not installed in ESP."); |
0974a682 | 231 | |
00f69504 | 232 | r = enumerate_binaries(esp_path, "EFI/BOOT", "boot"); |
fbf45d22 | 233 | if (r < 0) |
882b3bd6 | 234 | goto finish; |
0974a682 | 235 | if (r == 0) |
48184e43 | 236 | log_info("No default/fallback boot loader installed in ESP."); |
0974a682 | 237 | |
882b3bd6 | 238 | r = 0; |
c11ae0ba | 239 | |
882b3bd6 LP |
240 | finish: |
241 | printf("\n"); | |
242 | return r; | |
0974a682 KS |
243 | } |
244 | ||
245 | static int print_efi_option(uint16_t id, bool in_order) { | |
7cb0f263 TA |
246 | _cleanup_free_ char *title = NULL; |
247 | _cleanup_free_ char *path = NULL; | |
0974a682 KS |
248 | sd_id128_t partition; |
249 | bool active; | |
250 | int r = 0; | |
251 | ||
252 | r = efi_get_boot_option(id, &title, &partition, &path, &active); | |
253 | if (r < 0) | |
7cb0f263 | 254 | return r; |
7b4d7cc0 | 255 | |
0974a682 | 256 | /* print only configured entries with partition information */ |
3bbaff3e | 257 | if (!path || sd_id128_is_null(partition)) |
0974a682 KS |
258 | return 0; |
259 | ||
260 | efi_tilt_backslashes(path); | |
261 | ||
ba857253 | 262 | printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal()); |
0974a682 KS |
263 | printf(" ID: 0x%04X\n", id); |
264 | printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : ""); | |
bd44566c ZJS |
265 | printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n", |
266 | SD_ID128_FORMAT_VAL(partition)); | |
9a6f746f | 267 | printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path); |
0974a682 KS |
268 | printf("\n"); |
269 | ||
7cb0f263 | 270 | return 0; |
0974a682 KS |
271 | } |
272 | ||
273 | static int status_variables(void) { | |
d3226d77 | 274 | _cleanup_free_ uint16_t *options = NULL, *order = NULL; |
a36b411e | 275 | int n_options, n_order, i; |
0974a682 | 276 | |
0974a682 | 277 | n_options = efi_get_boot_options(&options); |
d3226d77 | 278 | if (n_options == -ENOENT) |
f939cff7 LP |
279 | return log_error_errno(n_options, |
280 | "Failed to access EFI variables, efivarfs" | |
d3226d77 | 281 | " needs to be available at /sys/firmware/efi/efivars/."); |
f939cff7 | 282 | if (n_options < 0) |
d3226d77 | 283 | return log_error_errno(n_options, "Failed to read EFI boot entries: %m"); |
0974a682 | 284 | |
0974a682 | 285 | n_order = efi_get_boot_order(&order); |
d3226d77 | 286 | if (n_order == -ENOENT) |
0974a682 | 287 | n_order = 0; |
d3226d77 | 288 | else if (n_order < 0) |
7709ef3a | 289 | return log_error_errno(n_order, "Failed to read EFI boot order: %m"); |
0974a682 KS |
290 | |
291 | /* print entries in BootOrder first */ | |
7fd66f3c | 292 | printf("Boot Loaders Listed in EFI Variables:\n"); |
0974a682 KS |
293 | for (i = 0; i < n_order; i++) |
294 | print_efi_option(order[i], true); | |
295 | ||
296 | /* print remaining entries */ | |
297 | for (i = 0; i < n_options; i++) { | |
298 | int j; | |
0974a682 KS |
299 | |
300 | for (j = 0; j < n_order; j++) | |
d3226d77 | 301 | if (options[i] == order[j]) |
a908cf0a | 302 | goto next_option; |
0974a682 KS |
303 | |
304 | print_efi_option(options[i], false); | |
a908cf0a MM |
305 | |
306 | next_option: | |
307 | continue; | |
0974a682 KS |
308 | } |
309 | ||
d3226d77 | 310 | return 0; |
0974a682 KS |
311 | } |
312 | ||
44e6a5ef ZJS |
313 | static int boot_entry_file_check(const char *root, const char *p) { |
314 | _cleanup_free_ char *path; | |
315 | ||
316 | path = path_join(root, p); | |
317 | if (!path) | |
318 | return log_oom(); | |
319 | ||
320 | if (access(path, F_OK) < 0) | |
321 | return -errno; | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
d3eb6072 | 326 | static void boot_entry_file_list(const char *field, const char *root, const char *p, int *ret_status) { |
44e6a5ef ZJS |
327 | int status = boot_entry_file_check(root, p); |
328 | ||
405b104d | 329 | printf("%13s%s ", strempty(field), field ? ":" : " "); |
44e6a5ef ZJS |
330 | if (status < 0) { |
331 | errno = -status; | |
332 | printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal()); | |
333 | } else | |
334 | printf("%s\n", p); | |
d3eb6072 ZJS |
335 | |
336 | if (*ret_status == 0 && status < 0) | |
337 | *ret_status = status; | |
44e6a5ef ZJS |
338 | } |
339 | ||
20a28174 | 340 | static int boot_entry_show(const BootEntry *e, bool show_as_default) { |
d3eb6072 ZJS |
341 | int status = 0; |
342 | ||
343 | /* Returns 0 on success, negative on processing error, and positive if something is wrong with the | |
344 | boot entry itself. */ | |
345 | ||
20a28174 LP |
346 | assert(e); |
347 | ||
ce4c4f81 ZJS |
348 | printf(" title: %s%s%s" "%s%s%s\n", |
349 | ansi_highlight(), boot_entry_title(e), ansi_normal(), | |
350 | ansi_highlight_green(), show_as_default ? " (default)" : "", ansi_normal()); | |
20a28174 LP |
351 | |
352 | if (e->id) | |
353 | printf(" id: %s\n", e->id); | |
cce9457c ZJS |
354 | if (e->path) { |
355 | _cleanup_free_ char *link = NULL; | |
356 | ||
357 | /* Let's urlify the link to make it easy to view in an editor, but only if it is a text | |
358 | * file. Unified images are binary ELFs, and EFI variables are not pure text either. */ | |
359 | if (e->type == BOOT_ENTRY_CONF) | |
360 | (void) terminal_urlify_path(e->path, NULL, &link); | |
361 | ||
362 | printf(" source: %s\n", link ?: e->path); | |
363 | } | |
20a28174 LP |
364 | if (e->version) |
365 | printf(" version: %s\n", e->version); | |
366 | if (e->machine_id) | |
367 | printf(" machine-id: %s\n", e->machine_id); | |
368 | if (e->architecture) | |
369 | printf(" architecture: %s\n", e->architecture); | |
370 | if (e->kernel) | |
d3eb6072 | 371 | boot_entry_file_list("linux", e->root, e->kernel, &status); |
20a28174 | 372 | |
44e6a5ef ZJS |
373 | char **s; |
374 | STRV_FOREACH(s, e->initrd) | |
375 | boot_entry_file_list(s == e->initrd ? "initrd" : NULL, | |
376 | e->root, | |
d3eb6072 ZJS |
377 | *s, |
378 | &status); | |
20a28174 | 379 | if (!strv_isempty(e->options)) { |
9dae4c8a ZJS |
380 | _cleanup_free_ char *t = NULL, *t2 = NULL; |
381 | _cleanup_strv_free_ char **ts = NULL; | |
20a28174 LP |
382 | |
383 | t = strv_join(e->options, " "); | |
384 | if (!t) | |
385 | return log_oom(); | |
386 | ||
9dae4c8a ZJS |
387 | ts = strv_split_newlines(t); |
388 | if (!ts) | |
389 | return log_oom(); | |
390 | ||
391 | t2 = strv_join(ts, "\n "); | |
392 | if (!t2) | |
393 | return log_oom(); | |
394 | ||
9dae4c8a | 395 | printf(" options: %s\n", t2); |
20a28174 LP |
396 | } |
397 | if (e->device_tree) | |
d3eb6072 | 398 | boot_entry_file_list("devicetree", e->root, e->device_tree, &status); |
20a28174 | 399 | |
d3eb6072 | 400 | return -status; |
20a28174 LP |
401 | } |
402 | ||
81fed855 LP |
403 | static int status_entries( |
404 | const char *esp_path, | |
405 | sd_id128_t esp_partition_uuid, | |
406 | const char *xbootldr_path, | |
407 | sd_id128_t xbootldr_partition_uuid) { | |
408 | ||
7e87c7d9 | 409 | _cleanup_(boot_config_free) BootConfig config = {}; |
81fed855 LP |
410 | sd_id128_t dollar_boot_partition_uuid; |
411 | const char *dollar_boot_path; | |
a099e035 | 412 | int r; |
7e87c7d9 | 413 | |
81fed855 LP |
414 | assert(esp_path || xbootldr_path); |
415 | ||
416 | if (xbootldr_path) { | |
417 | dollar_boot_path = xbootldr_path; | |
418 | dollar_boot_partition_uuid = xbootldr_partition_uuid; | |
419 | } else { | |
420 | dollar_boot_path = esp_path; | |
421 | dollar_boot_partition_uuid = esp_partition_uuid; | |
422 | } | |
423 | ||
424 | printf("Boot Loader Entries:\n" | |
425 | " $BOOT: %s", dollar_boot_path); | |
426 | if (!sd_id128_is_null(dollar_boot_partition_uuid)) | |
bd44566c ZJS |
427 | printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", |
428 | SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid)); | |
81fed855 LP |
429 | printf("\n\n"); |
430 | ||
fbf45d22 | 431 | r = boot_entries_load_config(esp_path, xbootldr_path, &config); |
7e87c7d9 | 432 | if (r < 0) |
21f7a622 | 433 | return r; |
7e87c7d9 ZJS |
434 | |
435 | if (config.default_entry < 0) | |
a099e035 | 436 | printf("%zu entries, no entry could be determined as default.\n", config.n_entries); |
7e87c7d9 | 437 | else { |
7fd66f3c | 438 | printf("Default Boot Loader Entry:\n"); |
a099e035 | 439 | |
d3eb6072 ZJS |
440 | r = boot_entry_show(config.entries + config.default_entry, false); |
441 | if (r > 0) | |
442 | /* < 0 is already logged by the function itself, let's just emit an extra warning if | |
443 | the default entry is broken */ | |
444 | printf("\nWARNING: default boot entry is broken\n"); | |
7e87c7d9 ZJS |
445 | } |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
0974a682 KS |
450 | static int compare_product(const char *a, const char *b) { |
451 | size_t x, y; | |
452 | ||
453 | assert(a); | |
454 | assert(b); | |
455 | ||
456 | x = strcspn(a, " "); | |
457 | y = strcspn(b, " "); | |
458 | if (x != y) | |
459 | return x < y ? -1 : x > y ? 1 : 0; | |
460 | ||
461 | return strncmp(a, b, x); | |
462 | } | |
463 | ||
464 | static int compare_version(const char *a, const char *b) { | |
465 | assert(a); | |
466 | assert(b); | |
467 | ||
468 | a += strcspn(a, " "); | |
469 | a += strspn(a, " "); | |
470 | b += strcspn(b, " "); | |
471 | b += strspn(b, " "); | |
472 | ||
473 | return strverscmp(a, b); | |
474 | } | |
475 | ||
175d308c | 476 | static int version_check(int fd_from, const char *from, int fd_to, const char *to) { |
d3226d77 | 477 | _cleanup_free_ char *a = NULL, *b = NULL; |
0974a682 KS |
478 | int r; |
479 | ||
175d308c | 480 | assert(fd_from >= 0); |
0974a682 | 481 | assert(from); |
175d308c | 482 | assert(fd_to >= 0); |
0974a682 KS |
483 | assert(to); |
484 | ||
175d308c | 485 | r = get_file_version(fd_from, &a); |
0974a682 | 486 | if (r < 0) |
d3226d77 | 487 | return r; |
baaa35ad ZJS |
488 | if (r == 0) |
489 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
490 | "Source file \"%s\" does not carry version information!", | |
491 | from); | |
0974a682 | 492 | |
175d308c | 493 | r = get_file_version(fd_to, &b); |
0974a682 | 494 | if (r < 0) |
d3226d77 | 495 | return r; |
baaa35ad ZJS |
496 | if (r == 0 || compare_product(a, b) != 0) |
497 | return log_notice_errno(SYNTHETIC_ERRNO(EEXIST), | |
498 | "Skipping \"%s\", since it's owned by another boot loader.", | |
499 | to); | |
0974a682 | 500 | |
78d5d4ed LP |
501 | if (compare_version(a, b) < 0) |
502 | return log_warning_errno(SYNTHETIC_ERRNO(ESTALE), "Skipping \"%s\", since a newer boot loader version exists already.", to); | |
0974a682 | 503 | |
d3226d77 | 504 | return 0; |
0974a682 KS |
505 | } |
506 | ||
175d308c LP |
507 | static int copy_file_with_version_check(const char *from, const char *to, bool force) { |
508 | _cleanup_close_ int fd_from = -1, fd_to = -1; | |
509 | _cleanup_free_ char *t = NULL; | |
0974a682 | 510 | int r; |
0974a682 | 511 | |
175d308c LP |
512 | fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
513 | if (fd_from < 0) | |
d3226d77 | 514 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from); |
0974a682 KS |
515 | |
516 | if (!force) { | |
175d308c LP |
517 | fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
518 | if (fd_to < 0) { | |
519 | if (errno != -ENOENT) | |
520 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to); | |
521 | } else { | |
522 | r = version_check(fd_from, from, fd_to, to); | |
523 | if (r < 0) | |
524 | return r; | |
525 | ||
526 | if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1) | |
6719ca72 | 527 | return log_error_errno(errno, "Failed to seek in \"%s\": %m", from); |
175d308c LP |
528 | |
529 | fd_to = safe_close(fd_to); | |
0974a682 | 530 | } |
175d308c | 531 | } |
d3226d77 | 532 | |
175d308c LP |
533 | r = tempfn_random(to, NULL, &t); |
534 | if (r < 0) | |
535 | return log_oom(); | |
0974a682 | 536 | |
175d308c LP |
537 | RUN_WITH_UMASK(0000) { |
538 | fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644); | |
539 | if (fd_to < 0) | |
540 | return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t); | |
0974a682 KS |
541 | } |
542 | ||
175d308c | 543 | r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK); |
0974a682 | 544 | if (r < 0) { |
0675e94a AJ |
545 | (void) unlink(t); |
546 | return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
0974a682 KS |
547 | } |
548 | ||
adc6f43b | 549 | (void) copy_times(fd_from, fd_to, 0); |
0974a682 | 550 | |
8ac2f74f | 551 | if (fsync(fd_to) < 0) { |
0675e94a AJ |
552 | (void) unlink_noerrno(t); |
553 | return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
554 | } | |
555 | ||
8ac2f74f LP |
556 | (void) fsync_directory_of_file(fd_to); |
557 | ||
b1c05b98 | 558 | if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) { |
175d308c LP |
559 | (void) unlink_noerrno(t); |
560 | return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to); | |
0974a682 KS |
561 | } |
562 | ||
d3226d77 | 563 | log_info("Copied \"%s\" to \"%s\".", from, to); |
0974a682 | 564 | |
175d308c | 565 | return 0; |
0974a682 KS |
566 | } |
567 | ||
0974a682 | 568 | static int mkdir_one(const char *prefix, const char *suffix) { |
e2600fd5 | 569 | _cleanup_free_ char *p = NULL; |
0974a682 | 570 | |
e2600fd5 | 571 | p = path_join(prefix, suffix); |
0974a682 | 572 | if (mkdir(p, 0700) < 0) { |
d3226d77 ZJS |
573 | if (errno != EEXIST) |
574 | return log_error_errno(errno, "Failed to create \"%s\": %m", p); | |
0974a682 | 575 | } else |
d3226d77 | 576 | log_info("Created \"%s\".", p); |
7b4d7cc0 | 577 | |
0974a682 KS |
578 | return 0; |
579 | } | |
580 | ||
fbf45d22 | 581 | static const char *const esp_subdirs[] = { |
e44c3229 | 582 | /* The directories to place in the ESP */ |
d3226d77 ZJS |
583 | "EFI", |
584 | "EFI/systemd", | |
00f69504 | 585 | "EFI/BOOT", |
d3226d77 | 586 | "loader", |
9ee051b9 | 587 | NULL |
d3226d77 ZJS |
588 | }; |
589 | ||
e44c3229 LP |
590 | static const char *const dollar_boot_subdirs[] = { |
591 | /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */ | |
592 | "loader", | |
593 | "loader/entries", /* Type #1 entries */ | |
594 | "EFI", | |
595 | "EFI/Linux", /* Type #2 entries */ | |
596 | NULL | |
597 | }; | |
598 | ||
599 | static int create_subdirs(const char *root, const char * const *subdirs) { | |
fbf45d22 | 600 | const char *const *i; |
0974a682 KS |
601 | int r; |
602 | ||
e44c3229 LP |
603 | STRV_FOREACH(i, subdirs) { |
604 | r = mkdir_one(root, *i); | |
d3226d77 ZJS |
605 | if (r < 0) |
606 | return r; | |
607 | } | |
7b4d7cc0 | 608 | |
7b4d7cc0 | 609 | return 0; |
7b4d7cc0 KS |
610 | } |
611 | ||
0974a682 | 612 | static int copy_one_file(const char *esp_path, const char *name, bool force) { |
5509f912 | 613 | const char *e; |
d3226d77 | 614 | char *p, *q; |
0974a682 KS |
615 | int r; |
616 | ||
d3226d77 ZJS |
617 | p = strjoina(BOOTLIBDIR "/", name); |
618 | q = strjoina(esp_path, "/EFI/systemd/", name); | |
175d308c | 619 | r = copy_file_with_version_check(p, q, force); |
0974a682 | 620 | |
5509f912 LP |
621 | e = startswith(name, "systemd-boot"); |
622 | if (e) { | |
0974a682 | 623 | int k; |
d3226d77 | 624 | char *v; |
0974a682 KS |
625 | |
626 | /* Create the EFI default boot loader name (specified for removable devices) */ | |
5509f912 | 627 | v = strjoina(esp_path, "/EFI/BOOT/BOOT", e); |
846b8fc3 | 628 | ascii_strupper(strrchr(v, '/') + 1); |
0974a682 | 629 | |
175d308c | 630 | k = copy_file_with_version_check(p, v, force); |
0974a682 | 631 | if (k < 0 && r == 0) |
d3226d77 | 632 | r = k; |
0974a682 KS |
633 | } |
634 | ||
635 | return r; | |
7b4d7cc0 KS |
636 | } |
637 | ||
0974a682 KS |
638 | static int install_binaries(const char *esp_path, bool force) { |
639 | struct dirent *de; | |
d3226d77 | 640 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 KS |
641 | int r = 0; |
642 | ||
e7dd673d | 643 | d = opendir(BOOTLIBDIR); |
d3226d77 ZJS |
644 | if (!d) |
645 | return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m"); | |
0974a682 | 646 | |
0d73a816 | 647 | FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) { |
0974a682 KS |
648 | int k; |
649 | ||
d3226d77 | 650 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
651 | continue; |
652 | ||
653 | k = copy_one_file(esp_path, de->d_name, force); | |
654 | if (k < 0 && r == 0) | |
655 | r = k; | |
656 | } | |
657 | ||
0974a682 | 658 | return r; |
7b4d7cc0 KS |
659 | } |
660 | ||
b461576d | 661 | static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) { |
d3226d77 | 662 | _cleanup_free_ char *opath = NULL; |
0974a682 | 663 | sd_id128_t ouuid; |
d3226d77 | 664 | int r; |
7b4d7cc0 | 665 | |
d3226d77 ZJS |
666 | r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL); |
667 | if (r < 0) | |
0974a682 KS |
668 | return false; |
669 | if (!sd_id128_equal(uuid, ouuid)) | |
d3226d77 | 670 | return false; |
0974a682 | 671 | if (!streq_ptr(path, opath)) |
d3226d77 | 672 | return false; |
0974a682 | 673 | |
d3226d77 | 674 | return true; |
0974a682 KS |
675 | } |
676 | ||
677 | static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) { | |
d3226d77 ZJS |
678 | _cleanup_free_ uint16_t *options = NULL; |
679 | int n, i; | |
0974a682 | 680 | |
d3226d77 ZJS |
681 | n = efi_get_boot_options(&options); |
682 | if (n < 0) | |
683 | return n; | |
0974a682 | 684 | |
e7dd673d | 685 | /* find already existing systemd-boot entry */ |
d3226d77 | 686 | for (i = 0; i < n; i++) |
0974a682 | 687 | if (same_entry(options[i], uuid, path)) { |
d3226d77 ZJS |
688 | *id = options[i]; |
689 | return 1; | |
0974a682 KS |
690 | } |
691 | ||
692 | /* find free slot in the sorted BootXXXX variable list */ | |
d3226d77 | 693 | for (i = 0; i < n; i++) |
0974a682 | 694 | if (i != options[i]) { |
d3226d77 ZJS |
695 | *id = i; |
696 | return 1; | |
0974a682 KS |
697 | } |
698 | ||
699 | /* use the next one */ | |
700 | if (i == 0xffff) | |
701 | return -ENOSPC; | |
d3226d77 ZJS |
702 | *id = i; |
703 | return 0; | |
0974a682 KS |
704 | } |
705 | ||
706 | static int insert_into_order(uint16_t slot, bool first) { | |
d3226d77 ZJS |
707 | _cleanup_free_ uint16_t *order = NULL; |
708 | uint16_t *t; | |
709 | int n, i; | |
0974a682 | 710 | |
d3226d77 ZJS |
711 | n = efi_get_boot_order(&order); |
712 | if (n <= 0) | |
0974a682 | 713 | /* no entry, add us */ |
d3226d77 | 714 | return efi_set_boot_order(&slot, 1); |
0974a682 KS |
715 | |
716 | /* are we the first and only one? */ | |
d3226d77 ZJS |
717 | if (n == 1 && order[0] == slot) |
718 | return 0; | |
0974a682 KS |
719 | |
720 | /* are we already in the boot order? */ | |
d3226d77 | 721 | for (i = 0; i < n; i++) { |
0974a682 KS |
722 | if (order[i] != slot) |
723 | continue; | |
724 | ||
725 | /* we do not require to be the first one, all is fine */ | |
726 | if (!first) | |
d3226d77 | 727 | return 0; |
0974a682 KS |
728 | |
729 | /* move us to the first slot */ | |
d3226d77 | 730 | memmove(order + 1, order, i * sizeof(uint16_t)); |
0974a682 | 731 | order[0] = slot; |
d3226d77 | 732 | return efi_set_boot_order(order, n); |
0974a682 KS |
733 | } |
734 | ||
735 | /* extend array */ | |
a7798cd8 | 736 | t = reallocarray(order, n + 1, sizeof(uint16_t)); |
d3226d77 ZJS |
737 | if (!t) |
738 | return -ENOMEM; | |
739 | order = t; | |
0974a682 KS |
740 | |
741 | /* add us to the top or end of the list */ | |
742 | if (first) { | |
d3226d77 | 743 | memmove(order + 1, order, n * sizeof(uint16_t)); |
0974a682 KS |
744 | order[0] = slot; |
745 | } else | |
d3226d77 | 746 | order[n] = slot; |
0974a682 | 747 | |
d3226d77 | 748 | return efi_set_boot_order(order, n + 1); |
0974a682 KS |
749 | } |
750 | ||
751 | static int remove_from_order(uint16_t slot) { | |
7cb0f263 | 752 | _cleanup_free_ uint16_t *order = NULL; |
d3226d77 | 753 | int n, i; |
0974a682 | 754 | |
d3226d77 ZJS |
755 | n = efi_get_boot_order(&order); |
756 | if (n <= 0) | |
757 | return n; | |
0974a682 | 758 | |
d3226d77 | 759 | for (i = 0; i < n; i++) { |
0974a682 KS |
760 | if (order[i] != slot) |
761 | continue; | |
762 | ||
d3226d77 ZJS |
763 | if (i + 1 < n) |
764 | memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t)); | |
765 | return efi_set_boot_order(order, n - 1); | |
0974a682 KS |
766 | } |
767 | ||
d3226d77 | 768 | return 0; |
0974a682 KS |
769 | } |
770 | ||
771 | static int install_variables(const char *esp_path, | |
772 | uint32_t part, uint64_t pstart, uint64_t psize, | |
773 | sd_id128_t uuid, const char *path, | |
774 | bool first) { | |
270384b2 | 775 | const char *p; |
0974a682 KS |
776 | uint16_t slot; |
777 | int r; | |
778 | ||
779 | if (!is_efi_boot()) { | |
d3226d77 | 780 | log_warning("Not booted with EFI, skipping EFI variable setup."); |
0974a682 KS |
781 | return 0; |
782 | } | |
783 | ||
270384b2 | 784 | p = prefix_roota(esp_path, path); |
0974a682 KS |
785 | if (access(p, F_OK) < 0) { |
786 | if (errno == ENOENT) | |
d3226d77 | 787 | return 0; |
f939cff7 LP |
788 | |
789 | return log_error_errno(errno, "Cannot access \"%s\": %m", p); | |
0974a682 | 790 | } |
7b4d7cc0 | 791 | |
0974a682 | 792 | r = find_slot(uuid, path, &slot); |
d3226d77 ZJS |
793 | if (r < 0) |
794 | return log_error_errno(r, | |
795 | r == -ENOENT ? | |
796 | "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" : | |
797 | "Failed to determine current boot order: %m"); | |
7b4d7cc0 | 798 | |
181ccb43 | 799 | if (first || r == 0) { |
0974a682 KS |
800 | r = efi_add_boot_option(slot, "Linux Boot Manager", |
801 | part, pstart, psize, | |
802 | uuid, path); | |
d3226d77 ZJS |
803 | if (r < 0) |
804 | return log_error_errno(r, "Failed to create EFI Boot variable entry: %m"); | |
0974a682 | 805 | |
d3226d77 ZJS |
806 | log_info("Created EFI boot entry \"Linux Boot Manager\"."); |
807 | } | |
0974a682 | 808 | |
d3226d77 | 809 | return insert_into_order(slot, first); |
0974a682 KS |
810 | } |
811 | ||
812 | static int remove_boot_efi(const char *esp_path) { | |
d3226d77 | 813 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 | 814 | struct dirent *de; |
270384b2 | 815 | const char *p; |
d3226d77 | 816 | int r, c = 0; |
0974a682 | 817 | |
270384b2 | 818 | p = prefix_roota(esp_path, "/EFI/BOOT"); |
0974a682 KS |
819 | d = opendir(p); |
820 | if (!d) { | |
d3226d77 ZJS |
821 | if (errno == ENOENT) |
822 | return 0; | |
0974a682 | 823 | |
d3226d77 | 824 | return log_error_errno(errno, "Failed to open directory \"%s\": %m", p); |
0974a682 KS |
825 | } |
826 | ||
e41256dc | 827 | FOREACH_DIRENT(de, d, break) { |
d3226d77 ZJS |
828 | _cleanup_close_ int fd = -1; |
829 | _cleanup_free_ char *v = NULL; | |
0974a682 | 830 | |
d3226d77 | 831 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
832 | continue; |
833 | ||
b7536c45 | 834 | if (!startswith_no_case(de->d_name, "boot")) |
0974a682 KS |
835 | continue; |
836 | ||
d3226d77 | 837 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
dd114e11 | 838 | if (fd < 0) |
d3226d77 | 839 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); |
0974a682 | 840 | |
d3226d77 | 841 | r = get_file_version(fd, &v); |
0974a682 | 842 | if (r < 0) |
d3226d77 ZJS |
843 | return r; |
844 | if (r > 0 && startswith(v, "systemd-boot ")) { | |
845 | r = unlinkat(dirfd(d), de->d_name, 0); | |
846 | if (r < 0) | |
847 | return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name); | |
848 | ||
a592ab6a | 849 | log_info("Removed \"%s/%s\".", p, de->d_name); |
0974a682 KS |
850 | } |
851 | ||
852 | c++; | |
0974a682 KS |
853 | } |
854 | ||
d3226d77 | 855 | return c; |
0974a682 KS |
856 | } |
857 | ||
858 | static int rmdir_one(const char *prefix, const char *suffix) { | |
270384b2 | 859 | const char *p; |
7b4d7cc0 | 860 | |
270384b2 | 861 | p = prefix_roota(prefix, suffix); |
0974a682 | 862 | if (rmdir(p) < 0) { |
fbf45d22 LP |
863 | bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY); |
864 | ||
865 | log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno, | |
866 | "Failed to remove directory \"%s\": %m", p); | |
867 | if (!ignore) | |
868 | return -errno; | |
7b4d7cc0 | 869 | } else |
d3226d77 | 870 | log_info("Removed \"%s\".", p); |
7b4d7cc0 | 871 | |
0974a682 | 872 | return 0; |
7b4d7cc0 KS |
873 | } |
874 | ||
e44c3229 LP |
875 | static int remove_subdirs(const char *root, const char *const *subdirs) { |
876 | int r, q; | |
fbf45d22 | 877 | |
e44c3229 LP |
878 | /* We use recursion here to destroy the directories in reverse order. Which should be safe given how |
879 | * short the array is. */ | |
fbf45d22 | 880 | |
e44c3229 LP |
881 | if (!subdirs[0]) /* A the end of the list */ |
882 | return 0; | |
fbf45d22 | 883 | |
e44c3229 LP |
884 | r = remove_subdirs(root, subdirs + 1); |
885 | q = rmdir_one(root, subdirs[0]); | |
886 | ||
887 | return r < 0 ? r : q; | |
888 | } | |
889 | ||
890 | static int remove_machine_id_directory(const char *root, sd_id128_t machine_id) { | |
891 | char buf[SD_ID128_STRING_MAX]; | |
892 | ||
893 | assert(root); | |
894 | ||
895 | return rmdir_one(root, sd_id128_to_string(machine_id, buf)); | |
fbf45d22 LP |
896 | } |
897 | ||
0974a682 | 898 | static int remove_binaries(const char *esp_path) { |
270384b2 | 899 | const char *p; |
0974a682 KS |
900 | int r, q; |
901 | ||
270384b2 | 902 | p = prefix_roota(esp_path, "/EFI/systemd"); |
c6878637 | 903 | r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); |
0974a682 KS |
904 | |
905 | q = remove_boot_efi(esp_path); | |
906 | if (q < 0 && r == 0) | |
907 | r = q; | |
908 | ||
0974a682 KS |
909 | return r; |
910 | } | |
911 | ||
e44c3229 | 912 | static int remove_file(const char *root, const char *file) { |
fbf45d22 LP |
913 | const char *p; |
914 | ||
e44c3229 LP |
915 | assert(root); |
916 | assert(file); | |
fbf45d22 | 917 | |
e44c3229 | 918 | p = prefix_roota(root, file); |
fbf45d22 | 919 | if (unlink(p) < 0) { |
e44c3229 LP |
920 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, |
921 | "Failed to unlink file \"%s\": %m", p); | |
fbf45d22 | 922 | |
e44c3229 LP |
923 | return errno == ENOENT ? 0 : -errno; |
924 | } | |
fbf45d22 | 925 | |
e44c3229 LP |
926 | log_info("Removed \"%s\".", p); |
927 | return 1; | |
fbf45d22 LP |
928 | } |
929 | ||
0974a682 KS |
930 | static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) { |
931 | uint16_t slot; | |
932 | int r; | |
933 | ||
934 | if (!is_efi_boot()) | |
935 | return 0; | |
936 | ||
937 | r = find_slot(uuid, path, &slot); | |
938 | if (r != 1) | |
939 | return 0; | |
940 | ||
941 | r = efi_remove_boot_option(slot); | |
942 | if (r < 0) | |
943 | return r; | |
944 | ||
945 | if (in_order) | |
d3226d77 | 946 | return remove_from_order(slot); |
f939cff7 LP |
947 | |
948 | return 0; | |
0974a682 KS |
949 | } |
950 | ||
e44c3229 LP |
951 | static int remove_loader_variables(void) { |
952 | const char *p; | |
953 | int r = 0; | |
954 | ||
955 | /* Remove all persistent loader variables we define */ | |
956 | ||
957 | FOREACH_STRING(p, | |
958 | "LoaderConfigTimeout", | |
959 | "LoaderConfigTimeoutOneShot", | |
960 | "LoaderEntryDefault", | |
961 | "LoaderEntryOneShot", | |
962 | "LoaderSystemToken") { | |
963 | ||
964 | int q; | |
965 | ||
966 | q = efi_set_variable(EFI_VENDOR_LOADER, p, NULL, 0); | |
967 | if (q == -ENOENT) | |
968 | continue; | |
969 | if (q < 0) { | |
970 | log_warning_errno(q, "Failed to remove %s variable: %m", p); | |
971 | if (r >= 0) | |
972 | r = q; | |
973 | } else | |
974 | log_info("Removed EFI variable %s.", p); | |
975 | } | |
976 | ||
977 | return r; | |
978 | } | |
979 | ||
341890de | 980 | static int install_loader_config(const char *esp_path, sd_id128_t machine_id) { |
d5ff6d6d | 981 | char machine_string[SD_ID128_STRING_MAX]; |
f5b84de2 LP |
982 | _cleanup_(unlink_and_freep) char *t = NULL; |
983 | _cleanup_fclose_ FILE *f = NULL; | |
d5ff6d6d | 984 | const char *p; |
f5b84de2 | 985 | int r, fd; |
0974a682 | 986 | |
270384b2 | 987 | p = prefix_roota(esp_path, "/loader/loader.conf"); |
f5b84de2 LP |
988 | if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */ |
989 | return 0; | |
990 | ||
991 | fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t); | |
992 | if (fd < 0) | |
993 | return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p); | |
994 | ||
e92aaed3 | 995 | f = fdopen(fd, "w"); |
f5b84de2 LP |
996 | if (!f) { |
997 | safe_close(fd); | |
998 | return log_oom(); | |
999 | } | |
0974a682 | 1000 | |
a36b411e LP |
1001 | fprintf(f, "#timeout 3\n" |
1002 | "#console-mode keep\n" | |
1003 | "default %s-*\n", sd_id128_to_string(machine_id, machine_string)); | |
0974a682 | 1004 | |
0675e94a | 1005 | r = fflush_sync_and_check(f); |
d5ff6d6d LP |
1006 | if (r < 0) |
1007 | return log_error_errno(r, "Failed to write \"%s\": %m", p); | |
0974a682 | 1008 | |
f5b84de2 LP |
1009 | r = link_tmpfile(fd, t, p); |
1010 | if (r == -EEXIST) | |
1011 | return 0; /* Silently skip creation if the file exists now (recheck) */ | |
1012 | if (r < 0) | |
1013 | return log_error_errno(r, "Failed to move \"%s\" into place: %m", p); | |
1014 | ||
1015 | t = mfree(t); | |
f5b84de2 | 1016 | return 1; |
0974a682 KS |
1017 | } |
1018 | ||
e44c3229 | 1019 | static int install_machine_id_directory(const char *root, sd_id128_t machine_id) { |
341890de ZJS |
1020 | char buf[SD_ID128_STRING_MAX]; |
1021 | ||
e44c3229 | 1022 | assert(root); |
341890de | 1023 | |
e44c3229 | 1024 | return mkdir_one(root, sd_id128_to_string(machine_id, buf)); |
fbf45d22 LP |
1025 | } |
1026 | ||
2f2c539c | 1027 | static int help(int argc, char *argv[], void *userdata) { |
37ec0fdd LP |
1028 | _cleanup_free_ char *link = NULL; |
1029 | int r; | |
1030 | ||
1031 | r = terminal_urlify_man("bootctl", "1", &link); | |
1032 | if (r < 0) | |
1033 | return log_oom(); | |
2f2c539c | 1034 | |
353b2baa | 1035 | printf("%s [OPTIONS...] COMMAND ...\n" |
b289de2b | 1036 | "\n%sInstall/update/remove the systemd-boot EFI boot manager and list/select entries.%s\n" |
d88c96ff | 1037 | "\nBoot Loader Commands:\n" |
2536752d ZJS |
1038 | " status Show status of installed systemd-boot and EFI variables\n" |
1039 | " install Install systemd-boot to the ESP and EFI variables\n" | |
1040 | " update Update systemd-boot in the ESP and EFI variables\n" | |
1041 | " remove Remove systemd-boot from the ESP and EFI variables\n" | |
1042 | " is-installed Test whether systemd-boot is installed in the ESP\n" | |
1043 | " random-seed Initialize random seed in ESP and EFI variables\n" | |
1044 | " systemd-efi-options Query or set system options string in EFI variable\n" | |
d88c96ff | 1045 | "\nBoot Loader Entries Commands:\n" |
2536752d ZJS |
1046 | " list List boot loader entries\n" |
1047 | " set-default ID Set default boot loader entry\n" | |
1048 | " set-oneshot ID Set default boot loader entry, for next boot only\n" | |
353b2baa | 1049 | "\nOptions:\n" |
e1fac8a6 ZJS |
1050 | " -h --help Show this help\n" |
1051 | " --version Print version\n" | |
1052 | " --esp-path=PATH Path to the EFI System Partition (ESP)\n" | |
1053 | " --boot-path=PATH Path to the $BOOT partition\n" | |
1054 | " -p --print-esp-path Print path to the EFI System Partition\n" | |
1055 | " -x --print-boot-path Print path to the $BOOT partition\n" | |
1056 | " --no-variables Don't touch EFI variables\n" | |
1057 | " --no-pager Do not pipe output into a pager\n" | |
37ec0fdd LP |
1058 | "\nSee the %s for details.\n" |
1059 | , program_invocation_short_name | |
353b2baa LP |
1060 | , ansi_highlight() |
1061 | , ansi_normal() | |
d88c96ff | 1062 | , link); |
0974a682 KS |
1063 | |
1064 | return 0; | |
1065 | } | |
1066 | ||
0974a682 KS |
1067 | static int parse_argv(int argc, char *argv[]) { |
1068 | enum { | |
fbf45d22 LP |
1069 | ARG_ESP_PATH = 0x100, |
1070 | ARG_BOOT_PATH, | |
0974a682 KS |
1071 | ARG_VERSION, |
1072 | ARG_NO_VARIABLES, | |
57db6f18 | 1073 | ARG_NO_PAGER, |
7b4d7cc0 KS |
1074 | }; |
1075 | ||
0974a682 | 1076 | static const struct option options[] = { |
fbf45d22 LP |
1077 | { "help", no_argument, NULL, 'h' }, |
1078 | { "version", no_argument, NULL, ARG_VERSION }, | |
1079 | { "esp-path", required_argument, NULL, ARG_ESP_PATH }, | |
1080 | { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */ | |
1081 | { "boot-path", required_argument, NULL, ARG_BOOT_PATH }, | |
1082 | { "print-esp-path", no_argument, NULL, 'p' }, | |
1083 | { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */ | |
fba4e945 | 1084 | { "print-boot-path", no_argument, NULL, 'x' }, |
fbf45d22 LP |
1085 | { "no-variables", no_argument, NULL, ARG_NO_VARIABLES }, |
1086 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
a36b411e | 1087 | {} |
0974a682 KS |
1088 | }; |
1089 | ||
2f2c539c | 1090 | int c, r; |
7b4d7cc0 KS |
1091 | |
1092 | assert(argc >= 0); | |
1093 | assert(argv); | |
1094 | ||
fba4e945 | 1095 | while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0) |
0974a682 | 1096 | switch (c) { |
7b4d7cc0 | 1097 | |
0974a682 | 1098 | case 'h': |
2f2c539c | 1099 | help(0, NULL, NULL); |
7b4d7cc0 | 1100 | return 0; |
7b4d7cc0 | 1101 | |
0974a682 | 1102 | case ARG_VERSION: |
3f6fd1ba | 1103 | return version(); |
7b4d7cc0 | 1104 | |
fbf45d22 LP |
1105 | case ARG_ESP_PATH: |
1106 | r = free_and_strdup(&arg_esp_path, optarg); | |
1107 | if (r < 0) | |
1108 | return log_oom(); | |
1109 | break; | |
1110 | ||
1111 | case ARG_BOOT_PATH: | |
1112 | r = free_and_strdup(&arg_xbootldr_path, optarg); | |
2f2c539c LP |
1113 | if (r < 0) |
1114 | return log_oom(); | |
0974a682 KS |
1115 | break; |
1116 | ||
30b50477 | 1117 | case 'p': |
aa467bca ZJS |
1118 | if (arg_print_dollar_boot_path) |
1119 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1120 | "--print-boot-path/-x cannot be combined with --print-esp-path/-p"); | |
fbf45d22 LP |
1121 | arg_print_esp_path = true; |
1122 | break; | |
1123 | ||
fba4e945 | 1124 | case 'x': |
aa467bca ZJS |
1125 | if (arg_print_esp_path) |
1126 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1127 | "--print-boot-path/-x cannot be combined with --print-esp-path/-p"); | |
fbf45d22 | 1128 | arg_print_dollar_boot_path = true; |
30b50477 ZJS |
1129 | break; |
1130 | ||
0974a682 KS |
1131 | case ARG_NO_VARIABLES: |
1132 | arg_touch_variables = false; | |
1133 | break; | |
1134 | ||
57db6f18 | 1135 | case ARG_NO_PAGER: |
0221d68a | 1136 | arg_pager_flags |= PAGER_DISABLE; |
57db6f18 LP |
1137 | break; |
1138 | ||
0974a682 KS |
1139 | case '?': |
1140 | return -EINVAL; | |
1141 | ||
1142 | default: | |
d3226d77 | 1143 | assert_not_reached("Unknown option"); |
7b4d7cc0 | 1144 | } |
7b4d7cc0 | 1145 | |
0974a682 KS |
1146 | return 1; |
1147 | } | |
7b4d7cc0 | 1148 | |
551710cf ZJS |
1149 | static void read_loader_efi_var(const char *name, char **var) { |
1150 | int r; | |
1151 | ||
1152 | r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var); | |
1153 | if (r < 0 && r != -ENOENT) | |
1154 | log_warning_errno(r, "Failed to read EFI variable %s: %m", name); | |
1155 | } | |
1156 | ||
2f2c539c | 1157 | static int verb_status(int argc, char *argv[], void *userdata) { |
fbf45d22 | 1158 | sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL; |
46fb255b | 1159 | int r, k; |
0974a682 | 1160 | |
fbf45d22 LP |
1161 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid); |
1162 | if (arg_print_esp_path) { | |
5caa3167 LP |
1163 | if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only |
1164 | * error the find_esp_and_warn() won't log on its own) */ | |
fbf45d22 | 1165 | return log_error_errno(r, "Failed to determine ESP location: %m"); |
30b50477 ZJS |
1166 | if (r < 0) |
1167 | return r; | |
1168 | ||
fbf45d22 LP |
1169 | puts(arg_esp_path); |
1170 | } | |
1171 | ||
1172 | r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid); | |
1173 | if (arg_print_dollar_boot_path) { | |
1174 | if (r == -EACCES) | |
1175 | return log_error_errno(r, "Failed to determine XBOOTLDR location: %m"); | |
1176 | if (r < 0) | |
1177 | return r; | |
1178 | ||
f8a2b09a YW |
1179 | const char *path = arg_dollar_boot_path(); |
1180 | if (!path) | |
1181 | return log_error_errno(SYNTHETIC_ERRNO(EACCES), "Failed to determine XBOOTLDR location: %m"); | |
1182 | ||
1183 | puts(path); | |
30b50477 | 1184 | } |
551710cf | 1185 | |
fbf45d22 LP |
1186 | if (arg_print_esp_path || arg_print_dollar_boot_path) |
1187 | return 0; | |
1188 | ||
5caa3167 LP |
1189 | r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we |
1190 | * can show */ | |
1191 | ||
0221d68a | 1192 | (void) pager_open(arg_pager_flags); |
57db6f18 | 1193 | |
2f2c539c | 1194 | if (is_efi_boot()) { |
80641a81 LP |
1195 | static const struct { |
1196 | uint64_t flag; | |
1197 | const char *name; | |
1198 | } flags[] = { | |
22c5ff51 LP |
1199 | { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" }, |
1200 | { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" }, | |
1201 | { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" }, | |
1202 | { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" }, | |
1203 | { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" }, | |
1204 | { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" }, | |
1205 | { EFI_LOADER_FEATURE_RANDOM_SEED, "Support for passing random seed to OS" }, | |
80641a81 LP |
1206 | }; |
1207 | ||
81375b9b | 1208 | _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL; |
25579a43 | 1209 | sd_id128_t loader_part_uuid = SD_ID128_NULL; |
80641a81 LP |
1210 | uint64_t loader_features = 0; |
1211 | size_t i; | |
0974a682 | 1212 | |
2f2c539c LP |
1213 | read_loader_efi_var("LoaderFirmwareType", &fw_type); |
1214 | read_loader_efi_var("LoaderFirmwareInfo", &fw_info); | |
1215 | read_loader_efi_var("LoaderInfo", &loader); | |
81375b9b | 1216 | read_loader_efi_var("StubInfo", &stub); |
2f2c539c | 1217 | read_loader_efi_var("LoaderImageIdentifier", &loader_path); |
80641a81 | 1218 | (void) efi_loader_get_features(&loader_features); |
551710cf | 1219 | |
2f2c539c LP |
1220 | if (loader_path) |
1221 | efi_tilt_backslashes(loader_path); | |
1222 | ||
46fb255b ZJS |
1223 | k = efi_loader_get_device_part_uuid(&loader_part_uuid); |
1224 | if (k < 0 && k != -ENOENT) | |
1225 | r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m"); | |
2f2c539c LP |
1226 | |
1227 | printf("System:\n"); | |
ba857253 | 1228 | printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal()); |
33987ba0 YW |
1229 | printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot())); |
1230 | printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user"); | |
2f2c539c LP |
1231 | printf("\n"); |
1232 | ||
7fd66f3c | 1233 | printf("Current Boot Loader:\n"); |
ba857253 | 1234 | printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal()); |
80641a81 LP |
1235 | |
1236 | for (i = 0; i < ELEMENTSOF(flags); i++) { | |
1237 | ||
1238 | if (i == 0) | |
1239 | printf(" Features: "); | |
1240 | else | |
1241 | printf(" "); | |
1242 | ||
1243 | if (FLAGS_SET(loader_features, flags[i].flag)) | |
9a6f746f | 1244 | printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name); |
80641a81 | 1245 | else |
9a6f746f | 1246 | printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name); |
80641a81 LP |
1247 | } |
1248 | ||
81375b9b ДГ |
1249 | if (stub) |
1250 | printf(" Stub: %s\n", stub); | |
e28973ee | 1251 | if (!sd_id128_is_null(loader_part_uuid)) |
bd44566c | 1252 | printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n", |
2f2c539c LP |
1253 | SD_ID128_FORMAT_VAL(loader_part_uuid)); |
1254 | else | |
cd2d4c7f | 1255 | printf(" ESP: n/a\n"); |
9a6f746f | 1256 | printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path)); |
2f2c539c | 1257 | printf("\n"); |
d6e9a347 LP |
1258 | |
1259 | printf("Random Seed:\n"); | |
1260 | printf(" Passed to OS: %s\n", yes_no(access("/sys/firmware/efi/efivars/LoaderRandomSeed-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0)); | |
1261 | printf(" System Token: %s\n", access("/sys/firmware/efi/efivars/LoaderSystemToken-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) >= 0 ? "set" : "not set"); | |
1262 | ||
1263 | if (arg_esp_path) { | |
1264 | _cleanup_free_ char *p = NULL; | |
1265 | ||
1266 | p = path_join(arg_esp_path, "/loader/random-seed"); | |
1267 | if (!p) | |
1268 | return log_oom(); | |
1269 | ||
1270 | printf(" Exists: %s\n", yes_no(access(p, F_OK) >= 0)); | |
1271 | } | |
1272 | ||
1273 | printf("\n"); | |
2f2c539c | 1274 | } else |
cd2d4c7f | 1275 | printf("System:\n Not booted with EFI\n\n"); |
7b4d7cc0 | 1276 | |
fbf45d22 LP |
1277 | if (arg_esp_path) { |
1278 | k = status_binaries(arg_esp_path, esp_uuid); | |
ecec2a5d LP |
1279 | if (k < 0) |
1280 | r = k; | |
1281 | } | |
2f2c539c | 1282 | |
cd2d4c7f | 1283 | if (is_efi_boot()) { |
46fb255b ZJS |
1284 | k = status_variables(); |
1285 | if (k < 0) | |
1286 | r = k; | |
cd2d4c7f | 1287 | } |
0974a682 | 1288 | |
fbf45d22 | 1289 | if (arg_esp_path || arg_xbootldr_path) { |
81fed855 | 1290 | k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid); |
ecec2a5d LP |
1291 | if (k < 0) |
1292 | r = k; | |
1293 | } | |
7e87c7d9 | 1294 | |
46fb255b | 1295 | return r; |
2f2c539c | 1296 | } |
7b4d7cc0 | 1297 | |
7e87c7d9 | 1298 | static int verb_list(int argc, char *argv[], void *userdata) { |
5caa3167 | 1299 | _cleanup_(boot_config_free) BootConfig config = {}; |
5caa3167 | 1300 | int r; |
7e87c7d9 | 1301 | |
5caa3167 LP |
1302 | /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn |
1303 | * off logging about access errors and turn off potentially privileged device probing. Here we're interested in | |
1304 | * the latter but not the former, hence request the mode, and log about EACCES. */ | |
7e87c7d9 | 1305 | |
fbf45d22 | 1306 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL); |
5caa3167 LP |
1307 | if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */ |
1308 | return log_error_errno(r, "Failed to determine ESP: %m"); | |
7e87c7d9 ZJS |
1309 | if (r < 0) |
1310 | return r; | |
1311 | ||
fbf45d22 LP |
1312 | r = acquire_xbootldr(geteuid() != 0, NULL); |
1313 | if (r == -EACCES) | |
1314 | return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m"); | |
1315 | if (r < 0) | |
1316 | return r; | |
1317 | ||
1318 | r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config); | |
7e87c7d9 | 1319 | if (r < 0) |
21f7a622 | 1320 | return r; |
7e87c7d9 | 1321 | |
93f14ce2 | 1322 | (void) boot_entries_augment_from_loader(&config, false); |
bd2865ca | 1323 | |
20a28174 LP |
1324 | if (config.n_entries == 0) |
1325 | log_info("No boot loader entries found."); | |
1326 | else { | |
1327 | size_t n; | |
7e87c7d9 | 1328 | |
0221d68a | 1329 | (void) pager_open(arg_pager_flags); |
57db6f18 | 1330 | |
20a28174 | 1331 | printf("Boot Loader Entries:\n"); |
7e87c7d9 | 1332 | |
20a28174 LP |
1333 | for (n = 0; n < config.n_entries; n++) { |
1334 | r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry); | |
1335 | if (r < 0) | |
1336 | return r; | |
7e87c7d9 | 1337 | |
4629499e LP |
1338 | if (n+1 < config.n_entries) |
1339 | putchar('\n'); | |
7e87c7d9 | 1340 | } |
cd2d4c7f | 1341 | } |
0974a682 | 1342 | |
7e87c7d9 | 1343 | return 0; |
2f2c539c | 1344 | } |
7b4d7cc0 | 1345 | |
e44c3229 LP |
1346 | static int install_random_seed(const char *esp) { |
1347 | _cleanup_(unlink_and_freep) char *tmp = NULL; | |
1348 | _cleanup_free_ void *buffer = NULL; | |
1349 | _cleanup_free_ char *path = NULL; | |
1350 | _cleanup_close_ int fd = -1; | |
1351 | size_t sz, token_size; | |
1352 | ssize_t n; | |
1353 | int r; | |
1354 | ||
1355 | assert(esp); | |
1356 | ||
1357 | path = path_join(esp, "/loader/random-seed"); | |
1358 | if (!path) | |
1359 | return log_oom(); | |
1360 | ||
1361 | sz = random_pool_size(); | |
1362 | ||
1363 | buffer = malloc(sz); | |
1364 | if (!buffer) | |
1365 | return log_oom(); | |
1366 | ||
1367 | r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK); | |
1368 | if (r < 0) | |
a4d20801 | 1369 | return log_error_errno(r, "Failed to acquire random seed: %m"); |
e44c3229 | 1370 | |
a4a55e9a LP |
1371 | /* Normally create_subdirs() should already have created everything we need, but in case "bootctl |
1372 | * random-seed" is called we want to just create the minimum we need for it, and not the full | |
1373 | * list. */ | |
1374 | r = mkdir_parents(path, 0755); | |
1375 | if (r < 0) | |
1376 | return log_error_errno(r, "Failed to create parent directory for %s: %m", path); | |
1377 | ||
e44c3229 LP |
1378 | r = tempfn_random(path, "bootctl", &tmp); |
1379 | if (r < 0) | |
1380 | return log_oom(); | |
1381 | ||
1382 | fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600); | |
1383 | if (fd < 0) { | |
1384 | tmp = mfree(tmp); | |
1385 | return log_error_errno(fd, "Failed to open random seed file for writing: %m"); | |
1386 | } | |
1387 | ||
1388 | n = write(fd, buffer, sz); | |
1389 | if (n < 0) | |
1390 | return log_error_errno(errno, "Failed to write random seed file: %m"); | |
1391 | if ((size_t) n != sz) | |
1392 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing random seed file."); | |
1393 | ||
1394 | if (rename(tmp, path) < 0) | |
1395 | return log_error_errno(r, "Failed to move random seed file into place: %m"); | |
1396 | ||
1397 | tmp = mfree(tmp); | |
1398 | ||
a4d20801 | 1399 | log_info("Random seed file %s successfully written (%zu bytes).", path, sz); |
e44c3229 LP |
1400 | |
1401 | if (!arg_touch_variables) | |
1402 | return 0; | |
1403 | ||
1404 | if (!is_efi_boot()) { | |
1405 | log_notice("Not booted with EFI, skipping EFI variable setup."); | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | r = getenv_bool("SYSTEMD_WRITE_SYSTEM_TOKEN"); | |
1410 | if (r < 0) { | |
1411 | if (r != -ENXIO) | |
1412 | log_warning_errno(r, "Failed to parse $SYSTEMD_WRITE_SYSTEM_TOKEN, ignoring."); | |
1413 | ||
1414 | if (detect_vm() > 0) { | |
1415 | /* Let's not write a system token if we detect we are running in a VM | |
1416 | * environment. Why? Our default security model for the random seed uses the system | |
1417 | * token as a mechanism to ensure we are not vulnerable to golden master sloppiness | |
1418 | * issues, i.e. that people initialize the random seed file, then copy the image to | |
1419 | * many systems and end up with the same random seed in each that is assumed to be | |
1420 | * valid but in reality is the same for all machines. By storing a system token in | |
1421 | * the EFI variable space we can make sure that even though the random seeds on disk | |
1422 | * are all the same they will be different on each system under the assumption that | |
1423 | * the EFI variable space is maintained separate from the random seed storage. That | |
1424 | * is generally the case on physical systems, as the ESP is stored on persistant | |
1425 | * storage, and the EFI variables in NVRAM. However in virtualized environments this | |
1426 | * is generally not true: the EFI variable set is typically stored along with the | |
1427 | * disk image itself. For example, using the OVMF EFI firmware the EFI variables are | |
1428 | * stored in a file in the ESP itself. */ | |
1429 | ||
1430 | log_notice("Not installing system token, since we are running in a virtualized environment."); | |
1431 | return 0; | |
1432 | } | |
1433 | } else if (r == 0) { | |
1434 | log_notice("Not writing system token, because $SYSTEMD_WRITE_SYSTEM_TOKEN is set to false."); | |
1435 | return 0; | |
1436 | } | |
1437 | ||
1438 | r = efi_get_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", NULL, NULL, &token_size); | |
1439 | if (r < 0) { | |
1440 | if (r != -ENOENT) | |
1441 | return log_error_errno(r, "Failed to test system token validity: %m"); | |
1442 | } else { | |
1443 | if (token_size >= sz) { | |
1444 | /* Let's avoid writes if we can, and initialize this only once. */ | |
1445 | log_debug("System token already written, not updating."); | |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | log_debug("Existing system token size (%zu) does not match our expectations (%zu), replacing.", token_size, sz); | |
1450 | } | |
1451 | ||
1452 | r = genuine_random_bytes(buffer, sz, RANDOM_BLOCK); | |
1453 | if (r < 0) | |
1454 | return log_error_errno(r, "Failed to acquire random seed: %m"); | |
1455 | ||
1456 | /* Let's write this variable with an umask in effect, so that unprivileged users can't see the token | |
1457 | * and possibly get identification information or too much insight into the kernel's entropy pool | |
1458 | * state. */ | |
1459 | RUN_WITH_UMASK(0077) { | |
1460 | r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderSystemToken", buffer, sz); | |
1461 | if (r < 0) | |
1462 | return log_error_errno(r, "Failed to set LoaderSystemToken EFI variable: %m"); | |
1463 | } | |
1464 | ||
1465 | log_info("Successfully initialized system token in EFI variable with %zu bytes.", sz); | |
1466 | return 0; | |
1467 | } | |
1468 | ||
fbf45d22 LP |
1469 | static int sync_everything(void) { |
1470 | int ret = 0, k; | |
e0e8d177 | 1471 | |
fbf45d22 LP |
1472 | if (arg_esp_path) { |
1473 | k = syncfs_path(AT_FDCWD, arg_esp_path); | |
1474 | if (k < 0) | |
1475 | ret = log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path); | |
1476 | } | |
e0e8d177 | 1477 | |
fbf45d22 LP |
1478 | if (arg_xbootldr_path) { |
1479 | k = syncfs_path(AT_FDCWD, arg_xbootldr_path); | |
1480 | if (k < 0) | |
1481 | ret = log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path); | |
1482 | } | |
e0e8d177 | 1483 | |
fbf45d22 | 1484 | return ret; |
e0e8d177 LP |
1485 | } |
1486 | ||
2f2c539c | 1487 | static int verb_install(int argc, char *argv[], void *userdata) { |
2f2c539c LP |
1488 | sd_id128_t uuid = SD_ID128_NULL; |
1489 | uint64_t pstart = 0, psize = 0; | |
1490 | uint32_t part = 0; | |
341890de | 1491 | sd_id128_t machine_id; |
2f2c539c LP |
1492 | bool install; |
1493 | int r; | |
1494 | ||
5caa3167 | 1495 | r = acquire_esp(false, &part, &pstart, &psize, &uuid); |
2f2c539c LP |
1496 | if (r < 0) |
1497 | return r; | |
1498 | ||
fbf45d22 LP |
1499 | r = acquire_xbootldr(false, NULL); |
1500 | if (r < 0) | |
1501 | return r; | |
1502 | ||
341890de ZJS |
1503 | r = sd_id128_get_machine(&machine_id); |
1504 | if (r < 0) | |
1505 | return log_error_errno(r, "Failed to get machine id: %m"); | |
1506 | ||
2f2c539c LP |
1507 | install = streq(argv[0], "install"); |
1508 | ||
1509 | RUN_WITH_UMASK(0002) { | |
fbf45d22 LP |
1510 | if (install) { |
1511 | /* Don't create any of these directories when we are just updating. When we update | |
1512 | * we'll drop-in our files (unless there are newer ones already), but we won't create | |
1513 | * the directories for them in the first place. */ | |
e44c3229 LP |
1514 | r = create_subdirs(arg_esp_path, esp_subdirs); |
1515 | if (r < 0) | |
1516 | return r; | |
1517 | ||
1518 | r = create_subdirs(arg_dollar_boot_path(), dollar_boot_subdirs); | |
fbf45d22 LP |
1519 | if (r < 0) |
1520 | return r; | |
1521 | } | |
1522 | ||
1523 | r = install_binaries(arg_esp_path, install); | |
0974a682 | 1524 | if (r < 0) |
d3226d77 | 1525 | return r; |
0974a682 | 1526 | |
2f2c539c | 1527 | if (install) { |
341890de | 1528 | r = install_loader_config(arg_esp_path, machine_id); |
fbf45d22 LP |
1529 | if (r < 0) |
1530 | return r; | |
1531 | ||
e44c3229 LP |
1532 | r = install_machine_id_directory(arg_dollar_boot_path(), machine_id); |
1533 | if (r < 0) | |
1534 | return r; | |
1535 | ||
1536 | r = install_random_seed(arg_esp_path); | |
d3226d77 ZJS |
1537 | if (r < 0) |
1538 | return r; | |
1539 | } | |
2f2c539c | 1540 | } |
0974a682 | 1541 | |
fbf45d22 | 1542 | (void) sync_everything(); |
e0e8d177 | 1543 | |
2f2c539c | 1544 | if (arg_touch_variables) |
fbf45d22 | 1545 | r = install_variables(arg_esp_path, |
2f2c539c LP |
1546 | part, pstart, psize, uuid, |
1547 | "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", | |
1548 | install); | |
7b4d7cc0 | 1549 | |
2f2c539c LP |
1550 | return r; |
1551 | } | |
0974a682 | 1552 | |
2f2c539c | 1553 | static int verb_remove(int argc, char *argv[], void *userdata) { |
e44c3229 | 1554 | sd_id128_t uuid = SD_ID128_NULL, machine_id; |
fbf45d22 | 1555 | int r, q; |
0974a682 | 1556 | |
5caa3167 | 1557 | r = acquire_esp(false, NULL, NULL, NULL, &uuid); |
2f2c539c LP |
1558 | if (r < 0) |
1559 | return r; | |
1560 | ||
fbf45d22 LP |
1561 | r = acquire_xbootldr(false, NULL); |
1562 | if (r < 0) | |
1563 | return r; | |
2f2c539c | 1564 | |
e44c3229 LP |
1565 | r = sd_id128_get_machine(&machine_id); |
1566 | if (r < 0) | |
1567 | return log_error_errno(r, "Failed to get machine id: %m"); | |
1568 | ||
fbf45d22 | 1569 | r = remove_binaries(arg_esp_path); |
e0e8d177 | 1570 | |
e44c3229 | 1571 | q = remove_file(arg_esp_path, "/loader/loader.conf"); |
fbf45d22 LP |
1572 | if (q < 0 && r >= 0) |
1573 | r = q; | |
1574 | ||
e44c3229 | 1575 | q = remove_file(arg_esp_path, "/loader/random-seed"); |
fbf45d22 LP |
1576 | if (q < 0 && r >= 0) |
1577 | r = q; | |
2f2c539c | 1578 | |
e44c3229 | 1579 | q = remove_subdirs(arg_esp_path, esp_subdirs); |
fbf45d22 LP |
1580 | if (q < 0 && r >= 0) |
1581 | r = q; | |
1582 | ||
e44c3229 LP |
1583 | q = remove_subdirs(arg_esp_path, dollar_boot_subdirs); |
1584 | if (q < 0 && r >= 0) | |
1585 | r = q; | |
fbf45d22 | 1586 | |
e44c3229 LP |
1587 | q = remove_machine_id_directory(arg_esp_path, machine_id); |
1588 | if (q < 0 && r >= 0) | |
1589 | r = 1; | |
1590 | ||
1591 | if (arg_xbootldr_path) { | |
1592 | /* Remove the latter two also in the XBOOTLDR partition if it exists */ | |
1593 | q = remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs); | |
1594 | if (q < 0 && r >= 0) | |
1595 | r = q; | |
1596 | ||
1597 | q = remove_machine_id_directory(arg_xbootldr_path, machine_id); | |
fbf45d22 | 1598 | if (q < 0 && r >= 0) |
2f2c539c | 1599 | r = q; |
7b4d7cc0 KS |
1600 | } |
1601 | ||
e44c3229 LP |
1602 | (void) sync_everything(); |
1603 | ||
1604 | if (!arg_touch_variables) | |
1605 | return r; | |
1606 | ||
1607 | q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true); | |
1608 | if (q < 0 && r >= 0) | |
1609 | r = q; | |
1610 | ||
1611 | q = remove_loader_variables(); | |
1612 | if (q < 0 && r >= 0) | |
1613 | r = q; | |
1614 | ||
d3226d77 | 1615 | return r; |
7b4d7cc0 KS |
1616 | } |
1617 | ||
a2aa605d LP |
1618 | static int verb_is_installed(int argc, char *argv[], void *userdata) { |
1619 | _cleanup_free_ char *p = NULL; | |
1620 | int r; | |
1621 | ||
1622 | r = acquire_esp(false, NULL, NULL, NULL, NULL); | |
1623 | if (r < 0) | |
1624 | return r; | |
1625 | ||
1626 | /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could | |
1627 | * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the | |
1628 | * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which | |
1629 | * should be a suitable and very minimal check for a number of reasons: | |
1630 | * | |
1631 | * → The check is architecture independent (i.e. we check if any systemd-boot loader is installed, not a | |
1632 | * specific one.) | |
1633 | * | |
1634 | * → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main | |
1635 | * /EFI/BOOT/BOOT*.EFI fallback binary. | |
1636 | * | |
1637 | * → It specifically checks for systemd-boot, not for other boot loaders (which a check for | |
1638 | * /boot/loader/entries would do). */ | |
1639 | ||
1640 | p = path_join(arg_esp_path, "/EFI/systemd/"); | |
1641 | if (!p) | |
1642 | return log_oom(); | |
1643 | ||
1644 | r = dir_is_empty(p); | |
1645 | if (r > 0 || r == -ENOENT) { | |
1646 | puts("no"); | |
1647 | return EXIT_FAILURE; | |
1648 | } | |
1649 | if (r < 0) | |
1650 | return log_error_errno(r, "Failed to detect whether systemd-boot is installed: %m"); | |
1651 | ||
1652 | puts("yes"); | |
1653 | return EXIT_SUCCESS; | |
1654 | } | |
1655 | ||
d88c96ff LP |
1656 | static int verb_set_default(int argc, char *argv[], void *userdata) { |
1657 | const char *name; | |
1658 | int r; | |
1659 | ||
baaa35ad ZJS |
1660 | if (!is_efi_boot()) |
1661 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1662 | "Not booted with UEFI."); | |
d88c96ff LP |
1663 | |
1664 | if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) { | |
1665 | if (errno == ENOENT) { | |
1666 | log_error_errno(errno, "Not booted with a supported boot loader."); | |
1667 | return -EOPNOTSUPP; | |
1668 | } | |
1669 | ||
1670 | return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", argv[0]); | |
1671 | } | |
1672 | ||
baaa35ad ZJS |
1673 | if (detect_container() > 0) |
1674 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1675 | "'%s' operation not supported in a container.", | |
1676 | argv[0]); | |
d88c96ff | 1677 | |
baaa35ad ZJS |
1678 | if (!arg_touch_variables) |
1679 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1680 | "'%s' operation cannot be combined with --touch-variables=no.", | |
1681 | argv[0]); | |
d88c96ff LP |
1682 | |
1683 | name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot"; | |
1684 | ||
1685 | if (isempty(argv[1])) { | |
1686 | r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0); | |
1687 | if (r < 0 && r != -ENOENT) | |
1688 | return log_error_errno(r, "Failed to remove EFI variale: %m"); | |
1689 | } else { | |
1690 | _cleanup_free_ char16_t *encoded = NULL; | |
1691 | ||
1692 | encoded = utf8_to_utf16(argv[1], strlen(argv[1])); | |
1693 | if (!encoded) | |
1694 | return log_oom(); | |
1695 | ||
1696 | r = efi_set_variable(EFI_VENDOR_LOADER, name, encoded, char16_strlen(encoded) * 2 + 2); | |
1697 | if (r < 0) | |
1698 | return log_error_errno(r, "Failed to update EFI variable: %m"); | |
1699 | } | |
1700 | ||
1701 | return 0; | |
1702 | } | |
1703 | ||
e44c3229 LP |
1704 | static int verb_random_seed(int argc, char *argv[], void *userdata) { |
1705 | int r; | |
1706 | ||
1707 | r = acquire_esp(false, NULL, NULL, NULL, NULL); | |
1708 | if (r < 0) | |
1709 | return r; | |
1710 | ||
1711 | r = install_random_seed(arg_esp_path); | |
1712 | if (r < 0) | |
1713 | return r; | |
1714 | ||
1715 | (void) sync_everything(); | |
1716 | return 0; | |
1717 | } | |
1718 | ||
2536752d | 1719 | static int verb_systemd_efi_options(int argc, char *argv[], void *userdata) { |
4e5aa791 ZJS |
1720 | int r; |
1721 | ||
1722 | if (argc == 1) { | |
1723 | _cleanup_free_ char *line = NULL; | |
1724 | ||
2536752d | 1725 | r = systemd_efi_options_variable(&line); |
4e5aa791 ZJS |
1726 | if (r < 0) |
1727 | return log_error_errno(r, "Failed to query SystemdOptions EFI variable: %m"); | |
1728 | ||
2536752d | 1729 | puts(line); |
4e5aa791 ZJS |
1730 | |
1731 | } else { | |
1732 | r = efi_set_variable_string(EFI_VENDOR_SYSTEMD, "SystemdOptions", argv[1]); | |
1733 | if (r < 0) | |
1734 | return log_error_errno(r, "Failed to set SystemdOptions EFI variable: %m"); | |
1735 | } | |
1736 | ||
1737 | return 0; | |
1738 | } | |
1739 | ||
2f2c539c | 1740 | static int bootctl_main(int argc, char *argv[]) { |
2f2c539c | 1741 | static const Verb verbs[] = { |
2536752d ZJS |
1742 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
1743 | { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status }, | |
1744 | { "install", VERB_ANY, 1, 0, verb_install }, | |
1745 | { "update", VERB_ANY, 1, 0, verb_install }, | |
1746 | { "remove", VERB_ANY, 1, 0, verb_remove }, | |
1747 | { "is-installed", VERB_ANY, 1, 0, verb_is_installed }, | |
1748 | { "list", VERB_ANY, 1, 0, verb_list }, | |
1749 | { "set-default", 2, 2, 0, verb_set_default }, | |
1750 | { "set-oneshot", 2, 2, 0, verb_set_default }, | |
1751 | { "random-seed", VERB_ANY, 1, 0, verb_random_seed }, | |
1752 | { "systemd-efi-options", VERB_ANY, 2, 0, verb_systemd_efi_options }, | |
2f2c539c LP |
1753 | {} |
1754 | }; | |
1755 | ||
1756 | return dispatch_verb(argc, argv, verbs, NULL); | |
1757 | } | |
1758 | ||
608f8ec9 | 1759 | static int run(int argc, char *argv[]) { |
601185b4 | 1760 | int r; |
7b4d7cc0 KS |
1761 | |
1762 | log_parse_environment(); | |
1763 | log_open(); | |
1764 | ||
341890de | 1765 | /* If we run in a container, automatically turn off EFI file system access */ |
2f2c539c LP |
1766 | if (detect_container() > 0) |
1767 | arg_touch_variables = false; | |
1768 | ||
7b4d7cc0 | 1769 | r = parse_argv(argc, argv); |
601185b4 | 1770 | if (r <= 0) |
608f8ec9 | 1771 | return r; |
57db6f18 | 1772 | |
608f8ec9 | 1773 | return bootctl_main(argc, argv); |
7b4d7cc0 | 1774 | } |
608f8ec9 YW |
1775 | |
1776 | DEFINE_MAIN_FUNCTION(run); |