利用QEMU模拟启动uboot 内核 busybox

环境配置:
实体机:Windows7
虚拟机:Linux ubuntu 4.15.0
模拟开发板:vexpress-a9
linux内核版本:linux-4.4.76.tar.xz
busybox版本:busybox-1.35.0.tar.bz2
u-boot版本:u-boot-2017.05.tar.bz2
其中内核、busybox、u-boot下载地址
http://t.csdn.cn/fjrVe

1.QEMU是什么

QEMU是一套由法布里斯·贝拉(Fabrice Bellard)所编写的以GPL许可证分发源码的模拟处理器软件,在GNU/Linux平台上使用广泛。Bochs,PearPC等与其类似,但不具备其许多特性,比如高速度及跨平台的特性,通过KQEMU这个闭源的加速器,QEMU能模拟至接近真实电脑的速度。
优点:
1.可以不需要依赖开发板,qemu支持多种开发板的模拟;
2.可以模拟arm mips等常见的架构

当然也有缺点,就是性能比起真实的开发板还是有差异,如果是学习其实已经足够。

2.安装QEMU

2.1源码安装

下载安装QEMU需要依赖的库
apt install zlib1g-dev
apt install libglib2.0-0 libglib2.0-dev
apt install libsdl1.2-dev
apt install libpixman-1-dev libfdt-dev

QEMU源码下载
https://www.qemu.org/download/
使用2.7.0版本进行编译安装
wget https://www.qemu.org/download/qemu-2.7.0.tar.xz
解压 tar -xvf qemu-2.7.0.tar.xz

进入源码目录
配置为ARM架构
./configure --target-list=arm-softmmu --audio-drv-list
make
make install

2.2联网安装

apt install qemu

查看支持哪些开发板
qemu-system-arm -M help

2.3安装交叉编译链

apt install arm-linux-gnueabi-gcc
//查看安装的apt软件
apt list --installed
sudo apt-get autoremove --purge
命令&参数解释:
sudo——获取 root 权限
apt-get——执行安装卸载功能的软件
autoremove——告诉 apt-get 我们所要做的操作是移除软件
–purge——注意这前面是两个短划线,这个参数是告诉他们要完整的干净的彻底的移除
加上即将卸载的软件名称
sudo apt-get autoremove --purge vim

3.Linux内核kernel

3.1内核编译

内核版本:linux-4.4.76.tar.xz
解压 :tar -xvf linux-4.4.76.tar.xz

1.修改定层Makefile文件,linux-4.4.7下的Makefile修改ARCH、CROSS_COMPILE为

255:ARCH                ?= arm
256:CROSS_COMPILE       ?= arm-linux-gnueabi-

2.linux-4.4.7下执行生成编译配置文件
make vexpress_defconfig
3.linux-4.4.7下执行生成uImage文件
make LOADADDR uImage -j
4.linux-4.4.7下执行编译设备树
make dtbs
文件生成:
linux-4.4.76/arch/arm/boot
此目录下回有对应的uImage dtb文件

4.Linux根文件系统busybox

4.1 编译busybox

版本:busybox-1.35.0.tar.bz2
解压:tar -xvf busybox-1.35.0.tar.bz2

1.修改Makefile文件
busybox的根目录下修改Makefile文件;

164:CROSS_COMPILE ?=arm-linux-gnueabi-
190:ARCH ?= arm

2.配置busybox
make defconfig
图形化配置:在此选择静态编译;
make menuconfig
在这里插入图片描述
3.编译busybox
make -j
4.安装busybox
make install
5.文件生成
会生成_install目录
busybox-1.35.0/_install

bin  linuxrc  sbin  usr

4.2 制作根文件系统

