]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bootspec.c
bootspec: also parse new 'beep' loader.conf variable
[thirdparty/systemd.git] / src / shared / bootspec.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
7e87c7d9
ZJS
2
3#include <stdio.h>
af918182 4#include <linux/magic.h>
ca78ad1d 5#include <unistd.h>
7e87c7d9 6
cedb9eec 7#include "sd-device.h"
dccca82b
LP
8#include "sd-id128.h"
9
7e87c7d9 10#include "alloc-util.h"
af918182 11#include "blkid-util.h"
c2caeb5d 12#include "bootspec-fundamental.h"
7e87c7d9
ZJS
13#include "bootspec.h"
14#include "conf-files.h"
15#include "def.h"
c67f84b0 16#include "device-nodes.h"
5e146a75 17#include "dirent-util.h"
7e87c7d9 18#include "efivars.h"
0bb2f0f1 19#include "efi-loader.h"
5e146a75 20#include "env-file.h"
8cbb7d87 21#include "env-util.h"
7c248223 22#include "errno-util.h"
7e87c7d9
ZJS
23#include "fd-util.h"
24#include "fileio.h"
af918182 25#include "parse-util.h"
cc7a0bfa 26#include "path-util.h"
5e146a75 27#include "pe-header.h"
760877e9 28#include "sort-util.h"
af918182 29#include "stat-util.h"
93f14ce2 30#include "string-table.h"
7e87c7d9
ZJS
31#include "string-util.h"
32#include "strv.h"
5e146a75 33#include "unaligned.h"
ca78ad1d 34#include "util.h"
af918182 35#include "virt.h"
7e87c7d9 36
0de2e1fd 37static void boot_entry_free(BootEntry *entry) {
4fe2ba0e 38 assert(entry);
7e87c7d9 39
12580bc3 40 free(entry->id);
15b82eec 41 free(entry->id_old);
2d3bfb69 42 free(entry->path);
43b736a8 43 free(entry->root);
7e87c7d9 44 free(entry->title);
64f05708 45 free(entry->show_title);
7e87c7d9
ZJS
46 free(entry->version);
47 free(entry->machine_id);
48 free(entry->architecture);
49 strv_free(entry->options);
50 free(entry->kernel);
51 free(entry->efi);
52 strv_free(entry->initrd);
53 free(entry->device_tree);
fdc5c042 54 strv_free(entry->device_tree_overlay);
7e87c7d9
ZJS
55}
56
43b736a8
LP
57static int boot_entry_load(
58 const char *root,
59 const char *path,
60 BootEntry *entry) {
61
93f14ce2
LP
62 _cleanup_(boot_entry_free) BootEntry tmp = {
63 .type = BOOT_ENTRY_CONF,
64 };
65
7e87c7d9
ZJS
66 _cleanup_fclose_ FILE *f = NULL;
67 unsigned line = 1;
736783d4 68 char *c;
7e87c7d9
ZJS
69 int r;
70
43b736a8 71 assert(root);
4fe2ba0e
LP
72 assert(path);
73 assert(entry);
74
736783d4
LP
75 r = path_extract_filename(path, &tmp.id);
76 if (r < 0)
77 return log_error_errno(r, "Failed to extract file name from path '%s': %m", path);
7e87c7d9 78
736783d4
LP
79 c = endswith_no_case(tmp.id, ".conf");
80 if (!c)
81 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", tmp.id);
7e87c7d9 82
eed7210a 83 if (!efi_loader_entry_name_valid(tmp.id))
dfc22cb4 84 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id);
eed7210a 85
736783d4
LP
86 tmp.id_old = strndup(tmp.id, c - tmp.id);
87 if (!tmp.id_old)
88 return log_oom();
89
2d3bfb69
ZJS
90 tmp.path = strdup(path);
91 if (!tmp.path)
92 return log_oom();
93
43b736a8
LP
94 tmp.root = strdup(root);
95 if (!tmp.root)
96 return log_oom();
97
263195c6
YW
98 f = fopen(path, "re");
99 if (!f)
100 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
101
7e87c7d9 102 for (;;) {
f99fdc3e
YW
103 _cleanup_free_ char *buf = NULL, *field = NULL;
104 const char *p;
7e87c7d9
ZJS
105
106 r = read_line(f, LONG_LINE_MAX, &buf);
107 if (r == 0)
108 break;
109 if (r == -ENOBUFS)
110 return log_error_errno(r, "%s:%u: Line too long", path, line);
111 if (r < 0)
112 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
113
114 line++;
115
116 if (IN_SET(*strstrip(buf), '#', '\0'))
117 continue;
118
f99fdc3e
YW
119 p = buf;
120 r = extract_first_word(&p, &field, " \t", 0);
121 if (r < 0) {
122 log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line);
123 continue;
124 }
125 if (r == 0) {
7e87c7d9
ZJS
126 log_warning("%s:%u: Bad syntax", path, line);
127 continue;
128 }
7e87c7d9 129
f99fdc3e 130 if (streq(field, "title"))
7e87c7d9 131 r = free_and_strdup(&tmp.title, p);
f99fdc3e 132 else if (streq(field, "version"))
7e87c7d9 133 r = free_and_strdup(&tmp.version, p);
f99fdc3e 134 else if (streq(field, "machine-id"))
7e87c7d9 135 r = free_and_strdup(&tmp.machine_id, p);
f99fdc3e 136 else if (streq(field, "architecture"))
7e87c7d9 137 r = free_and_strdup(&tmp.architecture, p);
f99fdc3e 138 else if (streq(field, "options"))
7e87c7d9 139 r = strv_extend(&tmp.options, p);
f99fdc3e 140 else if (streq(field, "linux"))
7e87c7d9 141 r = free_and_strdup(&tmp.kernel, p);
f99fdc3e 142 else if (streq(field, "efi"))
7e87c7d9 143 r = free_and_strdup(&tmp.efi, p);
f99fdc3e 144 else if (streq(field, "initrd"))
7e87c7d9 145 r = strv_extend(&tmp.initrd, p);
f99fdc3e 146 else if (streq(field, "devicetree"))
7e87c7d9 147 r = free_and_strdup(&tmp.device_tree, p);
fdc5c042
LP
148 else if (streq(field, "devicetree-overlay")) {
149 _cleanup_strv_free_ char **l = NULL;
150
151 l = strv_split(p, NULL);
152 if (!l)
153 return log_oom();
154
155 r = strv_extend_strv(&tmp.device_tree_overlay, l, false);
156 } else {
feb41f1f 157 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path, line, field);
7e87c7d9
ZJS
158 continue;
159 }
160 if (r < 0)
161 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
162 }
163
164 *entry = tmp;
165 tmp = (BootEntry) {};
166 return 0;
167}
168
169void boot_config_free(BootConfig *config) {
da6053d0 170 size_t i;
7e87c7d9 171
4fe2ba0e
LP
172 assert(config);
173
7e87c7d9
ZJS
174 free(config->default_pattern);
175 free(config->timeout);
176 free(config->editor);
c1d4e298
JJ
177 free(config->auto_entries);
178 free(config->auto_firmware);
72263375 179 free(config->console_mode);
fe5a698f 180 free(config->random_seed_mode);
d403d8f0 181 free(config->beep);
7e87c7d9
ZJS
182
183 free(config->entry_oneshot);
184 free(config->entry_default);
185
186 for (i = 0; i < config->n_entries; i++)
187 boot_entry_free(config->entries + i);
188 free(config->entries);
189}
190
0de2e1fd 191static int boot_loader_read_conf(const char *path, BootConfig *config) {
7e87c7d9
ZJS
192 _cleanup_fclose_ FILE *f = NULL;
193 unsigned line = 1;
194 int r;
195
4fe2ba0e
LP
196 assert(path);
197 assert(config);
198
7e87c7d9 199 f = fopen(path, "re");
f91ed3dc
LP
200 if (!f) {
201 if (errno == ENOENT)
202 return 0;
203
7e87c7d9 204 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
f91ed3dc 205 }
7e87c7d9
ZJS
206
207 for (;;) {
f99fdc3e
YW
208 _cleanup_free_ char *buf = NULL, *field = NULL;
209 const char *p;
7e87c7d9
ZJS
210
211 r = read_line(f, LONG_LINE_MAX, &buf);
212 if (r == 0)
213 break;
214 if (r == -ENOBUFS)
215 return log_error_errno(r, "%s:%u: Line too long", path, line);
216 if (r < 0)
217 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
218
219 line++;
220
221 if (IN_SET(*strstrip(buf), '#', '\0'))
222 continue;
223
f99fdc3e
YW
224 p = buf;
225 r = extract_first_word(&p, &field, " \t", 0);
226 if (r < 0) {
227 log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line);
228 continue;
229 }
230 if (r == 0) {
7e87c7d9
ZJS
231 log_warning("%s:%u: Bad syntax", path, line);
232 continue;
233 }
7e87c7d9 234
f99fdc3e 235 if (streq(field, "default"))
7e87c7d9 236 r = free_and_strdup(&config->default_pattern, p);
f99fdc3e 237 else if (streq(field, "timeout"))
7e87c7d9 238 r = free_and_strdup(&config->timeout, p);
f99fdc3e 239 else if (streq(field, "editor"))
7e87c7d9 240 r = free_and_strdup(&config->editor, p);
790f84eb 241 else if (streq(field, "auto-entries"))
c1d4e298 242 r = free_and_strdup(&config->auto_entries, p);
790f84eb 243 else if (streq(field, "auto-firmware"))
c1d4e298 244 r = free_and_strdup(&config->auto_firmware, p);
790f84eb 245 else if (streq(field, "console-mode"))
d37b0737 246 r = free_and_strdup(&config->console_mode, p);
fe5a698f
YW
247 else if (streq(field, "random-seed-mode"))
248 r = free_and_strdup(&config->random_seed_mode, p);
d403d8f0
LP
249 else if (streq(field, "beep"))
250 r = free_and_strdup(&config->beep, p);
7e87c7d9 251 else {
feb41f1f 252 log_notice("%s:%u: Unknown line \"%s\", ignoring.", path, line, field);
7e87c7d9
ZJS
253 continue;
254 }
255 if (r < 0)
256 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
257 }
258
f91ed3dc 259 return 1;
7e87c7d9
ZJS
260}
261
93bab288 262static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
8087644a 263 return strverscmp_improved(a->id, b->id);
7e87c7d9
ZJS
264}
265
43b736a8
LP
266static int boot_entries_find(
267 const char *root,
268 const char *dir,
a2f8664e
LP
269 BootEntry **entries,
270 size_t *n_entries) {
43b736a8 271
7e87c7d9
ZJS
272 _cleanup_strv_free_ char **files = NULL;
273 char **f;
274 int r;
7e87c7d9 275
43b736a8 276 assert(root);
4fe2ba0e 277 assert(dir);
a2f8664e
LP
278 assert(entries);
279 assert(n_entries);
4fe2ba0e 280
36b12282 281 r = conf_files_list(&files, ".conf", NULL, 0, dir);
7e87c7d9
ZJS
282 if (r < 0)
283 return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
284
285 STRV_FOREACH(f, files) {
319a4f4b 286 if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
7e87c7d9
ZJS
287 return log_oom();
288
a2f8664e 289 r = boot_entry_load(root, *f, *entries + *n_entries);
7e87c7d9
ZJS
290 if (r < 0)
291 continue;
292
a2f8664e 293 (*n_entries) ++;
7e87c7d9
ZJS
294 }
295
7e87c7d9
ZJS
296 return 0;
297}
298
5e146a75
LP
299static int boot_entry_load_unified(
300 const char *root,
301 const char *path,
302 const char *osrelease,
303 const char *cmdline,
304 BootEntry *ret) {
305
c2caeb5d
LP
306 _cleanup_free_ char *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL,
307 *os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
93f14ce2
LP
308 _cleanup_(boot_entry_free) BootEntry tmp = {
309 .type = BOOT_ENTRY_UNIFIED,
310 };
c2caeb5d 311 const char *k, *good_name, *good_version;
5e146a75 312 _cleanup_fclose_ FILE *f = NULL;
5e146a75
LP
313 int r;
314
315 assert(root);
316 assert(path);
317 assert(osrelease);
318
319 k = path_startswith(path, root);
320 if (!k)
321 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
322
673a1e6f 323 f = fmemopen_unlocked((void*) osrelease, strlen(osrelease), "r");
5e146a75
LP
324 if (!f)
325 return log_error_errno(errno, "Failed to open os-release buffer: %m");
326
15b82eec
SM
327 r = parse_env_file(f, "os-release",
328 "PRETTY_NAME", &os_pretty_name,
c2caeb5d
LP
329 "IMAGE_ID", &os_image_id,
330 "NAME", &os_name,
15b82eec 331 "ID", &os_id,
c2caeb5d
LP
332 "IMAGE_VERSION", &os_image_version,
333 "VERSION", &os_version,
334 "VERSION_ID", &os_version_id,
335 "BUILD_ID", &os_build_id);
5e146a75
LP
336 if (r < 0)
337 return log_error_errno(r, "Failed to parse os-release data from unified kernel image %s: %m", path);
338
c2caeb5d
LP
339 if (!bootspec_pick_name_version(
340 os_pretty_name,
341 os_image_id,
342 os_name,
343 os_id,
344 os_image_version,
345 os_version,
346 os_version_id,
347 os_build_id,
348 &good_name,
349 &good_version))
5e146a75
LP
350 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Missing fields in os-release data from unified kernel image %s, refusing.", path);
351
c2caeb5d
LP
352 r = path_extract_filename(path, &tmp.id);
353 if (r < 0)
354 return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
5e146a75 355
eed7210a 356 if (!efi_loader_entry_name_valid(tmp.id))
dfc22cb4 357 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id);
eed7210a 358
c2caeb5d
LP
359 if (os_id && os_version_id) {
360 tmp.id_old = strjoin(os_id, "-", os_version_id);
361 if (!tmp.id_old)
362 return log_oom();
363 }
364
5e146a75
LP
365 tmp.path = strdup(path);
366 if (!tmp.path)
367 return log_oom();
368
369 tmp.root = strdup(root);
370 if (!tmp.root)
371 return log_oom();
372
373 tmp.kernel = strdup(skip_leading_chars(k, "/"));
374 if (!tmp.kernel)
375 return log_oom();
376
377 tmp.options = strv_new(skip_leading_chars(cmdline, WHITESPACE));
378 if (!tmp.options)
379 return log_oom();
380
381 delete_trailing_chars(tmp.options[0], WHITESPACE);
382
c2caeb5d
LP
383 tmp.title = strdup(good_name);
384 if (!tmp.title)
385 return log_oom();
386
387 tmp.version = strdup(good_version);
388 if (!tmp.version)
389 return log_oom();
5e146a75
LP
390
391 *ret = tmp;
392 tmp = (BootEntry) {};
393 return 0;
394}
395
396/* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
397 * the ones we do care about and we are willing to load into memory have this size limit.) */
398#define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
399
400static int find_sections(
401 int fd,
402 char **ret_osrelease,
403 char **ret_cmdline) {
404
405 _cleanup_free_ struct PeSectionHeader *sections = NULL;
406 _cleanup_free_ char *osrelease = NULL, *cmdline = NULL;
407 size_t i, n_sections;
408 struct DosFileHeader dos;
409 struct PeHeader pe;
410 uint64_t start;
411 ssize_t n;
412
413 n = pread(fd, &dos, sizeof(dos), 0);
414 if (n < 0)
415 return log_error_errno(errno, "Failed read DOS header: %m");
416 if (n != sizeof(dos))
417 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading DOS header, refusing.");
418
419 if (dos.Magic[0] != 'M' || dos.Magic[1] != 'Z')
420 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "DOS executable magic missing, refusing.");
421
422 start = unaligned_read_le32(&dos.ExeHeader);
423 n = pread(fd, &pe, sizeof(pe), start);
424 if (n < 0)
425 return log_error_errno(errno, "Failed to read PE header: %m");
426 if (n != sizeof(pe))
427 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading PE header, refusing.");
428
429 if (pe.Magic[0] != 'P' || pe.Magic[1] != 'E' || pe.Magic[2] != 0 || pe.Magic[3] != 0)
430 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE executable magic missing, refusing.");
431
432 n_sections = unaligned_read_le16(&pe.FileHeader.NumberOfSections);
433 if (n_sections > 96)
434 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "PE header has too many sections, refusing.");
435
436 sections = new(struct PeSectionHeader, n_sections);
437 if (!sections)
438 return log_oom();
439
440 n = pread(fd, sections,
441 n_sections * sizeof(struct PeSectionHeader),
442 start + sizeof(pe) + unaligned_read_le16(&pe.FileHeader.SizeOfOptionalHeader));
443 if (n < 0)
444 return log_error_errno(errno, "Failed to read section data: %m");
445 if ((size_t) n != n_sections * sizeof(struct PeSectionHeader))
446 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading sections, refusing.");
447
448 for (i = 0; i < n_sections; i++) {
449 _cleanup_free_ char *k = NULL;
450 uint32_t offset, size;
451 char **b;
452
453 if (strneq((char*) sections[i].Name, ".osrel", sizeof(sections[i].Name)))
454 b = &osrelease;
455 else if (strneq((char*) sections[i].Name, ".cmdline", sizeof(sections[i].Name)))
456 b = &cmdline;
457 else
458 continue;
459
460 if (*b)
461 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Duplicate section %s, refusing.", sections[i].Name);
462
463 offset = unaligned_read_le32(&sections[i].PointerToRawData);
464 size = unaligned_read_le32(&sections[i].VirtualSize);
465
466 if (size > PE_SECTION_SIZE_MAX)
467 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Section %s too large, refusing.", sections[i].Name);
468
469 k = new(char, size+1);
470 if (!k)
471 return log_oom();
472
473 n = pread(fd, k, size, offset);
474 if (n < 0)
475 return log_error_errno(errno, "Failed to read section payload: %m");
a75fcef8 476 if ((size_t) n != size)
5e146a75
LP
477 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading section payload, refusing:");
478
479 /* Allow one trailing NUL byte, but nothing more. */
480 if (size > 0 && memchr(k, 0, size - 1))
481 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Section contains embedded NUL byte: %m");
482
483 k[size] = 0;
484 *b = TAKE_PTR(k);
485 }
486
487 if (!osrelease)
488 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Image lacks .osrel section, refusing.");
489
490 if (ret_osrelease)
491 *ret_osrelease = TAKE_PTR(osrelease);
492 if (ret_cmdline)
493 *ret_cmdline = TAKE_PTR(cmdline);
494
495 return 0;
496}
497
498static int boot_entries_find_unified(
499 const char *root,
500 const char *dir,
501 BootEntry **entries,
502 size_t *n_entries) {
503
504 _cleanup_(closedirp) DIR *d = NULL;
5e146a75
LP
505 int r;
506
507 assert(root);
508 assert(dir);
509 assert(entries);
510 assert(n_entries);
511
512 d = opendir(dir);
513 if (!d) {
514 if (errno == ENOENT)
515 return 0;
516
517 return log_error_errno(errno, "Failed to open %s: %m", dir);
518 }
519
520 FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", dir)) {
521 _cleanup_free_ char *j = NULL, *osrelease = NULL, *cmdline = NULL;
522 _cleanup_close_ int fd = -1;
523
524 if (!dirent_is_file(de))
525 continue;
526
527 if (!endswith_no_case(de->d_name, ".efi"))
528 continue;
529
319a4f4b 530 if (!GREEDY_REALLOC0(*entries, *n_entries + 1))
5e146a75
LP
531 return log_oom();
532
533 fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
534 if (fd < 0) {
535 log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", dir, de->d_name);
536 continue;
537 }
538
539 r = fd_verify_regular(fd);
540 if (r < 0) {
dba33c4a 541 log_warning_errno(r, "File %s/%s is not regular, ignoring: %m", dir, de->d_name);
5e146a75
LP
542 continue;
543 }
544
545 r = find_sections(fd, &osrelease, &cmdline);
546 if (r < 0)
547 continue;
548
549 j = path_join(dir, de->d_name);
550 if (!j)
551 return log_oom();
552
553 r = boot_entry_load_unified(root, j, osrelease, cmdline, *entries + *n_entries);
554 if (r < 0)
555 continue;
556
557 (*n_entries) ++;
5e146a75
LP
558 }
559
5e146a75
LP
560 return 0;
561}
562
64f05708 563static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) {
da6053d0 564 size_t i, j;
64f05708
ZJS
565 bool non_unique = false;
566
4fe2ba0e
LP
567 assert(entries || n_entries == 0);
568 assert(arr || n_entries == 0);
569
64f05708
ZJS
570 for (i = 0; i < n_entries; i++)
571 arr[i] = false;
572
573 for (i = 0; i < n_entries; i++)
574 for (j = 0; j < n_entries; j++)
575 if (i != j && streq(boot_entry_title(entries + i),
576 boot_entry_title(entries + j)))
577 non_unique = arr[i] = arr[j] = true;
578
579 return non_unique;
580}
581
582static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
583 char *s;
da6053d0 584 size_t i;
64f05708
ZJS
585 int r;
586 bool arr[n_entries];
587
4fe2ba0e
LP
588 assert(entries || n_entries == 0);
589
64f05708
ZJS
590 /* Find _all_ non-unique titles */
591 if (!find_nonunique(entries, n_entries, arr))
592 return 0;
593
594 /* Add version to non-unique titles */
595 for (i = 0; i < n_entries; i++)
596 if (arr[i] && entries[i].version) {
597 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version);
598 if (r < 0)
599 return -ENOMEM;
600
601 free_and_replace(entries[i].show_title, s);
602 }
603
604 if (!find_nonunique(entries, n_entries, arr))
605 return 0;
606
607 /* Add machine-id to non-unique titles */
608 for (i = 0; i < n_entries; i++)
609 if (arr[i] && entries[i].machine_id) {
610 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id);
611 if (r < 0)
612 return -ENOMEM;
613
614 free_and_replace(entries[i].show_title, s);
615 }
616
617 if (!find_nonunique(entries, n_entries, arr))
618 return 0;
619
620 /* Add file name to non-unique titles */
621 for (i = 0; i < n_entries; i++)
622 if (arr[i]) {
12580bc3 623 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id);
64f05708
ZJS
624 if (r < 0)
625 return -ENOMEM;
626
627 free_and_replace(entries[i].show_title, s);
628 }
629
630 return 0;
631}
632
ad1afd60 633static int boot_entries_select_default(const BootConfig *config) {
7e87c7d9
ZJS
634 int i;
635
4fe2ba0e 636 assert(config);
388d2993
ZJS
637 assert(config->entries || config->n_entries == 0);
638
639 if (config->n_entries == 0) {
640 log_debug("Found no default boot entry :(");
641 return -1; /* -1 means "no default" */
642 }
4fe2ba0e 643
7e87c7d9
ZJS
644 if (config->entry_oneshot)
645 for (i = config->n_entries - 1; i >= 0; i--)
12580bc3
LP
646 if (streq(config->entry_oneshot, config->entries[i].id)) {
647 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
648 config->entries[i].id);
7e87c7d9
ZJS
649 return i;
650 }
651
652 if (config->entry_default)
653 for (i = config->n_entries - 1; i >= 0; i--)
12580bc3
LP
654 if (streq(config->entry_default, config->entries[i].id)) {
655 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
656 config->entries[i].id);
7e87c7d9
ZJS
657 return i;
658 }
659
660 if (config->default_pattern)
661 for (i = config->n_entries - 1; i >= 0; i--)
12580bc3
LP
662 if (fnmatch(config->default_pattern, config->entries[i].id, FNM_CASEFOLD) == 0) {
663 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
664 config->entries[i].id, config->default_pattern);
7e87c7d9
ZJS
665 return i;
666 }
667
388d2993
ZJS
668 log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].id);
669 return config->n_entries - 1;
7e87c7d9
ZJS
670}
671
a2f8664e
LP
672int boot_entries_load_config(
673 const char *esp_path,
674 const char *xbootldr_path,
675 BootConfig *config) {
676
7e87c7d9
ZJS
677 const char *p;
678 int r;
679
4fe2ba0e
LP
680 assert(config);
681
a2f8664e
LP
682 if (esp_path) {
683 p = strjoina(esp_path, "/loader/loader.conf");
684 r = boot_loader_read_conf(p, config);
685 if (r < 0)
686 return r;
7e87c7d9 687
a2f8664e
LP
688 p = strjoina(esp_path, "/loader/entries");
689 r = boot_entries_find(esp_path, p, &config->entries, &config->n_entries);
690 if (r < 0)
691 return r;
5e146a75
LP
692
693 p = strjoina(esp_path, "/EFI/Linux/");
694 r = boot_entries_find_unified(esp_path, p, &config->entries, &config->n_entries);
695 if (r < 0)
696 return r;
a2f8664e
LP
697 }
698
699 if (xbootldr_path) {
700 p = strjoina(xbootldr_path, "/loader/entries");
701 r = boot_entries_find(xbootldr_path, p, &config->entries, &config->n_entries);
702 if (r < 0)
703 return r;
5e146a75
LP
704
705 p = strjoina(xbootldr_path, "/EFI/Linux/");
706 r = boot_entries_find_unified(xbootldr_path, p, &config->entries, &config->n_entries);
707 if (r < 0)
708 return r;
a2f8664e 709 }
7e87c7d9 710
dd2bf34c
LP
711 typesafe_qsort(config->entries, config->n_entries, boot_entry_compare);
712
64f05708
ZJS
713 r = boot_entries_uniquify(config->entries, config->n_entries);
714 if (r < 0)
715 return log_error_errno(r, "Failed to uniquify boot entries: %m");
716
9c4a6c13 717 if (is_efi_boot()) {
e6f055cb 718 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), &config->entry_oneshot);
bd29f9de
ZJS
719 if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) {
720 log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\": %m");
721 if (r == -ENOMEM)
722 return r;
723 }
9c4a6c13 724
e6f055cb 725 r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryDefault), &config->entry_default);
bd29f9de
ZJS
726 if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) {
727 log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\": %m");
728 if (r == -ENOMEM)
729 return r;
730 }
9c4a6c13 731 }
7e87c7d9
ZJS
732
733 config->default_entry = boot_entries_select_default(config);
734 return 0;
735}
af918182 736
eea4ce1e
LP
737int boot_entries_load_config_auto(
738 const char *override_esp_path,
739 const char *override_xbootldr_path,
740 BootConfig *config) {
741
742 _cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL;
743 int r;
744
745 assert(config);
746
747 /* This function is similar to boot_entries_load_config(), however we automatically search for the
748 * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
749 * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
750 * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
751 * it). This allows other boot loaders to pass boot loader entry information to our tools if they
752 * want to. */
753
754 if (!override_esp_path && !override_xbootldr_path) {
f40999f8
ZJS
755 if (access("/run/boot-loader-entries/", F_OK) >= 0)
756 return boot_entries_load_config("/run/boot-loader-entries/", NULL, config);
eea4ce1e 757
f40999f8
ZJS
758 if (errno != ENOENT)
759 return log_error_errno(errno,
760 "Failed to determine whether /run/boot-loader-entries/ exists: %m");
eea4ce1e
LP
761 }
762
763 r = find_esp_and_warn(override_esp_path, false, &esp_where, NULL, NULL, NULL, NULL);
cc5957dc 764 if (r < 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */
eea4ce1e
LP
765 return r;
766
767 r = find_xbootldr_and_warn(override_xbootldr_path, false, &xbootldr_where, NULL);
768 if (r < 0 && r != -ENOKEY)
769 return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
770
f40999f8 771 return boot_entries_load_config(esp_where, xbootldr_where, config);
eea4ce1e
LP
772}
773
d4bd786d
LP
774int boot_entries_augment_from_loader(
775 BootConfig *config,
9951736b
LP
776 char **found_by_loader,
777 bool only_auto) {
d4bd786d
LP
778
779 static const char *const title_table[] = {
93f14ce2
LP
780 /* Pretty names for a few well-known automatically discovered entries. */
781 "auto-osx", "macOS",
782 "auto-windows", "Windows Boot Manager",
783 "auto-efi-shell", "EFI Shell",
784 "auto-efi-default", "EFI Default Loader",
785 "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
786 };
787
93f14ce2 788 char **i;
93f14ce2
LP
789
790 assert(config);
791
792 /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
793 * already included there. */
794
93f14ce2 795 STRV_FOREACH(i, found_by_loader) {
ce4c4f81 796 _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
93f14ce2
LP
797 char **a, **b;
798
799 if (boot_config_has_entry(config, *i))
800 continue;
801
9951736b 802 if (only_auto && !startswith(*i, "auto-"))
93f14ce2
LP
803 continue;
804
805 c = strdup(*i);
806 if (!c)
807 return log_oom();
808
809 STRV_FOREACH_PAIR(a, b, (char**) title_table)
810 if (streq(*a, *i)) {
811 t = strdup(*b);
812 if (!t)
813 return log_oom();
814 break;
815 }
816
e6f055cb 817 p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntries)));
ce4c4f81
ZJS
818 if (!p)
819 return log_oom();
820
319a4f4b 821 if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
93f14ce2
LP
822 return log_oom();
823
824 config->entries[config->n_entries++] = (BootEntry) {
825 .type = BOOT_ENTRY_LOADER,
826 .id = TAKE_PTR(c),
827 .title = TAKE_PTR(t),
ce4c4f81 828 .path = TAKE_PTR(p),
93f14ce2
LP
829 };
830 }
831
832 return 0;
833}
834
af918182
ZJS
835/********************************************************************************/
836
575d4370
LP
837static int verify_esp_blkid(
838 dev_t devid,
5caa3167 839 bool searching,
af918182
ZJS
840 uint32_t *ret_part,
841 uint64_t *ret_pstart,
842 uint64_t *ret_psize,
843 sd_id128_t *ret_uuid) {
575d4370
LP
844
845 sd_id128_t uuid = SD_ID128_NULL;
846 uint64_t pstart = 0, psize = 0;
847 uint32_t part = 0;
848
4e066f7f 849#if HAVE_BLKID
8e766630 850 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
54b22b26 851 _cleanup_free_ char *node = NULL;
4e066f7f 852 const char *v;
af918182
ZJS
853 int r;
854
575d4370 855 r = device_path_make_major_minor(S_IFBLK, devid, &node);
54b22b26
LP
856 if (r < 0)
857 return log_error_errno(r, "Failed to format major/minor device path: %m");
575d4370 858
af918182 859 errno = 0;
54b22b26 860 b = blkid_new_probe_from_filename(node);
af918182 861 if (!b)
575d4370 862 return log_error_errno(errno ?: SYNTHETIC_ERRNO(ENOMEM), "Failed to open file system \"%s\": %m", node);
af918182
ZJS
863
864 blkid_probe_enable_superblocks(b, 1);
865 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
866 blkid_probe_enable_partitions(b, 1);
867 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
868
869 errno = 0;
870 r = blkid_do_safeprobe(b);
a7afbd60 871 if (r == -2)
575d4370 872 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" is ambiguous.", node);
a7afbd60 873 else if (r == 1)
575d4370 874 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" does not contain a label.", node);
a7afbd60 875 else if (r != 0)
575d4370 876 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe file system \"%s\": %m", node);
af918182 877
af918182
ZJS
878 r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
879 if (r != 0)
7ea3024b
LP
880 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
881 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
882 "No filesystem found on \"%s\": %m", node);
a7afbd60
LP
883 if (!streq(v, "vfat"))
884 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
885 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
575d4370 886 "File system \"%s\" is not FAT.", node);
af918182 887
af918182
ZJS
888 r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
889 if (r != 0)
7ea3024b
LP
890 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
891 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
892 "File system \"%s\" is not located on a partitioned block device.", node);
a7afbd60
LP
893 if (!streq(v, "gpt"))
894 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
895 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
575d4370 896 "File system \"%s\" is not on a GPT partition table.", node);
af918182
ZJS
897
898 errno = 0;
899 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
900 if (r != 0)
575d4370 901 return log_error_errno(errno ?: EIO, "Failed to probe partition type UUID of \"%s\": %m", node);
a7afbd60
LP
902 if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
903 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
904 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
575d4370 905 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node);
af918182
ZJS
906
907 errno = 0;
908 r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
909 if (r != 0)
575d4370 910 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition entry UUID of \"%s\": %m", node);
af918182 911 r = sd_id128_from_string(v, &uuid);
a7afbd60 912 if (r < 0)
575d4370 913 return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v);
af918182
ZJS
914
915 errno = 0;
916 r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
917 if (r != 0)
28b77ab2 918 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition number of \"%s\": %m", node);
af918182
ZJS
919 r = safe_atou32(v, &part);
920 if (r < 0)
921 return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field.");
922
923 errno = 0;
924 r = blkid_probe_lookup_value(b, "PART_ENTRY_OFFSET", &v, NULL);
925 if (r != 0)
575d4370 926 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition offset of \"%s\": %m", node);
af918182
ZJS
927 r = safe_atou64(v, &pstart);
928 if (r < 0)
929 return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field.");
930
931 errno = 0;
932 r = blkid_probe_lookup_value(b, "PART_ENTRY_SIZE", &v, NULL);
933 if (r != 0)
575d4370 934 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition size of \"%s\": %m", node);
af918182
ZJS
935 r = safe_atou64(v, &psize);
936 if (r < 0)
937 return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field.");
4e066f7f 938#endif
af918182 939
af918182
ZJS
940 if (ret_part)
941 *ret_part = part;
942 if (ret_pstart)
943 *ret_pstart = pstart;
944 if (ret_psize)
945 *ret_psize = psize;
946 if (ret_uuid)
947 *ret_uuid = uuid;
948
949 return 0;
950}
951
cedb9eec
LP
952static int verify_esp_udev(
953 dev_t devid,
954 bool searching,
955 uint32_t *ret_part,
956 uint64_t *ret_pstart,
957 uint64_t *ret_psize,
958 sd_id128_t *ret_uuid) {
959
960 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
961 _cleanup_free_ char *node = NULL;
962 sd_id128_t uuid = SD_ID128_NULL;
963 uint64_t pstart = 0, psize = 0;
964 uint32_t part = 0;
965 const char *v;
966 int r;
967
968 r = device_path_make_major_minor(S_IFBLK, devid, &node);
969 if (r < 0)
970 return log_error_errno(r, "Failed to format major/minor device path: %m");
971
972 r = sd_device_new_from_devnum(&d, 'b', devid);
973 if (r < 0)
974 return log_error_errno(r, "Failed to get device from device number: %m");
975
976 r = sd_device_get_property_value(d, "ID_FS_TYPE", &v);
977 if (r < 0)
978 return log_error_errno(r, "Failed to get device property: %m");
979 if (!streq(v, "vfat"))
980 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
981 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
982 "File system \"%s\" is not FAT.", node );
983
984 r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &v);
985 if (r < 0)
986 return log_error_errno(r, "Failed to get device property: %m");
987 if (!streq(v, "gpt"))
988 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
989 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
990 "File system \"%s\" is not on a GPT partition table.", node);
991
992 r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v);
993 if (r < 0)
994 return log_error_errno(r, "Failed to get device property: %m");
995 if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"))
996 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
997 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
998 "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node);
999
1000 r = sd_device_get_property_value(d, "ID_PART_ENTRY_UUID", &v);
1001 if (r < 0)
1002 return log_error_errno(r, "Failed to get device property: %m");
1003 r = sd_id128_from_string(v, &uuid);
1004 if (r < 0)
1005 return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v);
1006
1007 r = sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &v);
1008 if (r < 0)
1009 return log_error_errno(r, "Failed to get device property: %m");
1010 r = safe_atou32(v, &part);
1011 if (r < 0)
1012 return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field.");
1013
1014 r = sd_device_get_property_value(d, "ID_PART_ENTRY_OFFSET", &v);
1015 if (r < 0)
1016 return log_error_errno(r, "Failed to get device property: %m");
1017 r = safe_atou64(v, &pstart);
1018 if (r < 0)
1019 return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field.");
1020
1021 r = sd_device_get_property_value(d, "ID_PART_ENTRY_SIZE", &v);
1022 if (r < 0)
1023 return log_error_errno(r, "Failed to get device property: %m");
1024 r = safe_atou64(v, &psize);
1025 if (r < 0)
1026 return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field.");
1027
1028 if (ret_part)
1029 *ret_part = part;
1030 if (ret_pstart)
1031 *ret_pstart = pstart;
1032 if (ret_psize)
1033 *ret_psize = psize;
1034 if (ret_uuid)
1035 *ret_uuid = uuid;
1036
1037 return 0;
1038}
1039
18ae9ef1
LP
1040static int verify_fsroot_dir(
1041 const char *path,
1042 bool searching,
1043 bool unprivileged_mode,
1044 dev_t *ret_dev) {
1045
1046 struct stat st, st2;
8312d694 1047 const char *t2, *trigger;
18ae9ef1
LP
1048 int r;
1049
1050 assert(path);
1051 assert(ret_dev);
1052
8312d694
LP
1053 /* So, the ESP and XBOOTLDR partition are commonly located on an autofs mount. stat() on the
1054 * directory won't trigger it, if it is not mounted yet. Let's hence explicitly trigger it here,
1055 * before stat()ing */
1056 trigger = strjoina(path, "/trigger"); /* Filename doesn't matter... */
1057 (void) access(trigger, F_OK);
1058
18ae9ef1
LP
1059 if (stat(path, &st) < 0)
1060 return log_full_errno((searching && errno == ENOENT) ||
1061 (unprivileged_mode && errno == EACCES) ? LOG_DEBUG : LOG_ERR, errno,
1062 "Failed to determine block device node of \"%s\": %m", path);
1063
1064 if (major(st.st_dev) == 0)
1065 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1066 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
1067 "Block device node of \"%s\" is invalid.", path);
1068
eceb6111
LP
1069 if (path_equal(path, "/")) {
1070 /* Let's assume that the root directory of the OS is always the root of its file system
1071 * (which technically doesn't have to be the case, but it's close enough, and it's not easy
1072 * to be fully correct for it, since we can't look further up than the root dir easily.) */
1073 if (ret_dev)
1074 *ret_dev = st.st_dev;
1075
1076 return 0;
1077 }
1078
18ae9ef1
LP
1079 t2 = strjoina(path, "/..");
1080 if (stat(t2, &st2) < 0) {
1081 if (errno != EACCES)
1082 r = -errno;
1083 else {
1084 _cleanup_free_ char *parent = NULL;
1085
1086 /* If going via ".." didn't work due to EACCESS, then let's determine the parent path
1087 * directly instead. It's not as good, due to symlinks and such, but we can't do
1088 * anything better here. */
1089
1090 parent = dirname_malloc(path);
1091 if (!parent)
1092 return log_oom();
1093
7c248223 1094 r = RET_NERRNO(stat(parent, &st2));
18ae9ef1
LP
1095 }
1096
1097 if (r < 0)
1098 return log_full_errno(unprivileged_mode && r == -EACCES ? LOG_DEBUG : LOG_ERR, r,
1099 "Failed to determine block device node of parent of \"%s\": %m", path);
1100 }
1101
1102 if (st.st_dev == st2.st_dev)
1103 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1104 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
1105 "Directory \"%s\" is not the root of the file system.", path);
1106
1107 if (ret_dev)
1108 *ret_dev = st.st_dev;
1109
1110 return 0;
1111}
1112
575d4370
LP
1113static int verify_esp(
1114 const char *p,
1115 bool searching,
1116 bool unprivileged_mode,
1117 uint32_t *ret_part,
1118 uint64_t *ret_pstart,
1119 uint64_t *ret_psize,
1120 sd_id128_t *ret_uuid) {
1121
575d4370 1122 bool relax_checks;
18ae9ef1 1123 dev_t devid;
575d4370
LP
1124 int r;
1125
1126 assert(p);
1127
1128 /* This logs about all errors, except:
1129 *
1130 * -ENOENT → if 'searching' is set, and the dir doesn't exist
1131 * -EADDRNOTAVAIL → if 'searching' is set, and the dir doesn't look like an ESP
5238e957 1132 * -EACESS → if 'unprivileged_mode' is set, and we have trouble accessing the thing
575d4370
LP
1133 */
1134
1135 relax_checks = getenv_bool("SYSTEMD_RELAX_ESP_CHECKS") > 0;
1136
5238e957 1137 /* Non-root user can only check the status, so if an error occurred in the following, it does not cause any
575d4370
LP
1138 * issues. Let's also, silence the error messages. */
1139
1140 if (!relax_checks) {
18ae9ef1
LP
1141 struct statfs sfs;
1142
575d4370
LP
1143 if (statfs(p, &sfs) < 0)
1144 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
1145 return log_full_errno((searching && errno == ENOENT) ||
1146 (unprivileged_mode && errno == EACCES) ? LOG_DEBUG : LOG_ERR, errno,
1147 "Failed to check file system type of \"%s\": %m", p);
1148
1149 if (!F_TYPE_EQUAL(sfs.f_type, MSDOS_SUPER_MAGIC))
1150 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1151 SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
1152 "File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p);
1153 }
1154
18ae9ef1 1155 r = verify_fsroot_dir(p, searching, unprivileged_mode, &devid);
575d4370 1156 if (r < 0)
18ae9ef1 1157 return r;
575d4370
LP
1158
1159 /* In a container we don't have access to block devices, skip this part of the verification, we trust
cedb9eec
LP
1160 * the container manager set everything up correctly on its own. */
1161 if (detect_container() > 0 || relax_checks)
575d4370
LP
1162 goto finish;
1163
cedb9eec
LP
1164 /* If we are unprivileged we ask udev for the metadata about the partition. If we are privileged we
1165 * use blkid instead. Why? Because this code is called from 'bootctl' which is pretty much an
1166 * emergency recovery tool that should also work when udev isn't up (i.e. from the emergency shell),
1167 * however blkid can't work if we have no privileges to access block devices directly, which is why
1168 * we use udev in that case. */
1169 if (unprivileged_mode)
18ae9ef1 1170 return verify_esp_udev(devid, searching, ret_part, ret_pstart, ret_psize, ret_uuid);
cedb9eec 1171 else
18ae9ef1 1172 return verify_esp_blkid(devid, searching, ret_part, ret_pstart, ret_psize, ret_uuid);
575d4370
LP
1173
1174finish:
1175 if (ret_part)
1176 *ret_part = 0;
1177 if (ret_pstart)
1178 *ret_pstart = 0;
1179 if (ret_psize)
1180 *ret_psize = 0;
1181 if (ret_uuid)
1182 *ret_uuid = SD_ID128_NULL;
1183
1184 return 0;
1185}
1186
5caa3167
LP
1187int find_esp_and_warn(
1188 const char *path,
1189 bool unprivileged_mode,
1190 char **ret_path,
1191 uint32_t *ret_part,
1192 uint64_t *ret_pstart,
1193 uint64_t *ret_psize,
1194 sd_id128_t *ret_uuid) {
af918182 1195
af918182
ZJS
1196 int r;
1197
5caa3167
LP
1198 /* This logs about all errors except:
1199 *
1200 * -ENOKEY → when we can't find the partition
1201 * -EACCESS → when unprivileged_mode is true, and we can't access something
1202 */
af918182 1203
5caa3167
LP
1204 if (path) {
1205 r = verify_esp(path, false, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
af918182
ZJS
1206 if (r < 0)
1207 return r;
1208
5caa3167
LP
1209 goto found;
1210 }
1211
cc7a0bfa
LP
1212 path = getenv("SYSTEMD_ESP_PATH");
1213 if (path) {
baaa35ad
ZJS
1214 if (!path_is_valid(path) || !path_is_absolute(path))
1215 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1216 "$SYSTEMD_ESP_PATH does not refer to absolute path, refusing to use it: %s",
1217 path);
cc7a0bfa
LP
1218
1219 /* Note: when the user explicitly configured things with an env var we won't validate the mount
1220 * point. After all we want this to be useful for testing. */
1221 goto found;
1222 }
1223
5caa3167
LP
1224 FOREACH_STRING(path, "/efi", "/boot", "/boot/efi") {
1225
1226 r = verify_esp(path, true, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
1227 if (r >= 0)
1228 goto found;
1229 if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */
1230 return r;
1231 }
1232
1233 /* No logging here */
1234 return -ENOKEY;
1235
15cb6c98
LP
1236found:
1237 if (ret_path) {
1238 char *c;
1239
1240 c = strdup(path);
1241 if (!c)
1242 return log_oom();
1243
1244 *ret_path = c;
1245 }
1246
1247 return 0;
1248}
1249
ad95aa44
LP
1250static int verify_xbootldr_blkid(
1251 dev_t devid,
15cb6c98 1252 bool searching,
15cb6c98 1253 sd_id128_t *ret_uuid) {
ad95aa44
LP
1254
1255 sd_id128_t uuid = SD_ID128_NULL;
1256
15cb6c98
LP
1257#if HAVE_BLKID
1258 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
1259 _cleanup_free_ char *node = NULL;
1260 const char *v;
15cb6c98
LP
1261 int r;
1262
ad95aa44 1263 r = device_path_make_major_minor(S_IFBLK, devid, &node);
15cb6c98
LP
1264 if (r < 0)
1265 return log_error_errno(r, "Failed to format major/minor device path: %m");
1266 errno = 0;
1267 b = blkid_new_probe_from_filename(node);
1268 if (!b)
ad95aa44 1269 return log_error_errno(errno ?: SYNTHETIC_ERRNO(ENOMEM), "Failed to open file system \"%s\": %m", node);
15cb6c98
LP
1270
1271 blkid_probe_enable_partitions(b, 1);
1272 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
1273
1274 errno = 0;
1275 r = blkid_do_safeprobe(b);
1276 if (r == -2)
ad95aa44 1277 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" is ambiguous.", node);
15cb6c98 1278 else if (r == 1)
ad95aa44 1279 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" does not contain a label.", node);
15cb6c98 1280 else if (r != 0)
ad95aa44 1281 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe file system \"%s\": %m", node);
15cb6c98
LP
1282
1283 errno = 0;
1284 r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
1285 if (r != 0)
ad95aa44 1286 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition scheme of \"%s\": %m", node);
15cb6c98
LP
1287 if (streq(v, "gpt")) {
1288
1289 errno = 0;
1290 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
1291 if (r != 0)
ad95aa44 1292 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition type UUID of \"%s\": %m", node);
15cb6c98
LP
1293 if (!streq(v, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1294 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1295 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
ad95aa44 1296 "File system \"%s\" has wrong type for extended boot loader partition.", node);
15cb6c98
LP
1297
1298 errno = 0;
1299 r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
1300 if (r != 0)
ad95aa44 1301 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition entry UUID of \"%s\": %m", node);
15cb6c98
LP
1302 r = sd_id128_from_string(v, &uuid);
1303 if (r < 0)
ad95aa44 1304 return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v);
15cb6c98
LP
1305
1306 } else if (streq(v, "dos")) {
1307
1308 errno = 0;
1309 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
1310 if (r != 0)
ad95aa44 1311 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition type UUID of \"%s\": %m", node);
15cb6c98
LP
1312 if (!streq(v, "0xea"))
1313 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1314 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
ad95aa44 1315 "File system \"%s\" has wrong type for extended boot loader partition.", node);
15cb6c98
LP
1316
1317 } else
1318 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1319 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
ad95aa44 1320 "File system \"%s\" is not on a GPT or DOS partition table.", node);
15cb6c98
LP
1321#endif
1322
15cb6c98
LP
1323 if (ret_uuid)
1324 *ret_uuid = uuid;
1325
1326 return 0;
1327}
1328
85d02102
LP
1329static int verify_xbootldr_udev(
1330 dev_t devid,
1331 bool searching,
1332 sd_id128_t *ret_uuid) {
1333
1334 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1335 _cleanup_free_ char *node = NULL;
1336 sd_id128_t uuid = SD_ID128_NULL;
1337 const char *v;
1338 int r;
1339
1340 r = device_path_make_major_minor(S_IFBLK, devid, &node);
1341 if (r < 0)
1342 return log_error_errno(r, "Failed to format major/minor device path: %m");
1343
1344 r = sd_device_new_from_devnum(&d, 'b', devid);
1345 if (r < 0)
1346 return log_error_errno(r, "Failed to get device from device number: %m");
1347
1348 r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &v);
1349 if (r < 0)
1350 return log_error_errno(r, "Failed to get device property: %m");
1351
1352 if (streq(v, "gpt")) {
1353
1354 r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v);
1355 if (r < 0)
1356 return log_error_errno(r, "Failed to get device property: %m");
1357 if (!streq(v, "bc13c2ff-59e6-4262-a352-b275fd6f7172"))
1358 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1359 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
1360 "File system \"%s\" has wrong type for extended boot loader partition.", node);
1361
1362 r = sd_device_get_property_value(d, "ID_PART_ENTRY_UUID", &v);
1363 if (r < 0)
1364 return log_error_errno(r, "Failed to get device property: %m");
1365 r = sd_id128_from_string(v, &uuid);
1366 if (r < 0)
1367 return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v);
1368
1369 } else if (streq(v, "dos")) {
1370
1371 r = sd_device_get_property_value(d, "ID_PART_ENTRY_TYPE", &v);
1372 if (r < 0)
1373 return log_error_errno(r, "Failed to get device property: %m");
1374 if (!streq(v, "0xea"))
1375 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1376 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
1377 "File system \"%s\" has wrong type for extended boot loader partition.", node);
1378 } else
1379 return log_full_errno(searching ? LOG_DEBUG : LOG_ERR,
1380 searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
1381 "File system \"%s\" is not on a GPT or DOS partition table.", node);
1382
1383 if (ret_uuid)
1384 *ret_uuid = uuid;
1385
1386 return 0;
1387}
1388
ad95aa44
LP
1389static int verify_xbootldr(
1390 const char *p,
1391 bool searching,
1392 bool unprivileged_mode,
1393 sd_id128_t *ret_uuid) {
1394
ad95aa44 1395 bool relax_checks;
4a4994b6 1396 dev_t devid;
ad95aa44
LP
1397 int r;
1398
1399 assert(p);
1400
1401 relax_checks = getenv_bool("SYSTEMD_RELAX_XBOOTLDR_CHECKS") > 0;
1402
4a4994b6 1403 r = verify_fsroot_dir(p, searching, unprivileged_mode, &devid);
ad95aa44 1404 if (r < 0)
4a4994b6 1405 return r;
ad95aa44 1406
85d02102 1407 if (detect_container() > 0 || relax_checks)
ad95aa44
LP
1408 goto finish;
1409
85d02102 1410 if (unprivileged_mode)
4a4994b6 1411 return verify_xbootldr_udev(devid, searching, ret_uuid);
85d02102 1412 else
4a4994b6 1413 return verify_xbootldr_blkid(devid, searching, ret_uuid);
ad95aa44
LP
1414
1415finish:
1416 if (ret_uuid)
1417 *ret_uuid = SD_ID128_NULL;
1418
1419 return 0;
1420}
1421
15cb6c98
LP
1422int find_xbootldr_and_warn(
1423 const char *path,
1424 bool unprivileged_mode,
1425 char **ret_path,
1426 sd_id128_t *ret_uuid) {
1427
1428 int r;
1429
1430 /* Similar to find_esp_and_warn(), but finds the XBOOTLDR partition. Returns the same errors. */
1431
1432 if (path) {
1433 r = verify_xbootldr(path, false, unprivileged_mode, ret_uuid);
1434 if (r < 0)
1435 return r;
1436
1437 goto found;
1438 }
1439
1440 path = getenv("SYSTEMD_XBOOTLDR_PATH");
1441 if (path) {
1442 if (!path_is_valid(path) || !path_is_absolute(path))
1443 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1444 "$SYSTEMD_XBOOTLDR_PATH does not refer to absolute path, refusing to use it: %s",
1445 path);
1446
1447 goto found;
1448 }
1449
1450 r = verify_xbootldr("/boot", true, unprivileged_mode, ret_uuid);
1451 if (r >= 0) {
1452 path = "/boot";
1453 goto found;
1454 }
1455 if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */
1456 return r;
1457
1458 return -ENOKEY;
1459
5caa3167
LP
1460found:
1461 if (ret_path) {
1462 char *c;
1463
1464 c = strdup(path);
1465 if (!c)
af918182
ZJS
1466 return log_oom();
1467
5caa3167 1468 *ret_path = c;
af918182
ZJS
1469 }
1470
5caa3167 1471 return 0;
af918182 1472}