diff mbox series

[v2,09/13] cbfs: Record the start address in cbfs_priv

Message ID 20200522025909.249740-9-sjg@chromium.org
State Superseded
Headers show
Series x86: cbfs: Various clean-ups to CBFS implementation | expand

Commit Message

Simon Glass May 22, 2020, 2:59 a.m. UTC
The start address of the CBFS is used when scanning for files. It makes
sense to put this in our cbfs_priv struct and calculate it when we read
the header.

Update the code accordingly.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v2: None

 fs/cbfs/cbfs.c | 44 +++++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/fs/cbfs/cbfs.c b/fs/cbfs/cbfs.c
index 689f574cf12..abdc2731a14 100644
--- a/fs/cbfs/cbfs.c
+++ b/fs/cbfs/cbfs.c
@@ -14,8 +14,18 @@ 
 static const u32 good_magic = 0x4f524243;
 static const u8 good_file_magic[] = "LARCHIVE";
 
+/**
+ * struct cbfs_priv - Private data for this driver
+ *
+ * @initialised: true if this CBFS has been inited
+ * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
+ * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
+ * @file_cache: List of file headers read from CBFS
+ * @result: Success/error result
+ */
 struct cbfs_priv {
 	bool initialised;
+	void *start;
 	struct cbfs_header header;
 	struct cbfs_cachenode *file_cache;
 	enum cbfs_result result;
@@ -132,12 +142,12 @@  static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
 }
 
 /* Look through a CBFS instance and copy file metadata into regular memory. */
-static int file_cbfs_fill_cache(struct cbfs_priv *priv, void *start, u32 size,
-				u32 align)
+static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
 {
 	struct cbfs_cachenode *cache_node;
 	struct cbfs_cachenode *new_node;
 	struct cbfs_cachenode **cache_tail = &priv->file_cache;
+	void *start;
 
 	/* Clear out old information. */
 	cache_node = priv->file_cache;
@@ -148,6 +158,7 @@  static int file_cbfs_fill_cache(struct cbfs_priv *priv, void *start, u32 size,
 	}
 	priv->file_cache = NULL;
 
+	start = priv->start;
 	while (size >= align) {
 		int used;
 		int ret;
@@ -213,8 +224,14 @@  static int load_header(struct cbfs_priv *priv, ulong addr)
 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
 {
 	int offset = *(u32 *)(end_of_rom - 3);
+	int ret;
+
+	ret = load_header(priv, end_of_rom + offset + 1);
+	if (ret)
+		return ret;
+	priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
 
-	return load_header(priv, end_of_rom + offset + 1);
+	return 0;
 }
 
 /**
@@ -226,20 +243,22 @@  static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
  */
 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
 {
-	return load_header(priv, base + MASTER_HDR_OFFSET);
+	int ret;
+
+	ret = load_header(priv, base + MASTER_HDR_OFFSET);
+	if (ret)
+		return ret;
+	priv->start = (void *)base;
+
+	return 0;
 }
 
 static void cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
 {
-	void *start_of_rom;
-
 	if (file_cbfs_load_header(priv, end_of_rom))
 		return;
 
-	start_of_rom = (void *)(end_of_rom + 1 - priv->header.rom_size);
-
-	file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
-			     priv->header.align);
+	file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align);
 	if (priv->result == CBFS_SUCCESS)
 		priv->initialised = true;
 }
@@ -262,8 +281,7 @@  int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
 	if (ret)
 		return ret;
 
-	file_cbfs_fill_cache(priv, (void *)base, priv->header.rom_size,
-			     priv->header.align);
+	file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align);
 	if (priv->result != CBFS_SUCCESS)
 		return -EINVAL;
 
@@ -358,7 +376,7 @@  const struct cbfs_cachenode *file_cbfs_find_uncached(ulong end_of_rom,
 	if (file_cbfs_load_header(priv, end_of_rom))
 		return NULL;
 
-	start = (void *)(end_of_rom + 1 - priv->header.rom_size);
+	start = priv->start;
 	size = priv->header.rom_size;
 	align = priv->header.align;