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