Commit | Line | Data |
---|---|---|
950313eb MH |
1 | // 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> | |
e8684358 | 13 | #include <endian.h> |
950313eb | 14 | |
950313eb MH |
15 | #include <linux/bootconfig.h> |
16 | ||
160321b2 MH |
17 | #define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) |
18 | ||
e4f70b7b | 19 | static int xbc_show_value(struct xbc_node *node, bool semicolon) |
950313eb | 20 | { |
e4f70b7b | 21 | const char *val, *eol; |
272da327 | 22 | char q; |
950313eb MH |
23 | int i = 0; |
24 | ||
e4f70b7b | 25 | eol = semicolon ? ";\n" : "\n"; |
950313eb | 26 | xbc_array_for_each_value(node, val) { |
272da327 MH |
27 | if (strchr(val, '"')) |
28 | q = '\''; | |
29 | else | |
30 | q = '"'; | |
ca24306d | 31 | printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol); |
950313eb MH |
32 | i++; |
33 | } | |
34 | return i; | |
35 | } | |
36 | ||
37 | static void xbc_show_compact_tree(void) | |
38 | { | |
e5efaeb8 | 39 | struct xbc_node *node, *cnode = NULL, *vnode; |
950313eb MH |
40 | int depth = 0, i; |
41 | ||
42 | node = xbc_root_node(); | |
43 | while (node && xbc_node_is_key(node)) { | |
44 | for (i = 0; i < depth; i++) | |
45 | printf("\t"); | |
e5efaeb8 MH |
46 | if (!cnode) |
47 | cnode = xbc_node_get_child(node); | |
950313eb | 48 | while (cnode && xbc_node_is_key(cnode) && !cnode->next) { |
e5efaeb8 MH |
49 | vnode = xbc_node_get_child(cnode); |
50 | /* | |
51 | * If @cnode has value and subkeys, this | |
52 | * should show it as below. | |
53 | * | |
54 | * key(@node) { | |
55 | * key(@cnode) = value; | |
56 | * key(@cnode) { | |
57 | * subkeys; | |
58 | * } | |
59 | * } | |
60 | */ | |
61 | if (vnode && xbc_node_is_value(vnode) && vnode->next) | |
62 | break; | |
950313eb MH |
63 | printf("%s.", xbc_node_get_data(node)); |
64 | node = cnode; | |
e5efaeb8 | 65 | cnode = vnode; |
950313eb MH |
66 | } |
67 | if (cnode && xbc_node_is_key(cnode)) { | |
68 | printf("%s {\n", xbc_node_get_data(node)); | |
69 | depth++; | |
70 | node = cnode; | |
e5efaeb8 | 71 | cnode = NULL; |
950313eb MH |
72 | continue; |
73 | } else if (cnode && xbc_node_is_value(cnode)) { | |
74 | printf("%s = ", xbc_node_get_data(node)); | |
e4f70b7b | 75 | xbc_show_value(cnode, true); |
e5efaeb8 MH |
76 | /* |
77 | * If @node has value and subkeys, continue | |
78 | * looping on subkeys with same node. | |
79 | */ | |
80 | if (cnode->next) { | |
81 | cnode = xbc_node_get_next(cnode); | |
82 | continue; | |
83 | } | |
950313eb MH |
84 | } else { |
85 | printf("%s;\n", xbc_node_get_data(node)); | |
86 | } | |
e5efaeb8 | 87 | cnode = NULL; |
950313eb MH |
88 | |
89 | if (node->next) { | |
90 | node = xbc_node_get_next(node); | |
91 | continue; | |
92 | } | |
93 | while (!node->next) { | |
94 | node = xbc_node_get_parent(node); | |
95 | if (!node) | |
96 | return; | |
97 | if (!xbc_node_get_child(node)->next) | |
98 | continue; | |
e5efaeb8 MH |
99 | if (depth) { |
100 | depth--; | |
101 | for (i = 0; i < depth; i++) | |
102 | printf("\t"); | |
103 | printf("}\n"); | |
104 | } | |
950313eb MH |
105 | } |
106 | node = xbc_node_get_next(node); | |
107 | } | |
108 | } | |
109 | ||
e4f70b7b MH |
110 | static void xbc_show_list(void) |
111 | { | |
112 | char key[XBC_KEYLEN_MAX]; | |
113 | struct xbc_node *leaf; | |
114 | const char *val; | |
903bd067 | 115 | int ret; |
e4f70b7b MH |
116 | |
117 | xbc_for_each_key_value(leaf, val) { | |
903bd067 JF |
118 | ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX); |
119 | if (ret < 0) { | |
e5efaeb8 | 120 | fprintf(stderr, "Failed to compose key %d\n", ret); |
e4f70b7b | 121 | break; |
e5efaeb8 | 122 | } |
e4f70b7b MH |
123 | printf("%s = ", key); |
124 | if (!val || val[0] == '\0') { | |
125 | printf("\"\"\n"); | |
126 | continue; | |
127 | } | |
128 | xbc_show_value(xbc_node_get_child(leaf), false); | |
129 | } | |
130 | } | |
131 | ||
950313eb MH |
132 | #define PAGE_SIZE 4096 |
133 | ||
483ce670 | 134 | static int load_xbc_fd(int fd, char **buf, int size) |
950313eb MH |
135 | { |
136 | int ret; | |
137 | ||
138 | *buf = malloc(size + 1); | |
139 | if (!*buf) | |
140 | return -ENOMEM; | |
141 | ||
142 | ret = read(fd, *buf, size); | |
143 | if (ret < 0) | |
144 | return -errno; | |
145 | (*buf)[size] = '\0'; | |
146 | ||
147 | return ret; | |
148 | } | |
149 | ||
150 | /* Return the read size or -errno */ | |
483ce670 | 151 | static int load_xbc_file(const char *path, char **buf) |
950313eb MH |
152 | { |
153 | struct stat stat; | |
154 | int fd, ret; | |
155 | ||
156 | fd = open(path, O_RDONLY); | |
157 | if (fd < 0) | |
158 | return -errno; | |
159 | ret = fstat(fd, &stat); | |
160 | if (ret < 0) | |
161 | return -errno; | |
162 | ||
163 | ret = load_xbc_fd(fd, buf, stat.st_size); | |
164 | ||
165 | close(fd); | |
166 | ||
167 | return ret; | |
168 | } | |
169 | ||
a61ea637 MH |
170 | static int pr_errno(const char *msg, int err) |
171 | { | |
172 | pr_err("%s: %d\n", msg, err); | |
173 | return err; | |
174 | } | |
175 | ||
483ce670 | 176 | static int load_xbc_from_initrd(int fd, char **buf) |
950313eb MH |
177 | { |
178 | struct stat stat; | |
179 | int ret; | |
4f292c48 | 180 | uint32_t size = 0, csum = 0, rcsum; |
85c46b78 | 181 | char magic[BOOTCONFIG_MAGIC_LEN]; |
89b74cac | 182 | const char *msg; |
950313eb MH |
183 | |
184 | ret = fstat(fd, &stat); | |
185 | if (ret < 0) | |
186 | return -errno; | |
187 | ||
85c46b78 | 188 | if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN) |
950313eb MH |
189 | return 0; |
190 | ||
a61ea637 MH |
191 | if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) |
192 | return pr_errno("Failed to lseek for magic", -errno); | |
193 | ||
85c46b78 | 194 | if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0) |
a61ea637 MH |
195 | return pr_errno("Failed to read", -errno); |
196 | ||
85c46b78 MH |
197 | /* Check the bootconfig magic bytes */ |
198 | if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0) | |
199 | return 0; | |
200 | ||
a61ea637 MH |
201 | if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) |
202 | return pr_errno("Failed to lseek for size", -errno); | |
950313eb | 203 | |
4f292c48 | 204 | if (read(fd, &size, sizeof(uint32_t)) < 0) |
a61ea637 | 205 | return pr_errno("Failed to read size", -errno); |
e8684358 | 206 | size = le32toh(size); |
950313eb | 207 | |
4f292c48 | 208 | if (read(fd, &csum, sizeof(uint32_t)) < 0) |
a61ea637 | 209 | return pr_errno("Failed to read checksum", -errno); |
e8684358 | 210 | csum = le32toh(csum); |
950313eb | 211 | |
85c46b78 MH |
212 | /* Wrong size error */ |
213 | if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) { | |
214 | pr_err("bootconfig size is too big\n"); | |
215 | return -E2BIG; | |
216 | } | |
950313eb | 217 | |
85c46b78 | 218 | if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN), |
a61ea637 MH |
219 | SEEK_SET) < 0) |
220 | return pr_errno("Failed to lseek", -errno); | |
950313eb MH |
221 | |
222 | ret = load_xbc_fd(fd, buf, size); | |
223 | if (ret < 0) | |
224 | return ret; | |
225 | ||
85c46b78 | 226 | /* Wrong Checksum */ |
99f4f5d6 | 227 | rcsum = xbc_calc_checksum(*buf, size); |
950313eb | 228 | if (csum != rcsum) { |
97378001 | 229 | pr_err("checksum error: %d != %d\n", csum, rcsum); |
85c46b78 | 230 | return -EINVAL; |
950313eb MH |
231 | } |
232 | ||
bdac5c2b | 233 | ret = xbc_init(*buf, size, &msg, NULL); |
85c46b78 | 234 | /* Wrong data */ |
89b74cac MH |
235 | if (ret < 0) { |
236 | pr_err("parse error: %s.\n", msg); | |
85c46b78 | 237 | return ret; |
89b74cac | 238 | } |
950313eb MH |
239 | |
240 | return size; | |
241 | } | |
242 | ||
d052e1c6 MH |
243 | static void show_xbc_error(const char *data, const char *msg, int pos) |
244 | { | |
245 | int lin = 1, col, i; | |
246 | ||
247 | if (pos < 0) { | |
248 | pr_err("Error: %s.\n", msg); | |
249 | return; | |
250 | } | |
251 | ||
252 | /* Note that pos starts from 0 but lin and col should start from 1. */ | |
253 | col = pos + 1; | |
254 | for (i = 0; i < pos; i++) { | |
255 | if (data[i] == '\n') { | |
256 | lin++; | |
257 | col = pos - i; | |
258 | } | |
259 | } | |
260 | pr_err("Parse Error: %s at %d:%d\n", msg, lin, col); | |
261 | ||
262 | } | |
263 | ||
264 | static int init_xbc_with_error(char *buf, int len) | |
265 | { | |
266 | char *copy = strdup(buf); | |
267 | const char *msg; | |
268 | int ret, pos; | |
269 | ||
270 | if (!copy) | |
271 | return -ENOMEM; | |
272 | ||
bdac5c2b | 273 | ret = xbc_init(buf, len, &msg, &pos); |
d052e1c6 MH |
274 | if (ret < 0) |
275 | show_xbc_error(copy, msg, pos); | |
276 | free(copy); | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
483ce670 | 281 | static int show_xbc(const char *path, bool list) |
950313eb MH |
282 | { |
283 | int ret, fd; | |
284 | char *buf = NULL; | |
d052e1c6 MH |
285 | struct stat st; |
286 | ||
287 | ret = stat(path, &st); | |
288 | if (ret < 0) { | |
a61ea637 MH |
289 | ret = -errno; |
290 | pr_err("Failed to stat %s: %d\n", path, ret); | |
291 | return ret; | |
d052e1c6 | 292 | } |
950313eb MH |
293 | |
294 | fd = open(path, O_RDONLY); | |
295 | if (fd < 0) { | |
a61ea637 MH |
296 | ret = -errno; |
297 | pr_err("Failed to open initrd %s: %d\n", path, ret); | |
298 | return ret; | |
950313eb MH |
299 | } |
300 | ||
301 | ret = load_xbc_from_initrd(fd, &buf); | |
d052e1c6 | 302 | close(fd); |
f91cb5b7 | 303 | if (ret < 0) { |
97378001 | 304 | pr_err("Failed to load a boot config from initrd: %d\n", ret); |
f91cb5b7 MH |
305 | goto out; |
306 | } | |
d052e1c6 MH |
307 | /* Assume a bootconfig file if it is enough small */ |
308 | if (ret == 0 && st.st_size <= XBC_DATA_MAX) { | |
309 | ret = load_xbc_file(path, &buf); | |
310 | if (ret < 0) { | |
311 | pr_err("Failed to load a boot config: %d\n", ret); | |
312 | goto out; | |
313 | } | |
314 | if (init_xbc_with_error(buf, ret) < 0) | |
315 | goto out; | |
316 | } | |
e4f70b7b MH |
317 | if (list) |
318 | xbc_show_list(); | |
319 | else | |
320 | xbc_show_compact_tree(); | |
f91cb5b7 MH |
321 | ret = 0; |
322 | out: | |
950313eb MH |
323 | free(buf); |
324 | ||
325 | return ret; | |
326 | } | |
327 | ||
483ce670 | 328 | static int delete_xbc(const char *path) |
950313eb MH |
329 | { |
330 | struct stat stat; | |
331 | int ret = 0, fd, size; | |
332 | char *buf = NULL; | |
333 | ||
334 | fd = open(path, O_RDWR); | |
335 | if (fd < 0) { | |
a61ea637 MH |
336 | ret = -errno; |
337 | pr_err("Failed to open initrd %s: %d\n", path, ret); | |
338 | return ret; | |
950313eb MH |
339 | } |
340 | ||
950313eb | 341 | size = load_xbc_from_initrd(fd, &buf); |
950313eb MH |
342 | if (size < 0) { |
343 | ret = size; | |
97378001 | 344 | pr_err("Failed to load a boot config from initrd: %d\n", ret); |
950313eb MH |
345 | } else if (size > 0) { |
346 | ret = fstat(fd, &stat); | |
347 | if (!ret) | |
85c46b78 MH |
348 | ret = ftruncate(fd, stat.st_size |
349 | - size - 8 - BOOTCONFIG_MAGIC_LEN); | |
950313eb MH |
350 | if (ret) |
351 | ret = -errno; | |
352 | } /* Ignore if there is no boot config in initrd */ | |
353 | ||
354 | close(fd); | |
355 | free(buf); | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
483ce670 | 360 | static int apply_xbc(const char *path, const char *xbc_path) |
950313eb | 361 | { |
e1cef2d4 MH |
362 | char *buf, *data, *p; |
363 | size_t total_size; | |
a995e6bc | 364 | struct stat stat; |
e1cef2d4 | 365 | const char *msg; |
4f292c48 | 366 | uint32_t size, csum; |
e1cef2d4 | 367 | int pos, pad; |
950313eb MH |
368 | int ret, fd; |
369 | ||
370 | ret = load_xbc_file(xbc_path, &buf); | |
371 | if (ret < 0) { | |
97378001 | 372 | pr_err("Failed to load %s : %d\n", xbc_path, ret); |
950313eb MH |
373 | return ret; |
374 | } | |
375 | size = strlen(buf) + 1; | |
99f4f5d6 | 376 | csum = xbc_calc_checksum(buf, size); |
950313eb | 377 | |
e1cef2d4 MH |
378 | /* Backup the bootconfig data */ |
379 | data = calloc(size + BOOTCONFIG_ALIGN + | |
4f292c48 | 380 | sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN, 1); |
950313eb MH |
381 | if (!data) |
382 | return -ENOMEM; | |
e1cef2d4 | 383 | memcpy(data, buf, size); |
950313eb MH |
384 | |
385 | /* Check the data format */ | |
bdac5c2b | 386 | ret = xbc_init(buf, size, &msg, &pos); |
950313eb | 387 | if (ret < 0) { |
89b74cac | 388 | show_xbc_error(data, msg, pos); |
950313eb MH |
389 | free(data); |
390 | free(buf); | |
89b74cac | 391 | |
950313eb MH |
392 | return ret; |
393 | } | |
394 | printf("Apply %s to %s\n", xbc_path, path); | |
e306220c | 395 | xbc_get_info(&ret, NULL); |
0f0d0a77 | 396 | printf("\tNumber of nodes: %d\n", ret); |
950313eb MH |
397 | printf("\tSize: %u bytes\n", (unsigned int)size); |
398 | printf("\tChecksum: %d\n", (unsigned int)csum); | |
399 | ||
400 | /* TODO: Check the options by schema */ | |
115d4d08 | 401 | xbc_exit(); |
950313eb MH |
402 | free(buf); |
403 | ||
404 | /* Remove old boot config if exists */ | |
405 | ret = delete_xbc(path); | |
406 | if (ret < 0) { | |
97378001 | 407 | pr_err("Failed to delete previous boot config: %d\n", ret); |
88426044 | 408 | free(data); |
950313eb MH |
409 | return ret; |
410 | } | |
411 | ||
412 | /* Apply new one */ | |
413 | fd = open(path, O_RDWR | O_APPEND); | |
414 | if (fd < 0) { | |
a61ea637 MH |
415 | ret = -errno; |
416 | pr_err("Failed to open %s: %d\n", path, ret); | |
88426044 | 417 | free(data); |
a61ea637 | 418 | return ret; |
950313eb MH |
419 | } |
420 | /* TODO: Ensure the @path is initramfs/initrd image */ | |
a995e6bc | 421 | if (fstat(fd, &stat) < 0) { |
e8ba0b2b | 422 | ret = -errno; |
a995e6bc MH |
423 | pr_err("Failed to get the size of %s\n", path); |
424 | goto out; | |
425 | } | |
e1cef2d4 MH |
426 | |
427 | /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */ | |
4f292c48 | 428 | total_size = stat.st_size + size + sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN; |
e1cef2d4 MH |
429 | pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size; |
430 | size += pad; | |
431 | ||
432 | /* Add a footer */ | |
433 | p = data + size; | |
4f292c48 MH |
434 | *(uint32_t *)p = htole32(size); |
435 | p += sizeof(uint32_t); | |
e1cef2d4 | 436 | |
4f292c48 MH |
437 | *(uint32_t *)p = htole32(csum); |
438 | p += sizeof(uint32_t); | |
e1cef2d4 MH |
439 | |
440 | memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); | |
441 | p += BOOTCONFIG_MAGIC_LEN; | |
442 | ||
443 | total_size = p - data; | |
444 | ||
445 | ret = write(fd, data, total_size); | |
446 | if (ret < total_size) { | |
a995e6bc MH |
447 | if (ret < 0) |
448 | ret = -errno; | |
97378001 | 449 | pr_err("Failed to apply a boot config: %d\n", ret); |
e1cef2d4 MH |
450 | if (ret >= 0) |
451 | goto out_rollback; | |
452 | } else | |
453 | ret = 0; | |
454 | ||
88426044 | 455 | out: |
950313eb MH |
456 | close(fd); |
457 | free(data); | |
458 | ||
88426044 | 459 | return ret; |
a995e6bc MH |
460 | |
461 | out_rollback: | |
462 | /* Map the partial write to -ENOSPC */ | |
463 | if (ret >= 0) | |
464 | ret = -ENOSPC; | |
465 | if (ftruncate(fd, stat.st_size) < 0) { | |
466 | ret = -errno; | |
467 | pr_err("Failed to rollback the write error: %d\n", ret); | |
468 | pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path); | |
469 | } | |
470 | goto out; | |
950313eb MH |
471 | } |
472 | ||
483ce670 | 473 | static int usage(void) |
950313eb MH |
474 | { |
475 | printf("Usage: bootconfig [OPTIONS] <INITRD>\n" | |
d052e1c6 | 476 | "Or bootconfig <CONFIG>\n" |
950313eb MH |
477 | " Apply, delete or show boot config to initrd.\n" |
478 | " Options:\n" | |
479 | " -a <config>: Apply boot config to initrd\n" | |
e4f70b7b MH |
480 | " -d : Delete boot config file from initrd\n" |
481 | " -l : list boot config in initrd or file\n\n" | |
d052e1c6 | 482 | " If no option is given, show the bootconfig in the given file.\n"); |
950313eb MH |
483 | return -1; |
484 | } | |
485 | ||
486 | int main(int argc, char **argv) | |
487 | { | |
488 | char *path = NULL; | |
489 | char *apply = NULL; | |
e4f70b7b | 490 | bool delete = false, list = false; |
950313eb MH |
491 | int opt; |
492 | ||
e4f70b7b | 493 | while ((opt = getopt(argc, argv, "hda:l")) != -1) { |
950313eb MH |
494 | switch (opt) { |
495 | case 'd': | |
496 | delete = true; | |
497 | break; | |
498 | case 'a': | |
499 | apply = optarg; | |
500 | break; | |
e4f70b7b MH |
501 | case 'l': |
502 | list = true; | |
503 | break; | |
950313eb MH |
504 | case 'h': |
505 | default: | |
506 | return usage(); | |
507 | } | |
508 | } | |
509 | ||
e4f70b7b MH |
510 | if ((apply && delete) || (delete && list) || (apply && list)) { |
511 | pr_err("Error: You can give one of -a, -d or -l at once.\n"); | |
950313eb MH |
512 | return usage(); |
513 | } | |
514 | ||
515 | if (optind >= argc) { | |
97378001 | 516 | pr_err("Error: No initrd is specified.\n"); |
950313eb MH |
517 | return usage(); |
518 | } | |
519 | ||
520 | path = argv[optind]; | |
521 | ||
522 | if (apply) | |
523 | return apply_xbc(path, apply); | |
524 | else if (delete) | |
525 | return delete_xbc(path); | |
526 | ||
e4f70b7b | 527 | return show_xbc(path, list); |
950313eb | 528 | } |