August 9, 2018

The Nanopi Fire3 - rebuilding U-Boot

I am following the instructions in the "friendly arm" Wiki, correcting a few errors and typos. Before starting this, I installed the aarch64 Linaro compiler toolchain using the command dnf install gcc-aarch64-linux-gnu. This installs the compiler as aarch64-linux-gnu-gcc not aarch64-linux-gcc so I need to set the value of the CROSS_COMPILE variable accordingly:
git clone https://github.com/friendlyarm/u-boot.git
cd u-boot
git checkout nanopi2-v2016.01
make s5p6818_nanopi3_defconfig
make CROSS_COMPILE=aarch64-linux-gnu-
This seems to work just fine (albeit with a few compiler warnings) and ends like so:
  LDS     u-boot.lds
  LD      u-boot
  OBJCOPY u-boot.srec
  OBJCOPY u-boot.bin
start=$(aarch64-linux-gnu-nm u-boot | grep __rel_dyn_start | cut -f 1 -d ' '); end=$(aarch64-linux-gnu-nm u-boot | grep __rel_dyn_end | cut -f 1 -d ' '); tools/relocate-rela u-boot.bin 0x43C00000 $start $end
  CFG     u-boot.cfg
Firmware Image Package ToC:
---------------------------
- Non-Trusted Firmware BL33: offset=0x60, size=0x73720
  file: 'u-boot.bin'
---------------------------
Creating "fip-nonsecure.bin"
Creating "fip-nonsecure.img" (<-- tools/nexell/nsih/nanopi3.txt)
The Wiki then gives these cryptic instructions. Of course I am on a linux system and want to use "dd" to put the image onto my SD card.
After your compilation succeeds a fip-nonsecure.img will be generated. If you want to test it flash it to your installation SD card to replace an existing U-Boot v2016.01 file via fastboot, sd-fuse_s5p6818 or eflasher ROM.
Note: you cannot use mixed U-Boot files. For example you cannot use fastboot to update an existing U-Boot V2014.07 and you cannot use bootloader.img to replace an existing u-boot.bin.
A peek at the Makefile shows the following. Apparently there is some special "wrapping" of the U-Boot image that is required for the on chip bootloader in the S5P6818 SoC.
ifdef CONFIG_ARCH_S5P6818
BINGEN = tools/nexell/SECURE_BINGEN
FIP_CREATE = tools/fip_create/fip_create

fip-nonsecure.bin: u-boot.bin tools
        @if [ -e "$@" ]; then rm -f $@; fi
        $(Q)$(FIP_CREATE) --dump --bl33 $< $@

NSIH ?= tools/nexell/nsih/nanopi3.txt
BINGEN_FLAGS := -l 0x7df00000 -e 0x00000000 -n $(NSIH)

fip-nonsecure.img: fip-nonsecure.bin $(NSIH)
        @echo "Creating \"$@\" (<-- $(NSIH))"
        $(Q)$(BINGEN) -c S5P6818 -t 3rdboot $(BINGEN_FLAGS) -i $< -o $@
endif

Putting U-boot on the SD card

The first thing I do is to insert the SD card that currently boots linux into a card reader and take a look at it with "fdisk":
[root@trona tom]# fdisk /dev/sdc
Command (m for help): p
Disk /dev/sdc: 7.4 GiB, 7948206080 bytes, 15523840 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x217e8232

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdc1         8192   139263   131072   64M 83 Linux
/dev/sdc2       139264 15523839 15384576  7.3G 83 Linux
So there is a pretty big (8192 sector = 4M) area ahead of any partitions that could accomodate U-Boot. I copy this to a file so I can restore it easily if I want to run linux again, then zero the area to ensure that I don't somehow fool myself:
dd if=/dev/sdc of=original.bin count=8192
dd if=/dev/zero of=/dev/sdc count=8192
dd if=original.bin of=/dev/sdc count=2
dd if=fip-nonsecure.img of=/dev/sdc seek=2
All very nice, but this does not work at all. What I should do (but may not urgently need to do) is to read the information about booting in the S5P6818 user manual. The next section gives the important clues.

How to build a bootable SD card

Poking around the Wiki reveals some important details outside of the U-boot section.
git clone https://github.com/friendlyarm/sd-fuse_s5p6818.git
cd sd-fuse_s5p6818
cat fusing.sh
The "fusing.sh" script reveals the important details. The steps to put U-Boot onto the SD card are:
dd if=bl1-mmcboot.bin of=/dev/sdc bs=512 seek=1 conv=fdatasync
dd if=fip-loader.img  of=/dev/sdc bs=512 seek=129 conv=fdatasync
dd if=fip-secure.img  of=/dev/sdc bs=512 seek=769 conv=fdatasync
dd if=fip-nonsecure.img of=/dev/sdc bs=512 seek=3841 conv=fdatasync
The first three files are found in sd-fuse_s5p6818/prebuilt and at this point are mysterious binary blobs. Mysterious though this process is, it does work.
ls -l
-rw-rw-r-- 1 tom tom  21552 Aug 10 14:08 bl1-mmcboot.bin
-rw-rw-r-- 1 tom tom 279040 Aug 10 14:08 fip-loader.img
-rw-rw-r-- 1 tom tom 257136 Aug 10 14:08 fip-secure.img
-rw-rw-r-- 1 tom tom 473984 Aug 10 14:17 fip-nonsecure.img (U-Boot with wrapper)

Rebuild U-Boot and try to turn on networking

cd u-boot/configs
vi s5p6818_nanopi3_defconfig
cd ..
make distclean
make s5p6818_nanopi3_defconfig
make
cp fip-nonsecure.img ../mksd
cd ../mksd
make
I edit the defconfig file to set CONFIG_NET=y. I also add CROSS_COMPILE=aarch64-linux-gnu- at the front end of the Makefile so I won't have to remember it each time.

Now with the SD card in the board, I type reset and get the message "No ethernet found" -- so this has not gained me anything.

BL1 by Nexell V1.0.0-g44b48cd [Built on 2017-08-02 15:58:54]

Memory Initialize (0) Done.

Loading from sdmmc...
 Image Loading Done!
Launch to 0x7FD00800


U-Boot 2016.01-00690-g85eaed0682-dirty (Aug 10 2018 - 14:31:07 -0700)

Model: FriendlyElec boards based on Nexell s5p6818
Board: NanoPi Fire 3
DRAM:  987 MiB
Panel: N/A (-1)
MMC:   NEXELL DWMMC: 0, NEXELL DWMMC: 1
HDMI:  display.0, preset 0 (1280 *  720)
HDMI:  phy ready...
LCD:   [HDMI] dp.0.1 1280x720 32bpp FB:0x46000000
In:    serial
Out:   serial
Err:   serial

Net:   Net Initialization Skipped
No ethernet found.
Hit any key to stop autoboot:  0
14562000 bytes read in 693 ms (20 MiB/s)
4512648 bytes read in 223 ms (19.3 MiB/s)
47839 bytes read in 15 ms (3 MiB/s)
## Flattened Device Tree blob at 49000000
   Booting using the fdt blob at 0x49000000
   reserving fdt memory region: addr=7de00000 size=100000
   Using Device Tree in place at 0000000049000000, end 000000004900eade

Starting kernel ...


Have any comments? Questions? Drop me a line!

Tom's electronics pages / [email protected]