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
39 int (*function) (int argc, char **argv);
42 static int cbfs_add(int argc, char **argv)
44 char *romname = argv[1];
46 void *rom = loadrom(romname);
49 printf("Could not load ROM image '%s'.\n", romname);
54 printf("not enough arguments to '%s'.\n", cmd);
58 char *filename = argv[3];
59 char *cbfsname = argv[4];
61 uint32_t filesize = 0;
62 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
63 if (filedata == NULL) {
64 printf("Could not load file '%s'.\n", filename);
69 void *cbfsfile = NULL;
72 printf("not enough arguments to 'add'.\n");
76 if (intfiletype(argv[5]) != ((uint64_t) - 1))
77 type = intfiletype(argv[5]);
79 type = strtoul(argv[5], NULL, 0);
81 base = strtoul(argv[6], NULL, 0);
83 cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
84 if (add_file_to_cbfs(cbfsfile, filesize, base))
86 if (writerom(romname, rom, romsize))
91 static int cbfs_add_payload(int argc, char **argv)
93 char *romname = argv[1];
95 void *rom = loadrom(romname);
98 printf("Could not load ROM image '%s'.\n", romname);
103 printf("not enough arguments to '%s'.\n", cmd);
107 char *filename = argv[3];
108 char *cbfsname = argv[4];
110 uint32_t filesize = 0;
111 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
112 if (filedata == NULL) {
113 printf("Could not load file '%s'.\n", filename);
118 void *cbfsfile = NULL;
120 comp_algo algo = CBFS_COMPRESS_NONE;
122 if (argv[5][0] == 'l')
123 algo = CBFS_COMPRESS_LZMA;
126 base = strtoul(argv[6], NULL, 0);
128 unsigned char *payload;
129 filesize = parse_elf_to_payload(filedata, &payload, algo);
131 create_cbfs_file(cbfsname, payload, &filesize,
132 CBFS_COMPONENT_PAYLOAD, &base);
133 if (add_file_to_cbfs(cbfsfile, filesize, base))
135 if (writerom(romname, rom, romsize))
140 static int cbfs_add_stage(int argc, char **argv)
142 char *romname = argv[1];
144 void *rom = loadrom(romname);
147 printf("Could not load ROM image '%s'.\n", romname);
152 printf("not enough arguments to '%s'.\n", cmd);
156 char *filename = argv[3];
157 char *cbfsname = argv[4];
159 uint32_t filesize = 0;
160 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
161 if (filedata == NULL) {
162 printf("Could not load file '%s'.\n", filename);
167 void *cbfsfile = NULL;
169 comp_algo algo = CBFS_COMPRESS_NONE;
171 if (argv[5][0] == 'l')
172 algo = CBFS_COMPRESS_LZMA;
175 base = strtoul(argv[6], NULL, 0);
177 unsigned char *stage;
178 filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
180 create_cbfs_file(cbfsname, stage, &filesize,
181 CBFS_COMPONENT_STAGE, &base);
183 if (add_file_to_cbfs(cbfsfile, filesize, base))
185 if (writerom(romname, rom, romsize))
190 static int cbfs_create(int argc, char **argv)
192 char *romname = argv[1];
195 printf("not enough arguments to 'create'.\n");
200 uint32_t size = strtoul(argv[3], &suffix, 0);
201 if (tolower(suffix[0])=='k') {
204 if (tolower(suffix[0])=='m') {
207 char *bootblock = argv[4];
211 align = strtoul(argv[5], NULL, 0);
213 return create_cbfs_image(romname, size, bootblock, align);
216 static int cbfs_locate(int argc, char **argv)
218 char *romname = argv[1];
220 printf("not enough arguments to 'locate'.\n");
224 const char *file = argv[3];
225 uint32_t filesize = getfilesize(file);
226 const char *filename = argv[4];
227 int align = strtoul(argv[5], NULL, 0);
229 printf("%x\n", cbfs_find_location(romname, filesize, filename, align));
233 static int cbfs_print(int argc, char **argv)
235 char *romname = argv[1];
237 void *rom = loadrom(romname);
240 printf("Could not load ROM image '%s'.\n", romname);
244 print_cbfs_directory(romname);
248 static int cbfs_extract(int argc, char **argv)
250 char *romname = argv[1];
252 void *rom = loadrom(romname);
255 printf("Could not load ROM image '%s'.\n", romname);
261 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
265 return extract_file_from_cbfs(romname, argv[3], argv[4]);
268 struct command commands[] = {
269 {CMD_ADD, "add", cbfs_add},
270 {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
271 {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
272 {CMD_CREATE, "create", cbfs_create},
273 {CMD_LOCATE, "locate", cbfs_locate},
274 {CMD_PRINT, "print", cbfs_print},
275 {CMD_EXTRACT, "extract", cbfs_extract},
281 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
282 "USAGE:\n" " cbfstool [-h]\n"
283 " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
284 " -h Display this help message\n\n"
286 " add FILE NAME TYPE [base address] Add a component\n"
287 " add-payload FILE NAME [COMP] [base] Add a payload to the ROM\n"
288 " add-stage FILE NAME [COMP] [base] Add a stage to the ROM\n"
289 " create SIZE BOOTBLOCK [ALIGN] Create a ROM file\n"
290 " locate FILE NAME ALIGN Find a place for a file of that size\n"
291 " print Show the contents of the ROM\n"
292 " extract NAME FILE Extracts a raw payload from ROM\n"
296 print_supported_filetypes();
299 int main(int argc, char **argv)
310 for (i = 0; i < ARRAY_SIZE(commands); i++) {
311 if (strcmp(cmd, commands[i].name) != 0)
313 return commands[i].function(argc, argv);
316 printf("Unknown command '%s'.\n", cmd);