Skip to content

Commit

Permalink
mkvirtdisk: Handle fdisk offset issue
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Chan <[email protected]>
  • Loading branch information
erichchan999 authored and Ivan-Velickovic committed Aug 7, 2024
1 parent 37578e3 commit d13698b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions tools/mkvirtdisk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

set -e

function cleanup {
rm -f $BUILD_DIR/fat.img
}

trap cleanup EXIT

# Usage instructions
if [ $# -ne 4 ]; then
echo "Usage: $0 <virtual_disk_name> <num_partitions> <logical_size> <memsize>"
Expand All @@ -25,7 +31,12 @@ SDDF_TRANSFER_SIZE=4096
FDISK_LSIZE=512

if [ $(( LSIZE & (LSIZE - 1) )) -ne 0 ] || [ $LSIZE -lt $FDISK_LSIZE ]; then
echo "LSIZE must be greater than $FDISK_LSIZE and a power of 2"
echo "LSIZE must be greater than $FDISK_LSIZE and must be a power of 2"
exit 1
fi

if [ $((MEMSIZE)) -lt 114688 ]; then
echo "MEMSIZE must be greater than 114688(112 * 1024), sizes smaller than that break either fdisk or mkfs.fat"
exit 1
fi

Expand All @@ -37,13 +48,19 @@ LCM=$(( SDDF_TRANSFER_SIZE > LSIZE ? SDDF_TRANSFER_SIZE : LSIZE ))
# Since we are using fdisk, COUNT uses the logical size provided by fdisk which is always 512 bytes (FDISK_LSIZE)
MULTIPLE=$(( LCM / FDISK_LSIZE ))
COUNT=$(( MEMSIZE / FDISK_LSIZE ))
COUNT=$(( COUNT / MULTIPLE * MULTIPLE)) # Ensure COUNT is a multiple of sDDF transfer size and logical size

# Determine starting partition offset
if [ $COUNT -gt $(( 2048 * 4 )) ]; then
POFFSET=2048
else
POFFSET=$MULTIPLE
fi

COUNT=$(( COUNT / MULTIPLE * MULTIPLE)) # Now ensure the real COUNT is a multiple of sDDF transfer size and logical size

# Create a file to act as a virtual disk
dd if=/dev/zero of=$DISK_IMAGE bs=$FDISK_LSIZE count=$COUNT 2> /dev/null

POFFSET=$MULTIPLE

FS_COUNT=$(( (COUNT - POFFSET - 1) / NUM_PARTITIONS ))
FS_COUNT=$(( FS_COUNT / MULTIPLE * MULTIPLE )) # Ensures that both filesystems are a multiple of sDDF transfer size and logical size

Expand All @@ -68,8 +85,6 @@ done
echo w # Write changes
} | fdisk $DISK_IMAGE

fdisk -l $DISK_IMAGE

# Create the FAT filesystem
rm -f $BUILD_DIR/fat.img
mkfs.fat -C $BUILD_DIR/fat.img $(( (FS_COUNT * FDISK_LSIZE) / 1024 ))
Expand All @@ -79,4 +94,4 @@ for i in $(seq 0 $(( NUM_PARTITIONS - 1 )))
do
echo "Copying FAT filesystem to partition $i, seek=$((POFFSET + i * FS_COUNT)), count=$FS_COUNT, bs=$FDISK_LSIZE"
dd if=$BUILD_DIR/fat.img of=$DISK_IMAGE bs=$FDISK_LSIZE seek="$((POFFSET + i * FS_COUNT))" count=$FS_COUNT 2> /dev/null
done
done

0 comments on commit d13698b

Please sign in to comment.