• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 18297936107

06 Oct 2025 02:12PM UTC coverage: 72.276% (+0.03%) from 72.246%
18297936107

push

github

poettering
bootspec: also process uki-url boot loader spec field

Let's also add support for "uki-url", which was added at the same time to
the spec as "uki".

Follow-up for: 4a94a1b83

2 of 5 new or added lines in 1 file covered. (40.0%)

171 existing lines in 30 files now uncovered.

303492 of 419906 relevant lines covered (72.28%)

1100392.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.57
/src/shared/bootspec.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <unistd.h>
5

6
#include "sd-json.h"
7

8
#include "alloc-util.h"
9
#include "bootspec.h"
10
#include "bootspec-fundamental.h"
11
#include "chase.h"
12
#include "devnum-util.h"
13
#include "dirent-util.h"
14
#include "efi-loader.h"
15
#include "efivars.h"
16
#include "env-file.h"
17
#include "extract-word.h"
18
#include "fd-util.h"
19
#include "fileio.h"
20
#include "find-esp.h"
21
#include "log.h"
22
#include "parse-util.h"
23
#include "path-util.h"
24
#include "pe-binary.h"
25
#include "pretty-print.h"
26
#include "recurse-dir.h"
27
#include "set.h"
28
#include "sort-util.h"
29
#include "stat-util.h"
30
#include "string-table.h"
31
#include "string-util.h"
32
#include "strv.h"
33
#include "uki.h"
34

35
static const char* const boot_entry_type_description_table[_BOOT_ENTRY_TYPE_MAX] = {
36
        [BOOT_ENTRY_TYPE1]  = "Boot Loader Specification Type #1 (.conf)",
37
        [BOOT_ENTRY_TYPE2]  = "Boot Loader Specification Type #2 (UKI, .efi)",
38
        [BOOT_ENTRY_LOADER] = "Reported by Boot Loader",
39
        [BOOT_ENTRY_AUTO]   = "Automatic",
40
};
41

42
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type_description, BootEntryType);
22✔
43

44
static const char* const boot_entry_type_table[_BOOT_ENTRY_TYPE_MAX] = {
45
        [BOOT_ENTRY_TYPE1]  = "type1",
46
        [BOOT_ENTRY_TYPE2]  = "type2",
47
        [BOOT_ENTRY_LOADER] = "loader",
48
        [BOOT_ENTRY_AUTO]   = "auto",
49
};
50

51
DEFINE_STRING_TABLE_LOOKUP(boot_entry_type, BootEntryType);
15✔
52

53
static const char* const boot_entry_source_description_table[_BOOT_ENTRY_SOURCE_MAX] = {
54
        [BOOT_ENTRY_ESP]      = "EFI System Partition",
55
        [BOOT_ENTRY_XBOOTLDR] = "Extended Boot Loader Partition",
56
};
57

58
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source_description, BootEntrySource);
22✔
59

60
static const char* const boot_entry_source_table[_BOOT_ENTRY_SOURCE_MAX] = {
61
        [BOOT_ENTRY_ESP]      = "esp",
62
        [BOOT_ENTRY_XBOOTLDR] = "xbootldr",
63
};
64

65
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source, BootEntrySource);
15✔
66

67
static void boot_entry_addons_done(BootEntryAddons *addons) {
316✔
68
        assert(addons);
316✔
69

70
        FOREACH_ARRAY(addon, addons->items, addons->n_items) {
333✔
71
                free(addon->cmdline);
17✔
72
                free(addon->location);
17✔
73
        }
74
        addons->items = mfree(addons->items);
316✔
75
        addons->n_items = 0;
316✔
76
}
316✔
77

78
static void boot_entry_free(BootEntry *entry) {
152✔
79
        assert(entry);
152✔
80

81
        free(entry->id);
152✔
82
        free(entry->id_old);
152✔
83
        free(entry->id_without_profile);
152✔
84
        free(entry->path);
152✔
85
        free(entry->root);
152✔
86
        free(entry->title);
152✔
87
        free(entry->show_title);
152✔
88
        free(entry->sort_key);
152✔
89
        free(entry->version);
152✔
90
        free(entry->machine_id);
152✔
91
        free(entry->architecture);
152✔
92
        strv_free(entry->options);
152✔
93
        boot_entry_addons_done(&entry->local_addons);
152✔
94
        free(entry->kernel);
152✔
95
        free(entry->efi);
152✔
96
        free(entry->uki);
152✔
97
        free(entry->uki_url);
152✔
98
        strv_free(entry->initrd);
152✔
99
        free(entry->device_tree);
152✔
100
        strv_free(entry->device_tree_overlay);
152✔
101
}
152✔
102

103
static int mangle_path(
×
104
                const char *fname,
105
                unsigned line,
106
                const char *field,
107
                const char *p,
108
                char **ret) {
109

110
        _cleanup_free_ char *c = NULL;
×
111

112
        assert(field);
×
113
        assert(p);
×
114
        assert(ret);
×
115

116
        /* Spec leaves open if prefixed with "/" or not, let's normalize that */
117
        c = path_make_absolute(p, "/");
×
118
        if (!c)
×
119
                return -ENOMEM;
120

121
        /* We only reference files, never directories */
122
        if (endswith(c, "/")) {
×
123
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' has trailing slash, ignoring: %s", field, c);
×
124
                *ret = NULL;
×
125
                return 0;
×
126
        }
127

128
        /* Remove duplicate "/" */
129
        path_simplify(c);
×
130

131
        /* No ".." or "." or so */
132
        if (!path_is_normalized(c)) {
×
133
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' is not normalized, ignoring: %s", field, c);
×
134
                *ret = NULL;
×
135
                return 0;
×
136
        }
137

138
        *ret = TAKE_PTR(c);
×
139
        return 1;
×
140
}
141

142
static int parse_path_one(
×
143
                const char *fname,
144
                unsigned line,
145
                const char *field,
146
                char **s,
147
                const char *p) {
148

149
        _cleanup_free_ char *c = NULL;
×
150
        int r;
×
151

152
        assert(field);
×
153
        assert(s);
×
154
        assert(p);
×
155

156
        r = mangle_path(fname, line, field, p, &c);
×
157
        if (r <= 0)
×
158
                return r;
159

160
        return free_and_replace(*s, c);
×
161
}
162

163
static int parse_path_strv(
×
164
                const char *fname,
165
                unsigned line,
166
                const char *field,
167
                char ***s,
168
                const char *p) {
169

170
        char *c;
×
171
        int r;
×
172

173
        assert(field);
×
174
        assert(s);
×
175
        assert(p);
×
176

177
        r = mangle_path(fname, line, field, p, &c);
×
178
        if (r <= 0)
×
179
                return r;
×
180

181
        return strv_consume(s, c);
×
182
}
183

184
static int parse_path_many(
×
185
                const char *fname,
186
                unsigned line,
187
                const char *field,
188
                char ***s,
189
                const char *p) {
190

191
        _cleanup_strv_free_ char **l = NULL, **f = NULL;
×
192
        int r;
×
193

194
        l = strv_split(p, NULL);
×
195
        if (!l)
×
196
                return -ENOMEM;
197

198
        STRV_FOREACH(i, l) {
×
199
                char *c;
×
200

201
                r = mangle_path(fname, line, field, *i, &c);
×
202
                if (r < 0)
×
203
                        return r;
×
204
                if (r == 0)
×
205
                        continue;
×
206

207
                r = strv_consume(&f, c);
×
208
                if (r < 0)
×
209
                        return r;
210
        }
211

212
        return strv_extend_strv_consume(s, TAKE_PTR(f), /* filter_duplicates= */ false);
×
213
}
214

215
static int parse_tries(const char *fname, const char **p, unsigned *ret) {
36✔
216
        _cleanup_free_ char *d = NULL;
36✔
217
        unsigned tries;
36✔
218
        size_t n;
36✔
219
        int r;
36✔
220

221
        assert(fname);
36✔
222
        assert(p);
36✔
223
        assert(*p);
36✔
224
        assert(ret);
36✔
225

226
        n = strspn(*p, DIGITS);
36✔
227
        if (n == 0) {
36✔
228
                *ret = UINT_MAX;
2✔
229
                return 0;
2✔
230
        }
231

232
        d = strndup(*p, n);
34✔
233
        if (!d)
34✔
234
                return log_oom();
×
235

236
        r = safe_atou_full(d, 10, &tries);
34✔
237
        if (r >= 0 && tries > INT_MAX) /* sd-boot allows INT_MAX, let's use the same limit */
34✔
238
                r = -ERANGE;
239
        if (r < 0)
32✔
240
                return log_error_errno(r, "Failed to parse tries counter of filename '%s': %m", fname);
2✔
241

242
        *p = *p + n;
32✔
243
        *ret = tries;
32✔
244
        return 1;
32✔
245
}
246

247
int boot_filename_extract_tries(
88✔
248
                const char *fname,
249
                char **ret_stripped,
250
                unsigned *ret_tries_left,
251
                unsigned *ret_tries_done) {
252

253
        unsigned tries_left = UINT_MAX, tries_done = UINT_MAX;
88✔
254
        _cleanup_free_ char *stripped = NULL;
88✔
255
        const char *p, *suffix, *m;
88✔
256
        int r;
88✔
257

258
        assert(fname);
88✔
259
        assert(ret_stripped);
88✔
260
        assert(ret_tries_left);
88✔
261
        assert(ret_tries_done);
88✔
262

263
        /* Be liberal with suffix, only insist on a dot. After all we want to cover any capitalization here
264
         * (vfat is case insensitive after all), and at least .efi and .conf as suffix. */
265
        suffix = strrchr(fname, '.');
88✔
266
        if (!suffix)
88✔
267
                goto nothing;
1✔
268

269
        p = m = memrchr(fname, '+', suffix - fname);
87✔
270
        if (!p)
87✔
271
                goto nothing;
64✔
272
        p++;
23✔
273

274
        r = parse_tries(fname, &p, &tries_left);
23✔
275
        if (r < 0)
23✔
276
                return r;
277
        if (r == 0)
22✔
278
                goto nothing;
2✔
279

280
        if (*p == '-') {
20✔
281
                p++;
13✔
282

283
                r = parse_tries(fname, &p, &tries_done);
13✔
284
                if (r < 0)
13✔
285
                        return r;
286
                if (r == 0)
12✔
287
                        goto nothing;
×
288
        }
289

290
        if (p != suffix)
19✔
291
                goto nothing;
3✔
292

293
        stripped = strndup(fname, m - fname);
16✔
294
        if (!stripped)
16✔
295
                return log_oom();
×
296

297
        if (!strextend(&stripped, suffix))
16✔
298
                return log_oom();
×
299

300
        *ret_stripped = TAKE_PTR(stripped);
16✔
301
        *ret_tries_left = tries_left;
16✔
302
        *ret_tries_done = tries_done;
16✔
303

304
        return 0;
16✔
305

306
nothing:
70✔
307
        stripped = strdup(fname);
70✔
308
        if (!stripped)
70✔
309
                return log_oom();
×
310

311
        *ret_stripped = TAKE_PTR(stripped);
70✔
312
        *ret_tries_left = *ret_tries_done = UINT_MAX;
70✔
313
        return 0;
70✔
314
}
315

