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