wok view syslinux/stuff/tools/isohybrid.sh @ rev 20331
fasm: fix build
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Tue May 29 10:57:28 2018 +0200 (2018-05-29) |
parents | f215c72530a6 |
children | f582dbc28a10 |
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=
18 id=$(( ($RANDOM <<16) + $RANDOM))
19 offset=0
20 partok=0
21 hd0=0
22 always=0
24 while [ -n "$1" ]; do
25 case "${1/--/-}" in
26 -a*) always=1;;
27 -ct*) hd0=2;;
28 -e*) entry=$2; shift;;
29 -f*) hd0=1;;
30 -h) heads=$2; shift;;
31 -i*) id=$(($2)); shift;;
32 -noh*) hd0=0;;
33 -nop*) partok=0;;
34 -o*) offset=$(($2)); shift;;
35 -p*) partok=1;;
36 -s) sectors=$2; shift;;
37 -t*) partype=$(($2 & 255)); shift;;
38 *) iso=$1;;
39 esac
40 shift
41 done
43 if [ ! -f "$iso" ]; then
44 cat << EOT
45 usage: $0 [options] isoimage
46 options:
47 -h <X> Number of default geometry heads
48 -s <X> Number of default geometry sectors
49 -e --entry Specify partition entry number (1-4)
50 -o --offset Specify partition offset (default 0)
51 -t --type Specify partition type (default 0x17)
52 -i --id Specify MBR ID (default random)
53 --forcehd0 Assume we are loaded as disk ID 0
54 --ctrlhd0 Assume disk ID 0 if the Ctrl key is pressed
55 --partok Allow booting from within a partition
56 --always Do not abort on errors
57 EOT
58 exit 1
59 fi
61 ddq()
62 {
63 dd "$@" 2> /dev/null
64 }
66 readiso()
67 {
68 ddq bs=2k skip=$1 count=1 if=$iso | ddq bs=1 skip=$2 count=$3
69 }
71 # read a 32 bits data
72 read32()
73 {
74 readiso $1 $2 4 | od -N 4 -t u4 -An
75 }
77 # read a 16 bits data
78 read16()
79 {
80 readiso $1 $2 2 | od -N 2 -t u2 -An
81 }
83 # read a 8 bits data
84 read8()
85 {
86 readiso $1 $2 1 | od -N 1 -t u1 -An
87 }
89 # write a 32 bits data
90 store32()
91 {
92 n=$2; for i in 0 8 16 24; do
93 printf '\\\\x%02X' $((($n >> $i) & 255))
94 done | xargs echo -en | ddq bs=1 conv=notrunc of=$iso seek=$(($1))
95 }
97 store32sw()
98 {
99 n=$2; for i in 24 16 8 0; do
100 printf '\\\\x%02X' $((($n >> $i) & 255))
101 done | xargs echo -en | ddq bs=1 conv=notrunc of=$iso seek=$(($1))
102 }
104 crc32()
105 {
106 t0=00000000; t1=77073096; t2=EE0E612C; t3=990951BA;
107 t4=076DC419; t5=706AF48F; t6=E963A535; t7=9E6495A3;
108 t8=0EDB8832; t9=79DCB8A4; t10=E0D5E91E; t11=97D2D988;
109 t12=09B64C2B; t13=7EB17CBD; t14=E7B82D07; t15=90BF1D91;
110 t16=1DB71064; t17=6AB020F2; t18=F3B97148; t19=84BE41DE;
111 t20=1ADAD47D; t21=6DDDE4EB; t22=F4D4B551; t23=83D385C7;
112 t24=136C9856; t25=646BA8C0; t26=FD62F97A; t27=8A65C9EC;
113 t28=14015C4F; t29=63066CD9; t30=FA0F3D63; t31=8D080DF5;
114 t32=3B6E20C8; t33=4C69105E; t34=D56041E4; t35=A2677172;
115 t36=3C03E4D1; t37=4B04D447; t38=D20D85FD; t39=A50AB56B;
116 t40=35B5A8FA; t41=42B2986C; t42=DBBBC9D6; t43=ACBCF940;
117 t44=32D86CE3; t45=45DF5C75; t46=DCD60DCF; t47=ABD13D59;
118 t48=26D930AC; t49=51DE003A; t50=C8D75180; t51=BFD06116;
119 t52=21B4F4B5; t53=56B3C423; t54=CFBA9599; t55=B8BDA50F;
120 t56=2802B89E; t57=5F058808; t58=C60CD9B2; t59=B10BE924;
121 t60=2F6F7C87; t61=58684C11; t62=C1611DAB; t63=B6662D3D;
122 t64=76DC4190; t65=01DB7106; t66=98D220BC; t67=EFD5102A;
123 t68=71B18589; t69=06B6B51F; t70=9FBFE4A5; t71=E8B8D433;
124 t72=7807C9A2; t73=0F00F934; t74=9609A88E; t75=E10E9818;
125 t76=7F6A0DBB; t77=086D3D2D; t78=91646C97; t79=E6635C01;
126 t80=6B6B51F4; t81=1C6C6162; t82=856530D8; t83=F262004E;
127 t84=6C0695ED; t85=1B01A57B; t86=8208F4C1; t87=F50FC457;
128 t88=65B0D9C6; t89=12B7E950; t90=8BBEB8EA; t91=FCB9887C;
129 t92=62DD1DDF; t93=15DA2D49; t94=8CD37CF3; t95=FBD44C65;
130 t96=4DB26158; t97=3AB551CE; t98=A3BC0074; t99=D4BB30E2;
131 t100=4ADFA541; t101=3DD895D7; t102=A4D1C46D; t103=D3D6F4FB;
132 t104=4369E96A; t105=346ED9FC; t106=AD678846; t107=DA60B8D0;
133 t108=44042D73; t109=33031DE5; t110=AA0A4C5F; t111=DD0D7CC9;
134 t112=5005713C; t113=270241AA; t114=BE0B1010; t115=C90C2086;
135 t116=5768B525; t117=206F85B3; t118=B966D409; t119=CE61E49F;
136 t120=5EDEF90E; t121=29D9C998; t122=B0D09822; t123=C7D7A8B4;
137 t124=59B33D17; t125=2EB40D81; t126=B7BD5C3B; t127=C0BA6CAD;
138 t128=EDB88320; t129=9ABFB3B6; t130=03B6E20C; t131=74B1D29A;
139 t132=EAD54739; t133=9DD277AF; t134=04DB2615; t135=73DC1683;
140 t136=E3630B12; t137=94643B84; t138=0D6D6A3E; t139=7A6A5AA8;
141 t140=E40ECF0B; t141=9309FF9D; t142=0A00AE27; t143=7D079EB1;
142 t144=F00F9344; t145=8708A3D2; t146=1E01F268; t147=6906C2FE;
143 t148=F762575D; t149=806567CB; t150=196C3671; t151=6E6B06E7;
144 t152=FED41B76; t153=89D32BE0; t154=10DA7A5A; t155=67DD4ACC;
145 t156=F9B9DF6F; t157=8EBEEFF9; t158=17B7BE43; t159=60B08ED5;
146 t160=D6D6A3E8; t161=A1D1937E; t162=38D8C2C4; t163=4FDFF252;
147 t164=D1BB67F1; t165=A6BC5767; t166=3FB506DD; t167=48B2364B;
148 t168=D80D2BDA; t169=AF0A1B4C; t170=36034AF6; t171=41047A60;
149 t172=DF60EFC3; t173=A867DF55; t174=316E8EEF; t175=4669BE79;
150 t176=CB61B38C; t177=BC66831A; t178=256FD2A0; t179=5268E236;
151 t180=CC0C7795; t181=BB0B4703; t182=220216B9; t183=5505262F;
152 t184=C5BA3BBE; t185=B2BD0B28; t186=2BB45A92; t187=5CB36A04;
153 t188=C2D7FFA7; t189=B5D0CF31; t190=2CD99E8B; t191=5BDEAE1D;
154 t192=9B64C2B0; t193=EC63F226; t194=756AA39C; t195=026D930A;
155 t196=9C0906A9; t197=EB0E363F; t198=72076785; t199=05005713;
156 t200=95BF4A82; t201=E2B87A14; t202=7BB12BAE; t203=0CB61B38;
157 t204=92D28E9B; t205=E5D5BE0D; t206=7CDCEFB7; t207=0BDBDF21;
158 t208=86D3D2D4; t209=F1D4E242; t210=68DDB3F8; t211=1FDA836E;
159 t212=81BE16CD; t213=F6B9265B; t214=6FB077E1; t215=18B74777;
160 t216=88085AE6; t217=FF0F6A70; t218=66063BCA; t219=11010B5C;
161 t220=8F659EFF; t221=F862AE69; t222=616BFFD3; t223=166CCF45;
162 t224=A00AE278; t225=D70DD2EE; t226=4E048354; t227=3903B3C2;
163 t228=A7672661; t229=D06016F7; t230=4969474D; t231=3E6E77DB;
164 t232=AED16A4A; t233=D9D65ADC; t234=40DF0B66; t235=37D83BF0;
165 t236=A9BCAE53; t237=DEBB9EC5; t238=47B2CF7F; t239=30B5FFE9;
166 t240=BDBDF21C; t241=CABAC28A; t242=53B39330; t243=24B4A3A6;
167 t244=BAD03605; t245=CDD70693; t246=54DE5729; t247=23D967BF;
168 t248=B3667A2E; t249=C4614AB8; t250=5D681B02; t251=2A6F2B94;
169 t252=B40BBE37; t253=C30C8EA1; t254=5A05DF1B; t255=2D02EF8D;
170 crc=$((0xFFFFFFFF))
171 dd if=$iso bs=1 skip=$(($1)) count=$(($2)) 2> /dev/null | \
172 od -v -w1 -t u1 -An | {
173 while read n; do
174 local x=$((($crc ^ $n) & 255))
175 eval x=0x\$t$x
176 crc=$(((($crc >> 8) & 0x00FFFFFF) ^ $x))
177 done
178 echo $(($crc ^ 0xFFFFFFFF))
179 }
180 }
182 main()
183 {
184 uudecode | gunzip | ddq bs=512 count=1 of=$iso conv=notrunc \
185 skip=$(( (3*$partok) + $hd0))
186 store32 432 $(($lba * 4))
187 store32 440 $id
188 store32 508 0xAA550000
189 e=$(( ((${entry:-1} -1) % 4) *16 +446))
190 store32 $e 0x10080
191 esect=$(( ($sectors + ((($cylinders -1) & 0x300) >>2)) <<16))
192 ecyl=$(( (($cylinders -1) & 0xff) <<24))
193 epart=$(((($heads - 1) <<8) +$esect +$ecyl))
194 store32 $(($e + 4)) $(($partype + $epart))
195 store32 $(($e + 8)) $offset
196 sectorcount=$(($cylinders * $heads * $sectors))
197 store32 $(($e + 12)) $sectorcount
198 if [ -n "$efi_ofs" ]; then
199 [ $(read16 0 1024) -eq 35615 -a $(read16 11 0) -ne 35615 ] &&
200 ddq bs=512 conv=notrunc skip=2 seek=44 count=20 if=$iso of=$iso
201 store32 $((446)) $((0x10000))
202 store32 $((446+4)) $((0xFFFFFEEE))
203 store32 $((446+8)) $efi_ofs
204 store32 $((446+12)) $efi_len
205 uudecode <<EOT | unlzma | ddq bs=512 seek=1 of=$iso conv=notrunc
206 begin-base64 644 -
207 XQAAgAD//////////wAikYVN1N2VY3JXMnUMJn1RCdQOHN33EegtIBhrUQ7P
208 3PVRrH5+fynx2ZfhIpUTKFVouVH5CEgkcBOQqiTCVX7AsRmUkli6MUo+x+TC
209 9ftViSMiaEWDb8bI73GU3XCMZsZaT5nFwi6NqzhBI9sfRJz5i6cK7kSsPjxN
210 bK6ivatF20do2T7S8NSju5FOCCwMuMHuIcU+Ic8JzCvfjdjkHSkUjA6wIcQi
211 48FNYYyKv58kxngpmoy32Xc7rZ4cKAXJl3LmsFkYdw7QuZ7Rqv5sP3Z8St8D
212 krjcZVJf//Pj90o=
213 ====
214 EOT
215 lastlba=$(($sectorcount -1))
216 usablelba=34
217 store32 $((0x218)) 1
218 store32 $((0x220)) $lastlba
219 store32 $((0x228)) $usablelba
220 store32 $((0x230)) $(($lastlba-$usablelba+1))
221 store32 $((0x420)) $efi_ofs
222 store32 $((0x428)) $(($efi_ofs+$efi_len-1))
223 store32 $((0x4A8)) $(($lastlba-0x800))
224 store32 $((0x4B0)) 5
225 store32sw $((0x1008)) $(($efi_ofs/4))
226 store32sw $((0x1054)) $(($efi_len/4))
227 for i in 238 410 490 ; do
228 ddq if=/dev/urandom count=16 bs=1 conv=notrunc \
229 of=$iso seek=$((0x$i))
230 done
231 store32 $((0x258)) $(crc32 0x400 0x4000)
232 store32 $((0x210)) $(crc32 0x200 $(read32 0 $((0x20C))))
233 fi
234 }
236 abort()
237 {
238 echo "$iso: $1"
239 [ $always -eq 0 ] && exit 1
240 }
242 [ "$(readiso 17 7 23)" == "EL TORITO SPECIFICATION" ] ||
243 abort "no boot record found."
244 cat=$(read32 17 71)
245 [ $(read32 $cat 0) -eq 1 -a $(read32 $cat 30) -eq $(( 0x88AA55 )) ] ||
246 abort "invalid boot catalog."
247 efi_ofs=
248 if [ $(read8 $cat 65) -eq 239 ]; then
249 [ -n "$entry" ] && echo "$iso: efi boot ignore --entry $entry"
250 entry=2
251 partype=0
252 efi_ofs=$((4*$(read32 $cat 104)))
253 efi_len=$(($(read16 $(($efi_ofs/4)) 19)))
254 [ $efi_len -eq 0 ] && efi_len=$(($(read32 $(($efi_ofs/4)) 32)))
255 fi
256 lba=$(read32 $cat 40)
257 [ $(read32 $lba 64) -eq 1886961915 ] ||
258 abort "no isolinux.bin hybrid signature in bootloader."
259 size=$(stat -c "%s" $iso)
260 trksz=$(( 512 * $heads * $sectors ))
261 cylinders=$(( ($size + $trksz - 1) / $trksz ))
262 pad=$(( (($cylinders * $trksz) - $size) / 512 ))
263 #[ $pad -eq 0 ] || ddq bs=512 count=$pad if=/dev/zero >> $iso
264 if [ $cylinders -gt 1024 ]; then
265 cat 1>&2 <<EOT
266 Warning: more than 1024 cylinders ($cylinders).
267 Not all BIOSes will be able to boot this device.
268 EOT
269 cylinders=1024
270 fi
272 main <<EOT