This fixes a rather silly bug in cbfs with filenames > 16 characters.
[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 != 3) {
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 (isdigit(*(argv[2])))
239                 type = strtoul(argv[2], 0, 0);
240         else {
241                 ERROR("String types (%s) aren't implemented yet.\n", argv[2]);
242                 return -1;
243         }
244
245         return add_blob(rom, argv[0], argv[1], type);
246 }
247
248 char *find_tool(char *tool)
249 {
250         static char toolpath[MAX_PATH];
251         extern char cbfstool_bindir[];
252
253         snprintf(toolpath, MAX_PATH - 1, "tools/%s", tool);
254         if (!access(toolpath, X_OK))
255                 return toolpath;
256
257         snprintf(toolpath, MAX_PATH - 1, "%s/tools/%s", cbfstool_bindir, tool);
258
259         if (!access(toolpath, X_OK))
260                 return toolpath;
261
262         snprintf(toolpath, MAX_PATH - 1, "%s/%s", cbfstool_bindir, tool);
263
264         if (!access(toolpath, X_OK))
265                 return toolpath;
266
267         strncpy(toolpath, tool, MAX_PATH - 1);
268         return toolpath;
269 }
270
271 /* Invoke the cbfs-mkpayload utility */
272
273 int add_payload_handler(struct rom *rom, int argc, char **argv)
274 {
275         if (argc < 2) {
276                 add_payload_usage();
277                 return -1;
278         }
279
280         /* Make sure the ROM exists */
281
282         if (!rom_exists(rom)) {
283                 ERROR("You need to create the ROM before adding files to it\n");
284                 return -1;
285         }
286
287         /* Check that the incoming file exists */
288
289         if (access(argv[0], R_OK)) {
290                 ERROR("File %s does not exist\n", argv[0]);
291                 return -1;
292         }
293
294         return fork_tool_and_add(rom, find_tool("cbfs-mkpayload"), argv[0],
295                                  argv[1], CBFS_COMPONENT_PAYLOAD, argc - 2,
296                                  argc > 2 ? &argv[2] : NULL);
297 }
298
299 /* Invoke the cbfs-mkstage utility */
300
301 int add_stage_handler(struct rom *rom, int argc, char **argv)
302 {
303         if (argc < 2) {
304                 add_stage_usage();
305                 return -1;
306         }
307
308         /* Make sure the ROM exists */
309
310         if (!rom_exists(rom)) {
311                 ERROR("You need to create the ROM before adding files to it\n");
312                 return -1;
313         }
314
315         /* Check that the incoming file exists */
316
317         if (access(argv[0], R_OK)) {
318                 ERROR("File %s does not exist\n", argv[0]);
319                 return -1;
320         }
321
322         return fork_tool_and_add(rom, find_tool("cbfs-mkstage"), argv[0],
323                                  argv[1], CBFS_COMPONENT_STAGE, argc - 2,
324                                  argc > 2 ? &argv[2] : NULL);
325 }