Give layouts their own type

Introduce `struct flashrom_layout` and refactor layout.c a little, so
we can reuse the layout from there and have other sources of layouts
beside it.

I didn't want to clutter up flash.h any more. So things went into a new
layout.h.

Change-Id: Icea1a58c283131cc9c5fde6f16d783538dc1a4c7
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/17944
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: David Hendricks <david.hendricks@gmail.com>
Reviewed-by: Philippe Mathieu-Daudé <philippe.mathieu.daude@gmail.com>
diff --git a/layout.c b/layout.c
index f71eeaa..3d09011 100644
--- a/layout.c
+++ b/layout.c
@@ -25,25 +25,21 @@
 #include <limits.h>
 #include "flash.h"
 #include "programmer.h"
+#include "layout.h"
 
-#define MAX_ROMLAYOUT	32
-
-typedef struct {
-	chipoff_t start;
-	chipoff_t end;
-	unsigned int included;
-	char name[256];
-} romentry_t;
-
-/* rom_entries store the entries specified in a layout file and associated run-time data */
-static romentry_t rom_entries[MAX_ROMLAYOUT];
-static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */
+struct romentry entries[MAX_ROMLAYOUT];
+static struct flashrom_layout layout = { entries, 0 };
 
 /* include_args holds the arguments specified at the command line with -i. They must be processed at some point
- * so that desired regions are marked as "included" in the rom_entries list. */
+ * so that desired regions are marked as "included" in the layout. */
 static char *include_args[MAX_ROMLAYOUT];
 static int num_include_args = 0; /* the number of valid include_args. */
 
+const struct flashrom_layout *get_global_layout(void)
+{
+	return &layout;
+}
+
 #ifndef __LIBPAYLOAD__
 int read_romlayout(const char *name)
 {
@@ -62,13 +58,13 @@
 	while (!feof(romlayout)) {
 		char *tstr1, *tstr2;
 
-		if (num_rom_entries >= MAX_ROMLAYOUT) {
+		if (layout.num_entries >= MAX_ROMLAYOUT) {
 			msg_gerr("Maximum number of ROM images (%i) in layout "
 				 "file reached.\n", MAX_ROMLAYOUT);
 			(void)fclose(romlayout);
 			return 1;
 		}
-		if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, rom_entries[num_rom_entries].name))
+		if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name))
 			continue;
 #if 0
 		// fscanf does not like arbitrary comments like that :( later
@@ -83,16 +79,16 @@
 			(void)fclose(romlayout);
 			return 1;
 		}
-		rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
-		rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
-		rom_entries[num_rom_entries].included = 0;
-		num_rom_entries++;
+		layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16);
+		layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16);
+		layout.entries[layout.num_entries].included = 0;
+		layout.num_entries++;
 	}
 
-	for (i = 0; i < num_rom_entries; i++) {
+	for (i = 0; i < layout.num_entries; i++) {
 		msg_gdbg("romlayout %08x - %08x named %s\n",
-			     rom_entries[i].start,
-			     rom_entries[i].end, rom_entries[i].name);
+			     layout.entries[i].start,
+			     layout.entries[i].end, layout.entries[i].name);
 	}
 
 	(void)fclose(romlayout);
@@ -140,13 +136,13 @@
 {
 	int i;
 
-	if (num_rom_entries == 0)
+	if (layout.num_entries == 0)
 		return -1;
 
 	msg_gspew("Looking for region \"%s\"... ", name);
-	for (i = 0; i < num_rom_entries; i++) {
-		if (!strcmp(rom_entries[i].name, name)) {
-			rom_entries[i].included = 1;
+	for (i = 0; i < layout.num_entries; i++) {
+		if (!strcmp(layout.entries[i].name, name)) {
+			layout.entries[i].included = 1;
 			msg_gspew("found.\n");
 			return i;
 		}
@@ -167,7 +163,7 @@
 		return 0;
 
 	/* User has specified an area, but no layout file is loaded. */
-	if (num_rom_entries == 0) {
+	if (layout.num_entries == 0) {
 		msg_gerr("Region requested (with -i \"%s\"), "
 			 "but no layout data is available.\n",
 			 include_args[0]);
@@ -200,22 +196,22 @@
 	}
 	num_include_args = 0;
 
-	for (i = 0; i < num_rom_entries; i++) {
-		rom_entries[i].included = 0;
+	for (i = 0; i < layout.num_entries; i++) {
+		layout.entries[i].included = 0;
 	}
-	num_rom_entries = 0;
+	layout.num_entries = 0;
 }
 
-romentry_t *get_next_included_romentry(unsigned int start)
+struct romentry *get_next_included_romentry(unsigned int start)
 {
 	int i;
 	unsigned int best_start = UINT_MAX;
-	romentry_t *best_entry = NULL;
-	romentry_t *cur;
+	struct romentry *best_entry = NULL;
+	struct romentry *cur;
 
 	/* First come, first serve for overlapping regions. */
-	for (i = 0; i < num_rom_entries; i++) {
-		cur = &rom_entries[i];
+	for (i = 0; i < layout.num_entries; i++) {
+		cur = &layout.entries[i];
 		if (!cur->included)
 			continue;
 		/* Already past the current entry? */
@@ -240,16 +236,16 @@
 	int ret = 0;
 
 	int i;
-	for (i = 0; i < num_rom_entries; i++) {
-		if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) {
+	for (i = 0; i < layout.num_entries; i++) {
+		if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) {
 			msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
-				  "address space.\n", rom_entries[i].name);
-			if (rom_entries[i].included)
+				  "address space.\n", layout.entries[i].name);
+			if (layout.entries[i].included)
 				ret = 1;
 		}
-		if (rom_entries[i].start > rom_entries[i].end) {
+		if (layout.entries[i].start > layout.entries[i].end) {
 			msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
-				  rom_entries[i].name);
+				  layout.entries[i].name);
 			ret = 1;
 		}
 	}
@@ -281,7 +277,7 @@
 int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
 {
 	unsigned int start = 0;
-	romentry_t *entry;
+	struct romentry *entry;
 	unsigned int size = flash->chip->total_size * 1024;
 
 	/* If no regions were specified for inclusion, assume