使用QEMU模拟搭建ARM开发平台(三)——添加SCSI和MTD以及NAND flash支持

来源:互联网 发布:241数据库错误 请重试 编辑:程序博客网 时间:2024/05/02 09:42

使用versatile_defconfig编译的内核不能满足要求,现在,添加SCSI磁盘,MTD以及NAND flash的支持。

交叉编译linux内核

下载codesourcery的交叉编译工具链 https://sourcery.mentor.com/sgpp/lite/arm/portal/subscription?@template=lite, 选择目标OS为GNU/Linux。下载后解压,将/path/to/arm-2011.03/bin 添加到PATH中。

$ cd linux-2.6.39.2$ make ARCH=arm versatile_defconfig$ make ARCH=arm menuconfigKernel Features  -> Use the ARM EABI to compile the kernel  -> Allow old ABI binaries to run with this kernel (EXPERIMENTAL)$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-

编译完成后,会在arch/arm/boot/目录下生成zImage,可以使用qemu来测试编译的内核是否可以启动,如果没有安装qemu,则安装

# apt-get install qemu-system$ qemu-system-arm -M versatilepb -kernel arch/arm/boot/zImage -nographic -append "console=ttyAMA0"

可以看到,内核能启动,但是由于没有根文件系统而panic。

构建根文件系统

$ mkdir rootfs

将codesourcery工具链中针对arm的lib库复制到根文件系统中,这一步是可选的,因为在下面将把busybox编译成静态链接的包。

$ cp -r /path/to/arm-2011.03/arm-none-linux-gnueabi/libc/lib rootfs

将库文件strip,减小大小,可选的。

$ cd rootfs/lib$ arm-none-linux-gnueabi-strip *.so

提示libgcc_s.so不能识别,没关系,它是一个ASCII ld脚本,忽略即可。 现在,编译busybox-1.18.5

$ cd busybox-1.18.5$ make ARCH=arm defconfig$ make ARCH=arm menuconfigBusybox Settings--> Build Options  --> Build BusyBox as a static binary (no shared libs)

如果不将busybox编译成静态链接程序,那么前面的复制lib工作就是必须的。

$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- CONFIG_PREFIX=/patch/to/rootfs install // 将busybox安装到rootfs中将rootfs打包成一个initramfs$ cd rootfs$ find . | cpio -o -H newc | gzip -9 > ../rootfs.img$ qemu-system-arm -M versatilepb -nographic -kernel zImage -initrd rootfs.img -append "console=ttyAMA0"

可以看到,内核还是发生了panic,报错没有根文件系统,这里已经不是由kernel直接报错,而是有rootfs.img报错,因为它试图挂载真正的根文件系统。可以做如下测试

$ qemu-system-arm -M versatilepb -nographic -kernel zImage -initrd rootfs.img -append "console=ttyAMA0 root=/dev/ram"

现在,尽管最后还是panic,但是并不是没有合适的root=参数了,而是文件系统不能正确挂载。再进一步

$ qemu-system-arm -M versatilepb -nographic -kernel zImage -initrd rootfs.img -append "console=ttyAMA0 root=/dev/ram rdinit=/bin/sh"

可以发现,现在不再发生panic了,因为系统不再试图挂载真正的根文件系统。现在启动到了initramfs中,由于没有启动一个设备管理器,没有挂载proc, sysfs文件系统,busybox不能很好的工作,可以做如下测试

$ qemu-system-arm -M versatilepb -nographic -kernel zImage -initrd rootfs.img -append "console=ttyAMA0 root=/dev/ram rdinit=/sbin/init"

当busybox的/sbin/init启动后,会提示很多错误,包括没有设备文件等。现在来解决这个问题。

$ cd rootfs$ mkdir -p etc/init.d$ cat > etc/init.d/rcS <<EOF> #!/bin/sh> mkdir /proc> mkdir /sys> mount -t proc proc /proc> mount -t sysfs sysfs /sys> mdev -s> EOF$ cdmod +x etc/init.d/rcS$ find . | cpio -o -H newc | gzip -9 > ../rootfs.img$ qemu-system-arm -M versatilepb -nographic -kernel zImage -initrd rootfs.img -append "console=ttyAMA0 root=/dev/ram rdinit=/sbin/init"

现在,系统可以工作正常了,出现了提示

Please press Enter to activate this console.

编译mtd-utils

$ mkdir -p mtd/install$ tar xjf zlib-1.2.5.tar.bz2$ cd zlib-1.2.5$ ./configure --prefix=../mtd/install

在Makefile中,将gcc, ar, ranlib命令加上前缀arm-none-linux-gnueabi-

$ make$ make install$ tar xzf lzo-2.05.tar.gz$ cd lzo-2.05$ ./configure --host=arm-none-linux-gnueabi --prefix=/root/BELS/mtd/install // --prefix需要绝对路径$ make$ make install$ git clone git://git.infradead.org/mtd-utils.git$ cd mtd-utils$ git checkout v1.4.5 -b v1.4.5

现在,需要对mtd-utils打一个编译补丁,设置相应的头文件和库目录为刚才已经编译好的zlib, lzo。

diff --git a/Makefile b/Makefileindex 8bdba8e..ec7608f 100644--- a/Makefile+++ b/Makefile@@ -1,6 +1,16 @@# -*- sh -*-+PREFIX = /root/BELS/mtd/install++ZLIBCPPFLAGS = -I$(PREFIX)/include+LZOCPPFLAGS = -I$(PREFIX)/include/lzo++ZLIBLDFLAGS = -L$(PREFIX)/lib+LZOLDFLAGS = -L$(PREFIX)/lib++CFLAGS ?= -O2 -g $(ZLIBCPPFLAGS) $(LZOCPPFLAGS)+CPPFLAGS += -I./include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS)ifeq ($(WITHOUT_XATTR), 1)@@ -12,7 +22,8 @@ elseLZOLDLIBS = -llzo2endif-SUBDIRS = lib ubi-utils mkfs.ubifs+#SUBDIRS = lib ubi-utils mkfs.ubifs+SUBDIRS = lib ubi-utilsTESTS = testsTARGETS = ftl_format flash_erase nanddump doc_loadbios \diff --git a/common.mk b/common.mkindex 0f3d447..fea0651 100644--- a/common.mk+++ b/common.mk@@ -21,7 +21,7 @@ ifneq ($(WITHOUT_LARGEFILE), 1)endifDESTDIR?=-PREFIX=/usr+PREFIX?=/usrEXEC_PREFIX=$(PREFIX)SBINDIR=$(EXEC_PREFIX)/sbinMANDIR=$(PREFIX)/share/man

注意:上面的PREFIX变量的值设置为zlib和lzo的安装目录!

$ cd mtd-utils$ patch -p1 < /path/to/this.path

然后,运行如下命令编译

$ WITHOUT_XATTR=1 make CROSS=arm-none-linux-gnueabi-$ WITHOUT_XATTR=1 make CROSS=arm-none-linux-gnueabi- install

由于ubifs有额外的依赖关系,所以这里暂不编译ubifs,补丁中有一行删除了对ubifs的编译。。

支持SCSI,MTD, NAND flash

$ make ARCH=arm menuconfigmenuconfig需要配置的选项如下Kernel Features* --> Use the ARM EABI to compile the kernel* --> Allow old ABI binaries to run with this kernel (EXPERIMENTAL)Bus support* --> PCI supportDevice Drivers--> Generic Driver Options(/sbin/mdev) path to uevent helper // 这样,mdev能够帮助动态创建删除设备* --> Memory Technology Device (MTD) support--> Self-contained MTD device drivers[*|M] --> MTD using block device[*|M] --> NAND Device Support[*|M] --> Support for NAND flash Simulator[*|M] --> Support for generic platform NAND driver[*|M] --> Enable UBI - Unsorted block images[*|M] --> MTD device emulation driver (gluebi)--> SCSI device support[*] --> SCSI device support[*] --> SCSI disk support[*] --> SCSI low-level drivers<*> --> SYM53C8XX Version 2 SCSI support // 一定要选,否则不能支持SCSI设备 --> File systems[*] --> Miscellaneous filesystems[M] --> UBIFS file system support

现在重新编译

$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- INSTALL_MOD_PATH=/path/to/rootfs/ modules_install

重新打包rootfs

$ cd /path/to/rootfs$ find . | cpio -o -H newc | gzip -9 > ../rootfs.img

用qemu模拟

$ qemu-img create -f raw block.img 64M // 创建一个64M大小的磁盘$ qemu-system-arm -M versatilepb -kernel rootfs/boot/zImage-2.6.39.2 -initrd rootfs.img -nographic -append "console=ttyAMA0 root=/dev/ram rdinit=/sbin/init" -hda block.img

启动过后,就可以使用block2mtd来模拟MTD设备,也可以使用nandsim模块来模拟NAND flash设备,由于添加了/sbin/mdev为uevent helper,所以会在/dev/目录下自动创建/删除相应的设备。



