]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bootspec.c
shared/bootspec: remember the full path to boot entry and use it in logging
[thirdparty/systemd.git] / src / shared / bootspec.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdio.h>
4 #include <linux/magic.h>
5
6 #include "sd-id128.h"
7
8 #include "alloc-util.h"
9 #include "blkid-util.h"
10 #include "bootspec.h"
11 #include "conf-files.h"
12 #include "def.h"
13 #include "device-nodes.h"
14 #include "efivars.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "parse-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "virt.h"
22
23 void boot_entry_free(BootEntry *entry) {
24 assert(entry);
25
26 free(entry->id);
27 free(entry->path);
28 free(entry->title);
29 free(entry->show_title);
30 free(entry->version);
31 free(entry->machine_id);
32 free(entry->architecture);
33 strv_free(entry->options);
34 free(entry->kernel);
35 free(entry->efi);
36 strv_free(entry->initrd);
37 free(entry->device_tree);
38 }
39
40 int boot_entry_load(const char *path, BootEntry *entry) {
41 _cleanup_(boot_entry_free) BootEntry tmp = {};
42 _cleanup_fclose_ FILE *f = NULL;
43 unsigned line = 1;
44 char *b, *c;
45 int r;
46
47 assert(path);
48 assert(entry);
49
50 c = endswith_no_case(path, ".conf");
51 if (!c) {
52 log_error("Invalid loader entry filename: %s", path);
53 return -EINVAL;
54 }
55
56 b = basename(path);
57 tmp.id = strndup(b, c - b);
58 if (!tmp.id)
59 return log_oom();
60
61 tmp.path = strdup(path);
62 if (!tmp.path)
63 return log_oom();
64
65 f = fopen(path, "re");
66 if (!f)
67 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
68
69 for (;;) {
70 _cleanup_free_ char *buf = NULL, *field = NULL;
71 const char *p;
72
73 r = read_line(f, LONG_LINE_MAX, &buf);
74 if (r == 0)
75 break;
76 if (r == -ENOBUFS)
77 return log_error_errno(r, "%s:%u: Line too long", path, line);
78 if (r < 0)
79 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
80
81 line++;
82
83 if (IN_SET(*strstrip(buf), '#', '\0'))
84 continue;
85
86 p = buf;
87 r = extract_first_word(&p, &field, " \t", 0);
88 if (r < 0) {
89 log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line);
90 continue;
91 }
92 if (r == 0) {
93 log_warning("%s:%u: Bad syntax", path, line);
94 continue;
95 }
96
97 if (streq(field, "title"))
98 r = free_and_strdup(&tmp.title, p);
99 else if (streq(field, "version"))
100 r = free_and_strdup(&tmp.version, p);
101 else if (streq(field, "machine-id"))
102 r = free_and_strdup(&tmp.machine_id, p);
103 else if (streq(field, "architecture"))
104 r = free_and_strdup(&tmp.architecture, p);
105 else if (streq(field, "options"))
106 r = strv_extend(&tmp.options, p);
107 else if (streq(field, "linux"))
108 r = free_and_strdup(&tmp.kernel, p);
109 else if (streq(field, "efi"))
110 r = free_and_strdup(&tmp.efi, p);
111 else if (streq(field, "initrd"))
112 r = strv_extend(&tmp.initrd, p);
113 else if (streq(field, "devicetree"))
114 r = free_and_strdup(&tmp.device_tree, p);
115 else {
116 log_notice("%s:%u: Unknown line \"%s\"", path, line, field);
117 continue;
118 }
119 if (r < 0)
120 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
121 }
122
123 *entry = tmp;
124 tmp = (BootEntry) {};
125 return 0;
126 }
127
128 void boot_config_free(BootConfig *config) {
129 size_t i;
130
131 assert(config);
132
133 free(config->default_pattern);
134 free(config->timeout);
135 free(config->editor);
136 free(config->auto_entries);
137 free(config->auto_firmware);
138
139 free(config->entry_oneshot);
140 free(config->entry_default);
141
142 for (i = 0; i < config->n_entries; i++)
143 boot_entry_free(config->entries + i);
144 free(config->entries);
145 }
146
147 int boot_loader_read_conf(const char *path, BootConfig *config) {
148 _cleanup_fclose_ FILE *f = NULL;
149 unsigned line = 1;
150 int r;
151
152 assert(path);
153 assert(config);
154
155 f = fopen(path, "re");
156 if (!f)
157 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
158
159 for (;;) {
160 _cleanup_free_ char *buf = NULL, *field = NULL;
161 const char *p;
162
163 r = read_line(f, LONG_LINE_MAX, &buf);
164 if (r == 0)
165 break;
166 if (r == -ENOBUFS)
167 return log_error_errno(r, "%s:%u: Line too long", path, line);
168 if (r < 0)
169 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
170
171 line++;
172
173 if (IN_SET(*strstrip(buf), '#', '\0'))
174 continue;
175
176 p = buf;
177 r = extract_first_word(&p, &field, " \t", 0);
178 if (r < 0) {
179 log_error_errno(r, "Failed to parse config file %s line %u: %m", path, line);
180 continue;
181 }
182 if (r == 0) {
183 log_warning("%s:%u: Bad syntax", path, line);
184 continue;
185 }
186
187 if (streq(field, "default"))
188 r = free_and_strdup(&config->default_pattern, p);
189 else if (streq(field, "timeout"))
190 r = free_and_strdup(&config->timeout, p);
191 else if (streq(field, "editor"))
192 r = free_and_strdup(&config->editor, p);
193 else if (streq(field, "auto-entries"))
194 r = free_and_strdup(&config->auto_entries, p);
195 else if (streq(field, "auto-firmware"))
196 r = free_and_strdup(&config->auto_firmware, p);
197 else if (streq(field, "console-mode"))
198 r = free_and_strdup(&config->console_mode, p);
199 else {
200 log_notice("%s:%u: Unknown line \"%s\"", path, line, field);
201 continue;
202 }
203 if (r < 0)
204 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
205 }
206
207 return 0;
208 }
209
210 static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
211 return str_verscmp(a->id, b->id);
212 }
213
214 int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) {
215 _cleanup_strv_free_ char **files = NULL;
216 char **f;
217 int r;
218 BootEntry *array = NULL;
219 size_t n_allocated = 0, n = 0;
220
221 assert(dir);
222 assert(ret_entries);
223 assert(ret_n_entries);
224
225 r = conf_files_list(&files, ".conf", NULL, 0, dir, NULL);
226 if (r < 0)
227 return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
228
229 STRV_FOREACH(f, files) {
230 if (!GREEDY_REALLOC0(array, n_allocated, n + 1))
231 return log_oom();
232
233 r = boot_entry_load(*f, array + n);
234 if (r < 0)
235 continue;
236
237 n++;
238 }
239
240 typesafe_qsort(array, n, boot_entry_compare);
241
242 *ret_entries = array;
243 *ret_n_entries = n;
244
245 return 0;
246 }
247
248 static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) {
249 size_t i, j;
250 bool non_unique = false;
251
252 assert(entries || n_entries == 0);
253 assert(arr || n_entries == 0);
254
255 for (i = 0; i < n_entries; i++)
256 arr[i] = false;
257
258 for (i = 0; i < n_entries; i++)
259 for (j = 0; j < n_entries; j++)
260 if (i != j && streq(boot_entry_title(entries + i),
261 boot_entry_title(entries + j)))
262 non_unique = arr[i] = arr[j] = true;
263
264 return non_unique;
265 }
266
267 static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
268 char *s;
269 size_t i;
270 int r;
271 bool arr[n_entries];
272
273 assert(entries || n_entries == 0);
274
275 /* Find _all_ non-unique titles */
276 if (!find_nonunique(entries, n_entries, arr))
277 return 0;
278
279 /* Add version to non-unique titles */
280 for (i = 0; i < n_entries; i++)
281 if (arr[i] && entries[i].version) {
282 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version);
283 if (r < 0)
284 return -ENOMEM;
285
286 free_and_replace(entries[i].show_title, s);
287 }
288
289 if (!find_nonunique(entries, n_entries, arr))
290 return 0;
291
292 /* Add machine-id to non-unique titles */
293 for (i = 0; i < n_entries; i++)
294 if (arr[i] && entries[i].machine_id) {
295 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id);
296 if (r < 0)
297 return -ENOMEM;
298
299 free_and_replace(entries[i].show_title, s);
300 }
301
302 if (!find_nonunique(entries, n_entries, arr))
303 return 0;
304
305 /* Add file name to non-unique titles */
306 for (i = 0; i < n_entries; i++)
307 if (arr[i]) {
308 r = asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id);
309 if (r < 0)
310 return -ENOMEM;
311
312 free_and_replace(entries[i].show_title, s);
313 }
314
315 return 0;
316 }
317
318 static int boot_entries_select_default(const BootConfig *config) {
319 int i;
320
321 assert(config);
322
323 if (config->entry_oneshot)
324 for (i = config->n_entries - 1; i >= 0; i--)
325 if (streq(config->entry_oneshot, config->entries[i].id)) {
326 log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
327 config->entries[i].id);
328 return i;
329 }
330
331 if (config->entry_default)
332 for (i = config->n_entries - 1; i >= 0; i--)
333 if (streq(config->entry_default, config->entries[i].id)) {
334 log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
335 config->entries[i].id);
336 return i;
337 }
338
339 if (config->default_pattern)
340 for (i = config->n_entries - 1; i >= 0; i--)
341 if (fnmatch(config->default_pattern, config->entries[i].id, FNM_CASEFOLD) == 0) {
342 log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
343 config->entries[i].id, config->default_pattern);
344 return i;
345 }
346
347 if (config->n_entries > 0)
348 log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].id);
349 else
350 log_debug("Found no default boot entry :(");
351
352 return config->n_entries - 1; /* -1 means "no default" */
353 }
354
355 int boot_entries_load_config(const char *esp_path, BootConfig *config) {
356 const char *p;
357 int r;
358
359 assert(esp_path);
360 assert(config);
361
362 p = strjoina(esp_path, "/loader/loader.conf");
363 r = boot_loader_read_conf(p, config);
364 if (r < 0)
365 return log_error_errno(r, "Failed to read boot config from \"%s\": %m", p);
366
367 p = strjoina(esp_path, "/loader/entries");
368 r = boot_entries_find(p, &config->entries, &config->n_entries);
369 if (r < 0)
370 return log_error_errno(r, "Failed to read boot entries from \"%s\": %m", p);
371
372 r = boot_entries_uniquify(config->entries, config->n_entries);
373 if (r < 0)
374 return log_error_errno(r, "Failed to uniquify boot entries: %m");
375
376 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", &config->entry_oneshot);
377 if (r < 0 && r != -ENOENT)
378 return log_error_errno(r, "Failed to read EFI var \"LoaderEntryOneShot\": %m");
379
380 r = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntryDefault", &config->entry_default);
381 if (r < 0 && r != -ENOENT)
382 return log_error_errno(r, "Failed to read EFI var \"LoaderEntryDefault\": %m");
383
384 config->default_entry = boot_entries_select_default(config);
385 return 0;
386 }
387
388 /********************************************************************************/
389
390 static int verify_esp(
391 const char *p,
392 bool searching,
393 bool unprivileged_mode,
394 uint32_t *ret_part,
395 uint64_t *ret_pstart,
396 uint64_t *ret_psize,
397 sd_id128_t *ret_uuid) {
398 #if HAVE_BLKID
399 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
400 char t[DEV_NUM_PATH_MAX];
401 const char *v;
402 #endif
403 uint64_t pstart = 0, psize = 0;
404 struct stat st, st2;
405 const char *t2;
406 struct statfs sfs;
407 sd_id128_t uuid = SD_ID128_NULL;
408 uint32_t part = 0;
409 int r;
410
411 assert(p);
412
413 /* Non-root user can only check the status, so if an error occured in the following, it does not cause any
414 * issues. Let's also, silence the error messages. */
415
416 if (statfs(p, &sfs) < 0) {
417 /* If we are searching for the mount point, don't generate a log message if we can't find the path */
418 if (errno == ENOENT && searching)
419 return -ENOENT;
420
421 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
422 "Failed to check file system type of \"%s\": %m", p);
423 }
424
425 if (!F_TYPE_EQUAL(sfs.f_type, MSDOS_SUPER_MAGIC)) {
426 if (searching)
427 return -EADDRNOTAVAIL;
428
429 log_error("File system \"%s\" is not a FAT EFI System Partition (ESP) file system.", p);
430 return -ENODEV;
431 }
432
433 if (stat(p, &st) < 0)
434 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
435 "Failed to determine block device node of \"%s\": %m", p);
436
437 if (major(st.st_dev) == 0) {
438 log_error("Block device node of %p is invalid.", p);
439 return -ENODEV;
440 }
441
442 t2 = strjoina(p, "/..");
443 r = stat(t2, &st2);
444 if (r < 0)
445 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
446 "Failed to determine block device node of parent of \"%s\": %m", p);
447
448 if (st.st_dev == st2.st_dev) {
449 log_error("Directory \"%s\" is not the root of the EFI System Partition (ESP) file system.", p);
450 return -ENODEV;
451 }
452
453 /* In a container we don't have access to block devices, skip this part of the verification, we trust the
454 * container manager set everything up correctly on its own. Also skip the following verification for non-root user. */
455 if (detect_container() > 0 || unprivileged_mode)
456 goto finish;
457
458 #if HAVE_BLKID
459 xsprintf_dev_num_path(t, "block", st.st_dev);
460 errno = 0;
461 b = blkid_new_probe_from_filename(t);
462 if (!b)
463 return log_error_errno(errno ?: ENOMEM, "Failed to open file system \"%s\": %m", p);
464
465 blkid_probe_enable_superblocks(b, 1);
466 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
467 blkid_probe_enable_partitions(b, 1);
468 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
469
470 errno = 0;
471 r = blkid_do_safeprobe(b);
472 if (r == -2) {
473 log_error("File system \"%s\" is ambiguous.", p);
474 return -ENODEV;
475 } else if (r == 1) {
476 log_error("File system \"%s\" does not contain a label.", p);
477 return -ENODEV;
478 } else if (r != 0)
479 return log_error_errno(errno ?: EIO, "Failed to probe file system \"%s\": %m", p);
480
481 errno = 0;
482 r = blkid_probe_lookup_value(b, "TYPE", &v, NULL);
483 if (r != 0)
484 return log_error_errno(errno ?: EIO, "Failed to probe file system type \"%s\": %m", p);
485 if (!streq(v, "vfat")) {
486 log_error("File system \"%s\" is not FAT.", p);
487 return -ENODEV;
488 }
489
490 errno = 0;
491 r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
492 if (r != 0)
493 return log_error_errno(errno ?: EIO, "Failed to probe partition scheme \"%s\": %m", p);
494 if (!streq(v, "gpt")) {
495 log_error("File system \"%s\" is not on a GPT partition table.", p);
496 return -ENODEV;
497 }
498
499 errno = 0;
500 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
501 if (r != 0)
502 return log_error_errno(errno ?: EIO, "Failed to probe partition type UUID \"%s\": %m", p);
503 if (!streq(v, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")) {
504 log_error("File system \"%s\" has wrong type for an EFI System Partition (ESP).", p);
505 return -ENODEV;
506 }
507
508 errno = 0;
509 r = blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
510 if (r != 0)
511 return log_error_errno(errno ?: EIO, "Failed to probe partition entry UUID \"%s\": %m", p);
512 r = sd_id128_from_string(v, &uuid);
513 if (r < 0) {
514 log_error("Partition \"%s\" has invalid UUID \"%s\".", p, v);
515 return -EIO;
516 }
517
518 errno = 0;
519 r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
520 if (r != 0)
521 return log_error_errno(errno ?: EIO, "Failed to probe partition number \"%s\": m", p);
522 r = safe_atou32(v, &part);
523 if (r < 0)
524 return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field.");
525
526 errno = 0;
527 r = blkid_probe_lookup_value(b, "PART_ENTRY_OFFSET", &v, NULL);
528 if (r != 0)
529 return log_error_errno(errno ?: EIO, "Failed to probe partition offset \"%s\": %m", p);
530 r = safe_atou64(v, &pstart);
531 if (r < 0)
532 return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field.");
533
534 errno = 0;
535 r = blkid_probe_lookup_value(b, "PART_ENTRY_SIZE", &v, NULL);
536 if (r != 0)
537 return log_error_errno(errno ?: EIO, "Failed to probe partition size \"%s\": %m", p);
538 r = safe_atou64(v, &psize);
539 if (r < 0)
540 return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field.");
541 #endif
542
543 finish:
544 if (ret_part)
545 *ret_part = part;
546 if (ret_pstart)
547 *ret_pstart = pstart;
548 if (ret_psize)
549 *ret_psize = psize;
550 if (ret_uuid)
551 *ret_uuid = uuid;
552
553 return 0;
554 }
555
556 int find_esp_and_warn(
557 const char *path,
558 bool unprivileged_mode,
559 char **ret_path,
560 uint32_t *ret_part,
561 uint64_t *ret_pstart,
562 uint64_t *ret_psize,
563 sd_id128_t *ret_uuid) {
564
565 int r;
566
567 /* This logs about all errors except:
568 *
569 * -ENOKEY → when we can't find the partition
570 * -EACCESS → when unprivileged_mode is true, and we can't access something
571 */
572
573 if (path) {
574 r = verify_esp(path, false, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
575 if (r < 0)
576 return r;
577
578 goto found;
579 }
580
581 FOREACH_STRING(path, "/efi", "/boot", "/boot/efi") {
582
583 r = verify_esp(path, true, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
584 if (r >= 0)
585 goto found;
586 if (!IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* This one is not it */
587 return r;
588 }
589
590 /* No logging here */
591 return -ENOKEY;
592
593 found:
594 if (ret_path) {
595 char *c;
596
597 c = strdup(path);
598 if (!c)
599 return log_oom();
600
601 *ret_path = c;
602 }
603
604 return 0;
605 }
606
607 int find_default_boot_entry(
608 const char *esp_path,
609 char **esp_where,
610 BootConfig *config,
611 const BootEntry **e) {
612
613 _cleanup_free_ char *where = NULL;
614 int r;
615
616 assert(config);
617 assert(e);
618
619 r = find_esp_and_warn(esp_path, false, &where, NULL, NULL, NULL, NULL);
620 if (r < 0)
621 return r;
622
623 r = boot_entries_load_config(where, config);
624 if (r < 0)
625 return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", where);
626
627 if (config->default_entry < 0) {
628 log_error("No entry suitable as default, refusing to guess.");
629 return -ENOENT;
630 }
631
632 *e = &config->entries[config->default_entry];
633 log_debug("Found default boot entry in file \"%s\"", (*e)->path);
634
635 if (esp_where)
636 *esp_where = TAKE_PTR(where);
637
638 return 0;
639 }