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> 13950313ebSMasami Hiramatsu 14950313ebSMasami Hiramatsu #include <linux/kernel.h> 15950313ebSMasami Hiramatsu #include <linux/bootconfig.h> 16950313ebSMasami Hiramatsu 17*e4f70b7bSMasami Hiramatsu static int xbc_show_value(struct xbc_node *node, bool semicolon) 18950313ebSMasami Hiramatsu { 19*e4f70b7bSMasami Hiramatsu const char *val, *eol; 20272da327SMasami Hiramatsu char q; 21950313ebSMasami Hiramatsu int i = 0; 22950313ebSMasami Hiramatsu 23*e4f70b7bSMasami Hiramatsu eol = semicolon ? ";\n" : "\n"; 24950313ebSMasami Hiramatsu xbc_array_for_each_value(node, val) { 25272da327SMasami Hiramatsu if (strchr(val, '"')) 26272da327SMasami Hiramatsu q = '\''; 27272da327SMasami Hiramatsu else 28272da327SMasami Hiramatsu q = '"'; 29*e4f70b7bSMasami Hiramatsu printf("%c%s%c%s", q, val, q, node->next ? ", " : eol); 30950313ebSMasami Hiramatsu i++; 31950313ebSMasami Hiramatsu } 32950313ebSMasami Hiramatsu return i; 33950313ebSMasami Hiramatsu } 34950313ebSMasami Hiramatsu 35950313ebSMasami Hiramatsu static void xbc_show_compact_tree(void) 36950313ebSMasami Hiramatsu { 37950313ebSMasami Hiramatsu struct xbc_node *node, *cnode; 38950313ebSMasami Hiramatsu int depth = 0, i; 39950313ebSMasami Hiramatsu 40950313ebSMasami Hiramatsu node = xbc_root_node(); 41950313ebSMasami Hiramatsu while (node && xbc_node_is_key(node)) { 42950313ebSMasami Hiramatsu for (i = 0; i < depth; i++) 43950313ebSMasami Hiramatsu printf("\t"); 44950313ebSMasami Hiramatsu cnode = xbc_node_get_child(node); 45950313ebSMasami Hiramatsu while (cnode && xbc_node_is_key(cnode) && !cnode->next) { 46950313ebSMasami Hiramatsu printf("%s.", xbc_node_get_data(node)); 47950313ebSMasami Hiramatsu node = cnode; 48950313ebSMasami Hiramatsu cnode = xbc_node_get_child(node); 49950313ebSMasami Hiramatsu } 50950313ebSMasami Hiramatsu if (cnode && xbc_node_is_key(cnode)) { 51950313ebSMasami Hiramatsu printf("%s {\n", xbc_node_get_data(node)); 52950313ebSMasami Hiramatsu depth++; 53950313ebSMasami Hiramatsu node = cnode; 54950313ebSMasami Hiramatsu continue; 55950313ebSMasami Hiramatsu } else if (cnode && xbc_node_is_value(cnode)) { 56950313ebSMasami Hiramatsu printf("%s = ", xbc_node_get_data(node)); 57*e4f70b7bSMasami Hiramatsu xbc_show_value(cnode, true); 58950313ebSMasami Hiramatsu } else { 59950313ebSMasami Hiramatsu printf("%s;\n", xbc_node_get_data(node)); 60950313ebSMasami Hiramatsu } 61950313ebSMasami Hiramatsu 62950313ebSMasami Hiramatsu if (node->next) { 63950313ebSMasami Hiramatsu node = xbc_node_get_next(node); 64950313ebSMasami Hiramatsu continue; 65950313ebSMasami Hiramatsu } 66950313ebSMasami Hiramatsu while (!node->next) { 67950313ebSMasami Hiramatsu node = xbc_node_get_parent(node); 68950313ebSMasami Hiramatsu if (!node) 69950313ebSMasami Hiramatsu return; 70950313ebSMasami Hiramatsu if (!xbc_node_get_child(node)->next) 71950313ebSMasami Hiramatsu continue; 72950313ebSMasami Hiramatsu depth--; 73950313ebSMasami Hiramatsu for (i = 0; i < depth; i++) 74950313ebSMasami Hiramatsu printf("\t"); 75950313ebSMasami Hiramatsu printf("}\n"); 76950313ebSMasami Hiramatsu } 77950313ebSMasami Hiramatsu node = xbc_node_get_next(node); 78950313ebSMasami Hiramatsu } 79950313ebSMasami Hiramatsu } 80950313ebSMasami Hiramatsu 81*e4f70b7bSMasami Hiramatsu static void xbc_show_list(void) 82*e4f70b7bSMasami Hiramatsu { 83*e4f70b7bSMasami Hiramatsu char key[XBC_KEYLEN_MAX]; 84*e4f70b7bSMasami Hiramatsu struct xbc_node *leaf; 85*e4f70b7bSMasami Hiramatsu const char *val; 86*e4f70b7bSMasami Hiramatsu int ret = 0; 87*e4f70b7bSMasami Hiramatsu 88*e4f70b7bSMasami Hiramatsu xbc_for_each_key_value(leaf, val) { 89*e4f70b7bSMasami Hiramatsu ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX); 90*e4f70b7bSMasami Hiramatsu if (ret < 0) 91*e4f70b7bSMasami Hiramatsu break; 92*e4f70b7bSMasami Hiramatsu printf("%s = ", key); 93*e4f70b7bSMasami Hiramatsu if (!val || val[0] == '\0') { 94*e4f70b7bSMasami Hiramatsu printf("\"\"\n"); 95*e4f70b7bSMasami Hiramatsu continue; 96*e4f70b7bSMasami Hiramatsu } 97*e4f70b7bSMasami Hiramatsu xbc_show_value(xbc_node_get_child(leaf), false); 98*e4f70b7bSMasami Hiramatsu } 99*e4f70b7bSMasami Hiramatsu } 100*e4f70b7bSMasami Hiramatsu 101950313ebSMasami Hiramatsu /* Simple real checksum */ 102950313ebSMasami Hiramatsu int checksum(unsigned char *buf, int len) 103950313ebSMasami Hiramatsu { 104950313ebSMasami Hiramatsu int i, sum = 0; 105950313ebSMasami Hiramatsu 106950313ebSMasami Hiramatsu for (i = 0; i < len; i++) 107950313ebSMasami Hiramatsu sum += buf[i]; 108950313ebSMasami Hiramatsu 109950313ebSMasami Hiramatsu return sum; 110950313ebSMasami Hiramatsu } 111950313ebSMasami Hiramatsu 112950313ebSMasami Hiramatsu #define PAGE_SIZE 4096 113950313ebSMasami Hiramatsu 114950313ebSMasami Hiramatsu int load_xbc_fd(int fd, char **buf, int size) 115950313ebSMasami Hiramatsu { 116950313ebSMasami Hiramatsu int ret; 117950313ebSMasami Hiramatsu 118950313ebSMasami Hiramatsu *buf = malloc(size + 1); 119950313ebSMasami Hiramatsu if (!*buf) 120950313ebSMasami Hiramatsu return -ENOMEM; 121950313ebSMasami Hiramatsu 122950313ebSMasami Hiramatsu ret = read(fd, *buf, size); 123950313ebSMasami Hiramatsu if (ret < 0) 124950313ebSMasami Hiramatsu return -errno; 125950313ebSMasami Hiramatsu (*buf)[size] = '\0'; 126950313ebSMasami Hiramatsu 127950313ebSMasami Hiramatsu return ret; 128950313ebSMasami Hiramatsu } 129950313ebSMasami Hiramatsu 130950313ebSMasami Hiramatsu /* Return the read size or -errno */ 131950313ebSMasami Hiramatsu int load_xbc_file(const char *path, char **buf) 132950313ebSMasami Hiramatsu { 133950313ebSMasami Hiramatsu struct stat stat; 134950313ebSMasami Hiramatsu int fd, ret; 135950313ebSMasami Hiramatsu 136950313ebSMasami Hiramatsu fd = open(path, O_RDONLY); 137950313ebSMasami Hiramatsu if (fd < 0) 138950313ebSMasami Hiramatsu return -errno; 139950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 140950313ebSMasami Hiramatsu if (ret < 0) 141950313ebSMasami Hiramatsu return -errno; 142950313ebSMasami Hiramatsu 143950313ebSMasami Hiramatsu ret = load_xbc_fd(fd, buf, stat.st_size); 144950313ebSMasami Hiramatsu 145950313ebSMasami Hiramatsu close(fd); 146950313ebSMasami Hiramatsu 147950313ebSMasami Hiramatsu return ret; 148950313ebSMasami Hiramatsu } 149950313ebSMasami Hiramatsu 150950313ebSMasami Hiramatsu int load_xbc_from_initrd(int fd, char **buf) 151950313ebSMasami Hiramatsu { 152950313ebSMasami Hiramatsu struct stat stat; 153950313ebSMasami Hiramatsu int ret; 154950313ebSMasami Hiramatsu u32 size = 0, csum = 0, rcsum; 15585c46b78SMasami Hiramatsu char magic[BOOTCONFIG_MAGIC_LEN]; 15689b74cacSMasami Hiramatsu const char *msg; 157950313ebSMasami Hiramatsu 158950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 159950313ebSMasami Hiramatsu if (ret < 0) 160950313ebSMasami Hiramatsu return -errno; 161950313ebSMasami Hiramatsu 16285c46b78SMasami Hiramatsu if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN) 163950313ebSMasami Hiramatsu return 0; 164950313ebSMasami Hiramatsu 16585c46b78SMasami Hiramatsu if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) { 16685c46b78SMasami Hiramatsu pr_err("Failed to lseek: %d\n", -errno); 16785c46b78SMasami Hiramatsu return -errno; 16885c46b78SMasami Hiramatsu } 16985c46b78SMasami Hiramatsu if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0) 17085c46b78SMasami Hiramatsu return -errno; 17185c46b78SMasami Hiramatsu /* Check the bootconfig magic bytes */ 17285c46b78SMasami Hiramatsu if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0) 17385c46b78SMasami Hiramatsu return 0; 17485c46b78SMasami Hiramatsu 17585c46b78SMasami Hiramatsu if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) { 17697378001SMasami Hiramatsu pr_err("Failed to lseek: %d\n", -errno); 177950313ebSMasami Hiramatsu return -errno; 178950313ebSMasami Hiramatsu } 179950313ebSMasami Hiramatsu 180950313ebSMasami Hiramatsu if (read(fd, &size, sizeof(u32)) < 0) 181950313ebSMasami Hiramatsu return -errno; 182950313ebSMasami Hiramatsu 183950313ebSMasami Hiramatsu if (read(fd, &csum, sizeof(u32)) < 0) 184950313ebSMasami Hiramatsu return -errno; 185950313ebSMasami Hiramatsu 18685c46b78SMasami Hiramatsu /* Wrong size error */ 18785c46b78SMasami Hiramatsu if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) { 18885c46b78SMasami Hiramatsu pr_err("bootconfig size is too big\n"); 18985c46b78SMasami Hiramatsu return -E2BIG; 19085c46b78SMasami Hiramatsu } 191950313ebSMasami Hiramatsu 19285c46b78SMasami Hiramatsu if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN), 19385c46b78SMasami Hiramatsu SEEK_SET) < 0) { 19497378001SMasami Hiramatsu pr_err("Failed to lseek: %d\n", -errno); 195950313ebSMasami Hiramatsu return -errno; 196950313ebSMasami Hiramatsu } 197950313ebSMasami Hiramatsu 198950313ebSMasami Hiramatsu ret = load_xbc_fd(fd, buf, size); 199950313ebSMasami Hiramatsu if (ret < 0) 200950313ebSMasami Hiramatsu return ret; 201950313ebSMasami Hiramatsu 20285c46b78SMasami Hiramatsu /* Wrong Checksum */ 203950313ebSMasami Hiramatsu rcsum = checksum((unsigned char *)*buf, size); 204950313ebSMasami Hiramatsu if (csum != rcsum) { 20597378001SMasami Hiramatsu pr_err("checksum error: %d != %d\n", csum, rcsum); 20685c46b78SMasami Hiramatsu return -EINVAL; 207950313ebSMasami Hiramatsu } 208950313ebSMasami Hiramatsu 20989b74cacSMasami Hiramatsu ret = xbc_init(*buf, &msg, NULL); 21085c46b78SMasami Hiramatsu /* Wrong data */ 21189b74cacSMasami Hiramatsu if (ret < 0) { 21289b74cacSMasami Hiramatsu pr_err("parse error: %s.\n", msg); 21385c46b78SMasami Hiramatsu return ret; 21489b74cacSMasami Hiramatsu } 215950313ebSMasami Hiramatsu 216950313ebSMasami Hiramatsu return size; 217950313ebSMasami Hiramatsu } 218950313ebSMasami Hiramatsu 219d052e1c6SMasami Hiramatsu static void show_xbc_error(const char *data, const char *msg, int pos) 220d052e1c6SMasami Hiramatsu { 221d052e1c6SMasami Hiramatsu int lin = 1, col, i; 222d052e1c6SMasami Hiramatsu 223d052e1c6SMasami Hiramatsu if (pos < 0) { 224d052e1c6SMasami Hiramatsu pr_err("Error: %s.\n", msg); 225d052e1c6SMasami Hiramatsu return; 226d052e1c6SMasami Hiramatsu } 227d052e1c6SMasami Hiramatsu 228d052e1c6SMasami Hiramatsu /* Note that pos starts from 0 but lin and col should start from 1. */ 229d052e1c6SMasami Hiramatsu col = pos + 1; 230d052e1c6SMasami Hiramatsu for (i = 0; i < pos; i++) { 231d052e1c6SMasami Hiramatsu if (data[i] == '\n') { 232d052e1c6SMasami Hiramatsu lin++; 233d052e1c6SMasami Hiramatsu col = pos - i; 234d052e1c6SMasami Hiramatsu } 235d052e1c6SMasami Hiramatsu } 236d052e1c6SMasami Hiramatsu pr_err("Parse Error: %s at %d:%d\n", msg, lin, col); 237d052e1c6SMasami Hiramatsu 238d052e1c6SMasami Hiramatsu } 239d052e1c6SMasami Hiramatsu 240d052e1c6SMasami Hiramatsu static int init_xbc_with_error(char *buf, int len) 241d052e1c6SMasami Hiramatsu { 242d052e1c6SMasami Hiramatsu char *copy = strdup(buf); 243d052e1c6SMasami Hiramatsu const char *msg; 244d052e1c6SMasami Hiramatsu int ret, pos; 245d052e1c6SMasami Hiramatsu 246d052e1c6SMasami Hiramatsu if (!copy) 247d052e1c6SMasami Hiramatsu return -ENOMEM; 248d052e1c6SMasami Hiramatsu 249d052e1c6SMasami Hiramatsu ret = xbc_init(buf, &msg, &pos); 250d052e1c6SMasami Hiramatsu if (ret < 0) 251d052e1c6SMasami Hiramatsu show_xbc_error(copy, msg, pos); 252d052e1c6SMasami Hiramatsu free(copy); 253d052e1c6SMasami Hiramatsu 254d052e1c6SMasami Hiramatsu return ret; 255d052e1c6SMasami Hiramatsu } 256d052e1c6SMasami Hiramatsu 257*e4f70b7bSMasami Hiramatsu int show_xbc(const char *path, bool list) 258950313ebSMasami Hiramatsu { 259950313ebSMasami Hiramatsu int ret, fd; 260950313ebSMasami Hiramatsu char *buf = NULL; 261d052e1c6SMasami Hiramatsu struct stat st; 262d052e1c6SMasami Hiramatsu 263d052e1c6SMasami Hiramatsu ret = stat(path, &st); 264d052e1c6SMasami Hiramatsu if (ret < 0) { 265d052e1c6SMasami Hiramatsu pr_err("Failed to stat %s: %d\n", path, -errno); 266d052e1c6SMasami Hiramatsu return -errno; 267d052e1c6SMasami Hiramatsu } 268950313ebSMasami Hiramatsu 269950313ebSMasami Hiramatsu fd = open(path, O_RDONLY); 270950313ebSMasami Hiramatsu if (fd < 0) { 27197378001SMasami Hiramatsu pr_err("Failed to open initrd %s: %d\n", path, fd); 272950313ebSMasami Hiramatsu return -errno; 273950313ebSMasami Hiramatsu } 274950313ebSMasami Hiramatsu 275950313ebSMasami Hiramatsu ret = load_xbc_from_initrd(fd, &buf); 276d052e1c6SMasami Hiramatsu close(fd); 277f91cb5b7SMasami Hiramatsu if (ret < 0) { 27897378001SMasami Hiramatsu pr_err("Failed to load a boot config from initrd: %d\n", ret); 279f91cb5b7SMasami Hiramatsu goto out; 280f91cb5b7SMasami Hiramatsu } 281d052e1c6SMasami Hiramatsu /* Assume a bootconfig file if it is enough small */ 282d052e1c6SMasami Hiramatsu if (ret == 0 && st.st_size <= XBC_DATA_MAX) { 283d052e1c6SMasami Hiramatsu ret = load_xbc_file(path, &buf); 284d052e1c6SMasami Hiramatsu if (ret < 0) { 285d052e1c6SMasami Hiramatsu pr_err("Failed to load a boot config: %d\n", ret); 286d052e1c6SMasami Hiramatsu goto out; 287d052e1c6SMasami Hiramatsu } 288d052e1c6SMasami Hiramatsu if (init_xbc_with_error(buf, ret) < 0) 289d052e1c6SMasami Hiramatsu goto out; 290d052e1c6SMasami Hiramatsu } 291*e4f70b7bSMasami Hiramatsu if (list) 292*e4f70b7bSMasami Hiramatsu xbc_show_list(); 293*e4f70b7bSMasami Hiramatsu else 294950313ebSMasami Hiramatsu xbc_show_compact_tree(); 295f91cb5b7SMasami Hiramatsu ret = 0; 296f91cb5b7SMasami Hiramatsu out: 297950313ebSMasami Hiramatsu free(buf); 298950313ebSMasami Hiramatsu 299950313ebSMasami Hiramatsu return ret; 300950313ebSMasami Hiramatsu } 301950313ebSMasami Hiramatsu 302950313ebSMasami Hiramatsu int delete_xbc(const char *path) 303950313ebSMasami Hiramatsu { 304950313ebSMasami Hiramatsu struct stat stat; 305950313ebSMasami Hiramatsu int ret = 0, fd, size; 306950313ebSMasami Hiramatsu char *buf = NULL; 307950313ebSMasami Hiramatsu 308950313ebSMasami Hiramatsu fd = open(path, O_RDWR); 309950313ebSMasami Hiramatsu if (fd < 0) { 31097378001SMasami Hiramatsu pr_err("Failed to open initrd %s: %d\n", path, fd); 311950313ebSMasami Hiramatsu return -errno; 312950313ebSMasami Hiramatsu } 313950313ebSMasami Hiramatsu 314950313ebSMasami Hiramatsu size = load_xbc_from_initrd(fd, &buf); 315950313ebSMasami Hiramatsu if (size < 0) { 316950313ebSMasami Hiramatsu ret = size; 31797378001SMasami Hiramatsu pr_err("Failed to load a boot config from initrd: %d\n", ret); 318950313ebSMasami Hiramatsu } else if (size > 0) { 319950313ebSMasami Hiramatsu ret = fstat(fd, &stat); 320950313ebSMasami Hiramatsu if (!ret) 32185c46b78SMasami Hiramatsu ret = ftruncate(fd, stat.st_size 32285c46b78SMasami Hiramatsu - size - 8 - BOOTCONFIG_MAGIC_LEN); 323950313ebSMasami Hiramatsu if (ret) 324950313ebSMasami Hiramatsu ret = -errno; 325950313ebSMasami Hiramatsu } /* Ignore if there is no boot config in initrd */ 326950313ebSMasami Hiramatsu 327950313ebSMasami Hiramatsu close(fd); 328950313ebSMasami Hiramatsu free(buf); 329950313ebSMasami Hiramatsu 330950313ebSMasami Hiramatsu return ret; 331950313ebSMasami Hiramatsu } 332950313ebSMasami Hiramatsu 333950313ebSMasami Hiramatsu int apply_xbc(const char *path, const char *xbc_path) 334950313ebSMasami Hiramatsu { 335950313ebSMasami Hiramatsu u32 size, csum; 336950313ebSMasami Hiramatsu char *buf, *data; 337950313ebSMasami Hiramatsu int ret, fd; 33889b74cacSMasami Hiramatsu const char *msg; 33989b74cacSMasami Hiramatsu int pos; 340950313ebSMasami Hiramatsu 341950313ebSMasami Hiramatsu ret = load_xbc_file(xbc_path, &buf); 342950313ebSMasami Hiramatsu if (ret < 0) { 34397378001SMasami Hiramatsu pr_err("Failed to load %s : %d\n", xbc_path, ret); 344950313ebSMasami Hiramatsu return ret; 345950313ebSMasami Hiramatsu } 346950313ebSMasami Hiramatsu size = strlen(buf) + 1; 347950313ebSMasami Hiramatsu csum = checksum((unsigned char *)buf, size); 348950313ebSMasami Hiramatsu 349950313ebSMasami Hiramatsu /* Prepare xbc_path data */ 350950313ebSMasami Hiramatsu data = malloc(size + 8); 351950313ebSMasami Hiramatsu if (!data) 352950313ebSMasami Hiramatsu return -ENOMEM; 353950313ebSMasami Hiramatsu strcpy(data, buf); 354950313ebSMasami Hiramatsu *(u32 *)(data + size) = size; 355950313ebSMasami Hiramatsu *(u32 *)(data + size + 4) = csum; 356950313ebSMasami Hiramatsu 357950313ebSMasami Hiramatsu /* Check the data format */ 35889b74cacSMasami Hiramatsu ret = xbc_init(buf, &msg, &pos); 359950313ebSMasami Hiramatsu if (ret < 0) { 36089b74cacSMasami Hiramatsu show_xbc_error(data, msg, pos); 361950313ebSMasami Hiramatsu free(data); 362950313ebSMasami Hiramatsu free(buf); 36389b74cacSMasami Hiramatsu 364950313ebSMasami Hiramatsu return ret; 365950313ebSMasami Hiramatsu } 366950313ebSMasami Hiramatsu printf("Apply %s to %s\n", xbc_path, path); 3670f0d0a77SMasami Hiramatsu printf("\tNumber of nodes: %d\n", ret); 368950313ebSMasami Hiramatsu printf("\tSize: %u bytes\n", (unsigned int)size); 369950313ebSMasami Hiramatsu printf("\tChecksum: %d\n", (unsigned int)csum); 370950313ebSMasami Hiramatsu 371950313ebSMasami Hiramatsu /* TODO: Check the options by schema */ 372950313ebSMasami Hiramatsu xbc_destroy_all(); 373950313ebSMasami Hiramatsu free(buf); 374950313ebSMasami Hiramatsu 375950313ebSMasami Hiramatsu /* Remove old boot config if exists */ 376950313ebSMasami Hiramatsu ret = delete_xbc(path); 377950313ebSMasami Hiramatsu if (ret < 0) { 37897378001SMasami Hiramatsu pr_err("Failed to delete previous boot config: %d\n", ret); 37988426044SYunfeng Ye free(data); 380950313ebSMasami Hiramatsu return ret; 381950313ebSMasami Hiramatsu } 382950313ebSMasami Hiramatsu 383950313ebSMasami Hiramatsu /* Apply new one */ 384950313ebSMasami Hiramatsu fd = open(path, O_RDWR | O_APPEND); 385950313ebSMasami Hiramatsu if (fd < 0) { 38697378001SMasami Hiramatsu pr_err("Failed to open %s: %d\n", path, fd); 38788426044SYunfeng Ye free(data); 388950313ebSMasami Hiramatsu return fd; 389950313ebSMasami Hiramatsu } 390950313ebSMasami Hiramatsu /* TODO: Ensure the @path is initramfs/initrd image */ 391950313ebSMasami Hiramatsu ret = write(fd, data, size + 8); 392950313ebSMasami Hiramatsu if (ret < 0) { 39397378001SMasami Hiramatsu pr_err("Failed to apply a boot config: %d\n", ret); 39488426044SYunfeng Ye goto out; 395950313ebSMasami Hiramatsu } 39685c46b78SMasami Hiramatsu /* Write a magic word of the bootconfig */ 39785c46b78SMasami Hiramatsu ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); 39885c46b78SMasami Hiramatsu if (ret < 0) { 39985c46b78SMasami Hiramatsu pr_err("Failed to apply a boot config magic: %d\n", ret); 40088426044SYunfeng Ye goto out; 40185c46b78SMasami Hiramatsu } 4029d82ccdaSSteven Rostedt (VMware) ret = 0; 40388426044SYunfeng Ye out: 404950313ebSMasami Hiramatsu close(fd); 405950313ebSMasami Hiramatsu free(data); 406950313ebSMasami Hiramatsu 40788426044SYunfeng Ye return ret; 408950313ebSMasami Hiramatsu } 409950313ebSMasami Hiramatsu 410950313ebSMasami Hiramatsu int usage(void) 411950313ebSMasami Hiramatsu { 412950313ebSMasami Hiramatsu printf("Usage: bootconfig [OPTIONS] <INITRD>\n" 413d052e1c6SMasami Hiramatsu "Or bootconfig <CONFIG>\n" 414950313ebSMasami Hiramatsu " Apply, delete or show boot config to initrd.\n" 415950313ebSMasami Hiramatsu " Options:\n" 416950313ebSMasami Hiramatsu " -a <config>: Apply boot config to initrd\n" 417*e4f70b7bSMasami Hiramatsu " -d : Delete boot config file from initrd\n" 418*e4f70b7bSMasami Hiramatsu " -l : list boot config in initrd or file\n\n" 419d052e1c6SMasami Hiramatsu " If no option is given, show the bootconfig in the given file.\n"); 420950313ebSMasami Hiramatsu return -1; 421950313ebSMasami Hiramatsu } 422950313ebSMasami Hiramatsu 423950313ebSMasami Hiramatsu int main(int argc, char **argv) 424950313ebSMasami Hiramatsu { 425950313ebSMasami Hiramatsu char *path = NULL; 426950313ebSMasami Hiramatsu char *apply = NULL; 427*e4f70b7bSMasami Hiramatsu bool delete = false, list = false; 428950313ebSMasami Hiramatsu int opt; 429950313ebSMasami Hiramatsu 430*e4f70b7bSMasami Hiramatsu while ((opt = getopt(argc, argv, "hda:l")) != -1) { 431950313ebSMasami Hiramatsu switch (opt) { 432950313ebSMasami Hiramatsu case 'd': 433950313ebSMasami Hiramatsu delete = true; 434950313ebSMasami Hiramatsu break; 435950313ebSMasami Hiramatsu case 'a': 436950313ebSMasami Hiramatsu apply = optarg; 437950313ebSMasami Hiramatsu break; 438*e4f70b7bSMasami Hiramatsu case 'l': 439*e4f70b7bSMasami Hiramatsu list = true; 440*e4f70b7bSMasami Hiramatsu break; 441950313ebSMasami Hiramatsu case 'h': 442950313ebSMasami Hiramatsu default: 443950313ebSMasami Hiramatsu return usage(); 444950313ebSMasami Hiramatsu } 445950313ebSMasami Hiramatsu } 446950313ebSMasami Hiramatsu 447*e4f70b7bSMasami Hiramatsu if ((apply && delete) || (delete && list) || (apply && list)) { 448*e4f70b7bSMasami Hiramatsu pr_err("Error: You can give one of -a, -d or -l at once.\n"); 449950313ebSMasami Hiramatsu return usage(); 450950313ebSMasami Hiramatsu } 451950313ebSMasami Hiramatsu 452950313ebSMasami Hiramatsu if (optind >= argc) { 45397378001SMasami Hiramatsu pr_err("Error: No initrd is specified.\n"); 454950313ebSMasami Hiramatsu return usage(); 455950313ebSMasami Hiramatsu } 456950313ebSMasami Hiramatsu 457950313ebSMasami Hiramatsu path = argv[optind]; 458950313ebSMasami Hiramatsu 459950313ebSMasami Hiramatsu if (apply) 460950313ebSMasami Hiramatsu return apply_xbc(path, apply); 461950313ebSMasami Hiramatsu else if (delete) 462950313ebSMasami Hiramatsu return delete_xbc(path); 463950313ebSMasami Hiramatsu 464*e4f70b7bSMasami Hiramatsu return show_xbc(path, list); 465950313ebSMasami Hiramatsu } 466