316
static int boot_entry_load_type1(
8✔
317
                FILE *f,
318
                const char *root,
319
                const BootEntrySource source,
320
                const char *dir,
321
                const char *fname,
322
                BootEntry *ret) {
323

324
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_TYPE1, source);
8✔
325
        char *c;
8✔
326
        int r;
8✔
327

328
        assert(f);
8✔
329
        assert(root);
8✔
330
        assert(dir);
8✔
331
        assert(fname);
8✔
332
        assert(ret);
8✔
333

334
        /* Loads a Type #1 boot menu entry from the specified FILE* object */
335

336
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
8✔
337
        if (r < 0)
8✔
338
                return r;
339

340
        if (!efi_loader_entry_name_valid(tmp.id))
8✔
341
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", fname);
×
342

343
        c = endswith_no_case(tmp.id, ".conf");
8✔
344
        if (!c)
8✔
345
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", fname);
×
346

347
        tmp.id_old = strndup(tmp.id, c - tmp.id); /* Without .conf suffix */
8✔
348
        if (!tmp.id_old)
8✔
349
                return log_oom();
×
350

351
        tmp.path = path_join(dir, fname);
8✔
352
        if (!tmp.path)
8✔
353
                return log_oom();
×
354

355
        tmp.root = strdup(root);
8✔
356
        if (!tmp.root)
8✔
357
                return log_oom();
×
358

359
        for (unsigned line = 1;; line++) {
27✔
360
                _cleanup_free_ char *buf = NULL, *field = NULL;
27✔
361

362
                r = read_stripped_line(f, LONG_LINE_MAX, &buf);
35✔
363
                if (r == -ENOBUFS)
35✔
364
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long.");
×
365
                if (r < 0)
35✔
366
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m");
×
367
                if (r == 0)
35✔
368
                        break;
369

370
                if (IN_SET(buf[0], '#', '\0'))
27✔
371
                        continue;
×
372

373
                const char *p = buf;
27✔
374
                r = extract_first_word(&p, &field, NULL, 0);
27✔
375
                if (r < 0) {
27✔
376
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m");
×
377
                        continue;
×
378
                }
379
                if (r == 0) {
27✔
380
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Bad syntax, ignoring line.");
×
381
                        continue;
×
382
                }
383

384
                if (isempty(p)) {
27✔
385
                        /* Some fields can reasonably have an empty value. In other cases warn. */
386
                        if (!STR_IN_SET(field, "options", "devicetree-overlay"))
×
387
                                log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Field '%s' without value, ignoring line.", field);
×
388

389
                        continue;
×
390
                }
391

392
                if (streq(field, "title"))
27✔
393
                        r = free_and_strdup(&tmp.title, p);
8✔
394
                else if (streq(field, "sort-key"))
19✔
395
                        r = free_and_strdup(&tmp.sort_key, p);
3✔
396
                else if (streq(field, "version"))
16✔
397
                        r = free_and_strdup(&tmp.version, p);
8✔
398
                else if (streq(field, "machine-id"))
8✔
399
                        r = free_and_strdup(&tmp.machine_id, p);
8✔
400
                else if (streq(field, "architecture"))
×
401
                        r = free_and_strdup(&tmp.architecture, p);
×
402
                else if (streq(field, "options"))
×
403
                        r = strv_extend(&tmp.options, p);
×
404
                else if (streq(field, "linux"))
×
405
                        r = parse_path_one(tmp.path, line, field, &tmp.kernel, p);
×
406
                else if (streq(field, "efi"))
×
407
                        r = parse_path_one(tmp.path, line, field, &tmp.efi, p);
×
408
                else if (streq(field, "uki"))
×
409
                        r = parse_path_one(tmp.path, line, field, &tmp.uki, p);
×
NEW
410
                else if (streq(field, "uki-url"))
×
NEW
411
                        r = free_and_strdup(&tmp.uki_url, p);
×
412
                else if (streq(field, "profile"))
×
413
                        r = safe_atou_full(p, 10, &tmp.profile);
×
414
                else if (streq(field, "initrd"))
×
415
                        r = parse_path_strv(tmp.path, line, field, &tmp.initrd, p);
×
416
                else if (streq(field, "devicetree"))
×
417
                        r = parse_path_one(tmp.path, line, field, &tmp.device_tree, p);
×
418
                else if (streq(field, "devicetree-overlay"))
×
419
                        r = parse_path_many(tmp.path, line, field, &tmp.device_tree_overlay, p);
×
420
                else {
421
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Unknown line '%s', ignoring.", field);
×
422
                        continue;
×
423
                }
424
                if (r < 0)
27✔
425
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while parsing: %m");
×
426
        }
427

428
        *ret = TAKE_STRUCT(tmp);
8✔
429
        return 0;
8✔
430
}
431

432
int boot_config_load_type1(
8✔
433
                BootConfig *config,
434
                FILE *f,
435
                const char *root,
436
                const BootEntrySource source,
437
                const char *dir,
438
                const char *fname) {
439
        int r;
8✔
440

441
        assert(config);
8✔
442
        assert(f);
8✔
443
        assert(root);
8✔
444
        assert(dir);
8✔
445
        assert(fname);
8✔
446

447
        if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
8✔
448
                return log_oom();
×
449

450
        BootEntry *entry = config->entries + config->n_entries;
8✔
451

452
        r = boot_entry_load_type1(f, root, source, dir, fname, entry);
8✔
453
        if (r < 0)
8✔
454
                return r;
455
        config->n_entries++;
8✔
456

457
        entry->global_addons = &config->global_addons[source];
8✔
458

459
        return 0;
8✔
460
}
461

462
void boot_config_free(BootConfig *config) {
33✔
463
        assert(config);
33✔
464

465
        free(config->default_pattern);
33✔
466

467
        free(config->entry_oneshot);
33✔
468
        free(config->entry_default);
33✔
469
        free(config->entry_selected);
33✔
470
        free(config->entry_sysfail);
33✔
471

472
        FOREACH_ARRAY(i, config->entries, config->n_entries)
126✔
473
                boot_entry_free(i);
93✔
474
        free(config->entries);
33✔
475

476
        FOREACH_ELEMENT(i, config->global_addons)
99✔
477
                boot_entry_addons_done(i);
66✔
478

479
        set_free(config->inodes_seen);
33✔
480
}
33✔
481

482
int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
31✔
483
        int r;
31✔
484

485
        assert(config);
31✔
486
        assert(file);
31✔
487
        assert(path);
31✔
488

489
        for (unsigned line = 1;; line++) {
62✔
490
                _cleanup_free_ char *buf = NULL, *field = NULL;
62✔
491

492
                r = read_stripped_line(file, LONG_LINE_MAX, &buf);
93✔
493
                if (r == -ENOBUFS)
93✔
494
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long.");
×
495
                if (r < 0)
93✔
496
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m");
×
497
                if (r == 0)
93✔
498
                        break;
499

500
                if (IN_SET(buf[0], '#', '\0'))
62✔
501
                        continue;
62✔
502

503
                const char *p = buf;
×
504
                r = extract_first_word(&p, &field, NULL, 0);
×
505
                if (r < 0) {
×
506
                        log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m");
×
507
                        continue;
×
508
                }
509
                if (r == 0) {
×
510
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Bad syntax, ignoring line.");
×
511
                        continue;
×
512
                }
513
                if (isempty(p)) {
×
514
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Field '%s' without value, ignoring line.", field);
×
515
                        continue;
×
516
                }
517

518
                if (streq(field, "default"))
×
519
                        r = free_and_strdup(&config->default_pattern, p);
×
520
                else if (STR_IN_SET(field, "timeout", "editor", "auto-entries", "auto-firmware",
×
521
                                    "auto-poweroff", "auto-reboot", "beep", "reboot-for-bitlocker",
522
                                    "reboot-on-error", "secure-boot-enroll", "secure-boot-enroll-action",
523
                                    "secure-boot-enroll-timeout-sec", "console-mode", "log-level"))
524
                        r = 0; /* we don't parse these in userspace, but they are OK */
×
525
                else {
526
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Unknown line '%s', ignoring.", field);
×
527
                        continue;
×
528
                }
529
                if (r < 0)
×
530
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while parsing: %m");
×
531
        }
532

533
        return 1;
31✔
534
}
535

536
static int boot_loader_read_conf_path(BootConfig *config, const char *root, const char *path) {
33✔
537
        _cleanup_free_ char *full = NULL;
33✔
538
        _cleanup_fclose_ FILE *f = NULL;
33✔
539
        int r;
33✔
540

541
        assert(config);
33✔
542
        assert(path);
33✔
543

544
        r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, "re", &full, &f);
33✔
545
        config->loader_conf_status = r < 0 ? r : true;
33✔
546
        if (r == -ENOENT)
33✔
547
                return 0;
548
        if (r < 0)
31✔
549
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(path));
×
550

