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-
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|solidrun-imx6|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
]]; 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
]]; then
203 match
=\'${basedev}\''+([0-9])'
205 mount
-t "$3" "$1" "$2"
211 (( quiet
)) || print
"Creating filesystem on ${1}..."
216 tune2fs
-c 0 -i 0 "$1"
222 while read -p dev rest
; do
223 eval [[ \
$dev = $match ]] ||
continue
224 print
-u2 "Block device $tgt is in use, please umount first."
228 if (( !quiet
)); then
229 print
"WARNING: This will overwrite $basedev - type Yes to continue!"
231 [[ $x = Yes
]] ||
exit 0
234 if ! T
=$
(mktemp
-d /tmp
/openadk.XXXXXXXXXX
); then
235 print
-u2 Error creating temporary directory.
238 if [[ $ostype != Darwin
]]; then
242 mkdir
-p "$R" "$B" "$D"
246 dksz
=$
(dkgetsz
"$tgt")
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)
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
265 print
-u2 "Cannot combine GRUB with $target"
275 (( cyls
= dksz
/ heads
/ secs
))
276 if (( cyls
< (bootfssz
+ cfgfs
+ datafssz
+ 2) )); then
277 print
-u2 "Size of $tgt is $dksz, this looks fishy?"
282 if stat
-qs .
>/dev
/null
2>&1; then
283 statcmd
='stat -f %z' # BSD stat (or so we assume)
285 statcmd
='stat -c %s' # GNU stat
289 tar -xOf "$src" boot
/grub
/core.img
>"$T/core.img"
290 integer coreimgsz
=$
($statcmd "$T/core.img")
294 if (( coreimgsz
< 1024 )); then
295 print
-u2 core.img is probably too small
: $coreimgsz
299 if (( coreimgsz
> 65024 )); then
300 print
-u2 core.img is larger than
64K-512
: $coreimgsz
304 (( coreendsec
= (coreimgsz
+ 511) / 512 ))
305 if [[ $basedev = /dev
/svnd
+([0-9]) ]]; then
306 # BSD svnd0 mode: protect sector #1
309 corepatchofs
=$
((0x614))
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
318 if [[ $target = raspberry-pi ||
$target = raspberry-pi2
]]; then
319 (( spartofs
= partofs
+ (100 * 2048) ))
324 (( quiet
)) ||
if (( grub
)); then
325 print Preparing MBR and GRUB2...
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)
343 typeset
-Uui8 thecode
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
378 while (( curptr
< 16 )); do
379 ostr
=$ostr\\0${thecode[curptr++]#8#}
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 :)
387 typeset
-Uui8 thecode
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
422 while (( curptr
< 16 )); do
423 ostr
=$ostr\\0${thecode[curptr++]#8#}
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 :)
435 typeset
-Uui8 thecode
437 set -A g_code
$cyls $heads $secs
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
470 while (( curptr
< 16 )); do
471 ostr
=$ostr\\0${thecode[curptr++]#8#}
474 dd of
="$T/firsttrack" conv
=notrunc bs
=1 seek
=$
((0x1BE)) 2>/dev
/null
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
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
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")
505 dd if="$fwdir/u-boot-sunxi-with-spl.bin" of
="$tgt" bs
=1024 seek
=8 > /dev
/null
2>&1
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"
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
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/"
544 for x
in "$fwdir"/*.dtb
; do
545 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$B/"
549 for x
in "$B/"*-overlay.dtb
; do
550 [[ -e "$x" ]] && mv "$B/"*-overlay.dtb
"$B/"overlays
556 for x
in "$fwdir"/*.dtb
; do
557 [[ -e "$x" ]] && cp "$fwdir"/*.dtb
"$R/boot/"
564 dd if=$rnddev bs
=16 count
=1 >>etc
/.rnd
2>/dev
/null
565 (( quiet
)) || print Fixing up permissions...
568 [[ -f usr
/bin
/sudo
]] && chmod 4755 usr
/bin
/sudo
571 (( quiet
)) || print Configuring GRUB2 bootloader...
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"
582 print terminal_output console
583 print terminal_input console
584 consargs
="console=tty0"
587 print
'menuentry "GNU/Linux (OpenADK)" {'
588 linuxargs
="root=PARTUUID=$partuuid $consargs"
589 (( panicreboot
)) && linuxargs
="$linuxargs panic=$panicreboot"
590 print
"\tlinux /boot/kernel $linuxargs"
592 ) >boot
/grub
/grub.cfg
595 (( quiet
)) || print Finishing up...