Skip to contentSkip to conversation

sunxi: TOC0 image type support

PATCH Superseded Samuel Holland 4 participants 10 messages Oct 15, 2021 – Oct 19, 2021

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Samuel Holland

author maintainer
#0 lore raw

v3 cover sunxi: TOC0 image type support

This series adds support for the TOC0 image format used by the Allwinner secure boot ROM (SBROM). This series has been tested on the following SoCs/boards, with the eFuse burnt to enable secure mode:
- A50: Ainol Q88 Tablet
- A64: Pine A64 Plus
- H5: Orange Pi Zero Plus
- H6: Pine H64 Model B
- H616: Orange Pi Zero 2

This time I also tested it on boards that are not switched to secure mode (with A64, H3, and H5).

Due to both series changing Makefile.spl, the last patch depends on:
https://patchwork.ozlabs.org/project/uboot/list/?series=267136

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)
- Removed TOOLS_LIBCRYPTO selection for sunxi, since most boards do not need it
- Added __packed to all new "ABI" structs
- Added entry to MAINTAINERS for sunxi tools
- Fixed offset of magic passed to memcmp
- Refactored functions to not return pointers (fixes ambiguous NULL)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO
- Moved certificate and key item structures out of sunxi_image.h
- Renamed "main" and "item" variables for clarity
- Improved error messages, and added a hint about key generation
- Added a comment explaining the purpose of the various key files
- Mentioned testing this code on A50 in the commit message
- Moved SPL header signature checks out of sunxi_image.h
- Refactored SPL header signature checks to use fewer casts
- Rebase on top of Icenowy's RISC-V support series
- Rename Kconfig symbols to include the full image type name

Samuel Holland (4):
tools: Separate image types which depend on OpenSSL
tools: mkimage: Add Allwinner TOC0 support
sunxi: Support SPL in both eGON and TOC0 images
sunxi: Support building a SPL as a TOC0 image

 MAINTAINERS                           |   1 +
 arch/arm/Kconfig                      |   3 +
 arch/arm/include/asm/arch-sunxi/spl.h |   2 -
 arch/arm/mach-imx/mxs/Kconfig         |   2 +
 arch/arm/mach-sunxi/Kconfig           |   2 +
 arch/arm/mach-sunxi/board.c           |  34 +-
 board/sunxi/Kconfig                   |  24 +
 common/image.c                        |   1 +
 include/image.h                       |   1 +
 include/sunxi_image.h                 |  37 ++
 scripts/Makefile.spl                  |   5 +-
 scripts/config_whitelist.txt          |   1 -
 tools/Makefile                        |  20 +-
 tools/mxsimage.c                      |   3 -
 tools/sunxi_toc0.c                    | 907 ++++++++++++++++++++++++++
 15 files changed, 1016 insertions(+), 27 deletions(-)
 create mode 100644 board/sunxi/Kconfig
 create mode 100644 tools/sunxi_toc0.c
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH v3 1/4 tools: Separate image types which depend on OpenSSL

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

 arch/arm/Kconfig              |  3 +++
 arch/arm/mach-imx/mxs/Kconfig |  2 ++
 scripts/config_whitelist.txt  |  1 -
 tools/Makefile                | 19 +++++--------------
 tools/mxsimage.c              |  3 ---
 5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
 	select BOARD_EARLY_INIT_F
 	select CPU_ARM926EJS
 	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
 config ARCH_MVEBU
 	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
 	select OF_CONTROL
 	select OF_SEPARATE
 	select SPI
+	select TOOLS_LIBCRYPTO
 	imply CMD_DM
 
 config ARCH_ORION5X
 	bool "Marvell Orion"
 	select CPU_ARM926EJS
 	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
 config TARGET_STV0991
 	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
 config MX23
 	bool
 	default y
+	select TOOLS_LIBCRYPTO
 
 choice
 	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
 config MX28
 	bool
 	default y
+	select TOOLS_LIBCRYPTO
 
 choice
 	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
 CONFIG_MXC_USB_FLAGS
 CONFIG_MXC_USB_PORT
 CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
 CONFIG_MXS_AUART
 CONFIG_MXS_AUART_BASE
 CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
 AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
 					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
 ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
 			imximage.o \
 			imx8image.o \
 			imx8mimage.o \
-			kwbimage.o \
 			lib/md5.o \
 			lpc32xximage.o \
-			mxsimage.o \
 			omapimage.o \
 			os_support.o \
 			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
 fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
 file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
 ifdef CONFIG_TOOLS_LIBCRYPTO
 # This affects include/image.h, but including the board config file
 # is tricky, so manually define this options here.
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
 HOSTCFLAGS_kwbimage.o += \
 	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
 HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
  * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
  */
 
-#ifdef CONFIG_MXS
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
 	NULL,
 	mxsimage_generate
 );
-#endif
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH v3 1/4] tools: Separate image types which depend on OpenSSL

