All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/15] image: A partial series for the image clean-up
@ 2021-09-25 13:03 Simon Glass
  2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
                   ` (14 more replies)
  0 siblings, 15 replies; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut, T Karthik Reddy

Now that the competing image clean-up has landed and the smoke has
cleared, we can come back to this one.

This series includes about a third of the original patches, mostly the
ones that apply cleanly.

It does not reduce the #idefs by as much, but it does make some small
code improvements.

Further patches can be pulled in once these ones have been reviewed and
land.

Build here:

https://source.denx.de/u-boot/custodians/u-boot-dm/-/pipelines/9251

Changes in v4:
- Rebase to master

Changes in v3:
- Pick up only the first part of the original v2 series

Changes in v2:
- Add a patch to introduce a memdup() function
- Add new abuf_init_set() function
- Update abuf_realloc() to return after every case
- Use const for abuf_data() and abuf_size()
- Make use of memdup()
- Add abuf_init_move()
- Add comments about the assumptions made by lib_test_abuf_realloc()
- Add better comments about why some tests are skipped at present
- Correct 'hose' typo

Simon Glass (15):
  lib: Add memdup()
  Add support for an owned buffer
  compiler: Add a comment to host_build()
  zstd: Create a function for use from U-Boot
  btrfs: Use U-Boot API for decompression
  image: Avoid switch default in image_decomp()
  image: Update zstd to avoid reporting error twice
  gzip: Avoid use of u64
  image: Update image_decomp() to avoid ifdefs
  image: Split board code out into its own file
  image: Fix up checkpatch warnings in image-board.c
  image: Split host code out into its own file
  image: Create a function to do manual relocation
  image: Avoid #ifdefs for manual relocation
  image: Remove ifdefs around image_setup_linux() el at

 common/Makefile           |    2 +-
 common/bootm.c            |   16 +-
 common/image-board.c      |  939 +++++++++++++++++++++++++++++
 common/image-host.c       |   27 +
 common/image-sig.c        |   40 +-
 common/image.c            | 1177 +++----------------------------------
 configs/sandbox_defconfig |    1 +
 fs/btrfs/compression.c    |   51 +-
 include/abuf.h            |  159 +++++
 include/compiler.h        |    8 +
 include/gzip.h            |    8 +-
 include/linux/string.h    |   13 +
 include/linux/zstd.h      |   11 +
 include/relocate.h        |   30 +-
 lib/Makefile              |    1 +
 lib/abuf.c                |  109 ++++
 lib/gunzip.c              |   28 +-
 lib/string.c              |   13 +
 lib/zstd/Makefile         |    2 +-
 lib/zstd/zstd.c           |   64 ++
 test/lib/Makefile         |    1 +
 test/lib/abuf.c           |  344 +++++++++++
 test/lib/string.c         |   32 +
 tools/Makefile            |    1 +
 24 files changed, 1887 insertions(+), 1190 deletions(-)
 create mode 100644 common/image-board.c
 create mode 100644 common/image-host.c
 create mode 100644 include/abuf.h
 create mode 100644 lib/abuf.c
 create mode 100644 lib/zstd/zstd.c
 create mode 100644 test/lib/abuf.c

-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH v4 01/15] lib: Add memdup()
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 02/15] Add support for an owned buffer Simon Glass
                   ` (13 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini, Simon Glass

Add a function to duplicate a memory region, a little like strdup().

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

(no changes since v2)

Changes in v2:
- Add a patch to introduce a memdup() function

 include/linux/string.h | 13 +++++++++++++
 lib/string.c           | 13 +++++++++++++
 test/lib/string.c      | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index dd255f21633..3169c93796e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -129,6 +129,19 @@ extern void * memchr(const void *,int,__kernel_size_t);
 void *memchr_inv(const void *, int, size_t);
 #endif
 
+/**
+ * memdup() - allocate a buffer and copy in the contents
+ *
+ * Note that this returns a valid pointer even if @len is 0
+ *
+ * @src: data to copy in
+ * @len: number of bytes to copy
+ * @return allocated buffer with the copied contents, or NULL if not enough
+ *	memory is available
+ *
+ */
+char *memdup(const void *src, size_t len);
+
 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base);
 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base);
 
diff --git a/lib/string.c b/lib/string.c
index ba176fb08f7..78bd65c4136 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -659,6 +659,19 @@ void * memscan(void * addr, int c, size_t size)
 }
 #endif
 
+char *memdup(const void *src, size_t len)
+{
+	char *p;
+
+	p = malloc(len);
+	if (!p)
+		return NULL;
+
+	memcpy(p, src, len);
+
+	return p;
+}
+
 #ifndef __HAVE_ARCH_STRSTR
 /**
  * strstr - Find the first substring in a %NUL terminated string
diff --git a/test/lib/string.c b/test/lib/string.c
index 64234bef36c..5dcf4d6db00 100644
--- a/test/lib/string.c
+++ b/test/lib/string.c
@@ -23,6 +23,8 @@
 /* Allow for copying up to 32 bytes */
 #define BUFLEN (SWEEP + 33)
 
+#define TEST_STR	"hello"
+
 /**
  * init_buffer() - initialize buffer
  *
@@ -193,3 +195,33 @@ static int lib_memmove(struct unit_test_state *uts)
 }
 
 LIB_TEST(lib_memmove, 0);
+
+/** lib_memdup() - unit test for memdup() */
+static int lib_memdup(struct unit_test_state *uts)
+{
+	char buf[BUFLEN];
+	size_t len;
+	char *p, *q;
+
+	/* Zero size should do nothing */
+	p = memdup(NULL, 0);
+	ut_assertnonnull(p);
+	free(p);
+
+	p = memdup(buf, 0);
+	ut_assertnonnull(p);
+	free(p);
+
+	strcpy(buf, TEST_STR);
+	len = sizeof(TEST_STR);
+	p = memdup(buf, len);
+	ut_asserteq_mem(p, buf, len);
+
+	q = memdup(p, len);
+	ut_asserteq_mem(q, buf, len);
+	free(q);
+	free(p);
+
+	return 0;
+}
+LIB_TEST(lib_memdup, 0);
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 02/15] Add support for an owned buffer
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
  2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 03/15] compiler: Add a comment to host_build() Simon Glass
                   ` (12 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini, Simon Glass

When passing a data buffer back from a function, it is not always clear
who owns the buffer, i.e. who is responsible for freeing the memory used.
An example of this is where multiple files are decompressed from the
firmware image, using a temporary buffer for reading (since the
compressed data has to live somewhere) and producing a temporary or
permanent buffer with the resuilts.

Where the firmware image can be memory-mapped, as on x86, the compressed
data does not need to be buffered, but the complexity of having a buffer
which is either allocated or not, makes the code hard to understand.

Introduce a new 'abuf' which supports simple buffer operations:

- encapsulating a buffer and its size
- either allocated with malloc() or not
- able to be reliably freed if necessary
- able to be converted to an allocated buffer if needed

This simple API makes it easier to deal with allocated and memory-mapped
buffers.

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

(no changes since v2)

Changes in v2:
- Add new abuf_init_set() function
- Update abuf_realloc() to return after every case
- Use const for abuf_data() and abuf_size()
- Make use of memdup()
- Add abuf_init_move()
- Add comments about the assumptions made by lib_test_abuf_realloc()
- Add better comments about why some tests are skipped at present

 include/abuf.h    | 159 +++++++++++++++++++++
 lib/Makefile      |   1 +
 lib/abuf.c        | 109 +++++++++++++++
 test/lib/Makefile |   1 +
 test/lib/abuf.c   | 344 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 614 insertions(+)
 create mode 100644 include/abuf.h
 create mode 100644 lib/abuf.c
 create mode 100644 test/lib/abuf.c

diff --git a/include/abuf.h b/include/abuf.h
new file mode 100644
index 00000000000..d230f72806d
--- /dev/null
+++ b/include/abuf.h
@@ -0,0 +1,159 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Handles a buffer that can be allocated and freed
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#ifndef __ABUF_H
+#define __ABUF_H
+
+#include <linux/types.h>
+
+/**
+ * struct abuf - buffer that can be allocated and freed
+ *
+ * This is useful for a block of data which may be allocated with malloc(), or
+ * not, so that it needs to be freed correctly when finished with.
+ *
+ * For now it has a very simple purpose.
+ *
+ * Using memset() to zero all fields is guaranteed to be equivalent to
+ * abuf_init().
+ *
+ * @data: Pointer to data
+ * @size: Size of data in bytes
+ * @alloced: true if allocated with malloc(), so must be freed after use
+ */
+struct abuf {
+	void *data;
+	size_t size;
+	bool alloced;
+};
+
+static inline void *abuf_data(const struct abuf *abuf)
+{
+	return abuf->data;
+}
+
+static inline size_t abuf_size(const struct abuf *abuf)
+{
+	return abuf->size;
+}
+
+/**
+ * abuf_set() - set the (unallocated) data in a buffer
+ *
+ * This simply makes the abuf point to the supplied data, which must be live
+ * for the lifetime of the abuf. It is not alloced.
+ *
+ * Any existing data in the abuf is freed and the alloced member is set to
+ * false.
+ *
+ * @abuf: abuf to adjust
+ * @data: New contents of abuf
+ * @size: New size of abuf
+ */
+void abuf_set(struct abuf *abuf, void *data, size_t size);
+
+/**
+ * abuf_map_sysmem() - calls map_sysmem() to set up an abuf
+ *
+ * This is equivalent to abuf_set(abuf, map_sysmem(addr, size), size)
+ *
+ * Any existing data in the abuf is freed and the alloced member is set to
+ * false.
+ *
+ * @abuf: abuf to adjust
+ * @addr: Address to set the abuf to
+ * @size: New size of abuf
+ */
+void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size);
+
+/**
+ * abuf_realloc() - Change the size of a buffer
+ *
+ * This uses realloc() to change the size of the buffer, with the same semantics
+ * as that function. If the abuf is not currently alloced, then it will alloc
+ * it if the size needs to increase (i.e. set the alloced member to true)
+ *
+ * @abuf: abuf to adjust
+ * @new_size: new size in bytes.
+ *	if 0, the abuf is freed
+ *	if greater than the current size, the abuf is extended and the new
+ *	   space is not inited. The alloced member is set to true
+ *	if less than the current size, the abuf is contracted and the data at
+ *	   the end is lost. If @new_size is 0, this sets the alloced member to
+ *	   false
+ * @return true if OK, false if out of memory
+ */
+bool abuf_realloc(struct abuf *abuf, size_t new_size);
+
+/**
+ * abuf_uninit_move() - Return the allocated contents and uninit the abuf
+ *
+ * This returns the abuf data to the caller, allocating it if necessary, so that
+ * the caller receives data that it can be sure will hang around. The caller is
+ * responsible for freeing the data.
+ *
+ * If the abuf has allocated data, it is returned. If the abuf has data but it
+ * is not allocated, then it is first allocated, then returned.
+ *
+ * If the abuf size is 0, this returns NULL
+ *
+ * The abuf is uninited as part of this, except if the allocation fails, in
+ * which NULL is returned and the abuf remains untouched.
+ *
+ * The abuf must be inited before this can be called.
+ *
+ * @abuf: abuf to uninit
+ * @sizep: if non-NULL, returns the size of the returned data
+ * @return data contents, allocated with malloc(), or NULL if the data could not
+ *	be allocated, or the data size is 0
+ */
+void *abuf_uninit_move(struct abuf *abuf, size_t *sizep);
+
+/**
+ * abuf_init_move() - Make abuf take over the management of an allocated region
+ *
+ * After this, @data must not be used. All access must be via the abuf.
+ *
+ * @abuf: abuf to init
+ * @data: Existing allocated buffer to place in the abuf
+ * @size: Size of allocated buffer
+ */
+void abuf_init_move(struct abuf *abuf, void *data, size_t size);
+
+/**
+ * abuf_init_set() - Set up a new abuf
+ *
+ * Inits a new abuf and sets up its (unallocated) data
+ *
+ * @abuf: abuf to set up
+ * @data: New contents of abuf
+ * @size: New size of abuf
+ */
+void abuf_init_set(struct abuf *abuf, void *data, size_t size);
+
+/**
+ * abuf_uninit() - Free any memory used by an abuf
+ *
+ * The buffer must be inited before this can be called.
+ *
+ * @abuf: abuf to uninit
+ */
+void abuf_uninit(struct abuf *abuf);
+
+/**
+ * abuf_init() - Set up a new abuf
+ *
+ * This initially has no data and alloced is set to false. This is equivalent to
+ * setting all fields to 0, e.g. with memset(), so callers can do that instead
+ * if desired.
+ *
+ * @abuf: abuf to set up
+ */
+void abuf_init(struct abuf *abuf);
+
+#endif
diff --git a/lib/Makefile b/lib/Makefile
index dfe772aaff5..9c0373e2955 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -137,6 +137,7 @@ obj-$(CONFIG_OID_REGISTRY) += oid_registry.o
 obj-$(CONFIG_SSCANF) += sscanf.o
 endif
 
+obj-y += abuf.o
 obj-y += date.o
 obj-y += rtc-lib.o
 obj-$(CONFIG_LIB_ELF) += elf.o
diff --git a/lib/abuf.c b/lib/abuf.c
new file mode 100644
index 00000000000..4b17e0b8c0d
--- /dev/null
+++ b/lib/abuf.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Handles a buffer that can be allocated and freed
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <abuf.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <string.h>
+
+void abuf_set(struct abuf *abuf, void *data, size_t size)
+{
+	abuf_uninit(abuf);
+	abuf->data = data;
+	abuf->size = size;
+}
+
+void abuf_map_sysmem(struct abuf *abuf, ulong addr, size_t size)
+{
+	abuf_set(abuf, map_sysmem(addr, size), size);
+}
+
+bool abuf_realloc(struct abuf *abuf, size_t new_size)
+{
+	void *ptr;
+
+	if (!new_size) {
+		/* easy case, just need to uninit, freeing any allocation */
+		abuf_uninit(abuf);
+		return true;
+	} else if (abuf->alloced) {
+		/* currently allocated, so need to reallocate */
+		ptr = realloc(abuf->data, new_size);
+		if (!ptr)
+			return false;
+		abuf->data = ptr;
+		abuf->size = new_size;
+		return true;
+	} else if (new_size <= abuf->size) {
+		/*
+		 * not currently alloced and new size is no larger. Just update
+		 * it. Data is lost off the end if new_size < abuf->size
+		 */
+		abuf->size = new_size;
+		return true;
+	} else {
+		/* not currently allocated and new size is larger. Alloc and
+		 * copy in data. The new space is not inited.
+		 */
+		ptr = memdup(abuf->data, new_size);
+		if (!ptr)
+			return false;
+		abuf->data = ptr;
+		abuf->size = new_size;
+		abuf->alloced = true;
+		return true;
+	}
+}
+
+void *abuf_uninit_move(struct abuf *abuf, size_t *sizep)
+{
+	void *ptr;
+
+	if (sizep)
+		*sizep = abuf->size;
+	if (!abuf->size)
+		return NULL;
+	if (abuf->alloced) {
+		ptr = abuf->data;
+	} else {
+		ptr = memdup(abuf->data, abuf->size);
+		if (!ptr)
+			return NULL;
+	}
+	/* Clear everything out so there is no record of the data */
+	abuf_init(abuf);
+
+	return ptr;
+}
+
+void abuf_init_set(struct abuf *abuf, void *data, size_t size)
+{
+	abuf_init(abuf);
+	abuf_set(abuf, data, size);
+}
+
+void abuf_init_move(struct abuf *abuf, void *data, size_t size)
+{
+	abuf_init_set(abuf, data, size);
+	abuf->alloced = true;
+}
+
+void abuf_uninit(struct abuf *abuf)
+{
+	if (abuf->alloced)
+		free(abuf->data);
+	abuf_init(abuf);
+}
+
+void abuf_init(struct abuf *abuf)
+{
+	abuf->data = NULL;
+	abuf->size = 0;
+	abuf->alloced = false;
+}
diff --git a/test/lib/Makefile b/test/lib/Makefile
index 6fd05142510..d244bb431d4 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -3,6 +3,7 @@
 # (C) Copyright 2018
 # Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
 obj-y += cmd_ut_lib.o
+obj-y += abuf.o
 obj-$(CONFIG_EFI_LOADER) += efi_device_path.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 obj-y += hexdump.o
diff --git a/test/lib/abuf.c b/test/lib/abuf.c
new file mode 100644
index 00000000000..086c9b22821
--- /dev/null
+++ b/test/lib/abuf.c
@@ -0,0 +1,344 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <abuf.h>
+#include <mapmem.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static char test_data[] = "1234";
+#define TEST_DATA_LEN	sizeof(test_data)
+
+/* Test abuf_set() */
+static int lib_test_abuf_set(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong start;
+
+	start = ut_check_free();
+
+	abuf_init(&buf);
+	abuf_set(&buf, test_data, TEST_DATA_LEN);
+	ut_asserteq_ptr(test_data, buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Force it to allocate */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN + 1));
+	ut_assertnonnull(buf.data);
+	ut_asserteq(TEST_DATA_LEN + 1, buf.size);
+	ut_asserteq(true, buf.alloced);
+
+	/* Now set it again, to force it to free */
+	abuf_set(&buf, test_data, TEST_DATA_LEN);
+	ut_asserteq_ptr(test_data, buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Check for memory leaks */
+	ut_assertok(ut_check_delta(start));
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_set, 0);
+
+/* Test abuf_map_sysmem() */
+static int lib_test_abuf_map_sysmem(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong addr;
+
+	abuf_init(&buf);
+	addr = 0x100;
+	abuf_map_sysmem(&buf, addr, TEST_DATA_LEN);
+
+	ut_asserteq_ptr(map_sysmem(0x100, 0), buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_map_sysmem, 0);
+
+/* Test abuf_realloc() */
+static int lib_test_abuf_realloc(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong start;
+	void *ptr;
+
+	/*
+	 * TODO: crashes on sandbox sometimes due to an apparent bug in
+	 * realloc().
+	 */
+	return 0;
+
+	start = ut_check_free();
+
+	abuf_init(&buf);
+
+	/* Allocate an empty buffer */
+	ut_asserteq(true, abuf_realloc(&buf, 0));
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Allocate a non-empty abuf */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
+	ut_assertnonnull(buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(true, buf.alloced);
+	ptr = buf.data;
+
+	/*
+	 * Make it smaller; the pointer should remain the same. Note this relies
+	 * on knowledge of how U-Boot's realloc() works
+	 */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN - 1));
+	ut_asserteq(TEST_DATA_LEN - 1, buf.size);
+	ut_asserteq(true, buf.alloced);
+	ut_asserteq_ptr(ptr, buf.data);
+
+	/*
+	 * Make it larger, forcing reallocation. Note this relies on knowledge
+	 * of how U-Boot's realloc() works
+	 */
+	ut_asserteq(true, abuf_realloc(&buf, 0x1000));
+	ut_assert(buf.data != ptr);
+	ut_asserteq(0x1000, buf.size);
+	ut_asserteq(true, buf.alloced);
+
+	/* Free it */
+	ut_asserteq(true, abuf_realloc(&buf, 0));
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Check for memory leaks */
+	ut_assertok(ut_check_delta(start));
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_realloc, 0);
+
+/* Test handling of buffers that are too large */
+static int lib_test_abuf_large(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong start;
+	size_t size;
+	int delta;
+	void *ptr;
+
+	/*
+	 * This crashes at present due to trying to allocate more memory than
+	 * available, which breaks something on sandbox.
+	 */
+	return 0;
+
+	start = ut_check_free();
+
+	/* Try an impossible size */
+	abuf_init(&buf);
+	ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	abuf_uninit(&buf);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Start with a normal size then try to increase it, to check realloc */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
+	ut_assertnonnull(buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(true, buf.alloced);
+	ptr = buf.data;
+	delta = ut_check_delta(start);
+	ut_assert(delta > 0);
+
+	/* try to increase it */
+	ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
+	ut_asserteq_ptr(ptr, buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(true, buf.alloced);
+	ut_asserteq(delta, ut_check_delta(start));
+
+	/* Check for memory leaks */
+	abuf_uninit(&buf);
+	ut_assertok(ut_check_delta(start));
+
+	/* Start with a huge unallocated buf and try to move it */
+	abuf_init(&buf);
+	abuf_map_sysmem(&buf, 0, CONFIG_SYS_MALLOC_LEN);
+	ut_asserteq(CONFIG_SYS_MALLOC_LEN, buf.size);
+	ut_asserteq(false, buf.alloced);
+	ut_assertnull(abuf_uninit_move(&buf, &size));
+
+	/* Check for memory leaks */
+	abuf_uninit(&buf);
+	ut_assertok(ut_check_delta(start));
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_large, 0);
+
+/* Test abuf_uninit_move() */
+static int lib_test_abuf_uninit_move(struct unit_test_state *uts)
+{
+	void *ptr, *orig_ptr;
+	struct abuf buf;
+	size_t size;
+	ulong start;
+	int delta;
+
+	start = ut_check_free();
+
+	/*
+	 * TODO: crashes on sandbox sometimes due to an apparent bug in
+	 * realloc().
+	 */
+	return 0;
+
+	/* Move an empty buffer */
+	abuf_init(&buf);
+	ut_assertnull(abuf_uninit_move(&buf, &size));
+	ut_asserteq(0, size);
+	ut_assertnull(abuf_uninit_move(&buf, NULL));
+
+	/* Move an unallocated buffer */
+	abuf_set(&buf, test_data, TEST_DATA_LEN);
+	ut_assertok(ut_check_delta(start));
+	ptr = abuf_uninit_move(&buf, &size);
+	ut_asserteq(TEST_DATA_LEN, size);
+	ut_asserteq_str(ptr, test_data);
+	ut_assertnonnull(ptr);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Check that freeing it frees the only allocation */
+	delta = ut_check_delta(start);
+	ut_assert(delta > 0);
+	free(ptr);
+	ut_assertok(ut_check_delta(start));
+
+	/* Move an allocated buffer */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
+	orig_ptr = buf.data;
+	strcpy(orig_ptr, test_data);
+
+	delta = ut_check_delta(start);
+	ut_assert(delta > 0);
+	ptr = abuf_uninit_move(&buf, &size);
+	ut_asserteq(TEST_DATA_LEN, size);
+	ut_assertnonnull(ptr);
+	ut_asserteq_ptr(ptr, orig_ptr);
+	ut_asserteq_str(ptr, test_data);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Check there was no new allocation */
+	ut_asserteq(delta, ut_check_delta(start));
+
+	/* Check that freeing it frees the only allocation */
+	free(ptr);
+	ut_assertok(ut_check_delta(start));
+
+	/* Move an unallocated buffer, without the size */
+	abuf_set(&buf, test_data, TEST_DATA_LEN);
+	ut_assertok(ut_check_delta(start));
+	ptr = abuf_uninit_move(&buf, NULL);
+	ut_asserteq_str(ptr, test_data);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_uninit_move, 0);
+
+/* Test abuf_uninit() */
+static int lib_test_abuf_uninit(struct unit_test_state *uts)
+{
+	struct abuf buf;
+
+	/* Nothing in the buffer */
+	abuf_init(&buf);
+	abuf_uninit(&buf);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Not allocated */
+	abuf_set(&buf, test_data, TEST_DATA_LEN);
+	abuf_uninit(&buf);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_uninit, 0);
+
+/* Test abuf_init_set() */
+static int lib_test_abuf_init_set(struct unit_test_state *uts)
+{
+	struct abuf buf;
+
+	abuf_init_set(&buf, test_data, TEST_DATA_LEN);
+	ut_asserteq_ptr(test_data, buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_init_set, 0);
+
+/* Test abuf_init_move() */
+static int lib_test_abuf_init_move(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	void *ptr;
+
+	/*
+	 * TODO: crashes on sandbox sometimes due to an apparent bug in
+	 * realloc().
+	 */
+	return 0;
+
+	ptr = strdup(test_data);
+	ut_assertnonnull(ptr);
+
+	free(ptr);
+
+	abuf_init_move(&buf, ptr, TEST_DATA_LEN);
+	ut_asserteq_ptr(ptr, abuf_data(&buf));
+	ut_asserteq(TEST_DATA_LEN, abuf_size(&buf));
+	ut_asserteq(true, buf.alloced);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_init_move, 0);
+
+/* Test abuf_init() */
+static int lib_test_abuf_init(struct unit_test_state *uts)
+{
+	struct abuf buf;
+
+	buf.data = &buf;
+	buf.size = 123;
+	buf.alloced = true;
+	abuf_init(&buf);
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_init, 0);
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 03/15] compiler: Add a comment to host_build()
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
  2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
  2021-09-25 13:03 ` [PATCH v4 02/15] Add support for an owned buffer Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 04/15] zstd: Create a function for use from U-Boot Simon Glass
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini, Simon Glass

