layout: Introduce flashrom_layout_add_region()

Adds a region to an existing layout, as long as there is space.

Change-Id: I50d473d0d5d1fb38bd6f9ae3d7127e9ea66a94e1
Signed-off-by: Nico Huber <nico.h@gmx.de>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/33517
Original-Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Original-Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Original-Reviewed-by: Peter Marheine <pmarheine@chromium.org>
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72213
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/libflashrom.c b/libflashrom.c
index b9c2d35..feba00a 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -382,6 +382,8 @@
 		struct flashctx *const flashctx, const struct fmap *const fmap)
 {
 	int i;
+	char name[FMAP_STRLEN + 1];
+	const struct fmap_area *area;
 	struct flashrom_layout *l = get_global_layout();
 
 	if (!fmap || !l)
@@ -392,21 +394,10 @@
 		return 1;
 	}
 
-	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;
-		l->entries[l->num_entries].name =
-			strndup((const char *)fmap->areas[i].name, FMAP_STRLEN);
-		if (!l->entries[l->num_entries].name) {
-			msg_gerr("Error adding layout entry: %s\n", strerror(errno));
+	for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
+		snprintf(name, sizeof(name), "%s", area->name);
+		if (flashrom_layout_add_region(l, area->offset, area->offset + area->size - 1, name))
 			return 1;
-		}
-		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++;
 	}
 
 	*layout = l;