Naman Govil (namangov@gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/6441
-gerrit
commit a44c7fc774b7796170af3abbf8f2361c61a8b921 Author: Naman Govil namangov@gmail.com Date: Fri Aug 1 16:11:50 2014 +0530
Remove unnecessary mappings in cbfs_media
Reducing the sram usage while booting by removing unwanted map()s by creating a structure to replace it with appropriate read()s
Change-Id: Ibf045ffeb759c988f2c8b3fc749684d8da9899dc Signed-off-by: Naman Govil namangov@gmail.com --- src/include/cbfs_core.h | 9 +- src/lib/cbfs.c | 2 +- src/lib/cbfs_core.c | 167 +++++++++++++++------------------- src/lib/loaders/cbfs_payload_loader.c | 2 +- 4 files changed, 80 insertions(+), 100 deletions(-)
diff --git a/src/include/cbfs_core.h b/src/include/cbfs_core.h index 7c94be0..11de0ee 100644 --- a/src/include/cbfs_core.h +++ b/src/include/cbfs_core.h @@ -235,12 +235,17 @@ int cbfs_decompress(int algo, void *src, void *dst, int len); return 0 on success and <0 if header not found */ int cbfs_get_header(struct cbfs_media *media, struct cbfs_header *header);
-/* Returns success (0) on finding the file requested by verifying name/type; +/* Returns success (0) on finding the file requested by verifying name; -1 if file not found The absolute data_offset to the file is stored */ int cbfs_find_file(struct cbfs_media *media, struct cbfs_file_handle *f, - const char *name, int type); + const char *name);
+/* Returns success (0) on finding the file requested by verifying name and type; + -1 if file not found + The absolute data_offset to the file is stored */ +int cbfs_find_file_by_type(struct cbfs_media *media, struct cbfs_file_handle *f, + const char *name, int type);
/* returns pointer to file content inside CBFS after verifying if type is correct */ void *cbfs_get_file_content(struct cbfs_media *media, const char *name, diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c index e6c9afa..5442ddc 100644 --- a/src/lib/cbfs.c +++ b/src/lib/cbfs.c @@ -135,7 +135,7 @@ void * cbfs_load_stage(struct cbfs_media *media, const char *name) } }
- c = cbfs_find_file(media, &f, name, CBFS_TYPE_STAGE); + c = cbfs_find_file_by_type(media, &f, name, CBFS_TYPE_STAGE);
if (c < 0) { ERROR("Stage not loaded\n"); diff --git a/src/lib/cbfs_core.c b/src/lib/cbfs_core.c index d3f8efb..164473e 100644 --- a/src/lib/cbfs_core.c +++ b/src/lib/cbfs_core.c @@ -103,10 +103,10 @@ int cbfs_get_header(struct cbfs_media *media, struct cbfs_header *header)
/* public API starts here*/ /* This functions finds the absolute data_offset of a file searched for by - name/type. Returns 0 on success and -1 on failure + name. Returns 0 on success and -1 on failure */ int cbfs_find_file(struct cbfs_media *media, struct cbfs_file_handle *f, - const char *name, int type) + const char *name) { uint32_t offset, align, romsize,name_len; struct cbfs_media default_media; @@ -156,25 +156,22 @@ int cbfs_find_file(struct cbfs_media *media, struct cbfs_file_handle *f, continue; }
- if (f->file.type == type) { - - name_len = f->file.offset - sizeof(f->file); - DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,name_len); - file_name = (const char *)media->map(media, offset + sizeof(f->file), - name_len); - if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) { - ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); - } else if (strcmp(file_name, name) == 0) { - f->data_offset = offset + f->file.offset; - f->data_len = f->file.len; - media->unmap(media, file_name); - DEBUG("Found file:offset = 0x%x, len=%d\n", - f->data_offset, f->data_len); - return 0; - } else { - DEBUG("unmatched file offset = 0x%x : %s\n", offset, file_name); - media->unmap(media,file_name); - } + name_len = f->file.offset - sizeof(f->file); + DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,name_len); + file_name = (const char *)media->map(media, offset + sizeof(f->file), + name_len); + if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) { + ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); + } else if (strcmp(file_name, name) == 0) { + f->data_offset = offset + f->file.offset; + f->data_len = f->file.len; + media->unmap(media, file_name); + DEBUG("Found file:offset = 0x%x, len=%d\n", + f->data_offset, f->data_len); + return 0; + } else { + DEBUG("unmatched file offset = 0x%x : %s\n", offset, file_name); + media->unmap(media,file_name); } // Move to next file. offset += f->file.len + f->file.offset; @@ -188,13 +185,45 @@ int cbfs_find_file(struct cbfs_media *media, struct cbfs_file_handle *f, }
+/* This functions finds the absolute data_offset of a file searched for by + name and type using cbfs_find_file(). Returns 0 on success and -1 on failure + */ +int cbfs_find_file_by_type(struct cbfs_media *media, struct cbfs_file_handle *f, + const char *name, int type) +{ + struct cbfs_media default_media; + + if (media == CBFS_DEFAULT_MEDIA) { + media = &default_media; + if (init_default_cbfs_media(media) != 0) { + ERROR("Failed to initialize default media/\n"); + return -1; + } + } + + if (cbfs_find_file(media, f, name) < 0) { + ERROR("Failed to find file\n"); + return -1; + } + + if (f->file.type == type) { + DEBUG("File of matching type has been found\n"); + return 0; + } + else + { + ERROR("File of matching type not found\n"); + return -1; + } +} + + /*Returns pointer to file content inside CBFS after verifying type */ void *cbfs_get_file_content(struct cbfs_media *media, const char *name, int type, size_t *sz) { struct cbfs_file_handle f; - int c; struct cbfs_media default_media; void *content;
@@ -208,9 +237,8 @@ void *cbfs_get_file_content(struct cbfs_media *media, const char *name, return NULL; } } - c = cbfs_find_file(media, &f, name, type);
- if (c < 0) { + if (cbfs_find_file_by_type(media, &f, name, type) < 0) { ERROR("File not found\n"); return NULL; } @@ -219,91 +247,38 @@ void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
if (sz) *sz = f.data_len; + content = media->map(media, f.data_offset, f.data_len); + if (content == CBFS_MEDIA_INVALID_MAP_ADDRESS) return NULL; else return content; }
+/* returns pointer to file entry inside the CBFS or NULL +*/ struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name) { - const char *file_name; - uint32_t offset, align, romsize, name_len; - struct cbfs_header header; - struct cbfs_file file, *file_ptr; - struct cbfs_media default_media; - - if (media == CBFS_DEFAULT_MEDIA) { - media = &default_media; - if (init_default_cbfs_media(media) != 0) { - ERROR("Failed to initialize default media.\n"); - return NULL; - } + struct cbfs_file_handle f; + struct cbfs_media default_media; + struct cbfs_file *fileptr; + + if (media == CBFS_DEFAULT_MEDIA) { + media = &default_media; + if (init_default_cbfs_media(media) != 0) { + ERROR("Failed to initialize media\n"); + return NULL; + } }
- if (cbfs_get_header(media, &header)) + if (cbfs_find_file(media, &f, name) < 0) { + ERROR("Failed to find file\n"); return NULL; + }
- // Logical offset (for source media) of first file. - offset = header.offset; - align = header.align; - romsize = header.romsize; - - // TODO Add a "size" in CBFS header for a platform independent way to - // determine the end of CBFS data. -#if defined(CONFIG_ARCH_X86) && CONFIG_ARCH_X86 - romsize -= header.bootblocksize; -#endif - DEBUG("CBFS location: 0x%x~0x%x, align: %d\n", offset, romsize, align); - - DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset); - media->open(media); - while (offset < romsize && - media->read(media, &file, offset, sizeof(file)) == sizeof(file)) { - if (memcmp(CBFS_FILE_MAGIC, file.magic, - sizeof(file.magic)) != 0) { - uint32_t new_align = align; - if (offset % align) - new_align += align - (offset % align); - ERROR("ERROR: No file header found at 0x%x - " - "try next aligned address: 0x%x.\n", offset, - offset + new_align); - offset += new_align; - continue; - } - name_len = ntohl(file.offset) - sizeof(file); - DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset, - name_len); - - // load file name (arbitrary length). - file_name = (const char *)media->map( - media, offset + sizeof(file), name_len); - if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) { - ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); - } else if (strcmp(file_name, name) == 0) { - int file_offset = ntohl(file.offset), - file_len = ntohl(file.len); - DEBUG("Found file (offset=0x%x, len=%d).\n", - offset + file_offset, file_len); - media->unmap(media, file_name); - file_ptr = media->map(media, offset, - file_offset + file_len); - media->close(media); - return file_ptr; - } else { - DEBUG(" (unmatched file @0x%x: %s)\n", offset, - file_name); - media->unmap(media, file_name); - } - // Move to next file. - offset += ntohl(file.len) + ntohl(file.offset); - if (offset % align) - offset += align - (offset % align); - } - media->close(media); - LOG("WARNING: '%s' not found.\n", name); - return NULL; + fileptr = media->map(media, f.data_offset, f.data_len); + return fileptr; }
int cbfs_decompress(int algo, void *src, void *dst, int len) diff --git a/src/lib/loaders/cbfs_payload_loader.c b/src/lib/loaders/cbfs_payload_loader.c index f0b5bbc..d1ebd84 100644 --- a/src/lib/loaders/cbfs_payload_loader.c +++ b/src/lib/loaders/cbfs_payload_loader.c @@ -31,7 +31,7 @@ static int cbfs_locate_payload(struct payload *payload) if (init_default_cbfs_media(m) != 0) return -1;
- if (cbfs_find_file(m, &payload->f, payload->name, type) < 0) + if (cbfs_find_file_by_type(m, &payload->f, payload->name, type) < 0) return -1;
payload->media = CBFS_DEFAULT_MEDIA;