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

syslinux/iso2exe: speedup crc
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 02 21:46:16 2015 +0100 (2015-01-02)
parents 054f70cb9bec
children aa46cf9cc35a
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 $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/bin $TMP/dev
38 cp -a /dev/?d?* /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 &&
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 ( cd $TMP ; find * | cpio -o -H newc ) | compress $TMP/rootfs.gz
50 SIZE=$(wc -c < $TMP/rootfs.gz)
51 store 24 $SIZE $1
52 OFS=$(( $OFS - $SIZE ))
53 printf "Adding rootfs.gz file at %04X (%d bytes) ...\n" $OFS $SIZE
54 cat $TMP/rootfs.gz | ddq of=$1 bs=1 seek=$OFS conv=notrunc
55 rm -rf $TMP
56 }
58 add_dosexe()
59 {
60 OFS=$((0x7EE0))
61 printf "Adding DOS/EXE at %04X (%d bytes) ...\n" $OFS $((0x8000 - $OFS))
62 $0 --get bootiso.bin 2> /dev/null | \
63 ddq bs=1 skip=$OFS of=$1 seek=$OFS conv=notrunc
64 }
66 add_doscom()
67 {
68 SIZE=$($0 --get boot.com | wc -c)
69 OFS=$(( $OFS - $SIZE ))
70 printf "Adding DOS boot file at %04X (%d bytes) ...\n" $OFS $SIZE
71 $0 --get boot.com | ddq of=$1 bs=1 seek=$OFS conv=notrunc
72 store 66 $(($OFS+0xC0)) $1
73 }
75 add_win32exe()
76 {
77 SIZE=$($0 --get win32.exe 2> /dev/null | tee /tmp/exe$$ | wc -c)
78 printf "Adding WIN32 file at %04X (%d bytes) ...\n" 0 $SIZE
79 ddq if=/tmp/exe$$ of=$1 conv=notrunc
80 printf "Adding bootiso head at %04X...\n" 0
81 $0 --get bootiso.bin 2> /dev/null > /tmp/exe$$
82 ddq if=/tmp/exe$$ of=$1 bs=128 count=1 conv=notrunc
83 store $((0x94)) $((0xE0 - 12*8)) $1
84 store $((0xF4)) $((16 - 12)) $1
85 ddq if=$1 of=/tmp/coff$$ bs=1 skip=$((0x178)) count=$((0x88))
86 ddq if=/tmp/coff$$ of=$1 conv=notrunc bs=1 seek=$((0x178 - 12*8))
87 ddq if=/tmp/exe$$ of=$1 bs=1 count=30 seek=$((0x1A0)) skip=$((0x1A0)) conv=notrunc
88 ddq if=$2 bs=1 skip=$((0x1BE)) seek=$((0x1BE)) count=66 of=$1 conv=notrunc
89 store 69 $(($SIZE/512)) $1 8
90 store 510 $((0xAA55)) $1
91 rm -f /tmp/exe$$ /tmp/coff$$
92 printf "Moving syslinux hybrid boot record at %04X (512 bytes) ...\n" $SIZE
93 ddq if=$2 bs=1 count=512 of=$1 seek=$SIZE conv=notrunc
94 OFS=$(($SIZE+512))
95 }
97 add_fdbootstrap()
98 {
99 SIZE=$($0 --get bootfd.bin 2> /dev/null | wc -c)
100 if [ $SIZE -ne 0 ]; then
101 SIZE=$(( $SIZE - 512 )) # sector 2 is data
102 OFS=$(( $OFS - $SIZE ))
103 printf "Adding floppy bootstrap file at %04X (%d bytes) ...\n" $OFS $SIZE
104 $0 --get bootfd.bin | \
105 ddq of=$1 bs=1 count=512 seek=$OFS conv=notrunc
106 $0 --get bootfd.bin | \
107 ddq of=$1 bs=1 skip=1024 seek=$((512 + $OFS)) conv=notrunc
108 store 28 $(($SIZE/512)) $1 8
109 fi
110 }
111 case "$1" in
112 --build)
113 shift
114 ls -l $@
115 cat >> $0 <<EOM
116 $(tar cf - $@ | compress | uuencode -m -)
117 EOT
118 EOM
119 sed -i '/^case/,/^esac/d' $0
120 exit ;;
121 --get)
122 cat $2
123 exit ;;
124 --array)
125 DATA=/tmp/dataiso$$
126 ddq if=/dev/zero bs=32k count=1 of=$DATA
127 add_win32exe $DATA $2 > /dev/null
128 HSZ=$OFS
129 add_dosexe $DATA > /dev/null
130 add_rootfs $DATA > /dev/null
131 add_doscom $DATA > /dev/null
132 add_fdbootstrap $DATA > /dev/null
133 name=${3:-bootiso}
134 BOOTISOSZ=$((0x8000 - $OFS + $HSZ))
135 cat <<EOT
137 #define $(echo $name | tr '[a-z]' '[A-Z]')SZ $BOOTISOSZ
139 #ifdef WIN32
140 static char $name[] = {
141 $(hexdump -v -n $HSZ -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
142 $(hexdump -v -s $OFS -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
143 EOT
145 for mode in data offset ; do
146 ofs=0
147 while read tag str; do
148 if [ "$mode" == "data" ]; then
149 echo -en "$str\0" | hexdump -v -e '" " 16/1 "0x%02X, "' \
150 -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' | \
151 sed 's/ 0x ,/ /g'
152 else
153 if [ $ofs -eq 0 ]; then
154 cat <<EOT
155 };
156 #else
157 static char *$name;
158 #endif
159 #define ELTORITOOFS 3
160 EOT
161 fi
162 echo "#define $tag $(($BOOTISOSZ+$ofs))"
163 ofs=$(($(echo -en "$str\0" | wc -c)+$ofs))
164 fi
165 done <<EOT
166 READSECTORERR Read sector failure.
167 USAGE Usage: isohybrid.exe file.iso [--forced]
168 OPENERR Can't open r/w the iso file.
169 ELTORITOERR No EL TORITO SPECIFICATION signature.
170 CATALOGERR Invalid boot catalog.
171 HYBRIDERR No isolinux.bin hybrid signature.
172 SUCCESSMSG Now you can create a USB key with your .iso file.\\\\nSimply rename it to a .exe file and run it.
173 FORCEMSG You can add --forced to proceed anyway.
174 EOT
175 done
176 rm -rf $DATA
177 exit ;;
178 --exe)
179 # --exe mvcom.bin x.com y.exe > xy.exe
180 cat $4 $3 > /tmp/exe$$
181 S=$(stat -c %s /tmp/exe$$)
182 store 2 $(($S%512)) /tmp/exe$$
183 store 4 $((($S+511)/512)) /tmp/exe$$
184 store 14 -16 /tmp/exe$$
185 store 16 -2 /tmp/exe$$
186 store 20 256 /tmp/exe$$
187 store 22 -16 /tmp/exe$$
188 ddq if=$2 bs=1 seek=64 of=/tmp/exe$$ conv=notrunc
189 store 65 $(stat -c %s $3) /tmp/exe$$
190 store 68 $((0x100-0x40+$(stat -c %s $4))) /tmp/exe$$
191 cat /tmp/exe$$
192 rm -f /tmp/exe$$
193 exit ;;
194 esac
196 main()
197 {
198 [ $(id -u) -ne 0 ] && exec su -c "$0 $@" < /dev/tty
199 case "${1/--/-}" in
200 -get) shift
201 uudecode | unlzma | tar xOf - $@
202 exit ;;
203 *) cat > /dev/null
204 esac
206 [ ! -s "$1" ] && echo "usage: $0 image.iso [--undo]" 1>&2 && exit 1
207 case "${2/--/-}" in
208 -u*|-r*|-w*)
209 case "$(get 0 $1)" in
210 23117)
211 ddq if=$1 bs=512 count=2 skip=$(get 69 $1 1) of=$1 conv=notrunc
212 ddq if=/dev/zero bs=1k seek=1 count=31 of=$1 conv=notrunc ;;
213 *) ddq if=/dev/zero bs=1k count=32 of=$1 conv=notrunc ;;
214 esac
215 exit 0
216 esac
217 case "$(get 0 $1)" in
218 23117) echo "The file $1 is already an EXE file." 1>&2 && exit 1;;
219 0) $0 --get isohdpfx.bin | ddq bs=512 count=1 of=$1 conv=notrunc
220 esac
222 echo "Read hybrid & tazlito data..."
223 ddq if=$1 bs=512 count=1 of=/tmp/hybrid$$
224 ddq if=$1 bs=512 skip=2 count=20 of=/tmp/tazlito$$
225 add_win32exe $1 /tmp/hybrid$$
226 printf "Moving tazlito data record at %04X (512 bytes) ...\n" $OFS
227 ddq if=/tmp/tazlito$$ bs=1 count=512 of=$1 seek=$OFS conv=notrunc
228 rm -f /tmp/tazlito$$ /tmp/hybrid$$
229 HOLE=$(($OFS+512))
231 # keep the largest room for the tazlito info file
232 add_dosexe $1
233 add_rootfs $1
234 add_doscom $1
235 add_fdbootstrap $1
236 printf "%d free bytes in %04X..%04X\n" $(($OFS-$HOLE)) $HOLE $OFS
237 store 26 ${RANDOM:-0} $1
238 echo -n "Adding checksum..."
239 store 64 $(od -v -j 66 -N 32702 -t u2 -w2 -An $1 | \
240 awk '{ i+= $0 } END { print -(i % 65536) }') $1
241 echo " done."
242 }
244 main $@ <<EOT