orange/sd-card.nix
2023-04-04 13:53:15 -04:00

91 lines
3.3 KiB
Nix

{ pkgs, crossPkgs, compress ? true}:
let
firmwarePartition = {
offset = 8;
id = "0xfeed3425";
name = "FIRMWARE";
size = 504; # offset + size = 512. Must be power of two.
};
rootfsImage = import ./rootfs.nix { inherit pkgs; };
configTxt = pkgs.writeText "config.txt" (builtins.readFile ./config.txt);
populateFirmwareCommands = ''
# Add config.txt
cp ${configTxt} firmware/config.txt
# Copy the firmware files
cp ${crossPkgs.raspberrypifw}/share/raspberrypi/boot/bootcode.bin firmware/
cp ${crossPkgs.raspberrypifw}/share/raspberrypi/boot/fixup*.dat firmware/
cp ${crossPkgs.raspberrypifw}/share/raspberrypi/boot/start*.elf firmware/
# Add pi3 specific files
cp ${crossPkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
'';
in
pkgs.stdenv.mkDerivation {
name = "orange";
nativeBuildInputs = with pkgs; [ dosfstools e2fsprogs libfaketime mtools util-linux zstd ];
buildCommand = ''
mkdir $out
img=$out/sd-card.img
root_fs=./rootfs.img
zstd -d --no-progress "${rootfsImage}" -o $root_fs
# Gap in front of the first partition, in MiB
gap=${toString firmwarePartition.offset}
# Create the image file sized to fit /boot/firmware and /, plus slack for the gap.
rootSizeBlocks=$(du -B 512 --apparent-size $root_fs | awk '{ print $1 }')
firmwareSizeBlocks=$((${toString firmwarePartition.size} * 1024 * 1024 / 512))
imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024))
truncate -s $imageSize $img
# type=b is 'W95 FAT32', type=83 is 'Linux'.
# The "bootable" partition is where u-boot will look file for the bootloader
# information (dtbs, extlinux.conf file).
sfdisk $img <<EOF
label: dos
label-id: ${firmwarePartition.id}
start=''${gap}M, size=$firmwareSizeBlocks, type=b
start=$((gap + ${toString firmwarePartition.size}))M, type=83, bootable
EOF
# Copy the rootfs into the SD image
eval $(partx $img -o START,SECTORS --nr 2 --pairs)
dd conv=notrunc if=$root_fs of=$img seek=$START count=$SECTORS
# Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img
eval $(partx $img -o START,SECTORS --nr 1 --pairs)
truncate -s $((SECTORS * 512)) firmware_part.img
mkfs.vfat --invariant -i ${toString firmwarePartition.size} -n ${firmwarePartition.name} firmware_part.img
# Populate the files intended for /boot/firmware
mkdir firmware
${populateFirmwareCommands}
find firmware -exec touch --date=2000-01-01 {} +
# Copy the populated /boot/firmware into the SD image
cd firmware
# Force a fixed order in mcopy for better determinism, and avoid file globbing
for d in $(find . -type d -mindepth 1 | sort); do
faketime "2000-01-01 00:00:00" mmd -i ../firmware_part.img "::/$d"
done
for f in $(find . -type f | sort); do
mcopy -pvm -i ../firmware_part.img "$f" "::/$f"
done
cd ..
# Verify the FAT partition before copying it.
fsck.vfat -vn firmware_part.img
dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS
${pkgs.lib.optionalString compress ''
zstd -T$NIX_BUILD_CORES --rm $img
''}
'';
}