| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- # Install method "ubootenv" (Banana Pi M2 Berry/Ultra), sourced by
- # install-kernel.sh.
- #
- # These boards boot from a hand-written U-Boot environment in /boot/uboot.env
- # (FAT, mmc 0:1): fatload a fixed uImage and DTB name, bootm, and on marlena
- # also apply an LED overlay. The environment is read, never changed:
- # - every release goes to /boot/<subdir>/<release>/ (uImage, DTBs, overlays,
- # config, System.map), so older ones stay loadable by hand;
- # - the one to boot is copied over the files the environment loads
- # (load_kernel, load_dt), after the previous ones were kept as
- # uImage-<old release> / <dtb>.<old release> unless they are already in
- # /boot/<subdir>/<old release>/;
- # - modules to /lib/modules/<release>/.
- # /boot is noauto on these boards: it is mounted for the install and unmounted
- # again if it was not mounted before.
- #
- # Safety checks (ssh): Gentoo builds running (screen "profile23", emerge) stop
- # the install (FORCE_BUSY=1 overrides); a FAT dirty flag on /boot stops it
- # (FAT_DIRTY_OK=1 overrides); marlena's overlay must apply to the new DTB.
- # Settings: BOOT_DTB (our DTB to boot instead of the BOOT_DTB_MAP guess).
- # Provided by install-kernel.sh: TARGET, TARBALL, RELEASE, ROOT or HOST, trun,
- # tput_file.
- SUB="boot/$BOOT_SUBDIR"
- NEW="$SUB/$RELEASE"
- TMP="$(mktemp -d "${TMPDIR:-/tmp}/install-$BOARD.XXXXXX")"
- MOUNTED_BOOT=
- cleanup() {
- if [ -n "$MOUNTED_BOOT" ]; then
- info "unmounting /boot again"
- trun 'sync; umount /boot' || echo "WARNING: could not unmount /boot on the target" >&2
- fi
- rm -rf "$TMP"
- }
- trap cleanup EXIT
- texists() { trun "test -e '$1'"; }
- # ---- 1. nothing else running that a reboot or a full /boot would hurt --------
- if [ -n "$HOST" ]; then
- busy="$(trun 'screen -ls 2>/dev/null | grep -E "[.]profile23[[:space:]]"; pgrep -a -x emerge' || true)"
- if [ -n "$busy" ]; then
- echo "$busy" | sed 's/^/ /' >&2
- [ "${FORCE_BUSY:-0}" = 1 ] ||
- die "Gentoo builds are running on $HOST (above); install after they are done (FORCE_BUSY=1 overrides)"
- echo "WARNING: builds running on $HOST; installing anyway (FORCE_BUSY=1)" >&2
- fi
- fi
- # ---- 2. /boot: the FAT partition U-Boot reads -----------------------------------
- # Linux sets the FAT dirty flag itself while a volume is mounted read-write and
- # restores it on unmount, so the flag only tells something about an unmounted
- # volume. fat_dirty DEVICE: exit 0 if the flag is set.
- fat_dirty() {
- trun "dd if='$1' bs=512 count=1 status=none" > "$TMP/bootsector"
- python3 -I -c '
- import sys
- b = open(sys.argv[1], "rb").read()
- fat32 = b[82:90] == b"FAT32 "
- sys.exit(0 if b[0x41 if fat32 else 0x25] & 1 else 1)' "$TMP/bootsector"
- }
- fat_dirty_stop() {
- [ "${FAT_DIRTY_OK:-0}" = 1 ] && { echo "WARNING: $1; going on (FAT_DIRTY_OK=1)" >&2; return 0; }
- die "$1. Check it first: umount /boot; fsck.vfat -a $2 (sys-fs/dosfstools), or check the card on the PC. FAT_DIRTY_OK=1 overrides."
- }
- if [ -n "$HOST" ]; then
- if trun 'mountpoint -q /boot'; then
- bdev="$(trun 'findmnt -no SOURCE /boot')"
- if trun "dmesg | grep -q 'FAT-fs (${bdev#/dev/}): Volume was not properly unmounted'"; then
- fat_dirty_stop "/boot ($bdev) was not cleanly unmounted before it was mounted (dmesg)" "$bdev"
- fi
- else
- bdev="$(trun 'findmnt -sno SOURCE /boot')"
- [ -n "$bdev" ] || die "/boot is neither mounted nor in fstab on $HOST"
- if fat_dirty "$bdev"; then
- fat_dirty_stop "the FAT dirty flag is set on $bdev (/boot)" "$bdev"
- fi
- info "mounting /boot ($bdev)"
- trun 'mount /boot'
- MOUNTED_BOOT=1
- fi
- else
- findmnt -no TARGET "$ROOT/boot" | grep -qx "$ROOT/boot" ||
- die "$ROOT/boot is not a mount point; mount the card's first (FAT) partition there"
- bdev="$(findmnt -no SOURCE "$ROOT/boot")"
- fi
- if [ -n "$ROOT" ]; then bfs="$(findmnt -no FSTYPE "$ROOT/boot")"; else bfs="$(trun 'findmnt -no FSTYPE /boot')"; fi
- [ "$bfs" = vfat ] || die "/boot on the target is $bfs, not the FAT partition U-Boot reads"
- info "/boot: $bdev"
- # ---- 3. what the environment loads ------------------------------------------------
- texists "boot/$UBOOT_ENV_FILE" || die "no /boot/$UBOOT_ENV_FILE on the target; this install method needs its environment"
- trun "cat 'boot/$UBOOT_ENV_FILE'" > "$TMP/env"
- python3 -I "$TOP/lib/uboot-env.py" vars "$TMP/env" > "$TMP/env.txt" ||
- die "/boot/$UBOOT_ENV_FILE has no valid CRC; U-Boot is not using it"
- envvar() { sed -n "s/^$1=//p" "$TMP/env.txt" | head -1; }
- # last word of the fatload command in a variable
- envfile() { envvar "$1" | sed -n 's/.*fatload mmc 0:1 [^ ;]* \([^ ;]*\).*/\1/p'; }
- distro="$(envvar distro_bootcmd)"
- kfile="$(envfile load_kernel)"
- dfile="$(envfile load_dt)"
- case "$distro" in *"run load_kernel"*) ;; *) die "distro_bootcmd ($distro) does not run load_kernel" ;; esac
- case "$distro" in *"run load_dt"*) ;; *) die "distro_bootcmd ($distro) does not run load_dt" ;; esac
- [ -n "$kfile" ] && [ -n "$dfile" ] || die "could not find the kernel/DTB names in load_kernel/load_dt"
- case "$(envvar boot_kernel)" in bootm*) ;; *) die "boot_kernel ($(envvar boot_kernel)) is not bootm; our image is a uImage" ;; esac
- ofile=
- case "$distro" in *"run load_overlay"*) ofile="$(envvar load_overlay | grep -o '[^ ;]*\.dtbo' | head -1)" ;; esac
- info "environment loads: kernel /boot/$kfile, DTB /boot/$dfile${ofile:+, overlay /boot/$ofile}"
- info "bootargs: $(envvar bootargs)"
- # Our DTB for this board: BOOT_DTB, or the first BOOT_DTB_MAP pattern that
- # matches the name the environment loads.
- dtb="$BOOT_DTB"
- if [ -z "$dtb" ]; then
- for m in $BOOT_DTB_MAP; do
- # shellcheck disable=SC2254 # the map holds glob patterns
- case "$dfile" in ${m%%:*}) dtb="${m#*:}"; break ;; esac
- done
- fi
- [ -n "$dtb" ] || die "don't know which DTB replaces $dfile; set BOOT_DTB to one of: $BOARD_DTBS"
- case " $BOARD_DTBS " in *" $dtb "*) ;; *) die "BOOT_DTB=$dtb is not one of ours ($BOARD_DTBS)" ;; esac
- info "booting $dtb (as /boot/$dfile)"
- # ---- 4. unpack the boot files here, test the overlay --------------------------
- tar xzf "$TARBALL" -C "$TMP" "$NEW"
- [ -f "$TMP/$NEW/uImage" ] && [ -f "$TMP/$NEW/$dtb" ] || die "$TARBALL lacks uImage or $dtb"
- if [ -n "$ofile" ]; then
- texists "boot/$ofile" || die "the environment applies /boot/$ofile, which is missing; boot would fail"
- trun "cat 'boot/$ofile'" > "$TMP/overlay.dtbo"
- fdtoverlay -i "$TMP/$NEW/$dtb" -o "$TMP/overlay-test.dtb" "$TMP/overlay.dtbo" ||
- die "/boot/$ofile does not apply to $dtb; a failed 'fdt apply' can break the boot. Remove 'run load_overlay;' from distro_bootcmd first"
- info "/boot/$ofile applies to $dtb"
- fi
- # ---- 5. warnings about the running system -----------------------------------------
- # Working udev (kernel >= 5.10) names DT ethernet by its alias: end0, not eth0.
- if trun "grep -qE '^config_eth[0-9]+=' etc/conf.d/net 2>/dev/null" &&
- ! grep -q 'net.ifnames=0' <<<"$(envvar bootargs)" &&
- ! trun 'test -e etc/udev/rules.d/80-net-setup-link.rules || ls etc/systemd/network/*.link >/dev/null 2>&1'; then
- cat >&2 <<-EOF
- WARNING: /etc/conf.d/net configures eth0, but with this kernel udev works again and
- will most likely rename the interface to end0 (as on banana-backup), leaving
- the network unconfigured. Before rebooting, either rename the config to end0
- (config_end0, mac_end0, routes_end0, /etc/init.d/net.end0 in the runlevel) or
- keep the old name: ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules
- EOF
- fi
- # ---- 6. space on /boot --------------------------------------------------------------
- need_kb=$(( $(du -sk "$TMP/$NEW" | cut -f1) + $(du -k "$TMP/$NEW/uImage" | cut -f1) + 1024 ))
- free_kb="$(trun 'df -Pk boot | awk "NR==2 {print \$4}"')"
- [ "$free_kb" -gt "$need_kb" ] || die "/boot has $free_kb KiB free, $need_kb KiB needed; remove old releases from /boot/$BOOT_SUBDIR/"
- # ---- 7. keep what boots now ------------------------------------------------------------
- # The uImage header holds the release ("Linux-4.18.0-k4be") at offset 32.
- old_rel=
- if texists "boot/$kfile"; then
- old_rel="$(trun "dd if='boot/$kfile' bs=32 skip=1 count=1 status=none" | tr -d '\0')"
- case "$old_rel" in
- Linux-*) old_rel="${old_rel#Linux-}" ;;
- *) old_rel="unknown-$(date +%Y%m%d)" ;;
- esac
- old_rel="${old_rel//[^A-Za-z0-9._+-]/_}"
- fi
- roll_k=; roll_d=
- if [ -n "$old_rel" ] && [ "$old_rel" != "$RELEASE" ]; then
- if texists "$SUB/$old_rel/uImage"; then
- roll_k="$BOOT_SUBDIR/$old_rel/uImage"
- else
- texists "boot/$kfile-$old_rel" || trun "cp 'boot/$kfile' 'boot/$kfile-$old_rel'"
- roll_k="$kfile-$old_rel"
- fi
- if texists "boot/$dfile"; then
- if texists "$SUB/$old_rel/$dtb" && trun "cmp -s 'boot/$dfile' '$SUB/$old_rel/$dtb'"; then
- roll_d="$BOOT_SUBDIR/$old_rel/$dtb"
- else
- texists "boot/$dfile.$old_rel" || trun "cp 'boot/$dfile' 'boot/$dfile.$old_rel'"
- roll_d="$dfile.$old_rel"
- fi
- fi
- info "kept the current kernel ($old_rel): /boot/$roll_k, /boot/$roll_d"
- fi
- # Reinstalling the release that is already in place: the way back is the
- # newest older release in /boot/<subdir>/, else the newest kept original.
- if [ -z "$roll_k" ]; then
- prev="$(trun "ls -1 '$SUB' 2>/dev/null" | grep -vx "$RELEASE" | sort -V | tail -1 || true)"
- if [ -n "$prev" ]; then
- roll_k="$BOOT_SUBDIR/$prev/uImage"; roll_d="$BOOT_SUBDIR/$prev/$dtb"
- else
- prev="$(trun 'ls -1 boot' | sed -n "s/^$kfile-//p" | sort -V | tail -1 || true)"
- if [ -n "$prev" ]; then roll_k="$kfile-$prev"; roll_d="$dfile.$prev"; fi
- fi
- fi
- # ---- 8. install -----------------------------------------------------------------------------
- info "copying /$NEW/"
- trun "mkdir -p '$NEW/overlay'"
- (cd "$TMP" && find "$NEW" -type f) | while read -r f; do
- tput_file "$f" < "$TMP/$f"
- done
- info "modules to /lib/modules/$RELEASE/"
- if [ -n "$HOST" ]; then
- ssh "$HOST" "tar --keep-directory-symlink --no-same-owner -xzf - -C / lib" < "$TARBALL"
- else
- for p in lib lib/modules "lib/modules/$RELEASE"; do
- r="$(realpath -m -- "$ROOT/$p")"
- case "$r" in "$ROOT"/*) ;; *) die "/$p on the target resolves to $r, outside $ROOT; not writing there" ;; esac
- done
- tar --keep-directory-symlink -xzf "$TARBALL" -C "$ROOT" lib
- fi
- trun "sync"
- info "switching /boot/$kfile and /boot/$dfile to $RELEASE"
- trun "cp '$NEW/uImage' 'boot/$kfile.new' && mv -f 'boot/$kfile.new' 'boot/$kfile'"
- trun "cp '$NEW/$dtb' 'boot/$dfile.new' && mv -f 'boot/$dfile.new' 'boot/$dfile'"
- trun "sync"
- # verify what U-Boot will read
- want="$(cd "$TMP/$NEW" && md5sum uImage "$dtb" | cut -d' ' -f1)"
- have="$(trun "md5sum 'boot/$kfile' 'boot/$dfile'" | cut -d' ' -f1)"
- [ "$want" = "$have" ] || die "/boot/$kfile or /boot/$dfile does not match the build after copying"
- info "verified: /boot/$kfile and /boot/$dfile are $RELEASE"
- echo
- info "installed $RELEASE ($BOARD_NAME). Not rebooted."
- info "releases in /boot/$BOOT_SUBDIR/: $(trun "ls -1 '$SUB'" | sort -V | tr '\n' ' ')"
- cat <<EOF
- Rollback from the U-Boot prompt (serial console, stop autoboot):
- fatload mmc 0:1 0x47000000 ${roll_k:-<old uImage>}
- fatload mmc 0:1 0x43000000 ${roll_d:-<old dtb>}
- EOF
- [ -z "$ofile" ] || echo " fdt addr 0x43000000; fdt resize; fatload mmc 0:1 0x4300F000 $ofile; fdt apply 0x4300F000"
- cat <<EOF
- bootm 0x47000000 - 0x43000000
- then, once booted, copy the old files back over /boot/$kfile and /boot/$dfile.
- EOF
|