551
        return boot_loader_read_conf(config, f, full);
31✔
552
}
553

554
static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
42✔
555
        int r;
42✔
556

557
        assert(a);
42✔
558
        assert(b);
42✔
559

560
        /* This mimics a function of the same name in src/boot/efi/sd-boot.c */
561

562
        r = CMP(a->tries_left == 0, b->tries_left == 0);
42✔
563
        if (r != 0)
42✔
564
                return r;
×
565

566
        r = CMP(!a->sort_key, !b->sort_key);
42✔
567
        if (r != 0)
39✔
568
                return r;
3✔
569

570
        if (a->sort_key && b->sort_key) {
39✔
571
                r = strcmp(a->sort_key, b->sort_key);
36✔
572
                if (r != 0)
36✔
573
                        return r;
574

575
                r = strcmp_ptr(a->machine_id, b->machine_id);
35✔
576
                if (r != 0)
35✔
577
                        return r;
578

579
                r = -strverscmp_improved(a->version, b->version);
35✔
580
                if (r != 0)
35✔
581
                        return r;
582
        }
583

584
        r = -strverscmp_improved(a->id_without_profile ?: a->id, b->id_without_profile ?: b->id);
37✔
585
        if (r != 0)
37✔
586
                return r;
587

588
        if (a->id_without_profile && b->id_without_profile) {
34✔
589
                /* The strverscmp_improved() call above already established that we are talking about the
590
                 * same image here, hence order by profile, if there is one */
591
                r = CMP(a->profile, b->profile);
34✔
592
                if (r != 0)
×
593
                        return r;
34✔
594
        }
595

596
        if (a->tries_left != UINT_MAX || b->tries_left != UINT_MAX)
×
597
                return 0;
598

599
        r = -CMP(a->tries_left, b->tries_left);
×
600
        if (r != 0)
×
601
                return r;
×
602

603
        return CMP(a->tries_done, b->tries_done);
×
604
}
605

606
static int config_check_inode_relevant_and_unseen(BootConfig *config, int fd, const char *fname) {
42✔
607
        _cleanup_free_ char *d = NULL;
42✔
608
        struct stat st;
42✔
609

610
        assert(config);
42✔
611
        assert(fd >= 0);
42✔
612
        assert(fname);
42✔
613

614
        /* So, here's the thing: because of the mess around /efi/ vs. /boot/ vs. /boot/efi/ it might be that
615
         * people have these dirs, or subdirs of them symlinked or bind mounted, and we might end up
616
         * iterating though some dirs multiple times. Let's thus rather be safe than sorry, and track the
617
         * inodes we already processed: let's ignore inodes we have seen already. This should be robust
618
         * against any form of symlinking or bind mounting, and effectively suppress any such duplicates. */
619

620
        if (fstat(fd, &st) < 0)
42✔
621
                return log_error_errno(errno, "Failed to stat('%s'): %m", fname);
×
622
        if (!S_ISREG(st.st_mode)) {
42✔
623
                log_debug("File '%s' is not a regular file, ignoring.", fname);
×
624
                return false;
×
625
        }
626

627
        if (set_contains(config->inodes_seen, &st)) {
42✔
628
                log_debug("Inode '%s' already seen before, ignoring.", fname);
×
629
                return false;
×
630
        }
631

632
        d = memdup(&st, sizeof(st));
42✔
633
        if (!d)
42✔
634
                return log_oom();
×
635

636
        if (set_ensure_consume(&config->inodes_seen, &inode_hash_ops, TAKE_PTR(d)) < 0)
42✔
637
                return log_oom();
×
638

639
        return true;
640
}
641

642
static int boot_entries_find_type1(
47✔
643
                BootConfig *config,
644
                const char *root,
645
                const BootEntrySource source,
646
                const char *dir) {
647

648
        _cleanup_free_ DirectoryEntries *dentries = NULL;
94✔
649
        _cleanup_free_ char *full = NULL;
47✔
650
        _cleanup_close_ int dir_fd = -EBADF;
47✔
651
        int r;
47✔
652

653
        assert(config);
47✔
654
        assert(root);
47✔
655
        assert(dir);
47✔
656

657
        dir_fd = chase_and_open(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, O_DIRECTORY|O_CLOEXEC, &full);
47✔
658
        if (dir_fd == -ENOENT)
47✔
659
                return 0;
660
        if (dir_fd < 0)
33✔
661
                return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, skip_leading_slash(dir));
×
662

663
        r = readdir_all(dir_fd, RECURSE_DIR_IGNORE_DOT, &dentries);
33✔
664
        if (r < 0)
33✔
665
                return log_error_errno(r, "Failed to read directory '%s': %m", full);
×
666

667
        FOREACH_ARRAY(i, dentries->entries, dentries->n_entries) {
41✔
668
                const struct dirent *de = *i;
8✔
669
                _cleanup_fclose_ FILE *f = NULL;
8✔
670

671
                if (!dirent_is_file(de))
8✔
672
                        continue;
×
673

674
                if (!endswith_no_case(de->d_name, ".conf"))
8✔
675
                        continue;
×
676

677
                r = xfopenat(dir_fd, de->d_name, "re", O_NOFOLLOW|O_NOCTTY, &f);
8✔
678
                if (r < 0) {
8✔
679
                        log_warning_errno(r, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
680
                        continue;
×
681
                }
682

683
                r = config_check_inode_relevant_and_unseen(config, fileno(f), de->d_name);
8✔
684
                if (r < 0)
8✔
685
                        return r;
686
                if (r == 0) /* inode already seen or otherwise not relevant */
8✔
687
                        continue;
×
688

689
                r = boot_config_load_type1(config, f, root, source, full, de->d_name);
8✔
690
                if (r == -ENOMEM) /* ignore all other errors */
8✔
691
                        return log_oom();
×
692
        }
693

694
        return 0;
695
}
696

697
static int boot_entry_load_unified(
51✔
698
                const char *root,
699
                const BootEntrySource source,
700
                const char *path,
701
                unsigned profile,
702
                const char *osrelease_text,
703
                const char *profile_text,
704
                const char *cmdline_text,
705
                BootEntry *ret) {
706

707
        _cleanup_free_ char *fname = NULL, *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL,
×
708
                *os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
51✔
709
        const char *k, *good_name, *good_version, *good_sort_key;
51✔
710
        _cleanup_fclose_ FILE *f = NULL;
51✔
711
        int r;
51✔
712

713
        assert(root);
51✔
714
        assert(path);
51✔
715
        assert(osrelease_text);
51✔
716
        assert(ret);
51✔
717

718
        k = path_startswith(path, root);
51✔
719
        if (!k)
51✔
720
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
×
721

722
        f = fmemopen_unlocked((void*) osrelease_text, strlen(osrelease_text), "r");
51✔
723
        if (!f)
51✔
724
                return log_oom();
×
725

726
        r = parse_env_file(f, "os-release",
51✔
727
                           "PRETTY_NAME", &os_pretty_name,
728
                           "IMAGE_ID", &os_image_id,
729
                           "NAME", &os_name,
730
                           "ID", &os_id,
731
                           "IMAGE_VERSION", &os_image_version,
732
                           "VERSION", &os_version,
733
                           "VERSION_ID", &os_version_id,
734
                           "BUILD_ID", &os_build_id);
735
        if (r < 0)
51✔
736
                return log_error_errno(r, "Failed to parse os-release data from unified kernel image %s: %m", path);
×
737

738
        if (!bootspec_pick_name_version_sort_key(
51✔
739
                            os_pretty_name,
740
                            os_image_id,
741
                            os_name,
742
                            os_id,
743
                            os_image_version,
744
                            os_version,
745
                            os_version_id,
746
                            os_build_id,
747
                            &good_name,
748
                            &good_version,
749
                            &good_sort_key))
750
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Missing fields in os-release data from unified kernel image %s, refusing.", path);
×
751

752
        _cleanup_free_ char *profile_id = NULL, *profile_title = NULL;
51✔
753
        if (profile_text) {
51✔
754
                fclose(f);
51✔
755

756
                f = fmemopen_unlocked((void*) profile_text, strlen(profile_text), "r");
51✔
757
                if (!f)
51✔
758
                        return log_oom();
×
759

760
                r = parse_env_file(
51✔
761
                                f, "profile",
762
                                "ID", &profile_id,
763
                                "TITLE", &profile_title);
764
                if (r < 0)
51✔
765
                        return log_error_errno(r, "Failed to parse profile data from unified kernel image '%s': %m", path);
×
766
        }
767

768
        r = path_extract_filename(path, &fname);
51✔
769
        if (r < 0)
51✔
770
                return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
×
771

772
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_TYPE2, source);
51✔
773

774
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
51✔
775
        if (r < 0)
51✔
776
                return r;
777

778
        if (!efi_loader_entry_name_valid(tmp.id))
51✔
779
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id);
×
780

781
        tmp.profile = profile;
51✔
782

783
        if (profile_id || profile > 0) {
51✔
784
                tmp.id_without_profile = TAKE_PTR(tmp.id);
51✔
785

786
                if (profile_id)
51✔
787
                        tmp.id = strjoin(tmp.id_without_profile, "@", profile_id);
51✔
788
                else
789
                        (void) asprintf(&tmp.id, "%s@%u", tmp.id_without_profile, profile);
×
790
                if (!tmp.id)
51✔
791
                        return log_oom();
×
792
        }
793

794
        if (os_id && os_version_id) {
51✔
795
                tmp.id_old = strjoin(os_id, "-", os_version_id);
×
796
                if (!tmp.id_old)
×
797
                        return log_oom();
×
798
        }
799

800
        tmp.path = strdup(path);
51✔
801
        if (!tmp.path)
51✔
802
                return log_oom();
×
803

804
        tmp.root = strdup(root);
51✔
805
        if (!tmp.root)
51✔
806
                return log_oom();
×
807

808
        tmp.kernel = path_make_absolute(k, "/");
51✔
809
        if (!tmp.kernel)
51✔
810
                return log_oom();
×
811

812
        tmp.options = strv_new(cmdline_text);
