Skip to content

Commit fb9e886

Browse files
committed
ayufan: dev.mk: add rock64_bootloader.sh script
1 parent 3fb2930 commit fb9e886

File tree

7 files changed

+139
-55
lines changed

7 files changed

+139
-55
lines changed
Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,93 @@
11
#!/bin/bash
22

3+
error() {
4+
echo "$@" 1>&2
5+
}
6+
37
if [[ "$(id -u)" -ne "0" ]]; then
4-
echo "This script requires root."
8+
error "This script requires root."
59
exit 1
610
fi
711

812
if ! which nandwrite &>/dev/null; then
9-
echo "Install mtd-utils with 'apt-get install mtd-utils'"
13+
error "Install mtd-utils with 'apt-get install mtd-utils'"
1014
exit 1
1115
fi
1216

1317
if ! which flash_erase &>/dev/null; then
14-
echo "Install mtd-utils with 'apt-get install mtd-utils'"
18+
error "Install mtd-utils with 'apt-get install mtd-utils'"
1519
exit 1
1620
fi
1721

1822
if ! PACKAGE_NAME=$(dpkg -S "$0" | cut -d: -f1); then
19-
exit "Unknown package installed."
23+
error "Unknown package installed."
2024
exit 1
2125
fi
2226

2327
if ! debsums -s "${PACKAGE_NAME}"; then
24-
echo "Verification of '${PACKAGE_NAME}' failed."
25-
echo "Your disk might have got corrupted."
28+
error "Verification of '${PACKAGE_NAME}' failed."
29+
error "Your disk might have got corrupted."
2630
if [[ -z "IGNORE_VERIFICATION" ]]; then
27-
echo "Use 'export IGNORE_VERIFICATION=1'"
31+
error "Use 'export IGNORE_VERIFICATION=1'"
2832
exit 1
2933
fi
3034
fi
3135

3236
case "$PACKAGE_NAME" in
33-
*-rockchip-rock64-*)
37+
*-rock64-*)
3438
SD_LOADER=/usr/lib/u-boot-rock64/rksd_loader.img
3539
SPI_LOADER=/usr/lib/u-boot-rock64/rksd_loader.img
36-
BOARD=rock64
40+
SPI_OFFSET=$((64*512))
41+
BOARD=pine64,rock64
3742
;;
3843

39-
*-rockchip-rockpro64-*)
44+
*-rockpro64-*)
4045
SD_LOADER=/usr/lib/u-boot-rockpro64/rksd_loader.img
4146
SPI_LOADER=/usr/lib/u-boot-rockpro64/rkspi_loader.img
42-
BOARD=rockpro64
47+
SPI_OFFSET=$((64*512))
48+
BOARD=pine64,rockpro64
4349
;;
4450

45-
*-rockchip-pinebookpro-*)
51+
*-pinebookpro-*)
4652
SD_LOADER=/usr/lib/u-boot-pinebookpro/rksd_loader.img
4753
SPI_LOADER=/usr/lib/u-boot-pinebookpro/rkspi_loader.img
48-
BOARD=pinebookpro
54+
SPI_OFFSET=$((64*512))
55+
BOARD=pine64,pinebookpro
4956
;;
5057

51-
*-rockchip-rockpi4b-*)
58+
*-rockpi4b-*)
5259
SD_LOADER=/usr/lib/u-boot-rockpi4b/rksd_loader.img
5360
SPI_LOADER=/usr/lib/u-boot-rockpi4b/rkspi_loader.img
54-
BOARD=rockpi4b
61+
SPI_OFFSET=$((64*512))
62+
BOARD=radxa,rockpi4b
63+
;;
64+
65+
*-rock5b-*)
66+
SD_LOADER=/usr/lib/u-boot-rock5b/rksd_loader.img
67+
SPI_LOADER=/usr/lib/u-boot-rock5b/rksd_loader.img
68+
SPI_OFFSET=$((64*512))
69+
BOARD=radxa,rock-5b
5570
;;
5671

