]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bootspec.c
log: minimize includes in log.h
[thirdparty/systemd.git] / src / shared / bootspec.c
CommitLineData
7e87c7d9
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2017 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <stdio.h>
af918182 21#include <linux/magic.h>
7e87c7d9 22
dccca82b
LP
23#include "sd-id128.h"
24
7e87c7d9 25#include "alloc-util.h"
af918182 26#include "blkid-util.h"
7e87c7d9
ZJS
27#include "bootspec.h"
28#include "conf-files.h"
29#include "def.h"
c67f84b0 30#include "device-nodes.h"
7e87c7d9
ZJS
31#include "efivars.h"
32#include "fd-util.h"
33#include "fileio.h"
af918182
ZJS
34#include "parse-util.h"
35#include "stat-util.h"
7e87c7d9
ZJS
36#include "string-util.h"
37#include "strv.h"
af918182 38#include "virt.h"
7e87c7d9
ZJS
39
40void boot_entry_free(BootEntry *entry) {
4fe2ba0e 41 assert(entry);
7e87c7d9 42
4fe2ba0e 43 free(entry->filename);
7e87c7d9 44 free(entry->title);
64f05708 45 free(entry->show_title);
7e87c7d9
ZJS
46 free(entry->version);
47 free(entry->machine_id);
48 free(entry->architecture);
49 strv_free(entry->options);
50 free(entry->kernel);
51 free(entry->efi);
52 strv_free(entry->initrd);
53 free(entry->device_tree);
54}
55
56int boot_entry_load(const char *path, BootEntry *entry) {
263195c6 57 _cleanup_(boot_entry_free) BootEntry tmp = {};
7e87c7d9
ZJS
58 _cleanup_fclose_ FILE *f = NULL;
59 unsigned line = 1;
263195c6 60 char *b, *c;
7e87c7d9
ZJS
61 int r;
62
4fe2ba0e
LP
63 assert(path);
64 assert(entry);
65
263195c6
YW
66 c = endswith_no_case(path, ".conf");
67 if (!c) {
68 log_error("Invalid loader entry filename: %s", path);
69 return -EINVAL;
70 }
7e87c7d9 71
263195c6
YW
72 b = basename(path);
73 tmp.filename = strndup(b, c - b);
7e87c7d9
ZJS
74 if (!tmp.filename)
75 return log_oom();
76
263195c6
YW
77 f = fopen(path, "re");
78 if (!f)
79 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
80
7e87c7d9
ZJS
81 for (;;) {
82 _cleanup_free_ char *buf = NULL;
83 char *p;
84
85 r = read_line(f, LONG_LINE_MAX, &buf);
86 if (r == 0)
87 break;
88 if (r == -ENOBUFS)
89 return log_error_errno(r, "%s:%u: Line too long", path, line);
90 if (r < 0)
91 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
92
93 line++;
94
95 if (IN_SET(*strstrip(buf), '#', '\0'))
96 continue;
97
98 p = strchr(buf, ' ');
99 if (!p) {
100 log_warning("%s:%u: Bad syntax", path, line);
101 continue;
102 }
103 *p = '\0';
104 p = strstrip(p + 1);
105
106 if (streq(buf, "title"))
107 r = free_and_strdup(&tmp.title, p);
108 else if (streq(buf, "version"))
109 r = free_and_strdup(&tmp.version, p);
110 else if (streq(buf, "machine-id"))
111 r = free_and_strdup(&tmp.machine_id, p);
112 else if (streq(buf, "architecture"))
113 r = free_and_strdup(&tmp.architecture, p);
114 else if (streq(buf, "options"))
115 r = strv_extend(&tmp.options, p);
116 else if (streq(buf, "linux"))
117 r = free_and_strdup(&tmp.kernel, p);
118 else if (streq(buf, "efi"))
119 r = free_and_strdup(&tmp.efi, p);
120 else if (streq(buf, "initrd"))
121 r = strv_extend(&tmp.initrd, p);
122 else if (streq(buf, "devicetree"))
123 r = free_and_strdup(&tmp.device_tree, p);
124 else {
125 log_notice("%s:%u: Unknown line \"%s\"", path, line, buf);
126 continue;
127 }
128 if (r < 0)
129 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
130 }
131
132 *entry = tmp;
133 tmp = (BootEntry) {};
134 return 0;
135}
136
137void boot_config_free(BootConfig *config) {
138 unsigned i;
139
4fe2ba0e
LP
140 assert(config);
141
7e87c7d9
ZJS
142 free(config->default_pattern);
143 free(config->timeout);
144 free(config->editor);
145
146 free(config->entry_oneshot);
147 free(config->entry_default);
148
149 for (i = 0; i < config->n_entries; i++)
150 boot_entry_free(config->entries + i);
151 free(config->entries);
152}
153
154int boot_loader_read_conf(const char *path, BootConfig *config) {
155 _cleanup_fclose_ FILE *f = NULL;
156 unsigned line = 1;
157 int r;
158
4fe2ba0e
LP
159 assert(path);
160 assert(config);
161
7e87c7d9
ZJS
162 f = fopen(path, "re");
163 if (!f)
164 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
165
166 for (;;) {
167 _cleanup_free_ char *buf = NULL;
168 char *p;
169
170 r = read_line(f, LONG_LINE_MAX, &buf);
171 if (r == 0)
172 break;
173 if (r == -ENOBUFS)
174 return log_error_errno(r, "%s:%u: Line too long", path, line);
175 if (r < 0)
176 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
177
178 line++;
179
180 if (IN_SET(*strstrip(buf), '#', '\0'))
181 continue;
182
183 p = strchr(buf, ' ');
184 if (!p) {
185 log_warning("%s:%u: Bad syntax", path, line);
186 continue;
187 }
188 *p = '\0';
189 p = strstrip(p + 1);
190
191 if (streq(buf, "default"))
192 r = free_and_strdup(&config->default_pattern, p);
193 else if (streq(buf, "timeout"))
194 r = free_and_strdup(&config->timeout, p);
195 else if (streq(buf, "editor"))
196 r = free_and_strdup(&config->editor, p);
197 else {
198 log_notice("%s:%u: Unknown line \"%s\"", path, line, buf);
199 continue;
200 }
201 if (r < 0)
202 return log_error_errno(r, "%s:%u: Error while reading: %m", path, line);
203 }
204
205 return 0;
206}
207
7e87c7d9 208static int boot_entry_compare(const void *a, const void *b) {
68c58c67 209 const BootEntry *aa = a, *bb = b;
7e87c7d9
ZJS
210
211 return str_verscmp(aa->filename, bb->filename);
212}
213
4fe2ba0e 214int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) {
7e87c7d9
ZJS
215 _cleanup_strv_free_ char **files = NULL;
216 char **f;
217 int r;
7e87c7d9
ZJS
218 BootEntry *array = NULL;
219 size_t n_allocated = 0, n = 0;
220
4fe2ba0e
LP
221 assert(dir);
222 assert(ret_entries);
223 assert(ret_n_entries);
224
7e87c7d9
ZJS
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 qsort_safe(array, n, sizeof(BootEntry), boot_entry_compare);
241
4fe2ba0e
LP
242 *ret_entries = array;
243 *ret_n_entries = n;
244
7e87c7d9
ZJS
245 return 0;
246}
247
64f05708
ZJS
248static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) {
249 unsigned i, j;
250 bool non_unique = false;
251
4fe2ba0e
LP
252 assert(entries || n_entries == 0);
253 assert(arr || n_entries == 0);
254
64f05708
ZJS
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
267static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
268 char *s;
269 unsigned i;
270 int r;
271 bool arr[n_entries];
272
4fe2ba0e
LP
273 assert(entries || n_entries == 0);
274
64f05708
ZJS
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].filename);
309 if (r < 0)
310 return -ENOMEM;
311
312 free_and_replace(entries[i].show_title, s);
313 }
314
315 return 0;
316}
317
ad1afd60 318static int boot_entries_select_default(const BootConfig *config) {
7e87c7d9
ZJS
319 int i;
320
4fe2ba0e
LP
321 assert(config);
322
7e87c7d9
ZJS
323 if (config->entry_oneshot)
324 for (i = config->n_entries - 1; i >= 0; i--)
325 if (streq(config->entry_oneshot, config->entries[i].filename)) {
326 log_debug("Found default: filename \"%s\" is matched by LoaderEntryOneShot",
327 config->entries[i].filename);
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].filename)) {
334 log_debug("Found default: filename \"%s\" is matched by LoaderEntryDefault",
335 config->entries[i].filename);
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].filename, FNM_CASEFOLD) == 0) {
342 log_debug("Found default: filename \"%s\" is matched by pattern \"%s\"",
343 config->entries[i].filename, config->default_pattern);
344 return i;
345 }
346
347 if (config->n_entries > 0)
5838493a 348 log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].filename);
7e87c7d9
ZJS
349 else
350 log_debug("Found no default boot entry :(");
4fe2ba0e 351
7e87c7d9
ZJS
352 return config->n_entries - 1; /* -1 means "no default" */
353}
354
355int boot_entries_load_config(const char *esp_path, BootConfig *config) {
356 const char *p;
357 int r;
358
4fe2ba0e
LP
359 assert(esp_path);
360 assert(config);
361
7e87c7d9
ZJS
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
64f05708
ZJS
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
7e87c7d9
ZJS
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}
af918182
ZJS
387
388/********************************************************************************/
389
390static int verify_esp(
af918182 391 const char *p,
5caa3167
LP
392 bool searching,
393 bool unprivileged_mode,
af918182
ZJS
394 uint32_t *ret_part,
395 uint64_t *ret_pstart,
396 uint64_t *ret_psize,
397 sd_id128_t *ret_uuid) {
4e066f7f 398#if HAVE_BLKID
af918182 399 _cleanup_blkid_free_probe_ blkid_probe b = NULL;
c67f84b0 400 char t[DEV_NUM_PATH_MAX];
4e066f7f
YW
401 const char *v;
402#endif
af918182
ZJS
403 uint64_t pstart = 0, psize = 0;
404 struct stat st, st2;
4e066f7f 405 const char *t2;
af918182
ZJS
406 struct statfs sfs;
407 sd_id128_t uuid = SD_ID128_NULL;
408 uint32_t part = 0;
af918182
ZJS
409 int r;
410
411 assert(p);
412
5caa3167
LP
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. */
af918182
ZJS
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
5caa3167 421 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
af918182
ZJS
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)
5caa3167 434 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
af918182
ZJS
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)
5caa3167 445 return log_full_errno(unprivileged_mode && errno == EACCES ? LOG_DEBUG : LOG_ERR, errno,
af918182
ZJS
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. */
5caa3167 455 if (detect_container() > 0 || unprivileged_mode)
af918182
ZJS
456 goto finish;
457
4e066f7f 458#if HAVE_BLKID
c67f84b0 459 xsprintf_dev_num_path(t, "block", st.st_dev);
af918182
ZJS
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.");
4e066f7f 541#endif
af918182
ZJS
542
543finish:
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
5caa3167
LP
556int 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) {
af918182 564
af918182
ZJS
565 int r;
566
5caa3167
LP
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 */
af918182 572
5caa3167
LP
573 if (path) {
574 r = verify_esp(path, false, unprivileged_mode, ret_part, ret_pstart, ret_psize, ret_uuid);
af918182
ZJS
575 if (r < 0)
576 return r;
577
5caa3167
LP
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
593found:
594 if (ret_path) {
595 char *c;
596
597 c = strdup(path);
598 if (!c)
af918182
ZJS
599 return log_oom();
600
5caa3167 601 *ret_path = c;
af918182
ZJS
602 }
603
5caa3167 604 return 0;
af918182 605}