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

systemd / systemd / 13877892945

15 Mar 2025 08:56PM UTC coverage: 71.915% (+0.2%) from 71.757%
13877892945

push

github

web-flow
Fix bootctl status to not print strange glyphs in logs (#36745)

146 of 198 new or added lines in 57 files covered. (73.74%)

153 existing lines in 28 files now uncovered.

296065 of 411690 relevant lines covered (71.91%)

715276.25 hits per line

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

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

3
#include <unistd.h>
4

5
#include "bootspec-fundamental.h"
6
#include "bootspec.h"
7
#include "chase.h"
8
#include "conf-files.h"
9
#include "devnum-util.h"
10
#include "dirent-util.h"
11
#include "efi-loader.h"
12
#include "env-file.h"
13
#include "errno-util.h"
14
#include "fd-util.h"
15
#include "fileio.h"
16
#include "find-esp.h"
17
#include "path-util.h"
18
#include "pe-binary.h"
19
#include "pretty-print.h"
20
#include "recurse-dir.h"
21
#include "sort-util.h"
22
#include "stat-util.h"
23
#include "string-table.h"
24
#include "strv.h"
25
#include "terminal-util.h"
26
#include "uki.h"
27
#include "unaligned.h"
28

29
static const char* const boot_entry_type_table[_BOOT_ENTRY_TYPE_MAX] = {
30
        [BOOT_ENTRY_CONF]        = "Boot Loader Specification Type #1 (.conf)",
31
        [BOOT_ENTRY_UNIFIED]     = "Boot Loader Specification Type #2 (.efi)",
32
        [BOOT_ENTRY_LOADER]      = "Reported by Boot Loader",
33
        [BOOT_ENTRY_LOADER_AUTO] = "Automatic",
34
};
35

36
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type, BootEntryType);
15✔
37

38
static const char* const boot_entry_type_json_table[_BOOT_ENTRY_TYPE_MAX] = {
39
        [BOOT_ENTRY_CONF]        = "type1",
40
        [BOOT_ENTRY_UNIFIED]     = "type2",
41
        [BOOT_ENTRY_LOADER]      = "loader",
42
        [BOOT_ENTRY_LOADER_AUTO] = "auto",
43
};
44

45
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_type_json, BootEntryType);
9✔
46

47
static const char* const boot_entry_source_table[_BOOT_ENTRY_SOURCE_MAX] = {
48
        [BOOT_ENTRY_ESP]      = "EFI System Partition",
49
        [BOOT_ENTRY_XBOOTLDR] = "Extended Boot Loader Partition",
50
};
51

52
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source, BootEntrySource);
15✔
53

54
static const char* const boot_entry_source_json_table[_BOOT_ENTRY_SOURCE_MAX] = {
55
        [BOOT_ENTRY_ESP]      = "esp",
56
        [BOOT_ENTRY_XBOOTLDR] = "xbootldr",
57
};
58

59
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(boot_entry_source_json, BootEntrySource);
9✔
60

61
static void boot_entry_addons_done(BootEntryAddons *addons) {
258✔
62
        assert(addons);
258✔
63

64
        FOREACH_ARRAY(addon, addons->items, addons->n_items) {
258✔
65
                free(addon->cmdline);
×
66
                free(addon->location);
×
67
        }
68
        addons->items = mfree(addons->items);
258✔
69
        addons->n_items = 0;
258✔
70
}
258✔
71

72
static void boot_entry_free(BootEntry *entry) {
112✔
73
        assert(entry);
112✔
74

75
        free(entry->id);
112✔
76
        free(entry->id_old);
112✔
77
        free(entry->id_without_profile);
112✔
78
        free(entry->path);
112✔
79
        free(entry->root);
112✔
80
        free(entry->title);
112✔
81
        free(entry->show_title);
112✔
82
        free(entry->sort_key);
112✔
83
        free(entry->version);
112✔
84
        free(entry->machine_id);
112✔
85
        free(entry->architecture);
112✔
86
        strv_free(entry->options);
112✔
87
        boot_entry_addons_done(&entry->local_addons);
112✔
88
        free(entry->kernel);
112✔
89
        free(entry->efi);
112✔
90
        strv_free(entry->initrd);
112✔
91
        free(entry->device_tree);
112✔
92
        strv_free(entry->device_tree_overlay);
112✔
93
}
112✔
94

95
static int mangle_path(
×
96
                const char *fname,
97
                unsigned line,
98
                const char *field,
99
                const char *p,
100
                char **ret) {
101

102
        _cleanup_free_ char *c = NULL;
×
103

104
        assert(field);
×
105
        assert(p);
×
106
        assert(ret);
×
107

108
        /* Spec leaves open if prefixed with "/" or not, let's normalize that */
109
        c = path_make_absolute(p, "/");
×
110
        if (!c)
×
111
                return -ENOMEM;
112

113
        /* We only reference files, never directories */
114
        if (endswith(c, "/")) {
×
115
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' has trailing slash, ignoring: %s", field, c);
×
116
                *ret = NULL;
×
117
                return 0;
×
118
        }
119

120
        /* Remove duplicate "/" */
121
        path_simplify(c);
×
122

123
        /* No ".." or "." or so */
124
        if (!path_is_normalized(c)) {
×
125
                log_syntax(NULL, LOG_WARNING, fname, line, 0, "Path in field '%s' is not normalized, ignoring: %s", field, c);
×
126
                *ret = NULL;
×
127
                return 0;
×
128
        }
129

130
        *ret = TAKE_PTR(c);
×
131
        return 1;
×
132
}
133

134
static int parse_path_one(
×
135
                const char *fname,
136
                unsigned line,
137
                const char *field,
138
                char **s,
139
                const char *p) {
140

141
        _cleanup_free_ char *c = NULL;
×
142
        int r;
×
143

144
        assert(field);
×
145
        assert(s);
×
146
        assert(p);
×
147

148
        r = mangle_path(fname, line, field, p, &c);
×
149
        if (r <= 0)
×
150
                return r;
151

152
        return free_and_replace(*s, c);
×
153
}
154

155
static int parse_path_strv(
×
156
                const char *fname,
157
                unsigned line,
158
                const char *field,
159
                char ***s,
160
                const char *p) {
161

162
        char *c;
×
163
        int r;
×
164

165
        assert(field);
×
166
        assert(s);
×
167
        assert(p);
×
168

169
        r = mangle_path(fname, line, field, p, &c);
×
170
        if (r <= 0)
×
171
                return r;
×
172

173
        return strv_consume(s, c);
×
174
}
175

176
static int parse_path_many(
×
177
                const char *fname,
178
                unsigned line,
179
                const char *field,
180
                char ***s,
181
                const char *p) {
182

183
        _cleanup_strv_free_ char **l = NULL, **f = NULL;
×
184
        int r;
×
185

186
        l = strv_split(p, NULL);
×
187
        if (!l)
×
188
                return -ENOMEM;
189

190
        STRV_FOREACH(i, l) {
×
191
                char *c;
×
192

193
                r = mangle_path(fname, line, field, *i, &c);
×
194
                if (r < 0)
×
195
                        return r;
×
196
                if (r == 0)
×
197
                        continue;
×
198

199
                r = strv_consume(&f, c);
×
200
                if (r < 0)
×
201
                        return r;
202
        }
203

204
        return strv_extend_strv_consume(s, TAKE_PTR(f), /* filter_duplicates= */ false);
×
205
}
206

207
static int parse_tries(const char *fname, const char **p, unsigned *ret) {
36✔
208
        _cleanup_free_ char *d = NULL;
36✔
209
        unsigned tries;
36✔
210
        size_t n;
36✔
211
        int r;
36✔
212

213
        assert(fname);
36✔
214
        assert(p);
36✔
215
        assert(*p);
36✔
216
        assert(ret);
36✔
217

218
        n = strspn(*p, DIGITS);
36✔
219
        if (n == 0) {
36✔
220
                *ret = UINT_MAX;
2✔
221
                return 0;
2✔
222
        }
223

224
        d = strndup(*p, n);
34✔
225
        if (!d)
34✔
226
                return log_oom();
×
227

228
        r = safe_atou_full(d, 10, &tries);
34✔
229
        if (r >= 0 && tries > INT_MAX) /* sd-boot allows INT_MAX, let's use the same limit */
34✔
230
                r = -ERANGE;
231
        if (r < 0)
32✔
232
                return log_error_errno(r, "Failed to parse tries counter of filename '%s': %m", fname);
2✔
233

234
        *p = *p + n;
32✔
235
        *ret = tries;
32✔
236
        return 1;
32✔
237
}
238

