Skip to content

Commit

Permalink
ayufan: dev.mk: add rock64_bootloader.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
ayufan committed Nov 19, 2023
1 parent 3fb2930 commit fb9e886
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 55 deletions.
104 changes: 73 additions & 31 deletions dev-ayufan/root/usr/local/lib/u-boot-rockchip/loader-common
Original file line number Diff line number Diff line change
@@ -1,71 +1,93 @@
#!/bin/bash

error() {
echo "$@" 1>&2
}

if [[ "$(id -u)" -ne "0" ]]; then
echo "This script requires root."
error "This script requires root."
exit 1
fi

if ! which nandwrite &>/dev/null; then
echo "Install mtd-utils with 'apt-get install mtd-utils'"
error "Install mtd-utils with 'apt-get install mtd-utils'"
exit 1
fi

if ! which flash_erase &>/dev/null; then
echo "Install mtd-utils with 'apt-get install mtd-utils'"
error "Install mtd-utils with 'apt-get install mtd-utils'"
exit 1
fi

if ! PACKAGE_NAME=$(dpkg -S "$0" | cut -d: -f1); then
exit "Unknown package installed."
error "Unknown package installed."
exit 1
fi

if ! debsums -s "${PACKAGE_NAME}"; then
echo "Verification of '${PACKAGE_NAME}' failed."
echo "Your disk might have got corrupted."
error "Verification of '${PACKAGE_NAME}' failed."
error "Your disk might have got corrupted."
if [[ -z "IGNORE_VERIFICATION" ]]; then
echo "Use 'export IGNORE_VERIFICATION=1'"
error "Use 'export IGNORE_VERIFICATION=1'"
exit 1
fi
fi

case "$PACKAGE_NAME" in
*-rockchip-rock64-*)
*-rock64-*)
SD_LOADER=/usr/lib/u-boot-rock64/rksd_loader.img
SPI_LOADER=/usr/lib/u-boot-rock64/rksd_loader.img
BOARD=rock64
SPI_OFFSET=$((64*512))
BOARD=pine64,rock64
;;

*-rockchip-rockpro64-*)
*-rockpro64-*)
SD_LOADER=/usr/lib/u-boot-rockpro64/rksd_loader.img
SPI_LOADER=/usr/lib/u-boot-rockpro64/rkspi_loader.img
BOARD=rockpro64
SPI_OFFSET=$((64*512))
BOARD=pine64,rockpro64
;;

*-rockchip-pinebookpro-*)
*-pinebookpro-*)
SD_LOADER=/usr/lib/u-boot-pinebookpro/rksd_loader.img
SPI_LOADER=/usr/lib/u-boot-pinebookpro/rkspi_loader.img
BOARD=pinebookpro
SPI_OFFSET=$((64*512))
BOARD=pine64,pinebookpro
;;

*-rockchip-rockpi4b-*)
*-rockpi4b-*)
SD_LOADER=/usr/lib/u-boot-rockpi4b/rksd_loader.img
SPI_LOADER=/usr/lib/u-boot-rockpi4b/rkspi_loader.img
BOARD=rockpi4b
SPI_OFFSET=$((64*512))
BOARD=radxa,rockpi4b
;;

*-rock5b-*)
SD_LOADER=/usr/lib/u-boot-rock5b/rksd_loader.img
SPI_LOADER=/usr/lib/u-boot-rock5b/rksd_loader.img
SPI_OFFSET=$((64*512))
BOARD=radxa,rock-5b
;;

*)
echo "Cannot detect board from $PACKAGE_NAME."
error "Cannot detect board from $PACKAGE_NAME."
exit 1
;;
esac

if ! grep -qi "$BOARD" /proc/device-tree/compatible; then
echo "You are currently running on different board:"
echo "$(cat /proc/device-tree/model || true)"
echo "It may brick your device or the system unless"
echo "you know what are you doing."
echo ""
error "You are currently running on different board:"
error "$(cat /proc/device-tree/model || true)"
error "It may brick your device or the system unless"
error "you know what are you doing."
error ""
fi

if [[ -f "$SPI_LOADER" ]]; then
SPI_SIZE=$(stat -c%s "$SPI_LOADER")
elif [[ -n "$SPI_LOADER" ]]; then
error "The '$SPI_LOADER' is missing."
SPI_SIZE=0
fi

