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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
* 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 <string.h>
#include <stdlib.h>
#include "cbfstool.h"
int namelen(const char *name)
{
return ALIGN(strlen(name) + 1, 16);
}
/**
* Given a name, return the header size for that name.
* @param name The name
* @returns The header size given that name
*/
int headersize(const char *name)
{
return sizeof(struct cbfs_file) + namelen(name);
}
/**
* Given a name, set it into the header in a standard way
* @param file the cbfs file
* @param name The name
*/
void setname(struct cbfs_file *file, const char *name)
{
memset(CBFS_NAME(file), 0, namelen(name));
strcpy((char *)CBFS_NAME(file), name);
}
/**
* Given a name, size, and type, set them into the header in a standard way.
* Special case of size of -1: set the size to all of ROM
* @param rom The rom
* @param c The cbfs file
* @param name The name
* @param size The size
* @param type The type
* @returns Always 0 for now
*/
int rom_set_header(struct rom *rom, struct cbfs_file *c, const char *name, int size, int type)
{
unsigned int csize;
csize = headersize(name);
strcpy(c->magic, COMPONENT_MAGIC);
/* special case -- if size is -1, means "as much as you can"
* it's usually only used in init.
*/
if (size < 0)
size = rom->fssize - csize;
c->len = htonl(size);
c->offset = htonl(csize);
c->type = htonl(type);
setname(c, name);
return 0;
}
int nextfile(struct rom *rom, struct cbfs_file *c, int offset)
{
return ALIGN(offset + ntohl(c->len),
ntohl(rom->header->align));
}
/* split
* split is a basic primitive in cbfs. Over time, it should be the main operator
* used to allocate space. For now for testing we are only using it in the
* fixed-address allocation.
* Split takes a cbfs_file and splits it into two pieces, as determined
* by the size of the file desired. Split only makes sense on CBFS_COMPONENT_NULL
* files -- splitting real files is an error, but no checking is done.
* @param file cbfs_file to split
* @param size Size of the file desired.
* @returns pointer to a cbfs_file stuct.
*/
static struct cbfs_file *split(struct rom *rom, struct cbfs_file *file, int size)
{
struct cbfs_file *newfile = NULL;
unsigned long align = ntohl(rom->header->align);
unsigned long nextoffset, truncoffset;
unsigned long offset = ROM_OFFSET(rom, file);
/* figure out the real end of this file, and hence the size */
/* compute where the next file is */
nextoffset = ALIGN(offset + ntohl(file->len) + headersize(""), align);
/* compute where the end of this new file might be */
truncoffset = ALIGN(offset + size + headersize(""), align);
/* If there is more than align bytes difference, create a new empty file */
/* later, we can add code to merge all empty files. */
if (nextoffset - truncoffset > align) {
unsigned int csize;
csize = headersize("");
newfile = (struct cbfs_file *)ROM_PTR(rom, truncoffset);
rom_set_header(rom, newfile, "",
nextoffset - truncoffset - csize, CBFS_COMPONENT_NULL);
file->len = htonl(size);
}
return newfile;
}
/**
* rom_alloc_fixed
* Given a rom, walk the headers and find the first header of type
* CBFS_COMPONENT_NULL that is >= the desired size and
* contains the (address, length) desired.
* If the CBFS_COMPONENT_NULL is 'align' bytes > size,
* create a new header of CBFS_COMPONENT_NULL following the file.
* The 'len' structure member of the desired file is initialized, but
* nothing else is.
* Simple algorithm: walk until we find an empty file that contains our area,
* and then allocate out of it.
* @param rom The rom
* @param size the size of the file needed
* @returns pointer to a cbfs_file struct.
*/
struct cbfs_file * rom_alloc_fixed(struct rom *rom, const char *name, unsigned long start, unsigned long size, int type)
{
/* walk the rom and find an empty file with a base > base,
* and a large enough size
*/
unsigned long base, end, alen, baseoff;
unsigned int offset = ntohl(rom->header->offset);
int ret = -1;
struct cbfs_file *c = NULL;
unsigned long align = ntohl(rom->header->align);
/* compute a base that is aligned to align */
base = TRUNCATE(start, align);
/* have to leave room for a header! */
base -= headersize(name);
/* get an offset for that base */
baseoff = base - rom->rombase;
end = ALIGN(start + size, align);
alen = end - base;
while (offset < rom->fssize) {
c = (struct cbfs_file *)ROM_PTR(rom, offset);
if (!strcmp(c->magic, COMPONENT_MAGIC)) {
if (c->type != CBFS_COMPONENT_NULL) {
offset += ALIGN(ntohl(c->offset) + ntohl(c->len),
align);
continue;
}
/* could turn this into a function. */
/* is the start of this file < our desired start? */
if (offset > baseoff)
break;
/* Is this file big enough for our needs? */
if (ntohl(c->len) >= alen){
ret = offset;
break;
}
offset += ALIGN(ntohl(c->offset) + ntohl(c->len),
align);
} else {
fprintf(stderr, "Corrupt rom -- found no header at %d\n", offset);
exit(1);
}
}
if (ret < 0)
return NULL;
/* we have the base offset of our location, and we have the offset for the file we are going to
* split. Split it.
*/
if (baseoff > offset)
c = split(rom, c, baseoff - offset - headersize(""));
/* split off anything left at the end that we don't need */
split(rom, c, size);
c->len = htonl(size);
strcpy(c->magic, COMPONENT_MAGIC);
c->offset = htonl(headersize(name));
c->type = htonl(type);
setname(c, name);
return ((struct cbfs_file *)ROM_PTR(rom, ret));
}
/**
* rom_alloc
* Given a rom, walk the headers and find the first header of type
* CBFS_COMPONENT_NULL that is >= the desired size.
* If the CBFS_COMPONENT_NULL is 'align' bytes > size,
* create a new header of CBFS_COMPONENT_NULL following the file.
* The 'len' structure member of the desired file is initialized, but
* nothing else is.
* @param rom The rom
* @param size the size of the file needed
* @returns pointer to a cbfs_file struct.
*/
struct cbfs_file * rom_alloc(struct rom *rom, const char *name, unsigned long size, int type)
{
/* walk the rom and find an empty file with a base > base, and a large enough size */
unsigned int offset = ntohl(rom->header->offset);
int ret = -1;
struct cbfs_file *c = NULL;
unsigned long nextoffset, truncoffset;
struct cbfs_file *newfile = NULL;
while ((offset + size) < rom->fssize) {
c = (struct cbfs_file *)ROM_PTR(rom, offset);
if (!strcmp(c->magic, COMPONENT_MAGIC)) {
if (c->type != CBFS_COMPONENT_NULL) {
offset += ALIGN(ntohl(c->offset) + ntohl(c->len),
ntohl(rom->header->align));
continue;
}
/* Is this file big enough for our needs? */
if (ntohl(c->len) >= size){
ret = offset;
break;
}
offset += ALIGN(ntohl(c->offset) + ntohl(c->len),
ntohl(rom->header->align));
} else {
fprintf(stderr, "Corrupt rom -- found no header at %d\n", offset);
exit(1);
}
}
if (ret < 0)
return NULL;
/* figure out the real end of this file, and hence the size */
/* compute where the next file is */
nextoffset = ALIGN(ret + ntohl(c->len) + headersize(name),
ntohl(rom->header->align));
/* compute where the end of this new file might be */
truncoffset = ALIGN(ret + size + headersize(name),
ntohl(rom->header->align));
/* If there is more than align bytes difference, create a new empty file */
/* later, we can add code to merge all empty files. */
if (nextoffset - truncoffset > ntohl(rom->header->align)) {
unsigned int csize;
csize = headersize("");
newfile = (struct cbfs_file *)ROM_PTR(rom, truncoffset);
rom_set_header(rom, newfile, "",
nextoffset - truncoffset - csize, CBFS_COMPONENT_NULL);
} else truncoffset = nextoffset;
c->len = htonl(size);
strcpy(c->magic, COMPONENT_MAGIC);
c->offset = htonl(headersize(name));
c->type = htonl(type);
setname(c, name);
return ((struct cbfs_file *)ROM_PTR(rom, ret));
}
struct cbfs_file *rom_find(struct rom *rom, int offset)
{
while (offset < rom->fssize) {
struct cbfs_file *c =
(struct cbfs_file *)ROM_PTR(rom, offset);
if (!strcmp(c->magic, COMPONENT_MAGIC))
return c;
offset += ntohl(rom->header->align);
}
return NULL;
}
struct cbfs_file *rom_find_first(struct rom *rom)
{
return rom_find(rom, ntohl(rom->header->offset));
}
struct cbfs_file *rom_find_next(struct rom *rom, struct cbfs_file *prev)
{
unsigned int offset = ROM_OFFSET(rom, prev);
return rom_find(rom, offset +
ALIGN(ntohl(prev->offset) + ntohl(prev->len),
ntohl(rom->header->align)));
}
struct cbfs_file *rom_find_by_name(struct rom *rom, const char *name)
{
struct cbfs_file *c = rom_find_first(rom);
while (c) {
if (!strcmp((char *)CBFS_NAME(c), name))
return c;
c = rom_find_next(rom, c);
}
return NULL;
}
int rom_used_space(struct rom *rom)
{
struct cbfs_file *c = rom_find_first(rom);
unsigned int ret = 0;
while (c) {
int type;
type = ntohl(c->type);
if ((c->type == CBFS_COMPONENT_DELETED) ||
(c->type == CBFS_COMPONENT_NULL))
continue;
ret += ROM_OFFSET(rom, c) + ntohl(c->offset) + ntohl(c->len);
c = rom_find_next(rom, c);
}
return ret;
}
/**
* delete an item. This is a flash-friendly version -- it just blows the
* type to 0. Nothing else is changed.
* N.B. We no longer shuffle contents of ROM. That will come later.
* @param rom The rom
* @param name Name of file to remove.
* @return -1 on error, 0 if a file was set to deleted.
*/
int rom_remove(struct rom *rom, const char *name)
{
struct cbfs_file *c = rom_find_by_name(rom, name);
if (c == NULL) {
ERROR("Component %s does not exist\n", name);
return -1;
}
c->type = CBFS_COMPONENT_DELETED;
return 0;
}
int rom_extract(struct rom *rom, const char *name, void** buf, int *size )
{
struct cbfs_file *c = rom_find_by_name(rom, name);
if (c == NULL) {
ERROR("Component %s does not exist\n", name);
return -1;
}
*size = ntohl(c->len);
*buf = ((unsigned char *)c) + headersize(name);
return 0;
}
/**
* Add a new file named 'name', of type 'type', size 'size'. Initialize that file
* with the contents of 'buffer'.
* @param rom The rom
* @param name file name
* @param buffer file data
* @param address base address. 0 means 'whereever it fits'
* @param size Amount of data
* @param type File type
* @returns -1 on failure, 0 on success
*/
int rom_add(struct rom *rom, const char *name, void *buffer, unsigned long address, int size, int type)
{
struct cbfs_file *c;
if (rom_find_by_name(rom, name)) {
ERROR("Component %s already exists in this rom\n", name);
return -1;
}
if (address)
c = rom_alloc_fixed(rom, name, address, size, type);
else
c = rom_alloc(rom, name, size, type);
if (c == NULL) {
ERROR("There is not enough room in this ROM\n");
return -1;
}
memcpy(((unsigned char *)c) + ntohl(c->offset), buffer, size);
return 0;
}
|