User Tools

Site Tools



en:bpi-r3:uboot

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
en:bpi-r3:uboot [2022/08/20 17:14] – [booting kernel] franken:bpi-r3:uboot [2023/06/08 17:06] – external edit 127.0.0.1
Line 1: Line 1:
 +====== U-Boot ======
  
 +[[https://u-boot.readthedocs.io/en/latest/develop/release_cycle.html#current-status|Release cycle]]
 +
 +Mtk uboot: https://github.com/mtk-openwrt/u-boot
 +
 +Mtk atf: https://github.com/mtk-openwrt/arm-trusted-firmware
 +
 +My uboot: https://github.com/frank-w/u-boot/tree/2022-10-r3
 +===== building =====
 +
 +Uboot needs to build into atf bl33.
 +
 +  * https://github.com/frank-w/u-boot/tree/2022-10-r3
 +  * https://github.com/frank-w/u-boot/tree/r3-atf
 +
 +So basicly do these in both branches (first uboot then atf)
 +
 +  ./build.sh importconfig
 +  ./build.sh
 +
 +Then flash build/mt7986/release/ bl2.img to bl2-partition and fip.bin to fip-partition
 +
 +  sudo dd if=build/mt7986/release/bl2.img of=/dev/sdb1
 +  sudo dd if=build/mt7986/release/fip.bin of=/dev/sdb4
 +
 +ATF branch contains gpt file (gpt_sdmmc_100m6g.img) for flashing first and script to create basic image with bootchain
 +
 +  ./build.sh createimg
 +===== booting kernel =====
 +
 +
 +  setenv kaddr 0x46000000
 +  ls mmc 0:5
 +  fatload mmc 0:5 ${kaddr} bpi-r3-5.19.0-rc1-r3.itb
 +  #setenv bootargs "earlycon=uart8250,mmio32,0x11002000 console=ttyS0"
 +  bootm ${kaddr}
 +  
 +board seems to hang in 5.19-rc1 when console=ttyS0 is passed
 +
 +with my environment it is easy to boot kernel from usb
 +
 +  BPI-R3> usb start
 +  BPI-R3> setenv device usb #was mmc before
 +  BPI-R3> setenv partition 0:1 #was 0:5 before for mmc
 +  BPI-R3> setenv fit bpi-r3-sfp.itb #change filename if needed
 +  BPI-R3> run newboot #will load bpi-r3.itb
 +
 +load fip with alternate dtb (like from my current 5.19-r3 branch, second config contains emmc dtb)
 +
 +  BPI-R3> fatload usb 0:1 $loadaddr bpi-r3.itb
 +  17032308 bytes read in 1427 ms (11.4 MiB/s)
 +  BPI-R3> bootm ${loadaddr}#conf-2
 +
 +load external initrd (run before "run newboot"):
 +
 +  BPI-R3> setenv rdaddr 0x48000000
 +  BPI-R3> setenv root "/dev/ram0 rw"
 +  BPI-R3> setenv bootopts "debug=7 initrd=${rdaddr},20M"
 +  BPI-R3> fatload usb 0:1 ${rdaddr} rootfs.cpio.zst
 +
 +
 +latest uboot with usb, initrd and bootconf
 +
 +  BPI-R3> run useusb
 +  BPI-R3> setenv initrd rootfs.cpio.zst
 +  BPI-R3> setenv fit bpi-r3-sfp-no-rd.itb
 +  BPI-R3> setenv bootconf "#conf-2" #
 +  BPI-R3> run newboot
 +
 +  #using spi dt-overlay
 +  BPI-R3> setenv bootconf "#conf-sd-nor" #conf-sd-nor=sd base dts + nor dt overlay
 +
 +  setenv buildargs 'setenv bootargs "board=${board} earlycon=uart8250,mmio32,0x11002000  ${bootopts} root=${root}"'
 +===== bootmedium selection =====
 +
 +| |  top  ||  bottom  ||
 +| |sw1/A|sw2/B (spi/mmc)|sw5/C (nor/nand)|sw6/D (sd/emmc)|
 +|spim-nor|low|low|low|x|
 +|spim-nand|high|low|high|x|
 +|emmc|low|high|x|low|
 +|sd|high|high|x|high|
 +
 +To access emmc you need to go over spi-nand/nor:
 +
 +Bootup with sdcard (put spi-nand bl2 + fip with emmc-and usb-support as files on card) and flash them like described [[#nand_flash|here]] then boot from spi (sw2 to boot from spi-nand + change sw6 to have access to emmc) and then [[#writing_emmc|flash emmc]]. Finally change sw2 back to boot from emmc and sw1 has to be changed too.
 +===== nand flash =====
 +
 +https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/mediatek/mt7986a-bananapi-bpi-r3-nand.dtso
 +
 +<code>
 +BPI-R3> ls mmc 0:5
 + 17019020   bpi-r3.itb
 +   579401   fip_nand.bin
 +   217712   bl2_nand.img
 +
 +3 file(s), 0 dir(s)
 +
 +BPI-R3> mtd list
 +List of MTD devices:
 +* spi-nand0
 +  - device: spi_nand@1
 +  - parent: spi@1100a000
 +  - driver: spi_nand
 +  - path: /spi@1100a000/spi_nand@1
 +  - type: NAND flash
 +  - block size: 0x20000 bytes
 +  - min I/O: 0x800 bytes
 +  - OOB size: 64 bytes
 +  - OOB available: 24 bytes
 +  - 0x000000000000-0x000008000000 : "spi-nand0"
 +* nmbm0
 +  - type: Unknown
 +  - block size: 0x20000 bytes
 +  - min I/O: 0x800 bytes
 +  - OOB size: 64 bytes
 +  - OOB available: 24 bytes
 +  - 0x000000000000-0x000007800000 : "nmbm0"
 +          - 0x000000000000-0x000000100000 : "bl2"
 +          - 0x000000100000-0x000000180000 : "u-boot-env"
 +          - 0x000000180000-0x000000380000 : "factory"
 +          - 0x000000380000-0x000000580000 : "fip"
 +          - 0x000000580000-0x000004580000 : "ubi"
 +
 +BPI-R3> printenv loadaddr
 +loadaddr=0x46000000
 +
 +BPI-R3> mtd erase spi-nand0
 +Erasing 0x00000000 ... 0x07ffffff (1024 eraseblock(s))
 +BPI-R3> fatload mmc 0:5 $loadaddr bl2_nand.img
 +217712 bytes read in 24 ms (8.7 MiB/s)
 +BPI-R3> mtd write spi-nand0 $loadaddr 0x0 0x100000
 +Writing 1048576 byte(s) (512 page(s)) at offset 0x00000000
 +
 +BPI-R3> fatload mmc 0:5 $loadaddr fip_nand.bin
 +579401 bytes read in 60 ms (9.2 MiB/s)
 +BPI-R3> mtd write spi-nand0 $loadaddr 0x380000 0x200000
 +Writing 2097152 byte(s) (1024 page(s)) at offset 0x00380000
 +</code>
 +
 +===== nor flash =====
 +
 +https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/mediatek/mt7986a-bananapi-bpi-r3-nor.dtso
 +
 +<code>
 +MT7986> sf probe
 +SF: Detected w25q256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB
 +MT7986> sf erase 0 0x180000
 +SF: 1572864 bytes @ 0x0 Erased: OK
 +MT7986> fatload usb 0:1 ${loadaddr} 2023.04/bpi-r3_nor_bl2.img
 +202160 bytes read in 21 ms (9.2 MiB/s)
 +MT7986> sf write ${loadaddr} 0 0x40000
 +device 0 offset 0x0, size 0x40000
 +SF: 262144 bytes @ 0x0 Written: OK
 +MT7986> fatload usb 0:1 ${loadaddr} 2023.04/bpi-r3_nor_fip.bin
 +255509 bytes read in 25 ms (9.7 MiB/s)
 +MT7986> sf write ${loadaddr} 0x100000 0x80000
 +device 0 offset 0x100000, size 0x80000
 +SF: 524288 bytes @ 0x100000 Written: OK
 +</code>
 +
 +if you get this error on bootup
 +
 +  NOTICE:  BL31: Built : 20:24:28, Apr  5 2023                                    
 +  No valid device tree binary found at 0000000041e92f20                           
 +  initcall sequence 0000000041e621a8 failed at call 0000000041e57800 (err=-2)     
 +  ### ERROR ### Please RESET the board ###
 +
 +you need to compress bl31
 +
 +  xz -f -e -k -9 -C crc32 u-boot.bin
 +
 +and use the resulting u-boot.bin.xz as BL33
 +
 +==== kernel/initrd ====
 +
 +currently untested...
 +
 +<code>
 +label = "recovery"; //initrd max 10MB
 +reg = <0x180000 0xa80000>;
 +
 +label = "fit"; //kernel max 20MB
 +reg = <0xc00000 0x1400000>;
 +</code>
 +
 +<code>
 +MT7986> sf probe
 +SF: Detected w25q256 with page size 256 Bytes, erase size 4 KiB, total 32 MiB
 +MT7986> sf erase 0x180000 0xa80000
 +SF: 11010048 bytes @ 0x180000 Erased: OK
 +MT7986> fatload usb 0:1 ${loadaddr} rootfs_bpi-r3.cpio.zst
 +9501203 bytes read in 798 ms (11.4 MiB/s)
 +MT7986> sf write ${loadaddr} 0x180000 ${filesize}
 +device 0 offset 0x180000, size 0x90fa13
 +SF: 9501203 bytes @ 0x180000 Written: OK
 +MT7986> sf erase 0xc00000 0x1400000
 +SF: 20971520 bytes @ 0xc00000 Erased: OK
 +MT7986> fatload usb 0:1 ${loadaddr} bpi-r3-6.1.itb
 +8424540 bytes read in 708 ms (11.3 MiB/s)
 +MT7986> sf write ${loadaddr} 0xc00000 ${filesize}                               
 +device 0 offset 0xc00000, size 0x808c5c                                         
 +SF: 8424540 bytes @ 0xc00000 Written: OK
 +</code>
 +
 +loading it:
 +
 +<code>
 +MT7986> setenv useinitrd 'setenv root "/dev/ram0 rw";setenv bootopts "${bootopts} initrd=${rdaddr},20M";'
 +MT7986> sf read ${rdaddr} 0x180000 0xa80000
 +device 0 offset 0x180000, size 0xa80000
 +SF: 11010048 bytes @ 0x180000 Read: OK
 +MT7986> sf read ${kaddr} 0xc00000 0x1400000
 +device 0 offset 0xc00000, size 0x1400000
 +SF: 20971520 bytes @ 0xc00000 Read: OK
 +MT7986> setenv bootnor 'run useinitrd;run setbootconf; run buildargs; bootm ${kaddr}${bootconf};'
 +MT7986> run bootnor
 +</code>
 +===== usb =====
 +
 +<code>
 +BPI-R3> usb start
 +starting USB...
 +Bus xhci@11200000: xhci-mtk xhci@11200000: hcd: 0x0000000011200000, ippc: 0x0000
 +xhci-mtk xhci@11200000: ports disabled mask: u3p-0x0, u2p-0x0
 +xhci-mtk xhci@11200000: u2p:2, u3p:1
 +Register 300010f NbrPorts 3
 +Starting the controller
 +USB XHCI 1.10
 +scanning bus xhci@11200000 for devices... 3 USB Device(s) found
 +       scanning usb for storage devices... 1 Storage Device(s) found
 +BPI-R3> ls usb 0:1
 +            efi/
 +  1023582   ramdisk.img
 +  1365903   initrd.img
 +  2956137   install.img
 + 456908800   system.sfs
 +  4767728   kernel
 +
 +5 file(s), 1 dir(s)
 +
 +BPI-R3>
 +</code>
 +
 +from testing usb-sockets near gpio-header (connected to an usb2-hub):
 +
 +<code>
 +BPI-R3> usb tree                                                                
 +USB device tree:                                                                
 +  1  Hub (5 Gb/s, 0mA)                                                          
 +  |  U-Boot XHCI Host Controller                                                
 +  |                                                                             
 +  +-2  Hub (480 Mb/s, 100mA)                                                    
 +    |   USB 2.0 Hub                                                             
 +    |                                                                           
 +    +-3  Mass Storage (480 Mb/s, 200mA)                                         
 +         USB      Flash Disk       906B030002F4 
 +</code>
 +===== writing emmc =====
 +
 +get emmc/gpt information:
 +<hidden>
 +<code>
 +BPI-R3> mmc info
 +Device: mmc@11230000
 +Manufacturer ID: 11
 +OEM: 0
 +Name: 008GB0
 +Bus Speed: 52000000
 +Mode: MMC High Speed (52MHz)
 +Rd Block Len: 512
 +MMC version 5.1
 +High Capacity: Yes
 +Capacity: 7.3 GiB
 +Bus Width: 8-bit
 +Erase Group Size: 512 KiB
 +HC WP Group Size: 4 MiB
 +User Capacity: 7.3 GiB WRREL
 +Boot Capacity: 4 MiB ENH
 +RPMB Capacity: 4 MiB ENH
 +Boot area 0 is not write protected
 +Boot area 1 is not write protected
 +
 +BPI-R3> gpt read mmc 0
 +Partition 1:
 +Start 0MiB, size 0MiB
 +Block size 512, name gpt
 +Type U-Boot, bootable 0
 +UUID df7a38f6-9df4-11ec-a1e0-1c1b0d6d28f5
 +
 +Partition 2:
 +Start 4MiB, size 0MiB
 +Block size 512, name u-boot-env
 +Type U-Boot, bootable 0
 +UUID 19a4763a-6b19-4a4b-a0c4-8cc34f4c2ab9
 +
 +Partition 3:
 +Start 4MiB, size 2MiB
 +Block size 512, name factory
 +Type U-Boot, bootable 0
 +UUID 8142c1b2-1697-41d9-b1bf-a88d76c7213f
 +
 +Partition 4:
 +Start 6MiB, size 2MiB
 +Block size 512, name fip
 +Type U-Boot, bootable 0
 +UUID 18de6587-4f17-4e08-a6c9-d9d3d424f4c5
 +
 +Partition 5:
 +Start 8MiB, size 32MiB
 +Block size 512, name kernel
 +Type U-Boot, bootable 0
 +UUID 971f7556-ef1a-44cd-8b28-0cf8100b9c7e
 +
 +Partition 6:
 +Start 40MiB, size 256MiB
 +Block size 512, name rootfs
 +Type U-Boot, bootable 0
 +UUID 309a3e76-270b-41b2-b5d5-ed8154e7542b
 +
 +success!
 +</code>
 +</hidden>
 +
 +load gpt file and flash it
 +<code>
 +BPI-R3> usb start
 +starting USB...
 +Bus xhci@11200000: xhci-mtk xhci@11200000: hcd: 0x0000000011200000, ippc: 0x0000
 +xhci-mtk xhci@11200000: ports disabled mask: u3p-0x0, u2p-0x0
 +xhci-mtk xhci@11200000: u2p:2, u3p:1
 +Register 300010f NbrPorts 3
 +Starting the controller
 +USB XHCI 1.10
 +scanning bus xhci@11200000 for devices... 3 USB Device(s) found
 +       scanning usb for storage devices... 1 Storage Device(s) found
 +BPI-R3> ls usb 0:1
 +            efi/
 +  1365903   initrd.img
 +  2956137   install.img
 +  4767728   kernel
 +   606240   uboot_mtk_emmc_usb.bin
 +   217712   bl2_snand_usb.img
 +   631937   fip_snand_usb.bin
 +   634345   fip_emmc_usb.bin
 +   601344   uboot_emmc_usb.bin
 +    17408   gpt_emmc_100m6g.img
 +   200072   bl2_emmc_usb.img
 + 17032308   bpi-r3.itb
 +
 +11 file(s), 1 dir(s)
 +
 +BPI-R3> fatload usb 0:1 $loadaddr gpt_emmc_100m6g.img
 +17408 bytes read in 4 ms (4.2 MiB/s)
 +BPI-R3> mmc erase 0x0 0x400;mmc write ${loadaddr} 0x0 0x400
 +
 +MMC erase: dev # 0, block # 0, count 1024 ... 1024 blocks erased: OK
 +
 +MMC write: dev # 0, block # 0, count 1024 ... 1024 blocks written: OK
 +</code>
 +verify gpt is changed (kernel is now 100MiB - 32 before, rootfs 6144MiB - 256 before):
 +<hidden>
 +<code>
 +BPI-R3> gpt read mmc 0
 +Partition 1:
 +Start 0MiB, size 0MiB
 +Block size 512, name gpt
 +Type U-Boot, bootable 0
 +UUID 4a32da88-f334-11ec-94f0-112beaedcdcb
 +
 +Partition 2:
 +Start 4MiB, size 0MiB
 +Block size 512, name u-boot-env
 +Type U-Boot, bootable 0
 +UUID 19a4763a-6b19-4a4b-a0c4-8cc34f4c2ab9
 +
 +Partition 3:
 +Start 4MiB, size 2MiB
 +Block size 512, name factory
 +Type U-Boot, bootable 0
 +UUID 8142c1b2-1697-41d9-b1bf-a88d76c7213f
 +
 +Partition 4:
 +Start 6MiB, size 2MiB
 +Block size 512, name fip
 +Type U-Boot, bootable 0
 +UUID 18de6587-4f17-4e08-a6c9-d9d3d424f4c5
 +
 +Partition 5:
 +Start 8MiB, size 100MiB
 +Block size 512, name kernel
 +Type U-Boot, bootable 0
 +UUID 971f7556-ef1a-44cd-8b28-0cf8100b9c7e
 +
 +Partition 6:
 +Start 108MiB, size 6144MiB
 +Block size 512, name rootfs
 +Type U-Boot, bootable 0
 +UUID 309a3e76-270b-41b2-b5d5-ed8154e7542b
 +
 +success!
 +BPI-R3>
 +</code>
 +</hidden>
 +
 +now flash bl2 to boot0 block and fip to fip partition
 +
 +short way using my environment:
 +<code>
 +MT7986> setenv bl2file 2023.04/bpi-r3_emmc_bl2.img                                                                                                                                          
 +MT7986> setenv fipfile 2023.04/bpi-r3_emmc_fip.bin                                                                                                                                          
 +MT7986> run wremmc                                                                                                                                                                          
 +bl2file=2023.04/bpi-r3_emmc_bl2.img                                                                                                                                                         
 +195976 bytes read in 20 ms (9.3 MiB/s)                                                                                                                                                      
 +                                                                                                                                                                                            
 +MMC erase: dev # 0, block # 0, count 1024 ... 1024 blocks erased: OK                                                                                                                        
 +                                                                                                                                                                                            
 +MMC write: dev # 0, block # 0, count 1024 ... 1024 blocks written: OK                                                                                                                       
 +fipfile=2023.04/bpi-r3_emmc_fip.bin                                                                                                                                                         
 +645001 bytes read in 58 ms (10.6 MiB/s)                                                                                                                                                     
 +                                                                                                                                                                                            
 +MMC erase: dev # 0, block # 13312, count 4096 ... 4096 blocks erased: OK                                                                                                                    
 +                                                                                                                                                                                            
 +MMC write: dev # 0, block # 13312, count 4096 ... 4096 blocks written: OK
 +</code>
 +manual way:
 +<hidden>
 +<code>
 +BPI-R3> mmc dev 0
 +switch to partitions #0, OK
 +mmc0(part 0) is current device
 +#switch to boot0 block
 +BPI-R3> mmc partconf 0 1 1 1
 +BPI-R3> mmc erase 0x0 0x400
 +BPI-R3> fatload usb 0:1 $loadaddr bl2_emmc_usb.img
 +200072 bytes read in 19 ms (10 MiB/s)
 +BPI-R3> mmc write ${loadaddr} 0x0 0x400
 +
 +MMC write: dev # 0, block # 0, count 1024 ... 1024 blocks written: OK
 +#switch back to user-partition
 +BPI-R3> mmc partconf 0 1 1 0
 +
 +#fip partition starts at blockoffset 0x3400 (start-value from json 13312 in hex)
 +BPI-R3> mmc erase 0x3400 0x1000
 +
 +MMC erase: dev # 0, block # 13312, count 4096 ... 4096 blocks erased: OK
 +BPI-R3> fatload usb 0:1 $loadaddr fip_emmc_usb.bin
 +634345 bytes read in 55 ms (11 MiB/s)
 +BPI-R3> mmc write ${loadaddr} 0x3400 0x1000
 +
 +MMC write: dev # 0, block # 13312, count 4096 ... 4096 blocks written: OK
 +BPI-R3>
 +</code>
 +</hidden>
 +
 +If boot from emmc does not work,maybe bootbus needs to be changed to 0:
 +
 +  mmc bootbus 0 0 0 0
 +
 +
 +instead of writing the gpt image file you can bootup a linux with initrd
 +
 +you get all files here: https://drive.google.com/drive/folders/1ZsJ2jsOieg_6HM4LHWUimROX5p5Kx7vo?usp=share_link
 +
 +  BPI-R3> run useusb
 +  BPI-R3> setenv initrd rootfs.cpio.zst                                                                                                                                      
 +  BPI-R3> setenv fit bpi-r3.itb
 +  BPI-R3> run newboot
 +
 +and create gpt like i do it for [[https://github.com/frank-w/u-boot/blob/mtk-atf/build.sh#L114|sdcard]], original gpt uses same values for partitions and use a partition named gpt (0..33) instead of bl2 (34..8191)
 +
 +<code>
 +sudo sgdisk -o ${LDEV}
 +#sudo sgdisk -a 1 -n 1:34:8191 -A 1:set:2 -t 1:8300 -c 1:"bl2" ${LDEV} #sdcard only
 +sudo sgdisk -a 1 -n 1:0:33 -A 1:set:2 -t 1:8300 -c 1:"gpt" ${LDEV} #emmc only
 +sudo sgdisk -a 1 -n 2:8192:9215 -t 2:8300 -c 2:"u-boot-env" ${LDEV}
 +sudo sgdisk -a 1 -n 3:9216:13311 -t 3:8300 -c 3:"factory" ${LDEV}
 +sudo sgdisk -a 1 -n 4:13312:17407 -t 4:8300 -c 4:"fip" ${LDEV}
 +sudo sgdisk -a 1024 -n 5:17408:${bootend} -t 5:8300 -c 5:"boot" ${LDEV}
 +sudo sgdisk -a 1024 -n 6:${rootstart}:${rootend} -t 6:8300 -c 6:"rootfs" ${LDEV}
 +</code>
 +===== chainload uboot =====
 +
 +u-boot.bin (without mtk header and not fip) can be chainloaded from another
 +uboot for testing uboot-features without flashing
 +
 +<code>
 +BPI-R3> fatload usb 0:1 $loadaddr uboot_emmc_usb.bin
 +601344 bytes read in 52 ms (11 MiB/s)
 +BPI-R3> go $loadaddr
 +## Starting application at 0x46000000 ...
 +
 +U-Boot 2022.04-00557-g726479add2f1 (Jul 05 2022 - 18:14:31 +0200)
 +</code>
en/bpi-r3/uboot.txt · Last modified: 2024/04/07 09:39 by frank