move patch to correct directory
[openadk.git] / scripts / install.sh
blob81001381f1866e0379f92f5cb5dbf4255d377ab4
1 #!/usr/bin/env bash
2 #-
3 # Copyright © 2010-2015
4 # Waldemar Brodkorb <wbx@openadk.org>
5 # Thorsten Glaser <tg@mirbsd.org>
7 # Provided that these terms and disclaimer and all copyright notices
8 # are retained or reproduced in an accompanying document, permission
9 # is granted to deal in this work without restriction, including un‐
10 # limited rights to use, publicly perform, distribute, sell, modify,
11 # merge, give away, or sublicence.
13 # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
14 # the utmost extent permitted by applicable law, neither express nor
15 # implied; without malicious intent or gross negligence. In no event
16 # may a licensor, author or contributor be held liable for indirect,
17 # direct, other damage, loss, or other issues arising in any way out
18 # of dealing in the work, even if advised of the possibility of such
19 # damage or existence of a defect, except proven that it results out
20 # of said person’s immediate fault when using the work as intended.
22 # Alternatively, this work may be distributed under the terms of the
23 # General Public License, any version, as published by the Free Soft-
24 # ware Foundation.
26 # Prepare a USB stick or CF/SD/MMC card or hard disc for installation
27 # of OpenADK
29 ADK_TOPDIR=$(pwd)
30 HOST=$(gcc -dumpmachine)
31 me=$0
33 case :$PATH: in
34 (*:$ADK_TOPDIR/host_$HOST/usr/bin:*) ;;
35 (*) export PATH=$PATH:$ADK_TOPDIR/host_$HOST/usr/bin ;;
36 esac
38 test -n "$KSH_VERSION" || exec mksh "$me" "$@"
39 if test -z "$KSH_VERSION"; then
40 echo >&2 Fatal error: could not run myself with mksh!
41 exit 255
44 ### run with mksh from here onwards ###
46 me=${me##*/}
48 if (( USER_ID )); then
49 print -u2 Installation is only possible as root!
50 exit 1
53 ADK_TOPDIR=$(realpath .)
54 ostype=$(uname -s)
56 fs=ext4
57 cfgfs=1
58 datafssz=0
59 noformat=0
60 quiet=0
61 serial=0
62 speed=115200
63 panicreboot=10
64 keep=0
65 grub=0
67 function usage {
68 cat >&2 <<EOF
69 Syntax: $me [-f filesystem] [-c cfgfssize] [-d datafssize] [-k] [-n] [-g]
70 [-p panictime] [±q] [-s serialspeed] [±t] <target> <device> <archive>
71 Partition sizes are in MiB. Filesystem type is currently ignored (ext4).
72 To keep filesystem on data partition use -k.
73 Use -n to not format boot/root partition.
74 Defaults: -c 1 -p 10 -s 115200; -t = enable serial console
75 EOF
76 exit $1
79 while getopts "c:d:f:ghknp:qs:t" ch; do
80 case $ch {
81 (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 16 )); then
82 print -u2 "$me: -c $OPTARG out of bounds"
83 exit 1
84 fi ;;
85 (d) if (( (datafssz = OPTARG) < 0 )); then
86 print -u2 "$me: -d $OPTARG out of bounds"
87 exit 1
88 fi ;;
89 (f) if [[ $OPTARG != @(ext2|ext3|ext4|xfs) ]]; then
90 print -u2 "$me: filesystem $OPTARG invalid"
91 exit 1
93 fs=$OPTARG ;;
94 (h) usage 0 ;;
95 (k) keep=1 ;;
96 (g) grub=1 ;;
97 (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
98 print -u2 "$me: -p $OPTARG out of bounds"
99 exit 1
100 fi ;;
101 (q) quiet=1 ;;
102 (+q) quiet=0 ;;
103 (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
104 print -u2 "$me: serial speed $OPTARG invalid"
105 exit 1
107 speed=$OPTARG ;;
108 (n) noformat=1 ;;
109 (t) serial=1 ;;
110 (+t) serial=0 ;;
111 (*) usage 1 ;;
113 done
114 shift $((OPTIND - 1))
116 (( $# == 3 )) || usage 1
119 case $ostype {
120 (Linux)
121 tools="bc mkfs.$fs tune2fs partprobe"
123 (Darwin)
124 tools="bc diskutil"
127 print -u2 Sorry, not ported to the OS "'$ostype'" yet.
128 exit 1
131 for tool in $tools; do
132 print -n Checking if $tool is installed...
133 if whence -p $tool >/dev/null; then
134 print " okay"
135 else
136 print " failed"
139 done
140 (( f )) && exit 1
142 target=$1
143 tgt=$2
144 src=$3
146 case $target {
147 (banana-pro|pcengines-apu|raspberry-pi|raspberry-pi2|solidrun-imx6|default) ;;
149 print -u2 "Unknown target '$target', exiting"
150 exit 1 ;;
152 if [[ ! -b $tgt ]]; then
153 print -u2 "'$tgt' is not a block device, exiting"
154 exit 1
156 if [[ ! -f $src ]]; then
157 print -u2 "'$src' is not a file, exiting"
158 exit 1
160 (( quiet )) || print "Installing $src on $tgt."
162 case $ostype {
163 (Darwin)
164 R=/Volumes/ADKROOT; diskutil unmount $R
165 B=/Volumes/ADKBOOT; diskutil unmount $B
166 D=/Volumes/ADKDATA; diskutil unmount $D
167 basedev=$tgt
168 rootpart=${basedev}s1
169 datapart=${basedev}s2
170 if [[ $target = raspberry-pi || $target = raspberry-pi2 ]]; then
171 bootpart=${basedev}s1
172 rootpart=${basedev}s2
173 datapart=${basedev}s3
175 match=\'${basedev}\''?(s+([0-9]))'
176 function mount_fs {
178 function umount_fs {
179 diskutil unmount "$1"
181 function create_fs {
182 if [[ $3 = ext4 ]]; then
183 fstype=UFSD_EXTFS4
185 if [[ $3 = vfat ]]; then
186 fstype=fat32
188 diskutil eraseVolume $fstype "$2" "$1"
190 function tune_fs {
193 (Linux)
194 basedev=$tgt
195 rootpart=${basedev}1
196 datapart=${basedev}2
197 if [[ $target = raspberry-pi || $target = raspberry-pi2 ]]; then
198 bootpart=${basedev}1
199 rootpart=${basedev}2
200 datapart=${basedev}3
203 match=\'${basedev}\''+([0-9])'
204 function mount_fs {
205 mount -t "$3" "$1" "$2"
207 function umount_fs {
208 umount "$1"
210 function create_fs {
211 (( quiet )) || print "Creating filesystem on ${1}..."
212 partprobe $tgt
213 mkfs.$3 "$1"
215 function tune_fs {
216 tune2fs -c 0 -i 0 "$1"
221 mount |&
222 while read -p dev rest; do
223 eval [[ \$dev = $match ]] || continue
224 print -u2 "Block device $tgt is in use, please umount first."
225 exit 1
226 done
228 if (( !quiet )); then
229 print "WARNING: This will overwrite $basedev - type Yes to continue!"
230 read x
231 [[ $x = Yes ]] || exit 0
234 if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
235 print -u2 Error creating temporary directory.
236 exit 1
238 if [[ $ostype != Darwin ]]; then
239 R=$T/rootmnt
240 B=$T/bootmnt
241 D=$T/datamnt
242 mkdir -p "$R" "$B" "$D"
245 # get disk size
246 dksz=$(dkgetsz "$tgt")
248 # partition layouts:
249 # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
250 # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
251 # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
253 syspartno=0
255 # sizes:
256 # boot(raspberry) - fixed (100 MiB)
257 # cfgfs - fixed (parameter, max. 16 MiB)
258 # data - flexible (parameter)
259 # system - everything else
261 if [[ $target = raspberry-pi || $target = raspberry-pi2 ]]; then
262 syspartno=1
263 bootfssz=100
264 if (( grub )); then
265 print -u2 "Cannot combine GRUB with $target"
266 rm -rf "$T"
267 exit 1
269 else
270 bootfssz=0
273 heads=64
274 secs=32
275 (( cyls = dksz / heads / secs ))
276 if (( cyls < (bootfssz + cfgfs + datafssz + 2) )); then
277 print -u2 "Size of $tgt is $dksz, this looks fishy?"
278 rm -rf "$T"
279 exit 1
282 if stat -qs .>/dev/null 2>&1; then
283 statcmd='stat -f %z' # BSD stat (or so we assume)
284 else
285 statcmd='stat -c %s' # GNU stat
288 if (( grub )); then
289 tar -xOf "$src" boot/grub/core.img >"$T/core.img"
290 integer coreimgsz=$($statcmd "$T/core.img")
291 else
292 coreimgsz=65024
294 if (( coreimgsz < 1024 )); then
295 print -u2 core.img is probably too small: $coreimgsz
296 rm -rf "$T"
297 exit 1
299 if (( coreimgsz > 65024 )); then
300 print -u2 core.img is larger than 64K-512: $coreimgsz
301 rm -rf "$T"
302 exit 1
304 (( coreendsec = (coreimgsz + 511) / 512 ))
305 if [[ $basedev = /dev/svnd+([0-9]) ]]; then
306 # BSD svnd0 mode: protect sector #1
307 corestartsec=2
308 (( ++coreendsec ))
309 corepatchofs=$((0x614))
310 else
311 corestartsec=1
312 corepatchofs=$((0x414))
314 # partition offset: at least coreendsec+1 but aligned on a multiple of secs
315 #(( partofs = ((coreendsec / secs) + 1) * secs ))
316 # we just use 2048 all the time, since some loaders are longer
317 partofs=2048
318 if [[ $target = raspberry-pi || $target = raspberry-pi2 ]]; then
319 (( spartofs = partofs + (100 * 2048) ))
320 else
321 spartofs=$partofs
324 (( quiet )) || if (( grub )); then
325 print Preparing MBR and GRUB2...
326 else
327 print Preparing MBR...
329 dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
330 # add another MiB to clear the first partition
331 dd if=/dev/zero bs=1048576 count=1 >>"$T/firsttrack" 2>/dev/null
332 echo $corestartsec $coreendsec | mksh "$ADK_TOPDIR/scripts/bootgrub.mksh" \
333 -A -g $((cyls - bootfssz - cfgfs - datafssz)):$heads:$secs -M 1:0x83 \
334 -O $spartofs | dd of="$T/firsttrack" conv=notrunc 2>/dev/null
335 (( grub )) && dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
336 seek=$corestartsec 2>/dev/null
337 # set partition where it can find /boot/grub
338 (( grub )) && print -n '\0\0\0\0' | \
339 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
341 # create cfgfs partition (mostly taken from bootgrub.mksh)
342 set -A thecode
343 typeset -Uui8 thecode
344 mbrpno=0
345 set -A g_code $cyls $heads $secs
346 (( psz = g_code[0] * g_code[1] * g_code[2] ))
347 (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
348 set -A o_code # g_code equivalent for partition offset
349 (( o_code[2] = pofs % g_code[2] + 1 ))
350 (( o_code[1] = pofs / g_code[2] ))
351 (( o_code[0] = o_code[1] / g_code[1] + 1 ))
352 (( o_code[1] = o_code[1] % g_code[1] + 1 ))
353 # boot flag; C/H/S offset
354 thecode[mbrpno++]=0x00
355 (( thecode[mbrpno++] = o_code[1] - 1 ))
356 (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
357 (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
358 (( thecode[mbrpno++] = cylno & 0x00FF ))
359 # partition type; C/H/S end
360 (( thecode[mbrpno++] = 0x88 ))
361 (( thecode[mbrpno++] = g_code[1] - 1 ))
362 (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
363 (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
364 (( thecode[mbrpno++] = cylno & 0x00FF ))
365 # partition offset, size (LBA)
366 (( thecode[mbrpno++] = pofs & 0xFF ))
367 (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
368 (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
369 (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
370 (( pssz = psz - pofs ))
371 (( thecode[mbrpno++] = pssz & 0xFF ))
372 (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
373 (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
374 (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
375 # write partition table entry
376 ostr=
377 curptr=0
378 while (( curptr < 16 )); do
379 ostr=$ostr\\0${thecode[curptr++]#8#}
380 done
381 print -n "$ostr" | \
382 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1EE)) 2>/dev/null
384 if (( datafssz )); then
385 # create data partition (copy of the above :)
386 set -A thecode
387 typeset -Uui8 thecode
388 mbrpno=0
389 set -A g_code $cyls $heads $secs
390 (( psz = (cyls - cfgfs) * g_code[1] * g_code[2] ))
391 (( pofs = (cyls - cfgfs - datafssz) * g_code[1] * g_code[2] ))
392 set -A o_code # g_code equivalent for partition offset
393 (( o_code[2] = pofs % g_code[2] + 1 ))
394 (( o_code[1] = pofs / g_code[2] ))
395 (( o_code[0] = o_code[1] / g_code[1] + 1 ))
396 (( o_code[1] = o_code[1] % g_code[1] + 1 ))
397 # boot flag; C/H/S offset
398 thecode[mbrpno++]=0x00
399 (( thecode[mbrpno++] = o_code[1] - 1 ))
400 (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
401 (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
402 (( thecode[mbrpno++] = cylno & 0x00FF ))
403 # partition type; C/H/S end
404 (( thecode[mbrpno++] = 0x83 ))
405 (( thecode[mbrpno++] = g_code[1] - 1 ))
406 (( cylno = (g_code[0] - cfgfs) > 1024 ? 1023 : g_code[0] - cfgfs - 1 ))
407 (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
408 (( thecode[mbrpno++] = cylno & 0x00FF ))
409 # partition offset, size (LBA)
410 (( thecode[mbrpno++] = pofs & 0xFF ))
411 (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
412 (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
413 (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
414 (( pssz = psz - pofs ))
415 (( thecode[mbrpno++] = pssz & 0xFF ))
416 (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
417 (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
418 (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
419 # write partition table entry
420 ostr=
421 curptr=0
422 while (( curptr < 16 )); do
423 ostr=$ostr\\0${thecode[curptr++]#8#}
424 done
425 print -n "$ostr" | \
426 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
429 if [[ $target = raspberry-pi || $target = raspberry-pi2 ]]; then
430 # move system and data partition from #0/#1 to #1/#2
431 dd if="$T/firsttrack" bs=1 skip=$((0x1BE)) count=32 of="$T/x" 2>/dev/null
432 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) if="$T/x" 2>/dev/null
433 # create boot partition (copy of the above :)
434 set -A thecode
435 typeset -Uui8 thecode
436 mbrpno=0
437 set -A g_code $cyls $heads $secs
438 psz=$spartofs
439 pofs=$partofs
440 set -A o_code # g_code equivalent for partition offset
441 (( o_code[2] = pofs % g_code[2] + 1 ))
442 (( o_code[1] = pofs / g_code[2] ))
443 (( o_code[0] = o_code[1] / g_code[1] + 1 ))
444 (( o_code[1] = o_code[1] % g_code[1] + 1 ))
445 # boot flag; C/H/S offset
446 thecode[mbrpno++]=0x00
447 (( thecode[mbrpno++] = o_code[1] - 1 ))
448 (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
449 (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
450 (( thecode[mbrpno++] = cylno & 0x00FF ))
451 # partition type; C/H/S end
452 (( thecode[mbrpno++] = 0x0B ))
453 (( thecode[mbrpno++] = g_code[1] - 1 ))
454 (( cylno = (spartofs / 2048) > 1024 ? 1023 : (spartofs / 2048) - 1 ))
455 (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
456 (( thecode[mbrpno++] = cylno & 0x00FF ))
457 # partition offset, size (LBA)
458 (( thecode[mbrpno++] = pofs & 0xFF ))
459 (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
460 (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
461 (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
462 (( pssz = psz - pofs ))
463 (( thecode[mbrpno++] = pssz & 0xFF ))
464 (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
465 (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
466 (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
467 # write partition table entry
468 ostr=
469 curptr=0
470 while (( curptr < 16 )); do
471 ostr=$ostr\\0${thecode[curptr++]#8#}
472 done
473 print -n "$ostr" | \
474 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BE)) 2>/dev/null
477 # disk signature
478 rnddev=/dev/urandom
479 [[ -c /dev/arandom ]] && rnddev=/dev/arandom
480 dd if=$rnddev bs=4 count=1 2>/dev/null | \
481 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1B8)) 2>/dev/null
482 print -n '\0\0' | \
483 dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BC)) 2>/dev/null
484 partuuid=$(dd if="$T/firsttrack" bs=1 count=4 skip=$((0x1B8)) 2>/dev/null | \
485 hexdump -e '1/4 "%08x"')-0$((syspartno+1))
487 ((keep)) || if (( datafssz )); then
488 (( quiet )) || print Cleaning out data partition...
489 dd if=/dev/zero of="$tgt" bs=1048576 count=1 seek=$((cyls - cfgfs - datafssz)) > /dev/null 2>&1
491 (( quiet )) || print Cleaning out root partition...
492 dd if=/dev/zero bs=1048576 of="$tgt" count=1 seek=$((spartofs / 2048)) > /dev/null 2>&1
494 (( quiet )) || if (( grub )); then
495 print Writing MBR and GRUB2 to target device... system PARTUUID=$partuuid
496 else
497 print Writing MBR to target device... system PARTUUID=$partuuid
499 dd if="$T/firsttrack" of="$tgt" > /dev/null 2>&1
501 fwdir=$(dirname "$src")
503 case $target {
504 (banana-pro)
505 dd if="$fwdir/u-boot-sunxi-with-spl.bin" of="$tgt" bs=1024 seek=8 > /dev/null 2>&1
507 (solidrun-imx6)
508 dd if="$fwdir/SPL" of="$tgt" bs=1024 seek=1 > /dev/null 2>&1
509 dd if="$fwdir/u-boot.img" of="$tgt" bs=1024 seek=42 > /dev/null 2>&1
511 (raspberry-pi|raspberry-pi2)
512 (( noformat )) || create_fs "$bootpart" ADKBOOT vfat
516 (( noformat )) || create_fs "$rootpart" ADKROOT ext4
517 (( noformat )) || tune_fs "$rootpart"
519 (( quiet )) || print Extracting installation archive...
520 mount_fs "$rootpart" "$R" ext4
521 xz -dc "$src" | (cd "$R"; tar -xpf -)
523 if (( datafssz )); then
524 mkdir -m0755 "$R"/data
525 ((keep)) || create_fs "$datapart" ADKDATA ext4
526 ((keep)) || tune_fs "$datapart"
527 case $target {
528 (raspberry-pi|raspberry-pi2)
529 echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc/fstab
531 (banana-pro|solidrun-imx6)
532 echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
537 case $target {
538 (raspberry-pi|raspberry-pi2)
539 mount_fs "$bootpart" "$B" vfat
540 for x in "$R"/boot/*; do
541 [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
542 break
543 done
544 for x in "$fwdir"/*.dtb; do
545 [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
546 break
547 done
548 mkdir "$B/"overlays
549 for x in "$B/"*-overlay.dtb; do
550 [[ -e "$x" ]] && mv "$B/"*-overlay.dtb "$B/"overlays
551 break
552 done
553 umount_fs "$B"
555 (solidrun-imx6)
556 for x in "$fwdir"/*.dtb; do
557 [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
558 break
559 done
563 cd "$R"
564 dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
565 (( quiet )) || print Fixing up permissions...
566 chown 0:0 tmp
567 chmod 1777 tmp
568 [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
570 if (( grub )); then
571 (( quiet )) || print Configuring GRUB2 bootloader...
572 mkdir -p boot/grub
574 print set default=0
575 print set timeout=1
576 if (( serial )); then
577 print serial --unit=0 --speed=$speed
578 print terminal_output serial
579 print terminal_input serial
580 consargs="console=ttyS0,$speed console=tty0"
581 else
582 print terminal_output console
583 print terminal_input console
584 consargs="console=tty0"
586 print
587 print 'menuentry "GNU/Linux (OpenADK)" {'
588 linuxargs="root=PARTUUID=$partuuid $consargs"
589 (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
590 print "\tlinux /boot/kernel $linuxargs"
591 print '}'
592 ) >boot/grub/grub.cfg
595 (( quiet )) || print Finishing up...
596 cd "$ADK_TOPDIR"
597 umount_fs "$R"
598 rm -rf "$T"
599 exit 0