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

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