239
int boot_filename_extract_tries(
79✔
240
                const char *fname,
241
                char **ret_stripped,
242
                unsigned *ret_tries_left,
243
                unsigned *ret_tries_done) {
244

245
        unsigned tries_left = UINT_MAX, tries_done = UINT_MAX;
79✔
246
        _cleanup_free_ char *stripped = NULL;
79✔
247
        const char *p, *suffix, *m;
79✔
248
        int r;
79✔
249

250
        assert(fname);
79✔
251
        assert(ret_stripped);
79✔
252
        assert(ret_tries_left);
79✔
253
        assert(ret_tries_done);
79✔
254

255
        /* Be liberal with suffix, only insist on a dot. After all we want to cover any capitalization here
256
         * (vfat is case insensitive after all), and at least .efi and .conf as suffix. */
257
        suffix = strrchr(fname, '.');
79✔
258
        if (!suffix)
79✔
259
                goto nothing;
1✔
260

261
        p = m = memrchr(fname, '+', suffix - fname);
78✔
262
        if (!p)
78✔
263
                goto nothing;
55✔
264
        p++;
23✔
265

266
        r = parse_tries(fname, &p, &tries_left);
23✔
267
        if (r < 0)
23✔
268
                return r;
269
        if (r == 0)
22✔
270
                goto nothing;
2✔
271

272
        if (*p == '-') {
20✔
273
                p++;
13✔
274

275
                r = parse_tries(fname, &p, &tries_done);
13✔
276
                if (r < 0)
13✔
277
                        return r;
278
                if (r == 0)
12✔
279
                        goto nothing;
×
280
        }
281

282
        if (p != suffix)
19✔
283
                goto nothing;
3✔
284

285
        stripped = strndup(fname, m - fname);
16✔
286
        if (!stripped)
16✔
287
                return log_oom();
×
288

289
        if (!strextend(&stripped, suffix))
16✔
290
                return log_oom();
×
291

292
        *ret_stripped = TAKE_PTR(stripped);
16✔
293
        *ret_tries_left = tries_left;
16✔
294
        *ret_tries_done = tries_done;
16✔
295

296
        return 0;
16✔
297

298
nothing:
61✔
299
        stripped = strdup(fname);
61✔
300
        if (!stripped)
61✔
301
                return log_oom();
×
302

303
        *ret_stripped = TAKE_PTR(stripped);
61✔
304
        *ret_tries_left = *ret_tries_done = UINT_MAX;
61✔
305
        return 0;
61✔
306
}
307

308
static int boot_entry_load_type1(
8✔
309
                FILE *f,
310
                const char *root,
311
                const BootEntrySource source,
312
                const char *dir,
313
                const char *fname,
314
                BootEntry *ret) {
315

316
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_CONF, source);
8✔
317
        char *c;
8✔
318
        int r;
8✔
319

320
        assert(f);
8✔
321
        assert(root);
8✔
322
        assert(dir);
8✔
323
        assert(fname);
8✔
324
        assert(ret);
8✔
325

326
        /* Loads a Type #1 boot menu entry from the specified FILE* object */
327

328
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
8✔
329
        if (r < 0)
8✔
330
                return r;
331

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

335
        c = endswith_no_case(tmp.id, ".conf");
8✔
336
        if (!c)
8✔
337
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry file suffix: %s", fname);
×
338

339
        tmp.id_old = strndup(tmp.id, c - tmp.id); /* Without .conf suffix */
8✔
340
        if (!tmp.id_old)
8✔
341
                return log_oom();
×
342

343
        tmp.path = path_join(dir, fname);
8✔
344
        if (!tmp.path)
8✔
345
                return log_oom();
×
346

347
        tmp.root = strdup(root);
8✔
348
        if (!tmp.root)
8✔
349
                return log_oom();
×
350

351
        for (unsigned line = 1;; line++) {
27✔
352
                _cleanup_free_ char *buf = NULL, *field = NULL;
27✔
353

354
                r = read_stripped_line(f, LONG_LINE_MAX, &buf);
35✔
355
                if (r == -ENOBUFS)
35✔
356
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long.");
×
357
                if (r < 0)
35✔
358
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m");
×
359
                if (r == 0)
35✔
360
                        break;
361

362
                if (IN_SET(buf[0], '#', '\0'))
27✔
363
                        continue;
×
364

365
                const char *p = buf;
27✔
366
                r = extract_first_word(&p, &field, NULL, 0);
27✔
367
                if (r < 0) {
27✔
368
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m");
×
369
                        continue;
×
370
                }
371
                if (r == 0) {
27✔
372
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Bad syntax, ignoring line.");
×
373
                        continue;
×
374
                }
375

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

381
                        continue;
×
382
                }
383

384
                if (streq(field, "title"))
27✔
385
                        r = free_and_strdup(&tmp.title, p);
8✔
386
                else if (streq(field, "sort-key"))
19✔
387
                        r = free_and_strdup(&tmp.sort_key, p);
3✔
388
                else if (streq(field, "version"))
16✔
389
                        r = free_and_strdup(&tmp.version, p);
8✔
390
                else if (streq(field, "machine-id"))
8✔
391
                        r = free_and_strdup(&tmp.machine_id, p);
8✔
392
                else if (streq(field, "architecture"))
×
393
                        r = free_and_strdup(&tmp.architecture, p);
×
394
                else if (streq(field, "options"))
×
395
                        r = strv_extend(&tmp.options, p);
×
396
                else if (streq(field, "linux"))
×
397
                        r = parse_path_one(tmp.path, line, field, &tmp.kernel, p);
×
398
                else if (streq(field, "efi"))
×
399
                        r = parse_path_one(tmp.path, line, field, &tmp.efi, p);
×
400
                else if (streq(field, "initrd"))
×
401
                        r = parse_path_strv(tmp.path, line, field, &tmp.initrd, p);
×
402
                else if (streq(field, "devicetree"))
×
403
                        r = parse_path_one(tmp.path, line, field, &tmp.device_tree, p);
×
404
                else if (streq(field, "devicetree-overlay"))
×
405
                        r = parse_path_many(tmp.path, line, field, &tmp.device_tree_overlay, p);
×
406
                else {
407
                        log_syntax(NULL, LOG_WARNING, tmp.path, line, 0, "Unknown line '%s', ignoring.", field);
×
408
                        continue;
×
409
                }
410
                if (r < 0)
27✔
411
                        return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while parsing: %m");
×
412
        }
413

414
        *ret = TAKE_STRUCT(tmp);
8✔
415
        return 0;
8✔
416
}
417

418
int boot_config_load_type1(
8✔
419
                BootConfig *config,
420
                FILE *f,
421
                const char *root,
422
                const BootEntrySource source,
423
                const char *dir,
424
                const char *fname) {
425
        int r;
8✔
426

427
        assert(config);
8✔
428
        assert(f);
8✔
429
        assert(root);
8✔
430
        assert(dir);
8✔
431
        assert(fname);
8✔
432

433
        if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
8✔
434
                return log_oom();
×
435

436
        BootEntry *entry = config->entries + config->n_entries;
8✔
437

438
        r = boot_entry_load_type1(f, root, source, dir, fname, entry);
8✔
439
        if (r < 0)
8✔
440
                return r;
441
        config->n_entries++;
8✔
442

443
        entry->global_addons = &config->global_addons[source];
8✔
444

445
        return 0;
8✔
446
}
447

448
void boot_config_free(BootConfig *config) {
30✔
449
        assert(config);
30✔
450

451
        free(config->default_pattern);
30✔
452

453
        free(config->entry_oneshot);
30✔
454
        free(config->entry_default);
30✔
455
        free(config->entry_selected);
30✔
456

457
        FOREACH_ARRAY(i, config->entries, config->n_entries)
92✔
458
                boot_entry_free(i);
62✔
459
        free(config->entries);
30✔
460

461
        FOREACH_ELEMENT(i, config->global_addons)
90✔
462
                boot_entry_addons_done(i);
60✔
463

464
        set_free(config->inodes_seen);
30✔
465
}
30✔
466

467
int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
27✔
468
        int r;
27✔
469

470
        assert(config);
27✔
471
        assert(file);
27✔
472
        assert(path);
27✔
473

474
        for (unsigned line = 1;; line++) {
54✔
475
                _cleanup_free_ char *buf = NULL, *field = NULL;
54✔
476

477
                r = read_stripped_line(file, LONG_LINE_MAX, &buf);
81✔
478
                if (r == -ENOBUFS)
81✔
479
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long.");
×
480
                if (r < 0)
81✔
481
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m");
×
482
                if (r == 0)
81✔
483
                        break;
484

485
                if (IN_SET(buf[0], '#', '\0'))
54✔
486
                        continue;
54✔
487

488
                const char *p = buf;
×
489
                r = extract_first_word(&p, &field, NULL, 0);
×
490
                if (r < 0) {
×
491
                        log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m");
×
492
                        continue;
×
493
                }
494
                if (r == 0) {
×
495
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Bad syntax, ignoring line.");
×
496
                        continue;
×
497
                }
498
                if (isempty(p)) {
×
499
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Field '%s' without value, ignoring line.", field);
×
500
                        continue;
×
501
                }
502

503
                if (streq(field, "default"))
×
504
                        r = free_and_strdup(&config->default_pattern, p);
×
505
                else if (STR_IN_SET(field, "timeout", "editor", "auto-entries", "auto-firmware",
×
506
                                    "auto-poweroff", "auto-reboot", "beep", "reboot-for-bitlocker",
507
                                    "secure-boot-enroll", "console-mode"))
508
                        r = 0; /* we don't parse these in userspace, but they are OK */
×
509
                else {
510
                        log_syntax(NULL, LOG_WARNING, path, line, 0, "Unknown line '%s', ignoring.", field);
×
511
                        continue;
×
512
                }
513
                if (r < 0)
×
514
                        return log_syntax(NULL, LOG_ERR, path, line, r, "Error while parsing: %m");
×
515
        }
516

517
        return 1;
27✔
518
}
519

520
static int boot_loader_read_conf_path(BootConfig *config, const char *root, const char *path) {
30✔
521
        _cleanup_free_ char *full = NULL;
30✔
522
        _cleanup_fclose_ FILE *f = NULL;
30✔
523
        int r;
30✔
524

525
        assert(config);
30✔
526
        assert(path);
30✔
527

528
        r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, "re", &full, &f);
30✔
529
        if (r == -ENOENT)
30✔
530
                return 0;
531
        if (r < 0)
27✔
532
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, path);
×
533

534
        return boot_loader_read_conf(config, f, full);
27✔
535
}
536

537
static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
38✔
538
        int r;
38✔
539

540
        assert(a);
38✔
541
        assert(b);
38✔
542

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

545
        r = CMP(a->tries_left == 0, b->tries_left == 0);
38✔
546
        if (r != 0)
