wok-next view syslinux/stuff/tools/isohybrid.sh @ rev 8403

syslinux: add help in isohybrid
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Feb 05 19:43:16 2011 +0100 (2011-02-05)
parents 7d5ccd76597b
children f25f2e389934
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*|--e*) entry=$2; shift;;
27 -f*) hd0=1;;
28 -h) heads=$2; shift;;
29 -i|--i**) id=$(($2)); shift;;
30 -noh*) hd0=0;;
31 -nop*) partok=0;;
32 -o*|--o*) offset=$(($2)); shift;;
33 -p*) partok=1;;
34 -s) sectors=$2; shift;;
35 -t*|--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 [options] isoimage
44 options:
45 -h <X> Number of default geometry heads
46 -s <X> Number of default geometry sectors
47 -e --entry Specify partition entry number (1-4)
48 -o --offset Specify partition offset (default 0)
49 -t --type Specify partition type (default 0x17)
50 -i --id Specify MBR ID (default random)
51 --forcehd0 Assume we are loaded as disk ID 0
52 --ctrlhd0 Assume disk ID 0 if the Ctrl key is pressed
53 --partok Allow booting from within a partition
54 EOT
55 exit 1
56 fi
58 ddq()
59 {
60 dd "$@" 2> /dev/null
61 }
63 readiso()
64 {
65 ddq bs=2k skip=$1 count=1 if=$iso | ddq bs=1 skip=$2 count=$3
66 }
68 # read a 32 bits data
69 read32()
70 {
71 readiso $1 $2 4 | hexdump -e '"" 1/4 "%d" "\n"'
72 }
74 # write a 32 bits data
75 store32()
76 {
77 n=$2; i=4; while [ $i -ne 0 ]; do
78 printf '\\\\x%02X' $(($n & 255))
79 i=$(($i-1)); n=$(($n >> 8))
80 done | xargs echo -en | ddq bs=1 conv=notrunc of=$iso seek=$(($1))
81 }
83 main()
84 {
85 uudecode | gunzip | ddq bs=512 count=1 of=$iso conv=notrunc \
86 skip=$(( (3*$partok) + $hd0))
87 store32 432 $(($lba * 4))
88 store32 440 $id
89 store32 508 0xAA550000
90 e=$(( (($entry -1) % 4) *16 +446))
91 store32 $e 0x10080
92 esect=$(( ($sectors + ((($cylinders -1) & 0x300) >>2)) <<16))
93 ecyl=$(( (($cylinders -1) & 0xff) <<24))
94 store32 $(($e + 4)) $(($partype + (($heads - 1) <<8) +$esect +$ecyl))
95 store32 $(($e + 8)) $offset
96 store32 $(($e + 12)) $(($cylinders * $heads * $sectors))
97 }
99 abort()
100 {
101 echo "$iso: $1"
102 exit 1
103 }
105 [ "$(readiso 17 7 23)" == "EL TORITO SPECIFICATION" ] ||
106 abort "no boot record found."
107 cat=$(read32 17 71)
108 [ $(read32 $cat 0) -eq 1 -a $(read32 $cat 30) -eq $(( 0x88AA55 )) ] ||
109 abort "invalid boot catalog."
110 lba=$(read32 $cat 40)
111 [ $(read32 $lba 64) -eq 1886961915 ] ||
112 abort "no isolinux.bin hybrid signature in bootloader."
113 size=$(stat -c "%s" $iso)
114 trksz=$(( 512 * $heads * $sectors ))
115 cylinders=$(( ($size + $trksz - 1) / $trksz ))
116 pad=$(( (($cylinders * $trksz) - $size) / 512 ))
117 [ $pad -eq 0 ] || ddq bs=512 count=$pad if=/dev/zero >> $iso
118 if [ $cylinders -gt 1024 ]; then
119 cat 1>&2 <<EOT
120 Warning: more than 1024 cylinders ($cylinders).
121 Not all BIOSes will be able to boot this device.
122 EOT
123 cylinders=1024
124 fi
126 main <<EOT