# Banana Pi R4 - Debricking Guide
## Prerequisite
If you are using Ubuntu 24.04. You can install all the tools via:
```shell
$ sudo apt update
$ sudo apt install device-tree-compiler gcc-aarch64-linux-gnu \
gettext libncurses-dev git
```
## Build ATF
Since we need the ATF to have UART boot capacity. We need to rebuild it from sources.
You can get the source code from GitHub:
```shell
$ git clone -b mtksoc --single-branch https://github.com/mtk-openwrt/arm-trusted-firmware
```
Use the default config for convenience
```shell
$ make mt7988_rfb_spim_nand_defconfig
```
Then enter menuconfig for further configuration
```shell
$ make menuconfig
```
Ensure to make the following options properly configured:
- `_BOOT_DEVICE_RAM=y`
- `_RAM_BOOT_RAM_BOOT_UART_DL=y`
- `_BUILD_FIP=n`
Then build the BL2 image by
```shell
$ make
```
It should be located under `./build/mt7988/release/bl2.bin`
## Build mtk_uartboot
Install the rust compiler by the instructions at [Getting started - Rust Programming Language](https://www.rust-lang.org/learn/get-started)
Download the source code from GitHub
```shell
$ git clone https://github.com/981213/mtk_uartboot.git
```
Build it with
```shell
$ cargo build
```
The compiled executable should be placed under `./target/debug/`
## Debrick the device
Connect the debug console to your Banana Pi R4 and run the command to debrick
```shell
$ sudo ./mtk_uartboot -p <BL2_BIN> --aarch64 -f <FIP_BIN> -s <TTY_DEV> && sudo picocom -b 115200 <TTY_DEV>
```
Because the u-boot will be brought up right after recovery, we should run the console program just after the uartboot program finish. This makes us able to see u-boot's bootmenu.
After seeing `Handshake...`, you can turn on the power supply of your Banana Pi. It should negotiate the baudrate with the program and download all the files you provide automatically.
When seeing the bootmenu of u-boot, it means the recovery has completed. Congrats to you. 🎉