51✔
813
        if (!tmp.options)
51✔
814
                return log_oom();
×
815

816
        if (profile_title)
51✔
817
                tmp.title = strjoin(good_name, " (", profile_title, ")");
34✔
818
        else if (profile_id)
17✔
819
                tmp.title = strjoin(good_name, " (", profile_id, ")");
17✔
820
        else if (profile > 0)
×
821
                (void) asprintf(&tmp.title, "%s (@%u)", good_name, profile);
×
822
        else
823
                tmp.title = strdup(good_name);
×
824
        if (!tmp.title)
51✔
825
                return log_oom();
×
826

827
        if (good_sort_key) {
51✔
828
                tmp.sort_key = strdup(good_sort_key);
51✔
829
                if (!tmp.sort_key)
51✔
830
                        return log_oom();
×
831
        }
832

833
        if (good_version) {
51✔
834
                tmp.version = strdup(good_version);
51✔
835
                if (!tmp.version)
51✔
836
                        return log_oom();
×
837
        }
838

839
        *ret = TAKE_STRUCT(tmp);
51✔
840
        return 0;
51✔
841
}
842

843
static int pe_load_headers_and_sections(
85✔
844
                int fd,
845
                const char *path,
846
                IMAGE_SECTION_HEADER **ret_sections,
847
                PeHeader **ret_pe_header) {
848

849
        _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
170✔
850
        _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
×
851
        _cleanup_free_ PeHeader *pe_header = NULL;
85✔
852
        int r;
85✔
853

854
        assert(fd >= 0);
85✔
855
        assert(path);
85✔
856

857
        r = pe_load_headers(fd, &dos_header, &pe_header);
85✔
858
        if (r < 0)
85✔
859
                return log_error_errno(r, "Failed to parse PE file '%s': %m", path);
×
860

861
        r = pe_load_sections(fd, dos_header, pe_header, &sections);
85✔
862
        if (r < 0)
85✔
863
                return log_error_errno(r, "Failed to parse PE sections of '%s': %m", path);
×
864

865
        if (ret_pe_header)
85✔
866
                *ret_pe_header = TAKE_PTR(pe_header);
85✔
867
        if (ret_sections)
85✔
868
                *ret_sections = TAKE_PTR(sections);
85✔
869

870
        return 0;
871
}
872

873
static const IMAGE_SECTION_HEADER* pe_find_profile_section_table(
119✔
874
                const PeHeader *pe_header,
875
                const IMAGE_SECTION_HEADER *sections,
876
                unsigned profile,
877
                size_t *ret_n_sections) {
878

879
        assert(pe_header);
119✔
880

881
        /* Looks for the part of the section table that defines the specified profile. If 'profile' is
882
         * specified as UINT_MAX this will look for the base profile. */
883

884
        if (le16toh(pe_header->pe.NumberOfSections) == 0)
119✔
885
                return NULL;
886

887
        assert(sections);
119✔
888

889
        const IMAGE_SECTION_HEADER
119✔
890
                *p = sections,
119✔
891
                *e = sections + le16toh(pe_header->pe.NumberOfSections),
119✔
892
                *start = profile == UINT_MAX ? sections : NULL,
119✔
893
                *end;
894
        unsigned current_profile = UINT_MAX;
895

896
        for (;;) {
425✔
897
                p = pe_section_table_find(p, e - p, ".profile");
272✔
898
                if (!p) {
272✔
899
                        end = e;
900
                        break;
901
                }
902
                if (current_profile == profile) {
238✔
903
                        end = p;
904
                        break;
905
                }
906

907
                if (current_profile == UINT_MAX)
153✔
908
                        current_profile = 0;
909
                else
910
                        current_profile++;
85✔
911

912
                if (current_profile == profile)
153✔
913
                        start = p;
51✔
914

915
                p++; /* Continue scanning after the .profile entry we just found */
153✔
916
        }
917

918
        if (!start)
119✔
919
                return NULL;
920

921
        if (ret_n_sections)
102✔
922
                *ret_n_sections = end - start;
102✔
923

924
        return start;
925
}
926

927
static int trim_cmdline(char **cmdline) {
68✔
928
        assert(cmdline);
68✔
929

930
        /* Strips leading and trailing whitespace from command line */
931

932
        if (!*cmdline)
68✔
933
                return 0;
934

935
        const char *skipped = skip_leading_chars(*cmdline, WHITESPACE);
68✔
936

937
        if (isempty(skipped)) {
68✔
938
                *cmdline = mfree(*cmdline);
×
939
                return 0;
×
940
        }
941

942
        if (skipped != *cmdline) {
68✔
943
                _cleanup_free_ char *c = strdup(skipped);
×
944
                if (!c)
×
945
                        return -ENOMEM;
×
946

947
                free_and_replace(*cmdline, c);
×
948
        }
949

950
        delete_trailing_chars(*cmdline, WHITESPACE);
68✔
951
        return 1;
68✔
952
}
953

954
/* Maximum PE section we are willing to load (Note that sections we are not interested in may be larger, but
955
 * the ones we do care about and we are willing to load into memory have this size limit.) */
956
#define PE_SECTION_SIZE_MAX (4U*1024U*1024U)
957

958
static int pe_find_uki_sections(
68✔
959
                int fd,
960
                const char *path,
961
                unsigned profile,
962
                char **ret_osrelease,
963
                char **ret_profile,
964
                char **ret_cmdline) {
965

966
        _cleanup_free_ char *osrelease_text = NULL, *profile_text = NULL, *cmdline_text = NULL;
×
967
        _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
×
968
        _cleanup_free_ PeHeader *pe_header = NULL;
68✔
969
        int r;
68✔
970

971
        assert(fd >= 0);
68✔
972
        assert(path);
68✔
973
        assert(profile != UINT_MAX);
68✔
974
        assert(ret_osrelease);
68✔
975
        assert(ret_profile);
68✔
976
        assert(ret_cmdline);
68✔
977

978
        r = pe_load_headers_and_sections(fd, path, &sections, &pe_header);
68✔
979
        if (r < 0)
68✔
980
                return r;
981

982
        if (!pe_is_uki(pe_header, sections))
68✔
983
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parsed PE file '%s' is not a UKI.", path);
×
984

985
        if (!pe_is_native(pe_header)) /* Don't process non-native UKIs */
68✔
986
                goto nothing;
×
987

988
        /* Find part of the section table for this profile */
989
        size_t n_psections = 0;
68✔
990
        const IMAGE_SECTION_HEADER *psections = pe_find_profile_section_table(pe_header, sections, profile, &n_psections);
68✔
991
        if (!psections && profile != 0) /* Profile not found? (Profile @0 needs no explicit .profile!) */
68✔
992
                goto nothing;
17✔
993

994
        /* Find base profile part of section table */
995
        size_t n_bsections;
51✔
996
        const IMAGE_SECTION_HEADER *bsections = ASSERT_PTR(pe_find_profile_section_table(pe_header, sections, UINT_MAX, &n_bsections));
51✔
997

998
        struct {
51✔
999
                const char *name;
1000
                char **data;
1001
        } table[] = {
51✔
1002
                { ".osrel",   &osrelease_text },
1003
                { ".profile", &profile_text   },
1004
                { ".cmdline", &cmdline_text   },
1005
        };
1006

1007
        FOREACH_ELEMENT(t, table) {
204✔
1008
                const IMAGE_SECTION_HEADER *found;
153✔
1009

1010
                /* First look in the profile part of the section table, and if we don't find anything there, look into the base part */
1011
                found = pe_section_table_find(psections, n_psections, t->name);
153✔
1012
                if (!found) {
153✔
1013
                        found = pe_section_table_find(bsections, n_bsections, t->name);
68✔
1014
                        if (!found)
68✔
1015
                                continue;
×
1016
                }
1017

1018
                /* Permit "masking" of sections in the base profile */
1019
                if (found->VirtualSize == 0)
153✔
1020
                        continue;
×
1021

1022
                r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) t->data, /* ret_size= */ NULL);
153✔
1023
                if (r < 0)
153✔
1024
                        return log_error_errno(r, "Failed to load contents of section '%s': %m", t->name);
×
1025
        }
1026

1027
        if (!osrelease_text)
51✔
1028
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unified kernel image lacks .osrel data for profile @%u, refusing.", profile);
×
1029

1030
        if (trim_cmdline(&cmdline_text) < 0)
51✔
1031
                return log_oom();
×
1032

1033
        *ret_osrelease = TAKE_PTR(osrelease_text);
51✔
1034
        *ret_profile = TAKE_PTR(profile_text);
51✔
1035
        *ret_cmdline = TAKE_PTR(cmdline_text);
51✔
1036
        return 1;
51✔
1037

1038
nothing:
17✔
1039
        *ret_osrelease = *ret_profile = *ret_cmdline = NULL;
17✔
1040
        return 0;
17✔
1041
}
1042

1043
static int pe_find_addon_sections(
17✔
1044
                int fd,
1045
                const char *path,
1046
                char **ret_cmdline) {
1047

1048
        _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
34✔
1049
        _cleanup_free_ PeHeader *pe_header = NULL;
17✔
1050
        int r;
17✔
1051

1052
        assert(fd >= 0);
17✔
1053
        assert(path);
17✔
1054

1055
        r = pe_load_headers_and_sections(fd, path, &sections, &pe_header);
17✔
1056
        if (r < 0)
17✔
1057
                return r;
1058

1059
        if (!pe_is_addon(pe_header, sections))
17✔
1060
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parse PE file '%s' is not an add-on.", path);
×
1061

1062
        /* Define early, before the gotos below */
1063
        _cleanup_free_ char *cmdline_text = NULL;
17✔
1064

1065
        if (!pe_is_native(pe_header))
17✔
1066
                goto nothing;
×
1067

1068
        const IMAGE_SECTION_HEADER *found = pe_section_table_find(sections, le16toh(pe_header->pe.NumberOfSections), ".cmdline");
17✔
1069
        if (!found)
17✔
1070
                goto nothing;
×
1071

1072
        r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) &cmdline_text, /* ret_size= */ NULL);
