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

Ok, I hope we’ll manage to pick up that change in OpenWrt without breaking existing images and devices out there…

Regarding BL32/OP-TEE: Will MediaTek provide patches or a source repository to build OP-TEE? I’m asking because using OP-TEE and fTPM paves the road towards ARM System Ready (ie. standardized UEFI boot), which would obviously be very useful (at least on devices using eMMC/uSD storage) to install and run unmodified general purpose OS (given kernel driver support for the SoC and peripherals is present, of course).

I personally don’t like the UEFI approach to depend on a single DOS/FAT filesystem to load boot artifacts from, uImage.FIT single image would be my preference (as in that way the same image can work on NOR, NAND, eMMC and uSD). However, SystemReady seems to be what industry converges towards, esp. when it comes to white-label hardware.

This is fixed in 2023 version. The R64 reboots ok.

you mean version with mt7988 support? good to know, then i can also switch to new atf-code in my pipeline for all boards

Yup, the version with mt7988 support.

I have forked my ‘enhanced’ ATF also from the mtksoc-20230724 branch now, since the mt7622 code has the bug removed.

You can find it in my wip branch, it is also ready for R4, which should work , but is untested. R4 is in transit :wink:

Thanks to the apsoc_common code, now I only have 4 commits on top of the original code.

@hackpascal do you have plans to support usb-booting in uboot?

Imho soc bootrom only supports spi and mmc,but after uboot is loaded you can load kernel+rootfs from usb

Having it on uboot would be already nice, but I’d argue that even in the bootrom it could be a boon for recovery on designs such as the r3-mini.

Hi,

i tried the new way for creating the gpt on emmc (works on sdmmc without problems) on bpi-r3.

emmc-boot does not find fip partition, but it is there

EC: 0000 0000 [2000]

BL2: v2.9(release):v2.9.0-349-g55576307fd8c emmc
ERROR:   Partition 'fip' not found                                              
ERROR:   FIP boot source initialization failed with -2

when i boot from nand and boot kernel+initrd and run parted on the emmc i see this:

# parted /dev/mmcblk0                                                           
GNU Parted 3.3                                                                  
Using /dev/mmcblk0                                                              
Welcome to GNU Parted! Type 'help' to view a list of commands.                  
(parted) p                                                                      
Model: MMC 008GB0 (sd/mmc)                                                      
Disk /dev/mmcblk0: 7818MB                                                       
Sector size (logical/physical): 512B/512B                                       
Partition Table: gpt                                                            
Disk Flags:                                                                     
                                                                                
Number  Start   End     Size    File system  Name        Flags                  
 2      4194kB  4719kB  524kB                u-boot-env                         
 3      4719kB  6816kB  2097kB               factory                            
 4      6816kB  8913kB  2097kB               fip                                
 5      8913kB  114MB   105MB   fat16        boot                               
 6      114MB   6556MB  6442MB  ext4         rootfs                             
                                                                                
(parted)

so it looks right for me…do you have any idea?

this is how i built the partition table:

device=emmc does create a partition with label gpt instead of the bl2 partition

if i flash the older gpt image (generated by the python2-tool) fip partition is found, but if i then try parted, i get the error that main gpt is corrupt

# parted /dev/mmcblk0
GNU Parted 3.5                                                                  
Using /dev/mmcblk0                                                              
Welcome to GNU Parted! Type 'help' to view a list of commands.                  
(parted) p                                                                      
Error: The primary GPT table is corrupt, but the backup appears OK, so that will
be used.                                                                        
OK/Cancel?

do i miss anything when creating the partition with sgdisk?

Atf code for finding fip partition seems completely common for sd+emmc so i wonder why sd works and emmc not. I see there is a dump function…can i simply trigger it with increasing loglevel on compile? @ericwoud do you tried emmc on r3 yet and can help to debug this?

I have started on emmc many times, but it was version v2.8.

I remember, for R3 v2.8 used fixed ‘fip’ partition and did not search for partition name on R3. it was different from the R64, which did use the partlabel.

I changed that in my own fork of v2.8, to also use partlabel.

v2.9 is is common code, all model search fip partition.

This is how I create the gpt:

I have changed the name ‘fip’ to ‘bpir3-emmc-fip’ also in atf code, and it finds it ok.

is this your emmc-partition table? looks like the sdmmc one with bootable bl2 as first one…

i made for emmc the gpt-part instead based on the partition sheme i got in an older atf version, but my last misses this first one completely and causes different numbering (so boot and root-partition number do not match same on sdmmc)

but it looks like this first one breaks, see top-commit message

I use the same partition layout, sdmmc and emmc. I also use a trick on R3 so I do not have to use mmcblk0boot0. I already did on R64, but that trick did not work on R3, so I have another one. But it also works using mmcblk0boot0.

On R3mini the trick probably won’t work, so probably need to use mmcblk0boot0.

But if in your case parted complains about corrupt got table, then maybe this is why atf will also not be able to use it …

the old gpt img file works in gpt but also complains the gpt error which cannot be fixed (wants to use the backup-gpt which is the “fixed” from my image)…i cannot fix/print the partition from the old gpt.img (generated by the python2 script)

