Add a "locate" function cbfstool, which helps you find
authorPatrick Georgi <patrick.georgi@coresystems.de>
Mon, 9 Nov 2009 17:18:02 +0000 (17:18 +0000)
committerPatrick Georgi <patrick.georgi@coresystems.de>
Mon, 9 Nov 2009 17:18:02 +0000 (17:18 +0000)
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

util/cbfstool/cbfstool.c
util/cbfstool/common.c
util/cbfstool/common.h

index bf8b3812b8099de8eb00ba91051a9d7291be09d1..d8f2ac1bb324837c5177c07ee06af115e8cb4218 100644 (file)
@@ -28,6 +28,7 @@ typedef enum {
        CMD_ADD_PAYLOAD,
        CMD_ADD_STAGE,
        CMD_CREATE,
+       CMD_LOCATE,
        CMD_PRINT
 } cmd_t;
 
@@ -78,8 +79,7 @@ static int cbfs_add(int argc, char **argv)
        if (argc > 6) {
                base = strtoul(argv[6], NULL, 0);
        }
-       cbfsfile =
-           create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
+       cbfsfile = create_cbfs_file(cbfsname, filedata, &filesize, type, &base);
        if (add_file_to_cbfs(cbfsfile, filesize, base))
                return 1;
        writerom(romname, rom, romsize);
@@ -203,6 +203,23 @@ static int cbfs_create(int argc, char **argv)
        return create_cbfs_image(romname, size, bootblock, align);
 }
 
+static int cbfs_locate(int argc, char **argv)
+{
+       char *romname = argv[1];
+       if (argc < 6) {
+               printf("not enough arguments to 'locate'.\n");
+               return 1;
+       }
+
+       const char *file = argv[3];
+       uint32_t filesize = getfilesize(file);
+       const char *filename = argv[4];
+       int align = strtoul(argv[5], NULL, 0);
+
+       printf("%x\n", cbfs_find_location(romname, filesize, filename, align));
+       return 0;
+}
+
 static int cbfs_print(int argc, char **argv)
 {
        char *romname = argv[1];
@@ -223,6 +240,7 @@ struct command commands[] = {
        {CMD_ADD_PAYLOAD, "add-payload", cbfs_add_payload},
        {CMD_ADD_STAGE, "add-stage", cbfs_add_stage},
        {CMD_CREATE, "create", cbfs_create},
+       {CMD_LOCATE, "locate", cbfs_locate},
        {CMD_PRINT, "print", cbfs_print}
 };
 
@@ -238,6 +256,7 @@ void usage(void)
             "add-payload FILE NAME [COMP] [base]  Add a payload to the ROM\n"
             "add-stage FILE NAME [COMP] [base]    Add a stage to the ROM\n"
             "create SIZE BSIZE BOOTBLOCK [ALIGN]  Create a ROM file\n"
+            "locate FILE NAME ALIGN               Find a place for a file of that size\n"
             "print                                Show the contents of the ROM\n");
 }
 
index 4c453995edf5b4c587c6756b9b4e87dae6edf600..1f69fa37f5d192c7b97489d230cc80b0b3f33ef2 100644 (file)
 
 #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 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
        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;
+}
index a41eb8a439e468c6235189025f10b64fda62e8e2..ede06e5340b26a041d1078046af639e63e27ecf8 100644 (file)
@@ -34,6 +34,7 @@ static uint32_t virt_to_phys(void *addr)
 
 #define ALIGN(val, by) (((val) + (by)-1)&~((by)-1))
 
+uint32_t getfilesize(const char *filename);
 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
               int place);
 void *loadrom(const char *filename);
@@ -62,4 +63,7 @@ int create_cbfs_image(const char *romfile, uint32_t romsize,
 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location);
 void print_cbfs_directory(const char *filename);
 
+uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
+                           const char *filename, uint32_t align);
+
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))