wok view syslinux/stuff/tools/isohybrid.sh @ rev 7860

syslinux/isohybrid: fix last mbrs
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jan 01 18:19:39 2011 +0100 (2011-01-01)
parents b25fc6e108b2
children 7d497422d803
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 echo $2 | awk '{ for(n=$1,i=4;i--;n/=256) printf "\\\\x%02X",n%256 }' |\
68 xargs echo -en | ddq bs=1 conv=notrunc of=$iso seek=$(($1))
69 }
71 main()
72 {
73 uudecode | gunzip | ddq bs=512 count=1 of=$iso conv=notrunc \
74 skip=$(( (3*$partok) + $hd0))
75 store32 432 $(($lba * 4))
76 store32 440 $id
77 store32 508 $((0xAA550000))
78 e=$(( (($entry -1) % 4) *16 +446))
79 store32 $e $((0x10080))
80 esect=$(( ($sectors + ((($cylinders -1) & 0x300) >>2)) <<16))
81 ecyl=$(( (($cylinders -1) & 0xff) <<24))
82 store32 $(($e + 4)) $(($partype + (($heads - 1) <<8) +$esect +$ecyl))
83 store32 $(($e + 8)) $offset
84 store32 $(($e + 12)) $(($cylinders * $heads * $sectors))
85 }
87 abort()
88 {
89 echo "$iso: $1"
90 exit 1
91 }
93 [ "$(readiso 17 7 23)" == "EL TORITO SPECIFICATION" ] ||
94 abort "no boot record found."
95 cat=$(read32 17 71)
96 [ $(read32 $cat 0) -eq 1 -a $(read32 $cat 30) -eq $(( 0x88AA55 )) ] ||
97 abort "invalid boot catalog."
98 lba=$(read32 $cat 40)
99 [ $(read32 $lba 64) -eq 1886961915 ] ||
100 abort "no isolinux.bin hybrid signature in bootloader."
101 size=$(stat -c "%s" $iso)
102 trksz=$(( 512 * $heads * $sectors ))
103 cylinders=$(( ($size + $trksz - 1) / $trksz ))
104 pad=$(( (($cylinders * $trksz) - $size) / 512 ))
105 [ $pad -eq 0 ] || ddq bs=512 count=$pad if=/dev/zero >> $iso
106 if [ $cylinders -gt 1024 ]; then
107 cat 1>&2 <<EOT
108 Warning: more than 1024 cylinders ($cylinders).
109 Not all BIOSes will be able to boot this device.
110 EOT
111 cylinders=1024
112 fi
114 main <<EOT