From 25ef8fa486060eb3d5f5dfc5a1af9d0a126544dd Mon Sep 17 00:00:00 2001 From: Florian Agbuya Date: Fri, 16 Feb 2024 13:13:05 +0800 Subject: [PATCH] refactor build script for better board management --- flake.nix | 414 +++++++++++++++++++++++++++--------------------------- 1 file changed, 206 insertions(+), 208 deletions(-) diff --git a/flake.nix b/flake.nix index 54f8626..0ab1c60 100644 --- a/flake.nix +++ b/flake.nix @@ -169,167 +169,6 @@ hardeningDisable = [ "fortify" ]; }; - fsbl = { board ? "zc706" }: pkgs.stdenv.mkDerivation { - name = "${board}-fsbl"; - src = pkgs.fetchFromGitHub { - owner = "Xilinx"; - repo = "embeddedsw"; - rev = "xilinx_v2022.2"; - sha256 = "sha256-UDz9KK/Hw3qM1BAeKif30rE8Bi6C2uvuZlvyvtJCMfw="; - }; - nativeBuildInputs = [ - pkgs.gnumake - gnutoolchain.binutils - gnutoolchain.gcc - ]; - postUnpack = '' - mkdir -p $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo - cp $sourceRoot/lib/sw_apps/zynq_fsbl/misc/zc706/* $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo - cp ${fsbl-support}/* $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo - ''; - patches = [] ++ pkgs.lib.optional (board == "fast-servo") ./fast-servo/fsbl.patch; - postPatch = '' - patchShebangs lib/sw_apps/zynq_fsbl/misc/copy_bsp.sh - echo 'SEARCH_DIR("${gnutoolchain.newlib}/arm-none-eabi/lib");' >> lib/sw_apps/zynq_fsbl/src/lscript.ld - ''; - buildPhase = '' - cd lib/sw_apps/zynq_fsbl/src - make BOARD=${board} "CFLAGS=-DFSBL_DEBUG_INFO -g" - ''; - installPhase = '' - mkdir $out - cp fsbl.elf $out - ''; - doCheck = false; - dontFixup = true; - }; - - u-boot = { board ? "zc706" }: (pkgs.pkgsCross.armv7l-hf-multiplatform.buildUBoot { - defconfig = "xilinx_zynq_virt_defconfig"; - patches = [] ++ pkgs.lib.optional (board == "fast-servo") ./fast-servo/u-boot.patch; - preConfigure = '' - export DEVICE_TREE=zynq-${board} - ''; - extraConfig = '' - CONFIG_SYS_PROMPT="${board}-boot> " - CONFIG_AUTOBOOT=y - CONFIG_BOOTCOMMAND="${builtins.replaceStrings [ "\n" ] [ "; " ] '' - setenv bootargs 'root=/dev/mmcblk0p2 console=ttyPS0,115200n8 systemConfig=${builtins.unsafeDiscardStringContext not-os-cfg.build.toplevel}' - fatload mmc 0 0x6400000 uImage - fatload mmc 0 0x8000000 ${board}.dtb - fatload mmc 0 0xA400000 uRamdisk.image.gz - bootm 0x6400000 0xA400000 0x8000000 - ''}" - CONFIG_BOOTDELAY=0 - CONFIG_USE_BOOTCOMMAND=y - ''; - extraMeta.platforms = [ "armv7l-linux" ]; - filesToInstall = [ "u-boot.elf" ]; - }).overrideAttrs (oldAttrs: { - postUnpack = '' - cp ${dts-support}/fast-servo.dts $sourceRoot/arch/arm/dts/zynq-fast-servo.dts - ''; - postInstall = '' - mkdir -p $out/dts - cp arch/arm/dts/zynq-fast-servo.dts $out/dts - cp arch/arm/dts/zynq-zc706.dts $out/dts - cp arch/arm/dts/zynq-7000.dtsi $out/dts - ''; - }); - - bootimage = { board ? "zc706" }: pkgs.runCommand "${board}-bootimage" - { - buildInputs = [ mkbootimage ]; - } - '' - bifdir=`mktemp -d` - cd $bifdir - ln -s ${fsbl { inherit board; }}/fsbl.elf fsbl.elf - ln -s ${u-boot { inherit board; }}/u-boot.elf u-boot.elf - cat > boot.bif << EOF - the_ROM_image: - { - [bootloader]fsbl.elf - u-boot.elf - } - EOF - mkdir $out $out/nix-support - mkbootimage boot.bif $out/boot.bin - echo file binary-dist $out/boot.bin >> $out/nix-support/hydra-build-products - ''; - - dtb = { board ? "zc706" }: pkgs.runCommand "dtb" - { - buildInputs = [ pkgs.gcc pkgs.dtc ]; - } - '' - mkdir -p $out - DTSDIR=$(mktemp -d /tmp/dts-XXXXXX) - cd $DTSDIR - cp ${u-boot { inherit board; }}/dts/zynq-${board}.dts . - - if [ ${board} == "zc706" ]; then - mv zynq-${board}.dts zynq-${board}-top.dts - cp ${u-boot { inherit board; }}/dts/zynq-7000.dtsi . - gcc -E -nostdinc -undef -D__DTS__ -x assembler-with-cpp -o zynq-${board}.dts zynq-${board}-top.dts - fi - - dtc -I dts -O dtb -o ${board}.dtb zynq-${board}.dts - cp ${board}.dtb $out - rm -rf $DTSDIR - ''; - - sd-image = { board ? "zc706" }: let - rootfsImage = pkgs.callPackage (pkgs.path + "/nixos/lib/make-ext4-fs.nix") { - storePaths = [ not-os-cfg.build.toplevel ]; - volumeLabel = "ROOT"; - }; - # Current firmware (kernel, bootimage, etc..) takes ~18MB - firmwareSize = 30; - firmwarePartitionOffset = 8; - in pkgs.stdenv.mkDerivation { - name = "sd-image"; - nativeBuildInputs = with pkgs; [ dosfstools mtools libfaketime util-linux parted ]; - buildCommand = '' - mkdir -p $out/nix-support $out/sd-image - export img=$out/sd-image/sd-image.img - - echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system - echo "file sd-image $img" >> $out/nix-support/hydra-build-products - - gap=${toString firmwarePartitionOffset} - - rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }') - firmwareSizeBlocks=$((${toString firmwareSize} * 1024 * 1024 / 512)) - imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024)) - truncate -s $imageSize $img - - fat32Start="$((gap))MB" - fat32End="$((gap + ${toString firmwareSize}))MB" - - parted $img mklabel msdos - parted $img mkpart primary fat32 $fat32Start $fat32End - parted $img mkpart primary ext4 $fat32End 100% - parted $img set 1 boot on - - eval $(partx $img -o START,SECTORS --nr 2 --pairs) - dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS - - eval $(partx $img -o START,SECTORS --nr 1 --pairs) - truncate -s $((SECTORS * 512)) firmware_part.img - faketime "1970-01-01 00:00:00" mkfs.vfat -n BOOT firmware_part.img - - mkdir firmware - cp ${bootimage { inherit board; }}/boot.bin firmware/ - cp ${dtb { inherit board; }}/${board}.dtb firmware/ - cp ${not-os-cfg.build.kernel}/uImage firmware/ - cp ${not-os-cfg.build.uRamdisk}/initrd firmware/uRamdisk.image.gz - - (cd firmware; mcopy -psvm -i ../firmware_part.img ./* ::) - dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS - ''; - }; - # Pinned qemu version due to networking errors in recent version 8.2.0 qemu = pkgs.qemu.overrideAttrs (oldAttrs: rec { version = "8.1.3"; @@ -339,60 +178,219 @@ }; }); - not-os-qemu = { board ? "zc706" }: let - qemuScript = '' - #!/bin/bash - export PATH=${qemu}/bin:$PATH - IMGDIR=$(mktemp -d /tmp/not-os-qemu-XXXXXX) - BASE=$(realpath $(dirname $0)) - qemu-img create -F raw -f qcow2 -b $BASE/sd-image.img $IMGDIR/sd-overlay.qcow2 512M + board-package-set = { board }: let + fsbl = pkgs.stdenv.mkDerivation { + name = "${board}-fsbl"; + src = pkgs.fetchFromGitHub { + owner = "Xilinx"; + repo = "embeddedsw"; + rev = "xilinx_v2022.2"; + sha256 = "sha256-UDz9KK/Hw3qM1BAeKif30rE8Bi6C2uvuZlvyvtJCMfw="; + }; + nativeBuildInputs = [ + pkgs.gnumake + gnutoolchain.binutils + gnutoolchain.gcc + ]; + postUnpack = '' + mkdir -p $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo + cp $sourceRoot/lib/sw_apps/zynq_fsbl/misc/zc706/* $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo + cp ${fsbl-support}/* $sourceRoot/lib/sw_apps/zynq_fsbl/misc/fast-servo + ''; + patches = [] ++ pkgs.lib.optional (board == "fast-servo") ./fast-servo/fsbl.patch; + postPatch = '' + patchShebangs lib/sw_apps/zynq_fsbl/misc/copy_bsp.sh + echo 'SEARCH_DIR("${gnutoolchain.newlib}/arm-none-eabi/lib");' >> lib/sw_apps/zynq_fsbl/src/lscript.ld + ''; + buildPhase = '' + cd lib/sw_apps/zynq_fsbl/src + make BOARD=${board} "CFLAGS=-DFSBL_DEBUG_INFO -g" + ''; + installPhase = '' + mkdir $out + cp fsbl.elf $out + ''; + doCheck = false; + dontFixup = true; + }; - # Some command arguments are based from samples in Xilinx QEMU User Documentation - # See: https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/821854273/Running+Bare+Metal+Applications+on+QEMU + u-boot = (pkgs.pkgsCross.armv7l-hf-multiplatform.buildUBoot { + defconfig = "xilinx_zynq_virt_defconfig"; + patches = [] ++ pkgs.lib.optional (board == "fast-servo") ./fast-servo/u-boot.patch; + preConfigure = '' + export DEVICE_TREE=zynq-${board} + ''; + extraConfig = '' + CONFIG_SYS_PROMPT="${board}-boot> " + CONFIG_AUTOBOOT=y + CONFIG_BOOTCOMMAND="${builtins.replaceStrings [ "\n" ] [ "; " ] '' + setenv bootargs 'root=/dev/mmcblk0p2 console=ttyPS0,115200n8 systemConfig=${builtins.unsafeDiscardStringContext not-os-cfg.build.toplevel}' + fatload mmc 0 0x6400000 uImage + fatload mmc 0 0x8000000 ${board}.dtb + fatload mmc 0 0xA400000 uRamdisk.image.gz + bootm 0x6400000 0xA400000 0x8000000 + ''}" + CONFIG_BOOTDELAY=0 + CONFIG_USE_BOOTCOMMAND=y + ''; + extraMeta.platforms = [ "armv7l-linux" ]; + filesToInstall = [ "u-boot.elf" ]; + }).overrideAttrs (oldAttrs: { + postUnpack = '' + cp ${dts-support}/fast-servo.dts $sourceRoot/arch/arm/dts/zynq-fast-servo.dts + ''; + postInstall = '' + mkdir -p $out/dts + cp arch/arm/dts/zynq-fast-servo.dts $out/dts + cp arch/arm/dts/zynq-zc706.dts $out/dts + cp arch/arm/dts/zynq-7000.dtsi $out/dts + ''; + }); - qemu-system-arm \ - -M xilinx-zynq-a9 \ - -m 1024 \ - $([ ${board} = "zc706" ] && echo "-serial /dev/null") -serial stdio \ - -display none \ - -kernel $BASE/u-boot.elf \ - -sd $IMGDIR/sd-overlay.qcow2 + bootimage = pkgs.runCommand "${board}-bootimage" + { + buildInputs = [ mkbootimage ]; + } + '' + bifdir=`mktemp -d` + cd $bifdir + ln -s ${fsbl}/fsbl.elf fsbl.elf + ln -s ${u-boot}/u-boot.elf u-boot.elf + cat > boot.bif << EOF + the_ROM_image: + { + [bootloader]fsbl.elf + u-boot.elf + } + EOF + mkdir $out $out/nix-support + mkbootimage boot.bif $out/boot.bin + echo file binary-dist $out/boot.bin >> $out/nix-support/hydra-build-products + ''; - rm -rf $IMGDIR - ''; - in pkgs.runCommand "not-os-qemu" { - inherit qemuScript; - passAsFile = [ "qemuScript" ]; - preferLocalBuild = true; - } - '' - mkdir $out - cd $out - cp -s ${u-boot { inherit board; }}/u-boot.elf . - cp -s ${sd-image { inherit board; }}/sd-image/sd-image.img . - cp $qemuScriptPath qemu-script - chmod +x qemu-script - patchShebangs qemu-script - ''; + dtb = pkgs.runCommand "dtb" + { + buildInputs = [ pkgs.gcc pkgs.dtc ]; + } + '' + mkdir -p $out + DTSDIR=$(mktemp -d /tmp/dts-XXXXXX) + cd $DTSDIR + cp ${u-boot}/dts/zynq-${board}.dts . + + if [ ${board} == "zc706" ]; then + mv zynq-${board}.dts zynq-${board}-top.dts + cp ${u-boot}/dts/zynq-7000.dtsi . + gcc -E -nostdinc -undef -D__DTS__ -x assembler-with-cpp -o zynq-${board}.dts zynq-${board}-top.dts + fi + + dtc -I dts -O dtb -o ${board}.dtb zynq-${board}.dts + cp ${board}.dtb $out + rm -rf $DTSDIR + ''; + + sd-image = let + rootfsImage = pkgs.callPackage (pkgs.path + "/nixos/lib/make-ext4-fs.nix") { + storePaths = [ not-os-cfg.build.toplevel ]; + volumeLabel = "ROOT"; + }; + # Current firmware (kernel, bootimage, etc..) takes ~18MB + firmwareSize = 30; + firmwarePartitionOffset = 8; + in pkgs.stdenv.mkDerivation { + name = "sd-image"; + nativeBuildInputs = with pkgs; [ dosfstools mtools libfaketime util-linux parted ]; + buildCommand = '' + mkdir -p $out/nix-support $out/sd-image + export img=$out/sd-image/sd-image.img + + echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system + echo "file sd-image $img" >> $out/nix-support/hydra-build-products + + gap=${toString firmwarePartitionOffset} + + rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }') + firmwareSizeBlocks=$((${toString firmwareSize} * 1024 * 1024 / 512)) + imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024)) + truncate -s $imageSize $img + + fat32Start="$((gap))MB" + fat32End="$((gap + ${toString firmwareSize}))MB" + + parted $img mklabel msdos + parted $img mkpart primary fat32 $fat32Start $fat32End + parted $img mkpart primary ext4 $fat32End 100% + parted $img set 1 boot on + + eval $(partx $img -o START,SECTORS --nr 2 --pairs) + dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS + + eval $(partx $img -o START,SECTORS --nr 1 --pairs) + truncate -s $((SECTORS * 512)) firmware_part.img + faketime "1970-01-01 00:00:00" mkfs.vfat -n BOOT firmware_part.img + + mkdir firmware + cp ${bootimage}/boot.bin firmware/ + cp ${dtb}/${board}.dtb firmware/ + cp ${not-os-cfg.build.kernel}/uImage firmware/ + cp ${not-os-cfg.build.uRamdisk}/initrd firmware/uRamdisk.image.gz + + (cd firmware; mcopy -psvm -i ../firmware_part.img ./* ::) + dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS + ''; + }; + + not-os-qemu = let + qemuScript = '' + #!/bin/bash + export PATH=${qemu}/bin:$PATH + IMGDIR=$(mktemp -d /tmp/not-os-qemu-XXXXXX) + BASE=$(realpath $(dirname $0)) + qemu-img create -F raw -f qcow2 -b $BASE/sd-image.img $IMGDIR/sd-overlay.qcow2 512M + + # Some command arguments are based from samples in Xilinx QEMU User Documentation + # See: https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/821854273/Running+Bare+Metal+Applications+on+QEMU + + qemu-system-arm \ + -M xilinx-zynq-a9 \ + -m 1024 \ + $([ ${board} = "zc706" ] && echo "-serial /dev/null") -serial stdio \ + -display none \ + -kernel $BASE/u-boot.elf \ + -sd $IMGDIR/sd-overlay.qcow2 + + rm -rf $IMGDIR + ''; + in pkgs.runCommand "not-os-qemu" { + inherit qemuScript; + passAsFile = [ "qemuScript" ]; + preferLocalBuild = true; + } + '' + mkdir $out + cd $out + cp -s ${u-boot}/u-boot.elf . + cp -s ${sd-image}/sd-image/sd-image.img . + cp $qemuScriptPath qemu-script + chmod +x qemu-script + patchShebangs qemu-script + ''; + in { + "${board}-fsbl" = fsbl; + "${board}-u-boot" = u-boot; + "${board}-bootimage" = bootimage; + "${board}-dtb" = dtb; + "${board}-sd-image" = sd-image; + "${board}-qemu" = not-os-qemu; + }; in rec { packages.x86_64-linux = { inherit mkbootimage; }; - packages.armv7l-linux = { - not-os = not-os-cfg.build.zynq_image; - zc706-fsbl = fsbl { board = "zc706"; }; - zc706-u-boot = u-boot { board = "zc706"; }; - zc706-bootimage = bootimage { board = "zc706"; }; - zc706-dtb = dtb { board = "zc706"; }; - zc706-sd-image = sd-image { board = "zc706"; }; - zc706-qemu = not-os-qemu { board = "zc706"; }; - fast-servo-fsbl = fsbl { board = "fast-servo"; }; - fast-servo-u-boot = u-boot { board = "fast-servo"; }; - fast-servo-bootimage = bootimage { board = "fast-servo"; }; - fast-servo-dtb = dtb { board = "fast-servo"; }; - fast-servo-sd-image = sd-image { board = "fast-servo"; }; - fast-servo-qemu = not-os-qemu { board = "fast-servo"; }; - }; + packages.armv7l-linux = + (board-package-set { board = "zc706"; }) // + (board-package-set { board = "fast-servo"; }); + hydraJobs = packages.x86_64-linux // packages.armv7l-linux; };