it looks like the first partition (from offset 0 with label gpt) breaks the verify-function in bl2…i try to remove it and add bl2-partition like i do for sdmmc…

I di not see you use sgdisk to create a partition with label got…

No need for a gpt-partition at the gpt-table location…

i know, this is because of partition-numbering (to have boot=5 and root=6 when bl2 is missing), took this from an older json-file i got from bpi/mtk

have now manually created my gpt

# sgdisk -a 1 -n 1:34:8191 -A 1:set:2 -t 1:8300 -c 1:"bl2" /dev/mmcblk0
# sgdisk -a 1 -n 2:8192:9215 -A 2:set:63 -t 2:8300 -c 2:"u-boot-env" /dev/mmcblk0
# sgdisk -a 1 -n 3:9216:13311 -A 3:set:63 -t 3:8300 -c 3:"factory" /dev/mmcblk0                                                          
# sgdisk -a 1 -n 4:13312:17407 -A 4:set:63 -t 4:8300 -c 4:"fip" /dev/mmcblk0
# sgdisk -a 1024 -n 5:17408:222207 -t 5:8300 -c 5:"boot" /dev/mmcblk0
# sgdisk -a 1024 -n 6:222208:12805120 -t 6:8300 -c 6:"rootfs" /dev/mmcblk0
# sgdisk -p /dev/mmcblk0
[  715.532262]  mmcblk0: p1 p2 p3 p4 p5 p6
Disk /dev/mmcblk0: 15269888 sectors, 7.3 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 4C990539-7275-49B2-A19C-5F72A91B3FFC
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 15269854
Partitions will be aligned on 2-sector boundaries
Total free space is 2464734 sectors (1.2 GiB)                                                         
                                                                                                      
Number  Start (sector)    End (sector)  Size       Code  Name                                         
   1              34            8191   4.0 MiB     8300  bl2                                          
   2            8192            9215   512.0 KiB   8300  u-boot-env                                   
   3            9216           13311   2.0 MiB     8300  factory                                      
   4           13312           17407   2.0 MiB     8300  fip                                          
   5           17408          222207   100.0 MiB   8300  boot                                         
   6          222208        12805120   6.0 GiB     8300  rootfs

at least i have now the fip partition found…but booting fip does not work…maybe i have to flash fip again

Partition 'bl2' found, check for fip                                                                  
Partition 'u-boot-env' found, check for fip                                                           
Partition 'factory' found, check for fip                                                              
Partition 'fip' found, check for fip                                                                  
Partition 'fip' match fip ;)                                                                          
ERROR:   BL2: Failed to load image id 3 (-2)

i guess bl12 and fip are bundled (bl2 checks checksum of fip or something like this)

edit: yes reflashed bl2+fip from same compilation fixes emmc bootup…so sd+emmc partition-table should be same to work. @ericwoud thanks for looking into it, no need for further digging/testing

EC: 0000 0000 [2000]                                                                                  
NOTICE:  BL2: v2.9(release):v2.9.0-349-g55576307fd8c emmc                                                                                                  
NOTICE:  BL31: v2.9(release):v2.9.0-349-g55576307fd8c emmc
U-Boot 2024.01-rc1-bpi-00015-g1e4f6220a75b-dirty (Oct 24 2023 - 13:09:30 +0200)

Edit:after unpacking rootfs and kernel, system is running on emmc :slight_smile:

no. there’s no such bundle.

Thx for the info,but then it is unclear why the fip cannot be started and needed to be reflashed…used same offsets/sectors on recreation the gpt.with the bin gpt i flashed for testing fip was loaded and after that i only flashed bl2 with debug-code and recreated the gpt with sgdisk.

Now that i know that sdmmc gpt works too on emmc,easiest way for install on r3 is to simply flash sdmmc image (from initrd booted via spi-nand/nor) to emmc and maybe reflash fip afterwards and flash bl2 to boot0 (but eric says boot0 is not needed).

There is a trick that works on R3 only:

Build emmc ATF, but pack it with sdmmc bootrom-header. Then set the R3 switches in SD boot mode, but switch the MMC hardware to emmc. Set the MMC CSD boot to 7. Et voila, you can boot emmc without mmcblk0boot0.

Didn’t say it was easy :wink:

I’ve solve this issue.

This is caused by invalid value of ‘num of sectors’ of the MBR:

VERBOSE: MBR header entry has an invalid number of sectors
VERBOSE: Failed to access image id=28 (-22)
ERROR:   Failed to parse partition with image id = 28
ERROR:   Partition 'fip' not found
ERROR:   FIP boot source initialization failed with -2

Introduced by refactor(partition): get GPT header location from MBR · ARM-software/arm-trusted-firmware@fce8a70 · GitHub

The ‘num of sectors’ is intended to be the total number of blocks of the eMMC user data area.
However the actual number of blocks varies between emmc chips.
That is, all GPT header generated by tool offline can’t know the number of blocks, and that’s why the value is invalid (Filled with ffffffff).

Of course you can recreate the MBR/GPT online using parted. But to make it always work, I have to patch the ATF to tell it the actual ‘num of sectors’ before parsing MBR.

I’ll update the source code later

1 Like