Various fixes to cbfstool.
[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
236         printf("%x\n", cbfs_find_location(romname, filesize, filename, align));
237         return 0;
238 }
239
240 static int cbfs_print(int argc, char **argv)
241 {
242         char *romname = argv[1];
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         void *rom = loadrom(romname);
258
259         if (rom == NULL) {
260                 printf("Could not load ROM image '%s'.\n", romname);
261                 return 1;
262         }
263
264         if (argc != 5)
265         {
266                 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
267                 return 1;
268         }
269
270         return extract_file_from_cbfs(romname, argv[3], argv[4]);
271 }
272
273 static const struct command commands[] = {
274         {CMD_ADD, "add", cbfs_add},
275         {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
276         {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
277         {CMD_CREATE, "create", cbfs_create},
278         {CMD_LOCATE, "locate", cbfs_locate},
279         {CMD_PRINT, "print", cbfs_print},
280         {CMD_EXTRACT, "extract", cbfs_extract},
281 };
282
283 static void usage(void)
284 {
285         printf
286             ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
287              "USAGE:\n" " cbfstool [-h]\n"
288              " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
289              "  -h              Display this help message\n\n"
290              "COMMANDs:\n"
291              " add FILE NAME TYPE [base address]    Add a component\n"
292              " add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
293              " add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
294              " create SIZE BOOTBLOCK [ALIGN]        Create a ROM file\n"
295              " locate FILE NAME ALIGN               Find a place for a file of that size\n"
296              " print                                Show the contents of the ROM\n"
297              " extract NAME FILE                    Extracts a raw payload from ROM\n"
298              "\n"
299              "TYPEs:\n"
300              );
301         print_supported_filetypes();
302 }
303
304 /* Small, OS/libc independent runtime check
305  * for endianess
306  */
307 int host_bigendian = 0;
308
309 static void which_endian(void)
310 {
311         char test[4] = "1234";
312         uint32_t inttest = *(uint32_t *) test;
313         if (inttest == 0x31323334) {
314                 host_bigendian = 1;
315         }
316 }
317
318 int main(int argc, char **argv)
319 {
320         int i;
321
322         if (argc < 3) {
323                 usage();
324                 return 1;
325         }
326
327         which_endian();
328
329         char *cmd = argv[2];
330
331         for (i = 0; i < ARRAY_SIZE(commands); i++) {
332                 if (strcmp(cmd, commands[i].name) != 0)
333                         continue;
334                 return commands[i].function(argc, argv);
335         }
336
337         printf("Unknown command '%s'.\n", cmd);
338         usage();
339         return 1;
340 }