基于QEMU搭建完整的虚拟ARM开发环境(uboot+linux+rootfs)

 

主机环境:ubuntu 16.04虚拟机,目标机器:QEMU模拟vexpress-a9。
------------------------------------------------
一、准备工作
安装qemu, 交叉编译工具等略过。


konga@ubuntu:~/work/vexpress/uboot$ qemu-system-arm -M ? 
Supported machines are:
……
versatileab          ARM Versatile/AB (ARM926EJ-S)
versatilepb          ARM Versatile/PB (ARM926EJ-S)
vexpress-a15         ARM Versatile Express for Cortex-A15
vexpress-a9          ARM Versatile Express for Cortex-A9
……

准备一个单独的目录vexpress, uboot, linux和busybox源码并解压,创建镜像文件目录image

konga@ubuntu:~/work/vexpress$ ls 
busybox-1.27.1.tar.bz2  linux-4.9.39.tar.xz  u-boot-2017.07.tar.bz2

konga@ubuntu:~/work/vexpress$ tar xjf busybox-1.27.1.tar.bz2  
konga@ubuntu:~/work/vexpress$ mv busybox-1.27.1 busybox
konga@ubuntu:~/work/vexpress$ tar xJf linux-4.9.39.tar.xz 
konga@ubuntu:~/work/vexpress$ rm linux-4.9.39.tar.xz 
konga@ubuntu:~/work/vexpress$ mv linux-4.9.39/ linux
konga@ubuntu:~/work/vexpress$ tar xjf u-boot-2017.07.tar.bz2  
konga@ubuntu:~/work/vexpress$ rm u-boot-2017.07.tar.bz2 
konga@ubuntu:~/work/vexpress$ mv u-boot-2017.07/ uboot
konga@ubuntu:~/work/vexpress$ rm busybox-1.27.1.tar.bz2 

 

konga@ubuntu:~/work/vexpress$ mkdir image

konga@ubuntu:~/work/vexpress$ ls
busybox  image  linux  uboot


------------------------------------------------
二、编译运行uboot

创建mkuboot.sh脚本并添加执行权限,进行编译
konga@ubuntu:~/work/vexpress/uboot$ ls configs/vexpress_* 
configs/vexpress_aemv8a_dram_defconfig  configs/vexpress_aemv8a_semi_defconfig  configs/vexpress_ca5x2_defconfig
configs/vexpress_aemv8a_juno_defconfig  configs/vexpress_ca15_tc2_defconfig     configs/vexpress_ca9x4_defconfig

konga@ubuntu:~/work/vexpress/uboot$ cat mkuboot.sh 

	export ARCH=arm
	export CROSS_COMPILE=arm-linux-gnueabi-
	make  vexpress_ca9x4_defconfig
	make V=1

konga@ubuntu:~/work/vexpress/uboot$ chmod +x mkuboot.sh 
konga@ubuntu:~/work/vexpress/uboot$ ./mkuboot.sh

