1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-27 15:12:36 +02:00

layout: Introduce layout_next()

Also, a `layout.c` internal version mutable_layout_next() that
allows to modify layout entries and a shorthand to look up an
entry by name, _layout_entry_by_name().

Use the new functions where applicable and the code is not
dropped later in this train, and also to compare the layouts
in flashrom_layout_read_from_ifd() in depth.

Change-Id: I284958471c61344d29d92c95d88475065a9ca9aa
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/33542
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-by: Peter Marheine <pmarheine@chromium.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Nico Huber 2019-06-16 19:28:35 +02:00
parent 4abb62c4ac
commit 06a89d7139
3 changed files with 88 additions and 66 deletions

144
layout.c
View File

@ -41,6 +41,32 @@ const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const f
return &flashctx->fallback_layout.base; return &flashctx->fallback_layout.base;
} }
static struct romentry *mutable_layout_next(
const struct flashrom_layout *const layout, struct romentry *iterator)
{
const struct romentry *const end = layout->entries + layout->num_entries;
if (iterator)
++iterator;
else
iterator = &layout->entries[0];
if (iterator < end)
return iterator;
return NULL;
}
static struct romentry *_layout_entry_by_name(
const struct flashrom_layout *const layout, const char *name)
{
struct romentry *entry = NULL;
while ((entry = mutable_layout_next(layout, entry))) {
if (!strcmp(entry->name, name))
return entry;
}
return NULL;
}
#ifndef __LIBPAYLOAD__ #ifndef __LIBPAYLOAD__
int read_romlayout(const char *name) int read_romlayout(const char *name)
{ {
@ -157,14 +183,12 @@ error:
static int include_region(struct flashrom_layout *const l, const char *name, static int include_region(struct flashrom_layout *const l, const char *name,
const char *file) const char *file)
{ {
size_t i; struct romentry *const entry = _layout_entry_by_name(l, name);
for (i = 0; i < l->num_entries; ++i) { if (entry) {
if (!strcmp(l->entries[i].name, name)) { entry->included = true;
l->entries[i].included = true; if (file)
if (file) entry->file = strdup(file);
l->entries[i].file = strdup(file); return 0;
return 0;
}
} }
return 1; return 1;
} }
@ -187,13 +211,11 @@ static int find_romentry(struct flashrom_layout *const l, char *name, char *file
int get_region_range(struct flashrom_layout *const l, const char *name, int get_region_range(struct flashrom_layout *const l, const char *name,
unsigned int *start, unsigned int *len) unsigned int *start, unsigned int *len)
{ {
size_t i; const struct romentry *const entry = _layout_entry_by_name(l, name);
for (i = 0; i < l->num_entries; ++i) { if (entry) {
if (!strcmp(l->entries[i].name, name)) { *start = entry->start;
*start = l->entries[i].start; *len = entry->end - entry->start + 1;
*len = l->entries[i].end - l->entries[i].start + 1; return 0;
return 0;
}
} }
return 1; return 1;
} }
@ -246,30 +268,26 @@ int process_include_args(struct flashrom_layout *l, const struct layout_include_
/* returns boolean 1 if any regions overlap, 0 otherwise */ /* returns boolean 1 if any regions overlap, 0 otherwise */
int included_regions_overlap(const struct flashrom_layout *const l) int included_regions_overlap(const struct flashrom_layout *const l)
{ {
size_t i; const struct romentry *lhs = NULL;
int overlap_detected = 0; int overlap_detected = 0;
for (i = 0; i < l->num_entries; i++) { while ((lhs = layout_next(l, lhs))) {
size_t j; if (!lhs->included)
if (!l->entries[i].included)
continue; continue;
for (j = i + 1; j < l->num_entries; j++) { const struct romentry *rhs = lhs;
if (!l->entries[j].included) while ((rhs = layout_next(l, rhs))) {
if (rhs->included)
continue; continue;
if (l->entries[i].start > l->entries[j].end) if (lhs->start > rhs->end)
continue; continue;
if (l->entries[i].end < l->entries[j].start) if (lhs->end < rhs->start)
continue; continue;
msg_gdbg("Regions %s [0x%08x-0x%08x] and " msg_gdbg("Regions %s [0x%08x-0x%08x] and %s [0x%08x-0x%08x] overlap\n",
"%s [0x%08x-0x%08x] overlap\n", lhs->name, lhs->start, lhs->end, rhs->name, rhs->start, rhs->end);
l->entries[i].name, l->entries[i].start,
l->entries[i].end, l->entries[j].name,
l->entries[j].start, l->entries[j].end);
overlap_detected = 1; overlap_detected = 1;
} }
} }
@ -305,17 +323,17 @@ int normalize_romentries(const struct flashctx *flash)
chipsize_t total_size = flash->chip->total_size * 1024; chipsize_t total_size = flash->chip->total_size * 1024;
int ret = 0; int ret = 0;
unsigned int i; const struct romentry *entry = NULL;
for (i = 0; i < layout->num_entries; i++) { while ((entry = layout_next(layout, entry))) {
if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) { if (entry->start >= total_size || entry->end >= total_size) {
msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's " msg_gwarn("Warning: Address range of region \"%s\" "
"address space.\n", layout->entries[i].name); "exceeds the current chip's address space.\n", entry->name);
if (layout->entries[i].included) if (entry->included)
ret = 1; ret = 1;
} }
if (layout->entries[i].start > layout->entries[i].end) { if (entry->start > entry->end) {
msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
layout->entries[i].name); entry->name);
ret = 1; ret = 1;
} }
} }
@ -326,17 +344,18 @@ int normalize_romentries(const struct flashctx *flash)
void prepare_layout_for_extraction(struct flashctx *flash) void prepare_layout_for_extraction(struct flashctx *flash)
{ {
const struct flashrom_layout *const l = get_layout(flash); const struct flashrom_layout *const l = get_layout(flash);
unsigned int i, j; struct romentry *entry = NULL;
unsigned int i;
for (i = 0; i < l->num_entries; ++i) { while ((entry = mutable_layout_next(l, entry))) {
l->entries[i].included = true; entry->included = true;
if (!l->entries[i].file) if (!entry->file)
l->entries[i].file = strdup(l->entries[i].name); entry->file = strdup(entry->name);
for (j = 0; l->entries[i].file[j]; j++) { for (i = 0; entry->file[i]; ++i) {
if (isspace(l->entries[i].file[j])) if (isspace(entry->file[i]))
l->entries[i].file[j] = '_'; entry->file[i] = '_';
} }
} }
} }
@ -344,16 +363,15 @@ void prepare_layout_for_extraction(struct flashctx *flash)
const struct romentry *layout_next_included_region( const struct romentry *layout_next_included_region(
const struct flashrom_layout *const l, const chipoff_t where) const struct flashrom_layout *const l, const chipoff_t where)
{ {
unsigned int i; const struct romentry *entry = NULL, *lowest = NULL;
const struct romentry *lowest = NULL;
for (i = 0; i < l->num_entries; ++i) { while ((entry = layout_next(l, entry))) {
if (!l->entries[i].included) if (!entry->included)
continue; continue;
if (l->entries[i].end < where) if (entry->end < where)
continue; continue;
if (!lowest || lowest->start > l->entries[i].start) if (!lowest || lowest->start > entry->start)
lowest = &l->entries[i]; lowest = entry;
} }
return lowest; return lowest;
@ -362,19 +380,17 @@ const struct romentry *layout_next_included_region(
const struct romentry *layout_next_included( const struct romentry *layout_next_included(
const struct flashrom_layout *const layout, const struct romentry *iterator) const struct flashrom_layout *const layout, const struct romentry *iterator)
{ {
const struct romentry *const end = layout->entries + layout->num_entries; while ((iterator = layout_next(layout, iterator))) {
if (iterator->included)
if (iterator) break;
++iterator;
else
iterator = &layout->entries[0];
for (; iterator < end; ++iterator) {
if (!iterator->included)
continue;
return iterator;
} }
return NULL; return iterator;
}
const struct romentry *layout_next(
const struct flashrom_layout *const layout, const struct romentry *iterator)
{
return mutable_layout_next(layout, (struct romentry *)iterator);
} }
/** /**

View File

@ -70,6 +70,7 @@ int get_region_range(struct flashrom_layout *const l, const char *name,
int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args); int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args);
const struct romentry *layout_next_included_region(const struct flashrom_layout *, chipoff_t); const struct romentry *layout_next_included_region(const struct flashrom_layout *, chipoff_t);
const struct romentry *layout_next_included(const struct flashrom_layout *, const struct romentry *); const struct romentry *layout_next_included(const struct flashrom_layout *, const struct romentry *);
const struct romentry *layout_next(const struct flashrom_layout *, const struct romentry *);
int included_regions_overlap(const struct flashrom_layout *const flashrom_layout); int included_regions_overlap(const struct flashrom_layout *const flashrom_layout);
void prepare_layout_for_extraction(struct flashrom_flashctx *flash); void prepare_layout_for_extraction(struct flashrom_flashctx *flash);

View File

@ -470,8 +470,13 @@ int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct
goto _finalize_ret; goto _finalize_ret;
} }
if (chip_layout->base.num_entries != dump_layout.base.num_entries || const struct romentry *chip_entry = layout_next(&chip_layout->base, NULL);
memcmp(chip_layout->entries, dump_layout.entries, sizeof(dump_layout.entries))) { const struct romentry *dump_entry = layout_next(&dump_layout.base, NULL);
while (chip_entry && dump_entry && !memcmp(chip_entry, dump_entry, sizeof(*chip_entry))) {
chip_entry = layout_next(&chip_layout->base, chip_entry);
dump_entry = layout_next(&dump_layout.base, dump_entry);
}
if (chip_entry || dump_entry) {
msg_cerr("Descriptors don't match!\n"); msg_cerr("Descriptors don't match!\n");
ret = 5; ret = 5;
goto _finalize_ret; goto _finalize_ret;