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