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