layout: Implement flashprog_layout_get_region_range()

Similar to the function found in flashrom/master, implement a
libflashprog API to query the range of a layout region.

Change-Id: Idb3b1a4d24a34847102d66ee0e2cb4d93ae99eac
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72990
diff --git a/include/libflashprog.h b/include/libflashprog.h
index a2ea63c..e1d4d80 100644
--- a/include/libflashprog.h
+++ b/include/libflashprog.h
@@ -83,6 +83,7 @@
 		struct flashprog_flashctx *, const uint8_t *buf, size_t len);
 int flashprog_layout_add_region(struct flashprog_layout *, size_t start, size_t end, const char *name);
 int flashprog_layout_include_region(struct flashprog_layout *, const char *name);
+int flashprog_layout_get_region_range(const struct flashprog_layout *, const char *name, size_t *start, size_t *len);
 void flashprog_layout_release(struct flashprog_layout *);
 void flashprog_layout_set(struct flashprog_flashctx *, const struct flashprog_layout *);
 
diff --git a/layout.c b/layout.c
index 5ae8f95..ef4ebe9 100644
--- a/layout.c
+++ b/layout.c
@@ -348,6 +348,31 @@
 }
 
 /**
+ * @brief Get given region's offset and length.
+ *
+ * @param layout The layout to read from.
+ * @param name   The name of the region.
+ * @param start  The start address to be written.
+ * @param len    The length of the region to be written.
+ *
+ * @return 0 on success,
+ *         1 if the given name can't be found.
+ */
+int flashprog_layout_get_region_range(const struct flashprog_layout *const layout,
+				      const char *name, size_t *start, size_t *len)
+{
+	const struct romentry *entry = NULL;
+	while ((entry = layout_next(layout, entry))) {
+		if (!strcmp(entry->name, name)) {
+			*start = entry->start;
+			*len = entry->end - entry->start + 1;
+			return 0;
+		}
+	}
+	return 1;
+}
+
+/**
  * @brief Free a layout.
  *
  * @param layout Layout to free.
diff --git a/libflashprog.map b/libflashprog.map
index 9bf8ec0..a0dd125 100644
--- a/libflashprog.map
+++ b/libflashprog.map
@@ -11,6 +11,7 @@
     flashprog_image_write;
     flashprog_init;
     flashprog_layout_add_region;
+    flashprog_layout_get_region_range;
     flashprog_layout_include_region;
     flashprog_layout_new;
     flashprog_layout_read_fmap_from_buffer;