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