38✔
547
                return r;
×
548

549
        r = CMP(!a->sort_key, !b->sort_key);
38✔
550
        if (r != 0)
38✔
551
                return r;
3✔
552

553
        if (a->sort_key && b->sort_key) {
35✔
554
                r = strcmp(a->sort_key, b->sort_key);
31✔
555
                if (r != 0)
31✔
556
                        return r;
557

558
                r = strcmp_ptr(a->machine_id, b->machine_id);
29✔
559
                if (r != 0)
29✔
560
                        return r;
561

562
                r = -strverscmp_improved(a->version, b->version);
29✔
563
                if (r != 0)
29✔
564
                        return r;
565
        }
566

567
        r = -strverscmp_improved(a->id_without_profile ?: a->id, b->id_without_profile ?: b->id);
32✔
568
        if (r != 0)
32✔
569
                return r;
570

571
        if (a->id_without_profile && b->id_without_profile) {
28✔
572
                /* The strverscmp_improved() call above already established that we are talking about the
573
                 * same image here, hence order by profile, if there is one */
574
                r = CMP(a->profile, b->profile);
28✔
575
                if (r != 0)
×
576
                        return r;
28✔
577
        }
578

579
        if (a->tries_left != UINT_MAX || b->tries_left != UINT_MAX)
×
580
                return 0;
581

582
        r = -CMP(a->tries_left, b->tries_left);
×
583
        if (r != 0)
×
584
                return r;
×
585

586
        return CMP(a->tries_done, b->tries_done);
×
587
}
588

589
static int config_check_inode_relevant_and_unseen(BootConfig *config, int fd, const char *fname) {
22✔
590
        _cleanup_free_ char *d = NULL;
44✔
591
        struct stat st;
22✔
592

593
        assert(config);
22✔
594
        assert(fd >= 0);
22✔
595
        assert(fname);
22✔
596

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

603
        if (fstat(fd, &st) < 0)
22✔
604
                return log_error_errno(errno, "Failed to stat('%s'): %m", fname);
×
605
        if (!S_ISREG(st.st_mode)) {
22✔
606
                log_debug("File '%s' is not a regular file, ignoring.", fname);
×
607
                return false;
×
608
        }
609

610
        if (set_contains(config->inodes_seen, &st)) {
22✔
611
                log_debug("Inode '%s' already seen before, ignoring.", fname);
×
612
                return false;
×
613
        }
614

615
        d = memdup(&st, sizeof(st));
22✔
616
        if (!d)
22✔
617
                return log_oom();
×
618

619
        if (set_ensure_consume(&config->inodes_seen, &inode_hash_ops, TAKE_PTR(d)) < 0)
22✔
620
                return log_oom();
×
621

622
        return true;
623
}
624

625
static int boot_entries_find_type1(
44✔
626
                BootConfig *config,
627
                const char *root,
628
                const BootEntrySource source,
629
                const char *dir) {
630

631
        _cleanup_free_ DirectoryEntries *dentries = NULL;
88✔
632
        _cleanup_free_ char *full = NULL;
44✔
633
        _cleanup_close_ int dir_fd = -EBADF;
44✔
634
        int r;
44✔
635

636
        assert(config);
44✔
637
        assert(root);
44✔
638
        assert(dir);
44✔
639

640
        dir_fd = chase_and_open(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, O_DIRECTORY|O_CLOEXEC, &full);
44✔
641
        if (dir_fd == -ENOENT)
44✔
642
                return 0;
643
        if (dir_fd < 0)
29✔
644
                return log_error_errno(dir_fd, "Failed to open '%s/%s': %m", root, dir);
×
645

646
        r = readdir_all(dir_fd, RECURSE_DIR_IGNORE_DOT, &dentries);
29✔
647
        if (r < 0)
29✔
648
                return log_error_errno(r, "Failed to read directory '%s': %m", full);
×
649

650
        FOREACH_ARRAY(i, dentries->entries, dentries->n_entries) {
37✔
651
                const struct dirent *de = *i;
8✔
652
                _cleanup_fclose_ FILE *f = NULL;
8✔
653

654
                if (!dirent_is_file(de))
8✔
655
                        continue;
×
656

657
                if (!endswith_no_case(de->d_name, ".conf"))
8✔
658
                        continue;
×
659

660
                r = xfopenat(dir_fd, de->d_name, "re", O_NOFOLLOW|O_NOCTTY, &f);
8✔
661
                if (r < 0) {
8✔
662
                        log_warning_errno(r, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
663
                        continue;
×
664
                }
665

666
                r = config_check_inode_relevant_and_unseen(config, fileno(f), de->d_name);
8✔
667
                if (r < 0)
8✔
668
                        return r;
669
                if (r == 0) /* inode already seen or otherwise not relevant */
8✔
670
                        continue;
×
671

672
                r = boot_config_load_type1(config, f, root, source, full, de->d_name);
8✔
673
                if (r == -ENOMEM) /* ignore all other errors */
8✔
674
                        return log_oom();
×
675
        }
676

677
        return 0;
678
}
679

680
static int boot_entry_load_unified(
42✔
681
                const char *root,
682
                const BootEntrySource source,
683
                const char *path,
684
                unsigned profile,
685
                const char *osrelease_text,
686
                const char *profile_text,
687
                const char *cmdline_text,
688
                BootEntry *ret) {
689

690
        _cleanup_free_ char *fname = NULL, *os_pretty_name = NULL, *os_image_id = NULL, *os_name = NULL, *os_id = NULL,
×
691
                *os_image_version = NULL, *os_version = NULL, *os_version_id = NULL, *os_build_id = NULL;
42✔
692
        const char *k, *good_name, *good_version, *good_sort_key;
42✔
693
        _cleanup_fclose_ FILE *f = NULL;
42✔
694
        int r;
42✔
695

696
        assert(root);
42✔
697
        assert(path);
42✔
698
        assert(osrelease_text);
42✔
699
        assert(ret);
42✔
700

701
        k = path_startswith(path, root);
42✔
702
        if (!k)
42✔
703
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
×
704

705
        f = fmemopen_unlocked((void*) osrelease_text, strlen(osrelease_text), "r");
42✔
706
        if (!f)
42✔
707
                return log_oom();
×
708

709
        r = parse_env_file(f, "os-release",
42✔
710
                           "PRETTY_NAME", &os_pretty_name,
711
                           "IMAGE_ID", &os_image_id,
712
                           "NAME", &os_name,
713
                           "ID", &os_id,
714
                           "IMAGE_VERSION", &os_image_version,
715
                           "VERSION", &os_version,
716
                           "VERSION_ID", &os_version_id,
717
                           "BUILD_ID", &os_build_id);
718
        if (r < 0)
42✔
719
                return log_error_errno(r, "Failed to parse os-release data from unified kernel image %s: %m", path);
×
720

721
        if (!bootspec_pick_name_version_sort_key(
42✔
722
                            os_pretty_name,
723
                            os_image_id,
724
                            os_name,
725
                            os_id,
726
                            os_image_version,
727
                            os_version,
728
                            os_version_id,
729
                            os_build_id,
730
                            &good_name,
731
                            &good_version,
732
                            &good_sort_key))
733
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Missing fields in os-release data from unified kernel image %s, refusing.", path);
×
734

735
        _cleanup_free_ char *profile_id = NULL, *profile_title = NULL;
42✔
736
        if (profile_text) {
42✔
737
                fclose(f);
42✔
738

739
                f = fmemopen_unlocked((void*) profile_text, strlen(profile_text), "r");
42✔
740
                if (!f)
42✔
741
                        return log_oom();
×
742

743
                r = parse_env_file(
42✔
744
                                f, "profile",
745
                                "ID", &profile_id,
746
                                "TITLE", &profile_title);
747
                if (r < 0)
42✔
748
                        return log_error_errno(r, "Failed to parse profile data from unified kernel image '%s': %m", path);
×
749
        }
750

751
        r = path_extract_filename(path, &fname);
42✔
752
        if (r < 0)
42✔
753
                return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
×
754

755
        _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_UNIFIED, source);
42✔
756

757
        r = boot_filename_extract_tries(fname, &tmp.id, &tmp.tries_left, &tmp.tries_done);
42✔
758
        if (r < 0)
42✔
759
                return r;
760

761
        if (!efi_loader_entry_name_valid(tmp.id))
42✔
762
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid loader entry name: %s", tmp.id);
×
763

764
        tmp.profile = profile;
42✔
765

766
        if (profile_id || profile > 0) {
42✔
767
                tmp.id_without_profile = TAKE_PTR(tmp.id);
42✔
768

769
                if (profile_id)
42✔
770
                        tmp.id = strjoin(tmp.id_without_profile, "@", profile_id);
42✔
771
                else
772
                        (void) asprintf(&tmp.id, "%s@%u", tmp.id_without_profile, profile);
×
773
                if (!tmp.id)
42✔
774
                        return log_oom();
×
775
        }
776

777
        if (os_id && os_version_id) {
42✔
778
                tmp.id_old = strjoin(os_id, "-", os_version_id);
×
779
                if (!tmp.id_old)
×
780
                        return log_oom();
×
781
        }
782

783
        tmp.path = strdup(path);
42✔
784
        if (!tmp.path)
42✔
785
                return log_oom();
×
786

787
        tmp.root = strdup(root);
42✔
788
        if (!tmp.root)
42✔
789
                return log_oom();
×
790

791
        tmp.kernel = path_make_absolute(k, "/");
42✔
792
        if (!tmp.kernel)
42✔
793
                return log_oom();
×
794

795
        tmp.options = strv_new(cmdline_text);
42✔
796
        if (!tmp.options)
42✔
797
                return log_oom();
×
798

799
        if (profile_title)
42✔
800
                tmp.title = strjoin(good_name, " (", profile_title, ")");
28✔
801
        else if (profile_id)
14✔
802
                tmp.title = strjoin(good_name, " (", profile_id, ")");
14✔
803
        else if (profile > 0)
×
804
                (void) asprintf(&tmp.title, "%s (@%u)", good_name, profile);
×
805
        else
806
                tmp.title = strdup(good_name);
×
807
        if (!tmp.title)
42✔
808
                return log_oom();
×
809

810
        if (good_sort_key) {
42✔
811
                tmp.sort_key = strdup(good_sort_key);
42✔
812
                if (!tmp.sort_key)
42✔
813
                        return log_oom();
×
814
        }
815

816
        if (good_version) {
42✔
817
                tmp.version = strdup(good_version);
42✔
818
                if (!tmp.version)
42✔
819
                        return log_oom();
×
820
        }
821

822
        *ret = TAKE_STRUCT(tmp);
42✔
823
        return 0;
42✔
824
}
825

