3 # Copyright © 2010-2016
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-
26 # Prepare a USB stick or CF/SD/MMC card or hard disc for installation
30 HOST
=$
(gcc
-dumpmachine)
34 (*:$ADK_TOPDIR/host_
$HOST/usr
/bin
:*) ;;
35 (*) export PATH
=$PATH:$ADK_TOPDIR/host_
$HOST/usr
/bin
;;
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
!
44 ### run with mksh from here onwards ###
48 if (( USER_ID
)); then
49 print
-u2 Installation is only possible as root
!
53 ADK_TOPDIR
=$
(realpath .
)
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
79 while getopts "c:d:f:ghknp:qs:t" ch
; do
81 (c
) if (( (cfgfs
= OPTARG
) < 0 || cfgfs
> 16 )); then
82 print
-u2 "$me: -c $OPTARG out of bounds"
85 (d
) if (( (datafssz
= OPTARG
) < 0 )); then
86 print
-u2 "$me: -d $OPTARG out of bounds"
89 (f
) if [[ $OPTARG != @
(ext2|ext3|ext4|xfs
) ]]; then
90 print
-u2 "$me: filesystem $OPTARG invalid"
97 (p
) if (( (panicreboot
= OPTARG
) < 0 || panicreboot
> 300 )); then
98 print
-u2 "$me: -p $OPTARG out of bounds"
103 (s
) if [[ $OPTARG != @
(96|
192|
384|
576|
1152)00 ]]; then
104 print
-u2 "$me: serial speed $OPTARG invalid"
114 shift $
((OPTIND
- 1))
116 (( $# == 3 )) || usage
1
121 tools
="bc mkfs.$fs tune2fs partprobe"
127 print
-u2 Sorry
, not ported to the OS
"'$ostype'" yet.
131 for tool
in $tools; do
132 print
-n Checking
if $tool is installed...
133 if whence
-p $tool >/dev
/null
; then
147 (banana-pro|pcengines-apu|raspberry-pi|raspberry-pi2|raspberry-pi3|solidrun-imx6|solidrun-clearfog|default
) ;;
149 print
-u2 "Unknown target '$target', exiting"
152 if [[ ! -b $tgt ]]; then
153 print
-u2 "'$tgt' is not a block device, exiting"
156 if [[ ! -f $src ]]; then
157 print
-u2 "'$src' is not a file, exiting"
160 (( quiet
)) || print
"Installing $src on $tgt."
164 R
=/Volumes
/ADKROOT
; diskutil unmount
$R
165 B
=/Volumes
/ADKBOOT
; diskutil unmount
$B
166 D
=/Volumes
/ADKDATA
; diskutil unmount
$D
168 rootpart
=${basedev}s1
169 datapart
=${basedev}s2
170 if [[ $target = raspberry-pi ||
$target = raspberry-pi2 ||
$target = raspberry-pi3
]]; then
171 bootpart
=${basedev}s1
172 rootpart
=${basedev}s2
173 datapart
=${basedev}s3
175 match
=\'${basedev}\''?(s+([0-9]))'
179 diskutil unmount
"$1"
182 if [[ $3 = ext4
]]; then
185 if [[ $3 = vfat
]]; then
188 diskutil eraseVolume
$fstype "$2" "$1"
197 if [[ $target = raspberry-pi ||
$target = raspberry-pi2 ||
$target = raspberry-pi3
]]; then
203 match
=\'${basedev}\''+([0-9])'
205 mount
-t "$3" "$1" "$2"
211 (( quiet
)) || print
"Creating filesystem on ${1}..."
215 tune2fs
-c 0 -i 0 "$1"
221 while read -p dev rest
; do
222 eval [[ \
$dev = $match ]] ||
continue
223 print
-u2 "Block device $tgt is in use, please umount first."
227 if (( !quiet
)); then
228 print
"WARNING: This will overwrite $basedev - type Yes to continue!"
230 [[ $x = Yes
]] ||
exit 0
233 if ! T
=$
(mktemp
-d /tmp
/openadk.XXXXXXXXXX
); then
234 print
-u2 Error creating temporary directory.
237 if [[ $ostype != Darwin
]]; then
241 mkdir
-p "$R" "$B" "$D"
245 dksz
=$
(dkgetsz
"$tgt")
248 # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
249 # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
250 # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
255 # boot(raspberry) - fixed (100 MiB)
256 # cfgfs - fixed (parameter, max. 16 MiB)
257 # data - flexible (parameter)
258 # system - everything else
260 if [[ $target = raspberry-pi ||
$target = raspberry-pi2 ||
$target = raspberry-pi3
]]; then
264 print
-u2 "Cannot combine GRUB with $target"
274 (( cyls
= dksz
/ heads
/ secs
))
275 if (( cyls
< (bootfssz
+ cfgfs
+ datafssz
+ 2) )); then
276 print
-u2 "Size of $tgt is $dksz, this looks fishy?"
281 if stat
-qs .
>/dev
/null
2>&1; then
282 statcmd
='stat -f %z' # BSD stat (or so we assume)
284 statcmd
='stat -c %s' # GNU stat
288 tar -xOf "$src" boot
/grub
/core.img
>"$T/core.img"
289 integer coreimgsz
=$
($statcmd "$T/core.img")
293 if (( coreimgsz
< 1024 )); then
294 print
-u2 core.img is probably too small
: $coreimgsz
298 if (( coreimgsz
> 65024 )); then
299 print
-u2 core.img is larger than
64K-512
: $coreimgsz
303 (( coreendsec
= (coreimgsz
+ 511) / 512 ))
304 if [[ $basedev = /dev
/svnd
+([0-9]) ]]; then
305 # BSD svnd0 mode: protect sector #1
308 corepatchofs
=$
((0x614))
311 corepatchofs
=$
((0x414))
313 # partition offset: at least coreendsec+1 but aligned on a multiple of secs
314 #(( partofs = ((coreendsec / secs) + 1) * secs ))
315 # we just use 2048 all the time, since some loaders are longer
317 if [[ $target = raspberry-pi ||
$target = raspberry-pi2 ||
$target = raspberry-pi3
]]; then
318 (( spartofs
= partofs
+ (100 * 2048) ))
323 (( quiet
)) ||
if (( grub
)); then
324 print Preparing MBR and GRUB2...
326 print Preparing MBR...
328 dd if=/dev
/zero of
="$T/firsttrack" count
=$partofs 2>/dev
/null
329 # add another MiB to clear the first partition
330 dd if=/dev
/zero bs
=1048576 count
=1 >>"$T/firsttrack" 2>/dev
/null
331 echo $corestartsec $coreendsec | mksh
"$ADK_TOPDIR/scripts/bootgrub.mksh" \
332 -A -g $
((cyls
- bootfssz
- cfgfs
- datafssz
)):$heads:$secs -M 1:0x83 \
333 -O $spartofs |
dd of
="$T/firsttrack" conv
=notrunc
2>/dev
/null
334 (( grub
)) && dd if="$T/core.img" of
="$T/firsttrack" conv
=notrunc \
335 seek
=$corestartsec 2>/dev
/null
336 # set partition where it can find /boot/grub
337 (( grub
)) && print
-n '\0\0\0\0' | \
338 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$corepatchofs 2>/dev
/null
340 # create cfgfs partition (mostly taken from bootgrub.mksh)
342 typeset
-Uui8 thecode
344 set -A g_code
$cyls $heads $secs
345 (( psz
= g_code
[0] * g_code
[1] * g_code
[2] ))
346 (( pofs
= (cyls
- cfgfs
) * g_code
[1] * g_code
[2] ))
347 set -A o_code
# g_code equivalent for partition offset
348 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
349 (( o_code
[1] = pofs
/ g_code
[2] ))
350 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
351 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
352 # boot flag; C/H/S offset
353 thecode
[mbrpno
++]=0x00
354 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
355 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
356 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
357 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
358 # partition type; C/H/S end
359 (( thecode
[mbrpno
++] = 0x88 ))
360 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
361 (( cylno
= g_code
[0] > 1024 ?
1023 : g_code
[0] - 1 ))
362 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
363 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
364 # partition offset, size (LBA)
365 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
366 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
367 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
368 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
369 (( pssz
= psz
- pofs
))
370 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
371 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
372 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
373 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
374 # write partition table entry
377 while (( curptr
< 16 )); do
378 ostr
=$ostr\\0${thecode[curptr++]#8#}
381 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1EE)) 2>/dev
/null
383 if (( datafssz
)); then
384 # create data partition (copy of the above :)
386 typeset
-Uui8 thecode
388 set -A g_code
$cyls $heads $secs
389 (( psz
= (cyls
- cfgfs
) * g_code
[1] * g_code
[2] ))
390 (( pofs
= (cyls
- cfgfs
- datafssz
) * g_code
[1] * g_code
[2] ))
391 set -A o_code
# g_code equivalent for partition offset
392 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
393 (( o_code
[1] = pofs
/ g_code
[2] ))
394 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
395 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
396 # boot flag; C/H/S offset
397 thecode
[mbrpno
++]=0x00
398 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
399 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
400 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
401 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
402 # partition type; C/H/S end
403 (( thecode
[mbrpno
++] = 0x83 ))
404 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
405 (( cylno
= (g_code
[0] - cfgfs
) > 1024 ?
1023 : g_code
[0] - cfgfs
- 1 ))
406 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
407 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
408 # partition offset, size (LBA)
409 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
410 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
411 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
412 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
413 (( pssz
= psz
- pofs
))
414 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
415 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
416 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
417 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
418 # write partition table entry
421 while (( curptr
< 16 )); do
422 ostr
=$ostr\\0${thecode[curptr++]#8#}
425 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1CE)) 2>/dev
/null
428 if [[ $target = raspberry-pi ||
$target = raspberry-pi2 ||
$target = raspberry-pi3
]]; then
429 # move system and data partition from #0/#1 to #1/#2
430 dd if="$T/firsttrack" bs
=1 skip
=$
((0x1BE)) count
=32 of
="$T/x" 2>/dev
/null
431 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1CE)) if="$T/x" 2>/dev
/null
432 # create boot partition (copy of the above :)
434 typeset
-Uui8 thecode
436 set -A g_code
$cyls $heads $secs
439 set -A o_code
# g_code equivalent for partition offset
440 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
441 (( o_code
[1] = pofs
/ g_code
[2] ))
442 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
443 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
444 # boot flag; C/H/S offset
445 thecode
[mbrpno
++]=0x00
446 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
447 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
448 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
449 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
450 # partition type; C/H/S end
451 (( thecode
[mbrpno
++] = 0x0B ))
452 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
453 (( cylno
= (spartofs
/ 2048) > 1024 ?
1023 : (spartofs
/ 2048) - 1 ))
454 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
455 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
456 # partition offset, size (LBA)
457 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
458 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
459 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
460 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
461 (( pssz
= psz
- pofs
))
462 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
463 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
464 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
465 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
466 # write partition table entry
469 while (( curptr
< 16 )); do
470 ostr
=$ostr\\0${thecode[curptr++]#8#}
473 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1BE)) 2>/dev
/null
478 [[ -c /dev
/arandom
]] && rnddev
=/dev
/arandom
479 dd if=$rnddev bs
=4 count
=1 2>/dev
/null | \
480 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1B8)) 2>/dev
/null
482 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1BC)) 2>/dev
/null
483 partuuid
=$
(dd if="$T/firsttrack" bs
=1 count
=4 skip
=$
((0x1B8)) 2>/dev
/null | \
484 hexdump -e '1/4 "%08x"')-0$
((syspartno
+1))
486 ((keep
)) ||
if (( datafssz
)); then
487 (( quiet
)) || print Cleaning out data partition...
488 dd if=/dev
/zero of
="$tgt" bs
=1048576 count
=1 seek
=$
((cyls
- cfgfs
- datafssz
)) > /dev
/null
2>&1
490 (( quiet
)) || print Cleaning out root partition...
491 dd if=/dev
/zero bs
=1048576 of
="$tgt" count
=1 seek
=$
((spartofs
/ 2048)) > /dev
/null
2>&1
493 (( quiet
)) ||
if (( grub
)); then
494 print Writing MBR and GRUB2 to target device... system PARTUUID
=$partuuid
496 print Writing MBR to target device... system PARTUUID
=$partuuid
498 dd if="$T/firsttrack" of
="$tgt" > /dev
/null
2>&1
500 if [[ $ostype = Linux
]]; then
505 fwdir
=$
(dirname "$src")
509 dd if="$fwdir/u-boot-sunxi-with-spl.bin" of
="$tgt" bs
=1024 seek
=8 > /dev
/null
2>&1
512 dd if="$fwdir/u-boot-spl.kwb" of
="$tgt" bs
=512 seek
=1 > /dev
/null
2>&1
515 dd if="$fwdir/SPL" of
="$tgt" bs
=1024 seek
=1 > /dev
/null
2>&1
516 dd if="$fwdir/u-boot.img" of
="$tgt" bs
=1024 seek
=69 > /dev
/null
2>&1
518 (raspberry-pi|raspberry-pi2|raspberry-pi3
)
519 (( noformat
)) || create_fs
"$bootpart" ADKBOOT vfat
523 (( noformat
)) || create_fs
"$rootpart" ADKROOT ext4
524 (( noformat
)) || tune_fs
"$rootpart"
526 (( quiet
)) || print Extracting installation archive...
527 mount_fs
"$rootpart" "$R" ext4
528 xz
-dc "$src" |
(cd "$R"; tar -xpf -)
530 if (( datafssz
)); then
531 mkdir
-m0755 "$R"/data
532 ((keep
)) || create_fs
"$datapart" ADKDATA ext4
533 ((keep
)) || tune_fs
"$datapart"
535 (raspberry-pi|raspberry-pi2|raspberry-pi3
)
536 echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc
/fstab
538 (banana-pro|solidrun-imx6|solidrun-clearfog
)
539 echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc
/fstab
545 (raspberry-pi|raspberry-pi2|raspberry-pi3
)
546 mount_fs
"$bootpart" "$B" vfat
547 for x
in "$R"/boot
/*; do
548 [[ -e "$x" ]] && mv -f "$R"/boot
/* "$B/"
551 for x
in "$fwdir"/*.dtb
; do
552 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$B/"
556 for x
in "$fwdir"/overlays
/*.dtbo
; do
557 [[ -e "$x" ]] && cp "$fwdir"/overlays
/*.dtbo
"$B/"overlays
563 for x
in "$fwdir"/*.dtb
; do
564 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
567 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
568 -n "SolidrunClearfog" \
569 -d $fwdir/boot.
script.clearfog
$R/boot
/boot.scr.uimg
572 for x
in "$fwdir"/*.dtb
; do
573 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
576 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
578 -d $fwdir/boot.
script.imx6
$R/boot
/boot.scr.uimg
581 for x
in "$fwdir"/*.dtb
; do
582 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
585 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
587 -d $fwdir/boot.
script.bpi
$R/boot
/boot.scr.uimg
592 dd if=$rnddev bs
=16 count
=1 >>etc
/.rnd
2>/dev
/null
593 (( quiet
)) || print Fixing up permissions...
596 [[ -f usr
/bin
/sudo
]] && chmod 4755 usr
/bin
/sudo
599 (( quiet
)) || print Configuring GRUB2 bootloader...
604 if (( serial
)); then
605 print serial
--unit=0 --speed=$speed
606 print terminal_output serial
607 print terminal_input serial
608 consargs
="console=ttyS0,$speed console=tty0"
610 print terminal_output console
611 print terminal_input console
612 consargs
="console=tty0"
615 print
'menuentry "GNU/Linux (OpenADK)" {'
616 linuxargs
="root=PARTUUID=$partuuid $consargs"
617 (( panicreboot
)) && linuxargs
="$linuxargs panic=$panicreboot"
618 print
"\tlinux /boot/kernel $linuxargs"
620 ) >boot
/grub
/grub.cfg
623 (( quiet
)) || print Finishing up...