@@ -1,8 +1,12 @@
11# This module creates a bootable SD card image containing the given NixOS
2−# configuration. The generated image is MBR partitioned, with a FAT /boot
3−# partition, and ext4 root partition. The generated image is sized to fit
4−# its contents, and a boot script automatically resizes the root partition
5−# to fit the device on the first boot.
2+# configuration. The generated image is MBR partitioned, with a FAT
3+# /boot/firmware partition, and ext4 root partition. The generated image
4+# is sized to fit its contents, and a boot script automatically resizes
5+# the root partition to fit the device on the first boot.
6+#
7+# The firmware partition is built with expectation to hold the Raspberry
8+# Pi firmware and bootloader, and be removed and replaced with a firmware
9+# build for the target SoC for other board families.
610#
711# The derivation for the SD image will be placed in
812# config.system.build.sdImage
@@ -14,12 +18,18 @@ with lib;
1418let
1519 rootfsImage = pkgs.callPackage ../../../lib/make-ext4-fs.nix ({
1620 inherit (config.sdImage) storePaths;
21+ populateImageCommands = config.sdImage.populateRootCommands;
1722 volumeLabel = "NIXOS_SD";
1823 } // optionalAttrs (config.sdImage.rootPartitionUUID != null) {
1924 uuid = config.sdImage.rootPartitionUUID;
2025 });
2126in
2227{
28+ imports = [
29+ (mkRemovedOptionModule [ "sdImage" "bootPartitionID" ] "The FAT partition for SD image now only holds the Raspberry Pi firmware files. Use firmwarePartitionID to configure that partition's ID.")
30+ (mkRemovedOptionModule [ "sdImage" "bootSize" ] "The boot files for SD image have been moved to the main ext4 partition. The FAT partition now only holds the Raspberry Pi firmware files. Changing its size may not be required.")
31+ ];
32+
2333 options.sdImage = {
2434 imageName = mkOption {
2535 default = "${config.sdImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.img";
@@ -43,12 +53,12 @@ in
4353 '';
4454 };
4555
46− bootPartitionID = mkOption {
56+ firmwarePartitionID = mkOption {
4757 type = types.string;
4858 default = "0x2178694e";
4959 description = ''
50− Volume ID for the /boot partition on the SD card. This value must be a
51− 32-bit hexadecimal number.
60+ Volume ID for the /boot/firmware partition on the SD card. This value
61+ must be a 32-bit hexadecimal number.
5262 '';
5363 };
5464
@@ -61,29 +71,44 @@ in
6171 '';
6272 };
6373
64− bootSize = mkOption {
74+ firmwareSize = mkOption {
6575 type = types.int;
66− default = 120;
76+ # As of 2019-05-31 the Raspberry pi firmware + u-bot takes ~13MiB
77+ default = 20;
78+ description = ''
79+ Size of the /boot/firmware partition, in megabytes.
80+ '';
81+ };
82+
83+ populateFirmwareCommands = mkOption {
84+ example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin firmware/ ''";
6785 description = ''
68− Size of the /boot partition, in megabytes.
86+ Shell commands to populate the ./firmware directory.
87+ All files in that directory are copied to the
88+ /boot/firmware partition on the SD image.
6989 '';
7090 };
7191
72− populateBootCommands = mkOption {
73− example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin boot/ ''";
92+ populateRootCommands = mkOption {
93+ example = literalExample "''\${extlinux-conf-builder} -t 3 -c \${config.system.build.toplevel} -d ./files/boot''";
7494 description = ''
75− Shell commands to populate the ./boot directory.
95+ Shell commands to populate the ./files directory.
7696 All files in that directory are copied to the
77− /boot partition on the SD image.
97+ root (/) partition on the SD image. Use this to
98+ populate the ./files/boot (/boot) directory.
7899 '';
79100 };
80101 };
81102
82103 config = {
83104 fileSystems = {
84− "/boot" = {
85− device = "/dev/disk/by-label/NIXOS_BOOT";
105+ "/boot/firmware" = {
106+ device = "/dev/disk/by-label/FIRMWARE";
86107 fsType = "vfat";
108+ # Alternatively, this could be removed from the configuration.
109+ # The filesystem is not needed at runtime, it could be treated
110+ # as an opaque blob instead of a discrete FAT32 filesystem.
111+ options = [ "nofail" "noauto" ];
87112 };
88113 "/" = {
89114 device = "/dev/disk/by-label/NIXOS_SD";
@@ -105,39 +130,44 @@ in
105130 echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system
106131 echo "file sd-image $img" >> $out/nix-support/hydra-build-products
107132
108− # Create the image file sized to fit /boot and /, plus 20M of slack
133+ # Gap in front of the first partition, in MiB
134+ gap=8
135+
136+ # Create the image file sized to fit /boot/firmware and /, plus slack for the gap.
109137 rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }')
110− bootSizeBlocks=$((${toString config.sdImage.bootSize} * 1024 * 1024 / 512))
111− imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 20 * 1024 * 1024))
138+ firmwareSizeBlocks=$((${toString config.sdImage.firmwareSize} * 1024 * 1024 / 512))
139+ imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024))
112140 truncate -s $imageSize $img
113141
114142 # type=b is 'W95 FAT32', type=83 is 'Linux'.
143+ # The "bootable" partition is where u-boot will look file for the bootloader
144+ # information (dtbs, extlinux.conf file).
115145 sfdisk $img <<EOF
116146 label: dos
117− label-id: ${config.sdImage.bootPartitionID}
147+ label-id: ${config.sdImage.firmwarePartitionID}
118148
119− start=8M, size=$bootSizeBlocks, type=b, bootable
120− start=${toString (8 + config.sdImage.bootSize)}M, type=83
149+ start=''${gap}M, size=$firmwareSizeBlocks, type=b
150+ start=$((gap + ${toString config.sdImage.firmwareSize}))M, type=83, bootable
121151 EOF
122152
123153 # Copy the rootfs into the SD image
124154 eval $(partx $img -o START,SECTORS --nr 2 --pairs)
125155 dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS
126156
127− # Create a FAT32 /boot partition of suitable size into bootpart.img
157+ # Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img
128158 eval $(partx $img -o START,SECTORS --nr 1 --pairs)
129− truncate -s $((SECTORS * 512)) bootpart.img
130− faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.bootPartitionID} -n NIXOS_BOOT bootpart.img
159+ truncate -s $((SECTORS * 512)) firmware_part.img
160+ faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n FIRMWARE firmware_part.img
131161
132− # Populate the files intended for /boot
133− mkdir boot
134− ${config.sdImage.populateBootCommands}
162+ # Populate the files intended for /boot/firmware
163+ mkdir firmware
164+ ${config.sdImage.populateFirmwareCommands}
135165
136− # Copy the populated /boot into the SD image
137− (cd boot; mcopy -psvm -i ../bootpart.img ./* ::)
166+ # Copy the populated /boot/firmware into the SD image
167+ (cd firmware; mcopy -psvm -i ../firmware_part.img ./* ::)
138168 # Verify the FAT partition before copying it.
139− fsck.vfat -vn bootpart.img
140− dd conv=notrunc if=bootpart.img of=$img seek=$START count=$SECTORS
169+ fsck.vfat -vn firmware_part.img
170+ dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS
141171 '';
142172 }) {};
143173