1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/timex.h>
35 static int help(void) {
36 printf("%s [OPTIONS...] COMMAND ...\n\n"
37 "Query or change firmware and boot mananger settings.\n\n"
38 " -h --help Show this help\n"
39 " --version Show package version\n"
41 " status Show current time settings\n",
42 program_invocation_short_name);
47 static int parse_argv(int argc, char *argv[]) {
52 static const struct option options[] = {
53 { "help", no_argument, NULL, 'h' },
54 { "version", no_argument, NULL, ARG_VERSION },
62 while ((c = getopt_long(argc, argv, "+hH:P", options, NULL)) >= 0) {
72 puts(SYSTEMD_FEATURES);
79 log_error("Unknown option code %c", c);
87 static int boot_info_new(struct boot_info **info) {
91 in = new0(struct boot_info, 1);
95 err = sd_id128_get_machine(&in->machine_id);
99 err = sd_id128_get_boot(&in->boot_id);
103 in->fw_entry_active = -1;
104 in->loader_entry_active = -1;
113 static void boot_info_entries_free(struct boot_info_entry *entries, size_t n) {
116 for (i = 0; i < n; i++) {
117 free(entries[i].title);
118 free(entries[i].path);
123 static void boot_info_free(struct boot_info *info) {
126 boot_info_entries_free(info->fw_entries, info->fw_entries_count);
127 free(info->fw_entries_order);
129 free(info->loader_image_path);
130 free(info->loader_options_added);
131 boot_info_entries_free(info->loader_entries, info->loader_entries_count);
135 static int show_status(char **args, unsigned n) {
137 struct boot_info *info;
140 err = boot_info_new(&info);
144 err = boot_info_query(info);
146 printf(" Machine ID: %s\n", sd_id128_to_string(info->machine_id, buf));
147 printf(" Boot ID: %s\n", sd_id128_to_string(info->boot_id, buf));
149 printf(" Firmware: %s (%s)\n", info->fw_type, strna(info->fw_info));
151 if (info->fw_entry_active >= 0) {
152 printf("Firmware entry: %s\n", strna(info->fw_entries[info->fw_entry_active].title));
153 if (info->fw_entries[info->fw_entry_active].path)
154 printf(" %s\n", info->fw_entries[info->fw_entry_active].path);
155 if (!sd_id128_equal(info->fw_entries[info->fw_entry_active].part_uuid, SD_ID128_NULL))
156 printf(" /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
157 SD_ID128_FORMAT_VAL(info->fw_entries[info->fw_entry_active].part_uuid));
161 printf(" Loader: %s\n", info->loader);
162 printf(" %s\n", strna(info->loader_image_path));
163 if (!sd_id128_equal(info->loader_part_uuid, SD_ID128_NULL))
164 printf(" /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
165 SD_ID128_FORMAT_VAL(info->loader_part_uuid));
167 if (info->loader_entry_active >= 0) {
168 printf(" Loader entry: %s\n", strna(info->loader_entries[info->loader_entry_active].title));
169 printf(" %s\n", info->loader_entries[info->loader_entry_active].path);
172 printf("Loader options: %s\n", strna(info->loader_options_added));
174 printf("No suitable data is provided by the boot manager. See:\n"
175 " http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface\n"
176 " http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec\n"
180 boot_info_free(info);
184 static int bootctl_main(int argc, char *argv[]) {
185 static const struct {
193 int (* const dispatch)(char **args, unsigned n);
195 { "status", LESS, 1, show_status },
204 left = argc - optind;
207 /* Special rule: no arguments means "status" */
210 if (streq(argv[optind], "help")) {
215 for (i = 0; i < ELEMENTSOF(verbs); i++)
216 if (streq(argv[optind], verbs[i].verb))
219 if (i >= ELEMENTSOF(verbs)) {
220 log_error("Unknown operation %s", argv[optind]);
225 switch (verbs[i].argc_cmp) {
228 if (left != verbs[i].argc) {
229 log_error("Invalid number of arguments.");
235 if (left < verbs[i].argc) {
236 log_error("Too few arguments.");
242 if (left > verbs[i].argc) {
243 log_error("Too many arguments.");
249 assert_not_reached("Unknown comparison operator.");
252 return verbs[i].dispatch(argv + optind, left);
255 int main(int argc, char *argv[]) {
256 int r, retval = EXIT_FAILURE;
258 log_parse_environment();
261 r = parse_argv(argc, argv);
265 retval = EXIT_SUCCESS;
269 r = bootctl_main(argc, argv);
270 retval = r < 0 ? EXIT_FAILURE : r;