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

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