17✔
1073
        if (r < 0)
17✔
1074
                return log_error_errno(r, "Failed to load contents of section '.cmdline': %m");
×
1075

1076
        if (trim_cmdline(&cmdline_text) < 0)
17✔
1077
                return log_oom();
×
1078

1079
        *ret_cmdline = TAKE_PTR(cmdline_text);
17✔
1080
        return 1;
17✔
1081

1082
nothing:
×
1083
        *ret_cmdline = NULL;
×
1084
        return 0;
×
1085
}
1086

1087
static int insert_boot_entry_addon(
17✔
1088
                BootEntryAddons *addons,
1089
                char *location,
1090
                char *cmdline) {
1091

1092
        assert(addons);
17✔
1093

1094
        if (!GREEDY_REALLOC(addons->items, addons->n_items + 1))
17✔
1095
                return log_oom();
×
1096

1097
        addons->items[addons->n_items++] = (BootEntryAddon) {
17✔
1098
                .location = location,
1099
                .cmdline = cmdline,
1100
        };
1101

1102
        return 0;
17✔
1103
}
1104

1105
static int boot_entries_find_unified_addons(
98✔
1106
                BootConfig *config,
1107
                int d_fd,
1108
                const char *addon_dir,
1109
                const char *root,
1110
                BootEntryAddons *ret_addons) {
1111

1112
        _cleanup_closedir_ DIR *d = NULL;
98✔
1113
        _cleanup_free_ char *full = NULL;
98✔
1114
        _cleanup_(boot_entry_addons_done) BootEntryAddons addons = {};
98✔
1115
        int r;
98✔
1116

1117
        assert(ret_addons);
98✔
1118
        assert(config);
98✔
1119

1120
        r = chase_and_opendirat(d_fd, addon_dir, CHASE_AT_RESOLVE_IN_ROOT, &full, &d);
98✔
1121
        if (r == -ENOENT)
98✔
1122
                return 0;
1123
        if (r < 0)
17✔
1124
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(addon_dir));
×
1125

1126
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) {
68✔
1127
                _cleanup_free_ char *j = NULL, *cmdline = NULL, *location = NULL;
17✔
1128
                _cleanup_close_ int fd = -EBADF;
17✔
1129

1130
                if (!dirent_is_file(de))
17✔
1131
                        continue;
×
1132

1133
                if (!endswith_no_case(de->d_name, ".addon.efi"))
17✔
1134
                        continue;
×
1135

1136
                fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY);
17✔
1137
                if (fd < 0) {
17✔
1138
                        log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
1139
                        continue;
×
1140
                }
1141

1142
                r = config_check_inode_relevant_and_unseen(config, fd, de->d_name);
17✔
1143
                if (r < 0)
17✔
1144
                        return r;
1145
                if (r == 0) /* inode already seen or otherwise not relevant */
17✔
1146
                        continue;
×
1147

1148
                j = path_join(full, de->d_name);
17✔
1149
                if (!j)
17✔
1150
                        return log_oom();
×
1151

1152
                if (pe_find_addon_sections(fd, j, &cmdline) <= 0)
17✔
1153
                        continue;
×
1154

1155
                location = strdup(j);
17✔
1156
                if (!location)
17✔
1157
                        return log_oom();
×
1158

1159
                r = insert_boot_entry_addon(&addons, location, cmdline);
17✔
1160
                if (r < 0)
17✔
1161
                        return r;
1162

1163
                TAKE_PTR(location);
17✔
1164
                TAKE_PTR(cmdline);
17✔
1165
        }
1166

1167
        *ret_addons = TAKE_STRUCT(addons);
17✔
1168
        return 0;
17✔
1169
}
1170

1171
static int boot_entries_find_unified_global_addons(
47✔
1172
                BootConfig *config,
1173
                const char *root,
1174
                const char *d_name,
1175
                BootEntryAddons *ret_addons) {
1176

1177
        int r;
47✔
1178
        _cleanup_closedir_ DIR *d = NULL;
47✔
1179

1180
        assert(ret_addons);
47✔
1181

1182
        r = chase_and_opendir(root, NULL, CHASE_PROHIBIT_SYMLINKS, NULL, &d);
47✔
1183
        if (r == -ENOENT)
47✔
1184
                return 0;
1185
        if (r < 0)
47✔
1186
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(d_name));
×
1187

1188
        return boot_entries_find_unified_addons(config, dirfd(d), d_name, root, ret_addons);
47✔
1189
}
1190

1191
static int boot_entries_find_unified_local_addons(
51✔
1192
                BootConfig *config,
1193
                int d_fd,
1194
                const char *d_name,
1195
                const char *root,
1196
                BootEntry *ret) {
1197

1198
        _cleanup_free_ char *addon_dir = NULL;
51✔
1199

1200
        assert(ret);
51✔
1201

1202
        addon_dir = strjoin(d_name, ".extra.d");
51✔
1203
        if (!addon_dir)
51✔
1204
                return log_oom();
×
1205

1206
        return boot_entries_find_unified_addons(config, d_fd, addon_dir, root, &ret->local_addons);
51✔
1207
}
1208

1209
static int boot_entries_find_unified(
47✔
1210
                BootConfig *config,
1211
                const char *root,
1212
                BootEntrySource source,
1213
                const char *dir) {
1214

1215
        _cleanup_closedir_ DIR *d = NULL;
47✔
1216
        _cleanup_free_ char *full = NULL;
47✔
1217
        int r;
47✔
1218

1219
        assert(config);
47✔
1220
        assert(dir);
47✔
1221

1222
        r = chase_and_opendir(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &full, &d);
47✔
1223
        if (r == -ENOENT)
47✔
1224
                return 0;
1225
        if (r < 0)
31✔
1226
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, skip_leading_slash(dir));
×
1227

1228
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) {
110✔
1229
                if (!dirent_is_file(de))
17✔
1230
                        continue;
×
1231

1232
                if (!endswith_no_case(de->d_name, ".efi"))
17✔
1233
                        continue;
×
1234

1235
                _cleanup_close_ int fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY);
143✔
1236
                if (fd < 0) {
17✔
1237
                        log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
1238
                        continue;
×
1239
                }
1240

1241
                r = config_check_inode_relevant_and_unseen(config, fd, de->d_name);
17✔
1242
                if (r < 0)
17✔
1243
                        return r;
1244
                if (r == 0) /* inode already seen or otherwise not relevant */
17✔
1245
                        continue;
×
1246

1247
                _cleanup_free_ char *j = path_join(full, de->d_name);
34✔
1248
                if (!j)
17✔
1249
                        return log_oom();
×
1250

1251
                for (unsigned p = 0; p < UNIFIED_PROFILES_MAX; p++) {
68✔
1252
                        _cleanup_free_ char *osrelease = NULL, *profile = NULL, *cmdline = NULL;
68✔
1253

1254
                        r = pe_find_uki_sections(fd, j, p, &osrelease, &profile, &cmdline);
68✔
1255
                        if (r == 0) /* this profile does not exist, we are done */
68✔
1256
                                break;
1257
                        if (r < 0)
51✔
1258
                                continue;
×
1259

1260
                        if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
51✔
1261
                                return log_oom();
×
1262

1263
                        BootEntry *entry = config->entries + config->n_entries;
51✔
1264

1265
                        if (boot_entry_load_unified(root, source, j, p, osrelease, profile, cmdline, entry) < 0)
51✔
1266
                                continue;
×
1267

1268
                        /* look for .efi.extra.d */
1269
                        (void) boot_entries_find_unified_local_addons(config, dirfd(d), de->d_name, full, entry);
51✔
1270

1271
                        /* Set up the backpointer, so that we can find the global addons */
1272
                        entry->global_addons = &config->global_addons[source];
51✔
1273

1274
                        config->n_entries++;
51✔
1275
                }
1276
        }
1277

1278
        return 0;
1279
}
1280

1281
static bool find_nonunique(const BootEntry *entries, size_t n_entries, bool arr[]) {
56✔
1282
        bool non_unique = false;
56✔
1283

1284
        assert(entries || n_entries == 0);
56✔
1285
        assert(arr || n_entries == 0);
56✔
1286

1287
        for (size_t i = 0; i < n_entries; i++)
227✔
1288
                arr[i] = false;
171✔
1289

1290
        for (size_t i = 0; i < n_entries; i++)
227✔
1291
                for (size_t j = 0; j < n_entries; j++)
714✔
1292
                        if (i != j && streq(boot_entry_title(entries + i),
543✔
1293
                                            boot_entry_title(entries + j)))
1294
                                non_unique = arr[i] = arr[j] = true;
112✔
1295

1296
        return non_unique;
56✔
1297
}
1298

1299
static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
33✔
1300
        _cleanup_free_ bool *arr = NULL;
33✔
1301
        char *s;
33✔
1302

1303
        assert(entries || n_entries == 0);
33✔
1304

1305
        if (n_entries == 0)
33✔
1306
                return 0;
1307

1308
        arr = new(bool, n_entries);
19✔
1309
        if (!arr)
19✔
1310
                return -ENOMEM;
1311

1312
        /* Find _all_ non-unique titles */
1313
        if (!find_nonunique(entries, n_entries, arr))
19✔
1314
                return 0;
1315

1316
        /* Add version to non-unique titles */
1317
        for (size_t i = 0; i < n_entries; i++)
78✔
1318
                if (arr[i] && entries[i].version) {
59✔
1319
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version) < 0)
40✔
1320
                                return -ENOMEM;
1321

1322
                        free_and_replace(entries[i].show_title, s);
40✔
1323
                }
1324

1325
        if (!find_nonunique(entries, n_entries, arr))
19✔
1326
                return 0;
1327

1328
        /* Add machine-id to non-unique titles */
1329
        for (size_t i = 0; i < n_entries; i++)
71✔
1330
                if (arr[i] && entries[i].machine_id) {
53✔
1331
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id) < 0)
2✔
1332
                                return -ENOMEM;
1333

1334
                        free_and_replace(entries[i].show_title, s);
2✔
1335
                }
