]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7e87c7d9 | 2 | |
ca78ad1d | 3 | #include <unistd.h> |
7e87c7d9 | 4 | |
e94830c0 | 5 | #include "bootspec-fundamental.h" |
432ce537 | 6 | #include "bootspec.h" |
f461a28d | 7 | #include "chase.h" |
7e87c7d9 | 8 | #include "conf-files.h" |
7176f06c | 9 | #include "devnum-util.h" |
5e146a75 | 10 | #include "dirent-util.h" |
0bb2f0f1 | 11 | #include "efi-loader.h" |
5e146a75 | 12 | #include "env-file.h" |
432ce537 | 13 | #include "errno-util.h" |
7e87c7d9 ZJS |
14 | #include "fd-util.h" |
15 | #include "fileio.h" | |
e94830c0 | 16 | #include "find-esp.h" |
cc7a0bfa | 17 | #include "path-util.h" |
5e146a75 | 18 | #include "pe-header.h" |
432ce537 | 19 | #include "pretty-print.h" |
d04f0331 | 20 | #include "recurse-dir.h" |
760877e9 | 21 | #include "sort-util.h" |
ddfdf86f | 22 | #include "stat-util.h" |
432ce537 | 23 | #include "string-table.h" |
7e87c7d9 | 24 | #include "strv.h" |
432ce537 | 25 | #include "terminal-util.h" |
5e146a75 | 26 | #include "unaligned.h" |
7e87c7d9 | 27 | |
432ce537 ZJS |
28 | static const char* const boot_entry_type_table[_BOOT_ENTRY_TYPE_MAX] = { |
29 | [BOOT_ENTRY_CONF] = "Boot Loader Specification Type #1 (.conf)", | |
30 | [BOOT_ENTRY_UNIFIED] = "Boot Loader Specification Type #2 (.efi)", | |
31 | [BOOT_ENTRY_LOADER] = "Reported by Boot Loader", | |
32 | [BOOT_ENTRY_LOADER_AUTO] = "Automatic", | |
33 | }; | |
34 | ||
35 | DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type, BootEntryType); | |
36 | ||
4bc14b17 LN |
37 | static const char* const boot_entry_type_json_table[_BOOT_ENTRY_TYPE_MAX] = { |
38 | [BOOT_ENTRY_CONF] = "type1", | |
39 | [BOOT_ENTRY_UNIFIED] = "type2", | |
40 | [BOOT_ENTRY_LOADER] = "loader", | |
41 | [BOOT_ENTRY_LOADER_AUTO] = "auto", | |
42 | }; | |
43 | ||
44 | DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type_json, BootEntryType); | |
45 | ||
0de2e1fd | 46 | static void boot_entry_free(BootEntry *entry) { |
4fe2ba0e | 47 | assert(entry); |
7e87c7d9 | 48 | |
12580bc3 | 49 | free(entry->id); |
15b82eec | 50 | free(entry->id_old); |
2d3bfb69 | 51 | free(entry->path); |
43b736a8 | 52 | free(entry->root); |
7e87c7d9 | 53 | free(entry->title); |
64f05708 | 54 | free(entry->show_title); |
20ec8f53 | 55 | free(entry->sort_key); |
7e87c7d9 ZJS |
56 | free(entry->version); |
57 | free(entry->machine_id); | |
58 | free(entry->architecture); | |
59 | strv_free(entry->options); | |
60 | free(entry->kernel); | |
61 | free(entry->efi); | |
62 | strv_free(entry->initrd); | |
63 | free(entry->device_tree); | |
fdc5c042 | 64 | strv_free(entry->device_tree_overlay); |
7e87c7d9 ZJS |
65 | } |
66 | ||
d68c6bea LP |
67 | static int mangle_path( |
68 | const char *fname, | |
69 | unsigned line, | |
70 | const char *field, | |
71 | const char *p, | |
72 | char **ret) { | |
73 | ||
bb9133bb LP |
74 | _cleanup_free_ char *c = NULL; |
75 | ||
76 | assert(field); | |
77 | assert(p); | |
78 | assert(ret); | |
79 | ||
80 | /* Spec leaves open if prefixed with "/" or not, let's normalize that */ | |
81 | if (path_is_absolute(p)) | |
82 | c = strdup(p); | |
83 | else | |
84 | c = strjoin("/", p); | |
85 | if (!c) | |
86 | return -ENOMEM; | |
87 | ||
88 | /* We only reference files, never directories */ | |
89 | if (endswith(c, "/")) { | |
d68c6bea | 90 | log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' has trailing slash, ignoring: %s", field, c); |
bb9133bb LP |
91 | *ret = NULL; |
92 | return 0; | |
93 | } | |
94 | ||
95 | /* Remove duplicate "/" */ | |
96 | path_simplify(c); | |
97 | ||
98 | /* No ".." or "." or so */ | |
99 | if (!path_is_normalized(c)) { | |
d68c6bea | 100 | log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' is not normalized, ignoring: %s", field, c); |
bb9133bb LP |
101 | *ret = NULL; |
102 | return 0; | |
103 | } | |
104 | ||
105 | *ret = TAKE_PTR(c); | |
106 | return 1; | |
107 | } | |
108 | ||
d68c6bea LP |
109 | static int parse_path_one( |
110 | const char *fname, | |
111 | unsigned line, | |
112 | const char *field, | |
113 | char **s, | |
114 | const char *p) { | |
115 | ||
bb9133bb LP |
116 | _cleanup_free_ char *c = NULL; |
117 | int r; | |
118 | ||
119 | assert(field); | |
120 | assert(s); | |
121 | assert(p); | |
122 | ||
d68c6bea | 123 | r = mangle_path(fname, line, field, p, &c); |
bb9133bb LP |
124 | if (r <= 0) |
125 | return r; | |
126 | ||
28340719 | 127 | return free_and_replace(*s, c); |
bb9133bb LP |
128 | } |
129 | ||
d68c6bea LP |
130 | static int parse_path_strv( |
131 | const char *fname, | |
132 | unsigned line, | |
133 | const char *field, | |
134 | char ***s, | |
135 | const char *p) { | |
136 | ||
bb9133bb LP |
137 | char *c; |
138 | int r; | |
139 | ||
140 | assert(field); | |
141 | assert(s); | |
142 | assert(p); | |
143 | ||
d68c6bea | 144 | r = mangle_path(fname, line, field, p, &c); |
bb9133bb LP |
145 | if (r <= 0) |
146 | return r; | |
147 | ||
148 | return strv_consume(s, c); | |
149 | } | |
150 | ||
d68c6bea LP |
151 | static int parse_path_many( |
152 | const char *fname, | |
153 | unsigned line, | |
154 | const char *field, | |
155 | char ***s, | |
156 | const char *p) { | |
157 | ||
bb9133bb LP |
158 | _cleanup_strv_free_ char **l = NULL, **f = NULL; |
159 | int r; | |
160 | ||
161 | l = strv_split(p, NULL); | |
162 | if (!l) | |
163 | return -ENOMEM; | |
164 | ||
165 | STRV_FOREACH(i, l) { | |
166 | char *c; | |
167 | ||
d68c6bea | 168 | r = mangle_path(fname, line, field, *i, &c); |
bb9133bb LP |
169 | if (r < 0) |
170 | return r; | |
171 | if (r == 0) | |
172 | continue; | |
173 | ||
174 | r = strv_consume(&f, c); | |
175 | if (r < 0) | |
176 | return r; | |
177 | } | |
178 | ||
179 | return strv_extend_strv(s, f, /* filter_duplicates= */ false); | |
180 | } | |
181 | ||
7f5780ed LP |
182 | static int parse_tries(const char *fname, const char **p, unsigned *ret) { |
183 | _cleanup_free_ char *d = NULL; | |
184 | unsigned tries; | |
185 | size_t n; | |
186 | int r; | |
187 | ||
188 | assert(fname); | |
189 | assert(p); | |
190 | assert(*p); | |
191 | assert(ret); | |
192 | ||
193 | n = strspn(*p, DIGITS); | |
194 | if (n == 0) { | |
195 | *ret = UINT_MAX; | |
196 | return 0; | |
197 | } | |
198 | ||
199 | d = strndup(*p, n); | |
200 | if (!d) | |
201 | return log_oom(); | |
202 | ||
203 | r = safe_atou_full(d, 10, &tries); | |
204 | if (r >= 0 && tries > INT_MAX) /* sd-boot allows INT_MAX, let's use the same limit */ | |
205 | r = -ERANGE; | |
206 | if (r < 0) | |
207 | return log_error_errno(r, "Failed to parse tries counter of filename '%s': %m", fname); | |
208 | ||
209 | *p = *p + n; | |
210 | *ret = tries; | |
211 | return 1; | |
212 | } | |
213 | ||
214 | int boot_filename_extract_tries( | |
215 | const char *fname, | |
216 | char **ret_stripped, | |
217 | unsigned *ret_tries_left, | |
218 | unsigned *ret_tries_done) { | |
219 | ||
220 | unsigned tries_left = UINT_MAX, tries_done = UINT_MAX; | |
221 | _cleanup_free_ char *stripped = NULL; | |
222 | const char *p, *suffix, *m; | |
223 | int r; | |
224 | ||
225 | assert(fname); | |
226 | assert(ret_stripped); | |
227 | assert(ret_tries_left); | |
228 | assert(ret_tries_done); | |
229 | ||
230 | /* Be liberal with suffix, only insist on a dot. After all we want to cover any capitalization here | |
231 | * (vfat is case insensitive after all), and at least .efi and .conf as suffix. */ | |
232 | suffix = strrchr(fname, '.'); | |
233 | if (!suffix) | |
234 | goto nothing; | |
235 | ||
236 | p = m = memrchr(fname, '+', suffix - fname); | |
237 | if (!p) | |
238 | goto nothing; | |
239 | p++; | |
240 | ||
241 | r = parse_tries(fname, &p, &tries_left); | |
242 | if (r < 0) | |
243 | return r; | |
244 | if (r == 0) | |
245 | goto nothing; | |
246 | ||
247 | if (*p == '-') { | |
248 | p++; | |
249 | ||
250 | r = parse_tries(fname, &p, &tries_done); | |
251 | if (r < 0) | |
252 | return r; | |
253 | if (r == 0) | |
254 | goto nothing; | |
255 | } | |
256 | ||
257 | if (p != suffix) | |
258 | goto nothing; | |
259 | ||
260 | stripped = strndup(fname, m - fname); | |
261 | if (!stripped) | |
262 | return log_oom(); | |
263 | ||
264 | if (!strextend(&stripped, suffix)) | |
265 | return log_oom(); | |
266 | ||
267 | *ret_stripped = TAKE_PTR(stripped); | |
268 | *ret_tries_left = tries_left; | |
269 | *ret_tries_done = tries_done; | |
270 | ||
271 | return 0; | |
272 | ||
273 | nothing: | |
274 | stripped = strdup(fname); | |
275 | if (!stripped) | |
276 | return log_oom(); | |
277 | ||
278 | *ret_stripped = TAKE_PTR(stripped); | |
279 | *ret_tries_left = *ret_tries_done = UINT_MAX; | |
280 | return 0; | |
281 | } | |
282 | ||
85e17916 | 283 | static int boot_entry_load_type1( |
d04f0331 | 284 | FILE *f, |
43b736a8 | 285 | const char *root, |
d04f0331 | 286 | const char *dir, |
f70de82f | 287 | const char *fname, |
43b736a8 LP |
288 | BootEntry *entry) { |
289 | ||
7f5780ed | 290 | _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_CONF); |
7e87c7d9 | 291 | unsigned line = 1; |
736783d4 | 292 | char *c; |
7e87c7d9 ZJS |
293 | int r; |
294 | ||
d04f0331 | 295 | assert(f); |
43b736a8 | 296 | assert(root); |
d04f0331 | 297 | assert(dir); |
f70de82f | 298 | assert(fname); |
4fe2ba0e LP |
299 | assert(entry); |
300 | ||
d04f0331 | 301 | /* Loads a Type #1 boot menu entry from the specified FILE* object */ |
7e87c7d9 | 302 | |
7f5780ed LP |
303 | r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done); |
304 | if (r < 0) | |
305 | return r; | |
306 | ||
307 | if (!efi_loader_entry_name_valid(tmp.id)) | |
f70de82f | 308 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", fname); |
d04f0331 | 309 | |
7f5780ed | 310 | c = endswith_no_case(tmp.id, ".conf"); |
736783d4 | 311 | if (!c) |
f70de82f | 312 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", fname); |
7e87c7d9 | 313 | |
7f5780ed | 314 | tmp.id_old = strndup(tmp.id, c - tmp.id); /* Without .conf suffix */ |
736783d4 LP |
315 | if (!tmp.id_old) |
316 | return log_oom(); | |
317 | ||
f70de82f | 318 | tmp.path = path_join(dir, fname); |
2d3bfb69 ZJS |
319 | if (!tmp.path) |
320 | return log_oom(); | |
321 | ||
43b736a8 LP |
322 | tmp.root = strdup(root); |
323 | if (!tmp.root) | |
324 | return log_oom(); | |
325 | ||
7e87c7d9 | 326 | for (;;) { |
f99fdc3e YW |
327 | _cleanup_free_ char *buf = NULL, *field = NULL; |
328 | const char *p; | |
7e87c7d9 ZJS |
329 | |
330 | r = read_line(f, LONG_LINE_MAX, &buf); | |
331 | if (r == 0) | |
332 | break; | |
333 | if (r == -ENOBUFS) | |
d68c6bea | 334 | return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long."); |
7e87c7d9 | 335 | if (r < 0) |
d68c6bea | 336 | return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m"); |
7e87c7d9 ZJS |
337 | |
338 | line++; | |
339 | ||
da8f277c LP |
340 | p = strstrip(buf); |
341 | if (IN_SET(p[0], '#', '\0')) | |
7e87c7d9 ZJS |
342 | continue; |
343 | ||
da8f277c | 344 | r = extract_first_word(&p, &field, NULL, 0); |
f99fdc3e | 345 | if (r < 0) { |
d68c6bea | 346 | log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m"); |
f99fdc3e YW |
347 | continue; |
348 | } | |
349 | if (r == 0) { | |
d68c6bea | 350 | log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Bad syntax, ignoring line."); |
7e87c7d9 ZJS |
351 | continue; |
352 | } | |
7e87c7d9 | 353 | |
b6bd2562 ZJS |
354 | if (isempty(p)) { |
355 | /* Some fields can reasonably have an empty value. In other cases warn. */ | |
356 | if (!STR_IN_SET(field, "options", "devicetree-overlay")) | |
d68c6bea LP |
357 | log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Field '%s' without value, ignoring line.", field); |
358 | ||
b6bd2562 ZJS |
359 | continue; |
360 | } | |
361 | ||
f99fdc3e | 362 | if (streq(field, "title")) |
7e87c7d9 | 363 | r = free_and_strdup(&tmp.title, p); |
20ec8f53 LP |
364 | else if (streq(field, "sort-key")) |
365 | r = free_and_strdup(&tmp.sort_key, p); | |
f99fdc3e | 366 | else if (streq(field, "version")) |
7e87c7d9 | 367 | r = free_and_strdup(&tmp.version, p); |
f99fdc3e | 368 | else if (streq(field, "machine-id")) |
7e87c7d9 | 369 | r = free_and_strdup(&tmp.machine_id, p); |
f99fdc3e | 370 | else if (streq(field, "architecture")) |
7e87c7d9 | 371 | r = free_and_strdup(&tmp.architecture, p); |
f99fdc3e | 372 | else if (streq(field, "options")) |
7e87c7d9 | 373 | r = strv_extend(&tmp.options, p); |
f99fdc3e | 374 | else if (streq(field, "linux")) |
d68c6bea | 375 | r = parse_path_one(tmp.path, line, field, &tmp.kernel, p); |
f99fdc3e | 376 | else if (streq(field, "efi")) |
d68c6bea | 377 | r = parse_path_one(tmp.path, line, field, &tmp.efi, p); |
f99fdc3e | 378 | else if (streq(field, "initrd")) |
d68c6bea | 379 | r = parse_path_strv(tmp.path, line, field, &tmp.initrd, p); |
f99fdc3e | 380 | else if (streq(field, "devicetree")) |
d68c6bea | 381 | r = parse_path_one(tmp.path, line, field, &tmp.device_tree, p); |
bb9133bb | 382 | else if (streq(field, "devicetree-overlay")) |
d68c6bea | 383 | r = parse_path_many(tmp.path, line, field, &tmp.device_tree_overlay, p); |
bb9133bb | 384 | else { |
d68c6bea | 385 | log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Unknown line '%s', ignoring.", field); |
7e87c7d9 ZJS |
386 | continue; |
387 | } | |
388 | if (r < 0) | |
d68c6bea | 389 | return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while parsing: %m"); |
7e87c7d9 ZJS |
390 | } |
391 | ||
392 | *entry = tmp; | |
393 | tmp = (BootEntry) {}; | |
394 | return 0; | |
395 | } | |
396 | ||
a847b539 ZJS |
397 | int boot_config_load_type1( |
398 | BootConfig *config, | |
399 | FILE *f, | |
400 | const char *root, | |
401 | const char *dir, | |
f70de82f | 402 | const char *fname) { |
a847b539 ZJS |
403 | int r; |
404 | ||
405 | assert(config); | |
406 | assert(f); | |
407 | assert(root); | |
408 | assert(dir); | |
f70de82f | 409 | assert(fname); |
a847b539 ZJS |
410 | |
411 | if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1)) | |
412 | return log_oom(); | |
413 | ||
f70de82f | 414 | r = boot_entry_load_type1(f, root, dir, fname, config->entries + config->n_entries); |
a847b539 ZJS |
415 | if (r < 0) |
416 | return r; | |
417 | ||
418 | config->n_entries++; | |
419 | return 0; | |
420 | } | |
421 | ||
7e87c7d9 | 422 | void boot_config_free(BootConfig *config) { |
4fe2ba0e LP |
423 | assert(config); |
424 | ||
7e87c7d9 ZJS |
425 | free(config->default_pattern); |
426 | free(config->timeout); | |
427 | free(config->editor); | |
c1d4e298 JJ |
428 | free(config->auto_entries); |
429 | free(config->auto_firmware); | |
72263375 | 430 | free(config->console_mode); |
d403d8f0 | 431 | free(config->beep); |
7e87c7d9 ZJS |
432 | |
433 | free(config->entry_oneshot); | |
434 | free(config->entry_default); | |
a78e472d | 435 | free(config->entry_selected); |
7e87c7d9 | 436 | |
9817b7db | 437 | for (size_t i = 0; i < config->n_entries; i++) |
7e87c7d9 ZJS |
438 | boot_entry_free(config->entries + i); |
439 | free(config->entries); | |
d486a0ea LP |
440 | |
441 | set_free(config->inodes_seen); | |
7e87c7d9 ZJS |
442 | } |
443 | ||
5ba1550f | 444 | int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) { |
7e87c7d9 ZJS |
445 | unsigned line = 1; |
446 | int r; | |
447 | ||
4fe2ba0e | 448 | assert(config); |
5ba1550f ZJS |
449 | assert(file); |
450 | assert(path); | |
7e87c7d9 ZJS |
451 | |
452 | for (;;) { | |
f99fdc3e YW |
453 | _cleanup_free_ char *buf = NULL, *field = NULL; |
454 | const char *p; | |
7e87c7d9 | 455 | |
5ba1550f | 456 | r = read_line(file, LONG_LINE_MAX, &buf); |
7e87c7d9 ZJS |
457 | if (r == 0) |
458 | break; | |
459 | if (r == -ENOBUFS) | |
d68c6bea | 460 | return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long."); |
7e87c7d9 | 461 | if (r < 0) |
d68c6bea | 462 | return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m"); |
7e87c7d9 ZJS |
463 | |
464 | line++; | |
465 | ||
da8f277c LP |
466 | p = strstrip(buf); |
467 | if (IN_SET(p[0], '#', '\0')) | |
7e87c7d9 ZJS |
468 | continue; |
469 | ||
da8f277c | 470 | r = extract_first_word(&p, &field, NULL, 0); |
f99fdc3e | 471 | if (r < 0) { |
d68c6bea | 472 | log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m"); |
f99fdc3e YW |
473 | continue; |
474 | } | |
475 | if (r == 0) { | |
d68c6bea | 476 | log_syntax(NULL, LOG_WARNING, path, line, 0, "Bad syntax, ignoring line."); |
7e87c7d9 ZJS |
477 | continue; |
478 | } | |
9ac2a89a LP |
479 | if (isempty(p)) { |
480 | log_syntax(NULL, LOG_WARNING, path, line, 0, "Field '%s' without value, ignoring line.", field); | |
481 | continue; | |
482 | } | |
7e87c7d9 | 483 | |
f99fdc3e | 484 | if (streq(field, "default")) |
7e87c7d9 | 485 | r = free_and_strdup(&config->default_pattern, p); |
f99fdc3e | 486 | else if (streq(field, "timeout")) |
7e87c7d9 | 487 | r = free_and_strdup(&config->timeout, p); |
f99fdc3e | 488 | else if (streq(field, "editor")) |
7e87c7d9 | 489 | r = free_and_strdup(&config->editor, p); |
790f84eb | 490 | else if (streq(field, "auto-entries")) |
c1d4e298 | 491 | r = free_and_strdup(&config->auto_entries, p); |
790f84eb | 492 | else if (streq(field, "auto-firmware")) |
c1d4e298 | 493 | r = free_and_strdup(&config->auto_firmware, p); |
790f84eb | 494 | else if (streq(field, "console-mode")) |
d37b0737 | 495 | r = free_and_strdup(&config->console_mode, p); |
fe5a698f | 496 | else if (streq(field, "random-seed-mode")) |
47b3e966 | 497 | log_syntax(NULL, LOG_WARNING, path, line, 0, "'random-seed-mode' has been deprecated, ignoring."); |
d403d8f0 LP |
498 | else if (streq(field, "beep")) |
499 | r = free_and_strdup(&config->beep, p); | |
7e87c7d9 | 500 | else { |
d68c6bea | 501 | log_syntax(NULL, LOG_WARNING, path, line, 0, "Unknown line '%s', ignoring.", field); |
7e87c7d9 ZJS |
502 | continue; |
503 | } | |
504 | if (r < 0) | |
d68c6bea | 505 | return log_syntax(NULL, LOG_ERR, path, line, r, "Error while parsing: %m"); |
7e87c7d9 ZJS |
506 | } |
507 | ||
f91ed3dc | 508 | return 1; |
7e87c7d9 ZJS |
509 | } |
510 | ||
2683ae2d LP |
511 | static int boot_loader_read_conf_path(BootConfig *config, const char *root, const char *path) { |
512 | _cleanup_free_ char *full = NULL; | |
5ba1550f | 513 | _cleanup_fclose_ FILE *f = NULL; |
2683ae2d | 514 | int r; |
5ba1550f ZJS |
515 | |
516 | assert(config); | |
517 | assert(path); | |
518 | ||
f461a28d | 519 | r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, "re", &full, &f); |
2683ae2d LP |
520 | if (r == -ENOENT) |
521 | return 0; | |
522 | if (r < 0) | |
523 | return log_error_errno(r, "Failed to open '%s/%s': %m", root, path); | |
5ba1550f | 524 | |
2683ae2d | 525 | return boot_loader_read_conf(config, f, full); |
5ba1550f ZJS |
526 | } |
527 | ||
93bab288 | 528 | static int boot_entry_compare(const BootEntry *a, const BootEntry *b) { |
20ec8f53 LP |
529 | int r; |
530 | ||
531 | assert(a); | |
532 | assert(b); | |
533 | ||
534 | r = CMP(!a->sort_key, !b->sort_key); | |
535 | if (r != 0) | |
536 | return r; | |
62a4b584 | 537 | |
20ec8f53 LP |
538 | if (a->sort_key && b->sort_key) { |
539 | r = strcmp(a->sort_key, b->sort_key); | |
540 | if (r != 0) | |
541 | return r; | |
542 | ||
543 | r = strcmp_ptr(a->machine_id, b->machine_id); | |
544 | if (r != 0) | |
545 | return r; | |
546 | ||
547 | r = -strverscmp_improved(a->version, b->version); | |
548 | if (r != 0) | |
549 | return r; | |
550 | } | |
551 | ||
62a4b584 | 552 | return -strverscmp_improved(a->id, b->id); |
7e87c7d9 ZJS |
553 | } |
554 | ||
d486a0ea LP |
555 | static int config_check_inode_relevant_and_unseen(BootConfig *config, int fd, const char *fname) { |
556 | _cleanup_free_ char *d = NULL; | |
557 | struct stat st; | |
558 | ||
559 | assert(config); | |
560 | assert(fd >= 0); | |
561 | assert(fname); | |
562 | ||
563 | /* So, here's the thing: because of the mess around /efi/ vs. /boot/ vs. /boot/efi/ it might be that | |
564 | * people have these dirs, or subdirs of them symlinked or bind mounted, and we might end up | |
565 | * iterating though some dirs multiple times. Let's thus rather be safe than sorry, and track the | |
566 | * inodes we already processed: let's ignore inodes we have seen already. This should be robust | |
567 | * against any form of symlinking or bind mounting, and effectively suppress any such duplicates. */ | |
568 | ||
569 | if (fstat(fd, &st) < 0) | |
570 | return log_error_errno(errno, "Failed to stat('%s'): %m", fname); | |
571 | if (!S_ISREG(st.st_mode)) { | |
572 | log_debug("File '%s' is not a reguar file, ignoring.", fname); | |
573 | return false; | |
574 | } | |
575 | ||
576 | if (set_contains(config->inodes_seen, &st)) { | |
577 | log_debug("Inode '%s' already seen before, ignoring.", fname); | |
578 | return false; | |
579 | } | |
580 | d = memdup(&st, sizeof(st)); | |
581 | if (!d) | |
582 | return log_oom(); | |
583 | if (set_ensure_put(&config->inodes_seen, &inode_hash_ops, d) < 0) | |
584 | return log_oom(); | |
585 | ||
586 | TAKE_PTR(d); | |
587 | return true; | |
588 | } | |
589 | ||
85e17916 | 590 | static int boot_entries_find_type1( |
85f4ae2f | 591 | BootConfig *config, |
43b736a8 | 592 | const char *root, |
85f4ae2f | 593 | const char *dir) { |
43b736a8 | 594 | |
d04f0331 | 595 | _cleanup_free_ DirectoryEntries *dentries = NULL; |
2683ae2d | 596 | _cleanup_free_ char *full = NULL; |
254d1313 | 597 | _cleanup_close_ int dir_fd = -EBADF; |
7e87c7d9 | 598 | int r; |
7e87c7d9 | 599 | |
85f4ae2f | 600 | assert(config); |
43b736a8 | 601 | assert(root); |
4fe2ba0e | 602 | assert(dir); |
4fe2ba0e | 603 | |
f461a28d | 604 | dir_fd = chase_and_open(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, O_DIRECTORY|O_CLOEXEC, &full); |
2683ae2d LP |
605 | if (dir_fd == -ENOENT) |
606 | return 0; | |
607 | if (dir_fd < 0) | |
608 | return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, dir); | |
d04f0331 LP |
609 | |
610 | r = readdir_all(dir_fd, RECURSE_DIR_IGNORE_DOT, &dentries); | |
7e87c7d9 | 611 | if (r < 0) |
2683ae2d | 612 | return log_error_errno(r, "Failed to read directory '%s': %m", full); |
d04f0331 LP |
613 | |
614 | for (size_t i = 0; i < dentries->n_entries; i++) { | |
615 | const struct dirent *de = dentries->entries[i]; | |
616 | _cleanup_fclose_ FILE *f = NULL; | |
617 | ||
618 | if (!dirent_is_file(de)) | |
619 | continue; | |
620 | ||
621 | if (!endswith_no_case(de->d_name, ".conf")) | |
622 | continue; | |
7e87c7d9 | 623 | |
f2c51304 | 624 | r = xfopenat(dir_fd, de->d_name, "re", O_NOFOLLOW|O_NOCTTY, &f); |
d04f0331 | 625 | if (r < 0) { |
2683ae2d | 626 | log_warning_errno(r, "Failed to open %s/%s, ignoring: %m", full, de->d_name); |
d04f0331 LP |
627 | continue; |
628 | } | |
629 | ||
d486a0ea LP |
630 | r = config_check_inode_relevant_and_unseen(config, fileno(f), de->d_name); |
631 | if (r < 0) | |
632 | return r; | |
633 | if (r == 0) /* inode already seen or otherwise not relevant */ | |
d04f0331 | 634 | continue; |
d486a0ea | 635 | |
2683ae2d | 636 | r = boot_config_load_type1(config, f, root, full, de->d_name); |
293e2240 | 637 | if (r == -ENOMEM) /* ignore all other errors */ |
a847b539 | 638 | return r; |
7e87c7d9 ZJS |
639 | } |
640 | ||
7e87c7d9 ZJS |
641 | return 0; |
642 | } | |
643 | ||
5e146a75 LP |
644 | static int boot_entry_load_unified( |
645 | const char *root, | |
646 | const char *path, | |
647 | const char *osrelease, | |
648 | const char *cmdline, | |
649 | BootEntry *ret) { | |
650 | ||
7f5780ed | 651 | _cleanup_free_ char *fname = NULL, *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL, |
c2caeb5d | 652 | *os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL; |
7f5780ed | 653 | _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_UNIFIED); |
20ec8f53 | 654 | const char *k, *good_name, *good_version, *good_sort_key; |
5e146a75 | 655 | _cleanup_fclose_ FILE *f = NULL; |
5e146a75 LP |
656 | int r; |
657 | ||
658 | assert(root); | |
659 | assert(path); | |
660 | assert(osrelease); | |
661 | ||
662 | k = path_startswith(path, root); | |
663 | if (!k) | |
664 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path); | |
665 | ||
673a1e6f | 666 | f = fmemopen_unlocked((void*) osrelease, strlen(osrelease), "r"); |
5e146a75 LP |
667 | if (!f) |
668 | return log_error_errno(errno, "Failed to open os-release buffer: %m"); | |
669 | ||
15b82eec SM |
670 | r = parse_env_file(f, "os-release", |
671 | "PRETTY_NAME", &os_pretty_name, | |
c2caeb5d LP |
672 | "IMAGE_ID", &os_image_id, |
673 | "NAME", &os_name, | |
15b82eec | 674 | "ID", &os_id, |
c2caeb5d LP |
675 | "IMAGE_VERSION", &os_image_version, |
676 | "VERSION", &os_version, | |
677 | "VERSION_ID", &os_version_id, | |
678 | "BUILD_ID", &os_build_id); | |
5e146a75 LP |
679 | if (r < 0) |
680 | return log_error_errno(r, "Failed to parse os-release data from unified kernel image %s: %m", path); | |
681 | ||
20ec8f53 | 682 | if (!bootspec_pick_name_version_sort_key( |
c2caeb5d LP |
683 | os_pretty_name, |
684 | os_image_id, | |
685 | os_name, | |
686 | os_id, | |
687 | os_image_version, | |
688 | os_version, | |
689 | os_version_id, | |
690 | os_build_id, | |
691 | &good_name, | |
20ec8f53 LP |
692 | &good_version, |
693 | &good_sort_key)) | |
5e146a75 LP |
694 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Missing fields in os-release data from unified kernel image %s, refusing.", path); |
695 | ||
7f5780ed | 696 | r = path_extract_filename(path, &fname); |
c2caeb5d LP |
697 | if (r < 0) |
698 | return log_error_errno(r, "Failed to extract file name from '%s': %m", path); | |
5e146a75 | 699 | |
7f5780ed LP |
700 | r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done); |
701 | if (r < 0) | |
702 | return r; | |
703 | ||
eed7210a | 704 | if (!efi_loader_entry_name_valid(tmp.id)) |
dfc22cb4 | 705 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id); |
eed7210a | 706 | |
c2caeb5d LP |
707 | if (os_id && os_version_id) { |
708 | tmp.id_old = strjoin(os_id, "-", os_version_id); | |
709 | if (!tmp.id_old) | |
710 | return log_oom(); | |
711 | } | |
712 | ||
5e146a75 LP |
713 | tmp.path = strdup(path); |
714 | if (!tmp.path) | |
715 | return log_oom(); | |
716 | ||
717 | tmp.root = strdup(root); | |
718 | if (!tmp.root) | |
719 | return log_oom(); | |
720 | ||
78517322 | 721 | tmp.kernel = path_make_absolute(k, "/"); |
5e146a75 LP |
722 | if (!tmp.kernel) |
723 | return log_oom(); | |
724 | ||
725 | tmp.options = strv_new(skip_leading_chars(cmdline, WHITESPACE)); | |
726 | if (!tmp.options) | |
727 | return log_oom(); | |
728 | ||
729 | delete_trailing_chars(tmp.options[0], WHITESPACE); | |
730 | ||
c2caeb5d LP |
731 | tmp.title = strdup(good_name); |
732 | if (!tmp.title) | |
733 | return log_oom(); | |
734 | ||
6e689dc6 JJ |
735 | if (good_sort_key) { |
736 | tmp.sort_key = strdup(good_sort_key); | |
737 | if (!tmp.sort_key) | |
738 | return log_oom(); | |
739 | } | |
20ec8f53 | 740 | |
87c77795 VW |
741 | if (good_version) { |
742 | tmp.version = strdup(good_version); | |
743 | if (!tmp.version) | |
744 | return log_oom(); | |
745 | } | |
5e146a75 LP |
746 | |
747 | *ret = tmp; | |
748 | tmp = (BootEntry) {}; | |
749 | return 0; | |
750 | } | |
751 | ||
752 | /* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but | |
753 | * the ones we do care about and we are willing to load into memory have this size limit.) */ | |
754 | #define PE_SECTION_SIZE_MAX (4U*1024U*1024U) | |
755 | ||
756 | static int find_sections( | |
757 | int fd, | |
35148e8f | 758 | const char *path, |
5e146a75 LP |
759 | char **ret_osrelease, |
760 | char **ret_cmdline) { | |
761 | ||
762 | _cleanup_free_ struct PeSectionHeader *sections = NULL; | |
763 | _cleanup_free_ char *osrelease = NULL, *cmdline = NULL; | |
5e146a75 LP |
764 | ssize_t n; |
765 | ||
9817b7db | 766 | struct DosFileHeader dos; |
5e146a75 LP |
767 | n = pread(fd, &dos, sizeof(dos), 0); |
768 | if (n < 0) | |
35148e8f | 769 | return log_warning_errno(errno, "%s: Failed to read DOS header, ignoring: %m", path); |
5e146a75 | 770 | if (n != sizeof(dos)) |
35148e8f | 771 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "%s: Short read while reading DOS header, ignoring.", path); |
5e146a75 LP |
772 | |
773 | if (dos.Magic[0] != 'M' || dos.Magic[1] != 'Z') | |
35148e8f | 774 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: DOS executable magic missing, ignoring.", path); |
5e146a75 | 775 | |
9817b7db ZJS |
776 | uint64_t start = unaligned_read_le32(&dos.ExeHeader); |
777 | ||
778 | struct PeHeader pe; | |
5e146a75 LP |
779 | n = pread(fd, &pe, sizeof(pe), start); |
780 | if (n < 0) | |
35148e8f | 781 | return log_warning_errno(errno, "%s: Failed to read PE header, ignoring: %m", path); |
5e146a75 | 782 | if (n != sizeof(pe)) |
35148e8f | 783 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "%s: Short read while reading PE header, ignoring.", path); |
5e146a75 LP |
784 | |
785 | if (pe.Magic[0] != 'P' || pe.Magic[1] != 'E' || pe.Magic[2] != 0 || pe.Magic[3] != 0) | |
35148e8f | 786 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: PE executable magic missing, ignoring.", path); |
5e146a75 | 787 | |
9817b7db | 788 | size_t n_sections = unaligned_read_le16(&pe.FileHeader.NumberOfSections); |
5e146a75 | 789 | if (n_sections > 96) |
35148e8f | 790 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: PE header has too many sections, ignoring.", path); |
5e146a75 LP |
791 | |
792 | sections = new(struct PeSectionHeader, n_sections); | |
793 | if (!sections) | |
794 | return log_oom(); | |
795 | ||
796 | n = pread(fd, sections, | |
797 | n_sections * sizeof(struct PeSectionHeader), | |
798 | start + sizeof(pe) + unaligned_read_le16(&pe.FileHeader.SizeOfOptionalHeader)); | |
799 | if (n < 0) | |
35148e8f | 800 | return log_warning_errno(errno, "%s: Failed to read section data, ignoring: %m", path); |
5e146a75 | 801 | if ((size_t) n != n_sections * sizeof(struct PeSectionHeader)) |
35148e8f | 802 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "%s: Short read while reading sections, ignoring.", path); |
5e146a75 | 803 | |
9817b7db | 804 | for (size_t i = 0; i < n_sections; i++) { |
5e146a75 LP |
805 | _cleanup_free_ char *k = NULL; |
806 | uint32_t offset, size; | |
807 | char **b; | |
808 | ||
809 | if (strneq((char*) sections[i].Name, ".osrel", sizeof(sections[i].Name))) | |
810 | b = &osrelease; | |
811 | else if (strneq((char*) sections[i].Name, ".cmdline", sizeof(sections[i].Name))) | |
812 | b = &cmdline; | |
813 | else | |
814 | continue; | |
815 | ||
816 | if (*b) | |
35148e8f | 817 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: Duplicate section %s, ignoring.", path, sections[i].Name); |
5e146a75 LP |
818 | |
819 | offset = unaligned_read_le32(§ions[i].PointerToRawData); | |
820 | size = unaligned_read_le32(§ions[i].VirtualSize); | |
821 | ||
822 | if (size > PE_SECTION_SIZE_MAX) | |
35148e8f | 823 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: Section %s too large, ignoring.", path, sections[i].Name); |
5e146a75 LP |
824 | |
825 | k = new(char, size+1); | |
826 | if (!k) | |
827 | return log_oom(); | |
828 | ||
829 | n = pread(fd, k, size, offset); | |
830 | if (n < 0) | |
35148e8f | 831 | return log_warning_errno(errno, "%s: Failed to read section payload, ignoring: %m", path); |
a75fcef8 | 832 | if ((size_t) n != size) |
35148e8f | 833 | return log_warning_errno(SYNTHETIC_ERRNO(EIO), "%s: Short read while reading section payload, ignoring:", path); |
5e146a75 LP |
834 | |
835 | /* Allow one trailing NUL byte, but nothing more. */ | |
836 | if (size > 0 && memchr(k, 0, size - 1)) | |
35148e8f | 837 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: Section contains embedded NUL byte, ignoring.", path); |
5e146a75 LP |
838 | |
839 | k[size] = 0; | |
840 | *b = TAKE_PTR(k); | |
841 | } | |
842 | ||
843 | if (!osrelease) | |
35148e8f | 844 | return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: Image lacks .osrel section, ignoring.", path); |
5e146a75 LP |
845 | |
846 | if (ret_osrelease) | |
847 | *ret_osrelease = TAKE_PTR(osrelease); | |
848 | if (ret_cmdline) | |
849 | *ret_cmdline = TAKE_PTR(cmdline); | |
850 | ||
851 | return 0; | |
852 | } | |
853 | ||
854 | static int boot_entries_find_unified( | |
85f4ae2f | 855 | BootConfig *config, |
5e146a75 | 856 | const char *root, |
85f4ae2f | 857 | const char *dir) { |
5e146a75 LP |
858 | |
859 | _cleanup_(closedirp) DIR *d = NULL; | |
2683ae2d | 860 | _cleanup_free_ char *full = NULL; |
5e146a75 LP |
861 | int r; |
862 | ||
85f4ae2f | 863 | assert(config); |
5e146a75 | 864 | assert(dir); |
5e146a75 | 865 | |
f461a28d | 866 | r = chase_and_opendir(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &full, &d); |
2683ae2d LP |
867 | if (r == -ENOENT) |
868 | return 0; | |
869 | if (r < 0) | |
870 | return log_error_errno(r, "Failed to open '%s/%s': %m", root, dir); | |
5e146a75 | 871 | |
2683ae2d | 872 | FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) { |
5e146a75 | 873 | _cleanup_free_ char *j = NULL, *osrelease = NULL, *cmdline = NULL; |
254d1313 | 874 | _cleanup_close_ int fd = -EBADF; |
5e146a75 LP |
875 | |
876 | if (!dirent_is_file(de)) | |
877 | continue; | |
878 | ||
879 | if (!endswith_no_case(de->d_name, ".efi")) | |
880 | continue; | |
881 | ||
85f4ae2f | 882 | if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1)) |
5e146a75 LP |
883 | return log_oom(); |
884 | ||
f2c51304 | 885 | fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY); |
5e146a75 | 886 | if (fd < 0) { |
2683ae2d | 887 | log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name); |
5e146a75 LP |
888 | continue; |
889 | } | |
890 | ||
d486a0ea LP |
891 | r = config_check_inode_relevant_and_unseen(config, fd, de->d_name); |
892 | if (r < 0) | |
893 | return r; | |
894 | if (r == 0) /* inode already seen or otherwise not relevant */ | |
5e146a75 | 895 | continue; |
5e146a75 | 896 | |
2683ae2d | 897 | j = path_join(full, de->d_name); |
5e146a75 LP |
898 | if (!j) |
899 | return log_oom(); | |
900 | ||
35148e8f JJ |
901 | if (find_sections(fd, j, &osrelease, &cmdline) < 0) |
902 | continue; | |
903 | ||
85f4ae2f | 904 | r = boot_entry_load_unified(root, j, osrelease, cmdline, config->entries + config->n_entries); |
5e146a75 LP |
905 | if (r < 0) |
906 | continue; | |
907 | ||
85f4ae2f | 908 | config->n_entries++; |
5e146a75 LP |
909 | } |
910 | ||
5e146a75 LP |
911 | return 0; |
912 | } | |
913 | ||
bb682057 | 914 | static bool find_nonunique(const BootEntry *entries, size_t n_entries, bool arr[]) { |
64f05708 ZJS |
915 | bool non_unique = false; |
916 | ||
4fe2ba0e LP |
917 | assert(entries || n_entries == 0); |
918 | assert(arr || n_entries == 0); | |
919 | ||
d5ac1d4e | 920 | for (size_t i = 0; i < n_entries; i++) |
64f05708 ZJS |
921 | arr[i] = false; |
922 | ||
d5ac1d4e LP |
923 | for (size_t i = 0; i < n_entries; i++) |
924 | for (size_t j = 0; j < n_entries; j++) | |
64f05708 ZJS |
925 | if (i != j && streq(boot_entry_title(entries + i), |
926 | boot_entry_title(entries + j))) | |
927 | non_unique = arr[i] = arr[j] = true; | |
928 | ||
929 | return non_unique; | |
930 | } | |
931 | ||
932 | static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) { | |
d5ac1d4e | 933 | _cleanup_free_ bool *arr = NULL; |
64f05708 | 934 | char *s; |
64f05708 | 935 | |
4fe2ba0e LP |
936 | assert(entries || n_entries == 0); |
937 | ||
d5ac1d4e LP |
938 | if (n_entries == 0) |
939 | return 0; | |
940 | ||
941 | arr = new(bool, n_entries); | |
942 | if (!arr) | |
943 | return -ENOMEM; | |
944 | ||
64f05708 ZJS |
945 | /* Find _all_ non-unique titles */ |
946 | if (!find_nonunique(entries, n_entries, arr)) | |
947 | return 0; | |
948 | ||
949 | /* Add version to non-unique titles */ | |
d5ac1d4e | 950 | for (size_t i = 0; i < n_entries; i++) |
64f05708 | 951 | if (arr[i] && entries[i].version) { |
d5ac1d4e | 952 | if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version) < 0) |
64f05708 ZJS |
953 | return -ENOMEM; |
954 | ||
955 | free_and_replace(entries[i].show_title, s); | |
956 | } | |
957 | ||
958 | if (!find_nonunique(entries, n_entries, arr)) | |
959 | return 0; | |
960 | ||
961 | /* Add machine-id to non-unique titles */ | |
d5ac1d4e | 962 | for (size_t i = 0; i < n_entries; i++) |
64f05708 | 963 | if (arr[i] && entries[i].machine_id) { |
d5ac1d4e | 964 | if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id) < 0) |
64f05708 ZJS |
965 | return -ENOMEM; |
966 | ||
967 | free_and_replace(entries[i].show_title, s); | |
968 | } | |
969 | ||
970 | if (!find_nonunique(entries, n_entries, arr)) | |
971 | return 0; | |
972 | ||
973 | /* Add file name to non-unique titles */ | |
d5ac1d4e | 974 | for (size_t i = 0; i < n_entries; i++) |
64f05708 | 975 | if (arr[i]) { |
d5ac1d4e | 976 | if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id) < 0) |
64f05708 ZJS |
977 | return -ENOMEM; |
978 | ||
979 | free_and_replace(entries[i].show_title, s); | |
980 | } | |
981 | ||
982 | return 0; | |
983 | } | |
984 | ||
20ec8f53 LP |
985 | static int boot_config_find(const BootConfig *config, const char *id) { |
986 | assert(config); | |
987 | ||
988 | if (!id) | |
989 | return -1; | |
990 | ||
7941f11a JJ |
991 | if (id[0] == '@') { |
992 | if (!strcaseeq(id, "@saved")) | |
993 | return -1; | |
46dc0719 YW |
994 | if (!config->entry_selected) |
995 | return -1; | |
7941f11a JJ |
996 | id = config->entry_selected; |
997 | } | |
998 | ||
20ec8f53 LP |
999 | for (size_t i = 0; i < config->n_entries; i++) |
1000 | if (fnmatch(id, config->entries[i].id, FNM_CASEFOLD) == 0) | |
1001 | return i; | |
1002 | ||
1003 | return -1; | |
1004 | } | |
1005 | ||
ad1afd60 | 1006 | static int boot_entries_select_default(const BootConfig *config) { |
7e87c7d9 ZJS |
1007 | int i; |
1008 | ||
4fe2ba0e | 1009 | assert(config); |
388d2993 ZJS |
1010 | assert(config->entries || config->n_entries == 0); |
1011 | ||
1012 | if (config->n_entries == 0) { | |
1013 | log_debug("Found no default boot entry :("); | |
1014 | return -1; /* -1 means "no default" */ | |
1015 | } | |
4fe2ba0e | 1016 | |
20ec8f53 LP |
1017 | if (config->entry_oneshot) { |
1018 | i = boot_config_find(config, config->entry_oneshot); | |
1019 | if (i >= 0) { | |
1020 | log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot", | |
1021 | config->entries[i].id); | |
1022 | return i; | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | if (config->entry_default) { | |
1027 | i = boot_config_find(config, config->entry_default); | |
1028 | if (i >= 0) { | |
1029 | log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault", | |
1030 | config->entries[i].id); | |
1031 | return i; | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | if (config->default_pattern) { | |
1036 | i = boot_config_find(config, config->default_pattern); | |
1037 | if (i >= 0) { | |
1038 | log_debug("Found default: id \"%s\" is matched by pattern \"%s\"", | |
1039 | config->entries[i].id, config->default_pattern); | |
1040 | return i; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | log_debug("Found default: first entry \"%s\"", config->entries[0].id); | |
1045 | return 0; | |
7e87c7d9 ZJS |
1046 | } |
1047 | ||
a78e472d | 1048 | static int boot_entries_select_selected(const BootConfig *config) { |
a78e472d LP |
1049 | assert(config); |
1050 | assert(config->entries || config->n_entries == 0); | |
1051 | ||
1052 | if (!config->entry_selected || config->n_entries == 0) | |
1053 | return -1; | |
1054 | ||
20ec8f53 | 1055 | return boot_config_find(config, config->entry_selected); |
a78e472d LP |
1056 | } |
1057 | ||
80a2381d | 1058 | static int boot_load_efi_entry_pointers(BootConfig *config, bool skip_efivars) { |
a78e472d LP |
1059 | int r; |
1060 | ||
1061 | assert(config); | |
1062 | ||
80a2381d | 1063 | if (skip_efivars || !is_efi_boot()) |
a78e472d LP |
1064 | return 0; |
1065 | ||
1066 | /* Loads the three "pointers" to boot loader entries from their EFI variables */ | |
1067 | ||
1068 | r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot), &config->entry_oneshot); | |
92067ab6 LP |
1069 | if (r == -ENOMEM) |
1070 | return log_oom(); | |
1071 | if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) | |
1072 | log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\", ignoring: %m"); | |
a78e472d LP |
1073 | |
1074 | r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntryDefault), &config->entry_default); | |
92067ab6 LP |
1075 | if (r == -ENOMEM) |
1076 | return log_oom(); | |
1077 | if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) | |
1078 | log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\", ignoring: %m"); | |
a78e472d LP |
1079 | |
1080 | r = efi_get_variable_string(EFI_LOADER_VARIABLE(LoaderEntrySelected), &config->entry_selected); | |
92067ab6 LP |
1081 | if (r == -ENOMEM) |
1082 | return log_oom(); | |
1083 | if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA)) | |
1084 | log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySelected\", ignoring: %m"); | |
a78e472d LP |
1085 | |
1086 | return 1; | |
1087 | } | |
1088 | ||
80a2381d | 1089 | int boot_config_select_special_entries(BootConfig *config, bool skip_efivars) { |
f7a7a5e2 LP |
1090 | int r; |
1091 | ||
1092 | assert(config); | |
1093 | ||
80a2381d | 1094 | r = boot_load_efi_entry_pointers(config, skip_efivars); |
f7a7a5e2 LP |
1095 | if (r < 0) |
1096 | return r; | |
1097 | ||
1098 | config->default_entry = boot_entries_select_default(config); | |
1099 | config->selected_entry = boot_entries_select_selected(config); | |
1100 | ||
1101 | return 0; | |
1102 | } | |
1103 | ||
5ba1550f ZJS |
1104 | int boot_config_finalize(BootConfig *config) { |
1105 | int r; | |
1106 | ||
1107 | typesafe_qsort(config->entries, config->n_entries, boot_entry_compare); | |
1108 | ||
1109 | r = boot_entries_uniquify(config->entries, config->n_entries); | |
1110 | if (r < 0) | |
1111 | return log_error_errno(r, "Failed to uniquify boot entries: %m"); | |
1112 | ||
1113 | return 0; | |
1114 | } | |
1115 | ||
af9ae750 LP |
1116 | int boot_config_load( |
1117 | BootConfig *config, | |
a2f8664e | 1118 | const char *esp_path, |
af9ae750 | 1119 | const char *xbootldr_path) { |
a2f8664e | 1120 | |
7e87c7d9 ZJS |
1121 | int r; |
1122 | ||
4fe2ba0e LP |
1123 | assert(config); |
1124 | ||
a2f8664e | 1125 | if (esp_path) { |
2683ae2d | 1126 | r = boot_loader_read_conf_path(config, esp_path, "/loader/loader.conf"); |
a2f8664e LP |
1127 | if (r < 0) |
1128 | return r; | |
7e87c7d9 | 1129 | |
2683ae2d | 1130 | r = boot_entries_find_type1(config, esp_path, "/loader/entries"); |
a2f8664e LP |
1131 | if (r < 0) |
1132 | return r; | |
5e146a75 | 1133 | |
2683ae2d | 1134 | r = boot_entries_find_unified(config, esp_path, "/EFI/Linux/"); |
5e146a75 LP |
1135 | if (r < 0) |
1136 | return r; | |
a2f8664e LP |
1137 | } |
1138 | ||
1139 | if (xbootldr_path) { | |
2683ae2d | 1140 | r = boot_entries_find_type1(config, xbootldr_path, "/loader/entries"); |
a2f8664e LP |
1141 | if (r < 0) |
1142 | return r; | |
5e146a75 | 1143 | |
2683ae2d | 1144 | r = boot_entries_find_unified(config, xbootldr_path, "/EFI/Linux/"); |
5e146a75 LP |
1145 | if (r < 0) |
1146 | return r; | |
a2f8664e | 1147 | } |
7e87c7d9 | 1148 | |
5ba1550f | 1149 | return boot_config_finalize(config); |
7e87c7d9 | 1150 | } |
af918182 | 1151 | |
af9ae750 LP |
1152 | int boot_config_load_auto( |
1153 | BootConfig *config, | |
eea4ce1e | 1154 | const char *override_esp_path, |
af9ae750 | 1155 | const char *override_xbootldr_path) { |
eea4ce1e LP |
1156 | |
1157 | _cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL; | |
f63b5ad9 | 1158 | dev_t esp_devid = 0, xbootldr_devid = 0; |
eea4ce1e LP |
1159 | int r; |
1160 | ||
1161 | assert(config); | |
1162 | ||
1163 | /* This function is similar to boot_entries_load_config(), however we automatically search for the | |
1164 | * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass | |
1165 | * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's | |
1166 | * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from | |
1167 | * it). This allows other boot loaders to pass boot loader entry information to our tools if they | |
1168 | * want to. */ | |
1169 | ||
1170 | if (!override_esp_path && !override_xbootldr_path) { | |
f40999f8 | 1171 | if (access("/run/boot-loader-entries/", F_OK) >= 0) |
af9ae750 | 1172 | return boot_config_load(config, "/run/boot-loader-entries/", NULL); |
eea4ce1e | 1173 | |
f40999f8 ZJS |
1174 | if (errno != ENOENT) |
1175 | return log_error_errno(errno, | |
1176 | "Failed to determine whether /run/boot-loader-entries/ exists: %m"); | |
eea4ce1e LP |
1177 | } |
1178 | ||
80a2381d | 1179 | r = find_esp_and_warn(NULL, override_esp_path, /* unprivileged_mode= */ false, &esp_where, NULL, NULL, NULL, NULL, &esp_devid); |
cc5957dc | 1180 | if (r < 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */ |
eea4ce1e LP |
1181 | return r; |
1182 | ||
80a2381d | 1183 | r = find_xbootldr_and_warn(NULL, override_xbootldr_path, /* unprivileged_mode= */ false, &xbootldr_where, NULL, &xbootldr_devid); |
eea4ce1e LP |
1184 | if (r < 0 && r != -ENOKEY) |
1185 | return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */ | |
1186 | ||
f63b5ad9 | 1187 | /* If both paths actually refer to the same inode, suppress the xbootldr path */ |
7176f06c | 1188 | if (esp_where && xbootldr_where && devnum_set_and_equal(esp_devid, xbootldr_devid)) |
f63b5ad9 LP |
1189 | xbootldr_where = mfree(xbootldr_where); |
1190 | ||
af9ae750 | 1191 | return boot_config_load(config, esp_where, xbootldr_where); |
eea4ce1e LP |
1192 | } |
1193 | ||
af9ae750 | 1194 | int boot_config_augment_from_loader( |
d4bd786d | 1195 | BootConfig *config, |
9951736b LP |
1196 | char **found_by_loader, |
1197 | bool only_auto) { | |
d4bd786d LP |
1198 | |
1199 | static const char *const title_table[] = { | |
93f14ce2 LP |
1200 | /* Pretty names for a few well-known automatically discovered entries. */ |
1201 | "auto-osx", "macOS", | |
1202 | "auto-windows", "Windows Boot Manager", | |
1203 | "auto-efi-shell", "EFI Shell", | |
1204 | "auto-efi-default", "EFI Default Loader", | |
1205 | "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface", | |
d4f72d10 | 1206 | NULL, |
93f14ce2 LP |
1207 | }; |
1208 | ||
93f14ce2 LP |
1209 | assert(config); |
1210 | ||
1211 | /* Let's add the entries discovered by the boot loader to the end of our list, unless they are | |
1212 | * already included there. */ | |
1213 | ||
93f14ce2 | 1214 | STRV_FOREACH(i, found_by_loader) { |
bb682057 | 1215 | BootEntry *existing; |
ce4c4f81 | 1216 | _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL; |
93f14ce2 | 1217 | |
bb682057 LP |
1218 | existing = boot_config_find_entry(config, *i); |
1219 | if (existing) { | |
1220 | existing->reported_by_loader = true; | |
93f14ce2 | 1221 | continue; |
bb682057 | 1222 | } |
93f14ce2 | 1223 | |
9951736b | 1224 | if (only_auto && !startswith(*i, "auto-")) |
93f14ce2 LP |
1225 | continue; |
1226 | ||
1227 | c = strdup(*i); | |
1228 | if (!c) | |
1229 | return log_oom(); | |
1230 | ||
2034c8b8 | 1231 | STRV_FOREACH_PAIR(a, b, title_table) |
93f14ce2 LP |
1232 | if (streq(*a, *i)) { |
1233 | t = strdup(*b); | |
1234 | if (!t) | |
1235 | return log_oom(); | |
1236 | break; | |
1237 | } | |
1238 | ||
e6f055cb | 1239 | p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE(LoaderEntries))); |
ce4c4f81 ZJS |
1240 | if (!p) |
1241 | return log_oom(); | |
1242 | ||
319a4f4b | 1243 | if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1)) |
93f14ce2 LP |
1244 | return log_oom(); |
1245 | ||
1246 | config->entries[config->n_entries++] = (BootEntry) { | |
bb682057 | 1247 | .type = startswith(*i, "auto-") ? BOOT_ENTRY_LOADER_AUTO : BOOT_ENTRY_LOADER, |
93f14ce2 LP |
1248 | .id = TAKE_PTR(c), |
1249 | .title = TAKE_PTR(t), | |
ce4c4f81 | 1250 | .path = TAKE_PTR(p), |
bb682057 | 1251 | .reported_by_loader = true, |
7f5780ed LP |
1252 | .tries_left = UINT_MAX, |
1253 | .tries_done = UINT_MAX, | |
93f14ce2 LP |
1254 | }; |
1255 | } | |
1256 | ||
1257 | return 0; | |
1258 | } | |
3f8e42c0 LP |
1259 | |
1260 | BootEntry* boot_config_find_entry(BootConfig *config, const char *id) { | |
1261 | assert(config); | |
1262 | assert(id); | |
1263 | ||
1264 | for (size_t j = 0; j < config->n_entries; j++) | |
1265 | if (streq_ptr(config->entries[j].id, id) || | |
1266 | streq_ptr(config->entries[j].id_old, id)) | |
1267 | return config->entries + j; | |
1268 | ||
1269 | return NULL; | |
1270 | } | |
432ce537 | 1271 | |
2683ae2d LP |
1272 | static void boot_entry_file_list( |
1273 | const char *field, | |
1274 | const char *root, | |
1275 | const char *p, | |
1276 | int *ret_status) { | |
1277 | ||
1278 | assert(p); | |
1279 | assert(ret_status); | |
1280 | ||
f461a28d | 1281 | int status = chase_and_access(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, NULL); |
432ce537 | 1282 | |
78517322 | 1283 | /* Note that this shows two '/' between the root and the file. This is intentional to highlight (in |
6c2d70ce | 1284 | * the absence of color support) to the user that the boot loader is only interested in the second |
78517322 JJ |
1285 | * part of the file. */ |
1286 | printf("%13s%s %s%s/%s", strempty(field), field ? ":" : " ", ansi_grey(), root, ansi_normal()); | |
1287 | ||
432ce537 ZJS |
1288 | if (status < 0) { |
1289 | errno = -status; | |
1290 | printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal()); | |
1291 | } else | |
1292 | printf("%s\n", p); | |
1293 | ||
1294 | if (*ret_status == 0 && status < 0) | |
1295 | *ret_status = status; | |
1296 | } | |
1297 | ||
1298 | int show_boot_entry( | |
1299 | const BootEntry *e, | |
1300 | bool show_as_default, | |
1301 | bool show_as_selected, | |
1302 | bool show_reported) { | |
1303 | ||
1304 | int status = 0; | |
1305 | ||
1306 | /* Returns 0 on success, negative on processing error, and positive if something is wrong with the | |
1307 | boot entry itself. */ | |
1308 | ||
1309 | assert(e); | |
1310 | ||
1311 | printf(" type: %s\n", | |
1312 | boot_entry_type_to_string(e->type)); | |
1313 | ||
1314 | printf(" title: %s%s%s", | |
1315 | ansi_highlight(), boot_entry_title(e), ansi_normal()); | |
1316 | ||
1317 | if (show_as_default) | |
1318 | printf(" %s(default)%s", | |
1319 | ansi_highlight_green(), ansi_normal()); | |
1320 | ||
1321 | if (show_as_selected) | |
1322 | printf(" %s(selected)%s", | |
1323 | ansi_highlight_magenta(), ansi_normal()); | |
1324 | ||
1325 | if (show_reported) { | |
1326 | if (e->type == BOOT_ENTRY_LOADER) | |
1327 | printf(" %s(reported/absent)%s", | |
1328 | ansi_highlight_red(), ansi_normal()); | |
1329 | else if (!e->reported_by_loader && e->type != BOOT_ENTRY_LOADER_AUTO) | |
1330 | printf(" %s(not reported/new)%s", | |
1331 | ansi_highlight_green(), ansi_normal()); | |
1332 | } | |
1333 | ||
1334 | putchar('\n'); | |
1335 | ||
1336 | if (e->id) | |
1337 | printf(" id: %s\n", e->id); | |
1338 | if (e->path) { | |
5a65d2e5 ZJS |
1339 | _cleanup_free_ char *text = NULL, *link = NULL; |
1340 | ||
1341 | const char *p = e->root ? path_startswith(e->path, e->root) : NULL; | |
1342 | if (p) { | |
1343 | text = strjoin(ansi_grey(), e->root, "/", ansi_normal(), "/", p); | |
1344 | if (!text) | |
1345 | return log_oom(); | |
1346 | } | |
432ce537 ZJS |
1347 | |
1348 | /* Let's urlify the link to make it easy to view in an editor, but only if it is a text | |
1349 | * file. Unified images are binary ELFs, and EFI variables are not pure text either. */ | |
1350 | if (e->type == BOOT_ENTRY_CONF) | |
5a65d2e5 | 1351 | (void) terminal_urlify_path(e->path, text, &link); |
432ce537 | 1352 | |
5a65d2e5 | 1353 | printf(" source: %s\n", link ?: text ?: e->path); |
432ce537 | 1354 | } |
7f5780ed LP |
1355 | if (e->tries_left != UINT_MAX) { |
1356 | printf(" tries: %u left", e->tries_left); | |
1357 | ||
1358 | if (e->tries_done != UINT_MAX) | |
1359 | printf("; %u done\n", e->tries_done); | |
1360 | else | |
1361 | printf("\n"); | |
1362 | } | |
1363 | ||
432ce537 ZJS |
1364 | if (e->sort_key) |
1365 | printf(" sort-key: %s\n", e->sort_key); | |
1366 | if (e->version) | |
1367 | printf(" version: %s\n", e->version); | |
1368 | if (e->machine_id) | |
1369 | printf(" machine-id: %s\n", e->machine_id); | |
1370 | if (e->architecture) | |
1371 | printf(" architecture: %s\n", e->architecture); | |
1372 | if (e->kernel) | |
1373 | boot_entry_file_list("linux", e->root, e->kernel, &status); | |
e443cc7c LN |
1374 | if (e->efi) |
1375 | boot_entry_file_list("efi", e->root, e->efi, &status); | |
432ce537 ZJS |
1376 | |
1377 | STRV_FOREACH(s, e->initrd) | |
1378 | boot_entry_file_list(s == e->initrd ? "initrd" : NULL, | |
1379 | e->root, | |
1380 | *s, | |
1381 | &status); | |
1382 | ||
1383 | if (!strv_isempty(e->options)) { | |
1384 | _cleanup_free_ char *t = NULL, *t2 = NULL; | |
1385 | _cleanup_strv_free_ char **ts = NULL; | |
1386 | ||
1387 | t = strv_join(e->options, " "); | |
1388 | if (!t) | |
1389 | return log_oom(); | |
1390 | ||
1391 | ts = strv_split_newlines(t); | |
1392 | if (!ts) | |
1393 | return log_oom(); | |
1394 | ||
1395 | t2 = strv_join(ts, "\n "); | |
1396 | if (!t2) | |
1397 | return log_oom(); | |
1398 | ||
1399 | printf(" options: %s\n", t2); | |
1400 | } | |
1401 | ||
1402 | if (e->device_tree) | |
1403 | boot_entry_file_list("devicetree", e->root, e->device_tree, &status); | |
1404 | ||
1405 | STRV_FOREACH(s, e->device_tree_overlay) | |
1406 | boot_entry_file_list(s == e->device_tree_overlay ? "devicetree-overlay" : NULL, | |
1407 | e->root, | |
1408 | *s, | |
1409 | &status); | |
1410 | ||
1411 | return -status; | |
1412 | } | |
1413 | ||
1414 | int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) { | |
1415 | int r; | |
1416 | ||
8c87f247 LP |
1417 | assert(config); |
1418 | ||
432ce537 | 1419 | if (!FLAGS_SET(json_format, JSON_FORMAT_OFF)) { |
b570204a ZJS |
1420 | _cleanup_(json_variant_unrefp) JsonVariant *array = NULL; |
1421 | ||
432ce537 ZJS |
1422 | for (size_t i = 0; i < config->n_entries; i++) { |
1423 | _cleanup_free_ char *opts = NULL; | |
1424 | const BootEntry *e = config->entries + i; | |
1425 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
1426 | ||
1427 | if (!strv_isempty(e->options)) { | |
1428 | opts = strv_join(e->options, " "); | |
1429 | if (!opts) | |
1430 | return log_oom(); | |
1431 | } | |
1432 | ||
4d49d19e | 1433 | r = json_append(&v, JSON_BUILD_OBJECT( |
4bc14b17 | 1434 | JSON_BUILD_PAIR("type", JSON_BUILD_STRING(boot_entry_type_json_to_string(e->type))), |
432ce537 ZJS |
1435 | JSON_BUILD_PAIR_CONDITION(e->id, "id", JSON_BUILD_STRING(e->id)), |
1436 | JSON_BUILD_PAIR_CONDITION(e->path, "path", JSON_BUILD_STRING(e->path)), | |
1437 | JSON_BUILD_PAIR_CONDITION(e->root, "root", JSON_BUILD_STRING(e->root)), | |
1438 | JSON_BUILD_PAIR_CONDITION(e->title, "title", JSON_BUILD_STRING(e->title)), | |
1439 | JSON_BUILD_PAIR_CONDITION(boot_entry_title(e), "showTitle", JSON_BUILD_STRING(boot_entry_title(e))), | |
1440 | JSON_BUILD_PAIR_CONDITION(e->sort_key, "sortKey", JSON_BUILD_STRING(e->sort_key)), | |
1441 | JSON_BUILD_PAIR_CONDITION(e->version, "version", JSON_BUILD_STRING(e->version)), | |
1442 | JSON_BUILD_PAIR_CONDITION(e->machine_id, "machineId", JSON_BUILD_STRING(e->machine_id)), | |
1443 | JSON_BUILD_PAIR_CONDITION(e->architecture, "architecture", JSON_BUILD_STRING(e->architecture)), | |
1444 | JSON_BUILD_PAIR_CONDITION(opts, "options", JSON_BUILD_STRING(opts)), | |
1445 | JSON_BUILD_PAIR_CONDITION(e->kernel, "linux", JSON_BUILD_STRING(e->kernel)), | |
1446 | JSON_BUILD_PAIR_CONDITION(e->efi, "efi", JSON_BUILD_STRING(e->efi)), | |
1447 | JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", JSON_BUILD_STRV(e->initrd)), | |
1448 | JSON_BUILD_PAIR_CONDITION(e->device_tree, "devicetree", JSON_BUILD_STRING(e->device_tree)), | |
4d49d19e YW |
1449 | JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", JSON_BUILD_STRV(e->device_tree_overlay)))); |
1450 | if (r < 0) | |
1451 | return log_oom(); | |
1452 | ||
1453 | /* Sanitizers (only memory sanitizer?) do not like function call with too many | |
1454 | * arguments and trigger false positive warnings. Let's not add too many json objects | |
1455 | * at once. */ | |
1456 | r = json_append(&v, JSON_BUILD_OBJECT( | |
4bc14b17 | 1457 | JSON_BUILD_PAIR("isReported", JSON_BUILD_BOOLEAN(e->reported_by_loader)), |
7f5780ed | 1458 | JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", JSON_BUILD_UNSIGNED(e->tries_left)), |
4a6c6d35 LP |
1459 | JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", JSON_BUILD_UNSIGNED(e->tries_done)), |
1460 | JSON_BUILD_PAIR_CONDITION(config->default_entry >= 0, "isDefault", JSON_BUILD_BOOLEAN(i == (size_t) config->default_entry)), | |
1461 | JSON_BUILD_PAIR_CONDITION(config->selected_entry >= 0, "isSelected", JSON_BUILD_BOOLEAN(i == (size_t) config->selected_entry)))); | |
1462 | ||
432ce537 ZJS |
1463 | if (r < 0) |
1464 | return log_oom(); | |
1465 | ||
b570204a ZJS |
1466 | r = json_variant_append_array(&array, v); |
1467 | if (r < 0) | |
1468 | return log_oom(); | |
432ce537 ZJS |
1469 | } |
1470 | ||
c3f0bff9 | 1471 | json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL); |
b570204a | 1472 | |
432ce537 | 1473 | } else { |
432ce537 ZJS |
1474 | for (size_t n = 0; n < config->n_entries; n++) { |
1475 | r = show_boot_entry( | |
1476 | config->entries + n, | |
1477 | /* show_as_default= */ n == (size_t) config->default_entry, | |
1478 | /* show_as_selected= */ n == (size_t) config->selected_entry, | |
1479 | /* show_discovered= */ true); | |
1480 | if (r < 0) | |
1481 | return r; | |
1482 | ||
1483 | if (n+1 < config->n_entries) | |
1484 | putchar('\n'); | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | return 0; | |
1489 | } |