This function should have a comment explaining what it does. Add one.

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

(no changes since v2)

Changes in v2:
- Correct 'hose' typo

 include/compiler.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/compiler.h b/include/compiler.h
index 27b9843497a..67e52050b12 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -151,6 +151,11 @@ typedef unsigned long int uintptr_t;
 #define MEM_SUPPORT_64BIT_DATA	0
 #endif
 
+/**
+ * host_build() - check if we are building for the host
+ *
+ * @return true if building for the host, false if for a target
+ */
 static inline bool host_build(void) {
 #ifdef USE_HOSTCC
 	return true;
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 04/15] zstd: Create a function for use from U-Boot
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (2 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 03/15] compiler: Add a comment to host_build() Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 05/15] btrfs: Use U-Boot API for decompression Simon Glass
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini, Simon Glass

The existing zstd API requires the same sequence of calls to perform its
task. Create a helper for U-Boot, to avoid code duplication, as is done
with other compression algorithms. Make use of of this from the image
code.

Note that the zstd code lacks a test in test/compression.c and this should
be added by the maintainer.

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

(no changes since v1)

 common/image.c       | 50 +++++++---------------------------
 include/linux/zstd.h | 11 ++++++++
 lib/zstd/Makefile    |  2 +-
 lib/zstd/zstd.c      | 64 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+), 42 deletions(-)
 create mode 100644 lib/zstd/zstd.c

diff --git a/common/image.c b/common/image.c
index e199d61a4c3..e58793bd69f 100644
--- a/common/image.c
+++ b/common/image.c
@@ -22,6 +22,7 @@
 #include <status_led.h>
 #endif
 
+#include <abuf.h>
 #include <rtc.h>
 
 #include <gzip.h>
@@ -527,50 +528,17 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 #ifndef USE_HOSTCC
 #if CONFIG_IS_ENABLED(ZSTD)
 	case IH_COMP_ZSTD: {
-		size_t size = unc_len;
-		ZSTD_DStream *dstream;
-		ZSTD_inBuffer in_buf;
-		ZSTD_outBuffer out_buf;
-		void *workspace;
-		size_t wsize;
-
-		wsize = ZSTD_DStreamWorkspaceBound(image_len);
-		workspace = malloc(wsize);
-		if (!workspace) {
-			debug("%s: cannot allocate workspace of size %zu\n", __func__,
-			      wsize);
-			return -1;
-		}
-
-		dstream = ZSTD_initDStream(image_len, workspace, wsize);
-		if (!dstream) {
-			printf("%s: ZSTD_initDStream failed\n", __func__);
-			return ZSTD_getErrorCode(ret);
-		}
-
-		in_buf.src = image_buf;
-		in_buf.pos = 0;
-		in_buf.size = image_len;
+		struct abuf in, out;
 
-		out_buf.dst = load_buf;
-		out_buf.pos = 0;
-		out_buf.size = size;
-
-		while (1) {
-			size_t ret;
-
-			ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
-			if (ZSTD_isError(ret)) {
-				printf("%s: ZSTD_decompressStream error %d\n", __func__,
-				       ZSTD_getErrorCode(ret));
-				return ZSTD_getErrorCode(ret);
-			}
-
-			if (in_buf.pos >= image_len || !ret)
-				break;
+		abuf_init_set(&in, image_buf, image_len);
+		abuf_init_set(&in, load_buf, unc_len);
+		ret = zstd_decompress(&in, &out);
+		if (ret < 0) {
+			printf("ZSTD decompression failed\n");
+			return ret;
 		}
 
-		image_len = out_buf.pos;
+		image_len = ret;
 
 		break;
 	}
diff --git a/include/linux/zstd.h b/include/linux/zstd.h
index 724f69350e0..35ba4c90aa4 100644
--- a/include/linux/zstd.h
+++ b/include/linux/zstd.h
@@ -1144,4 +1144,15 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx *dctx, void *dst, size_t dstCapacity,
 size_t ZSTD_insertBlock(ZSTD_DCtx *dctx, const void *blockStart,
 	size_t blockSize);
 
+struct abuf;
+
+/**
+ * zstd_decompress() - Decompress Zstandard data
+ *
+ * @in: Input buffer to decompress
+ * @out: Output buffer to hold the results (must be large enough)
+ * @return size of the decompressed data, or -ve on error
+ */
+int zstd_decompress(struct abuf *in, struct abuf *out);
+
 #endif  /* ZSTD_H */
diff --git a/lib/zstd/Makefile b/lib/zstd/Makefile
index 33c1df48ec8..12170892923 100644
--- a/lib/zstd/Makefile
+++ b/lib/zstd/Makefile
@@ -1,4 +1,4 @@
 obj-y += zstd_decompress.o
 
 zstd_decompress-y := huf_decompress.o decompress.o \
-		     entropy_common.o fse_decompress.o zstd_common.o
+		     entropy_common.o fse_decompress.o zstd_common.o zstd.o
diff --git a/lib/zstd/zstd.c b/lib/zstd/zstd.c
new file mode 100644
index 00000000000..bf9cd19cfa3
--- /dev/null
+++ b/lib/zstd/zstd.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2021 Google LLC
+ */
+
+#define LOG_CATEGORY	LOGC_BOOT
+
+#include <common.h>
+#include <abuf.h>
+#include <log.h>
+#include <malloc.h>
+#include <linux/zstd.h>
+
+int zstd_decompress(struct abuf *in, struct abuf *out)
+{
+	ZSTD_DStream *dstream;
+	ZSTD_inBuffer in_buf;
+	ZSTD_outBuffer out_buf;
+	void *workspace;
+	size_t wsize;
+	int ret;
+
+	wsize = ZSTD_DStreamWorkspaceBound(abuf_size(in));
+	workspace = malloc(wsize);
+	if (!workspace) {
+		debug("%s: cannot allocate workspace of size %zu\n", __func__,
+			wsize);
+		return -ENOMEM;
+	}
+
+	dstream = ZSTD_initDStream(abuf_size(in), workspace, wsize);
+	if (!dstream) {
+		log_err("%s: ZSTD_initDStream failed\n", __func__);
+		ret = -EPERM;
+		goto do_free;
+	}
+
+	in_buf.src = abuf_data(in);
+	in_buf.pos = 0;
+	in_buf.size = abuf_size(in);
+
+	out_buf.dst = abuf_data(out);
+	out_buf.pos = 0;
+	out_buf.size = abuf_size(out);
+
+	while (1) {
+		size_t res;
+
+		res = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
+		if (ZSTD_isError(res)) {
+			ret = ZSTD_getErrorCode(res);
+			log_err("ZSTD_decompressStream error %d\n", ret);
+			goto do_free;
+		}
+
+		if (in_buf.pos >= abuf_size(in) || !res)
+			break;
+	}
+
+	ret = out_buf.pos;
+do_free:
+	free(workspace);
+	return ret;
+}
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 05/15] btrfs: Use U-Boot API for decompression
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (3 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 04/15] zstd: Create a function for use from U-Boot Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 06/15] image: Avoid switch default in image_decomp() Simon Glass
                   ` (9 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Qu Wenruo

Use the common function to avoid code duplication.

Acked-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 fs/btrfs/compression.c | 51 +++++-------------------------------------
 1 file changed, 5 insertions(+), 46 deletions(-)

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 23efefa1997..7adfbb04a7c 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -6,6 +6,7 @@
  */
 
 #include "btrfs.h"
+#include <abuf.h>
 #include <log.h>
 #include <malloc.h>
 #include <linux/lzo.h>
@@ -136,54 +137,12 @@ static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
 
 static u32 decompress_zstd(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
 {
-	ZSTD_DStream *dstream;
-	ZSTD_inBuffer in_buf;
-	ZSTD_outBuffer out_buf;
-	void *workspace;
-	size_t wsize;
-	u32 res = -1;
-
-	wsize = ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT);
-	workspace = malloc(wsize);
-	if (!workspace) {
-		debug("%s: cannot allocate workspace of size %zu\n", __func__,
-		      wsize);
-		return -1;
-	}
-
-	dstream = ZSTD_initDStream(ZSTD_BTRFS_MAX_INPUT, workspace, wsize);
-	if (!dstream) {
-		printf("%s: ZSTD_initDStream failed\n", __func__);
-		goto err_free;
-	}
+	struct abuf in, out;
 
-	in_buf.src = cbuf;
-	in_buf.pos = 0;
-	in_buf.size = clen;
+	abuf_init_set(&in, (u8 *)cbuf, clen);
+	abuf_init_set(&out, dbuf, dlen);
 
-	out_buf.dst = dbuf;
-	out_buf.pos = 0;
-	out_buf.size = dlen;
-
-	while (1) {
-		size_t ret;
-
-		ret = ZSTD_decompressStream(dstream, &out_buf, &in_buf);
-		if (ZSTD_isError(ret)) {
-			printf("%s: ZSTD_decompressStream error %d\n", __func__,
-			       ZSTD_getErrorCode(ret));
-			goto err_free;
-		}
-
-		if (in_buf.pos >= clen || !ret)
-			break;
-	}
-
-	res = out_buf.pos;
-
-err_free:
-	free(workspace);
-	return res;
+	return zstd_decompress(&in, &out);
 }
 
 u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 06/15] image: Avoid switch default in image_decomp()
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (4 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 05/15] btrfs: Use U-Boot API for decompression Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 07/15] image: Update zstd to avoid reporting error twice Simon Glass
                   ` (8 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

At present this function is full of preprocessor macros. Adjust it to
check for an unsupported algorithm after the switch(). This will allow
us to drop the macros.

Fix up the return-value path and an extra blank line while we are here.

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

(no changes since v1)

 common/image.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/common/image.c b/common/image.c
index e58793bd69f..d80781f2eb4 100644
--- a/common/image.c
+++ b/common/image.c
@@ -446,7 +446,7 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 		 void *load_buf, void *image_buf, ulong image_len,
 		 uint unc_len, ulong *load_end)
 {
-	int ret = 0;
+	int ret = -ENOSYS;
 
 	*load_end = load;
 	print_decomp_msg(comp, type, load == image_start);
@@ -458,6 +458,7 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 	 */
 	switch (comp) {
 	case IH_COMP_NONE:
+		ret = 0;
 		if (load == image_start)
 			break;
 		if (image_len <= unc_len)
@@ -539,22 +540,23 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 		}
 
 		image_len = ret;
-
 		break;
 	}
 #endif /* CONFIG_ZSTD */
 #endif
-	default:
+	}
+	if (ret == -ENOSYS) {
 		printf("Unimplemented compression type %d\n", comp);
-		return -ENOSYS;
+		return ret;
 	}
+	if (ret)
+		return ret;
 
 	*load_end = load + image_len;
 
-	return ret;
+	return 0;
 }
 
