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
41 int (*function) (int argc, char **argv);
44 static int cbfs_add(int argc, char **argv)
46 char *romname = argv[1];
48 void *rom = loadrom(romname);
51 printf("Could not load ROM image '%s'.\n", romname);
56 printf("not enough arguments to '%s'.\n", cmd);
60 char *filename = argv[3];
61 char *cbfsname = argv[4];
63 uint32_t filesize = 0;
64 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
65 if (filedata == NULL) {
66 printf("Could not load file '%s'.\n", filename);
71 void *cbfsfile = NULL;
74 printf("not enough arguments to 'add'.\n");
78 if (intfiletype(argv[5]) != ((uint64_t) - 1))
79 type = intfiletype(argv[5]);
81 type = strtoul(argv[5], NULL, 0);
83 base = strtoul(argv[6], NULL, 0);
85 cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
86 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
87 printf("Adding file '%s' failed.\n", filename);
90 if (writerom(romname, rom, romsize))
95 static int cbfs_add_payload(int argc, char **argv)
97 char *romname = argv[1];
99 void *rom = loadrom(romname);
102 printf("Could not load ROM image '%s'.\n", romname);
107 printf("not enough arguments to '%s'.\n", cmd);
111 char *filename = argv[3];
112 char *cbfsname = argv[4];
114 uint32_t filesize = 0;
115 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
116 if (filedata == NULL) {
117 printf("Could not load file '%s'.\n", filename);
122 void *cbfsfile = NULL;
124 comp_algo algo = CBFS_COMPRESS_NONE;
126 if (argv[5][0] == 'l')
127 algo = CBFS_COMPRESS_LZMA;
130 base = strtoul(argv[6], NULL, 0);
132 unsigned char *payload;
133 filesize = parse_elf_to_payload(filedata, &payload, algo);
135 create_cbfs_file(cbfsname, payload, &filesize,
136 CBFS_COMPONENT_PAYLOAD, &base);
137 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
138 printf("Adding payload '%s' failed.\n", filename);
141 if (writerom(romname, rom, romsize))
146 static int cbfs_add_stage(int argc, char **argv)
148 char *romname = argv[1];
150 void *rom = loadrom(romname);
153 printf("Could not load ROM image '%s'.\n", romname);
158 printf("not enough arguments to '%s'.\n", cmd);
162 char *filename = argv[3];
163 char *cbfsname = argv[4];
165 uint32_t filesize = 0;
166 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
167 if (filedata == NULL) {
168 printf("Could not load file '%s'.\n", filename);
173 void *cbfsfile = NULL;
175 comp_algo algo = CBFS_COMPRESS_NONE;
177 if (argv[5][0] == 'l')
178 algo = CBFS_COMPRESS_LZMA;
181 base = strtoul(argv[6], NULL, 0);
183 unsigned char *stage;
184 filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
186 create_cbfs_file(cbfsname, stage, &filesize,
187 CBFS_COMPONENT_STAGE, &base);
189 if (add_file_to_cbfs(cbfsfile, filesize, base)) {
190 printf("Adding stage '%s' failed.\n", filename);
193 if (writerom(romname, rom, romsize))
198 static int cbfs_create(int argc, char **argv)
200 char *romname = argv[1];
202 printf("not enough arguments to 'create'.\n");
207 uint32_t size = strtoul(argv[3], &suffix, 0);
208 if (tolower(suffix[0])=='k') {
211 if (tolower(suffix[0])=='m') {
214 char *bootblock = argv[4];
218 align = strtoul(argv[5], NULL, 0);
220 return create_cbfs_image(romname, size, bootblock, align);
223 static int cbfs_locate(int argc, char **argv)
225 char *romname = argv[1];
227 printf("not enough arguments to 'locate'.\n");
231 const char *file = argv[3];
232 uint32_t filesize = getfilesize(file);
233 const char *filename = argv[4];
234 int align = strtoul(argv[5], NULL, 0);
235 uint32_t location = cbfs_find_location(romname, filesize, filename, align);
237 printf("%x\n", location);
238 return location == 0 ? 1 : 0;
241 static int cbfs_print(int argc, char **argv)
243 char *romname = argv[1];
244 void *rom = loadrom(romname);
247 printf("Could not load ROM image '%s'.\n", romname);
251 print_cbfs_directory(romname);
255 static int cbfs_extract(int argc, char **argv)
257 char *romname = argv[1];
258 void *rom = loadrom(romname);
261 printf("Could not load ROM image '%s'.\n", romname);
267 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
271 return extract_file_from_cbfs(romname, argv[3], argv[4]);
274 static const struct command commands[] = {
275 {CMD_ADD, "add", cbfs_add},
276 {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
277 {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
278 {CMD_CREATE, "create", cbfs_create},
279 {CMD_LOCATE, "locate", cbfs_locate},
280 {CMD_PRINT, "print", cbfs_print},
281 {CMD_EXTRACT, "extract", cbfs_extract},
284 static void usage(void)
287 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
288 "USAGE:\n" " cbfstool [-h]\n"
289 " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
290 " -h Display this help message\n\n"
292 " add FILE NAME TYPE [base address] Add a component\n"
293 " add-payload FILE NAME [COMP] [base] Add a payload to the ROM\n"
294 " add-stage FILE NAME [COMP] [base] Add a stage to the ROM\n"
295 " create SIZE BOOTBLOCK [ALIGN] Create a ROM file\n"
296 " locate FILE NAME ALIGN Find a place for a file of that size\n"
297 " print Show the contents of the ROM\n"
298 " extract NAME FILE Extracts a raw payload from ROM\n"
302 print_supported_filetypes();
305 /* Small, OS/libc independent runtime check
308 int host_bigendian = 0;
310 static void which_endian(void)
312 char test[4] = "1234";
313 uint32_t inttest = *(uint32_t *) test;
314 if (inttest == 0x31323334) {
319 int main(int argc, char **argv)
332 for (i = 0; i < ARRAY_SIZE(commands); i++) {
333 if (strcmp(cmd, commands[i].name) != 0)
335 return commands[i].function(argc, argv);
338 printf("Unknown command '%s'.\n", cmd);