diff options
author | Bill Richardson <wfrichar@chromium.org> | 2014-08-27 15:42:24 -0700 |
---|---|---|
committer | chrome-internal-fetch <chrome-internal-fetch@google.com> | 2014-08-29 21:56:17 +0000 |
commit | c0777be638d0cb69caa4e6a8d1573c78435f1865 (patch) | |
tree | 11f39f3e11b7847636204637b77010820eb1a2df /host | |
parent | f16ed878799cc7cf11545c36d6f332c76d612260 (diff) | |
download | vboot_reference-c0777be638d0cb69caa4e6a8d1573c78435f1865.tar.gz |
cleanup: add some easier-to-use FMAP parsing functions.
The functions that look for the FMAP and its entries should return more
useful values.
BUG=none
BRANCH=ToT
TEST=make runtests
No functional changes.
Change-Id: I4b62ea0de972bceb3d58f4ee8eb82ad065ddcbae
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/214630
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'host')
-rw-r--r-- | host/lib/fmap.c | 54 | ||||
-rw-r--r-- | host/lib/include/fmap.h | 21 |
2 files changed, 45 insertions, 30 deletions
diff --git a/host/lib/fmap.c b/host/lib/fmap.c index 014c97b2..f9b0e5ed 100644 --- a/host/lib/fmap.c +++ b/host/lib/fmap.c @@ -9,27 +9,41 @@ #include "fmap.h" -const char* FmapFind(const char* ptr, size_t size) +/* Find and point to the FMAP header within the buffer */ +FmapHeader *fmap_find(uint8_t *ptr, size_t size) { - size_t i; - FmapHeader *fmap_header; - for (i=0; i<size; i += FMAP_SEARCH_STRIDE, ptr += FMAP_SEARCH_STRIDE) { - if (0 != strncmp(ptr, FMAP_SIGNATURE, FMAP_SIGNATURE_SIZE)) - continue; - // Image may have multiple signatures (ex, in code that handles FMAP itself) - // so we do want to check at least major version. - fmap_header = (FmapHeader *)ptr; - if (fmap_header->fmap_ver_major == FMAP_VER_MAJOR) - return ptr; - } - return NULL; + size_t i; + FmapHeader *fmap_header; + for (i=0; i<size; i += FMAP_SEARCH_STRIDE, ptr += FMAP_SEARCH_STRIDE) { + if (0 != memcmp(ptr, FMAP_SIGNATURE, FMAP_SIGNATURE_SIZE)) + continue; + fmap_header = (FmapHeader *)ptr; + if (fmap_header->fmap_ver_major == FMAP_VER_MAJOR) + return fmap_header; + } + return NULL; } -int FmapAreaIndex(const FmapHeader* fh, const FmapAreaHeader* ah, - const char* name) { - int i; - for (i = 0; i < fh->fmap_nareas; i++) - if (!strcmp((const char*) ah[i].area_name, name)) - return i; - return -1; +/* Search for an area by name, return pointer to its beginning */ +uint8_t *fmap_find_by_name(uint8_t *ptr, size_t size, FmapHeader *fmap, + const char *name, FmapAreaHeader **ah_ptr) +{ + int i; + FmapAreaHeader *ah; + + if (!fmap) + fmap = fmap_find(ptr, size); + if (!fmap) + return NULL; + + ah = (FmapAreaHeader*)((void *)fmap + sizeof(FmapHeader)); + for (i = 0; i < fmap->fmap_nareas; i++) + if (!strncmp(ah[i].area_name, name, FMAP_NAMELEN)) { + if (ah_ptr) + *ah_ptr = ah + i; + return ptr + ah[i].area_offset; + } + + return NULL; } + diff --git a/host/lib/include/fmap.h b/host/lib/include/fmap.h index 92d74fd3..7e627050 100644 --- a/host/lib/include/fmap.h +++ b/host/lib/include/fmap.h @@ -34,15 +34,16 @@ typedef struct _FmapAreaHeader { } __attribute__((packed)) FmapAreaHeader; -/* Scan firmware image, pointed by [ptr] with length [size], for fmap header. - * Return pointer to fmap header, or NULL if not found. - */ -const char* FmapFind(const char *ptr, size_t size); - -/* Look up fmap area by name, that is, strcmp(fh->fmap_name, name) == 0. - * Return index of fmap area, that is, ah[returned_index], - * or -1 if not found. */ -int FmapAreaIndex(const FmapHeader* fh, const FmapAreaHeader* ah, - const char* name); +/* Find and point to the FMAP header within the buffer */ +FmapHeader *fmap_find(uint8_t *ptr, size_t size); + +/* Search for an area by name, return pointer to its beginning */ +uint8_t *fmap_find_by_name(uint8_t *ptr, size_t size, + /* optional, will call fmap_find() if NULL */ + FmapHeader *fmap, + /* The area name to search for */ + const char *name, + /* optional, return pointer to entry if not NULL */ + FmapAreaHeader **ah); #endif /* __FMAP_H__ */ |