1950313ebSMasami Hiramatsu // SPDX-License-Identifier: GPL-2.0 2950313ebSMasami Hiramatsu /* 3950313ebSMasami Hiramatsu * Boot config tool for initrd image 4950313ebSMasami Hiramatsu */ 5950313ebSMasami Hiramatsu #include <stdio.h> 6950313ebSMasami Hiramatsu #include <stdlib.h> 7950313ebSMasami Hiramatsu #include <sys/types.h> 8950313ebSMasami Hiramatsu #include <sys/stat.h> 9950313ebSMasami Hiramatsu #include <fcntl.h> 10950313ebSMasami Hiramatsu #include <unistd.h> 11950313ebSMasami Hiramatsu #include <string.h> 12950313ebSMasami Hiramatsu #include <errno.h> 13e8684358SMasami Hiramatsu #include <endian.h> 14950313ebSMasami Hiramatsu 15950313ebSMasami Hiramatsu #include <linux/kernel.h> 16950313ebSMasami Hiramatsu #include <linux/bootconfig.h> 17950313ebSMasami Hiramatsu 18e4f70b7bSMasami Hiramatsu static int xbc_show_value(struct xbc_node *node, bool semicolon) 19950313ebSMasami Hiramatsu { 20e4f70b7bSMasami Hiramatsu const char *val, *eol; 21272da327SMasami Hiramatsu char q; 22950313ebSMasami Hiramatsu int i = 0; 23950313ebSMasami Hiramatsu 24e4f70b7bSMasami Hiramatsu eol = semicolon ? ";\n" : "\n"; 25950313ebSMasami Hiramatsu xbc_array_for_each_value(node, val) { 26272da327SMasami Hiramatsu if (strchr(val, '"')) 27272da327SMasami Hiramatsu q = '\''; 28272da327SMasami Hiramatsu else 29272da327SMasami Hiramatsu q = '"'; 30ca24306dSMasami Hiramatsu printf("%c%s%c%s", q, val, q, xbc_node_is_array(node) ? ", " : eol); 31950313ebSMasami Hiramatsu i++; 32950313ebSMasami Hiramatsu } 33950313ebSMasami Hiramatsu return i; 34950313ebSMasami Hiramatsu } 35950313ebSMasami Hiramatsu 36950313ebSMasami Hiramatsu static void xbc_show_compact_tree(void) 37950313ebSMasami Hiramatsu { 38*e5efaeb8SMasami Hiramatsu struct xbc_node *node, *cnode = NULL, *vnode; 39950313ebSMasami Hiramatsu int depth = 0, i; 40950313ebSMasami Hiramatsu 41950313ebSMasami Hiramatsu node = xbc_root_node(); 42950313ebSMasami Hiramatsu while (node && xbc_node_is_key(node)) { 43950313ebSMasami Hiramatsu for (i = 0; i < depth; i++) 44950313ebSMasami Hiramatsu printf("\t"); 45*e5efaeb8SMasami Hiramatsu if (!cnode) 46950313ebSMasami Hiramatsu cnode = xbc_node_get_child(node); 47950313ebSMasami Hiramatsu while (cnode && xbc_node_is_key(cnode) && !cnode->next) { 48*e5efaeb8SMasami Hiramatsu vnode = xbc_node_get_child(cnode); 49*e5efaeb8SMasami Hiramatsu /* 50*e5efaeb8SMasami Hiramatsu * If @cnode has value and subkeys, this 51*e5efaeb8SMasami Hiramatsu * should show it as below. 52*e5efaeb8SMasami Hiramatsu * 53*e5efaeb8SMasami Hiramatsu * key(@node) { 54*e5efaeb8SMasami Hiramatsu * key(@cnode) = value; 55*e5efaeb8SMasami Hiramatsu * key(@cnode) { 56*e5efaeb8SMasami Hiramatsu * subkeys; 57*e5efaeb8SMasami Hiramatsu * } 58*e5efaeb8SMasami Hiramatsu * } 59*e5efaeb8SMasami Hiramatsu */ 60*e5efaeb8SMasami Hiramatsu if (vnode && xbc_node_is_value(vnode) && vnode->next) 61*e5efaeb8SMasami Hiramatsu break; 62950313ebSMasami Hiramatsu printf("%s.", xbc_node_get_data(node)); 63950313ebSMasami Hiramatsu node = cnode; 64*e5efaeb8SMasami Hiramatsu cnode = vnode; 65950313ebSMasami Hiramatsu } 66950313ebSMasami Hiramatsu if (cnode && xbc_node_is_key(cnode)) { 67950313ebSMasami Hiramatsu printf("%s {\n", xbc_node_get_data(node)); 68950313ebSMasami Hiramatsu depth++; 69950313ebSMasami Hiramatsu node = cnode; 70*e5efaeb8SMasami Hiramatsu cnode = NULL; 71950313ebSMasami Hiramatsu continue; 72950313ebSMasami Hiramatsu } else if (cnode && xbc_node_is_value(cnode)) { 73950313ebSMasami Hiramatsu printf("%s = ", xbc_node_get_data(node)); 74e4f70b7bSMasami Hiramatsu xbc_show_value(cnode, true); 75*e5efaeb8SMasami Hiramatsu /* 76*e5efaeb8SMasami Hiramatsu * If @node has value and subkeys, continue 77*e5efaeb8SMasami Hiramatsu * looping on subkeys with same node. 78*e5efaeb8SMasami Hiramatsu */ 79*e5efaeb8SMasami Hiramatsu if (cnode->next) { 80*e5efaeb8SMasami Hiramatsu cnode = xbc_node_get_next(cnode); 81*e5efaeb8SMasami Hiramatsu continue; 82*e5efaeb8SMasami Hiramatsu } 83950313ebSMasami Hiramatsu } else { 84950313ebSMasami Hiramatsu printf("%s;\n", xbc_node_get_data(node)); 85950313ebSMasami Hiramatsu } 86*e5efaeb8SMasami Hiramatsu cnode = NULL; 87950313ebSMasami Hiramatsu 88950313ebSMasami Hiramatsu if (node->next) { 89950313ebSMasami Hiramatsu node = xbc_node_get_next(node); 90950313ebSMasami Hiramatsu continue; 91950313ebSMasami Hiramatsu } 92950313ebSMasami Hiramatsu while (!node->next) { 93950313ebSMasami Hiramatsu node = xbc_node_get_parent(node); 94950313ebSMasami Hiramatsu if (!node) 95950313ebSMasami Hiramatsu return; 96950313ebSMasami Hiramatsu if (!xbc_node_get_child(node)->next) 97950313ebSMasami Hiramatsu continue; 98*e5efaeb8SMasami Hiramatsu if (depth) { 99950313ebSMasami Hiramatsu depth--; 100950313ebSMasami Hiramatsu for (i = 0; i < depth; i++) 101950313ebSMasami Hiramatsu printf("\t"); 102950313ebSMasami Hiramatsu printf("}\n"); 103950313ebSMasami Hiramatsu } 104*e5efaeb8SMasami Hiramatsu } 105950313ebSMasami Hiramatsu node = xbc_node_get_next(node); 106950313ebSMasami Hiramatsu } 107950313ebSMasami Hiramatsu } 108950313ebSMasami Hiramatsu 109e4f70b7bSMasami Hiramatsu static void xbc_show_list(void) 110e4f70b7bSMasami Hiramatsu { 111e4f70b7bSMasami Hiramatsu char key[XBC_KEYLEN_MAX]; 112e4f70b7bSMasami Hiramatsu struct xbc_node *leaf; 113e4f70b7bSMasami Hiramatsu const char *val; 114e4f70b7bSMasami Hiramatsu 115e4f70b7bSMasami Hiramatsu xbc_for_each_key_value(leaf, val) { 116*e5efaeb8SMasami Hiramatsu if (xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX) < 0) { 117*e5efaeb8SMasami Hiramatsu fprintf(stderr, "Failed to compose key %d\n", ret); 118e4f70b7bSMasami Hiramatsu break; 119*e5efaeb8SMasami Hiramatsu } 120e4f70b7bSMasami Hiramatsu printf("%s = ", key); 121e4f70b7bSMasami Hiramatsu if (!val || val[0] == '\0') { 122e4f70b7bSMasami Hiramatsu printf("\"\"\n"); 123e4f70b7bSMasami Hiramatsu continue; 124e4f70b7bSMasami Hiramatsu } 125e4f70b7bSMasami Hiramatsu xbc_show_value(xbc_node_get_child(leaf), false); 126e4f70b7bSMasami Hiramatsu } 127e4f70b7bSMasami Hiramatsu } 128e4f70b7bSMasami Hiramatsu 129950313ebSMasami Hiramatsu /* Simple real checksum */ 130483ce670SMasami Hiramatsu static int checksum(unsigned char *buf, int len) 131950313ebSMasami Hiramatsu { 132950313ebSMasami Hiramatsu int i, sum = 0; 133950313ebSMasami Hiramatsu 134950313ebSMasami Hiramatsu for (i = 0; i < len; i++) 135950313ebSMasami Hiramatsu sum += buf[i]; 136950313ebSMasami Hiramatsu 137950313ebSMasami Hiramatsu return sum; 138950313ebSMasami Hiramatsu } 139950313ebSMasami Hiramatsu 140950313ebSMasami Hiramatsu #define PAGE_SIZE 4096 141950313ebSMasami Hiramatsu 142483ce670SMasami Hiramatsu static int load_xbc_fd(int fd, char **buf, int size) 143950313ebSMasami Hiramatsu { 144950313ebSMasami Hiramatsu int ret; 145950313ebSMasami Hiramatsu 146950313ebSMasami Hiramatsu *buf = malloc(size + 1); 147950313ebSMasami Hiramatsu if (!*buf) 148950313ebSMasami Hiramatsu return -ENOMEM; 149950313ebSMasami Hiramatsu 150950313ebSMasami Hiramatsu ret = read(fd, *buf, size); 151950313ebSMasami Hiramatsu if (ret < 0) 152950313ebSMasami Hiramatsu return -errno; 153950313ebSMasami Hiramatsu (*buf)[size] = '\0'; 154950313ebSMasami Hiramatsu 155950313ebSMasami Hiramatsu return ret; 156950313ebSMasami Hiramatsu } 157950313ebSMasami Hiramatsu 158950313ebSMasami Hiramatsu /* Return the read size or -errno */ 159483ce670SMasami Hiramatsu static int load_xbc_file(const char *path, char **buf) 160950313ebSMasami Hiramatsu { 161950313ebSMasami Hiramatsu struct stat stat; 162950313ebSMasami Hiramatsu int fd, ret; 163950313ebSMasami Hiramatsu 164950313ebSMasami Hiramatsu fd = open(path, O_RDONLY); 165950313ebSMasami Hiramatsu if (fd < 0) 166950313ebSMasami Hiramatsu return -errno; 167950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 168950313ebSMasami Hiramatsu if (ret < 0) 169950313ebSMasami Hiramatsu return -errno; 170950313ebSMasami Hiramatsu 171950313ebSMasami Hiramatsu ret = load_xbc_fd(fd, buf, stat.st_size); 172950313ebSMasami Hiramatsu 173950313ebSMasami Hiramatsu close(fd); 174950313ebSMasami Hiramatsu 175950313ebSMasami Hiramatsu return ret; 176950313ebSMasami Hiramatsu } 177950313ebSMasami Hiramatsu 178a61ea637SMasami Hiramatsu static int pr_errno(const char *msg, int err) 179a61ea637SMasami Hiramatsu { 180a61ea637SMasami Hiramatsu pr_err("%s: %d\n", msg, err); 181a61ea637SMasami Hiramatsu return err; 182a61ea637SMasami Hiramatsu } 183a61ea637SMasami Hiramatsu 184483ce670SMasami Hiramatsu static int load_xbc_from_initrd(int fd, char **buf) 185950313ebSMasami Hiramatsu { 186950313ebSMasami Hiramatsu struct stat stat; 187950313ebSMasami Hiramatsu int ret; 188950313ebSMasami Hiramatsu u32 size = 0, csum = 0, rcsum; 18985c46b78SMasami Hiramatsu char magic[BOOTCONFIG_MAGIC_LEN]; 19089b74cacSMasami Hiramatsu const char *msg; 191950313ebSMasami Hiramatsu 192950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 193950313ebSMasami Hiramatsu if (ret < 0) 194950313ebSMasami Hiramatsu return -errno; 195950313ebSMasami Hiramatsu 19685c46b78SMasami Hiramatsu if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN) 197950313ebSMasami Hiramatsu return 0; 198950313ebSMasami Hiramatsu 199a61ea637SMasami Hiramatsu if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) 200a61ea637SMasami Hiramatsu return pr_errno("Failed to lseek for magic", -errno); 201a61ea637SMasami Hiramatsu 20285c46b78SMasami Hiramatsu if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0) 203a61ea637SMasami Hiramatsu return pr_errno("Failed to read", -errno); 204a61ea637SMasami Hiramatsu 20585c46b78SMasami Hiramatsu /* Check the bootconfig magic bytes */ 20685c46b78SMasami Hiramatsu if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0) 20785c46b78SMasami Hiramatsu return 0; 20885c46b78SMasami Hiramatsu 209a61ea637SMasami Hiramatsu if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) 210a61ea637SMasami Hiramatsu return pr_errno("Failed to lseek for size", -errno); 211950313ebSMasami Hiramatsu 212950313ebSMasami Hiramatsu if (read(fd, &size, sizeof(u32)) < 0) 213a61ea637SMasami Hiramatsu return pr_errno("Failed to read size", -errno); 214e8684358SMasami Hiramatsu size = le32toh(size); 215950313ebSMasami Hiramatsu 216950313ebSMasami Hiramatsu if (read(fd, &csum, sizeof(u32)) < 0) 217a61ea637SMasami Hiramatsu return pr_errno("Failed to read checksum", -errno); 218e8684358SMasami Hiramatsu csum = le32toh(csum); 219950313ebSMasami Hiramatsu 22085c46b78SMasami Hiramatsu /* Wrong size error */ 22185c46b78SMasami Hiramatsu if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) { 22285c46b78SMasami Hiramatsu pr_err("bootconfig size is too big\n"); 22385c46b78SMasami Hiramatsu return -E2BIG; 22485c46b78SMasami Hiramatsu } 225950313ebSMasami Hiramatsu 22685c46b78SMasami Hiramatsu if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN), 227a61ea637SMasami Hiramatsu SEEK_SET) < 0) 228a61ea637SMasami Hiramatsu return pr_errno("Failed to lseek", -errno); 229950313ebSMasami Hiramatsu 230950313ebSMasami Hiramatsu ret = load_xbc_fd(fd, buf, size); 231950313ebSMasami Hiramatsu if (ret < 0) 232950313ebSMasami Hiramatsu return ret; 233950313ebSMasami Hiramatsu 23485c46b78SMasami Hiramatsu /* Wrong Checksum */ 235950313ebSMasami Hiramatsu rcsum = checksum((unsigned char *)*buf, size); 236950313ebSMasami Hiramatsu if (csum != rcsum) { 23797378001SMasami Hiramatsu pr_err("checksum error: %d != %d\n", csum, rcsum); 23885c46b78SMasami Hiramatsu return -EINVAL; 239950313ebSMasami Hiramatsu } 240950313ebSMasami Hiramatsu 24189b74cacSMasami Hiramatsu ret = xbc_init(*buf, &msg, NULL); 24285c46b78SMasami Hiramatsu /* Wrong data */ 24389b74cacSMasami Hiramatsu if (ret < 0) { 24489b74cacSMasami Hiramatsu pr_err("parse error: %s.\n", msg); 24585c46b78SMasami Hiramatsu return ret; 24689b74cacSMasami Hiramatsu } 247950313ebSMasami Hiramatsu 248950313ebSMasami Hiramatsu return size; 249950313ebSMasami Hiramatsu } 250950313ebSMasami Hiramatsu 251d052e1c6SMasami Hiramatsu static void show_xbc_error(const char *data, const char *msg, int pos) 252d052e1c6SMasami Hiramatsu { 253d052e1c6SMasami Hiramatsu int lin = 1, col, i; 254d052e1c6SMasami Hiramatsu 255d052e1c6SMasami Hiramatsu if (pos < 0) { 256d052e1c6SMasami Hiramatsu pr_err("Error: %s.\n", msg); 257d052e1c6SMasami Hiramatsu return; 258d052e1c6SMasami Hiramatsu } 259d052e1c6SMasami Hiramatsu 260d052e1c6SMasami Hiramatsu /* Note that pos starts from 0 but lin and col should start from 1. */ 261d052e1c6SMasami Hiramatsu col = pos + 1; 262d052e1c6SMasami Hiramatsu for (i = 0; i < pos; i++) { 263d052e1c6SMasami Hiramatsu if (data[i] == '\n') { 264d052e1c6SMasami Hiramatsu lin++; 265d052e1c6SMasami Hiramatsu col = pos - i; 266d052e1c6SMasami Hiramatsu } 267d052e1c6SMasami Hiramatsu } 268d052e1c6SMasami Hiramatsu pr_err("Parse Error: %s at %d:%d\n", msg, lin, col); 269d052e1c6SMasami Hiramatsu 270d052e1c6SMasami Hiramatsu } 271d052e1c6SMasami Hiramatsu 272d052e1c6SMasami Hiramatsu static int init_xbc_with_error(char *buf, int len) 273d052e1c6SMasami Hiramatsu { 274d052e1c6SMasami Hiramatsu char *copy = strdup(buf); 275d052e1c6SMasami Hiramatsu const char *msg; 276d052e1c6SMasami Hiramatsu int ret, pos; 277d052e1c6SMasami Hiramatsu 278d052e1c6SMasami Hiramatsu if (!copy) 279d052e1c6SMasami Hiramatsu return -ENOMEM; 280d052e1c6SMasami Hiramatsu 281d052e1c6SMasami Hiramatsu ret = xbc_init(buf, &msg, &pos); 282d052e1c6SMasami Hiramatsu if (ret < 0) 283d052e1c6SMasami Hiramatsu show_xbc_error(copy, msg, pos); 284d052e1c6SMasami Hiramatsu free(copy); 285d052e1c6SMasami Hiramatsu 286d052e1c6SMasami Hiramatsu return ret; 287d052e1c6SMasami Hiramatsu } 288d052e1c6SMasami Hiramatsu 289483ce670SMasami Hiramatsu static int show_xbc(const char *path, bool list) 290950313ebSMasami Hiramatsu { 291950313ebSMasami Hiramatsu int ret, fd; 292950313ebSMasami Hiramatsu char *buf = NULL; 293d052e1c6SMasami Hiramatsu struct stat st; 294d052e1c6SMasami Hiramatsu 295d052e1c6SMasami Hiramatsu ret = stat(path, &st); 296d052e1c6SMasami Hiramatsu if (ret < 0) { 297a61ea637SMasami Hiramatsu ret = -errno; 298a61ea637SMasami Hiramatsu pr_err("Failed to stat %s: %d\n", path, ret); 299a61ea637SMasami Hiramatsu return ret; 300d052e1c6SMasami Hiramatsu } 301950313ebSMasami Hiramatsu 302950313ebSMasami Hiramatsu fd = open(path, O_RDONLY); 303950313ebSMasami Hiramatsu if (fd < 0) { 304a61ea637SMasami Hiramatsu ret = -errno; 305a61ea637SMasami Hiramatsu pr_err("Failed to open initrd %s: %d\n", path, ret); 306a61ea637SMasami Hiramatsu return ret; 307950313ebSMasami Hiramatsu } 308950313ebSMasami Hiramatsu 309950313ebSMasami Hiramatsu ret = load_xbc_from_initrd(fd, &buf); 310d052e1c6SMasami Hiramatsu close(fd); 311f91cb5b7SMasami Hiramatsu if (ret < 0) { 31297378001SMasami Hiramatsu pr_err("Failed to load a boot config from initrd: %d\n", ret); 313f91cb5b7SMasami Hiramatsu goto out; 314f91cb5b7SMasami Hiramatsu } 315d052e1c6SMasami Hiramatsu /* Assume a bootconfig file if it is enough small */ 316d052e1c6SMasami Hiramatsu if (ret == 0 && st.st_size <= XBC_DATA_MAX) { 317d052e1c6SMasami Hiramatsu ret = load_xbc_file(path, &buf); 318d052e1c6SMasami Hiramatsu if (ret < 0) { 319d052e1c6SMasami Hiramatsu pr_err("Failed to load a boot config: %d\n", ret); 320d052e1c6SMasami Hiramatsu goto out; 321d052e1c6SMasami Hiramatsu } 322d052e1c6SMasami Hiramatsu if (init_xbc_with_error(buf, ret) < 0) 323d052e1c6SMasami Hiramatsu goto out; 324d052e1c6SMasami Hiramatsu } 325e4f70b7bSMasami Hiramatsu if (list) 326e4f70b7bSMasami Hiramatsu xbc_show_list(); 327e4f70b7bSMasami Hiramatsu else 328950313ebSMasami Hiramatsu xbc_show_compact_tree(); 329f91cb5b7SMasami Hiramatsu ret = 0; 330f91cb5b7SMasami Hiramatsu out: 331950313ebSMasami Hiramatsu free(buf); 332950313ebSMasami Hiramatsu 333950313ebSMasami Hiramatsu return ret; 334950313ebSMasami Hiramatsu } 335950313ebSMasami Hiramatsu 336483ce670SMasami Hiramatsu static int delete_xbc(const char *path) 337950313ebSMasami Hiramatsu { 338950313ebSMasami Hiramatsu struct stat stat; 339950313ebSMasami Hiramatsu int ret = 0, fd, size; 340950313ebSMasami Hiramatsu char *buf = NULL; 341950313ebSMasami Hiramatsu 342950313ebSMasami Hiramatsu fd = open(path, O_RDWR); 343950313ebSMasami Hiramatsu if (fd < 0) { 344a61ea637SMasami Hiramatsu ret = -errno; 345a61ea637SMasami Hiramatsu pr_err("Failed to open initrd %s: %d\n", path, ret); 346a61ea637SMasami Hiramatsu return ret; 347950313ebSMasami Hiramatsu } 348950313ebSMasami Hiramatsu 349950313ebSMasami Hiramatsu size = load_xbc_from_initrd(fd, &buf); 350950313ebSMasami Hiramatsu if (size < 0) { 351950313ebSMasami Hiramatsu ret = size; 35297378001SMasami Hiramatsu pr_err("Failed to load a boot config from initrd: %d\n", ret); 353950313ebSMasami Hiramatsu } else if (size > 0) { 354950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 355950313ebSMasami Hiramatsu if (!ret) 35685c46b78SMasami Hiramatsu ret = ftruncate(fd, stat.st_size 35785c46b78SMasami Hiramatsu - size - 8 - BOOTCONFIG_MAGIC_LEN); 358950313ebSMasami Hiramatsu if (ret) 359950313ebSMasami Hiramatsu ret = -errno; 360950313ebSMasami Hiramatsu } /* Ignore if there is no boot config in initrd */ 361950313ebSMasami Hiramatsu 362950313ebSMasami Hiramatsu close(fd); 363950313ebSMasami Hiramatsu free(buf); 364950313ebSMasami Hiramatsu 365950313ebSMasami Hiramatsu return ret; 366950313ebSMasami Hiramatsu } 367950313ebSMasami Hiramatsu 368483ce670SMasami Hiramatsu static int apply_xbc(const char *path, const char *xbc_path) 369950313ebSMasami Hiramatsu { 370e1cef2d4SMasami Hiramatsu char *buf, *data, *p; 371e1cef2d4SMasami Hiramatsu size_t total_size; 372a995e6bcSMasami Hiramatsu struct stat stat; 37389b74cacSMasami Hiramatsu const char *msg; 374e1cef2d4SMasami Hiramatsu u32 size, csum; 375e1cef2d4SMasami Hiramatsu int pos, pad; 376e1cef2d4SMasami Hiramatsu int ret, fd; 377950313ebSMasami Hiramatsu 378950313ebSMasami Hiramatsu ret = load_xbc_file(xbc_path, &buf); 379950313ebSMasami Hiramatsu if (ret < 0) { 38097378001SMasami Hiramatsu pr_err("Failed to load %s : %d\n", xbc_path, ret); 381950313ebSMasami Hiramatsu return ret; 382950313ebSMasami Hiramatsu } 383950313ebSMasami Hiramatsu size = strlen(buf) + 1; 384950313ebSMasami Hiramatsu csum = checksum((unsigned char *)buf, size); 385950313ebSMasami Hiramatsu 386e1cef2d4SMasami Hiramatsu /* Backup the bootconfig data */ 387e1cef2d4SMasami Hiramatsu data = calloc(size + BOOTCONFIG_ALIGN + 388e1cef2d4SMasami Hiramatsu sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1); 389950313ebSMasami Hiramatsu if (!data) 390950313ebSMasami Hiramatsu return -ENOMEM; 391e1cef2d4SMasami Hiramatsu memcpy(data, buf, size); 392950313ebSMasami Hiramatsu 393950313ebSMasami Hiramatsu /* Check the data format */ 39489b74cacSMasami Hiramatsu ret = xbc_init(buf, &msg, &pos); 395950313ebSMasami Hiramatsu if (ret < 0) { 39689b74cacSMasami Hiramatsu show_xbc_error(data, msg, pos); 397950313ebSMasami Hiramatsu free(data); 398950313ebSMasami Hiramatsu free(buf); 39989b74cacSMasami Hiramatsu 400950313ebSMasami Hiramatsu return ret; 401950313ebSMasami Hiramatsu } 402950313ebSMasami Hiramatsu printf("Apply %s to %s\n", xbc_path, path); 4030f0d0a77SMasami Hiramatsu printf("\tNumber of nodes: %d\n", ret); 404950313ebSMasami Hiramatsu printf("\tSize: %u bytes\n", (unsigned int)size); 405950313ebSMasami Hiramatsu printf("\tChecksum: %d\n", (unsigned int)csum); 406950313ebSMasami Hiramatsu 407950313ebSMasami Hiramatsu /* TODO: Check the options by schema */ 408950313ebSMasami Hiramatsu xbc_destroy_all(); 409950313ebSMasami Hiramatsu free(buf); 410950313ebSMasami Hiramatsu 411950313ebSMasami Hiramatsu /* Remove old boot config if exists */ 412950313ebSMasami Hiramatsu ret = delete_xbc(path); 413950313ebSMasami Hiramatsu if (ret < 0) { 41497378001SMasami Hiramatsu pr_err("Failed to delete previous boot config: %d\n", ret); 41588426044SYunfeng Ye free(data); 416950313ebSMasami Hiramatsu return ret; 417950313ebSMasami Hiramatsu } 418950313ebSMasami Hiramatsu 419950313ebSMasami Hiramatsu /* Apply new one */ 420950313ebSMasami Hiramatsu fd = open(path, O_RDWR | O_APPEND); 421950313ebSMasami Hiramatsu if (fd < 0) { 422a61ea637SMasami Hiramatsu ret = -errno; 423a61ea637SMasami Hiramatsu pr_err("Failed to open %s: %d\n", path, ret); 42488426044SYunfeng Ye free(data); 425a61ea637SMasami Hiramatsu return ret; 426950313ebSMasami Hiramatsu } 427950313ebSMasami Hiramatsu /* TODO: Ensure the @path is initramfs/initrd image */ 428a995e6bcSMasami Hiramatsu if (fstat(fd, &stat) < 0) { 429e8ba0b2bSZhen Lei ret = -errno; 430a995e6bcSMasami Hiramatsu pr_err("Failed to get the size of %s\n", path); 43188426044SYunfeng Ye goto out; 432950313ebSMasami Hiramatsu } 433e1cef2d4SMasami Hiramatsu 434e1cef2d4SMasami Hiramatsu /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */ 435e1cef2d4SMasami Hiramatsu total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN; 436e1cef2d4SMasami Hiramatsu pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size; 437e1cef2d4SMasami Hiramatsu size += pad; 438e1cef2d4SMasami Hiramatsu 439e1cef2d4SMasami Hiramatsu /* Add a footer */ 440e1cef2d4SMasami Hiramatsu p = data + size; 441e8684358SMasami Hiramatsu *(u32 *)p = htole32(size); 442e1cef2d4SMasami Hiramatsu p += sizeof(u32); 443e1cef2d4SMasami Hiramatsu 444e8684358SMasami Hiramatsu *(u32 *)p = htole32(csum); 445e1cef2d4SMasami Hiramatsu p += sizeof(u32); 446e1cef2d4SMasami Hiramatsu 447e1cef2d4SMasami Hiramatsu memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); 448e1cef2d4SMasami Hiramatsu p += BOOTCONFIG_MAGIC_LEN; 449e1cef2d4SMasami Hiramatsu 450e1cef2d4SMasami Hiramatsu total_size = p - data; 451e1cef2d4SMasami Hiramatsu 452e1cef2d4SMasami Hiramatsu ret = write(fd, data, total_size); 453e1cef2d4SMasami Hiramatsu if (ret < total_size) { 454a995e6bcSMasami Hiramatsu if (ret < 0) 455a995e6bcSMasami Hiramatsu ret = -errno; 456a995e6bcSMasami Hiramatsu pr_err("Failed to apply a boot config: %d\n", ret); 457e1cef2d4SMasami Hiramatsu if (ret >= 0) 458a995e6bcSMasami Hiramatsu goto out_rollback; 459e1cef2d4SMasami Hiramatsu } else 4609d82ccdaSSteven Rostedt (VMware) ret = 0; 461e1cef2d4SMasami Hiramatsu 46288426044SYunfeng Ye out: 463950313ebSMasami Hiramatsu close(fd); 464950313ebSMasami Hiramatsu free(data); 465950313ebSMasami Hiramatsu 46688426044SYunfeng Ye return ret; 467a995e6bcSMasami Hiramatsu 468a995e6bcSMasami Hiramatsu out_rollback: 469a995e6bcSMasami Hiramatsu /* Map the partial write to -ENOSPC */ 470a995e6bcSMasami Hiramatsu if (ret >= 0) 471a995e6bcSMasami Hiramatsu ret = -ENOSPC; 472a995e6bcSMasami Hiramatsu if (ftruncate(fd, stat.st_size) < 0) { 473a995e6bcSMasami Hiramatsu ret = -errno; 474a995e6bcSMasami Hiramatsu pr_err("Failed to rollback the write error: %d\n", ret); 475a995e6bcSMasami Hiramatsu pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path); 476a995e6bcSMasami Hiramatsu } 477a995e6bcSMasami Hiramatsu goto out; 478950313ebSMasami Hiramatsu } 479950313ebSMasami Hiramatsu 480483ce670SMasami Hiramatsu static int usage(void) 481950313ebSMasami Hiramatsu { 482950313ebSMasami Hiramatsu printf("Usage: bootconfig [OPTIONS] <INITRD>\n" 483d052e1c6SMasami Hiramatsu "Or bootconfig <CONFIG>\n" 484950313ebSMasami Hiramatsu " Apply, delete or show boot config to initrd.\n" 485950313ebSMasami Hiramatsu " Options:\n" 486950313ebSMasami Hiramatsu " -a <config>: Apply boot config to initrd\n" 487e4f70b7bSMasami Hiramatsu " -d : Delete boot config file from initrd\n" 488e4f70b7bSMasami Hiramatsu " -l : list boot config in initrd or file\n\n" 489d052e1c6SMasami Hiramatsu " If no option is given, show the bootconfig in the given file.\n"); 490950313ebSMasami Hiramatsu return -1; 491950313ebSMasami Hiramatsu } 492950313ebSMasami Hiramatsu 493950313ebSMasami Hiramatsu int main(int argc, char **argv) 494950313ebSMasami Hiramatsu { 495950313ebSMasami Hiramatsu char *path = NULL; 496950313ebSMasami Hiramatsu char *apply = NULL; 497e4f70b7bSMasami Hiramatsu bool delete = false, list = false; 498950313ebSMasami Hiramatsu int opt; 499950313ebSMasami Hiramatsu 500e4f70b7bSMasami Hiramatsu while ((opt = getopt(argc, argv, "hda:l")) != -1) { 501950313ebSMasami Hiramatsu switch (opt) { 502950313ebSMasami Hiramatsu case 'd': 503950313ebSMasami Hiramatsu delete = true; 504950313ebSMasami Hiramatsu break; 505950313ebSMasami Hiramatsu case 'a': 506950313ebSMasami Hiramatsu apply = optarg; 507950313ebSMasami Hiramatsu break; 508e4f70b7bSMasami Hiramatsu case 'l': 509e4f70b7bSMasami Hiramatsu list = true; 510e4f70b7bSMasami Hiramatsu break; 511950313ebSMasami Hiramatsu case 'h': 512950313ebSMasami Hiramatsu default: 513950313ebSMasami Hiramatsu return usage(); 514950313ebSMasami Hiramatsu } 515950313ebSMasami Hiramatsu } 516950313ebSMasami Hiramatsu 517e4f70b7bSMasami Hiramatsu if ((apply && delete) || (delete && list) || (apply && list)) { 518e4f70b7bSMasami Hiramatsu pr_err("Error: You can give one of -a, -d or -l at once.\n"); 519950313ebSMasami Hiramatsu return usage(); 520950313ebSMasami Hiramatsu } 521950313ebSMasami Hiramatsu 522950313ebSMasami Hiramatsu if (optind >= argc) { 52397378001SMasami Hiramatsu pr_err("Error: No initrd is specified.\n"); 524950313ebSMasami Hiramatsu return usage(); 525950313ebSMasami Hiramatsu } 526950313ebSMasami Hiramatsu 527950313ebSMasami Hiramatsu path = argv[optind]; 528950313ebSMasami Hiramatsu 529950313ebSMasami Hiramatsu if (apply) 530950313ebSMasami Hiramatsu return apply_xbc(path, apply); 531950313ebSMasami Hiramatsu else if (delete) 532950313ebSMasami Hiramatsu return delete_xbc(path); 533950313ebSMasami Hiramatsu 534e4f70b7bSMasami Hiramatsu return show_xbc(path, list); 535950313ebSMasami Hiramatsu } 536