Close

Step 2: enable SPI flash and and write u-boot

A project log for Orange Pi Zero native network boot

Tutorial describing steps to make Orange Pi Zero boot natively from network

andriymalyshenkoandriy.malyshenko 08/11/2021 at 22:300 Comments

 Next step is to flash u-boot (ie bootloader) to SPI flash.

When Pi starts normally first stage bootloader (this one is hard-coded into the CPU itself) will look for boot device. Normally it will find sd-card and boot from it (it has highest priority).

When it is not present however it will try luck with other boot sources, including SPI flash. So it will actually initialize SPI bus all on it's own and look for bootloader there (u-boot in our case). So in this step we will flash it there. Alternatively you may programm SPI flash directly on the Pi, but this is more advanced scenario and it is not covered in this tutorial.

Next steps are run on Pi

$ sudo apt update 
$ sudo apt upgrade
$ sudo apt-get install flashrom

 Next we will enable SPI flash to gain access to it (it is disabled by default) by adding device tree layer named spi-spidev

$ sudo nano /boot/armbianEnv.txt

 File will look like this after edit

verbosity=1
bootlogo=false
console=serial
disp_mode=1920x1080p60
overlay_prefix=sun8i-h3
overlays=usbhost2 usbhost3 spi-spidev
rootdev=UUID=3dbf7aef-312c-456c-a992-e6bde7857d3f
rootfstype=ext4
usbstoragequirks=0x2537:0x1066:u,0x2537:0x1068:u
param_spidev_spi_bus=0

 We reboot after that to confirm presense of SPI device

$ sudo reboot

[...]

$ ls -l /dev/spi*
crw------- 1 root root 153, 0 Aug 11 22:19 /dev/spidev0.0

Device is present, we can start preparing image to flash.

$ mkdir spiflash && cd spiflash

# create empty image, ouf flash chip can be larger, but for our purposes it is just enough
$ dd if=/dev/zero count=2048 bs=1K | tr '\000' '\377' > spi.img

# let's find u-boot binary in the local filesystem
$ ls -al /usr/lib/linux-u-boot-*_armhf/*.bin 
-rw-rw-r-- 1 root root 469855 Aug  8 14:21 /usr/lib/linux-u-boot-current-orangepizero_21.08.1_armhf/u-boot-sunxi-with-spl.bin

$ dd if=/usr/lib/linux-u-boot-current-orangepizero_21.08.1_armhf/u-boot-sunxi-with-spl.bin of=spi.img bs=1k conv=notrunc

Now we have an image in spi.img file, that we just need to flash

$ sudo flashrom -p linux_spi:dev=/dev/spidev0.0 -w spi.img -c MX25L1605A/MX25L1606E/MX25L1608E

flashrom v1.2 on Linux 5.10.43-sunxi (armv7l)
flashrom is free software, get the source code at https://flashrom.org

Using clock_gettime for delay loops (clk_id: 1, resolution: 1ns).
Using default 2000kHz clock. Use 'spispeed' parameter to override.
Found Macronix flash chip "MX25L1605A/MX25L1606E/MX25L1608E" (2048 kB, SPI) on linux_spi.
Reading old flash chip contents... done.
Erasing and writing flash chip... Erase/write done.
Verifying flash... VERIFIED.

Now we're ready to reboot and try out boot without SD-card present
 

$ sudo poweroff

 Upon reboot without SD-card

U-Boot SPL 2020.10-armbian (Aug 08 2021 - 16:21:05 +0200)
DRAM: 256 MiB
Trying to boot from sunxi SPI
   
   
U-Boot 2020.10-armbian (Aug 08 2021 - 16:21:05 +0200) Allwinner Technology
   
CPU:   Allwinner H3 (SUN8I 1680)
Model: Xunlong Orange Pi Zero
DRAM:  256 MiB
MMC:   mmc@1c0f000: 0, mmc@1c10000: 1
Loading Environment from FAT... MMC: no card present
In:    serial
Out:   serial
Err:   serial
Net:   phy interface0
eth0: ethernet@1c30000
starting USB...
Bus usb@1c1a000: USB EHCI 1.00
Bus usb@1c1a400: USB OHCI 1.0
Bus usb@1c1b000: USB EHCI 1.00
Bus usb@1c1b400: USB OHCI 1.0
Bus usb@1c1c000: USB EHCI 1.00
Bus usb@1c1c400: USB OHCI 1.0
scanning bus usb@1c1a000 for devices... 1 USB Device(s) found
scanning bus usb@1c1a400 for devices... 1 USB Device(s) found
scanning bus usb@1c1b000 for devices... 1 USB Device(s) found
scanning bus usb@1c1b400 for devices... 1 USB Device(s) found
scanning bus usb@1c1c000 for devices... 1 USB Device(s) found
scanning bus usb@1c1c400 for devices... 1 USB Device(s) found
       scanning usb for storage devices... 0 Storage Device(s) found
Autoboot in 1 seconds
MMC: no card present
   
Device 0: unknown device
ethernet@1c30000 Waiting for PHY auto negotiation to complete. done
BOOTP broadcast 1
DHCP client bound to address 192.168.1.202 (8 ms)

 What happens above is Pi able to boot from SPI flash, it found no boot media, initialized Ethernet and obtained IP address from DHCP server.

Next step is to configure that DHCP server with right commands for our Pi to boot from.

Discussions