826
static int pe_load_headers_and_sections(
56✔
827
                int fd,
828
                const char *path,
829
                IMAGE_SECTION_HEADER **ret_sections,
830
                PeHeader **ret_pe_header) {
831

832
        _cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL;
56✔
833
        IMAGE_SECTION_HEADER *sections;
56✔
834
        PeHeader *pe_header;
56✔
835
        int r;
56✔
836

837
        assert(fd >= 0);
56✔
838
        assert(path);
56✔
839

840
        r = pe_load_headers(fd, &dos_header, &pe_header);
56✔
841
        if (r < 0)
56✔
842
                return log_error_errno(r, "Failed to parse PE file '%s': %m", path);
×
843

844
        r = pe_load_sections(fd, dos_header, pe_header, &sections);
56✔
845
        if (r < 0)
56✔
846
                return log_error_errno(r, "Failed to parse PE sections of '%s': %m", path);
×
847

848
        if (ret_pe_header)
56✔
849
                *ret_pe_header = TAKE_PTR(pe_header);
56✔
850
        if (ret_sections)
56✔
851
                *ret_sections = TAKE_PTR(sections);
56✔
852

853
        return 0;
854
}
855

856
static const IMAGE_SECTION_HEADER* pe_find_profile_section_table(
98✔
857
                const PeHeader *pe_header,
858
                const IMAGE_SECTION_HEADER *sections,
859
                unsigned profile,
860
                size_t *ret_n_sections) {
861

862
        assert(pe_header);
98✔
863

864
        /* Looks for the part of the section table that defines the specified profile. If 'profile' is
865
         * specified as UINT_MAX this will look for the base profile. */
866

867
        if (le16toh(pe_header->pe.NumberOfSections) == 0)
98✔
868
                return NULL;
869

870
        assert(sections);
98✔
871

872
        const IMAGE_SECTION_HEADER
98✔
873
                *p = sections,
98✔
874
                *e = sections + le16toh(pe_header->pe.NumberOfSections),
98✔
875
                *start = profile == UINT_MAX ? sections : NULL,
98✔
876
                *end;
877
        unsigned current_profile = UINT_MAX;
878

879
        for (;;) {
350✔
880
                p = pe_section_table_find(p, e - p, ".profile");
224✔
881
                if (!p) {
224✔
882
                        end = e;
883
                        break;
884
                }
885
                if (current_profile == profile) {
196✔
886
                        end = p;
887
                        break;
888
                }
889

890
                if (current_profile == UINT_MAX)
126✔
891
                        current_profile = 0;
892
                else
893
                        current_profile++;
70✔
894

895
                if (current_profile == profile)
126✔
896
                        start = p;
42✔
897

898
                p++; /* Continue scanning after the .profile entry we just found */
126✔
899
        }
900

901
        if (!start)
98✔
902
                return NULL;
903

904
        if (ret_n_sections)
84✔
905
                *ret_n_sections = end - start;
84✔
906

907
        return start;
908
}
909

910
static int trim_cmdline(char **cmdline) {
42✔
911
        assert(cmdline);
42✔
912

913
        /* Strips leading and trailing whitespace from command line */
914

915
        if (!*cmdline)
42✔
916
                return 0;
917

918
        const char *skipped = skip_leading_chars(*cmdline, WHITESPACE);
42✔
919

920
        if (isempty(skipped)) {
42✔
921
                *cmdline = mfree(*cmdline);
×
922
                return 0;
×
923
        }
924

925
        if (skipped != *cmdline) {
42✔
926
                _cleanup_free_ char *c = strdup(skipped);
×
927
                if (!c)
×
928
                        return -ENOMEM;
×
929

930
                free_and_replace(*cmdline, c);
×
931
        }
932

933
        delete_trailing_chars(*cmdline, WHITESPACE);
42✔
934
        return 1;
42✔
935
}
936

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

941
static int pe_find_uki_sections(
56✔
942
                int fd,
943
                const char *path,
944
                unsigned profile,
945
                char **ret_osrelease,
946
                char **ret_profile,
947
                char **ret_cmdline) {
948

949
        _cleanup_free_ char *osrelease_text = NULL, *profile_text = NULL, *cmdline_text = NULL;
×
950
        _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
×
951
        _cleanup_free_ PeHeader *pe_header = NULL;
56✔
952
        int r;
56✔
953

954
        assert(fd >= 0);
56✔
955
        assert(path);
56✔
956
        assert(profile != UINT_MAX);
56✔
957
        assert(ret_osrelease);
56✔
958
        assert(ret_profile);
56✔
959
        assert(ret_cmdline);
56✔
960

961
        r = pe_load_headers_and_sections(fd, path, &sections, &pe_header);
56✔
962
        if (r < 0)
56✔
963
                return r;
964

965
        if (!pe_is_uki(pe_header, sections))
56✔
966
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parsed PE file '%s' is not a UKI.", path);
×
967

968
        if (!pe_is_native(pe_header)) /* Don't process non-native UKIs */
56✔
969
                goto nothing;
×
970

971
        /* Find part of the section table for this profile */
972
        size_t n_psections = 0;
56✔
973
        const IMAGE_SECTION_HEADER *psections = pe_find_profile_section_table(pe_header, sections, profile, &n_psections);
56✔
974
        if (!psections && profile != 0) /* Profile not found? (Profile @0 needs no explicit .profile!) */
56✔
975
                goto nothing;
14✔
976

977
        /* Find base profile part of section table */
978
        size_t n_bsections;
42✔
979
        const IMAGE_SECTION_HEADER *bsections = ASSERT_PTR(pe_find_profile_section_table(pe_header, sections, UINT_MAX, &n_bsections));
42✔
980

981
        struct {
42✔
982
                const char *name;
983
                char **data;
984
        } table[] = {
42✔
985
                { ".osrel",   &osrelease_text },
986
                { ".profile", &profile_text   },
987
                { ".cmdline", &cmdline_text   },
988
        };
989

990
        FOREACH_ELEMENT(t, table) {
168✔
991
                const IMAGE_SECTION_HEADER *found;
126✔
992

993
                /* First look in the profile part of the section table, and if we don't find anything there, look into the base part */
994
                found = pe_section_table_find(psections, n_psections, t->name);
126✔
995
                if (!found) {
126✔
996
                        found = pe_section_table_find(bsections, n_bsections, t->name);
56✔
997
                        if (!found)
56✔
998
                                continue;
×
999
                }
1000

1001
                /* Permit "masking" of sections in the base profile */
1002
                if (found->VirtualSize == 0)
126✔
1003
                        continue;
×
1004

1005
                r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) t->data, /* ret_data= */ NULL);
126✔
1006
                if (r < 0)
126✔
1007
                        return log_error_errno(r, "Failed to load contents of section '%s': %m", t->name);
×
1008
        }
1009

1010
        if (!osrelease_text)
42✔
1011
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unified kernel image lacks .osrel data for profile @%u, refusing.", profile);
×
1012

1013
        if (trim_cmdline(&cmdline_text) < 0)
42✔
1014
                return log_oom();
×
1015

1016
        *ret_osrelease = TAKE_PTR(osrelease_text);
42✔
1017
        *ret_profile = TAKE_PTR(profile_text);
42✔
1018
        *ret_cmdline = TAKE_PTR(cmdline_text);
42✔
1019
        return 1;
42✔
1020

1021
nothing:
14✔
1022
        *ret_osrelease = *ret_profile = *ret_cmdline = NULL;
14✔
1023
        return 0;
14✔
1024
}
1025

1026
static int pe_find_addon_sections(
×
1027
                int fd,
1028
                const char *path,
1029
                char **ret_cmdline) {
1030

1031
        _cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL;
×
1032
        _cleanup_free_ PeHeader *pe_header = NULL;
×
1033
        int r;
×
1034

1035
        assert(fd >= 0);
×
1036
        assert(path);
×
1037

1038
        r = pe_load_headers_and_sections(fd, path, &sections, &pe_header);
×
1039
        if (r < 0)
×
1040
                return r;
1041

1042
        if (!pe_is_addon(pe_header, sections))
×
1043
                return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Parse PE file '%s' is not an add-on.", path);
×
1044

1045
        /* Define early, before the gotos below */
1046
        _cleanup_free_ char *cmdline_text = NULL;
×
1047

1048
        if (!pe_is_native(pe_header))
×
1049
                goto nothing;
×
1050

1051
        const IMAGE_SECTION_HEADER *found = pe_section_table_find(sections, le16toh(pe_header->pe.NumberOfSections), ".cmdline");
×
1052
        if (!found)
×
1053
                goto nothing;
×
1054

1055
        r = pe_read_section_data(fd, found, PE_SECTION_SIZE_MAX, (void**) &cmdline_text, /* ret_size= */ NULL);
×
1056
        if (r < 0)
×
1057
                return log_error_errno(r, "Failed to load contents of section '.cmdline': %m");
×
1058

1059
        if (trim_cmdline(&cmdline_text) < 0)
×
1060
                return log_oom();
×
1061

1062
        *ret_cmdline = TAKE_PTR(cmdline_text);
×
1063
        return 1;
×
1064

1065
nothing:
×
1066
        *ret_cmdline = NULL;
×
1067
        return 0;
×
1068
}
1069

