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

systemd / systemd / 21192089104

20 Jan 2026 11:35PM UTC coverage: 72.524% (-0.3%) from 72.818%
21192089104

push

github

yuwata
mkdir: reset mtime *after* fchown()

Follow-up for 34c3d5747

Also, drop pointless shortcut.

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

2960 existing lines in 48 files now uncovered.

309808 of 427181 relevant lines covered (72.52%)

1236537.64 hits per line

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

81.09
/src/bootctl/bootctl-install.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

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

6
#include "alloc-util.h"
7
#include "boot-entry.h"
8
#include "bootctl.h"
9
#include "bootctl-install.h"
10
#include "bootctl-random-seed.h"
11
#include "bootctl-util.h"
12
#include "chase.h"
13
#include "copy.h"
14
#include "dirent-util.h"
15
#include "efi-api.h"
16
#include "efi-fundamental.h"
17
#include "efivars.h"
18
#include "env-file.h"
19
#include "fd-util.h"
20
#include "fileio.h"
21
#include "fs-util.h"
22
#include "glyph-util.h"
23
#include "id128-util.h"
24
#include "install-file.h"
25
#include "io-util.h"
26
#include "kernel-config.h"
27
#include "log.h"
28
#include "openssl-util.h"
29
#include "parse-argument.h"
30
#include "path-util.h"
31
#include "pe-binary.h"
32
#include "rm-rf.h"
33
#include "stat-util.h"
34
#include "string-util.h"
35
#include "strv.h"
36
#include "sync-util.h"
37
#include "time-util.h"
38
#include "tmpfile-util.h"
39
#include "umask-util.h"
40
#include "utf8.h"
41

42
static int load_etc_machine_id(void) {
155✔
43
        int r;
155✔
44

45
        r = sd_id128_get_machine(&arg_machine_id);
155✔
46
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Not set or empty */
155✔
47
                return 0;
48
        if (r < 0)
155✔
49
                return log_error_errno(r, "Failed to get machine-id: %m");
×
50

51
        log_debug("Loaded machine ID %s from /etc/machine-id.", SD_ID128_TO_STRING(arg_machine_id));
155✔
52
        return 0;
155✔
53
}
54

55
static int load_etc_machine_info(void) {
155✔
56
        /* systemd v250 added support to store the kernel-install layout setting and the machine ID to use
57
         * for setting up the ESP in /etc/machine-info. The newer /etc/kernel/entry-token file, as well as
58
         * the $layout field in /etc/kernel/install.conf are better replacements for this though, hence this
59
         * has been deprecated and is only returned for compatibility. */
60
        _cleanup_free_ char *p = NULL, *s = NULL, *layout = NULL;
155✔
61
        int r;
155✔
62

63
        p = path_join(arg_root, "/etc/machine-info");
155✔
64
        if (!p)
155✔
65
                return log_oom();
×
66

67
        r = parse_env_file(NULL, p,
155✔
68
                           "KERNEL_INSTALL_LAYOUT", &layout,
69
                           "KERNEL_INSTALL_MACHINE_ID", &s);
70
        if (r == -ENOENT)
155✔
71
                return 0;
72
        if (r < 0)
×
73
                return log_error_errno(r, "Failed to parse /etc/machine-info: %m");
×
74

75
        if (!isempty(s)) {
×
76
                if (!arg_quiet)
×
77
                        log_notice("Read $KERNEL_INSTALL_MACHINE_ID from /etc/machine-info. "
×
78
                                   "Please move it to /etc/kernel/entry-token.");
79

80
                r = sd_id128_from_string(s, &arg_machine_id);
×
81
                if (r < 0)
×
82
                        return log_error_errno(r, "Failed to parse KERNEL_INSTALL_MACHINE_ID=%s in /etc/machine-info: %m", s);
×
83

84
                log_debug("Loaded KERNEL_INSTALL_MACHINE_ID=%s from /etc/machine-info.",
×
85
                          SD_ID128_TO_STRING(arg_machine_id));
86
        }
87

88
        if (!isempty(layout)) {
155✔
89
                if (!arg_quiet)
×
90
                        log_notice("Read $KERNEL_INSTALL_LAYOUT from /etc/machine-info. "
×
91
                                   "Please move it to the layout= setting of /etc/kernel/install.conf.");
92

93
                log_debug("KERNEL_INSTALL_LAYOUT=%s is specified in /etc/machine-info.", layout);
×
94
                free_and_replace(arg_install_layout, layout);
×
95
        }
96

97
        return 0;
98
}
99

100
static int load_kernel_install_layout(void) {
155✔
101
        _cleanup_free_ char *layout = NULL;
155✔
102
        int r;
155✔
103

104
        r = load_kernel_install_conf(arg_root,
155✔
105
                                     secure_getenv("KERNEL_INSTALL_CONF_ROOT"),
155✔
106
                                     /* ret_machine_id= */ NULL,
107
                                     /* ret_boot_root= */ NULL,
108
                                     &layout,
109
                                     /* ret_initrd_generator= */ NULL,
110
                                     /* ret_uki_generator= */ NULL);
111
        if (r <= 0)
155✔
112
                return r;
113

114
        if (!isempty(layout)) {
155✔
115
                log_debug("layout=%s is specified in config.", layout);
×
116
                free_and_replace(arg_install_layout, layout);
×
117
        }
118

119
        return 0;
120
}
121

122
static bool use_boot_loader_spec_type1(void) {
155✔
123
        /* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
124
         * Specification Type #1 is the chosen format for our boot loader entries */
125
        return !arg_install_layout || streq(arg_install_layout, "bls");
155✔
126
}
127

128
static int settle_make_entry_directory(void) {
155✔
129
        int r;
155✔
130

131
        r = load_etc_machine_id();
155✔
132
        if (r < 0)
155✔
133
                return r;
134

135
        r = load_etc_machine_info();
155✔
136
        if (r < 0)
155✔
137
                return r;
138

139
        r = load_kernel_install_layout();
155✔
140
        if (r < 0)
155✔
141
                return r;
142

143
        r = settle_entry_token();
155✔
144
        if (r < 0)
155✔
145
                return r;
146

147
        bool layout_type1 = use_boot_loader_spec_type1();
155✔
148
        if (arg_make_entry_directory < 0) { /* Automatic mode */
155✔
149
                if (layout_type1) {
×
150
                        if (arg_entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID) {
×
151
                                r = path_is_temporary_fs("/etc/machine-id");
×
152
                                if (r < 0)
×
153
                                        return log_debug_errno(r, "Couldn't determine whether /etc/machine-id is on a temporary file system: %m");
×
154

155
                                arg_make_entry_directory = r == 0;
×
156
                        } else
157
                                arg_make_entry_directory = true;
×
158
                } else
159
                        arg_make_entry_directory = false;
×
160
        }
161

162
        if (arg_make_entry_directory > 0 && !layout_type1)
155✔
163
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
164
                                       "KERNEL_INSTALL_LAYOUT=%s is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
165
                                       arg_install_layout);
166

167
        return 0;
168
}
169

170
static int compare_product(const char *a, const char *b) {
258✔
171
        size_t x, y;
258✔
172

173
        assert(a);
258✔
174
        assert(b);
258✔
175

176
        x = strcspn(a, " ");
258✔
177
        y = strcspn(b, " ");
258✔
178
        if (x != y)
258✔
179
                return x < y ? -1 : x > y ? 1 : 0;
×
180

181
        return strncmp(a, b, x);
258✔
182
}
183

