diff mbox series

[1/3] board/freescale/imx: Add helper to generate fw binary

Message ID 20200427213402.6148-1-xroumegue@gmail.com
State New
Headers show
Series [1/3] board/freescale/imx: Add helper to generate fw binary | expand

Commit Message

Xavier Roumegue April 27, 2020, 9:34 p.m. UTC
This script aims to create an imx compatible boot image embedding
mainline components, using only upstream uboot mkimage tool, avoiding
dependencies on proprietary imx mkimage.

Mainline u-boot can generate a bootable image, but SPL soc proprietary
firmware dependencies have to be copied on uboot root dir.

This script prevents additional buildroot uboot recipe hacking to handle
custom SoC uboot build process.

The script actions summary is:
- Append DDR4 firmware to uboot spl binary
- Generate imx mkimage configuration file, extracting entry points from
  relevant elf files on the fly.
- Generate imx boot image using uboot upstream mkimage tool

Signed-off-by: Xavier Roumegue <xroumegue@gmail.com>
---
 .../common/imx/imx8-generate-fw-image.sh      | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100755 board/freescale/common/imx/imx8-generate-fw-image.sh

Comments

Thomas Petazzoni April 27, 2020, 9:57 p.m. UTC | #1
Hello Xavier,

On Mon, 27 Apr 2020 23:34:00 +0200
Xavier Roumegue <xroumegue@gmail.com> wrote:

> This script aims to create an imx compatible boot image embedding
> mainline components, using only upstream uboot mkimage tool, avoiding
> dependencies on proprietary imx mkimage.
> 
> Mainline u-boot can generate a bootable image, but SPL soc proprietary
> firmware dependencies have to be copied on uboot root dir.
> 
> This script prevents additional buildroot uboot recipe hacking to handle
> custom SoC uboot build process.
> 
> The script actions summary is:
> - Append DDR4 firmware to uboot spl binary
> - Generate imx mkimage configuration file, extracting entry points from
>   relevant elf files on the fly.
> - Generate imx boot image using uboot upstream mkimage tool
> 
> Signed-off-by: Xavier Roumegue <xroumegue@gmail.com>

Thanks for this proposal. Could you share a bit more details about the
upstream U-Boot i.MX8 image generation logic? How does it work, how do
you trigger it, which FW binaries need to be copied to the U-Boot build
directory, etc. ?

>  .../common/imx/imx8-generate-fw-image.sh      | 52 +++++++++++++++++++

We should perhaps find better names to distinguish
imx8-bootloader-prepare.sh from imx8-generate-fw-image.sh, and make it
clear which one is for which situation.

Also, is your script suitable for all i.MX8? I see upstream U-Boot has
separate image tools for i.MX8 and i.MX8M.

Thanks for your feedback,

Thomas
Gary Bisson April 28, 2020, 8:53 a.m. UTC | #2
Hi Xavier,

On Mon, Apr 27, 2020 at 11:57:04PM +0200, Thomas Petazzoni wrote:
> Hello Xavier,
> 
> On Mon, 27 Apr 2020 23:34:00 +0200
> Xavier Roumegue <xroumegue@gmail.com> wrote:
> 
> > This script aims to create an imx compatible boot image embedding
> > mainline components, using only upstream uboot mkimage tool, avoiding
> > dependencies on proprietary imx mkimage.
> > 
> > Mainline u-boot can generate a bootable image, but SPL soc proprietary
> > firmware dependencies have to be copied on uboot root dir.
> > 
> > This script prevents additional buildroot uboot recipe hacking to handle
> > custom SoC uboot build process.
> > 
> > The script actions summary is:
> > - Append DDR4 firmware to uboot spl binary
> > - Generate imx mkimage configuration file, extracting entry points from
> >   relevant elf files on the fly.
> > - Generate imx boot image using uboot upstream mkimage tool
> > 
> > Signed-off-by: Xavier Roumegue <xroumegue@gmail.com>
> 
> Thanks for this proposal. Could you share a bit more details about the
> upstream U-Boot i.MX8 image generation logic? How does it work, how do
> you trigger it, which FW binaries need to be copied to the U-Boot build
> directory, etc. ?

Same, thanks for this proposal. However I'm not sure adding another
script to basically do the same thing is the preferred option.
The main difference being that it uses the U-Boot mkimage instead of
imx-mkimage.

