util/cbfstool/tools/rom-mk*->cbfs-mk* rename
[coreboot.git] / util / cbfstool / add.c
1 /*
2  * cbfstool
3  *
4  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18  */
19
20 #include <ctype.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <sys/wait.h>
29 #include "cbfstool.h"
30
31 #define MAX_PATH 255
32
33 static int add_from_fd(struct rom *rom, const char *name, int type, int fd)
34 {
35         unsigned char *buffer = malloc(16 * 1024);
36         unsigned char *ptr = buffer;
37
38         int size = 0;
39         int aloc = 16 * 1024;
40         int remain = 16 * 1024;
41         int ret;
42
43         if (buffer == NULL)
44                 return -1;
45
46         while (1) {
47                 ret = read(fd, ptr, remain);
48
49                 if (ret <= 0)
50                         break;
51
52                 ptr += ret;
53                 remain -= ret;
54                 size += ret;
55
56                 if (remain == 0) {
57                         buffer = realloc(buffer, aloc + 16 * 1024);
58
59                         if (buffer == NULL) {
60                                 ret = -1;
61                                 break;
62                         }
63
64                         ptr = buffer + size;
65
66                         aloc += (16 * 1024);
67                         remain = 16 * 1024;
68                 }
69         }
70
71         if (ret == -1 || size == 0) {
72
73                 if (buffer != NULL)
74                         free(buffer);
75
76                 return -1;
77         }
78
79         ret = rom_add(rom, name, buffer, size, type);
80         free(buffer);
81
82         return ret;
83 }
84
85 int fork_tool_and_add(struct rom *rom, const char *tool, const char *input,
86                       const char *name, int type, int argc, char **argv)
87 {
88         int output[2];
89         pid_t pid;
90         int ret;
91         int status;
92         char **toolargs;
93         int i;
94
95         /* Create the pipe */
96
97         if (pipe(output)) {
98                 ERROR("Couldn't create a pipe: %m\n");
99                 return -1;
100         }
101
102         toolargs = (char **)malloc((5 + argc) * sizeof(char *));
103
104         if (toolargs == NULL) {
105                 ERROR("Unable to allocate memory: %m\n");
106                 return -1;
107         }
108
109         toolargs[0] = (char *)tool;
110
111         /* these are args. So they need a - in front */
112         for (i = 0; i < argc; i++) {
113                 /* I wish I had python */
114                 char *c = malloc(strlen(argv[i])) + 2;
115                 c[0] = '-';
116                 strcpy(&c[1], argv[i]);
117                 c[strlen(argv[i])+1] = 0;
118                 toolargs[1 + i] = c;
119         }
120
121         toolargs[1 + argc] = "-o";
122         toolargs[2 + argc] = "-";
123         toolargs[3 + argc] = (char *)input;
124         toolargs[4 + argc] = NULL;
125
126         pid = fork();
127
128         if (pid == 0) {
129
130                 /* Set up stdin/stdout for the child */
131
132                 dup2(output[1], STDOUT_FILENO);
133                 close(output[0]);
134
135                 /* Execute the tool */
136                 if (execv(tool, toolargs)) {
137                         ERROR("Unable to execute %s: %m\n", tool);
138                         exit(-1);
139                 }
140
141                 exit(0);
142         }
143
144         free(toolargs);
145
146         close(output[1]);
147
148         /* Read from the file */
149         ret = add_from_fd(rom, name, type, output[0]);
150
151         /* Reap the child */
152         waitpid(pid, &status, 0);
153
154         if (WIFSIGNALED(status)) {
155                 kill(pid, WTERMSIG(status));
156                 ERROR("Error while executing %s\n", tool);
157                 return -1;
158         } else if (WEXITSTATUS(status) != 0) {
159                 ERROR("Error while executing %s: %d\n", tool,
160                       (int)WEXITSTATUS(status));
161                 return -1;
162         }
163
164         return ret;
165 }
166
167 static int add_blob(struct rom *rom, const char *filename,
168                     const char *name, int type)
169 {
170         void *ptr;
171         struct stat s;
172         int fd, ret;
173
174         if (!strcmp(filename, "-"))
175                 return add_from_fd(rom, name, type, 0);
176
177         fd = open(filename, O_RDONLY);
178
179         if (fd == -1) {
180                 ERROR("Could not open %s: %m\n", filename);
181                 return -1;
182         }
183
184         if (fstat(fd, &s)) {
185                 ERROR("Could not stat %s: %m\n", filename);
186                 close(fd);
187                 return -1;
188         }
189
190         ptr = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
191
192         if (ptr == MAP_FAILED) {
193                 ERROR("Unable to map %s: %m\n", filename);
194                 close(fd);
195                 return -1;
196         }
197
198         ret = rom_add(rom, name, ptr, s.st_size, type);
199
200         munmap(ptr, s.st_size);
201         close(fd);
202
203         return ret;
204 }
205
206 void add_usage(void)
207 {
208         printf("add [FILE] [NAME] [TYPE]\tAdd a component\n");
209 }
210
211 void add_stage_usage(void)
212 {
213         printf("add-stage [FILE] [NAME] [OPTIONS]\tAdd a stage to the ROM\n");
214 }
215
216 void add_payload_usage(void)
217 {
218         printf
219             ("add-payload [FILE] [NAME] [OPTIONS]\tAdd a payload to the ROM\n");
220 }
221
222 int add_handler(struct rom *rom, int argc, char **argv)
223 {
224         unsigned int type = CBFS_COMPONENT_NULL;
225
226         if (argc < 2) {
227                 add_usage();
228                 return -1;
229         }
230
231         if (!rom_exists(rom)) {
232                 ERROR("You need to create the ROM before adding files to it\n");
233                 return -1;
234         }
235
236         /* There are two ways to specify the type - a string or a number */
237
238         if (argc == 3) {
239                 if (isdigit(*(argv[2])))
240                         type = strtoul(argv[2], 0, 0);
241         }
242
243         if (type == CBFS_COMPONENT_NULL)
244                 WARN("No file type was given for %s - using default\n",
245                      argv[0]);
246
247         return add_blob(rom, argv[0], argv[1], type);
248 }
249
250 char *find_tool(char *tool)
251 {
252         static char toolpath[MAX_PATH];
253         extern char cbfstool_bindir[];
254
255         snprintf(toolpath, MAX_PATH - 1, "tools/%s", tool);
256         if (!access(toolpath, X_OK))
257                 return toolpath;
258
259         snprintf(toolpath, MAX_PATH - 1, "%s/tools/%s", cbfstool_bindir, tool);
260
261         if (!access(toolpath, X_OK))
262                 return toolpath;
263
264         snprintf(toolpath, MAX_PATH - 1, "%s/%s", cbfstool_bindir, tool);
265
266         if (!access(toolpath, X_OK))
267                 return toolpath;
268
269         strncpy(toolpath, tool, MAX_PATH - 1);
270         return toolpath;
271 }
272
273 /* Invoke the cbfs-mkpayload utility */
274
275 int add_payload_handler(struct rom *rom, int argc, char **argv)
276 {
277         if (argc < 2) {
278                 add_payload_usage();
279                 return -1;
280         }
281
282         /* Make sure the ROM exists */
283
284         if (!rom_exists(rom)) {
285                 ERROR("You need to create the ROM before adding files to it\n");
286                 return -1;
287         }
288
289         /* Check that the incoming file exists */
290
291         if (access(argv[0], R_OK)) {
292                 ERROR("File %s does not exist\n", argv[0]);
293                 return -1;
294         }
295
296         return fork_tool_and_add(rom, find_tool("cbfs-mkpayload"), argv[0],
297                                  argv[1], CBFS_COMPONENT_PAYLOAD, argc - 2,
298                                  argc > 2 ? &argv[2] : NULL);
299 }
300
301 /* Invoke the cbfs-mkstage utility */
302
303 int add_stage_handler(struct rom *rom, int argc, char **argv)
304 {
305         if (argc < 2) {
306                 add_stage_usage();
307                 return -1;
308         }
309
310         /* Make sure the ROM exists */
311
312         if (!rom_exists(rom)) {
313                 ERROR("You need to create the ROM before adding files to it\n");
314                 return -1;
315         }
316
317         /* Check that the incoming file exists */
318
319         if (access(argv[0], R_OK)) {
320                 ERROR("File %s does not exist\n", argv[0]);
321                 return -1;
322         }
323
324         return fork_tool_and_add(rom, find_tool("cbfs-mkstage"), argv[0],
325                                  argv[1], CBFS_COMPONENT_STAGE, argc - 2,
326                                  argc > 2 ? &argv[2] : NULL);
327 }