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