Home | History | Annotate | Line # | Download | only in acorn32
mksparkive.sh revision 1.6
      1 #!/bin/sh -e
      2 #	$NetBSD: mksparkive.sh,v 1.6 2004/08/24 21:32:12 gavan Exp $
      3 #
      4 # Copyright (c) 2004 The NetBSD Foundation, Inc.
      5 # All rights reserved.
      6 #
      7 # This code is derived from software contributed to The NetBSD Foundation
      8 # by Gavan Fantom
      9 #
     10 # Redistribution and use in source and binary forms, with or without
     11 # modification, are permitted provided that the following conditions
     12 # are met:
     13 # 1. Redistributions of source code must retain the above copyright
     14 #    notice, this list of conditions and the following disclaimer.
     15 # 2. Redistributions in binary form must reproduce the above copyright
     16 #    notice, this list of conditions and the following disclaimer in the
     17 #    documentation and/or other materials provided with the distribution.
     18 # 3. All advertising materials mentioning features or use of this software
     19 #    must display the following acknowledgement:
     20 #        This product includes software developed by the NetBSD
     21 #        Foundation, Inc. and its contributors.
     22 # 4. Neither the name of The NetBSD Foundation nor the names of its
     23 #    contributors may be used to endorse or promote products derived
     24 #    from this software without specific prior written permission.
     25 #
     26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36 # POSSIBILITY OF SUCH DAMAGE.
     37 #
     38 
     39 #
     40 # Creates a spark format archive. Some metadata is included, notably
     41 # filetypes, but permissions are not. Filename translation is performed
     42 # according to RISC OS conventions.
     43 # 
     44 # This script is intended to provide sufficient functionality to create
     45 # an archive for distribution of the NetBSD/acorn32 bootloader which can be
     46 # used directly in RISC OS.
     47 #
     48 
     49 if [ -z "${TOOL_SPARKCRC}" ]
     50 then
     51 	TOOL_SPARKCRC=sparkcrc
     52 fi
     53 
     54 if [ -z "${TOOL_STAT}" ]
     55 then
     56 	TOOL_STAT=stat
     57 fi
     58 
     59 # Target byte order is little endian.
     60 
     61 print2()
     62 {
     63 	if [ -z "$1" ]
     64 	then
     65 		exit 1
     66 	fi
     67 	lowbyte=`expr $1 % 256 | xargs printf %02x`
     68 	highbyte=`expr $1 / 256 | xargs printf %02x`
     69 	printf "\x$lowbyte\x$highbyte"
     70 }
     71 
     72 print4()
     73 {
     74 	if [ -z "$1" ]
     75 	then
     76 		exit 1
     77 	fi
     78 	print2 `expr $1 % 65536`
     79 	print2 `expr $1 / 65536`
     80 }
     81 
     82 makeheader()
     83 {
     84 	filename="$1"
     85 	statfilename="$2"
     86 	realfilename="$3"
     87 	filetype=`printf %03s "$4"`
     88 	compressed="$5"
     89 	# length is only passed to length4, so we don't need to worry about
     90 	# extracting only the length here.
     91 	length=`wc -c "$filename"`
     92 	eval `${TOOL_STAT} -s "$statfilename"`
     93 	# centiseconds since 1st Jan 1900
     94 	timestamp=`expr $st_mtime \* 100 + 220898880000`
     95 	lowtype=`echo "$filetype" | sed s/.//`
     96 	hightype=`echo "$filetype" | sed s/..\$//`
     97 	highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
     98 	lowdate=`expr $timestamp % 4294967296`
     99 
    100 	# Header version number
    101 	if [ "$compressed" -ne 0 ]
    102 	then
    103 		printf \\xff
    104 	else
    105 		printf \\x82
    106 	fi
    107 	# Filename
    108 	printf %-13.13s "$realfilename" | tr " ." \\0/
    109 	# Compressed file length
    110 	print4 $length
    111 	# File date stamp
    112 	print2 0
    113 	# File time stamp
    114 	print2 0
    115 	# CRC
    116 	if [ "$compressed" -ne 0 ]
    117 	then
    118 		print2 `${TOOL_SPARKCRC} "$statfilename"`
    119 	else
    120 		print2 `${TOOL_SPARKCRC} "$filename"`
    121 	fi
    122 	# Original file length
    123 	if [ "$compressed" -ne 0 ]
    124 	then
    125 		print4 $st_size
    126 	else
    127 		print4 $length
    128 	fi
    129 	# Load address (FFFtttdd)
    130 	printf \\x$highdate
    131 	printf \\x$lowtype
    132 	printf \\xf$hightype
    133 	printf \\xff
    134 	# Exec address (dddddddd)
    135 	print4 $lowdate
    136 	# Attributes
    137 	# Public read, owner read/write
    138 	print4 19
    139 }
    140 
    141 makearchive()
    142 {
    143 	for file in "$@"
    144 	do
    145 		temp=`mktemp -t $progname` || exit 1
    146 		trap "rm -f $temp" 0
    147 		# Archive marker
    148 		printf \\x1a
    149 		if [ -f "$file" ]
    150 		then
    151 			case "$file" in
    152 				-*)	echo "Invalid filename" >&2
    153 					exit 1
    154 					;;
    155 				*,???)	type=`echo "$file" | \
    156 					    sed "s/.*,\(...\)$/\1/"`
    157 					filename=`echo "$file" | \
    158 					    sed "s/,...$//"`
    159 					;;
    160 				*)	type=fff
    161 					filename="$file"
    162 					;;
    163 			esac
    164 			# The compressed data in a sparkive is the output from
    165 			# compress, minus the two bytes of magic at the start.
    166 			# Compress also uses the top bit of the first byte
    167 			# to indicate its choice of algorithm. Spark doesn't
    168 			# understand that, so it must be stripped.
    169 			compress -c "$file" | tail -c +3 >"$temp"
    170 			size1=`wc -c "$file" | awk '{print $1}'`
    171 			size2=`wc -c "$temp" | awk '{print $1}'`
    172 			if [ $size1 -ge $size2 ]
    173 			then
    174 				makeheader "$temp" "$file" "$filename" "$type" 1
    175 				nbits=`dd if="$temp" bs=1 count=1 2>/dev/null| \
    176 				    od -t d1 | awk '{print $2}'`
    177 				if [ $nbits -ge 128 ]
    178 				then
    179 					nbits=`expr $nbits - 128`
    180 				fi
    181 				printf \\x`printf %02x $nbits`
    182 				tail -c +2 "$temp"
    183 			else
    184 				makeheader "$file" "$file" "$filename" "$type" 0
    185 				cat "$file"
    186 			fi
    187 		fi
    188 		if [ -d "$file" ]
    189 		then
    190 			(
    191 				cd "$file"
    192 				makearchive `ls -A` >$temp
    193 			)
    194 			if [ $? -ne 0 ]
    195 			then
    196 				exit 1
    197 			fi
    198 			makeheader "$temp" "$file" "$file" ddc 0
    199 			cat "$temp"
    200 		fi
    201 		rm -f "$temp"
    202 	done
    203 
    204 	# Archive marker
    205 	printf \\x1a
    206 	# Archive terminator
    207 	printf \\x00
    208 }
    209 
    210 progname=`basename $0`
    211 
    212 if [ $# -eq 0 ]
    213 then
    214 	echo "Usage: $progname filename"
    215 	echo "$progname: Outputs an uncompressed sparkive to stdout."
    216 fi
    217 
    218 makearchive "$@"
    219