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

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