Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | * cbfstool, CLI utility for CBFS file manipulation |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | * |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 19 | */ |
| 20 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdint.h> |
| 23 | #include "common.h" |
| 24 | #include "cbfs.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 25 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 26 | typedef enum { |
| 27 | CMD_ADD, |
| 28 | CMD_ADD_PAYLOAD, |
| 29 | CMD_ADD_STAGE, |
| 30 | CMD_CREATE, |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 31 | CMD_LOCATE, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 32 | CMD_PRINT |
| 33 | } cmd_t; |
| 34 | |
| 35 | struct command { |
| 36 | cmd_t id; |
| 37 | const char *name; |
| 38 | int (*function) (int argc, char **argv); |
| 39 | }; |
| 40 | |
| 41 | static int cbfs_add(int argc, char **argv) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 42 | { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 43 | char *romname = argv[1]; |
| 44 | char *cmd = argv[2]; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 45 | void *rom = loadrom(romname); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 46 | |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 47 | if (rom == NULL) { |
| 48 | printf("Could not load ROM image '%s'.\n", romname); |
| 49 | return 1; |
| 50 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 51 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 52 | if (argc < 5) { |
| 53 | printf("not enough arguments to '%s'.\n", cmd); |
| 54 | return 1; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 57 | char *filename = argv[3]; |
| 58 | char *cbfsname = argv[4]; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 59 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 60 | uint32_t filesize = 0; |
| 61 | void *filedata = loadfile(filename, &filesize, 0, SEEK_SET); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 62 | if (filedata == NULL) { |
| 63 | printf("Could not load file '%s'.\n", filename); |
| 64 | return 1; |
| 65 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 66 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 67 | uint32_t base = 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 68 | void *cbfsfile = NULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 69 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 70 | if (argc < 6) { |
| 71 | printf("not enough arguments to 'add'.\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | uint32_t type; |
| 75 | if (intfiletype(argv[5]) != ((uint64_t) - 1)) |
| 76 | type = intfiletype(argv[5]); |
| 77 | else |
| 78 | type = strtoul(argv[5], NULL, 0); |
| 79 | if (argc > 6) { |
| 80 | base = strtoul(argv[6], NULL, 0); |
| 81 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 82 | cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 83 | if (add_file_to_cbfs(cbfsfile, filesize, base)) |
| 84 | return 1; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 85 | writerom(romname, rom, romsize); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int cbfs_add_payload(int argc, char **argv) |
| 90 | { |
| 91 | char *romname = argv[1]; |
| 92 | char *cmd = argv[2]; |
| 93 | void *rom = loadrom(romname); |
| 94 | |
| 95 | if (rom == NULL) { |
| 96 | printf("Could not load ROM image '%s'.\n", romname); |
| 97 | return 1; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 100 | if (argc < 5) { |
| 101 | printf("not enough arguments to '%s'.\n", cmd); |
| 102 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 103 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 104 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 105 | char *filename = argv[3]; |
| 106 | char *cbfsname = argv[4]; |
| 107 | |
| 108 | uint32_t filesize = 0; |
| 109 | void *filedata = loadfile(filename, &filesize, 0, SEEK_SET); |
| 110 | if (filedata == NULL) { |
| 111 | printf("Could not load file '%s'.\n", filename); |
| 112 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 113 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 114 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 115 | uint32_t base = 0; |
| 116 | void *cbfsfile = NULL; |
| 117 | |
| 118 | comp_algo algo = CBFS_COMPRESS_NONE; |
| 119 | if (argc > 5) { |
| 120 | if (argv[5][0] == 'l') |
| 121 | algo = CBFS_COMPRESS_LZMA; |
| 122 | } |
| 123 | if (argc > 6) { |
| 124 | base = strtoul(argv[6], NULL, 0); |
| 125 | } |
| 126 | unsigned char *payload; |
| 127 | filesize = parse_elf_to_payload(filedata, &payload, algo); |
| 128 | cbfsfile = |
| 129 | create_cbfs_file(cbfsname, payload, &filesize, |
| 130 | CBFS_COMPONENT_PAYLOAD, &base); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 131 | if (add_file_to_cbfs(cbfsfile, filesize, base)) |
| 132 | return 1; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 133 | writerom(romname, rom, romsize); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int cbfs_add_stage(int argc, char **argv) |
| 138 | { |
| 139 | char *romname = argv[1]; |
| 140 | char *cmd = argv[2]; |
| 141 | void *rom = loadrom(romname); |
| 142 | |
| 143 | if (rom == NULL) { |
| 144 | printf("Could not load ROM image '%s'.\n", romname); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if (argc < 5) { |
| 149 | printf("not enough arguments to '%s'.\n", cmd); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | char *filename = argv[3]; |
| 154 | char *cbfsname = argv[4]; |
| 155 | |
| 156 | uint32_t filesize = 0; |
| 157 | void *filedata = loadfile(filename, &filesize, 0, SEEK_SET); |
| 158 | if (filedata == NULL) { |
| 159 | printf("Could not load file '%s'.\n", filename); |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | uint32_t base = 0; |
| 164 | void *cbfsfile = NULL; |
| 165 | |
| 166 | comp_algo algo = CBFS_COMPRESS_NONE; |
| 167 | if (argc > 5) { |
| 168 | if (argv[5][0] == 'l') |
| 169 | algo = CBFS_COMPRESS_LZMA; |
| 170 | } |
| 171 | if (argc > 6) { |
| 172 | base = strtoul(argv[6], NULL, 0); |
| 173 | } |
| 174 | unsigned char *stage; |
| 175 | filesize = parse_elf_to_stage(filedata, &stage, algo, &base); |
| 176 | cbfsfile = |
| 177 | create_cbfs_file(cbfsname, stage, &filesize, |
| 178 | CBFS_COMPONENT_STAGE, &base); |
| 179 | |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 180 | if (add_file_to_cbfs(cbfsfile, filesize, base)) |
| 181 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 182 | writerom(romname, rom, romsize); |
| 183 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 184 | } |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 185 | |
| 186 | static int cbfs_create(int argc, char **argv) |
| 187 | { |
| 188 | char *romname = argv[1]; |
| 189 | char *cmd = argv[2]; |
Patrick Georgi | 6056b97 | 2009-11-11 21:32:23 +0000 | [diff] [blame] | 190 | if (argc < 5) { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 191 | printf("not enough arguments to 'create'.\n"); |
| 192 | return 1; |
| 193 | } |
| 194 | |
Patrick Georgi | 467b12a | 2009-12-21 13:50:37 +0000 | [diff] [blame^] | 195 | char* suffix; |
| 196 | uint32_t size = strtoul(argv[3], &suffix, 0); |
| 197 | if (tolower(suffix[0])=='k') { |
| 198 | size *= 1024; |
| 199 | } |
| 200 | if (tolower(suffix[0])=='m') { |
| 201 | size *= 1024 * 1024; |
| 202 | } |
Patrick Georgi | 6056b97 | 2009-11-11 21:32:23 +0000 | [diff] [blame] | 203 | char *bootblock = argv[4]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 204 | uint32_t align = 0; |
| 205 | |
Patrick Georgi | 6056b97 | 2009-11-11 21:32:23 +0000 | [diff] [blame] | 206 | if (argc > 5) |
| 207 | align = strtoul(argv[5], NULL, 0); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 208 | |
| 209 | return create_cbfs_image(romname, size, bootblock, align); |
| 210 | } |
| 211 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 212 | static int cbfs_locate(int argc, char **argv) |
| 213 | { |
| 214 | char *romname = argv[1]; |
| 215 | if (argc < 6) { |
| 216 | printf("not enough arguments to 'locate'.\n"); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | const char *file = argv[3]; |
| 221 | uint32_t filesize = getfilesize(file); |
| 222 | const char *filename = argv[4]; |
| 223 | int align = strtoul(argv[5], NULL, 0); |
| 224 | |
| 225 | printf("%x\n", cbfs_find_location(romname, filesize, filename, align)); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 229 | static int cbfs_print(int argc, char **argv) |
| 230 | { |
| 231 | char *romname = argv[1]; |
| 232 | char *cmd = argv[2]; |
| 233 | void *rom = loadrom(romname); |
| 234 | |
| 235 | if (rom == NULL) { |
| 236 | printf("Could not load ROM image '%s'.\n", romname); |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | print_cbfs_directory(romname); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | struct command commands[] = { |
| 245 | {CMD_ADD, "add", cbfs_add}, |
| 246 | {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload}, |
| 247 | {CMD_ADD_STAGE, "add-stage", cbfs_add_stage}, |
| 248 | {CMD_CREATE, "create", cbfs_create}, |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 249 | {CMD_LOCATE, "locate", cbfs_locate}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 250 | {CMD_PRINT, "print", cbfs_print} |
| 251 | }; |
| 252 | |
| 253 | void usage(void) |
| 254 | { |
| 255 | printf |
| 256 | ("cbfstool: Management utility for CBFS formatted ROM images\n" |
| 257 | "USAGE:\n" "cbfstool [-h]\n" |
| 258 | "cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n" |
| 259 | " -h Display this help message\n\n" |
| 260 | "COMMANDs:\n" |
| 261 | "add FILE NAME TYPE [base address] Add a component\n" |
| 262 | "add-payload FILE NAME [COMP] [base] Add a payload to the ROM\n" |
| 263 | "add-stage FILE NAME [COMP] [base] Add a stage to the ROM\n" |
Patrick Georgi | 6056b97 | 2009-11-11 21:32:23 +0000 | [diff] [blame] | 264 | "create SIZE BOOTBLOCK [ALIGN] Create a ROM file\n" |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 265 | "locate FILE NAME ALIGN Find a place for a file of that size\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 266 | "print Show the contents of the ROM\n"); |
| 267 | } |
| 268 | |
| 269 | int main(int argc, char **argv) |
| 270 | { |
| 271 | int i; |
| 272 | |
| 273 | if (argc < 3) { |
| 274 | usage(); |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | char *cmd = argv[2]; |
| 279 | |
| 280 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 281 | if (strcmp(cmd, commands[i].name) != 0) |
| 282 | continue; |
| 283 | return commands[i].function(argc, argv); |
| 284 | } |
| 285 | |
| 286 | printf("Unknown command '%s'.\n", cmd); |
| 287 | usage(); |
| 288 | return 1; |
| 289 | } |