LicheePi NanoのSDブート用イメージをビルドしてみた

# LicheePi Nano
Sipeedの小型の格安SBC。日本国内では[秋月電子](https://akizukidenshi.com/catalog/g/gM-14787/)や[スイッチサイエンス](https://www.switch-science.com/catalog/5705/)で購入できる。
Socは[Allwinner F1C100s](https://linux-sunxi.org/F1C100s), 32MB DDRと16MB SPI Flashが載っている
公式のドキュメントは[こちら](http://nano.lichee.pro/)。中国語なのでChrome拡張のGoogle Translateとか使うと読みやすい

# ビルド環境
Windows10をメイン機としているのでWSL2でUbuntu 18.04LTSを動かしてビルドした。インストール方法などはググれば出てくるので割愛(Ubuntu 20.04LTSだとbuildrootのビルドでエラーが出たので18.04LTSを使用)

SDカードを作成するのにはSurface Pro 3にインストールしたUbuntu 20.04.1LTSを使用した。

# Toolchain
Linaro Toolchainから**arm-linux-gnueabi**の最新のバイナリをダウンロードする

```
$ wget http://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabi/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabi.tar.xz
```

適当な場所に配置する(例として/optに配置)

```
$ tar -vxJf gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabi.tar.xz
$ sudo cp -r ./gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabi /opt/
```

.bashrcにパスを通す

```/etc/bash.bashrc
# ファイル末尾に追加
PATH="$PATH:/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabi/bin"
```

# U-boot
Lichee-PiのgithubリポジトリからLicheePi Nano用っぽいブランチをクローンする

```
$ git clone --depth=1 -b nano-v2018.01 https://github.com/Lichee-Pi/u-boot.git
```
LicheePi Nano用の.configを生成してビルド

```
$ cd u-boot/
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- licheepi_nano_defconfig
$ make ARCH=arm menuconfig //設定の確認
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- //ビルド
```
必要に応じて`apt install`で不足しているパッケージをインストールする

```
# 必要になったのは下記のものあたり
$ sudo apt install build-essential
$ sudo apt install gcc python swig python-dev bc
$ sudo apt-get install libncurses5-dev
```

# Linux Kernel
Lichee-PiのgithubリポジトリからLicheePi Nano用っぽいブランチをクローンする

```
$ git clone --depth=1 -b nano-5.2-tf https://github.com/Lichee-Pi/linux.git
```
.confを公式からダウンロードする

```
$ cd linux
$ wget http://dl.sipeed.com/LICHEE/Nano/SDK/config
$ mv config .config
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4
```

必要に応じて`apt install`で不足しているパッケージをインストールする

```
# 必要になったのは下記のものあたり
$ sudo apt install flex
$ sudo apt install bison
$ sudo apt install libssl-dev
```
# buildroot

```
$ wget https://buildroot.org/downloads/buildroot-2017.08.tar.gz
$ tar xvf buildroot-2017.08.tar.gz
$ cd buildroot-2017.08/
# .configのダウンロード
$ wget https://fdvad021asfd8q.oss-cn-hangzhou.aliyuncs.com/migrate/buildroot.config
$ mv buildroot.config .config
$ make ARCH=arm menuconfig
$ make
```

必要に応じて`apt install`で不足しているパッケージをインストールする

```
# 必要になったのは下記のものあたり
$ sudo apt install unzip
$ sudo apt install libc6-i386 lib32stdc++6 lib32z1
```

# ブート設定ファイル
``` boot.cmd
setenv bootargs console=tty0 console=ttyS0,115200 panic=5 rootwait root=/dev/mmcblk0p2 rw
load mmc 0:1 0x80C00000 suniv-f1c100s-licheepi-nano.dtb
load mmc 0:1 0x80008000 zImage
bootz 0x80008000 - 0x80C00000
```
boot.cmdからboot.scrを生成する

```
$ ./u-boot/tools/mkimage -C none -A arm -T script -d boot.cmd boot.scr
```

# ブート用SDカード作成
WSL2だとUSBメモリにアクセスできないので別途Linux環境が必要(Surface Pro 3にインストールしたUbuntu 20.04.1LTSを使用した)
下記の必要なファイルをLinux環境にコピーする

- u-boot/u-boot-sunxi-with-spl.bin
- linux/arch/arm/boot/boot/zImage
- linux/arch/arm/boot/boot/dts/suniv-f1c100s-licheepi-nano.dtb
- buildroot-2017.08/output/images/rootfs.tar

以下ではSDカードは/dev/sdbとして認識されているとする

## SDカードの編集
```
# すでにマウントされている場合はアンマウントする
$ sudo umount /dev/sdbx

# パーティション編集
$ sudo fdisk /dev/sdb
d　//既存のパーティションを削除する
n　//第1パーティション作成
p
1
+32M
n  //第2パーティション作成
p
2
p
w

## パーティションタイプ変更
$ sudo mkfs.vfat /dev/sdb1
$ sudo mkfs.ext4 /dev/sdb2
```

## U-boot書き込み
```
$ sudo dd if=u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8
```

## bootファイルのコピー
```
$ sudo mount /dev/sdb1 /mnt/boot
$ sudo cp zImage /mnt/boot/
$ sudo cp boot.scr /mnt/boot/
$ sudo cp suniv-f1c100s-licheepi-nano.dtb /mnt/boot
$ sync
$ sudo umount /dev/sdb1
```

## File Systemのコピー
```
$ sudo mount /dev/sdb2 /mnt/rootfs
$ sudo tar xvf rootfs.tar -C /mnt/rootfs/
```

# Linux起動確認
```
U-Boot SPL 2018.01-g013ca45 (Sep 06 2020 - 06:07:34)
DRAM: 32 MiB
Trying to boot from MMC1


U-Boot 2018.01-g013ca45 (Sep 06 2020 - 06:07:34 +0900) Allwinner Technology

CPU:   Allwinner F Series (SUNIV)
Model: Lichee Pi Nano
DRAM:  32 MiB
MMC:   SUNXI SD/MMC: 0
*** Warning - bad CRC, using default environment

In:    serial@1c25000
Out:   serial@1c25000
Err:   serial@1c25000
Net:   No ethernet found.
starting USB...
No controllers found
Hit any key to stop autoboot:  0
switch to partitions #0, OK
mmc0 is current device
Scanning mmc 0:1...
Found U-Boot script /boot.scr
reading /boot.scr
279 bytes read in 14 ms (18.6 KiB/s)
## Executing script at 80c50000
reading suniv-f1c100s-licheepi-nano.dtb
5908 bytes read in 31 ms (185.5 KiB/s)
reading zImage
4173736 bytes read in 215 ms (18.5 MiB/s)
## Flattened Device Tree blob at 80c00000
   Booting using the fdt blob at 0x80c00000
   Loading Device Tree to 816fb000, end 816ff713 ... OK

Starting kernel ...

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 5.2.0-licheepi-nano+ (pi@Windows10) (gcc version 7.5.0 (Linaro GCC 7.5-2019.12)) #2 Sun Sep 6 08:58:05 JST 2020
[    0.000000] CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=0005317f
[    0.000000] CPU: VIVT data cache, VIVT instruction cache
[    0.000000] OF: fdt: Machine model: Lichee Pi Nano
[    0.000000] Memory policy: Data cache writeback
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 8128
[    0.000000] Kernel command line: console=tty0 console=ttyS0,115200 panic=5 rootwait root=/dev/mmcblk0p2 rw
[    0.000000] Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)
[    0.000000] Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)
[    0.000000] Memory: 22688K/32768K available (6144K kernel code, 240K rwdata, 1556K rodata, 1024K init, 241K bss, 10080K reserved, 0K cma-reserved, 0K highmem)
[    0.000000] SLUB: HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
[    0.000000] random: get_random_bytes called from start_kernel+0x254/0x42c with crng_init=0
[    0.000048] sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
[    0.000123] clocksource: timer: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 79635851949 ns
[    0.000639] Console: colour dummy device 80x30
[    0.001241] printk: console [tty0] enabled
[    0.001339] Calibrating delay loop... 203.16 BogoMIPS (lpj=1015808)
[    0.070270] pid_max: default: 32768 minimum: 301
[    0.070712] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes)
[    0.070782] Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes)
[    0.072428] CPU: Testing write buffer coherency: ok
[    0.074451] Setting up static identity map for 0x80100000 - 0x80100058
[    0.076748] devtmpfs: initialized
[    0.082497] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    0.082670] futex hash table entries: 256 (order: -1, 3072 bytes)
[    0.083015] pinctrl core: initialized pinctrl subsystem
[    0.085402] NET: Registered protocol family 16
[    0.086931] DMA: preallocated 256 KiB pool for atomic coherent allocations
[    0.089016] cpuidle: using governor menu
[    0.139913] SCSI subsystem initialized
[    0.140524] usbcore: registered new interface driver usbfs
[    0.140768] usbcore: registered new interface driver hub
[    0.141005] usbcore: registered new device driver usb
[    0.141499] pps_core: LinuxPPS API ver. 1 registered
[    0.141578] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[    0.141710] PTP clock support registered
[    0.142288] Advanced Linux Sound Architecture Driver Initialized.
[    0.143998] clocksource: Switched to clocksource timer
[    0.173383] NET: Registered protocol family 2
[    0.175173] tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 4096 bytes)
[    0.175363] TCP established hash table entries: 1024 (order: 0, 4096 bytes)
[    0.175476] TCP bind hash table entries: 1024 (order: 0, 4096 bytes)
[    0.175561] TCP: Hash tables configured (established 1024 bind 1024)
[    0.175881] UDP hash table entries: 256 (order: 0, 4096 bytes)
[    0.176002] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes)
[    0.176586] NET: Registered protocol family 1
[    0.178144] RPC: Registered named UNIX socket transport module.
[    0.178256] RPC: Registered udp transport module.
[    0.178310] RPC: Registered tcp transport module.
[    0.178357] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    0.180773] NetWinder Floating Point Emulator V0.97 (double precision)
[    0.182840] Initialise system trusted keyrings
[    0.183498] workingset: timestamp_bits=30 max_order=13 bucket_order=0
[    0.206814] NFS: Registering the id_resolver key type
[    0.206969] Key type id_resolver registered
[    0.207029] Key type id_legacy registered
[    0.213155] Key type asymmetric registered
[    0.213265] Asymmetric key parser 'x509' registered
[    0.213480] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 251)
[    0.213569] io scheduler mq-deadline registered
[    0.213619] io scheduler kyber registered
[    0.225263] suniv-f1c100s-pinctrl 1c20800.pinctrl: initialized sunXi PIO driver
[    0.407365] Serial: 8250/16550 driver, 8 ports, IRQ sharing disabled
[    0.413124] suniv-f1c100s-pinctrl 1c20800.pinctrl: 1c20800.pinctrl supply vcc-pe not found, using dummy regulator
[    0.415127] printk: console [ttyS0] disabled
[    0.435455] 1c25000.serial: ttyS0 at MMIO 0x1c25000 (irq = 22, base_baud = 6250000) is a 16550A
[    0.852596] printk: console [ttyS0] enabled
[    0.860564] suniv-f1c100s-pinctrl 1c20800.pinctrl: 1c20800.pinctrl supply vcc-pd not found, using dummy regulator
[    0.878394] SCSI Media Changer driver v0.25
[    0.884217] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    0.890823] ehci-platform: EHCI generic platform driver
[    0.896438] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[    0.902723] ohci-platform: OHCI generic platform driver
[    0.908481] usbcore: registered new interface driver usb-storage
[    0.915303] udc-core: couldn't find an available UDC - added [g_cdc] to list of pending drivers
[    0.924480] i2c /dev entries driver
[    0.929697] suniv-f1c100s-pinctrl 1c20800.pinctrl: 1c20800.pinctrl supply vcc-pf not found, using dummy regulator
[    0.967825] sunxi-mmc 1c0f000.mmc: initialized, max. request size: 16384 KB
[    0.977224] usbcore: registered new interface driver usbhid
[    0.982872] usbhid: USB HID core driver
[    1.002940] NET: Registered protocol family 17
[    1.007844] Key type dns_resolver registered
[    1.014854] Loading compiled-in X.509 certificates
[    1.029255] suniv-f1c100s-pinctrl 1c20800.pinctrl: 1c20800.pinctrl supply vcc-pd not found, using dummy regulator
[    1.040780] sun4i-backend 1e60000.display-backend: Couldn't find matching frontend, frontend features disabled
[    1.051747] sun4i-drm display-engine: bound 1e60000.display-backend (ops 0xc073b2d4)
[    1.060887] sun4i-drm display-engine: bound 1c0c000.lcd-controller (ops 0xc0739f2c)
[    1.068775] [drm] Supports vblank timestamp caching Rev 2 (21.10.2013).
[    1.075496] [drm] No driver support for vblank timestamp query.
[    1.082817] [drm] Initialized sun4i-drm 1.0.0 20150629 for display-engine on minor 0
[    1.305099] mmc0: host does not support reading read-only switch, assuming write-enable
[    1.308612] mmc0: new high speed SDHC card at address aaaa
[    1.312951] mmcblk0: mmc0:aaaa SL08G 7.40 GiB
[    1.335178] Console: switching to colour frame buffer device 100x30
[    1.338312]  mmcblk0: p1 p2
[    1.464230] sun4i-drm display-engine: fb0: sun4i-drmdrmfb frame buffer device
[    1.486568] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[    1.519601] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[    1.540946] ALSA device list:
[    1.558265]   #0: Loopback 1
[    1.576107] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[    1.599249] cfg80211: failed to load regulatory.db
[    1.653586] random: fast init done
[    1.977284] EXT4-fs (mmcblk0p2): recovery complete
[    2.145650] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[    2.168407] VFS: Mounted root (ext4 filesystem) on device 179:2.
[    2.194191] devtmpfs: mounted
[    2.217951] Freeing unused kernel memory: 1024K
[    2.237009] Run /sbin/init as init process
[    2.411834] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
Starting logging: OK
Initializing random number generator... [    2.774459] random: dd: uninitialized urandom read (512 bytes read)
done.

Welcome to Lichee Pi
Lichee login: root
Password:
# 
```

--------------------------------
WLS2でUSBメモリが使えればWindows10と別にLinux PCを準備する必要がなくなるのでWLS2がUSBデバイスサポートできるようにならないかなーと期待してる