今天看了《Embedded Programming with the GNU Toolchain》一文,其中介绍了利用QEMU进行ARM编程实验的一种方法。基本思想是,在一个X86的Linux开发机中安装一种称为codesourcery-tools的GNU工具链,该工具链中包含了交叉编译器,利用该工具链在宿主机中编译出ARM的可执行文件,然后利用QEMU仿真一个PXA255(connex)目标机,并将可执行文件加载到目标机中运行。

这种方法简便易行,但由于在开发机中缺少操作系统,因此在汇编语言程序中无法引发系统调用。此外,每次交叉编译完成后还要启动QEMU加载可执行文件,这也比较麻烦。因此想搭建一个运行在ARM上完整的Linux系统,能够在其中方便地进行汇编编程。在网上发现了一篇文章《Emulate an ARM Plaform with QEMU on Ubuntu 10.10》,其中采用的方法能够满足我的需要,因此进行了相应的实验(下面的操作都采用root用户身份)。

这种方法的基本思路是在一个Linux宿主机中利用QEMU运行一个虚拟机,并在虚拟机中安装一个完整的基于ARM平台的Linux环境,然后在虚拟机中安装必要的开发工具。这样,就可以在虚拟机中直接进行编程。具体的配置过程如下:

  1. 宿主机是X86,其中安装了Ubuntu 12.04。首先在宿主机中安装QEMU。
    apt-get install qemu-kvm qemu-kvm-extras
  2. Debian提供了ARM平台的内核镜像文件和initrd文件,利用这两个文件启动QEMU后可以开始一个安装过程,在QEMU的虚拟硬盘中安装一个基于ARM平台的基本操作环境。
    mkdir arm-emul; cd arm-emul
    wget ftp://ftp.debian.org/debian/dists/squeeze/main/installer-armel/current/images/versatile/netboot/vmlinuz-2.6.32-5-versatile
    wget ftp://ftp.debian.org/debian/dists/squeeze/main/installer-armel/current/images/versatile/netboot/initrd.gz
  3. 创建虚拟硬盘。
    qemu-img create -f raw hda.img 2G
  4. 启动QEMU创建一个versatilepb虚拟机,并开始安装过程。该安装过程需要从Debian服务器下载安装文件,因此在启动QEMU之前应保证宿主机已经连接Internet。
    qemu-system-arm -m 256 -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.gz -hda hda.img -append "root=/dev/ram"
    整个安装过程与通常Debian或Ubuntu的安装过程类似,只是在最后阶段选择软件包的时候可以将SSH服务器选上,这样今后可以通过SSH远程登录虚拟机。由于虚拟机是文本界面,其窗口较小,在宿主机中远程登录虚拟机后就可以在较大的终端窗口中进行编程。
    qemu-system-arm -m 256 -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.gz -hda hda.img -append "root=/dev/ram"
  5. 安装完成后,需要将虚拟机硬盘中的initrd文件拷贝到宿主机中。为此,应将虚拟机关闭,然后将虚拟硬盘中用于存储根文件系统的分区挂载到指定的目录,最后将文件复制出来。在挂载之前应先计算出硬盘分区的偏移量。具体的计算方法见《如何挂载一个镜像文件》一文。
    mount -o loop,offset=1048576 hda.img /mnt
    cp /mnt/boot/initrd.img-2.6.32-5-versatile .
    umount /mnt
  6. 编写一个启动脚本,用于在宿主机中启用TAP,然后启动虚拟机。虚拟机与宿主机在同一个网段中,并假设该网段中有DHCP服务器。如不存在DHCP服务,则需要相应地修改脚本。
    #! /bin/sh

    ifconfig eth0 down                  
    brctl addbr br0                     
    brctl addif br0 eth0               
    ifconfig br0 0.0.0.0 promisc up     
    ifconfig eth0 0.0.0.0 promisc up    
    dhclient br0                        
    tunctl -t tap0 -u root              
    brctl addif br0 tap0                
    ifconfig tap0 0.0.0.0 promisc up   

    qemu-system-arm -m 256 -M versatilepb -kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda hda.img -append "root=/dev/sda1" -net nic -net tap,ifname=tap0,script=no,downscript=no
  7. 重新启动虚拟机后,安装编程所需要的工具。
    apt-get install build-essential   # 工具链
    apt-get install jed               # 编辑器
  8. 在宿主机中开一个终端窗口,执行ssh命令远程登录虚拟机。现在就可以开始ARM的编程了。


参考文献:
Embedded Programming with the GNU Toolchain
codesourcery-tools
Emulate an ARM Plaform with QEMU on Ubuntu 10.10
Debian FTP
如何挂载一个镜像文件

0 0