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