the tool chain settings should not be in renamed (as they will never live in
[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, 0, 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, unsigned long address, 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, address, 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 [base address]\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 select_component_type(char *s)
223 {
224         int i = 0;
225         char *accepted_strings[] = {
226                 "stage",
227                 "payload",
228                 "optionrom",
229                 "deleted",
230                 "free",
231         };
232         for (i=0; i < 5; i++)
233                 if (!strcmp(s, accepted_strings[i]))
234                         return i;
235         return -1;
236 }
237
238 int add_handler(struct rom *rom, int argc, char **argv)
239 {
240         unsigned int type = CBFS_COMPONENT_NULL;
241         unsigned long address = 0;
242
243         if ((argc < 3) || (argc >  4)) {
244                 add_usage();
245                 return -1;
246         }
247
248         if (argc > 3) {
249                 address = strtoul(argv[3], 0, 0);
250         }
251
252         if (!rom_exists(rom)) {
253                 ERROR("You need to create the ROM before adding files to it\n");
254                 return -1;
255         }
256
257         int component_type;
258
259         /* There are two ways to specify the type - a string or a number */
260
261         if (isdigit(*(argv[2])))
262                 type = strtoul(argv[2], 0, 0);
263         else {
264                 switch( component_type = select_component_type(argv[2])) {
265                         case 0:
266                                 type = CBFS_COMPONENT_STAGE;
267                                 break;
268                         case 1:
269                                 type = CBFS_COMPONENT_PAYLOAD;
270                                 break;
271                         case 2:
272                                 type = CBFS_COMPONENT_OPTIONROM;
273                                 break;
274                         case 3:
275                                 type = CBFS_COMPONENT_DELETED;
276                                 break;
277                         case 4:
278                                 type = CBFS_COMPONENT_NULL;
279                                 break;
280                         default:
281                                 ERROR("Unrecognized component type %s.\nValid options are: stage, payload, optionrom, deleted, free.\n", argv[2]);
282                                 return -1;
283                 }
284         }
285
286         return add_blob(rom, argv[0], argv[1], address, type);
287 }
288
289 char *find_tool(char *tool)
290 {
291         static char toolpath[MAX_PATH];
292         extern char cbfstool_bindir[];
293
294         snprintf(toolpath, MAX_PATH - 1, "tools/%s", tool);
295         if (!access(toolpath, X_OK))
296                 return toolpath;
297
298         snprintf(toolpath, MAX_PATH - 1, "%s/tools/%s", cbfstool_bindir, tool);
299
300         if (!access(toolpath, X_OK))
301                 return toolpath;
302
303         snprintf(toolpath, MAX_PATH - 1, "%s/%s", cbfstool_bindir, tool);
304
305         if (!access(toolpath, X_OK))
306                 return toolpath;
307
308         strncpy(toolpath, tool, MAX_PATH - 1);
309         return toolpath;
310 }
311
312 /* Invoke the cbfs-mkpayload utility */
313
314 int add_payload_handler(struct rom *rom, int argc, char **argv)
315 {
316         if (argc < 2) {
317                 add_payload_usage();
318                 return -1;
319         }
320
321         /* Make sure the ROM exists */
322
323         if (!rom_exists(rom)) {
324                 ERROR("You need to create the ROM before adding files to it\n");
325                 return -1;
326         }
327
328         /* Check that the incoming file exists */
329
330         if (access(argv[0], R_OK)) {
331                 ERROR("File %s does not exist\n", argv[0]);
332                 return -1;
333         }
334
335         return fork_tool_and_add(rom, find_tool("cbfs-mkpayload"), argv[0],
336                                  argv[1], CBFS_COMPONENT_PAYLOAD, argc - 2,
337                                  argc > 2 ? &argv[2] : NULL);
338 }
339
340 /* Invoke the cbfs-mkstage utility */
341
342 int add_stage_handler(struct rom *rom, int argc, char **argv)
343 {
344         if (argc < 2) {
345                 add_stage_usage();
346                 return -1;
347         }
348
349         /* Make sure the ROM exists */
350
351         if (!rom_exists(rom)) {
352                 ERROR("You need to create the ROM before adding files to it\n");
353                 return -1;
354         }
355
356         /* Check that the incoming file exists */
357
358         if (access(argv[0], R_OK)) {
359                 ERROR("File %s does not exist\n", argv[0]);
360                 return -1;
361         }
362
363         return fork_tool_and_add(rom, find_tool("cbfs-mkstage"), argv[0],
364                                  argv[1], CBFS_COMPONENT_STAGE, argc - 2,
365                                  argc > 2 ? &argv[2] : NULL);
366 }