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

systemd / systemd / 13042844938

29 Jan 2025 04:17PM UTC coverage: 71.718% (+0.2%) from 71.53%
13042844938

push

github

bluca
tree-wide: remove some spurious newlines

Specifically, remove double newlines, and newlines before '}'.

292558 of 407928 relevant lines covered (71.72%)

710701.19 hits per line

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

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

3
#include <getopt.h>
4

5
#include "sd-varlink.h"
6

7
#include "blockdev-util.h"
8
#include "bootctl.h"
9
#include "bootctl-install.h"
10
#include "bootctl-random-seed.h"
11
#include "bootctl-reboot-to-firmware.h"
12
#include "bootctl-set-efivar.h"
13
#include "bootctl-status.h"
14
#include "bootctl-systemd-efi-options.h"
15
#include "bootctl-uki.h"
16
#include "build.h"
17
#include "devnum-util.h"
18
#include "dissect-image.h"
19
#include "efi-loader.h"
20
#include "escape.h"
21
#include "find-esp.h"
22
#include "main-func.h"
23
#include "mount-util.h"
24
#include "pager.h"
25
#include "parse-argument.h"
26
#include "path-util.h"
27
#include "pretty-print.h"
28
#include "utf8.h"
29
#include "varlink-io.systemd.BootControl.h"
30
#include "varlink-util.h"
31
#include "verbs.h"
32
#include "virt.h"
33

34
/* EFI_BOOT_OPTION_DESCRIPTION_MAX sets the maximum length for the boot option description
35
 * stored in NVRAM. The UEFI spec does not specify a minimum or maximum length for this
36
 * string, but we limit the length to something reasonable to prevent from the firmware
37
 * having to deal with a potentially too long string. */
38
#define EFI_BOOT_OPTION_DESCRIPTION_MAX ((size_t) 255)
39

40
char *arg_esp_path = NULL;
41
char *arg_xbootldr_path = NULL;
42
bool arg_print_esp_path = false;
43
bool arg_print_dollar_boot_path = false;
44
bool arg_print_loader_path = false;
45
bool arg_print_stub_path = false;
46
unsigned arg_print_root_device = 0;
47
bool arg_touch_variables = true;
48
bool arg_install_random_seed = true;
49
PagerFlags arg_pager_flags = 0;
50
bool arg_graceful = false;
51
bool arg_quiet = false;
52
int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */
53
sd_id128_t arg_machine_id = SD_ID128_NULL;
54
char *arg_install_layout = NULL;
55
BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
56
char *arg_entry_token = NULL;
57
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
58
bool arg_arch_all = false;
59
char *arg_root = NULL;
60
char *arg_image = NULL;
61
InstallSource arg_install_source = ARG_INSTALL_SOURCE_AUTO;
62
char *arg_efi_boot_option_description = NULL;
63
bool arg_dry_run = false;
64
ImagePolicy *arg_image_policy = NULL;
65
bool arg_varlink = false;
66
bool arg_secure_boot_auto_enroll = false;
67
char *arg_certificate = NULL;
68
CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE;
69
char *arg_certificate_source = NULL;
70
char *arg_private_key = NULL;
71
KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE;
72
char *arg_private_key_source = NULL;
73

74
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
253✔
75
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
253✔
76
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
253✔
77
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
253✔
78
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
253✔
79
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
253✔
80
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
253✔
81
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
253✔
82
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
253✔
83
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
253✔
84
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
253✔
85
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
253✔
86

87
int acquire_esp(
211✔
88
                int unprivileged_mode,
89
                bool graceful,
90
                uint32_t *ret_part,
91
                uint64_t *ret_pstart,
92
                uint64_t *ret_psize,
93
                sd_id128_t *ret_uuid,
94
                dev_t *ret_devid) {
95

96
        char *np;
211✔
97
        int r;
211✔
98

99
        /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on
100
         * its own, except for ENOKEY (which is good, we want to show our own message in that case,
101
         * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case
102
         * we simply eat up the error here, so that --list and --status work too, without noise about
103
         * this). */
104

105
        r = find_esp_and_warn(arg_root, arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
211✔
106
        if (r == -ENOKEY) {
211✔
107
                if (graceful)
5✔
108
                        return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
×
109
                                              "Couldn't find EFI system partition, skipping.");
110

111
                return log_error_errno(r,
5✔
112
                                       "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n"
113
                                       "Alternatively, use --esp-path= to specify path to mount point.");
114
        }
115
        if (r < 0)
206✔
116
                return r;
117

118
        free_and_replace(arg_esp_path, np);
205✔
119
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
205✔
120

121
        return 0;
122
}
123

