制作imx6ull Linux系统的img镜像文件
1.什么是img镜像文件
在讲解img镜像文件之前我们要了解一般的imx6ull系列开发板烧录F方法一般有两种,一种是通过通过USB刷机,用mfgtools工具将uboot kernel dtb rootfs这些文件烧写到emmc nandflash sd卡等存储介质上面,但是这种方法的缺点是烧写比较慢,而且只能一个一个刷写,官方推荐的量产烧写方式是通过SD卡刷机,就是把SD卡做成启动盘,然后启动系统,再把内容烧录到emmc或者nand flash里面。
另外一种就是通过SD卡(EMMC)进行烧录,这里介绍一种将uboot kernel dtb rootfs打包成img镜像,然后通过Win32 Disk Imager烧录的方法。
接着我们介绍一下img镜像文件,我们可以img文件可以简单理解为一个磁盘,可以存放二进制数据,可以进行分区,可以建立文件系统等操作。
一般img镜像文件分区如下表:
Start Address | size | Usage |
---|---|---|
0x0 | 2 sectors(1K) | Reserved for partition table |
2 sector(K,0x400) | 20414 sectors(9M+) | i.MX6MQ u-boot image |
20480 sector (10M,0xa00000) | 204800 sectors(100MB) | FAT32 Boot partition |
225280 sector(110M,0x6e00000) | Remaining Space | EXT4 filesystem for rootfs |
2.制作流程
1.使用dd 命令,根据固件的大小建立一个合适的 img 空文件。
2. 对 img 文件进行分区。
3. 用dd命令创建装载u-boot。
4. 用losetup命令将 img 文件虚拟成循环块设备 /dev/loopx ,并用 kpartx命令在/dev/mapper/下,将虚拟出的循环块设备分区并格式化。
5.创建挂载点,下面建立文件系统并且 mount 上来 。
6.使用文件复制命令cp将 zImage dtb rootfs 放到指定的文件系统分区 。
7.取消挂载,复制文件,烧录到 emmc卡。
下面开始制作img镜像
准备工作:
需要安装的软件:
sudo apt-get install dosfstools dump parted kpartx
将制作镜像需要的 zImage dtb rootfs等文件拷贝到imx6ull文件下并建立镜像文件夹:
mkdir images
我们先启动编译脚本编译生成内核:
./build.sh
内核编译生成后,我们进入images文件夹
开始制作镜像
首先我们用先创建一个空的img文件:
dd if=/dev/zero of=linuxsys_iMX6ULL_512MB.img bs=512k count=1024 && sync
of=linuxsys_iMX6ULL_512MB.img 表示输出文件为 linuxsys_iMX6ULL_512MB.img
bs=512 扇区是 512 字节
count=1024 表示扇区的数量
sync 是用于将内容直接写入磁盘
到这里我们的空镜像文件就已经创建好了
接下来我们根据分区表进行分区:
parted linuxsys_iMX6ULL_512MB.img mklabel msdos
parted linuxsys_iMX6ULL_512MB.img mkpart primary fat32 10M 110M
parted linuxsys_iMX6ULL_512MB.img mkpart primary ext4 110M 100%
sync
fat32 文件系统类型为 fat32 这是内核设备树的文件系统
10M 这个表示内核设备树扇区的起始地址(注意不是实际地址,扇区地址x扇区大小=实际地址),这个分区紧跟在 10M 的 u-boot 空间后面
110M内核设备树扇区的结束地址=扇区起始地址+扇区大小-1,
ext4 文件系统为 ext4 这是根文件系统的
110M 文件系统的扇区起始地址,100%为文件系统的结束地址剩余内存,也可以不填。
接下来安装u-boot
sudo dd if=`pwd`/../bin//u-boot-imx6ull-14x14-ddr512-emmc.imx of=linuxsys_iMX6ULL_512MB.img bs=1k seek=1 conv=notrunc,sync
将img文件虚拟成块设备
losetup /dev/loop0 linuxsys_iMX6ULL_512MB.img
这里的loop0如果被占用,那么可以用losetup -f 查找空闲的循环设备
将虚拟成的块设备分区
kpartx -av /dev/loop0
被分区后的设备在/dev/mapper/路径下
接着我们将分区格式化
mkfs.vfat /dev/mapper/loop0p1
mkfs.ext4 /dev/mapper/loop0p2
sync
格式化后,创建分区对应的挂载点,将分区挂载起来以便于拷贝内核和根文件系统
#创建内核挂载点
mkdir -p sd1
#将内核分区挂载到对应的挂载点
mount -t vfat /dev/mapper/loop0p1 sd1
#拷贝内核
cp `pwd`/../tarballs/zImage sd1
cp `pwd`/../tarballs//imx6ull-xly-emmc-lcd.dtb sd1
sync
#卸载挂载点
umount sd1
#删除sd1
rm -rf sd1
接着和上面一样,移植根文件系统
mkdir -p sd2
mount -t ext4 /dev/mapper/loop0p2 sd2
tar -xjf `pwd`/../tarballs//rootfs.tar.bz2 -C sd2 && sync
umount sd2
rm -rf sd2
接着我们要将刚才的虚拟和分区,卸载还原
kpartx -dv /dev/loop0
losetup -d /dev/loop0
最后我们将做好的img文件打包
bzip2 linuxsys_iMX6ULL_512MB.img
再拷贝到windows下,烧录就完成了