1336

1337
        if (!find_nonunique(entries, n_entries, arr))
18✔
1338
                return 0;
1339

1340
        /* Add file name to non-unique titles */
1341
        for (size_t i = 0; i < n_entries; i++)
71✔
1342
                if (arr[i]) {
53✔
1343
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id) < 0)
36✔
1344
                                return -ENOMEM;
1345

1346
                        free_and_replace(entries[i].show_title, s);
36✔
1347
                }
1348

1349
        return 0;
1350
}
1351

1352
static int boot_config_find(const BootConfig *config, const char *id) {
20✔
1353
        assert(config);
20✔
1354

1355
        if (!id)
20✔
1356
                return -1;
1357

1358
        if (id[0] == '@') {
20✔
1359
                if (!strcaseeq(id, "@saved"))
×
1360
                        return -1;
1361
                if (!config->entry_selected)
×
1362
                        return -1;
1363
                id = config->entry_selected;
1364
        }
1365

1366
        for (size_t i = 0; i < config->n_entries; i++)
70✔
1367
                if (fnmatch(id, config->entries[i].id, FNM_CASEFOLD) == 0)
70✔
1368
                        return i;
20✔
1369

1370
        return -1;
1371
}
1372

1373
static int boot_entries_select_default(const BootConfig *config) {
31✔
1374
        int i;
31✔
1375

1376
        assert(config);
31✔
1377
        assert(config->entries || config->n_entries == 0);
31✔
1378

1379
        if (config->n_entries == 0) {
31✔
1380
                log_debug("Found no default boot entry :(");
14✔
1381
                return -1; /* -1 means "no default" */
14✔
1382
        }
1383

1384
        if (config->entry_oneshot) {
17✔
1385
                i = boot_config_find(config, config->entry_oneshot);
×
1386
                if (i >= 0) {
×
1387
                        log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
×
1388
                                  config->entries[i].id);
1389
                        return i;
×
1390
                }
1391
        }
1392

1393
        if (config->entry_default) {
17✔
1394
                i = boot_config_find(config, config->entry_default);
3✔
1395
                if (i >= 0) {
3✔
1396
                        log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
3✔
1397
                                  config->entries[i].id);
1398
                        return i;
3✔
1399
                }
1400
        }
1401

1402
        if (config->default_pattern) {
14✔
1403
                i = boot_config_find(config, config->default_pattern);
×
1404
                if (i >= 0) {
×
1405
                        log_debug("Found default: id \"%s\" is matched by pattern \"%s\"",
×
1406
                                  config->entries[i].id, config->default_pattern);
1407
                        return i;
×
1408
                }
1409
        }
1410

1411
        log_debug("Found default: first entry \"%s\"", config->entries[0].id);
14✔
1412
        return 0;
1413
}
1414

1415
static int boot_entries_select_selected(const BootConfig *config) {
31✔
1416
        assert(config);
31✔
1417
        assert(config->entries || config->n_entries == 0);
31✔
1418

1419
        if (!config->entry_selected || config->n_entries == 0)
31✔
1420
                return -1;
1421

1422
        return boot_config_find(config, config->entry_selected);
17✔
1423
}
1424

1425
static int boot_load_efi_entry_pointers(BootConfig *config, bool skip_efivars) {
31✔
1426
        int r;
31✔
1427

1428
        assert(config);
31✔
1429

1430
        if (skip_efivars || !is_efi_boot())
31✔
1431
                return 0;
14✔
1432

1433
        /* Loads the three "pointers" to boot loader entries from their EFI variables */
1434

1435
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), &config->entry_oneshot);
17✔
1436
        if (r == -ENOMEM)
17✔
1437
                return log_oom();
×
1438
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
17✔
1439
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\", ignoring: %m");
×
1440

1441
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"), &config->entry_default);
17✔
1442
        if (r == -ENOMEM)
17✔
1443
                return log_oom();
×
1444
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
17✔
1445
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\", ignoring: %m");
×
1446

1447
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntrySelected"), &config->entry_selected);
17✔
1448
        if (r == -ENOMEM)
17✔
1449
                return log_oom();
×
1450
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
17✔
1451
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySelected\", ignoring: %m");
×
1452

1453
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"), &config->entry_sysfail);
17✔
1454
        if (r == -ENOMEM)
17✔
1455
                return log_oom();
×
1456
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
17✔
1457
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySysFail\", ignoring: %m");
×
1458

1459
        return 1;
1460
}
1461

1462
int boot_config_select_special_entries(BootConfig *config, bool skip_efivars) {
31✔
1463
        int r;
31✔
1464

1465
        assert(config);
31✔
1466

1467
        r = boot_load_efi_entry_pointers(config, skip_efivars);
31✔
1468
        if (r < 0)
31✔
1469
                return r;
1470

1471
        config->default_entry = boot_entries_select_default(config);
31✔
1472
        config->selected_entry = boot_entries_select_selected(config);
31✔
1473

1474
        return 0;
31✔
1475
}
1476

1477
int boot_config_finalize(BootConfig *config) {
33✔
1478
        int r;
33✔
1479

1480
        typesafe_qsort(config->entries, config->n_entries, boot_entry_compare);
33✔
1481

1482
        r = boot_entries_uniquify(config->entries, config->n_entries);
33✔
1483
        if (r < 0)
33✔
1484
                return log_error_errno(r, "Failed to uniquify boot entries: %m");
×
1485

1486
        return 0;
1487
}
1488

1489
int boot_config_load(
33✔
1490
                BootConfig *config,
1491
                const char *esp_path,
1492
                const char *xbootldr_path) {
1493

1494
        int r;
33✔
1495

1496
        assert(config);
33✔
1497

1498
        if (esp_path) {
33✔
1499
                r = boot_loader_read_conf_path(config, esp_path, "/loader/loader.conf");
33✔
1500
                if (r < 0)
33✔
1501
                        return r;
1502

1503
                r = boot_entries_find_type1(config, esp_path, BOOT_ENTRY_ESP, "/loader/entries");
33✔
1504
                if (r < 0)
33✔
1505
                        return r;
1506

1507
                r = boot_entries_find_unified(config, esp_path, BOOT_ENTRY_ESP, "/EFI/Linux/");
33✔
1508
                if (r < 0)
33✔
1509
                        return r;
1510

1511
                r = boot_entries_find_unified_global_addons(config, esp_path, "/loader/addons/",
33✔
1512
                                                            &config->global_addons[BOOT_ENTRY_ESP]);
1513
                if (r < 0)
33✔
1514
                        return r;
1515
        }
1516

1517
        if (xbootldr_path) {
33✔
1518
                r = boot_entries_find_type1(config, xbootldr_path, BOOT_ENTRY_XBOOTLDR, "/loader/entries");
14✔
1519
                if (r < 0)
14✔
1520
                        return r;
1521

1522
                r = boot_entries_find_unified(config, xbootldr_path, BOOT_ENTRY_XBOOTLDR, "/EFI/Linux/");
14✔
1523
                if (r < 0)
14✔
1524
                        return r;
1525

1526
                r = boot_entries_find_unified_global_addons(config, xbootldr_path, "/loader/addons/",
14✔
1527
                                                            &config->global_addons[BOOT_ENTRY_XBOOTLDR]);
1528
                if (r < 0)
14✔
1529
                        return r;
1530
        }
1531

1532
        return boot_config_finalize(config);
33✔
1533
}
1534

1535
int boot_config_load_auto(
×
1536
                BootConfig *config,
1537
                const char *override_esp_path,
1538
                const char *override_xbootldr_path) {
1539

1540
        _cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL;
×
1541
        dev_t esp_devid = 0, xbootldr_devid = 0;
×
1542
        int r;
×
1543

1544
        assert(config);
×
1545

1546
        /* This function is similar to boot_entries_load_config(), however we automatically search for the
1547
         * ESP and the XBOOTLDR partition unless it is explicitly specified. Also, if the user did not pass
1548
         * an ESP or XBOOTLDR path directly, let's see if /run/boot-loader-entries/ exists. If so, let's
1549
         * read data from there, as if it was an ESP (i.e. loading both entries and loader.conf data from
1550
         * it). This allows other boot loaders to pass boot loader entry information to our tools if they
1551
         * want to. */
1552

1553
        if (!override_esp_path && !override_xbootldr_path) {
×
1554
                if (access("/run/boot-loader-entries/", F_OK) >= 0)
×
1555
                        return boot_config_load(config, "/run/boot-loader-entries/", NULL);
×
1556

1557
                if (errno != ENOENT)
×
1558
                        return log_error_errno(errno,
×
1559
                                               "Failed to determine whether /run/boot-loader-entries/ exists: %m");
1560
        }
1561

1562
        r = find_esp_and_warn(NULL, override_esp_path, /* unprivileged_mode= */ false, &esp_where, NULL, NULL, NULL, NULL, &esp_devid);
×
1563
        if (r < 0) /* we don't log about ENOKEY here, but propagate it, leaving it to the caller to log */
×
1564
                return r;
1565

1566
        r = find_xbootldr_and_warn(NULL, override_xbootldr_path, /* unprivileged_mode= */ false, &xbootldr_where, NULL, &xbootldr_devid);
×
1567
        if (r < 0 && r != -ENOKEY)
×
1568
                return r; /* It's fine if the XBOOTLDR partition doesn't exist, hence we ignore ENOKEY here */
1569

1570
        /* If both paths actually refer to the same inode, suppress the xbootldr path */
1571
        if (esp_where && xbootldr_where && devnum_set_and_equal(esp_devid, xbootldr_devid))
×
1572
                xbootldr_where = mfree(xbootldr_where);
×
1573

1574
        return boot_config_load(config, esp_where, xbootldr_where);
×
1575
}
1576