Hello! I did more investigation for this topic during working on kwboot improvements, which resulted in this patch series:
https://lore.kernel.org/u-boot/[email protected]/

The result is that kwbimage is used only for: Kirkwood, Dove, A370, AXP, A375, A38x, A39x and MSYS. This information is now also in manpage:
https://lore.kernel.org/u-boot/[email protected]/

So, Orion, Discovery, A3700, A7k, A8k and CN913x does *not* use kwbimage and so they do do not need to select libcrypto.

Therefore you can drop selection of libcrypto from ARCH_ORION5X.

ARCH_KIRKWOOD includes only Kirkwood (which required kwbimage), but does not use any crypto. So selecting unused crypto seems suspicious for anybody who look at it... And it is there only for transitional kwbimage dependency, which is very unintuitive.

With ARCH_MVEBU it is quite complicated as it is used for both 32-bit SoCs (XP, 375, 38x) and also 64-bit SoCs (3700, 7k, 8k). kwbimage is used only for 32-bit mvebu SoCs.

I do not know how to express this correctly.

But I would rather suggest to introduce a new option which specify that tools/kwbimage.o is required and this dependency would selects its direct (crypto) dependency. Otherwise nobody would remember in future why non-crypto platforms are selecting crypto functionality, or why 64-bit platforms are enabling crypto functionality which is not used at all.

Quoted 157 lines · On Thursday 14 October 2021 22:19:13 Samuel Holland wrote:

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

 arch/arm/Kconfig              |  3 +++
 arch/arm/mach-imx/mxs/Kconfig |  2 ++
 scripts/config_whitelist.txt  |  1 -
 tools/Makefile                | 19 +++++--------------
 tools/mxsimage.c              |  3 ---
 5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
 	select BOARD_EARLY_INIT_F
 	select CPU_ARM926EJS
 	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
 config ARCH_MVEBU
 	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
 	select OF_CONTROL
 	select OF_SEPARATE
 	select SPI
+	select TOOLS_LIBCRYPTO
 	imply CMD_DM
 
 config ARCH_ORION5X
 	bool "Marvell Orion"
 	select CPU_ARM926EJS
 	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
 config TARGET_STV0991
 	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
 config MX23
 	bool
 	default y
+	select TOOLS_LIBCRYPTO
 
 choice
 	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
 config MX28
 	bool
 	default y
+	select TOOLS_LIBCRYPTO
 
 choice
 	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
 CONFIG_MXC_USB_FLAGS
 CONFIG_MXC_USB_PORT
 CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
 CONFIG_MXS_AUART
 CONFIG_MXS_AUART_BASE
 CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
 AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
 					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
 ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
 			imximage.o \
 			imx8image.o \
 			imx8mimage.o \
-			kwbimage.o \
 			lib/md5.o \
 			lpc32xximage.o \
-			mxsimage.o \
 			omapimage.o \
 			os_support.o \
 			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
 fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
 file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
 ifdef CONFIG_TOOLS_LIBCRYPTO
 # This affects include/image.h, but including the board config file
 # is tricky, so manually define this options here.
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
 HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
 HOSTCFLAGS_kwbimage.o += \
 	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
 HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
  * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
  */
 
-#ifdef CONFIG_MXS
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
 	NULL,
 	mxsimage_generate
 );
-#endif
Signature · 1 line
2.32.0
#3 lore raw

Re: [PATCH v3 1/4] tools: Separate image types which depend on OpenSSL

In reply to Samuel Holland
Quoted 8 lines · Samuel Holland

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

NAK.

The intent, as detailed in tools/Makefile, is to _NOT_ to conflate target options with tools options.

Disabling openssl libs is purely at the user's discretion. If platforms can't build a usable image, I suggest just printing a loud warning instead of overriding the user.

Alex

Quoted 147 lines

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

  arch/arm/Kconfig              |  3 +++
  arch/arm/mach-imx/mxs/Kconfig |  2 ++
  scripts/config_whitelist.txt  |  1 -
  tools/Makefile                | 19 +++++--------------
  tools/mxsimage.c              |  3 ---
  5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
  	select BOARD_EARLY_INIT_F
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config ARCH_MVEBU
  	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
  	select OF_CONTROL
  	select OF_SEPARATE
  	select SPI
+	select TOOLS_LIBCRYPTO
  	imply CMD_DM
 
  config ARCH_ORION5X
  	bool "Marvell Orion"
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config TARGET_STV0991
  	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
  config MX23
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
  config MX28
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
  CONFIG_MXC_USB_FLAGS
  CONFIG_MXC_USB_PORT
  CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
  CONFIG_MXS_AUART
  CONFIG_MXS_AUART_BASE
  CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
  AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
  					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
  ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
  			imximage.o \
  			imx8image.o \
  			imx8mimage.o \
-			kwbimage.o \
  			lib/md5.o \
  			lpc32xximage.o \
