Mikhail Zhilkin 620aa80064 mediatek: MERCUSYS MR90X v1: add OpenWrt U-Boot (UBI) layout
This commit adds OpenWrt U-Boot (UBI) layout support for MERCUSYS MR90X
v1.

Stock U-Boot UBI size:   50 MiB
OpenWrt U-boot UBI size: 126 MiB

Install
-------
1. Perform steps 1-14 of the OpenWrt installation guide (use OpenWrt
   initramfs-recovery.itb instead of initramfs-kernel.bin at the step 10,
   12 and 14).
Link: https://openwrt.org/toh/mercusys/mr90x_v1#installation

2. Make backups:
```
cat /dev/mtd0 > /tmp/boot.bin
cat /dev/mtd5 > /tmp/tp_data.bin
```
   Copy /tp_data dir content, /tmp/boot.bin and /tmp/tp_data.bin and to
   your PC using scp. You can also backup the remaining partititons. Copy
   backups to a safe place, they are required for the next steps and stock
   firmware recovery.

3. Reboot to OpenWrt initramfs:
```
reboot
```

4. Copy OpenWrt ubi-bl31-uboot.fip, ubi-preloader.bin,
   ubi-squashfs-sysupgrade.itb and MT7986_EEPROM.bin, default-mac (from
   /tp_data backup) to the /tmp folder of the router using scp.

5. Prepare UBI:
```
ubidetach -p /dev/mtd3; ubiformat /dev/mtd3 -y; ubiattach -p /dev/mtd3
ubimkvol /dev/ubi0 -N fip -t static -s 1MiB
ubiupdatevol /dev/ubi0_0 /tmp/ubi-bl31-uboot.fip
ubimkvol /dev/ubi0 -N ubootenv -s 0x1f000
ubimkvol /dev/ubi0 -N ubootenv2 -s 0x1f000
```

6.  Install kmod-mtd-rw and unlock partitions:
```
opkg update && opkg install kmod-mtd-rw
insmod mtd-rw i_want_a_brick=1
mtd unlock boot
mtd unlock bl2
mtd unlock factory
```

7. Prepare "factory" partition:
```
dd if=/dev/zero bs=$((0x8000)) count=1 | tr '\000' '\377' > /tmp/factory.bin
dd if=/tmp/MT7986_EEPROM.bin of=/tmp/factory.bin conv=notrunc
dd if=/tmp/default-mac >> /tmp/factory.bin
```

8. Write "factory" partition:
```
mtd erase factory
mtd write /tmp/factory.bin factory
```

9. Write preloader partition:
```
mtd erase bl2
mtd write /tmp/ubi-preloader.bin bl2
```

10. Write OpenWrt sysupgrade image:
```
sysupgrade -n /tmp/ubi-squashfs-sysupgrade.itb
```

Recovery
--------
1. Place OpenWrt initramfs-recovery.itb image (with original name) on the
   tftp server (IP: 192.168.1.254).
2. Press "reset" button and power on the router. After ~10 sec release the
   button.
3. Use OpenWrt initramfs system for recovery.

BL2 and FIP recovery
--------------------
Use mtk_uartboot and UART connetion if BL2 or FIP in UBI is destroyed:
Link: https://github.com/981213/mtk_uartboot
Link: https://openwrt.org/toh/mercusys/mr90x_v1#serial

Return to stock:
----------------
1. Copy "boot" partition backup (boot.bin) to the /tmp dir of the router
   using scp.

2. Install kmod-mtd-rw:
```
opkg update && opkg install kmod-mtd-rw
```

3. Restore stock U-Boot:
```
   insmod mtd-rw i_want_a_brick=1
   mtd unlock boot
   mtd erase boot
   mtd write /tmp/boot.bin boot
```

4. Erase UBI and reboot:
```
mtd erase ubi
reboot
```

5. Open U-Boot web recovery, upload stock firmware image and start
   upgrade.
Link: http://192.168.1.1

6. Complete steps 1-9 of the OpenWrt installation guide to get root
   rights.
Link: https://openwrt.org/toh/mercusys/mr90x_v1#installation

