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, 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, 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, s.st_size, type);
200 munmap(ptr, s.st_size);
208 printf("add [FILE] [NAME] [TYPE]\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 add_handler(struct rom *rom, int argc, char **argv)
224 unsigned int type = CBFS_COMPONENT_NULL;
231 if (!rom_exists(rom)) {
232 ERROR("You need to create the ROM before adding files to it\n");
236 /* There are two ways to specify the type - a string or a number */
239 if (isdigit(*(argv[2])))
240 type = strtoul(argv[2], 0, 0);
243 if (type == CBFS_COMPONENT_NULL)
244 WARN("No file type was given for %s - using default\n",
247 return add_blob(rom, argv[0], argv[1], type);
250 char *find_tool(char *tool)
252 static char toolpath[MAX_PATH];
253 extern char cbfstool_bindir[];
255 snprintf(toolpath, MAX_PATH - 1, "tools/%s", tool);
256 if (!access(toolpath, X_OK))
259 snprintf(toolpath, MAX_PATH - 1, "%s/tools/%s", cbfstool_bindir, tool);
261 if (!access(toolpath, X_OK))
264 snprintf(toolpath, MAX_PATH - 1, "%s/%s", cbfstool_bindir, tool);
266 if (!access(toolpath, X_OK))
269 strncpy(toolpath, tool, MAX_PATH - 1);
273 /* Invoke the cbfs-mkpayload utility */
275 int add_payload_handler(struct rom *rom, int argc, char **argv)
282 /* Make sure the ROM exists */
284 if (!rom_exists(rom)) {
285 ERROR("You need to create the ROM before adding files to it\n");
289 /* Check that the incoming file exists */
291 if (access(argv[0], R_OK)) {
292 ERROR("File %s does not exist\n", argv[0]);
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);
301 /* Invoke the cbfs-mkstage utility */
303 int add_stage_handler(struct rom *rom, int argc, char **argv)
310 /* Make sure the ROM exists */
312 if (!rom_exists(rom)) {
313 ERROR("You need to create the ROM before adding files to it\n");
317 /* Check that the incoming file exists */
319 if (access(argv[0], R_OK)) {
320 ERROR("File %s does not exist\n", argv[0]);
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);