-
 #ifndef USE_HOSTCC
 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
 /**
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 07/15] image: Update zstd to avoid reporting error twice
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (5 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 06/15] image: Avoid switch default in image_decomp() Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 08/15] gzip: Avoid use of u64 Simon Glass
                   ` (7 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

The zstd implementation prints the error in image_decomp() which is
incorrect and does not match other algorithms. Drop this and let the
caller report the error.

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

(no changes since v1)

 common/image.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/common/image.c b/common/image.c
index d80781f2eb4..7fd4a567626 100644
--- a/common/image.c
+++ b/common/image.c
@@ -534,12 +534,10 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 		abuf_init_set(&in, image_buf, image_len);
 		abuf_init_set(&in, load_buf, unc_len);
 		ret = zstd_decompress(&in, &out);
-		if (ret < 0) {
-			printf("ZSTD decompression failed\n");
-			return ret;
+		if (ret >= 0) {
+			image_len = ret;
+			ret = 0;
 		}
-
-		image_len = ret;
 		break;
 	}
 #endif /* CONFIG_ZSTD */
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 08/15] gzip: Avoid use of u64
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (6 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 07/15] image: Update zstd to avoid reporting error twice Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
                   ` (6 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini, Simon Glass

The gzip API uses the u64 type in it, which is not available in the host
build. This makes it impossible to include the header file.

We could make this type available, but it seems unnecessary. Limiting the
compression size to that of the 'unsigned long' type seems good enough. On
32-bit machines the limit then becomes 4GB, which likely exceeds available
RAM anyway, therefore it should be sufficient. On 64-bit machines this is
effectively u64 anyway.

Update the header file and implementation to use 'ulong' instead of 'u64'.

Add a definition of u32 for the cases that seem to need exactly that
length. This should be safe enough.

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

(no changes since v1)

 include/compiler.h |  3 +++
 include/gzip.h     |  8 ++++----
 lib/gunzip.c       | 28 ++++++++++++++--------------
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/include/compiler.h b/include/compiler.h
index 67e52050b12..6b0d3bf5374 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -68,6 +68,9 @@ typedef uint32_t __u32;
 typedef unsigned int uint;
 typedef unsigned long ulong;
 
+/* Define these on the host so we can build some target code */
+typedef __u32 u32;
+
 #define uswap_16(x) \
 	((((x) & 0xff00) >> 8) | \
 	 (((x) & 0x00ff) << 8))
diff --git a/include/gzip.h b/include/gzip.h
index 783acbb60d2..cb4db3d70fe 100644
--- a/include/gzip.h
+++ b/include/gzip.h
@@ -54,11 +54,11 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
  *	gzwrite_progress_finish called at end of loop to
  *		indicate success (retcode=0) or failure
  */
-void gzwrite_progress_init(u64 expected_size);
+void gzwrite_progress_init(ulong expected_size);
 
-void gzwrite_progress(int iteration, u64 bytes_written, u64 total_bytes);
+void gzwrite_progress(int iteration, ulong bytes_written, ulong total_bytes);
 
-void gzwrite_progress_finish(int retcode, u64 totalwritten, u64 totalsize,
+void gzwrite_progress_finish(int retcode, ulong totalwritten, ulong totalsize,
 			     u32 expected_crc, u32 calculated_crc);
 
 /**
@@ -74,7 +74,7 @@ void gzwrite_progress_finish(int retcode, u64 totalwritten, u64 totalsize,
  * @return 0 if OK, -1 on error
  */
 int gzwrite(unsigned char *src, int len, struct blk_desc *dev, ulong szwritebuf,
-	    u64 startoffs, u64 szexpected);
+	    ulong startoffs, ulong szexpected);
 
 /**
  * gzip()- Compress data into a buffer using the gzip algorithm
diff --git a/lib/gunzip.c b/lib/gunzip.c
index bee3b9261f3..a8e498d98d8 100644
--- a/lib/gunzip.c
+++ b/lib/gunzip.c
@@ -84,32 +84,32 @@ int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
 
 #ifdef CONFIG_CMD_UNZIP
 __weak
-void gzwrite_progress_init(u64 expectedsize)
+void gzwrite_progress_init(ulong expectedsize)
 {
 	putc('\n');
 }
 
 __weak
 void gzwrite_progress(int iteration,
-		     u64 bytes_written,
-		     u64 total_bytes)
+		     ulong bytes_written,
+		     ulong total_bytes)
 {
 	if (0 == (iteration & 3))
-		printf("%llu/%llu\r", bytes_written, total_bytes);
+		printf("%lu/%lu\r", bytes_written, total_bytes);
 }
 
 __weak
 void gzwrite_progress_finish(int returnval,
-			     u64 bytes_written,
-			     u64 total_bytes,
+			     ulong bytes_written,
+			     ulong total_bytes,
 			     u32 expected_crc,
 			     u32 calculated_crc)
 {
 	if (0 == returnval) {
-		printf("\n\t%llu bytes, crc 0x%08x\n",
+		printf("\n\t%lu bytes, crc 0x%08x\n",
 		       total_bytes, calculated_crc);
 	} else {
-		printf("\n\tuncompressed %llu of %llu\n"
+		printf("\n\tuncompressed %lu of %lu\n"
 		       "\tcrcs == 0x%08x/0x%08x\n",
 		       bytes_written, total_bytes,
 		       expected_crc, calculated_crc);
@@ -119,15 +119,15 @@ void gzwrite_progress_finish(int returnval,
 int gzwrite(unsigned char *src, int len,
 	    struct blk_desc *dev,
 	    unsigned long szwritebuf,
-	    u64 startoffs,
-	    u64 szexpected)
+	    ulong startoffs,
+	    ulong szexpected)
 {
 	int i, flags;
 	z_stream s;
 	int r = 0;
 	unsigned char *writebuf;
 	unsigned crc = 0;
-	u64 totalfilled = 0;
+	ulong totalfilled = 0;
 	lbaint_t blksperbuf, outblock;
 	u32 expected_crc;
 	u32 payload_size;
@@ -142,7 +142,7 @@ int gzwrite(unsigned char *src, int len,
 	}
 
 	if (startoffs & (dev->blksz-1)) {
-		printf("%s: start offset %llu not a multiple of %lu\n",
+		printf("%s: start offset %lu not a multiple of %lu\n",
 		       __func__, startoffs, dev->blksz);
 		return -1;
 	}
@@ -182,12 +182,12 @@ int gzwrite(unsigned char *src, int len,
 	if (szexpected == 0) {
 		szexpected = le32_to_cpu(szuncompressed);
 	} else if (szuncompressed != (u32)szexpected) {
-		printf("size of %llx doesn't match trailer low bits %x\n",
+		printf("size of %lx doesn't match trailer low bits %x\n",
 		       szexpected, szuncompressed);
 		return -1;
 	}
 	if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
-		printf("%s: uncompressed size %llu exceeds device size\n",
+		printf("%s: uncompressed size %lu exceeds device size\n",
 		       __func__, szexpected);
 		return -1;
 	}
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (7 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 08/15] gzip: Avoid use of u64 Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 10/15] image: Split board code out into its own file Simon Glass
                   ` (5 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

Adjust this function so that preprocessor macros are not needed. With
this, the host build uses more of the same header files as the target
build.

Rather than definining CONFIG_SYS_MALLOC_LEN, add a CONSERVE_MEMORY
define, since that is the purpose of the value.

This appears to have no impact on code size from a spot check of a few
boards (snow, firefly-rk3288, boston32r2el, m53menlo).

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

(no changes since v1)

 common/image.c            | 145 ++++++++++++++++++--------------------
 configs/sandbox_defconfig |   1 +
 2 files changed, 69 insertions(+), 77 deletions(-)

diff --git a/common/image.c b/common/image.c
index 7fd4a567626..80aeb4398ec 100644
--- a/common/image.c
+++ b/common/image.c
@@ -22,12 +22,9 @@
 #include <status_led.h>
 #endif
 
-#include <abuf.h>
 #include <rtc.h>
 
-#include <gzip.h>
 #include <image.h>
-#include <lz4.h>
 #include <mapmem.h>
 
 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
@@ -43,13 +40,6 @@
 #include <linux/errno.h>
 #include <asm/io.h>
 
-#include <bzlib.h>
-#include <linux/lzo.h>
-#include <lzma/LzmaTypes.h>
-#include <lzma/LzmaDec.h>
-#include <lzma/LzmaTools.h>
-#include <linux/zstd.h>
-
 #ifdef CONFIG_CMD_BDI
 extern int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
 		     char *const argv[]);
@@ -61,7 +51,15 @@ DECLARE_GLOBAL_DATA_PTR;
 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 						int verify);
 #endif
+
+/* Set this if we have less than 4 MB of malloc() space */
+#if CONFIG_SYS_MALLOC_LEN < (4096 * 1024)
+#define CONSERVE_MEMORY		true
 #else
+#define CONSERVE_MEMORY		false
+#endif
+
+#else /* USE_HOSTCC */
 #include "mkimage.h"
 #include <u-boot/md5.h>
 #include <time.h>
@@ -70,10 +68,23 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 #ifndef __maybe_unused
 # define __maybe_unused		/* unimplemented */
 #endif
+
+#define CONSERVE_MEMORY		false
+
 #endif /* !USE_HOSTCC*/
 
-#include <u-boot/crc.h>
+#include <abuf.h>
+#include <bzlib.h>
+#include <gzip.h>
+#include <lz4.h>
 #include <imximage.h>
+#include <linux/lzo.h>
+#include <linux/zstd.h>
+#include <linux/kconfig.h>
+#include <lzma/LzmaTypes.h>
+#include <lzma/LzmaDec.h>
+#include <lzma/LzmaTools.h>
+#include <u-boot/crc.h>
 
 #ifndef CONFIG_SYS_BARGSIZE
 #define CONFIG_SYS_BARGSIZE 512
@@ -466,83 +477,63 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 		else
 			ret = -ENOSPC;
 		break;
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(GZIP)
-	case IH_COMP_GZIP: {
-		ret = gunzip(load_buf, unc_len, image_buf, &image_len);
+	case IH_COMP_GZIP:
+		if (!host_build() && CONFIG_IS_ENABLED(GZIP))
+			ret = gunzip(load_buf, unc_len, image_buf, &image_len);
 		break;
-	}
-#endif /* CONFIG_GZIP */
-#endif
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(BZIP2)
-	case IH_COMP_BZIP2: {
-		uint size = unc_len;
+	case IH_COMP_BZIP2:
+		if (!host_build() && CONFIG_IS_ENABLED(BZIP2)) {
+			uint size = unc_len;
 
-		/*
-		 * If we've got less than 4 MB of malloc() space,
-		 * use slower decompression algorithm which requires
-		 * at most 2300 KB of memory.
-		 */
-		ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
-			image_buf, image_len,
-			CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
-		image_len = size;
+			/*
+			 * If we've got less than 4 MB of malloc() space,
+			 * use slower decompression algorithm which requires
+			 * at most 2300 KB of memory.
+			 */
+			ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
+				image_buf, image_len, CONSERVE_MEMORY, 0);
+			image_len = size;
+		}
 		break;
-	}
-#endif /* CONFIG_BZIP2 */
-#endif
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(LZMA)
-	case IH_COMP_LZMA: {
-		SizeT lzma_len = unc_len;
+	case IH_COMP_LZMA:
+		if (!host_build() && CONFIG_IS_ENABLED(LZMA)) {
+			SizeT lzma_len = unc_len;
 
-		ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
-					       image_buf, image_len);
-		image_len = lzma_len;
+			ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
+						       image_buf, image_len);
+			image_len = lzma_len;
+		}
 		break;
