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

syslinux: add isohybrid.exe
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Mar 04 09:52:12 2013 +0100 (2013-03-04)
parents e487d594117f
children 65b3fd0022ed
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 find $TMP -type f | xargs chmod +x
30 ( cd $TMP ; find * | cpio -o -H newc ) | \
31 lzma e $TMP/rootfs.gz -si 2> /dev/null
32 SIZE=$(wc -c < $TMP/rootfs.gz)
33 store 24 $SIZE $1
34 OFS=$(( 0x8000 - $SIZE ))
35 printf "Adding rootfs.gz file at %04X...\n" $OFS
36 cat $TMP/rootfs.gz | ddq of=$1 bs=1 seek=$OFS conv=notrunc
37 rm -rf $TMP
38 }
40 add_doscom()
41 {
42 SIZE=$($0 --get boot.com | wc -c)
43 OFS=$(( $OFS - $SIZE ))
44 printf "Adding DOS boot file at %04X...\n" $OFS
45 $0 --get boot.com | ddq of=$1 bs=1 seek=$OFS conv=notrunc
46 store 66 $(($OFS+0xC0)) $1
47 }
49 add_win32exe()
50 {
51 SIZE=$($0 --get win32.exe 2> /dev/null | tee /tmp/exe$$ | wc -c)
52 if [ $SIZE -ne 0 ]; then
53 OFS=$(( 128 + ( ($OFS - $SIZE + 128) & 0xFE00 ) ))
54 printf "Adding WIN32 file at %04X...\n" $OFS
55 LOC=$((0xAC+$(get 0x94 /tmp/exe$$)))
56 for i in $(seq 1 $(get 0x86 /tmp/exe$$)); do
57 CUR=$(get $LOC /tmp/exe$$)
58 [ $CUR -eq 0 ] || store $LOC $(($CUR+$OFS-128)) /tmp/exe$$
59 LOC=$(($LOC+40))
60 done
61 ddq if=/tmp/exe$$ of=$1 bs=1 skip=128 seek=$OFS conv=notrunc
62 fi
63 rm -f /tmp/exe$$
64 store 60 $OFS $1
65 }
66 case "$1" in
67 --build)
68 shift
69 [ $(tar cf - $@ | wc -c) -gt $((32 * 1024)) ] &&
70 echo "The file set $@ is too large (31K max) :" &&
71 ls -l $@ && exit 1
72 cat >> $0 <<EOM
73 $(tar cf - $@ | lzma e -si -so | uuencode -m -)
74 EOT
75 EOM
76 sed -i '/^case/,/^esac/d' $0
77 exit ;;
78 --get)
79 cat $2
80 exit ;;
81 --array)
82 DATA=/tmp/dataiso$$
83 ddq if=/dev/zero bs=32k count=1 of=$DATA
84 ddq if=bootiso.bin of=$DATA conv=notrunc
85 ddq if=$2 of=$DATA bs=512 seek=1 conv=notrunc
86 add_rootfs $DATA > /dev/null
87 add_doscom $DATA > /dev/null
88 add_win32exe $DATA > /dev/null
89 cat <<EOT
91 #define BOOTISOSZ $((0x8400 - $OFS))
93 #ifdef WIN32
94 static char bootiso[] = {
95 $(hexdump -v -n 1024 -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
96 $(hexdump -v -s $OFS -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
97 };
98 #endif
99 EOT
100 rm -rf $DATA
101 exit ;;
102 --exe)
103 # --exe mvcom.bin x.com y.exe > xy.exe
104 cat $4 $3 > /tmp/exe$$
105 S=$(stat -c %s /tmp/exe$$)
106 store 2 $(($S%512)) /tmp/exe$$
107 store 4 $((($S+511)/512)) /tmp/exe$$
108 store 14 -16 /tmp/exe$$
109 store 16 -2 /tmp/exe$$
110 store 20 256 /tmp/exe$$
111 store 22 -16 /tmp/exe$$
112 ddq if=$2 bs=1 seek=64 of=/tmp/exe$$ conv=notrunc
113 store 65 $(stat -c %s $3) /tmp/exe$$
114 store 68 $((0x100-0x40+$(stat -c %s $4))) /tmp/exe$$
115 cat /tmp/exe$$
116 rm -f /tmp/exe$$
117 exit ;;
118 esac
120 main()
121 {
122 case "$1" in
123 --get) shift
124 uudecode | unlzma | tar xOf - $@
125 exit ;;
126 *) cat > /dev/null
127 esac
129 [ ! -s "$1" ] && echo "usage: $0 image.iso" 1>&2 && exit 1
130 case "$(get 0 $1)" in
131 23117) echo "The file $1 is already an EXE file." 1>&2 && exit 1;;
132 0) [ -x /usr/bin/isohybrid ] && isohybrid $1
133 esac
135 echo "Moving syslinux hybrid boot record..."
136 ddq if=$1 bs=512 count=1 | ddq of=$1 bs=512 count=1 seek=1 conv=notrunc
138 echo "Inserting EXE boot record..."
139 $0 --get bootiso.bin | ddq of=$1 conv=notrunc
141 # keep the largest room for the tazlito info file
142 add_rootfs $1
143 add_doscom $1
144 add_win32exe $1
145 store 26 ${RANDOM:-0} $1
146 i=66
147 n=0
148 echo -n "Adding checksum..."
149 while [ $i -lt 32768 ]; do
150 n=$(($n + $(get $i $1) ))
151 i=$(($i + 2))
152 done
153 store 64 -$n $1
154 echo " done."
155 }
157 main $@ <<EOT