Home | History | Annotate | Line # | Download | only in acorn32
mksparkive.sh revision 1.1
      1  1.1  gavan #!/bin/sh
      2  1.1  gavan #
      3  1.1  gavan # Copyright (c) 2004 The NetBSD Foundation, Inc.
      4  1.1  gavan # All rights reserved.
      5  1.1  gavan #
      6  1.1  gavan # This code is derived from software contributed to The NetBSD Foundation
      7  1.1  gavan # by Gavan Fantom
      8  1.1  gavan #
      9  1.1  gavan # Redistribution and use in source and binary forms, with or without
     10  1.1  gavan # modification, are permitted provided that the following conditions
     11  1.1  gavan # are met:
     12  1.1  gavan # 1. Redistributions of source code must retain the above copyright
     13  1.1  gavan #    notice, this list of conditions and the following disclaimer.
     14  1.1  gavan # 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  gavan #    notice, this list of conditions and the following disclaimer in the
     16  1.1  gavan #    documentation and/or other materials provided with the distribution.
     17  1.1  gavan # 3. All advertising materials mentioning features or use of this software
     18  1.1  gavan #    must display the following acknowledgement:
     19  1.1  gavan #        This product includes software developed by the NetBSD
     20  1.1  gavan #        Foundation, Inc. and its contributors.
     21  1.1  gavan # 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.1  gavan #    contributors may be used to endorse or promote products derived
     23  1.1  gavan #    from this software without specific prior written permission.
     24  1.1  gavan #
     25  1.1  gavan # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.1  gavan # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  gavan # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  gavan # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.1  gavan # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  gavan # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  gavan # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  gavan # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  gavan # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  gavan # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  gavan # POSSIBILITY OF SUCH DAMAGE.
     36  1.1  gavan #
     37  1.1  gavan 
     38  1.1  gavan #
     39  1.1  gavan # Creates an uncompressed spark format archive. Some metadata is included,
     40  1.1  gavan # notably filetypes, but CRC calculations and permissions are not. Filename
     41  1.1  gavan # translation is performed according to RISC OS conventions.
     42  1.1  gavan # 
     43  1.1  gavan # This script is intended to provide sufficient functionality to create
     44  1.1  gavan # an archive for distribution of the NetBSD/acorn32 bootloader which can be
     45  1.1  gavan # used directly in RISC OS.
     46  1.1  gavan #
     47  1.1  gavan 
     48  1.1  gavan # Target byte order is little endian.
     49  1.1  gavan 
     50  1.1  gavan print2()
     51  1.1  gavan {
     52  1.1  gavan 	lowbyte=`expr $1 % 256 | xargs printf %02x`
     53  1.1  gavan 	highbyte=`expr $1 / 256 | xargs printf %02x`
     54  1.1  gavan 	printf "\x$lowbyte\x$highbyte"
     55  1.1  gavan }
     56  1.1  gavan 
     57  1.1  gavan print4()
     58  1.1  gavan {
     59  1.1  gavan 	print2 `expr $1 % 65536`
     60  1.1  gavan 	print2 `expr $1 / 65536`
     61  1.1  gavan }
     62  1.1  gavan 
     63  1.1  gavan makeheader()
     64  1.1  gavan {
     65  1.1  gavan 	filename="$1"
     66  1.1  gavan 	statfilename="$2"
     67  1.1  gavan 	realfilename="$3"
     68  1.1  gavan 	filetype=`printf %03s "$4"`
     69  1.1  gavan 	length=`wc -c "$filename"`
     70  1.1  gavan 	eval `stat -s "$statfilename"`
     71  1.1  gavan 	# centiseconds since 1st Jan 1900
     72  1.1  gavan 	timestamp=`expr $st_mtime \* 100 + 220898880000`
     73  1.1  gavan 	lowtype=`echo "$filetype" | sed s/.//`
     74  1.1  gavan 	hightype=`echo "$filetype" | sed s/..\$//`
     75  1.1  gavan 	highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
     76  1.1  gavan 	lowdate=`expr $timestamp % 4294967296`
     77  1.1  gavan 
     78  1.1  gavan 	# Header version number
     79  1.1  gavan 	printf \\x82
     80  1.1  gavan 	# Filename
     81  1.1  gavan 	printf %-13.13s "$realfilename" | tr " ." \\0/
     82  1.1  gavan 	# Compressed file length
     83  1.1  gavan 	print4 $length
     84  1.1  gavan 	# File date stamp
     85  1.1  gavan 	print2 0
     86  1.1  gavan 	# File time stamp
     87  1.1  gavan 	print2 0
     88  1.1  gavan 	# CRC
     89  1.1  gavan 	print2 0
     90  1.1  gavan 	# Original file length
     91  1.1  gavan 	print4 $length
     92  1.1  gavan 	# Load address (FFFtttdd)
     93  1.1  gavan 	printf \\x$highdate
     94  1.1  gavan 	printf \\x$lowtype
     95  1.1  gavan 	printf \\xf$hightype
     96  1.1  gavan 	printf \\xff
     97  1.1  gavan 	# Exec address (dddddddd)
     98  1.1  gavan 	print4 $lowdate
     99  1.1  gavan 	# Attributes
    100  1.1  gavan 	# Public read, owner read/write
    101  1.1  gavan 	print4 19
    102  1.1  gavan }
    103  1.1  gavan 
    104  1.1  gavan makearchive()
    105  1.1  gavan {
    106  1.1  gavan 	for file in "$@"
    107  1.1  gavan 	do
    108  1.1  gavan 		# Archive marker
    109  1.1  gavan 		printf \\x1a
    110  1.1  gavan 		if [ -f "$file" ]
    111  1.1  gavan 		then
    112  1.1  gavan 			case "$file" in
    113  1.1  gavan 				*,???)	type=`echo "$file" | \
    114  1.1  gavan 					    sed "s/.*,\(...\)$/\1/"`
    115  1.1  gavan 					filename=`echo "$file" | \
    116  1.1  gavan 					    sed "s/,...$//"`
    117  1.1  gavan 					;;
    118  1.1  gavan 				*)	type=fff
    119  1.1  gavan 					filename="$file"
    120  1.1  gavan 					;;
    121  1.1  gavan 			esac
    122  1.1  gavan 			makeheader "$file" "$file" "$filename" "$type"
    123  1.1  gavan 			cat "$file"
    124  1.1  gavan 		fi
    125  1.1  gavan 		if [ -d "$file" ]
    126  1.1  gavan 		then
    127  1.1  gavan 			temp=`mktemp -t $0` || exit 1
    128  1.1  gavan 			(
    129  1.1  gavan 				cd "$file"
    130  1.1  gavan 				makearchive `ls -A` >$temp
    131  1.1  gavan 			)
    132  1.1  gavan 			makeheader "$temp" "$file" "$file" ddc
    133  1.1  gavan 			cat "$temp"
    134  1.1  gavan 			rm -f "$temp"
    135  1.1  gavan 		fi
    136  1.1  gavan 	done
    137  1.1  gavan 
    138  1.1  gavan 	# Archive marker
    139  1.1  gavan 	printf \\x1a
    140  1.1  gavan 	# Archive terminator
    141  1.1  gavan 	printf \\x00
    142  1.1  gavan }
    143  1.1  gavan 
    144  1.1  gavan if [ $# -eq 0 ]
    145  1.1  gavan then
    146  1.1  gavan 	name=`basename $0`
    147  1.1  gavan 	echo "Usage: $name filename"
    148  1.1  gavan 	echo "$name: Outputs an uncompressed sparkive to stdout."
    149  1.1  gavan fi
    150  1.1  gavan 
    151  1.1  gavan makearchive "$@"
    152