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