编译完成后在当前目录有u-boot文件,用下面命令可以跑起来
konga@ubuntu:~/work/vexpress/uboot$ qemu-system-arm -M vexpress-a9 -m 512 -nographic  -kernel u-boot
audio: Could not init `oss' audio driver

U-Boot 2017.07 (Mar 17 2018 - 20:09:31 +0800)

DRAM:  512 MiB
WARNING: Caches not enabled
Flash: 128 MiB
MMC:   MMC: 0
*** Warning - bad CRC, using default environment
In:    serial
Out:   serial
Err:   serial
Net:   smc911x-0
Hit any key to stop autoboot:  0 
=> 

此时的uboot没有网络支持,无法通过tftp下载内核镜像,将其复制到image目录
konga@ubuntu:~/work/vexpress/uboot$  cp u-boot ../image/
konga@ubuntu:~/work/vexpress/uboot$  echo "cp u-boot ../image/"  >> mkuboot.sh

把uboot目录下的mkimage命令所在目录加到PATH环境变量(略过)
konga@ubuntu:~/work/vexpress/uboot$ find . -name mkimage
./tools/mkimage

------------------------------------------------------
三、编译linux并运行

konga@ubuntu:~/work/vexpress/linux$ ls arch/arm/configs/ve* 
arch/arm/configs/versatile_defconfig  arch/arm/configs/vexpress_defconfig

创建mklinux.sh脚本赋权限并执行
konga@ubuntu:~/work/vexpress/linux$ cat mklinux.sh 

export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
make vexpress_defconfig
make -j4
make modules -j4
make uImage LOADADDR=0x60000000
make dtbs

cp -f arch/arm/boot/uImage  ../image/
cp -f arch/arm/boot/zImage  ../image/
cp -f arch/arm/boot/dts/vexpress-v2p-ca9.dtb  ../image/device.dtb

konga@ubuntu:~/work/vexpress/linux$ ./mklinux.sh

编译完成后可用直接单独跑下内核:
konga@ubuntu:~/work/vexpress/linux$ qemu-system-arm -M vexpress-a9 -m 512 -kernel arch/arm/boot/zImage -nographic -append "console=ttyAMA0" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb

audio: Could not init `oss' audio driver
Booting Linux on physical CPU 0x0
Linux version 4.9.39 (konga@ubuntu) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) ) #1 SMP Sat Mar 17 20:31:19 CST 2018
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
OF: fdt:Machine model: V2P-CA9
Memory policy: Data cache writeback
……
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
Please append a correct "root=" boot option; here are the available partitions:
1f00          131072 mtdblock0  (driver?)
1f01           32768 mtdblock1  (driver?)
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.9.39 #1
Hardware name: ARM-Versatile Express
[<8010e5c8>] (unwind_backtrace) from [<8010b028>] (show_stack+0x10/0x14)
[<8010b028>] (show_stack) from [<80356200>] (dump_stack+0x84/0x98)
[<80356200>] (dump_stack) from [<801a8d38>] (panic+0xdc/0x248)
[<801a8d38>] (panic) from [<808012ac>] (mount_block_root+0x1c8/0x2a0)
[<808012ac>] (mount_block_root) from [<808014a8>] (mount_root+0x124/0x12c)
[<808014a8>] (mount_root) from [<80801600>] (prepare_namespace+0x150/0x198)
[<80801600>] (prepare_namespace) from [<80800eb4>] (kernel_init_freeable+0x250/0x260)
[<80800eb4>] (kernel_init_freeable) from [<805d276c>] (kernel_init+0x8/0x114)
[<805d276c>] (kernel_init) from [<80107578>] (ret_from_fork+0x14/0x3c)
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)


目前没有根文件系统,上面报错是正常的。


---------------------------------------------
四、配置tftp

konga@ubuntu:~/work/vexpress/linux$ sudo apt-get install tftp-hpa tftpd-hpa


修改tftp配置文件:
konga@ubuntu:/etc/default$ cat tftpd-hpa 

	# /etc/default/tftpd-hpa


	TFTP_USERNAME="tftp"
	TFTP_DIRECTORY="/home/konga/work/vexpress/image"
	TFTP_ADDRESS=":69"
	TFTP_OPTIONS="--secure"

重启tftp服务
konga@ubuntu:/etc/default$  sudo service tftpd-hpa restart


确保可以正常下载image目录的文件,必要时可能要增加image目录的权限
konga@ubuntu:~$ ls work/vexpress/image/
device.dtb  u-boot  uImage
konga@ubuntu:~$ tftp localhost
tftp> get uImage
tftp> quit
konga@ubuntu:~$ ls 
bin  Desktop  docs  uImage  work


-----------------------------------------------------
五、配置TUN/TAP虚拟网络

konga@ubuntu:~/work/vexpress/linux$ sudo apt-get install bridge-utils uml-utilities 