124
int acquire_xbootldr(
183✔
125
                int unprivileged_mode,
126
                sd_id128_t *ret_uuid,
127
                dev_t *ret_devid) {
128

129
        char *np;
183✔
130
        int r;
183✔
131

132
        r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
183✔
133
        if (r == -ENOKEY) {
183✔
134
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
90✔
135
                arg_xbootldr_path = mfree(arg_xbootldr_path);
90✔
136

137
                if (ret_uuid)
90✔
138
                        *ret_uuid = SD_ID128_NULL;
12✔
139
                if (ret_devid)
90✔
140
                        *ret_devid = 0;
17✔
141
                return 0;
90✔
142
        }
143
        if (r < 0)
93✔
144
                return r;
145

146
        free_and_replace(arg_xbootldr_path, np);
92✔
147
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
92✔
148

149
        return 1;
150
}
151

152
static int print_loader_or_stub_path(void) {
6✔
153
        _cleanup_free_ char *p = NULL;
6✔
154
        sd_id128_t uuid;
6✔
155
        int r;
6✔
156

157
        if (arg_print_loader_path) {
6✔
158
                r = efi_loader_get_device_part_uuid(&uuid);
×
159
                if (r == -ENOENT)
×
160
                        return log_error_errno(r, "No loader partition UUID passed.");
×
161
                if (r < 0)
×
162
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
163

164
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
×
165
                if (r == -ENOENT)
×
166
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
167
                if (r < 0)
×
168
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
169
        } else {
170
                assert(arg_print_stub_path);
6✔
171

172
                r = efi_stub_get_device_part_uuid(&uuid);
6✔
173
                if (r == -ENOENT)
6✔
174
                        return log_error_errno(r, "No stub partition UUID passed.");
×
175
                if (r < 0)
6✔
176
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
177

178
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
6✔
179
                if (r == -ENOENT)
6✔
180
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
181
                if (r < 0)
6✔
182
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
183
        }
184

185
        sd_id128_t esp_uuid;
6✔
186
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false,
6✔
187
                        /* ret_part= */ NULL, /* ret_pstart= */ NULL, /* ret_psize= */ NULL,
188
                        &esp_uuid, /* ret_devid= */ NULL);
189
        if (r < 0)
6✔
190
                return r;
191

192
        const char *found_path = NULL;
6✔
193
        if (sd_id128_equal(esp_uuid, uuid))
6✔
194
                found_path = arg_esp_path;
6✔
195
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
196
                sd_id128_t xbootldr_uuid;
×
197

198
                r = acquire_xbootldr(/* unprivileged_mode= */ false, &xbootldr_uuid, /* ret_devid= */ NULL);
×
199
                if (r < 0)
×
200
                        return r;
×
201

202
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
203
                        found_path = arg_xbootldr_path;
×
204
        }
205

206
        if (!found_path)
6✔
207
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to discover partition " SD_ID128_FORMAT_STR " among mounted boot partitions.", SD_ID128_FORMAT_VAL(uuid));
×
208

209
        _cleanup_free_ char *j = path_join(found_path, p);
12✔
210
        if (!j)
6✔
211
                return log_oom();
×
212

213
        puts(j);
6✔
214
        return 0;
215
}
216

