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