qemu运行uboot,在仿真中动态加载内核与文件系统

在主机搭建tftp服务器(把kernel放到tftp上,通过uboot引导)

  1. 安装必要的一下依赖软件
    sudo apt-get install tftp-hpa tftpd-hpa xinetd
  2. 查看/etc/xinetd.conf,是否如下(没有创建)
# Simple configuration file for xinetd   
#   
# Some defaults, and include /etc/xinetd.d/   
defaults   
{   
# Please note that you need a log_type line to be able to use log_on_success ont-size: 12pt; "> # log_type = SYSLOG daemon info   
}   
includedir /etc/xinetd.d  
  1. 配置tftp服务器如下
    sudo vi /etc/default/tftpd-hpa
# /etc/default/tftpd-hpa 
    TFTP_USERNAME="tftp"
    TFTP_DIRECTORY="/tftpboot"                          
#这是你tftp服务器的工作目录,自行修改,注意,在新建工作目录时,最好修改其权限为777,命令sudo chmod 777 /tftpboot
    TFTP_ADDRESS="0.0.0.0:69"
    TFTP_OPTIONS="-l -c -s" 
  1. 配置xinet(没有创建),其中server_args一行是配置服务器的文件存放的位置,就是进行tftp传输的时候,都是从该文件夹中搜索文件的。
    sudo vi /etc/xinetd.d/tftp
service tftp
    {
       socket_type = dgram
       wait = yes
       disable = no
       user = root
       protocol = udp
       server = /usr/sbin/in.tftpd
       server_args = -s /tftpboot
       #log_on_success += PID HOST DURATION
       #log_on_failure += HOST
       per_source = 11
       cps =100 2
       flags =IPv4
    }
  1. 建立tftp文件夹系统
    sudo mkdir /tftpboot
    sudo chmod 777 /tftpboot
  2. 重启tftp
    sudo service tftpd-hpa restart
    sudo /etc/init.d/xinetd reload
    sudo /etc/init.d/xinetd restart
  3. 本地测试tftp
    sudo tftp localhost
    如果进入tftp命令符,说明本地没有问题,远程需要注意防火墙之类的

5. uboot通过tftp加载uimage

  1. 修改uboot代码并编译
include/configs/vexpress_common.h   加入相应的宏定义
 #define CONFIG_IPADDR   10.8.6.152
 #define CONFIG_NETMASK  255.255.254.0
 #define CONFIG_SERVERIP 10.8.6.142
修改启动文件为uImage
#define CONFIG_BOOTFILE                "uImage"
修改启动命令为
 #define CONFIG_BOOTCOMMAND  "tftp 0x60003000 uImage; setenv bootargs'root=/dev/mmcblk0 console=ttyAMA0'; bootm 0x60003000"

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4

  1. kernel编译(LOADADDR和uboot的启动加载位置一致)
    make uImage LOADADDR=0x60003000 –j4

  2. 进行仿真(把uImage放到tftp目录下)
    sudo qemu-system-arm -M vexpress-a9 -m 256 -kernel u-boot -nographic -net nic,macaddr=00:16:3e:00:00:01 -net tap

  3. 出现下图类似情况,表示成功

    qemu运行uboot,在仿真中动态加载内核与文件系统

    这里卡在Staring Kernel,因为没有dtb

6. uboot通过tftp加载uimage,并加入dtb,使内核开始运行

  1. 进入内核目录并编译生成dtb
    make -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs

  2. 拷贝生成的dtb文件到tftp下
    cp arch/arm/boot/dts/vexpress-v2p-ca9.dtb ../tftpboot/dtb

  3. 进入uboot所在目录并使用qemu进行仿真
    sudo qemu-system-arm -M vexpress-a9 -m 512 -kernel u-boot -nographic -net nic,macaddr=00:16:3e:00:00:01 -net tap

  4. 分别通过下面命令加载UImage和dtb,出现如下界面表示加载成功

    tftp 0x60003000 uImage
    tftp 0x60500000 dtb

    qemu运行uboot,在仿真中动态加载内核与文件系统

  5. 使用下面命令启动uboot并进行传参
    bootm 0x60003000 - 0x60500000

  6. 成功出现如下界面,但是还是不能加载文件系统,后续加入

    qemu运行uboot,在仿真中动态加载内核与文件系统

    成功,但是不能加载文件系统

  7. 不能挂载文件系统的原因是因为没有设置启动命令,可以通过如下命令加载
    sudo qemu-system-arm -M vexpress-a9 -m 512 -kernel u-boot -nographic -net nic,macaddr=00:16:3e:00:00:01 -net tap -sd a9rootfs.ext3

  8. 进入uboot后输入下面命令