1070
static int insert_boot_entry_addon(
×
1071
                BootEntryAddons *addons,
1072
                char *location,
1073
                char *cmdline) {
1074

1075
        assert(addons);
×
1076

1077
        if (!GREEDY_REALLOC(addons->items, addons->n_items + 1))
×
1078
                return log_oom();
×
1079

1080
        addons->items[addons->n_items++] = (BootEntryAddon) {
×
1081
                .location = location,
1082
                .cmdline = cmdline,
1083
        };
1084

1085
        return 0;
×
1086
}
1087

1088
static int boot_entries_find_unified_addons(
86✔
1089
                BootConfig *config,
1090
                int d_fd,
1091
                const char *addon_dir,
1092
                const char *root,
1093
                BootEntryAddons *ret_addons) {
1094

1095
        _cleanup_closedir_ DIR *d = NULL;
86✔
1096
        _cleanup_free_ char *full = NULL;
86✔
1097
        _cleanup_(boot_entry_addons_done) BootEntryAddons addons = {};
86✔
1098
        int r;
86✔
1099

1100
        assert(ret_addons);
86✔
1101
        assert(config);
86✔
1102

1103
        r = chase_and_opendirat(d_fd, addon_dir, CHASE_AT_RESOLVE_IN_ROOT, &full, &d);
86✔
1104
        if (r == -ENOENT)
86✔
1105
                return 0;
1106
        if (r < 0)
×
1107
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, addon_dir);
×
1108

1109
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) {
×
1110
                _cleanup_free_ char *j = NULL, *cmdline = NULL, *location = NULL;
×
1111
                _cleanup_close_ int fd = -EBADF;
×
1112

1113
                if (!dirent_is_file(de))
×
1114
                        continue;
×
1115

1116
                if (!endswith_no_case(de->d_name, ".addon.efi"))
×
1117
                        continue;
×
1118

1119
                fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY);
×
1120
                if (fd < 0) {
×
1121
                        log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
1122
                        continue;
×
1123
                }
1124

1125
                r = config_check_inode_relevant_and_unseen(config, fd, de->d_name);
×
1126
                if (r < 0)
×
1127
                        return r;
1128
                if (r == 0) /* inode already seen or otherwise not relevant */
×
1129
                        continue;
×
1130

1131
                j = path_join(full, de->d_name);
×
1132
                if (!j)
×
1133
                        return log_oom();
×
1134

1135
                if (pe_find_addon_sections(fd, j, &cmdline) <= 0)
×
1136
                        continue;
×
1137

1138
                location = strdup(j);
×
1139
                if (!location)
×
1140
                        return log_oom();
×
1141

1142
                r = insert_boot_entry_addon(&addons, location, cmdline);
×
1143
                if (r < 0)
×
1144
                        return r;
1145

1146
                TAKE_PTR(location);
×
1147
                TAKE_PTR(cmdline);
×
1148
        }
1149

1150
        *ret_addons = TAKE_STRUCT(addons);
×
1151
        return 0;
×
1152
}
1153

1154
static int boot_entries_find_unified_global_addons(
44✔
1155
                BootConfig *config,
1156
                const char *root,
1157
                const char *d_name,
1158
                BootEntryAddons *ret_addons) {
1159

1160
        int r;
44✔
1161
        _cleanup_closedir_ DIR *d = NULL;
44✔
1162

1163
        assert(ret_addons);
44✔
1164

1165
        r = chase_and_opendir(root, NULL, CHASE_PROHIBIT_SYMLINKS, NULL, &d);
44✔
1166
        if (r == -ENOENT)
44✔
1167
                return 0;
1168
        if (r < 0)
44✔
1169
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, d_name);
×
1170

1171
        return boot_entries_find_unified_addons(config, dirfd(d), d_name, root, ret_addons);
44✔
1172
}
1173

1174
static int boot_entries_find_unified_local_addons(
42✔
1175
                BootConfig *config,
1176
                int d_fd,
1177
                const char *d_name,
1178
                const char *root,
1179
                BootEntry *ret) {
1180

1181
        _cleanup_free_ char *addon_dir = NULL;
42✔
1182

1183
        assert(ret);
42✔
1184

1185
        addon_dir = strjoin(d_name, ".extra.d");
42✔
1186
        if (!addon_dir)
42✔
1187
                return log_oom();
×
1188

1189
        return boot_entries_find_unified_addons(config, d_fd, addon_dir, root, &ret->local_addons);
42✔
1190
}
1191

1192
static int boot_entries_find_unified(
44✔
1193
                BootConfig *config,
1194
                const char *root,
1195
                BootEntrySource source,
1196
                const char *dir) {
1197

1198
        _cleanup_closedir_ DIR *d = NULL;
44✔
1199
        _cleanup_free_ char *full = NULL;
44✔
1200
        int r;
44✔
1201

1202
        assert(config);
44✔
1203
        assert(dir);
44✔
1204

1205
        r = chase_and_opendir(dir, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, &full, &d);
44✔
1206
        if (r == -ENOENT)
44✔
1207
                return 0;
1208
        if (r < 0)
28✔
1209
                return log_error_errno(r, "Failed to open '%s/%s': %m", root, dir);
×
1210

1211
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read %s: %m", full)) {
98✔
1212
                if (!dirent_is_file(de))
14✔
1213
                        continue;
×
1214

1215
                if (!endswith_no_case(de->d_name, ".efi"))
14✔
1216
                        continue;
×
1217

1218
                _cleanup_close_ int fd = openat(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_NOCTTY);
128✔
1219
                if (fd < 0) {
14✔
1220
                        log_warning_errno(errno, "Failed to open %s/%s, ignoring: %m", full, de->d_name);
×
1221
                        continue;
×
1222
                }
1223

1224
                r = config_check_inode_relevant_and_unseen(config, fd, de->d_name);
14✔
1225
                if (r < 0)
14✔
1226
                        return r;
1227
                if (r == 0) /* inode already seen or otherwise not relevant */
14✔
1228
                        continue;
×
1229

1230
                _cleanup_free_ char *j = path_join(full, de->d_name);
28✔
1231
                if (!j)
14✔
1232
                        return log_oom();
×
1233

1234
                for (unsigned p = 0; p < UNIFIED_PROFILES_MAX; p++) {
56✔
1235
                        _cleanup_free_ char *osrelease = NULL, *profile = NULL, *cmdline = NULL;
56✔
1236

1237
                        r = pe_find_uki_sections(fd, j, p, &osrelease, &profile, &cmdline);
56✔
1238
                        if (r == 0) /* this profile does not exist, we are done */
56✔
1239
                                break;
1240
                        if (r < 0)
42✔
1241
                                continue;
×
1242

1243
                        if (!GREEDY_REALLOC(config->entries, config->n_entries + 1))
42✔
1244
                                return log_oom();
×
1245

1246
                        BootEntry *entry = config->entries + config->n_entries;
42✔
1247

1248
                        if (boot_entry_load_unified(root, source, j, p, osrelease, profile, cmdline, entry) < 0)
42✔
1249
                                continue;
×
1250

1251
                        /* look for .efi.extra.d */
1252
                        (void) boot_entries_find_unified_local_addons(config, dirfd(d), de->d_name, full, entry);
42✔
1253

1254
                        /* Set up the backpointer, so that we can find the global addons */
1255
                        entry->global_addons = &config->global_addons[source];
42✔
1256

1257
                        config->n_entries++;
42✔
1258
                }
1259
        }
1260

1261
        return 0;
1262
}
1263

1264
static bool find_nonunique(const BootEntry *entries, size_t n_entries, bool arr[]) {
47✔
1265
        bool non_unique = false;
47✔
1266

1267
        assert(entries || n_entries == 0);
47✔
1268
        assert(arr || n_entries == 0);
47✔
1269

1270
        for (size_t i = 0; i < n_entries; i++)
191✔
1271
                arr[i] = false;
144✔
1272

1273
        for (size_t i = 0; i < n_entries; i++)
191✔
1274
                for (size_t j = 0; j < n_entries; j++)
606✔
1275
                        if (i != j && streq(boot_entry_title(entries + i),
462✔
1276
                                            boot_entry_title(entries + j)))
1277
                                non_unique = arr[i] = arr[j] = true;
94✔
1278

1279
        return non_unique;
47✔
1280
}
1281

1282
static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
30✔
1283
        _cleanup_free_ bool *arr = NULL;
30✔
1284
        char *s;
30✔
1285

1286
        assert(entries || n_entries == 0);
30✔
1287

1288
        if (n_entries == 0)
30✔
1289
                return 0;
1290

1291
        arr = new(bool, n_entries);
16✔
1292
        if (!arr)
16✔
1293
                return -ENOMEM;
1294

1295
        /* Find _all_ non-unique titles */
1296
        if (!find_nonunique(entries, n_entries, arr))
16✔
1297
                return 0;
1298

1299
        /* Add version to non-unique titles */
1300
        for (size_t i = 0; i < n_entries; i++)
66✔
1301
                if (arr[i] && entries[i].version) {
50✔
1302
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].version) < 0)
34✔
1303
                                return -ENOMEM;
1304

1305
                        free_and_replace(entries[i].show_title, s);
34✔
1306
                }
1307