-	}
-#endif /* CONFIG_LZMA */
-#endif
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(LZO)
-	case IH_COMP_LZO: {
-		size_t size = unc_len;
+	case IH_COMP_LZO:
+		if (!host_build() && CONFIG_IS_ENABLED(LZO)) {
+			size_t size = unc_len;
 
-		ret = lzop_decompress(image_buf, image_len, load_buf, &size);
-		image_len = size;
+			ret = lzop_decompress(image_buf, image_len, load_buf, &size);
+			image_len = size;
+		}
 		break;
-	}
-#endif /* CONFIG_LZO */
-#endif
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(LZ4)
-	case IH_COMP_LZ4: {
-		size_t size = unc_len;
+	case IH_COMP_LZ4:
+		if (!host_build() && CONFIG_IS_ENABLED(LZ4)) {
+			size_t size = unc_len;
 
-		ret = ulz4fn(image_buf, image_len, load_buf, &size);
-		image_len = size;
+			ret = ulz4fn(image_buf, image_len, load_buf, &size);
+			image_len = size;
+		}
 		break;
-	}
-#endif /* CONFIG_LZ4 */
-#endif
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(ZSTD)
-	case IH_COMP_ZSTD: {
-		struct abuf in, out;
-
-		abuf_init_set(&in, image_buf, image_len);
-		abuf_init_set(&in, load_buf, unc_len);
-		ret = zstd_decompress(&in, &out);
-		if (ret >= 0) {
-			image_len = ret;
-			ret = 0;
+	case IH_COMP_ZSTD:
+		if (!host_build() && CONFIG_IS_ENABLED(ZSTD)) {
+			struct abuf in, out;
+
+			abuf_init_set(&in, image_buf, image_len);
+			abuf_init_set(&in, load_buf, unc_len);
+			ret = zstd_decompress(&in, &out);
+			if (ret >= 0) {
+				image_len = ret;
+				ret = 0;
+			}
 		}
 		break;
 	}
-#endif /* CONFIG_ZSTD */
-#endif
-	}
 	if (ret == -ENOSYS) {
 		printf("Unimplemented compression type %d\n", comp);
 		return ret;
@@ -960,7 +951,7 @@ int get_table_entry_id(const table_entry_t *table,
 	const table_entry_t *t;
 
 	for (t = table; t->id >= 0; ++t) {
-#ifdef CONFIG_NEEDS_MANUAL_RELOC
+#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
 		if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
 #else
 		if (t->sname && strcasecmp(t->sname, name) == 0)
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index f1067b9adaa..d6016771907 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -56,6 +56,7 @@ CONFIG_CMD_MEMINFO=y
 CONFIG_CMD_MEM_SEARCH=y
 CONFIG_CMD_MX_CYCLIC=y
 CONFIG_CMD_MEMTEST=y
+CONFIG_CMD_UNZIP=y
 CONFIG_CMD_BIND=y
 CONFIG_CMD_DEMO=y
 CONFIG_CMD_GPIO=y
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 10/15] image: Split board code out into its own file
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (8 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
                   ` (4 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

To avoid a large #ifdef in the image.c file, move the affected code into
a separate file.

Avoid any style fix-ups for easier review. Those are in the next patch.

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

(no changes since v1)

 common/Makefile      |   2 +-
 common/image-board.c | 925 +++++++++++++++++++++++++++++++++++++++++++
 common/image.c       | 925 +------------------------------------------
 3 files changed, 928 insertions(+), 924 deletions(-)
 create mode 100644 common/image-board.c

diff --git a/common/Makefile b/common/Makefile
index fb8173a5b82..e7839027b6c 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -101,7 +101,7 @@ obj-y += malloc_simple.o
 endif
 endif
 
-obj-y += image.o
+obj-y += image.o image-board.o
 obj-$(CONFIG_$(SPL_TPL_)HASH) += hash.o
 obj-$(CONFIG_ANDROID_AB) += android_ab.o
 obj-$(CONFIG_ANDROID_BOOT_IMAGE) += image-android.o image-android-dt.o
diff --git a/common/image-board.c b/common/image-board.c
new file mode 100644
index 00000000000..f9053552e74
--- /dev/null
+++ b/common/image-board.c
@@ -0,0 +1,925 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Image code used by boards (and not host tools)
+ *
+ * (C) Copyright 2008 Semihalf
+ *
+ * (C) Copyright 2000-2006
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ */
+
+#include <common.h>
+#include <bootstage.h>
+#include <cpu_func.h>
+#include <env.h>
+#include <fpga.h>
+#include <image.h>
+#include <mapmem.h>
+#include <watchdog.h>
+#include <asm/cache.h>
+#include <asm/global_data.h>
+
+#ifndef CONFIG_SYS_BARGSIZE
+#define CONFIG_SYS_BARGSIZE 512
+#endif
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
+/**
+ * image_get_ramdisk - get and verify ramdisk image
+ * @rd_addr: ramdisk image start address
+ * @arch: expected ramdisk architecture
+ * @verify: checksum verification flag
+ *
+ * image_get_ramdisk() returns a pointer to the verified ramdisk image
+ * header. Routine receives image start address and expected architecture
+ * flag. Verification done covers data and header integrity and os/type/arch
+ * fields checking.
+ *
+ * returns:
+ *     pointer to a ramdisk image header, if image was found and valid
+ *     otherwise, return NULL
+ */
+static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
+						int verify)
+{
+	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
+
+	if (!image_check_magic(rd_hdr)) {
+		puts("Bad Magic Number\n");
+		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
+		return NULL;
+	}
+
+	if (!image_check_hcrc(rd_hdr)) {
+		puts("Bad Header Checksum\n");
+		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
+		return NULL;
+	}
+
+	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
+	image_print_contents(rd_hdr);
+
+	if (verify) {
+		puts("   Verifying Checksum ... ");
+		if (!image_check_dcrc(rd_hdr)) {
+			puts("Bad Data CRC\n");
+			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
+			return NULL;
+		}
+		puts("OK\n");
+	}
+
+	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
+
+	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
+	    !image_check_arch(rd_hdr, arch) ||
+	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
+		printf("No Linux %s Ramdisk Image\n",
+				genimg_get_arch_name(arch));
+		bootstage_error(BOOTSTAGE_ID_RAMDISK);
+		return NULL;
+	}
+
+	return rd_hdr;
+}
+#endif
+
+/*****************************************************************************/
+/* Shared dual-format routines */
+/*****************************************************************************/
+ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
+ulong image_save_addr;			/* Default Save Address */
+ulong image_save_size;			/* Default Save Size (in bytes) */
+
+static int on_loadaddr(const char *name, const char *value, enum env_op op,
+	int flags)
+{
+	switch (op) {
+	case env_op_create:
+	case env_op_overwrite:
+		image_load_addr = hextoul(value, NULL);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
+
+ulong env_get_bootm_low(void)
+{
+	char *s = env_get("bootm_low");
+	if (s) {
+		ulong tmp = hextoul(s, NULL);
+		return tmp;
+	}
+
+#if defined(CONFIG_SYS_SDRAM_BASE)
+	return CONFIG_SYS_SDRAM_BASE;
+#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
+	return gd->bd->bi_dram[0].start;
+#else
+	return 0;
+#endif
+}
+
+phys_size_t env_get_bootm_size(void)
+{
+	phys_size_t tmp, size;
+	phys_addr_t start;
+	char *s = env_get("bootm_size");
+	if (s) {
+		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
+		return tmp;
+	}
+
+	start = gd->ram_base;
+	size = gd->ram_size;
+
+	if (start + size > gd->ram_top)
+		size = gd->ram_top - start;
+
+	s = env_get("bootm_low");
+	if (s)
+		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
+	else
+		tmp = start;
+
+	return size - (tmp - start);
+}
+
+phys_size_t env_get_bootm_mapsize(void)
+{
+	phys_size_t tmp;
+	char *s = env_get("bootm_mapsize");
+	if (s) {
+		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
+		return tmp;
+	}
+
+#if defined(CONFIG_SYS_BOOTMAPSZ)
+	return CONFIG_SYS_BOOTMAPSZ;
+#else
+	return env_get_bootm_size();
+#endif
+}
+
+void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
+{
+	if (to == from)
+		return;
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+	if (to > from) {
+		from += len;
+		to += len;
+	}
+	while (len > 0) {
+		size_t tail = (len > chunksz) ? chunksz : len;
+		WATCHDOG_RESET();
+		if (to > from) {
+			to -= tail;
+			from -= tail;
+		}
+		memmove(to, from, tail);
+		if (to < from) {
+			to += tail;
+			from += tail;
+		}
+		len -= tail;
+	}
+#else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
+	memmove(to, from, len);
+#endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
+}
+
+/**
+ * genimg_get_kernel_addr_fit - get the real kernel address and return 2
+ *                              FIT strings
+ * @img_addr: a string might contain real image address
+ * @fit_uname_config: double pointer to a char, will hold pointer to a
+ *                    configuration unit name
+ * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
+ *                    name
+ *
+ * genimg_get_kernel_addr_fit get the real kernel start address from a string
+ * which is normally the first argv of bootm/bootz
+ *
+ * returns:
+ *     kernel start address
+ */
+ulong genimg_get_kernel_addr_fit(char * const img_addr,
+			     const char **fit_uname_config,
+			     const char **fit_uname_kernel)
+{
+	ulong kernel_addr;
+
+	/* find out kernel image address */
+	if (!img_addr) {
+		kernel_addr = image_load_addr;
+		debug("*  kernel: default image load address = 0x%08lx\n",
+		      image_load_addr);
+#if CONFIG_IS_ENABLED(FIT)
+	} else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
+				  fit_uname_config)) {
+		debug("*  kernel: config '%s' from image at 0x%08lx\n",
+		      *fit_uname_config, kernel_addr);
+	} else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
+				     fit_uname_kernel)) {
+		debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
+		      *fit_uname_kernel, kernel_addr);
+#endif
+	} else {
+		kernel_addr = hextoul(img_addr, NULL);
+		debug("*  kernel: cmdline image address = 0x%08lx\n",
+		      kernel_addr);
+	}
+
+	return kernel_addr;
+}
+
+/**
+ * genimg_get_kernel_addr() is the simple version of
+ * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
+ */
+ulong genimg_get_kernel_addr(char * const img_addr)
+{
+	const char *fit_uname_config = NULL;
+	const char *fit_uname_kernel = NULL;
+
+	return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
+					  &fit_uname_kernel);
+}
+
+/**
+ * genimg_get_format - get image format type
+ * @img_addr: image start address
+ *
+ * genimg_get_format() checks whether provided address points to a valid
+ * legacy or FIT image.
+ *
+ * New uImage format and FDT blob are based on a libfdt. FDT blob
+ * may be passed directly or embedded in a FIT image. In both situations
+ * genimg_get_format() must be able to dectect libfdt header.
+ *
+ * returns:
+ *     image format type or IMAGE_FORMAT_INVALID if no image is present
+ */
+int genimg_get_format(const void *img_addr)
+{
+#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
+	const image_header_t *hdr;
+
+	hdr = (const image_header_t *)img_addr;
+	if (image_check_magic(hdr))
+		return IMAGE_FORMAT_LEGACY;
+#endif
+#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
+	if (fdt_check_header(img_addr) == 0)
+		return IMAGE_FORMAT_FIT;
+#endif
+#ifdef CONFIG_ANDROID_BOOT_IMAGE
+	if (android_image_check_header(img_addr) == 0)
+		return IMAGE_FORMAT_ANDROID;
+#endif
+
+	return IMAGE_FORMAT_INVALID;
+}
+
+/**
+ * fit_has_config - check if there is a valid FIT configuration
+ * @images: pointer to the bootm command headers structure
+ *
+ * fit_has_config() checks if there is a FIT configuration in use
+ * (if FTI support is present).
+ *
+ * returns:
+ *     0, no FIT support or no configuration found
+ *     1, configuration found
+ */
+int genimg_has_config(bootm_headers_t *images)
+{
+#if IMAGE_ENABLE_FIT
+	if (images->fit_uname_cfg)
+		return 1;
+#endif
+	return 0;
+}
+
+/**
+ * boot_get_ramdisk - main ramdisk handling routine
+ * @argc: command argument count
+ * @argv: command argument list
+ * @images: pointer to the bootm images structure
+ * @arch: expected ramdisk architecture
+ * @rd_start: pointer to a ulong variable, will hold ramdisk start address
+ * @rd_end: pointer to a ulong variable, will hold ramdisk end
+ *
+ * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
+ * Curently supported are the following ramdisk sources:
+ *      - multicomponent kernel/ramdisk image,
+ *      - commandline provided address of decicated ramdisk image.
+ *
+ * returns:
+ *     0, if ramdisk image was found and valid, or skiped
+ *     rd_start and rd_end are set to ramdisk start/end addresses if
+ *     ramdisk image is found and valid
+ *
+ *     1, if ramdisk image is found but corrupted, or invalid
+ *     rd_start and rd_end are set to 0 if no ramdisk exists
+ */
+int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
+		     uint8_t arch, ulong *rd_start, ulong *rd_end)
+{
+	ulong rd_addr, rd_load;
+	ulong rd_data, rd_len;
+#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
+	const image_header_t *rd_hdr;
+#endif
+	void *buf;
+#ifdef CONFIG_SUPPORT_RAW_INITRD
+	char *end;
+#endif
+#if IMAGE_ENABLE_FIT
+	const char	*fit_uname_config = images->fit_uname_cfg;
+	const char	*fit_uname_ramdisk = NULL;
+	ulong		default_addr;
+	int		rd_noffset;
+#endif
+	const char *select = NULL;
+
+	*rd_start = 0;
+	*rd_end = 0;
+
+#ifdef CONFIG_ANDROID_BOOT_IMAGE
+	/*
+	 * Look for an Android boot image.
+	 */
+	buf = map_sysmem(images->os.start, 0);
+	if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
+		select = (argc == 0) ? env_get("loadaddr") : argv[0];
+#endif
+
+	if (argc >= 2)
+		select = argv[1];
+
+	/*
+	 * Look for a '-' which indicates to ignore the
+	 * ramdisk argument
+	 */
+	if (select && strcmp(select, "-") ==  0) {
+		debug("## Skipping init Ramdisk\n");
+		rd_len = rd_data = 0;
+	} else if (select || genimg_has_config(images)) {
+#if IMAGE_ENABLE_FIT
+		if (select) {
+			/*
+			 * If the init ramdisk comes from the FIT image and
+			 * the FIT image address is omitted in the command
+			 * line argument, try to use os FIT image address or
+			 * default load address.
+			 */
+			if (images->fit_uname_os)
+				default_addr = (ulong)images->fit_hdr_os;
+			else
+				default_addr = image_load_addr;
+
+			if (fit_parse_conf(select, default_addr,
+					   &rd_addr, &fit_uname_config)) {
+				debug("*  ramdisk: config '%s' from image at "
+						"0x%08lx\n",
+						fit_uname_config, rd_addr);
+			} else if (fit_parse_subimage(select, default_addr,
+						&rd_addr, &fit_uname_ramdisk)) {
+				debug("*  ramdisk: subimage '%s' from image at "
+						"0x%08lx\n",
+						fit_uname_ramdisk, rd_addr);
+			} else
+#endif
+			{
+				rd_addr = hextoul(select, NULL);
+				debug("*  ramdisk: cmdline image address = "
+						"0x%08lx\n",
+						rd_addr);
+			}
+#if IMAGE_ENABLE_FIT
+		} else {
+			/* use FIT configuration provided in first bootm
+			 * command argument. If the property is not defined,
+			 * quit silently.
+			 */
+			rd_addr = map_to_sysmem(images->fit_hdr_os);
+			rd_noffset = fit_get_node_from_config(images,
+					FIT_RAMDISK_PROP, rd_addr);
+			if (rd_noffset == -ENOENT)
+				return 0;
+			else if (rd_noffset < 0)
+				return 1;
+		}
+#endif
+
+		/*
+		 * Check if there is an initrd image at the
+		 * address provided in the second bootm argument
+		 * check image type, for FIT images get FIT node.
+		 */
+		buf = map_sysmem(rd_addr, 0);
+		switch (genimg_get_format(buf)) {
+#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
+		case IMAGE_FORMAT_LEGACY:
+			printf("## Loading init Ramdisk from Legacy "
+					"Image at %08lx ...\n", rd_addr);
+
+			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
+			rd_hdr = image_get_ramdisk(rd_addr, arch,
+							images->verify);
+
+			if (rd_hdr == NULL)
+				return 1;
+
+			rd_data = image_get_data(rd_hdr);
+			rd_len = image_get_data_size(rd_hdr);
+			rd_load = image_get_load(rd_hdr);
+			break;
+#endif
+#if IMAGE_ENABLE_FIT
+		case IMAGE_FORMAT_FIT:
+			rd_noffset = fit_image_load(images,
+					rd_addr, &fit_uname_ramdisk,
+					&fit_uname_config, arch,
+					IH_TYPE_RAMDISK,
+					BOOTSTAGE_ID_FIT_RD_START,
+					FIT_LOAD_OPTIONAL_NON_ZERO,
+					&rd_data, &rd_len);
+			if (rd_noffset < 0)
+				return 1;
+
+			images->fit_hdr_rd = map_sysmem(rd_addr, 0);
+			images->fit_uname_rd = fit_uname_ramdisk;
+			images->fit_noffset_rd = rd_noffset;
+			break;
+#endif
+#ifdef CONFIG_ANDROID_BOOT_IMAGE
+		case IMAGE_FORMAT_ANDROID:
+			android_image_get_ramdisk((void *)images->os.start,
+				&rd_data, &rd_len);
+			break;
+#endif
+		default:
+#ifdef CONFIG_SUPPORT_RAW_INITRD
+			end = NULL;
+			if (select)
+				end = strchr(select, ':');
+			if (end) {
+				rd_len = hextoul(++end, NULL);
+				rd_data = rd_addr;
+			} else
+#endif
+			{
+				puts("Wrong Ramdisk Image Format\n");
+				rd_data = rd_len = rd_load = 0;
+				return 1;
+			}
+		}
+	} else if (images->legacy_hdr_valid &&
+			image_check_type(&images->legacy_hdr_os_copy,
+						IH_TYPE_MULTI)) {
+
+		/*
+		 * Now check if we have a legacy mult-component image,
+		 * get second entry data start address and len.
+		 */
+		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
+		printf("## Loading init Ramdisk from multi component "
+				"Legacy Image at %08lx ...\n",
+				(ulong)images->legacy_hdr_os);
+
+		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
+	} else {
+		/*
+		 * no initrd image
+		 */
+		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
+		rd_len = rd_data = 0;
+	}
+
+	if (!rd_data) {
+		debug("## No init Ramdisk\n");
+	} else {
+		*rd_start = rd_data;
+		*rd_end = rd_data + rd_len;
+	}
+	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
+			*rd_start, *rd_end);
+
+	return 0;
+}
+
+#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
+/**
+ * boot_ramdisk_high - relocate init ramdisk
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
+ * @rd_data: ramdisk data start address
+ * @rd_len: ramdisk data length
+ * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
+ *      start address (after possible relocation)
+ * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
+ *      end address (after possible relocation)
+ *
+ * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
+ * variable and if requested ramdisk data is moved to a specified location.
+ *
+ * Initrd_start and initrd_end are set to final (after relocation) ramdisk
+ * start/end addresses if ramdisk image start and len were provided,
+ * otherwise set initrd_start and initrd_end set to zeros.
+ *
+ * returns:
+ *      0 - success
+ *     -1 - failure
+ */
+int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
+		  ulong *initrd_start, ulong *initrd_end)
+{
+	char	*s;
+	ulong	initrd_high;
+	int	initrd_copy_to_ram = 1;
+
+	s = env_get("initrd_high");
+	if (s) {
+		/* a value of "no" or a similar string will act like 0,
+		 * turning the "load high" feature off. This is intentional.
+		 */
+		initrd_high = hextoul(s, NULL);
+		if (initrd_high == ~0)
+			initrd_copy_to_ram = 0;
+	} else {
+		initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
+	}
+
+
+	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
+			initrd_high, initrd_copy_to_ram);
+
+	if (rd_data) {
+		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
+			debug("   in-place initrd\n");
+			*initrd_start = rd_data;
+			*initrd_end = rd_data + rd_len;
+			lmb_reserve(lmb, rd_data, rd_len);
+		} else {
+			if (initrd_high)
+				*initrd_start = (ulong)lmb_alloc_base(lmb,
+						rd_len, 0x1000, initrd_high);
+			else
+				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
+								 0x1000);
+
+			if (*initrd_start == 0) {
+				puts("ramdisk - allocation error\n");
+				goto error;
+			}
+			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
+
+			*initrd_end = *initrd_start + rd_len;
+			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
+					*initrd_start, *initrd_end);
+
+			memmove_wd((void *)*initrd_start,
+					(void *)rd_data, rd_len, CHUNKSZ);
+
+#ifdef CONFIG_MP
+			/*
+			 * Ensure the image is flushed to memory to handle
+			 * AMP boot scenarios in which we might not be
+			 * HW cache coherent
+			 */
+			flush_cache((unsigned long)*initrd_start,
+				    ALIGN(rd_len, ARCH_DMA_MINALIGN));
+#endif
+			puts("OK\n");
+		}
+	} else {
+		*initrd_start = 0;
+		*initrd_end = 0;
+	}
+	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
+			*initrd_start, *initrd_end);
+
+	return 0;
+
+error:
+	return -1;
+}
+#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
+
+int boot_get_setup(bootm_headers_t *images, uint8_t arch,
+		   ulong *setup_start, ulong *setup_len)
+{
+#if IMAGE_ENABLE_FIT
+	return boot_get_setup_fit(images, arch, setup_start, setup_len);
+#else
+	return -ENOENT;
+#endif
+}
+
+#if IMAGE_ENABLE_FIT
+#if defined(CONFIG_FPGA)
+int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
+		  uint8_t arch, const ulong *ld_start, ulong * const ld_len)
+{
+	ulong tmp_img_addr, img_data, img_len;
+	void *buf;
+	int conf_noffset;
+	int fit_img_result;
+	const char *uname, *name;
+	int err;
+	int devnum = 0; /* TODO support multi fpga platforms */
+
+	/* Check to see if the images struct has a FIT configuration */
+	if (!genimg_has_config(images)) {
+		debug("## FIT configuration was not specified\n");
+		return 0;
+	}
+
+	/*
+	 * Obtain the os FIT header from the images struct
+	 */
+	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
+	buf = map_sysmem(tmp_img_addr, 0);
+	/*
+	 * Check image type. For FIT images get FIT node
+	 * and attempt to locate a generic binary.
+	 */
+	switch (genimg_get_format(buf)) {
+	case IMAGE_FORMAT_FIT:
+		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
+
+		uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
+					   NULL);
+		if (!uname) {
+			debug("## FPGA image is not specified\n");
+			return 0;
+		}
+		fit_img_result = fit_image_load(images,
+						tmp_img_addr,
+						(const char **)&uname,
+						&(images->fit_uname_cfg),
+						arch,
+						IH_TYPE_FPGA,
+						BOOTSTAGE_ID_FPGA_INIT,
+						FIT_LOAD_OPTIONAL_NON_ZERO,
+						&img_data, &img_len);
+
+		debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
+		      uname, img_data, img_len);
+
+		if (fit_img_result < 0) {
+			/* Something went wrong! */
+			return fit_img_result;
+		}
+
+		if (!fpga_is_partial_data(devnum, img_len)) {
+			name = "full";
+			err = fpga_loadbitstream(devnum, (char *)img_data,
+						 img_len, BIT_FULL);
+			if (err)
+				err = fpga_load(devnum, (const void *)img_data,
+						img_len, BIT_FULL);
+		} else {
+			name = "partial";
+			err = fpga_loadbitstream(devnum, (char *)img_data,
+						 img_len, BIT_PARTIAL);
+			if (err)
+				err = fpga_load(devnum, (const void *)img_data,
+						img_len, BIT_PARTIAL);
+		}
+
+		if (err)
+			return err;
+
+		printf("   Programming %s bitstream... OK\n", name);
+		break;
+	default:
+		printf("The given image format is not supported (corrupt?)\n");
+		return 1;
+	}
+
+	return 0;
+}
+#endif
+
+static void fit_loadable_process(uint8_t img_type,
+				 ulong img_data,
+				 ulong img_len)
+{
+	int i;
+	const unsigned int count =
+			ll_entry_count(struct fit_loadable_tbl, fit_loadable);
+	struct fit_loadable_tbl *fit_loadable_handler =
+			ll_entry_start(struct fit_loadable_tbl, fit_loadable);
+	/* For each loadable handler */
+	for (i = 0; i < count; i++, fit_loadable_handler++)
+		/* matching this type */
+		if (fit_loadable_handler->type == img_type)
+			/* call that handler with this image data */
+			fit_loadable_handler->handler(img_data, img_len);
+}
+
+int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
+		      uint8_t arch, const ulong *ld_start, ulong * const ld_len)
+{
+	/*
+	 * These variables are used to hold the current image location
+	 * in system memory.
+	 */
+	ulong tmp_img_addr;
+	/*
+	 * These two variables are requirements for fit_image_load, but
+	 * their values are not used
+	 */
+	ulong img_data, img_len;
+	void *buf;
+	int loadables_index;
+	int conf_noffset;
+	int fit_img_result;
+	const char *uname;
+	uint8_t img_type;
+
+	/* Check to see if the images struct has a FIT configuration */
+	if (!genimg_has_config(images)) {
+		debug("## FIT configuration was not specified\n");
+		return 0;
+	}
+
+	/*
+	 * Obtain the os FIT header from the images struct
+	 */
+	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
+	buf = map_sysmem(tmp_img_addr, 0);
+	/*
+	 * Check image type. For FIT images get FIT node
+	 * and attempt to locate a generic binary.
+	 */
+	switch (genimg_get_format(buf)) {
+	case IMAGE_FORMAT_FIT:
+		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
+
+		for (loadables_index = 0;
+		     uname = fdt_stringlist_get(buf, conf_noffset,
+					FIT_LOADABLE_PROP, loadables_index,
+					NULL), uname;
+		     loadables_index++)
+		{
+			fit_img_result = fit_image_load(images,
+				tmp_img_addr,
+				&uname,
+				&(images->fit_uname_cfg), arch,
+				IH_TYPE_LOADABLE,
+				BOOTSTAGE_ID_FIT_LOADABLE_START,
+				FIT_LOAD_OPTIONAL_NON_ZERO,
+				&img_data, &img_len);
+			if (fit_img_result < 0) {
+				/* Something went wrong! */
+				return fit_img_result;
+			}
+
+			fit_img_result = fit_image_get_node(buf, uname);
+			if (fit_img_result < 0) {
+				/* Something went wrong! */
+				return fit_img_result;
+			}
+			fit_img_result = fit_image_get_type(buf,
+							    fit_img_result,
+							    &img_type);
+			if (fit_img_result < 0) {
+				/* Something went wrong! */
+				return fit_img_result;
+			}
+
+			fit_loadable_process(img_type, img_data, img_len);
+		}
+		break;
+	default:
+		printf("The given image format is not supported (corrupt?)\n");
+		return 1;
+	}
+
+	return 0;
+}
+#endif
+
+#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
+/**
+ * boot_get_cmdline - allocate and initialize kernel cmdline
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
+ * @cmd_start: pointer to a ulong variable, will hold cmdline start
+ * @cmd_end: pointer to a ulong variable, will hold cmdline end
+ *
+ * boot_get_cmdline() allocates space for kernel command line below
+ * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
+ * variable is present its contents is copied to allocated kernel
+ * command line.
+ *
+ * returns:
+ *      0 - success
+ *     -1 - failure
+ */
+int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
+{
+	char *cmdline;
+	char *s;
+
+	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
+				env_get_bootm_mapsize() + env_get_bootm_low());
+
+	if (cmdline == NULL)
+		return -1;
+
+	s = env_get("bootargs");
+	if (!s)
+		s = "";
+
+	strcpy(cmdline, s);
+
+	*cmd_start = (ulong) & cmdline[0];
+	*cmd_end = *cmd_start + strlen(cmdline);
+
+	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
+
+	return 0;
+}
+#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
+
+#ifdef CONFIG_SYS_BOOT_GET_KBD
+/**
+ * boot_get_kbd - allocate and initialize kernel copy of board info
+ * @lmb: pointer to lmb handle, will be used for memory mgmt
+ * @kbd: double pointer to board info data
+ *
+ * boot_get_kbd() allocates space for kernel copy of board info data below
+ * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
+ * with the current u-boot board info data.
+ *
+ * returns:
+ *      0 - success
+ *     -1 - failure
+ */
+int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
+{
+	*kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
+						       sizeof(struct bd_info),
+						       0xf,
+						       env_get_bootm_mapsize() + env_get_bootm_low());
+	if (*kbd == NULL)
+		return -1;
+
+	**kbd = *(gd->bd);
+
+	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
+
+#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
+	do_bdinfo(NULL, 0, 0, NULL);
+#endif
+
+	return 0;
+}
+#endif /* CONFIG_SYS_BOOT_GET_KBD */
+
+#ifdef CONFIG_LMB
+int image_setup_linux(bootm_headers_t *images)
+{
+	ulong of_size = images->ft_len;
+	char **of_flat_tree = &images->ft_addr;
+	struct lmb *lmb = &images->lmb;
+	int ret;
+
+	if (IMAGE_ENABLE_OF_LIBFDT)
+		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
+
+	if (IMAGE_BOOT_GET_CMDLINE) {
+		ret = boot_get_cmdline(lmb, &images->cmdline_start,
+				&images->cmdline_end);
+		if (ret) {
+			puts("ERROR with allocation of cmdline\n");
+			return ret;
+		}
+	}
+
+	if (IMAGE_ENABLE_OF_LIBFDT) {
+		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
+		if (ret)
+			return ret;
+	}
+
+	if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
+		ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+#endif /* CONFIG_LMB */
diff --git a/common/image.c b/common/image.c
index 80aeb4398ec..ed7f188b591 100644
--- a/common/image.c
+++ b/common/image.c
@@ -8,15 +8,11 @@
 
 #ifndef USE_HOSTCC
 #include <common.h>
-#include <bootstage.h>
-#include <cpu_func.h>
 #include <env.h>
 #include <lmb.h>
 #include <log.h>
 #include <malloc.h>
-#include <asm/cache.h>
 #include <u-boot/crc.h>
-#include <watchdog.h>
 
 #ifdef CONFIG_SHOW_BOOT_PROGRESS
 #include <status_led.h>
@@ -24,14 +20,9 @@
 
 #include <rtc.h>
 
-#include <image.h>
-#include <mapmem.h>
-
 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
 #include <linux/libfdt.h>
 #include <fdt_support.h>
-#include <fpga.h>
-#include <xilinx.h>
 #endif
 
 #include <asm/global_data.h>
@@ -47,11 +38,6 @@ extern int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc,
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
-static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
-						int verify);
-#endif
-
 /* Set this if we have less than 4 MB of malloc() space */
 #if CONFIG_SYS_MALLOC_LEN < (4096 * 1024)
 #define CONSERVE_MEMORY		true
@@ -63,7 +49,6 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 #include "mkimage.h"
 #include <u-boot/md5.h>
 #include <time.h>
-#include <image.h>
 
 #ifndef __maybe_unused
 # define __maybe_unused		/* unimplemented */
@@ -76,6 +61,7 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 #include <abuf.h>
 #include <bzlib.h>
 #include <gzip.h>
+#include <image.h>
 #include <lz4.h>
 #include <imximage.h>
 #include <linux/lzo.h>
@@ -86,10 +72,6 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 #include <lzma/LzmaTools.h>
 #include <u-boot/crc.h>
 
-#ifndef CONFIG_SYS_BARGSIZE
-#define CONFIG_SYS_BARGSIZE 512
-#endif
-
 static const table_entry_t uimage_arch[] = {
 	{	IH_ARCH_INVALID,	"invalid",	"Invalid ARCH",	},
 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
@@ -546,180 +528,7 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 	return 0;
 }
 
-#ifndef USE_HOSTCC
-#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
-/**
- * image_get_ramdisk - get and verify ramdisk image
- * @rd_addr: ramdisk image start address
- * @arch: expected ramdisk architecture
- * @verify: checksum verification flag
- *
- * image_get_ramdisk() returns a pointer to the verified ramdisk image
- * header. Routine receives image start address and expected architecture
- * flag. Verification done covers data and header integrity and os/type/arch
- * fields checking.
- *
- * returns:
- *     pointer to a ramdisk image header, if image was found and valid
- *     otherwise, return NULL
- */
-static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
-						int verify)
-{
-	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
-
-	if (!image_check_magic(rd_hdr)) {
-		puts("Bad Magic Number\n");
-		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
-		return NULL;
-	}
-
-	if (!image_check_hcrc(rd_hdr)) {
-		puts("Bad Header Checksum\n");
-		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
-		return NULL;
-	}
-
-	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
-	image_print_contents(rd_hdr);
-
-	if (verify) {
-		puts("   Verifying Checksum ... ");
-		if (!image_check_dcrc(rd_hdr)) {
-			puts("Bad Data CRC\n");
-			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
-			return NULL;
-		}
-		puts("OK\n");
-	}
-
-	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
-
-	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
-	    !image_check_arch(rd_hdr, arch) ||
-	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
-		printf("No Linux %s Ramdisk Image\n",
-				genimg_get_arch_name(arch));
-		bootstage_error(BOOTSTAGE_ID_RAMDISK);
-		return NULL;
-	}
-
-	return rd_hdr;
-}
-#endif
-#endif /* !USE_HOSTCC */
-
-/*****************************************************************************/
-/* Shared dual-format routines */
-/*****************************************************************************/
-#ifndef USE_HOSTCC
-ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
-ulong image_save_addr;			/* Default Save Address */
-ulong image_save_size;			/* Default Save Size (in bytes) */
-
-static int on_loadaddr(const char *name, const char *value, enum env_op op,
-	int flags)
-{
-	switch (op) {
-	case env_op_create:
-	case env_op_overwrite:
-		image_load_addr = hextoul(value, NULL);
-		break;
-	default:
-		break;
-	}
-
-	return 0;
-}
-U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
-
-ulong env_get_bootm_low(void)
-{
-	char *s = env_get("bootm_low");
-	if (s) {
-		ulong tmp = hextoul(s, NULL);
-		return tmp;
-	}
-
-#if defined(CONFIG_SYS_SDRAM_BASE)
-	return CONFIG_SYS_SDRAM_BASE;
-#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE)
-	return gd->bd->bi_dram[0].start;
-#else
-	return 0;
-#endif
-}
-
-phys_size_t env_get_bootm_size(void)
-{
-	phys_size_t tmp, size;
-	phys_addr_t start;
-	char *s = env_get("bootm_size");
-	if (s) {
-		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
-		return tmp;
-	}
-
-	start = gd->ram_base;
-	size = gd->ram_size;
-
-	if (start + size > gd->ram_top)
-		size = gd->ram_top - start;
-
-	s = env_get("bootm_low");
-	if (s)
-		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
-	else
-		tmp = start;
-
-	return size - (tmp - start);
-}
-
-phys_size_t env_get_bootm_mapsize(void)
-{
-	phys_size_t tmp;
-	char *s = env_get("bootm_mapsize");
-	if (s) {
-		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
-		return tmp;
-	}
-
-#if defined(CONFIG_SYS_BOOTMAPSZ)
-	return CONFIG_SYS_BOOTMAPSZ;
-#else
-	return env_get_bootm_size();
-#endif
-}
-
-void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
-{
-	if (to == from)
-		return;
-
-#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
-	if (to > from) {
-		from += len;
-		to += len;
-	}
-	while (len > 0) {
-		size_t tail = (len > chunksz) ? chunksz : len;
-		WATCHDOG_RESET();
-		if (to > from) {
-			to -= tail;
-			from -= tail;
-		}
-		memmove(to, from, tail);
-		if (to < from) {
-			to += tail;
-			from += tail;
-		}
-		len -= tail;
-	}
-#else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
-	memmove(to, from, len);
-#endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
-}
-#else	/* USE_HOSTCC */
+#ifdef USE_HOSTCC
 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
 {
 	memmove(to, from, len);
@@ -982,733 +791,3 @@ int genimg_get_comp_id(const char *name)
 {
 	return (get_table_entry_id(uimage_comp, "Compression", name));
 }
-
-#ifndef USE_HOSTCC
-/**
- * genimg_get_kernel_addr_fit - get the real kernel address and return 2
- *                              FIT strings
- * @img_addr: a string might contain real image address
- * @fit_uname_config: double pointer to a char, will hold pointer to a
- *                    configuration unit name
- * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
- *                    name
- *
- * genimg_get_kernel_addr_fit get the real kernel start address from a string
- * which is normally the first argv of bootm/bootz
- *
- * returns:
- *     kernel start address
- */
-ulong genimg_get_kernel_addr_fit(char * const img_addr,
-			     const char **fit_uname_config,
-			     const char **fit_uname_kernel)
-{
-	ulong kernel_addr;
-
-	/* find out kernel image address */
-	if (!img_addr) {
-		kernel_addr = image_load_addr;
-		debug("*  kernel: default image load address = 0x%08lx\n",
-		      image_load_addr);
-#if CONFIG_IS_ENABLED(FIT)
-	} else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
-				  fit_uname_config)) {
-		debug("*  kernel: config '%s' from image at 0x%08lx\n",
-		      *fit_uname_config, kernel_addr);
-	} else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
-				     fit_uname_kernel)) {
-		debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
-		      *fit_uname_kernel, kernel_addr);
-#endif
-	} else {
-		kernel_addr = hextoul(img_addr, NULL);
-		debug("*  kernel: cmdline image address = 0x%08lx\n",
-		      kernel_addr);
-	}
-
-	return kernel_addr;
-}
-
-/**
- * genimg_get_kernel_addr() is the simple version of
- * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
- */
-ulong genimg_get_kernel_addr(char * const img_addr)
-{
-	const char *fit_uname_config = NULL;
-	const char *fit_uname_kernel = NULL;
-
-	return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
-					  &fit_uname_kernel);
-}
-
-/**
- * genimg_get_format - get image format type
- * @img_addr: image start address
- *
- * genimg_get_format() checks whether provided address points to a valid
- * legacy or FIT image.
- *
- * New uImage format and FDT blob are based on a libfdt. FDT blob
- * may be passed directly or embedded in a FIT image. In both situations
- * genimg_get_format() must be able to dectect libfdt header.
- *
- * returns:
- *     image format type or IMAGE_FORMAT_INVALID if no image is present
- */
-int genimg_get_format(const void *img_addr)
-{
-#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
-	const image_header_t *hdr;
-
-	hdr = (const image_header_t *)img_addr;
-	if (image_check_magic(hdr))
-		return IMAGE_FORMAT_LEGACY;
-#endif
-#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
-	if (fdt_check_header(img_addr) == 0)
-		return IMAGE_FORMAT_FIT;
-#endif
-#ifdef CONFIG_ANDROID_BOOT_IMAGE
-	if (android_image_check_header(img_addr) == 0)
-		return IMAGE_FORMAT_ANDROID;
-#endif
-
-	return IMAGE_FORMAT_INVALID;
-}
-
-/**
- * fit_has_config - check if there is a valid FIT configuration
- * @images: pointer to the bootm command headers structure
- *
- * fit_has_config() checks if there is a FIT configuration in use
- * (if FTI support is present).
- *
- * returns:
- *     0, no FIT support or no configuration found
- *     1, configuration found
- */
-int genimg_has_config(bootm_headers_t *images)
-{
-#if IMAGE_ENABLE_FIT
-	if (images->fit_uname_cfg)
-		return 1;
-#endif
-	return 0;
-}
-
-/**
- * boot_get_ramdisk - main ramdisk handling routine
- * @argc: command argument count
- * @argv: command argument list
- * @images: pointer to the bootm images structure
- * @arch: expected ramdisk architecture
- * @rd_start: pointer to a ulong variable, will hold ramdisk start address
- * @rd_end: pointer to a ulong variable, will hold ramdisk end
- *
- * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
- * Curently supported are the following ramdisk sources:
- *      - multicomponent kernel/ramdisk image,
- *      - commandline provided address of decicated ramdisk image.
- *
- * returns:
- *     0, if ramdisk image was found and valid, or skiped
- *     rd_start and rd_end are set to ramdisk start/end addresses if
- *     ramdisk image is found and valid
- *
- *     1, if ramdisk image is found but corrupted, or invalid
- *     rd_start and rd_end are set to 0 if no ramdisk exists
- */
-int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
-		     uint8_t arch, ulong *rd_start, ulong *rd_end)
-{
-	ulong rd_addr, rd_load;
-	ulong rd_data, rd_len;
-#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
-	const image_header_t *rd_hdr;
-#endif
-	void *buf;
-#ifdef CONFIG_SUPPORT_RAW_INITRD
-	char *end;
-#endif
-#if IMAGE_ENABLE_FIT
-	const char	*fit_uname_config = images->fit_uname_cfg;
-	const char	*fit_uname_ramdisk = NULL;
-	ulong		default_addr;
-	int		rd_noffset;
-#endif
-	const char *select = NULL;
-
-	*rd_start = 0;
-	*rd_end = 0;
-
-#ifdef CONFIG_ANDROID_BOOT_IMAGE
-	/*
-	 * Look for an Android boot image.
-	 */
-	buf = map_sysmem(images->os.start, 0);
-	if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
-		select = (argc == 0) ? env_get("loadaddr") : argv[0];
-#endif
-
-	if (argc >= 2)
-		select = argv[1];
-
-	/*
-	 * Look for a '-' which indicates to ignore the
-	 * ramdisk argument
-	 */
-	if (select && strcmp(select, "-") ==  0) {
-		debug("## Skipping init Ramdisk\n");
-		rd_len = rd_data = 0;
-	} else if (select || genimg_has_config(images)) {
-#if IMAGE_ENABLE_FIT
-		if (select) {
-			/*
-			 * If the init ramdisk comes from the FIT image and
-			 * the FIT image address is omitted in the command
-			 * line argument, try to use os FIT image address or
-			 * default load address.
-			 */
-			if (images->fit_uname_os)
-				default_addr = (ulong)images->fit_hdr_os;
-			else
-				default_addr = image_load_addr;
-
-			if (fit_parse_conf(select, default_addr,
-					   &rd_addr, &fit_uname_config)) {
-				debug("*  ramdisk: config '%s' from image at "
-						"0x%08lx\n",
-						fit_uname_config, rd_addr);
-			} else if (fit_parse_subimage(select, default_addr,
-						&rd_addr, &fit_uname_ramdisk)) {
-				debug("*  ramdisk: subimage '%s' from image at "
-						"0x%08lx\n",
-						fit_uname_ramdisk, rd_addr);
-			} else
-#endif
-			{
-				rd_addr = hextoul(select, NULL);
-				debug("*  ramdisk: cmdline image address = "
-						"0x%08lx\n",
-						rd_addr);
-			}
-#if IMAGE_ENABLE_FIT
-		} else {
-			/* use FIT configuration provided in first bootm
-			 * command argument. If the property is not defined,
-			 * quit silently.
-			 */
-			rd_addr = map_to_sysmem(images->fit_hdr_os);
-			rd_noffset = fit_get_node_from_config(images,
-					FIT_RAMDISK_PROP, rd_addr);
-			if (rd_noffset == -ENOENT)
-				return 0;
-			else if (rd_noffset < 0)
-				return 1;
-		}
-#endif
-
-		/*
-		 * Check if there is an initrd image at the
-		 * address provided in the second bootm argument
-		 * check image type, for FIT images get FIT node.
-		 */
-		buf = map_sysmem(rd_addr, 0);
-		switch (genimg_get_format(buf)) {
-#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
-		case IMAGE_FORMAT_LEGACY:
-			printf("## Loading init Ramdisk from Legacy "
-					"Image at %08lx ...\n", rd_addr);
-
-			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
-			rd_hdr = image_get_ramdisk(rd_addr, arch,
-							images->verify);
-
-			if (rd_hdr == NULL)
-				return 1;
-
-			rd_data = image_get_data(rd_hdr);
-			rd_len = image_get_data_size(rd_hdr);
-			rd_load = image_get_load(rd_hdr);
-			break;
-#endif
-#if IMAGE_ENABLE_FIT
-		case IMAGE_FORMAT_FIT:
-			rd_noffset = fit_image_load(images,
-					rd_addr, &fit_uname_ramdisk,
-					&fit_uname_config, arch,
-					IH_TYPE_RAMDISK,
-					BOOTSTAGE_ID_FIT_RD_START,
-					FIT_LOAD_OPTIONAL_NON_ZERO,
-					&rd_data, &rd_len);
-			if (rd_noffset < 0)
-				return 1;
-
-			images->fit_hdr_rd = map_sysmem(rd_addr, 0);
-			images->fit_uname_rd = fit_uname_ramdisk;
-			images->fit_noffset_rd = rd_noffset;
-			break;
-#endif
-#ifdef CONFIG_ANDROID_BOOT_IMAGE
-		case IMAGE_FORMAT_ANDROID:
-			android_image_get_ramdisk((void *)images->os.start,
-				&rd_data, &rd_len);
-			break;
-#endif
-		default:
-#ifdef CONFIG_SUPPORT_RAW_INITRD
-			end = NULL;
-			if (select)
-				end = strchr(select, ':');
-			if (end) {
-				rd_len = hextoul(++end, NULL);
-				rd_data = rd_addr;
-			} else
-#endif
-			{
-				puts("Wrong Ramdisk Image Format\n");
-				rd_data = rd_len = rd_load = 0;
-				return 1;
-			}
-		}
-	} else if (images->legacy_hdr_valid &&
-			image_check_type(&images->legacy_hdr_os_copy,
-						IH_TYPE_MULTI)) {
-
-		/*
-		 * Now check if we have a legacy mult-component image,
-		 * get second entry data start address and len.
-		 */
-		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
-		printf("## Loading init Ramdisk from multi component "
-				"Legacy Image at %08lx ...\n",
-				(ulong)images->legacy_hdr_os);
-
-		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
-	} else {
-		/*
-		 * no initrd image
-		 */
-		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
-		rd_len = rd_data = 0;
-	}
-
-	if (!rd_data) {
-		debug("## No init Ramdisk\n");
-	} else {
-		*rd_start = rd_data;
-		*rd_end = rd_data + rd_len;
-	}
-	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
-			*rd_start, *rd_end);
-
-	return 0;
-}
-
-#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
-/**
- * boot_ramdisk_high - relocate init ramdisk
- * @lmb: pointer to lmb handle, will be used for memory mgmt
- * @rd_data: ramdisk data start address
- * @rd_len: ramdisk data length
- * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
- *      start address (after possible relocation)
- * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
- *      end address (after possible relocation)
- *
- * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
- * variable and if requested ramdisk data is moved to a specified location.
- *
- * Initrd_start and initrd_end are set to final (after relocation) ramdisk
- * start/end addresses if ramdisk image start and len were provided,
- * otherwise set initrd_start and initrd_end set to zeros.
- *
- * returns:
- *      0 - success
- *     -1 - failure
- */
-int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
-		  ulong *initrd_start, ulong *initrd_end)
-{
-	char	*s;
-	ulong	initrd_high;
-	int	initrd_copy_to_ram = 1;
-
-	s = env_get("initrd_high");
-	if (s) {
-		/* a value of "no" or a similar string will act like 0,
-		 * turning the "load high" feature off. This is intentional.
-		 */
-		initrd_high = hextoul(s, NULL);
-		if (initrd_high == ~0)
-			initrd_copy_to_ram = 0;
-	} else {
-		initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
-	}
-
-
-	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
-			initrd_high, initrd_copy_to_ram);
-
-	if (rd_data) {
-		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
-			debug("   in-place initrd\n");
-			*initrd_start = rd_data;
-			*initrd_end = rd_data + rd_len;
-			lmb_reserve(lmb, rd_data, rd_len);
-		} else {
-			if (initrd_high)
-				*initrd_start = (ulong)lmb_alloc_base(lmb,
-						rd_len, 0x1000, initrd_high);
-			else
-				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
-								 0x1000);
-
-			if (*initrd_start == 0) {
-				puts("ramdisk - allocation error\n");
-				goto error;
-			}
-			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
-
-			*initrd_end = *initrd_start + rd_len;
-			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
-					*initrd_start, *initrd_end);
-
-			memmove_wd((void *)*initrd_start,
-					(void *)rd_data, rd_len, CHUNKSZ);
-
-#ifdef CONFIG_MP
-			/*
-			 * Ensure the image is flushed to memory to handle
-			 * AMP boot scenarios in which we might not be
-			 * HW cache coherent
-			 */
-			flush_cache((unsigned long)*initrd_start,
-				    ALIGN(rd_len, ARCH_DMA_MINALIGN));
-#endif
-			puts("OK\n");
-		}
-	} else {
-		*initrd_start = 0;
-		*initrd_end = 0;
-	}
-	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
-			*initrd_start, *initrd_end);
-
-	return 0;
-
-error:
-	return -1;
-}
-#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
-
-int boot_get_setup(bootm_headers_t *images, uint8_t arch,
-		   ulong *setup_start, ulong *setup_len)
-{
-#if IMAGE_ENABLE_FIT
-	return boot_get_setup_fit(images, arch, setup_start, setup_len);
-#else
-	return -ENOENT;
-#endif
-}
-
-#if IMAGE_ENABLE_FIT
-#if defined(CONFIG_FPGA)
-int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
-		  uint8_t arch, const ulong *ld_start, ulong * const ld_len)
-{
-	ulong tmp_img_addr, img_data, img_len;
-	void *buf;
-	int conf_noffset;
-	int fit_img_result;
-	const char *uname, *name;
-	int err;
-	int devnum = 0; /* TODO support multi fpga platforms */
-
-	/* Check to see if the images struct has a FIT configuration */
-	if (!genimg_has_config(images)) {
-		debug("## FIT configuration was not specified\n");
-		return 0;
-	}
-
-	/*
-	 * Obtain the os FIT header from the images struct
-	 */
-	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
-	buf = map_sysmem(tmp_img_addr, 0);
-	/*
-	 * Check image type. For FIT images get FIT node
-	 * and attempt to locate a generic binary.
-	 */
-	switch (genimg_get_format(buf)) {
-	case IMAGE_FORMAT_FIT:
-		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
-
-		uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
-					   NULL);
-		if (!uname) {
-			debug("## FPGA image is not specified\n");
-			return 0;
-		}
-		fit_img_result = fit_image_load(images,
-						tmp_img_addr,
-						(const char **)&uname,
-						&(images->fit_uname_cfg),
-						arch,
-						IH_TYPE_FPGA,
-						BOOTSTAGE_ID_FPGA_INIT,
-						FIT_LOAD_OPTIONAL_NON_ZERO,
-						&img_data, &img_len);
-
-		debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
-		      uname, img_data, img_len);
-
-		if (fit_img_result < 0) {
-			/* Something went wrong! */
-			return fit_img_result;
-		}
-
-		if (!fpga_is_partial_data(devnum, img_len)) {
-			name = "full";
-			err = fpga_loadbitstream(devnum, (char *)img_data,
-						 img_len, BIT_FULL);
-			if (err)
-				err = fpga_load(devnum, (const void *)img_data,
-						img_len, BIT_FULL);
-		} else {
-			name = "partial";
-			err = fpga_loadbitstream(devnum, (char *)img_data,
-						 img_len, BIT_PARTIAL);
-			if (err)
-				err = fpga_load(devnum, (const void *)img_data,
-						img_len, BIT_PARTIAL);
-		}
-
-		if (err)
-			return err;
-
-		printf("   Programming %s bitstream... OK\n", name);
-		break;
-	default:
-		printf("The given image format is not supported (corrupt?)\n");
-		return 1;
-	}
-
-	return 0;
-}
-#endif
-
-static void fit_loadable_process(uint8_t img_type,
-				 ulong img_data,
-				 ulong img_len)
-{
-	int i;
-	const unsigned int count =
-			ll_entry_count(struct fit_loadable_tbl, fit_loadable);
-	struct fit_loadable_tbl *fit_loadable_handler =
-			ll_entry_start(struct fit_loadable_tbl, fit_loadable);
-	/* For each loadable handler */
-	for (i = 0; i < count; i++, fit_loadable_handler++)
-		/* matching this type */
-		if (fit_loadable_handler->type == img_type)
-			/* call that handler with this image data */
-			fit_loadable_handler->handler(img_data, img_len);
-}
-
-int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
-		      uint8_t arch, const ulong *ld_start, ulong * const ld_len)
-{
-	/*
-	 * These variables are used to hold the current image location
-	 * in system memory.
-	 */
-	ulong tmp_img_addr;
-	/*
-	 * These two variables are requirements for fit_image_load, but
-	 * their values are not used
-	 */
-	ulong img_data, img_len;
-	void *buf;
-	int loadables_index;
-	int conf_noffset;
-	int fit_img_result;
-	const char *uname;
-	uint8_t img_type;
-
-	/* Check to see if the images struct has a FIT configuration */
-	if (!genimg_has_config(images)) {
-		debug("## FIT configuration was not specified\n");
-		return 0;
-	}
-
-	/*
-	 * Obtain the os FIT header from the images struct
-	 */
-	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
-	buf = map_sysmem(tmp_img_addr, 0);
-	/*
-	 * Check image type. For FIT images get FIT node
-	 * and attempt to locate a generic binary.
-	 */
-	switch (genimg_get_format(buf)) {
-	case IMAGE_FORMAT_FIT:
-		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
-
-		for (loadables_index = 0;
-		     uname = fdt_stringlist_get(buf, conf_noffset,
-					FIT_LOADABLE_PROP, loadables_index,
-					NULL), uname;
-		     loadables_index++)
-		{
-			fit_img_result = fit_image_load(images,
-				tmp_img_addr,
-				&uname,
-				&(images->fit_uname_cfg), arch,
-				IH_TYPE_LOADABLE,
-				BOOTSTAGE_ID_FIT_LOADABLE_START,
-				FIT_LOAD_OPTIONAL_NON_ZERO,
-				&img_data, &img_len);
-			if (fit_img_result < 0) {
-				/* Something went wrong! */
-				return fit_img_result;
-			}
-
-			fit_img_result = fit_image_get_node(buf, uname);
-			if (fit_img_result < 0) {
-				/* Something went wrong! */
-				return fit_img_result;
-			}
-			fit_img_result = fit_image_get_type(buf,
-							    fit_img_result,
-							    &img_type);
-			if (fit_img_result < 0) {
-				/* Something went wrong! */
-				return fit_img_result;
-			}
-
-			fit_loadable_process(img_type, img_data, img_len);
-		}
-		break;
-	default:
-		printf("The given image format is not supported (corrupt?)\n");
-		return 1;
-	}
-
-	return 0;
-}
-#endif
-
-#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
-/**
- * boot_get_cmdline - allocate and initialize kernel cmdline
- * @lmb: pointer to lmb handle, will be used for memory mgmt
- * @cmd_start: pointer to a ulong variable, will hold cmdline start
- * @cmd_end: pointer to a ulong variable, will hold cmdline end
- *
- * boot_get_cmdline() allocates space for kernel command line below
- * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
- * variable is present its contents is copied to allocated kernel
- * command line.
- *
- * returns:
- *      0 - success
- *     -1 - failure
- */
-int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
-{
-	char *cmdline;
-	char *s;
-
-	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
-				env_get_bootm_mapsize() + env_get_bootm_low());
-
-	if (cmdline == NULL)
-		return -1;
-
-	s = env_get("bootargs");
-	if (!s)
-		s = "";
-
-	strcpy(cmdline, s);
-
-	*cmd_start = (ulong) & cmdline[0];
-	*cmd_end = *cmd_start + strlen(cmdline);
-
-	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
-
-	return 0;
-}
-#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
-
-#ifdef CONFIG_SYS_BOOT_GET_KBD
-/**
- * boot_get_kbd - allocate and initialize kernel copy of board info
- * @lmb: pointer to lmb handle, will be used for memory mgmt
- * @kbd: double pointer to board info data
- *
- * boot_get_kbd() allocates space for kernel copy of board info data below
- * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
- * with the current u-boot board info data.
- *
- * returns:
- *      0 - success
- *     -1 - failure
- */
-int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
-{
-	*kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
-						       sizeof(struct bd_info),
-						       0xf,
-						       env_get_bootm_mapsize() + env_get_bootm_low());
-	if (*kbd == NULL)
-		return -1;
-
-	**kbd = *(gd->bd);
-
-	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
-
-#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
-	do_bdinfo(NULL, 0, 0, NULL);
-#endif
-
-	return 0;
-}
-#endif /* CONFIG_SYS_BOOT_GET_KBD */
-
-#ifdef CONFIG_LMB
-int image_setup_linux(bootm_headers_t *images)
-{
-	ulong of_size = images->ft_len;
-	char **of_flat_tree = &images->ft_addr;
-	struct lmb *lmb = &images->lmb;
-	int ret;
-
-	if (IMAGE_ENABLE_OF_LIBFDT)
-		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
-
-	if (IMAGE_BOOT_GET_CMDLINE) {
-		ret = boot_get_cmdline(lmb, &images->cmdline_start,
-				&images->cmdline_end);
-		if (ret) {
-			puts("ERROR with allocation of cmdline\n");
-			return ret;
-		}
-	}
-
-	if (IMAGE_ENABLE_OF_LIBFDT) {
-		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
-		if (ret)
-			return ret;
-	}
-
-	if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
-		ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
-		if (ret)
-			return ret;
-	}
-
-	return 0;
-}
-#endif /* CONFIG_LMB */
-#endif /* !USE_HOSTCC */
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (9 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 10/15] image: Split board code out into its own file Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 12/15] image: Split host code out into its own file Simon Glass
                   ` (3 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

Tidy up the warnings.

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

(no changes since v1)

 common/image-board.c | 142 ++++++++++++++++++++++---------------------
 1 file changed, 72 insertions(+), 70 deletions(-)

diff --git a/common/image-board.c b/common/image-board.c
index f9053552e74..15419c78c48 100644
--- a/common/image-board.c
+++ b/common/image-board.c
@@ -41,8 +41,8 @@ DECLARE_GLOBAL_DATA_PTR;
  *     pointer to a ramdisk image header, if image was found and valid
  *     otherwise, return NULL
  */
-static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
-						int verify)
+static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
+					       int verify)
 {
 	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
 
@@ -77,7 +77,7 @@ static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
 	    !image_check_arch(rd_hdr, arch) ||
 	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
 		printf("No Linux %s Ramdisk Image\n",
-				genimg_get_arch_name(arch));
+		       genimg_get_arch_name(arch));
 		bootstage_error(BOOTSTAGE_ID_RAMDISK);
 		return NULL;
 	}
@@ -94,7 +94,7 @@ ulong image_save_addr;			/* Default Save Address */
 ulong image_save_size;			/* Default Save Size (in bytes) */
 
 static int on_loadaddr(const char *name, const char *value, enum env_op op,
-	int flags)
+		       int flags)
 {
 	switch (op) {
 	case env_op_create:
@@ -112,6 +112,7 @@ U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
 ulong env_get_bootm_low(void)
 {
 	char *s = env_get("bootm_low");
+
 	if (s) {
 		ulong tmp = hextoul(s, NULL);
 		return tmp;
@@ -131,6 +132,7 @@ phys_size_t env_get_bootm_size(void)
 	phys_size_t tmp, size;
 	phys_addr_t start;
 	char *s = env_get("bootm_size");
+
 	if (s) {
 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
 		return tmp;
@@ -155,6 +157,7 @@ phys_size_t env_get_bootm_mapsize(void)
 {
 	phys_size_t tmp;
 	char *s = env_get("bootm_mapsize");
+
 	if (s) {
 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
 		return tmp;
@@ -179,6 +182,7 @@ void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
 	}
 	while (len > 0) {
 		size_t tail = (len > chunksz) ? chunksz : len;
+
 		WATCHDOG_RESET();
 		if (to > from) {
 			to -= tail;
@@ -212,8 +216,8 @@ void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
  *     kernel start address
  */
 ulong genimg_get_kernel_addr_fit(char * const img_addr,
-			     const char **fit_uname_config,
-			     const char **fit_uname_kernel)
+				 const char **fit_uname_config,
+				 const char **fit_uname_kernel)
 {
 	ulong kernel_addr;
 
@@ -319,7 +323,7 @@ int genimg_has_config(bootm_headers_t *images)
  * @rd_end: pointer to a ulong variable, will hold ramdisk end
  *
  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
- * Curently supported are the following ramdisk sources:
+ * Currently supported are the following ramdisk sources:
  *      - multicomponent kernel/ramdisk image,
  *      - commandline provided address of decicated ramdisk image.
  *
@@ -332,7 +336,7 @@ int genimg_has_config(bootm_headers_t *images)
  *     rd_start and rd_end are set to 0 if no ramdisk exists
  */
 int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
-		     uint8_t arch, ulong *rd_start, ulong *rd_end)
+		     u8 arch, ulong *rd_start, ulong *rd_end)
 {
 	ulong rd_addr, rd_load;
 	ulong rd_data, rd_len;
@@ -372,7 +376,8 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 	 */
 	if (select && strcmp(select, "-") ==  0) {
 		debug("## Skipping init Ramdisk\n");
-		rd_len = rd_data = 0;
+		rd_len = 0;
+		rd_data = 0;
 	} else if (select || genimg_has_config(images)) {
 #if IMAGE_ENABLE_FIT
 		if (select) {
@@ -389,21 +394,19 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 
 			if (fit_parse_conf(select, default_addr,
 					   &rd_addr, &fit_uname_config)) {
-				debug("*  ramdisk: config '%s' from image at "
-						"0x%08lx\n",
-						fit_uname_config, rd_addr);
+				debug("*  ramdisk: config '%s' from image at 0x%08lx\n",
+				      fit_uname_config, rd_addr);
 			} else if (fit_parse_subimage(select, default_addr,
-						&rd_addr, &fit_uname_ramdisk)) {
-				debug("*  ramdisk: subimage '%s' from image at "
-						"0x%08lx\n",
-						fit_uname_ramdisk, rd_addr);
+						      &rd_addr,
+						      &fit_uname_ramdisk)) {
+				debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
+				      fit_uname_ramdisk, rd_addr);
 			} else
 #endif
 			{
 				rd_addr = hextoul(select, NULL);
-				debug("*  ramdisk: cmdline image address = "
-						"0x%08lx\n",
-						rd_addr);
+				debug("*  ramdisk: cmdline image address = 0x%08lx\n",
+				      rd_addr);
 			}
 #if IMAGE_ENABLE_FIT
 		} else {
@@ -413,7 +416,8 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 			 */
 			rd_addr = map_to_sysmem(images->fit_hdr_os);
 			rd_noffset = fit_get_node_from_config(images,
-					FIT_RAMDISK_PROP, rd_addr);
+							      FIT_RAMDISK_PROP,
+							      rd_addr);
 			if (rd_noffset == -ENOENT)
 				return 0;
 			else if (rd_noffset < 0)
@@ -430,14 +434,14 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 		switch (genimg_get_format(buf)) {
 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
 		case IMAGE_FORMAT_LEGACY:
-			printf("## Loading init Ramdisk from Legacy "
-					"Image at %08lx ...\n", rd_addr);
+			printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
+			       rd_addr);
 
 			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
 			rd_hdr = image_get_ramdisk(rd_addr, arch,
-							images->verify);
+						   images->verify);
 
-			if (rd_hdr == NULL)
+			if (!rd_hdr)
 				return 1;
 
 			rd_data = image_get_data(rd_hdr);
@@ -448,12 +452,12 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 #if IMAGE_ENABLE_FIT
 		case IMAGE_FORMAT_FIT:
 			rd_noffset = fit_image_load(images,
-					rd_addr, &fit_uname_ramdisk,
-					&fit_uname_config, arch,
-					IH_TYPE_RAMDISK,
-					BOOTSTAGE_ID_FIT_RD_START,
-					FIT_LOAD_OPTIONAL_NON_ZERO,
-					&rd_data, &rd_len);
+						    rd_addr, &fit_uname_ramdisk,
+						    &fit_uname_config, arch,
+						    IH_TYPE_RAMDISK,
+						    BOOTSTAGE_ID_FIT_RD_START,
+						    FIT_LOAD_OPTIONAL_NON_ZERO,
+						    &rd_data, &rd_len);
 			if (rd_noffset < 0)
 				return 1;
 
@@ -465,7 +469,7 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 #ifdef CONFIG_ANDROID_BOOT_IMAGE
 		case IMAGE_FORMAT_ANDROID:
 			android_image_get_ramdisk((void *)images->os.start,
-				&rd_data, &rd_len);
+						  &rd_data, &rd_len);
 			break;
 #endif
 		default:
@@ -480,22 +484,22 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 #endif
 			{
 				puts("Wrong Ramdisk Image Format\n");
-				rd_data = rd_len = rd_load = 0;
+				rd_data = 0;
+				rd_len = 0;
+				rd_load = 0;
 				return 1;
 			}
 		}
 	} else if (images->legacy_hdr_valid &&
 			image_check_type(&images->legacy_hdr_os_copy,
-						IH_TYPE_MULTI)) {
-
+					 IH_TYPE_MULTI)) {
 		/*
 		 * Now check if we have a legacy mult-component image,
 		 * get second entry data start address and len.
 		 */
 		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
-		printf("## Loading init Ramdisk from multi component "
-				"Legacy Image at %08lx ...\n",
-				(ulong)images->legacy_hdr_os);
+		printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
+		       (ulong)images->legacy_hdr_os);
 
 		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
 	} else {
@@ -503,7 +507,8 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 		 * no initrd image
 		 */
 		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
-		rd_len = rd_data = 0;
+		rd_len = 0;
+		rd_data = 0;
 	}
 
 	if (!rd_data) {
@@ -513,7 +518,7 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
 		*rd_end = rd_data + rd_len;
 	}
 	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
