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