184
static int compare_version(const char *a, const char *b) {
258✔
185
        assert(a);
258✔
186
        assert(b);
258✔
187

188
        a += strcspn(a, " ");
258✔
189
        a += strspn(a, " ");
258✔
190
        b += strcspn(b, " ");
258✔
191
        b += strspn(b, " ");
258✔
192

193
        return strverscmp_improved(a, b);
258✔
194
}
195

196
static int version_check(int fd_from, const char *from, int fd_to, const char *to) {
258✔
197
        _cleanup_free_ char *a = NULL, *b = NULL;
258✔
198
        int r;
258✔
199

200
        assert(fd_from >= 0);
258✔
201
        assert(from);
258✔
202
        assert(fd_to >= 0);
258✔
203
        assert(to);
258✔
204

205
        r = get_file_version(fd_from, &a);
258✔
206
        if (r == -ESRCH)
258✔
207
                return log_notice_errno(r, "Source file \"%s\" does not carry version information!", from);
×
208
        if (r < 0)
258✔
209
                return r;
210

211
        r = get_file_version(fd_to, &b);
258✔
212
        if (r == -ESRCH)
258✔
213
                return log_info_errno(r, "Skipping \"%s\", it's owned by another boot loader (no version info found).", to);
×
214
        if (r < 0)
258✔
215
                return r;
216
        if (compare_product(a, b) != 0)
258✔
217
                return log_info_errno(SYNTHETIC_ERRNO(ESRCH),
×
218
                                      "Skipping \"%s\", it's owned by another boot loader.", to);
219

220
        r = compare_version(a, b);
258✔
221
        log_debug("Comparing versions: \"%s\" %s \"%s\"", a, comparison_operator(r), b);
504✔
222
        if (r < 0)
258✔
223
                return log_warning_errno(SYNTHETIC_ERRNO(ESTALE),
×
224
                                         "Skipping \"%s\", newer boot loader version in place already.", to);
225
        if (r == 0)
258✔
226
                return log_info_errno(SYNTHETIC_ERRNO(ESTALE),
258✔
227
                                      "Skipping \"%s\", same boot loader version in place already.", to);
228

229
        return 0;
230
}
231

232
static int copy_file_with_version_check(const char *from, const char *to, bool force) {
296✔
233
        _cleanup_close_ int fd_from = -EBADF, fd_to = -EBADF;
296✔
234
        _cleanup_free_ char *t = NULL;
296✔
235
        int r;
296✔
236

237
        fd_from = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
296✔
238
        if (fd_from < 0)
296✔
239
                return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", from);
×
240

241
        if (!force) {
296✔
242
                fd_to = open(to, O_RDONLY|O_CLOEXEC|O_NOCTTY);
258✔
243
                if (fd_to < 0) {
258✔
244
                        if (errno != ENOENT)
×
245
                                return log_error_errno(errno, "Failed to open \"%s\" for reading: %m", to);
×
246
                } else {
247
                        r = version_check(fd_from, from, fd_to, to);
258✔
248
                        if (r < 0)
258✔
249
                                return r;
250

251
                        if (lseek(fd_from, 0, SEEK_SET) < 0)
×
252
                                return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
×
253

254
                        fd_to = safe_close(fd_to);
×
255
                }
256
        }
257

258
        r = tempfn_random(to, NULL, &t);
38✔
259
        if (r < 0)
38✔
260
                return log_oom();
×
261

262
        WITH_UMASK(0000) {
76✔
263
                fd_to = open(t, O_WRONLY|O_CREAT|O_CLOEXEC|O_EXCL|O_NOFOLLOW, 0644);
38✔
264
                if (fd_to < 0)
38✔
265
                        return log_error_errno(errno, "Failed to open \"%s\" for writing: %m", t);
×
266
        }
267

268
        r = copy_bytes(fd_from, fd_to, UINT64_MAX, COPY_REFLINK);
38✔
269
        if (r < 0) {
38✔
270
                (void) unlink(t);
×
271
                return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
×
272
        }
273

274
        (void) copy_times(fd_from, fd_to, 0);
38✔
275

276
        r = fsync_full(fd_to);
38✔
277
        if (r < 0) {
38✔
278
                (void) unlink(t);
×
279
                return log_error_errno(r, "Failed to copy data from \"%s\" to \"%s\": %m", from, t);
×
280
        }
281

282
        r = RET_NERRNO(renameat(AT_FDCWD, t, AT_FDCWD, to));
38✔
283
        if (r < 0) {
×
284
                (void) unlink(t);
×
285
                return log_error_errno(r, "Failed to rename \"%s\" to \"%s\": %m", t, to);
×
286
        }
287

288
        log_info("Copied \"%s\" to \"%s\".", from, to);
38✔
289

290
        return 0;
291
}
292

293
static int mkdir_one(const char *prefix, const char *suffix) {
125✔
294
        _cleanup_free_ char *p = NULL;
125✔
295

296
        p = path_join(prefix, suffix);
125✔
297
        if (mkdir(p, 0700) < 0) {
125✔
298
                if (errno != EEXIST)
44✔
299
                        return log_error_errno(errno, "Failed to create \"%s\": %m", p);
×
300
        } else
301
                log_info("Created \"%s\".", p);
81✔
302

303
        return 0;
304
}
305

306
static const char *const esp_subdirs[] = {
307
        /* The directories to place in the ESP */
308
        "EFI",
309
        "EFI/systemd",
310
        "EFI/BOOT",
311
        "loader",
312
        "loader/keys",
313
        NULL
314
};
315

316
static const char *const dollar_boot_subdirs[] = {
317
        /* The directories to place in the XBOOTLDR partition or the ESP, depending what exists */
318
        "loader",
319
        "loader/entries",  /* Type #1 entries */
320
        "EFI",
321
        "EFI/Linux",       /* Type #2 entries */
322
        NULL
323
};
324

325
static int create_subdirs(const char *root, const char * const *subdirs) {
26✔
326
        int r;
26✔
327

328
        STRV_FOREACH(i, subdirs) {
143✔
329
                r = mkdir_one(root, *i);
117✔
330
                if (r < 0)
117✔
331
                        return r;
332
        }
333

334
        return 0;
335
}
336

337
static int update_efi_boot_binaries(
129✔
338
                const char *esp_path,
339
                const char *source_path,
340
                const char *ignore_filename) {
341

342
        _cleanup_closedir_ DIR *d = NULL;
129✔
343
        _cleanup_free_ char *p = NULL;
129✔
344
        int r, ret = 0;
129✔
345

346
        assert(esp_path);
129✔
347
        assert(source_path);
129✔
348

349
        r = chase_and_opendir("/EFI/BOOT", esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &p, &d);
129✔
350
        if (r == -ENOENT)
129✔
351
                return 0;
352
        if (r < 0)
129✔
353
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", esp_path);
×
354

355
        FOREACH_DIRENT(de, d, break) {
645✔
356
                _cleanup_close_ int fd = -EBADF;
645✔
357

358
                if (!endswith_no_case(de->d_name, ".efi"))
258✔
359
                        continue;
×
360

361
                if (strcaseeq_ptr(ignore_filename, de->d_name))
258✔
362
                        continue;
129✔
363

364
                fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ 0);
129✔
365
                if (fd < 0)
129✔
366
                        return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
×
367

368
                r = pe_is_native_fd(fd);
129✔
369
                if (r < 0) {
129✔
370
                        log_warning_errno(r, "Failed to detect if \"%s/%s\" is native architecture, ignoring: %m", p, de->d_name);
×
371
                        continue;
×
372
                }
373
                if (r == 0)
129✔
374
                        continue;
129✔
375

376
                _cleanup_free_ char *dest_path = path_join(p, de->d_name);
×
377
                if (!dest_path)
×
378
                        return log_oom();
×
379

380
                r = copy_file_with_version_check(source_path, dest_path, /* force= */ false);
×
381
                if (IN_SET(r, -ESTALE, -ESRCH))
×
382
                        continue;
×
383
                RET_GATHER(ret, r);
×
384
        }
