diff mbox series

[U-Boot,v2,5/6] cbfs: Add functions to support multiple CBFSs

Message ID 20190815015615.164307-6-sjg@chromium.org
State Accepted
Commit 630b2f39ddabd43a27a98f0356b948df18e97d88
Delegated to: Bin Meng
Headers show
Series cbfs: Allow use before relocation / BSS | expand

Commit Message

Simon Glass Aug. 15, 2019, 1:56 a.m. UTC
Sometimes an image has multiple CBFS. The current CBFS API is limited to
handling only one at time. Also it keeps track of the CBFS internally in
BSS, which does not work before relocation, for example.

Add a few new functions to overcome these limitations.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
---

Changes in v2: None

 fs/cbfs/cbfs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/cbfs.h | 22 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)

Comments

Bin Meng Aug. 16, 2019, 2:43 p.m. UTC | #1
On Thu, Aug 15, 2019 at 9:56 AM Simon Glass <sjg@chromium.org> wrote:
>
> Sometimes an image has multiple CBFS. The current CBFS API is limited to
> handling only one at time. Also it keeps track of the CBFS internally in
> BSS, which does not work before relocation, for example.
>
> Add a few new functions to overcome these limitations.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
> ---
>
> Changes in v2: None
>
>  fs/cbfs/cbfs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  include/cbfs.h | 22 ++++++++++++++++++++++
>  2 files changed, 68 insertions(+)
>

Tested-by: Bin Meng <bmeng.cn@gmail.com>
Bin Meng Aug. 18, 2019, 1:59 p.m. UTC | #2
On Fri, Aug 16, 2019 at 10:43 PM Bin Meng <bmeng.cn@gmail.com> wrote:
>
> On Thu, Aug 15, 2019 at 9:56 AM Simon Glass <sjg@chromium.org> wrote:
> >
> > Sometimes an image has multiple CBFS. The current CBFS API is limited to
> > handling only one at time. Also it keeps track of the CBFS internally in
> > BSS, which does not work before relocation, for example.
> >
> > Add a few new functions to overcome these limitations.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
> > ---
> >
> > Changes in v2: None
> >
> >  fs/cbfs/cbfs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
> >  include/cbfs.h | 22 ++++++++++++++++++++++
> >  2 files changed, 68 insertions(+)
> >
>
> Tested-by: Bin Meng <bmeng.cn@gmail.com>

applied to u-boot-x86, thanks!
diff mbox series

Patch

diff --git a/fs/cbfs/cbfs.c b/fs/cbfs/cbfs.c
index d9a8562e5c..35f919afee 100644
--- a/fs/cbfs/cbfs.c
+++ b/fs/cbfs/cbfs.c
@@ -187,6 +187,23 @@  static int file_cbfs_load_header(uintptr_t end_of_rom,
 	return 0;
 }
 
+static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base,
+				struct cbfs_header *header)
+{
+	struct cbfs_header *header_in_rom;
+
+	header_in_rom = (struct cbfs_header *)base;
+	swap_header(header, header_in_rom);
+
+	if (header->magic != good_magic || header->offset >
+			header->rom_size - header->boot_block_size) {
+		priv->result = CBFS_BAD_HEADER;
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
 static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom)
 {
 	u8 *start_of_rom;
@@ -209,6 +226,35 @@  void file_cbfs_init(uintptr_t end_of_rom)
 	cbfs_init(&cbfs_s, end_of_rom);
 }
 
+int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
+{
+	struct cbfs_priv priv_s, *priv = &priv_s;
+	int ret;
+
+	/*
+	 * Use a local variable to start with until we know that the CBFS is
+	 * valid. Assume that a master header appears at the start, at offset
+	 * 0x38.
+	 */
+	ret = cbfs_load_header_ptr(priv, base + 0x38, &priv->header);
+	if (ret)
+		return ret;
+
+	file_cbfs_fill_cache(priv, (u8 *)base, priv->header.rom_size,
+			     priv->header.align);
+	if (priv->result != CBFS_SUCCESS)
+		return -EINVAL;
+
+	priv->initialized = 1;
+	priv = malloc(sizeof(priv_s));
+	if (!priv)
+		return -ENOMEM;
+	memcpy(priv, &priv_s, sizeof(priv_s));
+	*privp = priv;
+
+	return 0;
+}
+
 const struct cbfs_header *file_cbfs_get_header(void)
 {
 	struct cbfs_priv *priv = &cbfs_s;
diff --git a/include/cbfs.h b/include/cbfs.h
index 742e34e24f..6d4c4d4b06 100644
--- a/include/cbfs.h
+++ b/include/cbfs.h
@@ -135,6 +135,28 @@  void file_cbfs_get_next(const struct cbfs_cachenode **file);
  */
 const struct cbfs_cachenode *file_cbfs_find(const char *name);
 
+struct cbfs_priv *priv;
+
+/**
+ * cbfs_find_file() - Find a file in a given CBFS
+ *
+ * @cbfs: CBFS to look in (use cbfs_init_mem() to set it up)
+ * @name: Filename to look for
+ * @return pointer to CBFS node if found, else NULL
+ */
+const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *cbfs,
+					    const char *name);
+
+/**
+ * cbfs_init_mem() - Set up a new CBFS
+ *
+ * @base: Base address of CBFS
+ * @size: Size of CBFS in bytes
+ * @cbfsp: Returns a pointer to CBFS on success
+ * @return 0 if OK, -ve on error
+ */
+int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp);
+
 
 /***************************************************************************/
 /* All of the functions below can be used without first initializing CBFS. */