install-ubootenv.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Install method "ubootenv" (Banana Pi M2 Berry/Ultra), sourced by
  2. # install-kernel.sh.
  3. #
  4. # These boards boot from a hand-written U-Boot environment in /boot/uboot.env
  5. # (FAT, mmc 0:1): fatload a fixed uImage and DTB name, bootm, and on marlena
  6. # also apply an LED overlay. The environment is read, never changed:
  7. # - every release goes to /boot/<subdir>/<release>/ (uImage, DTBs, overlays,
  8. # config, System.map), so older ones stay loadable by hand;
  9. # - the one to boot is copied over the files the environment loads
  10. # (load_kernel, load_dt), after the previous ones were kept as
  11. # uImage-<old release> / <dtb>.<old release> unless they are already in
  12. # /boot/<subdir>/<old release>/;
  13. # - modules to /lib/modules/<release>/.
  14. # /boot is noauto on these boards: it is mounted for the install and unmounted
  15. # again if it was not mounted before.
  16. #
  17. # Safety checks (ssh): Gentoo builds running (screen "profile23", emerge) stop
  18. # the install (FORCE_BUSY=1 overrides); a FAT dirty flag on /boot stops it
  19. # (FAT_DIRTY_OK=1 overrides); marlena's overlay must apply to the new DTB.
  20. # Settings: BOOT_DTB (our DTB to boot instead of the BOOT_DTB_MAP guess).
  21. # Provided by install-kernel.sh: TARGET, TARBALL, RELEASE, ROOT or HOST, trun,
  22. # tput_file.
  23. SUB="boot/$BOOT_SUBDIR"
  24. NEW="$SUB/$RELEASE"
  25. TMP="$(mktemp -d "${TMPDIR:-/tmp}/install-$BOARD.XXXXXX")"
  26. MOUNTED_BOOT=
  27. cleanup() {
  28. if [ -n "$MOUNTED_BOOT" ]; then
  29. info "unmounting /boot again"
  30. trun 'sync; umount /boot' || echo "WARNING: could not unmount /boot on the target" >&2
  31. fi
  32. rm -rf "$TMP"
  33. }
  34. trap cleanup EXIT
  35. texists() { trun "test -e '$1'"; }
  36. # ---- 1. nothing else running that a reboot or a full /boot would hurt --------
  37. if [ -n "$HOST" ]; then
  38. busy="$(trun 'screen -ls 2>/dev/null | grep -E "[.]profile23[[:space:]]"; pgrep -a -x emerge' || true)"
  39. if [ -n "$busy" ]; then
  40. echo "$busy" | sed 's/^/ /' >&2
  41. [ "${FORCE_BUSY:-0}" = 1 ] ||
  42. die "Gentoo builds are running on $HOST (above); install after they are done (FORCE_BUSY=1 overrides)"
  43. echo "WARNING: builds running on $HOST; installing anyway (FORCE_BUSY=1)" >&2
  44. fi
  45. fi
  46. # ---- 2. /boot: the FAT partition U-Boot reads -----------------------------------
  47. # Linux sets the FAT dirty flag itself while a volume is mounted read-write and
  48. # restores it on unmount, so the flag only tells something about an unmounted
  49. # volume. fat_dirty DEVICE: exit 0 if the flag is set.
  50. fat_dirty() {
  51. trun "dd if='$1' bs=512 count=1 status=none" > "$TMP/bootsector"
  52. python3 -I -c '
  53. import sys
  54. b = open(sys.argv[1], "rb").read()
  55. fat32 = b[82:90] == b"FAT32 "
  56. sys.exit(0 if b[0x41 if fat32 else 0x25] & 1 else 1)' "$TMP/bootsector"
  57. }
  58. fat_dirty_stop() {
  59. [ "${FAT_DIRTY_OK:-0}" = 1 ] && { echo "WARNING: $1; going on (FAT_DIRTY_OK=1)" >&2; return 0; }
  60. 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."
  61. }
  62. if [ -n "$HOST" ]; then
  63. if trun 'mountpoint -q /boot'; then
  64. bdev="$(trun 'findmnt -no SOURCE /boot')"
  65. if trun "dmesg | grep -q 'FAT-fs (${bdev#/dev/}): Volume was not properly unmounted'"; then
  66. fat_dirty_stop "/boot ($bdev) was not cleanly unmounted before it was mounted (dmesg)" "$bdev"
  67. fi
  68. else
  69. bdev="$(trun 'findmnt -sno SOURCE /boot')"
  70. [ -n "$bdev" ] || die "/boot is neither mounted nor in fstab on $HOST"
  71. if fat_dirty "$bdev"; then
  72. fat_dirty_stop "the FAT dirty flag is set on $bdev (/boot)" "$bdev"
  73. fi
  74. info "mounting /boot ($bdev)"
  75. trun 'mount /boot'
  76. MOUNTED_BOOT=1
  77. fi
  78. else
  79. findmnt -no TARGET "$ROOT/boot" | grep -qx "$ROOT/boot" ||
  80. die "$ROOT/boot is not a mount point; mount the card's first (FAT) partition there"
  81. bdev="$(findmnt -no SOURCE "$ROOT/boot")"
  82. fi
  83. if [ -n "$ROOT" ]; then bfs="$(findmnt -no FSTYPE "$ROOT/boot")"; else bfs="$(trun 'findmnt -no FSTYPE /boot')"; fi
  84. [ "$bfs" = vfat ] || die "/boot on the target is $bfs, not the FAT partition U-Boot reads"
  85. info "/boot: $bdev"
  86. # ---- 3. what the environment loads ------------------------------------------------
  87. texists "boot/$UBOOT_ENV_FILE" || die "no /boot/$UBOOT_ENV_FILE on the target; this install method needs its environment"
  88. trun "cat 'boot/$UBOOT_ENV_FILE'" > "$TMP/env"
  89. python3 -I "$TOP/lib/uboot-env.py" vars "$TMP/env" > "$TMP/env.txt" ||
  90. die "/boot/$UBOOT_ENV_FILE has no valid CRC; U-Boot is not using it"
  91. envvar() { sed -n "s/^$1=//p" "$TMP/env.txt" | head -1; }
  92. # last word of the fatload command in a variable
  93. envfile() { envvar "$1" | sed -n 's/.*fatload mmc 0:1 [^ ;]* \([^ ;]*\).*/\1/p'; }
  94. distro="$(envvar distro_bootcmd)"
  95. kfile="$(envfile load_kernel)"
  96. dfile="$(envfile load_dt)"
  97. case "$distro" in *"run load_kernel"*) ;; *) die "distro_bootcmd ($distro) does not run load_kernel" ;; esac
  98. case "$distro" in *"run load_dt"*) ;; *) die "distro_bootcmd ($distro) does not run load_dt" ;; esac
  99. [ -n "$kfile" ] && [ -n "$dfile" ] || die "could not find the kernel/DTB names in load_kernel/load_dt"
  100. case "$(envvar boot_kernel)" in bootm*) ;; *) die "boot_kernel ($(envvar boot_kernel)) is not bootm; our image is a uImage" ;; esac
  101. ofile=
  102. case "$distro" in *"run load_overlay"*) ofile="$(envvar load_overlay | grep -o '[^ ;]*\.dtbo' | head -1)" ;; esac
  103. info "environment loads: kernel /boot/$kfile, DTB /boot/$dfile${ofile:+, overlay /boot/$ofile}"
  104. info "bootargs: $(envvar bootargs)"
  105. # Our DTB for this board: BOOT_DTB, or the first BOOT_DTB_MAP pattern that
  106. # matches the name the environment loads.
  107. dtb="$BOOT_DTB"
  108. if [ -z "$dtb" ]; then
  109. for m in $BOOT_DTB_MAP; do
  110. # shellcheck disable=SC2254 # the map holds glob patterns
  111. case "$dfile" in ${m%%:*}) dtb="${m#*:}"; break ;; esac
  112. done
  113. fi
  114. [ -n "$dtb" ] || die "don't know which DTB replaces $dfile; set BOOT_DTB to one of: $BOARD_DTBS"
  115. case " $BOARD_DTBS " in *" $dtb "*) ;; *) die "BOOT_DTB=$dtb is not one of ours ($BOARD_DTBS)" ;; esac
  116. info "booting $dtb (as /boot/$dfile)"
  117. # ---- 4. unpack the boot files here, test the overlay --------------------------
  118. tar xzf "$TARBALL" -C "$TMP" "$NEW"
  119. [ -f "$TMP/$NEW/uImage" ] && [ -f "$TMP/$NEW/$dtb" ] || die "$TARBALL lacks uImage or $dtb"
  120. if [ -n "$ofile" ]; then
  121. texists "boot/$ofile" || die "the environment applies /boot/$ofile, which is missing; boot would fail"
  122. trun "cat 'boot/$ofile'" > "$TMP/overlay.dtbo"
  123. fdtoverlay -i "$TMP/$NEW/$dtb" -o "$TMP/overlay-test.dtb" "$TMP/overlay.dtbo" ||
  124. die "/boot/$ofile does not apply to $dtb; a failed 'fdt apply' can break the boot. Remove 'run load_overlay;' from distro_bootcmd first"
  125. info "/boot/$ofile applies to $dtb"
  126. fi
  127. # ---- 5. warnings about the running system -----------------------------------------
  128. # Working udev (kernel >= 5.10) names DT ethernet by its alias: end0, not eth0.
  129. if trun "grep -qE '^config_eth[0-9]+=' etc/conf.d/net 2>/dev/null" &&
  130. ! grep -q 'net.ifnames=0' <<<"$(envvar bootargs)" &&
  131. ! trun 'test -e etc/udev/rules.d/80-net-setup-link.rules || ls etc/systemd/network/*.link >/dev/null 2>&1'; then
  132. cat >&2 <<-EOF
  133. WARNING: /etc/conf.d/net configures eth0, but with this kernel udev works again and
  134. will most likely rename the interface to end0 (as on banana-backup), leaving
  135. the network unconfigured. Before rebooting, either rename the config to end0
  136. (config_end0, mac_end0, routes_end0, /etc/init.d/net.end0 in the runlevel) or
  137. keep the old name: ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules
  138. EOF
  139. fi
  140. # ---- 6. space on /boot --------------------------------------------------------------
  141. need_kb=$(( $(du -sk "$TMP/$NEW" | cut -f1) + $(du -k "$TMP/$NEW/uImage" | cut -f1) + 1024 ))
  142. free_kb="$(trun 'df -Pk boot | awk "NR==2 {print \$4}"')"
  143. [ "$free_kb" -gt "$need_kb" ] || die "/boot has $free_kb KiB free, $need_kb KiB needed; remove old releases from /boot/$BOOT_SUBDIR/"
  144. # ---- 7. keep what boots now ------------------------------------------------------------
  145. # The uImage header holds the release ("Linux-4.18.0-k4be") at offset 32.
  146. old_rel=
  147. if texists "boot/$kfile"; then
  148. old_rel="$(trun "dd if='boot/$kfile' bs=32 skip=1 count=1 status=none" | tr -d '\0')"
  149. case "$old_rel" in
  150. Linux-*) old_rel="${old_rel#Linux-}" ;;
  151. *) old_rel="unknown-$(date +%Y%m%d)" ;;
  152. esac
  153. old_rel="${old_rel//[^A-Za-z0-9._+-]/_}"
  154. fi
  155. roll_k=; roll_d=
  156. if [ -n "$old_rel" ] && [ "$old_rel" != "$RELEASE" ]; then
  157. if texists "$SUB/$old_rel/uImage"; then
  158. roll_k="$BOOT_SUBDIR/$old_rel/uImage"
  159. else
  160. texists "boot/$kfile-$old_rel" || trun "cp 'boot/$kfile' 'boot/$kfile-$old_rel'"
  161. roll_k="$kfile-$old_rel"
  162. fi
  163. if texists "boot/$dfile"; then
  164. if texists "$SUB/$old_rel/$dtb" && trun "cmp -s 'boot/$dfile' '$SUB/$old_rel/$dtb'"; then
  165. roll_d="$BOOT_SUBDIR/$old_rel/$dtb"
  166. else
  167. texists "boot/$dfile.$old_rel" || trun "cp 'boot/$dfile' 'boot/$dfile.$old_rel'"
  168. roll_d="$dfile.$old_rel"
  169. fi
  170. fi
  171. info "kept the current kernel ($old_rel): /boot/$roll_k, /boot/$roll_d"
  172. fi
  173. # Reinstalling the release that is already in place: the way back is the
  174. # newest older release in /boot/<subdir>/, else the newest kept original.
  175. if [ -z "$roll_k" ]; then
  176. prev="$(trun "ls -1 '$SUB' 2>/dev/null" | grep -vx "$RELEASE" | sort -V | tail -1 || true)"
  177. if [ -n "$prev" ]; then
  178. roll_k="$BOOT_SUBDIR/$prev/uImage"; roll_d="$BOOT_SUBDIR/$prev/$dtb"
  179. else
  180. prev="$(trun 'ls -1 boot' | sed -n "s/^$kfile-//p" | sort -V | tail -1 || true)"
  181. if [ -n "$prev" ]; then roll_k="$kfile-$prev"; roll_d="$dfile.$prev"; fi
  182. fi
  183. fi
  184. # ---- 8. install -----------------------------------------------------------------------------
  185. info "copying /$NEW/"
  186. trun "mkdir -p '$NEW/overlay'"
  187. (cd "$TMP" && find "$NEW" -type f) | while read -r f; do
  188. tput_file "$f" < "$TMP/$f"
  189. done
  190. info "modules to /lib/modules/$RELEASE/"
  191. if [ -n "$HOST" ]; then
  192. ssh "$HOST" "tar --keep-directory-symlink --no-same-owner -xzf - -C / lib" < "$TARBALL"
  193. else
  194. for p in lib lib/modules "lib/modules/$RELEASE"; do
  195. r="$(realpath -m -- "$ROOT/$p")"
  196. case "$r" in "$ROOT"/*) ;; *) die "/$p on the target resolves to $r, outside $ROOT; not writing there" ;; esac
  197. done
  198. tar --keep-directory-symlink -xzf "$TARBALL" -C "$ROOT" lib
  199. fi
  200. trun "sync"
  201. info "switching /boot/$kfile and /boot/$dfile to $RELEASE"
  202. trun "cp '$NEW/uImage' 'boot/$kfile.new' && mv -f 'boot/$kfile.new' 'boot/$kfile'"
  203. trun "cp '$NEW/$dtb' 'boot/$dfile.new' && mv -f 'boot/$dfile.new' 'boot/$dfile'"
  204. trun "sync"
  205. # verify what U-Boot will read
  206. want="$(cd "$TMP/$NEW" && md5sum uImage "$dtb" | cut -d' ' -f1)"
  207. have="$(trun "md5sum 'boot/$kfile' 'boot/$dfile'" | cut -d' ' -f1)"
  208. [ "$want" = "$have" ] || die "/boot/$kfile or /boot/$dfile does not match the build after copying"
  209. info "verified: /boot/$kfile and /boot/$dfile are $RELEASE"
  210. echo
  211. info "installed $RELEASE ($BOARD_NAME). Not rebooted."
  212. info "releases in /boot/$BOOT_SUBDIR/: $(trun "ls -1 '$SUB'" | sort -V | tr '\n' ' ')"
  213. cat <<EOF
  214. Rollback from the U-Boot prompt (serial console, stop autoboot):
  215. fatload mmc 0:1 0x47000000 ${roll_k:-<old uImage>}
  216. fatload mmc 0:1 0x43000000 ${roll_d:-<old dtb>}
  217. EOF
  218. [ -z "$ofile" ] || echo " fdt addr 0x43000000; fdt resize; fatload mmc 0:1 0x4300F000 $ofile; fdt apply 0x4300F000"
  219. cat <<EOF
  220. bootm 0x47000000 - 0x43000000
  221. then, once booted, copy the old files back over /boot/$kfile and /boot/$dfile.
  222. EOF