385

386
        return ret;
387
}
388

389
static int copy_one_file(const char *esp_path, const char *name, bool force) {
148✔
390
        char *root = IN_SET(arg_install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE) ? arg_root : NULL;
148✔
391
        _cleanup_free_ char *source_path = NULL, *dest_path = NULL, *p = NULL, *q = NULL;
148✔
392
        const char *e;
148✔
393
        char *dest_name, *s;
148✔
394
        int r, ret;
148✔
395

396
        dest_name = strdupa_safe(name);
148✔
397
        s = endswith_no_case(dest_name, ".signed");
148✔
398
        if (s)
148✔
399
                *s = 0;
148✔
400

401
        p = path_join(BOOTLIBDIR, name);
148✔
402
        if (!p)
148✔
403
                return log_oom();
×
404

405
        r = chase(p, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &source_path, NULL);
148✔
406
        /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
407
        if (r == -ENOENT && root && arg_install_source == INSTALL_SOURCE_AUTO)
148✔
408
                r = chase(p, NULL, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &source_path, NULL);
16✔
409
        if (r < 0)
148✔
410
                return log_error_errno(r,
×
411
                                       "Failed to resolve path %s%s%s: %m",
412
                                       p,
413
                                       root ? " under directory " : "",
414
                                       strempty(root));
415

416
        q = path_join("/EFI/systemd/", dest_name);
148✔
417
        if (!q)
148✔
418
                return log_oom();
×
419

420
        r = chase(q, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_NONEXISTENT|CHASE_TRIGGER_AUTOFS, &dest_path, NULL);
148✔
421
        if (r < 0)
148✔
422
                return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", q, esp_path);
×
423

424
        /* Note that if this fails we do the second copy anyway, but return this error code,
425
         * so we stash it away in a separate variable. */
426
        ret = copy_file_with_version_check(source_path, dest_path, force);
148✔
427

428
        e = startswith(dest_name, "systemd-boot");
148✔
429
        if (e) {
148✔
430
                _cleanup_free_ char *default_dest_path = NULL;
148✔
431
                char *v;
148✔
432

433
                /* Create the EFI default boot loader name (specified for removable devices) */
434
                v = strjoina("/EFI/BOOT/BOOT", e);
740✔
435
                const char *boot_dot_efi = ascii_strupper(strrchr(v, '/') + 1);
148✔
436

437
                r = chase(v, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_NONEXISTENT|CHASE_TRIGGER_AUTOFS, &default_dest_path, NULL);
148✔
438
                if (r < 0)
148✔
439
                        return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", v, esp_path);
×
440

441
                RET_GATHER(ret, copy_file_with_version_check(source_path, default_dest_path, force));
148✔
442

443
                /* If we were installed under any other name in /EFI/BOOT/, make sure we update those binaries
444
                 * as well. */
445
                if (!force)
148✔
446
                        RET_GATHER(ret, update_efi_boot_binaries(esp_path, source_path, boot_dot_efi));
129✔
447
        }
448

449
        return ret;
450
}
451

452
static int install_binaries(const char *esp_path, const char *arch, bool force) {
142✔
453
        char *root = IN_SET(arg_install_source, INSTALL_SOURCE_AUTO, INSTALL_SOURCE_IMAGE) ? arg_root : NULL;
142✔
454
        _cleanup_closedir_ DIR *d = NULL;
142✔
455
        _cleanup_free_ char *path = NULL;
142✔
456
        int r;
142✔
457

458
        r = chase_and_opendir(BOOTLIBDIR, root, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &path, &d);
142✔
459
        /* If we had a root directory to try, we didn't find it and we are in auto mode, retry on the host */
460
        if (r == -ENOENT && root && arg_install_source == INSTALL_SOURCE_AUTO)
142✔
461
                r = chase_and_opendir(BOOTLIBDIR, NULL, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &path, &d);
12✔
462
        if (r == -ENOENT && arg_graceful() != ARG_GRACEFUL_NO) {
142✔
463
                log_debug("Source directory does not exist, ignoring.");
×
464
                return 0;
×
465
        }
466
        if (r < 0)
142✔
467
                return log_error_errno(r, "Failed to open boot loader directory %s%s: %m", strempty(root), BOOTLIBDIR);
×
468

469
        const char *suffix = strjoina(arch, ".efi");
710✔
470
        const char *suffix_signed = strjoina(arch, ".efi.signed");
710✔
471

472
        FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", path)) {
1,562✔
473
                int k;
1,136✔
474

475
                if (!endswith_no_case(de->d_name, suffix) && !endswith_no_case(de->d_name, suffix_signed))
1,136✔
476
                        continue;
840✔
477

478
                /* skip the .efi file, if there's a .signed version of it */
479
                if (endswith_no_case(de->d_name, ".efi")) {
296✔
480
                        _cleanup_free_ const char *s = strjoin(de->d_name, ".signed");
296✔
481
                        if (!s)
148✔
482
                                return log_oom();
×
483
                        if (faccessat(dirfd(d), s, F_OK, 0) >= 0)
148✔
484
                                continue;
148✔
485
                }
486

487
                k = copy_one_file(esp_path, de->d_name, force);
148✔
488
                /* Don't propagate an error code if no update necessary, installed version already equal or
489
                 * newer version, or other boot loader in place. */
490
                if (arg_graceful() != ARG_GRACEFUL_NO && IN_SET(k, -ESTALE, -ESRCH))
148✔
491
                        continue;
126✔
492
                RET_GATHER(r, k);
22✔
493
        }
494

495
        return r;
496
}
497

498
static int install_loader_config(const char *esp_path) {
13✔
499
        _cleanup_(unlink_and_freep) char *t = NULL;
×
500
        _cleanup_fclose_ FILE *f = NULL;
13✔
501
        _cleanup_free_ char *p = NULL;
13✔
502
        int r;
13✔
503

504
        assert(arg_make_entry_directory >= 0);
13✔
505

506
        p = path_join(esp_path, "/loader/loader.conf");
13✔
507
        if (!p)
13✔
508
                return log_oom();
×
509
        if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
13✔
510
                return 0;
511

512
        r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
11✔
513
        if (r < 0)
11✔
514
                return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
×
515

516
        fprintf(f, "#timeout 3\n"
11✔
517
                   "#console-mode keep\n");
518

519
        if (arg_make_entry_directory) {
11✔
520
                assert(arg_entry_token);
5✔
521
                fprintf(f, "default %s-*\n", arg_entry_token);
5✔
522
        }
523

524
        r = flink_tmpfile(f, t, p, LINK_TMPFILE_SYNC);
11✔
525
        if (r == -EEXIST)
11✔
526
                return 0; /* Silently skip creation if the file exists now (recheck) */
527
        if (r < 0)
11✔
528
                return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
×
529

530
        t = mfree(t);
11✔
531
        return 1;
11✔
532
}
533