7. Upload "tp_data" partition backup (tp_data.bin) to the /tmp folder of
   the router using scp.

8. Restore stock calibrations:
```
mtd write /tmp/tp_data.bin tp_data
reboot
```

9. Perform "factory restore" via stock firmware web interface.

Signed-off-by: Mikhail Zhilkin <csharper2005@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/16744
(cherry picked from commit ee8df790cae843f11f78511dd9ffac7e80a4d707)
Signed-off-by: Mikhail Zhilkin <csharper2005@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/17129
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2024-12-10 01:08:03 +01:00

244 lines
4.8 KiB
Bash
Executable File

REQUIRE_IMAGE_METADATA=1
RAMFS_COPY_BIN='fitblk'
asus_initial_setup()
{
# initialize UBI if it's running on initramfs
[ "$(rootfs_type)" = "tmpfs" ] || return 0
ubirmvol /dev/ubi0 -N rootfs
ubirmvol /dev/ubi0 -N rootfs_data
ubirmvol /dev/ubi0 -N jffs2
ubimkvol /dev/ubi0 -N jffs2 -s 0x3e000
}
xiaomi_initial_setup()
{
# initialize UBI and setup uboot-env if it's running on initramfs
[ "$(rootfs_type)" = "tmpfs" ] || return 0
local mtdnum="$( find_mtd_index ubi )"
if [ ! "$mtdnum" ]; then
echo "unable to find mtd partition ubi"
return 1
fi
local kern_mtdnum="$( find_mtd_index ubi_kernel )"
if [ ! "$kern_mtdnum" ]; then
echo "unable to find mtd partition ubi_kernel"
return 1
fi
ubidetach -m "$mtdnum"
ubiformat /dev/mtd$mtdnum -y
ubidetach -m "$kern_mtdnum"
ubiformat /dev/mtd$kern_mtdnum -y
if ! fw_printenv -n flag_try_sys2_failed &>/dev/null; then
echo "failed to access u-boot-env. skip env setup."
return 0
fi
fw_setenv boot_wait on
fw_setenv uart_en 1
fw_setenv flag_boot_rootfs 0
fw_setenv flag_last_success 1
fw_setenv flag_boot_success 1
fw_setenv flag_try_sys1_failed 8
fw_setenv flag_try_sys2_failed 8
local board=$(board_name)
case "$board" in
xiaomi,mi-router-ax3000t|\
xiaomi,mi-router-wr30u-stock)
fw_setenv mtdparts "nmbm0:1024k(bl2),256k(Nvram),256k(Bdata),2048k(factory),2048k(fip),256k(crash),256k(crash_log),34816k(ubi),34816k(ubi1),32768k(overlay),12288k(data),256k(KF)"
;;
xiaomi,redmi-router-ax6000-stock)
fw_setenv mtdparts "nmbm0:1024k(bl2),256k(Nvram),256k(Bdata),2048k(factory),2048k(fip),256k(crash),256k(crash_log),30720k(ubi),30720k(ubi1),51200k(overlay)"
;;
esac
}
platform_do_upgrade() {
local board=$(board_name)
case "$board" in
abt,asr3000|\
bananapi,bpi-r3|\
bananapi,bpi-r3-mini|\
bananapi,bpi-r4|\
bananapi,bpi-r4-poe|\
cmcc,rax3000m|\
gatonetworks,gdsp|\
h3c,magic-nx30-pro|\
jcg,q30-pro|\
jdcloud,re-cp-03|\
mediatek,mt7981-rfb|\
mediatek,mt7988a-rfb|\
mercusys,mr90x-v1-ubi|\
nokia,ea0326gmp|\
openwrt,one|\
netcore,n60|\
qihoo,360t7|\
routerich,ax3000-ubootmod|\
tplink,tl-xdr4288|\
tplink,tl-xdr6086|\
tplink,tl-xdr6088|\
tplink,tl-xtr8488|\
xiaomi,mi-router-ax3000t-ubootmod|\
xiaomi,redmi-router-ax6000-ubootmod|\
xiaomi,mi-router-wr30u-ubootmod|\
zyxel,ex5601-t0-ubootmod)
fit_do_upgrade "$1"
;;
acer,predator-w6|\
acer,predator-w6d|\
acer,vero-w6m|\
arcadyan,mozart|\
glinet,gl-mt2500|\
glinet,gl-mt6000|\
glinet,gl-x3000|\
glinet,gl-xe3000|\
smartrg,sdg-8612|\
smartrg,sdg-8614|\
smartrg,sdg-8622|\
smartrg,sdg-8632|\
smartrg,sdg-8733|\
smartrg,sdg-8733a|\
smartrg,sdg-8734)
CI_KERNPART="kernel"
CI_ROOTPART="rootfs"
emmc_do_upgrade "$1"
;;
asus,rt-ax59u|\
asus,tuf-ax4200|\
asus,tuf-ax6000)
CI_UBIPART="UBI_DEV"
CI_KERNPART="linux"
nand_do_upgrade "$1"
;;
cudy,re3000-v1|\
cudy,wr3000-v1|\
yuncore,ax835)
default_do_upgrade "$1"
;;
mercusys,mr90x-v1|\
tplink,re6000xd)
CI_UBIPART="ubi0"
nand_do_upgrade "$1"
;;
ubnt,unifi-6-plus)
CI_KERNPART="kernel0"
EMMC_ROOT_DEV="$(cmdline_get_var root)"
emmc_do_upgrade "$1"
;;
unielec,u7981-01*)
local rootdev="$(cmdline_get_var root)"
rootdev="${rootdev##*/}"
rootdev="${rootdev%p[0-9]*}"
case "$rootdev" in
mmc*)
CI_ROOTDEV="$rootdev"
CI_KERNPART="kernel"
CI_ROOTPART="rootfs"
emmc_do_upgrade "$1"
;;
*)
CI_KERNPART="fit"
nand_do_upgrade "$1"
;;
esac
;;
xiaomi,mi-router-ax3000t|\
xiaomi,mi-router-wr30u-stock|\
xiaomi,redmi-router-ax6000-stock)
CI_KERN_UBIPART=ubi_kernel
CI_ROOT_UBIPART=ubi
nand_do_upgrade "$1"
;;
*)
nand_do_upgrade "$1"
;;
esac
}
PART_NAME=firmware
platform_check_image() {
local board=$(board_name)
local magic="$(get_magic_long "$1")"
[ "$#" -gt 1 ] && return 1
case "$board" in
bananapi,bpi-r3|\
bananapi,bpi-r3-mini|\
bananapi,bpi-r4|\
bananapi,bpi-r4-poe|\
cmcc,rax3000m)
[ "$magic" != "d00dfeed" ] && {
echo "Invalid image type."
return 1
}
return 0
;;
*)
nand_do_platform_check "$board" "$1"
return $?
;;
esac
return 0
}
platform_copy_config() {
case "$(board_name)" in
bananapi,bpi-r3|\
bananapi,bpi-r3-mini|\
bananapi,bpi-r4|\
bananapi,bpi-r4-poe|\
cmcc,rax3000m)
if [ "$CI_METHOD" = "emmc" ]; then
emmc_copy_config
fi
;;
acer,predator-w6|\
acer,predator-w6d|\
acer,vero-w6m|\
arcadyan,mozart|\
glinet,gl-mt2500|\
glinet,gl-mt6000|\
glinet,gl-x3000|\
glinet,gl-xe3000|\
jdcloud,re-cp-03|\
smartrg,sdg-8612|\
smartrg,sdg-8614|\
smartrg,sdg-8622|\
smartrg,sdg-8632|\
smartrg,sdg-8733|\
smartrg,sdg-8733a|\
smartrg,sdg-8734|\
ubnt,unifi-6-plus)
emmc_copy_config
;;
esac
}
platform_pre_upgrade() {
local board=$(board_name)
case "$board" in
asus,rt-ax59u|\
asus,tuf-ax4200|\
asus,tuf-ax6000)
asus_initial_setup
;;
xiaomi,mi-router-ax3000t|\
xiaomi,mi-router-wr30u-stock|\
xiaomi,redmi-router-ax6000-stock)
xiaomi_initial_setup
;;
esac
}