Annotation of hatari/tools/atari-hd-image.sh, revision 1.1.1.5

1.1       root        1: #!/bin/sh
1.1.1.2   root        2: # script for creating a compatible DOS HD image for Hatari with
                      3: # a single FAT16 partition of given size, with given contents
1.1       root        4: 
                      5: # defaults for disk attributes
                      6: diskfile=hd.img   # HD image filename
                      7: partname=DOS      # partition name
                      8: 
1.1.1.2   root        9: # no args or first arg has non-digit characters?
                     10: if [ $# -lt 1 ] || [ \! -z "$(echo $1|tr -d 0-9)" ]; then
1.1       root       11:        name=${0##*/}
                     12:        echo
                     13:        echo "usage: $name <size> [filename] [partition name] [directory]"
                     14:        echo
                     15:        echo "Create an ACSI/IDE harddisk image for Hatari with a single Atari"
                     16:        echo "compatible DOS partition.  Arguments are (defaults in parenthesis):"
1.1.1.3   root       17:        echo "- size: harddisk image size in megabytes, 8-512"
1.1       root       18:        echo "- filename: name for the harddisk image ($diskfile)"
                     19:        echo "- partition name: name for that single partition ($partname)"
                     20:        echo "- directory: directory for initial content copied to the image"
                     21:        echo
                     22:        echo "For example:"
                     23:        echo "- 16MB '$diskfile' HD image:"
                     24:        echo "  $name 16"
                     25:        echo "-  8MB image with 'TEST' partition having files from content/:"
                     26:        echo "  $name 8 8mb-disk.img TEST content/"
                     27:        echo
                     28:        exit 1
                     29: fi
                     30: 
                     31: # sfdisk/mkdosfs reside in /sbin
                     32: PATH=/sbin:$PATH
                     33: export PATH
                     34: 
                     35: # check tools
1.1.1.2   root       36: if [ -z $(which mkdosfs) ] || [ -z $(which python) ]; then
                     37:        echo "ERROR: either mkdosfs or python tool missing!"
1.1       root       38:        exit 1
                     39: fi
                     40: 
                     41: # check disk size
1.1.1.4   root       42: disksize=$1
                     43: if [ $disksize -lt 5 ]; then
1.1       root       44:        echo "ERROR: disk size needs to be at least 5 (MB) to work properly."
                     45:        exit 1
                     46: fi
1.1.1.4   root       47: if [ $disksize -gt 512 ]; then
1.1.1.3   root       48:        echo "ERROR: mkdosfs supports Atari compatible partitions only up to 512 MB."
1.1       root       49:        exit 1
                     50: fi
1.1.1.2   root       51: 
1.1       root       52: # check optional arguments
                     53: if [ \! -z $2 ]; then
                     54:        diskfile=$2
                     55: fi
                     56: if [ \! -z $3 ]; then
                     57:        partname=$3
                     58: fi
1.1.1.4   root       59: 
                     60: # check content
                     61: convertdir=""
1.1       root       62: if [ \! -z $4 ]; then
1.1.1.4   root       63:        contentdir=${4%/}
                     64:        if [ \! -d $contentdir ]; then
                     65:                echo "ERROR: given content directory doesn't exist!"
                     66:                exit 1
                     67:        fi
                     68:        contentsize=$(du -ks $contentdir | awk '{printf("%d", $1/1024)}')
                     69:        if [ $contentsize -ge $disksize ]; then
                     70:                echo "ERROR: '$contentdir' directory contents ($contentsize MB) don't fit to given image size ($disksize MB)!"
                     71:                exit 1
                     72:        fi
                     73:        # name conversion script should be in same dir as this script, or in PATH
                     74:        convert=${0%/*}/atari-convert-dir.py
                     75:        if [ \! -x $convert ]; then
                     76:                if [ -z $(which atari-convert-dir) ]; then
                     77:                        echo "ERROR: $convert script for file name conversion missing!"
                     78:                        exit 1
                     79:                fi
                     80:                convert=atari-convert-dir
                     81:        fi
                     82:        convertdir=$contentdir.converted
1.1       root       83:        if [ -z $(which mcopy) ]; then
                     84:                echo "ERROR: mcopy (from Mtools) missing!"
                     85:                exit 1
                     86:        fi
                     87: fi
                     88: 
                     89: # don't overwrite files by accident
                     90: if [ -f $diskfile ]; then
                     91:        echo "ERROR: given harddisk image already exits. Give another name or remove it:"
                     92:        echo "  rm $diskfile"
                     93:        exit 1
                     94: fi
                     95: 
1.1.1.4   root       96: # disk geometry
                     97: skip=0            # alignment / "padding" sectors between MBR before partition
                     98: diskheads=16
                     99: tracksectors=32          # same as used by mkdosfs
                    100: sectorsize=512
                    101: 
                    102: # partition size in sectors:
                    103: # 16*32*512 is 1/4MB -> multiply by 4 to get number of required sectors
                    104: partsectors=$((4*$disksize*$diskheads*$tracksectors))
                    105: 
1.1       root      106: # temporary files
                    107: tmppart=$diskfile.part
                    108: 
1.1.1.4   root      109: # ------------------------------------------------------------------
                    110: 
1.1.1.2   root      111: error="premature script exit"
1.1       root      112: # script exit/error handling
                    113: exit_cleanup ()
                    114: {
                    115:        echo
1.1.1.2   root      116:        if [ -z "$error" ]; then
1.1       root      117:                echo "$step) Cleaning up..."
                    118:        else
1.1.1.2   root      119:                echo "ERROR: $error"
1.1       root      120:                echo
1.1.1.2   root      121:                echo "cleaning up..."
1.1.1.4   root      122:                if [ -f $diskfile ]; then
                    123:                        echo "rm -f $diskfile"
                    124:                        rm -f $diskfile
                    125:                fi
                    126:        fi
                    127:        if [ -f $tmppart ]; then
                    128:                echo "rm -f $tmppart"
                    129:                rm -f $tmppart
                    130:        fi
                    131:        if [ \! -z $convertdir ] && [ -d $convertdir ]; then
                    132:                echo "rm -f $convertdir"
                    133:                rm -f $convertdir
1.1       root      134:        fi
                    135:        echo "Done."
                    136: }
                    137: trap exit_cleanup EXIT
                    138: 
1.1.1.4   root      139: # ------------------------------------------------------------------
                    140: 
1.1       root      141: echo
                    142: step=1
1.1.1.4   root      143: echo "$step) Create DOS Master Boot Record / partition table..."
1.1.1.2   root      144: # See:
                    145: # - http://en.wikipedia.org/wiki/Master_boot_record
                    146: # - http://en.wikipedia.org/wiki/Cylinder-head-sector
                    147: # - http://en.wikipedia.org/wiki/File_Allocation_Table#Boot_Sector
                    148: # For DOS MBR, the values are little endian.
                    149: # -----------
                    150: python << EOF
                    151: #!/usr/bin/env python
                    152: mbr = bytearray(512)
                    153: 
                    154: def set_long(idx, value):
                    155:     mbr[idx+0] = value & 0xff
                    156:     mbr[idx+1] = value >> 8 & 0xff
                    157:     mbr[idx+2] = value >> 16 & 0xff
                    158:     mbr[idx+3] = value >> 24
                    159: 
                    160: def set_word(idx, value):
                    161:     mbr[idx] = value & 0xff
                    162:     mbr[idx+1] = value >> 8 & 0xff
                    163: 
                    164: def set_CHS(idx, values):
                    165:     c, h, s = values
                    166:     print "CHS: %3d,%3d,%3d @ $%x" % (c,h,s,idx)
                    167:     mbr[idx] = h
                    168:     mbr[idx+1] = (s & 0x3F) | ((c >> 2) & 0xC0)
                    169:     mbr[idx+2] = c & 0xFF
                    170: 
                    171: def LBA2CHS(lba):
                    172:     c = lba / ($tracksectors * $diskheads)
                    173:     h = (lba / $tracksectors) % $diskheads
                    174:     s = (lba % $tracksectors) + 1
                    175:     return (c,h,s)
                    176: 
                    177: # disk size
                    178: sectors = 1 + $skip + $partsectors
                    179: if sectors < 65536:
                    180:     set_word(0x13, sectors)
                    181:     set_long(0x20, sectors)
                    182:     parttype=0x4
                    183: else:
                    184:     set_long(0x20, sectors)
                    185:     parttype=0x6
                    186: 
                    187: # reserved sectors = MBR
                    188: mbr[0x0E] = 1
                    189: 
                    190: # CHS information
                    191: set_word(0x0B, $sectorsize)
                    192: mbr[0x0D] = 2 # sectors / cluster
                    193: set_word(0x18, $tracksectors)
                    194: set_word(0x1A, $diskheads)
                    195:   
                    196: # non-removable disk
                    197: mbr[0x15] = 0xF8
                    198: mbr[0x24] = 0x80
                    199: 
                    200: # partition size in sectors
                    201: partsectors = $partsectors - 1
                    202: # first partition takes all
                    203: offset = 0x1BE
                    204: mbr[offset] = 0x80 # bootable
                    205: mbr[offset+4] = parttype
                    206: # partition start & sector count in LBA
                    207: set_long(offset + 0x08, 1)
                    208: set_long(offset + 0x0C, partsectors)
                    209: # partition start & end in CHS
                    210: set_CHS(offset + 1, LBA2CHS(1))
                    211: set_CHS(offset + 5, LBA2CHS(partsectors))
                    212: # 3 last partitions are empty
                    213: for i in (1,2,3):
                    214:     offset += 0x10
                    215:     set_long(offset + 0x08, partsectors+1)
                    216:     set_long(offset + 0x0C, 0)
                    217:     set_CHS(offset + 1, LBA2CHS(partsectors+1))
                    218:     set_CHS(offset + 5, LBA2CHS(partsectors))
                    219: 
                    220: # MBR signature
                    221: mbr[0x1FE] = 0x55
                    222: mbr[0x1FF] = 0xAA
1.1       root      223: 
1.1.1.2   root      224: open("$diskfile", "wb").write(bytes(mbr))
1.1       root      225: EOF
1.1.1.2   root      226: # -----------
                    227: od -t x1 $diskfile
1.1       root      228: 
1.1.1.4   root      229: # ------------------------------------------------------------------
                    230: 
1.1       root      231: echo
                    232: step=$(($step+1))
1.1.1.4   root      233: echo "$step) Create an Atari TOS compatible DOS partition..."
1.1       root      234: # mkdosfs keeps the sector count below 32765 when -A is used by increasing
                    235: # the logical sector size (this is for TOS compatibility, -A guarantees
                    236: # also 2 sectors / cluster and Atari serial number etc).  Mtools barfs
                    237: # if partition size doesn't divide evenly with its track size.  Determine
                    238: # suitable cluster count & corresponding track size and align (decrease)
                    239: # the file system sector count accordingly.
                    240: tracksize=32
1.1.1.2   root      241: clustertmp=$((partsectors/2))
                    242: echo "Sectors: $partsectors, sectors/track: $tracksize, clusters: $clustertmp"
1.1       root      243: while [ $clustertmp -gt 32765 ]; do
                    244:        clustertmp=$((clustertmp/2))
                    245:        tracksize=$(($tracksize*2))
                    246:        echo "Doubling sector size as >32765 clusters -> $clustertmp clusters"
                    247: done
1.1.1.2   root      248: sectors=$(($partsectors/$tracksize))
1.1       root      249: sectors=$(($sectors*$tracksize))
                    250: kilobytes=$(($sectors/2))
1.1.1.2   root      251: if [ $sectors -ne $partsectors ]; then
                    252:        echo "Align sector count with clusters/sectors/track: $partsectors -> $sectors ($kilobytes kB)"
1.1       root      253: fi
1.1.1.2   root      254: echo "mkdosfs -A -F 16 -n $partname -C $tmppart $kilobytes"
                    255: mkdosfs -A -F 16 -n $partname -C $tmppart $kilobytes
1.1       root      256: 
1.1.1.4   root      257: # ------------------------------------------------------------------
                    258: 
1.1       root      259: if [ \! -z $contentdir ]; then
                    260:        echo
                    261:        step=$(($step+1))
1.1.1.4   root      262:        echo "$step) Clip/convert long file names to Atari compatible 8+3 format..."
                    263:        echo "$convert $contentdir $convertdir"
                    264:        $convert $contentdir $convertdir
                    265:        if [ $? -ne 0 ]; then
                    266:                error="conversion failed."
                    267:                exit 2
                    268:        fi
                    269: 
                    270:        echo
                    271:        step=$(($step+1))
1.1       root      272:        # copy contents of given directory to the new partition
1.1.1.4   root      273:        echo "$step) Copy the initial content to the partition..."
                    274:        echo "MTOOLS_NO_VFAT=1 mcopy -i $tmppart -spmv $convertdir/* ::"
                    275:        MTOOLS_NO_VFAT=1 mcopy -i $tmppart -spmv $convertdir/* ::
1.1       root      276:        if [ $? -ne 0 ]; then
1.1.1.2   root      277:                error="mcopy failed."
1.1       root      278:                exit 2
                    279:        fi
1.1.1.4   root      280:        echo "rm -rf $convertdir"
                    281:        rm -rf $convertdir
1.1       root      282: fi
                    283: 
1.1.1.4   root      284: # ------------------------------------------------------------------
                    285: 
1.1       root      286: echo
                    287: step=$(($step+1))
                    288: # copy the partition into disk
1.1.1.4   root      289: echo "$step) Copy the partition to disk image..."
1.1.1.2   root      290: echo "dd if=$tmppart of=$diskfile bs=512 seek=$((1+$skip)) count=$sectors"
                    291: dd if=$tmppart of=$diskfile bs=512 seek=$((1+$skip)) count=$sectors
1.1       root      292: 
                    293: step=$(($step+1))
                    294: # cleanup is done by exit_cleanup() trap
1.1.1.2   root      295: error=""

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.