534
static int install_loader_specification(const char *root) {
139✔
535
        _cleanup_(unlink_and_freep) char *t = NULL;
×
536
        _cleanup_fclose_ FILE *f = NULL;
139✔
537
        _cleanup_free_ char *p = NULL;
139✔
538
        int r;
139✔
539

540
        p = path_join(root, "/loader/entries.srel");
139✔
541
        if (!p)
139✔
542
                return log_oom();
×
543

544
        if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
139✔
545
                return 0;
546

547
        r = fopen_tmpfile_linkable(p, O_WRONLY|O_CLOEXEC, &t, &f);
11✔
548
        if (r < 0)
11✔
549
                return log_error_errno(r, "Failed to open \"%s\" for writing: %m", p);
×
550

551
        fprintf(f, "type1\n");
11✔
552

553
        r = flink_tmpfile(f, t, p, LINK_TMPFILE_SYNC);
11✔
554
        if (r == -EEXIST)
11✔
555
                return 0; /* Silently skip creation if the file exists now (recheck) */
556
        if (r < 0)
11✔
557
                return log_error_errno(r, "Failed to move \"%s\" into place: %m", p);
×
558

559
        t = mfree(t);
11✔
560
        return 1;
11✔
561
}
562

563
static int install_entry_directory(const char *root) {
13✔
564
        assert(root);
13✔
565
        assert(arg_make_entry_directory >= 0);
13✔
566

567
        if (!arg_make_entry_directory)
13✔
568
                return 0;
569

570
        assert(arg_entry_token);
7✔
571
        return mkdir_one(root, arg_entry_token);
7✔
572
}
573

574
static int install_entry_token(void) {
13✔
575
        _cleanup_free_ char* p = NULL;
13✔
576
        int r;
13✔
577

578
        assert(arg_make_entry_directory >= 0);
13✔
579
        assert(arg_entry_token);
13✔
580

581
        /* Let's save the used entry token in /etc/kernel/entry-token if we used it to create the entry
582
         * directory, or if anything else but the machine ID */
583

584
        if (!arg_make_entry_directory && arg_entry_token_type == BOOT_ENTRY_TOKEN_MACHINE_ID)
13✔
585
                return 0;
586

587
        p = path_join(arg_root, secure_getenv("KERNEL_INSTALL_CONF_ROOT") ?: "/etc/kernel/", "entry-token");
26✔
588
        if (!p)
13✔
589
                return log_oom();
×
590

591
        r = write_string_file(p, arg_entry_token, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
13✔
592
        if (r < 0)
13✔
593
                return log_error_errno(r, "Failed to write entry token '%s' to %s: %m", arg_entry_token, p);
×
594

595
        return 0;
596
}
597

598
#if HAVE_OPENSSL
599
static int efi_timestamp(EFI_TIME *ret) {
1✔
600
        struct tm tm = {};
1✔
601
        int r;
1✔
602

603
        assert(ret);
1✔
604

605
        r = localtime_or_gmtime_usec(source_date_epoch_or_now(), /* utc= */ true, &tm);
1✔
606
        if (r < 0)
1✔
UNCOV
607
                return log_error_errno(r, "Failed to convert timestamp to calendar time: %m");
×
608

609
        *ret = (EFI_TIME) {
1✔
610
                .Year = 1900 + tm.tm_year,
1✔
611
                /* tm_mon starts at 0, EFI_TIME months start at 1. */
612
                .Month = tm.tm_mon + 1,
1✔
613
                .Day = tm.tm_mday,
1✔
614
                .Hour = tm.tm_hour,
1✔
615
                .Minute = tm.tm_min,
1✔
616
                .Second = tm.tm_sec,
1✔
617
        };
618

619
        return 0;
1✔
620
}
621

622
static int install_secure_boot_auto_enroll(const char *esp, X509 *certificate, EVP_PKEY *private_key) {
13✔
623
        int r;
13✔
624

625
        if (!arg_secure_boot_auto_enroll)
13✔
626
                return 0;
13✔
627

628
        _cleanup_free_ uint8_t *dercert = NULL;
1✔
629
        int dercertsz;
1✔
630
        dercertsz = i2d_X509(certificate, &dercert);
1✔
631
        if (dercertsz < 0)
1✔
UNCOV
632
                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert X.509 certificate to DER: %s",
×
633
                                       ERR_error_string(ERR_get_error(), NULL));
634

635
        r = mkdir_one(esp, "loader/keys/auto");
1✔
636
        if (r < 0)
1✔
637
                return r;
638

639
        _cleanup_close_ int keys_fd = chase_and_open("loader/keys/auto", esp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, O_DIRECTORY, NULL);
2✔
640
        if (keys_fd < 0)
1✔
UNCOV
641
                return log_error_errno(keys_fd, "Failed to chase loader/keys/auto in the ESP: %m");
×
642

643
        uint32_t siglistsz = offsetof(EFI_SIGNATURE_LIST, Signatures) + offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz;
1✔
644
        /* We use malloc0() to zero-initialize the SignatureOwner field of Signatures[0]. */
645
        _cleanup_free_ EFI_SIGNATURE_LIST *siglist = malloc0(siglistsz);
2✔
646
        if (!siglist)
1✔
UNCOV
647
                return log_oom();
×
648

649
        *siglist = (EFI_SIGNATURE_LIST) {
1✔
650
                .SignatureType = EFI_CERT_X509_GUID,
651
                .SignatureListSize = siglistsz,
652
                .SignatureSize = offsetof(EFI_SIGNATURE_DATA, SignatureData) + dercertsz,
1✔
653
        };
654

655
        memcpy(siglist->Signatures[0].SignatureData, dercert, dercertsz);
1✔
656

657
        EFI_TIME timestamp;
1✔
658
        r = efi_timestamp(&timestamp);
1✔
659
        if (r < 0)
1✔
660
                return r;
661

662
        uint32_t attrs =
1✔
663
                EFI_VARIABLE_NON_VOLATILE|
664
                EFI_VARIABLE_BOOTSERVICE_ACCESS|
665
                EFI_VARIABLE_RUNTIME_ACCESS|
666
                EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
667

668
        FOREACH_STRING(db, "PK", "KEK", "db") {
4✔
669
                _cleanup_(BIO_freep) BIO *bio = NULL;
3✔
670

671
                bio = BIO_new(BIO_s_mem());
3✔
672
                if (!bio)
3✔
UNCOV
673
                        return log_oom();
×
674

675
                _cleanup_free_ char16_t *db16 = utf8_to_utf16(db, SIZE_MAX);
6✔
676
                if (!db16)
3✔
UNCOV
677
                        return log_oom();
×
678

679
                /* Don't count the trailing NUL terminator. */
680
                if (BIO_write(bio, db16, char16_strsize(db16) - sizeof(char16_t)) < 0)
3✔
UNCOV
681
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable name to bio");
×
682

683
                EFI_GUID *guid = STR_IN_SET(db, "PK", "KEK") ? &(EFI_GUID) EFI_GLOBAL_VARIABLE : &(EFI_GUID) EFI_IMAGE_SECURITY_DATABASE_GUID;
3✔
684

685
                if (BIO_write(bio, guid, sizeof(*guid)) < 0)
3✔
686
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable GUID to bio");
×
687

688
                if (BIO_write(bio, &attrs, sizeof(attrs)) < 0)
3✔
UNCOV
689
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write variable attributes to bio");
×
690

691
                if (BIO_write(bio, &timestamp, sizeof(timestamp)) < 0)
3✔
UNCOV
692
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write timestamp to bio");
×
693

694
                if (BIO_write(bio, siglist, siglistsz) < 0)
3✔
UNCOV
695
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write signature list to bio");
×
696

697
                _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
3✔
698
                p7 = PKCS7_sign(certificate, private_key, /* certs= */ NULL, bio, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSMIMECAP);
3✔
699
                if (!p7)
3✔
700
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
×
701
                                               ERR_error_string(ERR_get_error(), NULL));
702

UNCOV
703
                _cleanup_free_ uint8_t *sig = NULL;
×
704
                int sigsz = i2d_PKCS7(p7, &sig);
3✔
705
                if (sigsz < 0)
3✔
UNCOV
706
                        return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
×
707
                                               ERR_error_string(ERR_get_error(), NULL));
