Skip to content

Instantly share code, notes, and snippets.

@sakkke
Created January 3, 2024 02:32
Show Gist options
  • Save sakkke/2185ac8770b9dd334aaabb36e2d7473d to your computer and use it in GitHub Desktop.
Save sakkke/2185ac8770b9dd334aaabb36e2d7473d to your computer and use it in GitHub Desktop.
function fat32_img_size {
declare dir="$1"
declare -i unit_sector=512
declare -i reserved_sectors=32
declare -i num_fats=2
declare -i num_paths=$(find "$dir" -mindepth 1 | wc -l)
declare -i fat_sectors=$((num_paths * unit_sector / num_fats - 1))
declare -i user_data_sectors=0
while read -r path; do
declare -i path_size=$(du -b "$path" | cut -f 1)
declare -i sectors=$((path_size / unit_sector))
[[ $((path_size % unit_sector)) -eq 0 ]] || ((sectors++))
((user_data_sectors += sectors))
done < <(find "$dir" -mindepth 1 -type f)
declare -i fat32_img_size=$(((reserved_sectors + fat_sectors * num_fats + num_paths + user_data_sectors) * unit_sector))
declare -i sectors_per_cluster=8
declare -i cluster_size=$((sectors_per_cluster * unit_sector))
while ((fat32_img_size % cluster_size != 0)); do
((fat32_img_size += unit_sector))
done
echo "$fat32_img_size"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment