* [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support
@ 2026-08-22 8:56 Junhui Liu
2026-08-22 8:56 ` [PATCH 1/2] " Junhui Liu
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Junhui Liu @ 2026-08-22 8:56 UTC (permalink / raw)
To: u-boot
Cc: Tom Rini, Junhui Liu, Quentin Schulz, Simon Glass, Daniel Golle,
Randolph Sapp, Ilias Apalodimas, Kory Maincent, Guodong Xu,
Yixun Lan, spacemit
The SpacemiT BootROM loads a first-stage bootloader from storage or over
USB in MaskROM mode. Vendor firmware packages this bootloader as FSBL.bin.
Its headers use the magic "AIHD".
This series documents the AIHD image format and adds the "aihdimage"
mkimage type for the CRC32-protected K3 variant used in non-secure boot
mode. It provides the FSBL packaging needed for future K3 SPL support.
Select K3 with "-n k3" when creating an image.
K1 and K3 secure boot use RSA authentication and are not supported yet.
How to test
-----------
Rebuild mkimage, wrap a small UART test payload, and load it through K3
MaskROM fastboot. The payload is linked at 0xc0801000 because the BootROM
places the complete image at 0xc0800000 and enters the payload after the
4 KiB prefix. UART0 should print "Hello SpacemiT K3!" and then loop
indefinitely.
$ make tools-only_defconfig
$ make tools-only
$ cat > k3-uart.S << 'EOF'
.equ UART0_THR, 0xd4017000
.equ UART0_LSR, 0xd4017014
.section .text
.global _start
_start:
la s0, str
1:
lbu a0, (s0)
beqz a0, exit
call uart_send
addi s0, s0, 1
j 1b
exit:
j exit
uart_send:
li t0, UART0_LSR
lw t1, (t0)
andi t1, t1, 0x20
beqz t1, uart_send
li t0, UART0_THR
sw a0, (t0)
ret
.section .rodata
str:
.asciz "Hello SpacemiT K3!\n"
EOF
$ riscv64-linux-gnu-gcc -nostdlib -fno-builtin -fno-pie -no-pie -static \
-march=rv64gc -mabi=lp64 -g -Wall -Wl,--build-id=none \
-Ttext=0xc0801000 -o k3-uart.elf k3-uart.S
$ riscv64-linux-gnu-objcopy -O binary k3-uart.elf k3-uart.bin
$ ./tools/mkimage -T aihdimage -n k3 -d k3-uart.bin k3-uart-aihd.bin
Hold the FDL button, power on the board to enter MaskROM, then run
fastboot on host:
$ fastboot stage k3-uart-aihd.bin
$ fastboot continue
---
Junhui Liu (2):
tools: mkimage: add SpacemiT K3 AIHD image support
doc: board: spacemit: document the AIHD image format
boot/image.c | 1 +
doc/board/spacemit/aihd.rst | 145 +++++++++++++++++++++++++++++++
doc/board/spacemit/index.rst | 2 +-
include/image.h | 1 +
tools/Makefile | 3 +-
tools/aihdimage.c | 202 +++++++++++++++++++++++++++++++++++++++++++
6 files changed, 352 insertions(+), 2 deletions(-)
---
base-commit: 6073c36b2c8d39afe3ecc789b281667a3ddebc70
change-id: 20260820-spacemit-aihd-05c46aae886e
Best regards,
--
Junhui Liu <junhui.liu@pigmoral.tech>
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 1/2] tools: mkimage: add SpacemiT K3 AIHD image support 2026-08-22 8:56 [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support Junhui Liu @ 2026-08-22 8:56 ` Junhui Liu 2026-09-05 11:07 ` Yixun Lan 2026-08-22 8:56 ` [PATCH 2/2] doc: board: spacemit: document the AIHD image format Junhui Liu 2026-08-31 6:09 ` [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support E Shattow 2 siblings, 1 reply; 5+ messages in thread From: Junhui Liu @ 2026-08-22 8:56 UTC (permalink / raw) To: u-boot Cc: Tom Rini, Junhui Liu, Quentin Schulz, Simon Glass, Daniel Golle, Randolph Sapp, Ilias Apalodimas, Kory Maincent, Guodong Xu, Yixun Lan, spacemit The SpacemiT BootROM loads the FSBL from an AIHD image: a fixed 4 KiB header with two metadata headers and authentication information, followed by a 32-byte-aligned SPL payload and a trailing authentication area. K1 and K3 share the same layout, but use different integrity and authentication schemes. K1 requires RSA-signed images, while K3 uses CRC32 to verify images in non-secure boot mode. Add mkimage type "aihdimage" for the K3 CRC32 path. Populate both metadata headers, fill their CRC32 values and the payload CRC32, and reserve the authentication areas required by the layout. Only "-n k3" is wired up so far, but SoC differences already go through a variant table so K1 RSA signing can be added later. The AIHD header has no reliable K1/K3 identifier, so the SoC is selected with -n. dumpimage is omitted for the same reason: it cannot take -n and therefore cannot differentiate between the two variants. Only support for non-secure boot mode on the K3 SoC is implemented in this patch. K1 support and the RSA authentication scheme can be added later. Signed-off-by: Junhui Liu <junhui.liu@pigmoral.tech> --- boot/image.c | 1 + include/image.h | 1 + tools/Makefile | 3 +- tools/aihdimage.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+), 1 deletion(-) diff --git a/boot/image.c b/boot/image.c index 185d52ba492f..8dbfb677e631 100644 --- a/boot/image.c +++ b/boot/image.c @@ -179,6 +179,7 @@ static const table_entry_t uimage_type[] = { { IH_TYPE_TFA_BL31, "tfa-bl31", "TFA BL31 Image", }, { IH_TYPE_STM32IMAGE_V2, "stm32imagev2", "STMicroelectronics STM32 Image V2.0" }, { IH_TYPE_AMLIMAGE, "amlimage", "Amlogic Boot Image" }, + { IH_TYPE_AIHDIMAGE, "aihdimage", "SpacemiT AIHD Boot Image" }, { -1, "", "", }, }; diff --git a/include/image.h b/include/image.h index 6edcb1995bfe..7d101960cac6 100644 --- a/include/image.h +++ b/include/image.h @@ -235,6 +235,7 @@ enum image_type_t { IH_TYPE_TFA_BL31, /* TFA BL31 image */ IH_TYPE_STM32IMAGE_V2, /* STMicroelectronics STM32 Image V2.0 */ IH_TYPE_AMLIMAGE, /* Amlogic Boot Image */ + IH_TYPE_AIHDIMAGE, /* SpacemiT AIHD Boot Image */ IH_TYPE_COUNT, /* Number of image types */ }; diff --git a/tools/Makefile b/tools/Makefile index 535a5d51c89e..e838e16e0f05 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -110,7 +110,8 @@ KWB_IMAGE_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := kwbimage.o 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 \ +dumpimage-mkimage-objs := aihdimage.o \ + aisimage.o \ amlimage.o \ atmelimage.o \ $(FIT_OBJS-y) \ diff --git a/tools/aihdimage.c b/tools/aihdimage.c new file mode 100644 index 000000000000..e4e9ecdd97d9 --- /dev/null +++ b/tools/aihdimage.c @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2026 Junhui Liu <junhui.liu@pigmoral.tech> + * + * The SpacemiT AIHD boot image consists of a fixed image header followed by an + * aligned SPL payload and a trailing authentication area. The image header + * contains two metadata headers and areas reserved for authentication data. + * + * The image layout is: + * + * 0x000 root key + * 0x100 header0 + * 0x120 key metadata + * 0x300 OEM keys + * 0xb00 signature0 + * 0xfe0 header1 + * 0x1000 SPL payload, with its end aligned to 32 bytes + * payload end signature1 (0x100 bytes) + */ + +#include <image.h> +#include <u-boot/crc.h> +#include "imagetool.h" + +#define AIHD_MAGIC "AIHD" +#define AIHD_PAYLOAD_ALIGN 32 +#define AIHD_AUTH_SIZE 0x100 + +#define AIHD_FLAG_CRC32 (1U << 0) + +/** + * struct aihdimage_header - metadata header within an AIHD image header + * + * @magic: Magic (must be AIHD_MAGIC) + * @version: Image version used when anti-rollback is enabled by eFuse + * @secure: Version slot used when anti-rollback is enabled by eFuse + * @reserved: Reserved + * @image_size: Header0 stores the image header size + * Header1 stores the aligned payload size + * @load_addr: Unused + * @header_crc: Header0 and Header1 store the CRC32 of the preceding 24 bytes on K3 + * @image_crc: Header1 stores the CRC32 of the aligned payload on K3 + */ +struct aihdimage_header { + uint8_t magic[4]; + uint8_t version; + uint8_t secure; + uint16_t reserved; + uint64_t image_size; + uint64_t load_addr; + uint32_t header_crc; + uint32_t image_crc; +} __packed; + +/** + * struct aihdimage_image_header - fixed header area of an AIHD image + * + * @root_key: Root public key + * @header0: Image layout and load information + * @key_data: Key metadata + * @oem_keys: OEM public-key area + * @signature0: Signature of the image configuration + * @reserved: Reserved + * @header1: SPL payload information + */ +struct aihdimage_image_header { + uint8_t root_key[0x100]; + struct aihdimage_header header0; + uint8_t key_data[0x1e0]; + uint8_t oem_keys[0x800]; + uint8_t signature0[AIHD_AUTH_SIZE]; + uint8_t reserved[0x3e0]; + struct aihdimage_header header1; +} __packed; + +/** + * struct aihdimage_variant - SoC-specific AIHD properties + * + * @name: SoC name passed to mkimage with -n + * @flags: SoC-specific AIHD flags + */ +struct aihdimage_variant { + const char *name; + unsigned int flags; +}; + +static const struct aihdimage_variant aihdimage_variants[] = { + { + .name = "k3", + .flags = AIHD_FLAG_CRC32, + }, +}; + +static const struct aihdimage_variant *aihdimage_get_variant(const char *name) +{ + int i; + + if (!name || !*name) + return NULL; + + for (i = 0; i < ARRAY_SIZE(aihdimage_variants); i++) + if (!strcmp(name, aihdimage_variants[i].name)) + return &aihdimage_variants[i]; + + return NULL; +} + +static void aihdimage_init_header(struct aihdimage_header *header, uint64_t image_size, + const struct aihdimage_variant *variant) +{ + uint32_t crc; + + memcpy(header->magic, AIHD_MAGIC, sizeof(header->magic)); + + header->image_size = cpu_to_le64(image_size); + + if (variant->flags & AIHD_FLAG_CRC32) { + crc = crc32(0, (uint8_t *)header, + offsetof(struct aihdimage_header, header_crc)); + header->header_crc = cpu_to_le32(crc); + } +} + +static int aihdimage_check_params(struct image_tool_params *params) +{ + if (!aihdimage_get_variant(params->imagename)) { + fprintf(stderr, "%s: SpacemiT SoC must be specified with -n\n", + params->cmdname); + return EXIT_FAILURE; + } + + if (!params->dflag) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + +static void aihdimage_print_header(const void *buf, struct image_tool_params *params) +{ + const struct aihdimage_image_header *image_header = buf; + const struct aihdimage_header *header1 = &image_header->header1; + uint64_t payload_size = le64_to_cpu(header1->image_size); + + printf("SpacemiT AIHD Boot Image (%s)\n", params->imagename); + printf("Total Size: %llu bytes\n", + (unsigned long long)(sizeof(*image_header) + payload_size + AIHD_AUTH_SIZE)); + printf("Payload Size: %llu bytes\n", (unsigned long long)payload_size); +} + +static void aihdimage_set_header(void *buf, struct stat *sbuf, int infd, + struct image_tool_params *params) +{ + const struct aihdimage_variant *variant = aihdimage_get_variant(params->imagename); + struct aihdimage_image_header *image_header = buf; + struct aihdimage_header *header0 = &image_header->header0; + struct aihdimage_header *header1 = &image_header->header1; + size_t payload_size; + uint32_t crc; + + payload_size = sbuf->st_size - sizeof(*image_header) - AIHD_AUTH_SIZE; + + aihdimage_init_header(header0, sizeof(*image_header), variant); + aihdimage_init_header(header1, payload_size, variant); + + if (variant->flags & AIHD_FLAG_CRC32) { + crc = crc32(0, (uint8_t *)(image_header + 1), payload_size); + header1->image_crc = cpu_to_le32(crc); + } +} + +static int aihdimage_check_image_type(uint8_t type) +{ + if (type == IH_TYPE_AIHDIMAGE) + return EXIT_SUCCESS; + + return EXIT_FAILURE; +} + +static int aihdimage_vrec_header(struct image_tool_params *params, + struct image_type_params *tparams) +{ + size_t payload_size = params->file_size - tparams->header_size; + + tparams->hdr = calloc(tparams->header_size, 1); + + return ALIGN(payload_size, AIHD_PAYLOAD_ALIGN) - payload_size + AIHD_AUTH_SIZE; +} + +U_BOOT_IMAGE_TYPE( + aihdimage, + "SpacemiT AIHD Boot Image support", + sizeof(struct aihdimage_image_header), + NULL, + aihdimage_check_params, + NULL, + aihdimage_print_header, + aihdimage_set_header, + NULL, + aihdimage_check_image_type, + NULL, + aihdimage_vrec_header +); -- 2.55.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] tools: mkimage: add SpacemiT K3 AIHD image support 2026-08-22 8:56 ` [PATCH 1/2] " Junhui Liu @ 2026-09-05 11:07 ` Yixun Lan 0 siblings, 0 replies; 5+ messages in thread From: Yixun Lan @ 2026-09-05 11:07 UTC (permalink / raw) To: Junhui Liu Cc: u-boot, Tom Rini, Quentin Schulz, Simon Glass, Daniel Golle, Randolph Sapp, Ilias Apalodimas, Kory Maincent, Guodong Xu, spacemit Hi Junhui, On 16:56 Sat 22 Aug , Junhui Liu wrote: > The SpacemiT BootROM loads the FSBL from an AIHD image: a fixed 4 KiB > header with two metadata headers and authentication information, followed > by a 32-byte-aligned SPL payload and a trailing authentication area. K1 > and K3 share the same layout, but use different integrity and > authentication schemes. K1 requires RSA-signed images, while K3 uses > CRC32 to verify images in non-secure boot mode. > > Add mkimage type "aihdimage" for the K3 CRC32 path. Populate both > metadata headers, fill their CRC32 values and the payload CRC32, and > reserve the authentication areas required by the layout. Only "-n k3" is > wired up so far, but SoC differences already go through a variant table > so K1 RSA signing can be added later. > > The AIHD header has no reliable K1/K3 identifier, so the SoC is selected > with -n. dumpimage is omitted for the same reason: it cannot take -n and > therefore cannot differentiate between the two variants. > > Only support for non-secure boot mode on the K3 SoC is implemented in > this patch. K1 support and the RSA authentication scheme can be added > later. > > Signed-off-by: Junhui Liu <junhui.liu@pigmoral.tech> > --- > boot/image.c | 1 + > include/image.h | 1 + > tools/Makefile | 3 +- > tools/aihdimage.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 206 insertions(+), 1 deletion(-) > > diff --git a/boot/image.c b/boot/image.c > index 185d52ba492f..8dbfb677e631 100644 > --- a/boot/image.c > +++ b/boot/image.c > @@ -179,6 +179,7 @@ static const table_entry_t uimage_type[] = { > { IH_TYPE_TFA_BL31, "tfa-bl31", "TFA BL31 Image", }, > { IH_TYPE_STM32IMAGE_V2, "stm32imagev2", "STMicroelectronics STM32 Image V2.0" }, > { IH_TYPE_AMLIMAGE, "amlimage", "Amlogic Boot Image" }, > + { IH_TYPE_AIHDIMAGE, "aihdimage", "SpacemiT AIHD Boot Image" }, Per documentation of RISC-V toolchain's vendor prefix convention[1], I'd suggest to name it as IH_TYPE_SMTIMAGE, "smtimage", "SpacemiT Boot Image", vendor pick "AIHD" as magnic number but has no speficial meaning, while using "smt" as the prefix would be better to recognize the format Plus you may also need to adapt the commit message and code accordingly.. Link: https://github.com/riscv-non-isa/riscv-toolchain-conventions/blob/main/src/toolchain-conventions.adoc#list-of-vendor-prefixes [1] > { -1, "", "", }, > }; > > diff --git a/include/image.h b/include/image.h > index 6edcb1995bfe..7d101960cac6 100644 > --- a/include/image.h > +++ b/include/image.h > @@ -235,6 +235,7 @@ enum image_type_t { > IH_TYPE_TFA_BL31, /* TFA BL31 image */ > IH_TYPE_STM32IMAGE_V2, /* STMicroelectronics STM32 Image V2.0 */ > IH_TYPE_AMLIMAGE, /* Amlogic Boot Image */ > + IH_TYPE_AIHDIMAGE, /* SpacemiT AIHD Boot Image */ > ditto > IH_TYPE_COUNT, /* Number of image types */ > }; > diff --git a/tools/Makefile b/tools/Makefile > index 535a5d51c89e..e838e16e0f05 100644 > --- a/tools/Makefile > +++ b/tools/Makefile > @@ -110,7 +110,8 @@ KWB_IMAGE_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := kwbimage.o > 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 \ > +dumpimage-mkimage-objs := aihdimage.o \ > + aisimage.o \ > amlimage.o \ > atmelimage.o \ > $(FIT_OBJS-y) \ > diff --git a/tools/aihdimage.c b/tools/aihdimage.c > new file mode 100644 > index 000000000000..e4e9ecdd97d9 > --- /dev/null > +++ b/tools/aihdimage.c > @@ -0,0 +1,202 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (C) 2026 Junhui Liu <junhui.liu@pigmoral.tech> > + * > + * The SpacemiT AIHD boot image consists of a fixed image header followed by an > + * aligned SPL payload and a trailing authentication area. The image header > + * contains two metadata headers and areas reserved for authentication data. > + * > + * The image layout is: > + * > + * 0x000 root key > + * 0x100 header0 > + * 0x120 key metadata > + * 0x300 OEM keys > + * 0xb00 signature0 > + * 0xfe0 header1 > + * 0x1000 SPL payload, with its end aligned to 32 bytes > + * payload end signature1 (0x100 bytes) > + */ > + > +#include <image.h> > +#include <u-boot/crc.h> > +#include "imagetool.h" > + > +#define AIHD_MAGIC "AIHD" > +#define AIHD_PAYLOAD_ALIGN 32 > +#define AIHD_AUTH_SIZE 0x100 > + > +#define AIHD_FLAG_CRC32 (1U << 0) > + > +/** > + * struct aihdimage_header - metadata header within an AIHD image header > + * > + * @magic: Magic (must be AIHD_MAGIC) > + * @version: Image version used when anti-rollback is enabled by eFuse > + * @secure: Version slot used when anti-rollback is enabled by eFuse > + * @reserved: Reserved > + * @image_size: Header0 stores the image header size > + * Header1 stores the aligned payload size > + * @load_addr: Unused > + * @header_crc: Header0 and Header1 store the CRC32 of the preceding 24 bytes on K3 > + * @image_crc: Header1 stores the CRC32 of the aligned payload on K3 > + */ > +struct aihdimage_header { > + uint8_t magic[4]; > + uint8_t version; > + uint8_t secure; > + uint16_t reserved; > + uint64_t image_size; > + uint64_t load_addr; > + uint32_t header_crc; > + uint32_t image_crc; > +} __packed; > + > +/** > + * struct aihdimage_image_header - fixed header area of an AIHD image > + * > + * @root_key: Root public key > + * @header0: Image layout and load information > + * @key_data: Key metadata > + * @oem_keys: OEM public-key area > + * @signature0: Signature of the image configuration > + * @reserved: Reserved > + * @header1: SPL payload information > + */ > +struct aihdimage_image_header { > + uint8_t root_key[0x100]; > + struct aihdimage_header header0; > + uint8_t key_data[0x1e0]; > + uint8_t oem_keys[0x800]; > + uint8_t signature0[AIHD_AUTH_SIZE]; > + uint8_t reserved[0x3e0]; > + struct aihdimage_header header1; > +} __packed; > + > +/** > + * struct aihdimage_variant - SoC-specific AIHD properties > + * > + * @name: SoC name passed to mkimage with -n > + * @flags: SoC-specific AIHD flags > + */ > +struct aihdimage_variant { > + const char *name; > + unsigned int flags; > +}; > + > +static const struct aihdimage_variant aihdimage_variants[] = { > + { > + .name = "k3", > + .flags = AIHD_FLAG_CRC32, > + }, > +}; > + > +static const struct aihdimage_variant *aihdimage_get_variant(const char *name) > +{ > + int i; > + > + if (!name || !*name) > + return NULL; > + > + for (i = 0; i < ARRAY_SIZE(aihdimage_variants); i++) > + if (!strcmp(name, aihdimage_variants[i].name)) > + return &aihdimage_variants[i]; > + > + return NULL; > +} > + > +static void aihdimage_init_header(struct aihdimage_header *header, uint64_t image_size, > + const struct aihdimage_variant *variant) > +{ > + uint32_t crc; > + > + memcpy(header->magic, AIHD_MAGIC, sizeof(header->magic)); > + > + header->image_size = cpu_to_le64(image_size); > + > + if (variant->flags & AIHD_FLAG_CRC32) { > + crc = crc32(0, (uint8_t *)header, > + offsetof(struct aihdimage_header, header_crc)); > + header->header_crc = cpu_to_le32(crc); > + } > +} > + > +static int aihdimage_check_params(struct image_tool_params *params) > +{ > + if (!aihdimage_get_variant(params->imagename)) { > + fprintf(stderr, "%s: SpacemiT SoC must be specified with -n\n", > + params->cmdname); > + return EXIT_FAILURE; > + } > + > + if (!params->dflag) > + return EXIT_FAILURE; > + > + return EXIT_SUCCESS; > +} > + > +static void aihdimage_print_header(const void *buf, struct image_tool_params *params) > +{ > + const struct aihdimage_image_header *image_header = buf; > + const struct aihdimage_header *header1 = &image_header->header1; > + uint64_t payload_size = le64_to_cpu(header1->image_size); > + > + printf("SpacemiT AIHD Boot Image (%s)\n", params->imagename); > + printf("Total Size: %llu bytes\n", > + (unsigned long long)(sizeof(*image_header) + payload_size + AIHD_AUTH_SIZE)); > + printf("Payload Size: %llu bytes\n", (unsigned long long)payload_size); > +} > + > +static void aihdimage_set_header(void *buf, struct stat *sbuf, int infd, > + struct image_tool_params *params) > +{ > + const struct aihdimage_variant *variant = aihdimage_get_variant(params->imagename); > + struct aihdimage_image_header *image_header = buf; > + struct aihdimage_header *header0 = &image_header->header0; > + struct aihdimage_header *header1 = &image_header->header1; > + size_t payload_size; > + uint32_t crc; > + > + payload_size = sbuf->st_size - sizeof(*image_header) - AIHD_AUTH_SIZE; > + > + aihdimage_init_header(header0, sizeof(*image_header), variant); > + aihdimage_init_header(header1, payload_size, variant); > + > + if (variant->flags & AIHD_FLAG_CRC32) { > + crc = crc32(0, (uint8_t *)(image_header + 1), payload_size); > + header1->image_crc = cpu_to_le32(crc); > + } > +} > + > +static int aihdimage_check_image_type(uint8_t type) > +{ > + if (type == IH_TYPE_AIHDIMAGE) > + return EXIT_SUCCESS; > + > + return EXIT_FAILURE; > +} > + > +static int aihdimage_vrec_header(struct image_tool_params *params, > + struct image_type_params *tparams) > +{ > + size_t payload_size = params->file_size - tparams->header_size; > + > + tparams->hdr = calloc(tparams->header_size, 1); > + > + return ALIGN(payload_size, AIHD_PAYLOAD_ALIGN) - payload_size + AIHD_AUTH_SIZE; > +} > + .. > +U_BOOT_IMAGE_TYPE( > + aihdimage, > + "SpacemiT AIHD Boot Image support", > + sizeof(struct aihdimage_image_header), > + NULL, > + aihdimage_check_params, > + NULL, > + aihdimage_print_header, > + aihdimage_set_header, > + NULL, > + aihdimage_check_image_type, > + NULL, > + aihdimage_vrec_header > +); > ditto > -- > 2.55.0 > -- Yixun Lan (dlan) ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] doc: board: spacemit: document the AIHD image format 2026-08-22 8:56 [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support Junhui Liu 2026-08-22 8:56 ` [PATCH 1/2] " Junhui Liu @ 2026-08-22 8:56 ` Junhui Liu 2026-08-31 6:09 ` [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support E Shattow 2 siblings, 0 replies; 5+ messages in thread From: Junhui Liu @ 2026-08-22 8:56 UTC (permalink / raw) To: u-boot Cc: Tom Rini, Junhui Liu, Quentin Schulz, Simon Glass, Daniel Golle, Randolph Sapp, Ilias Apalodimas, Kory Maincent, Guodong Xu, Yixun Lan, spacemit AIHD is the first-stage boot image format used by the SpacemiT K1 and K3 BootROMs. Document its layout and the K3 non-secure CRC32 path currently supported by mkimage. Signed-off-by: Junhui Liu <junhui.liu@pigmoral.tech> --- doc/board/spacemit/aihd.rst | 145 +++++++++++++++++++++++++++++++++++++++++++ doc/board/spacemit/index.rst | 2 +- 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/doc/board/spacemit/aihd.rst b/doc/board/spacemit/aihd.rst new file mode 100644 index 000000000000..9a14fefc4d75 --- /dev/null +++ b/doc/board/spacemit/aihd.rst @@ -0,0 +1,145 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +SpacemiT AIHD image format +========================== + +The SpacemiT K1 and K3 BootROMs load an AIHD image as the first-stage +bootloader (FSBL), either from persistent storage or over USB in MaskROM mode. +Vendor firmware calls this image ``FSBL.bin``. The format is named after its +AIHD header magic. + +Image layout +------------ + +K1 and K3 share the same layout. A fixed 4 KiB prefix contains two 32-byte +metadata headers, key slots, and signature slots. The aligned SPL payload and +a trailing authentication area follow:: + + offset size + 0x000 +-------------------------------+ + | root RSA-2048 modulus | 0x100 + 0x100 +-------------------------------+ + | header0 | 0x020 + 0x120 +-------------------------------+ + | key-selection metadata | 0x1e0 + 0x300 +-------------------------------+ + | OEM public-key slots | 0x800 + 0xb00 +-------------------------------+ + | signature0 | 0x100 + 0xc00 +-------------------------------+ + | reserved | 0x3e0 + 0xfe0 +-------------------------------+ + | header1 | 0x020 + 0x1000 +-------------------------------+ + | SPL payload (32-byte aligned) | + +-------------------------------+ + | signature1 | 0x100 + +-------------------------------+ + +For payload size ``P`` and ``A = ALIGN(P, 32)``, the image size is +``0x1000 + A + 0x100``. + +Metadata header +--------------- + +Both header0 and header1 use the following format: + +.. list-table:: + :header-rows: 1 + + * - Offset + - Size + - Field + - Description + * - 0x00 + - 4 + - ``magic`` + - ``AIHD`` + * - 0x04 + - 1 + - ``version`` + - Anti-rollback image version + * - 0x05 + - 1 + - ``secure`` + - Zero selects anti-rollback bank 0 + + Non-zero selects anti-rollback bank 1 + * - 0x06 + - 2 + - ``reserved`` + - Reserved + * - 0x08 + - 8 + - ``image_size`` + - header0: prefix information + + header1: aligned payload size + * - 0x10 + - 8 + - ``load_addr`` + - Unused by the common authentication code + * - 0x18 + - 4 + - ``header_crc`` + - K3: CRC32 over header bytes ``[0x00, 0x18)`` + * - 0x1c + - 4 + - ``image_crc`` + - K3: CRC32 over the payload in header1 only + +``header1.image_size`` records the aligned payload size and therefore +determines the location of signature1. + +Authentication +-------------- + +K1 and K3 apply different authentication policies: + +.. list-table:: + :header-rows: 1 + + * - Area + - K1 + - K3 non-secure boot mode + - K3 secure boot mode + * - Root and OEM keys + - RSA-2048 moduli with an eFuse root-hash check when secure boot is + enabled + - Unused + - RSA-2048 moduli with an eFuse root-hash check + * - Header CRC + - Not checked separately + - header1 verified + + header0 ignored + - Both verified and covered by RSA + * - Image CRC + - Not checked separately + - Payload CRC32 in header1 + - Covered by RSA but not checked separately + * - signature0 + - Verified with the root key over ``[0x100, 0xb00)`` + - Unused + - Verified with the root key over ``[0x100, 0xb00)`` + * - signature1 + - Verified with the SPL key over header1 and the aligned payload + - Unused + - Verified with the SPL key over header1 and the aligned payload + +K1 verifies both signatures even without a programmed root-key hash. In this +case, the root key comes from the image and is not authenticated by hardware, +so the image, keys, and signatures can be replaced together. + +K3 uses CRC32 in non-secure boot mode and hardware-rooted RSA in secure boot +mode. + +Creating an image +----------------- + +U-Boot currently creates only K3 images for non-secure boot mode. Package an +SPL payload with:: + + $ tools/mkimage -T aihdimage -n k3 -d u-boot-spl.bin FSBL.bin + +K1 images and RSA-authenticated K3 images are not yet supported. diff --git a/doc/board/spacemit/index.rst b/doc/board/spacemit/index.rst index a5e35ee12ab6..45ef54f1543b 100644 --- a/doc/board/spacemit/index.rst +++ b/doc/board/spacemit/index.rst @@ -5,6 +5,6 @@ SpacemiT .. toctree:: :maxdepth: 1 + aihd bananapi-f3 k1-spl - -- 2.55.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support 2026-08-22 8:56 [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support Junhui Liu 2026-08-22 8:56 ` [PATCH 1/2] " Junhui Liu 2026-08-22 8:56 ` [PATCH 2/2] doc: board: spacemit: document the AIHD image format Junhui Liu @ 2026-08-31 6:09 ` E Shattow 2 siblings, 0 replies; 5+ messages in thread From: E Shattow @ 2026-08-31 6:09 UTC (permalink / raw) To: Junhui Liu, u-boot Cc: Tom Rini, Quentin Schulz, Simon Glass, Daniel Golle, Randolph Sapp, Ilias Apalodimas, Kory Maincent, Guodong Xu, Yixun Lan, spacemit Hi Junhui, On 8/22/26 01:56, Junhui Liu wrote: > The SpacemiT BootROM loads a first-stage bootloader from storage or over > USB in MaskROM mode. Vendor firmware packages this bootloader as FSBL.bin. > Its headers use the magic "AIHD". > > This series documents the AIHD image format and adds the "aihdimage" > mkimage type for the CRC32-protected K3 variant used in non-secure boot > mode. It provides the FSBL packaging needed for future K3 SPL support. > Select K3 with "-n k3" when creating an image. > > K1 and K3 secure boot use RSA authentication and are not supported yet. > > How to test > ----------- > > Rebuild mkimage, wrap a small UART test payload, and load it through K3 > MaskROM fastboot. The payload is linked at 0xc0801000 because the BootROM > places the complete image at 0xc0800000 and enters the payload after the > 4 KiB prefix. UART0 should print "Hello SpacemiT K3!" and then loop > indefinitely. > > $ make tools-only_defconfig > $ make tools-only > > $ cat > k3-uart.S << 'EOF' > .equ UART0_THR, 0xd4017000 > .equ UART0_LSR, 0xd4017014 > > .section .text > .global _start > _start: > la s0, str > 1: > lbu a0, (s0) > beqz a0, exit > call uart_send > addi s0, s0, 1 > j 1b > > exit: > j exit > > uart_send: > li t0, UART0_LSR > lw t1, (t0) > andi t1, t1, 0x20 > beqz t1, uart_send > li t0, UART0_THR > sw a0, (t0) > ret > > .section .rodata > str: > .asciz "Hello SpacemiT K3!\n" > EOF > > $ riscv64-linux-gnu-gcc -nostdlib -fno-builtin -fno-pie -no-pie -static \ > -march=rv64gc -mabi=lp64 -g -Wall -Wl,--build-id=none \ > -Ttext=0xc0801000 -o k3-uart.elf k3-uart.S > $ riscv64-linux-gnu-objcopy -O binary k3-uart.elf k3-uart.bin > $ ./tools/mkimage -T aihdimage -n k3 -d k3-uart.bin k3-uart-aihd.bin > > Hold the FDL button, power on the board to enter MaskROM, then run > fastboot on host: > > $ fastboot stage k3-uart-aihd.bin > $ fastboot continue > > --- > Junhui Liu (2): > tools: mkimage: add SpacemiT K3 AIHD image support > doc: board: spacemit: document the AIHD image format > > boot/image.c | 1 + > doc/board/spacemit/aihd.rst | 145 +++++++++++++++++++++++++++++++ > doc/board/spacemit/index.rst | 2 +- > include/image.h | 1 + > tools/Makefile | 3 +- > tools/aihdimage.c | 202 +++++++++++++++++++++++++++++++++++++++++++ > 6 files changed, 352 insertions(+), 2 deletions(-) > --- > base-commit: 6073c36b2c8d39afe3ecc789b281667a3ddebc70 > change-id: 20260820-spacemit-aihd-05c46aae886e > > Best regards, > -- > Junhui Liu <junhui.liu@pigmoral.tech> > I confirm this output as described with Sipeed K3 Pico-ITX and Debian 13 Trixie host build OS For the series, Tested-by: E Shattow <e@freeshell.de> ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-09-05 11:07 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-08-22 8:56 [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support Junhui Liu 2026-08-22 8:56 ` [PATCH 1/2] " Junhui Liu 2026-09-05 11:07 ` Yixun Lan 2026-08-22 8:56 ` [PATCH 2/2] doc: board: spacemit: document the AIHD image format Junhui Liu 2026-08-31 6:09 ` [PATCH 0/2] tools: mkimage: add SpacemiT K3 AIHD image support E Shattow
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox