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 [pi02]
28 kernel=u-boot-rpi3.bin
29
30 [pi4]
31 kernel=u-boot-rpi4.bin
32 enable_gic=1
33 armstub=armstub8-gic.bin
34
35 # Otherwise the resolution will be weird in most cases, compared to
36 # what the pi3 firmware does by default.
37 disable_overscan=1
38
39 # Supported in newer board revisions
40 arm_boost=1
41
42 [cm4]
43 # Enable host mode on the 2711 built-in XHCI USB controller.
44 # This line should be removed if the legacy DWC2 controller is required
45 # (e.g. for USB device mode) or if USB support is not required.
46 otg_mode=1
47
48 [all]
49 # Boot in 64-bit mode.
50 arm_64bit=1
51
52 # U-Boot needs this to work, regardless of whether UART is actually used or not.
53 # Look in arch/arm/mach-bcm283x/Kconfig in the U-Boot tree to see if this is still
54 # a requirement in the future.
55 enable_uart=1
56
57 # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
58 # when attempting to show low-voltage or overtemperature warnings.
59 avoid_warnings=1
60 '';
61 in ''
62 (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
63
64 # Add the config
65 cp ${configTxt} firmware/config.txt
66
67 # Add pi3 specific files
68 cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
69
70 # Add pi4 specific files
71 cp ${pkgs.ubootRaspberryPi4_64bit}/u-boot.bin firmware/u-boot-rpi4.bin
72 cp ${pkgs.raspberrypi-armstubs}/armstub8-gic.bin firmware/armstub8-gic.bin
73 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-4-b.dtb firmware/
74 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-400.dtb firmware/
75 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-cm4.dtb firmware/
76 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-cm4s.dtb firmware/
77 '';
78 populateRootCommands = ''
79 mkdir -p ./files/boot
80 ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
81 '';
82 };
83}