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}: { pkgs, crossPkgs, compress ? true}:
let let
firmwarePartition = { fwPart = {
offset = 8; offset = 8;
id = "0xfeed3425"; id = "0xfeed3425";
name = "FIRMWARE"; name = "FIRMWARE";
size = 504; # offset + size = 512. Must be power of two. size = 512;
}; };
rootfsImage = import ./rootfs.nix { inherit pkgs; }; rootfsImage = import ./rootfs.nix { inherit pkgs; };
@ -34,13 +34,10 @@ in
root_fs=./rootfs.img root_fs=./rootfs.img
zstd -d --no-progress "${rootfsImage}" -o $root_fs 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. # 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 }') rootSizeBlocks=$(du -B 512 --apparent-size $root_fs | awk '{ print $1 }')
firmwareSizeBlocks=$((${toString firmwarePartition.size} * 1024 * 1024 / 512)) firmwareSizeBlocks=$((${toString fwPart.size} * 1024 * 1024 / 512))
imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512 + gap * 1024 * 1024)) imageSize=$((rootSizeBlocks * 512 + firmwareSizeBlocks * 512))
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'.
@ -48,9 +45,9 @@ in
# information (dtbs, extlinux.conf file). # information (dtbs, extlinux.conf file).
sfdisk $img <<EOF sfdisk $img <<EOF
label: dos label: dos
label-id: ${firmwarePartition.id} label-id: ${fwPart.id}
start=''${gap}M, size=$firmwareSizeBlocks, type=b start=${toString fwPart.offset}M, size=${toString (fwPart.size - fwPart.offset)}M, type=b
start=$((gap + ${toString firmwarePartition.size}))M, type=83, bootable start=${toString fwPart.size}M, type=83, bootable
EOF EOF
# Copy the rootfs into the SD image # 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 # 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)) firmware_part.img 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 # Populate the files intended for /boot/firmware
mkdir firmware mkdir firmware