sd-image: Moves `/boot` into rootfs

The current FAT32 partition is kept as it is required for the Raspberry
Pi family of hardware. It is where the firmware is kept.

The partition is kept bootable, and the boot files kept in there until
the following commits, to keep all commits of this series individually
bootable.
This commit is contained in:
Samuel Dionne-Riel 2019-06-01 18:03:44 -04:00
parent 7815c86c10
commit 6e9e78b618
4 changed files with 44 additions and 43 deletions

View File

@ -27,7 +27,7 @@ in
boot.kernelParams = ["cma=32M" "console=ttyS0,115200n8" "console=ttyAMA0,115200n8" "console=tty0"]; boot.kernelParams = ["cma=32M" "console=ttyS0,115200n8" "console=ttyAMA0,115200n8" "console=tty0"];
sdImage = { sdImage = {
populateBootCommands = let populateFirmwareCommands = let
configTxt = pkgs.writeText "config.txt" '' configTxt = pkgs.writeText "config.txt" ''
kernel=u-boot-rpi3.bin kernel=u-boot-rpi3.bin
@ -43,10 +43,10 @@ in
avoid_warnings=1 avoid_warnings=1
''; '';
in '' in ''
(cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/) (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin boot/u-boot-rpi3.bin cp ${pkgs.ubootRaspberryPi3_64bit}/u-boot.bin firmware/u-boot-rpi3.bin
cp ${configTxt} boot/config.txt cp ${configTxt} firmware/config.txt
${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./firmware
''; '';
}; };
} }

View File

@ -29,7 +29,7 @@ in
boot.kernelParams = ["console=ttyS0,115200n8" "console=ttymxc0,115200n8" "console=ttyAMA0,115200n8" "console=ttyO0,115200n8" "console=ttySAC2,115200n8" "console=tty0"]; boot.kernelParams = ["console=ttyS0,115200n8" "console=ttymxc0,115200n8" "console=ttyAMA0,115200n8" "console=ttyO0,115200n8" "console=ttySAC2,115200n8" "console=tty0"];
sdImage = { sdImage = {
populateBootCommands = let populateFirmwareCommands = let
configTxt = pkgs.writeText "config.txt" '' configTxt = pkgs.writeText "config.txt" ''
# Prevent the firmware from smashing the framebuffer setup done by the mainline kernel # Prevent the firmware from smashing the framebuffer setup done by the mainline kernel
# when attempting to show low-voltage or overtemperature warnings. # when attempting to show low-voltage or overtemperature warnings.
@ -46,11 +46,11 @@ in
enable_uart=1 enable_uart=1
''; '';
in '' in ''
(cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/) (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
cp ${pkgs.ubootRaspberryPi2}/u-boot.bin boot/u-boot-rpi2.bin cp ${pkgs.ubootRaspberryPi2}/u-boot.bin firmware/u-boot-rpi2.bin
cp ${pkgs.ubootRaspberryPi3_32bit}/u-boot.bin boot/u-boot-rpi3.bin cp ${pkgs.ubootRaspberryPi3_32bit}/u-boot.bin firmware/u-boot-rpi3.bin
cp ${configTxt} boot/config.txt cp ${configTxt} firmware/config.txt
${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./firmware
''; '';
}; };
} }

View File

@ -35,11 +35,11 @@ in
kernel=u-boot-rpi1.bin kernel=u-boot-rpi1.bin
''; '';
in '' in ''
(cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/boot/) (cd ${pkgs.raspberrypifw}/share/raspberrypi/boot && cp bootcode.bin fixup*.dat start*.elf $NIX_BUILD_TOP/firmware/)
cp ${pkgs.ubootRaspberryPiZero}/u-boot.bin boot/u-boot-rpi0.bin cp ${pkgs.ubootRaspberryPiZero}/u-boot.bin firmware/u-boot-rpi0.bin
cp ${pkgs.ubootRaspberryPi}/u-boot.bin boot/u-boot-rpi1.bin cp ${pkgs.ubootRaspberryPi}/u-boot.bin firmware/u-boot-rpi1.bin
cp ${configTxt} boot/config.txt cp ${configTxt} firmware/config.txt
${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./boot ${extlinux-conf-builder} -t 3 -c ${config.system.build.toplevel} -d ./firmware
''; '';
}; };
} }

View File

