Skip to content

Instantly share code, notes, and snippets.

@kirelagin
Created September 15, 2012 23:01
Show Gist options
  • Save kirelagin/3730254 to your computer and use it in GitHub Desktop.
Save kirelagin/3730254 to your computer and use it in GitHub Desktop.
Script to extract useful information from FAT32 header
#!/usr/bin/env sh
# Display information about internal layout of a FAT32 filesystem.
#
# Script directly reads data from FAT32 header. This might be useful
# for aligning fs structures to speed up flash drive access.
#
# http://kirelagin.ru/p/gist/3730254
#
# -- Kirill Elagin <kirelagin@gmail.com>
# http://kirelagin.ru/
device="$1"
if [ -z "$device" ]; then
echo "Usage: $0 <device>." >&2
exit 1
fi
if [ ! -e "$device" ]; then
echo "Error: '$device' does not exist." >&2
exit 2
fi
if [ ! -b "$device" ]; then
echo "Error: '$device' is not a block device." >&2
exit 2
fi
if [ ! -r "$device" ]; then
echo "Error: read on '$device' is not permitted. You probably want to run this script as root." >&2
exit 2
fi
function readfat32() {
offset="$2"
len="$3"
format="-tu$len"
let $1=$(dd 2>/dev/null if="$device" bs=1 count="$len" skip="$offset" | od -An $format | tr -d ' ')
echo "$1 : ${!1}" >&2
}
readfat32 signature 66 1
if [ "$signature" != "41" -a "$signature" != "40" ]; then
echo "Error: seems that '$device' has filesystem different from FAT32." >&2
exit 3
fi
readfat32 sector_bytes 11 2
readfat32 cluster_sectors 13 1
readfat32 sectors_reserved 14 2
readfat32 fats_count 16 1
readfat32 fat_sectors 36 4
readfat32 fs_sectors 32 4
header_sectors=$((fat_sectors*fats_count + sectors_reserved))
echo "Header size is $header_sectors sectors ($((header_sectors * sector_bytes / 1024)) kbytes)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment