939221e66d61133d372594a7da65f3ab7382e156
[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 <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "common.h"
26 #include "cbfs.h"
27
28 typedef enum {
29         CMD_ADD,
30         CMD_ADD_PAYLOAD,
31         CMD_ADD_STAGE,
32         CMD_CREATE,
33         CMD_LOCATE,
34         CMD_PRINT,
35         CMD_EXTRACT,
36 } cmd_t;
37
38 struct command {
39         cmd_t id;
40         const char *name;
41         int (*function) (int argc, char **argv);
42 };
43
44 static int cbfs_add(int argc, char **argv)
45 {
46         char *romname = argv[1];
47         char *cmd = argv[2];
48         void *rom = loadrom(romname);
49
50         if (rom == NULL) {
51                 printf("Could not load ROM image '%s'.\n", romname);
52                 return 1;
53         }
54
55         if (argc < 5) {
56                 printf("not enough arguments to '%s'.\n", cmd);
57                 return 1;
58         }
59
60         char *filename = argv[3];
61         char *cbfsname = argv[4];
62
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);
67                 return 1;
68         }
69
70         uint32_t base = 0;
71         void *cbfsfile = NULL;
72
73         if (argc < 6) {
74                 printf("not enough arguments to 'add'.\n");
75                 return 1;
76         }
77         uint32_t type;
78         if (intfiletype(argv[5]) != ((uint64_t) - 1))
79                 type = intfiletype(argv[5]);
80         else
81                 type = strtoul(argv[5], NULL, 0);
82         if (argc > 6) {
83                 base = strtoul(argv[6], NULL, 0);
84         }
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);
88                 return 1;
89         }
90         if (writerom(romname, rom, romsize))
91                 return 1;
92         return 0;
93 }
94
95 static int cbfs_add_payload(int argc, char **argv)
96 {
97         char *romname = argv[1];
98         char *cmd = argv[2];
99         void *rom = loadrom(romname);
100
101         if (rom == NULL) {
102                 printf("Could not load ROM image '%s'.\n", romname);
103                 return 1;
104         }
105
106         if (argc < 5) {
107                 printf("not enough arguments to '%s'.\n", cmd);
108                 return 1;
109         }
110
111         char *filename = argv[3];
112         char *cbfsname = argv[4];
113
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);
118                 return 1;
119         }
120
121         uint32_t base = 0;
122         void *cbfsfile = NULL;
123
124         comp_algo algo = CBFS_COMPRESS_NONE;
125         if (argc > 5) {
126                 if (argv[5][0] == 'l')
127                         algo = CBFS_COMPRESS_LZMA;
128         }
129         if (argc > 6) {
130                 base = strtoul(argv[6], NULL, 0);
131         }
132         unsigned char *payload;
133         filesize = parse_elf_to_payload(filedata, &payload, algo);
134         cbfsfile =
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);
139                 return 1;
140         }
141         if (writerom(romname, rom, romsize))
142                 return 1;
143         return 0;
144 }
145
146 static int cbfs_add_stage(int argc, char **argv)
147 {
148         char *romname = argv[1];
149         char *cmd = argv[2];
150         void *rom = loadrom(romname);
151
152         if (rom == NULL) {
153                 printf("Could not load ROM image '%s'.\n", romname);
154                 return 1;
155         }
156
157         if (argc < 5) {
158                 printf("not enough arguments to '%s'.\n", cmd);
159                 return 1;
160         }
161
162         char *filename = argv[3];
163         char *cbfsname = argv[4];
164
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);
169                 return 1;
170         }
171
172         uint32_t base = 0;
173         void *cbfsfile = NULL;
174
175         comp_algo algo = CBFS_COMPRESS_NONE;
176         if (argc > 5) {
177                 if (argv[5][0] == 'l')
178                         algo = CBFS_COMPRESS_LZMA;
179         }
180         if (argc > 6) {
181                 base = strtoul(argv[6], NULL, 0);
182         }
183         unsigned char *stage;
184         filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
185         cbfsfile =
186             create_cbfs_file(cbfsname, stage, &filesize,
187                              CBFS_COMPONENT_STAGE, &base);
188
189         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
190                 printf("Adding stage '%s' failed.\n", filename);
191                 return 1;
192         }
193         if (writerom(romname, rom, romsize))
194                 return 1;
195         return 0;
196 }
197
198 static int cbfs_create(int argc, char **argv)
199 {
200         char *romname = argv[1];
201         if (argc < 5) {
202                 printf("not enough arguments to 'create'.\n");
203                 return 1;
204         }
205
206         char* suffix;
207         uint32_t size = strtoul(argv[3], &suffix, 0);
208         if (tolower(suffix[0])=='k') {
209                 size *= 1024;
210         }
211         if (tolower(suffix[0])=='m') {
212                 size *= 1024 * 1024;
213         }
214         char *bootblock = argv[4];
215         uint32_t align = 0;
216
217         if (argc > 5)
218                 align = strtoul(argv[5], NULL, 0);
219
220         return create_cbfs_image(romname, size, bootblock, align);
221 }
222
223 static int cbfs_locate(int argc, char **argv)
224 {
225         char *romname = argv[1];
226         if (argc < 6) {
227                 printf("not enough arguments to 'locate'.\n");
228                 return 1;
229         }
230
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);
236
237         printf("%x\n", location);
238         return location == 0 ? 1 : 0;
239 }
240
241 static int cbfs_print(int argc, char **argv)
242 {
243         char *romname = argv[1];
244         void *rom = loadrom(romname);
245
246         if (rom == NULL) {
247                 printf("Could not load ROM image '%s'.\n", romname);
248                 return 1;
249         }
250
251         print_cbfs_directory(romname);
252         return 0;
253 }
254
255 static int cbfs_extract(int argc, char **argv)
256 {
257         char *romname = argv[1];
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 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},
282 };
283
284 static 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 /* Small, OS/libc independent runtime check
306  * for endianess
307  */
308 int host_bigendian = 0;
309
310 static void which_endian(void)
311 {
312         char test[4] = "1234";
313         uint32_t inttest = *(uint32_t *) test;
314         if (inttest == 0x31323334) {
315                 host_bigendian = 1;
316         }
317 }
318
319 int main(int argc, char **argv)
320 {
321         int i;
322
323         if (argc < 3) {
324                 usage();
325                 return 1;
326         }
327
328         which_endian();
329
330         char *cmd = argv[2];
331
332         for (i = 0; i < ARRAY_SIZE(commands); i++) {
333                 if (strcmp(cmd, commands[i].name) != 0)
334                         continue;
335                 return commands[i].function(argc, argv);
336         }
337
338         printf("Unknown command '%s'.\n", cmd);
339         usage();
340         return 1;
341 }