217
static int help(int argc, char *argv[], void *userdata) {
3✔
218
        _cleanup_free_ char *link = NULL;
3✔
219
        int r;
3✔
220

221
        pager_open(arg_pager_flags);
3✔
222

223
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
224
        if (r < 0)
3✔
225
                return log_oom();
×
226

227
        printf("%1$s [OPTIONS...] COMMAND ...\n"
6✔
228
               "\n%5$sControl EFI firmware boot settings and manage boot loader.%6$s\n"
229
               "\n%3$sGeneric EFI Firmware/Boot Loader Commands:%4$s\n"
230
               "  status               Show status of installed boot loader and EFI variables\n"
231
               "  reboot-to-firmware [BOOL]\n"
232
               "                       Query or set reboot-to-firmware EFI flag\n"
233
               "\n%3$sBoot Loader Specification Commands:%4$s\n"
234
               "  list                 List boot loader entries\n"
235
               "  unlink ID            Remove boot loader entry\n"
236
               "  cleanup              Remove files in ESP not referenced in any boot entry\n"
237
               "\n%3$sBoot Loader Interface Commands:%4$s\n"
238
               "  set-default ID       Set default boot loader entry\n"
239
               "  set-oneshot ID       Set default boot loader entry, for next boot only\n"
240
               "  set-timeout SECONDS  Set the menu timeout\n"
241
               "  set-timeout-oneshot SECONDS\n"
242
               "                       Set the menu timeout for the next boot only\n"
243
               "\n%3$ssystemd-boot Commands:%4$s\n"
244
               "  install              Install systemd-boot to the ESP and EFI variables\n"
245
               "  update               Update systemd-boot in the ESP and EFI variables\n"
246
               "  remove               Remove systemd-boot from the ESP and EFI variables\n"
247
               "  is-installed         Test whether systemd-boot is installed in the ESP\n"
248
               "  random-seed          Initialize or refresh random seed in ESP and EFI\n"
249
               "                       variables\n"
250
               "\n%3$sKernel Image Commands:%4$s\n"
251
               "  kernel-identify      Identify kernel image type\n"
252
               "  kernel-inspect       Prints details about the kernel image\n"
253
               "\n%3$sBlock Device Discovery Commands:%4$s\n"
254
               "  -p --print-esp-path  Print path to the EFI System Partition mount point\n"
255
               "  -x --print-boot-path Print path to the $BOOT partition mount point\n"
256
               "     --print-loader-path\n"
257
               "                       Print path to currently booted boot loader binary\n"
258
               "     --print-stub-path Print path to currently booted unified kernel binary\n"
259
               "  -R --print-root-device\n"
260
               "                       Print path to the block device node backing the\n"
261
               "                       root file system (returns e.g. /dev/nvme0n1p5)\n"
262
               "  -RR                  Print path to the whole disk block device node\n"
263
               "                       backing the root FS (returns e.g. /dev/nvme0n1)\n"
264
               "\n%3$sOptions:%4$s\n"
265
               "  -h --help            Show this help\n"
266
               "     --version         Print version\n"
267
               "     --esp-path=PATH   Path to the EFI System Partition (ESP)\n"
268
               "     --boot-path=PATH  Path to the $BOOT partition\n"
269
               "     --root=PATH       Operate on an alternate filesystem root\n"
270
               "     --image=PATH      Operate on disk image as filesystem root\n"
271
               "     --image-policy=POLICY\n"
272
               "                       Specify disk image dissection policy\n"
273
               "     --install-source=auto|image|host\n"
274
               "                       Where to pick files when using --root=/--image=\n"
275
               "     --no-variables    Don't touch EFI variables\n"
276
               "     --random-seed=yes|no\n"
277
               "                       Whether to create random-seed file during install\n"
278
               "     --no-pager        Do not pipe output into a pager\n"
279
               "     --graceful        Don't fail when the ESP cannot be found or EFI\n"
280
               "                       variables cannot be written\n"
281
               "  -q --quiet           Suppress output\n"
282
               "     --make-entry-directory=yes|no|auto\n"
283
               "                       Create $BOOT/ENTRY-TOKEN/ directory\n"
284
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
285
               "                       Entry token to use for this installation\n"
286
               "     --json=pretty|short|off\n"
287
               "                       Generate JSON output\n"
288
               "     --all-architectures\n"
289
               "                       Install all supported EFI architectures\n"
290
               "     --efi-boot-option-description=DESCRIPTION\n"
291
               "                       Description of the entry in the boot option list\n"
292
               "     --dry-run         Dry run (unlink and cleanup)\n"
293
               "     --secure-boot-auto-enroll\n"
294
               "                       Set up secure boot auto-enrollment\n"
295
               "     --private-key=PATH|URI\n"
296
               "                       Private key to use when setting up secure boot\n"
297
               "                       auto-enrollment or an engine or provider specific\n"
298
               "                       designation if --private-key-source= is used\n"
299
               "     --private-key-source=file|provider:PROVIDER|engine:ENGINE\n"
300
               "                       Specify how to use KEY for --private-key=. Allows\n"
301
               "                       an OpenSSL engine/provider to be used when setting\n"
302
               "                       up secure boot auto-enrollment\n"
303
               "     --certificate=PATH|URI\n"
304
               "                       PEM certificate to use when setting up Secure Boot\n"
305
               "                       auto-enrollment, or a provider specific designation\n"
306
               "                       if --certificate-source= is used\n"
307
               "     --certificate-source=file|provider:PROVIDER\n"
308
               "                       Specify how to interpret the certificate from\n"
309
               "                       --certificate=. Allows the certificate to be loaded\n"
310
               "                       from an OpenSSL provider\n"
311
               "\nSee the %2$s for details.\n",
312
               program_invocation_short_name,
313
               link,
314
               ansi_underline(),
315
               ansi_normal(),
316
               ansi_highlight(),
317
               ansi_normal());
318

319
        return 0;
320
}
321

