mkfat.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. #
  3. # mkfat.sh
  4. #
  5. # Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. OUTPUT_FILE=floppy.img
  21. ROOT_DIR_ENTRIES=224
  22. NUMBER_OF_FATS=2
  23. TOTAL_SECTORS=2880
  24. BYTES_PER_SECTOR=512
  25. SECTORS_PER_FAT=$((((TOTAL_SECTORS * 3 / 2) + BYTES_PER_SECTOR - 1) / BYTES_PER_SECTOR))
  26. FIRST_ROOT_SECTOR=$((1 + NUMBER_OF_FATS * SECTORS_PER_FAT))
  27. ROOT_DIR_OFFSET=$((FIRST_ROOT_SECTOR * BYTES_PER_SECTOR))
  28. FIRST_DATA_SECTOR=$((FIRST_ROOT_SECTOR + (ROOT_DIR_ENTRIES * 32) / BYTES_PER_SECTOR))
  29. function get_free_cluster()
  30. {
  31. local cluster=2
  32. while [[ "$cluster" -lt $TOTAL_SECTORS ]]
  33. do
  34. local cluster_pair=`dd if=$OUTPUT_FILE skip=$((BYTES_PER_SECTOR + 3 * (cluster / 2))) bs=1 count=3 2>/dev/null | xxd -p`
  35. if [[ "${cluster_pair:3:1}${cluster_pair:0:2}" == "000" ]] && [[ "$cluster" -ne 2 ]]
  36. then
  37. echo "Found a free cluster: $cluster" > /dev/stderr
  38. echo $cluster
  39. return
  40. elif [[ "${cluster_pair:4:2}${cluster_pair:2:1}" == "000" ]]
  41. then
  42. echo "Found a free cluster: $((cluster + 1))" > /dev/stderr
  43. echo $((cluster + 1))
  44. return
  45. fi
  46. cluster=$((cluster + 2))
  47. done
  48. echo 'Ran out of the free clusters.' > /dev/stderr
  49. rm $OUTPUT_FILE
  50. exit 1
  51. }
  52. function set_next_cluster()
  53. {
  54. echo "Setting the next cluster of $1 to $2" > /dev/stderr
  55. local cluster=$1
  56. local value=$(printf "%03X" $2)
  57. local cluster_pair=`dd if=$OUTPUT_FILE skip=$((BYTES_PER_SECTOR + 3 * (cluster / 2))) bs=1 count=3 2>/dev/null | xxd -p`
  58. if [[ "$((cluster % 2))" -eq 0 ]]
  59. then
  60. cluster_pair="${value:1:2}${cluster_pair:2:1}${value:0:1}${cluster_pair:4:2}"
  61. else
  62. cluster_pair="${cluster_pair:0:2}${value:2:1}${cluster_pair:3:1}${value:0:2}"
  63. fi
  64. for fat in `seq 0 $((NUMBER_OF_FATS - 1))`
  65. do
  66. echo $cluster_pair | xxd -r -p | dd conv=notrunc \
  67. iflag=fullblock \
  68. oflag=seek_bytes \
  69. of=$OUTPUT_FILE \
  70. seek=$((BYTES_PER_SECTOR * (1 + fat * SECTORS_PER_FAT) + 3 * (cluster / 2))) \
  71. bs=3 \
  72. count=1 \
  73. 2>/dev/null
  74. done
  75. }
  76. dd if=/dev/zero of=$OUTPUT_FILE bs=$BYTES_PER_SECTOR count=$TOTAL_SECTORS 2>/dev/null
  77. /sbin/mkfs.vfat -r $ROOT_DIR_ENTRIES -f $NUMBER_OF_FATS -s 1 -S $BYTES_PER_SECTOR $OUTPUT_FILE >/dev/null 2>&1
  78. echo "Created empty FAT12 disk image $OUTPUT_FILE" > /dev/stderr
  79. for file in *.prg
  80. do
  81. [[ -f $file ]] || break
  82. echo "Copying $file to the disk image..." > /dev/stderr
  83. size=`stat --printf '%s' $file`
  84. clusters=$(((size + BYTES_PER_SECTOR - 1) / BYTES_PER_SECTOR))
  85. dosname=$(printf '%-8s%-3s' `echo ${file%%.*} | tr 'a-z' 'A-Z'` `echo ${file##*.} | tr 'a-z' 'A-Z'`)
  86. if [[ ${#dosname} -ne 11 ]]
  87. then
  88. echo "Cannot easily create a DOS name for $file" > /dev/stderr
  89. continue
  90. fi
  91. for dirent in `seq 0 $((ROOT_DIR_ENTRIES - 1))`
  92. do
  93. if [[ `dd if=$OUTPUT_FILE skip=$((dirent * 32 + ROOT_DIR_OFFSET)) bs=1 count=1 2>/dev/null | xxd -p` == "00" ]]
  94. then
  95. break
  96. fi
  97. done
  98. if [[ $dirent -ge $ROOT_DIR_ENTRIES ]]
  99. then
  100. echo 'Too many files, sorry.' > /dev/stderr
  101. exit 1
  102. fi
  103. echo "$file will be placed in directory entry number $dirent" > /dev/stderr
  104. if [[ $clusters -ne 0 ]]
  105. then
  106. first_cluster=`get_free_cluster`
  107. if [[ $? -ne 0 ]]
  108. then
  109. echo 'No more free clusters...'
  110. exit 1
  111. fi
  112. else
  113. first_cluster=0
  114. fi
  115. (
  116. echo -en "${dosname}\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  117. printf '%02X%02X' $((first_cluster & 0xFF)) $((first_cluster >> 8)) | xxd -r -p
  118. printf '%02X%02X%02X%02X' \
  119. $((size & 0xFF)) \
  120. $(((size >> 8) & 0xFF)) \
  121. $(((size >> 16) & 0xFF)) \
  122. $(((size >> 24) & 0xFF)) | xxd -r -p
  123. ) | dd conv=notrunc \
  124. iflag=fullblock \
  125. of=$OUTPUT_FILE \
  126. seek=$(((ROOT_DIR_OFFSET / 32) + dirent)) \
  127. bs=32 \
  128. count=1 \
  129. 2>/dev/null
  130. current_cluster=$first_cluster
  131. for ((i = 0; i < $clusters; i++))
  132. do
  133. echo "Writing cluster #$i of $file to cluster $current_cluster" > /dev/stderr
  134. dd conv=notrunc \
  135. if=$file \
  136. of=$OUTPUT_FILE \
  137. skip=$i \
  138. seek=$((FIRST_DATA_SECTOR + current_cluster - 2)) \
  139. bs=$BYTES_PER_SECTOR \
  140. count=1 \
  141. 2>/dev/null
  142. set_next_cluster $current_cluster 4095
  143. if [[ "$i" -ne "$((clusters - 1))" ]]
  144. then
  145. next_cluster=`get_free_cluster`
  146. set_next_cluster $current_cluster $next_cluster
  147. current_cluster=$next_cluster
  148. fi
  149. done
  150. done
  151. echo "All done!" > /dev/stderr