4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
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.
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.
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
33 static int add_from_fd(struct rom *rom, const char *name, int type, int fd)
35 unsigned char *buffer = malloc(16 * 1024);
36 unsigned char *ptr = buffer;
40 int remain = 16 * 1024;
47 ret = read(fd, ptr, remain);
57 buffer = realloc(buffer, aloc + 16 * 1024);
71 if (ret == -1 || size == 0) {
79 ret = rom_add(rom, name, buffer, 0, size, type);
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)
98 ERROR("Couldn't create a pipe: %m\n");
102 toolargs = (char **)malloc((5 + argc) * sizeof(char *));
104 if (toolargs == NULL) {
105 ERROR("Unable to allocate memory: %m\n");
109 toolargs[0] = (char *)tool;
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;
116 strcpy(&c[1], argv[i]);
117 c[strlen(argv[i])+1] = 0;
121 toolargs[1 + argc] = "-o";
122 toolargs[2 + argc] = "-";
123 toolargs[3 + argc] = (char *)input;
124 toolargs[4 + argc] = NULL;
130 /* Set up stdin/stdout for the child */
132 dup2(output[1], STDOUT_FILENO);
135 /* Execute the tool */
136 if (execv(tool, toolargs)) {
137 ERROR("Unable to execute %s: %m\n", tool);
148 /* Read from the file */
149 ret = add_from_fd(rom, name, type, output[0]);
152 waitpid(pid, &status, 0);
154 if (WIFSIGNALED(status)) {
155 kill(pid, WTERMSIG(status));
156 ERROR("Error while executing %s\n", tool);
158 } else if (WEXITSTATUS(status) != 0) {
159 ERROR("Error while executing %s: %d\n", tool,
160 (int)WEXITSTATUS(status));
167 static int add_blob(struct rom *rom, const char *filename,
168 const char *name, unsigned long address, int type)
174 if (!strcmp(filename, "-"))
175 return add_from_fd(rom, name, type, 0);
177 fd = open(filename, O_RDONLY);
180 ERROR("Could not open %s: %m\n", filename);
185 ERROR("Could not stat %s: %m\n", filename);
190 ptr = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
192 if (ptr == MAP_FAILED) {
193 ERROR("Unable to map %s: %m\n", filename);
198 ret = rom_add(rom, name, ptr, address, s.st_size, type);
200 munmap(ptr, s.st_size);
208 printf("add FILE NAME TYPE [base address]\tAdd a component\n");
211 void add_stage_usage(void)
213 printf("add-stage FILE NAME [OPTIONS]\tAdd a stage to the ROM\n");
216 void add_payload_usage(void)
219 ("add-payload FILE NAME [OPTIONS]\tAdd a payload to the ROM\n");
222 int select_component_type(char *s)
225 char *accepted_strings[] = {
232 for (i=0; i < 5; i++)
233 if (!strcmp(s, accepted_strings[i]))
238 int add_handler(struct rom *rom, int argc, char **argv)
240 unsigned int type = CBFS_COMPONENT_NULL;
241 unsigned long address = 0;
243 if ((argc < 3) || (argc > 4)) {
249 address = strtoul(argv[3], 0, 0);
252 if (!rom_exists(rom)) {
253 ERROR("You need to create the ROM before adding files to it\n");
259 /* There are two ways to specify the type - a string or a number */
261 if (isdigit(*(argv[2])))
262 type = strtoul(argv[2], 0, 0);
264 switch( component_type = select_component_type(argv[2])) {
266 type = CBFS_COMPONENT_STAGE;
269 type = CBFS_COMPONENT_PAYLOAD;
272 type = CBFS_COMPONENT_OPTIONROM;
275 type = CBFS_COMPONENT_DELETED;
278 type = CBFS_COMPONENT_NULL;
281 ERROR("Unrecognized component type %s.\nValid options are: stage, payload, optionrom, deleted, free.\n", argv[2]);
286 return add_blob(rom, argv[0], argv[1], address, type);
289 char *find_tool(char *tool)
291 static char toolpath[MAX_PATH];
292 extern char cbfstool_bindir[];
294 snprintf(toolpath, MAX_PATH - 1, "tools/%s", tool);
295 if (!access(toolpath, X_OK))
298 snprintf(toolpath, MAX_PATH - 1, "%s/tools/%s", cbfstool_bindir, tool);
300 if (!access(toolpath, X_OK))
303 snprintf(toolpath, MAX_PATH - 1, "%s/%s", cbfstool_bindir, tool);
305 if (!access(toolpath, X_OK))
308 strncpy(toolpath, tool, MAX_PATH - 1);
312 /* Invoke the cbfs-mkpayload utility */
314 int add_payload_handler(struct rom *rom, int argc, char **argv)
321 /* Make sure the ROM exists */
323 if (!rom_exists(rom)) {
324 ERROR("You need to create the ROM before adding files to it\n");
328 /* Check that the incoming file exists */
330 if (access(argv[0], R_OK)) {
331 ERROR("File %s does not exist\n", argv[0]);
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);
340 /* Invoke the cbfs-mkstage utility */
342 int add_stage_handler(struct rom *rom, int argc, char **argv)
349 /* Make sure the ROM exists */
351 if (!rom_exists(rom)) {
352 ERROR("You need to create the ROM before adding files to it\n");
356 /* Check that the incoming file exists */
358 if (access(argv[0], R_OK)) {
359 ERROR("File %s does not exist\n", argv[0]);
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);