David Hendricks has uploaded this change for review. ( https://review.coreboot.org/27479
Change subject: Forward-port Arthur's patch for latest upstream flashrom ......................................................................
Forward-port Arthur's patch for latest upstream flashrom
This should probably be squashed into Arthur's patch.
Change-Id: I0e0866cb836e6a3af2294d4bdcfefe38d65d0e6a Signed-off-by: David Hendricks dhendricks@fb.com --- M cli_classic.c M fmap.h M layout.c M libflashrom.c M libflashrom.h 5 files changed, 73 insertions(+), 52 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/79/27479/1
diff --git a/cli_classic.c b/cli_classic.c index b3a7478..4b1999f 100644 --- a/cli_classic.c +++ b/cli_classic.c @@ -420,12 +420,7 @@ goto out; }
- if (fmapfile && read_fmapfile(fmapfile)) { - ret = 1; - goto out; - } - - if (!ifd && process_include_args(get_global_layout())) { + if (!ifd && !fmapfile && process_include_args(get_global_layout())) { ret = 1; goto out; } @@ -584,6 +579,10 @@ process_include_args(layout))) { ret = 1; goto out_shutdown; + } else if (fmapfile && (flashrom_layout_read_from_fmap(&layout, fill_flash, fmapfile) || + process_include_args(layout))) { + ret = 1; + goto out_shutdown; }
flashrom_layout_set(fill_flash, layout); diff --git a/fmap.h b/fmap.h index 51655c2..ee4c16f 100644 --- a/fmap.h +++ b/fmap.h @@ -26,4 +26,3 @@ } __attribute__((packed));
long int fmap_find(const uint8_t *image, unsigned int image_len); -int read_fmapfile(const char *filename); diff --git a/layout.c b/layout.c index 4a093c7..5207bb3 100644 --- a/layout.c +++ b/layout.c @@ -102,50 +102,6 @@ return 0; }
-int read_fmapfile(const char *filename) -{ - int i; - FILE *fmapfile; - struct stat st; - - stat(filename, &st); - size_t filesize = st.st_size; - uint8_t *buff = malloc(filesize); - - fmapfile = fopen(filename, "r"); - - if (!fmapfile) { - msg_gerr("ERROR: Could not open fmap file (%s).\n", filename); - return -1; - } - - fread(buff, filesize, 1, fmapfile); - unsigned long int offset = fmap_find(buff, filesize); - if (offset < 0) { - msg_gerr("ERROR: Invalid fmap file (%s).\n", filename); - return -1; - } - - const struct fmap *fmap = (const struct fmap *)(buff + offset); - - for (i = 0; i < fmap->nareas; i++) { - layout.entries[layout.num_entries].start = fmap->areas[i].offset; - layout.entries[layout.num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1; - layout.entries[layout.num_entries].included = false; - memcpy(layout.entries[layout.num_entries].name, fmap->areas[i].name, - sizeof(layout.entries[i].name)); - msg_gdbg("fmap %08x - %08x named %s\n", - layout.entries[layout.num_entries].start, - layout.entries[layout.num_entries].end, - layout.entries[layout.num_entries].name); - layout.num_entries++; - } - - free(buff); - (void)fclose(fmapfile); - return 0; -} - #endif
/* returns the index of the entry (or a negative value if it is not found) */ diff --git a/libflashrom.c b/libflashrom.c index 34e881a..28e1999 100644 --- a/libflashrom.c +++ b/libflashrom.c @@ -23,8 +23,10 @@ #include <stdlib.h> #include <string.h> #include <stdarg.h> +#include <sys/stat.h>
#include "flash.h" +#include "fmap.h" #include "programmer.h" #include "layout.h" #include "hwaccess.h" @@ -383,6 +385,69 @@ }
/** + * @brief Read a layout from flashmap (fmap) from a file. + * + * @param[out] layout Points to a struct flashrom_layout pointer that + * gets set if the fmap is read and parsed successfully. + * @param[in] flashctx Flash context + * + * @return 0 on success, + * 3 if the fmap in file couldn't be parsed, + * 2 if the fmap in file couldn't be read, + * 1 on any other error. + */ +int flashrom_layout_read_from_fmap(struct flashrom_layout **const layout, struct flashctx *const flashctx, + const char *filename) +{ + int i; + FILE *fmapfile; + struct stat st; + + stat(filename, &st); + size_t filesize = st.st_size; + uint8_t *buff = malloc(filesize); + + fmapfile = fopen(filename, "r"); + + if (!fmapfile) { + msg_gerr("ERROR: Could not open fmap file (%s).\n", filename); + return 1; + } + + if (!fread(buff, filesize, 1, fmapfile)) { + msg_gerr("ERROR: Cannot read fmap file (%s).\n", filename); + return 1; + } + + unsigned long int offset = fmap_find(buff, filesize); + if (offset < 0) { + msg_gerr("ERROR: Invalid fmap file (%s).\n", filename); + return 2; + } + + const struct fmap *fmap = (const struct fmap *)(buff + offset); + + struct flashrom_layout *l = get_global_layout(); + for (i = 0; i < fmap->nareas; i++) { + l->entries[l->num_entries].start = fmap->areas[i].offset; + l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1; + l->entries[l->num_entries].included = false; + memcpy(l->entries[l->num_entries].name, fmap->areas[i].name, + sizeof(l->entries[i].name)); + msg_gdbg("fmap %08x - %08x named %s\n", + l->entries[l->num_entries].start, + l->entries[l->num_entries].end, + l->entries[l->num_entries].name); + l->num_entries++; + } + + free(buff); + (void)fclose(fmapfile); + *layout = l; + return 0; +} + +/** * @brief Free a layout. * * @param layout Layout to free. diff --git a/libflashrom.h b/libflashrom.h index 04907b2..2cf6d90 100644 --- a/libflashrom.h +++ b/libflashrom.h @@ -61,8 +61,10 @@
struct flashrom_layout; int flashrom_layout_read_from_ifd(struct flashrom_layout **, struct flashrom_flashctx *, const void *dump, size_t len); +int flashrom_layout_read_from_fmap(struct flashrom_layout **const layout, struct flashrom_flashctx *const flashctx, + const char *filename); int flashrom_layout_include_region(struct flashrom_layout *, const char *name); void flashrom_layout_release(struct flashrom_layout *); void flashrom_layout_set(struct flashrom_flashctx *, const struct flashrom_layout *);
-#endif /* !__LIBFLASHROM_H__ */ \ No newline at end of file +#endif /* !__LIBFLASHROM_H__ */