wok view syslinux/stuff/iso2exe/iso2exe.sh @ rev 19670

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