dd if=/dev/zero of=rootfs.ext3 bs=1M count=32
mkfs.ext3 rootfs.ext3
mount -t ext3 rootfs.ext3 /mnt/ -o loop
cp -r rootfs/* /mnt/

/etc下增加如下目录文件
./HOSTNAME
./sysconfig
./sysconfig/HOSTNAME
./inittab
./init.d
./init.d/rcS
./fstab

/etc # cat inittab
#system script file
::sysinit:/etc/init.d/rcS
::askfirst:-/bin/sh
::ctrlaltdel:-/sbin/reboot
#umount all filesystem
::shutdown:/bin/umount -a -r
#restart init process
::restart:/sbin/init
**************************************************************************************************
/etc # cat fstab

#       <file system>   <mount point>   <type>  <options>       <dump>  <pass>
        proc                    /proc                   proc    defaults        0               0
        sysfs                   /sys                    sysfs   defaults        0               0
        tmpfs                   /var                    tmpfs   defaults        0               0
        tmpfs                   /tmp                    tmpfs   defaults        0               0
        tmpfs                   /dev                    tmpfs   defaults        0               0
        
**************************************************************************************************
/etc/init.d # cat rcS
#!bin/sh
echo "-----------------------------------------------------------"

echo "                welcome to busybox!!!                      "

echo "-----------------------------------------------------------"

PATH=/sbin:/bin:/usr/sbin:/usr/bin
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
mount -a
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
/bin/hostname -F /etc/sysconfig/HOSTNAME
ifconfig eth0 192.168.17.138

unmount /mnt/

4.3修改根文件系统增加欢迎界面

内核挂载根文件系统后,首先执行busybox的init_main函数:
1.设置信号
2.解析etc/inittab文件
3.执行应用程序

我们的根文件系统目前还没有etc/inittab文件,默认执行etc/init.d/rcS文件。
我们在rcS中加入欢迎界面,从新启动内核与根文件系统,可以看见rcS文件被正确执行。

5.bootloader启动

5.1介绍

在嵌入式操作系统中,BootLoader是在操作系统内核运行之前运行。可以初始化硬件设备、建立内存空间映射图,从而将系统的软硬件环境带到一个合适状态,以便为最终调用操作系统内核准备好正确的环境。在嵌入式系统中,通常并没有像BIOS那样的固件程序(注,有的嵌入式CPU也会内嵌一段短小的启动程序),因此整个系统的加载启动任务就完全由BootLoader来完成。
嵌入式中常使用uboot作为启动程序,以此来启动内核。

5.2编译u-boot

u-boot版本:u-boot-2017.05.tar.bz2

sudo apt-get install u-boot-tools
修改Makefile
ARCH=arm	config.mk												
CROSS_COMPILE=arm-linux-gnueabi-   //Makefile文件
make vexpress-ca9x4-defconfig
make LOADADDR=0x60003000 -j

5.3配置网络相关

网络功能
apt install uml-utilities bridge-utils
ls /dev/net
vim /etc/network/interfaces
auto ens33
auto br0
iface br0 inet dhcp
bridge_ports ens33

安装主机的TFTP
apt-get install tftp-hpa tftpd-hpa xinetd
/etc/default/tftpd-hpa

配置tftp参数etc/interface/interfaces

auto lo
iface lo inet loopback

auto ens33
auto br0
iface br0 inet dhcp
bridge_ports ens33

5.3.1配置tftp

新建home/tftpboot目录

root@ubuntu:/etc/network# cat /etc/default/tftpd-hpa
# /etc/default/tftpd-hpa

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/home/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -c -s"

5.4通过uboot启动内核

u-boot中手动启动内核;
ipaddr为开发板的ip,server为服务器的ip
setenv ipaddr 192.168.17.138
setenv serverip 192.168.17.130
tftp 0x60003000 uImage
tftp 0x60500000 vexpress-v2p-ca9.dtb
setenv bootargs ”root=/dev/mmcblk0 rw console=ttyAMA0“
bootm 0x60003000 - 0x60500000

5.5效果展示

boot.sh的内容
qemu-system-arm -M vexpress-a9 -m 512M -kernel u-boot -nographic -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 -append “root=/dev/mmcblk0 rw console=ttyAMA0” -sd rootfs.ext3

root@ubuntu:/home/tftpboot# ./boot.sh
+ switch=br0
+ [ -n tap0 ]
+ whoami
+ ip tuntap add tap0 mode tap user root
ioctl(TUNSETIFF): Device or resource busy
+ ip link set tap0 up
+ sleep 0.5s
+ ip link set tap0 master br0
+ exit 0
WARNING: Image format was not specified for 'rootfs.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.


U-Boot 2017.05 (Nov 26 2022 - 22:50:16 -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
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 192.168.17.130; our IP address is 192.168.17.138
Filename 'uImage'.
Load address: 0x60003000
Loading: #################################################################
         #################################################################
         #################################################################
         ###########################################
         2.7 MiB/s
done
Bytes transferred = 3486048 (353160 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 192.168.17.130; our IP address is 192.168.17.138
Filename 'vexpress-v2p-ca9.dtb'.
Load address: 0x60500000
Loading: #
         3.4 MiB/s
done
Bytes transferred = 14360 (3818 hex)
smc911x: MAC 52:54:00:12:34:56
## Booting kernel from Legacy Image at 60003000 ...
   Image Name:   Linux-4.4.76
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    3485984 Bytes = 3.3 MiB
   Load Address: 60003000
   Entry Point:  60003000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 60500000
   Booting using the fdt blob at 0x60500000
   Loading Kernel Image ... OK
   Loading Device Tree to 7fe54000, end 7fe5a817 ... OK

Starting kernel ...

Booting Linux on physical CPU 0x0
Initializing cgroup subsys cpuset
Linux version 4.4.76 (root@ubuntu) (gcc version 5.3.1 20160113 (Linaro GCC 5.3-2016.02) ) #1 SMP Tue Nov 22 02:18:49 PST 2022
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
Machine model: V2P-CA9
Memory policy: Data cache writeback
CPU: All CPU(s) started in SVC mode.
PERCPU: Embedded 12 pages/cpu @9ffbd000 s18240 r8192 d22720 u49152
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 130048
Kernel command line: root=/dev/mmcblk0 rw 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
early log buf free: 14956(91%)
PID hash table entries: 2048 (order: 1, 8192 bytes)
Dentry cache hash table entries: 65536 (order: 6, 262144 bytes)
Inode-cache hash table entries: 32768 (order: 5, 131072 bytes)
Memory: 512584K/524288K available (4889K kernel code, 156K rwdata, 1392K rodata, 284K init, 153K bss, 11704K reserved, 0K cma-reserved)
Virtual kernel memory layout:
    vector  : 0xffff0000 - 0xffff1000   (   4 kB)
    fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
    vmalloc : 0xa0800000 - 0xff800000   (1520 MB)
    lowmem  : 0x80000000 - 0xa0000000   ( 512 MB)
    modules : 0x7f000000 - 0x80000000   (  16 MB)
      .text : 0x80008000 - 0x8062a7a8   (6282 kB)
      .init : 0x8062b000 - 0x80672000   ( 284 kB)
      .data : 0x80672000 - 0x806992a0   ( 157 kB)
       .bss : 0x8069c000 - 0x806c24f8   ( 154 kB)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
Hierarchical RCU implementation.
        Build-time adjustment of leaf fanout to 32.
        RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.
RCU: Adjusting geometry for rcu_fanout_leaf=32, nr_cpu_ids=4
NR_IRQS:16 nr_irqs:16 16
GIC CPU mask not found - kernel will fail to boot.
GIC CPU mask not found - kernel will fail to boot.
L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
L2C-310 enabling early BRESP for Cortex-A9
L2C-310 full line of zeros enabled for Cortex-A9
L2C-310 dynamic clock gating disabled, standby mode disabled
L2C-310 cache controller enabled, 8 ways, 128 kB
L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
smp_twd: clock not found -2
sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
Console: colour dummy device 80x30
Calibrating local timer... 86.48MHz.
Calibrating delay loop... 264.19 BogoMIPS (lpj=1320960)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 1024 (order: 0, 4096 bytes)
Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes)
CPU: Testing write buffer coherency: ok
CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
Setting up static identity map for 0x60008280 - 0x600082d8
Brought up 1 CPUs
SMP: Total of 1 processors activated (264.19 BogoMIPS).
CPU: All CPU(s) started in SVC mode.
devtmpfs: initialized
VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 0
clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
futex hash table entries: 1024 (order: 4, 65536 bytes)
NET: Registered protocol family 16
DMA: preallocated 256 KiB pool for atomic coherent allocations
cpuidle: using governor ladder
cpuidle: using governor menu
of_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e0000
of_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e1000
of_amba_device_create(): amba_device_add() failed (-19) for /watchdog@100e5000
of_amba_device_create(): amba_device_add() failed (-19) for /smb/motherboard/iofpga@7,00000000/sysctl@01000
of_amba_device_create(): amba_device_add() failed (-19) for /smb/motherboard/iofpga@7,00000000/wdt@0f000
hw-breakpoint: debug architecture 0x4 unsupported.
Serial: AMBA PL011 UART driver
10009000.uart: ttyAMA0 at MMIO 0x10009000 (irq = 35, base_baud = 0) is a PL011 rev1
console [ttyAMA0] enabled
1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = 36, base_baud = 0) is a PL011 rev1
1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = 37, base_baud = 0) is a PL011 rev1
1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = 38, base_baud = 0) is a PL011 rev1
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
Advanced Linux Sound Architecture Driver Initialized.
clocksource: Switched to clocksource arm,sp804
NET: Registered protocol family 2
TCP established hash table entries: 4096 (order: 2, 16384 bytes)
TCP bind hash table entries: 4096 (order: 3, 32768 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
UDP hash table entries: 256 (order: 1, 8192 bytes)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
hw perfevents: enabled with armv7_cortex_a9 PMU driver, 1 counters available
squashfs: version 4.0 (2009/01/31) Phillip Lougher
jffs2: version 2.2. (NAND) © 2001-2006 Red Hat, Inc.
9p: Installing v9fs 9p2000 file system support
io scheduler noop registered (default)
clcd-pl11x 10020000.clcd: PL111 rev2 at 0x10020000
clcd-pl11x 10020000.clcd: /clcd@10020000 hardware, 1024x768@59 display
Console: switching to colour frame buffer device 128x48
clcd-pl11x 1001f000.clcd: PL111 rev2 at 0x1001f000
clcd-pl11x 1001f000.clcd: /smb/motherboard/iofpga@7,00000000/clcd@1f000 hardware, 640x480@59 display
40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
Concatenating MTD devices:
(0): "40000000.flash"
(1): "40000000.flash"
into device "40000000.flash"
libphy: smsc911x-mdio: probed
smsc911x 4e000000.ethernet eth0: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.etherne:01, irq=-1)
smsc911x 4e000000.ethernet eth0: MAC Address: 52:54:00:12:34:56
isp1760 4f000000.usb: bus width: 32, oc: digital
isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
isp1760 4f000000.usb: new USB bus registered, assigned bus number 1
isp1760 4f000000.usb: Scratch test failed.
isp1760 4f000000.usb: can't setup: -19
isp1760 4f000000.usb: USB bus 1 deregistered
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
rtc-pl031 10017000.rtc: rtc core: registered pl031 as rtc0
mmci-pl18x 10005000.mmci: Got CD GPIO
mmci-pl18x 10005000.mmci: Got WP GPIO
mmci-pl18x 10005000.mmci: No vqmmc regulator found
mmci-pl18x 10005000.mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 31,32 (pio)
input: AT Raw Set 2 keyboard as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
ledtrig-cpu: registered to indicate activity on CPUs
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
mmc0: new SD card at address 4567
mmcblk0: mmc0:4567 QEMU! 32.0 MiB
aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
aaci-pl041 10004000.aaci: FIFO 512 entries
oprofile: using arm/armv7-ca9
NET: Registered protocol family 17
9pnet: Installing 9P2000 support
Registering SWP/SWPB emulation handler
rtc-pl031 10017000.rtc: setting system clock to 2022-11-27 13:07:03 UTC (1669554423)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
EXT4-fs (mmcblk0): mounting ext3 file system using the ext4 subsystem
EXT4-fs (mmcblk0): recovery complete
EXT4-fs (mmcblk0): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext3 filesystem) on device 179:0.
Freeing unused kernel memory: 284K (8062b000 - 80672000)
random: nonblocking pool is initialized
-----------------------------------------------------------
                welcome to busybox!!!
-----------------------------------------------------------

smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa12a0000, IRQ: 28

Please press Enter to activate this console. / #
/ # uname -a
Linux echo 4.4.76 #1 SMP Tue Nov 22 02:18:49 PST 2022 armv7l GNU/Linux
/ #

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值