HamsterBear 构建可启动的镜像

Allwinner SoC 上电后会执行BootROM中的程序,会依次从SDIO,SPI等接口查询可引导的设备,
SPI设备具有最低引导权,若无法查询到可引导设备,会自动进入FEL模式,此时可使用sunxi-fel
通过USB启动spl或者uboot,也可以将引导文件烧写进外部flash。

提供两种启动方式

  • Nor Flash - XT25F128B等
  • eMMC/MMC TF - SDIN5D2-4G TF等

两种启动方式的配置互相冲突


从Nor Flash启动

uboot 添加 flash id

其他id的flash的添加操作类似
vim drivers/mtd/spi/spi_flash_ids.c +154

#ifdef CONFIG_SPI_FLASH_WINBOND         /* WINBOND */
        {"w25p80",         INFO(0xef2014, 0x0,  64 * 1024,    16, 0) },
        {"w25p16",         INFO(0xef2015, 0x0,  64 * 1024,    32, 0) },
        {"w25p32",         INFO(0xef2016, 0x0,  64 * 1024,    64, 0) },
        {"w25x40",         INFO(0xef3013, 0x0,  64 * 1024,     8, SECT_4K) },
        {"w25x16",         INFO(0xef3015, 0x0,  64 * 1024,    32, SECT_4K) },
        {"w25x32",         INFO(0xef3016, 0x0,  64 * 1024,    64, SECT_4K) },
        {"w25x64",         INFO(0xef3017, 0x0,  64 * 1024,   128, SECT_4K) },
        {"w25q80bl",       INFO(0xef4014, 0x0,  64 * 1024,    16, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q16cl",       INFO(0xef4015, 0x0,  64 * 1024,    32, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q32bv",       INFO(0xef4016, 0x0,  64 * 1024,    64, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q64cv",       INFO(0xef4017, 0x0,  64 * 1024,   128, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q128bv",      INFO(0xef4018, 0x0,  64 * 1024,   256, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q256",        INFO(0xef4019, 0x0,  64 * 1024,   512, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q80bw",       INFO(0xef5014, 0x0,  64 * 1024,    16, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q16dw",       INFO(0xef6015, 0x0,  64 * 1024,    32, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q32dw",       INFO(0xef6016, 0x0,  64 * 1024,    64, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q64dw",       INFO(0xef6017, 0x0,  64 * 1024,   128, RD_FULL | WR_QPP | SECT_4K) },
        {"w25q128fw",      INFO(0xef6018, 0x0,  64 * 1024,   256, RD_FULL | WR_QPP | SECT_4K) },
        {"xt25f128",       INFO(0x0b4018, 0x0,  64 * 1024,   256, 0) },

uboot 添加 CONFIG_BOOTCMD

vim u-boot/include/configs/suniv.h
添加

#define CONFIG_BOOTCOMMAND   "sf probe 0 50000000; "                           \
                             "sf read 0x80C00000 0x100000 0x4000; "  \
                             "sf read 0x80008000 0x110000 0x400000; " \
                             "bootz 0x80008000 - 0x80C00000"

uboot 修改 bootargs

# uboot中使用了一块内存作为了LCD framebuffer,如果不限制内核内存空间,会导致lcd花屏一段时间
# mem=31M 是最简单的处理办法
make menuconfig
勾选Enable boot arguments
在Boot arguments后的括号中粘贴如下内容
console=tty0 console=ttyS0,115200 panic=5 root=/dev/mtdblock3 rw rootfstype=jffs2 mem=31M

修改linux dts

修改spi0节点

&spi0 {
    pinctrl-names = "default";
    pinctrl-0 = <&spi0_pins_a>;
    status = "okay";
    spi-max-frequency = <50000000>;
    flash: xt25f128@0 {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "winbond,xt25f128", "jedec,spi-nor";
        reg = <0>;
        spi-max-frequency = <50000000>;
        partitions {
            compatible = "fixed-partitions";
            #address-cells = <1>;
            #size-cells = <1>;

            partition@0 {
                label = "u-boot";
                reg = <0x000000 0x100000>;
                read-only;
            };

            partition@100000 {
                label = "dtb";
                reg = <0x100000 0x10000>;
                read-only;
            };

            partition@110000 {
                label = "kernel";
                reg = <0x110000 0x400000>;
                read-only;
            };

            partition@510000 {
                label = "rootfs";
                reg = <0x510000 0xAF0000>;
            };
        };
    };
};

自定义根文件系统

# 解包根文件系统
tar zxvf rootfs.tar.gz -C rootfs/

# 拷贝需要的文件
cp demo rootfs/root/
cp lib/module/5.19-model/ rootfs/lib/module/ -r

# 重新打包
mkfs.jffs2 -L -s 0x100 -e 0x10000 --pad=0xAF0000 -d rootfs/ -o rootfs.jffs2

创建大小为16M的空文件

dd if=/dev/zero of=nor_flash_image bs=1M count=16

将对应文件写入启动文件 或者 flash

# 写入文件
dd if=u-boot-sunxi-with-spl.bin of=nor_flash_image bs=1K conv=notrunc
dd if=suniv-f1c100s-licheepi-nano-with-lcd.dtb of=nor_flash_image bs=1K seek=1024  conv=notrunc
dd if=zImage of=nor_flash_image bs=1K seek=1088  conv=notrunc
dd if=rootfs.jffs2 of=nor_flash_image  bs=1K seek=5184  conv=notrunc
# 直接写入flash
sudo sunxi-fel -p spiflash-write 0x000000 u-boot-sunxi-with-spl.bin
sudo sunxi-fel -p spiflash-write 0x100000 suniv-f1c100s-licheepi-nano-with-lcd.dtb
sudo sunxi-fel -p spiflash-write 0x110000 zImage
sudo sunxi-fel -p spiflash-write 0x510000 rootfs.jffs2

启动日志


从 MMC/eMMC TF 启动

创建大小为256M的空文件

dd if=/dev/zero of=tf_image bs=1M count=256

文件分区操作

# 1M,16M,c 表示
# start : 1M
# size  :  16M
# MBR 分区ID : c
# ,,L 表示 将扩展分区全部划成逻辑分区
cat <<EOT |sudo sfdisk tf_image
1M,16M,c
,,L
EOT

# 分区结构
Disk /dev/loop25: 256 MiB, 268435456 bytes, 524288 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: 0x517353ca

Device        Boot Start    End Sectors  Size Id Type
/dev/loop25p1       2048  34815   32768   16M  c W95 FAT32 (LBA)
/dev/loop25p2      34816 524287  489472  239M 83 Linux

将文件挂载为loop设备

losetup -f tf_image

格式化分区

mkfs.vfat /dev/loop25p1
mkfs.ext4 /dev/loop25p2

将uboot镜像烧录到文件头部8K偏移处

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

挂载loop设备

mount /dev/loop25p1 boot/
mount /dev/loop25p2 rootfs/

编译boot.cmd

# boot.cmd内容
setenv bootargs console=tty0 console=ttyS0,115200 panic=5 rw rootwait root=/dev/mmcblk0p2 mem=64M
load mmc 0:1 0x80C00000 suniv-f1c100s-licheepi-nano-with-lcd.dtb
load mmc 0:1 0x80008000 zImage
bootz 0x80008000 - 0x80C00000

# 编译生成boot.scr
mkimage -C none -A arm -T script -d boot.cmd boot.scr

拷贝文件到对应分区

# boot启动分区
cp zImage boot/
cp boot.scr boot/
cp suniv-f1c100s-licheepi-nano-with-lcd.dtb boot/
# rootfs根文件系统分区
tar zxvf rootfs.tar.gz -C rootfs/

驱动文件拷贝

外部程序导入

取消挂载,并移除loop设备

umount boot/ && umount rootfs && losetup -d /dev/loop25

将启动镜像烧录进容器

dd if=tf_image of=/dev/sdb bs=1M

启动日志

U-Boot SPL 2018.01-05679-g013ca457fd-dirty (Mar 19 2022 - 17:51:31)
DRAM: 64 MiB
Trying to boot from MMC1


U-Boot 2018.01-05679-g013ca457fd-dirty (Mar 19 2022 - 17:51:31 -0400)HamsterBear-Model0

CPU:   Allwinner F Series (SUNIV)
Model: Lichee Pi Nano
DRAM:  64 MiB
MMC:   SUNXI SD/MMC: 0
SF: unrecognized JEDEC id bytes: 00, 00, 00
*** Warning - spi_flash_probe_bus_cs() failed, 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
289 bytes read in 14 ms (19.5 KiB/s)
## Executing script at 80c50000
reading suniv-f1c100s-licheepi-nano-with-lcd.dtb
8200 bytes read in 28 ms (285.2 KiB/s)
reading zImage
3836184 bytes read in 186 ms (19.7 MiB/s)
## Flattened Device Tree blob at 80c00000
   Booting using the fdt blob at 0x80c00000
   Loading Device Tree to 816fa000, end 816ff007 ... OK

Starting kernel ...

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 4.15.0-rc8-licheepi-nano+ (root@100ask) (gcc version 7.5.0 (Linaro GCC 7.5-2019.12)) #6 Tue Mar 8 13:40:38 EST 2022
[    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

本文来自博客园,作者:jensenhua,转载请注明原文链接:https://www.cnblogs.com/hfwz/p/16028929.html

03-20 09:09