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{
4 config,
5 lib,
6 pkgs,
7 ...
8}:
9
10{
11 imports = [
12 ../../profiles/base.nix
13 ./sd-image.nix
14 ];
15
16 boot.loader.grub.enable = false;
17 boot.loader.generic-extlinux-compatible.enable = true;
18
19 boot.consoleLogLevel = lib.mkDefault 7;
20
21 # The serial ports listed here are:
22 # - ttyS0: for Tegra (Jetson TX1)
23 # - ttyAMA0: for QEMU's -machine virt
24 boot.kernelParams = [
25 "console=ttyS0,115200n8"
26 "console=ttyAMA0,115200n8"
27 "console=tty0"
28 ];
29
30 sdImage = {
31 populateFirmwareCommands =
32 let
33 configTxt = pkgs.writeText "config.txt" ''
34 [pi3]
35 kernel=u-boot-rpi3.bin
36
37 # Otherwise the serial output will be garbled.
38 core_freq=250
39
40 [pi02]
41 kernel=u-boot-rpi3.bin
42
43 [pi4]
44 kernel=u-boot-rpi4.bin
45 enable_gic=1
46 armstub=armstub8-gic.bin
47
48 # Otherwise the resolution will be weird in most cases, compared to
49 # what the pi3 firmware does by default.
50 disable_overscan=1
51
52 # Supported in newer board revisions
53 arm_boost=1
54
55 [cm4]
56 # Enable host mode on the 2711 built-in XHCI USB controller.
57 # This line should be removed if the legacy DWC2 controller is required
58 # (e.g. for USB device mode) or if USB support is not required.
59 otg_mode=1
60
61 [all]
62 # Boot in 64-bit mode.
63 arm_64bit=1
64
65 # U-Boot needs this to work, regardless of whether UART is actually used or not.
66 # Look in arch/arm/mach-bcm283x/Kconfig in the U-Boot tree to see if this is still
67 # a requirement in the future.
68 enable_uart=1
69
70 # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
71 # when attempting to show low-voltage or overtemperature warnings.
72 avoid_warnings=1
73 '';
74 in
75 ''
76 (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
77
78 # Add the config
79 cp ${configTxt} firmware/config.txt
80
81 # Add pi3 specific files
82 cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
83 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-2-b.dtb firmware/
84 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-3-b.dtb firmware/
85 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-3-b-plus.dtb firmware/
86 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-cm3.dtb firmware/
87 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-zero-2.dtb firmware/
88 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2710-rpi-zero-2-w.dtb firmware/
89
90 # Add pi4 specific files
91 cp ${pkgs.ubootRaspberryPi4_64bit}/u-boot.bin firmware/u-boot-rpi4.bin
92 cp ${pkgs.raspberrypi-armstubs}/armstub8-gic.bin firmware/armstub8-gic.bin
93 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-4-b.dtb firmware/
94 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-400.dtb firmware/
95 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-cm4.dtb firmware/
96 cp ${pkgs.raspberrypifw}/share/raspberrypi/boot/bcm2711-rpi-cm4s.dtb firmware/
97 '';
98 populateRootCommands = ''
99 mkdir -p ./files/boot
100 ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
101 '';
102 };
103}