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