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