322
static int parse_argv(int argc, char *argv[]) {
253✔
323
        enum {
253✔
324
                ARG_ESP_PATH = 0x100,
325
                ARG_BOOT_PATH,
326
                ARG_ROOT,
327
                ARG_IMAGE,
328
                ARG_IMAGE_POLICY,
329
                ARG_INSTALL_SOURCE,
330
                ARG_VERSION,
331
                ARG_NO_VARIABLES,
332
                ARG_RANDOM_SEED,
333
                ARG_NO_PAGER,
334
                ARG_GRACEFUL,
335
                ARG_MAKE_ENTRY_DIRECTORY,
336
                ARG_ENTRY_TOKEN,
337
                ARG_JSON,
338
                ARG_ARCH_ALL,
339
                ARG_EFI_BOOT_OPTION_DESCRIPTION,
340
                ARG_DRY_RUN,
341
                ARG_PRINT_LOADER_PATH,
342
                ARG_PRINT_STUB_PATH,
343
                ARG_SECURE_BOOT_AUTO_ENROLL,
344
                ARG_CERTIFICATE,
345
                ARG_CERTIFICATE_SOURCE,
346
                ARG_PRIVATE_KEY,
347
                ARG_PRIVATE_KEY_SOURCE,
348
        };
349

350
        static const struct option options[] = {
253✔
351
                { "help",                        no_argument,       NULL, 'h'                             },
352
                { "version",                     no_argument,       NULL, ARG_VERSION                     },
353
                { "esp-path",                    required_argument, NULL, ARG_ESP_PATH                    },
354
                { "path",                        required_argument, NULL, ARG_ESP_PATH                    }, /* Compatibility alias */
355
                { "boot-path",                   required_argument, NULL, ARG_BOOT_PATH                   },
356
                { "root",                        required_argument, NULL, ARG_ROOT                        },
357
                { "image",                       required_argument, NULL, ARG_IMAGE                       },
358
                { "image-policy",                required_argument, NULL, ARG_IMAGE_POLICY                },
359
                { "install-source",              required_argument, NULL, ARG_INSTALL_SOURCE              },
360
                { "print-esp-path",              no_argument,       NULL, 'p'                             },
361
                { "print-path",                  no_argument,       NULL, 'p'                             }, /* Compatibility alias */
362
                { "print-boot-path",             no_argument,       NULL, 'x'                             },
363
                { "print-loader-path",           no_argument,       NULL, ARG_PRINT_LOADER_PATH           },
364
                { "print-stub-path",             no_argument,       NULL, ARG_PRINT_STUB_PATH             },
365
                { "print-root-device",           no_argument,       NULL, 'R'                             },
366
                { "no-variables",                no_argument,       NULL, ARG_NO_VARIABLES                },
367
                { "random-seed",                 required_argument, NULL, ARG_RANDOM_SEED                 },
368
                { "no-pager",                    no_argument,       NULL, ARG_NO_PAGER                    },
369
                { "graceful",                    no_argument,       NULL, ARG_GRACEFUL                    },
370
                { "quiet",                       no_argument,       NULL, 'q'                             },
371
                { "make-entry-directory",        required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY        },
372
                { "make-machine-id-directory",   required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY        }, /* Compatibility alias */
373
                { "entry-token",                 required_argument, NULL, ARG_ENTRY_TOKEN                 },
374
                { "json",                        required_argument, NULL, ARG_JSON                        },
375
                { "all-architectures",           no_argument,       NULL, ARG_ARCH_ALL                    },
376
                { "efi-boot-option-description", required_argument, NULL, ARG_EFI_BOOT_OPTION_DESCRIPTION },
377
                { "dry-run",                     no_argument,       NULL, ARG_DRY_RUN                     },
378
                { "secure-boot-auto-enroll",     required_argument, NULL, ARG_SECURE_BOOT_AUTO_ENROLL     },
379
                { "certificate",                 required_argument, NULL, ARG_CERTIFICATE                 },
380
                { "certificate-source",          required_argument, NULL, ARG_CERTIFICATE_SOURCE          },
381
                { "private-key",                 required_argument, NULL, ARG_PRIVATE_KEY                 },
382
                { "private-key-source",          required_argument, NULL, ARG_PRIVATE_KEY_SOURCE          },
383
                {}
384
        };
385

386
        int c, r;
253✔
387

388
        assert(argc >= 0);
253✔
389
        assert(argv);
253✔
390

391
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
640✔
392
                switch (c) {
393✔
393

394
                case 'h':
3✔
395
                        help(0, NULL, NULL);
3✔
396
                        return 0;
3✔
397

398
                case ARG_VERSION:
3✔
399
                        return version();
3✔
400

401
                case ARG_ESP_PATH:
3✔
402
                        r = free_and_strdup(&arg_esp_path, optarg);
3✔
403
                        if (r < 0)
3✔
404
                                return log_oom();
×
405
                        break;
406

407
                case ARG_BOOT_PATH:
3✔
408
                        r = free_and_strdup(&arg_xbootldr_path, optarg);
3✔
409
                        if (r < 0)
3✔
410
                                return log_oom();
×
411
                        break;
412

413
                case ARG_ROOT:
34✔
414
                        r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
34✔
415
                        if (r < 0)
34✔
416
                                return r;
417
                        break;
418

419
                case ARG_IMAGE:
29✔
420
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
29✔
421
                        if (r < 0)
29✔
422
                                return r;
423
                        break;
424

425
                case ARG_IMAGE_POLICY:
×
426
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
427
                        if (r < 0)
×
428
                                return r;
429
                        break;
430

431
                case ARG_INSTALL_SOURCE:
×
432
                        if (streq(optarg, "auto"))
×
433
                                arg_install_source = ARG_INSTALL_SOURCE_AUTO;
×
434
                        else if (streq(optarg, "image"))
×
435
                                arg_install_source = ARG_INSTALL_SOURCE_IMAGE;
×
436
                        else if (streq(optarg, "host"))
×
437
                                arg_install_source = ARG_INSTALL_SOURCE_HOST;
×
438
                        else
439
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
440
                                                       "Unexpected parameter for --install-source=: %s", optarg);
441

442
                        break;
443

444
                case 'p':
10✔
445
                        arg_print_esp_path = true;
10✔
446
                        break;
10✔
447

448
                case 'x':
8✔
449
                        arg_print_dollar_boot_path = true;
8✔
450
                        break;
8✔
451

452
                case ARG_PRINT_LOADER_PATH:
×
453
                        arg_print_loader_path = true;
×
454
                        break;
×
455

456
                case ARG_PRINT_STUB_PATH:
6✔
457
                        arg_print_stub_path = true;
6✔
458
                        break;
6✔
459

460
                case 'R':
7✔
461
                        arg_print_root_device++;
7✔
462
                        break;
7✔
463

464
                case ARG_NO_VARIABLES:
115✔
465
                        arg_touch_variables = false;
115✔
466
                        break;
115✔
467

468
                case ARG_RANDOM_SEED:
×
469
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
470
                        if (r < 0)
×
471
                                return r;
472
                        break;
473

474
                case ARG_NO_PAGER:
×
475
                        arg_pager_flags |= PAGER_DISABLE;
×
476
                        break;
×
477

478
                case ARG_GRACEFUL:
132✔
479
                        arg_graceful = true;
132✔
480
                        break;
132✔
481

482
                case 'q':
6✔
483
                        arg_quiet = true;
6✔
484
                        break;
6✔
485

486
                case ARG_ENTRY_TOKEN:
×
487
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
488
                        if (r < 0)
×
489
                                return r;
490
                        break;
491

492
                case ARG_MAKE_ENTRY_DIRECTORY:
13✔
493
                        if (streq(optarg, "auto"))  /* retained for backwards compatibility */
13✔
494
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
495
                        else {
496
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
13✔
497
                                if (r < 0)
13✔
498
                                        return r;
499

500
                                arg_make_entry_directory = r;
13✔
501
                        }
502
                        break;
503

504
                case ARG_JSON:
6✔
505
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
506
                        if (r <= 0)
6✔
507
                                return r;
508
                        break;
509

510
                case ARG_ARCH_ALL:
12✔
511
                        arg_arch_all = true;
12✔
512
                        break;
12✔
513

514
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
515
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
516
                                _cleanup_free_ char *escaped = NULL;
×
517

518
                                escaped = cescape(optarg);
×
519
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
520
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
521
                        }
