Home | History | Annotate | Line # | Download | only in acorn32
mksparkive.sh revision 1.7
      1 #!/bin/sh -e
      2 #	$NetBSD: mksparkive.sh,v 1.7 2004/11/10 03:55:28 jmc 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 if [ -z "${TOOL_MKTEMP}" ]
     60 then
     61         TOOL_MKTEMP=mktemp
     62 fi
     63 
     64 
     65 # Target byte order is little endian.
     66 
     67 print2()
     68 {
     69 	if [ -z "$1" ]
     70 	then
     71 		exit 1
     72 	fi
     73 	lowbyte=`expr $1 % 256 | xargs printf %02x`
     74 	highbyte=`expr $1 / 256 | xargs printf %02x`
     75 	printf "\x$lowbyte\x$highbyte"
     76 }
     77 
     78 print4()
     79 {
     80 	if [ -z "$1" ]
     81 	then
     82 		exit 1
     83 	fi
     84 	print2 `expr $1 % 65536`
     85 	print2 `expr $1 / 65536`
     86 }
     87 
     88 makeheader()
     89 {
     90 	filename="$1"
     91 	statfilename="$2"
     92 	realfilename="$3"
     93 	filetype=`printf %03s "$4"`
     94 	compressed="$5"
     95 	# length is only passed to length4, so we don't need to worry about
     96 	# extracting only the length here.
     97 	length=`wc -c "$filename"`
     98 	eval `${TOOL_STAT} -s "$statfilename"`
     99 	# centiseconds since 1st Jan 1900
    100 	timestamp=`expr $st_mtime \* 100 + 220898880000`
    101 	lowtype=`echo "$filetype" | sed s/.//`
    102 	hightype=`echo "$filetype" | sed s/..\$//`
    103 	highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
    104 	lowdate=`expr $timestamp % 4294967296`
    105 
    106 	# Header version number
    107 	if [ "$compressed" -ne 0 ]
    108 	then
    109 		printf \\xff
    110 	else
    111 		printf \\x82
    112 	fi
    113 	# Filename
    114 	printf %-13.13s "$realfilename" | tr " ." \\0/
    115 	# Compressed file length
    116 	print4 $length
    117 	# File date stamp
    118 	print2 0
    119 	# File time stamp
    120 	print2 0
    121 	# CRC
    122 	if [ "$compressed" -ne 0 ]
    123 	then
    124 		print2 `${TOOL_SPARKCRC} "$statfilename"`
    125 	else
    126 		print2 `${TOOL_SPARKCRC} "$filename"`
    127 	fi
    128 	# Original file length
    129 	if [ "$compressed" -ne 0 ]
    130 	then
    131 		print4 $st_size
    132 	else
    133 		print4 $length
    134 	fi
    135 	# Load address (FFFtttdd)
    136 	printf \\x$highdate
    137 	printf \\x$lowtype
    138 	printf \\xf$hightype
    139 	printf \\xff
    140 	# Exec address (dddddddd)
    141 	print4 $lowdate
    142 	# Attributes
    143 	# Public read, owner read/write
    144 	print4 19
    145 }
    146 
    147 makearchive()
    148 {
    149 	for file in "$@"
    150 	do
    151 		temp=`${TOOL_MKTEMP} -t $progname` || exit 1
    152 		trap "rm -f $temp" 0
    153 		# Archive marker
    154 		printf \\x1a
    155 		if [ -f "$file" ]
    156 		then
    157 			case "$file" in
    158 				-*)	echo "Invalid filename" >&2
    159 					exit 1
    160 					;;
    161 				*,???)	type=`echo "$file" | \
    162 					    sed "s/.*,\(...\)$/\1/"`
    163 					filename=`echo "$file" | \
    164 					    sed "s/,...$//"`
    165 					;;
    166 				*)	type=fff
    167 					filename="$file"
    168 					;;
    169 			esac
    170 			# The compressed data in a sparkive is the output from
    171 			# compress, minus the two bytes of magic at the start.
    172 			# Compress also uses the top bit of the first byte
    173 			# to indicate its choice of algorithm. Spark doesn't
    174 			# understand that, so it must be stripped.
    175 			compress -c "$file" | tail -c +3 >"$temp"
    176 			size1=`wc -c "$file" | awk '{print $1}'`
    177 			size2=`wc -c "$temp" | awk '{print $1}'`
    178 			if [ $size1 -ge $size2 ]
    179 			then
    180 				makeheader "$temp" "$file" "$filename" "$type" 1
    181 				nbits=`dd if="$temp" bs=1 count=1 2>/dev/null| \
    182 				    od -t d1 | awk '{print $2}'`
    183 				if [ $nbits -ge 128 ]
    184 				then
    185 					nbits=`expr $nbits - 128`
    186 				fi
    187 				printf \\x`printf %02x $nbits`
    188 				tail -c +2 "$temp"
    189 			else
    190 				makeheader "$file" "$file" "$filename" "$type" 0
    191 				cat "$file"
    192 			fi
    193 		fi
    194 		if [ -d "$file" ]
    195 		then
    196 			(
    197 				cd "$file"
    198 				makearchive `ls -A` >$temp
    199 			)
    200 			if [ $? -ne 0 ]
    201 			then
    202 				exit 1
    203 			fi
    204 			makeheader "$temp" "$file" "$file" ddc 0
    205 			cat "$temp"
    206 		fi
    207 		rm -f "$temp"
    208 	done
    209 
    210 	# Archive marker
    211 	printf \\x1a
    212 	# Archive terminator
    213 	printf \\x00
    214 }
    215 
    216 progname=`basename $0`
    217 
    218 if [ $# -eq 0 ]
    219 then
    220 	echo "Usage: $progname filename"
    221 	echo "$progname: Outputs an uncompressed sparkive to stdout."
    222 fi
    223 
    224 makearchive "$@"
    225