1308
        if (!find_nonunique(entries, n_entries, arr))
16✔
1309
                return 0;
1310

1311
        /* Add machine-id to non-unique titles */
1312
        for (size_t i = 0; i < n_entries; i++)
59✔
1313
                if (arr[i] && entries[i].machine_id) {
44✔
1314
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].machine_id) < 0)
2✔
1315
                                return -ENOMEM;
1316

1317
                        free_and_replace(entries[i].show_title, s);
2✔
1318
                }
1319

1320
        if (!find_nonunique(entries, n_entries, arr))
15✔
1321
                return 0;
1322

1323
        /* Add file name to non-unique titles */
1324
        for (size_t i = 0; i < n_entries; i++)
59✔
1325
                if (arr[i]) {
44✔
1326
                        if (asprintf(&s, "%s (%s)", boot_entry_title(entries + i), entries[i].id) < 0)
30✔
1327
                                return -ENOMEM;
1328

1329
                        free_and_replace(entries[i].show_title, s);
30✔
1330
                }
1331

1332
        return 0;
1333
}
1334

1335
static int boot_config_find(const BootConfig *config, const char *id) {
9✔
1336
        assert(config);
9✔
1337

1338
        if (!id)
9✔
1339
                return -1;
1340

1341
        if (id[0] == '@') {
9✔
1342
                if (!strcaseeq(id, "@saved"))
×
1343
                        return -1;
1344
                if (!config->entry_selected)
×
1345
                        return -1;
1346
                id = config->entry_selected;
1347
        }
1348

1349
        for (size_t i = 0; i < config->n_entries; i++)
26✔
1350
                if (fnmatch(id, config->entries[i].id, FNM_CASEFOLD) == 0)
26✔
1351
                        return i;
9✔
1352

1353
        return -1;
1354
}
1355

1356
static int boot_entries_select_default(const BootConfig *config) {
28✔
1357
        int i;
28✔
1358

1359
        assert(config);
28✔
1360
        assert(config->entries || config->n_entries == 0);
28✔
1361

1362
        if (config->n_entries == 0) {
28✔
1363
                log_debug("Found no default boot entry :(");
14✔
1364
                return -1; /* -1 means "no default" */
14✔
1365
        }
1366

1367
        if (config->entry_oneshot) {
14✔
1368
                i = boot_config_find(config, config->entry_oneshot);
×
1369
                if (i >= 0) {
×
1370
                        log_debug("Found default: id \"%s\" is matched by LoaderEntryOneShot",
×
1371
                                  config->entries[i].id);
1372
                        return i;
×
1373
                }
1374
        }
1375

1376
        if (config->entry_default) {
14✔
1377
                i = boot_config_find(config, config->entry_default);
3✔
1378
                if (i >= 0) {
3✔
1379
                        log_debug("Found default: id \"%s\" is matched by LoaderEntryDefault",
3✔
1380
                                  config->entries[i].id);
1381
                        return i;
3✔
1382
                }
1383
        }
1384

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

1394
        log_debug("Found default: first entry \"%s\"", config->entries[0].id);
11✔
1395
        return 0;
1396
}
1397

1398
static int boot_entries_select_selected(const BootConfig *config) {
28✔
1399
        assert(config);
28✔
1400
        assert(config->entries || config->n_entries == 0);
28✔
1401

1402
        if (!config->entry_selected || config->n_entries == 0)
28✔
1403
                return -1;
1404

1405
        return boot_config_find(config, config->entry_selected);
6✔
1406
}
1407

1408
static int boot_load_efi_entry_pointers(BootConfig *config, bool skip_efivars) {
28✔
1409
        int r;
28✔
1410

1411
        assert(config);
28✔
1412

1413
        if (skip_efivars || !is_efi_boot())
28✔
1414
                return 0;
22✔
1415

1416
        /* Loads the three "pointers" to boot loader entries from their EFI variables */
1417

1418
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), &config->entry_oneshot);
6✔
1419
        if (r == -ENOMEM)
6✔
1420
                return log_oom();
×
1421
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
6✔
1422
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryOneShot\", ignoring: %m");
×
1423

1424
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"), &config->entry_default);
6✔
1425
        if (r == -ENOMEM)
6✔
1426
                return log_oom();
×
1427
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
6✔
1428
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntryDefault\", ignoring: %m");
×
1429

1430
        r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntrySelected"), &config->entry_selected);
6✔
1431
        if (r == -ENOMEM)
6✔
1432
                return log_oom();
×
1433
        if (r < 0 && !IN_SET(r, -ENOENT, -ENODATA))
6✔
1434
                log_warning_errno(r, "Failed to read EFI variable \"LoaderEntrySelected\", ignoring: %m");
×
1435

1436
        return 1;
1437
}
1438

1439
int boot_config_select_special_entries(BootConfig *config, bool skip_efivars) {
28✔
1440
        int r;
28✔
1441

1442
        assert(config);
28✔
1443

1444
        r = boot_load_efi_entry_pointers(config, skip_efivars);
28✔
1445
        if (r < 0)
28✔
1446
                return r;
1447

1448
        config->default_entry = boot_entries_select_default(config);
28✔
1449
        config->selected_entry = boot_entries_select_selected(config);
28✔
1450

1451
        return 0;
28✔
1452
}
1453

1454
int boot_config_finalize(BootConfig *config) {
30✔
1455
        int r;
30✔
1456

1457
        typesafe_qsort(config->entries, config->n_entries, boot_entry_compare);
30✔
1458

1459
        r = boot_entries_uniquify(config->entries, config->n_entries);
30✔
1460
        if (r < 0)
30✔
1461
                return log_error_errno(r, "Failed to uniquify boot entries: %m");
×
1462

1463
        return 0;
1464
}
1465

1466
int boot_config_load(
30✔
1467
                BootConfig *config,
1468
                const char *esp_path,
1469
                const char *xbootldr_path) {
1470

1471
        int r;
30✔
1472

1473
        assert(config);
30✔
1474

1475
        if (esp_path) {
30✔
1476
                r = boot_loader_read_conf_path(config, esp_path, "/loader/loader.conf");
30✔
1477
                if (r < 0)
30✔
1478
                        return r;
1479

1480
                r = boot_entries_find_type1(config, esp_path, BOOT_ENTRY_ESP, "/loader/entries");
30✔
1481
                if (r < 0)
30✔
1482
                        return r;
1483

1484
                r = boot_entries_find_unified(config, esp_path, BOOT_ENTRY_ESP, "/EFI/Linux/");
30✔
1485
                if (r < 0)
30✔
1486
                        return r;
1487

1488
                r = boot_entries_find_unified_global_addons(config, esp_path, "/loader/addons/",
30✔
1489
                                                            &config->global_addons[BOOT_ENTRY_ESP]);
1490
                if (r < 0)
30✔
1491
                        return r;
1492
        }
1493

1494
        if (xbootldr_path) {
30✔
1495
                r = boot_entries_find_type1(config, xbootldr_path, BOOT_ENTRY_XBOOTLDR, "/loader/entries");
14✔
1496
                if (r < 0)
14✔
1497
                        return r;
1498

1499
                r = boot_entries_find_unified(config, xbootldr_path, BOOT_ENTRY_XBOOTLDR, "/EFI/Linux/");
14✔
1500
                if (r < 0)
14✔
1501
                        return r;
1502

1503
                r = boot_entries_find_unified_global_addons(config, xbootldr_path, "/loader/addons/",
14✔
1504
                                                            &config->global_addons[BOOT_ENTRY_XBOOTLDR]);
1505
                if (r < 0)
14✔
1506
                        return r;
1507
        }
1508

1509
        return boot_config_finalize(config);
30✔
1510
}
1511

1512
int boot_config_load_auto(
×
1513
                BootConfig *config,
1514
                const char *override_esp_path,
1515
                const char *override_xbootldr_path) {
1516

1517
        _cleanup_free_ char *esp_where = NULL, *xbootldr_where = NULL;
×
1518
        dev_t esp_devid = 0, xbootldr_devid = 0;
×
1519
        int r;
×
1520

1521
        assert(config);
×
1522

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

1530
        if (!override_esp_path && !override_xbootldr_path) {
×
1531
                if (access("/run/boot-loader-entries/", F_OK) >= 0)
×
1532
                        return boot_config_load(config, "/run/boot-loader-entries/", NULL);
×
1533

1534
                if (errno != ENOENT)
×
1535
                        return log_error_errno(errno,
×
1536
                                               "Failed to determine whether /run/boot-loader-entries/ exists: %m");
1537
        }
1538

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

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

1547
        /* If both paths actually refer to the same inode, suppress the xbootldr path */
1548
        if (esp_where && xbootldr_where && devnum_set_and_equal(esp_devid, xbootldr_devid))
×
1549
                xbootldr_where = mfree(xbootldr_where);
×
1550

1551
        return boot_config_load(config, esp_where, xbootldr_where);
×
1552
}
1553

