2 # $NetBSD: mksparkive.sh,v 1.7 2004/11/10 03:55:28 jmc Exp $
4 # Copyright (c) 2004 The NetBSD Foundation, Inc.
7 # This code is derived from software contributed to The NetBSD Foundation
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
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.
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.
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.
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.
42 if [ -z "${TOOL_SPARKCRC}" ]
44 TOOL_SPARKCRC
=sparkcrc
47 if [ -z "${TOOL_STAT}" ]
52 if [ -z "${TOOL_MKTEMP}" ]
58 # Target byte order is little endian.
66 lowbyte
=`expr $1 % 256 | xargs printf %02x`
67 highbyte
=`expr $1 / 256 | xargs printf %02x`
68 printf "\x$lowbyte\x$highbyte"
77 print2
`expr $1 % 65536`
78 print2
`expr $1 / 65536`
86 filetype
=`printf %03s "$4"`
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`
99 # Header version number
100 if [ "$compressed" -ne 0 ]
107 printf %-13.13s
"$realfilename" |
tr " ." \\0/
108 # Compressed file length
115 if [ "$compressed" -ne 0 ]
117 print2
`${TOOL_SPARKCRC} "$statfilename"`
119 print2
`${TOOL_SPARKCRC} "$filename"`
121 # Original file length
122 if [ "$compressed" -ne 0 ]
128 # Load address (FFFtttdd)
133 # Exec address (dddddddd)
136 # Public read, owner read/write
144 temp
=`${TOOL_MKTEMP} -t $progname` ||
exit 1
151 -*) echo "Invalid filename" >&2
154 *,???
) type=`echo "$file" | \
155 sed "s/.*,\(...\)$/\1/"`
156 filename
=`echo "$file" | \
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 ]
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 ]
178 nbits
=`expr $nbits - 128`
180 printf \\x
`printf %02x $nbits`
183 makeheader
"$file" "$file" "$filename" "$type" 0
191 makearchive
`ls -A` >$temp
197 makeheader
"$temp" "$file" "$file" ddc
0
209 progname
=`basename $0`
213 echo "Usage: $progname filename"
214 echo "$progname: Outputs an uncompressed sparkive to stdout."