From a6866906d0708793d8b49a5b1e9b4db682765571 Mon Sep 17 00:00:00 2001 From: Saqoosha Date: Sat, 19 Sep 2026 22:15:49 +0900 Subject: [PATCH] Fix MBR partition type after formatting SD card - mkfs.vfat only rewrites the filesystem inside /dev/mmcblk0p1; the MBR partition type byte stays whatever the card shipped with. SDXC cards come as exFAT with type 0x07, so after "Format SD Card" they hold FAT32 in a partition still typed 0x07 - Linux mounts that fine, but macOS selects the filesystem driver from the type byte: 0x07 is only claimed by exfat.fs, fsck_exfat rejects the FAT32 boot sector, and the OS reports "The disk you attached was not readable by this computer" - After a successful mkfs, read the type byte at MBR offset 450 and, only if it is 0x07, set it to 0x0C (FAT32 LBA), then re-read the partition table. 0xEE (GPT protective MBR) and already FAT-typed partitions are left untouched, and nothing is done when formatting the whole device Co-Authored-By: Claude Opus 5 (1M context) --- mkapp/app/script/formatsd.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mkapp/app/script/formatsd.sh b/mkapp/app/script/formatsd.sh index 4b3f543db..8d6464711 100755 --- a/mkapp/app/script/formatsd.sh +++ b/mkapp/app/script/formatsd.sh @@ -17,9 +17,10 @@ else echo "Umounting SD Card: FAILURE" fi -BLKDEV=/dev/mmcblk0p1 +DISKDEV=/dev/mmcblk0 +BLKDEV=${DISKDEV}p1 if [ ! -b "$BLKDEV" ]; then - BLKDEV=/dev/mmcblk0 + BLKDEV=$DISKDEV fi rm -f /tmp/mkfs.result @@ -28,6 +29,25 @@ RESULT=$? echo "mkfs result: $RESULT" >> /tmp/mkfs.log echo $RESULT > /tmp/mkfs.result +# mkfs.vfat only rewrites the filesystem inside the partition; the MBR +# partition type byte is left as it was. SDXC cards (>32GB) ship as exFAT +# with type 0x07 (NTFS/exFAT), so after formatting they hold FAT32 in a +# partition still labelled 0x07. Linux ignores the type byte and mounts +# fine, but macOS picks the filesystem driver from it, tries exFAT, and +# reports "The disk you attached was not readable by this computer". +# Set the type to 0x0C (FAT32 LBA) so the card mounts on every OS. +# Only the 0x07 case is touched; 0xEE (GPT protective MBR) and anything +# already FAT-typed are left alone. +if [ "$RESULT" -eq 0 ] && [ "$BLKDEV" != "$DISKDEV" ]; then + PTYPE=$(dd if="$DISKDEV" bs=1 skip=450 count=1 2>/dev/null | hexdump -v -e '1/1 "%02x"') + echo "partition type: 0x$PTYPE" >> /tmp/mkfs.log + if [ "$PTYPE" = "07" ]; then + printf '\014' | dd of="$DISKDEV" bs=1 seek=450 count=1 conv=notrunc,fsync >> /tmp/mkfs.log 2>&1 + blockdev --rereadpt "$DISKDEV" >> /tmp/mkfs.log 2>&1 + echo "partition type changed to 0x0c (FAT32 LBA)" >> /tmp/mkfs.log + fi +fi + echo "Mounting SD Card" mount "$BLKDEV" /mnt/extsd if [ $? -eq 0 ]; then