Debian Codegraph @

src/bootctl/bootctl-reboot-to-firmware.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "sd-json.h"
6 #include "sd-varlink.h"
7
8 #include "bootctl-reboot-to-firmware.h"
9 #include "bootctl-util.h"
10 #include "efi-api.h"
11 #include "errno-util.h"
12 #include "log.h"
13 #include "parse-util.h"
14
15 int verb_reboot_to_firmware(int argc, char *argv[], uintptr_t _data, void *userdata) {
16 int r;
17
18 r = verify_touch_variables_allowed(argv[0]);
19 if (r < 0)
20 return r;
21
22 if (argc < 2) {
23 r = efi_get_reboot_to_firmware();
24 if (r > 0) {
25 puts("active");
26 return 0; /* success */
27 }
28 if (r == 0) {
29 puts("supported");
30 return 1; /* recognizable error #1 */
31 }
32 if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
33 puts("not supported");
34 return 2; /* recognizable error #2 */
35 }
36
37 log_error_errno(r, "Failed to query reboot-to-firmware state: %m");
38 return 3; /* other kind of error */
39 } else {
40 r = parse_boolean(argv[1]);
41 if (r < 0)
42 return log_error_errno(r, "Failed to parse argument: %s", argv[1]);
43
44 r = efi_set_reboot_to_firmware(r);
45 if (r < 0)
46 return log_error_errno(r, "Failed to set reboot-to-firmware option: %m");
47
48 return 0;
49 }
50 }
51
52 int vl_method_set_reboot_to_firmware(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
53 static const sd_json_dispatch_field dispatch_table[] = {
54 { "state", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, 0, 0 },
55 {}
56 };
57 bool b;
58 int r;
59
60 r = sd_varlink_dispatch(link, parameters, dispatch_table, &b);
61 if (r != 0)
62 return r;
63
64 r = efi_set_reboot_to_firmware(b);
65 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
66 return sd_varlink_error(link, "io.systemd.BootControl.RebootToFirmwareNotSupported", NULL);
67 if (r < 0)
68 return r;
69
70 return sd_varlink_reply(link, NULL);
71 }
72
73 int vl_method_get_reboot_to_firmware(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
74 int r;
75
76 r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL);
77 if (r != 0)
78 return r;
79
80 r = efi_get_reboot_to_firmware();
81 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
82 return sd_varlink_error(link, "io.systemd.BootControl.RebootToFirmwareNotSupported", NULL);
83 if (r < 0)
84 return r;
85
86 return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_BOOLEAN("state", r));
87 }