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|orange-pi0|pcengines-apu|raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|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-pi0 ||
$target = raspberry-pi2 ||
$target = raspberry-pi3 ||
$target = raspberry-pi3-64
]]; 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"
196 if [[ $basedev = /dev
/loop
* ]]; then
197 (( quiet
)) || print
"${tgt} is a loop device"
201 rootpart
=${basedev}${partitionsep}1
202 datapart
=${basedev}${partitionsep}2
203 if [[ $target = raspberry-pi ||
$target = raspberry-pi0 ||
$target = raspberry-pi2 ||
$target = raspberry-pi3 ||
$target = raspberry-pi3-64
]]; then
204 bootpart
=${basedev}${partitionsep}1
205 rootpart
=${basedev}${partitionsep}2
206 datapart
=${basedev}${partitionsep}3
209 match
=\'${basedev}${partitionsep}\''+([0-9])'
211 mount
-t "$3" "$1" "$2"
217 (( quiet
)) || print
"Creating filesystem on ${1}..."
221 tune2fs
-c 0 -i 0 "$1"
227 while read -p dev rest
; do
228 eval [[ \
$dev = $match ]] ||
continue
229 print
-u2 "Block device $tgt is in use, please umount first."
233 if (( !quiet
)); then
234 print
"WARNING: This will overwrite $basedev - type Yes to continue!"
236 [[ $x = Yes
]] ||
exit 0
239 if ! T
=$
(mktemp
-d /tmp
/openadk.XXXXXXXXXX
); then
240 print
-u2 Error creating temporary directory.
243 if [[ $ostype != Darwin
]]; then
247 mkdir
-p "$R" "$B" "$D"
251 dksz
=$
(dkgetsz
"$tgt")
254 # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
255 # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
256 # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
261 # boot(raspberry) - fixed (100 MiB)
262 # cfgfs - fixed (parameter, max. 16 MiB)
263 # data - flexible (parameter)
264 # system - everything else
266 if [[ $target = raspberry-pi ||
$target = raspberry-pi0 ||
$target = raspberry-pi2 ||
$target = raspberry-pi3 ||
$target = raspberry-pi3-64
]]; then
270 print
-u2 "Cannot combine GRUB with $target"
280 (( cyls
= dksz
/ heads
/ secs
))
281 if (( cyls
< (bootfssz
+ cfgfs
+ datafssz
+ 2) )); then
282 print
-u2 "Size of $tgt is $dksz, this looks fishy?"
287 if stat
-qs .
>/dev
/null
2>&1; then
288 statcmd
='stat -f %z' # BSD stat (or so we assume)
290 statcmd
='stat -c %s' # GNU stat
294 tar -xOf "$src" boot
/grub
/core.img
>"$T/core.img"
295 integer coreimgsz
=$
($statcmd "$T/core.img")
299 if (( coreimgsz
< 1024 )); then
300 print
-u2 core.img is probably too small
: $coreimgsz
304 if (( coreimgsz
> 65024 )); then
305 print
-u2 core.img is larger than
64K-512
: $coreimgsz
309 (( coreendsec
= (coreimgsz
+ 511) / 512 ))
310 if [[ $basedev = /dev
/svnd
+([0-9]) ]]; then
311 # BSD svnd0 mode: protect sector #1
314 corepatchofs
=$
((0x614))
317 corepatchofs
=$
((0x414))
319 # partition offset: at least coreendsec+1 but aligned on a multiple of secs
320 #(( partofs = ((coreendsec / secs) + 1) * secs ))
321 # we just use 2048 all the time, since some loaders are longer
323 if [[ $target = raspberry-pi ||
$target = raspberry-pi0 ||
$target = raspberry-pi2 ||
$target = raspberry-pi3 ||
$target = raspberry-pi3-64
]]; then
324 (( spartofs
= partofs
+ (100 * 2048) ))
329 (( quiet
)) ||
if (( grub
)); then
330 print Preparing MBR and GRUB2...
332 print Preparing MBR...
334 dd if=/dev
/zero of
="$T/firsttrack" count
=$partofs 2>/dev
/null
335 # add another MiB to clear the first partition
336 dd if=/dev
/zero bs
=1048576 count
=1 >>"$T/firsttrack" 2>/dev
/null
337 echo $corestartsec $coreendsec | mksh
"$ADK_TOPDIR/scripts/bootgrub.mksh" \
338 -A -g $
((cyls
- bootfssz
- cfgfs
- datafssz
)):$heads:$secs -M 1:0x83 \
339 -O $spartofs |
dd of
="$T/firsttrack" conv
=notrunc
2>/dev
/null
340 (( grub
)) && dd if="$T/core.img" of
="$T/firsttrack" conv
=notrunc \
341 seek
=$corestartsec 2>/dev
/null
342 # set partition where it can find /boot/grub
343 (( grub
)) && print
-n '\0\0\0\0' | \
344 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$corepatchofs 2>/dev
/null
346 # create cfgfs partition (mostly taken from bootgrub.mksh)
348 typeset
-Uui8 thecode
350 set -A g_code
$cyls $heads $secs
351 (( psz
= g_code
[0] * g_code
[1] * g_code
[2] ))
352 (( pofs
= (cyls
- cfgfs
) * g_code
[1] * g_code
[2] ))
353 set -A o_code
# g_code equivalent for partition offset
354 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
355 (( o_code
[1] = pofs
/ g_code
[2] ))
356 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
357 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
358 # boot flag; C/H/S offset
359 thecode
[mbrpno
++]=0x00
360 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
361 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
362 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
363 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
364 # partition type; C/H/S end
365 (( thecode
[mbrpno
++] = 0x88 ))
366 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
367 (( cylno
= g_code
[0] > 1024 ?
1023 : g_code
[0] - 1 ))
368 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
369 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
370 # partition offset, size (LBA)
371 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
372 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
373 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
374 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
375 (( pssz
= psz
- pofs
))
376 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
377 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
378 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
379 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
380 # write partition table entry
383 while (( curptr
< 16 )); do
384 ostr
=$ostr\\0${thecode[curptr++]#8#}
387 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1EE)) 2>/dev
/null
389 if (( datafssz
)); then
390 # create data partition (copy of the above :)
392 typeset
-Uui8 thecode
394 set -A g_code
$cyls $heads $secs
395 (( psz
= (cyls
- cfgfs
) * g_code
[1] * g_code
[2] ))
396 (( pofs
= (cyls
- cfgfs
- datafssz
) * g_code
[1] * g_code
[2] ))
397 set -A o_code
# g_code equivalent for partition offset
398 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
399 (( o_code
[1] = pofs
/ g_code
[2] ))
400 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
401 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
402 # boot flag; C/H/S offset
403 thecode
[mbrpno
++]=0x00
404 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
405 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
406 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
407 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
408 # partition type; C/H/S end
409 (( thecode
[mbrpno
++] = 0x83 ))
410 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
411 (( cylno
= (g_code
[0] - cfgfs
) > 1024 ?
1023 : g_code
[0] - cfgfs
- 1 ))
412 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
413 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
414 # partition offset, size (LBA)
415 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
416 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
417 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
418 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
419 (( pssz
= psz
- pofs
))
420 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
421 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
422 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
423 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
424 # write partition table entry
427 while (( curptr
< 16 )); do
428 ostr
=$ostr\\0${thecode[curptr++]#8#}
431 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1CE)) 2>/dev
/null
434 if [[ $target = raspberry-pi ||
$target = raspberry-pi0 ||
$target = raspberry-pi2 ||
$target = raspberry-pi3 ||
$target = raspberry-pi3-64
]]; then
435 # move system and data partition from #0/#1 to #1/#2
436 dd if="$T/firsttrack" bs
=1 skip
=$
((0x1BE)) count
=32 of
="$T/x" 2>/dev
/null
437 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1CE)) if="$T/x" 2>/dev
/null
438 # create boot partition (copy of the above :)
440 typeset
-Uui8 thecode
442 set -A g_code
$cyls $heads $secs
445 set -A o_code
# g_code equivalent for partition offset
446 (( o_code
[2] = pofs
% g_code
[2] + 1 ))
447 (( o_code
[1] = pofs
/ g_code
[2] ))
448 (( o_code
[0] = o_code
[1] / g_code
[1] + 1 ))
449 (( o_code
[1] = o_code
[1] % g_code
[1] + 1 ))
450 # boot flag; C/H/S offset
451 thecode
[mbrpno
++]=0x00
452 (( thecode
[mbrpno
++] = o_code
[1] - 1 ))
453 (( cylno
= o_code
[0] > 1024 ?
1023 : o_code
[0] - 1 ))
454 (( thecode
[mbrpno
++] = o_code
[2] |
((cylno
& 0x0300) >> 2) ))
455 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
456 # partition type; C/H/S end
457 (( thecode
[mbrpno
++] = 0x0B ))
458 (( thecode
[mbrpno
++] = g_code
[1] - 1 ))
459 (( cylno
= (spartofs
/ 2048) > 1024 ?
1023 : (spartofs
/ 2048) - 1 ))
460 (( thecode
[mbrpno
++] = g_code
[2] |
((cylno
& 0x0300) >> 2) ))
461 (( thecode
[mbrpno
++] = cylno
& 0x00FF ))
462 # partition offset, size (LBA)
463 (( thecode
[mbrpno
++] = pofs
& 0xFF ))
464 (( thecode
[mbrpno
++] = (pofs
>> 8) & 0xFF ))
465 (( thecode
[mbrpno
++] = (pofs
>> 16) & 0xFF ))
466 (( thecode
[mbrpno
++] = (pofs
>> 24) & 0xFF ))
467 (( pssz
= psz
- pofs
))
468 (( thecode
[mbrpno
++] = pssz
& 0xFF ))
469 (( thecode
[mbrpno
++] = (pssz
>> 8) & 0xFF ))
470 (( thecode
[mbrpno
++] = (pssz
>> 16) & 0xFF ))
471 (( thecode
[mbrpno
++] = (pssz
>> 24) & 0xFF ))
472 # write partition table entry
475 while (( curptr
< 16 )); do
476 ostr
=$ostr\\0${thecode[curptr++]#8#}
479 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1BE)) 2>/dev
/null
484 [[ -c /dev
/arandom
]] && rnddev
=/dev
/arandom
485 dd if=$rnddev bs
=4 count
=1 2>/dev
/null | \
486 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1B8)) 2>/dev
/null
488 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1BC)) 2>/dev
/null
489 partuuid
=$
(dd if="$T/firsttrack" bs
=1 count
=4 skip
=$
((0x1B8)) 2>/dev
/null | \
490 hexdump -e '1/4 "%08x"')-0$
((syspartno
+1))
492 ((keep
)) ||
if (( datafssz
)); then
493 (( quiet
)) || print Cleaning out data partition...
494 dd if=/dev
/zero of
="$tgt" bs
=1048576 count
=1 seek
=$
((cyls
- cfgfs
- datafssz
)) > /dev
/null
2>&1
496 (( quiet
)) || print Cleaning out root partition...
497 dd if=/dev
/zero bs
=1048576 of
="$tgt" count
=1 seek
=$
((spartofs
/ 2048)) > /dev
/null
2>&1
499 (( quiet
)) ||
if (( grub
)); then
500 print Writing MBR and GRUB2 to target device... system PARTUUID
=$partuuid
502 print Writing MBR to target device... system PARTUUID
=$partuuid
504 dd if="$T/firsttrack" of
="$tgt" > /dev
/null
2>&1
506 if [[ $ostype = Linux
]]; then
511 fwdir
=$
(dirname "$src")
514 (banana-pro|orange-pi0
)
515 dd if="$fwdir/u-boot-sunxi-with-spl.bin" of
="$tgt" bs
=1024 seek
=8 > /dev
/null
2>&1
518 dd if="$fwdir/u-boot-spl.kwb" of
="$tgt" bs
=512 seek
=1 > /dev
/null
2>&1
521 dd if="$fwdir/SPL" of
="$tgt" bs
=1024 seek
=1 > /dev
/null
2>&1
522 dd if="$fwdir/u-boot.img" of
="$tgt" bs
=1024 seek
=69 > /dev
/null
2>&1
524 (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64
)
525 (( noformat
)) || create_fs
"$bootpart" ADKBOOT vfat
529 (( noformat
)) || create_fs
"$rootpart" ADKROOT ext4
530 (( noformat
)) || tune_fs
"$rootpart"
532 (( quiet
)) || print Extracting installation archive...
533 mount_fs
"$rootpart" "$R" ext4
534 xz
-dc "$src" |
(cd "$R"; tar -xpf -)
536 if (( datafssz
)); then
537 mkdir
-m0755 "$R"/data
538 ((keep
)) || create_fs
"$datapart" ADKDATA ext4
539 ((keep
)) || tune_fs
"$datapart"
541 (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64
)
542 echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc
/fstab
544 (banana-pro|orange-pi0|solidrun-imx6|solidrun-clearfog
)
545 echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc
/fstab
551 (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64
)
552 mount_fs
"$bootpart" "$B" vfat
553 for x
in "$R"/boot
/*; do
554 [[ -e "$x" ]] && mv -f "$R"/boot
/* "$B/"
557 for x
in "$fwdir"/*.dtb
; do
558 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$B/"
561 # use static dtoverlay, rename to *.dtb
563 for x
in "$fwdir"/overlays
/*.dtbo
; do
564 y
=$
(basename ${x} .dtbo
)
565 [[ -e "$x" ]] && cp "$fwdir"/overlays
/${y}.dtbo
"$B/"overlays
/${y}.dtb
570 for x
in "$fwdir"/*.dtb
; do
571 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
574 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
575 -n "SolidrunClearfog" \
576 -d $fwdir/boot.
script.clearfog
$R/boot
/boot.scr.uimg
579 for x
in "$fwdir"/*.dtb
; do
580 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
583 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
585 -d $fwdir/boot.
script.imx6
$R/boot
/boot.scr.uimg
588 for x
in "$fwdir"/*.dtb
; do
589 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
592 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
594 -d $fwdir/boot.
script.opi
$R/boot
/boot.scr.uimg
597 for x
in "$fwdir"/*.dtb
; do
598 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
601 mkimage
-A arm
-O linux
-T script -C none
-a 0 -e 0 \
603 -d $fwdir/boot.
script.bpi
$R/boot
/boot.scr.uimg
608 dd if=$rnddev bs
=16 count
=1 >>etc
/.rnd
2>/dev
/null
609 (( quiet
)) || print Fixing up permissions...
612 [[ -f usr
/bin
/sudo
]] && chmod 4755 usr
/bin
/sudo
615 (( quiet
)) || print Configuring GRUB2 bootloader...
620 if (( serial
)); then
621 print serial
--unit=0 --speed=$speed
622 print terminal_output serial
623 print terminal_input serial
624 consargs
="console=ttyS0,$speed console=tty0"
626 print terminal_output console
627 print terminal_input console
628 consargs
="console=tty0"
631 print
'menuentry "GNU/Linux (OpenADK)" {'
632 linuxargs
="root=PARTUUID=$partuuid $consargs"
633 (( panicreboot
)) && linuxargs
="$linuxargs panic=$panicreboot"
634 print
"\tlinux /boot/kernel $linuxargs"
636 ) >boot
/grub
/grub.cfg
639 (( quiet
)) || print Finishing up...
642 if (( datafssz
)); then