-			*rd_start, *rd_end);
+	      *rd_start, *rd_end);
 
 	return 0;
 }
@@ -541,7 +546,7 @@ int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
  *     -1 - failure
  */
 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
-		  ulong *initrd_start, ulong *initrd_end)
+		      ulong *initrd_start, ulong *initrd_end)
 {
 	char	*s;
 	ulong	initrd_high;
@@ -559,9 +564,8 @@ int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
 		initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
 	}
 
-
 	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
-			initrd_high, initrd_copy_to_ram);
+	      initrd_high, initrd_copy_to_ram);
 
 	if (rd_data) {
 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
@@ -585,10 +589,10 @@ int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
 
 			*initrd_end = *initrd_start + rd_len;
 			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
-					*initrd_start, *initrd_end);
+			       *initrd_start, *initrd_end);
 
 			memmove_wd((void *)*initrd_start,
-					(void *)rd_data, rd_len, CHUNKSZ);
+				   (void *)rd_data, rd_len, CHUNKSZ);
 
 #ifdef CONFIG_MP
 			/*
@@ -606,7 +610,7 @@ int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
 		*initrd_end = 0;
 	}
 	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
-			*initrd_start, *initrd_end);
+	      *initrd_start, *initrd_end);
 
 	return 0;
 
@@ -615,7 +619,7 @@ error:
 }
 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
 
-int boot_get_setup(bootm_headers_t *images, uint8_t arch,
+int boot_get_setup(bootm_headers_t *images, u8 arch,
 		   ulong *setup_start, ulong *setup_len)
 {
 #if IMAGE_ENABLE_FIT
@@ -628,7 +632,7 @@ int boot_get_setup(bootm_headers_t *images, uint8_t arch,
 #if IMAGE_ENABLE_FIT
 #if defined(CONFIG_FPGA)
 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
-		  uint8_t arch, const ulong *ld_start, ulong * const ld_len)
+		  u8 arch, const ulong *ld_start, ulong * const ld_len)
 {
 	ulong tmp_img_addr, img_data, img_len;
 	void *buf;
@@ -666,7 +670,7 @@ int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
 		fit_img_result = fit_image_load(images,
 						tmp_img_addr,
 						(const char **)&uname,
-						&(images->fit_uname_cfg),
+						&images->fit_uname_cfg,
 						arch,
 						IH_TYPE_FPGA,
 						BOOTSTAGE_ID_FPGA_INIT,
@@ -711,7 +715,7 @@ int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
 }
 #endif
 
-static void fit_loadable_process(uint8_t img_type,
+static void fit_loadable_process(u8 img_type,
 				 ulong img_data,
 				 ulong img_len)
 {
@@ -729,7 +733,7 @@ static void fit_loadable_process(uint8_t img_type,
 }
 
 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
-		      uint8_t arch, const ulong *ld_start, ulong * const ld_len)
+		      u8 arch, const ulong *ld_start, ulong * const ld_len)
 {
 	/*
 	 * These variables are used to hold the current image location
@@ -746,7 +750,7 @@ int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
 	int conf_noffset;
 	int fit_img_result;
 	const char *uname;
-	uint8_t img_type;
+	u8 img_type;
 
 	/* Check to see if the images struct has a FIT configuration */
 	if (!genimg_has_config(images)) {
@@ -769,18 +773,16 @@ int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
 
 		for (loadables_index = 0;
 		     uname = fdt_stringlist_get(buf, conf_noffset,
-					FIT_LOADABLE_PROP, loadables_index,
-					NULL), uname;
-		     loadables_index++)
-		{
-			fit_img_result = fit_image_load(images,
-				tmp_img_addr,
-				&uname,
-				&(images->fit_uname_cfg), arch,
-				IH_TYPE_LOADABLE,
-				BOOTSTAGE_ID_FIT_LOADABLE_START,
-				FIT_LOAD_OPTIONAL_NON_ZERO,
-				&img_data, &img_len);
+						FIT_LOADABLE_PROP,
+						loadables_index, NULL), uname;
+		     loadables_index++) {
+			fit_img_result = fit_image_load(images, tmp_img_addr,
+							&uname,
+							&images->fit_uname_cfg,
+							arch, IH_TYPE_LOADABLE,
+							BOOTSTAGE_ID_FIT_LOADABLE_START,
+							FIT_LOAD_OPTIONAL_NON_ZERO,
+							&img_data, &img_len);
 			if (fit_img_result < 0) {
 				/* Something went wrong! */
 				return fit_img_result;
@@ -834,8 +836,7 @@ int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
 
 	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
 				env_get_bootm_mapsize() + env_get_bootm_low());
-
-	if (cmdline == NULL)
+	if (!cmdline)
 		return -1;
 
 	s = env_get("bootargs");
@@ -844,7 +845,7 @@ int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
 
 	strcpy(cmdline, s);
 
-	*cmd_start = (ulong) & cmdline[0];
+	*cmd_start = (ulong)cmdline;
 	*cmd_end = *cmd_start + strlen(cmdline);
 
 	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
@@ -872,11 +873,12 @@ int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
 	*kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
 						       sizeof(struct bd_info),
 						       0xf,
-						       env_get_bootm_mapsize() + env_get_bootm_low());
-	if (*kbd == NULL)
+						       env_get_bootm_mapsize() +
+						       env_get_bootm_low());
+	if (!*kbd)
 		return -1;
 
