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

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