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