-	**kbd = *(gd->bd);
+	**kbd = *gd->bd;
 
 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
 
@@ -901,7 +903,7 @@ int image_setup_linux(bootm_headers_t *images)
 
 	if (IMAGE_BOOT_GET_CMDLINE) {
 		ret = boot_get_cmdline(lmb, &images->cmdline_start,
-				&images->cmdline_end);
+				       &images->cmdline_end);
 		if (ret) {
 			puts("ERROR with allocation of cmdline\n");
 			return ret;
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 12/15] image: Split host code out into its own file
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (10 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:38   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 13/15] image: Create a function to do manual relocation Simon Glass
                   ` (2 subsequent siblings)
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

To avoid having #ifdefs in a few functions which are completely different
in the board and host code, create a new image-host.c file.

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

(no changes since v1)

 common/image-board.c | 17 +++++++++++++++++
 common/image-host.c  | 27 +++++++++++++++++++++++++++
 common/image.c       | 38 +-------------------------------------
 tools/Makefile       |  1 +
 4 files changed, 46 insertions(+), 37 deletions(-)
 create mode 100644 common/image-host.c

diff --git a/common/image-board.c b/common/image-board.c
index 15419c78c48..d0307d7625c 100644
--- a/common/image-board.c
+++ b/common/image-board.c
@@ -15,6 +15,7 @@
 #include <fpga.h>
 #include <image.h>
 #include <mapmem.h>
+#include <rtc.h>
 #include <watchdog.h>
 #include <asm/cache.h>
 #include <asm/global_data.h>
@@ -925,3 +926,19 @@ int image_setup_linux(bootm_headers_t *images)
 	return 0;
 }
 #endif /* CONFIG_LMB */
+
+void genimg_print_size(uint32_t size)
+{
+	printf("%d Bytes = ", size);
+	print_size(size, "\n");
+}
+
+void genimg_print_time(time_t timestamp)
+{
+	struct rtc_time tm;
+
+	rtc_to_tm(timestamp, &tm);
+	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
+	       tm.tm_year, tm.tm_mon, tm.tm_mday,
+	       tm.tm_hour, tm.tm_min, tm.tm_sec);
+}
diff --git a/common/image-host.c b/common/image-host.c
new file mode 100644
index 00000000000..20a9521948b
--- /dev/null
+++ b/common/image-host.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Image code used by host tools (and not boards)
+ *
+ * (C) Copyright 2008 Semihalf
+ *
+ * (C) Copyright 2000-2006
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ */
+
+#include <time.h>
+
+void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
+{
+	memmove(to, from, len);
+}
+
+void genimg_print_size(uint32_t size)
+{
+	printf("%d Bytes = %.2f KiB = %.2f MiB\n", size, (double)size / 1.024e3,
+	       (double)size / 1.048576e6);
+}
+
+void genimg_print_time(time_t timestamp)
+{
+	printf("%s", ctime(&timestamp));
+}
diff --git a/common/image.c b/common/image.c
index ed7f188b591..3eb6a7fca1d 100644
--- a/common/image.c
+++ b/common/image.c
@@ -18,8 +18,6 @@
 #include <status_led.h>
 #endif
 
-#include <rtc.h>
-
 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
 #include <linux/libfdt.h>
 #include <fdt_support.h>
@@ -60,6 +58,7 @@ DECLARE_GLOBAL_DATA_PTR;
 
 #include <abuf.h>
 #include <bzlib.h>
+#include <display_options.h>
 #include <gzip.h>
 #include <image.h>
 #include <lz4.h>
@@ -528,41 +527,6 @@ int image_decomp(int comp, ulong load, ulong image_start, int type,
 	return 0;
 }
 
-#ifdef USE_HOSTCC
-void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
-{
-	memmove(to, from, len);
-}
-#endif /* !USE_HOSTCC */
-
-void genimg_print_size(uint32_t size)
-{
-#ifndef USE_HOSTCC
-	printf("%d Bytes = ", size);
-	print_size(size, "\n");
-#else
-	printf("%d Bytes = %.2f KiB = %.2f MiB\n",
-			size, (double)size / 1.024e3,
-			(double)size / 1.048576e6);
-#endif
-}
-
-#if IMAGE_ENABLE_TIMESTAMP
-void genimg_print_time(time_t timestamp)
-{
-#ifndef USE_HOSTCC
-	struct rtc_time tm;
-
-	rtc_to_tm(timestamp, &tm);
-	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
-			tm.tm_year, tm.tm_mon, tm.tm_mday,
-			tm.tm_hour, tm.tm_min, tm.tm_sec);
-#else
-	printf("%s", ctime(&timestamp));
-#endif
-}
-#endif
-
 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
 {
 	for (; table->id >= 0; ++table) {
diff --git a/tools/Makefile b/tools/Makefile
index 4a86321f646..999fd465316 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -113,6 +113,7 @@ dumpimage-mkimage-objs := aisimage.o \
 			lib/fdtdec_common.o \
 			lib/fdtdec.o \
 			common/image.o \
+			common/image-host.o \
 			imagetool.o \
 			imximage.o \
 			imx8image.o \
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 13/15] image: Create a function to do manual relocation
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (11 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 12/15] image: Split host code out into its own file Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:39   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 14/15] image: Avoid #ifdefs for " Simon Glass
  2021-09-25 13:03 ` [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

Rather than adding an #ifdef and open-coding this calculation, add a
helper function to handle it. Use this in the image code.

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

(no changes since v1)

 common/image.c     | 33 +++++++--------------------------
 include/relocate.h | 24 +++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/common/image.c b/common/image.c
index 3eb6a7fca1d..2f2fd052c50 100644
--- a/common/image.c
+++ b/common/image.c
@@ -63,6 +63,7 @@ DECLARE_GLOBAL_DATA_PTR;
 #include <image.h>
 #include <lz4.h>
 #include <imximage.h>
+#include <relocate.h>
 #include <linux/lzo.h>
 #include <linux/zstd.h>
 #include <linux/kconfig.h>
@@ -565,11 +566,7 @@ const char *genimg_get_cat_name(enum ih_category category, uint id)
 	entry = get_table_entry(table_info[category].table, id);
 	if (!entry)
 		return unknown_msg(category);
-#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
-	return entry->lname;
-#else
-	return entry->lname + gd->reloc_off;
-#endif
+	return manual_reloc(entry->lname);
 }
 
 /**
@@ -589,11 +586,7 @@ const char *genimg_get_cat_short_name(enum ih_category category, uint id)
 	entry = get_table_entry(table_info[category].table, id);
 	if (!entry)
 		return unknown_msg(category);
-#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
-	return entry->sname;
-#else
-	return entry->sname + gd->reloc_off;
-#endif
+	return manual_reloc(entry->sname);
 }
 
 int genimg_get_cat_count(enum ih_category category)
@@ -643,11 +636,7 @@ char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
 	table = get_table_entry(table, id);
 	if (!table)
 		return msg;
-#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
-	return table->lname;
-#else
-	return table->lname + gd->reloc_off;
-#endif
+	return manual_reloc(table->lname);
 }
 
 const char *genimg_get_os_name(uint8_t os)
@@ -677,11 +666,7 @@ static const char *genimg_get_short_name(const table_entry_t *table, int val)
 	table = get_table_entry(table, val);
 	if (!table)
 		return "unknown";
-#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
-	return table->sname;
-#else
-	return table->sname + gd->reloc_off;
-#endif
+	return manual_reloc(table->sname);
 }
 
 const char *genimg_get_type_short_name(uint8_t type)
@@ -724,12 +709,8 @@ int get_table_entry_id(const table_entry_t *table,
 	const table_entry_t *t;
 
 	for (t = table; t->id >= 0; ++t) {
-#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
-		if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
-#else
-		if (t->sname && strcasecmp(t->sname, name) == 0)
-#endif
-			return (t->id);
+		if (t->sname && !strcasecmp(manual_reloc(t->sname), name))
+			return t->id;
 	}
 	debug("Invalid %s Type: %s\n", table_name, name);
 
diff --git a/include/relocate.h b/include/relocate.h
index 9ceeecdbe71..c4fad336128 100644
--- a/include/relocate.h
+++ b/include/relocate.h
@@ -7,7 +7,11 @@
 #ifndef _RELOCATE_H_
 #define _RELOCATE_H_
 
-#include <common.h>
+#ifndef USE_HOSTCC
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+#endif
 
 /**
  * copy_uboot_to_ram() - Copy U-Boot to its new relocated position
@@ -35,4 +39,22 @@ int clear_bss(void);
  */
 int do_elf_reloc_fixups(void);
 
