Created NanoPi R2S (Rockchip rk3328) 16MB image. Next: boot from SPI flash

This continues from here under a better title so people that need it can find it.

I'm working at getting the NanoPi R2S to boot off SPI-flash that one can solder at an empty spot on the board. First step was to get the image to fit the 16MB flash chip I'd like to use. (I could use a pin-compatible 64MB SPI flash chip, but the images made by the OpenWrt support for this hardware were even over 64MB.

The problem was that the two ext4 partitions were placed at 32MB and 64MB offset, and that the second stage of the bootloader by default is expected at 8MB, which would be smack in the middle of my 16MB.

So I modified the u-boot configuration to expect the second stage at 0x120 (in 512 byte blocks), or 144 kB. First and second stage now fit under 1 MB, and I made a new script to create the images that leaves a 1MB gap before the first partition but not between further partitions. This script also gzips the kernel and modifies the u-boot boot script to uncompress the kernel on load (going from 12 MB to 4.8 MB kernel).

Somewhat unrelated, I changed the serial speed to 115200 because I had trouble with multiple adapters doing 1.5Mbps reliably. I also added fdisk to busybox.

If you want to do this too, here is how you get my changes and how to make your image:

wget https://rop.nl/openwrt-nanopi-r2s/diffconfig
wget https://rop.nl/openwrt-nanopi-r2s/git_diff.patch
git clone https://git.openwrt.org/openwrt/openwrt.git openwrt
cd openwrt
git checkout v21.02.0-rc4
git apply ../git_diff.patch
cp ../diffconfig .config
./scripts/feeds update -a && ./scripts/feeds install -a
make -j $(nproc) defconfig download clean world

Tada:

root@OpenWrt:/# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 2.5M      2.5M         0 100% /rom
tmpfs                   495.6M     56.0K    495.6M   0% /tmp
/dev/loop0                5.6M     83.0K      5.0M   2% /overlay
overlayfs:/overlay        5.6M     83.0K      5.0M   2% /
tmpfs                   512.0K         0    512.0K   0% /dev

So now there's a method to make a useable image that's 16MB, not 67MB. Probably not that important if you boot from SD-card, because they're plenty large. (You can set the rootfs partition to any size you like, the resulting image will just be a little better optimized than before.)

Now I need to move to getting it to boot from SPI flash and making a truly read-only system. Not because we couldn't do a JFFS in mtd, but because I would like it to boot completely read-only. (I even plan to put a jumper on the R/_W pin on the flash chip.)

Maybe some of you can help on my journey forward:

  • First I need to put the SPI flash in the device tree files. I think I can just copy bits and pieces from existing SPI-flash configs elsewhere and make it work somehow. Since my plan is to have it all read-only, my plan is to keep the image as-is and just make the flash one big partition and not use mtd to make multiple partitions in it, so I can use the same image for SD and SPI-flash boot. Does that make sense? Can you have one big SPI-flash partition that has a partition table in it? The alternative is to do multiple mtd partitions, which would also work.

  • I cannot easily figure out how create an image that uses squashfs and then puts the overlay on tmpfs. mount_root, pivot, tons of scripts and binaries all over the place and after hours of staring at it I simply lost track. I'm sure it's easy once you know...

1 Like

Hi @rop I am also trying to boot openwrt on 16MB SPI Flash

I modified Makefile to fit Openwrt image on to SPI
with this changes i builded openwrt and resulting image is now 5.9MB
SPI Flash sysupgrade image support

for adding SPI node to DTB i copied SPI nodes from Orangepi R1 's SPI Flash Supported DTB
https://github.com/melsem/openwrt-19.07.7-spi/raw/master/sun8i-h2-plus-orangepi-r1-dtb
so SPI flash worked with this parts.(I don't know if this partitioning valid for R1 plus )
I am stucked this step and don't know how to implement uboot .

Any help would be appreciated

here is my thread

root@OpenWrt:/# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00080000 00010000 "uboot"
mtd1: 00010000 00010000 "dtb"
mtd2: 00f70000 00010000 "firmware"
root@OpenWrt:/#