| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #!/bin/bash
- # Write out/u-boot/u-boot-sunxi-with-spl.bin to an SD card (or image file) at
- # 8 KiB, where the H2+ 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.
- #
- # 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
- 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: CONFIG_ENV_OFFSET=0x88000, 128 KiB.
- OLD_ENV_KIB=544
- 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
- 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 [ "$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="$OUT/backup"
- 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"
- }
- 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"
- info "card in $host: $dev"
- 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"
- 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"
- 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 written to $TARGET"
|