Simplifies calculation of partition sizes.

This commit is contained in:
Bailey Stevens 2023-04-05 19:49:42 -04:00
parent 426fe7353a
commit 7bf87946d2

View file

@ -1,11 +1,11 @@
{ pkgs, crossPkgs, compress ? true}:
let
firmwarePartition = {
fwPart = {
offset = 8;
id = "0xfeed3425";
name = "FIRMWARE";
size = 504; # offset + size = 512. Must be power of two.
size = 512;
};
rootfsImage = import ./rootfs.nix { inherit pkgs; };
@ -34,13 +34,10 @@ in
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))
firmwareSizeBlocks=$((${toString fwPart.size} * 1024 * 1024 / 512))
imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512))
truncate -s $imageSize $img
# type=b is 'W95 FAT32', type=83 is 'Linux'.
@ -48,9 +45,9 @@ in
# 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
label-id: ${fwPart.id}
start=${toString fwPart.offset}M, size=${toString (fwPart.size - fwPart.offset)}M, type=b
start=${toString fwPart.size}M, type=83, bootable
EOF
# Copy the rootfs into the SD image
@ -60,7 +57,7 @@ in
# 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
mkfs.vfat --invariant -i ${toString fwPart.size} -n ${fwPart.name} firmware_part.img
# Populate the files intended for /boot/firmware
mkdir firmware