From 67f257e5243c5a4f09a21890451c8c411461f92b Mon Sep 17 00:00:00 2001 From: Travis Staton Date: Thu, 23 Feb 2023 20:18:42 -0500 Subject: [PATCH] use nix based config.txt generation --- rpi/config.nix | 13 ++++++--- rpi/default.nix | 19 ++++++++---- sd-image/default.nix | 69 +++----------------------------------------- 3 files changed, 26 insertions(+), 75 deletions(-) diff --git a/rpi/config.nix b/rpi/config.nix index 19efc94..9afec3d 100644 --- a/rpi/config.nix +++ b/rpi/config.nix @@ -22,11 +22,16 @@ let args = render-dt-kvs v; }) overlays); render-config-section = k: - { options, base-dtb-params, dt-overlays }: '' + { options, base-dtb-params, dt-overlays }: + let + all-config = lib.concatStringsSep "\n" (lib.filter (x: x != "") [ + (render-options options) + (render-base-dt-params base-dtb-params) + (render-dt-overlays dt-overlays) + ]); + in '' [${k}] - ${render-options options} - ${render-base-dt-params base-dtb-params} - ${render-dt-overlays dt-overlays} + ${all-config} ''; in conf: lib.strings.concatStringsSep "\n" diff --git a/rpi/default.nix b/rpi/default.nix index 7a8186f..8ca7cb7 100644 --- a/rpi/default.nix +++ b/rpi/default.nix @@ -4,6 +4,17 @@ { imports = [ ../sd-image ./config.nix ]; + system.activationScripts.raspberrypi = { + text = '' + if ! grep -qs '/boot/firmware ' /proc/mounts; then + mount /dev/disk/by-label/${config.sdImage.firmwarePartitionName} /boot/firmware + fi + cp ${pkgs.uboot_rpi_arm64}/u-boot.bin /boot/firmware/u-boot-rpi-arm64.bin + cp -r ${pkgs.raspberrypifw}/share/raspberrypi/boot/{start*.elf,*.dtb,bootcode.bin,fixup*.dat,overlays} /boot/firmware + cp ${config.raspberrypi-config-output} /boot/firmware/config.txt + ''; + }; + raspberrypi-config = { pi4 = { options = { @@ -17,16 +28,12 @@ pi02 = { dt-overlays = { vc4-kms-v3d = { cma-256 = null; }; }; }; all = { options = { - kernel = "u-boot-rpi_arm64.bin"; + kernel = "u-boot-rpi-arm64.bin"; enable_uart = true; avoid_warnings = true; arm_64bit = true; }; - base-dtb-params = { - i2c = "on"; - audio = "on"; - krnbt = "on"; - }; + base-dtb-params = { krnbt = "on"; }; }; }; diff --git a/sd-image/default.nix b/sd-image/default.nix index eb57c26..0c44d91 100644 --- a/sd-image/default.nix +++ b/sd-image/default.nix @@ -2,12 +2,6 @@ { imports = [ ./sd-image.nix ]; - options.hardware.raspberry-pi = { - extra-config = lib.mkOption { - type = lib.types.str; - default = ""; - }; - }; config = { boot.loader.grub.enable = false; @@ -19,65 +13,10 @@ boot.kernelParams = [ "console=serial0,115200n8" "console=tty1" ]; sdImage = { - populateFirmwareCommands = let - inherit (pkgs) raspberrypifw; - configTxt = pkgs.writeText "config.txt" '' - [pi02] - kernel=u-boot-rpi_arm64.bin - - [pi3+] - kernel=u-boot-rpi_arm64.bin - - [pi4] - kernel=u-boot-rpi_arm64.bin - enable_gic=1 - armstub=armstub8-gic.bin - arm_boost=1 - dtoverlay=vc4-fkms-v3d-pi4 - dtoverlay= - - # Otherwise the resolution will be weird in most cases, compared to - # what the pi3 firmware does by default. - disable_overscan=1 - - [all] - # Boot in 64-bit mode. - arm_64bit=1 - dtparam=audio=on - dtparam=krnbt=on - - # U-Boot needs this to work, regardless of whether UART is actually used or not. - # Look in arch/arm/mach-bcm283x/Kconfig in the U-Boot tree to see if this is still - # a requirement in the future. - enable_uart=1 - - # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel - # when attempting to show low-voltage or overtemperature warnings. - avoid_warnings=1 - ${config.hardware.raspberry-pi.extra-config} - ''; - in '' - (cd ${raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/) - - # Add the config - cp ${configTxt} firmware/config.txt - - # Add rpi generic u-boot - cp ${pkgs.uboot_rpi_arm64}/u-boot.bin firmware/u-boot-rpi_arm64.bin - - # Add pi3 specific files - cp ${raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-3-b-plus.dtb firmware/ - - # Add pi4 specific files - cp ${pkgs.raspberrypi-armstubs}/armstub8-gic.bin firmware/armstub8-gic.bin - cp ${raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-4-b.dtb firmware/ - - # Add pi-zero-2 specific files - cp ${raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-zero-2.dtb firmware/ - cp ${raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-zero-2-w.dtb firmware/ - - # Add overlays - cp -r ${raspberrypifw}/share/raspberrypi/boot/overlays firmware/ + populateFirmwareCommands = '' + cp ${pkgs.uboot_rpi_arm64}/u-boot.bin firmware/u-boot-rpi-arm64.bin + cp -r ${pkgs.raspberrypifw}/share/raspberrypi/boot/{start*.elf,*.dtb,bootcode.bin,fixup*.dat,overlays} firmware + cp ${config.raspberrypi-config-output} firmware/config.txt ''; populateRootCommands = '' mkdir -p ./files/boot