Ideally I'd like to be able to use the flash.bin U-Boot target so that
we get rid of those post-build scripts for good.

Thomas, what U-Boot needs (for imx8m) are the DDR/HDMI firmware files from
firmware-imx package. The main issue actually is that U-Boot looks for
those binaries at its root. If we could make it point to $BINARIES_DIR
instead, we could use 'flash.bin' target with U-Boot custom dependencies
[1] (if this patch gets accepted) to ensure firmware-imx was built
beforehand.

Otherwise, if we decide to keep a post-build script, I'd rather have
only one, preferrably this one as it doesn't depend on imx-mkimage.
But as Fabio mentioned, it also needs to support 8QXP/8QM, then we could
replaces imx8-bootloader-prepare.sh and also remove imx-mkimage package.

Let me know your thoughts.

Regards,
Gary

[1] http://patchwork.ozlabs.org/project/buildroot/patch/20200425000629.2068191-1-heiko@sntech.de/
Fabio Estevam April 28, 2020, 8:05 p.m. UTC | #3
On Tue, Apr 28, 2020 at 5:53 AM Gary Bisson
<gary.bisson@boundarydevices.com> wrote:

> Same, thanks for this proposal. However I'm not sure adding another
> script to basically do the same thing is the preferred option.
> The main difference being that it uses the U-Boot mkimage instead of
> imx-mkimage.
>
> Ideally I'd like to be able to use the flash.bin U-Boot target so that
> we get rid of those post-build scripts for good.
>
> Thomas, what U-Boot needs (for imx8m) are the DDR/HDMI firmware files from
> firmware-imx package. The main issue actually is that U-Boot looks for
> those binaries at its root. If we could make it point to $BINARIES_DIR
> instead, we could use 'flash.bin' target with U-Boot custom dependencies
> [1] (if this patch gets accepted) to ensure firmware-imx was built
> beforehand.
>
> Otherwise, if we decide to keep a post-build script, I'd rather have
> only one, preferrably this one as it doesn't depend on imx-mkimage.
> But as Fabio mentioned, it also needs to support 8QXP/8QM, then we could
> replaces imx8-bootloader-prepare.sh and also remove imx-mkimage package.
>
> Let me know your thoughts.

In case it helps, Jon Nettleton also worked on adding Buildroot
support for i.MX8M booting mainline U-Boot at:
https://github.com/SolidRun/buildroot/commit/0f2b2971e836b508bf9c2dac34426a59a9d83572

Glad to see Xavier's patch and looking forward to being able to
generate a Buildroot image for i.MX8M/8X that can boot mainline U-Boot
and kernel.

Thanks
Xavier Roumegue April 28, 2020, 9:50 p.m. UTC | #4
On Mon, Apr 27, 2020 at 11:57:04PM +0200, Thomas Petazzoni wrote:
> Hello Xavier,
>
> On Mon, 27 Apr 2020 23:34:00 +0200
> Xavier Roumegue <xroumegue@gmail.com> wrote:
>
> > This script aims to create an imx compatible boot image embedding
> > mainline components, using only upstream uboot mkimage tool, avoiding
> > dependencies on proprietary imx mkimage.
> >
> > Mainline u-boot can generate a bootable image, but SPL soc proprietary
> > firmware dependencies have to be copied on uboot root dir.
> >
> > This script prevents additional buildroot uboot recipe hacking to handle
> > custom SoC uboot build process.
> >
> > The script actions summary is:
> > - Append DDR4 firmware to uboot spl binary
> > - Generate imx mkimage configuration file, extracting entry points from
> >   relevant elf files on the fly.
> > - Generate imx boot image using uboot upstream mkimage tool
> >
> > Signed-off-by: Xavier Roumegue <xroumegue@gmail.com>
>
> Thanks for this proposal. Could you share a bit more details about the
> upstream U-Boot i.MX8 image generation logic? How does it work, how do
> you trigger it, which FW binaries need to be copied to the U-Boot build
> directory, etc. ?

Hello Thomas,
iMX8 Upstream uboot adds a custom target 'flash.bin', a bootable medium once
written to 33k offset.

1) On iMX8m[mqn], the (lp)ddr4 firmware, and hdmi fw for iMX8mq, have to be copied
in the uboot build dir prior to execute the build.
Additional SoC firmwares (system and security controllers) have to be copied
for iMX8X and iMX8QM.