1577
int boot_config_augment_from_loader(
17✔
1578
                BootConfig *config,
1579
                char **found_by_loader,
1580
                bool auto_only) {
1581

1582
        static const BootEntryAddons no_addons = (BootEntryAddons) {};
17✔
1583
        static const char *const title_table[] = {
17✔
1584
                /* Pretty names for a few well-known automatically discovered entries. */
1585
                "auto-osx",                      "macOS",
1586
                "auto-windows",                  "Windows Boot Manager",
1587
                "auto-efi-shell",                "EFI Shell",
1588
                "auto-efi-default",              "EFI Default Loader",
1589
                "auto-poweroff",                 "Power Off The System",
1590
                "auto-reboot",                   "Reboot The System",
1591
                "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
1592
                NULL,
1593
        };
1594

1595
        assert(config);
17✔
1596

1597
        /* Let's add the entries discovered by the boot loader to the end of our list, unless they are
1598
         * already included there. */
1599

1600
        STRV_FOREACH(i, found_by_loader) {
85✔
1601
                BootEntry *existing;
68✔
1602
                _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
34✔
1603

1604
                existing = boot_config_find_entry(config, *i);
68✔
1605
                if (existing) {
68✔
1606
                        existing->reported_by_loader = true;
34✔
1607
                        continue;
34✔
1608
                }
1609

1610
                if (auto_only && !startswith(*i, "auto-"))
34✔
1611
                        continue;
×
1612

1613
                c = strdup(*i);
34✔
1614
                if (!c)
34✔
1615
                        return log_oom();
×
1616

1617
                STRV_FOREACH_PAIR(a, b, title_table)
255✔
1618
                        if (streq(*a, *i)) {
238✔
1619
                                t = strdup(*b);
17✔
1620
                                if (!t)
17✔
1621
                                        return log_oom();
×
1622
                                break;
1623
                        }
1624

1625
                p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderEntries")));
34✔
1626
                if (!p)
34✔
1627
                        return log_oom();
×
1628

1629
                if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
34✔
1630
                        return log_oom();
×
1631

1632
                config->entries[config->n_entries++] = (BootEntry) {
68✔
1633
                        .type = startswith(*i, "auto-") ? BOOT_ENTRY_AUTO : BOOT_ENTRY_LOADER,
34✔
1634
                        .id = TAKE_PTR(c),
34✔
1635
                        .title = TAKE_PTR(t),
34✔
1636
                        .path = TAKE_PTR(p),
34✔
1637
                        .reported_by_loader = true,
1638
                        .tries_left = UINT_MAX,
1639
                        .tries_done = UINT_MAX,
1640
                        .profile = UINT_MAX,
1641
                        .global_addons = &no_addons,
1642
                };
1643
        }
1644

1645
        return 0;
1646
}
1647

1648
BootEntry* boot_config_find_entry(BootConfig *config, const char *id) {
72✔
1649
        assert(config);
72✔
1650
        assert(id);
72✔
1651

1652
        for (size_t j = 0; j < config->n_entries; j++)
245✔
1653
                if (strcaseeq_ptr(config->entries[j].id, id) ||
210✔
1654
                    strcaseeq_ptr(config->entries[j].id_old, id))
173✔
1655
                        return config->entries + j;
1656

1657
        return NULL;
1658
}
1659

1660
static void boot_entry_file_list(
18✔
1661
                const char *field,
1662
                const char *root,
1663
                const char *p,
1664
                int *ret_status) {
1665

1666
        assert(p);
18✔
1667
        assert(ret_status);
18✔
1668

1669
        int status = chase_and_access(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, NULL);
18✔
1670

1671
        /* Note that this shows two '/' between the root and the file. This is intentional to highlight (in
1672
         * the absence of color support) to the user that the boot loader is only interested in the second
1673
         * part of the file. */
1674
        printf("%13s%s %s%s/%s", strempty(field), field ? ":" : " ", ansi_grey(), root, ansi_normal());
36✔
1675

1676
        if (status < 0) {
18✔
1677
                errno = -status;
×
1678
                printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal());
×
1679
        } else
1680
                printf("%s\n", p);
18✔
1681

1682
        if (*ret_status == 0 && status < 0)
18✔
1683
                *ret_status = status;
×
1684
}
18✔
1685

1686
static void print_addon(
18✔
1687
                BootEntryAddon *addon,
1688
                const char *addon_str) {
1689

1690
        printf("  %s: %s\n", addon_str, addon->location);
18✔
1691
        printf("      options: %s%s\n", glyph(GLYPH_TREE_RIGHT), addon->cmdline);
18✔
1692
}
18✔
1693

1694
static int indent_embedded_newlines(char *cmdline, char **ret_cmdline) {
36✔
1695
        _cleanup_free_ char *t = NULL;
36✔
1696
        _cleanup_strv_free_ char **ts = NULL;
36✔
1697

1698
        assert(ret_cmdline);
36✔
1699

1700
        ts = strv_split_newlines(cmdline);
36✔
1701
        if (!ts)
36✔
1702
                return -ENOMEM;
1703

1704
        t = strv_join(ts, "\n              ");
36✔
1705
        if (!t)
36✔
1706
                return -ENOMEM;
1707

1708
        *ret_cmdline = TAKE_PTR(t);
36✔
1709

1710
        return 0;
36✔
1711
}
1712

1713
static int print_cmdline(const BootEntry *e) {
22✔
1714

1715
        _cleanup_free_ char *options = NULL, *combined_cmdline = NULL, *t2 = NULL;
22✔
1716

1717
        assert(e);
22✔
1718

1719
        if (!strv_isempty(e->options)) {
22✔
1720
                _cleanup_free_ char *t = NULL;
18✔
1721

1722
                options = strv_join(e->options, " ");
18✔
1723
                if (!options)
18✔
1724
                        return log_oom();
×
1725

1726
                if (indent_embedded_newlines(options, &t) < 0)
18✔
1727
                        return log_oom();
×
1728

1729
                printf("      options: %s\n", t);
18✔
1730
                t2 = strdup(options);
18✔
1731
                if (!t2)
18✔
1732
                        return log_oom();
×
1733
        }
1734

1735
        FOREACH_ARRAY(addon, e->global_addons->items, e->global_addons->n_items) {
40✔
1736
                print_addon(addon, "global-addon");
18✔
1737
                if (!strextend(&t2, " ", addon->cmdline))
18✔
1738
                        return log_oom();
×
1739
        }
1740

1741
        FOREACH_ARRAY(addon, e->local_addons.items, e->local_addons.n_items) {
22✔
1742
                /* Add space at the beginning of addon_str to align it correctly */
1743
                print_addon(addon, " local-addon");
×
1744
                if (!strextend(&t2, " ", addon->cmdline))
×
1745
                        return log_oom();
×
1746
        }
1747

1748
        /* Don't print the combined cmdline if it's same as options. */
1749
        if (streq_ptr(t2, options))
22✔
1750
                return 0;
1751

1752
        if (indent_embedded_newlines(t2, &combined_cmdline) < 0)
18✔
1753
                return log_oom();
×
1754

1755
        if (combined_cmdline)
18✔
1756
                printf("      cmdline: %s\n", combined_cmdline);
18✔
1757

1758
        return 0;
1759
}
1760

1761
static int json_addon(
9✔
1762
                BootEntryAddon *addon,
1763
                const char *addon_str,
1764
                sd_json_variant **array) {
1765

1766
        int r;
9✔
1767

1768
        assert(addon);
9✔
1769
        assert(addon_str);
9✔
1770

1771
        r = sd_json_variant_append_arraybo(
9✔
1772
                        array,
1773
                        SD_JSON_BUILD_PAIR(addon_str, SD_JSON_BUILD_STRING(addon->location)),
1774
                        SD_JSON_BUILD_PAIR("options", SD_JSON_BUILD_STRING(addon->cmdline)));
1775
        if (r < 0)
9✔
1776
                return log_oom();
×
1777

1778
        return 0;
1779
}
1780

1781
static int json_cmdline(
15✔
1782
                const BootEntry *e,
1783
                const char *def_cmdline,
1784
                sd_json_variant **v) {
1785

1786
        _cleanup_free_ char *combined_cmdline = NULL;
15✔
1787
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *addons_array = NULL;
15✔
1788
        int r;
15✔
1789

1790
        assert(e);
15✔
1791

1792
        if (def_cmdline) {
15✔
1793
                combined_cmdline = strdup(def_cmdline);
9✔
1794
                if (!combined_cmdline)
9✔
1795
                        return log_oom();
×
1796
        }
1797

1798
        FOREACH_ARRAY(addon, e->global_addons->items, e->global_addons->n_items) {
24✔
1799
                r = json_addon(addon, "globalAddon", &addons_array);
9✔
1800
                if (r < 0)
9✔
1801
                        return r;
1802
                if (!strextend(&combined_cmdline, " ", addon->cmdline))
9✔
1803
                        return log_oom();
×
1804
        }
1805

1806
        FOREACH_ARRAY(addon, e->local_addons.items, e->local_addons.n_items) {
15✔
1807
                r = json_addon(addon, "localAddon", &addons_array);
×
1808
                if (r < 0)
×
1809
                        return r;
1810
                if (!strextend(&combined_cmdline, " ", addon->cmdline))
×
1811
                        return log_oom();
×
1812
        }
1813

1814
        r = sd_json_variant_merge_objectbo(
15✔
1815
                        v,
1816
                        SD_JSON_BUILD_PAIR("addons", SD_JSON_BUILD_VARIANT(addons_array)),
1817
                        SD_JSON_BUILD_PAIR_CONDITION(!!combined_cmdline, "cmdline", SD_JSON_BUILD_STRING(combined_cmdline)));
1818
        if (r < 0)
15✔
1819
                return log_oom();
×
1820
        return 0;
1821
}
1822