708

709
                size_t authsz = offsetof(EFI_VARIABLE_AUTHENTICATION_2, AuthInfo.CertData) + sigsz;
3✔
UNCOV
710
                _cleanup_free_ EFI_VARIABLE_AUTHENTICATION_2 *auth = malloc(authsz);
×
711
                if (!auth)
3✔
UNCOV
712
                        return log_oom();
×
713

714
                *auth = (EFI_VARIABLE_AUTHENTICATION_2) {
3✔
715
                        .TimeStamp = timestamp,
716
                        .AuthInfo = {
717
                                .Hdr = {
718
                                        .dwLength = offsetof(WIN_CERTIFICATE_UEFI_GUID, CertData) + sigsz,
3✔
719
                                        .wRevision = 0x0200,
720
                                        .wCertificateType = 0x0EF1, /* WIN_CERT_TYPE_EFI_GUID */
721
                                },
722
                                .CertType = EFI_CERT_TYPE_PKCS7_GUID,
723
                        }
724
                };
725

726
                memcpy(auth->AuthInfo.CertData, sig, sigsz);
3✔
727

728
                _cleanup_free_ char *filename = strjoin(db, ".auth");
6✔
729
                if (!filename)
3✔
UNCOV
730
                        return log_oom();
×
731

732
                _cleanup_close_ int fd = openat(keys_fd, filename, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600);
6✔
733
                if (fd < 0)
3✔
UNCOV
734
                        return log_error_errno(fd, "Failed to open secure boot auto-enrollment file for writing: %m");
×
735

736
                r = loop_write(fd, auth, authsz);
3✔
737
                if (r < 0)
3✔
UNCOV
738
                        return log_error_errno(r, "Failed to write authentication descriptor to secure boot auto-enrollment file: %m");
×
739

740
                r = loop_write(fd, siglist, siglistsz);
3✔
741
                if (r < 0)
3✔
UNCOV
742
                        return log_error_errno(r, "Failed to write signature list to secure boot auto-enrollment file: %m");
×
743

744
                if (fsync(fd) < 0 || fsync(keys_fd) < 0)
3✔
UNCOV
745
                        return log_error_errno(errno, "Failed to sync secure boot auto-enrollment file: %m");
×
746

747
                log_info("Secure boot auto-enrollment file %s/loader/keys/auto/%s successfully written.", esp, filename);
3✔
748
        }
749

750
        return 0;
1✔
751
}
752
#endif
753

754
static bool same_entry(uint16_t id, sd_id128_t uuid, const char *path) {
25✔
755
        _cleanup_free_ char *opath = NULL;
25✔
756
        sd_id128_t ouuid;
25✔
757
        int r;
25✔
758

759
        r = efi_get_boot_option(id, NULL, &ouuid, &opath, NULL);
25✔
760
        if (r < 0)
25✔
761
                return false;
762
        if (!sd_id128_equal(uuid, ouuid))
25✔
763
                return false;
21✔
764

765
        /* Some motherboards convert the path to uppercase under certain circumstances
766
         * (e.g. after booting into the Boot Menu in the ASUS ROG STRIX B350-F GAMING),
767
         * so use case-insensitive checking */
768
        if (!strcaseeq_ptr(path, opath))
4✔
UNCOV
769
                return false;
×
770

771
        return true;
772
}
773

774
static int find_slot(sd_id128_t uuid, const char *path, uint16_t *id) {
7✔
775
        _cleanup_free_ uint16_t *options = NULL;
7✔
776

777
        int n = efi_get_boot_options(&options);
7✔
778
        if (n < 0)
7✔
779
                return n;
780

781
        /* find already existing systemd-boot entry */
782
        for (int i = 0; i < n; i++)
28✔
783
                if (same_entry(options[i], uuid, path)) {
25✔
784
                        *id = options[i];
4✔
785
                        return 1;
4✔
786
                }
787

788
        /* find free slot in the sorted BootXXXX variable list */
789
        for (int i = 0; i < n; i++)
12✔
790
                if (i != options[i]) {
9✔
UNCOV
791
                        *id = i;
×
UNCOV
792
                        return 0;
×
793
                }
794

795
        /* use the next one */
796
        if (n == 0xffff)
3✔
797
                return -ENOSPC;
798
        *id = n;
3✔
799
        return 0;
3✔
800
}
801

802
static int insert_into_order(uint16_t slot, bool first) {
4✔
803
        _cleanup_free_ uint16_t *order = NULL;
4✔
804
        uint16_t *t;
4✔
805
        int n;
4✔
806

807
        n = efi_get_boot_order(&order);
4✔
808
        if (n <= 0)
4✔
809
                /* no entry, add us */
UNCOV
810
                return efi_set_boot_order(&slot, 1);
×
811

812
        /* are we the first and only one? */
813
        if (n == 1 && order[0] == slot)
4✔
814
                return 0;
815

816
        /* are we already in the boot order? */
817
        for (int i = 0; i < n; i++) {
13✔
818
                if (order[i] != slot)
10✔
819
                        continue;
9✔
820

821
                /* we do not require to be the first one, all is fine */
822
                if (!first)
1✔
823
                        return 0;
824

825
                /* move us to the first slot */
UNCOV
826
                memmove(order + 1, order, i * sizeof(uint16_t));
×
UNCOV
827
                order[0] = slot;
×
UNCOV
828
                return efi_set_boot_order(order, n);
×
829
        }
830

831
        /* extend array */
832
        t = reallocarray(order, n + 1, sizeof(uint16_t));
3✔
833
        if (!t)
3✔
834
                return -ENOMEM;
835
        order = t;
3✔
836

837
        /* add us to the top or end of the list */
838
        if (first) {
3✔
839
                memmove(order + 1, order, n * sizeof(uint16_t));
3✔
840
                order[0] = slot;
3✔
841
        } else
UNCOV
842
                order[n] = slot;
×
843

844
        return efi_set_boot_order(order, n + 1);
3✔
845
}
846

847
static int remove_from_order(uint16_t slot) {
3✔
848
        _cleanup_free_ uint16_t *order = NULL;
3✔
849
        int n;
3✔
850

851
        n = efi_get_boot_order(&order);
3✔
852
        if (n <= 0)
3✔
853
                return n;
854

855
        for (int i = 0; i < n; i++) {
3✔
856
                if (order[i] != slot)
3✔
UNCOV
857
                        continue;
×
858

859
                if (i + 1 < n)
3✔
860
                        memmove(order + i, order + i+1, (n - i) * sizeof(uint16_t));
3✔
861
                return efi_set_boot_order(order, n - 1);
3✔
862
        }
863

864
        return 0;
865
}
866