+/**
+ * manual_reloc() - Manually relocate a pointer if needed
+ *
+ * This is a nop in almost all cases, except for the systems with a broken gcc
+ * which need to manually relocate some things.
+ *
+ * @ptr: Pointer to relocate
+ * @return new pointer value
+ */
+static inline void *manual_reloc(void *ptr)
+{
+#ifndef USE_HOSTCC
+	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC))
+		return ptr + gd->reloc_off;
+#endif
+		return ptr;
+}
+
 #endif	/* _RELOCATE_H_ */
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 14/15] image: Avoid #ifdefs for manual relocation
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (12 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 13/15] image: Create a function to do manual relocation Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:39   ` Tom Rini
  2021-09-25 13:03 ` [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut, T Karthik Reddy

Add a macro to handle manually relocating a pointer. Update the iamge code
to use this to avoid needing #ifdefs.

This also fixes a bug where the 'done' flag was not set.

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

(no changes since v1)

 common/image-sig.c | 40 ++++++++++++++++++++++------------------
 include/relocate.h |  6 ++++++
 2 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/common/image-sig.c b/common/image-sig.c
index fa9407bb300..1aa0b586450 100644
--- a/common/image-sig.c
+++ b/common/image-sig.c
@@ -9,6 +9,7 @@
 #include <asm/global_data.h>
 DECLARE_GLOBAL_DATA_PTR;
 #include <image.h>
+#include <relocate.h>
 #include <u-boot/ecdsa.h>
 #include <u-boot/rsa.h>
 #include <u-boot/hash-checksum.h>
@@ -56,17 +57,19 @@ struct checksum_algo *image_get_checksum_algo(const char *full_name)
 	int i;
 	const char *name;
 
-#if defined(CONFIG_NEEDS_MANUAL_RELOC)
-	static bool done;
+	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
+		static bool done;
 
-	if (!done) {
-		done = true;
-		for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
-			checksum_algos[i].name += gd->reloc_off;
-			checksum_algos[i].calculate += gd->reloc_off;
+		if (!done) {
+			done = true;
+			for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
+				struct checksum_algo *algo = &checksum_algos[i];
+
+				MANUAL_RELOC(algo->name);
+				MANUAL_RELOC(algo->calculate);
+			}
 		}
 	}
