Skip to content

Instantly share code, notes, and snippets.

@agurtovoy
Last active March 24, 2025 05:49
Show Gist options
  • Select an option

  • Save agurtovoy/7d060d43af4262a6a0ea1ff6165feda5 to your computer and use it in GitHub Desktop.

Select an option

Save agurtovoy/7d060d43af4262a6a0ea1ff6165feda5 to your computer and use it in GitHub Desktop.
A script to create MAME-compatible FAT32 disk image for the Foenix F256 on a Mac
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "Usage: $0 [-q] <disk_name> <size_in_mb>"
exit 1
fi
QUIET=false
while getopts "q" opt; do
case $opt in
q) QUIET=true ;;
*) ;;
esac
done
shift $((OPTIND - 1))
DISK_NAME=$1
SIZE_MB=$2
IMG_FILE="${DISK_NAME}.img"
IMG_PATH="$(pwd)/$IMG_FILE"
abort () {
if ! $QUIET; then
command echo "❌ $@"
else
command echo "[ERROR] $@"
fi
exit 1
}
step () {
if ! $QUIET; then
command echo "➜ $@"
fi
}
if [[ -e "$IMG_FILE" ]]; then
if ! $QUIET; then
read -p "The file '$IMG_FILE' already exists. Do you want to override it? [n] " CHOICE
CHOICE="${CHOICE:-n}"
if [[ "$CHOICE" != "y" ]]; then
abort "File already exists, aborted"
fi
else
abort "File already exists, aborted"
fi
if hdiutil info | grep -q "$IMG_FILE"; then
abort "Disk image '$IMG_FILE' is currently mounted. Please unmount it first."
fi
fi
if [ "$SIZE_MB" -lt 34 ]; then
abort "FAT32 requires a disk size of at least 34MB (32MB + partition table)."
fi
step "Creating a raw ${SIZE_MB}MB disk image '$IMG_FILE'"
OUT=$(dd if=/dev/zero of=$IMG_FILE bs=1M count=$SIZE_MB 2>&1)
if [[ $? -ne 0 ]]; then
abort "Failed to create disk image: $OUT"
fi
step "Attaching the raw disk image"
OUT=$(hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount $IMG_FILE 2>&1)
DEVICE=$(echo "$OUT" | head -n 1)
if [[ ! $DEVICE =~ "/dev/" ]]; then
abort "Failed to attach disk image: $OUT"
fi
step "Disk image attached as $DEVICE"
detach () {
hdiutil detach $DEVICE >/dev/null
}
step "Partitioning/formatting the disk"
OUT=$(diskutil partitionDisk $DEVICE MBR "MS-DOS FAT32" $DISK_NAME 100% 2>&1)
if [[ $? -ne 0 ]]; then
detach
abort "Failed to partition disk image: $(echo "$OUT" | tail -n 1)"
fi
step "Re-mounting the disk"
OUT=$(detach && hdiutil attach $IMG_FILE 2>&1)
if [[ $? -ne 0 ]]; then
detach
abort "Failed to mount the formatted disk: $OUT"
fi
VOLUME_INFO=$(echo "$OUT" | head -n 2 | tail -n 1)
read -ra VOLUME <<< "$VOLUME_INFO"
VOLUME_PATH="${VOLUME[2]}"
if ! $QUIET; then
echo "✨ Mounted at $VOLUME_PATH ✨"
else
echo "$VOLUME_PATH"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment