layout: Kill the global layout

Change-Id: Ic302e9c5faf1368e5ca244ce461e55e14f916ab8
Signed-off-by: Nico Huber <nico.h@gmx.de>
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/54286
Original-Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72222
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/cli_classic.c b/cli_classic.c
index e0b5094..c222325 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -440,12 +440,12 @@
 	}
 	msg_gdbg("\n");
 
-	if (layoutfile && read_romlayout(layoutfile)) {
+	if (layoutfile && layout_from_file(&layout, layoutfile)) {
 		ret = 1;
 		goto out;
 	}
 
-	if (!ifd && !fmap && process_include_args(get_global_layout(), include_args)) {
+	if (!ifd && !fmap && process_include_args(layout, include_args)) {
 		ret = 1;
 		goto out;
 	}
@@ -615,9 +615,7 @@
 		goto out_shutdown;
 	}
 
-	if (layoutfile) {
-		layout = get_global_layout();
-	} else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
+	if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) ||
 			   process_include_args(layout, include_args))) {
 		ret = 1;
 		goto out_shutdown;
diff --git a/flash.h b/flash.h
index 9caf4e3..4045aca 100644
--- a/flash.h
+++ b/flash.h
@@ -488,7 +488,6 @@
 
 /* layout.c */
 int register_include_arg(struct layout_include_args **args, char *name);
-int read_romlayout(const char *name);
 void layout_cleanup(struct layout_include_args **args);
 
 /* spi.c */
diff --git a/layout.c b/layout.c
index 2c455e2..79965b3 100644
--- a/layout.c
+++ b/layout.c
@@ -33,15 +33,6 @@
 	struct layout_include_args *next;
 };
 
-static struct flashrom_layout *global_layout;
-
-struct flashrom_layout *get_global_layout(void)
-{
-	if (!global_layout)
-		flashrom_layout_new(&global_layout);
-	return global_layout;
-}
-
 const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx)
 {
 	return flashctx->default_layout;
@@ -62,13 +53,15 @@
 }
 
 #ifndef __LIBPAYLOAD__
-int read_romlayout(const char *name)
+int layout_from_file(struct flashrom_layout **layout, const char *name)
 {
-	struct flashrom_layout *const layout = get_global_layout();
 	FILE *romlayout;
 	char tempstr[256], tempname[256];
 	int ret = 1;
 
+	if (flashrom_layout_new(layout))
+		return 1;
+
 	romlayout = fopen(name, "r");
 
 	if (!romlayout) {
@@ -94,7 +87,7 @@
 			msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
 			goto _close_ret;
 		}
-		if (flashrom_layout_add_region(layout,
+		if (flashrom_layout_add_region(*layout,
 				strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname))
 			goto _close_ret;
 	}
@@ -164,7 +157,7 @@
 		return 0;
 
 	/* User has specified an area, but no layout file is loaded. */
-	if (!l->head) {
+	if (!l || !l->head) {
 		msg_gerr("Region requested (with -i \"%s\"), "
 			 "but no layout data is available.\n",
 			 args->name);
@@ -195,7 +188,6 @@
 
 void layout_cleanup(struct layout_include_args **args)
 {
-	struct flashrom_layout *const layout = get_global_layout();
 	struct layout_include_args *tmp;
 
 	while (*args) {
@@ -203,9 +195,6 @@
 		free(*args);
 		*args = tmp;
 	}
-
-	global_layout = NULL;
-	flashrom_layout_release(layout);
 }
 
 int layout_sanity_checks(const struct flashrom_flashctx *const flash)
@@ -359,9 +348,6 @@
  */
 void flashrom_layout_release(struct flashrom_layout *const layout)
 {
-	if (layout == global_layout)
-		return;
-
 	if (!layout)
 		return;
 
diff --git a/layout.h b/layout.h
index 2beb776..d58113f 100644
--- a/layout.h
+++ b/layout.h
@@ -49,10 +49,11 @@
 struct layout_include_args;
 
 struct flashrom_flashctx;
-struct flashrom_layout *get_global_layout(void);
 const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *);
 const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx);
 
+int layout_from_file(struct flashrom_layout **, const char *name);
+
 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(const struct flashrom_layout *, const struct romentry *);