[Tutorial] Build, customize and use MediaTek open-source U-Boot and ATF

I don’t know what you are missing, but I can confirm that ATF bl2 built from mtk-openwrt tree definitely works fine, you can download the resulting bl2 here to give it a shot: https://downloads.openwrt.org/snapshots/targets/mediatek/filogic/openwrt-mediatek-filogic-bananapi_bpi-r4-snand-preloader.bin

Thanks hackpascal!

I was able to build latest version of U-boot with latest version for AT-F with patch for PCIe 3.0 from here: https://forum.banana-pi.org/t/bpi-r4-failed-attempt-add-nvme-support-on-uboot/ and I’m able to see the NVME in U-boot. Now I’m working to a script to automate the images build for different scenarios:

  • MMC/eMMC - Firmware + U-boot + Booting + Root FS on MMC/eMMC
  • MMC/eMMC + NVME - Firmware + U-boot on MMC/eMMC and Booting + Root FS on MMC/eMMC on NVME
  • FLASH + NVME - Firmware + U-boot on SPI Flash and Booting + Root FS on MMC/eMMC on NVME

But I have a question to all of you. The original SPI Flash U-Boot has these commands:

mtkautoboot- Display MediaTek bootmenu
mtkboardboot- Boot MTK firmware
mtkload   - MTK image loading utility
mtkupgrade- MTK firmware/bootloader upgrading utility

Where are these commands present? There is a custom U-boot that has these commands? I have no clue where I can add these commands. Anybody has any idea?

These are commands from sdk,not upstreamed in u-boot…why do you need these commands? There is a bootmenu,so you do not need mtk specific implementation.load is maybe combination of load+bootm/go. mtkboardboot sounds similar and upgrade will only work with sdk image…openwrt has different structure and has its own upgrade mechanism.

So why do you need these commands? If you really miss any feature from it it should be added to mainline commands in a generic way.

Thanks for clarifications.

I’ve checked the U-boot OpenWRT source and I found all these commands in boards/mediatek/common.

Anyway there are not portable to newer versions of U-boot mainly because ubi.c moved a lot of functions to static, functions used by one of the helpers.

As you says and also as I’ve noticed I don’t need them.

@hackpascal In the case of MT7981B/MT7986A, when there is no switch, the three storage media SPI-NOR, SPI-NAND, and SD have systems, and can the default access to the system be from the SD card? When there is no SD card or the SD card system is damaged, can the system be accessed from SPI-NOR or SPI-NAND?

Booting from SD is a fallback if booting from SPI-NAND fails. For other boot devices, you must change the bootstrap pins to the corresponding combinations.

Here’s updated procedures for flashing OpenWRT v23.05.0 & later on BPi R3:

Does mt7981b support nand booting the system, and then mounting emmc or SD card as storage space?

Yes, that should work. Since I can see the NAND when booting from eMMC, it should work vice versa.

does anyone know How to configure mt7981, uboot, openwrt, dts, default spi nand to boot into the system, and mount SD card as an external simple storage device? Thank you

Why asking again? This thread is only uboot+atf,no kernel/userspace.

When you want openwrt please ask there or in a openwrtone thread (maybe create one).

Ok, sorry, I’ll try to ask on openwrt one, thank you

Hi @hackpascal, it looks like current atf code misses some files (wdt,mmc,spi,efuse)

Or has build been changed? I see menuconfig support compared to my old tree.

after i got the missing efuse error also on my default atf-branch i had to delete ./build/mt7988/release/ manually (make clean does not clean it up) to be able to compile my old branch again, but i got a strange error

  LD      build/mt7988/release/bl2/bl2.elf
/bin/sh: 1: -g: not found

when compiling the new tree, it also seems like the MAKE_BUILD_STRINGS was dropped from make_helpers/build_macros.mk which i had used to print the bootdevice in bl2/fip

looks like the error above was caused by my comit readding the MAKE_BUILD_STRINGS, to where was this moved?

uploaded my full tree to GitHub - frank-w/u-boot at mtk-atf-2025

finally got the 8GB variant working with the new atf-code…

NOTICE:  BL2: v2.12.0(release):v2.11.0-1599-gc6dad9faceb5
NOTICE:  BL2: Built : 23:34:17, Apr  7 2025
NOTICE:  WDT: [40000000] Software reset (reboot)
NOTICE:  CPU: MT7988
NOTICE:  EMI: DDR4 4BG mode
NOTICE:  EMI: Using DDR unknown settings
NOTICE:  EMI: Detected DRAM size: 8192 MB
NOTICE:  EMI: complex R/W mem test passed
NOTICE:  LVTS: Enable thermal HW reset
NOTICE:  BL2: Booting BL31
NOTICE:  BL31: v2.12.0(release):v2.11.0-1599-gc6dad9faceb5
NOTICE:  BL31: Built : 23:34:20, Apr  7 2025

only can’t get the build-version string working

linux also sees full 8G of ram

root@bpi-r4-8G:~# free
               total        used        free      shared  buff/cache   available
Mem:         8140504      202848     7922844        1592       89244     7937656
Swap:              0           0           0

but noticed that powerdown is not supported by atf-code (was working with old atf)

[   82.829046] reboot: Power down
ERROR:   Power-down unsupported.
PA�
F0: 102B 0000

power down doesn’t really supported (was implemented as WFI). Only soft reset is supported.

But my older tree does not print this eror and does not reboot…then atf imgo should hang if it cannot powerdown the pmic.

If I remember correctly, I was also struggling, it has to do with the tags in the git repo. Also pull the tags when cloning.

I got this working passing BUILD_STRING as parameter. See mtk-atf-2025 branch. This is also the way openwrt does it to extend the version string

@hackpascal do you miss drivers (mmc,…) above in current atf? I cherry-picked them into my new branch and this way it works,but have to remove build dir completely when using the older atf before (make clean is not enough,so there is something missing too).

regarding old ATF and poweroff…the older ATF shows this, but does not do reboot on poweroff

[   82.860016] reboot: Power down
PANIC at PC : 0x00000000430052a4
1 Like

u-boot .config : config.txt (44,7 KB) german (Beschreibung): summary.pdf (214,6 KB) english description: summary_en.pdf (211,2 KB)

Hello, i built an openbsd7.6 image with u-boot patch, like descrition above. But the .dtb files are still for raspberry pi, can it be fixed, by adding the openwrt boot .dtb files? Can anybody share the .config for the openwrt u-boot ? Because it must look very similar to the openbsd u-boot above : config.txt, or a diff?

The problem is, that the banana RPI R3 will not boot with mine. Something important must be left in the BOOT partition, you can reproduce mine, by following the description above.

Anybody interested? or have an idea to fix the boot?

I would make an extern topic, but this issue is also very common.

https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=package/boot/uboot-mediatek/patches/442-add-bpi-r3-mini.patch;hb=HEAD

Openwrt does not use BOOT gpt partition like i do (and sdk did)…they flash kernel fit/itb (including dtbs) to specific offset or ubi partition.

Not sure openbsd has all necessary drivers as imho they do not use linux kernel

Is there any output on uart? Without uart it is nearly impossible to find error…

edit: tried without the additional patches (mmc,spi,efuse) and still seem to work…looks like my error was only because of unclean build-directory, dropped the commits from the tree to avoid confusion

Is there any possible solution to use a general atf .bin to use on both 4GB and 8GB versions of the R4?

Seems like the R4 gives nvme and nand errors when booting 8bg board with 4gb atf build.

To reduce the number of pre-build images, it would be nice if both versions can boot from 1 atf .bin, and once booted, the 8gb build can be written to mmc/nand of the 8gb board.