confirm() {
Expand Down Expand Up @@ -101,17 +123,37 @@ boot_device() {
esac
}

version() {
local DEVICE="$(boot_device "$1")"

echo -n "Current version: "
if strings "$DEVICE" | grep "^U-Boot [a-z0-9.-]*$"; then
echo -n "Board: "
strings "$DEVICE" | grep -E "^board="
echo -n "FDT: "
strings "$DEVICE" | grep -E "^fdtfile="
version2() {
echo "$1:"
if VER=$(strings "$2" | grep "^U-Boot [0-9]*.[0-9]*-[a-z0-9.-]* ("); then
echo "- Version: $VER"
echo "- Board: $(strings "$2" | grep -E "^board=")"
echo "- FDT: $(strings "$2" | grep -E "^fdtfile=")"
else
echo "not installed on $DEVICE."
echo "- Not installed on $2."
fi
echo
}

version() {
version2 "Current" "$(boot_device "$1")"
}

find_mtd() {
for mtd in /sys/class/mtd/mtd?; do
local offset=0
[[ ! -e "$mtd/offset" ]] || offset=$(cat "$mtd/offset")
local size=$(cat "$mtd/size")

[[ $offset -le $SPI_OFFSET ]] || continue
[[ $(($offset+$size)) -ge $(($SPI_OFFSET+$SPI_SIZE)) ]] || continue

MTD=$(basename "$mtd")
MTD_DEV="/dev/$MTD"
MTD_OFFSET="$(($SPI_OFFSET-$offset))"
return 0
done

error "Could not find MTD device to wrote $SPI_SIZE bytes at $SPI_OFFSET."
return 1
}
10 changes: 3 additions & 7 deletions dev-ayufan/root/usr/local/sbin/rock64_erase_spi_flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ echo " and it will require that you use eMMC or SD"
echo " as your boot device."
echo ""

if ! MTD=$(grep \"loader\" /proc/mtd | cut -d: -f1); then
echo "loader partition on MTD is not found"
exit 1
fi

version "/dev/${MTD}"
find_mtd
version "$MTD_DEV"
confirm

flash_erase "/dev/$MTD" 0 0
flash_erase "$MTD_DEV" "$MTD_OFFSET" 0

echo Done.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ write_sd() {
}

write_sd "$MNT_DEV" "$SD_LOADER"

sync

echo Done.
18 changes: 5 additions & 13 deletions dev-ayufan/root/usr/local/sbin/rock64_write_spi_flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ echo " and it will require that you use eMMC or SD"
echo " as your boot device."
echo ""

if ! MTD=$(grep \"loader\" /proc/mtd | cut -d: -f1); then
echo "loader partition on MTD is not found"
exit 1
fi

version "/dev/${MTD}"
find_mtd
version "$MTD_DEV"
confirm

write_nand() {
echo "Writing /dev/$MTD with content of $2"
flash_erase "/dev/$MTD" 0 0
nandwrite "/dev/$MTD" < "$2"
}

write_nand loader "$SPI_LOADER"
echo "Writing "$MTD_DEV" with content at $MTD_OFFSET"
flash_erase "$MTD_DEV" "$MTD_OFFSET" 0
nandwrite -s "$MTD_OFFSET" "$MTD_DEV" "$SPI_LOADER"

echo Done.
55 changes: 55 additions & 0 deletions dev-ayufan/root/usr/local/sbin/upgrade_bootloader.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

set -eo pipefail

source /usr/local/lib/u-boot-rockchip/loader-common

cnt=1
methods=()

version2 "Latest version" "$SD_LOADER"
echo "0) Do not do any changes."
echo
methods[0]="# no-op"

while read MNT_DEV; do
version2 "$MNT_DEV" "$MNT_DEV"

echo "$cnt) Install u-boot on $MNT_DEV"
methods[$cnt]="dd if=\"$SD_LOADER\" of=\"$MNT_DEV\" iflag=fullblock oflag=direct"
cnt=$((cnt+1))
echo "$cnt) Remove bootloader on $MNT_DEV"
methods[$cnt]="dd if=/dev/zero of=\"$MNT_DEV\" iflag=fullblock oflag=direct"
cnt=$((cnt+1))
echo
done < <(blkid /dev/disk/by-id/{ata,usb,mmc}-* -o device -t PARTLABEL="loader1")

if find_mtd; then
version2 "SPI Flash" "$MTD_DEV"

echo "$cnt) Install u-boot on $MTD_DEV"
methods[$cnt]="flash_erase \"$MTD_DEV\" \"$MTD_OFFSET\" 0 && nandwrite -s \"$MTD_OFFSET\" \"$MTD_DEV\" \"$SPI_LOADER\""
cnt=$((cnt+1))
echo "$cnt) Remove bootloader on $MTD_DEV"
methods[$cnt]="flash_erase \"$MTD_DEV\" \"$MTD_OFFSET\" 0"
cnt=$((cnt+1))
echo
fi

while true; do
echo "Select one of the options or Ctrl-C to abort:"
read OPTION
echo

if [[ "$OPTION" == "0" ]]; then
exit
elif [[ -z "${methods[$OPTION]}" ]]; then
echo "The '$OPTION' is invalid option"
continue
fi

echo "Executing '${methods[$OPTION]}'..."
confirm
eval "${methods[$OPTION]}"
echo
done
2 changes: 1 addition & 1 deletion dev-ayufan/scripts/postinst.deb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ case "$1" in
;;

configure)
echo "Consider upgrading device bootloader with 'rock64_upgrade_bootloader.sh'."
echo "Consider upgrading device bootloader with 'upgrade_bootloader.sh'."
;;

*)
Expand Down
4 changes: 2 additions & 2 deletions dev.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ version: .scmversion

deploy: u-boot-package
scp u-boot-$(BOARD_TARGET)-$(RELEASE_NAME).deb root@$(TARGET_HOST):
ssh root@$(TARGET_HOST) apt install ./u-boot-$(BOARD_TARGET)-$(RELEASE_NAME).deb
ssh root@$(TARGET_HOST) rock64_upgrade_bootloader.sh
ssh root@$(TARGET_HOST) dpkg -i ./u-boot-$(BOARD_TARGET)-$(RELEASE_NAME).deb
#ssh root@$(TARGET_HOST) rock64_upgrade_bootloader.sh

endif

0 comments on commit fb9e886

Please sign in to comment.