Home | History | Annotate | Line # | Download | only in acorn32
mksparkive.sh revision 1.1
      1 #!/bin/sh
      2 #
      3 # Copyright (c) 2004 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # This code is derived from software contributed to The NetBSD Foundation
      7 # by Gavan Fantom
      8 #
      9 # Redistribution and use in source and binary forms, with or without
     10 # modification, are permitted provided that the following conditions
     11 # are met:
     12 # 1. Redistributions of source code must retain the above copyright
     13 #    notice, this list of conditions and the following disclaimer.
     14 # 2. Redistributions in binary form must reproduce the above copyright
     15 #    notice, this list of conditions and the following disclaimer in the
     16 #    documentation and/or other materials provided with the distribution.
     17 # 3. All advertising materials mentioning features or use of this software
     18 #    must display the following acknowledgement:
     19 #        This product includes software developed by the NetBSD
     20 #        Foundation, Inc. and its contributors.
     21 # 4. Neither the name of The NetBSD Foundation nor the names of its
     22 #    contributors may be used to endorse or promote products derived
     23 #    from this software without specific prior written permission.
     24 #
     25 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35 # POSSIBILITY OF SUCH DAMAGE.
     36 #
     37 
     38 #
     39 # Creates an uncompressed spark format archive. Some metadata is included,
     40 # notably filetypes, but CRC calculations and permissions are not. Filename
     41 # translation is performed according to RISC OS conventions.
     42 # 
     43 # This script is intended to provide sufficient functionality to create
     44 # an archive for distribution of the NetBSD/acorn32 bootloader which can be
     45 # used directly in RISC OS.
     46 #
     47 
     48 # Target byte order is little endian.
     49 
     50 print2()
     51 {
     52 	lowbyte=`expr $1 % 256 | xargs printf %02x`
     53 	highbyte=`expr $1 / 256 | xargs printf %02x`
     54 	printf "\x$lowbyte\x$highbyte"
     55 }
     56 
     57 print4()
     58 {
     59 	print2 `expr $1 % 65536`
     60 	print2 `expr $1 / 65536`
     61 }
     62 
     63 makeheader()
     64 {
     65 	filename="$1"
     66 	statfilename="$2"
     67 	realfilename="$3"
     68 	filetype=`printf %03s "$4"`
     69 	length=`wc -c "$filename"`
     70 	eval `stat -s "$statfilename"`
     71 	# centiseconds since 1st Jan 1900
     72 	timestamp=`expr $st_mtime \* 100 + 220898880000`
     73 	lowtype=`echo "$filetype" | sed s/.//`
     74 	hightype=`echo "$filetype" | sed s/..\$//`
     75 	highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
     76 	lowdate=`expr $timestamp % 4294967296`
     77 
     78 	# Header version number
     79 	printf \\x82
     80 	# Filename
     81 	printf %-13.13s "$realfilename" | tr " ." \\0/
     82 	# Compressed file length
     83 	print4 $length
     84 	# File date stamp
     85 	print2 0
     86 	# File time stamp
     87 	print2 0
     88 	# CRC
     89 	print2 0
     90 	# Original file length
     91 	print4 $length
     92 	# Load address (FFFtttdd)
     93 	printf \\x$highdate
     94 	printf \\x$lowtype
     95 	printf \\xf$hightype
     96 	printf \\xff
     97 	# Exec address (dddddddd)
     98 	print4 $lowdate
     99 	# Attributes
    100 	# Public read, owner read/write
    101 	print4 19
    102 }
    103 
    104 makearchive()
    105 {
    106 	for file in "$@"
    107 	do
    108 		# Archive marker
    109 		printf \\x1a
    110 		if [ -f "$file" ]
    111 		then
    112 			case "$file" in
    113 				*,???)	type=`echo "$file" | \
    114 					    sed "s/.*,\(...\)$/\1/"`
    115 					filename=`echo "$file" | \
    116 					    sed "s/,...$//"`
    117 					;;
    118 				*)	type=fff
    119 					filename="$file"
    120 					;;
    121 			esac
    122 			makeheader "$file" "$file" "$filename" "$type"
    123 			cat "$file"
    124 		fi
    125 		if [ -d "$file" ]
    126 		then
    127 			temp=`mktemp -t $0` || exit 1
    128 			(
    129 				cd "$file"
    130 				makearchive `ls -A` >$temp
    131 			)
    132 			makeheader "$temp" "$file" "$file" ddc
    133 			cat "$temp"
    134 			rm -f "$temp"
    135 		fi
    136 	done
    137 
    138 	# Archive marker
    139 	printf \\x1a
    140 	# Archive terminator
    141 	printf \\x00
    142 }
    143 
    144 if [ $# -eq 0 ]
    145 then
    146 	name=`basename $0`
    147 	echo "Usage: $name filename"
    148 	echo "$name: Outputs an uncompressed sparkive to stdout."
    149 fi
    150 
    151 makearchive "$@"
    152