5772
*)
58-
echo "Cannot detect board from $PACKAGE_NAME."
73+
error "Cannot detect board from $PACKAGE_NAME."
5974
exit 1
6075
;;
6176
esac
6277

6378
if ! grep -qi "$BOARD" /proc/device-tree/compatible; then
64-
echo "You are currently running on different board:"
65-
echo "$(cat /proc/device-tree/model || true)"
66-
echo "It may brick your device or the system unless"
67-
echo "you know what are you doing."
68-
echo ""
79+
error "You are currently running on different board:"
80+
error "$(cat /proc/device-tree/model || true)"
81+
error "It may brick your device or the system unless"
82+
error "you know what are you doing."
83+
error ""
84+
fi
85+
86+
if [[ -f "$SPI_LOADER" ]]; then
87+
SPI_SIZE=$(stat -c%s "$SPI_LOADER")
88+
elif [[ -n "$SPI_LOADER" ]]; then
89+
error "The '$SPI_LOADER' is missing."
90+
SPI_SIZE=0
6991
fi
7092

7193
confirm() {
@@ -101,17 +123,37 @@ boot_device() {
101123
esac
102124
}
103125

104-
version() {
105-
local DEVICE="$(boot_device "$1")"
106-
107-
echo -n "Current version: "
108-
if strings "$DEVICE" | grep "^U-Boot [a-z0-9.-]*$"; then
109-
echo -n "Board: "
110-
strings "$DEVICE" | grep -E "^board="
111-
echo -n "FDT: "
112-
strings "$DEVICE" | grep -E "^fdtfile="
126+
version2() {
127+
echo "$1:"
128+
if VER=$(strings "$2" | grep "^U-Boot [0-9]*.[0-9]*-[a-z0-9.-]* ("); then
129+
echo "- Version: $VER"
130+
echo "- Board: $(strings "$2" | grep -E "^board=")"
131+
echo "- FDT: $(strings "$2" | grep -E "^fdtfile=")"
113132
else
114-
echo "not installed on $DEVICE."
133+
echo "- Not installed on $2."
115134
fi
116135
echo
117136
}
137+
138+
version() {
139+
version2 "Current" "$(boot_device "$1")"
140+
}
141+
142+
find_mtd() {
143+
for mtd in /sys/class/mtd/mtd?; do
144+
local offset=0
145+
[[ ! -e "$mtd/offset" ]] || offset=$(cat "$mtd/offset")
146+
local size=$(cat "$mtd/size")
147+
148+
[[ $offset -le $SPI_OFFSET ]] || continue
149+
[[ $(($offset+$size)) -ge $(($SPI_OFFSET+$SPI_SIZE)) ]] || continue
150+
151+
MTD=$(basename "$mtd")
152+
MTD_DEV="/dev/$MTD"
153+
MTD_OFFSET="$(($SPI_OFFSET-$offset))"
154+
return 0
155+
done
156+
157+
error "Could not find MTD device to wrote $SPI_SIZE bytes at $SPI_OFFSET."
158+
return 1
159+
}

dev-ayufan/root/usr/local/sbin/rock64_erase_spi_flash.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ echo " and it will require that you use eMMC or SD"
99
echo " as your boot device."
1010
echo ""
1111

12-
if ! MTD=$(grep \"loader\" /proc/mtd | cut -d: -f1); then
13-
echo "loader partition on MTD is not found"
14-
exit 1
15-
fi
16-
17-
version "/dev/${MTD}"
12+
find_mtd
13+
version "$MTD_DEV"
1814
confirm
1915

20-
flash_erase "/dev/$MTD" 0 0
16+
flash_erase "$MTD_DEV" "$MTD_OFFSET" 0
2117

2218
echo Done.

dev-ayufan/root/usr/local/sbin/rock64_upgrade_bootloader.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ write_sd() {
2020
}
2121

2222
write_sd "$MNT_DEV" "$SD_LOADER"
23-
2423
sync
2524

2625
echo Done.

dev-ayufan/root/usr/local/sbin/rock64_write_spi_flash.sh

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ echo " and it will require that you use eMMC or SD"
99
echo " as your boot device."
1010
echo ""
1111

12-
if ! MTD=$(grep \"loader\" /proc/mtd | cut -d: -f1); then
13-
echo "loader partition on MTD is not found"
14-
exit 1
15-
fi
16-
17-
version "/dev/${MTD}"
12+
find_mtd
13+
version "$MTD_DEV"
1814
confirm
1915

20-
write_nand() {
21-
echo "Writing /dev/$MTD with content of $2"
22-
flash_erase "/dev/$MTD" 0 0
23-
nandwrite "/dev/$MTD" < "$2"
24-
}
25-
26-
write_nand loader "$SPI_LOADER"
16+
echo "Writing "$MTD_DEV" with content at $MTD_OFFSET"
17+
flash_erase "$MTD_DEV" "$MTD_OFFSET" 0
18+
nandwrite -s "$MTD_OFFSET" "$MTD_DEV" "$SPI_LOADER"
2719

2820
echo Done.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
set -eo pipefail
4+
5+
source /usr/local/lib/u-boot-rockchip/loader-common
6+
7+
cnt=1
8+
methods=()
9+
10+
version2 "Latest version" "$SD_LOADER"
11+
echo "0) Do not do any changes."
12+
echo
13+
methods[0]="# no-op"
14+
15+
while read MNT_DEV; do
16+
version2 "$MNT_DEV" "$MNT_DEV"
17+
18+
echo "$cnt) Install u-boot on $MNT_DEV"
19+
methods[$cnt]="dd if=\"$SD_LOADER\" of=\"$MNT_DEV\" iflag=fullblock oflag=direct"
20+
cnt=$((cnt+1))
21+
echo "$cnt) Remove bootloader on $MNT_DEV"
22+
methods[$cnt]="dd if=/dev/zero of=\"$MNT_DEV\" iflag=fullblock oflag=direct"
23+
cnt=$((cnt+1))
24+
echo
25+
done < <(blkid /dev/disk/by-id/{ata,usb,mmc}-* -o device -t PARTLABEL="loader1")
26+
27+
if find_mtd; then
28+
version2 "SPI Flash" "$MTD_DEV"
29+
30+
echo "$cnt) Install u-boot on $MTD_DEV"
31+
methods[$cnt]="flash_erase \"$MTD_DEV\" \"$MTD_OFFSET\" 0 && nandwrite -s \"$MTD_OFFSET\" \"$MTD_DEV\" \"$SPI_LOADER\""
32+
cnt=$((cnt+1))
33+
echo "$cnt) Remove bootloader on $MTD_DEV"
34+
methods[$cnt]="flash_erase \"$MTD_DEV\" \"$MTD_OFFSET\" 0"
35+
cnt=$((cnt+1))
36+
echo
37+
fi
38+
39+
while true; do
40+
echo "Select one of the options or Ctrl-C to abort:"
41+
read OPTION
42+
echo
43+
44+
if [[ "$OPTION" == "0" ]]; then
45+
exit
46+
elif [[ -z "${methods[$OPTION]}" ]]; then
47+
echo "The '$OPTION' is invalid option"
48+
continue
49+
fi
50+
51+
echo "Executing '${methods[$OPTION]}'..."
52+
confirm
53+
eval "${methods[$OPTION]}"
54+
echo
55+
done

dev-ayufan/scripts/postinst.deb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ case "$1" in
77
;;
88

99
configure)
10-
echo "Consider upgrading device bootloader with 'rock64_upgrade_bootloader.sh'."
10+
echo "Consider upgrading device bootloader with 'upgrade_bootloader.sh'."
1111
;;
1212

1313
*)

dev.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ version: .scmversion
3232

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

3838
endif

0 commit comments

Comments
 (0)