Home | History | Annotate | Line # | Download | only in acorn32
mksparkive.sh revision 1.2.2.2
      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.2  jmc # Creates an uncompressed spark format archive. Some metadata is included,
     40  1.2.2.2  jmc # notably filetypes, but CRC calculations and permissions are not. Filename
     41  1.2.2.2  jmc # translation is performed 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.2  jmc # Target byte order is little endian.
     49  1.2.2.2  jmc 
     50  1.2.2.2  jmc print2()
     51  1.2.2.2  jmc {
     52  1.2.2.2  jmc 	lowbyte=`expr $1 % 256 | xargs printf %02x`
     53  1.2.2.2  jmc 	highbyte=`expr $1 / 256 | xargs printf %02x`
     54  1.2.2.2  jmc 	printf "\x$lowbyte\x$highbyte"
     55  1.2.2.2  jmc }
     56  1.2.2.2  jmc 
     57  1.2.2.2  jmc print4()
     58  1.2.2.2  jmc {
     59  1.2.2.2  jmc 	print2 `expr $1 % 65536`
     60  1.2.2.2  jmc 	print2 `expr $1 / 65536`
     61  1.2.2.2  jmc }
     62  1.2.2.2  jmc 
     63  1.2.2.2  jmc makeheader()
     64  1.2.2.2  jmc {
     65  1.2.2.2  jmc 	filename="$1"
     66  1.2.2.2  jmc 	statfilename="$2"
     67  1.2.2.2  jmc 	realfilename="$3"
     68  1.2.2.2  jmc 	filetype=`printf %03s "$4"`
     69  1.2.2.2  jmc 	length=`wc -c "$filename"`
     70  1.2.2.2  jmc 	eval `stat -s "$statfilename"`
     71  1.2.2.2  jmc 	# centiseconds since 1st Jan 1900
     72  1.2.2.2  jmc 	timestamp=`expr $st_mtime \* 100 + 220898880000`
     73  1.2.2.2  jmc 	lowtype=`echo "$filetype" | sed s/.//`
     74  1.2.2.2  jmc 	hightype=`echo "$filetype" | sed s/..\$//`
     75  1.2.2.2  jmc 	highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
     76  1.2.2.2  jmc 	lowdate=`expr $timestamp % 4294967296`
     77  1.2.2.2  jmc 
     78  1.2.2.2  jmc 	# Header version number
     79  1.2.2.2  jmc 	printf \\x82
     80  1.2.2.2  jmc 	# Filename
     81  1.2.2.2  jmc 	printf %-13.13s "$realfilename" | tr " ." \\0/
     82  1.2.2.2  jmc 	# Compressed file length
     83  1.2.2.2  jmc 	print4 $length
     84  1.2.2.2  jmc 	# File date stamp
     85  1.2.2.2  jmc 	print2 0
     86  1.2.2.2  jmc 	# File time stamp
     87  1.2.2.2  jmc 	print2 0
     88  1.2.2.2  jmc 	# CRC
     89  1.2.2.2  jmc 	print2 0
     90  1.2.2.2  jmc 	# Original file length
     91  1.2.2.2  jmc 	print4 $length
     92  1.2.2.2  jmc 	# Load address (FFFtttdd)
     93  1.2.2.2  jmc 	printf \\x$highdate
     94  1.2.2.2  jmc 	printf \\x$lowtype
     95  1.2.2.2  jmc 	printf \\xf$hightype
     96  1.2.2.2  jmc 	printf \\xff
     97  1.2.2.2  jmc 	# Exec address (dddddddd)
     98  1.2.2.2  jmc 	print4 $lowdate
     99  1.2.2.2  jmc 	# Attributes
    100  1.2.2.2  jmc 	# Public read, owner read/write
    101  1.2.2.2  jmc 	print4 19
    102  1.2.2.2  jmc }
    103  1.2.2.2  jmc 
    104  1.2.2.2  jmc makearchive()
    105  1.2.2.2  jmc {
    106  1.2.2.2  jmc 	for file in "$@"
    107  1.2.2.2  jmc 	do
    108  1.2.2.2  jmc 		# Archive marker
    109  1.2.2.2  jmc 		printf \\x1a
    110  1.2.2.2  jmc 		if [ -f "$file" ]
    111  1.2.2.2  jmc 		then
    112  1.2.2.2  jmc 			case "$file" in
    113  1.2.2.2  jmc 				*,???)	type=`echo "$file" | \
    114  1.2.2.2  jmc 					    sed "s/.*,\(...\)$/\1/"`
    115  1.2.2.2  jmc 					filename=`echo "$file" | \
    116  1.2.2.2  jmc 					    sed "s/,...$//"`
    117  1.2.2.2  jmc 					;;
    118  1.2.2.2  jmc 				*)	type=fff
    119  1.2.2.2  jmc 					filename="$file"
    120  1.2.2.2  jmc 					;;
    121  1.2.2.2  jmc 			esac
    122  1.2.2.2  jmc 			makeheader "$file" "$file" "$filename" "$type"
    123  1.2.2.2  jmc 			cat "$file"
    124  1.2.2.2  jmc 		fi
    125  1.2.2.2  jmc 		if [ -d "$file" ]
    126  1.2.2.2  jmc 		then
    127  1.2.2.2  jmc 			temp=`mktemp -t $0` || exit 1
    128  1.2.2.2  jmc 			(
    129  1.2.2.2  jmc 				cd "$file"
    130  1.2.2.2  jmc 				makearchive `ls -A` >$temp
    131  1.2.2.2  jmc 			)
    132  1.2.2.2  jmc 			makeheader "$temp" "$file" "$file" ddc
    133  1.2.2.2  jmc 			cat "$temp"
    134  1.2.2.2  jmc 			rm -f "$temp"
    135  1.2.2.2  jmc 		fi
    136  1.2.2.2  jmc 	done
    137  1.2.2.2  jmc 
    138  1.2.2.2  jmc 	# Archive marker
    139  1.2.2.2  jmc 	printf \\x1a
    140  1.2.2.2  jmc 	# Archive terminator
    141  1.2.2.2  jmc 	printf \\x00
    142  1.2.2.2  jmc }
    143  1.2.2.2  jmc 
    144  1.2.2.2  jmc if [ $# -eq 0 ]
    145  1.2.2.2  jmc then
    146  1.2.2.2  jmc 	name=`basename $0`
    147  1.2.2.2  jmc 	echo "Usage: $name filename"
    148  1.2.2.2  jmc 	echo "$name: Outputs an uncompressed sparkive to stdout."
    149  1.2.2.2  jmc fi
    150  1.2.2.2  jmc 
    151  1.2.2.2  jmc makearchive "$@"
    152