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
37 int (*function) (int argc, char **argv);
40 static int cbfs_add(int argc, char **argv)
42 char *romname = argv[1];
44 void *rom = loadrom(romname);
47 printf("Could not load ROM image '%s'.\n", romname);
52 printf("not enough arguments to '%s'.\n", cmd);
56 char *filename = argv[3];
57 char *cbfsname = argv[4];
59 uint32_t filesize = 0;
60 void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
61 if (filedata == NULL) {
62 printf("Could not load file '%s'.\n", filename);
67 void *cbfsfile = NULL;
70 printf("not enough arguments to 'add'.\n");
74 if (intfiletype(argv[5]) != ((uint64_t) - 1))
75 type = intfiletype(argv[5]);
77 type = strtoul(argv[5], NULL, 0);
79 base = strtoul(argv[6], NULL, 0);
82 create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
83 if (add_file_to_cbfs(cbfsfile, filesize, base))
85 writerom(romname, rom, romsize);
89 static int cbfs_add_payload(int argc, char **argv)
91 char *romname = argv[1];
93 void *rom = loadrom(romname);
96 printf("Could not load ROM image '%s'.\n", romname);
101 printf("not enough arguments to '%s'.\n", cmd);
105 char *filename = argv[3];
106 char *cbfsname = argv[4];
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);
116 void *cbfsfile = NULL;
118 comp_algo algo = CBFS_COMPRESS_NONE;
120 if (argv[5][0] == 'l')
121 algo = CBFS_COMPRESS_LZMA;
124 base = strtoul(argv[6], NULL, 0);
126 unsigned char *payload;
127 filesize = parse_elf_to_payload(filedata, &payload, algo);
129 create_cbfs_file(cbfsname, payload, &filesize,
130 CBFS_COMPONENT_PAYLOAD, &base);
131 if (add_file_to_cbfs(cbfsfile, filesize, base))
133 writerom(romname, rom, romsize);
137 static int cbfs_add_stage(int argc, char **argv)
139 char *romname = argv[1];
141 void *rom = loadrom(romname);
144 printf("Could not load ROM image '%s'.\n", romname);
149 printf("not enough arguments to '%s'.\n", cmd);
153 char *filename = argv[3];
154 char *cbfsname = argv[4];
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);
164 void *cbfsfile = NULL;
166 comp_algo algo = CBFS_COMPRESS_NONE;
168 if (argv[5][0] == 'l')
169 algo = CBFS_COMPRESS_LZMA;
172 base = strtoul(argv[6], NULL, 0);
174 unsigned char *stage;
175 filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
177 create_cbfs_file(cbfsname, stage, &filesize,
178 CBFS_COMPONENT_STAGE, &base);
180 if (add_file_to_cbfs(cbfsfile, filesize, base))
182 writerom(romname, rom, romsize);
186 static int cbfs_create(int argc, char **argv)
188 char *romname = argv[1];
191 printf("not enough arguments to 'create'.\n");
195 uint32_t size = strtoul(argv[3], NULL, 0);
196 /* ignore bootblock size. we use whatever we get and won't allocate any larger */
197 char *bootblock = argv[5];
201 align = strtoul(argv[6], NULL, 0);
203 return create_cbfs_image(romname, size, bootblock, align);
206 static int cbfs_print(int argc, char **argv)
208 char *romname = argv[1];
210 void *rom = loadrom(romname);
213 printf("Could not load ROM image '%s'.\n", romname);
217 print_cbfs_directory(romname);
221 struct command commands[] = {
222 {CMD_ADD, "add", cbfs_add},
223 {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
224 {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
225 {CMD_CREATE, "create", cbfs_create},
226 {CMD_PRINT, "print", cbfs_print}
232 ("cbfstool: Management utility for CBFS formatted ROM images\n"
233 "USAGE:\n" "cbfstool [-h]\n"
234 "cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
235 " -h Display this help message\n\n"
237 "add FILE NAME TYPE [base address] Add a component\n"
238 "add-payload FILE NAME [COMP] [base] Add a payload to the ROM\n"
239 "add-stage FILE NAME [COMP] [base] Add a stage to the ROM\n"
240 "create SIZE BSIZE BOOTBLOCK [ALIGN] Create a ROM file\n"
241 "print Show the contents of the ROM\n");
244 int main(int argc, char **argv)
255 for (i = 0; i < ARRAY_SIZE(commands); i++) {
256 if (strcmp(cmd, commands[i].name) != 0)
258 return commands[i].function(argc, argv);
261 printf("Unknown command '%s'.\n", cmd);