Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 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 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/bootconfig.h> |
| 16 | |
| 17 | int pr_output = 1; |
| 18 | |
| 19 | static int xbc_show_array(struct xbc_node *node) |
| 20 | { |
| 21 | const char *val; |
| 22 | int i = 0; |
| 23 | |
| 24 | xbc_array_for_each_value(node, val) { |
| 25 | printf("\"%s\"%s", val, node->next ? ", " : ";\n"); |
| 26 | i++; |
| 27 | } |
| 28 | return i; |
| 29 | } |
| 30 | |
| 31 | static void xbc_show_compact_tree(void) |
| 32 | { |
| 33 | struct xbc_node *node, *cnode; |
| 34 | int depth = 0, i; |
| 35 | |
| 36 | node = xbc_root_node(); |
| 37 | while (node && xbc_node_is_key(node)) { |
| 38 | for (i = 0; i < depth; i++) |
| 39 | printf("\t"); |
| 40 | cnode = xbc_node_get_child(node); |
| 41 | while (cnode && xbc_node_is_key(cnode) && !cnode->next) { |
| 42 | printf("%s.", xbc_node_get_data(node)); |
| 43 | node = cnode; |
| 44 | cnode = xbc_node_get_child(node); |
| 45 | } |
| 46 | if (cnode && xbc_node_is_key(cnode)) { |
| 47 | printf("%s {\n", xbc_node_get_data(node)); |
| 48 | depth++; |
| 49 | node = cnode; |
| 50 | continue; |
| 51 | } else if (cnode && xbc_node_is_value(cnode)) { |
| 52 | printf("%s = ", xbc_node_get_data(node)); |
| 53 | if (cnode->next) |
| 54 | xbc_show_array(cnode); |
| 55 | else |
| 56 | printf("\"%s\";\n", xbc_node_get_data(cnode)); |
| 57 | } else { |
| 58 | printf("%s;\n", xbc_node_get_data(node)); |
| 59 | } |
| 60 | |
| 61 | if (node->next) { |
| 62 | node = xbc_node_get_next(node); |
| 63 | continue; |
| 64 | } |
| 65 | while (!node->next) { |
| 66 | node = xbc_node_get_parent(node); |
| 67 | if (!node) |
| 68 | return; |
| 69 | if (!xbc_node_get_child(node)->next) |
| 70 | continue; |
| 71 | depth--; |
| 72 | for (i = 0; i < depth; i++) |
| 73 | printf("\t"); |
| 74 | printf("}\n"); |
| 75 | } |
| 76 | node = xbc_node_get_next(node); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* Simple real checksum */ |
| 81 | int checksum(unsigned char *buf, int len) |
| 82 | { |
| 83 | int i, sum = 0; |
| 84 | |
| 85 | for (i = 0; i < len; i++) |
| 86 | sum += buf[i]; |
| 87 | |
| 88 | return sum; |
| 89 | } |
| 90 | |
| 91 | #define PAGE_SIZE 4096 |
| 92 | |
| 93 | int load_xbc_fd(int fd, char **buf, int size) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | *buf = malloc(size + 1); |
| 98 | if (!*buf) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | ret = read(fd, *buf, size); |
| 102 | if (ret < 0) |
| 103 | return -errno; |
| 104 | (*buf)[size] = '\0'; |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* Return the read size or -errno */ |
| 110 | int load_xbc_file(const char *path, char **buf) |
| 111 | { |
| 112 | struct stat stat; |
| 113 | int fd, ret; |
| 114 | |
| 115 | fd = open(path, O_RDONLY); |
| 116 | if (fd < 0) |
| 117 | return -errno; |
| 118 | ret = fstat(fd, &stat); |
| 119 | if (ret < 0) |
| 120 | return -errno; |
| 121 | |
| 122 | ret = load_xbc_fd(fd, buf, stat.st_size); |
| 123 | |
| 124 | close(fd); |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | int load_xbc_from_initrd(int fd, char **buf) |
| 130 | { |
| 131 | struct stat stat; |
| 132 | int ret; |
| 133 | u32 size = 0, csum = 0, rcsum; |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 134 | char magic[BOOTCONFIG_MAGIC_LEN]; |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 135 | |
| 136 | ret = fstat(fd, &stat); |
| 137 | if (ret < 0) |
| 138 | return -errno; |
| 139 | |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 140 | if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN) |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 141 | return 0; |
| 142 | |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 143 | if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) { |
| 144 | pr_err("Failed to lseek: %d\n", -errno); |
| 145 | return -errno; |
| 146 | } |
| 147 | if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0) |
| 148 | return -errno; |
| 149 | /* Check the bootconfig magic bytes */ |
| 150 | if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0) |
| 151 | return 0; |
| 152 | |
| 153 | if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 154 | pr_err("Failed to lseek: %d\n", -errno); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 155 | return -errno; |
| 156 | } |
| 157 | |
| 158 | if (read(fd, &size, sizeof(u32)) < 0) |
| 159 | return -errno; |
| 160 | |
| 161 | if (read(fd, &csum, sizeof(u32)) < 0) |
| 162 | return -errno; |
| 163 | |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 164 | /* Wrong size error */ |
| 165 | if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) { |
| 166 | pr_err("bootconfig size is too big\n"); |
| 167 | return -E2BIG; |
| 168 | } |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 169 | |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 170 | if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN), |
| 171 | SEEK_SET) < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 172 | pr_err("Failed to lseek: %d\n", -errno); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 173 | return -errno; |
| 174 | } |
| 175 | |
| 176 | ret = load_xbc_fd(fd, buf, size); |
| 177 | if (ret < 0) |
| 178 | return ret; |
| 179 | |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 180 | /* Wrong Checksum */ |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 181 | rcsum = checksum((unsigned char *)*buf, size); |
| 182 | if (csum != rcsum) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 183 | pr_err("checksum error: %d != %d\n", csum, rcsum); |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 184 | return -EINVAL; |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | ret = xbc_init(*buf); |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 188 | /* Wrong data */ |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 189 | if (ret < 0) |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 190 | return ret; |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 191 | |
| 192 | return size; |
| 193 | } |
| 194 | |
| 195 | int show_xbc(const char *path) |
| 196 | { |
| 197 | int ret, fd; |
| 198 | char *buf = NULL; |
| 199 | |
| 200 | fd = open(path, O_RDONLY); |
| 201 | if (fd < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 202 | pr_err("Failed to open initrd %s: %d\n", path, fd); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 203 | return -errno; |
| 204 | } |
| 205 | |
| 206 | ret = load_xbc_from_initrd(fd, &buf); |
| 207 | if (ret < 0) |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 208 | pr_err("Failed to load a boot config from initrd: %d\n", ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 209 | else |
| 210 | xbc_show_compact_tree(); |
| 211 | |
| 212 | close(fd); |
| 213 | free(buf); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | int delete_xbc(const char *path) |
| 219 | { |
| 220 | struct stat stat; |
| 221 | int ret = 0, fd, size; |
| 222 | char *buf = NULL; |
| 223 | |
| 224 | fd = open(path, O_RDWR); |
| 225 | if (fd < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 226 | pr_err("Failed to open initrd %s: %d\n", path, fd); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 227 | return -errno; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Suppress error messages in xbc_init() because it can be just a |
| 232 | * data which concidentally matches the size and checksum footer. |
| 233 | */ |
| 234 | pr_output = 0; |
| 235 | size = load_xbc_from_initrd(fd, &buf); |
| 236 | pr_output = 1; |
| 237 | if (size < 0) { |
| 238 | ret = size; |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 239 | pr_err("Failed to load a boot config from initrd: %d\n", ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 240 | } else if (size > 0) { |
| 241 | ret = fstat(fd, &stat); |
| 242 | if (!ret) |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 243 | ret = ftruncate(fd, stat.st_size |
| 244 | - size - 8 - BOOTCONFIG_MAGIC_LEN); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 245 | if (ret) |
| 246 | ret = -errno; |
| 247 | } /* Ignore if there is no boot config in initrd */ |
| 248 | |
| 249 | close(fd); |
| 250 | free(buf); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | int apply_xbc(const char *path, const char *xbc_path) |
| 256 | { |
| 257 | u32 size, csum; |
| 258 | char *buf, *data; |
| 259 | int ret, fd; |
| 260 | |
| 261 | ret = load_xbc_file(xbc_path, &buf); |
| 262 | if (ret < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 263 | pr_err("Failed to load %s : %d\n", xbc_path, ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | size = strlen(buf) + 1; |
| 267 | csum = checksum((unsigned char *)buf, size); |
| 268 | |
| 269 | /* Prepare xbc_path data */ |
| 270 | data = malloc(size + 8); |
| 271 | if (!data) |
| 272 | return -ENOMEM; |
| 273 | strcpy(data, buf); |
| 274 | *(u32 *)(data + size) = size; |
| 275 | *(u32 *)(data + size + 4) = csum; |
| 276 | |
| 277 | /* Check the data format */ |
| 278 | ret = xbc_init(buf); |
| 279 | if (ret < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 280 | pr_err("Failed to parse %s: %d\n", xbc_path, ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 281 | free(data); |
| 282 | free(buf); |
| 283 | return ret; |
| 284 | } |
| 285 | printf("Apply %s to %s\n", xbc_path, path); |
Masami Hiramatsu | 0f0d0a7 | 2020-02-05 22:50:13 +0900 | [diff] [blame] | 286 | printf("\tNumber of nodes: %d\n", ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 287 | printf("\tSize: %u bytes\n", (unsigned int)size); |
| 288 | printf("\tChecksum: %d\n", (unsigned int)csum); |
| 289 | |
| 290 | /* TODO: Check the options by schema */ |
| 291 | xbc_destroy_all(); |
| 292 | free(buf); |
| 293 | |
| 294 | /* Remove old boot config if exists */ |
| 295 | ret = delete_xbc(path); |
| 296 | if (ret < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 297 | pr_err("Failed to delete previous boot config: %d\n", ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | /* Apply new one */ |
| 302 | fd = open(path, O_RDWR | O_APPEND); |
| 303 | if (fd < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 304 | pr_err("Failed to open %s: %d\n", path, fd); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 305 | return fd; |
| 306 | } |
| 307 | /* TODO: Ensure the @path is initramfs/initrd image */ |
| 308 | ret = write(fd, data, size + 8); |
| 309 | if (ret < 0) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 310 | pr_err("Failed to apply a boot config: %d\n", ret); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 311 | return ret; |
| 312 | } |
Masami Hiramatsu | 85c46b7 | 2020-02-20 21:18:42 +0900 | [diff] [blame^] | 313 | /* Write a magic word of the bootconfig */ |
| 314 | ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); |
| 315 | if (ret < 0) { |
| 316 | pr_err("Failed to apply a boot config magic: %d\n", ret); |
| 317 | return ret; |
| 318 | } |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 319 | close(fd); |
| 320 | free(data); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int usage(void) |
| 326 | { |
| 327 | printf("Usage: bootconfig [OPTIONS] <INITRD>\n" |
| 328 | " Apply, delete or show boot config to initrd.\n" |
| 329 | " Options:\n" |
| 330 | " -a <config>: Apply boot config to initrd\n" |
| 331 | " -d : Delete boot config file from initrd\n\n" |
| 332 | " If no option is given, show current applied boot config.\n"); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | int main(int argc, char **argv) |
| 337 | { |
| 338 | char *path = NULL; |
| 339 | char *apply = NULL; |
| 340 | bool delete = false; |
| 341 | int opt; |
| 342 | |
| 343 | while ((opt = getopt(argc, argv, "hda:")) != -1) { |
| 344 | switch (opt) { |
| 345 | case 'd': |
| 346 | delete = true; |
| 347 | break; |
| 348 | case 'a': |
| 349 | apply = optarg; |
| 350 | break; |
| 351 | case 'h': |
| 352 | default: |
| 353 | return usage(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (apply && delete) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 358 | pr_err("Error: You can not specify both -a and -d at once.\n"); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 359 | return usage(); |
| 360 | } |
| 361 | |
| 362 | if (optind >= argc) { |
Masami Hiramatsu | 9737800 | 2020-02-09 22:05:13 +0900 | [diff] [blame] | 363 | pr_err("Error: No initrd is specified.\n"); |
Masami Hiramatsu | 950313e | 2020-01-11 01:03:56 +0900 | [diff] [blame] | 364 | return usage(); |
| 365 | } |
| 366 | |
| 367 | path = argv[optind]; |
| 368 | |
| 369 | if (apply) |
| 370 | return apply_xbc(path, apply); |
| 371 | else if (delete) |
| 372 | return delete_xbc(path); |
| 373 | |
| 374 | return show_xbc(path); |
| 375 | } |