867
static const char *pick_efi_boot_option_description(void) {
6✔
868
        return arg_efi_boot_option_description ?: "Linux Boot Manager";
6✔
869
}
870

871
static int install_variables(
4✔
872
                const char *esp_path,
873
                uint32_t part,
874
                uint64_t pstart,
875
                uint64_t psize,
876
                sd_id128_t uuid,
877
                const char *path,
878
                bool first,
879
                bool graceful) {
880

881
        uint16_t slot;
4✔
882
        int r;
4✔
883

884
        r = chase_and_access(path, esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, F_OK, NULL);
4✔
885
        if (r == -ENOENT)
4✔
886
                return 0;
4✔
887
        if (r < 0)
4✔
UNCOV
888
                return log_error_errno(r, "Cannot access \"%s/%s\": %m", esp_path, skip_leading_slash(path));
×
889

890
        r = find_slot(uuid, path, &slot);
4✔
891
        if (r < 0) {
4✔
UNCOV
892
                int level = graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
893
                const char *skip = graceful ? ", skipping" : "";
894

UNCOV
895
                log_full_errno(level, r,
×
896
                               r == -ENOENT ?
897
                               "Failed to access EFI variables%s. Is the \"efivarfs\" filesystem mounted?" :
898
                               "Failed to determine current boot order%s: %m", skip);
899

900
                return graceful ? 0 : r;
×
901
        }
902

903
        bool existing = r > 0;
4✔
904

905
        if (first || !existing) {
4✔
906
                r = efi_add_boot_option(
3✔
907
                                slot,
908
                                pick_efi_boot_option_description(),
909
                                part,
910
                                pstart,
911
                                psize,
912
                                uuid,
913
                                path);
914
                if (r < 0) {
3✔
UNCOV
915
                        int level = graceful ? arg_quiet ? LOG_DEBUG : LOG_INFO : LOG_ERR;
×
916
                        const char *skip = graceful ? ", skipping" : "";
917

UNCOV
918
                        log_full_errno(level, r, "Failed to create EFI Boot variable entry%s: %m", skip);
×
919

920
                        return graceful ? 0 : r;
×
921
                }
922

923
                log_info("%s EFI boot entry \"%s\".",
6✔
924
                         existing ? "Updated" : "Created",
925
                         pick_efi_boot_option_description());
926
        }
927

928
        return insert_into_order(slot, first);
4✔
929
}
930

931
static int are_we_installed(const char *esp_path) {
141✔
932
        int r;
141✔
933

934
        /* Tests whether systemd-boot is installed. It's not obvious what to use as check here: we could
935
         * check EFI variables, we could check what binary /EFI/BOOT/BOOT*.EFI points to, or whether the
936
         * loader entries directory exists. Here we opted to check whether /EFI/systemd/ is non-empty, which
937
         * should be a suitable and very minimal check for a number of reasons:
938
         *
939
         *  → The check is architecture independent (i.e. we check if any systemd-boot loader is installed,
940
         *    not a specific one.)
941
         *
942
         *  → It doesn't assume we are the only boot loader (i.e doesn't check if we own the main
943
         *    /EFI/BOOT/BOOT*.EFI fallback binary.
944
         *
945
         *  → It specifically checks for systemd-boot, not for other boot loaders (which a check for
946
         *    /boot/loader/entries would do). */
947

948
        _cleanup_free_ char *p = path_join(esp_path, "/EFI/systemd/");
282✔
949
        if (!p)
141✔
UNCOV
950
                return log_oom();
×
951

952
        log_debug("Checking whether %s contains any files%s", p, glyph(GLYPH_ELLIPSIS));
264✔
953
        r = dir_is_empty(p, /* ignore_hidden_or_backup= */ false);
141✔
954
        if (r < 0 && r != -ENOENT)
141✔
955
                return log_error_errno(r, "Failed to check whether %s contains any files: %m", p);
×
956

957
        return r == 0;
141✔
958
}
959

960
#if HAVE_OPENSSL
961
static int load_secure_boot_auto_enroll(
142✔
962
                X509 **ret_certificate,
963
                EVP_PKEY **ret_private_key,
964
                OpenSSLAskPasswordUI **ret_ui) {
965

966
        int r;
142✔
967

968
        assert(ret_certificate);
142✔
969
        assert(ret_private_key);
142✔
970
        assert(ret_ui);
142✔
971

972
        if (!arg_secure_boot_auto_enroll) {
142✔
973
                *ret_certificate = NULL;
141✔
974
                *ret_private_key = NULL;
141✔
975
                return 0;
141✔
976
        }
977

978
        if (arg_certificate_source_type == OPENSSL_CERTIFICATE_SOURCE_FILE) {
1✔
979
                r = parse_path_argument(arg_certificate, /* suppress_root= */ false, &arg_certificate);
1✔
980
                if (r < 0)
1✔
981
                        return r;
982
        }
983

984
        _cleanup_(X509_freep) X509 *certificate = NULL;
142✔
985
        r = openssl_load_x509_certificate(
1✔
986
                        arg_certificate_source_type,
987
                        arg_certificate_source,
988
                        arg_certificate,
989
                        &certificate);
990
        if (r < 0)
1✔
UNCOV
991
                return log_error_errno(r, "Failed to load X.509 certificate from %s: %m", arg_certificate);
×
992

993
        if (arg_private_key_source_type == OPENSSL_KEY_SOURCE_FILE) {
1✔
994
                r = parse_path_argument(arg_private_key, /* suppress_root= */ false, &arg_private_key);
1✔
995
                if (r < 0)
1✔
996
                        return log_error_errno(r, "Failed to parse private key path %s: %m", arg_private_key);
×
997
        }
998

999
        r = openssl_load_private_key(
2✔
1000
                        arg_private_key_source_type,
1001
                        arg_private_key_source,
1002
                        arg_private_key,
1003
                        &(AskPasswordRequest) {
1✔
1004
                                .tty_fd = -EBADF,
1005
                                .id = "bootctl-private-key-pin",
1006
                                .keyring = arg_private_key,
1007
                                .credential = "bootctl.private-key-pin",
1008
                                .until = USEC_INFINITY,
1009
                                .hup_fd = -EBADF,
1010
                        },
1011
                        ret_private_key,
1012
                        ret_ui);
1013
        if (r < 0)
1✔
UNCOV
1014
                return log_error_errno(r, "Failed to load private key from %s: %m", arg_private_key);
×
1015

1016
        *ret_certificate = TAKE_PTR(certificate);
1✔
1017

1018
        return 0;
1✔
1019
}
1020
#endif
1021

