1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Kay Sievers
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <locale.h>
#include <string.h>
#include <sys/timex.h>
#include "boot.h"
#include "build.h"
#include "util.h"
#include "utf8.h"
static void help(void) {
printf("%s [OPTIONS...] COMMAND ...\n\n"
"Query or change firmware and boot manager settings.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
"Commands:\n"
" status Show current boot settings\n"
, program_invocation_short_name);
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{}
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
puts(PACKAGE_STRING);
puts(SYSTEMD_FEATURES);
return 0;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
static int boot_info_new(struct boot_info **info) {
struct boot_info *in;
int err;
in = new0(struct boot_info, 1);
if (!in)
return -ENOMEM;
err = sd_id128_get_machine(&in->machine_id);
if (err < 0)
goto err;
err = sd_id128_get_boot(&in->boot_id);
if (err < 0)
goto err;
in->fw_entry_active = -1;
in->loader_entry_active = -1;
*info = in;
return 0;
err:
free(in);
return err;
}
static void boot_info_entries_free(struct boot_info_entry *entries, size_t n) {
size_t i;
for (i = 0; i < n; i++) {
free(entries[i].title);
free(entries[i].path);
}
free(entries);
}
static void boot_info_free(struct boot_info *info) {
free(info->fw_type);
free(info->fw_info);
boot_info_entries_free(info->fw_entries, info->fw_entries_count);
free(info->fw_entries_order);
free(info->loader);
free(info->loader_image_path);
free(info->loader_options_added);
boot_info_entries_free(info->loader_entries, info->loader_entries_count);
free(info);
}
static int show_status(char **args, unsigned n) {
char buf[64];
struct boot_info *info;
int err;
err = boot_info_new(&info);
if (err < 0)
return -ENOMEM;
err = boot_info_query(info);
printf("System:\n");
printf(" Machine ID: %s\n", sd_id128_to_string(info->machine_id, buf));
printf(" Boot ID: %s\n", sd_id128_to_string(info->boot_id, buf));
if (info->fw_type)
printf(" Firmware: %s (%s)\n", info->fw_type, strna(info->fw_info));
if (info->fw_secure_boot >= 0)
printf(" Secure Boot: %s\n", info->fw_secure_boot ? "enabled" : "disabled");
if (info->fw_secure_boot_setup_mode >= 0)
printf(" Setup Mode: %s\n", info->fw_secure_boot_setup_mode ? "setup" : "user");
printf("\n");
if (info->fw_entry_active >= 0) {
printf("Selected Firmware Entry:\n");
printf(" Title: %s\n", strna(info->fw_entries[info->fw_entry_active].title));
if (!sd_id128_equal(info->fw_entries[info->fw_entry_active].part_uuid, SD_ID128_NULL))
printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
SD_ID128_FORMAT_VAL(info->fw_entries[info->fw_entry_active].part_uuid));
else
printf(" Partition: n/a\n");
if (info->fw_entries[info->fw_entry_active].path)
printf(" File: %s%s\n", draw_special_char(DRAW_TREE_RIGHT), info->fw_entries[info->fw_entry_active].path);
}
printf("\n");
if (info->loader) {
printf("Boot Loader:\n");
printf(" Product: %s\n", info->loader);
if (!sd_id128_equal(info->loader_part_uuid, SD_ID128_NULL))
printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
SD_ID128_FORMAT_VAL(info->loader_part_uuid));
else
printf(" Partition: n/a\n");
printf(" File: %s%s\n", draw_special_char(DRAW_TREE_RIGHT), strna(info->loader_image_path));
printf("\n");
if (info->loader_entry_active >= 0) {
printf("Selected Boot Loader Entry:\n");
printf(" Title: %s\n", strna(info->loader_entries[info->loader_entry_active].title));
printf(" File: %s\n", info->loader_entries[info->loader_entry_active].path);
if (info->loader_options_added)
printf(" Options: %s\n", info->loader_options_added);
}
} else
printf("No suitable data is provided by the boot manager. See:\n"
" http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface\n"
" http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec\n"
"for details.\n");
printf("\n");
boot_info_free(info);
return err;
}
static int bootctl_main(int argc, char *argv[]) {
static const struct {
const char* verb;
const enum {
MORE,
LESS,
EQUAL
} argc_cmp;
const int argc;
int (* const dispatch)(char **args, unsigned n);
} verbs[] = {
{ "status", LESS, 1, show_status },
};
int left;
unsigned i;
assert(argc >= 0);
assert(argv);
left = argc - optind;
if (left <= 0)
/* Special rule: no arguments means "status" */
i = 0;
else {
if (streq(argv[optind], "help")) {
help();
return 0;
}
for (i = 0; i < ELEMENTSOF(verbs); i++)
if (streq(argv[optind], verbs[i].verb))
break;
if (i >= ELEMENTSOF(verbs)) {
log_error("Unknown operation %s", argv[optind]);
return -EINVAL;
}
}
switch (verbs[i].argc_cmp) {
case EQUAL:
if (left != verbs[i].argc) {
log_error("Invalid number of arguments.");
return -EINVAL;
}
break;
case MORE:
if (left < verbs[i].argc) {
log_error("Too few arguments.");
return -EINVAL;
}
break;
case LESS:
if (left > verbs[i].argc) {
log_error("Too many arguments.");
return -EINVAL;
}
break;
default:
assert_not_reached("Unknown comparison operator.");
}
return verbs[i].dispatch(argv + optind, left);
}
int main(int argc, char *argv[]) {
int r;
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
r = bootctl_main(argc, argv);
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
|