1554
int boot_config_augment_from_loader(
6✔
1555
                BootConfig *config,
1556
                char **found_by_loader,
1557
                bool only_auto) {
1558

1559
        static const BootEntryAddons no_addons = (BootEntryAddons) {};
6✔
1560
        static const char *const title_table[] = {
6✔
1561
                /* Pretty names for a few well-known automatically discovered entries. */
1562
                "auto-osx",                      "macOS",
1563
                "auto-windows",                  "Windows Boot Manager",
1564
                "auto-efi-shell",                "EFI Shell",
1565
                "auto-efi-default",              "EFI Default Loader",
1566
                "auto-poweroff",                 "Power Off The System",
1567
                "auto-reboot",                   "Reboot The System",
1568
                "auto-reboot-to-firmware-setup", "Reboot Into Firmware Interface",
1569
                NULL,
1570
        };
1571

1572
        assert(config);
6✔
1573

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

1577
        STRV_FOREACH(i, found_by_loader) {
30✔
1578
                BootEntry *existing;
24✔
1579
                _cleanup_free_ char *c = NULL, *t = NULL, *p = NULL;
12✔
1580

1581
                existing = boot_config_find_entry(config, *i);
24✔
1582
                if (existing) {
24✔
1583
                        existing->reported_by_loader = true;
12✔
1584
                        continue;
12✔
1585
                }
1586

1587
                if (only_auto && !startswith(*i, "auto-"))
12✔
1588
                        continue;
×
1589

1590
                c = strdup(*i);
12✔
1591
                if (!c)
12✔
1592
                        return log_oom();
×
1593

1594
                STRV_FOREACH_PAIR(a, b, title_table)
90✔
1595
                        if (streq(*a, *i)) {
84✔
1596
                                t = strdup(*b);
6✔
1597
                                if (!t)
6✔
1598
                                        return log_oom();
×
1599
                                break;
1600
                        }
1601

1602
                p = strdup(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderEntries")));
12✔
1603
                if (!p)
12✔
1604
                        return log_oom();
×
1605

1606
                if (!GREEDY_REALLOC0(config->entries, config->n_entries + 1))
12✔
1607
                        return log_oom();
×
1608

1609
                config->entries[config->n_entries++] = (BootEntry) {
24✔
1610
                        .type = startswith(*i, "auto-") ? BOOT_ENTRY_LOADER_AUTO : BOOT_ENTRY_LOADER,
12✔
1611
                        .id = TAKE_PTR(c),
12✔
1612
                        .title = TAKE_PTR(t),
12✔
1613
                        .path = TAKE_PTR(p),
12✔
1614
                        .reported_by_loader = true,
1615
                        .tries_left = UINT_MAX,
1616
                        .tries_done = UINT_MAX,
1617
                        .global_addons = &no_addons,
1618
                };
1619
        }
1620

1621
        return 0;
1622
}
1623

1624
BootEntry* boot_config_find_entry(BootConfig *config, const char *id) {
28✔
1625
        assert(config);
28✔
1626
        assert(id);
28✔
1627

1628
        for (size_t j = 0; j < config->n_entries; j++)
91✔
1629
                if (strcaseeq_ptr(config->entries[j].id, id) ||
78✔
1630
                    strcaseeq_ptr(config->entries[j].id_old, id))
63✔
1631
                        return config->entries + j;
1632

1633
        return NULL;
1634
}
1635

1636
static void boot_entry_file_list(
15✔
1637
                const char *field,
1638
                const char *root,
1639
                const char *p,
1640
                int *ret_status) {
1641

1642
        assert(p);
15✔
1643
        assert(ret_status);
15✔
1644

1645
        int status = chase_and_access(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, F_OK, NULL);
15✔
1646

1647
        /* Note that this shows two '/' between the root and the file. This is intentional to highlight (in
1648
         * the absence of color support) to the user that the boot loader is only interested in the second
1649
         * part of the file. */
1650
        printf("%13s%s %s%s/%s", strempty(field), field ? ":" : " ", ansi_grey(), root, ansi_normal());
30✔
1651

1652
        if (status < 0) {
15✔
1653
                errno = -status;
×
1654
                printf("%s%s%s (%m)\n", ansi_highlight_red(), p, ansi_normal());
×
1655
        } else
1656
                printf("%s\n", p);
15✔
1657

1658
        if (*ret_status == 0 && status < 0)
15✔
1659
                *ret_status = status;
×
1660
}
15✔
1661

1662
static void print_addon(
×
1663
                BootEntryAddon *addon,
1664
                const char *addon_str) {
1665

1666
        printf("  %s: %s\n", addon_str, addon->location);
×
NEW
1667
        printf("      options: %s%s\n", glyph(GLYPH_TREE_RIGHT), addon->cmdline);
×
1668
}
×
1669

1670
static int indent_embedded_newlines(char *cmdline, char **ret_cmdline) {
15✔
1671
        _cleanup_free_ char *t = NULL;
15✔
1672
        _cleanup_strv_free_ char **ts = NULL;
15✔
1673

1674
        assert(ret_cmdline);
15✔
1675

1676
        ts = strv_split_newlines(cmdline);
15✔
1677
        if (!ts)
15✔
1678
                return -ENOMEM;
1679

1680
        t = strv_join(ts, "\n              ");
15✔
1681
        if (!t)
15✔
1682
                return -ENOMEM;
1683

1684
        *ret_cmdline = TAKE_PTR(t);
15✔
1685

1686
        return 0;
15✔
1687
}
1688

1689
static int print_cmdline(const BootEntry *e) {
15✔
1690

1691
        _cleanup_free_ char *options = NULL, *combined_cmdline = NULL, *t2 = NULL;
15✔
1692

1693
        assert(e);
15✔
1694

1695
        if (!strv_isempty(e->options)) {
15✔
1696
                _cleanup_free_ char *t = NULL;
15✔
1697

1698
                options = strv_join(e->options, " ");
15✔
1699
                if (!options)
15✔
1700
                        return log_oom();
×
1701

1702
                if (indent_embedded_newlines(options, &t) < 0)
15✔
1703
                        return log_oom();
×
1704

1705
                printf("      options: %s\n", t);
15✔
1706
                t2 = strdup(options);
15✔
1707
                if (!t2)
15✔
1708
                        return log_oom();
×
1709
        }
1710

1711
        FOREACH_ARRAY(addon, e->global_addons->items, e->global_addons->n_items) {
15✔
1712
                print_addon(addon, "global-addon");
×
1713
                if (!strextend(&t2, " ", addon->cmdline))
×
1714
                        return log_oom();
×
1715
        }
1716

1717
        FOREACH_ARRAY(addon, e->local_addons.items, e->local_addons.n_items) {
15✔
1718
                /* Add space at the beginning of addon_str to align it correctly */
1719
                print_addon(addon, " local-addon");
×
1720
                if (!strextend(&t2, " ", addon->cmdline))
×
1721
                        return log_oom();
×
1722
        }
1723

1724
        /* Don't print the combined cmdline if it's same as options. */
1725
        if (streq_ptr(t2, options))
15✔
1726
                return 0;
1727

1728
        if (indent_embedded_newlines(t2, &combined_cmdline) < 0)
×
1729
                return log_oom();
×
1730

1731
        if (combined_cmdline)
×
1732
                printf("      cmdline: %s\n", combined_cmdline);
×
1733

1734
        return 0;
1735
}
1736

1737
static int json_addon(
×
1738
                BootEntryAddon *addon,
1739
                const char *addon_str,
1740
                sd_json_variant **array) {
1741

1742
        int r;
×
1743

1744
        assert(addon);
×
1745
        assert(addon_str);
×
1746

1747
        r = sd_json_variant_append_arraybo(
×
1748
                        array,
1749
                        SD_JSON_BUILD_PAIR(addon_str, SD_JSON_BUILD_STRING(addon->location)),
1750
                        SD_JSON_BUILD_PAIR("options", SD_JSON_BUILD_STRING(addon->cmdline)));
1751
        if (r < 0)
×
1752
                return log_oom();
×
1753

1754
        return 0;
1755
}
1756

1757
static int json_cmdline(
9✔
1758
                const BootEntry *e,
1759
                const char *def_cmdline,
1760
                sd_json_variant **v) {
1761

1762
        _cleanup_free_ char *combined_cmdline = NULL;
9✔
1763
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *addons_array = NULL;
9✔
1764
        int r;
9✔
1765

1766
        assert(e);
9✔
1767

1768
        if (def_cmdline) {
9✔
1769
                combined_cmdline = strdup(def_cmdline);
9✔
1770
                if (!combined_cmdline)
9✔
1771
                        return log_oom();
×
1772
        }
1773

1774
        FOREACH_ARRAY(addon, e->global_addons->items, e->global_addons->n_items) {
9✔
1775
                r = json_addon(addon, "globalAddon", &addons_array);
×
1776
                if (r < 0)
×
1777
                        return r;
1778
                if (!strextend(&combined_cmdline, " ", addon->cmdline))
×
1779
                        return log_oom();
×
1780
        }
1781

1782
        FOREACH_ARRAY(addon, e->local_addons.items, e->local_addons.n_items) {
9✔
1783
                r = json_addon(addon, "localAddon", &addons_array);
×
1784
                if (r < 0)
×
1785
                        return r;
1786
                if (!strextend(&combined_cmdline, " ", addon->cmdline))
×
1787
                        return log_oom();
×
1788
        }
1789

1790
        r = sd_json_variant_merge_objectbo(
9✔
1791
                        v,
1792
                        SD_JSON_BUILD_PAIR("addons", SD_JSON_BUILD_VARIANT(addons_array)),
1793
                        SD_JSON_BUILD_PAIR_CONDITION(!!combined_cmdline, "cmdline", SD_JSON_BUILD_STRING(combined_cmdline)));
1794
        if (r < 0)
9✔
1795
                return log_oom();
×
1796
        return 0;
1797
}
1798

1799
int show_boot_entry(
15✔
1800
                const BootEntry *e,
1801
                bool show_as_default,
1802
                bool show_as_selected,
1803
                bool show_reported) {
1804

1805
        int status = 0, r = 0;
15✔
1806

1807
        /* Returns 0 on success, negative on processing error, and positive if something is wrong with the
1808
           boot entry itself. */
1809

1810
        assert(e);
15✔
1811

1812
        printf("         type: %s\n",
15✔
1813
               boot_entry_type_to_string(e->type));
15✔
1814

1815
        printf("        title: %s%s%s",
45✔
1816
               ansi_highlight(), boot_entry_title(e), ansi_normal());
1817

1818
        if (show_as_default)
15✔
1819
                printf(" %s(default)%s",
4✔
1820
                       ansi_highlight_green(), ansi_normal());
1821

1822
        if (show_as_selected)
15✔
1823
                printf(" %s(selected)%s",
×
1824
                       ansi_highlight_magenta(), ansi_normal());
1825

1826
        if (show_reported) {
15✔
1827
                if (e->type == BOOT_ENTRY_LOADER)
6✔
1828
                        printf(" %s(reported/absent)%s",
×
1829
                               ansi_highlight_red(), ansi_normal());
1830
                else if (!e->reported_by_loader && e->type != BOOT_ENTRY_LOADER_AUTO)
6✔
1831
                        printf(" %s(not reported/new)%s",
12✔
1832
                               ansi_highlight_green(), ansi_normal());
1833
        }
1834

1835
        putchar('\n');
15✔
1836

1837
        if (e->id) {
15✔
1838
                printf("           id: %s", e->id);
15✔
1839

1840
                if (e->id_without_profile && !streq_ptr(e->id, e->id_without_profile))
15✔
1841
                        printf(" (without profile: %s)\n", e->id_without_profile);
15✔
1842
                else
1843
                        putchar('\n');
×
1844
        }
1845
        if (e->path) {
15✔
1846
                _cleanup_free_ char *text = NULL, *link = NULL;
15✔
1847

1848
                const char *p = e->root ? path_startswith(e->path, e->root) : NULL;
15✔
1849
                if (p) {
15✔
1850
                        text = strjoin(ansi_grey(), e->root, "/", ansi_normal(), "/", p);
30✔
1851
                        if (!text)
15✔
1852
                                return log_oom();
×
1853
                }
1854

1855
                /* Let's urlify the link to make it easy to view in an editor, but only if it is a text
1856
                 * file. Unified images are binary ELFs, and EFI variables are not pure text either. */
1857
                if (e->type == BOOT_ENTRY_CONF)
15✔
1858
                        (void) terminal_urlify_path(e->path, text, &link);
×
1859

1860
                printf("       source: %s (on the %s)\n",
15✔
1861
                       link ?: text ?: e->path,
15✔
1862
                       boot_entry_source_to_string(e->source));
15✔
1863
        }
1864
        if (e->tries_left != UINT_MAX) {
15✔
1865
                printf("        tries: %u left", e->tries_left);
×
1866

1867
                if (e->tries_done != UINT_MAX)
×
1868
                        printf("; %u done\n", e->tries_done);
×
1869
                else
1870
                        putchar('\n');
×
1871
        }
1872

1873
        if (e->sort_key)
15✔
1874
                printf("     sort-key: %s\n", e->sort_key);
15✔
1875
        if (e->version)
15✔
1876
                printf("      version: %s\n", e->version);
15✔
1877
        if (e->machine_id)
15✔
1878
                printf("   machine-id: %s\n", e->machine_id);
×
1879
        if (e->architecture)
15✔
1880
                printf(" architecture: %s\n", e->architecture);
×
1881
        if (e->kernel)
15✔
1882
                boot_entry_file_list("linux", e->root, e->kernel, &status);
15✔
1883
        if (e->efi)
15✔
1884
                boot_entry_file_list("efi", e->root, e->efi, &status);
×
1885

1886
        STRV_FOREACH(s, e->initrd)
15✔
1887
                boot_entry_file_list(s == e->initrd ? "initrd" : NULL,
×
1888
                                     e->root,
×
1889
                                     *s,
1890
                                     &status);
1891

1892
        r = print_cmdline(e);
15✔
1893
        if (r < 0)
15✔
1894
                return r;
1895

1896
        if (e->device_tree)
15✔
1897
                boot_entry_file_list("devicetree", e->root, e->device_tree, &status);
×
1898

1899
        STRV_FOREACH(s, e->device_tree_overlay)
15✔
1900
                boot_entry_file_list(s == e->device_tree_overlay ? "devicetree-overlay" : NULL,
×
1901
                                     e->root,
×
1902
                                     *s,
1903
                                     &status);
1904

1905
        return -status;
15✔
1906
}
1907

1908
int boot_entry_to_json(const BootConfig *c, size_t i, sd_json_variant **ret) {
9✔
1909
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
9✔
1910
        _cleanup_free_ char *opts = NULL;
9✔
1911
        const BootEntry *e;
9✔
1912
        int r;
9✔
1913

1914
        assert(c);
9✔
1915
        assert(ret);
9✔
1916

1917
        if (i >= c->n_entries) {
9✔
1918
                *ret = NULL;
×
1919
                return 0;
×
1920
        }
1921

1922
        e = c->entries + i;
9✔
1923

1924
        if (!strv_isempty(e->options)) {
9✔
1925
                opts = strv_join(e->options, " ");
9✔
1926
                if (!opts)
9✔
1927
                        return log_oom();
×
1928
        }
1929

1930
        r = sd_json_variant_merge_objectbo(
9✔
1931
                        &v,
1932
                        SD_JSON_BUILD_PAIR("type", SD_JSON_BUILD_STRING(boot_entry_type_json_to_string(e->type))),
1933
                        SD_JSON_BUILD_PAIR("source", SD_JSON_BUILD_STRING(boot_entry_source_json_to_string(e->source))),
1934
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->id, "id", SD_JSON_BUILD_STRING(e->id)),
1935
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->path, "path", SD_JSON_BUILD_STRING(e->path)),
1936
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->root, "root", SD_JSON_BUILD_STRING(e->root)),
1937
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->title, "title", SD_JSON_BUILD_STRING(e->title)),
1938
                        SD_JSON_BUILD_PAIR_CONDITION(!!boot_entry_title(e), "showTitle", SD_JSON_BUILD_STRING(boot_entry_title(e))),
