1 #!/bin/sh -e 2 # $NetBSD: mksparkive.sh,v 1.9 2024/02/09 15:34:34 christos 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 42 if [ -z "${TOOL_SPARKCRC}" ] 43 then 44 TOOL_SPARKCRC=sparkcrc 45 fi 46 47 if [ -z "${TOOL_STAT}" ] 48 then 49 TOOL_STAT=stat 50 fi 51 52 if [ -z "${TOOL_MKTEMP}" ] 53 then 54 TOOL_MKTEMP=mktemp 55 fi 56 57 58 # Target byte order is little endian. 59 60 print2() 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 71 print4() 72 { 73 if [ -z "$1" ] 74 then 75 exit 1 76 fi 77 print2 $(expr $1 % 65536) 78 print2 $(expr $1 / 65536) 79 } 80 81 makeheader() 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 [ -n "${MKREPRO_TIMESTAMP}" ] && st_mtime=${MKREPRO_TIMESTAMP} 93 # centiseconds since 1st Jan 1900 94 timestamp=$(expr $st_mtime \* 100 + 220898880000) 95 lowtype=$(echo "$filetype" | sed s/.//) 96 hightype=$(echo "$filetype" | sed s/..\$//) 97 highdate=$(expr $timestamp / 4294967296 | xargs printf %02x) 98 lowdate=$(expr $timestamp % 4294967296) 99 100 # Header version number 101 if [ "$compressed" -ne 0 ] 102 then 103 printf \\xff 104 else 105 printf \\x82 106 fi 107 # Filename 108 printf %-13.13s "$realfilename" | tr " ." \\0/ 109 # Compressed file length 110 print4 $length 111 # File date stamp 112 print2 0 113 # File time stamp 114 print2 0 115 # CRC 116 if [ "$compressed" -ne 0 ] 117 then 118 print2 $(${TOOL_SPARKCRC} "$statfilename") 119 else 120 print2 $(${TOOL_SPARKCRC} "$filename") 121 fi 122 # Original file length 123 if [ "$compressed" -ne 0 ] 124 then 125 print4 $st_size 126 else 127 print4 $length 128 fi 129 # Load address (FFFtttdd) 130 printf \\x$highdate 131 printf \\x$lowtype 132 printf \\xf$hightype 133 printf \\xff 134 # Exec address (dddddddd) 135 print4 $lowdate 136 # Attributes 137 # Public read, owner read/write 138 print4 19 139 } 140 141 makearchive() 142 { 143 for file in "$@" 144 do 145 temp=$(${TOOL_MKTEMP} -t $progname) || exit 1 146 trap "rm -f $temp" 0 147 # Archive marker 148 printf \\x1a 149 if [ -f "$file" ] 150 then 151 case "$file" in 152 -*) echo "Invalid filename" >&2 153 exit 1 154 ;; 155 *,???) type=$(echo "$file" | \ 156 sed "s/.*,\(...\)$/\1/") 157 filename=$(echo "$file" | \ 158 sed "s/,...$//") 159 ;; 160 *) type=fff 161 filename="$file" 162 ;; 163 esac 164 # The compressed data in a sparkive is the output from 165 # compress, minus the two bytes of magic at the start. 166 # Compress also uses the top bit of the first byte 167 # to indicate its choice of algorithm. Spark doesn't 168 # understand that, so it must be stripped. 169 compress -c "$file" | tail -c +3 >"$temp" 170 size1=$(wc -c "$file" | awk '{print $1}') 171 size2=$(wc -c "$temp" | awk '{print $1}') 172 if [ $size1 -ge $size2 ] 173 then 174 makeheader "$temp" "$file" "$filename" "$type" 1 175 nbits=$(dd if="$temp" bs=1 count=1 \ 176 2>/dev/null | od -t d1 | awk '{print $2}') 177 if [ $nbits -ge 128 ] 178 then 179 nbits=$(expr $nbits - 128) 180 fi 181 printf \\x$(printf %02x $nbits) 182 tail -c +2 "$temp" 183 else 184 makeheader "$file" "$file" "$filename" "$type" 0 185 cat "$file" 186 fi 187 fi 188 if [ -d "$file" ] 189 then 190 ( 191 cd "$file" 192 makearchive $(ls -A) >$temp 193 ) 194 if [ $? -ne 0 ] 195 then 196 exit 1 197 fi 198 makeheader "$temp" "$file" "$file" ddc 0 199 cat "$temp" 200 fi 201 rm -f "$temp" 202 done 203 204 # Archive marker 205 printf \\x1a 206 # Archive terminator 207 printf \\x00 208 } 209 210 progname=$(basename $0) 211 212 if [ $# -eq 0 ] 213 then 214 echo "Usage: $progname filename" 215 echo "$progname: Outputs an uncompressed sparkive to stdout." 216 fi 217 218 makearchive "$@" 219