#!/bin/bash # Write out//u-boot/u-boot-sunxi-with-spl.bin of the selected board type # (./select-board.sh) to an SD card (or image file) at 8 KiB, where the sunxi # boot ROM looks for the SPL. The partition table and the partitions are not # touched. The area about to be overwritten (8 KiB up to 1 MiB, or to the # first partition if that is lower) is saved to out/backup/ first, so the old # bootloader can be put back. # # Boards whose U-Boot keeps its environment in uboot.env on the FAT partition # (bpi-m2: the boot commands live only there) are checked first: the target's # file must pass the CRC check for this build's CONFIG_ENV_SIZE, otherwise # U-Boot would ignore it and not find the kernel. # # Usage: # sudo ./write-uboot.sh /dev/sdX card in a USB reader on this PC # sudo ./write-uboot.sh /dev/mmcblkN card in a built-in SD slot # ./write-uboot.sh ssh:root@radio.lo card in the running board # ./write-uboot.sh card.img image file # Environment: # YES=1 skip the confirmation prompt # FORCE=1 allow a whole disk that does not look like an SD card # (neither removable, hotplug nor an SD card in an SD slot) # UBOOT_BIN=f write f instead of the build result, e.g. a backup from # out/backup/ or Armbian's u-boot-sunxi-with-spl.bin # DEV=/dev/xxx ssh: the card's device on the board (default: the disk # that holds the board's root filesystem) # NO_BACKUP=1 don't save the old area first # ENV_CHECK_SKIP=1 don't check the target's uboot.env (FAT-environment boards) # FORCE=1 also: write over ssh to a board of another type set -euo pipefail . "$(dirname "$0")/config.sh" BIN="${UBOOT_BIN:-$UOUT/u-boot-sunxi-with-spl.bin}" [ -f "$BIN" ] || die "$BIN not found, run ./build-uboot.sh first" [ $# -eq 1 ] || die "usage: $0 /dev/sdX | /dev/mmcblkN | ssh:user@host | file.img" TARGET="$1" SIZE=$(stat -c %s "$BIN") END=$(( UBOOT_SEEK_KIB * 1024 + SIZE )) END_KIB=$(( (END + 1023) / 1024 )) # rounded up, for messages # The backup covers at least this much (the Armbian U-Boot and the raw # environment slot older U-Boots used at 544 KiB fit well below it). BACKUP_END_KIB=1024 # Where 2019-era sunxi U-Boot (e.g. Armbian's 2019.04) kept a raw-MMC # environment (boards/.sh; empty: no such area). OLD_ENV_KIB=${UBOOT_OLD_RAW_ENV_KIB:-0} confirm() { [ "${YES:-0}" = 1 ] && return 0 local a read -r -p "Write U-Boot ($SIZE bytes at ${UBOOT_SEEK_KIB} KiB) to $1? Type 'yes': " a [ "$a" = yes ] || die "aborted" } # The environment of this U-Boot must not be inside the area written. The # sunxi default is a file (uboot.env) on a FAT partition, not raw MMC; only # when ENV_IS_IN_MMC is enabled does the raw offset limit the image size. check_env() { local cfg="$OUT/ubuild/.config" off [ -z "${UBOOT_BIN:-}" ] && [ -f "$cfg" ] || return 0 [ -n "$UBOOT_OLD_RAW_ENV_KIB" ] || OLD_ENV_KIB=0 if grep -qx 'CONFIG_ENV_IS_IN_MMC=y' "$cfg"; then off=$(( $(sed -n 's/^CONFIG_ENV_OFFSET=//p' "$cfg") )) [ "$END" -le "$off" ] || 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" fi if [ "$OLD_ENV_KIB" -gt 0 ] && [ "$END" -gt $(( OLD_ENV_KIB * 1024 )) ]; then 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" fi } # check_gap FIRST_PARTITION_START_SECTOR check_gap() { local start="$1" [ -n "$start" ] || { echo "no partitions found"; return 0; } if [ $(( start * 512 )) -lt "$END" ]; then die "first partition starts at sector $start ($(( start / 2 )) KiB), U-Boot would end at $END_KIB KiB and overwrite it" fi echo "first partition starts at $(( start / 2 )) KiB, U-Boot ends at $END_KIB KiB: OK" } # backup_kib FIRST_PARTITION_START_SECTOR -> KiB to save from UBOOT_SEEK_KIB on backup_kib() { local start="$1" end=$BACKUP_END_KIB need=$END_KIB [ "$end" -ge "$need" ] || end=$need if [ -n "$start" ] && [ $(( start / 2 )) -lt "$end" ]; then end=$(( start / 2 )); fi echo $(( end - UBOOT_SEEK_KIB )) } # backup NAME KIB READER...: run READER (prints the area to stdout) into # out/backup/u-boot-area-NAME-DATE.bin and check the length. backup() { local name="$1" kib="$2"; shift 2 if [ "${NO_BACKUP:-0}" = 1 ]; then echo "backup skipped (NO_BACKUP=1)"; return 0; fi local dir="$BACKUP_DIR" local f; f="$dir/u-boot-area-${name//[^A-Za-z0-9._-]/_}-$(date +%Y%m%d-%H%M%S).bin" mkdir -p "$dir" "$@" > "$f" || die "could not read the old area for the backup" [ "$(stat -c %s "$f")" -eq $(( kib * 1024 )) ] || die "backup $f is incomplete" # under sudo, leave the backup to the invoking user if [ -n "${SUDO_UID:-}" ]; then chown "$SUDO_UID:${SUDO_GID:-$SUDO_UID}" "$dir" "$f" || true; fi echo "old ${UBOOT_SEEK_KIB}–$(( UBOOT_SEEK_KIB + kib )) KiB saved to $f" echo " restore: UBOOT_BIN=$f $0 $TARGET" } # check_fat_env FILE WHERE: FILE is the target's uboot.env; die unless this # U-Boot accepts it (CRC over CONFIG_ENV_SIZE bytes). check_fat_env() { local f="$1" where="$2" cfg="$OUT/ubuild/.config" size size="$(sed -n 's/^CONFIG_ENV_SIZE=//p' "$cfg" 2>/dev/null)" [ -n "$size" ] || die "no CONFIG_ENV_SIZE in $cfg; build U-Boot first (or ENV_CHECK_SKIP=1 with UBOOT_BIN)" [ -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)" if python3 -I "$TOP/lib/uboot-env.py" check "$f" "$size"; then echo "$where: $UBOOT_ENV_FILE is valid for CONFIG_ENV_SIZE=$size: OK" python3 -I "$TOP/lib/uboot-env.py" vars "$f" | grep -E '^(bootcmd|distro_bootcmd)=' | sed 's/^/ /' else 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)" fi } first_start() { sfdisk -d "$1" 2>/dev/null | sed -n 's/.*start= *\([0-9]*\).*/\1/p' | head -1; } check_env case "$TARGET" in ssh:*) host="${TARGET#ssh:}" need_tools ssh # The card holding the board's / (mmcblk0 on mainline, but the number # follows probe order, so look it up). The PC-side safety checks don't # apply here: this is the board's own boot card by definition. dev="${DEV:-$(ssh "$host" 'p=$(lsblk -no PKNAME "$(findmnt -no SOURCE /)" | head -1); [ -n "$p" ] && echo "/dev/$p"')}" [ -n "$dev" ] || die "could not find the board's root disk; set DEV=/dev/mmcblkN" compat="$(ssh "$host" 'tr "\0" "\n" < /proc/device-tree/compatible' 2>/dev/null || true)" grep -qx "$BOARD_DT_COMPAT" <<<"$compat" || [ "${FORCE:-0}" = 1 ] || die "$host is not a $BOARD_NAME (compatible: $(echo $compat)); select its board type with ./select-board.sh (FORCE=1 overrides)" info "card in $host: $dev" if [ "$UBOOT_ENV_FAT" = 1 ] && [ "${ENV_CHECK_SKIP:-0}" != 1 ]; then envf="$(mktemp)" # /boot may be noauto: mount it read-only just for reading 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 ( check_fat_env "$envf" "$host:/boot" ) || { rm -f "$envf"; exit 1; } rm -f "$envf" fi 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)\"" start=$(ssh "$host" "sfdisk -d $dev 2>/dev/null | sed -n 's/.*start= *\([0-9]*\).*/\1/p' | head -1") check_gap "$start" confirm "$host:$dev" kib=$(backup_kib "$start") backup "$host" "$kib" ssh "$host" "dd if=$dev bs=1024 skip=$UBOOT_SEEK_KIB count=$kib status=none" ssh "$host" "dd of=$dev bs=1024 seek=$UBOOT_SEEK_KIB conv=fsync status=none && sync" < "$BIN" ;; /dev/*) need_tools lsblk sfdisk dd [ -b "$TARGET" ] || die "$TARGET is not a block device" [ "$(lsblk -dno TYPE "$TARGET")" = disk ] || die "$TARGET is not a whole disk (give /dev/sdX, not /dev/sdX1)" name="$(basename "$(readlink -f "$TARGET")")" case "$name" in mmcblk*boot*|mmcblk*rpmb) die "$TARGET is an eMMC boot/RPMB area, not an SD card" ;; esac # Refuse disks with a filesystem mounted at a system path on this PC. if lsblk -nro MOUNTPOINTS "$TARGET" | grep -qxE '/|/boot(/.*)?|/usr|/var|/home|\[SWAP\]'; then die "$TARGET holds a system filesystem of this machine" fi rm_flag=$(lsblk -dno RM "$TARGET" | tr -d ' ') hotplug=$(lsblk -dno HOTPLUG "$TARGET" | tr -d ' ') # Built-in SD slots (SDHCI, /dev/mmcblkN) usually report RM=0; the MMC # core's card type tells an SD card from soldered eMMC ("MMC"). mmc_type=$(cat "/sys/block/$name/device/type" 2>/dev/null || true) if [ "$rm_flag" != 1 ] && [ "$hotplug" != 1 ] && [ "$mmc_type" != SD ] && [ "${FORCE:-0}" != 1 ]; then 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" fi lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,MOUNTPOINTS,MODEL "$TARGET" if [ "$UBOOT_ENV_FAT" = 1 ] && [ "${ENV_CHECK_SKIP:-0}" != 1 ]; then p1="$(lsblk -lnpo NAME,TYPE "$TARGET" | awk '$2 == "part" { print $1; exit }')" mp="$(lsblk -no MOUNTPOINT "$p1" 2>/dev/null | head -1)" [ -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)" check_fat_env "$mp/$UBOOT_ENV_FILE" "$p1" fi start=$(first_start "$TARGET") check_gap "$start" confirm "$TARGET" kib=$(backup_kib "$start") backup "$name" "$kib" dd if="$TARGET" bs=1024 skip="$UBOOT_SEEK_KIB" count="$kib" status=none dd if="$BIN" of="$TARGET" bs=1024 seek="$UBOOT_SEEK_KIB" conv=fsync status=none sync ;; *) [ -f "$TARGET" ] || die "$TARGET: no such image file" [ "$UBOOT_ENV_FAT" != 1 ] || echo "note: $UBOOT_ENV_FILE in the image is not checked; make sure it suits CONFIG_ENV_SIZE" start=$(first_start "$TARGET") check_gap "$start" confirm "$TARGET" kib=$(backup_kib "$start") backup "$(basename "$TARGET")" "$kib" dd if="$TARGET" bs=1024 skip="$UBOOT_SEEK_KIB" count="$kib" status=none dd if="$BIN" of="$TARGET" bs=1024 seek="$UBOOT_SEEK_KIB" conv=notrunc,fsync status=none ;; esac info "U-Boot ($BOARD_NAME) written to $TARGET; it is used from the next reboot on"