write-uboot.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/bash
  2. # Write out/<board>/u-boot/u-boot-sunxi-with-spl.bin of the selected board type
  3. # (./select-board.sh) to an SD card (or image file) at 8 KiB, where the sunxi
  4. # boot ROM looks for the SPL. The partition table and the partitions are not
  5. # touched. The area about to be overwritten (8 KiB up to 1 MiB, or to the
  6. # first partition if that is lower) is saved to out/backup/ first, so the old
  7. # bootloader can be put back.
  8. #
  9. # Boards whose U-Boot keeps its environment in uboot.env on the FAT partition
  10. # (bpi-m2: the boot commands live only there) are checked first: the target's
  11. # file must pass the CRC check for this build's CONFIG_ENV_SIZE, otherwise
  12. # U-Boot would ignore it and not find the kernel.
  13. #
  14. # Usage:
  15. # sudo ./write-uboot.sh /dev/sdX card in a USB reader on this PC
  16. # sudo ./write-uboot.sh /dev/mmcblkN card in a built-in SD slot
  17. # ./write-uboot.sh ssh:root@radio.lo card in the running board
  18. # ./write-uboot.sh card.img image file
  19. # Environment:
  20. # YES=1 skip the confirmation prompt
  21. # FORCE=1 allow a whole disk that does not look like an SD card
  22. # (neither removable, hotplug nor an SD card in an SD slot)
  23. # UBOOT_BIN=f write f instead of the build result, e.g. a backup from
  24. # out/backup/ or Armbian's u-boot-sunxi-with-spl.bin
  25. # DEV=/dev/xxx ssh: the card's device on the board (default: the disk
  26. # that holds the board's root filesystem)
  27. # NO_BACKUP=1 don't save the old area first
  28. # ENV_CHECK_SKIP=1 don't check the target's uboot.env (FAT-environment boards)
  29. # FORCE=1 also: write over ssh to a board of another type
  30. set -euo pipefail
  31. . "$(dirname "$0")/config.sh"
  32. BIN="${UBOOT_BIN:-$UOUT/u-boot-sunxi-with-spl.bin}"
  33. [ -f "$BIN" ] || die "$BIN not found, run ./build-uboot.sh first"
  34. [ $# -eq 1 ] || die "usage: $0 /dev/sdX | /dev/mmcblkN | ssh:user@host | file.img"
  35. TARGET="$1"
  36. SIZE=$(stat -c %s "$BIN")
  37. END=$(( UBOOT_SEEK_KIB * 1024 + SIZE ))
  38. END_KIB=$(( (END + 1023) / 1024 )) # rounded up, for messages
  39. # The backup covers at least this much (the Armbian U-Boot and the raw
  40. # environment slot older U-Boots used at 544 KiB fit well below it).
  41. BACKUP_END_KIB=1024
  42. # Where 2019-era sunxi U-Boot (e.g. Armbian's 2019.04) kept a raw-MMC
  43. # environment (boards/<board>.sh; empty: no such area).
  44. OLD_ENV_KIB=${UBOOT_OLD_RAW_ENV_KIB:-0}
  45. confirm() {
  46. [ "${YES:-0}" = 1 ] && return 0
  47. local a
  48. read -r -p "Write U-Boot ($SIZE bytes at ${UBOOT_SEEK_KIB} KiB) to $1? Type 'yes': " a
  49. [ "$a" = yes ] || die "aborted"
  50. }
  51. # The environment of this U-Boot must not be inside the area written. The
  52. # sunxi default is a file (uboot.env) on a FAT partition, not raw MMC; only
  53. # when ENV_IS_IN_MMC is enabled does the raw offset limit the image size.
  54. check_env() {
  55. local cfg="$OUT/ubuild/.config" off
  56. [ -z "${UBOOT_BIN:-}" ] && [ -f "$cfg" ] || return 0
  57. [ -n "$UBOOT_OLD_RAW_ENV_KIB" ] || OLD_ENV_KIB=0
  58. if grep -qx 'CONFIG_ENV_IS_IN_MMC=y' "$cfg"; then
  59. off=$(( $(sed -n 's/^CONFIG_ENV_OFFSET=//p' "$cfg") ))
  60. [ "$END" -le "$off" ] ||
  61. die "U-Boot would end at $END_KIB KiB, past its own MMC environment at $(( off / 1024 )) KiB; make CONFIG_ENV_OFFSET larger or the image smaller"
  62. fi
  63. if [ "$OLD_ENV_KIB" -gt 0 ] && [ "$END" -gt $(( OLD_ENV_KIB * 1024 )) ]; then
  64. echo "note: U-Boot ends at $END_KIB KiB ($END bytes), past ${OLD_ENV_KIB} KiB where the old Armbian U-Boot kept its environment; this U-Boot does not read it there, and the backup keeps the old one"
  65. fi
  66. }
  67. # check_gap FIRST_PARTITION_START_SECTOR
  68. check_gap() {
  69. local start="$1"
  70. [ -n "$start" ] || { echo "no partitions found"; return 0; }
  71. if [ $(( start * 512 )) -lt "$END" ]; then
  72. die "first partition starts at sector $start ($(( start / 2 )) KiB), U-Boot would end at $END_KIB KiB and overwrite it"
  73. fi
  74. echo "first partition starts at $(( start / 2 )) KiB, U-Boot ends at $END_KIB KiB: OK"
  75. }
  76. # backup_kib FIRST_PARTITION_START_SECTOR -> KiB to save from UBOOT_SEEK_KIB on
  77. backup_kib() {
  78. local start="$1" end=$BACKUP_END_KIB need=$END_KIB
  79. [ "$end" -ge "$need" ] || end=$need
  80. if [ -n "$start" ] && [ $(( start / 2 )) -lt "$end" ]; then end=$(( start / 2 )); fi
  81. echo $(( end - UBOOT_SEEK_KIB ))
  82. }
  83. # backup NAME KIB READER...: run READER (prints the area to stdout) into
  84. # out/backup/u-boot-area-NAME-DATE.bin and check the length.
  85. backup() {
  86. local name="$1" kib="$2"; shift 2
  87. if [ "${NO_BACKUP:-0}" = 1 ]; then echo "backup skipped (NO_BACKUP=1)"; return 0; fi
  88. local dir="$BACKUP_DIR"
  89. local f; f="$dir/u-boot-area-${name//[^A-Za-z0-9._-]/_}-$(date +%Y%m%d-%H%M%S).bin"
  90. mkdir -p "$dir"
  91. "$@" > "$f" || die "could not read the old area for the backup"
  92. [ "$(stat -c %s "$f")" -eq $(( kib * 1024 )) ] || die "backup $f is incomplete"
  93. # under sudo, leave the backup to the invoking user
  94. if [ -n "${SUDO_UID:-}" ]; then chown "$SUDO_UID:${SUDO_GID:-$SUDO_UID}" "$dir" "$f" || true; fi
  95. echo "old ${UBOOT_SEEK_KIB}–$(( UBOOT_SEEK_KIB + kib )) KiB saved to $f"
  96. echo " restore: UBOOT_BIN=$f $0 $TARGET"
  97. }
  98. # check_fat_env FILE WHERE: FILE is the target's uboot.env; die unless this
  99. # U-Boot accepts it (CRC over CONFIG_ENV_SIZE bytes).
  100. check_fat_env() {
  101. local f="$1" where="$2" cfg="$OUT/ubuild/.config" size
  102. size="$(sed -n 's/^CONFIG_ENV_SIZE=//p' "$cfg" 2>/dev/null)"
  103. [ -n "$size" ] || die "no CONFIG_ENV_SIZE in $cfg; build U-Boot first (or ENV_CHECK_SKIP=1 with UBOOT_BIN)"
  104. [ -s "$f" ] || die "$where has no $UBOOT_ENV_FILE: U-Boot would start with its default environment and not find the kernel (ENV_CHECK_SKIP=1 overrides)"
  105. if python3 -I "$TOP/lib/uboot-env.py" check "$f" "$size"; then
  106. echo "$where: $UBOOT_ENV_FILE is valid for CONFIG_ENV_SIZE=$size: OK"
  107. python3 -I "$TOP/lib/uboot-env.py" vars "$f" | grep -E '^(bootcmd|distro_bootcmd)=' | sed 's/^/ /'
  108. else
  109. die "$where: $UBOOT_ENV_FILE ($(python3 -I "$TOP/lib/uboot-env.py" size "$f" || echo "no valid CRC")) does not pass the CRC check of this U-Boot (CONFIG_ENV_SIZE=$size); it would boot the default environment. Fix files/$BOARD/u-boot.config (ENV_CHECK_SKIP=1 overrides)"
  110. fi
  111. }
  112. first_start() { sfdisk -d "$1" 2>/dev/null | sed -n 's/.*start= *\([0-9]*\).*/\1/p' | head -1; }
  113. check_env
  114. case "$TARGET" in
  115. ssh:*)
  116. host="${TARGET#ssh:}"
  117. need_tools ssh
  118. # The card holding the board's / (mmcblk0 on mainline, but the number
  119. # follows probe order, so look it up). The PC-side safety checks don't
  120. # apply here: this is the board's own boot card by definition.
  121. dev="${DEV:-$(ssh "$host" 'p=$(lsblk -no PKNAME "$(findmnt -no SOURCE /)" | head -1); [ -n "$p" ] && echo "/dev/$p"')}"
  122. [ -n "$dev" ] || die "could not find the board's root disk; set DEV=/dev/mmcblkN"
  123. compat="$(ssh "$host" 'tr "\0" "\n" < /proc/device-tree/compatible' 2>/dev/null || true)"
  124. grep -qx "$BOARD_DT_COMPAT" <<<"$compat" || [ "${FORCE:-0}" = 1 ] ||
  125. die "$host is not a $BOARD_NAME (compatible: $(echo $compat)); select its board type with ./select-board.sh (FORCE=1 overrides)"
  126. info "card in $host: $dev"
  127. if [ "$UBOOT_ENV_FAT" = 1 ] && [ "${ENV_CHECK_SKIP:-0}" != 1 ]; then
  128. envf="$(mktemp)"
  129. # /boot may be noauto: mount it read-only just for reading
  130. ssh "$host" "if mountpoint -q /boot; then cat /boot/$UBOOT_ENV_FILE; else mount -o ro /boot && { cat /boot/$UBOOT_ENV_FILE; umount /boot; }; fi" > "$envf" || true
  131. ( check_fat_env "$envf" "$host:/boot" ) || { rm -f "$envf"; exit 1; }
  132. rm -f "$envf"
  133. fi
  134. ssh "$host" "cat /proc/device-tree/model 2>/dev/null; echo; lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,MOUNTPOINTS $dev; echo \"device type: \$(cat /sys/block/${dev#/dev/}/device/type 2>/dev/null || echo unknown)\""
  135. start=$(ssh "$host" "sfdisk -d $dev 2>/dev/null | sed -n 's/.*start= *\([0-9]*\).*/\1/p' | head -1")
  136. check_gap "$start"
  137. confirm "$host:$dev"
  138. kib=$(backup_kib "$start")
  139. backup "$host" "$kib" ssh "$host" "dd if=$dev bs=1024 skip=$UBOOT_SEEK_KIB count=$kib status=none"
  140. ssh "$host" "dd of=$dev bs=1024 seek=$UBOOT_SEEK_KIB conv=fsync status=none && sync" < "$BIN"
  141. ;;
  142. /dev/*)
  143. need_tools lsblk sfdisk dd
  144. [ -b "$TARGET" ] || die "$TARGET is not a block device"
  145. [ "$(lsblk -dno TYPE "$TARGET")" = disk ] || die "$TARGET is not a whole disk (give /dev/sdX, not /dev/sdX1)"
  146. name="$(basename "$(readlink -f "$TARGET")")"
  147. case "$name" in
  148. mmcblk*boot*|mmcblk*rpmb) die "$TARGET is an eMMC boot/RPMB area, not an SD card" ;;
  149. esac
  150. # Refuse disks with a filesystem mounted at a system path on this PC.
  151. if lsblk -nro MOUNTPOINTS "$TARGET" | grep -qxE '/|/boot(/.*)?|/usr|/var|/home|\[SWAP\]'; then
  152. die "$TARGET holds a system filesystem of this machine"
  153. fi
  154. rm_flag=$(lsblk -dno RM "$TARGET" | tr -d ' ')
  155. hotplug=$(lsblk -dno HOTPLUG "$TARGET" | tr -d ' ')
  156. # Built-in SD slots (SDHCI, /dev/mmcblkN) usually report RM=0; the MMC
  157. # core's card type tells an SD card from soldered eMMC ("MMC").
  158. mmc_type=$(cat "/sys/block/$name/device/type" 2>/dev/null || true)
  159. if [ "$rm_flag" != 1 ] && [ "$hotplug" != 1 ] && [ "$mmc_type" != SD ] && [ "${FORCE:-0}" != 1 ]; then
  160. die "$TARGET is neither removable, hotplug nor an SD card${mmc_type:+ (MMC type: $mmc_type)}; set FORCE=1 if this really is the card"
  161. fi
  162. lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,MOUNTPOINTS,MODEL "$TARGET"
  163. if [ "$UBOOT_ENV_FAT" = 1 ] && [ "${ENV_CHECK_SKIP:-0}" != 1 ]; then
  164. p1="$(lsblk -lnpo NAME,TYPE "$TARGET" | awk '$2 == "part" { print $1; exit }')"
  165. mp="$(lsblk -no MOUNTPOINT "$p1" 2>/dev/null | head -1)"
  166. [ -n "$mp" ] || die "mount the card's first partition ($p1, read-only is fine) so its $UBOOT_ENV_FILE can be checked (ENV_CHECK_SKIP=1 overrides)"
  167. check_fat_env "$mp/$UBOOT_ENV_FILE" "$p1"
  168. fi
  169. start=$(first_start "$TARGET")
  170. check_gap "$start"
  171. confirm "$TARGET"
  172. kib=$(backup_kib "$start")
  173. backup "$name" "$kib" dd if="$TARGET" bs=1024 skip="$UBOOT_SEEK_KIB" count="$kib" status=none
  174. dd if="$BIN" of="$TARGET" bs=1024 seek="$UBOOT_SEEK_KIB" conv=fsync status=none
  175. sync
  176. ;;
  177. *)
  178. [ -f "$TARGET" ] || die "$TARGET: no such image file"
  179. [ "$UBOOT_ENV_FAT" != 1 ] || echo "note: $UBOOT_ENV_FILE in the image is not checked; make sure it suits CONFIG_ENV_SIZE"
  180. start=$(first_start "$TARGET")
  181. check_gap "$start"
  182. confirm "$TARGET"
  183. kib=$(backup_kib "$start")
  184. backup "$(basename "$TARGET")" "$kib" dd if="$TARGET" bs=1024 skip="$UBOOT_SEEK_KIB" count="$kib" status=none
  185. dd if="$BIN" of="$TARGET" bs=1024 seek="$UBOOT_SEEK_KIB" conv=notrunc,fsync status=none
  186. ;;
  187. esac
  188. info "U-Boot ($BOARD_NAME) written to $TARGET; it is used from the next reboot on"