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

syslinux/iso2exe: check kernel version, add floppy bootstrap stub
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Mar 28 11:51:42 2013 +0100 (2013-03-28)
parents a26ba54f3ea7
children 5ed4d6b2d690
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 store 60 $OFS $1
63 fi
64 rm -f /tmp/exe$$
65 }
67 add_fdbootstrap()
68 {
69 SIZE=$($0 --get bootfd.bin 2> /dev/null | tee /tmp/exe$$ | wc -c)
70 if [ $SIZE -ne 0 ]; then
71 OFS=$(( $OFS - $SIZE ))
72 printf "Adding floppy bootstrap file at %04X...\n" $OFS
73 $0 --get bootfd.bin | ddq of=$1 bs=1 seek=$OFS conv=notrunc
74 store 28 $(($SIZE/512)) $1 8
75 fi
76 }
77 case "$1" in
78 --build)
79 shift
80 [ $(tar cf - $@ | wc -c) -gt $((32 * 1024)) ] &&
81 echo "WARNING: The file set $@ is too large (31K max) :" &&
82 ls -l $@
83 cat >> $0 <<EOM
84 $(tar cf - $@ | lzma e -si -so | uuencode -m -)
85 EOT
86 EOM
87 sed -i '/^case/,/^esac/d' $0
88 exit ;;
89 --get)
90 cat $2
91 exit ;;
92 --array)
93 DATA=/tmp/dataiso$$
94 ddq if=/dev/zero bs=32k count=1 of=$DATA
95 ddq if=bootiso.bin of=$DATA conv=notrunc
96 ddq if=$2 of=$DATA bs=512 seek=1 conv=notrunc
97 add_rootfs $DATA > /dev/null
98 add_doscom $DATA > /dev/null
99 add_win32exe $DATA > /dev/null
100 add_fdbootstrap $DATA > /dev/null
101 name=${3:-bootiso}
102 cat <<EOT
104 #define $(echo $name | tr '[a-z]' '[A-Z]')SZ $((0x8400 - $OFS))
106 #ifdef WIN32
107 static char $name[] = {
108 $(hexdump -v -n 1024 -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
109 $(hexdump -v -s $OFS -e '" " 16/1 "0x%02X, "' -e '" // %04.4_ax |" 16/1 "%_p" "| \n"' $DATA | sed 's/ 0x ,/ /g')
110 };
111 #endif
112 EOT
113 rm -rf $DATA
114 exit ;;
115 --exe)
116 # --exe mvcom.bin x.com y.exe > xy.exe
117 cat $4 $3 > /tmp/exe$$
118 S=$(stat -c %s /tmp/exe$$)
119 store 2 $(($S%512)) /tmp/exe$$
120 store 4 $((($S+511)/512)) /tmp/exe$$
121 store 14 -16 /tmp/exe$$
122 store 16 -2 /tmp/exe$$
123 store 20 256 /tmp/exe$$
124 store 22 -16 /tmp/exe$$
125 ddq if=$2 bs=1 seek=64 of=/tmp/exe$$ conv=notrunc
126 store 65 $(stat -c %s $3) /tmp/exe$$
127 store 68 $((0x100-0x40+$(stat -c %s $4))) /tmp/exe$$
128 cat /tmp/exe$$
129 rm -f /tmp/exe$$
130 exit ;;
131 esac
133 main()
134 {
135 [ $(id -u) -ne 0 ] && exec su -c "$0 $@" < /dev/tty
136 case "$1" in
137 --get) shift
138 uudecode | unlzma | tar xOf - $@
139 exit ;;
140 *) cat > /dev/null
141 esac
143 [ ! -s "$1" ] && echo "usage: $0 image.iso" 1>&2 && exit 1
144 case "$(get 0 $1)" in
145 23117) echo "The file $1 is already an EXE file." 1>&2 && exit 1;;
146 0) [ -x /usr/bin/isohybrid ] && isohybrid $1
147 esac
149 echo "Moving syslinux hybrid boot record..."
150 ddq if=$1 bs=512 count=1 | ddq of=$1 bs=512 count=1 seek=1 conv=notrunc
152 echo "Inserting EXE boot record..."
153 $0 --get bootiso.bin | ddq of=$1 conv=notrunc
155 # keep the largest room for the tazlito info file
156 add_rootfs $1
157 add_doscom $1
158 add_win32exe $1
159 add_fdbootstrap $1
160 store 26 ${RANDOM:-0} $1
161 i=66
162 n=0
163 echo -n "Adding checksum..."
164 while [ $i -lt 32768 ]; do
165 n=$(($n + $(get $i $1) ))
166 i=$(($i + 2))
167 done
168 store 64 -$n $1
169 echo " done."
170 }
172 main $@ <<EOT