blob: eb92027817a770eecb629022c5ad94d783f3cea6 [file] [log] [blame]
Masami Hiramatsu950313e2020-01-11 01:03:56 +09001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Boot config tool for initrd image
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <string.h>
12#include <errno.h>
13
14#include <linux/kernel.h>
15#include <linux/bootconfig.h>
16
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090017static int xbc_show_value(struct xbc_node *node, bool semicolon)
Masami Hiramatsu950313e2020-01-11 01:03:56 +090018{
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090019 const char *val, *eol;
Masami Hiramatsu272da322020-06-16 19:14:17 +090020 char q;
Masami Hiramatsu950313e2020-01-11 01:03:56 +090021 int i = 0;
22
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090023 eol = semicolon ? ";\n" : "\n";
Masami Hiramatsu950313e2020-01-11 01:03:56 +090024 xbc_array_for_each_value(node, val) {
Masami Hiramatsu272da322020-06-16 19:14:17 +090025 if (strchr(val, '"'))
26 q = '\'';
27 else
28 q = '"';
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090029 printf("%c%s%c%s", q, val, q, node->next ? ", " : eol);
Masami Hiramatsu950313e2020-01-11 01:03:56 +090030 i++;
31 }
32 return i;
33}
34
35static void xbc_show_compact_tree(void)
36{
37 struct xbc_node *node, *cnode;
38 int depth = 0, i;
39
40 node = xbc_root_node();
41 while (node && xbc_node_is_key(node)) {
42 for (i = 0; i < depth; i++)
43 printf("\t");
44 cnode = xbc_node_get_child(node);
45 while (cnode && xbc_node_is_key(cnode) && !cnode->next) {
46 printf("%s.", xbc_node_get_data(node));
47 node = cnode;
48 cnode = xbc_node_get_child(node);
49 }
50 if (cnode && xbc_node_is_key(cnode)) {
51 printf("%s {\n", xbc_node_get_data(node));
52 depth++;
53 node = cnode;
54 continue;
55 } else if (cnode && xbc_node_is_value(cnode)) {
56 printf("%s = ", xbc_node_get_data(node));
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090057 xbc_show_value(cnode, true);
Masami Hiramatsu950313e2020-01-11 01:03:56 +090058 } else {
59 printf("%s;\n", xbc_node_get_data(node));
60 }
61
62 if (node->next) {
63 node = xbc_node_get_next(node);
64 continue;
65 }
66 while (!node->next) {
67 node = xbc_node_get_parent(node);
68 if (!node)
69 return;
70 if (!xbc_node_get_child(node)->next)
71 continue;
72 depth--;
73 for (i = 0; i < depth; i++)
74 printf("\t");
75 printf("}\n");
76 }
77 node = xbc_node_get_next(node);
78 }
79}
80
Masami Hiramatsue4f70b72020-08-10 17:34:51 +090081static void xbc_show_list(void)
82{
83 char key[XBC_KEYLEN_MAX];
84 struct xbc_node *leaf;
85 const char *val;
86 int ret = 0;
87
88 xbc_for_each_key_value(leaf, val) {
89 ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
90 if (ret < 0)
91 break;
92 printf("%s = ", key);
93 if (!val || val[0] == '\0') {
94 printf("\"\"\n");
95 continue;
96 }
97 xbc_show_value(xbc_node_get_child(leaf), false);
98 }
99}
100
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900101/* Simple real checksum */
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900102static int checksum(unsigned char *buf, int len)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900103{
104 int i, sum = 0;
105
106 for (i = 0; i < len; i++)
107 sum += buf[i];
108
109 return sum;
110}
111
112#define PAGE_SIZE 4096
113
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900114static int load_xbc_fd(int fd, char **buf, int size)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900115{
116 int ret;
117
118 *buf = malloc(size + 1);
119 if (!*buf)
120 return -ENOMEM;
121
122 ret = read(fd, *buf, size);
123 if (ret < 0)
124 return -errno;
125 (*buf)[size] = '\0';
126
127 return ret;
128}
129
130/* Return the read size or -errno */
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900131static int load_xbc_file(const char *path, char **buf)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900132{
133 struct stat stat;
134 int fd, ret;
135
136 fd = open(path, O_RDONLY);
137 if (fd < 0)
138 return -errno;
139 ret = fstat(fd, &stat);
140 if (ret < 0)
141 return -errno;
142
143 ret = load_xbc_fd(fd, buf, stat.st_size);
144
145 close(fd);
146
147 return ret;
148}
149
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900150static int load_xbc_from_initrd(int fd, char **buf)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900151{
152 struct stat stat;
153 int ret;
154 u32 size = 0, csum = 0, rcsum;
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900155 char magic[BOOTCONFIG_MAGIC_LEN];
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900156 const char *msg;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900157
158 ret = fstat(fd, &stat);
159 if (ret < 0)
160 return -errno;
161
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900162 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900163 return 0;
164
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900165 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
166 pr_err("Failed to lseek: %d\n", -errno);
167 return -errno;
168 }
169 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
170 return -errno;
171 /* Check the bootconfig magic bytes */
172 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
173 return 0;
174
175 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900176 pr_err("Failed to lseek: %d\n", -errno);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900177 return -errno;
178 }
179
180 if (read(fd, &size, sizeof(u32)) < 0)
181 return -errno;
182
183 if (read(fd, &csum, sizeof(u32)) < 0)
184 return -errno;
185
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900186 /* Wrong size error */
187 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
188 pr_err("bootconfig size is too big\n");
189 return -E2BIG;
190 }
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900191
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900192 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
193 SEEK_SET) < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900194 pr_err("Failed to lseek: %d\n", -errno);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900195 return -errno;
196 }
197
198 ret = load_xbc_fd(fd, buf, size);
199 if (ret < 0)
200 return ret;
201
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900202 /* Wrong Checksum */
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900203 rcsum = checksum((unsigned char *)*buf, size);
204 if (csum != rcsum) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900205 pr_err("checksum error: %d != %d\n", csum, rcsum);
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900206 return -EINVAL;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900207 }
208
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900209 ret = xbc_init(*buf, &msg, NULL);
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900210 /* Wrong data */
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900211 if (ret < 0) {
212 pr_err("parse error: %s.\n", msg);
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900213 return ret;
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900214 }
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900215
216 return size;
217}
218
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900219static void show_xbc_error(const char *data, const char *msg, int pos)
220{
221 int lin = 1, col, i;
222
223 if (pos < 0) {
224 pr_err("Error: %s.\n", msg);
225 return;
226 }
227
228 /* Note that pos starts from 0 but lin and col should start from 1. */
229 col = pos + 1;
230 for (i = 0; i < pos; i++) {
231 if (data[i] == '\n') {
232 lin++;
233 col = pos - i;
234 }
235 }
236 pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
237
238}
239
240static int init_xbc_with_error(char *buf, int len)
241{
242 char *copy = strdup(buf);
243 const char *msg;
244 int ret, pos;
245
246 if (!copy)
247 return -ENOMEM;
248
249 ret = xbc_init(buf, &msg, &pos);
250 if (ret < 0)
251 show_xbc_error(copy, msg, pos);
252 free(copy);
253
254 return ret;
255}
256
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900257static int show_xbc(const char *path, bool list)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900258{
259 int ret, fd;
260 char *buf = NULL;
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900261 struct stat st;
262
263 ret = stat(path, &st);
264 if (ret < 0) {
265 pr_err("Failed to stat %s: %d\n", path, -errno);
266 return -errno;
267 }
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900268
269 fd = open(path, O_RDONLY);
270 if (fd < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900271 pr_err("Failed to open initrd %s: %d\n", path, fd);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900272 return -errno;
273 }
274
275 ret = load_xbc_from_initrd(fd, &buf);
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900276 close(fd);
Masami Hiramatsuf91cb5b2020-06-16 19:14:25 +0900277 if (ret < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900278 pr_err("Failed to load a boot config from initrd: %d\n", ret);
Masami Hiramatsuf91cb5b2020-06-16 19:14:25 +0900279 goto out;
280 }
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900281 /* Assume a bootconfig file if it is enough small */
282 if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
283 ret = load_xbc_file(path, &buf);
284 if (ret < 0) {
285 pr_err("Failed to load a boot config: %d\n", ret);
286 goto out;
287 }
288 if (init_xbc_with_error(buf, ret) < 0)
289 goto out;
290 }
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900291 if (list)
292 xbc_show_list();
293 else
294 xbc_show_compact_tree();
Masami Hiramatsuf91cb5b2020-06-16 19:14:25 +0900295 ret = 0;
296out:
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900297 free(buf);
298
299 return ret;
300}
301
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900302static int delete_xbc(const char *path)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900303{
304 struct stat stat;
305 int ret = 0, fd, size;
306 char *buf = NULL;
307
308 fd = open(path, O_RDWR);
309 if (fd < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900310 pr_err("Failed to open initrd %s: %d\n", path, fd);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900311 return -errno;
312 }
313
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900314 size = load_xbc_from_initrd(fd, &buf);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900315 if (size < 0) {
316 ret = size;
Masami Hiramatsu97378002020-02-09 22:05:13 +0900317 pr_err("Failed to load a boot config from initrd: %d\n", ret);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900318 } else if (size > 0) {
319 ret = fstat(fd, &stat);
320 if (!ret)
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900321 ret = ftruncate(fd, stat.st_size
322 - size - 8 - BOOTCONFIG_MAGIC_LEN);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900323 if (ret)
324 ret = -errno;
325 } /* Ignore if there is no boot config in initrd */
326
327 close(fd);
328 free(buf);
329
330 return ret;
331}
332
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900333static int apply_xbc(const char *path, const char *xbc_path)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900334{
335 u32 size, csum;
336 char *buf, *data;
337 int ret, fd;
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900338 const char *msg;
339 int pos;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900340
341 ret = load_xbc_file(xbc_path, &buf);
342 if (ret < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900343 pr_err("Failed to load %s : %d\n", xbc_path, ret);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900344 return ret;
345 }
346 size = strlen(buf) + 1;
347 csum = checksum((unsigned char *)buf, size);
348
349 /* Prepare xbc_path data */
350 data = malloc(size + 8);
351 if (!data)
352 return -ENOMEM;
353 strcpy(data, buf);
354 *(u32 *)(data + size) = size;
355 *(u32 *)(data + size + 4) = csum;
356
357 /* Check the data format */
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900358 ret = xbc_init(buf, &msg, &pos);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900359 if (ret < 0) {
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900360 show_xbc_error(data, msg, pos);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900361 free(data);
362 free(buf);
Masami Hiramatsu89b74ca2020-03-03 20:24:50 +0900363
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900364 return ret;
365 }
366 printf("Apply %s to %s\n", xbc_path, path);
Masami Hiramatsu0f0d0a72020-02-05 22:50:13 +0900367 printf("\tNumber of nodes: %d\n", ret);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900368 printf("\tSize: %u bytes\n", (unsigned int)size);
369 printf("\tChecksum: %d\n", (unsigned int)csum);
370
371 /* TODO: Check the options by schema */
372 xbc_destroy_all();
373 free(buf);
374
375 /* Remove old boot config if exists */
376 ret = delete_xbc(path);
377 if (ret < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900378 pr_err("Failed to delete previous boot config: %d\n", ret);
Yunfeng Ye88426042020-05-07 17:23:36 +0800379 free(data);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900380 return ret;
381 }
382
383 /* Apply new one */
384 fd = open(path, O_RDWR | O_APPEND);
385 if (fd < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900386 pr_err("Failed to open %s: %d\n", path, fd);
Yunfeng Ye88426042020-05-07 17:23:36 +0800387 free(data);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900388 return fd;
389 }
390 /* TODO: Ensure the @path is initramfs/initrd image */
391 ret = write(fd, data, size + 8);
392 if (ret < 0) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900393 pr_err("Failed to apply a boot config: %d\n", ret);
Yunfeng Ye88426042020-05-07 17:23:36 +0800394 goto out;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900395 }
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900396 /* Write a magic word of the bootconfig */
397 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
398 if (ret < 0) {
399 pr_err("Failed to apply a boot config magic: %d\n", ret);
Yunfeng Ye88426042020-05-07 17:23:36 +0800400 goto out;
Masami Hiramatsu85c46b72020-02-20 21:18:42 +0900401 }
Steven Rostedt (VMware)9d82ccd2020-05-08 11:07:56 -0400402 ret = 0;
Yunfeng Ye88426042020-05-07 17:23:36 +0800403out:
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900404 close(fd);
405 free(data);
406
Yunfeng Ye88426042020-05-07 17:23:36 +0800407 return ret;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900408}
409
Masami Hiramatsu483ce672020-08-10 17:35:01 +0900410static int usage(void)
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900411{
412 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900413 "Or bootconfig <CONFIG>\n"
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900414 " Apply, delete or show boot config to initrd.\n"
415 " Options:\n"
416 " -a <config>: Apply boot config to initrd\n"
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900417 " -d : Delete boot config file from initrd\n"
418 " -l : list boot config in initrd or file\n\n"
Masami Hiramatsud052e1c2020-08-10 17:34:41 +0900419 " If no option is given, show the bootconfig in the given file.\n");
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900420 return -1;
421}
422
423int main(int argc, char **argv)
424{
425 char *path = NULL;
426 char *apply = NULL;
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900427 bool delete = false, list = false;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900428 int opt;
429
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900430 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900431 switch (opt) {
432 case 'd':
433 delete = true;
434 break;
435 case 'a':
436 apply = optarg;
437 break;
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900438 case 'l':
439 list = true;
440 break;
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900441 case 'h':
442 default:
443 return usage();
444 }
445 }
446
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900447 if ((apply && delete) || (delete && list) || (apply && list)) {
448 pr_err("Error: You can give one of -a, -d or -l at once.\n");
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900449 return usage();
450 }
451
452 if (optind >= argc) {
Masami Hiramatsu97378002020-02-09 22:05:13 +0900453 pr_err("Error: No initrd is specified.\n");
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900454 return usage();
455 }
456
457 path = argv[optind];
458
459 if (apply)
460 return apply_xbc(path, apply);
461 else if (delete)
462 return delete_xbc(path);
463
Masami Hiramatsue4f70b72020-08-10 17:34:51 +0900464 return show_xbc(path, list);
Masami Hiramatsu950313e2020-01-11 01:03:56 +0900465}