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