wok annotate syslinux/stuff/iso2exe/iso2exe.sh @ rev 20771

cpige: add man page for cook.slitaz.org
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Feb 13 16:56:42 2019 +0100 (23 months ago)
parents a9c77509261e
children 01b30edcef4e
rev   line source
pascal@13691 1 #!/bin/sh
pascal@13691 2
pascal@13691 3 ddq()
pascal@13691 4 {
pascal@13691 5 dd $@ 2> /dev/null
pascal@13691 6 }
pascal@13691 7
pascal@13691 8 store()
pascal@13691 9 {
pascal@13691 10 n=$2; for i in $(seq 8 8 ${4:-16}); do
pascal@13691 11 printf '\\\\x%02X' $(($n & 255))
pascal@13691 12 n=$(($n >> 8))
pascal@13691 13 done | xargs echo -en | ddq bs=1 conv=notrunc of=$3 seek=$(($1))
pascal@13691 14 }
pascal@13691 15
pascal@13972 16 get()
pascal@13972 17 {
pascal@18824 18 echo $(od -j $(($1)) -N ${3:-2} -t u${3:-2} -An "$2")
pascal@13972 19 }
pascal@13972 20
pascal@17476 21 compress()
pascal@17476 22 {
pascal@17489 23 if [ "$1" ]; then
pascal@17489 24 gzip -9 > $1
pascal@17489 25 [ "$(which advdef 2> /dev/null)" ] &&
pascal@19425 26 advdef -z4 -i100 $1 > /dev/null
pascal@17489 27 elif [ "$(which xz 2> /dev/null)" ]; then
pascal@17476 28 xz -z -e --format=lzma --lzma1=mode=normal --stdout
pascal@17476 29 else
pascal@17476 30 lzma e -si -so
pascal@17476 31 fi 2> /dev/null
pascal@17476 32 }
pascal@17476 33
pascal@14150 34 add_rootfs()
pascal@13691 35 {
pascal@13691 36 TMP=/tmp/iso2exe$$
pascal@19848 37 mkdir -p $TMP
pascal@19848 38 $0 --get rootfs.gz > $TMP/rootfs.gz
pascal@13691 39 SIZE=$(wc -c < $TMP/rootfs.gz)
pascal@13972 40 store 24 $SIZE $1
pascal@14261 41 OFS=$(( $OFS - $SIZE ))
pascal@17454 42 printf "Adding rootfs.gz file at %04X (%d bytes) ...\n" $OFS $SIZE
pascal@13691 43 cat $TMP/rootfs.gz | ddq of=$1 bs=1 seek=$OFS conv=notrunc
pascal@13691 44 rm -rf $TMP
pascal@14150 45 }
pascal@14150 46
pascal@14261 47 add_dosexe()
pascal@14261 48 {
pascal@17492 49 TMP=/tmp/bootiso$$
pascal@20771 50 $0 --get bootiso.bin > $TMP 2> /dev/null
pascal@17492 51 OFS=$(($(get 20 $TMP) - 0xC0))
pascal@19870 52 printf "Adding DOS/EXE stub at %04X (%d bytes) ...\n" $OFS $((0x7FF0 - $OFS))
pascal@17492 53 ddq if=$TMP bs=1 skip=$OFS of=$1 seek=$OFS conv=notrunc
pascal@17492 54 rm -f $TMP
pascal@14261 55 }
pascal@14261 56
pascal@14150 57 add_doscom()
pascal@14150 58 {
pascal@13972 59 SIZE=$($0 --get boot.com | wc -c)
pascal@13691 60 OFS=$(( $OFS - $SIZE ))
pascal@17454 61 printf "Adding DOS boot file at %04X (%d bytes) ...\n" $OFS $SIZE
pascal@13972 62 $0 --get boot.com | ddq of=$1 bs=1 seek=$OFS conv=notrunc
pascal@17810 63 store 64 $(($OFS+0xC0)) $1
pascal@14150 64 }
pascal@14150 65
pascal@18062 66 add_tazlito_info()
pascal@18062 67 {
pascal@18062 68 HOLE=$OFS
pascal@18062 69 [ $(get 0 $2) -eq 35615 ] || return
pascal@19008 70 zcat $2 | compress /tmp/rezipped$$.gz
pascal@18062 71 n=$(stat -c %s /tmp/rezipped$$.gz)
pascal@18062 72 printf "Moving tazlito data record at %04X ($n bytes) ...\n" $OFS
pascal@18062 73 ddq if=/tmp/rezipped$$.gz bs=1 of=$1 seek=$OFS conv=notrunc
pascal@18062 74 HOLE=$(($HOLE+$n))
pascal@18062 75 rm -f /tmp/rezipped$$.gz
pascal@19425 76 if [ -n "$gpt" ]; then
pascal@19425 77 store $((0x25E)) $n $1
pascal@19425 78 store $((0x25C)) $OFS $1
pascal@19425 79 fi
pascal@18062 80 }
pascal@18062 81
pascal@14150 82 add_win32exe()
pascal@14150 83 {
pascal@13974 84 SIZE=$($0 --get win32.exe 2> /dev/null | tee /tmp/exe$$ | wc -c)
pascal@19425 85 [ -n "$gpt" ] && SIZE=$(($SIZE+1024))
pascal@19425 86 [ -n "$mac" ] && SIZE=$(($SIZE+3072))
pascal@17454 87 printf "Adding WIN32 file at %04X (%d bytes) ...\n" 0 $SIZE
pascal@14261 88 ddq if=/tmp/exe$$ of=$1 conv=notrunc
pascal@19425 89 if [ -n "$gpt" ]; then
pascal@19425 90 printf "Adding GPT at %04X (1024 bytes) ...\n" 512
pascal@19425 91 ddq if=$2 bs=512 skip=1 of=$1 seek=1 conv=notrunc
pascal@19425 92 i=3; [ -n "$mac" ] && i=9
pascal@19425 93 ddq if=/tmp/exe$$ bs=512 skip=1 of=$1 seek=$i conv=notrunc
pascal@19425 94 for i in 12C 154 17C ; do # always 3 UPX sections
pascal@19670 95 store $((0x$i)) $((1024 + $(get 0x$i $1))) $1
pascal@19425 96 done
pascal@19425 97 fi
pascal@14261 98 printf "Adding bootiso head at %04X...\n" 0
pascal@14261 99 $0 --get bootiso.bin 2> /dev/null > /tmp/exe$$
pascal@14261 100 ddq if=/tmp/exe$$ of=$1 bs=128 count=1 conv=notrunc
pascal@14266 101 store $((0x94)) $((0xE0 - 12*8)) $1
pascal@14266 102 store $((0xF4)) $((16 - 12)) $1
pascal@14267 103 ddq if=$1 of=/tmp/coff$$ bs=1 skip=$((0x178)) count=$((0x88))
pascal@14267 104 ddq if=/tmp/coff$$ of=$1 conv=notrunc bs=1 seek=$((0x178 - 12*8))
pascal@17810 105 ddq if=/tmp/exe$$ of=$1 bs=1 count=24 seek=$((0x1A0)) skip=$((0x1A0)) conv=notrunc
pascal@17810 106 ddq if=$2 bs=1 skip=$((0x1B8)) seek=$((0x1B8)) count=72 of=$1 conv=notrunc
pascal@14261 107 store 510 $((0xAA55)) $1
pascal@19425 108 i=$SIZE; OFS=$(($SIZE+512))
pascal@19425 109 [ -n "$mac" ] && OFS=$SIZE && i=1536
pascal@19425 110 store 417 $(($i/512)) $1 8
pascal@19425 111 printf "Moving syslinux hybrid boot record at %04X (512 bytes) ...\n" $i
pascal@19425 112 ddq if=$2 bs=1 count=512 of=$1 seek=$i conv=notrunc
pascal@19828 113 if [ $(get $((0x7C00)) /tmp/exe$$) -eq 60905 ]; then
pascal@19827 114 ddq if=/tmp/exe$$ bs=1 count=66 skip=$((0x7DBE)) of=$1 seek=$(($i + 0x1BE)) conv=notrunc
pascal@19827 115 ddq if=$1 bs=1 count=3 skip=$i of=$1 seek=$(($i + 0x1BE)) conv=notrunc
pascal@19827 116 ddq if=/tmp/exe$$ bs=1 count=3 skip=$((0x7C00)) of=$1 seek=$i conv=notrunc
pascal@19827 117 fi
pascal@19827 118 rm -f /tmp/exe$$ /tmp/coff$$
pascal@20332 119 if [ -z "$RECURSIVE_PARTITION" -a $(get 454 $1 4) -eq 0 ]; then
pascal@20332 120 store 448 $((1+$i/512)) $1 8
pascal@20332 121 store 454 $(($i/512)) $1 8
pascal@20332 122 store 458 $(($(get 458 $1 4) - $i/512)) $1 32
pascal@19670 123 fi
pascal@14257 124 }
pascal@14257 125
pascal@14257 126 add_fdbootstrap()
pascal@14257 127 {
pascal@14268 128 SIZE=$($0 --get bootfd.bin 2> /dev/null | wc -c)
pascal@14257 129 if [ $SIZE -ne 0 ]; then
pascal@17454 130 SIZE=$(( $SIZE - 512 )) # sector 2 is data
pascal@17454 131 OFS=$(( $OFS - $SIZE ))
pascal@17454 132 printf "Adding floppy bootstrap file at %04X (%d bytes) ...\n" $OFS $SIZE
pascal@14268 133 $0 --get bootfd.bin | \
pascal@14268 134 ddq of=$1 bs=1 count=512 seek=$OFS conv=notrunc
pascal@14268 135 $0 --get bootfd.bin | \
pascal@14268 136 ddq of=$1 bs=1 skip=1024 seek=$((512 + $OFS)) conv=notrunc
pascal@17810 137 store 26 $(($SIZE/512)) $1 8
pascal@14257 138 fi
pascal@14150 139 }
pascal@18670 140
pascal@18824 141 gzsize()
pascal@18824 142 {
pascal@18824 143 echo $(($(hexdump -C | awk ' {
pascal@19649 144 for (i = 17; i > 1; i--) if ($i != "00") break;
pascal@19649 145 if (i == 1) {
pascal@19649 146 print "0x" $1 " + 1 + 1 - " n
pascal@18824 147 exit
pascal@18824 148 }
pascal@19649 149 n = 17 - i
pascal@18824 150 }')))
pascal@18824 151 }
pascal@18824 152
pascal@18804 153 fileofs()
pascal@18804 154 {
pascal@19425 155 [ $(get 1024 "$ISO") -eq 35615 ] && x=1024 ||
pascal@19425 156 x=$((512*(1+$(get 417 "$ISO" 1))))
pascal@20725 157 [ $x -gt 32768 ] && x=6656
pascal@18824 158 stub=$(($(get 20 "$ISO") - 0xC0))
pascal@18848 159 c=$(custom_config_sector "$ISO")
pascal@18804 160 SIZE=0; OFFSET=0
pascal@18804 161 case "$1" in
pascal@19425 162 win32.exe) [ $x -eq 2048 ] && x=10752
pascal@19425 163 [ $x -eq 1024 ] || SIZE=$(($x - 512));;
pascal@19425 164 syslinux.mbr) [ $x -eq 1024 ] || OFFSET=$(($x - 512)); SIZE=512;;
pascal@19425 165 flavor.info) [ $(get 22528 "$ISO") -eq 35615 ] && OFFSET=22528
pascal@19425 166 [ $x -eq 2048 ] && x=$(get 0x25C "$ISO") &&
pascal@19425 167 SIZE=$(get 0x25E "$ISO")
pascal@19425 168 [ $(get $x "$ISO") -eq 35615 ] && OFFSET=$x
pascal@19425 169 [ $OFFSET -ne 0 ] && [ $SIZE -eq 0 ] &&
pascal@19425 170 SIZE=$(ddq bs=512 skip=$(($OFFSET/512)) if="$ISO" | gzsize);;
pascal@18824 171 floppy.boot) SIZE=$(($(get 26 "$ISO" 1)*512))
pascal@18824 172 OFFSET=$(($(get 64 "$ISO") - 0xC0 - $SIZE));;
pascal@18824 173 rootfs.gz) SIZE=$(get 24 "$ISO"); OFFSET=$(($stub - $SIZE));;
pascal@18824 174 tazboot.com) OFFSET=$(($(get 64 "$ISO") - 0xC0))
pascal@18824 175 SIZE=$(($stub - $(get 24 "$ISO") - $OFFSET));;
pascal@19870 176 dosstub) OFFSET=$stub; SIZE=$((0x7FF0 - $OFFSET));;
pascal@20724 177 boot.md5) [ $(get 0 "$ISO") -eq 23117 ] &&
pascal@20724 178 [ $(get 18 "$ISO") -ne 0 ] &&
pascal@20724 179 OFFSET=$((0x7FF0)) && SIZE=16;;
pascal@18848 180 fs.iso) OFFSET=$((0x8000))
pascal@18848 181 SIZE=$((2048*$c - $OFFSET));;
pascal@18848 182 custom.magic) ddq bs=2k skip=$c if="$ISO" | ddq bs=1 count=6 | \
pascal@18848 183 grep -q '#!boot' && OFFSET=$((2048*$c)) &&
pascal@18848 184 SIZE=39 ;;
pascal@18848 185 custom.append) OFFSET=$((2048*$c+47)) &&
pascal@18848 186 SIZE=$(ddq bs=2k skip=$c if="$ISO" count=1 | \
pascal@18848 187 sed '/^append=/!d;s/^[^=]*=.//' | wc -c);;
pascal@19450 188 custom.initrd) x=$(ddq bs=2k skip=$c if="$ISO" count=1 | \
pascal@18848 189 sed '/^append=\|^initrd:/!d' | wc -c)
pascal@19425 190 OFFSET=$((2048*$c+$x+40))
pascal@18848 191 SIZE=$(($(ddq bs=2k skip=$c if="$ISO" count=1 | \
pascal@18848 192 sed '/^initrd:/!d;s/.*://') + 0));;
pascal@18804 193 esac
pascal@18804 194 }
pascal@18804 195
pascal@19848 196 trailer()
pascal@19848 197 {
pascal@19848 198 OFFSET=$(stat -c %s "$1")
pascal@19848 199 [ $OFFSET -gt $HEAP ] &&
pascal@19848 200 printf "%d free bytes in %04X..%04X\n" $(($OFFSET - $HEAP)) $HEAP $OFFSET
pascal@19848 201 if [ $(get 510 "$1") -eq 43605 ]; then
pascal@19848 202 echo "MBR partitions :"
pascal@19848 203 for i in 0 1 2 3; do
pascal@19848 204 SIZE=$(get $((446+12+16*i)) "$1" 4)
pascal@19848 205 [ $SIZE -eq 0 ] && continue
pascal@19848 206 OFFSET=$(get $((446+8+16*i)) "$1" 4)
pascal@19848 207 printf " $i:%08X %08X %02X\n" $OFFSET $SIZE \
pascal@19848 208 $(get $((446+4+16*i)) "$1" 1)
pascal@19848 209 done
pascal@20332 210 if [ $(get 450 "$1") -eq 65262 ]; then
pascal@19848 211 echo "EFI partitions :"
pascal@20332 212 n=$(get 592 "$1")
pascal@19848 213 s=$(get 596 "$1")
pascal@20332 214 o=$(($(get 584 "$1")*512))
pascal@19848 215 i=0
pascal@20332 216 while [ $i -lt $n ]; do
pascal@19848 217 f=$(get $(($o+0x20)) "$1" 4)
pascal@19848 218 l=$(($(get $(($o+0x28)) "$1" 4)-$f))
pascal@19848 219 [ $l -eq 0 ] && break
pascal@19848 220 printf " $i:%08X %08X %s\n" $f $(($l+1)) \
pascal@20332 221 "$(od -An -N 72 -w72 -j $(($o+0x38)) -t a "$1" \
pascal@20332 222 | sed 's/ nul//g;s/ //g;s/ sp//g')"
pascal@19848 223 o=$(($o+$s))
pascal@19848 224 i=$(($i+1))
pascal@19848 225 done
pascal@19848 226 fi
pascal@19848 227 fi
pascal@19848 228 o=2048
pascal@19848 229 if [ $(get $o "$1") -eq 19792 ]; then
pascal@19848 230 echo "Apple partitions :"
pascal@19848 231 i=0
pascal@19848 232 while [ $(get $o "$1") -eq 19792 ]; do
pascal@19848 233 f=$((0x$(od -An -N 4 -j $(($o+8)) -t x1 "$1" | sed 's/ //g')))
pascal@19848 234 l=$((0x$(od -An -N 4 -j $(($o+0x54)) -t x1 "$1" | sed 's/ //g')))
pascal@19848 235 printf " $i:%08X %08X %s\n" $f $l \
pascal@19848 236 "$(ddq bs=1 skip=$(($o+16)) count=32 if="$1")"
pascal@19848 237 o=$(($o+2048))
pascal@19848 238 i=$(($i+1))
pascal@19848 239 done
pascal@19848 240 fi
pascal@19848 241 }
pascal@19848 242
pascal@18804 243 list()
pascal@18804 244 {
pascal@18824 245 HEAP=0
pascal@18848 246 for f in win32.exe syslinux.mbr flavor.info floppy.boot tazboot.com \
pascal@18848 247 rootfs.gz dosstub boot.md5 fs.iso custom.magic custom.append \
pascal@18848 248 custom.initrd; do
pascal@18804 249 fileofs $f
pascal@18848 250 [ $SIZE -le 0 ] && continue
pascal@18848 251 [ "${OFFSET:8}" ] && continue
pascal@18820 252 [ $OFFSET -lt 0 ] && continue
pascal@18824 253 [ $(get $OFFSET "$ISO") -eq 0 ] && continue
pascal@18848 254 [ $OFFSET -gt $HEAP ] && [ $(($OFFSET - $HEAP)) -gt 16 ] &&
pascal@18848 255 printf "%d free bytes in %04X..%04X\n" $(($OFFSET - $HEAP)) $HEAP $OFFSET
pascal@18848 256 [ $OFFSET -ge $HEAP ] && HEAP=$(($OFFSET+$SIZE))
pascal@18824 257 printf "$f at %04X ($SIZE bytes).\n" $OFFSET
pascal@18804 258 done
pascal@19848 259 trailer $ISO
pascal@18804 260 }
pascal@18804 261
pascal@19838 262 restore_hybrid_mbr()
pascal@19838 263 {
pascal@19838 264 if [ $(get 0 "$1") -eq 60905 ]; then
pascal@19838 265 ddq bs=1 conv=notrunc if="$1" of="$1" skip=$((0x1BE)) seek=0 count=3
pascal@19838 266 ddq bs=1 skip=$((0x1BE)) count=66 if="$2" | \
pascal@19838 267 ddq bs=1 seek=$((0x1BE)) count=66 of="$1" conv=notrunc
pascal@19857 268 if [ -n "$RECURSIVE_PARTITION" ]; then
pascal@19857 269 for i in 0 1 2 3 ; do
pascal@19857 270 n=$(get $((0x1C6+16*i)) $1 4)
pascal@19857 271 [ $n -eq 0 -o $n -gt 64 ] && continue
pascal@19857 272 store $((0x1C0+16*i)) 1 $1 8
pascal@19857 273 store $((0x1C6+16*i)) 0 $1 32
pascal@19857 274 store $((0x1CA+16*i)) $(($(get $((0x1CA+16*i)) $1 4)+$n)) $1 32
pascal@19857 275 done
pascal@19857 276 fi
pascal@19838 277 fi
pascal@19838 278 }
pascal@19838 279
pascal@18804 280 extract()
pascal@18804 281 {
pascal@18804 282 for f in $@; do
pascal@18804 283 fileofs $f
pascal@18824 284 [ $SIZE -eq 0 ] ||
pascal@18824 285 ddq bs=1 count=$SIZE skip=$OFFSET if="$ISO" >$f
pascal@19838 286 [ "$f" == "syslinux.mbr" ] && restore_hybrid_mbr "$f" "$ISO"
pascal@18804 287 done
pascal@18804 288 }
pascal@18804 289
pascal@18670 290 custom_config_sector()
pascal@18670 291 {
pascal@19324 292 get 32848 "$1" 4
pascal@18670 293 }
pascal@18670 294
pascal@18773 295 clear_custom_config()
pascal@18773 296 {
pascal@19425 297 start=$(custom_config_sector $1)
pascal@19425 298 cnt=$((512 - ($start % 512)))
pascal@19425 299 [ $cnt -ne 512 ] &&
pascal@19425 300 ddq if=/dev/zero of=$1 bs=2k seek=$start count=$cnt
pascal@18773 301 }
pascal@14150 302 case "$1" in
pascal@14150 303 --build)
pascal@14150 304 shift
pascal@19848 305 TMP=/tmp/iso2exe$$
pascal@19848 306 mkdir -p $TMP/dev
pascal@19848 307 cp -a /dev/tty /dev/tty0 $TMP/dev
pascal@19848 308 cat init > $TMP/init.exe
pascal@19848 309 find $TMP -type f -print0 | xargs -0 chmod +x
pascal@20771 310 find $TMP -print0 | xargs -0 touch -t 197001010100.00
pascal@19861 311 ( cd $TMP; find dev init.exe | cpio -o -H newc ) | compress rootfs.gz
pascal@19848 312 rm -rf $TMP
pascal@19848 313 ls -l $@ rootfs.gz
pascal@14150 314 cat >> $0 <<EOM
pascal@19848 315 $(tar cf - ${@/init/rootfs.gz} | compress | uuencode -m -)
pascal@14150 316 EOT
pascal@14150 317 EOM
pascal@14150 318 sed -i '/^case/,/^esac/d' $0
pascal@14150 319 exit ;;
pascal@14150 320 --get)
pascal@14150 321 cat $2
pascal@14150 322 exit ;;
pascal@14150 323 --array)
pascal@14150 324 DATA=/tmp/dataiso$$
pascal@14150 325 ddq if=/dev/zero bs=32k count=1 of=$DATA
pascal@14261 326 add_win32exe $DATA $2 > /dev/null
pascal@14261 327 HSZ=$OFS
pascal@14261 328 add_dosexe $DATA > /dev/null
pascal@14150 329 add_rootfs $DATA > /dev/null
pascal@14150 330 add_doscom $DATA > /dev/null
pascal@14257 331 add_fdbootstrap $DATA > /dev/null
pascal@14257 332 name=${3:-bootiso}
pascal@17451 333 BOOTISOSZ=$((0x8000 - $OFS + $HSZ))
pascal@14150 334 cat <<EOT
pascal@14150 335
pascal@17451 336 #define $(echo $name | tr '[a-z]' '[A-Z]')SZ $BOOTISOSZ
pascal@14150 337
pascal@18775 338 #ifndef __MSDOS__
pascal@14257 339 static char $name[] = {
pascal@17591 340 /* head */
pascal@18909 341 $(hexdump -v -n $HSZ -e '" " 16/1 "0x%02X, "' -e '" /* %04.4_ax */ \n"' $DATA | sed 's/ 0x ,/ /g')
pascal@17591 342 /* tail */
pascal@18909 343 $(hexdump -v -s $OFS -e '" " 16/1 "0x%02X, "' -e '" /* %04.4_ax */ \n"' $DATA | sed 's/ 0x ,/ /g')
pascal@17591 344
pascal@17591 345 /* These strange constants are defined in RFC 1321 as
pascal@17591 346 T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
pascal@17591 347 */
pascal@17591 348 /* static const uint32_t C_array[64] */
pascal@17591 349 EOT
pascal@17591 350 while read a b c d; do
pascal@17591 351 for i in $a $b $c $d; do
pascal@17591 352 echo $i | sed 's/0x\(..\)\(..\)\(..\)\(..\),/0x\4, 0x\3, 0x\2, 0x\1, /'
pascal@17591 353 done
pascal@17591 354 done <<EOT
pascal@17591 355 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
pascal@17591 356 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
pascal@17591 357 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
pascal@17591 358 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
pascal@17591 359 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
pascal@17591 360 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
pascal@17591 361 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
pascal@17591 362 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
pascal@17591 363 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
pascal@17591 364 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
pascal@17591 365 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
pascal@17591 366 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
pascal@17591 367 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
pascal@17591 368 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
pascal@17591 369 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
pascal@17591 370 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
pascal@17591 371 EOT
pascal@17591 372 cat <<EOT
pascal@17591 373 /* static const char P_array[64] */
pascal@17591 374 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
pascal@17591 375 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
pascal@17591 376 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
pascal@17591 377 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, /* 4 */
pascal@17591 378 /* static const char S_array[16] */
pascal@17591 379 7, 12, 17, 22,
pascal@17591 380 5, 9, 14, 20,
pascal@17591 381 4, 11, 16, 23,
pascal@17591 382 6, 10, 15, 21,
pascal@17451 383 EOT
pascal@17451 384
pascal@17451 385 for mode in data offset ; do
pascal@17451 386 ofs=0
pascal@17451 387 while read tag str; do
pascal@17451 388 if [ "$mode" == "data" ]; then
pascal@17451 389 echo -en "$str\0" | hexdump -v -e '" " 16/1 "0x%02X, "' \
pascal@18909 390 -e '" /* %04.4_ax */ \n"' | \
pascal@17451 391 sed 's/ 0x ,/ /g'
pascal@17451 392 else
pascal@17451 393 if [ $ofs -eq 0 ]; then
pascal@17451 394 cat <<EOT
pascal@14150 395 };
pascal@14150 396 #endif
pascal@17591 397
pascal@17591 398 #define C_array (uint32_t *) ($name + $(($BOOTISOSZ)))
pascal@17591 399 #define P_array (char *) ($name + $(($BOOTISOSZ+(64*4))))
pascal@17591 400 #define S_array (char *) ($name + $(($BOOTISOSZ+(64*4)+64)))
pascal@17451 401 #define ELTORITOOFS 3
pascal@14150 402 EOT
pascal@17451 403 fi
pascal@17591 404 echo "#define $tag $(($BOOTISOSZ+(64*4)+64+16+$ofs))"
pascal@17451 405 ofs=$(($(echo -en "$str\0" | wc -c)+$ofs))
pascal@17451 406 fi
pascal@17451 407 done <<EOT
pascal@17451 408 READSECTORERR Read sector failure.
pascal@18897 409 USAGE Usage: isohybrid.exe [--list|--read] [--append cmdline] [--initrd file] file.iso [--forced|--undo|--quick|filename...]
pascal@18909 410 OPENERR Can't open the iso file.
pascal@17451 411 ELTORITOERR No EL TORITO SPECIFICATION signature.
pascal@17451 412 CATALOGERR Invalid boot catalog.
pascal@17451 413 HYBRIDERR No isolinux.bin hybrid signature.
paul@17671 414 SUCCESSMSG Now you can create a USB key with your .iso file.\\\\nSimply rename it to an .exe file and run it.
pascal@17451 415 FORCEMSG You can add --forced to proceed anyway.
pascal@17591 416 MD5MSG Computing md5sum...
pascal@17591 417 UNINSTALLMSG Uninstall done.
pascal@18775 418 OPENINITRDERR Can't open the initrd file.
pascal@18780 419 ALREADYEXEERR Already an EXE file.
pascal@18897 420 WIN32_EXE win32.exe
pascal@18897 421 SYSLINUX_MBR syslinux.mbr
pascal@18897 422 FLAVOR_INFO flavor.info
pascal@18897 423 FLOPPY_BOOT floppy.boot
pascal@18897 424 TAZBOOT_COM tazboot.com
pascal@18897 425 ROOTFS_GZ rootfs.gz
pascal@18897 426 DOSSTUB dosstub
pascal@18897 427 BOOT_MD5 boot.md5
pascal@18897 428 FS_ISO fs.iso
pascal@18897 429 CUSTOM_MAGIC custom.magic
pascal@18897 430 CUSTOM_APPEND custom.append
pascal@18897 431 CUSTOM_INITRD custom.initrd
pascal@18909 432 CUSTOM_HEADER #!boot 00000000000000000000000000000000\\\\n
pascal@18909 433 FREE_FORMAT %ld free bytes in %04lX..%04lX\\\\n
pascal@18909 434 USED_FORMAT %s at %04lX (%ld bytes).\\\\n
pascal@18909 435 CMDLINE_TAG append=
pascal@18909 436 INITRD_TAG initrd:
pascal@17451 437 EOT
pascal@17451 438 done
pascal@18909 439 cat <<EOT
pascal@18909 440 #ifdef __MSDOS__
pascal@18909 441 #define BOOTISOFULLSIZE $(($BOOTISOSZ+(64*4)+64+16+$ofs))
pascal@18909 442 static char bootiso[BOOTISOFULLSIZE];
pascal@18909 443 static data_fixup(void)
pascal@18909 444 {
pascal@18909 445 #asm
pascal@18909 446 push ds
pascal@18909 447 push ds
pascal@18909 448 pop es
pascal@18909 449 mov ax,ds
pascal@18909 450 sub ax,#0x1000
pascal@18909 451 mov ds,ax
pascal@18909 452 xor si,si
pascal@18909 453 scanlp:
pascal@18909 454 dec si
pascal@18909 455 jz copydone
pascal@18909 456 cmp byte ptr [si+2],#0xEB
pascal@18909 457 jne scanlp
pascal@18909 458 cmp word ptr [si],#0x5A4D
pascal@18909 459 jne scanlp
pascal@18909 460 mov cx,#BOOTISOFULLSIZE
pascal@18909 461 mov di,#_bootiso
pascal@18909 462 cld
pascal@18909 463 rep
pascal@18909 464 movsb
pascal@18909 465 copydone:
pascal@18909 466 pop ds
pascal@18909 467 #endasm
pascal@18909 468 if (!bootiso[0]) {
pascal@18909 469 puts("No bootiso data");
pascal@18909 470 exit(-1);
pascal@18909 471 }
pascal@18909 472 }
pascal@18909 473 #else
pascal@18909 474 #define data_fixup()
pascal@18909 475 #endif
pascal@18909 476 EOT
pascal@14150 477 rm -rf $DATA
pascal@14150 478 exit ;;
pascal@14150 479 --exe)
pascal@14150 480 # --exe mvcom.bin x.com y.exe > xy.exe
pascal@14150 481 cat $4 $3 > /tmp/exe$$
pascal@14150 482 S=$(stat -c %s /tmp/exe$$)
pascal@14150 483 store 2 $(($S%512)) /tmp/exe$$
pascal@14150 484 store 4 $((($S+511)/512)) /tmp/exe$$
pascal@14150 485 store 14 -16 /tmp/exe$$
pascal@14150 486 store 16 -2 /tmp/exe$$
pascal@14150 487 store 20 256 /tmp/exe$$
pascal@14150 488 store 22 -16 /tmp/exe$$
pascal@14150 489 ddq if=$2 bs=1 seek=64 of=/tmp/exe$$ conv=notrunc
pascal@14150 490 store 65 $(stat -c %s $3) /tmp/exe$$
pascal@14150 491 store 68 $((0x100-0x40+$(stat -c %s $4))) /tmp/exe$$
pascal@14150 492 cat /tmp/exe$$
pascal@14150 493 rm -f /tmp/exe$$
pascal@14150 494 exit ;;
pascal@14150 495 esac
pascal@14150 496
pascal@14150 497 main()
pascal@14150 498 {
pascal@18802 499 [ $(id -u) -ne 0 ] && cmd="$0 $@" && exec su -c "$cmd" < /dev/tty
pascal@18670 500 append=
pascal@18670 501 initrd=
pascal@19425 502
pascal@18670 503 while [ "$1" ]; do
pascal@18670 504 case "${1/--/-}" in
pascal@18670 505 -get) shift
pascal@18670 506 uudecode | unlzma | tar xOf - $@
pascal@18670 507 exit ;;
pascal@18670 508 -a*) append="$2" ; shift 2 ;;
pascal@18670 509 -i*) initrd="$2" ; shift 2 ;;
pascal@18848 510 -r*|-l*)
pascal@18848 511 ISO="$2" ; shift 2
pascal@18804 512 [ -z "$1" ] && list || extract $@
pascal@18804 513 exit ;;
pascal@18670 514 *) cat > /dev/null
pascal@18670 515 break
pascal@18670 516 esac
pascal@18670 517 done
pascal@14150 518
pascal@18670 519 [ ! -s "$1" ] && cat 1>&2 <<EOT && exit 1
pascal@18848 520 usage: $0 [--list|--read] [--append custom_cmdline ] [ --initrd custom_initrd ] image.iso [--force|--undo|"DOS help message"|filename...]
pascal@18670 521 EOT
pascal@17451 522 case "${2/--/-}" in
pascal@18820 523 -u*|-r*|-w*|-f*)
pascal@17451 524 case "$(get 0 $1)" in
pascal@17451 525 23117)
pascal@18063 526 b=$(get 417 $1 1)
pascal@18063 527 n=$(($(get 64 $1) + 0xC0 - ($(get 26 $1 1)*512) - ($b+1)*512))
pascal@19838 528 ddq if=$1 bs=512 count=1 skip=$b of=/tmp/hymbr$$
pascal@19838 529 restore_hybrid_mbr /tmp/hymbr$$ $1
pascal@19838 530 ddq if=/tmp/hymbr$$ of=$1 conv=notrunc
pascal@19838 531 rm -f /tmp/hymbr$$
pascal@19425 532 if [ $(get 512 $1) -eq 17989 ]; then
pascal@19425 533 n=$(($(get 0x25C $1)/512))
pascal@19425 534 ddq if=$1 bs=512 seek=44 count=20 skip=$n of=$1 conv=notrunc
pascal@19425 535 ddq if=/dev/zero bs=512 seek=9 count=35 of=$1 conv=notrunc
pascal@19425 536 ddq if=/dev/zero bs=512 seek=3 count=1 of=$1 conv=notrunc
pascal@19425 537 else
pascal@19425 538 ddq if=/dev/zero bs=512 seek=1 count=1 of=$1 conv=notrunc
pascal@19425 539 ddq if=$1 bs=512 seek=2 count=30 skip=$(($b+1)) of=$1 conv=notrunc
pascal@19425 540 ddq if=/dev/zero bs=1 seek=$n count=$((0x8000 - $n)) of=$1 conv=notrunc
pascal@19425 541 fi ;;
pascal@17451 542 *) ddq if=/dev/zero bs=1k count=32 of=$1 conv=notrunc ;;
pascal@17451 543 esac
pascal@18820 544 case "${2/--/-}" in
pascal@18820 545 -f*)
pascal@19077 546 [ "$append$initrd" ] && clear_custom_config $1
pascal@19077 547 set -- "$1" "$3" ;;
pascal@18820 548 *)
pascal@18820 549 clear_custom_config $1
pascal@18820 550 exit 0 ;;
pascal@18820 551 esac
pascal@17451 552 esac
pascal@14150 553 case "$(get 0 $1)" in
pascal@14150 554 23117) echo "The file $1 is already an EXE file." 1>&2 && exit 1;;
pascal@19827 555 0) [ -x /usr/bin/isohybrid ] && isohybrid -entry 2 $1;;
pascal@14150 556 esac
pascal@19425 557
pascal@20332 558 gpt= ; [ $(get 450 $1) -eq 65262 ] && gpt=1
pascal@19425 559 mac= ; [ $(get 2048 $1) -eq 19792 ] && mac=1
pascal@14261 560 echo "Read hybrid & tazlito data..."
pascal@19430 561 if [ -n "$gpt" ]; then
pascal@19425 562 echo "GUID Partition Table..."
pascal@19430 563 n=3; [ -n "$mac" ] && n=9 && echo "Apple Partition Table..."
pascal@19425 564 ddq if=$1 bs=512 count=$n of=/tmp/hybrid$$
pascal@19425 565 ddq if=$1 bs=512 skip=44 count=20 of=/tmp/tazlito$$
pascal@19425 566 else
pascal@19425 567 ddq if=$1 bs=512 count=1 of=/tmp/hybrid$$
pascal@19425 568 ddq if=$1 bs=512 skip=2 count=20 of=/tmp/tazlito$$
pascal@19425 569 fi
pascal@14261 570 add_win32exe $1 /tmp/hybrid$$
pascal@18062 571 add_tazlito_info $1 /tmp/tazlito$$
pascal@14261 572 rm -f /tmp/tazlito$$ /tmp/hybrid$$
pascal@14150 573
pascal@14150 574 # keep the largest room for the tazlito info file
pascal@14261 575 add_dosexe $1
pascal@14150 576 add_rootfs $1
pascal@14150 577 add_doscom $1
pascal@14257 578 add_fdbootstrap $1
pascal@17454 579 printf "%d free bytes in %04X..%04X\n" $(($OFS-$HOLE)) $HOLE $OFS
pascal@17810 580 store 440 $(date +%s) $1 32
pascal@17562 581 [ "$2" ] && echo "$2 " | \
pascal@17562 582 ddq bs=1 seek=$((0x7FDE)) count=15 conv=notrunc of=$1
pascal@17562 583 if [ $(stat -c %s $1) -gt 34816 ]; then
pascal@17562 584 echo "Adding ISO image md5 at 7FF0 (16 bytes) ..."
pascal@19323 585 echo -en "$(ddq if=$1 bs=2k skip=16 count=$(($(get 32848 "$1" 4)-16)) | \
pascal@17562 586 md5sum | cut -c-32 | sed 's/\(..\)/\\x\1/g')" | \
pascal@17562 587 ddq bs=16 seek=2047 conv=notrunc of=$1
pascal@17496 588 fi
pascal@19848 589 HEAP=$(($(custom_config_sector $1)*2048))
pascal@18670 590 if [ "$append$initrd" ]; then
pascal@18670 591 echo -n "Adding custom config... "
pascal@18676 592 DATA=/tmp/$(basename $0)$$
pascal@18670 593 rm -f $DATA > /dev/null
pascal@18672 594 isosz=$(stat -c %s $1)
pascal@18670 595 [ "$append" ] && echo "append=$append" >> $DATA
pascal@18670 596 [ -s "$initrd" ] && echo "initrd:$(stat -c %s $initrd)" >> $DATA &&
pascal@18670 597 cat $initrd >> $DATA
pascal@18670 598 echo "#!boot $(md5sum $DATA | sed 's/ .*//')" | cat - $DATA | \
pascal@19848 599 ddq bs=2k seek=$(custom_config_sector $1) of=$1
pascal@19027 600 newsz=$(stat -c %s $1)
pascal@19848 601 mb=$(((($newsz -1)/1048576)+1))
pascal@19848 602 HEAP=$(($mb*1048576))
pascal@19848 603 ddq bs=1048576 seek=$mb count=0 of=$1
pascal@19848 604 h=$(get 417 "$1" 1)
pascal@19848 605 [ -z "$RECURSIVE_PARTITION" ] || h=0
pascal@19258 606 for i in 0 1 2 3 ; do
pascal@19841 607 [ $(get $((0x1BE+16*i)) $1 2) == $((0x0080)) ] || continue
pascal@19670 608 store $((0x1CA+16*i)) $(($mb*2048-$h)) $1 32
pascal@19849 609 store $((0x1C5+16*i)) $(($mb-1)) $1 8
pascal@19027 610 done
pascal@19027 611 if [ $newsz -gt $isosz ]; then
pascal@19027 612 echo "$(($newsz - $isosz)) extra bytes."
pascal@18670 613 else
pascal@20771 614 echo "$(($isosz - 2048*$(get 32848 $1 4)
pascal@18670 615 - $(stat -c %s $DATA) - 24)) bytes free."
pascal@18670 616 fi
pascal@18670 617 rm -f $DATA > /dev/null
pascal@18670 618 fi
pascal@19027 619 echo -n "Adding boot checksum..."
pascal@19027 620 if [ $(stat -c %s $1) -gt 32768 ]; then
pascal@19027 621 n=$(($(get 2 $1) - 1 + ($(get 4 $1) - 1)*512))
pascal@19027 622 n=$(($(od -v -N $n -t u2 -w2 -An $1 | \
pascal@19027 623 awk '{ i+= $0 } END { print (i % 65536) }') \
pascal@19027 624 + $(get $(($n+1)) $1 1)))
pascal@19027 625 store 18 $(( (-$n -1) % 65536 )) $1
pascal@19027 626 fi
pascal@19027 627 echo " done."
pascal@19848 628 trailer $1
pascal@13691 629 }
pascal@13691 630
pascal@18672 631 main "$@" <<EOT