From a77a86d6b2f59491e43c1954fed38631f3f7231b Mon Sep 17 00:00:00 2001 From: Chen Wang Date: Mon, 3 Feb 2025 20:03:10 +0800 Subject: bsp: cvitek: fix build error for aarch64 This patch fixed the error "bash: mksdimg.sh: No such file or directory" when building cv18xx_aarch64. The issue is introduced by commit "bsp: cvitek: removed useless files after using rttpkgtool". In addition, in order to unify the logic with riscv as much as possible, the name of the "milkv-duo256m" directory under cv18xx_aarch64 is uniformly changed to "duo256m". This patch also improve the README, adding instructions to install xz-utils. Signed-off-by: Chen Wang --- bsp/cvitek/README.md | 4 +- bsp/cvitek/cv18xx_aarch64/combine.sh | 4 +- bsp/cvitek/cv18xx_aarch64/dtb/duo256m/multi.its | 56 ++ .../dtb/duo256m/sg2002_milkv_duo256m_arm_sd.dtb | Bin 0 -> 20046 bytes .../cv18xx_aarch64/dtb/milkv-duo256m/multi.its | 56 -- .../milkv-duo256m/sg2002_milkv_duo256m_arm_sd.dtb | Bin 20046 -> 0 bytes bsp/cvitek/cv18xx_aarch64/mksdimg.sh | 25 + bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl2.bin | Bin 0 -> 49664 bytes .../cv18xx_aarch64/prebuild/duo256m/bl31.bin | Bin 0 -> 28688 bytes .../cv18xx_aarch64/prebuild/duo256m/blmacros.env | 2 + .../cv18xx_aarch64/prebuild/duo256m/chip_conf.bin | Bin 0 -> 24 bytes .../cv18xx_aarch64/prebuild/duo256m/ddr_param.bin | Bin 0 -> 8192 bytes .../cv18xx_aarch64/prebuild/duo256m/empty.bin | 0 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fip.bin | Bin 0 -> 366592 bytes .../cv18xx_aarch64/prebuild/duo256m/fiptool.py | 803 +++++++++++++++++++++ .../cv18xx_aarch64/prebuild/duo256m/u-boot-raw.bin | Bin 0 -> 497800 bytes .../cv18xx_aarch64/prebuild/milkv-duo256m/bl2.bin | Bin 49664 -> 0 bytes .../cv18xx_aarch64/prebuild/milkv-duo256m/bl31.bin | Bin 28688 -> 0 bytes .../prebuild/milkv-duo256m/blmacros.env | 2 - .../prebuild/milkv-duo256m/chip_conf.bin | Bin 24 -> 0 bytes .../prebuild/milkv-duo256m/ddr_param.bin | Bin 8192 -> 0 bytes .../prebuild/milkv-duo256m/empty.bin | 0 .../cv18xx_aarch64/prebuild/milkv-duo256m/fip.bin | Bin 366592 -> 0 bytes .../prebuild/milkv-duo256m/fiptool.py | 803 --------------------- .../prebuild/milkv-duo256m/u-boot-raw.bin | Bin 497800 -> 0 bytes bsp/cvitek/cv18xx_aarch64/rtconfig.py | 2 +- 26 files changed, 892 insertions(+), 865 deletions(-) mode change 100755 => 100644 bsp/cvitek/README.md create mode 100644 bsp/cvitek/cv18xx_aarch64/dtb/duo256m/multi.its create mode 100644 bsp/cvitek/cv18xx_aarch64/dtb/duo256m/sg2002_milkv_duo256m_arm_sd.dtb delete mode 100644 bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/multi.its delete mode 100644 bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/sg2002_milkv_duo256m_arm_sd.dtb create mode 100755 bsp/cvitek/cv18xx_aarch64/mksdimg.sh create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl2.bin create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl31.bin create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/blmacros.env create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/chip_conf.bin create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/ddr_param.bin create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/empty.bin create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fip.bin create mode 100755 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fiptool.py create mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/u-boot-raw.bin delete mode 100755 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/bl2.bin delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/bl31.bin delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/blmacros.env delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/chip_conf.bin delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/ddr_param.bin delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/empty.bin delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/fip.bin delete mode 100755 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/fiptool.py delete mode 100644 bsp/cvitek/cv18xx_aarch64/prebuild/milkv-duo256m/u-boot-raw.bin diff --git a/bsp/cvitek/README.md b/bsp/cvitek/README.md old mode 100755 new mode 100644 index 91b99895de..913c921883 --- a/bsp/cvitek/README.md +++ b/bsp/cvitek/README.md @@ -128,9 +128,11 @@ export RTT_EXEC_PATH=/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin ## 4.2. 依赖安装 ```shell -$ sudo apt install -y scons libncurses5-dev device-tree-compiler u-boot-tools +$ sudo apt install -y scons libncurses5-dev device-tree-compiler u-boot-tools xz-utils ``` +其中 u-boot-tools 包含了打包需要的 mkimage, xz-utils 包含了打包需要的 lzma。 + ## 4.3. 构建 异构芯片需单独编译每个核的 OS,在大/小核对应的目录下,依次执行: diff --git a/bsp/cvitek/cv18xx_aarch64/combine.sh b/bsp/cvitek/cv18xx_aarch64/combine.sh index af53969004..07767e8bba 100755 --- a/bsp/cvitek/cv18xx_aarch64/combine.sh +++ b/bsp/cvitek/cv18xx_aarch64/combine.sh @@ -1,7 +1,7 @@ #!/bin/bash C906_LITTLE_BIN_PATH=../c906_little/rtthread.bin -GEN_FIP_PATH=../output/milkv-duo256m/ -DEPENDS_FILE_PATH=./prebuild/milkv-duo256m +GEN_FIP_PATH=../output/duo256m/ +DEPENDS_FILE_PATH=./prebuild/duo256m BLCP_IMG_RUNADDR=0x05200200 BLCP_PARAM_LOADADDR=0 diff --git a/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/multi.its b/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/multi.its new file mode 100644 index 0000000000..d3d76fd8ce --- /dev/null +++ b/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/multi.its @@ -0,0 +1,56 @@ +/* + * U-Boot uImage source file with multiple kernels, ramdisks and FDT blobs + */ + +/dts-v1/; + +/ { + description = "Various kernels, ramdisks and FDT blobs"; + #address-cells = <2>; + + images { + kernel-1 { + description = "cvitek kernel"; + data = /incbin/("./Image.lzma"); + type = "kernel"; + arch = "arm64"; + os = "linux"; + compression = "lzma"; + load = <0x0 0x80200000>; + entry = <0x0 0x80200000>; + hash-2 { + algo = "crc32"; + }; + }; + + + /*FDT*/ + + fdt-sg2002_milkv_duo256m_arm_sd { + description = "cvitek device tree - sg2002_milkv_duo256m_arm_sd"; + data = /incbin/("./sg2002_milkv_duo256m_arm_sd.dtb"); + type = "flat_dt"; + arch = "arm64"; + compression = "none"; + hash-1 { + algo = "sha256"; + }; + }; + + + + }; + + /*CFG*/ + configurations { + + config-sg2002_milkv_duo256m_arm_sd { + description = "boot cvitek system with board sg2002_milkv_duo256m_arm_sd"; + kernel = "kernel-1"; + fdt = "fdt-sg2002_milkv_duo256m_arm_sd"; + }; + + }; + + +}; diff --git a/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/sg2002_milkv_duo256m_arm_sd.dtb b/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/sg2002_milkv_duo256m_arm_sd.dtb new file mode 100644 index 0000000000..6ef447d6e2 Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/dtb/duo256m/sg2002_milkv_duo256m_arm_sd.dtb differ diff --git a/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/multi.its b/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/multi.its deleted file mode 100644 index d3d76fd8ce..0000000000 --- a/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/multi.its +++ /dev/null @@ -1,56 +0,0 @@ -/* - * U-Boot uImage source file with multiple kernels, ramdisks and FDT blobs - */ - -/dts-v1/; - -/ { - description = "Various kernels, ramdisks and FDT blobs"; - #address-cells = <2>; - - images { - kernel-1 { - description = "cvitek kernel"; - data = /incbin/("./Image.lzma"); - type = "kernel"; - arch = "arm64"; - os = "linux"; - compression = "lzma"; - load = <0x0 0x80200000>; - entry = <0x0 0x80200000>; - hash-2 { - algo = "crc32"; - }; - }; - - - /*FDT*/ - - fdt-sg2002_milkv_duo256m_arm_sd { - description = "cvitek device tree - sg2002_milkv_duo256m_arm_sd"; - data = /incbin/("./sg2002_milkv_duo256m_arm_sd.dtb"); - type = "flat_dt"; - arch = "arm64"; - compression = "none"; - hash-1 { - algo = "sha256"; - }; - }; - - - - }; - - /*CFG*/ - configurations { - - config-sg2002_milkv_duo256m_arm_sd { - description = "boot cvitek system with board sg2002_milkv_duo256m_arm_sd"; - kernel = "kernel-1"; - fdt = "fdt-sg2002_milkv_duo256m_arm_sd"; - }; - - }; - - -}; diff --git a/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/sg2002_milkv_duo256m_arm_sd.dtb b/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/sg2002_milkv_duo256m_arm_sd.dtb deleted file mode 100644 index 6ef447d6e2..0000000000 Binary files a/bsp/cvitek/cv18xx_aarch64/dtb/milkv-duo256m/sg2002_milkv_duo256m_arm_sd.dtb and /dev/null differ diff --git a/bsp/cvitek/cv18xx_aarch64/mksdimg.sh b/bsp/cvitek/cv18xx_aarch64/mksdimg.sh new file mode 100755 index 0000000000..75c7caf194 --- /dev/null +++ b/bsp/cvitek/cv18xx_aarch64/mksdimg.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -e + +PROJECT_PATH=$1 +IMAGE_NAME=$2 + +if [ -z "$PROJECT_PATH" ] || [ -z "$IMAGE_NAME" ]; then + echo "Usage: $0 " + exit 1 +fi + +ROOT_PATH=$PROJECT_PATH/.. + +source ${ROOT_PATH}/tools.sh + +BOARD_TYPE=`get_board_type $PROJECT_PATH` + +echo "start compress kernel..." + +lzma -c -9 -f -k ${PROJECT_PATH}/${IMAGE_NAME} > ${PROJECT_PATH}/dtb/${BOARD_TYPE}/Image.lzma + +mkdir -p ${ROOT_PATH}/output/${BOARD_TYPE} +mkimage -f ${PROJECT_PATH}/dtb/${BOARD_TYPE}/multi.its -r ${ROOT_PATH}/output/${BOARD_TYPE}/boot.sd + +rm -rf ${PROJECT_PATH}/dtb/${BOARD_TYPE}/Image.lzma diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl2.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl2.bin new file mode 100644 index 0000000000..197e5a1f3a Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl2.bin differ diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl31.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl31.bin new file mode 100644 index 0000000000..cf38b2e291 Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/bl31.bin differ diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/blmacros.env b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/blmacros.env new file mode 100644 index 0000000000..ae3059bd57 --- /dev/null +++ b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/blmacros.env @@ -0,0 +1,2 @@ +MONITOR_RUNADDR=0x0000000080000000 +BLCP_2ND_RUNADDR=0x000000008fe00000 diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/chip_conf.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/chip_conf.bin new file mode 100644 index 0000000000..d2dabd6868 Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/chip_conf.bin differ diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/ddr_param.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/ddr_param.bin new file mode 100644 index 0000000000..fb4c648fd7 Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/ddr_param.bin differ diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/empty.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/empty.bin new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fip.bin b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fip.bin new file mode 100644 index 0000000000..be1eeeb042 Binary files /dev/null and b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fip.bin differ diff --git a/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fiptool.py b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fiptool.py new file mode 100755 index 0000000000..6943e5f799 --- /dev/null +++ b/bsp/cvitek/cv18xx_aarch64/prebuild/duo256m/fiptool.py @@ -0,0 +1,803 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# PYTHON_ARGCOMPLETE_OK + +import sys +import logging +import os +import os.path +import argparse +from collections import OrderedDict +import binascii +from struct import pack, unpack +import lzma +import pprint + + +PYTHON_MIN_VERSION = (3, 5, 2) # Ubuntu 16.04 LTS contains Python v3.5.2 by default + + +if sys.version_info < PYTHON_MIN_VERSION: + print("Python >= %r is required" % (PYTHON_MIN_VERSION,)) + sys.exit(-1) + + +try: + import coloredlogs +except ImportError: + coloredlogs = None + +try: + import argcomplete +except ImportError: + argcomplete = None + + +LOADER_2ND_MAGIC_ORIG = b"BL33" +LOADER_2ND_MAGIC_LZMA = b"B3MA" +LOADER_2ND_MAGIC_LZ4 = b"B3Z4" + +LOADER_2ND_MAGIC_LIST = [ + LOADER_2ND_MAGIC_ORIG, + LOADER_2ND_MAGIC_LZMA, + LOADER_2ND_MAGIC_LZ4, +] + +IMAGE_ALIGN = 512 +PARAM1_SIZE = 0x1000 +PARAM1_SIZE_WO_SIG = 0x800 +PARAM2_SIZE = 0x1000 + + +def round_up(divident, divisor): + return ((divident + divisor - 1) // divisor) * divisor + + +def lzma_compress(body): + z = lzma.LZMACompressor(lzma.FORMAT_ALONE, preset=lzma.PRESET_EXTREME) + compressed = z.compress(body) + compressed += z.flush() + + return compressed + + +def lz4_compress(body): + try: + import lz4.frame + except ImportError: + logging.error("lz4 is not installed. Run 'pip install lz4'.") + raise + + compressed = lz4.frame.compress(body) + return compressed + + +class Entry: + __slots__ = "name", "type", "addr", "_content", "entry_size" + + def __init__(self): + self.addr = None + self._content = None + + @property + def end(self): + return self.addr + self.entry_size + + @property + def content(self): + return self._content + + @content.setter + def content(self, value): + if type(value) == int: + value = value.to_bytes(self.entry_size, "little") + + if self.entry_size is not None: + if len(value) > self.entry_size: + raise ValueError("%s (%d bytes) must <= %#r" % (self.name, len(value), self.entry_size)) + value = value + b"\0" * (self.entry_size - len(value)) + + self._content = value + + @classmethod + def make(cls, name, entry_size, _type, init=None): + entry = Entry() + entry.name = name + entry.type = _type + entry.entry_size = entry_size + + if type(init) in (bytes, bytearray): + entry.content = bytes(init) + elif entry_size is not None: + entry.content = b"\0" * entry.entry_size + else: + entry.content = b"" + + return (name, entry) + + def toint(self): + if self.type != int: + raise TypeError("%s is not int type" % self.name) + + return int.from_bytes(self.content, "little") + + def tostr(self): + v = self.content + if self.type == int: + v = "%#08x" % self.toint() + elif type(self.content) in [bytes, bytearray]: + v = v.hex() + if len(v) > 32: + v = v[:32] + "..." + + return v + + def __str__(self): + v = self.tostr() + return "<%s=%s (%dbytes)>" % (self.name, v, self.entry_size) + + def __repr__(self): + v = self.tostr() + return "<%s: a=%#x s=%#x c=%s %r>" % (self.name, self.addr, self.entry_size, v, self.type) + + +class FIP: + param1 = OrderedDict( + [ + Entry.make("MAGIC1", 8, int, b"CVBL01\n\0"), + Entry.make("MAGIC2", 4, int), + Entry.make("PARAM_CKSUM", 4, int), + Entry.make("NAND_INFO", 128, int), + Entry.make("NOR_INFO", 36, int), + Entry.make("FIP_FLAGS", 8, int), + Entry.make("CHIP_CONF_SIZE", 4, int), + Entry.make("BLCP_IMG_CKSUM", 4, int), + Entry.make("BLCP_IMG_SIZE", 4, int), + Entry.make("BLCP_IMG_RUNADDR", 4, int), + Entry.make("BLCP_PARAM_LOADADDR", 4, int), + Entry.make("BLCP_PARAM_SIZE", 4, int), + Entry.make("BL2_IMG_CKSUM", 4, int), + Entry.make("BL2_IMG_SIZE", 4, int), + Entry.make("BLD_IMG_SIZE", 4, int), + Entry.make("PARAM2_LOADADDR", 4, int), + Entry.make("RESERVED1", 4, int), + Entry.make("CHIP_CONF", 760, bytes), + Entry.make("BL_EK", 32, bytes), + Entry.make("ROOT_PK", 512, bytes), + Entry.make("BL_PK", 512, bytes), + Entry.make("BL_PK_SIG", 512, bytes), + Entry.make("CHIP_CONF_SIG", 512, bytes), + Entry.make("BL2_IMG_SIG", 512, bytes), + Entry.make("BLCP_IMG_SIG", 512, bytes), + ] + ) + + body1 = OrderedDict( + [ + Entry.make("BLCP", None, bytes), + Entry.make("BL2", None, bytes), + ] + ) + + param2 = OrderedDict( + [ + Entry.make("MAGIC1", 8, int, b"CVLD02\n\0"), + Entry.make("PARAM2_CKSUM", 4, int), + Entry.make("RESERVED1", 4, bytes), + # DDR param + Entry.make("DDR_PARAM_CKSUM", 4, int), + Entry.make("DDR_PARAM_LOADADDR", 4, int), + Entry.make("DDR_PARAM_SIZE", 4, int), + Entry.make("DDR_PARAM_RESERVED", 4, int), + # BLCP_2ND + Entry.make("BLCP_2ND_CKSUM", 4, int), + Entry.make("BLCP_2ND_LOADADDR", 4, int), + Entry.make("BLCP_2ND_SIZE", 4, int), + Entry.make("BLCP_2ND_RUNADDR", 4, int), + # ATF-BL31 or OpenSBI + Entry.make("MONITOR_CKSUM", 4, int), + Entry.make("MONITOR_LOADADDR", 4, int), + Entry.make("MONITOR_SIZE", 4, int), + Entry.make("MONITOR_RUNADDR", 4, int), + # u-boot + Entry.make("LOADER_2ND_RESERVED0", 4, int), + Entry.make("LOADER_2ND_LOADADDR", 4, int), + Entry.make("LOADER_2ND_RESERVED1", 4, int), + Entry.make("LOADER_2ND_RESERVED2", 4, int), + # Reserved + Entry.make("RESERVED_LAST", 4096 - 16 * 5, bytes), + ] + ) + + body2 = OrderedDict( + [ + Entry.make("DDR_PARAM", None, bytes), + Entry.make("BLCP_2ND", None, bytes), + Entry.make("MONITOR", None, bytes), + Entry.make("LOADER_2ND", None, bytes), + ] + ) + + ldr_2nd_hdr = OrderedDict( + [ + Entry.make("JUMP0", 4, int), + Entry.make("MAGIC", 4, int), + Entry.make("CKSUM", 4, int), + Entry.make("SIZE", 4, int), + Entry.make("RUNADDR", 8, int), + Entry.make("RESERVED1", 4, int), + Entry.make("RESERVED2", 4, int), + ] + ) + + FIP_FLAGS_SCS_MASK = 0x000c + FIP_FLAGS_ENCRYPTED_MASK = 0x0030 + + def _param_size(self, param): + return max((e.end for e in param.values())) + + def _gen_param(self): + addr = 0 + for entry in self.param1.values(): + entry.addr = addr + addr += entry.entry_size + + assert PARAM1_SIZE_WO_SIG == self.param1["BL_PK_SIG"].addr + + addr = 0 + for entry in self.param2.values(): + entry.addr = addr + addr += entry.entry_size + + assert PARAM2_SIZE == self.param2["RESERVED_LAST"].addr + self.param2["RESERVED_LAST"].entry_size + + addr = 0 + for entry in self.ldr_2nd_hdr.values(): + entry.addr = addr + addr += entry.entry_size + + def __init__(self): + self.compress_algo = None + self._gen_param() + + def image_crc(self, image): + crc = binascii.crc_hqx(image, 0) + crc = pack("= %r is required" % (PYTHON_MIN_VERSION,)) - sys.exit(-1) - - -try: - import coloredlogs -except ImportError: - coloredlogs = None - -try: - import argcomplete -except ImportError: - argcomplete = None - - -LOADER_2ND_MAGIC_ORIG = b"BL33" -LOADER_2ND_MAGIC_LZMA = b"B3MA" -LOADER_2ND_MAGIC_LZ4 = b"B3Z4" - -LOADER_2ND_MAGIC_LIST = [ - LOADER_2ND_MAGIC_ORIG, - LOADER_2ND_MAGIC_LZMA, - LOADER_2ND_MAGIC_LZ4, -] - -IMAGE_ALIGN = 512 -PARAM1_SIZE = 0x1000 -PARAM1_SIZE_WO_SIG = 0x800 -PARAM2_SIZE = 0x1000 - - -def round_up(divident, divisor): - return ((divident + divisor - 1) // divisor) * divisor - - -def lzma_compress(body): - z = lzma.LZMACompressor(lzma.FORMAT_ALONE, preset=lzma.PRESET_EXTREME) - compressed = z.compress(body) - compressed += z.flush() - - return compressed - - -def lz4_compress(body): - try: - import lz4.frame - except ImportError: - logging.error("lz4 is not installed. Run 'pip install lz4'.") - raise - - compressed = lz4.frame.compress(body) - return compressed - - -class Entry: - __slots__ = "name", "type", "addr", "_content", "entry_size" - - def __init__(self): - self.addr = None - self._content = None - - @property - def end(self): - return self.addr + self.entry_size - - @property - def content(self): - return self._content - - @content.setter - def content(self, value): - if type(value) == int: - value = value.to_bytes(self.entry_size, "little") - - if self.entry_size is not None: - if len(value) > self.entry_size: - raise ValueError("%s (%d bytes) must <= %#r" % (self.name, len(value), self.entry_size)) - value = value + b"\0" * (self.entry_size - len(value)) - - self._content = value - - @classmethod - def make(cls, name, entry_size, _type, init=None): - entry = Entry() - entry.name = name - entry.type = _type - entry.entry_size = entry_size - - if type(init) in (bytes, bytearray): - entry.content = bytes(init) - elif entry_size is not None: - entry.content = b"\0" * entry.entry_size - else: - entry.content = b"" - - return (name, entry) - - def toint(self): - if self.type != int: - raise TypeError("%s is not int type" % self.name) - - return int.from_bytes(self.content, "little") - - def tostr(self): - v = self.content - if self.type == int: - v = "%#08x" % self.toint() - elif type(self.content) in [bytes, bytearray]: - v = v.hex() - if len(v) > 32: - v = v[:32] + "..." - - return v - - def __str__(self): - v = self.tostr() - return "<%s=%s (%dbytes)>" % (self.name, v, self.entry_size) - - def __repr__(self): - v = self.tostr() - return "<%s: a=%#x s=%#x c=%s %r>" % (self.name, self.addr, self.entry_size, v, self.type) - - -class FIP: - param1 = OrderedDict( - [ - Entry.make("MAGIC1", 8, int, b"CVBL01\n\0"), - Entry.make("MAGIC2", 4, int), - Entry.make("PARAM_CKSUM", 4, int), - Entry.make("NAND_INFO", 128, int), - Entry.make("NOR_INFO", 36, int), - Entry.make("FIP_FLAGS", 8, int), - Entry.make("CHIP_CONF_SIZE", 4, int), - Entry.make("BLCP_IMG_CKSUM", 4, int), - Entry.make("BLCP_IMG_SIZE", 4, int), - Entry.make("BLCP_IMG_RUNADDR", 4, int), - Entry.make("BLCP_PARAM_LOADADDR", 4, int), - Entry.make("BLCP_PARAM_SIZE", 4, int), - Entry.make("BL2_IMG_CKSUM", 4, int), - Entry.make("BL2_IMG_SIZE", 4, int), - Entry.make("BLD_IMG_SIZE", 4, int), - Entry.make("PARAM2_LOADADDR", 4, int), - Entry.make("RESERVED1", 4, int), - Entry.make("CHIP_CONF", 760, bytes), - Entry.make("BL_EK", 32, bytes), - Entry.make("ROOT_PK", 512, bytes), - Entry.make("BL_PK", 512, bytes), - Entry.make("BL_PK_SIG", 512, bytes), - Entry.make("CHIP_CONF_SIG", 512, bytes), - Entry.make("BL2_IMG_SIG", 512, bytes), - Entry.make("BLCP_IMG_SIG", 512, bytes), - ] - ) - - body1 = OrderedDict( - [ - Entry.make("BLCP", None, bytes), - Entry.make("BL2", None, bytes), - ] - ) - - param2 = OrderedDict( - [ - Entry.make("MAGIC1", 8, int, b"CVLD02\n\0"), - Entry.make("PARAM2_CKSUM", 4, int), - Entry.make("RESERVED1", 4, bytes), - # DDR param - Entry.make("DDR_PARAM_CKSUM", 4, int), - Entry.make("DDR_PARAM_LOADADDR", 4, int), - Entry.make("DDR_PARAM_SIZE", 4, int), - Entry.make("DDR_PARAM_RESERVED", 4, int), - # BLCP_2ND - Entry.make("BLCP_2ND_CKSUM", 4, int), - Entry.make("BLCP_2ND_LOADADDR", 4, int), - Entry.make("BLCP_2ND_SIZE", 4, int), - Entry.make("BLCP_2ND_RUNADDR", 4, int), - # ATF-BL31 or OpenSBI - Entry.make("MONITOR_CKSUM", 4, int), - Entry.make("MONITOR_LOADADDR", 4, int), - Entry.make("MONITOR_SIZE", 4, int), - Entry.make("MONITOR_RUNADDR", 4, int), - # u-boot - Entry.make("LOADER_2ND_RESERVED0", 4, int), - Entry.make("LOADER_2ND_LOADADDR", 4, int), - Entry.make("LOADER_2ND_RESERVED1", 4, int), - Entry.make("LOADER_2ND_RESERVED2", 4, int), - # Reserved - Entry.make("RESERVED_LAST", 4096 - 16 * 5, bytes), - ] - ) - - body2 = OrderedDict( - [ - Entry.make("DDR_PARAM", None, bytes), - Entry.make("BLCP_2ND", None, bytes), - Entry.make("MONITOR", None, bytes), - Entry.make("LOADER_2ND", None, bytes), - ] - ) - - ldr_2nd_hdr = OrderedDict( - [ - Entry.make("JUMP0", 4, int), - Entry.make("MAGIC", 4, int), - Entry.make("CKSUM", 4, int), - Entry.make("SIZE", 4, int), - Entry.make("RUNADDR", 8, int), - Entry.make("RESERVED1", 4, int), - Entry.make("RESERVED2", 4, int), - ] - ) - - FIP_FLAGS_SCS_MASK = 0x000c - FIP_FLAGS_ENCRYPTED_MASK = 0x0030 - - def _param_size(self, param): - return max((e.end for e in param.values())) - - def _gen_param(self): - addr = 0 - for entry in self.param1.values(): - entry.addr = addr - addr += entry.entry_size - - assert PARAM1_SIZE_WO_SIG == self.param1["BL_PK_SIG"].addr - - addr = 0 - for entry in self.param2.values(): - entry.addr = addr - addr += entry.entry_size - - assert PARAM2_SIZE == self.param2["RESERVED_LAST"].addr + self.param2["RESERVED_LAST"].entry_size - - addr = 0 - for entry in self.ldr_2nd_hdr.values(): - entry.addr = addr - addr += entry.entry_size - - def __init__(self): - self.compress_algo = None - self._gen_param() - - def image_crc(self, image): - crc = binascii.crc_hqx(image, 0) - crc = pack(" rtt.asm\n' POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n' POST_ACTION += OBJCPY + ' -O binary $TARGET Image \n' + SIZE + ' $TARGET \n' -POST_ACTION += './combine.sh && cd .. && bash mksdimg.sh ' + os.getcwd() + ' Image \n' +POST_ACTION += './combine.sh && ./mksdimg.sh ' + os.getcwd() + ' Image \n' -- cgit v1.3.1