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