Add a "locate" function cbfstool, which helps you find
out a suitable address to put a XIP stage to.
Specifically, you pass it the file (to get its filesize), its filename
(as the header has a variable length that depends on it), and the
granularity requirement it has to fit in (for XIP).
The granularity is MTRR-style: when you request 0x10000, cbfstool looks
for a suitable place in a 64kb-aligned 64kb block.
cbfstool simply prints out a hex value which is the start address of a
suitably located free memory block. That value can then be used with
cbfs add-stage to store the file in the ROM image.
It's a two-step operation (instead of being merged into cbfs add-stage)
because the image must be linked twice: First, with some bogus, but safe
base address (eg. 0) to figure out the target address (based on file
size). Then a second time at the target address.
The work flow is:
- link file
- cbfstool locate
- link file again
- cbfstool add-stage.
Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Acked-by: Stefan Reinauer <stepan@coresystems.de>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@4929 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 4c45399..1f69fa3 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -27,6 +27,16 @@
#define dprintf
+uint32_t getfilesize(const char *filename)
+{
+ uint32_t size;
+ FILE *file = fopen(filename, "rb");
+ fseek(file, 0, SEEK_END);
+ size = ftell(file);
+ fclose(file);
+ return size;
+}
+
void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
int place)
{
@@ -323,3 +333,58 @@
writerom(romfile, romarea, romsize);
return 0;
}
+
+static int in_segment(int addr, int size, int gran)
+{
+ return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
+}
+
+uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
+ const char *filename, uint32_t alignment)
+{
+ void *rom = loadrom(romfile);
+ int filename_size = strlen(filename);
+
+ int headersize =
+ sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
+ 16) + sizeof(struct cbfs_stage);
+ int totalsize = headersize + filesize;
+
+ uint32_t current = phys_start;
+ while (current < phys_end) {
+ if (!cbfs_file_header(current)) {
+ current += align;
+ continue;
+ }
+ struct cbfs_file *thisfile =
+ (struct cbfs_file *)phys_to_virt(current);
+
+ uint32_t top =
+ current + ntohl(thisfile->len) + ntohl(thisfile->offset);
+ if (((ntohl(thisfile->type) == 0x0)
+ || (ntohl(thisfile->type) == 0xffffffff))
+ && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
+ totalsize)) {
+ if (in_segment
+ (current + headersize, filesize, alignment))
+ return current + headersize;
+ if ((ALIGN(current, alignment) + filesize < top)
+ && (ALIGN(current, alignment) - headersize >
+ current)
+ && in_segment(ALIGN(current, alignment), filesize,
+ alignment))
+ return ALIGN(current, alignment);
+ if ((ALIGN(current, alignment) + alignment + filesize <
+ top)
+ && (ALIGN(current, alignment) + alignment -
+ headersize > current)
+ && in_segment(ALIGN(current, alignment) + alignment,
+ filesize, alignment))
+ return ALIGN(current, alignment) + alignment;
+ }
+ current =
+ ALIGN(current + ntohl(thisfile->len) +
+ ntohl(thisfile->offset), align);
+ }
+ return 0;
+}