]>
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" |
9dae4c8a | 28 | #include "escape.h" |
3ffd4af2 | 29 | #include "fd-util.h" |
0d39fa9c | 30 | #include "fileio.h" |
175d308c | 31 | #include "fs-util.h" |
8752c575 | 32 | #include "locale-util.h" |
608f8ec9 | 33 | #include "main-func.h" |
57db6f18 | 34 | #include "pager.h" |
2f2c539c | 35 | #include "parse-util.h" |
294bf0c3 | 36 | #include "pretty-print.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; | |
104 | return 0; | |
105 | } | |
106 | if (r < 0) | |
107 | return r; | |
108 | ||
109 | free_and_replace(arg_xbootldr_path, np); | |
110 | log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path); | |
111 | ||
112 | return 1; | |
7b4d7cc0 KS |
113 | } |
114 | ||
e7dd673d | 115 | /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */ |
d3226d77 | 116 | static int get_file_version(int fd, char **v) { |
0974a682 KS |
117 | struct stat st; |
118 | char *buf; | |
119 | const char *s, *e; | |
120 | char *x = NULL; | |
121 | int r = 0; | |
7b4d7cc0 | 122 | |
d3226d77 | 123 | assert(fd >= 0); |
0974a682 | 124 | assert(v); |
7b4d7cc0 | 125 | |
d3226d77 | 126 | if (fstat(fd, &st) < 0) |
db6d9fae | 127 | return log_error_errno(errno, "Failed to stat EFI binary: %m"); |
7b4d7cc0 | 128 | |
c4ba5b51 LP |
129 | r = stat_verify_regular(&st); |
130 | if (r < 0) | |
d90f2add | 131 | return log_error_errno(r, "EFI binary is not a regular file: %m"); |
c4ba5b51 | 132 | |
db6d9fae LP |
133 | if (st.st_size < 27) { |
134 | *v = NULL; | |
0974a682 | 135 | return 0; |
db6d9fae | 136 | } |
eb9da376 | 137 | |
d3226d77 | 138 | buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
0974a682 | 139 | if (buf == MAP_FAILED) |
db6d9fae | 140 | return log_error_errno(errno, "Failed to memory map EFI binary: %m"); |
7b4d7cc0 | 141 | |
0974a682 KS |
142 | s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17); |
143 | if (!s) | |
144 | goto finish; | |
145 | s += 17; | |
7b4d7cc0 | 146 | |
0974a682 KS |
147 | e = memmem(s, st.st_size - (s - buf), " ####", 5); |
148 | if (!e || e - s < 3) { | |
78d5d4ed | 149 | r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Malformed version string."); |
0974a682 KS |
150 | goto finish; |
151 | } | |
7b4d7cc0 | 152 | |
0974a682 KS |
153 | x = strndup(s, e - s); |
154 | if (!x) { | |
d3226d77 | 155 | r = log_oom(); |
0974a682 KS |
156 | goto finish; |
157 | } | |
158 | r = 1; | |
7b4d7cc0 | 159 | |
0974a682 | 160 | finish: |
db6d9fae | 161 | (void) munmap(buf, st.st_size); |
0974a682 KS |
162 | *v = x; |
163 | return r; | |
164 | } | |
7b4d7cc0 | 165 | |
0974a682 | 166 | static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { |
d3226d77 | 167 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 | 168 | struct dirent *de; |
270384b2 | 169 | const char *p; |
fbf45d22 | 170 | int c = 0, r; |
fbf45d22 LP |
171 | |
172 | assert(esp_path); | |
173 | assert(path); | |
0974a682 | 174 | |
270384b2 | 175 | p = prefix_roota(esp_path, path); |
0974a682 KS |
176 | d = opendir(p); |
177 | if (!d) { | |
d3226d77 ZJS |
178 | if (errno == ENOENT) |
179 | return 0; | |
7b4d7cc0 | 180 | |
d3226d77 | 181 | return log_error_errno(errno, "Failed to read \"%s\": %m", p); |
0974a682 KS |
182 | } |
183 | ||
e41256dc | 184 | FOREACH_DIRENT(de, d, break) { |
d3226d77 | 185 | _cleanup_free_ char *v = NULL; |
fbf45d22 | 186 | _cleanup_close_ int fd = -1; |
0974a682 | 187 | |
d3226d77 | 188 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
189 | continue; |
190 | ||
d3226d77 | 191 | if (prefix && !startswith_no_case(de->d_name, prefix)) |
0974a682 KS |
192 | continue; |
193 | ||
d3226d77 ZJS |
194 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
195 | if (fd < 0) | |
196 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); | |
0974a682 | 197 | |
d3226d77 | 198 | r = get_file_version(fd, &v); |
0974a682 | 199 | if (r < 0) |
d3226d77 | 200 | return r; |
0974a682 | 201 | if (r > 0) |
9a6f746f | 202 | printf(" File: %s/%s/%s (%s%s%s)\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name, ansi_highlight(), v, ansi_normal()); |
0974a682 | 203 | else |
9a6f746f | 204 | printf(" File: %s/%s/%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path, de->d_name); |
fbf45d22 | 205 | |
0974a682 | 206 | c++; |
0974a682 KS |
207 | } |
208 | ||
d3226d77 | 209 | return c; |
7b4d7cc0 KS |
210 | } |
211 | ||
0974a682 KS |
212 | static int status_binaries(const char *esp_path, sd_id128_t partition) { |
213 | int r; | |
214 | ||
7fd66f3c | 215 | printf("Available Boot Loaders on ESP:\n"); |
0974a682 | 216 | |
cd2d4c7f YW |
217 | if (!esp_path) { |
218 | printf(" ESP: Cannot find or access mount point of ESP.\n\n"); | |
219 | return -ENOENT; | |
220 | } | |
221 | ||
222 | printf(" ESP: %s", esp_path); | |
223 | if (!sd_id128_is_null(partition)) | |
bd44566c | 224 | printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", SD_ID128_FORMAT_VAL(partition)); |
cd2d4c7f | 225 | printf("\n"); |
0974a682 KS |
226 | |
227 | r = enumerate_binaries(esp_path, "EFI/systemd", NULL); | |
fbf45d22 | 228 | if (r < 0) |
882b3bd6 | 229 | goto finish; |
0974a682 | 230 | if (r == 0) |
48184e43 | 231 | log_info("systemd-boot not installed in ESP."); |
0974a682 | 232 | |
00f69504 | 233 | r = enumerate_binaries(esp_path, "EFI/BOOT", "boot"); |
fbf45d22 | 234 | if (r < 0) |
882b3bd6 | 235 | goto finish; |
0974a682 | 236 | if (r == 0) |
48184e43 | 237 | log_info("No default/fallback boot loader installed in ESP."); |
0974a682 | 238 | |
882b3bd6 | 239 | r = 0; |
c11ae0ba | 240 | |
882b3bd6 LP |
241 | finish: |
242 | printf("\n"); | |
243 | return r; | |
0974a682 KS |
244 | } |
245 | ||
246 | static int print_efi_option(uint16_t id, bool in_order) { | |
7cb0f263 TA |
247 | _cleanup_free_ char *title = NULL; |
248 | _cleanup_free_ char *path = NULL; | |
0974a682 KS |
249 | sd_id128_t partition; |
250 | bool active; | |
251 | int r = 0; | |
252 | ||
253 | r = efi_get_boot_option(id, &title, &partition, &path, &active); | |
254 | if (r < 0) | |
7cb0f263 | 255 | return r; |
7b4d7cc0 | 256 | |
0974a682 | 257 | /* print only configured entries with partition information */ |
3bbaff3e | 258 | if (!path || sd_id128_is_null(partition)) |
0974a682 KS |
259 | return 0; |
260 | ||
261 | efi_tilt_backslashes(path); | |
262 | ||
ba857253 | 263 | printf(" Title: %s%s%s\n", ansi_highlight(), strna(title), ansi_normal()); |
0974a682 KS |
264 | printf(" ID: 0x%04X\n", id); |
265 | printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : ""); | |
bd44566c ZJS |
266 | printf(" Partition: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n", |
267 | SD_ID128_FORMAT_VAL(partition)); | |
9a6f746f | 268 | printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), path); |
0974a682 KS |
269 | printf("\n"); |
270 | ||
7cb0f263 | 271 | return 0; |
0974a682 KS |
272 | } |
273 | ||
274 | static int status_variables(void) { | |
d3226d77 | 275 | _cleanup_free_ uint16_t *options = NULL, *order = NULL; |
a36b411e | 276 | int n_options, n_order, i; |
0974a682 | 277 | |
0974a682 | 278 | n_options = efi_get_boot_options(&options); |
d3226d77 | 279 | if (n_options == -ENOENT) |
f939cff7 LP |
280 | return log_error_errno(n_options, |
281 | "Failed to access EFI variables, efivarfs" | |
d3226d77 | 282 | " needs to be available at /sys/firmware/efi/efivars/."); |
f939cff7 | 283 | if (n_options < 0) |
d3226d77 | 284 | return log_error_errno(n_options, "Failed to read EFI boot entries: %m"); |
0974a682 | 285 | |
0974a682 | 286 | n_order = efi_get_boot_order(&order); |
d3226d77 | 287 | if (n_order == -ENOENT) |
0974a682 | 288 | n_order = 0; |
d3226d77 | 289 | else if (n_order < 0) |
7709ef3a | 290 | return log_error_errno(n_order, "Failed to read EFI boot order: %m"); |
0974a682 KS |
291 | |
292 | /* print entries in BootOrder first */ | |
7fd66f3c | 293 | printf("Boot Loaders Listed in EFI Variables:\n"); |
0974a682 KS |
294 | for (i = 0; i < n_order; i++) |
295 | print_efi_option(order[i], true); | |
296 | ||
297 | /* print remaining entries */ | |
298 | for (i = 0; i < n_options; i++) { | |
299 | int j; | |
0974a682 KS |
300 | |
301 | for (j = 0; j < n_order; j++) | |
d3226d77 | 302 | if (options[i] == order[j]) |
a908cf0a | 303 | goto next_option; |
0974a682 KS |
304 | |
305 | print_efi_option(options[i], false); | |
a908cf0a MM |
306 | |
307 | next_option: | |
308 | continue; | |
0974a682 KS |
309 | } |
310 | ||
d3226d77 | 311 | return 0; |
0974a682 KS |
312 | } |
313 | ||
44e6a5ef ZJS |
314 | static int boot_entry_file_check(const char *root, const char *p) { |
315 | _cleanup_free_ char *path; | |
316 | ||
317 | path = path_join(root, p); | |
318 | if (!path) | |
319 | return log_oom(); | |
320 | ||
321 | if (access(path, F_OK) < 0) | |
322 | return -errno; | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
d3eb6072 | 327 | static void boot_entry_file_list(const char *field, const char *root, const char *p, int *ret_status) { |
44e6a5ef ZJS |
328 | int status = boot_entry_file_check(root, p); |
329 | ||
405b104d | 330 | printf("%13s%s ", strempty(field), field ? ":" : " "); |
44e6a5ef ZJS |
331 | if (status < 0) { |
332 | errno = -status; | |
333 | printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal()); | |
334 | } else | |
335 | printf("%s\n", p); | |
d3eb6072 ZJS |
336 | |
337 | if (*ret_status == 0 && status < 0) | |
338 | *ret_status = status; | |
44e6a5ef ZJS |
339 | } |
340 | ||
20a28174 | 341 | static int boot_entry_show(const BootEntry *e, bool show_as_default) { |
d3eb6072 ZJS |
342 | int status = 0; |
343 | ||
344 | /* Returns 0 on success, negative on processing error, and positive if something is wrong with the | |
345 | boot entry itself. */ | |
346 | ||
20a28174 LP |
347 | assert(e); |
348 | ||
ce4c4f81 ZJS |
349 | printf(" title: %s%s%s" "%s%s%s\n", |
350 | ansi_highlight(), boot_entry_title(e), ansi_normal(), | |
351 | ansi_highlight_green(), show_as_default ? " (default)" : "", ansi_normal()); | |
20a28174 LP |
352 | |
353 | if (e->id) | |
354 | printf(" id: %s\n", e->id); | |
cce9457c ZJS |
355 | if (e->path) { |
356 | _cleanup_free_ char *link = NULL; | |
357 | ||
358 | /* Let's urlify the link to make it easy to view in an editor, but only if it is a text | |
359 | * file. Unified images are binary ELFs, and EFI variables are not pure text either. */ | |
360 | if (e->type == BOOT_ENTRY_CONF) | |
361 | (void) terminal_urlify_path(e->path, NULL, &link); | |
362 | ||
363 | printf(" source: %s\n", link ?: e->path); | |
364 | } | |
20a28174 LP |
365 | if (e->version) |
366 | printf(" version: %s\n", e->version); | |
367 | if (e->machine_id) | |
368 | printf(" machine-id: %s\n", e->machine_id); | |
369 | if (e->architecture) | |
370 | printf(" architecture: %s\n", e->architecture); | |
371 | if (e->kernel) | |
d3eb6072 | 372 | boot_entry_file_list("linux", e->root, e->kernel, &status); |
20a28174 | 373 | |
44e6a5ef ZJS |
374 | char **s; |
375 | STRV_FOREACH(s, e->initrd) | |
376 | boot_entry_file_list(s == e->initrd ? "initrd" : NULL, | |
377 | e->root, | |
d3eb6072 ZJS |
378 | *s, |
379 | &status); | |
20a28174 | 380 | if (!strv_isempty(e->options)) { |
9dae4c8a ZJS |
381 | _cleanup_free_ char *t = NULL, *t2 = NULL; |
382 | _cleanup_strv_free_ char **ts = NULL; | |
20a28174 LP |
383 | |
384 | t = strv_join(e->options, " "); | |
385 | if (!t) | |
386 | return log_oom(); | |
387 | ||
9dae4c8a ZJS |
388 | ts = strv_split_newlines(t); |
389 | if (!ts) | |
390 | return log_oom(); | |
391 | ||
392 | t2 = strv_join(ts, "\n "); | |
393 | if (!t2) | |
394 | return log_oom(); | |
395 | ||
9dae4c8a | 396 | printf(" options: %s\n", t2); |
20a28174 LP |
397 | } |
398 | if (e->device_tree) | |
d3eb6072 | 399 | boot_entry_file_list("devicetree", e->root, e->device_tree, &status); |
20a28174 | 400 | |
d3eb6072 | 401 | return -status; |
20a28174 LP |
402 | } |
403 | ||
81fed855 LP |
404 | static int status_entries( |
405 | const char *esp_path, | |
406 | sd_id128_t esp_partition_uuid, | |
407 | const char *xbootldr_path, | |
408 | sd_id128_t xbootldr_partition_uuid) { | |
409 | ||
7e87c7d9 | 410 | _cleanup_(boot_config_free) BootConfig config = {}; |
81fed855 LP |
411 | sd_id128_t dollar_boot_partition_uuid; |
412 | const char *dollar_boot_path; | |
a099e035 | 413 | int r; |
7e87c7d9 | 414 | |
81fed855 LP |
415 | assert(esp_path || xbootldr_path); |
416 | ||
417 | if (xbootldr_path) { | |
418 | dollar_boot_path = xbootldr_path; | |
419 | dollar_boot_partition_uuid = xbootldr_partition_uuid; | |
420 | } else { | |
421 | dollar_boot_path = esp_path; | |
422 | dollar_boot_partition_uuid = esp_partition_uuid; | |
423 | } | |
424 | ||
425 | printf("Boot Loader Entries:\n" | |
426 | " $BOOT: %s", dollar_boot_path); | |
427 | if (!sd_id128_is_null(dollar_boot_partition_uuid)) | |
bd44566c ZJS |
428 | printf(" (/dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR ")", |
429 | SD_ID128_FORMAT_VAL(dollar_boot_partition_uuid)); | |
81fed855 LP |
430 | printf("\n\n"); |
431 | ||
fbf45d22 | 432 | r = boot_entries_load_config(esp_path, xbootldr_path, &config); |
7e87c7d9 | 433 | if (r < 0) |
21f7a622 | 434 | return r; |
7e87c7d9 ZJS |
435 | |
436 | if (config.default_entry < 0) | |
a099e035 | 437 | printf("%zu entries, no entry could be determined as default.\n", config.n_entries); |
7e87c7d9 | 438 | else { |
7fd66f3c | 439 | printf("Default Boot Loader Entry:\n"); |
a099e035 | 440 | |
d3eb6072 ZJS |
441 | r = boot_entry_show(config.entries + config.default_entry, false); |
442 | if (r > 0) | |
443 | /* < 0 is already logged by the function itself, let's just emit an extra warning if | |
444 | the default entry is broken */ | |
445 | printf("\nWARNING: default boot entry is broken\n"); | |
7e87c7d9 ZJS |
446 | } |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
0974a682 KS |
451 | static int compare_product(const char *a, const char *b) { |
452 | size_t x, y; | |
453 | ||
454 | assert(a); | |
455 | assert(b); | |
456 | ||
457 | x = strcspn(a, " "); | |
458 | y = strcspn(b, " "); | |
459 | if (x != y) | |
460 | return x < y ? -1 : x > y ? 1 : 0; | |
461 | ||
462 | return strncmp(a, b, x); | |
463 | } | |
464 | ||
465 | static int compare_version(const char *a, const char *b) { | |
466 | assert(a); | |
467 | assert(b); | |
468 | ||
469 | a += strcspn(a, " "); | |
470 | a += strspn(a, " "); | |
471 | b += strcspn(b, " "); | |
472 | b += strspn(b, " "); | |
473 | ||
474 | return strverscmp(a, b); | |
475 | } | |
476 | ||
175d308c | 477 | static int version_check(int fd_from, const char *from, int fd_to, const char *to) { |
d3226d77 | 478 | _cleanup_free_ char *a = NULL, *b = NULL; |
0974a682 KS |
479 | int r; |
480 | ||
175d308c | 481 | assert(fd_from >= 0); |
0974a682 | 482 | assert(from); |
175d308c | 483 | assert(fd_to >= 0); |
0974a682 KS |
484 | assert(to); |
485 | ||
175d308c | 486 | r = get_file_version(fd_from, &a); |
0974a682 | 487 | if (r < 0) |
d3226d77 | 488 | return r; |
baaa35ad ZJS |
489 | if (r == 0) |
490 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
491 | "Source file \"%s\" does not carry version information!", | |
492 | from); | |
0974a682 | 493 | |
175d308c | 494 | r = get_file_version(fd_to, &b); |
0974a682 | 495 | if (r < 0) |
d3226d77 | 496 | return r; |
baaa35ad ZJS |
497 | if (r == 0 || compare_product(a, b) != 0) |
498 | return log_notice_errno(SYNTHETIC_ERRNO(EEXIST), | |
499 | "Skipping \"%s\", since it's owned by another boot loader.", | |
500 | to); | |
0974a682 | 501 | |
78d5d4ed LP |
502 | if (compare_version(a, b) < 0) |
503 | return log_warning_errno(SYNTHETIC_ERRNO(ESTALE), "Skipping \"%s\", since a newer boot loader version exists already.", to); | |
0974a682 | 504 | |
d3226d77 | 505 | return 0; |
0974a682 KS |
506 | } |
507 | ||
175d308c LP |
508 | static int copy_file_with_version_check(const char *from, const char *to, bool force) { |
509 | _cleanup_close_ int fd_from = -1, fd_to = -1; | |
510 | _cleanup_free_ char *t = NULL; | |
0974a682 | 511 | int r; |
0974a682 | 512 | |
175d308c LP |
513 | fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
514 | if (fd_from < 0) | |
d3226d77 | 515 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from); |
0974a682 KS |
516 | |
517 | if (!force) { | |
175d308c LP |
518 | fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
519 | if (fd_to < 0) { | |
520 | if (errno != -ENOENT) | |
521 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to); | |
522 | } else { | |
523 | r = version_check(fd_from, from, fd_to, to); | |
524 | if (r < 0) | |
525 | return r; | |
526 | ||
527 | if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1) | |
6719ca72 | 528 | return log_error_errno(errno, "Failed to seek in \"%s\": %m", from); |
175d308c LP |
529 | |
530 | fd_to = safe_close(fd_to); | |
0974a682 | 531 | } |
175d308c | 532 | } |
d3226d77 | 533 | |
175d308c LP |
534 | r = tempfn_random(to, NULL, &t); |
535 | if (r < 0) | |
536 | return log_oom(); | |
0974a682 | 537 | |
175d308c LP |
538 | RUN_WITH_UMASK(0000) { |
539 | fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644); | |
540 | if (fd_to < 0) | |
541 | return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t); | |
0974a682 KS |
542 | } |
543 | ||
175d308c | 544 | r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK); |
0974a682 | 545 | if (r < 0) { |
0675e94a AJ |
546 | (void) unlink(t); |
547 | return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
0974a682 KS |
548 | } |
549 | ||
adc6f43b | 550 | (void) copy_times(fd_from, fd_to, 0); |
0974a682 | 551 | |
8ac2f74f | 552 | if (fsync(fd_to) < 0) { |
0675e94a AJ |
553 | (void) unlink_noerrno(t); |
554 | return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
555 | } | |
556 | ||
8ac2f74f LP |
557 | (void) fsync_directory_of_file(fd_to); |
558 | ||
b1c05b98 | 559 | if (renameat(AT_FDCWD, t, AT_FDCWD, to) < 0) { |
175d308c LP |
560 | (void) unlink_noerrno(t); |
561 | return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to); | |
0974a682 KS |
562 | } |
563 | ||
d3226d77 | 564 | log_info("Copied \"%s\" to \"%s\".", from, to); |
0974a682 | 565 | |
175d308c | 566 | return 0; |
0974a682 KS |
567 | } |
568 | ||
0974a682 | 569 | static int mkdir_one(const char *prefix, const char *suffix) { |
e2600fd5 | 570 | _cleanup_free_ char *p = NULL; |
0974a682 | 571 | |
e2600fd5 | 572 | p = path_join(prefix, suffix); |
0974a682 | 573 | if (mkdir(p, 0700) < 0) { |
d3226d77 ZJS |
574 | if (errno != EEXIST) |
575 | return log_error_errno(errno, "Failed to create \"%s\": %m", p); | |
0974a682 | 576 | } else |
d3226d77 | 577 | log_info("Created \"%s\".", p); |
7b4d7cc0 | 578 | |
0974a682 KS |
579 | return 0; |
580 | } | |
581 | ||
fbf45d22 | 582 | static const char *const esp_subdirs[] = { |
d3226d77 ZJS |
583 | "EFI", |
584 | "EFI/systemd", | |
00f69504 | 585 | "EFI/BOOT", |
d3226d77 | 586 | "loader", |
fbf45d22 LP |
587 | /* Note that "/loader/entries" is not listed here, since it should be placed in $BOOT, which might |
588 | * not necessarily be the ESP */ | |
9ee051b9 | 589 | NULL |
d3226d77 ZJS |
590 | }; |
591 | ||
fbf45d22 LP |
592 | static int create_esp_subdirs(const char *esp_path) { |
593 | const char *const *i; | |
0974a682 KS |
594 | int r; |
595 | ||
fbf45d22 | 596 | STRV_FOREACH(i, esp_subdirs) { |
181ccb43 | 597 | r = mkdir_one(esp_path, *i); |
d3226d77 ZJS |
598 | if (r < 0) |
599 | return r; | |
600 | } | |
7b4d7cc0 | 601 | |
7b4d7cc0 | 602 | return 0; |
7b4d7cc0 KS |
603 | } |
604 | ||
0974a682 | 605 | static int copy_one_file(const char *esp_path, const char *name, bool force) { |
5509f912 | 606 | const char *e; |
d3226d77 | 607 | char *p, *q; |
0974a682 KS |
608 | int r; |
609 | ||
d3226d77 ZJS |
610 | p = strjoina(BOOTLIBDIR "/", name); |
611 | q = strjoina(esp_path, "/EFI/systemd/", name); | |
175d308c | 612 | r = copy_file_with_version_check(p, q, force); |
0974a682 | 613 | |
5509f912 LP |
614 | e = startswith(name, "systemd-boot"); |
615 | if (e) { | |
0974a682 | 616 | int k; |
d3226d77 | 617 | char *v; |
0974a682 KS |
618 | |
619 | /* Create the EFI default boot loader name (specified for removable devices) */ | |
5509f912 | 620 | v = strjoina(esp_path, "/EFI/BOOT/BOOT", e); |
846b8fc3 | 621 | ascii_strupper(strrchr(v, '/') + 1); |
0974a682 | 622 | |
175d308c | 623 | k = copy_file_with_version_check(p, v, force); |
0974a682 | 624 | if (k < 0 && r == 0) |
d3226d77 | 625 | r = k; |
0974a682 KS |
626 | } |
627 | ||
628 | return r; | |
7b4d7cc0 KS |
629 | } |
630 | ||
0974a682 KS |
631 | static int install_binaries(const char *esp_path, bool force) { |
632 | struct dirent *de; | |
d3226d77 | 633 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 KS |
634 | int r = 0; |
635 | ||
e7dd673d | 636 | d = opendir(BOOTLIBDIR); |
d3226d77 ZJS |
637 | if (!d) |
638 | return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m"); | |
0974a682 | 639 | |
0d73a816 | 640 | FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) { |
0974a682 KS |
641 | int k; |
642 | ||
d3226d77 | 643 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
644 | continue; |
645 | ||
646 | k = copy_one_file(esp_path, de->d_name, force); | |
647 | if (k < 0 && r == 0) | |
648 | r = k; | |
649 | } | |
650 | ||
0974a682 | 651 | return r; |
7b4d7cc0 KS |
652 | } |
653 | ||
0974a682 | 654 | static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) { |
d3226d77 | 655 | _cleanup_free_ char *opath = NULL; |
0974a682 | 656 | sd_id128_t ouuid; |
d3226d77 | 657 | int r; |
7b4d7cc0 | 658 | |
d3226d77 ZJS |
659 | r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL); |
660 | if (r < 0) | |
0974a682 KS |
661 | return false; |
662 | if (!sd_id128_equal(uuid, ouuid)) | |
d3226d77 | 663 | return false; |
0974a682 | 664 | if (!streq_ptr(path, opath)) |
d3226d77 | 665 | return false; |
0974a682 | 666 | |
d3226d77 | 667 | return true; |
0974a682 KS |
668 | } |
669 | ||
670 | static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) { | |
d3226d77 ZJS |
671 | _cleanup_free_ uint16_t *options = NULL; |
672 | int n, i; | |
0974a682 | 673 | |
d3226d77 ZJS |
674 | n = efi_get_boot_options(&options); |
675 | if (n < 0) | |
676 | return n; | |
0974a682 | 677 | |
e7dd673d | 678 | /* find already existing systemd-boot entry */ |
d3226d77 | 679 | for (i = 0; i < n; i++) |
0974a682 | 680 | if (same_entry(options[i], uuid, path)) { |
d3226d77 ZJS |
681 | *id = options[i]; |
682 | return 1; | |
0974a682 KS |
683 | } |
684 | ||
685 | /* find free slot in the sorted BootXXXX variable list */ | |
d3226d77 | 686 | for (i = 0; i < n; i++) |
0974a682 | 687 | if (i != options[i]) { |
d3226d77 ZJS |
688 | *id = i; |
689 | return 1; | |
0974a682 KS |
690 | } |
691 | ||
692 | /* use the next one */ | |
693 | if (i == 0xffff) | |
694 | return -ENOSPC; | |
d3226d77 ZJS |
695 | *id = i; |
696 | return 0; | |
0974a682 KS |
697 | } |
698 | ||
699 | static int insert_into_order(uint16_t slot, bool first) { | |
d3226d77 ZJS |
700 | _cleanup_free_ uint16_t *order = NULL; |
701 | uint16_t *t; | |
702 | int n, i; | |
0974a682 | 703 | |
d3226d77 ZJS |
704 | n = efi_get_boot_order(&order); |
705 | if (n <= 0) | |
0974a682 | 706 | /* no entry, add us */ |
d3226d77 | 707 | return efi_set_boot_order(&slot, 1); |
0974a682 KS |
708 | |
709 | /* are we the first and only one? */ | |
d3226d77 ZJS |
710 | if (n == 1 && order[0] == slot) |
711 | return 0; | |
0974a682 KS |
712 | |
713 | /* are we already in the boot order? */ | |
d3226d77 | 714 | for (i = 0; i < n; i++) { |
0974a682 KS |
715 | if (order[i] != slot) |
716 | continue; | |
717 | ||
718 | /* we do not require to be the first one, all is fine */ | |
719 | if (!first) | |
d3226d77 | 720 | return 0; |
0974a682 KS |
721 | |
722 | /* move us to the first slot */ | |
d3226d77 | 723 | memmove(order + 1, order, i * sizeof(uint16_t)); |
0974a682 | 724 | order[0] = slot; |
d3226d77 | 725 | return efi_set_boot_order(order, n); |
0974a682 KS |
726 | } |
727 | ||
728 | /* extend array */ | |
a7798cd8 | 729 | t = reallocarray(order, n + 1, sizeof(uint16_t)); |
d3226d77 ZJS |
730 | if (!t) |
731 | return -ENOMEM; | |
732 | order = t; | |
0974a682 KS |
733 | |
734 | /* add us to the top or end of the list */ | |
735 | if (first) { | |
d3226d77 | 736 | memmove(order + 1, order, n * sizeof(uint16_t)); |
0974a682 KS |
737 | order[0] = slot; |
738 | } else | |
d3226d77 | 739 | order[n] = slot; |
0974a682 | 740 | |
d3226d77 | 741 | return efi_set_boot_order(order, n + 1); |
0974a682 KS |
742 | } |
743 | ||
744 | static int remove_from_order(uint16_t slot) { | |
7cb0f263 | 745 | _cleanup_free_ uint16_t *order = NULL; |
d3226d77 | 746 | int n, i; |
0974a682 | 747 | |
d3226d77 ZJS |
748 | n = efi_get_boot_order(&order); |
749 | if (n <= 0) | |
750 | return n; | |
0974a682 | 751 | |
d3226d77 | 752 | for (i = 0; i < n; i++) { |
0974a682 KS |
753 | if (order[i] != slot) |
754 | continue; | |
755 | ||
d3226d77 ZJS |
756 | if (i + 1 < n) |
757 | memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t)); | |
758 | return efi_set_boot_order(order, n - 1); | |
0974a682 KS |
759 | } |
760 | ||
d3226d77 | 761 | return 0; |
0974a682 KS |
762 | } |
763 | ||
764 | static int install_variables(const char *esp_path, | |
765 | uint32_t part, uint64_t pstart, uint64_t psize, | |
766 | sd_id128_t uuid, const char *path, | |
767 | bool first) { | |
270384b2 | 768 | const char *p; |
0974a682 KS |
769 | uint16_t slot; |
770 | int r; | |
771 | ||
772 | if (!is_efi_boot()) { | |
d3226d77 | 773 | log_warning("Not booted with EFI, skipping EFI variable setup."); |
0974a682 KS |
774 | return 0; |
775 | } | |
776 | ||
270384b2 | 777 | p = prefix_roota(esp_path, path); |
0974a682 KS |
778 | if (access(p, F_OK) < 0) { |
779 | if (errno == ENOENT) | |
d3226d77 | 780 | return 0; |
f939cff7 LP |
781 | |
782 | return log_error_errno(errno, "Cannot access \"%s\": %m", p); | |
0974a682 | 783 | } |
7b4d7cc0 | 784 | |
0974a682 | 785 | r = find_slot(uuid, path, &slot); |
d3226d77 ZJS |
786 | if (r < 0) |
787 | return log_error_errno(r, | |
788 | r == -ENOENT ? | |
789 | "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" : | |
790 | "Failed to determine current boot order: %m"); | |
7b4d7cc0 | 791 | |
181ccb43 | 792 | if (first || r == 0) { |
0974a682 KS |
793 | r = efi_add_boot_option(slot, "Linux Boot Manager", |
794 | part, pstart, psize, | |
795 | uuid, path); | |
d3226d77 ZJS |
796 | if (r < 0) |
797 | return log_error_errno(r, "Failed to create EFI Boot variable entry: %m"); | |
0974a682 | 798 | |
d3226d77 ZJS |
799 | log_info("Created EFI boot entry \"Linux Boot Manager\"."); |
800 | } | |
0974a682 | 801 | |
d3226d77 | 802 | return insert_into_order(slot, first); |
0974a682 KS |
803 | } |
804 | ||
805 | static int remove_boot_efi(const char *esp_path) { | |
d3226d77 | 806 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 | 807 | struct dirent *de; |
270384b2 | 808 | const char *p; |
d3226d77 | 809 | int r, c = 0; |
0974a682 | 810 | |
270384b2 | 811 | p = prefix_roota(esp_path, "/EFI/BOOT"); |
0974a682 KS |
812 | d = opendir(p); |
813 | if (!d) { | |
d3226d77 ZJS |
814 | if (errno == ENOENT) |
815 | return 0; | |
0974a682 | 816 | |
d3226d77 | 817 | return log_error_errno(errno, "Failed to open directory \"%s\": %m", p); |
0974a682 KS |
818 | } |
819 | ||
e41256dc | 820 | FOREACH_DIRENT(de, d, break) { |
d3226d77 ZJS |
821 | _cleanup_close_ int fd = -1; |
822 | _cleanup_free_ char *v = NULL; | |
0974a682 | 823 | |
d3226d77 | 824 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
825 | continue; |
826 | ||
b7536c45 | 827 | if (!startswith_no_case(de->d_name, "boot")) |
0974a682 KS |
828 | continue; |
829 | ||
d3226d77 | 830 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
dd114e11 | 831 | if (fd < 0) |
d3226d77 | 832 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); |
0974a682 | 833 | |
d3226d77 | 834 | r = get_file_version(fd, &v); |
0974a682 | 835 | if (r < 0) |
d3226d77 ZJS |
836 | return r; |
837 | if (r > 0 && startswith(v, "systemd-boot ")) { | |
838 | r = unlinkat(dirfd(d), de->d_name, 0); | |
839 | if (r < 0) | |
840 | return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name); | |
841 | ||
a592ab6a | 842 | log_info("Removed \"%s/%s\".", p, de->d_name); |
0974a682 KS |
843 | } |
844 | ||
845 | c++; | |
0974a682 KS |
846 | } |
847 | ||
d3226d77 | 848 | return c; |
0974a682 KS |
849 | } |
850 | ||
851 | static int rmdir_one(const char *prefix, const char *suffix) { | |
270384b2 | 852 | const char *p; |
7b4d7cc0 | 853 | |
270384b2 | 854 | p = prefix_roota(prefix, suffix); |
0974a682 | 855 | if (rmdir(p) < 0) { |
fbf45d22 LP |
856 | bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY); |
857 | ||
858 | log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno, | |
859 | "Failed to remove directory \"%s\": %m", p); | |
860 | if (!ignore) | |
861 | return -errno; | |
7b4d7cc0 | 862 | } else |
d3226d77 | 863 | log_info("Removed \"%s\".", p); |
7b4d7cc0 | 864 | |
0974a682 | 865 | return 0; |
7b4d7cc0 KS |
866 | } |
867 | ||
fbf45d22 LP |
868 | static int remove_esp_subdirs(const char *esp_path) { |
869 | size_t i; | |
870 | int r = 0; | |
871 | ||
872 | for (i = ELEMENTSOF(esp_subdirs)-1; i > 0; i--) { | |
873 | int q; | |
874 | ||
875 | q = rmdir_one(esp_path, esp_subdirs[i-1]); | |
876 | if (q < 0 && r >= 0) | |
877 | r = q; | |
878 | } | |
879 | ||
880 | return r; | |
881 | } | |
882 | ||
0974a682 | 883 | static int remove_binaries(const char *esp_path) { |
270384b2 | 884 | const char *p; |
0974a682 KS |
885 | int r, q; |
886 | ||
270384b2 | 887 | p = prefix_roota(esp_path, "/EFI/systemd"); |
c6878637 | 888 | r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); |
0974a682 KS |
889 | |
890 | q = remove_boot_efi(esp_path); | |
891 | if (q < 0 && r == 0) | |
892 | r = q; | |
893 | ||
0974a682 KS |
894 | return r; |
895 | } | |
896 | ||
fbf45d22 LP |
897 | static int remove_loader_config(const char *esp_path) { |
898 | const char *p; | |
899 | ||
900 | assert(esp_path); | |
901 | ||
270384b2 | 902 | p = prefix_roota(esp_path, "/loader/loader.conf"); |
fbf45d22 LP |
903 | if (unlink(p) < 0) { |
904 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to unlink file \"%s\": %m", p); | |
905 | if (errno != ENOENT) | |
906 | return -errno; | |
907 | } else | |
908 | log_info("Removed \"%s\".", p); | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | static int remove_entries_directory(const char *dollar_boot_path) { | |
914 | assert(dollar_boot_path); | |
915 | ||
916 | return rmdir_one(dollar_boot_path, "/loader/entries"); | |
917 | } | |
918 | ||
0974a682 KS |
919 | static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) { |
920 | uint16_t slot; | |
921 | int r; | |
922 | ||
923 | if (!is_efi_boot()) | |
924 | return 0; | |
925 | ||
926 | r = find_slot(uuid, path, &slot); | |
927 | if (r != 1) | |
928 | return 0; | |
929 | ||
930 | r = efi_remove_boot_option(slot); | |
931 | if (r < 0) | |
932 | return r; | |
933 | ||
934 | if (in_order) | |
d3226d77 | 935 | return remove_from_order(slot); |
f939cff7 LP |
936 | |
937 | return 0; | |
0974a682 KS |
938 | } |
939 | ||
341890de | 940 | static int install_loader_config(const char *esp_path, sd_id128_t machine_id) { |
d5ff6d6d | 941 | char machine_string[SD_ID128_STRING_MAX]; |
f5b84de2 LP |
942 | _cleanup_(unlink_and_freep) char *t = NULL; |
943 | _cleanup_fclose_ FILE *f = NULL; | |
d5ff6d6d | 944 | const char *p; |
f5b84de2 | 945 | int r, fd; |
0974a682 | 946 | |
270384b2 | 947 | p = prefix_roota(esp_path, "/loader/loader.conf"); |
f5b84de2 LP |
948 | if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */ |
949 | return 0; | |
950 | ||
951 | fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t); | |
952 | if (fd < 0) | |
953 | return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p); | |
954 | ||
e92aaed3 | 955 | f = fdopen(fd, "w"); |
f5b84de2 LP |
956 | if (!f) { |
957 | safe_close(fd); | |
958 | return log_oom(); | |
959 | } | |
0974a682 | 960 | |
a36b411e LP |
961 | fprintf(f, "#timeout 3\n" |
962 | "#console-mode keep\n" | |
963 | "default %s-*\n", sd_id128_to_string(machine_id, machine_string)); | |
0974a682 | 964 | |
0675e94a | 965 | r = fflush_sync_and_check(f); |
d5ff6d6d LP |
966 | if (r < 0) |
967 | return log_error_errno(r, "Failed to write \"%s\": %m", p); | |
0974a682 | 968 | |
f5b84de2 LP |
969 | r = link_tmpfile(fd, t, p); |
970 | if (r == -EEXIST) | |
971 | return 0; /* Silently skip creation if the file exists now (recheck) */ | |
972 | if (r < 0) | |
973 | return log_error_errno(r, "Failed to move \"%s\" into place: %m", p); | |
974 | ||
975 | t = mfree(t); | |
f5b84de2 | 976 | return 1; |
0974a682 KS |
977 | } |
978 | ||
341890de ZJS |
979 | static int install_entries_directories(const char *dollar_boot_path, sd_id128_t machine_id) { |
980 | int r; | |
981 | char buf[SD_ID128_STRING_MAX]; | |
982 | ||
fbf45d22 LP |
983 | assert(dollar_boot_path); |
984 | ||
341890de ZJS |
985 | /* Both /loader/entries and the entry directories themselves should be located on the same |
986 | * partition. Also create the parent directory for entry directories, so that kernel-install | |
987 | * knows where to put them. */ | |
988 | ||
e2600fd5 | 989 | r = mkdir_one(dollar_boot_path, "loader/entries"); |
341890de ZJS |
990 | if (r < 0) |
991 | return r; | |
992 | ||
993 | return mkdir_one(dollar_boot_path, sd_id128_to_string(machine_id, buf)); | |
fbf45d22 LP |
994 | } |
995 | ||
2f2c539c | 996 | static int help(int argc, char *argv[], void *userdata) { |
37ec0fdd LP |
997 | _cleanup_free_ char *link = NULL; |
998 | int r; | |
999 | ||
1000 | r = terminal_urlify_man("bootctl", "1", &link); | |
1001 | if (r < 0) | |
1002 | return log_oom(); | |
2f2c539c | 1003 | |
37ec0fdd | 1004 | printf("%s [COMMAND] [OPTIONS...]\n\n" |
68cc17f1 | 1005 | "Install, update or remove the systemd-boot EFI boot manager.\n\n" |
fbf45d22 LP |
1006 | " -h --help Show this help\n" |
1007 | " --version Print version\n" | |
1008 | " --esp-path=PATH Path to the EFI System Partition (ESP)\n" | |
1009 | " --boot-path=PATH Path to the $BOOT partition\n" | |
1010 | " -p --print-esp-path Print path to the EFI System Partition\n" | |
fba4e945 | 1011 | " -x --print-boot-path Print path to the $BOOT partition\n" |
fbf45d22 LP |
1012 | " --no-variables Don't touch EFI variables\n" |
1013 | " --no-pager Do not pipe output into a pager\n" | |
d88c96ff | 1014 | "\nBoot Loader Commands:\n" |
fbf45d22 LP |
1015 | " status Show status of installed systemd-boot and EFI variables\n" |
1016 | " install Install systemd-boot to the ESP and EFI variables\n" | |
1017 | " update Update systemd-boot in the ESP and EFI variables\n" | |
1018 | " remove Remove systemd-boot from the ESP and EFI variables\n" | |
d88c96ff | 1019 | "\nBoot Loader Entries Commands:\n" |
fbf45d22 LP |
1020 | " list List boot loader entries\n" |
1021 | " set-default ID Set default boot loader entry\n" | |
1022 | " set-oneshot ID Set default boot loader entry, for next boot only\n" | |
37ec0fdd LP |
1023 | "\nSee the %s for details.\n" |
1024 | , program_invocation_short_name | |
d88c96ff | 1025 | , link); |
0974a682 KS |
1026 | |
1027 | return 0; | |
1028 | } | |
1029 | ||
0974a682 KS |
1030 | static int parse_argv(int argc, char *argv[]) { |
1031 | enum { | |
fbf45d22 LP |
1032 | ARG_ESP_PATH = 0x100, |
1033 | ARG_BOOT_PATH, | |
0974a682 KS |
1034 | ARG_VERSION, |
1035 | ARG_NO_VARIABLES, | |
57db6f18 | 1036 | ARG_NO_PAGER, |
7b4d7cc0 KS |
1037 | }; |
1038 | ||
0974a682 | 1039 | static const struct option options[] = { |
fbf45d22 LP |
1040 | { "help", no_argument, NULL, 'h' }, |
1041 | { "version", no_argument, NULL, ARG_VERSION }, | |
1042 | { "esp-path", required_argument, NULL, ARG_ESP_PATH }, | |
1043 | { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */ | |
1044 | { "boot-path", required_argument, NULL, ARG_BOOT_PATH }, | |
1045 | { "print-esp-path", no_argument, NULL, 'p' }, | |
1046 | { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */ | |
fba4e945 | 1047 | { "print-boot-path", no_argument, NULL, 'x' }, |
fbf45d22 LP |
1048 | { "no-variables", no_argument, NULL, ARG_NO_VARIABLES }, |
1049 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
a36b411e | 1050 | {} |
0974a682 KS |
1051 | }; |
1052 | ||
2f2c539c | 1053 | int c, r; |
7b4d7cc0 KS |
1054 | |
1055 | assert(argc >= 0); | |
1056 | assert(argv); | |
1057 | ||
fba4e945 | 1058 | while ((c = getopt_long(argc, argv, "hpx", options, NULL)) >= 0) |
0974a682 | 1059 | switch (c) { |
7b4d7cc0 | 1060 | |
0974a682 | 1061 | case 'h': |
2f2c539c | 1062 | help(0, NULL, NULL); |
7b4d7cc0 | 1063 | return 0; |
7b4d7cc0 | 1064 | |
0974a682 | 1065 | case ARG_VERSION: |
3f6fd1ba | 1066 | return version(); |
7b4d7cc0 | 1067 | |
fbf45d22 LP |
1068 | case ARG_ESP_PATH: |
1069 | r = free_and_strdup(&arg_esp_path, optarg); | |
1070 | if (r < 0) | |
1071 | return log_oom(); | |
1072 | break; | |
1073 | ||
1074 | case ARG_BOOT_PATH: | |
1075 | r = free_and_strdup(&arg_xbootldr_path, optarg); | |
2f2c539c LP |
1076 | if (r < 0) |
1077 | return log_oom(); | |
0974a682 KS |
1078 | break; |
1079 | ||
30b50477 | 1080 | case 'p': |
aa467bca ZJS |
1081 | if (arg_print_dollar_boot_path) |
1082 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1083 | "--print-boot-path/-x cannot be combined with --print-esp-path/-p"); | |
fbf45d22 LP |
1084 | arg_print_esp_path = true; |
1085 | break; | |
1086 | ||
fba4e945 | 1087 | case 'x': |
aa467bca ZJS |
1088 | if (arg_print_esp_path) |
1089 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1090 | "--print-boot-path/-x cannot be combined with --print-esp-path/-p"); | |
fbf45d22 | 1091 | arg_print_dollar_boot_path = true; |
30b50477 ZJS |
1092 | break; |
1093 | ||
0974a682 KS |
1094 | case ARG_NO_VARIABLES: |
1095 | arg_touch_variables = false; | |
1096 | break; | |
1097 | ||
57db6f18 | 1098 | case ARG_NO_PAGER: |
0221d68a | 1099 | arg_pager_flags |= PAGER_DISABLE; |
57db6f18 LP |
1100 | break; |
1101 | ||
0974a682 KS |
1102 | case '?': |
1103 | return -EINVAL; | |
1104 | ||
1105 | default: | |
d3226d77 | 1106 | assert_not_reached("Unknown option"); |
7b4d7cc0 | 1107 | } |
7b4d7cc0 | 1108 | |
0974a682 KS |
1109 | return 1; |
1110 | } | |
7b4d7cc0 | 1111 | |
551710cf ZJS |
1112 | static void read_loader_efi_var(const char *name, char **var) { |
1113 | int r; | |
1114 | ||
1115 | r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var); | |
1116 | if (r < 0 && r != -ENOENT) | |
1117 | log_warning_errno(r, "Failed to read EFI variable %s: %m", name); | |
1118 | } | |
1119 | ||
2f2c539c | 1120 | static int verb_status(int argc, char *argv[], void *userdata) { |
fbf45d22 | 1121 | sd_id128_t esp_uuid = SD_ID128_NULL, xbootldr_uuid = SD_ID128_NULL; |
46fb255b | 1122 | int r, k; |
0974a682 | 1123 | |
fbf45d22 LP |
1124 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &esp_uuid); |
1125 | if (arg_print_esp_path) { | |
5caa3167 LP |
1126 | if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only |
1127 | * error the find_esp_and_warn() won't log on its own) */ | |
fbf45d22 | 1128 | return log_error_errno(r, "Failed to determine ESP location: %m"); |
30b50477 ZJS |
1129 | if (r < 0) |
1130 | return r; | |
1131 | ||
fbf45d22 LP |
1132 | puts(arg_esp_path); |
1133 | } | |
1134 | ||
1135 | r = acquire_xbootldr(geteuid() != 0, &xbootldr_uuid); | |
1136 | if (arg_print_dollar_boot_path) { | |
1137 | if (r == -EACCES) | |
1138 | return log_error_errno(r, "Failed to determine XBOOTLDR location: %m"); | |
1139 | if (r < 0) | |
1140 | return r; | |
1141 | ||
1142 | puts(arg_dollar_boot_path()); | |
30b50477 | 1143 | } |
551710cf | 1144 | |
fbf45d22 LP |
1145 | if (arg_print_esp_path || arg_print_dollar_boot_path) |
1146 | return 0; | |
1147 | ||
5caa3167 LP |
1148 | r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we |
1149 | * can show */ | |
1150 | ||
0221d68a | 1151 | (void) pager_open(arg_pager_flags); |
57db6f18 | 1152 | |
2f2c539c | 1153 | if (is_efi_boot()) { |
80641a81 LP |
1154 | static const struct { |
1155 | uint64_t flag; | |
1156 | const char *name; | |
1157 | } flags[] = { | |
3a76fbe0 ZJS |
1158 | { EFI_LOADER_FEATURE_BOOT_COUNTING, "Boot counting" }, |
1159 | { EFI_LOADER_FEATURE_CONFIG_TIMEOUT, "Menu timeout control" }, | |
1160 | { EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT, "One-shot menu timeout control" }, | |
1161 | { EFI_LOADER_FEATURE_ENTRY_DEFAULT, "Default entry control" }, | |
1162 | { EFI_LOADER_FEATURE_ENTRY_ONESHOT, "One-shot entry control" }, | |
1163 | { EFI_LOADER_FEATURE_XBOOTLDR, "Support for XBOOTLDR partition" }, | |
80641a81 LP |
1164 | }; |
1165 | ||
81375b9b | 1166 | _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL; |
25579a43 | 1167 | sd_id128_t loader_part_uuid = SD_ID128_NULL; |
80641a81 LP |
1168 | uint64_t loader_features = 0; |
1169 | size_t i; | |
0974a682 | 1170 | |
2f2c539c LP |
1171 | read_loader_efi_var("LoaderFirmwareType", &fw_type); |
1172 | read_loader_efi_var("LoaderFirmwareInfo", &fw_info); | |
1173 | read_loader_efi_var("LoaderInfo", &loader); | |
81375b9b | 1174 | read_loader_efi_var("StubInfo", &stub); |
2f2c539c | 1175 | read_loader_efi_var("LoaderImageIdentifier", &loader_path); |
80641a81 | 1176 | (void) efi_loader_get_features(&loader_features); |
551710cf | 1177 | |
2f2c539c LP |
1178 | if (loader_path) |
1179 | efi_tilt_backslashes(loader_path); | |
1180 | ||
46fb255b ZJS |
1181 | k = efi_loader_get_device_part_uuid(&loader_part_uuid); |
1182 | if (k < 0 && k != -ENOENT) | |
1183 | r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m"); | |
2f2c539c LP |
1184 | |
1185 | printf("System:\n"); | |
ba857253 | 1186 | printf(" Firmware: %s%s (%s)%s\n", ansi_highlight(), strna(fw_type), strna(fw_info), ansi_normal()); |
33987ba0 YW |
1187 | printf(" Secure Boot: %sd\n", enable_disable(is_efi_secure_boot())); |
1188 | printf(" Setup Mode: %s\n", is_efi_secure_boot_setup_mode() ? "setup" : "user"); | |
2f2c539c LP |
1189 | printf("\n"); |
1190 | ||
7fd66f3c | 1191 | printf("Current Boot Loader:\n"); |
ba857253 | 1192 | printf(" Product: %s%s%s\n", ansi_highlight(), strna(loader), ansi_normal()); |
80641a81 LP |
1193 | |
1194 | for (i = 0; i < ELEMENTSOF(flags); i++) { | |
1195 | ||
1196 | if (i == 0) | |
1197 | printf(" Features: "); | |
1198 | else | |
1199 | printf(" "); | |
1200 | ||
1201 | if (FLAGS_SET(loader_features, flags[i].flag)) | |
9a6f746f | 1202 | printf("%s%s%s %s\n", ansi_highlight_green(), special_glyph(SPECIAL_GLYPH_CHECK_MARK), ansi_normal(), flags[i].name); |
80641a81 | 1203 | else |
9a6f746f | 1204 | printf("%s%s%s %s\n", ansi_highlight_red(), special_glyph(SPECIAL_GLYPH_CROSS_MARK), ansi_normal(), flags[i].name); |
80641a81 LP |
1205 | } |
1206 | ||
81375b9b ДГ |
1207 | if (stub) |
1208 | printf(" Stub: %s\n", stub); | |
e28973ee | 1209 | if (!sd_id128_is_null(loader_part_uuid)) |
bd44566c | 1210 | printf(" ESP: /dev/disk/by-partuuid/" SD_ID128_UUID_FORMAT_STR "\n", |
2f2c539c LP |
1211 | SD_ID128_FORMAT_VAL(loader_part_uuid)); |
1212 | else | |
cd2d4c7f | 1213 | printf(" ESP: n/a\n"); |
9a6f746f | 1214 | printf(" File: %s%s\n", special_glyph(SPECIAL_GLYPH_TREE_RIGHT), strna(loader_path)); |
2f2c539c LP |
1215 | printf("\n"); |
1216 | } else | |
cd2d4c7f | 1217 | printf("System:\n Not booted with EFI\n\n"); |
7b4d7cc0 | 1218 | |
fbf45d22 LP |
1219 | if (arg_esp_path) { |
1220 | k = status_binaries(arg_esp_path, esp_uuid); | |
ecec2a5d LP |
1221 | if (k < 0) |
1222 | r = k; | |
1223 | } | |
2f2c539c | 1224 | |
cd2d4c7f | 1225 | if (is_efi_boot()) { |
46fb255b ZJS |
1226 | k = status_variables(); |
1227 | if (k < 0) | |
1228 | r = k; | |
cd2d4c7f | 1229 | } |
0974a682 | 1230 | |
fbf45d22 | 1231 | if (arg_esp_path || arg_xbootldr_path) { |
81fed855 | 1232 | k = status_entries(arg_esp_path, esp_uuid, arg_xbootldr_path, xbootldr_uuid); |
ecec2a5d LP |
1233 | if (k < 0) |
1234 | r = k; | |
1235 | } | |
7e87c7d9 | 1236 | |
46fb255b | 1237 | return r; |
2f2c539c | 1238 | } |
7b4d7cc0 | 1239 | |
7e87c7d9 | 1240 | static int verb_list(int argc, char *argv[], void *userdata) { |
5caa3167 | 1241 | _cleanup_(boot_config_free) BootConfig config = {}; |
5caa3167 | 1242 | int r; |
7e87c7d9 | 1243 | |
5caa3167 LP |
1244 | /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn |
1245 | * off logging about access errors and turn off potentially privileged device probing. Here we're interested in | |
1246 | * the latter but not the former, hence request the mode, and log about EACCES. */ | |
7e87c7d9 | 1247 | |
fbf45d22 | 1248 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, NULL); |
5caa3167 LP |
1249 | if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */ |
1250 | return log_error_errno(r, "Failed to determine ESP: %m"); | |
7e87c7d9 ZJS |
1251 | if (r < 0) |
1252 | return r; | |
1253 | ||
fbf45d22 LP |
1254 | r = acquire_xbootldr(geteuid() != 0, NULL); |
1255 | if (r == -EACCES) | |
1256 | return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m"); | |
1257 | if (r < 0) | |
1258 | return r; | |
1259 | ||
1260 | r = boot_entries_load_config(arg_esp_path, arg_xbootldr_path, &config); | |
7e87c7d9 | 1261 | if (r < 0) |
21f7a622 | 1262 | return r; |
7e87c7d9 | 1263 | |
93f14ce2 | 1264 | (void) boot_entries_augment_from_loader(&config, false); |
bd2865ca | 1265 | |
20a28174 LP |
1266 | if (config.n_entries == 0) |
1267 | log_info("No boot loader entries found."); | |
1268 | else { | |
1269 | size_t n; | |
7e87c7d9 | 1270 | |
0221d68a | 1271 | (void) pager_open(arg_pager_flags); |
57db6f18 | 1272 | |
20a28174 | 1273 | printf("Boot Loader Entries:\n"); |
7e87c7d9 | 1274 | |
20a28174 LP |
1275 | for (n = 0; n < config.n_entries; n++) { |
1276 | r = boot_entry_show(config.entries + n, n == (size_t) config.default_entry); | |
1277 | if (r < 0) | |
1278 | return r; | |
7e87c7d9 | 1279 | |
4629499e LP |
1280 | if (n+1 < config.n_entries) |
1281 | putchar('\n'); | |
7e87c7d9 | 1282 | } |
cd2d4c7f | 1283 | } |
0974a682 | 1284 | |
7e87c7d9 | 1285 | return 0; |
2f2c539c | 1286 | } |
7b4d7cc0 | 1287 | |
fbf45d22 LP |
1288 | static int sync_everything(void) { |
1289 | int ret = 0, k; | |
e0e8d177 | 1290 | |
fbf45d22 LP |
1291 | if (arg_esp_path) { |
1292 | k = syncfs_path(AT_FDCWD, arg_esp_path); | |
1293 | if (k < 0) | |
1294 | ret = log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path); | |
1295 | } | |
e0e8d177 | 1296 | |
fbf45d22 LP |
1297 | if (arg_xbootldr_path) { |
1298 | k = syncfs_path(AT_FDCWD, arg_xbootldr_path); | |
1299 | if (k < 0) | |
1300 | ret = log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path); | |
1301 | } | |
e0e8d177 | 1302 | |
fbf45d22 | 1303 | return ret; |
e0e8d177 LP |
1304 | } |
1305 | ||
2f2c539c | 1306 | static int verb_install(int argc, char *argv[], void *userdata) { |
2f2c539c LP |
1307 | sd_id128_t uuid = SD_ID128_NULL; |
1308 | uint64_t pstart = 0, psize = 0; | |
1309 | uint32_t part = 0; | |
341890de | 1310 | sd_id128_t machine_id; |
2f2c539c LP |
1311 | bool install; |
1312 | int r; | |
1313 | ||
5caa3167 | 1314 | r = acquire_esp(false, &part, &pstart, &psize, &uuid); |
2f2c539c LP |
1315 | if (r < 0) |
1316 | return r; | |
1317 | ||
fbf45d22 LP |
1318 | r = acquire_xbootldr(false, NULL); |
1319 | if (r < 0) | |
1320 | return r; | |
1321 | ||
341890de ZJS |
1322 | r = sd_id128_get_machine(&machine_id); |
1323 | if (r < 0) | |
1324 | return log_error_errno(r, "Failed to get machine id: %m"); | |
1325 | ||
2f2c539c LP |
1326 | install = streq(argv[0], "install"); |
1327 | ||
1328 | RUN_WITH_UMASK(0002) { | |
fbf45d22 LP |
1329 | if (install) { |
1330 | /* Don't create any of these directories when we are just updating. When we update | |
1331 | * we'll drop-in our files (unless there are newer ones already), but we won't create | |
1332 | * the directories for them in the first place. */ | |
1333 | r = create_esp_subdirs(arg_esp_path); | |
1334 | if (r < 0) | |
1335 | return r; | |
1336 | } | |
1337 | ||
1338 | r = install_binaries(arg_esp_path, install); | |
0974a682 | 1339 | if (r < 0) |
d3226d77 | 1340 | return r; |
0974a682 | 1341 | |
2f2c539c | 1342 | if (install) { |
341890de | 1343 | r = install_loader_config(arg_esp_path, machine_id); |
fbf45d22 LP |
1344 | if (r < 0) |
1345 | return r; | |
1346 | ||
341890de | 1347 | r = install_entries_directories(arg_dollar_boot_path(), machine_id); |
d3226d77 ZJS |
1348 | if (r < 0) |
1349 | return r; | |
1350 | } | |
2f2c539c | 1351 | } |
0974a682 | 1352 | |
fbf45d22 | 1353 | (void) sync_everything(); |
e0e8d177 | 1354 | |
2f2c539c | 1355 | if (arg_touch_variables) |
fbf45d22 | 1356 | r = install_variables(arg_esp_path, |
2f2c539c LP |
1357 | part, pstart, psize, uuid, |
1358 | "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", | |
1359 | install); | |
7b4d7cc0 | 1360 | |
2f2c539c LP |
1361 | return r; |
1362 | } | |
0974a682 | 1363 | |
2f2c539c LP |
1364 | static int verb_remove(int argc, char *argv[], void *userdata) { |
1365 | sd_id128_t uuid = SD_ID128_NULL; | |
fbf45d22 | 1366 | int r, q; |
0974a682 | 1367 | |
5caa3167 | 1368 | r = acquire_esp(false, NULL, NULL, NULL, &uuid); |
2f2c539c LP |
1369 | if (r < 0) |
1370 | return r; | |
1371 | ||
fbf45d22 LP |
1372 | r = acquire_xbootldr(false, NULL); |
1373 | if (r < 0) | |
1374 | return r; | |
2f2c539c | 1375 | |
fbf45d22 | 1376 | r = remove_binaries(arg_esp_path); |
e0e8d177 | 1377 | |
fbf45d22 LP |
1378 | q = remove_loader_config(arg_esp_path); |
1379 | if (q < 0 && r >= 0) | |
1380 | r = q; | |
1381 | ||
1382 | q = remove_entries_directory(arg_dollar_boot_path()); | |
1383 | if (q < 0 && r >= 0) | |
1384 | r = q; | |
2f2c539c | 1385 | |
fbf45d22 LP |
1386 | q = remove_esp_subdirs(arg_esp_path); |
1387 | if (q < 0 && r >= 0) | |
1388 | r = q; | |
1389 | ||
1390 | (void) sync_everything(); | |
1391 | ||
1392 | if (arg_touch_variables) { | |
2f2c539c | 1393 | q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true); |
fbf45d22 | 1394 | if (q < 0 && r >= 0) |
2f2c539c | 1395 | r = q; |
7b4d7cc0 KS |
1396 | } |
1397 | ||
d3226d77 | 1398 | return r; |
7b4d7cc0 KS |
1399 | } |
1400 | ||
d88c96ff LP |
1401 | static int verb_set_default(int argc, char *argv[], void *userdata) { |
1402 | const char *name; | |
1403 | int r; | |
1404 | ||
baaa35ad ZJS |
1405 | if (!is_efi_boot()) |
1406 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1407 | "Not booted with UEFI."); | |
d88c96ff LP |
1408 | |
1409 | if (access("/sys/firmware/efi/efivars/LoaderInfo-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f", F_OK) < 0) { | |
1410 | if (errno == ENOENT) { | |
1411 | log_error_errno(errno, "Not booted with a supported boot loader."); | |
1412 | return -EOPNOTSUPP; | |
1413 | } | |
1414 | ||
1415 | return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", argv[0]); | |
1416 | } | |
1417 | ||
baaa35ad ZJS |
1418 | if (detect_container() > 0) |
1419 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1420 | "'%s' operation not supported in a container.", | |
1421 | argv[0]); | |
d88c96ff | 1422 | |
baaa35ad ZJS |
1423 | if (!arg_touch_variables) |
1424 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1425 | "'%s' operation cannot be combined with --touch-variables=no.", | |
1426 | argv[0]); | |
d88c96ff LP |
1427 | |
1428 | name = streq(argv[0], "set-default") ? "LoaderEntryDefault" : "LoaderEntryOneShot"; | |
1429 | ||
1430 | if (isempty(argv[1])) { | |
1431 | r = efi_set_variable(EFI_VENDOR_LOADER, name, NULL, 0); | |
1432 | if (r < 0 && r != -ENOENT) | |
1433 | return log_error_errno(r, "Failed to remove EFI variale: %m"); | |
1434 | } else { | |
1435 | _cleanup_free_ char16_t *encoded = NULL; | |
1436 | ||
1437 | encoded = utf8_to_utf16(argv[1], strlen(argv[1])); | |
1438 | if (!encoded) | |
1439 | return log_oom(); | |
1440 | ||
1441 | r = efi_set_variable(EFI_VENDOR_LOADER, name, encoded, char16_strlen(encoded) * 2 + 2); | |
1442 | if (r < 0) | |
1443 | return log_error_errno(r, "Failed to update EFI variable: %m"); | |
1444 | } | |
1445 | ||
1446 | return 0; | |
1447 | } | |
1448 | ||
2f2c539c | 1449 | static int bootctl_main(int argc, char *argv[]) { |
2f2c539c | 1450 | static const Verb verbs[] = { |
7c3ce8b5 ZJS |
1451 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
1452 | { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status }, | |
1453 | { "install", VERB_ANY, 1, 0, verb_install }, | |
1454 | { "update", VERB_ANY, 1, 0, verb_install }, | |
1455 | { "remove", VERB_ANY, 1, 0, verb_remove }, | |
1456 | { "list", VERB_ANY, 1, 0, verb_list }, | |
1457 | { "set-default", 2, 2, 0, verb_set_default }, | |
1458 | { "set-oneshot", 2, 2, 0, verb_set_default }, | |
2f2c539c LP |
1459 | {} |
1460 | }; | |
1461 | ||
1462 | return dispatch_verb(argc, argv, verbs, NULL); | |
1463 | } | |
1464 | ||
608f8ec9 | 1465 | static int run(int argc, char *argv[]) { |
601185b4 | 1466 | int r; |
7b4d7cc0 KS |
1467 | |
1468 | log_parse_environment(); | |
1469 | log_open(); | |
1470 | ||
341890de | 1471 | /* If we run in a container, automatically turn off EFI file system access */ |
2f2c539c LP |
1472 | if (detect_container() > 0) |
1473 | arg_touch_variables = false; | |
1474 | ||
7b4d7cc0 | 1475 | r = parse_argv(argc, argv); |
601185b4 | 1476 | if (r <= 0) |
608f8ec9 | 1477 | return r; |
57db6f18 | 1478 | |
608f8ec9 | 1479 | return bootctl_main(argc, argv); |
7b4d7cc0 | 1480 | } |
608f8ec9 YW |
1481 | |
1482 | DEFINE_MAIN_FUNCTION(run); |