F1C100S linux 启动编译流程 (tf / spi flash)

前言

最近项目上再次使用FC100s跑linux,顺带详细记录一下编译整个linux的步骤,以作记录备份。

由于TF卡和SPI FLASH启动在某些方面不一样,在有区别的时候会进行特殊说明

整体思路:

  • 安装工具链
  • 编译 uboot
  • 编译 kernel
  • 编译 buildroot
  • 制作镜像

安装工具链

工具链下载地址

http://releases.linaro.org/components/toolchain/binaries/7.2-2017.11/arm-linux-gnueabi/

工具链

gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabi.tar

安装工具链
  • 解压 tar 文件 : tar -xvf xxxxx.tar
  • 修改环境变量,指定交叉工具链的位置: vim /etc/profile
  • 使环境变量生效: source /etc/profile
  • 测试交叉编译工具: arm-linux-gnueabi-gcc -v

编译 uboot

源码下载

git clone https://github.com/Lichee-Pi/u-boot.git -b nano-v2018.01

1
2
3
注意:
1.建议下载单独分支,这样下载速度会快一些
2.github下载可能会很慢,可以使用:https://hub.fgit.cf/ 加速
  • TF卡启动配置:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- licheepi_nano_defconfig

  • FLASH 卡启动配置:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- licheepi_nano_spiflash_defconfig

  • 编译:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j32

  • 如果编译出错:

sudo apt-get install swig

sudo apt-get install python-dev

  • 编译成功后会在根目录下生产烧录文件

u-boot-sunxi-with-spl.bin


编译 kernel

源码下载

git clone https://hub.fgit.cf/Lichee-Pi/linux.git -b nano-5.2-flash

  • 建议TF卡和FLASH 都选择这个分支,这个分支包含FLASH驱动

  • 环境

sudo apt-get install libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf

  • 配置

make ARCH=arm licheepi_nano_defconfig

  • 编译

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j32

  • 生成文件位置

arch/arm/boot/ 目录下的 zImage

arch/arm/boot/dts/ 目录下的 suniv-f1c100s-licheepi-nano.dtb

编译buildroot

源码下载

https://buildroot.org/downloads/buildroot-2022.02.tar.gz

  • 注意

为了方便这里使用buildroot来制作rootfs,这里有个坑。这里如果使用linaro等组织提供的现成的交叉编译工具链来编译buildroot项目生成rootfs,在使用时系统启动过程中可能会出现 Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b ,而使用buildroot自己生成的编译工具链就不会出现这个问题了。

  • 配置
1
2
3
4
5
6
7
8
9
10
Target options  ->
Target Architecture (ARM (little endian))
Target Architecture Variant (arm926t)
Floating point strategy (Soft float)

Toolchain ->
Toolchain type (Buildroot toolchain)
C library (glibc)
Kernel Headers (Linux 4.19.x kernel headers)
Enable C++ support
  • 注意:

内核头文件根据内核版本去选择,向下选择最高的版本

  • 编译

make -j32

  • 生成文件位置

编译器: buildroot-2023.11/output/host/arm-buildroot-linux-gnueabi

文件系统: buildroot-2023.11/output/images

  • 注意

应用层代码需要使用 buildroot 自带的编译器编译,如果需要编译 APP代码,请将 buildroot 生成的编译添加到环境变量中


制作 TF 镜像

TF卡镜像
  • 将 TF 分为两个分区

第一个分区 FAT格式 100M,用来存放 内核和设备树
第二个分区 ext4格式 可以将剩余空间都分配给它, 用来存放根文件系统

  • 烧录

将 uboot 烧录到 TF卡 8K位置处,

1
sudo dd if=u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8

将kernel和dtb放到TF卡的第一个分区的根目录下

将rootfs.tar解压到第二个分区下, 每次操作TF记得 sync 一下,不然启动可能会出粗

修改 uboot 环境变量

1
2
3
4
5
setenv bootcmd 'load mmc 0:1 0x80000000 zImage; load mmc 0:1 0x80C00000 suniv-f1c100s-licheepi-nano.dtb; bootz 0x80000000 - 0x80C00000'

setenv bootargs 'console=tty0 console=ttyS0,115200 panic=5 rootwait root=/dev/mmcblk0p2 rw'

saveenv

制作 FLASH 镜像

flash 烧录可以参考以下链接:

https://whycan.com/t_7558.html

分区说明
1
2
3
4
5
分区序号         分区大小              分区作用	     地址空间及分区名
mtd0 1MB (0x100000) spl+uboot 0x0000000-0x0100000 : “uboot”
mtd1 64KB (0x10000) dtb文件 0x0100000-0x0110000 : “dtb”
mtd2 4MB (0x400000) linux内核 0x0110000-0x0510000 : “kernel”
mtd3 剩余 (0xAF0000) 根文件系统 0x0510000-0x1000000 : “rootfs”
FALSH 烧录工具
  • 源码

https://github.com/Icenowy/sunxi-tools -b f1c100s 克隆对应的 f1c100s分支

  • 编译和安装

make && sudo make install

  • 烧录
1
2
3
4
sudo sunxi-fel -p spiflash-write 0 ./u-boot/u-boot-sunxi-with-spl.bin   
sudo sunxi-fel -p spiflash-write 0x0100000 ./linux/arch/arm/boot/dts/suniv-f1c100s-licheepi-nano.dtb
sudo sunxi-fel -p spiflash-write 0x0110000 ./linux/arch/arm/boot/zImage
sudo sunxi-fel -p spiflash-write 0x0510000 ./buildroot-2017.08/output/images/rootfs.tar

也可以把文件系统放在TF卡, 以上文件系统放到FLASH上没有试过, 可以试试下面参数

修改 uboot 环境变量

1
2
3
4
5
setenv bootcmd 'sf probe 0 50000000; sf read 0x80C00000 0x100000 0x4000; sf read 0x80008000 0x110000 0x400000;bootz 0x80008000 - 0x80C00000;'

setenv bootargs 'console=tty0 console=ttyS0,115200 panic=5 rootwait root=/dev/mmcblk0p2 rw'

saveenv