-			mxsimage.o \
  			omapimage.o \
  			os_support.o \
  			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
  fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
  file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
  ifdef CONFIG_TOOLS_LIBCRYPTO
  # This affects include/image.h, but including the board config file
  # is tricky, so manually define this options here.
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
  HOSTCFLAGS_kwbimage.o += \
  	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
  HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
   * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
   */
 
-#ifdef CONFIG_MXS
-
  #include <errno.h>
  #include <fcntl.h>
  #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
  	NULL,
  	mxsimage_generate
  );
-#endif
#4 lore raw

Re: [PATCH v3 1/4] tools: Separate image types which depend on OpenSSL

On Mon, 18 Oct 2021 09:09:04 -0500
"Alex G." <[email protected]> wrote:

Hi,

Quoted 8 lines · Samuel Holland
Quoted 7 lines

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

Quoted 6 lines
Quoted 1 line

NAK.

The intent, as detailed in tools/Makefile, is to _NOT_ to conflate target options with tools options.

I am a bit undecided, because I think the intent was more for *just* building mkimage (tools-only_defconfig, for the u-boot-tools distro package, for instance). (Which doesn't seem to work, btw, with or without this patch.)
However just building mkimage because it's needed to create a certain board firmware is a different story, I think, and including OpenSSL (if the platform requires that) is hardly a user's choice at this point.

But anyway: Samuel, what is the actual problem this patch is solving? TOOLS_LIBCRYPTO is default y, so normally (make foo_defconfig; make) everything should be fine? And it only breaks if a user deliberately and manually deselects it, between "make foo_defconfig" and "make"?

So this patch is somewhat optional, at least for the purpose of TOC0 support?

Cheers,
Andre

Quoted 153 lines

Disabling openssl libs is purely at the user's discretion. If platforms can't build a usable image, I suggest just printing a loud warning instead of overriding the user.

Alex

Quoted 147 lines

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

  arch/arm/Kconfig              |  3 +++
  arch/arm/mach-imx/mxs/Kconfig |  2 ++
  scripts/config_whitelist.txt  |  1 -
  tools/Makefile                | 19 +++++--------------
  tools/mxsimage.c              |  3 ---
  5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
  	select BOARD_EARLY_INIT_F
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config ARCH_MVEBU
  	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
  	select OF_CONTROL
  	select OF_SEPARATE
  	select SPI
+	select TOOLS_LIBCRYPTO
  	imply CMD_DM
 
  config ARCH_ORION5X
  	bool "Marvell Orion"
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config TARGET_STV0991
  	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
  config MX23
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
  config MX28
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
  CONFIG_MXC_USB_FLAGS
  CONFIG_MXC_USB_PORT
  CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
  CONFIG_MXS_AUART
  CONFIG_MXS_AUART_BASE
  CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
  AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
  					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
  ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
  			imximage.o \
  			imx8image.o \
  			imx8mimage.o \
-			kwbimage.o \
  			lib/md5.o \
  			lpc32xximage.o \
-			mxsimage.o \
  			omapimage.o \
  			os_support.o \
  			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
  fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
  file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
  ifdef CONFIG_TOOLS_LIBCRYPTO
  # This affects include/image.h, but including the board config file
  # is tricky, so manually define this options here.
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
  HOSTCFLAGS_kwbimage.o += \
  	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
  HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
   * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
   */
 
-#ifdef CONFIG_MXS
-
  #include <errno.h>
  #include <fcntl.h>
  #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
  	NULL,
  	mxsimage_generate
  );
-#endif
author maintainer
#5 lore raw

Re: [PATCH v3 1/4] tools: Separate image types which depend on OpenSSL

Quoted 17 lines · Andre Przywara

On Mon, 18 Oct 2021 09:09:04 -0500
"Alex G." <[email protected]> wrote:

Hi,

Quoted 12 lines · Samuel Holland
Quoted 8 lines

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

NAK.

Quoted 7 lines
Quoted 2 lines

The intent, as detailed in tools/Makefile, is to _NOT_ to conflate target options with tools options.

