wok-4.x view syslinux/stuff/tools/isohybrid.sh @ rev 8399
linux,syslinux: no more awk in shell tools
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Feb 05 16:34:18 2011 +0100 (2011-02-05) |
parents | a80a9972e567 |
children | 7d5ccd76597b |
line source
1 #!/bin/sh
3 if [ "$1" == "--build" ]; then
4 cat >> $0 <<EOM
5 $(for i in fx fx_f fx_c px px_f px_c ; do
6 cat mbr/isohdp$i.bin /dev/zero | dd bs=1 count=512 2> /dev/null
7 done | gzip -9 | uuencode -m -)
8 EOT
9 EOM
10 sed -i '/--build/,/^fi/d' $0
11 exit
12 fi
13 iso=
14 heads=64 # zipdrive-style geometry
15 sectors=32
16 partype=23 # "Windows hidden IFS"
17 entry=1
18 id=$(( ($RANDOM <<16) + $RANDOM))
19 offset=0
20 partok=0
21 hd0=0
23 while [ -n "$1" ]; do
24 case "$1" in
25 -ct*) hd0=2;;
26 -e*) entry=$2; shift;;
27 -f*) hd0=1;;
28 -h) heads=$2; shift;;
29 -i*) id=$(($2)); shift;;
30 -noh*) hd0=0;;
31 -nop*) partok=0;;
32 -o*) offset=$(($2)); shift;;
33 -p*) partok=1;;
34 -s) sectors=$2; shift;;
35 -t*) partype=$(($2 & 255)); shift;;
36 *) iso=$1;;
37 esac
38 shift
39 done
41 if [ ! -f "$iso" ]; then
42 cat << EOT
43 usage: $0 isoimage
44 EOT
45 exit 1
46 fi
48 ddq()
49 {
50 dd "$@" 2> /dev/null
51 }
53 readiso()
54 {
55 ddq bs=2k skip=$1 count=1 if=$iso | ddq bs=1 skip=$2 count=$3
56 }
58 # read a 32 bits data
59 read32()
60 {
61 readiso $1 $2 4 | hexdump -e '"" 1/4 "%d" "\n"'
62 }
64 # write a 32 bits data
65 store32()
66 {
67 n=$2; i=4; while [ $i -ne 0 ]; do
68 printf '\\\\x%02X' $(($n & 255))
69 i=$(($i-1)); n=$(($n >> 8))
70 done | xargs echo -en | ddq bs=1 conv=notrunc of=$iso seek=$(($1))
71 }
73 main()
74 {
75 uudecode | gunzip | ddq bs=512 count=1 of=$iso conv=notrunc \
76 skip=$(( (3*$partok) + $hd0))
77 store32 432 $(($lba * 4))
78 store32 440 $id
79 store32 508 $((0xAA550000))
80 e=$(( (($entry -1) % 4) *16 +446))
81 store32 $e $((0x10080))
82 esect=$(( ($sectors + ((($cylinders -1) & 0x300) >>2)) <<16))
83 ecyl=$(( (($cylinders -1) & 0xff) <<24))
84 store32 $(($e + 4)) $(($partype + (($heads - 1) <<8) +$esect +$ecyl))
85 store32 $(($e + 8)) $offset
86 store32 $(($e + 12)) $(($cylinders * $heads * $sectors))
87 }
89 abort()
90 {
91 echo "$iso: $1"
92 exit 1
93 }
95 [ "$(readiso 17 7 23)" == "EL TORITO SPECIFICATION" ] ||
96 abort "no boot record found."
97 cat=$(read32 17 71)
98 [ $(read32 $cat 0) -eq 1 -a $(read32 $cat 30) -eq $(( 0x88AA55 )) ] ||
99 abort "invalid boot catalog."
100 lba=$(read32 $cat 40)
101 [ $(read32 $lba 64) -eq 1886961915 ] ||
102 abort "no isolinux.bin hybrid signature in bootloader."
103 size=$(stat -c "%s" $iso)
104 trksz=$(( 512 * $heads * $sectors ))
105 cylinders=$(( ($size + $trksz - 1) / $trksz ))
106 pad=$(( (($cylinders * $trksz) - $size) / 512 ))
107 [ $pad -eq 0 ] || ddq bs=512 count=$pad if=/dev/zero >> $iso
108 if [ $cylinders -gt 1024 ]; then
109 cat 1>&2 <<EOT
110 Warning: more than 1024 cylinders ($cylinders).
111 Not all BIOSes will be able to boot this device.
112 EOT
113 cylinders=1024
114 fi
116 main <<EOT