| 1 | #!/bin/bash -eux |
| 2 | |
| 3 | # |
| 4 | # SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 5 | # |
| 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | # |
| 8 | |
| 9 | BUILD_CACHE=${BUILD_CACHE:-${HOME}/.cache/kleidiai} |
| 10 | download_and_extract() |
| 11 | { |
| 12 | URL="$1" |
| 13 | FOLDER="$2" |
| 14 | ARCHIVE="${3:-$(basename $1)}" |
| 15 | |
| 16 | wget -cO ${BUILD_CACHE}/${ARCHIVE} "$URL" |
| 17 | mkdir -p ${FOLDER} |
| 18 | tar -xa -f ${BUILD_CACHE}/${ARCHIVE} --strip-components=1 -C ${FOLDER} |
| 19 | } |
| 20 | |
| 21 | TARGETARCH=${TARGETARCH:-$(if [ "`uname -m`" == "aarch64" ]; then echo "arm64"; else echo "amd64"; fi)} |
| 22 | |
| 23 | # This script is used by Dockerfile to create a Linux bootloader with the latest Linux kernel. |
| 24 | if [ "`uname -s`" = "Darwin" ]; then |
| 25 | HOST_ARCH=darwin-arm64 |
| 26 | TARGETARCH=arm64 |
| 27 | elif [ "${TARGETARCH}" = "amd64" ] ; then |
| 28 | HOST_ARCH=x86_64 |
| 29 | elif [ "${TARGETARCH}" = "arm64" ] ; then |
| 30 | HOST_ARCH=aarch64 |
| 31 | else |
| 32 | echo "Unknown $TARGETARCH" && exit 1 |
| 33 | fi |
| 34 | |
| 35 | TOOLCHAIN_VERSION=14.3.rel1 |
| 36 | TOOLCHAIN_TYPE=aarch64-none-elf |
| 37 | TOOLCHAIN_DIR=$(pwd)/toolchain-${TOOLCHAIN_TYPE} |
| 38 | CROSS_COMPILE=${TOOLCHAIN_DIR}/bin/${TOOLCHAIN_TYPE}- |
| 39 | KERNEL_VERSION=6.16 |
| 40 | # Derive DTS version from kernel version |
| 41 | DTS_VERSION=$(echo $KERNEL_VERSION | cut -d '.' -f 1,2) |
| 42 | BOOTLOADER_VERSION=785302c1f7b9eceab3b72a8cb3d79eaf526fd2e3 |
| 43 | |
| 44 | mkdir -p ${BUILD_CACHE} |
| 45 | |
| 46 | # Downloads tools and source code. |
| 47 | # Download Arm toolchain |
| 48 | download_and_extract \ |
| 49 | "https://developer.arm.com/-/media/Files/downloads/gnu/${TOOLCHAIN_VERSION}/binrel/arm-gnu-toolchain-${TOOLCHAIN_VERSION}-${HOST_ARCH}-${TOOLCHAIN_TYPE}.tar.xz" \ |
| 50 | "${TOOLCHAIN_DIR}" |
| 51 | |
| 52 | # Download Linux Kernel |
| 53 | if [[ "${KERNEL_VERSION}" =~ "-rc" ]]; then |
| 54 | download_and_extract \ |
| 55 | "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/snapshot/linux-${KERNEL_VERSION}.tar.gz" \ |
| 56 | "linux-${KERNEL_VERSION}" |
| 57 | else |
| 58 | download_and_extract \ |
| 59 | "https://cdn.kernel.org/pub/linux/kernel/v$(echo $KERNEL_VERSION | cut -d '.' -f 1).x/linux-${KERNEL_VERSION}.tar.xz" \ |
| 60 | "linux-${KERNEL_VERSION}" |
| 61 | fi |
| 62 | |
| 63 | # Download booloader |
| 64 | download_and_extract \ |
| 65 | "https://git.kernel.org/pub/scm/linux/kernel/git/mark/boot-wrapper-aarch64.git/snapshot/boot-wrapper-aarch64-${BOOTLOADER_VERSION}.tar.gz" \ |
| 66 | boot-wrapper-aarch64 |
| 67 | |
| 68 | # Download DTS tooling |
| 69 | download_and_extract \ |
| 70 | "https://git.kernel.org/pub/scm/linux/kernel/git/devicetree/devicetree-rebasing.git/snapshot/devicetree-rebasing-${DTS_VERSION}-dts.tar.gz" \ |
| 71 | devicetree-rebasing |
| 72 | |
| 73 | # Builds the Linux kernel. |
| 74 | cd linux-${KERNEL_VERSION} |
| 75 | CCACHE_DIR=${BUILD_CACHE}/ccache make ARCH=arm64 CROSS_COMPILE="ccache ${CROSS_COMPILE}" defconfig |
| 76 | CCACHE_DIR=${BUILD_CACHE}/ccache make ARCH=arm64 CROSS_COMPILE="ccache ${CROSS_COMPILE}" "-j$(nproc)" Image |
| 77 | cd .. |
| 78 | |
| 79 | # Builds the device tree. |
| 80 | cd devicetree-rebasing |
| 81 | # Reduce number of CPUs to 2 and disable second cluster to keep in sync with FVP parameters. In FVP no more |
| 82 | # than 2 CPUs are needed. cpu0 to run Linux kernel and system services. cpu1 used to run a test program in isolation. |
| 83 | cat <<EOF > src/arm64/arm/kleidiai_fvp.dts |
| 84 | /dts-v1/; |
| 85 | |
| 86 | #include "fvp-base-revc.dts" |
| 87 | |
| 88 | /delete-node/ &cpu2; |
| 89 | /delete-node/ &cpu3; |
| 90 | /delete-node/ &cpu4; |
| 91 | /delete-node/ &cpu5; |
| 92 | /delete-node/ &cpu6; |
| 93 | /delete-node/ &cpu7; |
| 94 | /delete-node/ &C1_L2; |
| 95 | |
| 96 | / { |
| 97 | /delete-node/ ete-2; |
| 98 | /delete-node/ ete-3; |
| 99 | /delete-node/ ete-4; |
| 100 | /delete-node/ ete-5; |
| 101 | /delete-node/ ete-6; |
| 102 | /delete-node/ ete-7; |
| 103 | }; |
| 104 | EOF |
| 105 | make CPP=${CROSS_COMPILE}cpp src/arm64/arm/kleidiai_fvp.dtb |
| 106 | cd .. |
| 107 | |
| 108 | # Builds the bootloader. |
| 109 | cd boot-wrapper-aarch64 |
| 110 | PATH=$(dirname ${CROSS_COMPILE}gcc):$PATH |
| 111 | export PATH |
| 112 | autoreconf -i |
| 113 | # Extra CFLAGS are needed to avoid using standard libraries when using baremetal toolchain |
| 114 | # -O2 required as otherwise boot-wrapper would not boot |
| 115 | CFLAGS="-ffreestanding -nostdlib -O2" ./configure --host=${TOOLCHAIN_TYPE} \ |
| 116 | --enable-psci \ |
| 117 | --enable-gicv3 \ |
| 118 | --with-kernel-dir=../linux-${KERNEL_VERSION} \ |
| 119 | --with-dtb=../devicetree-rebasing/src/arm64/arm/kleidiai_fvp.dtb \ |
| 120 | --with-cmdline="console=ttyAMA0 earlycon=pl011,0x1c090000 panic=1 root=/dev/vda rw isolcpus=1 init=/bin/bash -- /root/startup" |
| 121 | make "-j$(nproc)" |
| 122 | cd .. |
| 123 | |
| 124 | mv boot-wrapper-aarch64/linux-system.axf . |
| 125 | |
| 126 | # Cleans up. |
| 127 | rm -rf \ |
| 128 | ${TOOLCHAIN_DIR} \ |
| 129 | linux-${KERNEL_VERSION} \ |
| 130 | devicetree-rebasing \ |
| 131 | boot-wrapper-aarch64 |