f017b2e2e07f3d58e2c1d938b0e4ba09edd0693b
[coreboot.git] / util / cbfstool / cbfstool.c
1 /*
2  * cbfstool, CLI utility for CBFS file manipulation
3  *
4  * Copyright (C) 2009 coresystems GmbH
5  *                 written by Patrick Georgi <patrick.georgi@coresystems.de>
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
21 #include <stdio.h>
22 #include <string.h>
23 #include "common.h"
24 #include "cbfs.h"
25
26 typedef enum {
27         CMD_ADD,
28         CMD_ADD_PAYLOAD,
29         CMD_ADD_STAGE,
30         CMD_CREATE,
31         CMD_LOCATE,
32         CMD_PRINT,
33         CMD_EXTRACT,
34 } cmd_t;
35
36 struct command {
37         cmd_t id;
38         const char *name;
39         int (*function) (int argc, char **argv);
40 };
41
42 static int cbfs_add(int argc, char **argv)
43 {
44         char *romname = argv[1];
45         char *cmd = argv[2];
46         void *rom = loadrom(romname);
47
48         if (rom == NULL) {
49                 printf("Could not load ROM image '%s'.\n", romname);
50                 return 1;
51         }
52
53         if (argc < 5) {
54                 printf("not enough arguments to '%s'.\n", cmd);
55                 return 1;
56         }
57
58         char *filename = argv[3];
59         char *cbfsname = argv[4];
60
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);
65                 return 1;
66         }
67
68         uint32_t base = 0;
69         void *cbfsfile = NULL;
70
71         if (argc < 6) {
72                 printf("not enough arguments to 'add'.\n");
73                 return 1;
74         }
75         uint32_t type;
76         if (intfiletype(argv[5]) != ((uint64_t) - 1))
77                 type = intfiletype(argv[5]);
78         else
79                 type = strtoul(argv[5], NULL, 0);
80         if (argc > 6) {
81                 base = strtoul(argv[6], NULL, 0);
82         }
83         cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
84         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
85                 printf("Adding file '%s' failed.\n", filename);
86                 return 1;
87         }
88         if (writerom(romname, rom, romsize))
89                 return 1;
90         return 0;
91 }
92
93 static int cbfs_add_payload(int argc, char **argv)
94 {
95         char *romname = argv[1];
96         char *cmd = argv[2];
97         void *rom = loadrom(romname);
98
99         if (rom == NULL) {
100                 printf("Could not load ROM image '%s'.\n", romname);
101                 return 1;
102         }
103
104         if (argc < 5) {
105                 printf("not enough arguments to '%s'.\n", cmd);
106                 return 1;
107         }
108
109         char *filename = argv[3];
110         char *cbfsname = argv[4];
111
112         uint32_t filesize = 0;
113         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
114         if (filedata == NULL) {
115                 printf("Could not load file '%s'.\n", filename);
116                 return 1;
117         }
118
119         uint32_t base = 0;
120         void *cbfsfile = NULL;
121
122         comp_algo algo = CBFS_COMPRESS_NONE;
123         if (argc > 5) {
124                 if (argv[5][0] == 'l')
125                         algo = CBFS_COMPRESS_LZMA;
126         }
127         if (argc > 6) {
128                 base = strtoul(argv[6], NULL, 0);
129         }
130         unsigned char *payload;
131         filesize = parse_elf_to_payload(filedata, &payload, algo);
132         cbfsfile =
133             create_cbfs_file(cbfsname, payload, &filesize,
134                              CBFS_COMPONENT_PAYLOAD, &base);
135         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
136                 printf("Adding payload '%s' failed.\n", filename);
137                 return 1;
138         }
139         if (writerom(romname, rom, romsize))
140                 return 1;
141         return 0;
142 }
143
144 static int cbfs_add_stage(int argc, char **argv)
145 {
146         char *romname = argv[1];
147         char *cmd = argv[2];
148         void *rom = loadrom(romname);
149
150         if (rom == NULL) {
151                 printf("Could not load ROM image '%s'.\n", romname);
152                 return 1;
153         }
154
155         if (argc < 5) {
156                 printf("not enough arguments to '%s'.\n", cmd);
157                 return 1;
158         }
159
160         char *filename = argv[3];
161         char *cbfsname = argv[4];
162
163         uint32_t filesize = 0;
164         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
165         if (filedata == NULL) {
166                 printf("Could not load file '%s'.\n", filename);
167                 return 1;
168         }
169
170         uint32_t base = 0;
171         void *cbfsfile = NULL;
172
173         comp_algo algo = CBFS_COMPRESS_NONE;
174         if (argc > 5) {
175                 if (argv[5][0] == 'l')
176                         algo = CBFS_COMPRESS_LZMA;
177         }
178         if (argc > 6) {
179                 base = strtoul(argv[6], NULL, 0);
180         }
181         unsigned char *stage;
182         filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
183         cbfsfile =
184             create_cbfs_file(cbfsname, stage, &filesize,
185                              CBFS_COMPONENT_STAGE, &base);
186
187         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
188                 printf("Adding stage '%s' failed.\n", filename);
189                 return 1;
190         }
191         if (writerom(romname, rom, romsize))
192                 return 1;
193         return 0;
194 }
195
196 static int cbfs_create(int argc, char **argv)
197 {
198         char *romname = argv[1];
199         char *cmd = argv[2];
200         if (argc < 5) {
201                 printf("not enough arguments to 'create'.\n");
202                 return 1;
203         }
204
205         char* suffix;
206         uint32_t size = strtoul(argv[3], &suffix, 0);
207         if (tolower(suffix[0])=='k') {
208                 size *= 1024;
209         }
210         if (tolower(suffix[0])=='m') {
211                 size *= 1024 * 1024;
212         }
213         char *bootblock = argv[4];
214         uint32_t align = 0;
215
216         if (argc > 5)
217                 align = strtoul(argv[5], NULL, 0);
218
219         return create_cbfs_image(romname, size, bootblock, align);
220 }
221
222 static int cbfs_locate(int argc, char **argv)
223 {
224         char *romname = argv[1];
225         if (argc < 6) {
226                 printf("not enough arguments to 'locate'.\n");
227                 return 1;
228         }
229
230         const char *file = argv[3];
231         uint32_t filesize = getfilesize(file);
232         const char *filename = argv[4];
233         int align = strtoul(argv[5], NULL, 0);
234
235         printf("%x\n", cbfs_find_location(romname, filesize, filename, align));
236         return 0;
237 }
238
239 static int cbfs_print(int argc, char **argv)
240 {
241         char *romname = argv[1];
242         char *cmd = argv[2];
243         void *rom = loadrom(romname);
244
245         if (rom == NULL) {
246                 printf("Could not load ROM image '%s'.\n", romname);
247                 return 1;
248         }
249
250         print_cbfs_directory(romname);
251         return 0;
252 }
253
254 static int cbfs_extract(int argc, char **argv)
255 {
256         char *romname = argv[1];
257         char *cmd = argv[2];
258         void *rom = loadrom(romname);
259
260         if (rom == NULL) {
261                 printf("Could not load ROM image '%s'.\n", romname);
262                 return 1;
263         }
264
265         if (argc != 5)
266         {
267                 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
268                 return 1;
269         }
270
271         return extract_file_from_cbfs(romname, argv[3], argv[4]);
272 }
273
274 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},
282 };
283
284 void usage(void)
285 {
286         printf
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"
291              "COMMANDs:\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"
299              "\n"
300              "TYPEs:\n"
301              );
302         print_supported_filetypes();
303 }
304
305 int main(int argc, char **argv)
306 {
307         int i;
308
309         if (argc < 3) {
310                 usage();
311                 return 1;
312         }
313
314         char *cmd = argv[2];
315
316         for (i = 0; i < ARRAY_SIZE(commands); i++) {
317                 if (strcmp(cmd, commands[i].name) != 0)
318                         continue;
319                 return commands[i].function(argc, argv);
320         }
321
322         printf("Unknown command '%s'.\n", cmd);
323         usage();
324         return 1;
325 }