1022
int verb_install(int argc, char *argv[], void *userdata) {
142✔
1023
        sd_id128_t uuid = SD_ID128_NULL;
142✔
1024
        uint64_t pstart = 0, psize = 0;
142✔
1025
        uint32_t part = 0;
142✔
1026
        bool install, graceful;
142✔
1027
        int r;
142✔
1028

1029
        /* Invoked for both "update" and "install" */
1030

1031
        install = streq(argv[0], "install");
142✔
1032

1033
        /* Support graceful mode only for updates, unless forcibly enabled in chroot environments */
1034
        graceful = arg_graceful() == ARG_GRACEFUL_FORCE || (!install && arg_graceful() != ARG_GRACEFUL_NO);
142✔
1035

1036
#if HAVE_OPENSSL
1037
        _cleanup_(openssl_ask_password_ui_freep) OpenSSLAskPasswordUI *ui = NULL;
142✔
1038
        _cleanup_(EVP_PKEY_freep) EVP_PKEY *private_key = NULL;
142✔
1039
        _cleanup_(X509_freep) X509 *certificate = NULL;
142✔
1040
        r = load_secure_boot_auto_enroll(&certificate, &private_key, &ui);
142✔
1041
        if (r < 0)
142✔
1042
                return r;
1043
#endif
1044

1045
        r = acquire_esp(/* unprivileged_mode= */ false, graceful, &part, &pstart, &psize, &uuid, NULL);
142✔
1046
        if (graceful && r == -ENOKEY)
142✔
1047
                return 0; /* If --graceful is specified and we can't find an ESP, handle this cleanly */
1048
        if (r < 0)
142✔
1049
                return r;
1050

1051
        if (!install) {
142✔
1052
                /* If we are updating, don't do anything if sd-boot wasn't actually installed. */
1053
                r = are_we_installed(arg_esp_path);
129✔
1054
                if (r < 0)
129✔
1055
                        return r;
1056
                if (r == 0) {
129✔
UNCOV
1057
                        log_debug("Skipping update because sd-boot is not installed in the ESP.");
×
UNCOV
1058
                        return 0;
×
1059
                }
1060
        }
1061

1062
        r = acquire_xbootldr(/* unprivileged_mode= */ false, NULL, NULL);
142✔
1063
        if (r < 0)
142✔
1064
                return r;
1065

1066
        r = settle_make_entry_directory();
142✔
1067
        if (r < 0)
142✔
1068
                return r;
1069

1070
        const char *arch = arg_arch_all ? "" : get_efi_arch();
142✔
1071

1072
        WITH_UMASK(0002) {
284✔
1073
                if (install) {
142✔
1074
                        /* Don't create any of these directories when we are just updating. When we update
1075
                         * we'll drop-in our files (unless there are newer ones already), but we won't create
1076
                         * the directories for them in the first place. */
1077
                        r = create_subdirs(arg_esp_path, esp_subdirs);
13✔
1078
                        if (r < 0)
13✔
1079
                                return r;
1080

1081
                        r = create_subdirs(arg_dollar_boot_path(), dollar_boot_subdirs);
18✔
1082
                        if (r < 0)
13✔
1083
                                return r;
1084
                }
1085

1086
                r = install_binaries(arg_esp_path, arch, install);
142✔
1087
                if (r < 0)
142✔
1088
                        return r;
1089

1090
                if (install) {
139✔
1091
                        r = install_loader_config(arg_esp_path);
13✔
1092
                        if (r < 0)
13✔
1093
                                return r;
1094

1095
                        r = install_entry_directory(arg_dollar_boot_path());
18✔
1096
                        if (r < 0)
13✔
1097
                                return r;
1098

1099
                        r = install_entry_token();
13✔
1100
                        if (r < 0)
13✔
1101
                                return r;
1102

1103
                        r = install_random_seed(arg_esp_path);
13✔
1104
                        if (r < 0)
13✔
1105
                                return r;
1106

1107
#if HAVE_OPENSSL
1108
                        r = install_secure_boot_auto_enroll(arg_esp_path, certificate, private_key);
13✔
1109
                        if (r < 0)
13✔
1110
                                return r;
1111
#endif
1112
                }
1113

1114
                r = install_loader_specification(arg_dollar_boot_path());
213✔
1115
                if (r < 0)
139✔
1116
                        return r;
1117
        }
1118

1119
        (void) sync_everything();
139✔
1120

1121
        if (!touch_variables())
139✔
1122
                return 0;
1123

1124
        if (arg_arch_all) {
6✔
1125
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1126
                return 0;
2✔
1127
        }
1128

1129
        char *path = strjoina("/EFI/systemd/systemd-boot", arch, ".efi");
28✔
1130
        return install_variables(arg_esp_path, part, pstart, psize, uuid, path, install, graceful);
4✔
1131
}
1132

1133
static int remove_boot_efi(const char *esp_path) {
13✔
1134
        _cleanup_closedir_ DIR *d = NULL;
13✔
1135
        _cleanup_free_ char *p = NULL;
13✔
1136
        int r, c = 0;
13✔
1137

1138
        r = chase_and_opendir("/EFI/BOOT", esp_path, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_TRIGGER_AUTOFS, &p, &d);
13✔
1139
        if (r == -ENOENT)
13✔
1140
                return 0;
1141
        if (r < 0)
13✔
UNCOV
1142
                return log_error_errno(r, "Failed to open directory \"%s/EFI/BOOT\": %m", esp_path);
×
1143

1144
        FOREACH_DIRENT(de, d, break) {
63✔
1145
                _cleanup_close_ int fd = -EBADF;
24✔
1146
                _cleanup_free_ char *v = NULL;
24✔
1147

1148
                if (!endswith_no_case(de->d_name, ".efi"))
24✔
UNCOV
1149
                        continue;
×
1150

1151
                fd = xopenat_full(dirfd(d), de->d_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, /* mode= */ 0);
24✔
1152
                if (fd < 0)
24✔
UNCOV
1153
                        return log_error_errno(fd, "Failed to open \"%s/%s\" for reading: %m", p, de->d_name);
×
1154

1155
                r = pe_is_native_fd(fd);
24✔
1156
                if (r < 0) {
24✔
UNCOV
1157
                        log_warning_errno(r, "Failed to detect if \"%s/%s\" is native architecture, ignoring: %m", p, de->d_name);
×
1158
                        continue;
×
1159
                }
1160
                if (r == 0)
24✔
1161
                        continue;
11✔
1162

1163
                r = get_file_version(fd, &v);
13✔
1164
                if (r == -ESRCH)
13✔
UNCOV
1165
                        continue;  /* No version information */
×
1166
                if (r < 0)
13✔
1167
                        return r;
1168
                if (startswith(v, "systemd-boot ")) {
13✔
1169
                        if (unlinkat(dirfd(d), de->d_name, 0) < 0)
13✔
1170
                                return log_error_errno(errno, "Failed to remove \"%s/%s\": %m", p, de->d_name);
×
1171

1172
                        log_info("Removed \"%s/%s\".", p, de->d_name);
13✔
1173
                }
1174

1175
                c++;
13✔
1176
        }
1177

1178
        return c;
1179
}
1180

1181
static int rmdir_one(const char *prefix, const char *suffix) {
172✔
1182
        _cleanup_free_ char *p = path_join(prefix, suffix);
344✔
1183
        if (!p)
172✔
UNCOV
1184
                return log_oom();
×
1185

1186
        if (rmdir(p) < 0) {
172✔
1187
                bool ignore = IN_SET(errno, ENOENT, ENOTEMPTY);
102✔
1188

1189
                log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, errno,
102✔
1190
                               "Failed to remove directory \"%s\": %m", p);
1191
                if (!ignore)
102✔
UNCOV
1192
                        return -errno;
×
1193
        } else
1194
                log_info("Removed \"%s\".", p);
70✔
1195

1196
        return 0;
1197
}
1198

1199
static int remove_subdirs(const char *root, const char *const *subdirs) {
183✔
1200
        int r;
183✔
1201

1202
        /* We use recursion here to destroy the directories in reverse order. Which should be safe given how
1203
         * short the array is. */
1204

1205
        if (!subdirs[0]) /* A the end of the list */
183✔
1206
                return 0;
183✔
1207

1208
        r = remove_subdirs(root, subdirs + 1);
149✔
1209
        return RET_GATHER(r, rmdir_one(root, subdirs[0]));
149✔
1210
}
1211

