diff options
author | Ronald G. Minnich <rminnich@gmail.com> | 2009-06-08 03:33:57 +0000 |
---|---|---|
committer | Ronald G. Minnich <rminnich@gmail.com> | 2009-06-08 03:33:57 +0000 |
commit | 6ce41c06ccdf1c80600ef4cf8e140f928b09d754 (patch) | |
tree | f80df8e3ebddadf14070382474fabcd494561ed2 | |
parent | d64f403f8eeb2da55472d9f6f65b657c063414ed (diff) |
This is transition code for cbfs to implement
cbfs files at fixed addresses.
I call this transitional as the approach I am taking is to add
capability to cbfstool but not change code in a way that will break
existing usages. Later, once we're sure nothing has broken, we can start to
smooth the edges.
Right now, fixed address file are only supported via the add command.
There is one additional command syntax, so, example:
cbfstool add rom romstrap optionrom 0xffffd000
Will add the file to that fix location for a romstrap.
The assumption is that the ROM is based at the end of a 32-bit address
space. As you can see from the code, that assumption can easily be
over-ridden, if we ever need to, with a command option.
Here is one example output result.
rminnich@xcpu2:~/src/bios/coreboot-v2/util/cbfstool$ ./cbfstool x.cbf print
x.cbf: 1024 kB, bootblocksize 32768, romsize 1048576, offset 0x0
Alignment: 16 bytes
Name Offset Type Size
h 0x0 optionrom 251
0x130 free 917120
h3 0xdffe0 optionrom 251
0xe0110 free 97960
The way this is implemented is pretty simple. I introduce a new
operator, split, that splits an unallocated area into two unallocated
areas. Then, allocation merely becomes a matter of 0, 1, or 2 splits:
0 split -- the free area is the exact fit
1 splits -- need to split some off the front or back
2 splits -- need to split off BOTH the front and back
I think you'll be able to see what I've done. I call this transitional
because, in the end state, we only need one allocate function; for now
I've left two in, to make sure I don't break compatibilty.
Why I like this better than ldscript approach: I like having the
ROMSTRAP located by cbfs, not linker scripts. For one thing, it makes
romstrap visible as a first class object. I think I would have latched
onto a problem I was having much more quickly had I remembered the
ROMSTRAP. It gets lost in the linker scripts.
At this point, we should be able to start removing special ROMSTRAP location
code from linker scripts.
Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Acked-by: Patrick Georgi <patrick.georgi@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@4351 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
-rw-r--r-- | util/cbfstool/add.c | 17 | ||||
-rw-r--r-- | util/cbfstool/cbfstool.h | 7 | ||||
-rw-r--r-- | util/cbfstool/fs.c | 131 | ||||
-rw-r--r-- | util/cbfstool/util.c | 5 |
4 files changed, 151 insertions, 9 deletions
diff --git a/util/cbfstool/add.c b/util/cbfstool/add.c index a62a15857b..9950d5bdf9 100644 --- a/util/cbfstool/add.c +++ b/util/cbfstool/add.c @@ -76,7 +76,7 @@ static int add_from_fd(struct rom *rom, const char *name, int type, int fd) return -1; } - ret = rom_add(rom, name, buffer, size, type); + ret = rom_add(rom, name, buffer, 0, size, type); free(buffer); return ret; @@ -165,7 +165,7 @@ int fork_tool_and_add(struct rom *rom, const char *tool, const char *input, } static int add_blob(struct rom *rom, const char *filename, - const char *name, int type) + const char *name, unsigned long address, int type) { void *ptr; struct stat s; @@ -195,7 +195,7 @@ static int add_blob(struct rom *rom, const char *filename, return -1; } - ret = rom_add(rom, name, ptr, s.st_size, type); + ret = rom_add(rom, name, ptr, address, s.st_size, type); munmap(ptr, s.st_size); close(fd); @@ -205,7 +205,7 @@ static int add_blob(struct rom *rom, const char *filename, void add_usage(void) { - printf("add FILE NAME TYPE\tAdd a component\n"); + printf("add FILE NAME TYPE [base address]\tAdd a component\n"); } void add_stage_usage(void) @@ -238,12 +238,17 @@ int select_component_type(char *s) int add_handler(struct rom *rom, int argc, char **argv) { unsigned int type = CBFS_COMPONENT_NULL; + unsigned long address; - if (argc != 3) { + if ((argc < 3) || (argc > 4)) { add_usage(); return -1; } + if (argc > 3) { + address = strtoul(argv[3], 0, 0); + } + if (!rom_exists(rom)) { ERROR("You need to create the ROM before adding files to it\n"); return -1; @@ -278,7 +283,7 @@ int add_handler(struct rom *rom, int argc, char **argv) } } - return add_blob(rom, argv[0], argv[1], type); + return add_blob(rom, argv[0], argv[1], address, type); } char *find_tool(char *tool) diff --git a/util/cbfstool/cbfstool.h b/util/cbfstool/cbfstool.h index 5804fdef50..adfa9d0f31 100644 --- a/util/cbfstool/cbfstool.h +++ b/util/cbfstool/cbfstool.h @@ -31,6 +31,10 @@ struct rom { unsigned char *name; unsigned char *ptr; + /* this will *almost* *always* be 0-rom->size, save for some really + * misdesigned systems (which have existed) + */ + unsigned long rombase; int fd; int size; @@ -51,6 +55,7 @@ struct rom { #define WARN(err, args...) fprintf(stderr, "(cbfstool) W: " err, ##args) #define VERBOSE(str, args...) printf(str, ##args) +#define TRUNCATE(_v, _a) ( (_v) & ~( (_a) - 1 ) ) #define ALIGN(_v, _a) ( ( (_v) + ( (_a) - 1 ) ) & ~( (_a) - 1 ) ) /* Function prototypes */ @@ -71,7 +76,7 @@ int add_bootblock(struct rom *rom, const char *filename); struct cbfs_file *rom_find(struct rom *rom, int offset); struct cbfs_file *rom_find_first(struct rom *); struct cbfs_file *rom_find_next(struct rom *, struct cbfs_file *); -int rom_add(struct rom *rom, const char *name, void *, int size, int type); +int rom_add(struct rom *rom, const char *name, void *, unsigned long address, int size, int type); int rom_set_header(struct rom *rom, struct cbfs_file *c, const char*name, int size, int type); int rom_extract(struct rom *rom, const char *name, void **buf, int *size); diff --git a/util/cbfstool/fs.c b/util/cbfstool/fs.c index a3a1cc1d3b..4f7109a83f 100644 --- a/util/cbfstool/fs.c +++ b/util/cbfstool/fs.c @@ -83,6 +83,129 @@ int nextfile(struct rom *rom, struct cbfs_file *c, int offset) 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 @@ -263,11 +386,12 @@ int rom_extract(struct rom *rom, const char *name, void** buf, int *size ) * @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, int size, int type) +int rom_add(struct rom *rom, const char *name, void *buffer, unsigned long address, int size, int type) { struct cbfs_file *c; @@ -276,7 +400,10 @@ int rom_add(struct rom *rom, const char *name, void *buffer, int size, int type) return -1; } - c = rom_alloc(rom, name, size, type); + 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 no more room in this ROM\n"); diff --git a/util/cbfstool/util.c b/util/cbfstool/util.c index f77b74634b..e1da4cb6fc 100644 --- a/util/cbfstool/util.c +++ b/util/cbfstool/util.c @@ -158,6 +158,11 @@ int open_rom(struct rom *rom, const char *filename) } rom->size = ntohl(rom->header->romsize); + /* compute a 32-bit value of rombase. + * This does the right thing on 64-bit machines. + */ + rom->rombase = 0-rom->size; + rom->rombase &= 0xffffffff; rom->fssize = rom->size - ntohl(rom->header->bootblocksize); return 0; |