2 * cbfstool, CLI utility for CBFS file manipulation
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
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.
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.
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
42 int (*function) (int argc, char **argv);
45 static int cbfs_add(int argc, char **argv)
47 char *romname = argv[1];
49 void *rom = loadrom(romname);
52 printf("Could not load ROM image '%s'.\n", romname);
57 printf("not enough arguments to '%s'.\n", cmd);
61 char *filename = argv[3];
62 char *cbfsname = argv[4];
64 uint32_t filesize = 0;
65 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
66 if (filedata == NULL) {
67 printf("Could not load file '%s'.\n", filename);
72 void *cbfsfile = NULL;
75 printf("not enough arguments to 'add'.\n");
79 if (intfiletype(argv[5]) != ((uint64_t) - 1))
80 type = intfiletype(argv[5]);
82 type = strtoul(argv[5], NULL, 0);
84 base = strtoul(argv[6], NULL, 0);
86 cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
87 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
88 printf("Adding file '%s' failed.\n", filename);
91 if (writerom(romname, rom, romsize))
96 static int cbfs_add_payload(int argc, char **argv)
98 char *romname = argv[1];
100 void *rom = loadrom(romname);
103 printf("Could not load ROM image '%s'.\n", romname);
108 printf("not enough arguments to '%s'.\n", cmd);
112 char *filename = argv[3];
113 char *cbfsname = argv[4];
115 uint32_t filesize = 0;
116 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
117 if (filedata == NULL) {
118 printf("Could not load file '%s'.\n", filename);
123 void *cbfsfile = NULL;
125 comp_algo algo = CBFS_COMPRESS_NONE;
127 if (argv[5][0] == 'l')
128 algo = CBFS_COMPRESS_LZMA;
131 base = strtoul(argv[6], NULL, 0);
133 unsigned char *payload;
134 filesize = parse_elf_to_payload(filedata, &payload, algo);
136 create_cbfs_file(cbfsname, payload, &filesize,
137 CBFS_COMPONENT_PAYLOAD, &base);
138 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
139 printf("Adding payload '%s' failed.\n", filename);
142 if (writerom(romname, rom, romsize))
147 static int cbfs_add_stage(int argc, char **argv)
149 char *romname = argv[1];
151 void *rom = loadrom(romname);
154 printf("Could not load ROM image '%s'.\n", romname);
159 printf("not enough arguments to '%s'.\n", cmd);
163 char *filename = argv[3];
164 char *cbfsname = argv[4];
166 uint32_t filesize = 0;
167 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
168 if (filedata == NULL) {
169 printf("Could not load file '%s'.\n", filename);
174 void *cbfsfile = NULL;
176 comp_algo algo = CBFS_COMPRESS_NONE;
178 if (argv[5][0] == 'l')
179 algo = CBFS_COMPRESS_LZMA;
182 base = strtoul(argv[6], NULL, 0);
184 unsigned char *stage;
185 filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
187 create_cbfs_file(cbfsname, stage, &filesize,
188 CBFS_COMPONENT_STAGE, &base);
190 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
191 printf("Adding stage '%s' failed.\n", filename);
194 if (writerom(romname, rom, romsize))
199 static int cbfs_remove(int argc, char **argv)
201 char *romname = argv[1];
203 void *rom = loadrom(romname);
206 printf("Could not load ROM image '%s'.\n", romname);
211 printf("not enough arguments to '%s'.\n", cmd);
215 char *cbfsname = argv[3];
217 if (remove_file_from_cbfs(cbfsname)) {
218 printf("Removing file '%s' failed.\n", cbfsname);
221 if (writerom(romname, rom, romsize))
226 static int cbfs_create(int argc, char **argv)
228 char *romname = argv[1];
230 printf("not enough arguments to 'create'.\n");
235 uint32_t size = strtoul(argv[3], &suffix, 0);
236 if (tolower(suffix[0])=='k') {
239 if (tolower(suffix[0])=='m') {
242 char *bootblock = argv[4];
246 align = strtoul(argv[5], NULL, 0);
248 return create_cbfs_image(romname, size, bootblock, align);
251 static int cbfs_locate(int argc, char **argv)
253 char *romname = argv[1];
255 printf("not enough arguments to 'locate'.\n");
259 const char *file = argv[3];
260 uint32_t filesize = getfilesize(file);
261 const char *filename = argv[4];
262 int align = strtoul(argv[5], NULL, 0);
263 uint32_t location = cbfs_find_location(romname, filesize, filename, align);
265 printf("%x\n", location);
266 return location == 0 ? 1 : 0;
269 static int cbfs_print(int argc, char **argv)
271 char *romname = argv[1];
272 void *rom = loadrom(romname);
275 printf("Could not load ROM image '%s'.\n", romname);
279 print_cbfs_directory(romname);
283 static int cbfs_extract(int argc, char **argv)
285 char *romname = argv[1];
286 void *rom = loadrom(romname);
289 printf("Could not load ROM image '%s'.\n", romname);
295 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
299 return extract_file_from_cbfs(romname, argv[3], argv[4]);
302 static const struct command commands[] = {
303 {CMD_ADD, "add", cbfs_add},
304 {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
305 {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
306 {CMD_REMOVE, "remove", cbfs_remove},
307 {CMD_CREATE, "create", cbfs_create},
308 {CMD_LOCATE, "locate", cbfs_locate},
309 {CMD_PRINT, "print", cbfs_print},
310 {CMD_EXTRACT, "extract", cbfs_extract},
313 static void usage(void)
316 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
317 "USAGE:\n" " cbfstool [-h]\n"
318 " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
319 " -h Display this help message\n\n"
321 " add FILE NAME TYPE [base address] Add a component\n"
322 " add-payload FILE NAME [COMP] [base] Add a payload to the ROM\n"
323 " add-stage FILE NAME [COMP] [base] Add a stage to the ROM\n"
324 " remove FILE NAME Remove a component\n"
325 " create SIZE BOOTBLOCK [ALIGN] Create a ROM file\n"
326 " locate FILE NAME ALIGN Find a place for a file of that size\n"
327 " print Show the contents of the ROM\n"
328 " extract NAME FILE Extracts a raw payload from ROM\n"
332 print_supported_filetypes();
335 /* Small, OS/libc independent runtime check
338 int host_bigendian = 0;
340 static void which_endian(void)
342 char test[4] = "1234";
343 uint32_t inttest = *(uint32_t *) test;
344 if (inttest == 0x31323334) {
349 int main(int argc, char **argv)
362 for (i = 0; i < ARRAY_SIZE(commands); i++) {
363 if (strcmp(cmd, commands[i].name) != 0)
365 return commands[i].function(argc, argv);
368 printf("Unknown command '%s'.\n", cmd);