summaryrefslogtreecommitdiff
path: root/util/cbfstool/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/cbfstool/common.c')
-rw-r--r--util/cbfstool/common.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 4c453995ed..1f69fa37f5 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 @@ 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;
+}