]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7b4d7cc0 KS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
0974a682 KS |
5 | Copyright 2013-2015 Kay Sievers |
6 | Copyright 2013 Lennart Poettering | |
7b4d7cc0 KS |
7 | ***/ |
8 | ||
6b5cf3ea | 9 | #include <blkid.h> |
0974a682 | 10 | #include <ctype.h> |
3f6fd1ba LP |
11 | #include <dirent.h> |
12 | #include <errno.h> | |
0974a682 | 13 | #include <ftw.h> |
3f6fd1ba LP |
14 | #include <getopt.h> |
15 | #include <limits.h> | |
5fa6c13c | 16 | #include <linux/magic.h> |
0974a682 | 17 | #include <stdbool.h> |
3f6fd1ba LP |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <sys/mman.h> | |
22 | #include <sys/stat.h> | |
23 | #include <sys/statfs.h> | |
24 | #include <unistd.h> | |
7b4d7cc0 | 25 | |
dccca82b LP |
26 | #include "sd-id128.h" |
27 | ||
b5efdb8a | 28 | #include "alloc-util.h" |
3f6fd1ba | 29 | #include "blkid-util.h" |
7e87c7d9 | 30 | #include "bootspec.h" |
175d308c | 31 | #include "copy.h" |
e41256dc | 32 | #include "dirent-util.h" |
0974a682 | 33 | #include "efivars.h" |
3ffd4af2 | 34 | #include "fd-util.h" |
0d39fa9c | 35 | #include "fileio.h" |
175d308c | 36 | #include "fs-util.h" |
8752c575 | 37 | #include "locale-util.h" |
2f2c539c | 38 | #include "parse-util.h" |
c6878637 | 39 | #include "rm-rf.h" |
175d308c | 40 | #include "stat-util.h" |
07630cea | 41 | #include "string-util.h" |
2f2c539c | 42 | #include "strv.h" |
7e87c7d9 | 43 | #include "terminal-util.h" |
2f2c539c | 44 | #include "umask-util.h" |
3f6fd1ba | 45 | #include "util.h" |
2f2c539c LP |
46 | #include "verbs.h" |
47 | #include "virt.h" | |
7b4d7cc0 | 48 | |
2f2c539c | 49 | static char *arg_path = NULL; |
30b50477 | 50 | static bool arg_print_path = false; |
25579a43 LP |
51 | static bool arg_touch_variables = true; |
52 | ||
5caa3167 LP |
53 | static int acquire_esp( |
54 | bool unprivileged_mode, | |
55 | uint32_t *ret_part, | |
56 | uint64_t *ret_pstart, | |
57 | uint64_t *ret_psize, | |
58 | sd_id128_t *ret_uuid) { | |
59 | ||
60 | char *np; | |
2f2c539c LP |
61 | int r; |
62 | ||
5caa3167 LP |
63 | /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on its own, |
64 | * except for ENOKEY (which is good, we want to show our own message in that case, suggesting use of --path=) | |
65 | * and EACCESS (only when we request unprivileged mode; in this case we simply eat up the error here, so that | |
66 | * --list and --status work too, without noise about this). */ | |
67 | ||
68 | r = find_esp_and_warn(arg_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid); | |
69 | if (r == -ENOKEY) | |
af918182 | 70 | return log_error_errno(r, |
5caa3167 | 71 | "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n" |
af918182 | 72 | "Alternatively, use --path= to specify path to mount point."); |
5caa3167 LP |
73 | if (r < 0) |
74 | return r; | |
75 | ||
76 | free_and_replace(arg_path, np); | |
0974a682 | 77 | |
30b50477 | 78 | log_debug("Using EFI System Partition at %s.", arg_path); |
5caa3167 | 79 | |
0974a682 | 80 | return 0; |
7b4d7cc0 KS |
81 | } |
82 | ||
e7dd673d | 83 | /* search for "#### LoaderInfo: systemd-boot 218 ####" string inside the binary */ |
d3226d77 | 84 | static int get_file_version(int fd, char **v) { |
0974a682 KS |
85 | struct stat st; |
86 | char *buf; | |
87 | const char *s, *e; | |
88 | char *x = NULL; | |
89 | int r = 0; | |
7b4d7cc0 | 90 | |
d3226d77 | 91 | assert(fd >= 0); |
0974a682 | 92 | assert(v); |
7b4d7cc0 | 93 | |
d3226d77 | 94 | if (fstat(fd, &st) < 0) |
db6d9fae | 95 | return log_error_errno(errno, "Failed to stat EFI binary: %m"); |
7b4d7cc0 | 96 | |
db6d9fae LP |
97 | if (st.st_size < 27) { |
98 | *v = NULL; | |
0974a682 | 99 | return 0; |
db6d9fae | 100 | } |
eb9da376 | 101 | |
d3226d77 | 102 | buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
0974a682 | 103 | if (buf == MAP_FAILED) |
db6d9fae | 104 | return log_error_errno(errno, "Failed to memory map EFI binary: %m"); |
7b4d7cc0 | 105 | |
0974a682 KS |
106 | s = memmem(buf, st.st_size - 8, "#### LoaderInfo: ", 17); |
107 | if (!s) | |
108 | goto finish; | |
109 | s += 17; | |
7b4d7cc0 | 110 | |
0974a682 KS |
111 | e = memmem(s, st.st_size - (s - buf), " ####", 5); |
112 | if (!e || e - s < 3) { | |
d3226d77 | 113 | log_error("Malformed version string."); |
0974a682 KS |
114 | r = -EINVAL; |
115 | goto finish; | |
116 | } | |
7b4d7cc0 | 117 | |
0974a682 KS |
118 | x = strndup(s, e - s); |
119 | if (!x) { | |
d3226d77 | 120 | r = log_oom(); |
0974a682 KS |
121 | goto finish; |
122 | } | |
123 | r = 1; | |
7b4d7cc0 | 124 | |
0974a682 | 125 | finish: |
db6d9fae | 126 | (void) munmap(buf, st.st_size); |
0974a682 KS |
127 | *v = x; |
128 | return r; | |
129 | } | |
7b4d7cc0 | 130 | |
0974a682 | 131 | static int enumerate_binaries(const char *esp_path, const char *path, const char *prefix) { |
d3226d77 ZJS |
132 | char *p; |
133 | _cleanup_closedir_ DIR *d = NULL; | |
0974a682 | 134 | struct dirent *de; |
0974a682 KS |
135 | int r = 0, c = 0; |
136 | ||
d3226d77 | 137 | p = strjoina(esp_path, "/", path); |
0974a682 KS |
138 | d = opendir(p); |
139 | if (!d) { | |
d3226d77 ZJS |
140 | if (errno == ENOENT) |
141 | return 0; | |
7b4d7cc0 | 142 | |
d3226d77 | 143 | return log_error_errno(errno, "Failed to read \"%s\": %m", p); |
0974a682 KS |
144 | } |
145 | ||
e41256dc | 146 | FOREACH_DIRENT(de, d, break) { |
d3226d77 ZJS |
147 | _cleanup_close_ int fd = -1; |
148 | _cleanup_free_ char *v = NULL; | |
0974a682 | 149 | |
d3226d77 | 150 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
151 | continue; |
152 | ||
d3226d77 | 153 | if (prefix && !startswith_no_case(de->d_name, prefix)) |
0974a682 KS |
154 | continue; |
155 | ||
d3226d77 ZJS |
156 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
157 | if (fd < 0) | |
158 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); | |
0974a682 | 159 | |
d3226d77 | 160 | r = get_file_version(fd, &v); |
0974a682 | 161 | if (r < 0) |
d3226d77 | 162 | return r; |
0974a682 | 163 | if (r > 0) |
323b7dc9 | 164 | printf(" File: %s/%s/%s (%s)\n", special_glyph(TREE_RIGHT), path, de->d_name, v); |
0974a682 | 165 | else |
323b7dc9 | 166 | printf(" File: %s/%s/%s\n", special_glyph(TREE_RIGHT), path, de->d_name); |
0974a682 | 167 | c++; |
0974a682 KS |
168 | } |
169 | ||
d3226d77 | 170 | return c; |
7b4d7cc0 KS |
171 | } |
172 | ||
0974a682 KS |
173 | static int status_binaries(const char *esp_path, sd_id128_t partition) { |
174 | int r; | |
175 | ||
176 | printf("Boot Loader Binaries:\n"); | |
177 | ||
cd2d4c7f YW |
178 | if (!esp_path) { |
179 | printf(" ESP: Cannot find or access mount point of ESP.\n\n"); | |
180 | return -ENOENT; | |
181 | } | |
182 | ||
183 | printf(" ESP: %s", esp_path); | |
184 | if (!sd_id128_is_null(partition)) | |
185 | printf(" (/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)", SD_ID128_FORMAT_VAL(partition)); | |
186 | printf("\n"); | |
0974a682 KS |
187 | |
188 | r = enumerate_binaries(esp_path, "EFI/systemd", NULL); | |
189 | if (r == 0) | |
d3226d77 | 190 | log_error("systemd-boot not installed in ESP."); |
0974a682 KS |
191 | else if (r < 0) |
192 | return r; | |
193 | ||
00f69504 | 194 | r = enumerate_binaries(esp_path, "EFI/BOOT", "boot"); |
0974a682 | 195 | if (r == 0) |
d3226d77 | 196 | log_error("No default/fallback boot loader installed in ESP."); |
0974a682 KS |
197 | else if (r < 0) |
198 | return r; | |
199 | ||
c11ae0ba KS |
200 | printf("\n"); |
201 | ||
0974a682 KS |
202 | return 0; |
203 | } | |
204 | ||
205 | static int print_efi_option(uint16_t id, bool in_order) { | |
7cb0f263 TA |
206 | _cleanup_free_ char *title = NULL; |
207 | _cleanup_free_ char *path = NULL; | |
0974a682 KS |
208 | sd_id128_t partition; |
209 | bool active; | |
210 | int r = 0; | |
211 | ||
212 | r = efi_get_boot_option(id, &title, &partition, &path, &active); | |
213 | if (r < 0) | |
7cb0f263 | 214 | return r; |
7b4d7cc0 | 215 | |
0974a682 | 216 | /* print only configured entries with partition information */ |
3bbaff3e | 217 | if (!path || sd_id128_is_null(partition)) |
0974a682 KS |
218 | return 0; |
219 | ||
220 | efi_tilt_backslashes(path); | |
221 | ||
222 | printf(" Title: %s\n", strna(title)); | |
223 | printf(" ID: 0x%04X\n", id); | |
224 | printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : ""); | |
225 | printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", SD_ID128_FORMAT_VAL(partition)); | |
323b7dc9 | 226 | printf(" File: %s%s\n", special_glyph(TREE_RIGHT), path); |
0974a682 KS |
227 | printf("\n"); |
228 | ||
7cb0f263 | 229 | return 0; |
0974a682 KS |
230 | } |
231 | ||
232 | static int status_variables(void) { | |
233 | int n_options, n_order; | |
d3226d77 ZJS |
234 | _cleanup_free_ uint16_t *options = NULL, *order = NULL; |
235 | int i; | |
0974a682 | 236 | |
0974a682 | 237 | n_options = efi_get_boot_options(&options); |
d3226d77 | 238 | if (n_options == -ENOENT) |
f939cff7 LP |
239 | return log_error_errno(n_options, |
240 | "Failed to access EFI variables, efivarfs" | |
d3226d77 | 241 | " needs to be available at /sys/firmware/efi/efivars/."); |
f939cff7 | 242 | if (n_options < 0) |
d3226d77 | 243 | return log_error_errno(n_options, "Failed to read EFI boot entries: %m"); |
0974a682 | 244 | |
0974a682 | 245 | n_order = efi_get_boot_order(&order); |
d3226d77 | 246 | if (n_order == -ENOENT) |
0974a682 | 247 | n_order = 0; |
d3226d77 ZJS |
248 | else if (n_order < 0) |
249 | return log_error_errno(n_order, "Failed to read EFI boot order."); | |
0974a682 KS |
250 | |
251 | /* print entries in BootOrder first */ | |
d3226d77 | 252 | printf("Boot Loader Entries in EFI Variables:\n"); |
0974a682 KS |
253 | for (i = 0; i < n_order; i++) |
254 | print_efi_option(order[i], true); | |
255 | ||
256 | /* print remaining entries */ | |
257 | for (i = 0; i < n_options; i++) { | |
258 | int j; | |
0974a682 KS |
259 | |
260 | for (j = 0; j < n_order; j++) | |
d3226d77 | 261 | if (options[i] == order[j]) |
a908cf0a | 262 | goto next_option; |
0974a682 KS |
263 | |
264 | print_efi_option(options[i], false); | |
a908cf0a MM |
265 | |
266 | next_option: | |
267 | continue; | |
0974a682 KS |
268 | } |
269 | ||
d3226d77 | 270 | return 0; |
0974a682 KS |
271 | } |
272 | ||
7e87c7d9 ZJS |
273 | static int status_entries(const char *esp_path, sd_id128_t partition) { |
274 | int r; | |
275 | ||
276 | _cleanup_(boot_config_free) BootConfig config = {}; | |
277 | ||
278 | printf("Default Boot Entry:\n"); | |
279 | ||
280 | r = boot_entries_load_config(esp_path, &config); | |
281 | if (r < 0) | |
282 | return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", | |
283 | esp_path); | |
284 | ||
285 | if (config.default_entry < 0) | |
cce5b4f1 | 286 | printf("%zu entries, no entry suitable as default\n", config.n_entries); |
7e87c7d9 ZJS |
287 | else { |
288 | const BootEntry *e = &config.entries[config.default_entry]; | |
289 | ||
64f05708 | 290 | printf(" title: %s\n", boot_entry_title(e)); |
7e87c7d9 ZJS |
291 | if (e->version) |
292 | printf(" version: %s\n", e->version); | |
293 | if (e->kernel) | |
294 | printf(" linux: %s\n", e->kernel); | |
295 | if (!strv_isempty(e->initrd)) { | |
296 | _cleanup_free_ char *t; | |
297 | ||
298 | t = strv_join(e->initrd, " "); | |
299 | if (!t) | |
300 | return log_oom(); | |
301 | ||
302 | printf(" initrd: %s\n", t); | |
303 | } | |
304 | if (!strv_isempty(e->options)) { | |
305 | _cleanup_free_ char *t; | |
306 | ||
307 | t = strv_join(e->options, " "); | |
308 | if (!t) | |
309 | return log_oom(); | |
310 | ||
311 | printf(" options: %s\n", t); | |
312 | } | |
313 | if (e->device_tree) | |
314 | printf(" devicetree: %s\n", e->device_tree); | |
315 | puts(""); | |
316 | } | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
0974a682 KS |
321 | static int compare_product(const char *a, const char *b) { |
322 | size_t x, y; | |
323 | ||
324 | assert(a); | |
325 | assert(b); | |
326 | ||
327 | x = strcspn(a, " "); | |
328 | y = strcspn(b, " "); | |
329 | if (x != y) | |
330 | return x < y ? -1 : x > y ? 1 : 0; | |
331 | ||
332 | return strncmp(a, b, x); | |
333 | } | |
334 | ||
335 | static int compare_version(const char *a, const char *b) { | |
336 | assert(a); | |
337 | assert(b); | |
338 | ||
339 | a += strcspn(a, " "); | |
340 | a += strspn(a, " "); | |
341 | b += strcspn(b, " "); | |
342 | b += strspn(b, " "); | |
343 | ||
344 | return strverscmp(a, b); | |
345 | } | |
346 | ||
175d308c | 347 | static int version_check(int fd_from, const char *from, int fd_to, const char *to) { |
d3226d77 | 348 | _cleanup_free_ char *a = NULL, *b = NULL; |
0974a682 KS |
349 | int r; |
350 | ||
175d308c | 351 | assert(fd_from >= 0); |
0974a682 | 352 | assert(from); |
175d308c | 353 | assert(fd_to >= 0); |
0974a682 KS |
354 | assert(to); |
355 | ||
175d308c | 356 | r = get_file_version(fd_from, &a); |
0974a682 | 357 | if (r < 0) |
d3226d77 | 358 | return r; |
0974a682 | 359 | if (r == 0) { |
d3226d77 ZJS |
360 | log_error("Source file \"%s\" does not carry version information!", from); |
361 | return -EINVAL; | |
0974a682 KS |
362 | } |
363 | ||
175d308c | 364 | r = get_file_version(fd_to, &b); |
0974a682 | 365 | if (r < 0) |
d3226d77 | 366 | return r; |
0974a682 | 367 | if (r == 0 || compare_product(a, b) != 0) { |
d3226d77 ZJS |
368 | log_notice("Skipping \"%s\", since it's owned by another boot loader.", to); |
369 | return -EEXIST; | |
0974a682 KS |
370 | } |
371 | ||
372 | if (compare_version(a, b) < 0) { | |
d3226d77 ZJS |
373 | log_warning("Skipping \"%s\", since a newer boot loader version exists already.", to); |
374 | return -ESTALE; | |
0974a682 KS |
375 | } |
376 | ||
d3226d77 | 377 | return 0; |
0974a682 KS |
378 | } |
379 | ||
175d308c LP |
380 | static int copy_file_with_version_check(const char *from, const char *to, bool force) { |
381 | _cleanup_close_ int fd_from = -1, fd_to = -1; | |
382 | _cleanup_free_ char *t = NULL; | |
0974a682 | 383 | int r; |
0974a682 | 384 | |
175d308c LP |
385 | fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
386 | if (fd_from < 0) | |
d3226d77 | 387 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from); |
0974a682 KS |
388 | |
389 | if (!force) { | |
175d308c LP |
390 | fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
391 | if (fd_to < 0) { | |
392 | if (errno != -ENOENT) | |
393 | return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to); | |
394 | } else { | |
395 | r = version_check(fd_from, from, fd_to, to); | |
396 | if (r < 0) | |
397 | return r; | |
398 | ||
399 | if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1) | |
6719ca72 | 400 | return log_error_errno(errno, "Failed to seek in \"%s\": %m", from); |
175d308c LP |
401 | |
402 | fd_to = safe_close(fd_to); | |
0974a682 | 403 | } |
175d308c | 404 | } |
d3226d77 | 405 | |
175d308c LP |
406 | r = tempfn_random(to, NULL, &t); |
407 | if (r < 0) | |
408 | return log_oom(); | |
0974a682 | 409 | |
175d308c LP |
410 | RUN_WITH_UMASK(0000) { |
411 | fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644); | |
412 | if (fd_to < 0) | |
413 | return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t); | |
0974a682 KS |
414 | } |
415 | ||
175d308c | 416 | r = copy_bytes(fd_from, fd_to, (uint64_t) -1, COPY_REFLINK); |
0974a682 | 417 | if (r < 0) { |
0675e94a AJ |
418 | (void) unlink(t); |
419 | return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
0974a682 KS |
420 | } |
421 | ||
175d308c | 422 | (void) copy_times(fd_from, fd_to); |
0974a682 | 423 | |
8ac2f74f | 424 | if (fsync(fd_to) < 0) { |
0675e94a AJ |
425 | (void) unlink_noerrno(t); |
426 | return log_error_errno(errno, "Failed to copy data from \"%s\" to \"%s\": %m", from, t); | |
427 | } | |
428 | ||
8ac2f74f LP |
429 | (void) fsync_directory_of_file(fd_to); |
430 | ||
175d308c | 431 | r = renameat(AT_FDCWD, t, AT_FDCWD, to); |
0974a682 | 432 | if (r < 0) { |
175d308c LP |
433 | (void) unlink_noerrno(t); |
434 | return log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", t, to); | |
0974a682 KS |
435 | } |
436 | ||
d3226d77 | 437 | log_info("Copied \"%s\" to \"%s\".", from, to); |
0974a682 | 438 | |
175d308c | 439 | return 0; |
0974a682 KS |
440 | } |
441 | ||
0974a682 KS |
442 | static int mkdir_one(const char *prefix, const char *suffix) { |
443 | char *p; | |
444 | ||
d3226d77 | 445 | p = strjoina(prefix, "/", suffix); |
0974a682 | 446 | if (mkdir(p, 0700) < 0) { |
d3226d77 ZJS |
447 | if (errno != EEXIST) |
448 | return log_error_errno(errno, "Failed to create \"%s\": %m", p); | |
0974a682 | 449 | } else |
d3226d77 | 450 | log_info("Created \"%s\".", p); |
7b4d7cc0 | 451 | |
0974a682 KS |
452 | return 0; |
453 | } | |
454 | ||
d3226d77 ZJS |
455 | static const char *efi_subdirs[] = { |
456 | "EFI", | |
457 | "EFI/systemd", | |
00f69504 | 458 | "EFI/BOOT", |
d3226d77 | 459 | "loader", |
9ee051b9 YW |
460 | "loader/entries", |
461 | NULL | |
d3226d77 ZJS |
462 | }; |
463 | ||
0974a682 | 464 | static int create_dirs(const char *esp_path) { |
181ccb43 | 465 | const char **i; |
0974a682 KS |
466 | int r; |
467 | ||
181ccb43 LP |
468 | STRV_FOREACH(i, efi_subdirs) { |
469 | r = mkdir_one(esp_path, *i); | |
d3226d77 ZJS |
470 | if (r < 0) |
471 | return r; | |
472 | } | |
7b4d7cc0 | 473 | |
7b4d7cc0 | 474 | return 0; |
7b4d7cc0 KS |
475 | } |
476 | ||
0974a682 | 477 | static int copy_one_file(const char *esp_path, const char *name, bool force) { |
d3226d77 | 478 | char *p, *q; |
0974a682 KS |
479 | int r; |
480 | ||
d3226d77 ZJS |
481 | p = strjoina(BOOTLIBDIR "/", name); |
482 | q = strjoina(esp_path, "/EFI/systemd/", name); | |
175d308c | 483 | r = copy_file_with_version_check(p, q, force); |
0974a682 | 484 | |
e7dd673d | 485 | if (startswith(name, "systemd-boot")) { |
0974a682 | 486 | int k; |
d3226d77 | 487 | char *v; |
0974a682 KS |
488 | |
489 | /* Create the EFI default boot loader name (specified for removable devices) */ | |
fbd0b64f LP |
490 | v = strjoina(esp_path, "/EFI/BOOT/BOOT", |
491 | name + STRLEN("systemd-boot")); | |
846b8fc3 | 492 | ascii_strupper(strrchr(v, '/') + 1); |
0974a682 | 493 | |
175d308c | 494 | k = copy_file_with_version_check(p, v, force); |
0974a682 | 495 | if (k < 0 && r == 0) |
d3226d77 | 496 | r = k; |
0974a682 KS |
497 | } |
498 | ||
499 | return r; | |
7b4d7cc0 KS |
500 | } |
501 | ||
0974a682 KS |
502 | static int install_binaries(const char *esp_path, bool force) { |
503 | struct dirent *de; | |
d3226d77 | 504 | _cleanup_closedir_ DIR *d = NULL; |
0974a682 KS |
505 | int r = 0; |
506 | ||
507 | if (force) { | |
508 | /* Don't create any of these directories when we are | |
509 | * just updating. When we update we'll drop-in our | |
510 | * files (unless there are newer ones already), but we | |
511 | * won't create the directories for them in the first | |
512 | * place. */ | |
513 | r = create_dirs(esp_path); | |
514 | if (r < 0) | |
515 | return r; | |
516 | } | |
517 | ||
e7dd673d | 518 | d = opendir(BOOTLIBDIR); |
d3226d77 ZJS |
519 | if (!d) |
520 | return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m"); | |
0974a682 | 521 | |
e41256dc | 522 | FOREACH_DIRENT(de, d, break) { |
0974a682 KS |
523 | int k; |
524 | ||
d3226d77 | 525 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
526 | continue; |
527 | ||
528 | k = copy_one_file(esp_path, de->d_name, force); | |
529 | if (k < 0 && r == 0) | |
530 | r = k; | |
531 | } | |
532 | ||
0974a682 | 533 | return r; |
7b4d7cc0 KS |
534 | } |
535 | ||
0974a682 | 536 | static bool same_entry(uint16_t id, const sd_id128_t uuid, const char *path) { |
d3226d77 | 537 | _cleanup_free_ char *opath = NULL; |
0974a682 | 538 | sd_id128_t ouuid; |
d3226d77 | 539 | int r; |
7b4d7cc0 | 540 | |
d3226d77 ZJS |
541 | r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL); |
542 | if (r < 0) | |
0974a682 KS |
543 | return false; |
544 | if (!sd_id128_equal(uuid, ouuid)) | |
d3226d77 | 545 | return false; |
0974a682 | 546 | if (!streq_ptr(path, opath)) |
d3226d77 | 547 | return false; |
0974a682 | 548 | |
d3226d77 | 549 | return true; |
0974a682 KS |
550 | } |
551 | ||
552 | static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) { | |
d3226d77 ZJS |
553 | _cleanup_free_ uint16_t *options = NULL; |
554 | int n, i; | |
0974a682 | 555 | |
d3226d77 ZJS |
556 | n = efi_get_boot_options(&options); |
557 | if (n < 0) | |
558 | return n; | |
0974a682 | 559 | |
e7dd673d | 560 | /* find already existing systemd-boot entry */ |
d3226d77 | 561 | for (i = 0; i < n; i++) |
0974a682 | 562 | if (same_entry(options[i], uuid, path)) { |
d3226d77 ZJS |
563 | *id = options[i]; |
564 | return 1; | |
0974a682 KS |
565 | } |
566 | ||
567 | /* find free slot in the sorted BootXXXX variable list */ | |
d3226d77 | 568 | for (i = 0; i < n; i++) |
0974a682 | 569 | if (i != options[i]) { |
d3226d77 ZJS |
570 | *id = i; |
571 | return 1; | |
0974a682 KS |
572 | } |
573 | ||
574 | /* use the next one */ | |
575 | if (i == 0xffff) | |
576 | return -ENOSPC; | |
d3226d77 ZJS |
577 | *id = i; |
578 | return 0; | |
0974a682 KS |
579 | } |
580 | ||
581 | static int insert_into_order(uint16_t slot, bool first) { | |
d3226d77 ZJS |
582 | _cleanup_free_ uint16_t *order = NULL; |
583 | uint16_t *t; | |
584 | int n, i; | |
0974a682 | 585 | |
d3226d77 ZJS |
586 | n = efi_get_boot_order(&order); |
587 | if (n <= 0) | |
0974a682 | 588 | /* no entry, add us */ |
d3226d77 | 589 | return efi_set_boot_order(&slot, 1); |
0974a682 KS |
590 | |
591 | /* are we the first and only one? */ | |
d3226d77 ZJS |
592 | if (n == 1 && order[0] == slot) |
593 | return 0; | |
0974a682 KS |
594 | |
595 | /* are we already in the boot order? */ | |
d3226d77 | 596 | for (i = 0; i < n; i++) { |
0974a682 KS |
597 | if (order[i] != slot) |
598 | continue; | |
599 | ||
600 | /* we do not require to be the first one, all is fine */ | |
601 | if (!first) | |
d3226d77 | 602 | return 0; |
0974a682 KS |
603 | |
604 | /* move us to the first slot */ | |
d3226d77 | 605 | memmove(order + 1, order, i * sizeof(uint16_t)); |
0974a682 | 606 | order[0] = slot; |
d3226d77 | 607 | return efi_set_boot_order(order, n); |
0974a682 KS |
608 | } |
609 | ||
610 | /* extend array */ | |
d3226d77 ZJS |
611 | t = realloc(order, (n + 1) * sizeof(uint16_t)); |
612 | if (!t) | |
613 | return -ENOMEM; | |
614 | order = t; | |
0974a682 KS |
615 | |
616 | /* add us to the top or end of the list */ | |
617 | if (first) { | |
d3226d77 | 618 | memmove(order + 1, order, n * sizeof(uint16_t)); |
0974a682 KS |
619 | order[0] = slot; |
620 | } else | |
d3226d77 | 621 | order[n] = slot; |
0974a682 | 622 | |
d3226d77 | 623 | return efi_set_boot_order(order, n + 1); |
0974a682 KS |
624 | } |
625 | ||
626 | static int remove_from_order(uint16_t slot) { | |
7cb0f263 | 627 | _cleanup_free_ uint16_t *order = NULL; |
d3226d77 | 628 | int n, i; |
0974a682 | 629 | |
d3226d77 ZJS |
630 | n = efi_get_boot_order(&order); |
631 | if (n <= 0) | |
632 | return n; | |
0974a682 | 633 | |
d3226d77 | 634 | for (i = 0; i < n; i++) { |
0974a682 KS |
635 | if (order[i] != slot) |
636 | continue; | |
637 | ||
d3226d77 ZJS |
638 | if (i + 1 < n) |
639 | memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t)); | |
640 | return efi_set_boot_order(order, n - 1); | |
0974a682 KS |
641 | } |
642 | ||
d3226d77 | 643 | return 0; |
0974a682 KS |
644 | } |
645 | ||
646 | static int install_variables(const char *esp_path, | |
647 | uint32_t part, uint64_t pstart, uint64_t psize, | |
648 | sd_id128_t uuid, const char *path, | |
649 | bool first) { | |
d3226d77 | 650 | char *p; |
0974a682 KS |
651 | uint16_t slot; |
652 | int r; | |
653 | ||
654 | if (!is_efi_boot()) { | |
d3226d77 | 655 | log_warning("Not booted with EFI, skipping EFI variable setup."); |
0974a682 KS |
656 | return 0; |
657 | } | |
658 | ||
d3226d77 | 659 | p = strjoina(esp_path, path); |
0974a682 KS |
660 | if (access(p, F_OK) < 0) { |
661 | if (errno == ENOENT) | |
d3226d77 | 662 | return 0; |
f939cff7 LP |
663 | |
664 | return log_error_errno(errno, "Cannot access \"%s\": %m", p); | |
0974a682 | 665 | } |
7b4d7cc0 | 666 | |
0974a682 | 667 | r = find_slot(uuid, path, &slot); |
d3226d77 ZJS |
668 | if (r < 0) |
669 | return log_error_errno(r, | |
670 | r == -ENOENT ? | |
671 | "Failed to access EFI variables. Is the \"efivarfs\" filesystem mounted?" : | |
672 | "Failed to determine current boot order: %m"); | |
7b4d7cc0 | 673 | |
181ccb43 | 674 | if (first || r == 0) { |
0974a682 KS |
675 | r = efi_add_boot_option(slot, "Linux Boot Manager", |
676 | part, pstart, psize, | |
677 | uuid, path); | |
d3226d77 ZJS |
678 | if (r < 0) |
679 | return log_error_errno(r, "Failed to create EFI Boot variable entry: %m"); | |
0974a682 | 680 | |
d3226d77 ZJS |
681 | log_info("Created EFI boot entry \"Linux Boot Manager\"."); |
682 | } | |
0974a682 | 683 | |
d3226d77 | 684 | return insert_into_order(slot, first); |
0974a682 KS |
685 | } |
686 | ||
687 | static int remove_boot_efi(const char *esp_path) { | |
d3226d77 ZJS |
688 | char *p; |
689 | _cleanup_closedir_ DIR *d = NULL; | |
0974a682 | 690 | struct dirent *de; |
d3226d77 | 691 | int r, c = 0; |
0974a682 | 692 | |
00f69504 | 693 | p = strjoina(esp_path, "/EFI/BOOT"); |
0974a682 KS |
694 | d = opendir(p); |
695 | if (!d) { | |
d3226d77 ZJS |
696 | if (errno == ENOENT) |
697 | return 0; | |
0974a682 | 698 | |
d3226d77 | 699 | return log_error_errno(errno, "Failed to open directory \"%s\": %m", p); |
0974a682 KS |
700 | } |
701 | ||
e41256dc | 702 | FOREACH_DIRENT(de, d, break) { |
d3226d77 ZJS |
703 | _cleanup_close_ int fd = -1; |
704 | _cleanup_free_ char *v = NULL; | |
0974a682 | 705 | |
d3226d77 | 706 | if (!endswith_no_case(de->d_name, ".efi")) |
0974a682 KS |
707 | continue; |
708 | ||
b7536c45 | 709 | if (!startswith_no_case(de->d_name, "boot")) |
0974a682 KS |
710 | continue; |
711 | ||
d3226d77 | 712 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC); |
dd114e11 | 713 | if (fd < 0) |
d3226d77 | 714 | return log_error_errno(errno, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name); |
0974a682 | 715 | |
d3226d77 | 716 | r = get_file_version(fd, &v); |
0974a682 | 717 | if (r < 0) |
d3226d77 ZJS |
718 | return r; |
719 | if (r > 0 && startswith(v, "systemd-boot ")) { | |
720 | r = unlinkat(dirfd(d), de->d_name, 0); | |
721 | if (r < 0) | |
722 | return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name); | |
723 | ||
a592ab6a | 724 | log_info("Removed \"%s/%s\".", p, de->d_name); |
0974a682 KS |
725 | } |
726 | ||
727 | c++; | |
0974a682 KS |
728 | } |
729 | ||
d3226d77 | 730 | return c; |
0974a682 KS |
731 | } |
732 | ||
733 | static int rmdir_one(const char *prefix, const char *suffix) { | |
734 | char *p; | |
7b4d7cc0 | 735 | |
d3226d77 | 736 | p = strjoina(prefix, "/", suffix); |
0974a682 | 737 | if (rmdir(p) < 0) { |
d3226d77 ZJS |
738 | if (!IN_SET(errno, ENOENT, ENOTEMPTY)) |
739 | return log_error_errno(errno, "Failed to remove \"%s\": %m", p); | |
7b4d7cc0 | 740 | } else |
d3226d77 | 741 | log_info("Removed \"%s\".", p); |
7b4d7cc0 | 742 | |
0974a682 | 743 | return 0; |
7b4d7cc0 KS |
744 | } |
745 | ||
0974a682 KS |
746 | static int remove_binaries(const char *esp_path) { |
747 | char *p; | |
748 | int r, q; | |
d3226d77 | 749 | unsigned i; |
0974a682 | 750 | |
d3226d77 | 751 | p = strjoina(esp_path, "/EFI/systemd"); |
c6878637 | 752 | r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); |
0974a682 KS |
753 | |
754 | q = remove_boot_efi(esp_path); | |
755 | if (q < 0 && r == 0) | |
756 | r = q; | |
757 | ||
88a00ac5 | 758 | for (i = ELEMENTSOF(efi_subdirs)-1; i > 0; i--) { |
d3226d77 ZJS |
759 | q = rmdir_one(esp_path, efi_subdirs[i-1]); |
760 | if (q < 0 && r == 0) | |
761 | r = q; | |
762 | } | |
0974a682 KS |
763 | |
764 | return r; | |
765 | } | |
766 | ||
767 | static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) { | |
768 | uint16_t slot; | |
769 | int r; | |
770 | ||
771 | if (!is_efi_boot()) | |
772 | return 0; | |
773 | ||
774 | r = find_slot(uuid, path, &slot); | |
775 | if (r != 1) | |
776 | return 0; | |
777 | ||
778 | r = efi_remove_boot_option(slot); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
782 | if (in_order) | |
d3226d77 | 783 | return remove_from_order(slot); |
f939cff7 LP |
784 | |
785 | return 0; | |
0974a682 KS |
786 | } |
787 | ||
788 | static int install_loader_config(const char *esp_path) { | |
0974a682 | 789 | |
d5ff6d6d | 790 | char machine_string[SD_ID128_STRING_MAX]; |
f5b84de2 LP |
791 | _cleanup_(unlink_and_freep) char *t = NULL; |
792 | _cleanup_fclose_ FILE *f = NULL; | |
d5ff6d6d LP |
793 | sd_id128_t machine_id; |
794 | const char *p; | |
f5b84de2 | 795 | int r, fd; |
0974a682 | 796 | |
d5ff6d6d LP |
797 | r = sd_id128_get_machine(&machine_id); |
798 | if (r < 0) | |
0675e94a | 799 | return log_error_errno(r, "Failed to get machine id: %m"); |
0974a682 | 800 | |
d5ff6d6d | 801 | p = strjoina(esp_path, "/loader/loader.conf"); |
f5b84de2 LP |
802 | |
803 | if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */ | |
804 | return 0; | |
805 | ||
806 | fd = open_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t); | |
807 | if (fd < 0) | |
808 | return log_error_errno(fd, "Failed to open \"%s\" for writing: %m", p); | |
809 | ||
810 | f = fdopen(fd, "we"); | |
811 | if (!f) { | |
812 | safe_close(fd); | |
813 | return log_oom(); | |
814 | } | |
0974a682 | 815 | |
d5ff6d6d | 816 | fprintf(f, "#timeout 3\n"); |
d37b0737 | 817 | fprintf(f, "#console-mode keep\n"); |
d5ff6d6d | 818 | fprintf(f, "default %s-*\n", sd_id128_to_string(machine_id, machine_string)); |
0974a682 | 819 | |
0675e94a | 820 | r = fflush_sync_and_check(f); |
d5ff6d6d LP |
821 | if (r < 0) |
822 | return log_error_errno(r, "Failed to write \"%s\": %m", p); | |
0974a682 | 823 | |
f5b84de2 LP |
824 | r = link_tmpfile(fd, t, p); |
825 | if (r == -EEXIST) | |
826 | return 0; /* Silently skip creation if the file exists now (recheck) */ | |
827 | if (r < 0) | |
828 | return log_error_errno(r, "Failed to move \"%s\" into place: %m", p); | |
829 | ||
830 | t = mfree(t); | |
831 | ||
832 | return 1; | |
0974a682 KS |
833 | } |
834 | ||
2f2c539c LP |
835 | static int help(int argc, char *argv[], void *userdata) { |
836 | ||
0974a682 KS |
837 | printf("%s [COMMAND] [OPTIONS...]\n" |
838 | "\n" | |
68cc17f1 | 839 | "Install, update or remove the systemd-boot EFI boot manager.\n\n" |
0974a682 KS |
840 | " -h --help Show this help\n" |
841 | " --version Print version\n" | |
842 | " --path=PATH Path to the EFI System Partition (ESP)\n" | |
30b50477 | 843 | " -p --print-path Print path to the EFI partition\n" |
0974a682 KS |
844 | " --no-variables Don't touch EFI variables\n" |
845 | "\n" | |
71744250 | 846 | "Commands:\n" |
e7dd673d | 847 | " status Show status of installed systemd-boot and EFI variables\n" |
7e87c7d9 | 848 | " list List boot entries\n" |
e7dd673d TG |
849 | " install Install systemd-boot to the ESP and EFI variables\n" |
850 | " update Update systemd-boot in the ESP and EFI variables\n" | |
851 | " remove Remove systemd-boot from the ESP and EFI variables\n", | |
0974a682 KS |
852 | program_invocation_short_name); |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
0974a682 KS |
857 | static int parse_argv(int argc, char *argv[]) { |
858 | enum { | |
859 | ARG_PATH = 0x100, | |
860 | ARG_VERSION, | |
861 | ARG_NO_VARIABLES, | |
7b4d7cc0 KS |
862 | }; |
863 | ||
0974a682 KS |
864 | static const struct option options[] = { |
865 | { "help", no_argument, NULL, 'h' }, | |
866 | { "version", no_argument, NULL, ARG_VERSION }, | |
867 | { "path", required_argument, NULL, ARG_PATH }, | |
30b50477 | 868 | { "print-path", no_argument, NULL, 'p' }, |
0974a682 KS |
869 | { "no-variables", no_argument, NULL, ARG_NO_VARIABLES }, |
870 | { NULL, 0, NULL, 0 } | |
871 | }; | |
872 | ||
2f2c539c | 873 | int c, r; |
7b4d7cc0 KS |
874 | |
875 | assert(argc >= 0); | |
876 | assert(argv); | |
877 | ||
30b50477 | 878 | while ((c = getopt_long(argc, argv, "hp", options, NULL)) >= 0) |
0974a682 | 879 | switch (c) { |
7b4d7cc0 | 880 | |
0974a682 | 881 | case 'h': |
2f2c539c | 882 | help(0, NULL, NULL); |
7b4d7cc0 | 883 | return 0; |
7b4d7cc0 | 884 | |
0974a682 | 885 | case ARG_VERSION: |
3f6fd1ba | 886 | return version(); |
7b4d7cc0 | 887 | |
0974a682 | 888 | case ARG_PATH: |
2f2c539c LP |
889 | r = free_and_strdup(&arg_path, optarg); |
890 | if (r < 0) | |
891 | return log_oom(); | |
0974a682 KS |
892 | break; |
893 | ||
30b50477 ZJS |
894 | case 'p': |
895 | arg_print_path = true; | |
896 | break; | |
897 | ||
0974a682 KS |
898 | case ARG_NO_VARIABLES: |
899 | arg_touch_variables = false; | |
900 | break; | |
901 | ||
902 | case '?': | |
903 | return -EINVAL; | |
904 | ||
905 | default: | |
d3226d77 | 906 | assert_not_reached("Unknown option"); |
7b4d7cc0 | 907 | } |
7b4d7cc0 | 908 | |
0974a682 KS |
909 | return 1; |
910 | } | |
7b4d7cc0 | 911 | |
551710cf ZJS |
912 | static void read_loader_efi_var(const char *name, char **var) { |
913 | int r; | |
914 | ||
915 | r = efi_get_variable_string(EFI_VENDOR_LOADER, name, var); | |
916 | if (r < 0 && r != -ENOENT) | |
917 | log_warning_errno(r, "Failed to read EFI variable %s: %m", name); | |
918 | } | |
919 | ||
2f2c539c | 920 | static int verb_status(int argc, char *argv[], void *userdata) { |
0974a682 | 921 | |
2f2c539c | 922 | sd_id128_t uuid = SD_ID128_NULL; |
46fb255b | 923 | int r, k; |
0974a682 | 924 | |
5caa3167 | 925 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &uuid); |
0974a682 | 926 | |
30b50477 | 927 | if (arg_print_path) { |
5caa3167 LP |
928 | if (r == -EACCES) /* If we couldn't acquire the ESP path, log about access errors (which is the only |
929 | * error the find_esp_and_warn() won't log on its own) */ | |
930 | return log_error_errno(r, "Failed to determine ESP: %m"); | |
30b50477 ZJS |
931 | if (r < 0) |
932 | return r; | |
933 | ||
934 | puts(arg_path); | |
935 | return 0; | |
936 | } | |
551710cf | 937 | |
5caa3167 LP |
938 | r = 0; /* If we couldn't determine the path, then don't consider that a problem from here on, just show what we |
939 | * can show */ | |
940 | ||
2f2c539c | 941 | if (is_efi_boot()) { |
81375b9b | 942 | _cleanup_free_ char *fw_type = NULL, *fw_info = NULL, *loader = NULL, *loader_path = NULL, *stub = NULL; |
25579a43 | 943 | sd_id128_t loader_part_uuid = SD_ID128_NULL; |
0974a682 | 944 | |
2f2c539c LP |
945 | read_loader_efi_var("LoaderFirmwareType", &fw_type); |
946 | read_loader_efi_var("LoaderFirmwareInfo", &fw_info); | |
947 | read_loader_efi_var("LoaderInfo", &loader); | |
81375b9b | 948 | read_loader_efi_var("StubInfo", &stub); |
2f2c539c | 949 | read_loader_efi_var("LoaderImageIdentifier", &loader_path); |
551710cf | 950 | |
2f2c539c LP |
951 | if (loader_path) |
952 | efi_tilt_backslashes(loader_path); | |
953 | ||
46fb255b ZJS |
954 | k = efi_loader_get_device_part_uuid(&loader_part_uuid); |
955 | if (k < 0 && k != -ENOENT) | |
956 | r = log_warning_errno(k, "Failed to read EFI variable LoaderDevicePartUUID: %m"); | |
2f2c539c LP |
957 | |
958 | printf("System:\n"); | |
959 | printf(" Firmware: %s (%s)\n", strna(fw_type), strna(fw_info)); | |
960 | ||
46fb255b ZJS |
961 | k = is_efi_secure_boot(); |
962 | if (k < 0) | |
963 | r = log_warning_errno(k, "Failed to query secure boot status: %m"); | |
2f2c539c | 964 | else |
46fb255b | 965 | printf(" Secure Boot: %sd\n", enable_disable(k)); |
0974a682 | 966 | |
46fb255b ZJS |
967 | k = is_efi_secure_boot_setup_mode(); |
968 | if (k < 0) | |
969 | r = log_warning_errno(k, "Failed to query secure boot mode: %m"); | |
2f2c539c | 970 | else |
46fb255b | 971 | printf(" Setup Mode: %s\n", k ? "setup" : "user"); |
2f2c539c LP |
972 | printf("\n"); |
973 | ||
cd2d4c7f | 974 | printf("Current Loader:\n"); |
2f2c539c | 975 | printf(" Product: %s\n", strna(loader)); |
81375b9b ДГ |
976 | if (stub) |
977 | printf(" Stub: %s\n", stub); | |
e28973ee | 978 | if (!sd_id128_is_null(loader_part_uuid)) |
cd2d4c7f | 979 | printf(" ESP: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", |
2f2c539c LP |
980 | SD_ID128_FORMAT_VAL(loader_part_uuid)); |
981 | else | |
cd2d4c7f | 982 | printf(" ESP: n/a\n"); |
2f2c539c LP |
983 | printf(" File: %s%s\n", special_glyph(TREE_RIGHT), strna(loader_path)); |
984 | printf("\n"); | |
985 | } else | |
cd2d4c7f | 986 | printf("System:\n Not booted with EFI\n\n"); |
7b4d7cc0 | 987 | |
ecec2a5d LP |
988 | if (arg_path) { |
989 | k = status_binaries(arg_path, uuid); | |
990 | if (k < 0) | |
991 | r = k; | |
992 | } | |
2f2c539c | 993 | |
cd2d4c7f | 994 | if (is_efi_boot()) { |
46fb255b ZJS |
995 | k = status_variables(); |
996 | if (k < 0) | |
997 | r = k; | |
cd2d4c7f | 998 | } |
0974a682 | 999 | |
ecec2a5d LP |
1000 | if (arg_path) { |
1001 | k = status_entries(arg_path, uuid); | |
1002 | if (k < 0) | |
1003 | r = k; | |
1004 | } | |
7e87c7d9 | 1005 | |
46fb255b | 1006 | return r; |
2f2c539c | 1007 | } |
7b4d7cc0 | 1008 | |
7e87c7d9 | 1009 | static int verb_list(int argc, char *argv[], void *userdata) { |
5caa3167 | 1010 | _cleanup_(boot_config_free) BootConfig config = {}; |
7e87c7d9 | 1011 | sd_id128_t uuid = SD_ID128_NULL; |
7e87c7d9 | 1012 | unsigned n; |
5caa3167 | 1013 | int r; |
7e87c7d9 | 1014 | |
5caa3167 LP |
1015 | /* If we lack privileges we invoke find_esp_and_warn() in "unprivileged mode" here, which does two things: turn |
1016 | * off logging about access errors and turn off potentially privileged device probing. Here we're interested in | |
1017 | * the latter but not the former, hence request the mode, and log about EACCES. */ | |
7e87c7d9 | 1018 | |
5caa3167 LP |
1019 | r = acquire_esp(geteuid() != 0, NULL, NULL, NULL, &uuid); |
1020 | if (r == -EACCES) /* We really need the ESP path for this call, hence also log about access errors */ | |
1021 | return log_error_errno(r, "Failed to determine ESP: %m"); | |
7e87c7d9 ZJS |
1022 | if (r < 0) |
1023 | return r; | |
1024 | ||
1025 | r = boot_entries_load_config(arg_path, &config); | |
1026 | if (r < 0) | |
1027 | return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", | |
1028 | arg_path); | |
1029 | ||
1030 | printf("Available boot entries:\n"); | |
1031 | ||
1032 | for (n = 0; n < config.n_entries; n++) { | |
1033 | const BootEntry *e = &config.entries[n]; | |
1034 | ||
1035 | printf(" title: %s%s%s%s%s%s\n", | |
1036 | ansi_highlight(), | |
64f05708 | 1037 | boot_entry_title(e), |
7e87c7d9 ZJS |
1038 | ansi_normal(), |
1039 | ansi_highlight_green(), | |
5364c686 | 1040 | n == (unsigned) config.default_entry ? " (default)" : "", |
7e87c7d9 ZJS |
1041 | ansi_normal()); |
1042 | if (e->version) | |
1043 | printf(" version: %s\n", e->version); | |
1044 | if (e->machine_id) | |
1045 | printf(" machine-id: %s\n", e->machine_id); | |
1046 | if (e->architecture) | |
1047 | printf(" architecture: %s\n", e->architecture); | |
1048 | if (e->kernel) | |
1049 | printf(" linux: %s\n", e->kernel); | |
1050 | if (!strv_isempty(e->initrd)) { | |
1051 | _cleanup_free_ char *t; | |
1052 | ||
1053 | t = strv_join(e->initrd, " "); | |
1054 | if (!t) | |
1055 | return log_oom(); | |
1056 | ||
1057 | printf(" initrd: %s\n", t); | |
1058 | } | |
1059 | if (!strv_isempty(e->options)) { | |
1060 | _cleanup_free_ char *t; | |
1061 | ||
1062 | t = strv_join(e->options, " "); | |
1063 | if (!t) | |
1064 | return log_oom(); | |
1065 | ||
1066 | printf(" options: %s\n", t); | |
1067 | } | |
1068 | if (e->device_tree) | |
1069 | printf(" devicetree: %s\n", e->device_tree); | |
1070 | ||
1071 | puts(""); | |
cd2d4c7f | 1072 | } |
0974a682 | 1073 | |
7e87c7d9 | 1074 | return 0; |
2f2c539c | 1075 | } |
7b4d7cc0 | 1076 | |
2f2c539c | 1077 | static int verb_install(int argc, char *argv[], void *userdata) { |
0974a682 | 1078 | |
2f2c539c LP |
1079 | sd_id128_t uuid = SD_ID128_NULL; |
1080 | uint64_t pstart = 0, psize = 0; | |
1081 | uint32_t part = 0; | |
1082 | bool install; | |
1083 | int r; | |
1084 | ||
5caa3167 | 1085 | r = acquire_esp(false, &part, &pstart, &psize, &uuid); |
2f2c539c LP |
1086 | if (r < 0) |
1087 | return r; | |
1088 | ||
1089 | install = streq(argv[0], "install"); | |
1090 | ||
1091 | RUN_WITH_UMASK(0002) { | |
1092 | r = install_binaries(arg_path, install); | |
0974a682 | 1093 | if (r < 0) |
d3226d77 | 1094 | return r; |
0974a682 | 1095 | |
2f2c539c | 1096 | if (install) { |
d3226d77 ZJS |
1097 | r = install_loader_config(arg_path); |
1098 | if (r < 0) | |
1099 | return r; | |
1100 | } | |
2f2c539c | 1101 | } |
0974a682 | 1102 | |
2f2c539c LP |
1103 | if (arg_touch_variables) |
1104 | r = install_variables(arg_path, | |
1105 | part, pstart, psize, uuid, | |
1106 | "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", | |
1107 | install); | |
7b4d7cc0 | 1108 | |
2f2c539c LP |
1109 | return r; |
1110 | } | |
0974a682 | 1111 | |
2f2c539c LP |
1112 | static int verb_remove(int argc, char *argv[], void *userdata) { |
1113 | sd_id128_t uuid = SD_ID128_NULL; | |
1114 | int r; | |
0974a682 | 1115 | |
5caa3167 | 1116 | r = acquire_esp(false, NULL, NULL, NULL, &uuid); |
2f2c539c LP |
1117 | if (r < 0) |
1118 | return r; | |
1119 | ||
1120 | r = remove_binaries(arg_path); | |
1121 | ||
1122 | if (arg_touch_variables) { | |
1123 | int q; | |
1124 | ||
1125 | q = remove_variables(uuid, "/EFI/systemd/systemd-boot" EFI_MACHINE_TYPE_NAME ".efi", true); | |
1126 | if (q < 0 && r == 0) | |
1127 | r = q; | |
7b4d7cc0 KS |
1128 | } |
1129 | ||
d3226d77 | 1130 | return r; |
7b4d7cc0 KS |
1131 | } |
1132 | ||
2f2c539c LP |
1133 | static int bootctl_main(int argc, char *argv[]) { |
1134 | ||
1135 | static const Verb verbs[] = { | |
c56d1e2c CW |
1136 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
1137 | { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status }, | |
1138 | { "list", VERB_ANY, 1, 0, verb_list }, | |
1139 | { "install", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_install }, | |
1140 | { "update", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_install }, | |
1141 | { "remove", VERB_ANY, 1, VERB_MUST_BE_ROOT, verb_remove }, | |
2f2c539c LP |
1142 | {} |
1143 | }; | |
1144 | ||
1145 | return dispatch_verb(argc, argv, verbs, NULL); | |
1146 | } | |
1147 | ||
7b4d7cc0 | 1148 | int main(int argc, char *argv[]) { |
601185b4 | 1149 | int r; |
7b4d7cc0 KS |
1150 | |
1151 | log_parse_environment(); | |
1152 | log_open(); | |
1153 | ||
2f2c539c LP |
1154 | /* If we run in a container, automatically turn of EFI file system access */ |
1155 | if (detect_container() > 0) | |
1156 | arg_touch_variables = false; | |
1157 | ||
7b4d7cc0 | 1158 | r = parse_argv(argc, argv); |
601185b4 | 1159 | if (r <= 0) |
7b4d7cc0 | 1160 | goto finish; |
7b4d7cc0 KS |
1161 | |
1162 | r = bootctl_main(argc, argv); | |
601185b4 ZJS |
1163 | |
1164 | finish: | |
2f2c539c | 1165 | free(arg_path); |
601185b4 | 1166 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
7b4d7cc0 | 1167 | } |