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