I am a bit undecided, because I think the intent was more for *just* building mkimage (tools-only_defconfig, for the u-boot-tools distro package, for instance). (Which doesn't seem to work, btw, with or without this patch.)

TOOLS_LIBCRYPTO=n works for me with this patch, and I just double-checked and verified that mkimage is compiled/linked without OpenSSL.

Quoted 5 lines

However just building mkimage because it's needed to create a certain board firmware is a different story, I think, and including OpenSSL (if the platform requires that) is hardly a user's choice at this point.

But anyway: Samuel, what is the actual problem this patch is solving?

The actual problem is that TOOLS_LIBCRYPTO=n is broken, and would be further broken by adding sunxi_toc0.o to dumpimage-mkimage-objs. Fixing that requires moving objects that depend on OpenSSL to LIBCRYPTO_OBJS, and adding sunxi_toc0.o there in patch 2.

Quoted 6 lines

TOOLS_LIBCRYPTO is default y, so normally (make foo_defconfig; make) everything should be fine? And it only breaks if a user deliberately and manually deselects it, between "make foo_defconfig" and "make"?

So this patch is somewhat optional, at least for the purpose of TOC0 support?

The Makefile changes are needed for TOC0 support. The Kconfig changes are not. And I think the only controversial part of this patch is the "select TOOLS_LIBCRYPTO" lines. So I suggest omitting all of the Kconfig changes from this patch (and removing those lines from the commit message). I can send v4 or you can fix it up.

Regards,
Samuel

Quoted 157 lines

Cheers,
Andre

Quoted 153 lines

Disabling openssl libs is purely at the user's discretion. If platforms can't build a usable image, I suggest just printing a loud warning instead of overriding the user.

Alex

Quoted 147 lines

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

  arch/arm/Kconfig              |  3 +++
  arch/arm/mach-imx/mxs/Kconfig |  2 ++
  scripts/config_whitelist.txt  |  1 -
  tools/Makefile                | 19 +++++--------------
  tools/mxsimage.c              |  3 ---
  5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
  	select BOARD_EARLY_INIT_F
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config ARCH_MVEBU
  	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
  	select OF_CONTROL
  	select OF_SEPARATE
  	select SPI
+	select TOOLS_LIBCRYPTO
  	imply CMD_DM
 
  config ARCH_ORION5X
  	bool "Marvell Orion"
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config TARGET_STV0991
  	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
  config MX23
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
  config MX28
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
  CONFIG_MXC_USB_FLAGS
  CONFIG_MXC_USB_PORT
  CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
  CONFIG_MXS_AUART
  CONFIG_MXS_AUART_BASE
  CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
  AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
  					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
  ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
  			imximage.o \
  			imx8image.o \
  			imx8mimage.o \
-			kwbimage.o \
  			lib/md5.o \
  			lpc32xximage.o \
-			mxsimage.o \
  			omapimage.o \
  			os_support.o \
  			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
  fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
  file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
  ifdef CONFIG_TOOLS_LIBCRYPTO
  # This affects include/image.h, but including the board config file
  # is tricky, so manually define this options here.
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
  HOSTCFLAGS_kwbimage.o += \
  	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
  HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
   * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
   */
 
-#ifdef CONFIG_MXS
-
  #include <errno.h>
  #include <fcntl.h>
  #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
  	NULL,
  	mxsimage_generate
  );
-#endif
#6 lore raw

Re: [PATCH v3 1/4] tools: Separate image types which depend on OpenSSL

On Tue, 19 Oct 2021 08:28:44 -0500
Samuel Holland <[email protected]> wrote:

Hi Samuel,

Quoted 21 lines · Andre Przywara
Quoted 20 lines

On Mon, 18 Oct 2021 09:09:04 -0500
"Alex G." <[email protected]> wrote:

Hi,

Quoted 14 lines · Samuel Holland
Quoted 8 lines

Some image types (kwbimage and mxsimage) always depend on OpenSSL, so they can only be included in mkimage when TOOLS_LIBCRYPTO is selected. Use Makefile logic to conditionally link the files.

When building for platforms which use those image types, automatically select TOOLS_LIBCRYPTO, since it is required for the build to complete.

NAK.

The intent, as detailed in tools/Makefile, is to _NOT_ to conflate target options with tools options.

Quoted 7 lines
Quoted 4 lines