522
                        if (strlen(optarg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
523
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
524
                                                       "--efi-boot-option-description= too long: %zu > %zu",
525
                                                       strlen(optarg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
526
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, optarg);
×
527
                        if (r < 0)
×
528
                                return r;
529
                        break;
530

531
                case ARG_DRY_RUN:
×
532
                        arg_dry_run = true;
×
533
                        break;
×
534

535
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
536
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
537
                        if (r < 0)
1✔
538
                                return r;
539
                        break;
540

541
                case ARG_CERTIFICATE:
1✔
542
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
543
                        if (r < 0)
1✔
544
                                return r;
545
                        break;
546

547
                case ARG_CERTIFICATE_SOURCE:
×
548
                        r = parse_openssl_certificate_source_argument(
×
549
                                        optarg,
550
                                        &arg_certificate_source,
551
                                        &arg_certificate_source_type);
552
                        if (r < 0)
×
553
                                return r;
554
                        break;
555

556
                case ARG_PRIVATE_KEY: {
1✔
557
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
558
                        if (r < 0)
1✔
559
                                return r;
560
                        break;
561
                }
562

563
                case ARG_PRIVATE_KEY_SOURCE:
×
564
                        r = parse_openssl_key_source_argument(
×
565
                                        optarg,
566
                                        &arg_private_key_source,
567
                                        &arg_private_key_source_type);
568
                        if (r < 0)
×
569
                                return r;
570
                        break;
571

572
                case '?':
573
                        return -EINVAL;
574

575
                default:
×
576
                        assert_not_reached();
×
577
                }
578

579
        if (!!arg_print_esp_path + !!arg_print_dollar_boot_path + (arg_print_root_device > 0) + arg_print_loader_path + arg_print_stub_path > 1)
247✔
580
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
581
                                                       "--print-esp-path/-p, --print-boot-path/-x, --print-root-device=/-R, --print-loader-path, --print-stub-path cannot be combined.");
582

583
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
247✔
584
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
585
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
586
                                       "Options --root= and --image= are not supported with verb %s.",
587
                                       argv[optind]);
588

589
        if (arg_root && arg_image)
247✔
590
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
591

592
        if (arg_install_source != ARG_INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
247✔
593
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
594

595
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
247✔
596
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry is only supported with --unlink or --cleanup");
×
597

598
        if (arg_secure_boot_auto_enroll && !arg_certificate)
247✔
599
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided");
×
600

601
        if (arg_secure_boot_auto_enroll && !arg_private_key)
247✔
602
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided");
×
603

604
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
247✔
605
        if (r < 0)
247✔
606
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
607
        if (r > 0) {
247✔
608
                arg_varlink = true;
16✔
609
                arg_pager_flags |= PAGER_DISABLE;
16✔
610
        }
611

612
        return 1;
613
}
614

615
static int bootctl_main(int argc, char *argv[]) {
220✔
616
        static const Verb verbs[] = {
220✔
617
                { "help",                VERB_ANY, VERB_ANY, 0,            help                     },
618
                { "status",              VERB_ANY, 1,        VERB_DEFAULT, verb_status              },
619
                { "install",             VERB_ANY, 1,        0,            verb_install             },
620
                { "update",              VERB_ANY, 1,        0,            verb_install             },
621
                { "remove",              VERB_ANY, 1,        0,            verb_remove              },
622
                { "is-installed",        VERB_ANY, 1,        0,            verb_is_installed        },
623
                { "kernel-identify",     2,        2,        0,            verb_kernel_identify     },
624
                { "kernel-inspect",      2,        2,        0,            verb_kernel_inspect      },
625
                { "list",                VERB_ANY, 1,        0,            verb_list                },
626
                { "unlink",              2,        2,        0,            verb_unlink              },
627
                { "cleanup",             VERB_ANY, 1,        0,            verb_list                },
628
                { "set-default",         2,        2,        0,            verb_set_efivar          },
629
                { "set-oneshot",         2,        2,        0,            verb_set_efivar          },
630
                { "set-timeout",         2,        2,        0,            verb_set_efivar          },
631
                { "set-timeout-oneshot", 2,        2,        0,            verb_set_efivar          },
632
                { "random-seed",         VERB_ANY, 1,        0,            verb_random_seed         },
633
                { "systemd-efi-options", VERB_ANY, 2,        0,            verb_systemd_efi_options },
634
                { "reboot-to-firmware",  VERB_ANY, 2,        0,            verb_reboot_to_firmware  },
635
                {}
636
        };
637

638
        return dispatch_verb(argc, argv, verbs, NULL);
220✔
639
}
640

641
static int run(int argc, char *argv[]) {
253✔
642
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
253✔
643
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
253✔
644
        int r;
253✔
645

646
        log_setup();
253✔
647

648
        /* If we run in a container, automatically turn off EFI file system access */
649
        if (detect_container() > 0)
253✔
650
                arg_touch_variables = false;
66✔
651

652
        r = parse_argv(argc, argv);
253✔
653
        if (r <= 0)
253✔
654
                return r;
655

656
        if (arg_varlink) {
247✔
657
                _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
16✔
658

659
                /* Invocation as Varlink service */
660

661
                r = varlink_server_new(&varlink_server, SD_VARLINK_SERVER_ROOT_ONLY, NULL);
16✔
662
                if (r < 0)
16✔
663
                        return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
664

665
                r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
16✔
666
                if (r < 0)
16✔
667
                        return log_error_errno(r, "Failed to add Varlink interface: %m");
×
668

669
                r = sd_varlink_server_bind_method_many(
16✔
670
                                varlink_server,
671
                                "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
672
                                "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
673
                                "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware);
674
                if (r < 0)
16✔
675
                        return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
676

677
                r = sd_varlink_server_loop_auto(varlink_server);
16✔
678
                if (r < 0)
16✔
679
                        return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
680

681
                return EXIT_SUCCESS;
682
        }
683

684
        if (arg_print_root_device > 0) {
231✔
685
                _cleanup_free_ char *path = NULL;
5✔
686
                dev_t devno;
5✔
687

688
                r = blockdev_get_root(LOG_ERR, &devno);
5✔
689
                if (r < 0)
5✔
690
                        return r;
691
                if (r == 0) {
5✔
692
                        log_error("Root file system not backed by a (single) whole block device.");
×
693
                        return 80; /* some recognizable error code */
×
694
                }
695

696
                if (arg_print_root_device > 1) {
5✔
697
                        r = block_get_whole_disk(devno, &devno);
2✔
698
                        if (r < 0)
2✔
699
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
700
                }
701

702
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
703
                if (r < 0)
5✔
704
                        return log_error_errno(r,
×
705
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
706
                                               DEVNUM_FORMAT_VAL(devno));
707

708
                puts(path);
5✔
709
                return 0;
710
        }
711

712
        if (arg_print_loader_path || arg_print_stub_path)
226✔
713
                return print_loader_or_stub_path();
6✔
714

715
        /* Open up and mount the image */
716
        if (arg_image) {
220✔
717
                assert(!arg_root);
26✔
718

719
                r = mount_image_privately_interactively(
26✔
720
                                arg_image,
721
                                arg_image_policy,
722
                                DISSECT_IMAGE_GENERIC_ROOT |
723
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
724
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
725
                                &mounted_dir,
726
                                /* ret_dir_fd= */ NULL,
727
                                &loop_device);
728
                if (r < 0)
26✔
729
                        return r;
730

731
                arg_root = strdup(mounted_dir);
26✔
732
                if (!arg_root)
26✔
733
                        return log_oom();
×
734
        }
735

736
        return bootctl_main(argc, argv);
220✔
737
}
738

739
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
506✔
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