前言
前面尝试着编译了u-boot,kernel,并添加了rootfs,这次来生成img镜像
复制生成文件
mkdir ~/Qubot-M2H-BSP/output cd ~/Qubot-M2H-BSP/u-boot/u-boot-sunxi-with-spl.bin ~/Qubot-M2H-BSP/output cd ~/Qubot-M2H-BSP/kernel/arch/arm64/boot/Image ~/Qubot-M2H-BSP/output cd ~/Qubot-M2H-BSP/kernel/arch/arm64/boot/dts/allwinner/sun50i-h618-bananapi-m2h.dtb ~/Qubot-M2H-BSP/output cd ~/Qubot-M2H-BSP/rootfs/rootfs.tar.gz ~/Qubot-M2H-BSP/output
生成一个2G的img文件
cd output sudo dd if=/dev/zero of=./sdcard.img bs=1M count=2048
复制生成文件
sudo fdisk sdcard.img Welcome to fdisk (util-linux 2.37.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x67a959c0. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-4194303, default 2048): 40960 Last sector, +/-sectors or +/-size{K,M,G,T,P} (40960-4194303, default 4194303): 303104 Created a new partition 1 of type 'Linux' and of size 128 MiB. Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): First sector (2048-4194303, default 2048): 303106 Last sector, +/-sectors or +/-size{K,M,G,T,P} (303106-4194303, default 4194303): 4194303 Created a new partition 2 of type 'Linux' and of size 1.9 GiB. Command (m for help): w The partition table has been altered. Syncing disks.
关联 IMG 文件到 LOOP
sudo losetup -f ./sdcard.img
查看关联到了哪里
sudo losetup -l
可以看到是/dev/loop18
写入 UBOOT 到 IMG 文件中偏移 8KB 的位置
sudo dd if=./u-boot-sunxi-with-spl.bin of=/dev/loop18 bs=8K seek=1
查看分区信息
sudo fdisk -l
将这两个分区映射为 loop 设备
sudo losetup -f -o 20971520 --sizelimit 134217728 sdcard.img sudo losetup -f -o 155238272 --sizelimit 1994740736 sdcard.img
对于上面的代码,-o接起始偏移量 = 起始扇区 * 扇区大小,–sizelimit接分区大小 = 分区扇区数 * 扇区大小
sudo losetup -l
可以看到,一个19,一个20
格式化分区并挂载
sudo mkfs.fat /dev/loop19 sudo mkfs.ext4 /dev/loop20 sudo mount /dev/loop19 /mnt/boot sudo mount /dev/loop20 /mnt/rootfs
创建引导脚本
nano boot.cmd
将一下命令复制到boot.cmd文件
setenv bootcmd 'fatload mmc 0:1 0x40200000 Image;fatload mmc 0:1 0x4fa00000 sun50i-h618-bananapi-m2h.dtb;booti 0x40200000 - 0x4fa00000' setenv bootargs 'console=ttyS0,115200 root=/dev/mmcblk1p2 rootfstype=ext4 rootwait rw'
生成boot.scr文件
mkimage -C none -A arm64 -T script -d boot.cmd boot.scr
复制boot分区
sudo cp ./Image /mnt/boot/ sudo cp sun50i-h618-bananapi-m2h.dtb /mnt/boot sudo cp boot.scr /mnt/boot
解压rootfs到rootfs分区
sudo tar -xzvf ./rootfs.tar.gz -C /mnt/rootfs/
安装内核模块到 rootfs 分区
cd ../kernel/ sudo make INSTALL_MOD_PATH=/mnt/rootfs/ modules_install
取消挂载文件
sync sudo umount /mnt/rootfs sudo umount /mnt/boot
取消关联
sudo losetup -d /dev/loop18 sudo losetup -d /dev/loop19 sudo losetup -d /dev/loop20
写入到 TF 卡进行测试
sudo dd if=./sdcard.img of=/dev/sdx
暂无评论