I am a bit undecided, because I think the intent was more for *just* building mkimage (tools-only_defconfig, for the u-boot-tools distro package, for instance). (Which doesn't seem to work, btw, with or without this patch.)

TOOLS_LIBCRYPTO=n works for me with this patch, and I just double-checked and verified that mkimage is compiled/linked without OpenSSL.

For tools-only_defconfig and "make tools"?
I see that the SSL linking errors vanish, but I still get this instead:

$ make -s tools
/usr/bin/ld: tools/image-host.o: in function `fit_image_setup_sig':
image-host.c:(.text+0xec): undefined reference to `image_get_checksum_algo'
/usr/bin/ld: image-host.c:(.text+0xfc): undefined reference to `image_get_crypto_algo'
/usr/bin/ld: image-host.c:(.text+0x108): undefined reference to `image_get_padding_algo'
/usr/bin/ld: tools/image-host.o: in function `fit_config_add_verification_data':
image-host.c:(.text+0x7b0): undefined reference to `fit_region_make_list'
/usr/bin/ld: tools/image-host.o: in function `fit_check_sign':
image-host.c:(.text+0x1548): undefined reference to `fit_config_verify'
/usr/bin/ld: tools/common/image-fit.o: in function `fit_image_verify_with_data':
image-fit.c:(.text+0x17c0): undefined reference to `fit_image_verify_required_sigs'
/usr/bin/ld: image-fit.c:(.text+0x19e0): undefined reference to `fit_image_check_sig'
/usr/bin/ld: tools/common/image-fit.o: in function `fit_image_load':
image-fit.c:(.text+0x27ec): undefined reference to `fit_config_verify'
collect2: error: ld returned 1 exit status
make[1]: *** [scripts/Makefile.host:104: tools/dumpimage] Error 1
make: *** [Makefile:1800: tools] Error 2
(On Ubuntu 20.04 arm64)
Quoted 11 lines
Quoted 6 lines

However just building mkimage because it's needed to create a certain board firmware is a different story, I think, and including OpenSSL (if the platform requires that) is hardly a user's choice at this point.

But anyway: Samuel, what is the actual problem this patch is solving?

The actual problem is that TOOLS_LIBCRYPTO=n is broken, and would be further broken by adding sunxi_toc0.o to dumpimage-mkimage-objs. Fixing that requires moving objects that depend on OpenSSL to LIBCRYPTO_OBJS, and adding sunxi_toc0.o there in patch 2.

Right, I was just confused because it is still broken for me (see above).

Quoted 9 lines
Quoted 6 lines

TOOLS_LIBCRYPTO is default y, so normally (make foo_defconfig; make) everything should be fine? And it only breaks if a user deliberately and manually deselects it, between "make foo_defconfig" and "make"?

So this patch is somewhat optional, at least for the purpose of TOC0 support?

The Makefile changes are needed for TOC0 support. The Kconfig changes are not.

Right, I see.

Quoted 4 lines

And I think the only controversial part of this patch is the "select TOOLS_LIBCRYPTO" lines. So I suggest omitting all of the Kconfig changes from this patch (and removing those lines from the commit message). I can send v4 or you can fix it up.

You should probably send a v4, so that we get Alex' OK for that. I need to test on actual non-secure hardware tonight, to verify that it still works (which I strongly assume now, from looking at the code). Then I would be eager to merge it, since it booted my Remix Mini PC beautifully.

Cheers,
Andre

Quoted 156 lines
Quoted 155 lines
Quoted 154 lines

Disabling openssl libs is purely at the user's discretion. If platforms can't build a usable image, I suggest just printing a loud warning instead of overriding the user.

Alex

Quoted 147 lines

---

Changes in v3:
- Selected TOOLS_LIBCRYPTO on all platforms that use kwbimage (as best as I can tell, using the suggestions from Pali Rohár)

Changes in v2:
- Refactored the first patch on top of TOOLS_LIBCRYPTO

  arch/arm/Kconfig              |  3 +++
  arch/arm/mach-imx/mxs/Kconfig |  2 ++
  scripts/config_whitelist.txt  |  1 -
  tools/Makefile                | 19 +++++--------------
  tools/mxsimage.c              |  3 ---
  5 files changed, 10 insertions(+), 18 deletions(-)
arch/arm/Kconfig +3 -0
index d8c041a877..380ad4f670 100644
@@ -566,6 +566,7 @@ config ARCH_KIRKWOOD
  	select BOARD_EARLY_INIT_F
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config ARCH_MVEBU
  	bool "Marvell MVEBU family (Armada XP/375/38x/3700/7K/8K)"
@@ -580,12 +581,14 @@ config ARCH_MVEBU
  	select OF_CONTROL
  	select OF_SEPARATE
  	select SPI
+	select TOOLS_LIBCRYPTO
  	imply CMD_DM
 
  config ARCH_ORION5X
  	bool "Marvell Orion"
  	select CPU_ARM926EJS
  	select GPIO_EXTRA_HEADER
+	select TOOLS_LIBCRYPTO
 
  config TARGET_STV0991
  	bool "Support stv0991"
arch/arm/mach-imx/mxs/Kconfig +2 -0
index b2026a3758..6f138d25e9 100644
@@ -3,6 +3,7 @@ if ARCH_MX23
  config MX23
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX23 board select"
@@ -34,6 +35,7 @@ if ARCH_MX28
  config MX28
  	bool
  	default y
+	select TOOLS_LIBCRYPTO
 
  choice
  	prompt "MX28 board select"
scripts/config_whitelist.txt +0 -1
index 3a6865dc70..bea6b6f83b 100644
@@ -838,7 +838,6 @@ CONFIG_MXC_UART_BASE
  CONFIG_MXC_USB_FLAGS
  CONFIG_MXC_USB_PORT
  CONFIG_MXC_USB_PORTSC
-CONFIG_MXS
  CONFIG_MXS_AUART
  CONFIG_MXS_AUART_BASE
  CONFIG_MXS_OCOTP
tools/Makefile +5 -14
index 999fd46531..a9b3d982d8 100644
@@ -94,9 +94,11 @@ ECDSA_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/ecdsa/, ecdsa-libcrypto.
  AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
  					aes-encrypt.o aes-decrypt.o)
 
-# Cryptographic helpers that depend on openssl/libcrypto
-LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/, \
-					fdt-libcrypto.o)
+# Cryptographic helpers and image types that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
+			lib/fdt-libcrypto.o \
+			kwbimage.o \
+			mxsimage.o
 
  ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
@@ -118,10 +120,8 @@ dumpimage-mkimage-objs := aisimage.o \
  			imximage.o \
  			imx8image.o \
  			imx8mimage.o \
-			kwbimage.o \
  			lib/md5.o \
  			lpc32xximage.o \
-			mxsimage.o \
  			omapimage.o \
  			os_support.o \
  			pblimage.o \
@@ -156,22 +156,13 @@ fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
  fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
  file2include-objs := file2include.o
 
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_TOOLS_LIBCRYPTO),)
-# Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
-# the mxsimage support within tools/mxsimage.c .
-HOSTCFLAGS_mxsimage.o += -DCONFIG_MXS
-endif
-
  ifdef CONFIG_TOOLS_LIBCRYPTO
  # This affects include/image.h, but including the board config file
  # is tricky, so manually define this options here.
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_SIGNATURE_MAX_SIZE=0xffffffff
  HOST_EXTRACFLAGS	+= -DCONFIG_FIT_CIPHER
-endif
 
-# MXSImage needs LibSSL
-ifneq ($(CONFIG_MX23)$(CONFIG_MX28)$(CONFIG_ARMADA_38X)$(CONFIG_TOOLS_LIBCRYPTO),)
  HOSTCFLAGS_kwbimage.o += \
  	$(shell pkg-config --cflags libssl libcrypto 2> /dev/null || echo "")
  HOSTLDLIBS_mkimage += \
tools/mxsimage.c +0 -3
index 002f4b525a..2bfbb421eb 100644
@@ -5,8 +5,6 @@
   * Copyright (C) 2012-2013 Marek Vasut <[email protected]>
   */
 
-#ifdef CONFIG_MXS
-
  #include <errno.h>
  #include <fcntl.h>
  #include <stdio.h>
@@ -2363,4 +2361,3 @@ U_BOOT_IMAGE_TYPE(
  	NULL,
  	mxsimage_generate
  );
-#endif
author maintainer
#7 lore raw

PATCH v3 2/4 tools: mkimage: Add Allwinner TOC0 support

In reply to Samuel Holland

Most Allwinner sunxi SoCs have separate boot ROMs in non-secure and secure mode. The "non-secure" or "normal" boot ROM (NBROM) uses the existing sunxi_egon image type. The secure boot ROM (SBROM) uses a completely different image type, known as TOC0.

A TOC0 image is composed of a header and two or more items. One item is the firmware binary. The others form a chain linking the firmware signature to the root-of-trust public key (ROTPK), which has its hash burned in the SoC's eFuses. Signatures are made using RSA-2048 + SHA256.

The pseudo-ASN.1 structure is manually assembled; this is done to work around bugs/quirks in the boot ROM, which vary between SoCs. This TOC0 implementation has been verified to work with the A50, A64, H5, H6, and H616 SBROMs, and it may work with other SoCs.

---

Changes in v3:
- Removed TOOLS_LIBCRYPTO selection for sunxi, since most boards do not need it
- Added __packed to all new "ABI" structs
- Added entry to MAINTAINERS for sunxi tools

Changes in v2:
- Moved certificate and key item structures out of sunxi_image.h
- Renamed "main" and "item" variables for clarity
- Improved error messages, and added a hint about key generation
- Added a comment explaining the purpose of the various key files
- Mentioned testing this code on A50 in the commit message

 MAINTAINERS           |   1 +
 common/image.c        |   1 +
 include/image.h       |   1 +
 include/sunxi_image.h |  37 ++
 tools/Makefile        |   3 +-
 tools/sunxi_toc0.c    | 907 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 949 insertions(+), 1 deletion(-)
 create mode 100644 tools/sunxi_toc0.c
MAINTAINERS +1 -0
index 71f468c00a..0d62829f51 100644
@@ -475,6 +475,7 @@ F:	board/sunxi/
 F:	drivers/clk/sunxi/
 F:	drivers/phy/allwinner/
 F:	drivers/video/sunxi/
+F:	tools/sunxi*
 
 ARM TEGRA
 M:	Tom Warren <[email protected]>
common/image.c +1 -0
index 3fa60b5827..d15b47ebbe 100644
@@ -185,6 +185,7 @@ static const table_entry_t uimage_type[] = {
 	{	IH_TYPE_MTKIMAGE,   "mtk_image",   "MediaTek BootROM loadable Image" },
 	{	IH_TYPE_COPRO, "copro", "Coprocessor Image"},
 	{	IH_TYPE_SUNXI_EGON, "sunxi_egon",  "Allwinner eGON Boot Image" },
+	{	IH_TYPE_SUNXI_TOC0, "sunxi_toc0",  "Allwinner TOC0 Boot Image" },
 	{	-1,		    "",		  "",			},
 };
 
include/image.h +1 -0
index 34d13ada84..1547246ec8 100644
@@ -227,6 +227,7 @@ enum {
 	IH_TYPE_IMX8IMAGE,		/* Freescale IMX8Boot Image	*/
 	IH_TYPE_COPRO,			/* Coprocessor Image for remoteproc*/
 	IH_TYPE_SUNXI_EGON,		/* Allwinner eGON Boot Image */
+	IH_TYPE_SUNXI_TOC0,		/* Allwinner TOC0 Boot Image */
 
 	IH_TYPE_COUNT,			/* Number of image types */
 };
include/sunxi_image.h +37 -0
index 5b2055c0af..379ca9196e 100644
@@ -9,9 +9,13 @@
  *
  * Shared between mkimage and the SPL.
  */
+
 #ifndef	SUNXI_IMAGE_H
 #define	SUNXI_IMAGE_H
 
+#include <linux/compiler_attributes.h>
+#include <linux/types.h>
+
 #define BOOT0_MAGIC		"eGON.BT0"
 #define BROM_STAMP_VALUE	0x5f0a6c39
 #define SPL_SIGNATURE		"SPL" /* marks "sunxi" SPL header */
@@ -79,4 +83,37 @@ struct boot_file_head {
 /* Compile time check to assure proper alignment of structure */
 typedef char boot_file_head_not_multiple_of_32[1 - 2*(sizeof(struct boot_file_head) % 32)];
 
+struct __packed toc0_main_info {
+	uint8_t	name[8];
+	__le32	magic;
+	__le32	checksum;
+	__le32	serial;
+	__le32	status;
+	__le32	num_items;
+	__le32	length;
+	uint8_t	platform[4];
+	uint8_t	reserved[8];
+	uint8_t	end[4];
+};
+
+#define TOC0_MAIN_INFO_NAME		"TOC0.GLH"
+#define TOC0_MAIN_INFO_MAGIC		0x89119800
+#define TOC0_MAIN_INFO_END		"MIE;"
+
+struct __packed toc0_item_info {
+	__le32	name;
+	__le32	offset;
+	__le32	length;
+	__le32	status;
+	__le32	type;
+	__le32	load_addr;
+	uint8_t	reserved[4];
+	uint8_t	end[4];
+};
+
+#define TOC0_ITEM_INFO_NAME_CERT	0x00010101
+#define TOC0_ITEM_INFO_NAME_FIRMWARE	0x00010202
+#define TOC0_ITEM_INFO_NAME_KEY		0x00010303
+#define TOC0_ITEM_INFO_END		"IIE;"
+
 #endif
tools/Makefile +2 -1
index a9b3d982d8..e2aeb097aa 100644
@@ -98,7 +98,8 @@ AES_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := $(addprefix lib/aes/, \
 LIBCRYPTO_OBJS-$(CONFIG_TOOLS_LIBCRYPTO) := \
 			lib/fdt-libcrypto.o \
 			kwbimage.o \
-			mxsimage.o
+			mxsimage.o \
+			sunxi_toc0.o
 
 ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
tools/sunxi_toc0.c new file +907 -0

907 lines not on this page · raw message

Signature · 1 line
2.32.0
author maintainer
#8 lore raw

PATCH v3 3/4 sunxi: Support SPL in both eGON and TOC0 images

In reply to Samuel Holland

SPL uses the image header to detect the boot device and to find the offset of the next U-Boot stage. Since this information is stored differently in the eGON and TOC0 image headers, add code to find the correct value based on the image type currently in use.

---

Changes in v3:
- Fixed offset of magic passed to memcmp
- Refactored functions to not return pointers (fixes ambiguous NULL)

Changes in v2:
- Moved SPL header signature checks out of sunxi_image.h
- Refactored SPL header signature checks to use fewer casts

 arch/arm/include/asm/arch-sunxi/spl.h |  2 --
 arch/arm/mach-sunxi/board.c           | 34 ++++++++++++++++++++++-----
 2 files changed, 28 insertions(+), 8 deletions(-)
arch/arm/include/asm/arch-sunxi/spl.h +0 -2
index 58cdf806d9..157b11e489 100644
@@ -19,8 +19,6 @@
 #define SUNXI_BOOTED_FROM_MMC0_HIGH	0x10
 #define SUNXI_BOOTED_FROM_MMC2_HIGH	0x12
 
-#define is_boot0_magic(addr)	(memcmp((void *)(addr), BOOT0_MAGIC, 8) == 0)
-
 uint32_t sunxi_get_boot_device(void);
 
 #endif
arch/arm/mach-sunxi/board.c +28 -6
index b4ba2a72c4..b2cd64bb3f 100644
@@ -243,12 +243,28 @@ void s_init(void)
 
 #define SUNXI_INVALID_BOOT_SOURCE	-1
 
+static int sunxi_egon_valid(struct boot_file_head *egon_head)
+{
+	return !memcmp(egon_head->magic, BOOT0_MAGIC, 8); /* eGON.BT0 */
+}
+
+static int sunxi_toc0_valid(struct toc0_main_info *toc0_info)
+{
+	return !memcmp(toc0_info->name, TOC0_MAIN_INFO_NAME, 8); /* TOC0.GLH */
+}
+
 static int sunxi_get_boot_source(void)
 {
-	if (!is_boot0_magic(SPL_ADDR + 4)) /* eGON.BT0 */
-		return SUNXI_INVALID_BOOT_SOURCE;
+	struct boot_file_head *egon_head = (void *)SPL_ADDR;
+	struct toc0_main_info *toc0_info = (void *)SPL_ADDR;
+
+	if (sunxi_egon_valid(egon_head))
+		return readb(&egon_head->boot_media);
+	if (sunxi_toc0_valid(toc0_info))
+		return readb(&toc0_info->platform[0]);
 
-	return readb(SPL_ADDR + 0x28);
+	/* Not a valid image, so we must have been booted via FEL. */
+	return SUNXI_INVALID_BOOT_SOURCE;
 }
 
 /* The sunxi internal brom will try to loader external bootloader
@@ -296,10 +312,16 @@ uint32_t sunxi_get_boot_device(void)
 #ifdef CONFIG_SPL_BUILD
 static u32 sunxi_get_spl_size(void)
 {
-	if (!is_boot0_magic(SPL_ADDR + 4)) /* eGON.BT0 */
-		return 0;
+	struct boot_file_head *egon_head = (void *)SPL_ADDR;
+	struct toc0_main_info *toc0_info = (void *)SPL_ADDR;
 
-	return readl(SPL_ADDR + 0x10);
+	if (sunxi_egon_valid(egon_head))
+		return readl(&egon_head->length);
+	if (sunxi_toc0_valid(toc0_info))
+		return readl(&toc0_info->length);
+
+	/* Not a valid image, so use the default U-Boot offset. */
+	return 0;
 }
 
 /*
Signature · 1 line
2.32.0
author maintainer
#9 lore raw

PATCH v3 4/4 sunxi: Support building a SPL as a TOC0 image

In reply to Samuel Holland

Now that mkimage can generate TOC0 images, and the SPL can interpret them, hook up the build infrastructure so the user can choose which image type to build. Since the absolute load address is stored in the TOC0 header, that information must be passed to mkimage.

---

(no changes since v2)

Changes in v2:
- Rebase on top of Icenowy's RISC-V support series
- Rename Kconfig symbols to include the full image type name

 arch/arm/mach-sunxi/Kconfig |  2 ++
 board/sunxi/Kconfig         | 24 ++++++++++++++++++++++++
 scripts/Makefile.spl        |  5 ++++-
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 board/sunxi/Kconfig
arch/arm/mach-sunxi/Kconfig +2 -0
index 2c18cf02d1..879efb9f61 100644
@@ -1050,6 +1050,8 @@ config BLUETOOTH_DT_DEVICE_FIXUP
 	  The used address is "bdaddr" if set, and "ethaddr" with the LSB
 	  flipped elsewise.
 
+source "board/sunxi/Kconfig"
+
 endif
 
 config CHIP_DIP_SCAN
board/sunxi/Kconfig new file +24 -0
new file mode 100644
index 0000000000..084a8b0c6c
@@ -0,0 +1,24 @@
+choice
+	prompt "SPL Image Type"
+	default SPL_IMAGE_TYPE_SUNXI_EGON
+
+config SPL_IMAGE_TYPE_SUNXI_EGON
+	bool "eGON (normal)"
+	help
+	  Select this option to embed the SPL binary in an eGON.BT0 image,
+	  which is compatible with the normal boot ROM (NBROM).
+
+	  This is usually the correct option to choose.
+
+config SPL_IMAGE_TYPE_SUNXI_TOC0
+	bool "TOC0 (secure)"
+	help
+	  Select this option to embed the SPL binary in a TOC0 image,
+	  which is compatible with the secure boot ROM (SBROM).
+
+endchoice
+
+config SPL_IMAGE_TYPE
+	string
+	default "sunxi_egon" if SPL_IMAGE_TYPE_SUNXI_EGON
+	default "sunxi_toc0" if SPL_IMAGE_TYPE_SUNXI_TOC0
scripts/Makefile.spl +4 -1
index 4cc23799db..635fa14cb9 100644
@@ -411,7 +411,10 @@ endif
 $(obj)/$(SPL_BIN).sfp: $(obj)/$(SPL_BIN).bin FORCE
 	$(call if_changed,mkimage)
 
-MKIMAGEFLAGS_sunxi-spl.bin = -A $(ARCH) -T sunxi_egon \
+MKIMAGEFLAGS_sunxi-spl.bin = \
+	-A $(ARCH) \
+	-T $(CONFIG_SPL_IMAGE_TYPE) \
+	-a $(CONFIG_SPL_TEXT_BASE) \
 	-n $(CONFIG_DEFAULT_DEVICE_TREE)
 
 OBJCOPYFLAGS_u-boot-spl-dtb.hex := -I binary -O ihex --change-address=$(CONFIG_SPL_TEXT_BASE)
Signature · 1 line
2.32.0