1212
static int remove_entry_directory(const char *root) {
21✔
1213
        assert(root);
21✔
1214
        assert(arg_make_entry_directory >= 0);
21✔
1215

1216
        if (!arg_make_entry_directory || !arg_entry_token)
21✔
1217
                return 0;
1218

1219
        return rmdir_one(root, arg_entry_token);
10✔
1220
}
1221

1222
static int remove_binaries(const char *esp_path) {
13✔
1223
        int r;
13✔
1224

1225
        _cleanup_free_ char *p = path_join(esp_path, "/EFI/systemd");
26✔
1226
        if (!p)
13✔
UNCOV
1227
                return log_oom();
×
1228

1229
        r = rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
13✔
1230
        return RET_GATHER(r, remove_boot_efi(esp_path));
13✔
1231
}
1232

1233
static int remove_file(const char *root, const char *file) {
86✔
1234
        assert(root);
86✔
1235
        assert(file);
86✔
1236

1237
        _cleanup_free_ char *p = path_join(root, file);
172✔
1238
        if (!p)
86✔
UNCOV
1239
                return log_oom();
×
1240

1241
        if (unlink(p) < 0) {
86✔
1242
                log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
41✔
1243
                               "Failed to unlink file \"%s\": %m", p);
1244

1245
                return errno == ENOENT ? 0 : -errno;
41✔
1246
        }
1247

1248
        log_info("Removed \"%s\".", p);
45✔
1249
        return 1;
1250
}
1251

1252
static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
3✔
1253
        uint16_t slot;
3✔
1254
        int r;
3✔
1255

1256
        r = find_slot(uuid, path, &slot);
3✔
1257
        if (r != 1)
3✔
1258
                return 0;
3✔
1259

1260
        r = efi_remove_boot_option(slot);
3✔
1261
        if (r < 0)
3✔
1262
                return r;
1263

1264
        if (in_order)
3✔
1265
                return remove_from_order(slot);
3✔
1266

1267
        return 0;
1268
}
1269

1270
static int remove_loader_variables(void) {
3✔
1271
        int r = 0;
3✔
1272

1273
        /* Remove all persistent loader variables we define */
1274

1275
        FOREACH_STRING(var,
27✔
1276
                       EFI_LOADER_VARIABLE_STR("LoaderConfigConsoleMode"),
1277
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeout"),
1278
                       EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"),
1279
                       EFI_LOADER_VARIABLE_STR("LoaderEntryDefault"),
1280
                       EFI_LOADER_VARIABLE_STR("LoaderEntrySysFail"),
1281
                       EFI_LOADER_VARIABLE_STR("LoaderEntryLastBooted"),
1282
                       EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"),
1283
                       EFI_LOADER_VARIABLE_STR("LoaderSystemToken")) {
1284

1285
                int q;
24✔
1286

1287
                q = efi_set_variable(var, NULL, 0);
24✔
1288
                if (q == -ENOENT)
24✔
1289
                        continue;
21✔
1290
                if (q < 0)
3✔
UNCOV
1291
                        RET_GATHER(r, log_warning_errno(q, "Failed to remove EFI variable %s: %m", var));
×
1292
                else
1293
                        log_info("Removed EFI variable %s.", var);
3✔
1294
        }
1295

1296
        return r;
3✔
1297
}
1298

1299
int verb_remove(int argc, char *argv[], void *userdata) {
13✔
1300
        sd_id128_t uuid = SD_ID128_NULL;
13✔
1301
        int r;
13✔
1302

1303
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false, NULL, NULL, NULL, &uuid, NULL);
13✔
1304
        if (r < 0)
13✔
1305
                return r;
13✔
1306

1307
        r = acquire_xbootldr(/* unprivileged_mode= */ false, NULL, NULL);
13✔
1308
        if (r < 0)
13✔
1309
                return r;
1310

1311
        r = settle_make_entry_directory();
13✔
1312
        if (r < 0)
13✔
1313
                return r;
1314

1315
        r = remove_binaries(arg_esp_path);
13✔
1316
        RET_GATHER(r, remove_file(arg_esp_path, "/loader/loader.conf"));
13✔
1317
        RET_GATHER(r, remove_file(arg_esp_path, "/loader/random-seed"));
13✔
1318
        RET_GATHER(r, remove_file(arg_esp_path, "/loader/entries.srel"));
13✔
1319

1320
        FOREACH_STRING(db, "PK.auth", "KEK.auth", "db.auth") {
52✔
1321
                _cleanup_free_ char *p = path_join("/loader/keys/auto", db);
78✔
1322
                if (!p)
39✔
UNCOV
1323
                        return log_oom();
×
1324

1325
                RET_GATHER(r, remove_file(arg_esp_path, p));
39✔
1326
        }
1327

1328
        RET_GATHER(r, rmdir_one(arg_esp_path, "/loader/keys/auto"));
13✔
1329
        RET_GATHER(r, remove_subdirs(arg_esp_path, esp_subdirs));
13✔
1330
        RET_GATHER(r, remove_subdirs(arg_esp_path, dollar_boot_subdirs));
13✔
1331
        RET_GATHER(r, remove_entry_directory(arg_esp_path));
13✔
1332

1333
        if (arg_xbootldr_path) {
13✔
1334
                /* Remove a subset of these also from the XBOOTLDR partition if it exists */
1335
                RET_GATHER(r, remove_file(arg_xbootldr_path, "/loader/entries.srel"));
8✔
1336
                RET_GATHER(r, remove_subdirs(arg_xbootldr_path, dollar_boot_subdirs));
8✔
1337
                RET_GATHER(r, remove_entry_directory(arg_xbootldr_path));
8✔
1338
        }
1339

1340
        (void) sync_everything();
13✔
1341

1342
        if (!touch_variables())
13✔
1343
                return r;
1344

1345
        if (arg_arch_all) {
5✔
1346
                log_info("Not changing EFI variables with --all-architectures.");
2✔
1347
                return r;
2✔
1348
        }
1349

1350
        char *path = strjoina("/EFI/systemd/systemd-boot", get_efi_arch(), ".efi");
21✔
1351
        RET_GATHER(r, remove_variables(uuid, path, /* in_order= */ true));
3✔
1352
        return RET_GATHER(r, remove_loader_variables());
3✔
1353
}
1354

1355
int verb_is_installed(int argc, char *argv[], void *userdata) {
12✔
1356
        int r;
12✔
1357

1358
        r = acquire_esp(/* unprivileged_mode= */ false,
12✔
1359
                        /* graceful= */ arg_graceful() != ARG_GRACEFUL_NO,
12✔
1360
                        NULL, NULL, NULL, NULL, NULL);
1361
        if (r < 0)
12✔
1362
                return r;
1363

1364
        r = are_we_installed(arg_esp_path);
12✔
1365
        if (r < 0)
12✔
1366
                return r;
1367

1368
        if (r > 0) {
12✔
1369
                if (!arg_quiet)
6✔
1370
                        puts("yes");
6✔
1371
                return EXIT_SUCCESS;
6✔
1372
        } else {
1373
                if (!arg_quiet)
6✔
1374
                        puts("no");
6✔
1375
                return EXIT_FAILURE;
6✔
1376
        }
1377
}
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