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