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