1823
int show_boot_entry(
22✔
1824
                const BootEntry *e,
1825
                bool show_as_default,
1826
                bool show_as_selected,
1827
                bool show_reported) {
1828

1829
        int status = 0, r = 0;
22✔
1830

1831
        /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
1832
           boot entry itself. */
1833

1834
        assert(e);
22✔
1835

1836
        printf("         type: %s\n",
22✔
1837
               boot_entry_type_description_to_string(e->type));
22✔
1838

1839
        printf("        title: %s%s%s",
66✔
1840
               ansi_highlight(), boot_entry_title(e), ansi_normal());
1841

1842
        if (show_as_default)
22✔
1843
                printf(" %s(default)%s",
4✔
1844
                       ansi_highlight_green(), ansi_normal());
1845

1846
        if (show_as_selected)
22✔
1847
                printf(" %s(selected)%s",
4✔
1848
                       ansi_highlight_magenta(), ansi_normal());
1849

1850
        if (show_reported) {
22✔
1851
                if (e->type == BOOT_ENTRY_LOADER)
10✔
1852
                        printf(" %s(reported/absent)%s",
4✔
1853
                               ansi_highlight_red(), ansi_normal());
1854
                else if (!e->reported_by_loader && e->type != BOOT_ENTRY_AUTO)
8✔
1855
                        printf(" %s(not reported/new)%s",
4✔
1856
                               ansi_highlight_green(), ansi_normal());
1857
        }
1858

1859
        putchar('\n');
22✔
1860

1861
        if (e->id) {
22✔
1862
                printf("           id: %s", e->id);
22✔
1863

1864
                if (e->id_without_profile && !streq_ptr(e->id, e->id_without_profile))
22✔
1865
                        printf(" (without profile: %s)\n", e->id_without_profile);
18✔
1866
                else
1867
                        putchar('\n');
4✔
1868
        }
1869
        if (e->path) {
22✔
1870
                _cleanup_free_ char *text = NULL, *link = NULL;
22✔
1871

1872
                const char *p = e->root ? path_startswith(e->path, e->root) : NULL;
22✔
1873
                if (p) {
18✔
1874
                        text = strjoin(ansi_grey(), e->root, "/", ansi_normal(), "/", p);
36✔
1875
                        if (!text)
18✔
1876
                                return log_oom();
×
1877
                }
1878

1879
                /* Let's urlify the link to make it easy to view in an editor, but only if it is a text
1880
                 * file. Unified images are binary ELFs, and EFI variables are not pure text either. */
1881
                if (e->type == BOOT_ENTRY_TYPE1)
22✔
1882
                        (void) terminal_urlify_path(e->path, text, &link);
×
1883

1884
                printf("       source: %s (on the %s)\n",
22✔
1885
                       link ?: text ?: e->path,
22✔
1886
                       boot_entry_source_description_to_string(e->source));
22✔
1887
        }
1888
        if (e->tries_left != UINT_MAX) {
22✔
1889
                printf("        tries: %u left", e->tries_left);
×
1890

1891
                if (e->tries_done != UINT_MAX)
×
1892
                        printf("; %u done\n", e->tries_done);
×
1893
                else
1894
                        putchar('\n');
×
1895
        }
1896

1897
        if (e->sort_key)
22✔
1898
                printf("     sort-key: %s\n", e->sort_key);
18✔
1899
        if (e->version)
22✔
1900
                printf("      version: %s\n", e->version);
18✔
1901
        if (e->machine_id)
22✔
1902
                printf("   machine-id: %s\n", e->machine_id);
×
1903
        if (e->architecture)
22✔
1904
                printf(" architecture: %s\n", e->architecture);
×
1905
        if (e->kernel)
22✔
1906
                boot_entry_file_list("linux", e->root, e->kernel, &status);
18✔
1907
        if (e->efi)
22✔
1908
                boot_entry_file_list("efi", e->root, e->efi, &status);
×
1909
        if (e->uki)
22✔
1910
                boot_entry_file_list("uki", e->root, e->uki, &status);
×
1911
        if (e->uki_url)
22✔
NEW
1912
                printf("      uki-url: %s\n", e->uki_url);
×
1913
        if (e->profile != UINT_MAX)
22✔
1914
                printf("      profile: %u\n", e->profile);
18✔
1915

1916
        STRV_FOREACH(s, e->initrd)
22✔
1917
                boot_entry_file_list(s == e->initrd ? "initrd" : NULL,
×
1918
                                     e->root,
×
1919
                                     *s,
1920
                                     &status);
1921

1922
        r = print_cmdline(e);
22✔
1923
        if (r < 0)
22✔
1924
                return r;
1925

1926
        if (e->device_tree)
22✔
1927
                boot_entry_file_list("devicetree", e->root, e->device_tree, &status);
×
1928

1929
        STRV_FOREACH(s, e->device_tree_overlay)
22✔
1930
                boot_entry_file_list(s == e->device_tree_overlay ? "devicetree-overlay" : NULL,
×
1931
                                     e->root,
×
1932
                                     *s,
1933
                                     &status);
1934

1935
        return -status;
22✔
1936
}
1937

1938
int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret) {
15✔
1939
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
15✔
1940
        _cleanup_free_ char *opts = NULL;
15✔
1941
        const BootEntry *e;
15✔
1942
        int r;
15✔
1943

1944
        assert(c);
15✔
1945
        assert(ret);
15✔
1946

1947
        if (i >= c->n_entries) {
15✔
1948
                *ret = NULL;
×
1949
                return 0;
×
1950
        }
1951

1952
        e = c->entries + i;
15✔
1953

1954
        if (!strv_isempty(e->options)) {
15✔
1955
                opts = strv_join(e->options, " ");
9✔
1956
                if (!opts)
9✔
1957
                        return log_oom();
×
1958
        }
1959

1960
        r = sd_json_variant_merge_objectbo(
15✔
1961
                        &v,
1962
                        SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(boot_entry_type_to_string(e->type))),
1963
                        SD_JSON_BUILD_PAIR("source", SD_JSON_BUILD_STRING(boot_entry_source_to_string(e->source))),
1964
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->id, "id", SD_JSON_BUILD_STRING(e->id)),
1965
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->path, "path", SD_JSON_BUILD_STRING(e->path)),
1966
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->root, "root", SD_JSON_BUILD_STRING(e->root)),
1967
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->title, "title", SD_JSON_BUILD_STRING(e->title)),
1968
                        SD_JSON_BUILD_PAIR_CONDITION(!!boot_entry_title(e), "showTitle", SD_JSON_BUILD_STRING(boot_entry_title(e))),
1969
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->sort_key, "sortKey", SD_JSON_BUILD_STRING(e->sort_key)),
1970
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->version, "version", SD_JSON_BUILD_STRING(e->version)),
1971
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->machine_id, "machineId", SD_JSON_BUILD_STRING(e->machine_id)),
1972
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->architecture, "architecture", SD_JSON_BUILD_STRING(e->architecture)),
1973
                        SD_JSON_BUILD_PAIR_CONDITION(!!opts, "options", SD_JSON_BUILD_STRING(opts)),
1974
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->kernel, "linux", SD_JSON_BUILD_STRING(e->kernel)),
1975
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->efi, "efi", SD_JSON_BUILD_STRING(e->efi)),
1976
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->uki, "uki", SD_JSON_BUILD_STRING(e->uki)),
1977
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->uki_url, "ukiUrl", SD_JSON_BUILD_STRING(e->uki_url)),
1978
                        SD_JSON_BUILD_PAIR_CONDITION(e->profile != UINT_MAX, "profile", SD_JSON_BUILD_UNSIGNED(e->profile)),
1979
                        SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", SD_JSON_BUILD_STRV(e->initrd)));
1980
        if (r < 0)
15✔
1981
                return log_oom();
×
1982

1983
        /* Sanitizers (only memory sanitizer?) do not like function call with too many
1984
         * arguments and trigger false positive warnings. Let's not add too many json objects
1985
         * at once. */
1986
        r = sd_json_variant_merge_objectbo(
15✔
1987
                        &v,
1988
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->device_tree, "devicetree", SD_JSON_BUILD_STRING(e->device_tree)),
1989
                        SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", SD_JSON_BUILD_STRV(e->device_tree_overlay)),
1990
                        SD_JSON_BUILD_PAIR("isReported", SD_JSON_BUILD_BOOLEAN(e->reported_by_loader)),
1991
                        SD_JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", SD_JSON_BUILD_UNSIGNED(e->tries_left)),
1992
                        SD_JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", SD_JSON_BUILD_UNSIGNED(e->tries_done)),
1993
                        SD_JSON_BUILD_PAIR_CONDITION(c->default_entry >= 0, "isDefault", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->default_entry)),
1994
                        SD_JSON_BUILD_PAIR_CONDITION(c->selected_entry >= 0, "isSelected", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->selected_entry)));
1995
        if (r < 0)
15✔
1996
                return log_oom();
×
1997

1998
        r = json_cmdline(e, opts, &v);
15✔
1999
        if (r < 0)
15✔
2000
                return log_oom();
×
2001

2002
        *ret = TAKE_PTR(v);
15✔
2003
        return 1;
15✔
2004
}
2005

2006
int show_boot_entries(const BootConfig *config, sd_json_format_flags_t json_format) {
8✔
2007
        int r;
8✔
2008

2009
        assert(config);
8✔
2010

2011
        if (sd_json_format_enabled(json_format)) {
8✔
2012
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
6✔
2013

2014
                for (size_t i = 0; i < config->n_entries; i++) {
16✔
2015
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
10✔
2016

2017
                        r = boot_entry_to_json(config, i, &v);
10✔
2018
                        if (r < 0)
10✔
2019
                                return log_oom();
×
2020

2021
                        r = sd_json_variant_append_array(&array, v);
10✔
2022
                        if (r < 0)
10✔
2023
                                return log_oom();
×
2024
                }
2025

2026
                return sd_json_variant_dump(array, json_format | SD_JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
6✔
2027
        } else
2028
                for (size_t n = 0; n < config->n_entries; n++) {
12✔
2029
                        r = show_boot_entry(
20✔
2030
                                        config->entries + n,
10✔
2031
                                        /* show_as_default= */  n == (size_t) config->default_entry,
10✔
2032
                                        /* show_as_selected= */ n == (size_t) config->selected_entry,
10✔
2033
                                        /* show_reported= */  true);
2034
                        if (r < 0)
10✔
2035
                                return r;
2036

2037
                        if (n+1 < config->n_entries)
10✔
2038
                                putchar('\n');
8✔
2039
                }
2040

2041
        return 0;
2042
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc