HamsterBear 構建可啟動的映象(更新中)

jensenhua發表於2022-03-20

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

相關文章