2) (lp)ddr4 firmware is appended to uboot spl thanks to tools/imx8m_image.sh.
3) A config file per imx platform (*.cfg) is describing the imx mkimage parameters,
..i.e. loaders file paths and entry points, SoC firmwares, boot rom options,
etc..
4) Final loader is uboot.itb, generated as dependency of flash.bin for iMX
platforms, and using an its file generated by arch/arm/mach-imx/mkimage_fit_atf.sh.
ATF, TEE images have as well to be copied to uboot build dir, and their entry
points can be specified through environment variables.

5) Generate 'flash.bin'

The proposed script is taking care of 2), 3) and 5), with intention to support all
iMX8 platforms generating the mkimage config file on the fly and relying on
buildroot recipes to copy the required firmwares in images directory.

Current proposal does not generate the uboot FIT image, but this would likely be
an enhancement I would like to bring once 3) is achieved for all iMX8.

>
> >  .../common/imx/imx8-generate-fw-image.sh      | 52 +++++++++++++++++++
>
> We should perhaps find better names to distinguish
> imx8-bootloader-prepare.sh from imx8-generate-fw-image.sh, and make it
> clear which one is for which situation.
Would 'imx8-generate-upstream-image.sh' be more adapted ?

>
> Also, is your script suitable for all i.MX8? I see upstream U-Boot has
> separate image tools for i.MX8 and i.MX8M.
The current script supports only iMX8MM and iMX8MQ, but the concept is to
generate mkimage configuration file compatible with imx u-boot mkimage. So, this
scales up to any SoC supported by mainline u-boot.

Xavier
>
> Thanks for your feedback,
>
> Thomas
> --
> Thomas Petazzoni, CTO, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com
diff mbox series

Patch

diff --git a/board/freescale/common/imx/imx8-generate-fw-image.sh b/board/freescale/common/imx/imx8-generate-fw-image.sh
new file mode 100755
index 0000000000..eb9e96265b
--- /dev/null
+++ b/board/freescale/common/imx/imx8-generate-fw-image.sh
@@ -0,0 +1,52 @@ 
+#!/usr/bin/env bash
+
+get_entry()
+{
+    local elf_file=$1
+    readelf -h "$elf_file" | sed -e '/Entry/!d;s/.*:.*\(0x[0-9a-fA-F]\)/\1/g'
+}
+
+gen_cfg_file()
+{
+cat <<EOF > "$CFG_OUTFILE"
+#define __ASSEMBLY__
+
+FIT
+BOOT_FROM	sd
+LOADER		${SPL_DDR_BIN}	${SPL_ENTRY}
+SECOND_LOADER	${UBOOT_BIN}		${UBOOT_ENTRY} 0x60000
+
+EOF
+}
+
+gen_1stloader_bin()
+{
+    SPL_BIN_PAD="$(mktemp --suffix spl_pad.bin)"
+    dd if="${SPL_BIN}" of="${SPL_BIN_PAD}"  bs=4 conv=sync
+    cat "${SPL_BIN_PAD}" "${DDR_FW_BIN}" > "${SPL_DDR_BIN}"
+}
+
+gen_fw_image()
+{
+    "${HOST_DIR}"/bin/mkimage -n "${CFG_OUTFILE}" -T imx8mimage -e "${SPL_ENTRY}" -d "${UBOOT_BIN}" "${FLASH_BIN}"
+
+}
+main()
+{
+    UBOOT_BIN="${BINARIES_DIR}/u-boot.itb"
+    UBOOT_ELF="${BINARIES_DIR}/u-boot"
+    SPL_DDR_BIN="${BINARIES_DIR}/u-boot-spl-ddr.bin"
+    SPL_BIN="${BINARIES_DIR}/u-boot-spl.bin"
+    SPL_ELF="${BINARIES_DIR}/u-boot-spl"
+    CFG_OUTFILE="${BINARIES_DIR}/bootimage.cfg"
+    FLASH_BIN="${BINARIES_DIR}/imx8-boot-sd.bin"
+    UBOOT_ENTRY="$(get_entry "${UBOOT_ELF}")"
+    SPL_ENTRY="$(get_entry "${SPL_ELF}")"
+    DDR_FW_BIN="${BINARIES_DIR}/lpddr4_pmu_train_fw.bin"
+
+    gen_1stloader_bin
+    gen_cfg_file
+    gen_fw_image
+}
+
+main "$@"