minor include cleanups
[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_t;
34
35 struct command {
36         cmd_t id;
37         const char *name;
38         int (*function) (int argc, char **argv);
39 };
40
41 static int cbfs_add(int argc, char **argv)
42 {
43         char *romname = argv[1];
44         char *cmd = argv[2];
45         void *rom = loadrom(romname);
46
47         if (rom == NULL) {
48                 printf("Could not load ROM image '%s'.\n", romname);
49                 return 1;
50         }
51
52         if (argc < 5) {
53                 printf("not enough arguments to '%s'.\n", cmd);
54                 return 1;
55         }
56
57         char *filename = argv[3];
58         char *cbfsname = argv[4];
59
60         uint32_t filesize = 0;
61         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
62         if (filedata == NULL) {
63                 printf("Could not load file '%s'.\n", filename);
64                 return 1;
65         }
66
67         uint32_t base = 0;
68         void *cbfsfile = NULL;
69
70         if (argc < 6) {
71                 printf("not enough arguments to 'add'.\n");
72                 return 1;
73         }
74         uint32_t type;
75         if (intfiletype(argv[5]) != ((uint64_t) - 1))
76                 type = intfiletype(argv[5]);
77         else
78                 type = strtoul(argv[5], NULL, 0);
79         if (argc > 6) {
80                 base = strtoul(argv[6], NULL, 0);
81         }
82         cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
83         if (add_file_to_cbfs(cbfsfile, filesize, base))
84                 return 1;
85         if (writerom(romname, rom, romsize))
86                 return 1;
87         return 0;
88 }
89
90 static int cbfs_add_payload(int argc, char **argv)
91 {
92         char *romname = argv[1];
93         char *cmd = argv[2];
94         void *rom = loadrom(romname);
95
96         if (rom == NULL) {
97                 printf("Could not load ROM image '%s'.\n", romname);
98                 return 1;
99         }
100
101         if (argc < 5) {
102                 printf("not enough arguments to '%s'.\n", cmd);
103                 return 1;
104         }
105
106         char *filename = argv[3];
107         char *cbfsname = argv[4];
108
109         uint32_t filesize = 0;
110         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
111         if (filedata == NULL) {
112                 printf("Could not load file '%s'.\n", filename);
113                 return 1;
114         }
115
116         uint32_t base = 0;
117         void *cbfsfile = NULL;
118
119         comp_algo algo = CBFS_COMPRESS_NONE;
120         if (argc > 5) {
121                 if (argv[5][0] == 'l')
122                         algo = CBFS_COMPRESS_LZMA;
123         }
124         if (argc > 6) {
125                 base = strtoul(argv[6], NULL, 0);
126         }
127         unsigned char *payload;
128         filesize = parse_elf_to_payload(filedata, &payload, algo);
129         cbfsfile =
130             create_cbfs_file(cbfsname, payload, &filesize,
131                              CBFS_COMPONENT_PAYLOAD, &base);
132         if (add_file_to_cbfs(cbfsfile, filesize, base))
133                 return 1;
134         if (writerom(romname, rom, romsize))
135                 return 1;
136         return 0;
137 }
138
139 static int cbfs_add_stage(int argc, char **argv)
140 {
141         char *romname = argv[1];
142         char *cmd = argv[2];
143         void *rom = loadrom(romname);
144
145         if (rom == NULL) {
146                 printf("Could not load ROM image '%s'.\n", romname);
147                 return 1;
148         }
149
150         if (argc < 5) {
151                 printf("not enough arguments to '%s'.\n", cmd);
152                 return 1;
153         }
154
155         char *filename = argv[3];
156         char *cbfsname = argv[4];
157
158         uint32_t filesize = 0;
159         void *filedata = loadfile(filename, &filesize, 0, SEEK_SET);
160         if (filedata == NULL) {
161                 printf("Could not load file '%s'.\n", filename);
162                 return 1;
163         }
164
165         uint32_t base = 0;
166         void *cbfsfile = NULL;
167
168         comp_algo algo = CBFS_COMPRESS_NONE;
169         if (argc > 5) {
170                 if (argv[5][0] == 'l')
171                         algo = CBFS_COMPRESS_LZMA;
172         }
173         if (argc > 6) {
174                 base = strtoul(argv[6], NULL, 0);
175         }
176         unsigned char *stage;
177         filesize = parse_elf_to_stage(filedata, &stage, algo, &base);
178         cbfsfile =
179             create_cbfs_file(cbfsname, stage, &filesize,
180                              CBFS_COMPONENT_STAGE, &base);
181
182         if (add_file_to_cbfs(cbfsfile, filesize, base))
183                 return 1;
184         if (writerom(romname, rom, romsize))
185                 return 1;
186         return 0;
187 }
188
189 static int cbfs_create(int argc, char **argv)
190 {
191         char *romname = argv[1];
192         char *cmd = argv[2];
193         if (argc < 5) {
194                 printf("not enough arguments to 'create'.\n");
195                 return 1;
196         }
197
198         char* suffix;
199         uint32_t size = strtoul(argv[3], &suffix, 0);
200         if (tolower(suffix[0])=='k') {
201                 size *= 1024;
202         }
203         if (tolower(suffix[0])=='m') {
204                 size *= 1024 * 1024;
205         }
206         char *bootblock = argv[4];
207         uint32_t align = 0;
208
209         if (argc > 5)
210                 align = strtoul(argv[5], NULL, 0);
211
212         return create_cbfs_image(romname, size, bootblock, align);
213 }
214
215 static int cbfs_locate(int argc, char **argv)
216 {
217         char *romname = argv[1];
218         if (argc < 6) {
219                 printf("not enough arguments to 'locate'.\n");
220                 return 1;
221         }
222
223         const char *file = argv[3];
224         uint32_t filesize = getfilesize(file);
225         const char *filename = argv[4];
226         int align = strtoul(argv[5], NULL, 0);
227
228         printf("%x\n", cbfs_find_location(romname, filesize, filename, align));
229         return 0;
230 }
231
232 static int cbfs_print(int argc, char **argv)
233 {
234         char *romname = argv[1];
235         char *cmd = argv[2];
236         void *rom = loadrom(romname);
237
238         if (rom == NULL) {
239                 printf("Could not load ROM image '%s'.\n", romname);
240                 return 1;
241         }
242
243         print_cbfs_directory(romname);
244         return 0;
245 }
246
247 struct command commands[] = {
248         {CMD_ADD, "add", cbfs_add},
249         {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
250         {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
251         {CMD_CREATE, "create", cbfs_create},
252         {CMD_LOCATE, "locate", cbfs_locate},
253         {CMD_PRINT, "print", cbfs_print}
254 };
255
256 void usage(void)
257 {
258         printf
259             ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
260              "USAGE:\n" " cbfstool [-h]\n"
261              " cbfstool FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n"
262              "  -h              Display this help message\n\n"
263              "COMMANDs:\n"
264              " add FILE NAME TYPE [base address]    Add a component\n"
265              " add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
266              " add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
267              " create SIZE BOOTBLOCK [ALIGN]        Create a ROM file\n"
268              " locate FILE NAME ALIGN               Find a place for a file of that size\n"
269              " print                                Show the contents of the ROM\n\n"
270              "TYPEs:\n"
271              );
272         print_supported_filetypes();
273 }
274
275 int main(int argc, char **argv)
276 {
277         int i;
278
279         if (argc < 3) {
280                 usage();
281                 return 1;
282         }
283
284         char *cmd = argv[2];
285
286         for (i = 0; i < ARRAY_SIZE(commands); i++) {
287                 if (strcmp(cmd, commands[i].name) != 0)
288                         continue;
289                 return commands[i].function(argc, argv);
290         }
291
292         printf("Unknown command '%s'.\n", cmd);
293         usage();
294         return 1;
295 }