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

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