@ -43,12 +43,12 @@ in
''; '';
}; };
bootPartitionID = mkOption { firmwarePartitionID = mkOption {
type = types.string; type = types.string;
default = "0x2178694e"; default = "0x2178694e";
description = '' description = ''
Volume ID for the /boot partition on the SD card. This value must be a Volume ID for the /boot/firmware partition on the SD card. This value
32-bit hexadecimal number. must be a 32-bit hexadecimal number.
''; '';
}; };
@ -61,29 +61,30 @@ in
''; '';
}; };
bootSize = mkOption { firmwareSize = mkOption {
type = types.int; type = types.int;
default = 120; default = 120;
description = '' description = ''
Size of the /boot partition, in megabytes. Size of the /boot/firmware partition, in megabytes.
''; '';
}; };
populateBootCommands = mkOption { populateFirmwareCommands = mkOption {
example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin boot/ ''"; example = literalExample "'' cp \${pkgs.myBootLoader}/u-boot.bin firmware/ ''";
description = '' description = ''
Shell commands to populate the ./boot directory. Shell commands to populate the ./firmware directory.
All files in that directory are copied to the All files in that directory are copied to the
/boot partition on the SD image. /boot/firmware partition on the SD image.
''; '';
}; };
}; };
config = { config = {
fileSystems = { fileSystems = {
"/boot" = { "/boot/firmware" = {
device = "/dev/disk/by-label/NIXOS_BOOT"; device = "/dev/disk/by-label/FIRMWARE";
fsType = "vfat"; fsType = "vfat";
options = [ "nofail" "noauto" ];
}; };
"/" = { "/" = {
device = "/dev/disk/by-label/NIXOS_SD"; device = "/dev/disk/by-label/NIXOS_SD";
@ -105,39 +106,39 @@ in
echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system echo "${pkgs.stdenv.buildPlatform.system}" > $out/nix-support/system
echo "file sd-image $img" >> $out/nix-support/hydra-build-products echo "file sd-image $img" >> $out/nix-support/hydra-build-products
# Create the image file sized to fit /boot and /, plus 20M of slack # Create the image file sized to fit /boot/firmware and /, plus 20M of slack
rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }') rootSizeBlocks=$(du -B 512 --apparent-size ${rootfsImage} | awk '{ print $1 }')
bootSizeBlocks=$((${toString config.sdImage.bootSize} * 1024 * 1024 / 512)) firmwareSizeBlocks=$((${toString config.sdImage.firmwareSize} * 1024 * 1024 / 512))
imageSize=$((rootSizeBlocks * 512 + bootSizeBlocks * 512 + 20 * 1024 * 1024)) imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + 20 * 1024 * 1024))
truncate -s $imageSize $img truncate -s $imageSize $img
# type=b is 'W95 FAT32', type=83 is 'Linux'. # type=b is 'W95 FAT32', type=83 is 'Linux'.
sfdisk $img <<EOF sfdisk $img <<EOF
label: dos label: dos
label-id: ${config.sdImage.bootPartitionID} label-id: ${config.sdImage.firmwarePartitionID}
start=8M, size=$bootSizeBlocks, type=b, bootable start=8M, size=$firmwareSizeBlocks, type=b, bootable
start=${toString (8 + config.sdImage.bootSize)}M, type=83 start=${toString (8 + config.sdImage.firmwareSize)}M, type=83
EOF EOF
# Copy the rootfs into the SD image # Copy the rootfs into the SD image
eval $(partx $img -o START,SECTORS --nr 2 --pairs) eval $(partx $img -o START,SECTORS --nr 2 --pairs)
dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS dd conv=notrunc if=${rootfsImage} of=$img seek=$START count=$SECTORS
# Create a FAT32 /boot partition of suitable size into bootpart.img # Create a FAT32 /boot/firmware partition of suitable size into firmware_part.img
eval $(partx $img -o START,SECTORS --nr 1 --pairs) eval $(partx $img -o START,SECTORS --nr 1 --pairs)
truncate -s $((SECTORS * 512)) bootpart.img truncate -s $((SECTORS * 512)) firmware_part.img
faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.bootPartitionID} -n NIXOS_BOOT bootpart.img faketime "1970-01-01 00:00:00" mkfs.vfat -i ${config.sdImage.firmwarePartitionID} -n FIRMWARE firmware_part.img
# Populate the files intended for /boot # Populate the files intended for /boot/firmware
mkdir boot mkdir firmware
${config.sdImage.populateBootCommands} ${config.sdImage.populateFirmwareCommands}
# Copy the populated /boot into the SD image # Copy the populated /boot/firmware into the SD image
(cd boot; mcopy -psvm -i ../bootpart.img ./* ::) (cd firmware; mcopy -psvm -i ../firmware_part.img ./* ::)
# Verify the FAT partition before copying it. # Verify the FAT partition before copying it.
fsck.vfat -vn bootpart.img fsck.vfat -vn firmware_part.img
dd conv=notrunc if=bootpart.img of=$img seek=$START count=$SECTORS dd conv=notrunc if=firmware_part.img of=$img seek=$START count=$SECTORS
''; '';
}) {}; }) {};