image: A partial series for the image clean-up
Subsystems (4)
v4 cover image: A partial series for the image clean-up
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
Signature · 1 line
2.33.0.685.g46640cef36-goog
PATCH v4 1/15 lib: Add memdup()
Add a function to duplicate a memory region, a little like strdup().
---
(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(+)
include/linux/string.h +13 -0
@@ -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);
lib/string.c +13 -0
@@ -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
test/lib/string.c +32 -0
@@ -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 );
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 01/15] lib: Add memdup()
Quoted 3 lines · Simon Glass
Add a function to duplicate a memory region, a little like
strdup().Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 2/15 Add support for an owned buffer
In reply to Simon GlassWhen 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.
---
(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
include/abuf.h new file +159 -0
@@ -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 <[email protected]> +*/ + +#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
lib/Makefile +1 -0
@@ -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
lib/abuf.c new file +109 -0
109 lines not on this page · raw message
test/lib/Makefile +1 -0
@@ -3,6 +3,7 @@ # (C) Copyright 2018 # Mario Six, Guntermann & Drunck GmbH, [email protected] 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
test/lib/abuf.c new file +344 -0
344 lines not on this page · raw message
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 02/15] Add support for an owned buffer
Quoted 18 lines · 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 withmalloc()or not
- able to be reliably freed if necessary
- able to be converted to an allocated buffer if needed
Quoted 4 lines
This simple API makes it easier to deal with allocated and memory-mapped buffers.
Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 3/15 compiler: Add a comment to host_build()
In reply to Simon GlassThis function should have a comment explaining what it does. Add one.
---
(no changes since v2)
Changes in v2:
- Correct 'hose' typo
include/compiler.h | 5 +++++ 1 file changed, 5 insertions(+)
include/compiler.h +5 -0
@@ -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;
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 03/15] compiler: Add a comment to host_build()
Quoted 3 lines · Simon Glass
This function should have a comment explaining what it does. Add one.
Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 4/15 zstd: Create a function for use from U-Boot
In reply to Simon GlassThe 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.
---
(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
common/image.c +9 -41
@@ -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 ; }
include/linux/zstd.h +11 -0
@@ -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 */
lib/zstd/Makefile +1 -1
@@ -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
lib/zstd/zstd.c new file +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 04/15] zstd: Create a function for use from U-Boot
Quoted 9 lines · 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.cand this should be added by the maintainer.Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 5/15 btrfs: Use U-Boot API for decompression
In reply to Simon GlassUse the common function to avoid code duplication.
---
(no changes since v1)
fs/btrfs/compression.c | 51 +++++------------------------------------- 1 file changed, 5 insertions(+), 46 deletions(-)
fs/btrfs/compression.c +5 -46
@@ -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)
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 05/15] btrfs: Use U-Boot API for decompression
Quoted 4 lines · Simon Glass
Use the common function to avoid code duplication.
Acked-by Qu Wenruo <[email protected]>Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 6/15 image: Avoid switch default in image_decomp()
In reply to Simon GlassAt 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.
---
(no changes since v1)
common/image.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
common/image.c +8 -6
@@ -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 )/**
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 06/15] image: Avoid switch default in image_decomp()
Quoted 7 lines · Simon Glass
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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 7/15 image: Update zstd to avoid reporting error twice
In reply to Simon GlassThe 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.
---
(no changes since v1)
common/image.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
common/image.c +3 -5
@@ -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 */
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 07/15] image: Update zstd to avoid reporting error twice
Quoted 5 lines · Simon Glass
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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 8/15 gzip: Avoid use of u64
In reply to Simon GlassThe 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.
---
(no changes since v1)
include/compiler.h | 3 +++ include/gzip.h | 8 ++++---- lib/gunzip.c | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-)
include/compiler.h +3 -0
@@ -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))
include/gzip.h +4 -4
@@ -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
lib/gunzip.c +14 -14
@@ -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 ; }
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 08/15] gzip: Avoid use of u64
Quoted 9 lines · 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.
Quoted 6 lines
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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 9/15 image: Update image_decomp() to avoid ifdefs
In reply to Simon GlassAdjust 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).
---
(no changes since v1)
common/image.c | 145 ++++++++++++++++++-------------------- configs/sandbox_defconfig | 1 + 2 files changed, 69 insertions(+), 77 deletions(-)
common/image.c +68 -77
@@ -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 ) #elseif (t->sname &&strcasecmp (t->sname, name) ==0 )
configs/sandbox_defconfig +1 -0
@@ -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
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 09/15] image: Update image_decomp() to avoid ifdefs
Quoted 11 lines · Simon Glass
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 aCONSERVE_MEMORYdefine, 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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 10/15 image: Split board code out into its own file
In reply to Simon GlassTo 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.
---
(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
common/Makefile +1 -1
@@ -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
common/image-board.c new file +925 -0
925 lines not on this page · raw message
common/image.c +2 -923
978 lines not on this page · raw message
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 10/15] image: Split board code out into its own file
Quoted 6 lines · Simon Glass
To avoid a large #ifdef in the
image.cfile, 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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 11/15 image: Fix up checkpatch warnings in image-board.c
In reply to Simon GlassTidy up the warnings.
---
(no changes since v1)
common/image-board.c | 142 ++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 70 deletions(-)
common/image-board.c +72 -70
@@ -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;
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 11/15] image: Fix up checkpatch warnings in image-board.c
Quoted 3 lines · Simon Glass
Tidy up the warnings.
Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 12/15 image: Split host code out into its own file
In reply to Simon GlassTo avoid having #ifdefs in a few functions which are completely different in the board and host code, create a new image-host.c file.
---
(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
common/image-board.c +17 -0
@@ -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); +}
common/image-host.c new file +27 -0
@@ -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, [email protected]. +*/ + +#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 (×tamp)); +}
common/image.c +1 -37
@@ -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 (×tamp)); -#endif -} -#endif -const table_entry_t *get_table_entry (const table_entry_t *table,int id) {for (; table->id >=0 ; ++table) {
tools/Makefile +1 -0
@@ -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 \
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 12/15] image: Split host code out into its own file
Quoted 4 lines · Simon Glass
To avoid having #ifdefs in a few functions which are completely different in the board and host code, create a new
image-host.cfile.Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 13/15 image: Create a function to do manual relocation
In reply to Simon GlassRather than adding an #ifdef and open-coding this calculation, add a helper function to handle it. Use this in the image code.
---
(no changes since v1)
common/image.c | 33 +++++++-------------------------- include/relocate.h | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 27 deletions(-)
common/image.c +7 -26
@@ -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);
include/relocate.h +23 -1
@@ -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_ */
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 13/15] image: Create a function to do manual relocation
Quoted 4 lines · Simon Glass
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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 14/15 image: Avoid #ifdefs for manual relocation
In reply to Simon GlassAdd 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.
---
(no changes since v1)
common/image-sig.c | 40 ++++++++++++++++++++++------------------ include/relocate.h | 6 ++++++ 2 files changed, 28 insertions(+), 18 deletions(-)
common/image-sig.c +22 -18
@@ -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,',' );
include/relocate.h +6 -0
@@ -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_ */
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 14/15] image: Avoid #ifdefs for manual relocation
Quoted 6 lines · Simon Glass
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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
PATCH v4 15/15 image: Remove ifdefs around image_setup_linux() el at
In reply to Simon GlassDrop some more ifdefs in image-board.c and also the FPGA part of bootm.c which calls into it.
---
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(-)
common/bootm.c +8 -8
@@ -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,
common/image-board.c +3 -8
@@ -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) {
Signature · 1 line
2.33.0.685.g46640cef36-goog
Re: [PATCH v4 15/15] image: Remove ifdefs around image_setup_linux() el at
Quoted 4 lines · Simon Glass
Drop some more ifdefs in
image-board.cand also the FPGA part ofbootm.cwhich calls into it.Signed-off-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom