1# To build, use:
2# nix-build nixos -I nixos-config=nixos/modules/installer/sd-card/sd-image-aarch64.nix -A config.system.build.sdImage
3{ config, lib, pkgs, ... }:
4
5{
6 imports = [
7 ../../profiles/base.nix
8 ./sd-image.nix
9 ];
10
11 boot.loader.grub.enable = false;
12 boot.loader.generic-extlinux-compatible.enable = true;
13
14 boot.consoleLogLevel = lib.mkDefault 7;
15
16 # The serial ports listed here are:
17 # - ttyS0: for Tegra (Jetson TX1)
18 # - ttyAMA0: for QEMU's -machine virt
19 boot.kernelParams = ["console=ttyS0,115200n8" "console=ttyAMA0,115200n8" "console=tty0"];
20
21 sdImage = {
22 populateFirmwareCommands = let
23 configTxt = pkgs.writeText "config.txt" ''
24 [pi3]
25 kernel=u-boot-rpi3.bin
26
27 [pi4]
28 kernel=u-boot-rpi4.bin
29 enable_gic=1
30 armstub=armstub8-gic.bin
31
32 # Otherwise the resolution will be weird in most cases, compared to
33 # what the pi3 firmware does by default.
34 disable_overscan=1
35
36 [all]
37 # Boot in 64-bit mode.
38 arm_64bit=1
39
40 # U-Boot needs this to work, regardless of whether UART is actually used or not.
41 # Look in arch/arm/mach-bcm283x/Kconfig in the U-Boot tree to see if this is still
42 # a requirement in the future.
43 enable_uart=1
44
45 # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
46 # when attempting to show low-voltage or overtemperature warnings.
47 avoid_warnings=1
48 '';
49 in ''
50 (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
51
52 # Add the config
53 cp ${configTxt} firmware/config.txt
54
55 # Add pi3 specific files
56 cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
57
58 # Add pi4 specific files
59 cp ${pkgs.ubootRaspberryPi4_64bit}/u-boot.bin firmware/u-boot-rpi4.bin
60 cp ${pkgs.raspberrypi-armstubs}/armstub8-gic.bin firmware/armstub8-gic.bin
61 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-4-b.dtb firmware/
62 '';
63 populateRootCommands = ''
64 mkdir -p ./files/boot
65 ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
66 '';
67 };
68}