-#endif
 
 	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
 		name = checksum_algos[i].name;
@@ -84,18 +87,19 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name)
 	struct crypto_algo *crypto, *end;
 	const char *name;
 
-#if defined(CONFIG_NEEDS_MANUAL_RELOC)
-	static bool done;
-
-	if (!done) {
-		crypto = ll_entry_start(struct crypto_algo, cryptos);
-		end = ll_entry_end(struct crypto_algo, cryptos);
-		for (; crypto < end; crypto++) {
-			crypto->name += gd->reloc_off;
-			crypto->verify += gd->reloc_off;
+	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
+		static bool done;
+
+		if (!done) {
+			done = true;
+			crypto = ll_entry_start(struct crypto_algo, cryptos);
+			end = ll_entry_end(struct crypto_algo, cryptos);
+			for (; crypto < end; crypto++) {
+				MANUAL_RELOC(crypto->name);
+				MANUAL_RELOC(crypto->verify);
+			}
 		}
 	}
-#endif
 
 	/* Move name to after the comma */
 	name = strchr(full_name, ',');
diff --git a/include/relocate.h b/include/relocate.h
index c4fad336128..26682da955f 100644
--- a/include/relocate.h
+++ b/include/relocate.h
@@ -57,4 +57,10 @@ static inline void *manual_reloc(void *ptr)
 		return ptr;
 }
 
+#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
+#define MANUAL_RELOC(ptr)	(ptr) = manual_reloc(ptr)
+#else
+#define MANUAL_RELOC(ptr)	(void)(ptr)
+#endif
+
 #endif	/* _RELOCATE_H_ */
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at
  2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
                   ` (13 preceding siblings ...)
  2021-09-25 13:03 ` [PATCH v4 14/15] image: Avoid #ifdefs for " Simon Glass
@ 2021-09-25 13:03 ` Simon Glass
  2021-10-09  1:39   ` Tom Rini
  14 siblings, 1 reply; 31+ messages in thread
From: Simon Glass @ 2021-09-25 13:03 UTC (permalink / raw)
  To: U-Boot Mailing List
  Cc: Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng, Tom Rini,
	Simon Glass, Joe Hershberger, Marek Vasut

Drop some more ifdefs in image-board.c and also the FPGA part of bootm.c
which calls into it.

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

Changes in v4:
- Rebase to master

Changes in v3:
- Pick up only the first part of the original v2 series

 common/bootm.c       | 16 ++++++++--------
 common/image-board.c | 11 +++--------
 2 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index ea71522d0c9..fe17d1da9e5 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -296,15 +296,15 @@ int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
 #endif
 
 #if IMAGE_ENABLE_FIT
-#if defined(CONFIG_FPGA)
-	/* find bitstreams */
-	ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
-			    NULL, NULL);
-	if (ret) {
-		printf("FPGA image is corrupted or invalid\n");
-		return 1;
+	if (IS_ENABLED(CONFIG_FPGA)) {
+		/* find bitstreams */
+		ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
+				    NULL, NULL);
+		if (ret) {
+			printf("FPGA image is corrupted or invalid\n");
+			return 1;
+		}
 	}
-#endif
 
 	/* find all of the loadables */
 	ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
diff --git a/common/image-board.c b/common/image-board.c
index d0307d7625c..c13d0493ca3 100644
--- a/common/image-board.c
+++ b/common/image-board.c
@@ -814,7 +814,6 @@ int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
 }
 #endif
 
-#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
 /**
  * boot_get_cmdline - allocate and initialize kernel cmdline
  * @lmb: pointer to lmb handle, will be used for memory mgmt
@@ -853,9 +852,7 @@ int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
 
 	return 0;
 }
-#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
 
-#ifdef CONFIG_SYS_BOOT_GET_KBD
 /**
  * boot_get_kbd - allocate and initialize kernel copy of board info
  * @lmb: pointer to lmb handle, will be used for memory mgmt
@@ -883,15 +880,14 @@ int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
 
 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
 
-#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
-	do_bdinfo(NULL, 0, 0, NULL);
+#if defined(DEBUG)
+	if (IS_ENABLED(CONFIG_CMD_BDI)
+		do_bdinfo(NULL, 0, 0, NULL);
 #endif
 
 	return 0;
 }
-#endif /* CONFIG_SYS_BOOT_GET_KBD */
 
-#ifdef CONFIG_LMB
 int image_setup_linux(bootm_headers_t *images)
 {
 	ulong of_size = images->ft_len;
@@ -925,7 +921,6 @@ int image_setup_linux(bootm_headers_t *images)
 
 	return 0;
 }
-#endif /* CONFIG_LMB */
 
 void genimg_print_size(uint32_t size)
 {
-- 
2.33.0.685.g46640cef36-goog


^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 01/15] lib: Add memdup()
  2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng

[-- Attachment #1: Type: text/plain, Size: 238 bytes --]

On Sat, Sep 25, 2021 at 07:03:06AM -0600, Simon Glass wrote:

> Add a function to duplicate a memory region, a little like strdup().
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 02/15] Add support for an owned buffer
  2021-09-25 13:03 ` [PATCH v4 02/15] Add support for an owned buffer Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng

[-- Attachment #1: Type: text/plain, Size: 1149 bytes --]

On Sat, Sep 25, 2021 at 07:03:07AM -0600, Simon Glass wrote:

> When passing a data buffer back from a function, it is not always clear
> who owns the buffer, i.e. who is responsible for freeing the memory used.
> An example of this is where multiple files are decompressed from the
> firmware image, using a temporary buffer for reading (since the
> compressed data has to live somewhere) and producing a temporary or
> permanent buffer with the resuilts.
> 
> Where the firmware image can be memory-mapped, as on x86, the compressed
> data does not need to be buffered, but the complexity of having a buffer
> which is either allocated or not, makes the code hard to understand.
> 
> Introduce a new 'abuf' which supports simple buffer operations:
> 
> - encapsulating a buffer and its size
> - either allocated with malloc() or not
> - able to be reliably freed if necessary
> - able to be converted to an allocated buffer if needed
> 
> This simple API makes it easier to deal with allocated and memory-mapped
> buffers.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 03/15] compiler: Add a comment to host_build()
  2021-09-25 13:03 ` [PATCH v4 03/15] compiler: Add a comment to host_build() Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng

[-- Attachment #1: Type: text/plain, Size: 239 bytes --]

On Sat, Sep 25, 2021 at 07:03:08AM -0600, Simon Glass wrote:

> This function should have a comment explaining what it does. Add one.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 04/15] zstd: Create a function for use from U-Boot
  2021-09-25 13:03 ` [PATCH v4 04/15] zstd: Create a function for use from U-Boot Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng

[-- Attachment #1: Type: text/plain, Size: 512 bytes --]

On Sat, Sep 25, 2021 at 07:03:09AM -0600, Simon Glass wrote:

> The existing zstd API requires the same sequence of calls to perform its
> task. Create a helper for U-Boot, to avoid code duplication, as is done
> with other compression algorithms. Make use of of this from the image
> code.
> 
> Note that the zstd code lacks a test in test/compression.c and this should
> be added by the maintainer.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 05/15] btrfs: Use U-Boot API for decompression
  2021-09-25 13:03 ` [PATCH v4 05/15] btrfs: Use U-Boot API for decompression Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Qu Wenruo

[-- Attachment #1: Type: text/plain, Size: 258 bytes --]

On Sat, Sep 25, 2021 at 07:03:10AM -0600, Simon Glass wrote:

> Use the common function to avoid code duplication.
> 
> Acked-by: Qu Wenruo <wqu@suse.com>
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 06/15] image: Avoid switch default in image_decomp()
  2021-09-25 13:03 ` [PATCH v4 06/15] image: Avoid switch default in image_decomp() Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 418 bytes --]

On Sat, Sep 25, 2021 at 07:03:11AM -0600, Simon Glass wrote:

> At present this function is full of preprocessor macros. Adjust it to
> check for an unsupported algorithm after the switch(). This will allow
> us to drop the macros.
> 
> Fix up the return-value path and an extra blank line while we are here.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 07/15] image: Update zstd to avoid reporting error twice
  2021-09-25 13:03 ` [PATCH v4 07/15] image: Update zstd to avoid reporting error twice Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 337 bytes --]

On Sat, Sep 25, 2021 at 07:03:12AM -0600, Simon Glass wrote:

> The zstd implementation prints the error in image_decomp() which is
> incorrect and does not match other algorithms. Drop this and let the
> caller report the error.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 08/15] gzip: Avoid use of u64
  2021-09-25 13:03 ` [PATCH v4 08/15] gzip: Avoid use of u64 Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc, Bin Meng

[-- Attachment #1: Type: text/plain, Size: 843 bytes --]

On Sat, Sep 25, 2021 at 07:03:13AM -0600, Simon Glass wrote:

> The gzip API uses the u64 type in it, which is not available in the host
> build. This makes it impossible to include the header file.
> 
> We could make this type available, but it seems unnecessary. Limiting the
> compression size to that of the 'unsigned long' type seems good enough. On
> 32-bit machines the limit then becomes 4GB, which likely exceeds available
> RAM anyway, therefore it should be sufficient. On 64-bit machines this is
> effectively u64 anyway.
> 
> Update the header file and implementation to use 'ulong' instead of 'u64'.
> 
> Add a definition of u32 for the cases that seem to need exactly that
> length. This should be safe enough.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs
  2021-09-25 13:03 ` [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 584 bytes --]

On Sat, Sep 25, 2021 at 07:03:14AM -0600, Simon Glass wrote:

> Adjust this function so that preprocessor macros are not needed. With
> this, the host build uses more of the same header files as the target
> build.
> 
> Rather than definining CONFIG_SYS_MALLOC_LEN, add a CONSERVE_MEMORY
> define, since that is the purpose of the value.
> 
> This appears to have no impact on code size from a spot check of a few
> boards (snow, firefly-rk3288, boston32r2el, m53menlo).
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 10/15] image: Split board code out into its own file
  2021-09-25 13:03 ` [PATCH v4 10/15] image: Split board code out into its own file Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 341 bytes --]

On Sat, Sep 25, 2021 at 07:03:15AM -0600, Simon Glass wrote:

> To avoid a large #ifdef in the image.c file, move the affected code into
> a separate file.
> 
> Avoid any style fix-ups for easier review. Those are in the next patch.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c
  2021-09-25 13:03 ` [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 191 bytes --]

On Sat, Sep 25, 2021 at 07:03:16AM -0600, Simon Glass wrote:

> Tidy up the warnings.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 12/15] image: Split host code out into its own file
  2021-09-25 13:03 ` [PATCH v4 12/15] image: Split host code out into its own file Simon Glass
@ 2021-10-09  1:38   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:38 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 306 bytes --]

On Sat, Sep 25, 2021 at 07:03:17AM -0600, Simon Glass wrote:

> To avoid having #ifdefs in a few functions which are completely different
> in the board and host code, create a new image-host.c file.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 13/15] image: Create a function to do manual relocation
  2021-09-25 13:03 ` [PATCH v4 13/15] image: Create a function to do manual relocation Simon Glass
@ 2021-10-09  1:39   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:39 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 299 bytes --]

On Sat, Sep 25, 2021 at 07:03:18AM -0600, Simon Glass wrote:

> Rather than adding an #ifdef and open-coding this calculation, add a
> helper function to handle it. Use this in the image code.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 14/15] image: Avoid #ifdefs for manual relocation
  2021-09-25 13:03 ` [PATCH v4 14/15] image: Avoid #ifdefs for " Simon Glass
@ 2021-10-09  1:39   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:39 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut, T Karthik Reddy

[-- Attachment #1: Type: text/plain, Size: 349 bytes --]

On Sat, Sep 25, 2021 at 07:03:19AM -0600, Simon Glass wrote:

> Add a macro to handle manually relocating a pointer. Update the iamge code
> to use this to avoid needing #ifdefs.
> 
> This also fixes a bug where the 'done' flag was not set.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at
  2021-09-25 13:03 ` [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass
@ 2021-10-09  1:39   ` Tom Rini
  0 siblings, 0 replies; 31+ messages in thread
From: Tom Rini @ 2021-10-09  1:39 UTC (permalink / raw)
  To: Simon Glass
  Cc: U-Boot Mailing List, Heinrich Schuchardt, Alexandru Gagniuc,
	Bin Meng, Joe Hershberger, Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 266 bytes --]

On Sat, Sep 25, 2021 at 07:03:20AM -0600, Simon Glass wrote:

> Drop some more ifdefs in image-board.c and also the FPGA part of bootm.c
> which calls into it.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2021-10-09  1:41 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-25 13:03 [PATCH v4 00/15] image: A partial series for the image clean-up Simon Glass
2021-09-25 13:03 ` [PATCH v4 01/15] lib: Add memdup() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 02/15] Add support for an owned buffer Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 03/15] compiler: Add a comment to host_build() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 04/15] zstd: Create a function for use from U-Boot Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 05/15] btrfs: Use U-Boot API for decompression Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 06/15] image: Avoid switch default in image_decomp() Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 07/15] image: Update zstd to avoid reporting error twice Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 08/15] gzip: Avoid use of u64 Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 10/15] image: Split board code out into its own file Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 12/15] image: Split host code out into its own file Simon Glass
2021-10-09  1:38   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 13/15] image: Create a function to do manual relocation Simon Glass
2021-10-09  1:39   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 14/15] image: Avoid #ifdefs for " Simon Glass
2021-10-09  1:39   ` Tom Rini
2021-09-25 13:03 ` [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at Simon Glass
2021-10-09  1:39   ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.