setenv bootargs 'root=/dev/mmcblk0 console=ttyAMA0'
tftp 0x60003000 uImage
tftp 0x60500000 dtb
bootm 0x60003000 - 0x60500000
  1. 出现如下界面表示成功

    qemu运行uboot,在仿真中动态加载内核与文件系统

7. 网络配置

lubuntu中需要加上sudo apt install ifupdown来开启ifupdowm,否则直接修改interface无效

sudo apt-get install uml-utilities bridge-utils

关于新的网络脚本,修改 /etc/network/interfaces 文件,最后需要重启计算机,使新的 /etc/network/interfaces 配置文件生效

auto lo
iface lo inet loopback

# The eth0 network interface(s)
# auto eth0
# iface eth0 inet dhcp

# The bridge network interface(s)
auto br0
iface br0 inet dhcp
# iface br0 inet static
# address 192.168.0.1
# netmask 255.255.255.0
# gateway 192.168.0.254
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

# The tap0 network interface(s)
auto tap0
iface tap0 inet manual
# iface tap0 inet static
# address 192.168.0.2
# netmask 255.255.255.0
# gateway 192.168.0.254
pre-up tunctl -t tap0 -u root
pre-up ifconfig tap0 0.0.0.0 promisc up
post-up brctl addif br0 tap0

使用qemu-ifup脚本,权限755 sudo chmod 755 qemu-ifup

#!/bin/sh
echo "net up"
switch=br0
ifconfig $1 up
#ip link set $1 up
brctl addif ${switch} $1

使用qemu-ifdown脚本,权限755 sudo chmod 755 qemu-ifdown

#!/bin/sh
echo "Close tap!"
switch=br0
brctl delif ${switch} $1
ifconfig $1 down
#ip link set $1 down
#tunctl -d $1

使用命令启动uboot,用ping测试

setenv ipaddr 10.8.30.33; setenv serverip 10.8.30.39;  tftp 0x60003000  uImage; tftp 0x60500000 dtb; setenv bootargs \"root=/dev/nfs rw nfsroot=10.8.30.39:/home/vencol/code/nfs ip=10.8.30.33  console=ttyAMA0\"; bootm 0x60003000 - 0x60500000
sudo qemu-system-arm -M vexpress-a9 -m 256 -kernel u-boot -nographic -net nic -net tap,ifname=tap0,script=./qemu-ifup,downscript=./qemu-ifdown

9. uboot通过tftp加载uImage和dtb,加载文件系统

  1. 修改vi output/build/uboot-2018.09/include/configs/vexpress_common.h,其中bootargs 是引导内核的参数
#define CONFIG_IPADDR   10.8.30.33
#define CONFIG_NETMASK  255.255.255.0
#define CONFIG_SERVERIP 10.8.30.39
#define CONFIG_BOOTFILE                "uImage"
#define CONFIG_BOOTCOMMAND  "tftp 0x60003000 uImage; tftp 0x60500000 dtb; setenv bootargs \"root=/dev/mmcblk0 consol    e=ttyAMA0\"; bootm 0x60003000 - 0x60500000"
 20
  1. 修改后编译
  2. 使用qemu仿真,加载加载uImage和dtb,加载文件系统
sudo qemu-system-arm -M vexpress-a9 -m 256 -kernel u-boot
 -nographic -net nic -net tap,ifname=tap0,script=./qemu-ifup,downscript=./qemu-ifdown -sd rootfs.ext2

qemu运行uboot,在仿真中动态加载内核与文件系统

im

上一篇:2021-2022-1 20211420《信息安全专业导论》小学四则运算编程实践


下一篇:qemu虚拟机的关机方式