tools: mkimage: Add Amlogic Boot Image type
cover tools: mkimage: Add Amlogic Boot Image type
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
The first patch add the new amlimage type. The second patch add a eMMC relocation workaround that can be used to generate a boot image that can be booted from both eMMC and SD-card on GXBB.
The amlimage type and the eMMC relocation workaround is loosely based on my prior work, aml_chksum, a small tool that can write boot image headers with updated offsets and checksums, see [1].
This has been tested booting a very limited U-Boot SPL on boards with e.g. S905, S922X, S905X3 and A311D SoCs.
[1] https://github.com/Kwiboo/u-boot/commit/6d0a17632922077a2e64b13ae1a6bdf0024b718f
Jonas Karlman (2):
tools: mkimage: Add Amlogic Boot Image type
tools: amlimage: Add support for GXBB eMMC header
MAINTAINERS | 1 + boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 1 + tools/amlimage-gxbb-relocate.c | 79 +++++++++ tools/amlimage.c | 283 +++++++++++++++++++++++++++++++++ 6 files changed, 366 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.c create mode 100644 tools/amlimage.c
Signature · 1 line
2.47.1
PATCH 1/2 tools: mkimage: Add Amlogic Boot Image type
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
Images can optionally be signed for secure boot scenario, however creation of signed images has not been implemented.
Example of how to use it:
# Create an amlogic boot image
tools/mkimage -T amlimage -n gxbb -d u-boot-spl.bin u-boot-amlogic.bin
# List boot image header information
tools/mkimage -l u-boot-amlogic.bin
# Extract amlogic boot image payload
tools/dumpimage -T amlimage -o bl2-payload.bin u-boot-amlogic.bin
Or with binman using something like:
binman {
u-boot-amlogic {
filename = "u-boot-amlogic.bin";
pad-byte = <0xff>;
mkimage {
filename = "bl2.bin";
args = "-n", "gxbb", "-T", "amlimage";
u-boot-spl {
};
};
};};
--- MAINTAINERS | 1 + boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 1 + tools/amlimage.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+) create mode 100644 tools/amlimage.c
MAINTAINERS +1 -0
@@ -181,6 +181,7 @@ F: drivers/video/meson/ F: drivers/watchdog/meson_gxbb_wdt.c F: include/configs/meson64.h F: include/configs/meson64_android.h +F: tools/amlimage* F: doc/board/amlogic/ N: meson
boot/image.c +1 -0
@@ -183,6 +183,7 @@ static const table_entry_t uimage_type[] = { { IH_TYPE_FDT_LEGACY, "fdt_legacy", "legacy Image with Flat Device Tree ", }, { IH_TYPE_RENESAS_SPKG, "spkgimage", "Renesas SPKG Image" }, { IH_TYPE_STARFIVE_SPL, "sfspl", "StarFive SPL Image" }, + { IH_TYPE_AMLIMAGE, "amlimage", "Amlogic Boot Image" }, { -1, "", "", }, };
include/image.h +1 -0
@@ -232,6 +232,7 @@ enum image_type_t {IH_TYPE_FDT_LEGACY ,/* Binary Flat Device Tree Blob in a Legacy Image */ IH_TYPE_RENESAS_SPKG ,/* Renesas SPKG image */ IH_TYPE_STARFIVE_SPL ,/* StarFive SPL image */ +IH_TYPE_AMLIMAGE ,/* Amlogic Boot Image */ IH_TYPE_COUNT, /* Number of image types */ };
tools/Makefile +1 -0
@@ -100,6 +100,7 @@ ROCKCHIP_OBS = generated/lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o # common objs for dumpimage and mkimage dumpimage-mkimage-objs := aisimage.o \ + amlimage.o \ atmelimage.o \ $(FIT_OBJS-y) \ $(FIT_SIG_OBJS-y) \
tools/amlimage.c new file +246 -0
246 lines not on this page · raw message
Signature · 1 line
2.47.1
Hi,
Quoted 30 lines
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
Images can optionally be signed for secure boot scenario, however creation of signed images has not been implemented.
Example of how to use it:
# Create an amlogic boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin u-boot-amlogic.bin# List boot image header information
tools/mkimage-l u-boot-amlogic.bin# Extract amlogic boot image payload
tools/dumpimage-T amlimage -o bl2-payload.bin u-boot-amlogic.binOr with binman using something like:
binman { u-boot-amlogic { filename = "u-boot-amlogic.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; };
Quoted 4 lines
};
Signed-off-by Jonas Karlman <[email protected]>---
Thanks for sending those patches upstream :) I've been using them for a while now, and they work nicely.
Cheers!
Quoted 332 lines · Jonas Karlman
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
Images can optionally be signed for secure boot scenario, however creation of signed images has not been implemented.
Example of how to use it:
# Create an amlogic boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin u-boot-amlogic.bin# List boot image header information
tools/mkimage-l u-boot-amlogic.bin# Extract amlogic boot image payload
tools/dumpimage-T amlimage -o bl2-payload.bin u-boot-amlogic.binOr with binman using something like:
binman { u-boot-amlogic { filename = "u-boot-amlogic.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; };Signed-off-by Jonas Karlman <[email protected]>--- MAINTAINERS | 1 + boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 1 + tools/amlimage.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+)create mode 100644
tools/amlimage.cMAINTAINERS +1
-0@@ -181,6 +181,7 @@ F: drivers/video/meson/ F: drivers/watchdog/meson_gxbb_wdt.c F: include/configs/meson64.h F: include/configs/meson64_android.h +F: tools/amlimage* F: doc/board/amlogic/ N: mesonboot/image.c +1
-0@@ -183,6 +183,7 @@ static const table_entry_t uimage_type[] = { { IH_TYPE_FDT_LEGACY, "fdt_legacy", "legacy Image with Flat Device Tree ", }, { IH_TYPE_RENESAS_SPKG, "spkgimage", "Renesas SPKG Image" }, { IH_TYPE_STARFIVE_SPL, "sfspl", "StarFive SPL Image" }, + { IH_TYPE_AMLIMAGE, "amlimage", "Amlogic Boot Image" }, { -1, "", "", }, };include/image.h +1
-0@@ -232,6 +232,7 @@ enum image_type_t {IH_TYPE_FDT_LEGACY ,/* Binary Flat Device Tree Blob in a Legacy Image */ IH_TYPE_RENESAS_SPKG ,/* Renesas SPKG image */ IH_TYPE_STARFIVE_SPL ,/* StarFive SPL image */ +IH_TYPE_AMLIMAGE ,/* Amlogic Boot Image */ IH_TYPE_COUNT, /* Number of image types */ };tools/Makefile +1
-0@@ -100,6 +100,7 @@ ROCKCHIP_OBS = generated/lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o # common objs for dumpimage and mkimage dumpimage-mkimage-objs := aisimage.o \ + amlimage.o \ atmelimage.o \ $(FIT_OBJS-y) \ $(FIT_SIG_OBJS-y) \tools/amlimage.c new file +236
-0@@ -0,0 +1,246 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright Contributors to the U-Boot project. + +#include "imagetool.h" +#include <u-boot/sha256.h> + +/* +* Image contain data in the following order: +* Nonce 16 byte +* Header 64 byte +* Digest 32 byte +* Padding align up to 4K +* Payload +*/ + +#define HEADER_MAGIC 0x4c4d4140/* @AML */ +#define HEADER_OFFSET 0x10/* 16 */ +#define HEADER_SIZE 0x40/* 64 */ +#define PAYLOAD_OFFSET 0x1000/* 4096 */ + +struct amlimage_header { +uint32_t magic; +uint32_t total_size; +uint8_t header_size; +uint8_t root_key_index; +uint8_t version_major; +uint8_t version_minor; +uint32_t padding1; +uint32_t digest_type; +uint32_t digest_offset; +uint32_t digest_size; +uint32_t data_offset; +uint32_t key_type; +uint32_t key_offset; +uint32_t key_size; +uint32_t data_size; +uint32_t payload_type; +uint32_t payload_offset; +uint32_t payload_size; +uint32_t padding2; +} __packed; + +struct amlimage_variant { +const char *name; +const struct amlimage_header hdr; +}; + +#define VARIANT (name, major, minor, size) \ + { name, { .magic = HEADER_MAGIC, .header_size = HEADER_SIZE, \ + .version_major = major, .version_minor = minor, \ + .payload_size = size, } } + +static const struct amlimage_variant variants[] = { +VARIANT ("gxbb" ,1 ,0 ,0xb000 ), +VARIANT ("gxl" ,1 ,1 ,0xb000 ), +VARIANT ("gxm" ,1 ,1 ,0xb000 ), +VARIANT ("axg" ,1 ,1 ,0xb000 ), +VARIANT ("g12a" ,1 ,1 ,0xf000 ), +VARIANT ("g12b" ,1 ,1 ,0xf000 ), +VARIANT ("sm1" ,1 ,1 ,0xf000 ), +}; + +static const struct amlimage_variant *amlimage_get_variant (const char *name) +{ +if (!name) +return NULL ; + +for (int i =0 ; i <ARRAY_SIZE (variants); i++) +if (!strcmp (name, variants[i].name)) +return &variants[i]; + +return NULL ; +} + +static int amlimage_check_params (struct image_tool_params *params) +{ +const struct amlimage_variant *variant = +amlimage_get_variant (params->imagename); +int datafile_size; + +if (params->lflag || params->iflag) +return EXIT_SUCCESS ; + +if (!variant) { +fprintf (stderr,"%s: unsupported image name: %s\n" , + params->cmdname, params->imagename); +exit (EXIT_FAILURE ); + } + + datafile_size =imagetool_get_filesize (params, params->datafile); +if (datafile_size <0 ) { +exit (EXIT_FAILURE ); + }else if (datafile_size > variant->hdr.payload_size) { +fprintf (stderr,"%s: datafile is too large (%#x > %#x)\n" , + params->cmdname, datafile_size, + variant->hdr.payload_size); +exit (EXIT_FAILURE ); + } + +return EXIT_SUCCESS ; +} + +static int amlimage_verify_header (unsigned char *buf,int size, +struct image_tool_params *params) +{ +const struct amlimage_header *hdr = (void *)buf +HEADER_OFFSET ; + +if (size >=HEADER_OFFSET +HEADER_SIZE +SHA256_SUM_LEN && + hdr->magic ==HEADER_MAGIC && hdr->header_size ==HEADER_SIZE ) +return 0 ; + +return -1 ; +} + +static void amlimage_print_header (const void *buf, +struct image_tool_params *params) +{ +const struct amlimage_header *hdr = buf +HEADER_OFFSET ; +uint8_t digest[SHA256_SUM_LEN ]; +sha256_context ctx; +bool valid; + +printf ("Amlogic Boot Image %" PRIu8".%" PRIu8"\n" , + hdr->version_major, hdr->version_minor); +printf ("Total size: %" PRIu32"\n" , hdr->total_size); +printf ("Digest %" PRIu32": %" PRIu32" @ 0x%" PRIx32"\n" , + hdr->digest_type, hdr->digest_size, hdr->digest_offset); +printf ("Key %" PRIu32": %" PRIu32" @ 0x%" PRIx32"\n" , + hdr->key_type, hdr->key_size, hdr->key_offset); +printf ("Payload %" PRIu32": %" PRIu32" @ 0x%" PRIx32"\n" , + hdr->payload_type, hdr->payload_size, hdr->payload_offset); + +sha256_starts (&ctx); +/* Header and data is used as input for sha256 digest */ +sha256_update (&ctx, (void *)hdr, hdr->header_size); +sha256_update (&ctx, (void *)hdr + hdr->data_offset, hdr->data_size); +sha256_finish (&ctx, digest); + + valid = !memcmp ((void *)hdr + hdr->digest_offset, + digest,SHA256_SUM_LEN ); + +printf ("Data: %" PRIu32" @ 0x%" PRIx32" - %s\n" , + hdr->data_size, hdr->data_offset, valid ?"OK" :"BAD" ); +} + +static void amlimage_set_header (void *buf,struct stat *sbuf,int ifd, +struct image_tool_params *params) +{ +struct amlimage_header *hdr = buf +HEADER_OFFSET ; +sha256_context ctx; + +/* Use header size as initial size */ + hdr->total_size = hdr->header_size; + +/* Use sha256 digest (normal boot) */ + hdr->digest_type =0 ; +/* The sha256 digest is stored directly following the header */ + hdr->digest_offset = hdr->total_size; +/* Unknown if this is used as block size instead of digest size */ + hdr->digest_size =512 ; + hdr->total_size += hdr->digest_size; + +/* Use key as padding so that payload ends up 4K aligned in TZRAM */ + hdr->key_type =0 ; + hdr->key_offset = hdr->total_size; + hdr->key_size =PAYLOAD_OFFSET -HEADER_OFFSET - hdr->key_offset; + hdr->total_size += hdr->key_size; + +/* With padding above payload will have a 0x1000 offset in TZRAM */ + hdr->payload_type =0 ; + hdr->payload_offset = hdr->total_size; +/* Payload size has already been copied from the variant header */ + hdr->total_size += hdr->payload_size; + +/* Set the data range to be used as input for sha256 digest */ + hdr->data_offset = hdr->digest_offset +SHA256_SUM_LEN ; + hdr->data_size = hdr->total_size - hdr->data_offset; + +sha256_starts (&ctx); +/* Header and data is used as input for sha256 digest */ +sha256_update (&ctx, (void *)hdr, hdr->header_size); +sha256_update (&ctx, (void *)hdr + hdr->data_offset, hdr->data_size); +/* Write sha256 digest to the 32 bytes directly following the header */ +sha256_finish (&ctx, (void *)hdr + hdr->digest_offset); +} + +static int amlimage_extract_subimage (void *buf, +struct image_tool_params *params) +{ +const struct amlimage_header *hdr = buf +HEADER_OFFSET ; + +/* Save payload as the subimage */ +return imagetool_save_subimage (params->outfile, + (ulong )hdr + hdr->payload_offset, + hdr->payload_size); +} + +static int amlimage_check_image_type (uint8_t type) +{ +if (type ==IH_TYPE_AMLIMAGE ) +return EXIT_SUCCESS ; + +return EXIT_FAILURE ; +} + +static int amlimage_vrec_header (struct image_tool_params *params, +struct image_type_params *tparams) +{ +const struct amlimage_variant *variant = +amlimage_get_variant (params->imagename); +const struct amlimage_header *hdr = &variant->hdr; + +/* Use payload offset as header size, datafile will be appended */ + tparams->header_size =PAYLOAD_OFFSET ; + + tparams->hdr =calloc (1 , tparams->header_size); +if (!tparams->hdr) { +fprintf (stderr,"%s: Can't alloc header: %s\n" , + params->cmdname,strerror (errno)); +exit (EXIT_FAILURE ); + } + +/* Start with a copy of the variant header */ +memcpy (tparams->hdr +HEADER_OFFSET , hdr, hdr->header_size); + +/* Pad up to payload size of the variant header */ +return hdr->payload_size - params->file_size; +} + +/* +* amlimage parameters +*/ +U_BOOT_IMAGE_TYPE( + amlimage, + "Amlogic Boot Image", + 0,
tools/amlimage.c line 246
Quoted 10 lines
tools/amlimage.c new file +10
-0+NULL , + amlimage_check_params, + amlimage_verify_header, + amlimage_print_header, + amlimage_set_header, + amlimage_extract_subimage, + amlimage_check_image_type, +NULL , + amlimage_vrec_header +);
Via which tree this should be merged ?
Neil
On Mon, Jan 06, 2025 at 10:27:39AM +0100, Neil Armstrong wrote:
[snip]
Quoted 4 lines
Reviewed-by Neil Armstrong <[email protected]>Via which tree this should be merged ?
Your amlogic tree when ready, thanks!
Signature · 1 line
Tom
PATCH 2/2 tools: amlimage: Add support for GXBB eMMC header
In reply to Jonas KarlmanGXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at 0x1200 offset in TZRAM to the expected offset of 0x1000, into the padding area at offset 0x200 when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.
Example of how to use it:
# Create a normal boot image
tools/mkimage -T amlimage -n gxbb -d u-boot-spl.bin bl2.bin
# Create a boot image with a special eMMC header tools/mkimage -T amlimage -n emmc -d bl2.bin bl2-emmc.bin
# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman {
multiple-images;
u-boot-gxbb-sd {
filename = "u-boot-gxbb-sd.bin";
pad-byte = <0xff>;
mkimage {
filename = "bl2.bin";
args = "-n", "gxbb", "-T", "amlimage";
u-boot-spl {
};
};
};
u-boot-gxbb-emmc {
filename = "u-boot-gxbb-emmc.bin";
pad-byte = <0xff>;
mkimage {
filename = "bl2-emmc.bin";
args = "-n", "emmc", "-T", "amlimage";
blob-ext {
filename = "bl2.bin";
}
};
};};
--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.c
tools/amlimage-gxbb-relocate.c new file +79 -0
@@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright Contributors to the U-Boot project. + +/* +* The following commands can be used to reproduce the gxbb_relocate +* byte array in amlimage.c +* +* Start U-Boot CI docker container from U-Boot source code root folder: +* docker run --rm -v $(pwd):/build -u uboot -it docker.io/trini/u-boot-gitlab-ci-runner:jammy-20240911.1-08Dec2024 +* +* Run the following commands inside the docker container: +* export PATH=/opt/gcc-13.2.0-nolibc/aarch64-linux/bin:$PATH +* cd /build/tools +* +* Generate assembly code for the c code in this file: +* aarch64-linux-gcc -nostdlib -ffreestanding -Os -S -o amlimage-gxbb-relocate.S amlimage-gxbb-relocate.c +* +* Manually remove 'mov x16, x2' and replace 'x16' with 'x2' on the last line. +* +* Compile assembly code and extract the AArch64 binary code: +* aarch64-linux-as -o amlimage-gxbb-relocate.o amlimage-gxbb-relocate.S +* aarch64-linux-objcopy -O binary -j .text amlimage-gxbb-relocate.o amlimage-gxbb-relocate.bin +* +* Print binary code as a byte array that can be copied into amlimage.c +* hexdump -ve '1/1 "0x%.2x, "' amlimage-gxbb-relocate.bin | fold -w 72 && echo +* +* Remember to update assembly code below when byte array is updated. +*/ + +#include <stdint.h> + +#define TZRAM_BASE 0xd9000000 +#define PAYLOAD_OFFSET 0x200 +#define BL2_OFFSET 0x1000 +#define BL2_BASE (void *)(TZRAM_BASE + BL2_OFFSET) +#define BL2_SIZE 0xb000 + +void _start (uint64_t x0,uint64_t x1) +{ +void (*bl2)(uint64_t ,uint64_t ) =BL2_BASE ; +uint64_t i, *dst =BL2_BASE , *src =BL2_BASE +PAYLOAD_OFFSET ; + +/* memmove payload from 0x1200 to 0x1000 offset in TZRAM */ +for (i =0 ; i <BL2_SIZE /sizeof (*src); i++) + *(dst + i) = *(src + i); + +/* goto entry point with x0 and x1 reg intact */ +bl2 (x0, x1); +} + +/* +.arch armv8-a +.file "amlimage-gxbb-relocate.c" +.text +.align 2 +.global _start +.type _start, %function +_start: +.LFB0: +.cfi_startproc +mov x2, 4608 +movk x2, 0xd900, lsl 16 +add x3, x2, 45056 +.L2: +sub x4, x2, #32768 +add x2, x2, 8 +ldr x5, [x2, -8] +str x5, [x4, 32256] +cmp x2, x3 +bne .L2 +mov x2, 4096 +movk x2, 0xd900, lsl 16 +br x2 +.cfi_endproc +.LFE0: +.size _start, .-_start +.ident "GCC: (GNU) 13.2.0" +.section .note.GNU-stack,"",@progbits +*/
tools/amlimage.c +37 -0
@@ -51,6 +51,7 @@ struct amlimage_variant { .payload_size = size, } }static const struct amlimage_variant variants[] = { +VARIANT ("emmc" ,1 ,0 ,0xb000 +PAYLOAD_OFFSET ),VARIANT ("gxbb" ,1 ,0 ,0xb000 ),VARIANT ("gxl" ,1 ,1 ,0xb000 ),VARIANT ("gxm" ,1 ,1 ,0xb000 ),
@@ -176,6 +177,17 @@ static void amlimage_set_header(void *buf, struct stat *sbuf, int ifd, hdr->data_offset = hdr->digest_offset +SHA256_SUM_LEN ; hdr->data_size = hdr->total_size - hdr->data_offset; +/* Adjust offset and size in GXBB eMMC header */ +if (!strcmp ("emmc" , params->imagename)) { + hdr->total_size -=PAYLOAD_OFFSET ; +/* Use offset to relocate code relative to eMMC header */ + hdr->payload_offset =0x400 -HEADER_OFFSET ; + hdr->payload_size = hdr->total_size - hdr->payload_offset; +/* Use 0x200 offset to exclude MBR from the data range */ + hdr->data_offset =0x200 -HEADER_OFFSET ; + hdr->data_size = hdr->total_size - hdr->data_offset; + } +sha256_starts (&ctx);/* Header and data is used as input for sha256 digest */ sha256_update (&ctx, (void *)hdr, hdr->header_size);
@@ -203,6 +215,22 @@ static int amlimage_check_image_type(uint8_t type)return EXIT_FAILURE ; } +/* +* AArch64 binary code to relocate payload when booting from eMMC on GXBB. +* +* Payload is relocated from offset 0x1200 to 0x1000 in TZRAM, similar to: +* memmove(0xd9001000, 0xd9001200, 0xb000) +* goto 0xd9001000 +* +* See amlimage-gxbb-relocate.c on how to reproduce the following byte array. +*/ +static const uint8_t gxbb_relocate[] = { +0x02 ,0x40 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x43 ,0x2c ,0x40 ,0x91 , +0x44 ,0x20 ,0x40 ,0xd1 ,0x42 ,0x20 ,0x00 ,0x91 ,0x45 ,0x80 ,0x5f ,0xf8 , +0x85 ,0x00 ,0x3f ,0xf9 ,0x5f ,0x00 ,0x03 ,0xeb ,0x61 ,0xff ,0xff ,0x54 , +0x02 ,0x00 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x40 ,0x00 ,0x1f ,0xd6 , +}; +static int amlimage_vrec_header (struct image_tool_params *params,struct image_type_params *tparams) {
@@ -213,6 +241,10 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Use payload offset as header size, datafile will be appended */ tparams->header_size =PAYLOAD_OFFSET ; +/* Only prepend 512 bytes for GXBB eMMC header */ +if (!strcmp ("emmc" , variant->name)) + tparams->header_size =0x200 ; + tparams->hdr =calloc (1 , tparams->header_size);if (!tparams->hdr) { fprintf(stderr, "%s: Can't alloc header: %s\n",
@@ -223,6 +255,11 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Start with a copy of the variant header */ memcpy (tparams->hdr +HEADER_OFFSET , hdr, hdr->header_size); +/* Insert relocate code to move payload from 0x1200 to 0x1000 on GXBB */ +if (!strcmp ("gxbb" , variant->name)) +memcpy (tparams->hdr +0x200 , + gxbb_relocate,sizeof (gxbb_relocate)); +/* Pad up to payload size of the variant header */ return hdr->payload_size - params->file_size; }
Signature · 1 line
2.47.1
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
Hi Jonas,
Quoted 63 lines · Jonas Karlman
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; };
Quoted 9 lines
};
Signed-off-by Jonas Karlman <[email protected]>--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.c
I sent [1] some years ago and would quite like to enable the odroid-c2 in my lab. Is that an older version of what you are supporting here?
I heard that there are people working on open source tools, but it has been a few years and I'd be quite OK to just use vendor tools.
For your docs, could you put them in doc/ instead?
[..]
Regards,
Simon
[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
Hi Simon,
Quoted 76 lines · Simon Glass
Hi Jonas,
Quoted 63 lines · Jonas Karlman
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; };Quoted 9 lines
};
Signed-off-by Jonas Karlman <[email protected]>--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.c
Quoted 2 lines
I sent [1] some years ago and would quite like to enable the odroid-c2 in my lab. Is that an older version of what you are supporting here?
The amlimage type in this series is only for creating an image format that the SoCs BootROM (BL1) can understand. This image type will most likely only be useful together with Ferass El Hafidi's U-Boot SPL work or for someone wanting to run bare metal code from SRAM on these SoCs.
Vendor bl2.bin blobs is based on TF-A, is already in correct image format and expect next stage payload to be signed using a Amlogic FIP format. For this you will still need to use vendor tools or open source tools like meson-tools, gxlimg or meson64-tools.
The amlogic-boot-fip repo [2] has blobs, vendor tools and Makefiles to combine the blobs with a U-Boot proper u-boot.bin to create a firmware image that should boot for a wide range of boards.
[2] https://github.com/LibreELEC/amlogic-boot-fip
Quoted 3 lines
I heard that there are people working on open source tools, but it has been a few years and I'd be quite OK to just use vendor tools.
Open-source tools exist to replace the binary-only Amlogic tools:
- https://github.com/afaerber/meson-tools (GXBB, GXL & GXM only)
- https://github.com/repk/gxlimg (GXBB, GXL, GXM & AXG only)
- https://github.com/angerman/meson64-tools (developed for G12B, should work on G12A & SM1)
Would be nice if someone would combine those into a mkimage amlfip type or similar (a different format than this amlimage format).
Quoted 2 lines
For your docs, could you put them in doc/ instead?
Not fully sure what you want me to put in doc/, please be specific. The code comments and commit message should already document as much as possible until there is upstream use of the GXBB eMMC header workaround.
At such time something like following could be documented:
Write image to SD-card or eMMC module:
dd if=u-boot-amlogic.bin of=/path/to/dev bs=512 seek=1 conv=fsync,notrunc
Write the eMMC header to eMMC module:
dd if=u-boot-gxbb-emmc.bin of=/path/to/dev bs=1 count=112 conv=fsync,notrunc
Until Ferass El Hafidi's U-Boot SPL work has made its way upstream such commands and information would purely be speculative and will be easier to document in a future series.
Regards,
Jonas
Quoted 7 lines
[..]
Regards,
Simon[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
Hi Jonas and Simon,
Quoted 79 lines · Jonas Karlman
Hi Simon,
Quoted 76 lines · Simon Glass
Hi Jonas,
Quoted 72 lines · Jonas Karlman
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; };};
Signed-off-by Jonas Karlman <[email protected]>--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.c
Quoted 7 lines
Quoted 2 lines
I sent [1] some years ago and would quite like to enable the odroid-c2 in my lab. Is that an older version of what you are supporting here?
The amlimage type in this series is only for creating an image format that the SoCs BootROM (BL1) can understand. This image type will most likely only be useful together with Ferass El Hafidi's U-Boot SPL work or for someone wanting to run bare metal code from SRAM on these SoCs.
I'm happy to announce that U-Boot SPL can now boot on gxbb and gxl SoCs, and I'll be sending some patches to upstream that work later on.
Quoted 13 lines
Vendor bl2.bin blobs is based on TF-A, is already in correct image format and expect next stage payload to be signed using a Amlogic FIP format. For this you will still need to use vendor tools or open source tools like meson-tools, gxlimg or meson64-tools.
The amlogic-boot-fip repo [2] has blobs, vendor tools and Makefiles to combine the blobs with a U-Boot proper u-boot.bin to create a firmware image that should boot for a wide range of boards.
[2] https://github.com/LibreELEC/amlogic-boot-fip
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
I heard that there are people working on open source tools, but it has been a few years and I'd be quite OK to just use vendor tools.
Open-source tools exist to replace the binary-only Amlogic tools:
- https://github.com/afaerber/meson-tools (GXBB, GXL & GXM only)
- https://github.com/repk/gxlimg (GXBB, GXL, GXM & AXG only)
- https://github.com/angerman/meson64-tools (developed for G12B, should work on G12A & SM1)
gxlimg now also supports G12A/G12B, but it does not support GXBB at the time of writing.
Quoted 16 lines
Would be nice if someone would combine those into a mkimage amlfip type or similar (a different format than this amlimage format).
Quoted 2 lines
For your docs, could you put them in doc/ instead?
Not fully sure what you want me to put in doc/, please be specific. The code comments and commit message should already document as much as possible until there is upstream use of the GXBB eMMC header workaround.
At such time something like following could be documented:
Write image to SD-card or eMMC module:
dd if=u-boot-amlogic.bin of=/path/to/dev bs=512 seek=1 conv=fsync,notrunc
Quoted 6 lines
Write the eMMC header to eMMC module:
dd if=u-boot-gxbb-emmc.bin of=/path/to/dev bs=1 count=112 conv=fsync,notruncUntil Ferass El Hafidi's U-Boot SPL work has made its way upstream such commands and information would purely be speculative and will be easier to document in a future series.
Yes, my tree has documentation on how to make use of images generated by amlimage. I believe such documentation should be documented when there actually is U-Boot SPL support as well.
Best regards.
Quoted 11 lines
Regards,
JonasQuoted 7 lines
[..]
Regards,
Simon[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
Quoted 85 lines · Ferass El Hafidi
Hi Jonas and Simon,
Quoted 82 lines · Jonas Karlman
Hi Simon,
Quoted 78 lines · Simon Glass
Hi Jonas,
Quoted 65 lines · Jonas Karlman
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; }; };Quoted 7 lines
Signed-off-by Jonas Karlman <[email protected]>--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+)create mode 100644
tools/amlimage-gxbb-relocate.cI sent [1] some years ago and would quite like to enable the odroid-c2 in my lab. Is that an older version of what you are supporting here?
Quoted 7 lines
Quoted 4 lines
The amlimage type in this series is only for creating an image format that the SoCs BootROM (BL1) can understand. This image type will most likely only be useful together with Ferass El Hafidi's U-Boot SPL work or for someone wanting to run bare metal code from SRAM on these SoCs.
I'm happy to announce that U-Boot SPL can now boot on gxbb and gxl SoCs, and I'll be sending some patches to upstream that work later on.
\o/
Quoted 17 lines
Quoted 17 lines
Vendor bl2.bin blobs is based on TF-A, is already in correct image format and expect next stage payload to be signed using a Amlogic FIP format. For this you will still need to use vendor tools or open source tools like meson-tools, gxlimg or meson64-tools.
The amlogic-boot-fip repo [2] has blobs, vendor tools and Makefiles to combine the blobs with a U-Boot proper u-boot.bin to create a firmware image that should boot for a wide range of boards.
[2] https://github.com/LibreELEC/amlogic-boot-fip
Quoted 3 lines
I heard that there are people working on open source tools, but it has been a few years and I'd be quite OK to just use vendor tools.
Quoted 7 lines
Quoted 4 lines
Open-source tools exist to replace the binary-only Amlogic tools:
- https://github.com/afaerber/meson-tools (GXBB, GXL & GXM only)
- https://github.com/repk/gxlimg (GXBB, GXL, GXM & AXG only)
- https://github.com/angerman/meson64-tools (developed for G12B, should work on G12A & SM1)gxlimg now also supports G12A/G12B, but it does not support GXBB at the time of writing.
Exact :-)
GXBB is radically different, it was heavily based on the TF-A FIP architecture, then Amlogic switched to custom headers for GXL and followings.
I would instead support a full SPL boot with associated tooling in U-Boot source an leave the legacy FIP stuff where it should be, out of tree.
Each board has basically a custom BL2 provided by Amlogic, with sometimes some custom tools, so supporting this in U-Boot would be an endless pain.
Neil
Quoted 41 lines
Quoted 17 lines
Would be nice if someone would combine those into a mkimage amlfip type or similar (a different format than this amlimage format).
Quoted 2 lines
For your docs, could you put them in doc/ instead?
Not fully sure what you want me to put in doc/, please be specific. The code comments and commit message should already document as much as possible until there is upstream use of the GXBB eMMC header workaround.
At such time something like following could be documented:
Write image to SD-card or eMMC module:
dd if=u-boot-amlogic.bin of=/path/to/dev bs=512 seek=1 conv=fsync,notruncQuoted 6 lines
Write the eMMC header to eMMC module:
dd if=u-boot-gxbb-emmc.bin of=/path/to/dev bs=1 count=112 conv=fsync,notruncUntil Ferass El Hafidi's U-Boot SPL work has made its way upstream such commands and information would purely be speculative and will be easier to document in a future series.
Yes, my tree has documentation on how to make use of images generated by amlimage. I believe such documentation should be documented when there actually is U-Boot SPL support as well.
Best regards.
Quoted 11 lines
Regards,
JonasQuoted 7 lines
[..]
Regards,
Simon[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
In reply to Jonas KarlmanQuoted 222 lines · Jonas Karlman
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; }; };Signed-off-by Jonas Karlman <[email protected]>--- tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+)create mode 100644
tools/amlimage-gxbb-relocate.ctools/amlimage-gxbb-relocate.c new file +79
-0@@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright Contributors to the U-Boot project. + +/* +* The following commands can be used to reproduce the gxbb_relocate +* byte array in amlimage.c +* +* Start U-Boot CI docker container from U-Boot source code root folder: +* docker run --rm -v $(pwd):/build -u uboot -it docker.io/trini/u-boot-gitlab-ci-runner:jammy-20240911.1-08Dec2024 +* +* Run the following commands inside the docker container: +* export PATH=/opt/gcc-13.2.0-nolibc/aarch64-linux/bin:$PATH +* cd /build/tools +* +* Generate assembly code for the c code in this file: +* aarch64-linux-gcc -nostdlib -ffreestanding -Os -S -o amlimage-gxbb-relocate.S amlimage-gxbb-relocate.c +* +* Manually remove 'mov x16, x2' and replace 'x16' with 'x2' on the last line. +* +* Compile assembly code and extract the AArch64 binary code: +* aarch64-linux-as -o amlimage-gxbb-relocate.o amlimage-gxbb-relocate.S +* aarch64-linux-objcopy -O binary -j .text amlimage-gxbb-relocate.o amlimage-gxbb-relocate.bin +* +* Print binary code as a byte array that can be copied into amlimage.c +* hexdump -ve '1/1 "0x%.2x, "' amlimage-gxbb-relocate.bin | fold -w 72 && echo +* +* Remember to update assembly code below when byte array is updated. +*/ + +#include <stdint.h> + +#define TZRAM_BASE 0xd9000000 +#define PAYLOAD_OFFSET 0x200 +#define BL2_OFFSET 0x1000 +#define BL2_BASE (void *)(TZRAM_BASE + BL2_OFFSET) +#define BL2_SIZE 0xb000 + +void _start (uint64_t x0,uint64_t x1) +{ +void (*bl2)(uint64_t ,uint64_t ) =BL2_BASE ; +uint64_t i, *dst =BL2_BASE , *src =BL2_BASE +PAYLOAD_OFFSET ; + +/* memmove payload from 0x1200 to 0x1000 offset in TZRAM */ +for (i =0 ; i <BL2_SIZE /sizeof (*src); i++) + *(dst + i) = *(src + i); + +/* goto entry point with x0 and x1 reg intact */ +bl2 (x0, x1); +} + +/* +.arch armv8-a +.file "amlimage-gxbb-relocate.c" +.text +.align 2 +.global _start +.type _start, %function +_start: +.LFB0: +.cfi_startproc +mov x2, 4608 +movk x2, 0xd900, lsl 16 +add x3, x2, 45056 +.L2: +sub x4, x2, #32768 +add x2, x2, 8 +ldr x5, [x2, -8] +str x5, [x4, 32256] +cmp x2, x3 +bne .L2 +mov x2, 4096 +movk x2, 0xd900, lsl 16 +br x2 +.cfi_endproc +.LFE0: +.size _start, .-_start +.ident "GCC: (GNU) 13.2.0" +.section .note.GNU-stack,"",@progbits +*/ tools/amlimage.c +32
-0@@ -51,6 +51,7 @@ struct amlimage_variant { .payload_size = size, } }static const struct amlimage_variant variants[] = { +VARIANT ("emmc" ,1 ,0 ,0xb000 +PAYLOAD_OFFSET ),VARIANT ("gxbb" ,1 ,0 ,0xb000 ),VARIANT ("gxl" ,1 ,1 ,0xb000 ),VARIANT ("gxm" ,1 ,1 ,0xb000 ),@@ -176,6 +177,17 @@ static void amlimage_set_header(void *buf, struct stat *sbuf, int ifd, hdr->data_offset = hdr->digest_offset +SHA256_SUM_LEN ; hdr->data_size = hdr->total_size - hdr->data_offset; +/* Adjust offset and size in GXBB eMMC header */ +if (!strcmp ("emmc" , params->imagename)) { + hdr->total_size -=PAYLOAD_OFFSET ; +/* Use offset to relocate code relative to eMMC header */ + hdr->payload_offset =0x400 -HEADER_OFFSET ; + hdr->payload_size = hdr->total_size - hdr->payload_offset; +/* Use 0x200 offset to exclude MBR from the data range */ + hdr->data_offset =0x200 -HEADER_OFFSET ; + hdr->data_size = hdr->total_size - hdr->data_offset; + } +sha256_starts (&ctx);/* Header and data is used as input for sha256 digest */ sha256_update (&ctx, (void *)hdr, hdr->header_size);@@ -203,6 +215,22 @@ static int amlimage_check_image_type(uint8_t type)return EXIT_FAILURE ; } +/* +* AArch64 binary code to relocate payload when booting from eMMC on GXBB. +* +* Payload is relocated from offset 0x1200 to 0x1000 in TZRAM, similar to: +* memmove(0xd9001000, 0xd9001200, 0xb000) +* goto 0xd9001000 +* +* See amlimage-gxbb-relocate.c on how to reproduce the following byte array. +*/ +static const uint8_t gxbb_relocate[] = { +0x02 ,0x40 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x43 ,0x2c ,0x40 ,0x91 , +0x44 ,0x20 ,0x40 ,0xd1 ,0x42 ,0x20 ,0x00 ,0x91 ,0x45 ,0x80 ,0x5f ,0xf8 , +0x85 ,0x00 ,0x3f ,0xf9 ,0x5f ,0x00 ,0x03 ,0xeb ,0x61 ,0xff ,0xff ,0x54 , +0x02 ,0x00 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x40 ,0x00 ,0x1f ,0xd6 , +}; +static int amlimage_vrec_header (struct image_tool_params *params,struct image_type_params *tparams) {@@ -213,6 +241,10 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Use payload offset as header size, datafile will be appended */ tparams->header_size =PAYLOAD_OFFSET ; +/* Only prepend 512 bytes for GXBB eMMC header */ +if (!strcmp ("emmc" , variant->name)) + tparams->header_size =0x200 ; + tparams->hdr =calloc (1 , tparams->header_size);if (!tparams->hdr) { fprintf(stderr, "%s: Can't alloc header: %s\n",@@ -223,6 +255,11 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Start with a copy of the variant header */
tools/amlimage.c static int amlimage_vrec_header(struct image_tool_params *params, · line 265
Quoted 10 lines
tools/amlimage.c +5
-0memcpy (tparams->hdr +HEADER_OFFSET , hdr, hdr->header_size); +/* Insert relocate code to move payload from 0x1200 to 0x1000 on GXBB */ +if (!strcmp ("gxbb" , variant->name)) +memcpy (tparams->hdr +0x200 , + gxbb_relocate,sizeof (gxbb_relocate)); +/* Pad up to payload size of the variant header */ return hdr->payload_size - params->file_size; }
Awesome work !
Re: [PATCH 2/2] tools: amlimage: Add support for GXBB eMMC header
In reply to Jonas KarlmanHi,
Quoted 62 lines · Jonas Karlman via groups.io
GXBB BL1 only tries to read boot image from sector 0 on eMMC and sector 1 on SD-card. GXL and newer read boot image from sector 1 on both eMMC and SD-card.
Vendor BL2 have solved the issue with different offsets by considering where BL2 was loaded from to adjust the offset where BL3 is read from.
This provide a different solution to create a boot image that can be booted from both eMMC and SD-card and where the offset for reading next stage loader can be shared for both boot options.
Inject code, that relocate the payload located at
0x1200offset in TZRAM to the expected offset of0x1000, into the padding area at offset0x200when a normal GXBB boot image is created. A special GXBB eMMC header can then be created that have the payload offset point to this relocate code, BL1 will jump to this relocate code when booted from eMMC instead of the normal payload start. One effect of this is that the payload size limit must be reduced by 512 bytes on GXBB.Example of how to use it:
# Create a normal boot image
tools/mkimage-T amlimage -n gxbb -d u-boot-spl.bin bl2.bin# Create a boot image with a special eMMC header
tools/mkimage-T amlimage -n emmc -d bl2.bin bl2-emmc.bin# Write normal boot image to sector 1 of eMMC/SD-card dd if=bl2.bin of=/path/to/dev bs=512 seek=1
# Write eMMC header, 112 bytes, to start of eMMC dd if=bl2-emmc.bin of=/path/to/dev bs=1 count=112
Or with binman using something like:
binman { multiple-images; u-boot-gxbb-sd { filename = "u-boot-gxbb-sd.bin"; pad-byte = <0xff>; mkimage { filename = "bl2.bin"; args = "-n", "gxbb", "-T", "amlimage"; u-boot-spl { }; }; }; u-boot-gxbb-emmc { filename = "u-boot-gxbb-emmc.bin"; pad-byte = <0xff>; mkimage { filename = "bl2-emmc.bin"; args = "-n", "emmc", "-T", "amlimage"; blob-ext { filename = "bl2.bin"; } }; };
Quoted 4 lines
};
Signed-off-by Jonas Karlman <[email protected]>---
Quoted 166 lines
tools/amlimage-gxbb-relocate.c | 79 ++++++++++++++++++++++++++++++++++ tools/amlimage.c | 37 ++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/amlimage-gxbb-relocate.ctools/amlimage-gxbb-relocate.c new file +79
-0@@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright Contributors to the U-Boot project. + +/* +* The following commands can be used to reproduce the gxbb_relocate +* byte array in amlimage.c +* +* Start U-Boot CI docker container from U-Boot source code root folder: +* docker run --rm -v $(pwd):/build -u uboot -it docker.io/trini/u-boot-gitlab-ci-runner:jammy-20240911.1-08Dec2024 +* +* Run the following commands inside the docker container: +* export PATH=/opt/gcc-13.2.0-nolibc/aarch64-linux/bin:$PATH +* cd /build/tools +* +* Generate assembly code for the c code in this file: +* aarch64-linux-gcc -nostdlib -ffreestanding -Os -S -o amlimage-gxbb-relocate.S amlimage-gxbb-relocate.c +* +* Manually remove 'mov x16, x2' and replace 'x16' with 'x2' on the last line. +* +* Compile assembly code and extract the AArch64 binary code: +* aarch64-linux-as -o amlimage-gxbb-relocate.o amlimage-gxbb-relocate.S +* aarch64-linux-objcopy -O binary -j .text amlimage-gxbb-relocate.o amlimage-gxbb-relocate.bin +* +* Print binary code as a byte array that can be copied into amlimage.c +* hexdump -ve '1/1 "0x%.2x, "' amlimage-gxbb-relocate.bin | fold -w 72 && echo +* +* Remember to update assembly code below when byte array is updated. +*/ + +#include <stdint.h> + +#define TZRAM_BASE 0xd9000000 +#define PAYLOAD_OFFSET 0x200 +#define BL2_OFFSET 0x1000 +#define BL2_BASE (void *)(TZRAM_BASE + BL2_OFFSET) +#define BL2_SIZE 0xb000 + +void _start (uint64_t x0,uint64_t x1) +{ +void (*bl2)(uint64_t ,uint64_t ) =BL2_BASE ; +uint64_t i, *dst =BL2_BASE , *src =BL2_BASE +PAYLOAD_OFFSET ; + +/* memmove payload from 0x1200 to 0x1000 offset in TZRAM */ +for (i =0 ; i <BL2_SIZE /sizeof (*src); i++) + *(dst + i) = *(src + i); + +/* goto entry point with x0 and x1 reg intact */ +bl2 (x0, x1); +} + +/* +.arch armv8-a +.file "amlimage-gxbb-relocate.c" +.text +.align 2 +.global _start +.type _start, %function +_start: +.LFB0: +.cfi_startproc +mov x2, 4608 +movk x2, 0xd900, lsl 16 +add x3, x2, 45056 +.L2: +sub x4, x2, #32768 +add x2, x2, 8 +ldr x5, [x2, -8] +str x5, [x4, 32256] +cmp x2, x3 +bne .L2 +mov x2, 4096 +movk x2, 0xd900, lsl 16 +br x2 +.cfi_endproc +.LFE0: +.size _start, .-_start +.ident "GCC: (GNU) 13.2.0" +.section .note.GNU-stack,"",@progbits +*/ tools/amlimage.c +37
-0@@ -51,6 +51,7 @@ struct amlimage_variant { .payload_size = size, } }static const struct amlimage_variant variants[] = { +VARIANT ("emmc" ,1 ,0 ,0xb000 +PAYLOAD_OFFSET ),VARIANT ("gxbb" ,1 ,0 ,0xb000 ),VARIANT ("gxl" ,1 ,1 ,0xb000 ),VARIANT ("gxm" ,1 ,1 ,0xb000 ),@@ -176,6 +177,17 @@ static void amlimage_set_header(void *buf, struct stat *sbuf, int ifd, hdr->data_offset = hdr->digest_offset +SHA256_SUM_LEN ; hdr->data_size = hdr->total_size - hdr->data_offset; +/* Adjust offset and size in GXBB eMMC header */ +if (!strcmp ("emmc" , params->imagename)) { + hdr->total_size -=PAYLOAD_OFFSET ; +/* Use offset to relocate code relative to eMMC header */ + hdr->payload_offset =0x400 -HEADER_OFFSET ; + hdr->payload_size = hdr->total_size - hdr->payload_offset; +/* Use 0x200 offset to exclude MBR from the data range */ + hdr->data_offset =0x200 -HEADER_OFFSET ; + hdr->data_size = hdr->total_size - hdr->data_offset; + } +sha256_starts (&ctx);/* Header and data is used as input for sha256 digest */ sha256_update (&ctx, (void *)hdr, hdr->header_size);@@ -203,6 +215,22 @@ static int amlimage_check_image_type(uint8_t type)return EXIT_FAILURE ; } +/* +* AArch64 binary code to relocate payload when booting from eMMC on GXBB. +* +* Payload is relocated from offset 0x1200 to 0x1000 in TZRAM, similar to: +* memmove(0xd9001000, 0xd9001200, 0xb000) +* goto 0xd9001000 +* +* See amlimage-gxbb-relocate.c on how to reproduce the following byte array. +*/ +static const uint8_t gxbb_relocate[] = { +0x02 ,0x40 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x43 ,0x2c ,0x40 ,0x91 , +0x44 ,0x20 ,0x40 ,0xd1 ,0x42 ,0x20 ,0x00 ,0x91 ,0x45 ,0x80 ,0x5f ,0xf8 , +0x85 ,0x00 ,0x3f ,0xf9 ,0x5f ,0x00 ,0x03 ,0xeb ,0x61 ,0xff ,0xff ,0x54 , +0x02 ,0x00 ,0x82 ,0xd2 ,0x02 ,0x20 ,0xbb ,0xf2 ,0x40 ,0x00 ,0x1f ,0xd6 , +}; +static int amlimage_vrec_header (struct image_tool_params *params,struct image_type_params *tparams) {@@ -213,6 +241,10 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Use payload offset as header size, datafile will be appended */ tparams->header_size =PAYLOAD_OFFSET ; +/* Only prepend 512 bytes for GXBB eMMC header */ +if (!strcmp ("emmc" , variant->name)) + tparams->header_size =0x200 ; + tparams->hdr =calloc (1 , tparams->header_size);if (!tparams->hdr) { fprintf(stderr, "%s: Can't alloc header: %s\n",@@ -223,6 +255,11 @@ static int amlimage_vrec_header(struct image_tool_params *params,/* Start with a copy of the variant header */ memcpy (tparams->hdr +HEADER_OFFSET , hdr, hdr->header_size); +/* Insert relocate code to move payload from 0x1200 to 0x1000 on GXBB */ +if (!strcmp ("gxbb" , variant->name)) +memcpy (tparams->hdr +0x200 , + gxbb_relocate,sizeof (gxbb_relocate)); +/* Pad up to payload size of the variant header */ return hdr->payload_size - params->file_size; }
Jonas, f_,
Quoted 7 lines · Jonas Karlman
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
The first patch add the new amlimage type. The second patch add a eMMC relocation workaround that can be used to generate a boot image that can be booted from both eMMC and SD-card on GXBB.
Quoted 8 lines
The amlimage type and the eMMC relocation workaround is loosely based on my prior work,
aml_chksum, a small tool that can write boot image headers with updated offsets and checksums, see [1].This has been tested booting a very limited U-Boot SPL on boards with e.g. S905, S922X, S905X3 and A311D SoCs.
[1] https://github.com/Kwiboo/u-boot/commit/6d0a17632922077a2e64b13ae1a6bdf0024b718f
Is this serie ready to be taken ? what's missing ?
Neil
Quoted 15 lines
Jonas Karlman (2):
tools: mkimage: Add Amlogic Boot Image type
tools: amlimage: Add support for GXBB eMMC headerMAINTAINERS | 1 + boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 1 + tools/amlimage-gxbb-relocate.c | 79 +++++++++ tools/amlimage.c | 283 +++++++++++++++++++++++++++++++++ 6 files changed, 366 insertions(+)create mode 100644
tools/amlimage-gxbb-relocate.ccreate mode 100644tools/amlimage.c
Hi Neil,
Quoted 14 lines · Neil Armstrong via groups.io
Jonas, f_,
Quoted 11 lines · Jonas Karlman
Add support for creating an Amlogic Boot Image that pass CHK in BL1 on Amlogic AArch64 SoCs.
The first patch add the new amlimage type. The second patch add a eMMC relocation workaround that can be used to generate a boot image that can be booted from both eMMC and SD-card on GXBB.
The amlimage type and the eMMC relocation workaround is loosely based on my prior work,
aml_chksum, a small tool that can write boot image headers with updated offsets and checksums, see [1].
Quoted 7 lines
Quoted 4 lines
This has been tested booting a very limited U-Boot SPL on boards with e.g. S905, S922X, S905X3 and A311D SoCs.
[1] https://github.com/Kwiboo/u-boot/commit/6d0a17632922077a2e64b13ae1a6bdf0024b718f
Is this serie ready to be taken ? what's missing ?
I suppose it is ready, but I'd wait for Jonas' confirmation on this.
Quoted 17 lines
Neil
Quoted 15 lines
Jonas Karlman (2):
tools: mkimage: Add Amlogic Boot Image type
tools: amlimage: Add support for GXBB eMMC headerMAINTAINERS | 1 + boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 1 + tools/amlimage-gxbb-relocate.c | 79 +++++++++ tools/amlimage.c | 283 +++++++++++++++++++++++++++++++++ 6 files changed, 366 insertions(+)create mode 100644
tools/amlimage-gxbb-relocate.ccreate mode 100644tools/amlimage.c