检查是否有/dev/net/tun 设备文件,无则自行配置安装。

创建脚本并放在自己的bin目录(ens38为虚拟网卡接口,网段为10.8.8.0,VMWare中的"仅主机模式"):
konga@ubuntu:~/bin$ cat tuntap.sh 

#!/bin/bash
if test -z $1 ; then
	echo need a arg: down/up
	exit
fi 

if [ "up" = $1 ] ; then

	brctl addbr br0

	ifconfig ens38 down
	brctl addif br0 ens38

	#关闭生成树协议
	brctl stp br0 off

	ifconfig br0 10.8.8.3 netmask 255.255.255.0 promisc up
	ifconfig ens38 10.8.8.2 netmask 255.255.255.0 promisc up

	tunctl -t tap0 -u konga
	ifconfig tap0 10.8.8.4 netmask 255.255.255.0 promisc up
	brctl addif br0 tap0
else
    	ifconfig tap0 down
	brctl delif br0 tap0

	ifconfig ens38 down
	brctl delif br0 ens38

	ifconfig br0 down
	brctl delbr br0

        ifconfig ens38 10.8.8.2 netmask 255.255.255.0
fi

上面脚本2021.04 修改简化:

#!/bin/bash
#sudo apt-get install bridge-utils uml-utilities

if test -z $1 ; then
	echo need a arg: down/up
	exit
fi 

if [ "up" = $1 ] ; then

	brctl addbr br0
	brctl stp br0 off   #关闭生成树协议
	ifconfig br0 10.2.2.20 netmask 255.255.255.0 up

	ifconfig eth2 up
	brctl addif br0 eth2

	tunctl -t tap0 -u konga
	ifconfig tap0 up
	brctl addif br0 tap0

else
	brctl delif br0 tap0
	tunctl -d tap0
	
	brctl delif br0 eth2
	ifconfig eth2 down

	ifconfig br0 down
	brctl delbr br0
fi

为了方便执行,在~/.bashrc加入两行:

alias ttup='sudo ~/bin/tuntap.sh up'
alias ttdown='sudo ~/bin/tuntap.sh down'

konga@ubuntu:~/bin$  source ~/.bashrc


-----------------------------------------
六、用uboot下载内核并执行

konga@ubuntu:~/work/vexpress/image$ ls
device.dtb  u-boot  uImage

konga@ubuntu:~/work/vexpress/image$ ttup 
Set 'tap0' persistent and owned by uid 1000

带网络执行uboot,进入命令行:
konga@ubuntu:~/work/vexpress/image$ qemu-system-arm -M vexpress-a9 -m 512 -nographic  -net nic -net tap,ifname=tap0,script=no -kernel u-boot
audio: Could not init `oss' audio driver


U-Boot 2017.07 (Mar 17 2018 - 20:17:48 +0800)

DRAM:  512 MiB
WARNING: Caches not enabled
Flash: 128 MiB
MMC:   MMC: 0
*** Warning - bad CRC, using default environment


In:    serial
Out:   serial
Err:   serial
Net:   smc911x-0
Hit any key to stop autoboot:  0 

=> ping 10.8.8.2
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
smc911x: MAC 52:54:00:12:34:56
host 10.8.8.2 is alive

=> ping 10.8.8.3
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
smc911x: MAC 52:54:00:12:34:56
host 10.8.8.3 is alive

=> ping 10.8.8.4
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
smc911x: MAC 52:54:00:12:34:56
host 10.8.8.4 is alive

设置环境变量,自己IP,tftp服务器:
=> setenv ipaddr 10.8.8.100; setenv netmask 255.255.255.0; setenv serverip 10.8.8.3;

下载内核镜像、设备树并启动内核(如果没有加载设备树,会卡在 Starting kernel...这一步):
=> tftp 0x60000000 uImage; tftp 0x70000000 device.dtb; fdt addr 0x70000000; bootm 0x60000000 - 0x70000000

smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
TFTP from server 10.8.8.3; our IP address is 10.8.8.100
Filename 'uImage'.
Load address: 0x60000000
Loading: #################################################################
#################################################################
#################################################################
############################################
5.1 MiB/s
done
Bytes transferred = 3500592 (356a30 hex)
smc911x: MAC 52:54:00:12:34:56
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
TFTP from server 10.8.8.3; our IP address is 10.8.8.100
Filename 'device.dtb'.
Load address: 0x70000000
Loading: ##
50.8 KiB/s
done
Bytes transferred = 14708 (3974 hex)
smc911x: MAC 52:54:00:12:34:56
## Booting kernel from Legacy Image at 60000000 ...
   Image Name:   Linux-4.9.39
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    3500528 Bytes = 3.3 MiB
   Load Address: 60000000
   Entry Point:  60000000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 70000000
   Booting using the fdt blob at 0x70000000
   Loading Kernel Image ... OK
   Loading Device Tree to 7fed4000, end 7feda973 ... OK

Starting kernel ...

Booting Linux on physical CPU 0x0
Linux version 4.9.39 (konga@ubuntu) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) ) #1 SMP Sat Mar 17 20:31:19 CST 2018
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
OF: fdt:Machine model: V2P-CA9
Memory policy: Data cache writeback
CPU: All CPU(s) started in SVC mode.
percpu: Embedded 13 pages/cpu @9ffba000 s23372 r8192 d21684 u53248
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 130048
Kernel command line: console=ttyAMA0
log_buf_len individual max cpu contribution: 4096 bytes
log_buf_len total cpu_extra contributions: 12288 bytes
log_buf_len min size: 16384 bytes
log_buf_len: 32768 bytes
。。。。。。
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
Please append a correct "root=" boot option; here are the available partitions:
1f00          131072 mtdblock0  (driver?)
1f01           32768 mtdblock1  (driver?)
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.9.39 #1
Hardware name: ARM-Versatile Express
[<8010e5c8>] (unwind_backtrace) from [<8010b028>] (show_stack+0x10/0x14)
[<8010b028>] (show_stack) from [<80356200>] (dump_stack+0x84/0x98)
[<80356200>] (dump_stack) from [<801a8d38>] (panic+0xdc/0x248)
[<801a8d38>] (panic) from [<808012ac>] (mount_block_root+0x1c8/0x2a0)
[<808012ac>] (mount_block_root) from [<808014a8>] (mount_root+0x124/0x12c)
[<808014a8>] (mount_root) from [<80801600>] (prepare_namespace+0x150/0x198)
[<80801600>] (prepare_namespace) from [<80800eb4>] (kernel_init_freeable+0x250/0x260)


内核报错,还是因为没有根文件系统。


------------------------------------
七、制作根文件系统
创建脚本并执行:
konga@ubuntu:~/work/vexpress/busybox$ cat mkbusybox.sh

 

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- install


dd if=/dev/zero of=sdcard.ext3 bs=1M count=32
mkfs.ext3 sdcard.ext3
sudo rm -fr rootfs;
mkdir rootfs
sudo mount -t ext3 -o loop sdcard.ext3  rootfs/
cd rootfs;
sudo mkdir lib proc sys dev etc etc/init.d
sudo cp -r ../_install/* .
sudo cp -P /usr/arm-linux-gnueabi/lib/*  lib/


sudo cp ../examples/bootfloppy/etc/inittab  etc/
sudo touch etc/init.d/rcS ;
sudo chmod 0777 etc/init.d/rcS;
sudo echo "#!/bin/sh" >> etc/init.d/rcS
sudo echo "mount -t proc none /proc" >> etc/init.d/rcS
sudo echo "mount -t sysfs none /sys" >> etc/init.d/rcS
sudo echo "mount -t tmpfs none /dev" >> etc/init.d/rcS
sudo echo "/sbin/mdev -s" >> etc/init.d/rcS
sudo chmod 0755 etc/init.d/rcS;


cd ..;
sudo umount rootfs/
cp sdcard.ext3 ../image/


konga@ubuntu:~/work/vexpress/busybox$ bash -x mkbusybox.sh
编译制作根文件系统完成。

----------------------------------------------
八、完整运行

konga@ubuntu:~/work/vexpress/image$ ls
device.dtb  sdcard.ext3  u-boot  uImage

konga@ubuntu:~/work/vexpress/image$ qemu-system-arm -M vexpress-a9 -m 512 -nographic -net nic -net tap,ifname=tap0,script=no -sd sdcard.ext3 -kernel u-boot

WARNING: Image format was not specified for 'sdcard.ext3' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
audio: Could not init `oss' audio driver

U-Boot 2017.07 (Mar 17 2018 - 20:17:48 +0800)

DRAM:  512 MiB
WARNING: Caches not enabled
Flash: 128 MiB
MMC:   MMC: 0
*** Warning - bad CRC, using default environment


In:    serial
Out:   serial
Err:   serial
Net:   smc911x-0
Hit any key to stop autoboot:  0 
=>

 

=> setenv ipaddr 10.8.8.100; setenv netmask 255.255.255.0; setenv serverip 10.8.8.3; setenv bootargs "console=ttyAMA0 root=/dev/mmcblk0 init=/linuxrc rw rootwait"

=> tftp 0x60000000 uImage; tftp 0x70000000 device.dtb; fdt addr 0x70000000; bootm 0x60000000 - 0x70000000 
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
TFTP from server 10.8.8.3; our IP address is 10.8.8.100
Filename 'uImage'.
Load address: 0x60000000
Loading: #################################################################
#################################################################
#################################################################
############################################
8.5 MiB/s
done
Bytes transferred = 3500592 (356a30 hex)
smc911x: MAC 52:54:00:12:34:56
smc911x: MAC 52:54:00:12:34:56
smc911x: detected LAN9118 controller
smc911x: phy initialized
smc911x: MAC 52:54:00:12:34:56
Using smc911x-0 device
TFTP from server 10.8.8.3; our IP address is 10.8.8.100
Filename 'device.dtb'.
Load address: 0x70000000
Loading: ##
2.3 MiB/s
done
Bytes transferred = 14708 (3974 hex)
smc911x: MAC 52:54:00:12:34:56
## Booting kernel from Legacy Image at 60000000 ...
   Image Name:   Linux-4.9.39
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    3500528 Bytes = 3.3 MiB
   Load Address: 60000000
   Entry Point:  60000000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 70000000
   Booting using the fdt blob at 0x70000000
   Loading Kernel Image ... OK
   Loading Device Tree to 7fed4000, end 7feda973 ... OK


Starting kernel ...


Booting Linux on physical CPU 0x0
Linux version 4.9.39 (konga@ubuntu) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) ) #1 SMP Sat Mar 17 20:31:19 CST 2018
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
OF: fdt:Machine model: V2P-CA9
Memory policy: Data cache writeback
。。。。。。


Please press Enter to activate this console. 
/ #
/ # uname -a 
Linux (none) 4.9.39 #1 SMP Sat Mar 17 20:31:19 CST 2018 armv7l GNU/Linux


/ # cat /proc/cpuinfo 
processor : 0
model name : ARMv7 Processor rev 0 (v7l)
BogoMIPS : 666.41
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc09
CPU revision : 0


Hardware : ARM-Versatile Express
Revision : 0000
Serial : 0000000000000000

 

 

/ #

 

 

=======================

九、只运行内核和根文件系统

qemu-system-arm -M vexpress-a9 -m 512 -sd sdcard.ext3 -nographic -kernel zImage  -dtb device.dtb   -append "console=ttyAMA0 rootwait rw root=/dev/mmcblk0 init=/linuxrc"

 

 

 

 

  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值