1939
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->sort_key, "sortKey", SD_JSON_BUILD_STRING(e->sort_key)),
1940
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->version, "version", SD_JSON_BUILD_STRING(e->version)),
1941
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->machine_id, "machineId", SD_JSON_BUILD_STRING(e->machine_id)),
1942
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->architecture, "architecture", SD_JSON_BUILD_STRING(e->architecture)),
1943
                        SD_JSON_BUILD_PAIR_CONDITION(!!opts, "options", SD_JSON_BUILD_STRING(opts)),
1944
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->kernel, "linux", SD_JSON_BUILD_STRING(e->kernel)),
1945
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->efi, "efi", SD_JSON_BUILD_STRING(e->efi)),
1946
                        SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->initrd), "initrd", SD_JSON_BUILD_STRV(e->initrd)),
1947
                        SD_JSON_BUILD_PAIR_CONDITION(!!e->device_tree, "devicetree", SD_JSON_BUILD_STRING(e->device_tree)),
1948
                        SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(e->device_tree_overlay), "devicetreeOverlay", SD_JSON_BUILD_STRV(e->device_tree_overlay)));
1949
        if (r < 0)
9✔
1950
                return log_oom();
×
1951

1952
        /* Sanitizers (only memory sanitizer?) do not like function call with too many
1953
         * arguments and trigger false positive warnings. Let's not add too many json objects
1954
         * at once. */
1955
        r = sd_json_variant_merge_objectbo(
9✔
1956
                        &v,
1957
                        SD_JSON_BUILD_PAIR("isReported", SD_JSON_BUILD_BOOLEAN(e->reported_by_loader)),
1958
                        SD_JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", SD_JSON_BUILD_UNSIGNED(e->tries_left)),
1959
                        SD_JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", SD_JSON_BUILD_UNSIGNED(e->tries_done)),
1960
                        SD_JSON_BUILD_PAIR_CONDITION(c->default_entry >= 0, "isDefault", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->default_entry)),
1961
                        SD_JSON_BUILD_PAIR_CONDITION(c->selected_entry >= 0, "isSelected", SD_JSON_BUILD_BOOLEAN(i == (size_t) c->selected_entry)));
1962
        if (r < 0)
9✔
1963
                return log_oom();
×
1964

1965
        r = json_cmdline(e, opts, &v);
9✔
1966
        if (r < 0)
9✔
1967
                return log_oom();
×
1968

1969
        *ret = TAKE_PTR(v);
9✔
1970
        return 1;
9✔
1971
}
1972

1973
int show_boot_entries(const BootConfig *config, sd_json_format_flags_t json_format) {
8✔
1974
        int r;
8✔
1975

1976
        assert(config);
8✔
1977

1978
        if (sd_json_format_enabled(json_format)) {
8✔
1979
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL;
6✔
1980

1981
                for (size_t i = 0; i < config->n_entries; i++) {
12✔
1982
                        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
6✔
1983

1984
                        r = boot_entry_to_json(config, i, &v);
6✔
1985
                        if (r < 0)
6✔
1986
                                return log_oom();
×
1987

1988
                        r = sd_json_variant_append_array(&array, v);
6✔
1989
                        if (r < 0)
6✔
1990
                                return log_oom();
×
1991
                }
1992

1993
                return sd_json_variant_dump(array, json_format | SD_JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
6✔
1994
        } else
1995
                for (size_t n = 0; n < config->n_entries; n++) {
8✔
1996
                        r = show_boot_entry(
12✔
1997
                                        config->entries + n,
6✔
1998
                                        /* show_as_default= */  n == (size_t) config->default_entry,
6✔
1999
                                        /* show_as_selected= */ n == (size_t) config->selected_entry,
6✔
2000
                                        /* show_discovered= */  true);
2001
                        if (r < 0)
6✔
2002
                                return r;
2003

2004
                        if (n+1 < config->n_entries)
6✔
2005
                                putchar('\n');
4✔
2006
                }
2007

2008
        return 0;
2009
}
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