nvramtool: 64bit safe CBFS handling
[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_REMOVE,
33         CMD_CREATE,
34         CMD_LOCATE,
35         CMD_PRINT,
36         CMD_EXTRACT,
37 } cmd_t;
38
39 struct command {
40         cmd_t id;
41         const char *name;
42         int (*function) (int argc, char **argv);
43 };
44
45 static int cbfs_add(int argc, char **argv)
46 {
47         char *romname = argv[1];
48         char *cmd = argv[2];
49         void *rom = loadrom(romname);
50
51         if (rom == NULL) {
52                 printf("Could not load ROM image '%s'.\n", romname);
53                 return 1;
54         }
55
56         if (argc < 5) {
57                 printf("not enough arguments to '%s'.\n", cmd);
58                 return 1;
59         }
60
61         char *filename = argv[3];
62         char *cbfsname = argv[4];
63
64         uint32_t filesize = 0;
65         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
66         if (filedata == NULL) {
67                 printf("Could not load file '%s'.\n", filename);
68                 return 1;
69         }
70
71         uint32_t base = 0;
72         void *cbfsfile = NULL;
73
74         if (argc < 6) {
75                 printf("not enough arguments to 'add'.\n");
76                 return 1;
77         }
78         uint32_t type;
79         if (intfiletype(argv[5]) != ((uint64_t) - 1))
80                 type = intfiletype(argv[5]);
81         else
82                 type = strtoul(argv[5], NULL, 0);
83         if (argc > 6) {
84                 base = strtoul(argv[6], NULL, 0);
85         }
86         cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
87         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
88                 printf("Adding file '%s' failed.\n", filename);
89                 return 1;
90         }
91         if (writerom(romname, rom, romsize))
92                 return 1;
93         return 0;
94 }
95
96 static int cbfs_add_payload(int argc, char **argv)
97 {
98         char *romname = argv[1];
99         char *cmd = argv[2];
100         void *rom = loadrom(romname);
101
102         if (rom == NULL) {
103                 printf("Could not load ROM image '%s'.\n", romname);
104                 return 1;
105         }
106
107         if (argc < 5) {
108                 printf("not enough arguments to '%s'.\n", cmd);
109                 return 1;
110         }
111
112         char *filename = argv[3];
113         char *cbfsname = argv[4];
114
115         uint32_t filesize = 0;
116         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
117         if (filedata == NULL) {
118                 printf("Could not load file '%s'.\n", filename);
119                 return 1;
120         }
121
122         uint32_t base = 0;
123         void *cbfsfile = NULL;
124
125         comp_algo algo = CBFS_COMPRESS_NONE;
126         if (argc > 5) {
127                 if (argv[5][0] == 'l')
128                         algo = CBFS_COMPRESS_LZMA;
129         }
130         if (argc > 6) {
131                 base = strtoul(argv[6], NULL, 0);
132         }
133         unsigned char *payload;
134         filesize = parse_elf_to_payload(filedata, &payload, algo);
135         cbfsfile =
136             create_cbfs_file(cbfsname, payload, &filesize,
137                              CBFS_COMPONENT_PAYLOAD, &base);
138         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
139                 printf("Adding payload '%s' failed.\n", filename);
140                 return 1;
141         }
142         if (writerom(romname, rom, romsize))
143                 return 1;
144         return 0;
145 }
146
147 static int cbfs_add_stage(int argc, char **argv)
148 {
149         char *romname = argv[1];
150         char *cmd = argv[2];
151         void *rom = loadrom(romname);
152
153         if (rom == NULL) {
154                 printf("Could not load ROM image '%s'.\n", romname);
155                 return 1;
156         }
157
158         if (argc < 5) {
159                 printf("not enough arguments to '%s'.\n", cmd);
160                 return 1;
161         }
162
163         char *filename = argv[3];
164         char *cbfsname = argv[4];
165
166         uint32_t filesize = 0;
167         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
168         if (filedata == NULL) {
169                 printf("Could not load file '%s'.\n", filename);
170                 return 1;
171         }
172
173         uint32_t base = 0;
174         void *cbfsfile = NULL;
175
176         comp_algo algo = CBFS_COMPRESS_NONE;
177         if (argc > 5) {
178                 if (argv[5][0] == 'l')
179                         algo = CBFS_COMPRESS_LZMA;
180         }
181         if (argc > 6) {
182                 base = strtoul(argv[6], NULL, 0);
183         }
184         unsigned char *stage;
185         filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
186         cbfsfile =
187             create_cbfs_file(cbfsname, stage, &filesize,
188                              CBFS_COMPONENT_STAGE, &base);
189
190         if (add_file_to_cbfs(cbfsfile, filesize, base)) {
191                 printf("Adding stage '%s' failed.\n", filename);
192                 return 1;
193         }
194         if (writerom(romname, rom, romsize))
195                 return 1;
196         return 0;
197 }
198
199 static int cbfs_remove(int argc, char **argv)
200 {
201         char *romname = argv[1];
202         char *cmd = argv[2];
203         void *rom = loadrom(romname);
204
205         if (rom == NULL) {
206                 printf("Could not load ROM image '%s'.\n", romname);
207                 return 1;
208         }
209
210         if (argc < 4) {
211                 printf("not enough arguments to '%s'.\n", cmd);
212                 return 1;
213         }
214
215         char *cbfsname = argv[3];
216
217         if (remove_file_from_cbfs(cbfsname)) {
218                 printf("Removing file '%s' failed.\n", cbfsname);
219                 return 1;
220         }
221         if (writerom(romname, rom, romsize))
222                 return 1;
223         return 0;
224 }
225
226 static int cbfs_create(int argc, char **argv)
227 {
228         char *romname = argv[1];
229         if (argc < 5) {
230                 printf("not enough arguments to 'create'.\n");
231                 return 1;
232         }
233
234         char* suffix;
235         uint32_t size = strtoul(argv[3], &suffix, 0);
236         if (tolower(suffix[0])=='k') {
237                 size *= 1024;
238         }
239         if (tolower(suffix[0])=='m') {
240                 size *= 1024 * 1024;
241         }
242         char *bootblock = argv[4];
243         uint32_t align = 0;
244
245         if (argc > 5)
246                 align = strtoul(argv[5], NULL, 0);
247
248         return create_cbfs_image(romname, size, bootblock, align);
249 }
250
251 static int cbfs_locate(int argc, char **argv)
252 {
253         char *romname = argv[1];
254         if (argc < 6) {
255                 printf("not enough arguments to 'locate'.\n");
256                 return 1;
257         }
258
259         const char *file = argv[3];
260         uint32_t filesize = getfilesize(file);
261         const char *filename = argv[4];
262         int align = strtoul(argv[5], NULL, 0);
263         uint32_t location = cbfs_find_location(romname, filesize, filename, align);
264
265         printf("%x\n", location);
266         return location == 0 ? 1 : 0;
267 }
268
269 static int cbfs_print(int argc, char **argv)
270 {
271         char *romname = argv[1];
272         void *rom = loadrom(romname);
273
274         if (rom == NULL) {
275                 printf("Could not load ROM image '%s'.\n", romname);
276                 return 1;
277         }
278
279         print_cbfs_directory(romname);
280         return 0;
281 }
282
283 static int cbfs_extract(int argc, char **argv)
284 {
285         char *romname = argv[1];
286         void *rom = loadrom(romname);
287
288         if (rom == NULL) {
289                 printf("Could not load ROM image '%s'.\n", romname);
290                 return 1;
291         }
292
293         if (argc != 5)
294         {
295                 printf("Error: you must specify a CBFS name and a file to dump it in.\n");
296                 return 1;
297         }
298
299         return extract_file_from_cbfs(romname, argv[3], argv[4]);
300 }
301
302 static const struct command commands[] = {
303         {CMD_ADD, "add", cbfs_add},
304         {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
305         {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
306         {CMD_REMOVE, "remove", cbfs_remove},
307         {CMD_CREATE, "create", cbfs_create},
308         {CMD_LOCATE, "locate", cbfs_locate},
309         {CMD_PRINT, "print", cbfs_print},
310         {CMD_EXTRACT, "extract", cbfs_extract},
311 };
312
313 static void usage(void)
314 {
315         printf
316             ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
317              "USAGE:\n" " cbfstool [-h]\n"
318              " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
319              "  -h              Display this help message\n\n"
320              "COMMANDs:\n"
321              " add FILE NAME TYPE [base address]    Add a component\n"
322              " add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
323              " add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
324              " remove FILE NAME                     Remove a component\n"
325              " create SIZE BOOTBLOCK [ALIGN]        Create a ROM file\n"
326              " locate FILE NAME ALIGN               Find a place for a file of that size\n"
327              " print                                Show the contents of the ROM\n"
328              " extract NAME FILE                    Extracts a raw payload from ROM\n"
329              "\n"
330              "TYPEs:\n"
331              );
332         print_supported_filetypes();
333 }
334
335 /* Small, OS/libc independent runtime check
336  * for endianess
337  */
338 int host_bigendian = 0;
339
340 static void which_endian(void)
341 {
342         char test[4] = "1234";
343         uint32_t inttest = *(uint32_t *) test;
344         if (inttest == 0x31323334) {
345                 host_bigendian = 1;
346         }
347 }
348
349 int main(int argc, char **argv)
350 {
351         int i;
352
353         if (argc < 3) {
354                 usage();
355                 return 1;
356         }
357
358         which_endian();
359
360         char *cmd = argv[2];
361
362         for (i = 0; i < ARRAY_SIZE(commands); i++) {
363                 if (strcmp(cmd, commands[i].name) != 0)
364                         continue;
365                 return commands[i].function(argc, argv);
366         }
367
368         printf("Unknown command '%s'.\n", cmd);
369         usage();
370         return 1;
371 }