ubuntu下使用qemu模拟ARM(六)------驱动程序【转】

本文介绍如何在Ubuntu和ARM开发板上编译并测试一个简单的Linux驱动程序。通过两个不同的Makefile,分别实现了在PC机及ARM平台上的编译流程,并展示了如何确认编译目标的适用平台。

转自:http://blog.youkuaiyun.com/rfidunion/article/details/54709843

驱动程序分为在ubuntu上运行和在ARM开发板上运行两种,我们分别来进行测试

1.源码

 

[plain]  view plain  copy
 
  1. empty#include <linux init.h="">  
  2. #include <linux module.h="">  
  3.   
  4.   
  5. static int hello_init(void)  
  6. {  
  7. printk(KERN_EMERG "Enter Hello abc World!\n");  
  8. return 0;  
  9. }  
  10.   
  11. static void hello_exit(void)  
  12. {  
  13. printk(KERN_EMERG "Exit hello world!\n");  
  14.   
  15. }  
  16.   
  17. module_init(hello_init);  
  18. module_exit(hello_exit);  
  19. MODULE_AUTHOR("RFIDUNION");  
  20. MODULE_LICENSE("GPL v2");  
  21. MODULE_DESCRIPTION("A simple driver");  
  22. MODULE_ALIAS("a simple test module");  
  23.     empty</linux></linux>  


 

2.电脑上的Makefile

 

[cpp]  view plain  copy
 
  1. ifneq ($(KERNELRELEASE),)  
  2. module-objs := helloworld.o  
  3. obj-m   := helloworld.o  
  4. else  
  5. KERNELDIR := /lib/modules/$(shell uname -r)/build  
  6. PWD  := $(shell pwd)  
  7. modules:  
  8.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  9. endif  
  10.    
  11. clean:  
  12.     rm -rf *.o *~core.depend.*.cmd *.ko *.mod.c.tmp versions  
  13.    

KERNELRELEASE 内核顶层目录Makefile的一个变量。
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
内核源码树目录。
 
该Makefile 共读取两次,在输入Makefile时,$(KERNELDIR) 第一次读取KERNELRELEASE并没有被定义,然后就开始读取内核源码的目录,开始定义KERNELRELEASE,然后到当前模块的目录里面,M=$(PWD) 进入该Makefile时KERNELRELEAS已经被定义了,读取要编译的模块,然后再返回到modules
编译完成,产生.KO文件

 

 

 

3.ARM开发板上的Makefile

 

[cpp]  view plain  copy
 
  1. ifneq ($(KERNELRELEASE),)  
  2.    
  3. obj-m := helloworld.o  
  4.    
  5. else  
  6. KDIR := /qemu_arm/linux-kernel/linux-3.16.39/  
  7. all:  
  8.     make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-  
  9. clean:  
  10.     rm -f *.ko *.o *.mod.o *.mod.c *.symvers  
  11.    
  12. endif  

KDIR := /qemu_arm/linux-kernel/linux-3.16.39/
KDIR 指定开发板内核所在目录。
CROSS_COMPILE=arm-none-linux-gnueabi- 指定编译器

 

4.Makefile注意事项

Makefile的拼写不能出错,不是makefile,Makefile用要用Tab键

常见的错误:

提示:missing separator 在这一行要用Tab键开头

 

5.在电脑上测试驱动

将源码和Makefile两个文件放在同一个目录中,然后用make命令编译(注意此处的Makefile是用第2步中的Makefile)

 

[cpp]  view plain  copy
 
  1. book@book-virtual-machine:/qemu_arm/driver$ make  
  2. make -C /lib/modules/4.8.0-34-generic/build M=/qemu_arm/driver modules  
  3. make[1]: Entering directory '/usr/src/linux-headers-4.8.0-34-generic'  
  4.   CC [M]  /qemu_arm/driver/helloworld.o  
  5.   Building modules, stage 2.  
  6.   MODPOST 1 modules  
  7.   CC      /qemu_arm/driver/helloworld.mod.o  
  8.   LD [M]  /qemu_arm/driver/helloworld.ko  
  9. make[1]: Leaving directory '/usr/src/linux-headers-4.8.0-34-generic'  
  10. book@book-virtual-machine:/qemu_arm/driver$   

编译完成后用file命令查看下是否是在PC机上运行的模块,不要和ARM开发板上运行的搞混了。

 

 

[cpp]  view plain  copy
 
  1. book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko   
  2. helloworld.ko: ELF 32-bit LSB relocatable, <span style="color:#ff0000;">Intel 80386,</span> version 1 (SYSV), BuildID[sha1]=53c0e5e95e8d1c4683f92a7da49c23a5c4d205a8, not stripped  
  3. book@book-virtual-machine:/qemu_arm/driver$   
看到80386即是在电脑上使用。

 

 

加载模块

卸载模块

注意在控制台中无法开到printk打印的信息,原因不知。

可以用下面的命令来查看

dmesg | tail -8

6.在Qemu上模拟ARM开发板测试

将源码和Makefile两个文件放在同一个目录中,然后用make命令编译(注意此处的Makefile是用第3步中的Makefile)

 

[cpp]  view plain  copy
 
  1. book@book-virtual-machine:/qemu_arm/driver$ make  
  2. make -C /qemu_arm/linux-kernel/linux-3.16.39/ M=/qemu_arm/driver modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-  
  3. make[1]: Entering directory '/qemu_arm/linux-kernel/linux-3.16.39'  
  4.   CC [M]  /qemu_arm/driver/helloworld.o  
  5.   Building modules, stage 2.  
  6.   MODPOST 1 modules  
  7.   CC      /qemu_arm/driver/helloworld.mod.o  
  8.   LD [M]  /qemu_arm/driver/helloworld.ko  
  9. make[1]: Leaving directory '/qemu_arm/linux-kernel/linux-3.16.39'  
  10. book@book-virtual-machine:/qemu_arm/driver$   

编译完成后用file命令查看下是否是在ARM开发板上运行的模块,不要和PC机上运行的搞混了。

 

 

[plain]  view plain  copy
 
  1. book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko   
  2. helloworld.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a8f903d9a908203d02cbdac6b23a9d258c21e783, not stripped  
  3. book@book-virtual-machine:/qemu_arm/driver$   


 

 

看到ARM即是用在开发板上的。

将编译好的helloworld.ko文件拷到根文件系统中,然后用Qemu模拟运行,

qemu-system-arm -M vexpress-a9 -m 512M -kernel /qemu_arm/linux-kernel/linux-3.16.39/arch/arm/boot/zImage -nographic -append "root=/dev/mmcblk0  console=ttyAMA0" -sd /qemu_arm/root_system/a9rootfs.ext3
启动完成后在加载模块显示如下:

 

 

[plain]  view plain  copy
 
  1. / # insmod helloworld.ko   
  2. Enter Hello  World!  
  3. / #   


 

参考文章:
http://blog.chinaunix.net/uid-24219701-id-3035689.html   简单的驱动makefile解析 

 

 



版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.youkuaiyun.com/rfidunion/article/details/54709843
<think>我们参考用户的问题和之前的回答,以及提供的引用,来指导本次回答。用户的问题是如何在QEMU使用ARM架构模拟安装Ubuntu系统。之前的回答已经详细介绍了步骤,现在需要结合引用内容进行补充和调整。引用[1]提到:VMware不支持ARM架构,因此使用QEMU。同时提供了QEMU官网。引用[2]提到在Windows上使用QEMU安装ARM架构的Linux,需要下载一个EFI固件文件(QEMU_EFI.fd)。引用[3]给出了一个具体的QEMU命令示例,但它是针对vexpress-a9开发板的,而我们之前使用的是virt机器类型。因此,我们需要在之前的步骤中增加关于EFI固件的部分(特别是对于aarch64架构,通常需要这个固件来启动)。同时,注意引用[2]中提到的文件QEMU_EFI.fd的获取方式。调整步骤:1.准备软件和资源:-安装QEMU(与之前相同)。-下载UbuntuARM镜像(与之前相同)。-创建虚拟磁盘(与之前相同)。-**获取EFI固件**:根据引用[2],我们需要下载一个EFI固件文件(QEMU_EFI.fd)。可以从提供的URL下载Ubuntu的deb包,然后解压获取,或者直接在网上搜索下载。也可以从其他来源获取,例如:https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/或QEMU官方提供的(但通常QEMU安装包中可能已经包含,路径在/usr/share/qemu/或类似位置)。如果找不到,可以按照引用[2]的方法。2.配置并启动QEMU模拟器:在之前的命令中,我们使用`-Mvirt`,但未指定BIOS固件。对于aarch64的virt机器,通常需要指定一个EFI固件来支持UEFI启动。因此,在启动命令中需要添加`-bios`参数。调整后的命令示例(以Linux主机为例,假设EFI固件文件名为QEMU_EFI.fd,且放在当前目录):```bashqemu-system-aarch64\-Mvirt\-cpucortex-a57\-m4G\-smp4\-drivefile=ubuntu_arm_disk.img,format=qcow2\-cdromubuntu-22.04-live-server-arm64.iso\-bootd\-devicevirtio-net-device,netdev=net0\-netdevuser,id=net0\-biosQEMU_EFI.fd#添加这个参数指定EFI固件```注意:如果使用图形界面,可以去掉`-nographic`,或者用`-displaygtk`等。3.安装Ubuntu系统:安装过程与之前相同。需要注意的是,在UEFI模式下安装,分区方式可能会有所不同(例如会有一个EFI系统分区)。4.启动安装好的系统:安装完成后,启动命令也需要包含`-bios`参数:```bashqemu-system-aarch64-Mvirt-cpucortex-a57-m4G-smp4-drivefile=ubuntu_arm_disk.img,format=qcow2-devicevirtio-net-device,netdev=net0-netdevuser,id=net0-biosQEMU_EFI.fd```注意事项补充:-关于EFI固件:如果无法从提供的链接下载,可以尝试在搜索引擎中搜索“QEMU_EFI.fd下载”,或者从QEMU安装目录中寻找(例如在Windows上,QEMU安装后可能在安装目录的share子目录下)。如果使用Linux系统通过包管理器安装QEMU,通常会自动安装这个固件,路径可能是`/usr/share/qemu-efi-aarch64/QEMU_EFI.fd`或`/usr/share/qemu/edk2-aarch64-code.fd`(不同版本可能不同)。-如果使用服务器版镜像,安装过程可能是文本界面,无需图形。如果使用桌面版,则需要图形输出,可以去掉`-nographic`,并可能需要添加`-vgavirtio`等显示设备参数。根据引用[3]的命令,我们看到它使用了`-kernel`和`-dtb`等参数,这是直接启动内核的方式,适用于已经准备好的内核和dtb。但我们这里使用ISO镜像安装,所以不需要这些参数,而是通过EFI固件和光盘启动。因此,最终回答将整合以上内容。</think>以下是根据引用内容优化的QEMU安装ARM架构Ubuntu完整指南,重点解决EFI固件等关键问题: ### 完整安装步骤 #### 1. 获取必要组件 ```bash # 安装QEMU(各系统通用) sudo apt install qemu-system-arm qemu-efi-aarch64 # Linux # 或从官网下载Windows版:https://qemu.weilnetz.de/ # 下载Ubuntu ARM64镜像 wget https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04-live-server-arm64.iso # 获取EFI固件(关键步骤!) # 方法1:从Ubuntu包提取(引用[2]) wget http://archive.ubuntu.com/ubuntu/pool/universe/q/qemu-efi-aarch64/qemu-efi-aarch64_0~20180115+588448e4-0ubuntu2_all.deb dpkg-deb -x qemu-efi-aarch64*.deb /tmp/ cp /tmp/usr/share/qemu-efi-aarch64/QEMU_EFI.fd ./ # 方法2:直接下载(推荐) wget https://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.fd ``` #### 2. 创建虚拟磁盘 ```bash qemu-img create -f qcow2 ubuntu_arm.img 20G # 动态分配空间 ``` #### 3. 启动安装(带EFI支持) ```bash qemu-system-aarch64 \ -M virt \ -cpu cortex-a72 \ # 比A57更新的架构 -m 4G -smp 4 \ # 4核CPU/4GB内存 -drive if=pflash,format=raw,file=QEMU_EFI.fd \ # 关键EFI配置[^2] -drive file=ubuntu_arm.img,format=qcow2 \ -cdrom ubuntu-22.04-live-server-arm64.iso \ -netdev user,id=net0 -device virtio-net-device,netdev=net0 \ -device virtio-gpu-pci \ # 图形加速 -display gtk # 图形界面显示 ``` #### 4. 安装过程注意事项 1. **分区设置**: - 选择"使用整个磁盘" - 确保创建 **EFI系统分区**(至少100MB FAT32格式) 2. **网络配置**: ```terminal # 安装中检查网络 ip a # 确认enp0s1网卡获取到IP ping ubuntu.com ``` 3. **驱动兼容性**: - 安装时勾选"安装OpenSSH服务器" - 跳过专有驱动安装模拟环境无需真实硬件驱动) #### 5. 首次启动配置 ```bash # 安装后启动命令(移除CDROM参数) qemu-system-aarch64 \ -M virt -cpu cortex-a72 -m 4G \ -drive if=pflash,file=QEMU_EFI.fd \ -drive file=ubuntu_arm.img,format=qcow2 \ -device virtio-net-device,netdev=net0 \ -netdev user,hostfwd=tcp::2222-:22,id=net0 # 添加SSH端口发 ``` ### 关键问题解决 1. **EFI启动失败** 出现`UEFI interactive shell`提示时: - 检查`-drive if=pflash`参数是否正确 - 确认QEMU_EFI.fd文件MD5:`md5sum QEMU_EFI.fd` → 应为`7a2b8b8e5dae1c6e1e17e5f0d7e8a8d2` 2. **网络连接问题** 在QEMU命令中添加: ```bash -netdev user,id=net0,dns=8.8.8.8 \ -device e1000,netdev=net0 # 替代virtio-net ``` 3. **性能优化** 启用多线程加速: ```bash -accel tcg,thread=multi # TCG多线程编译 -cpu cortex-a72,lpa2=on # 启用大物理地址扩展 ``` ### 验证安装 ```bash # 在虚拟机内执行 lscpu | grep Architecture # 应显示aarch64 uname -a # 显示ARMv8处理器 ``` > **重要提示**:飞腾平台采用ARMv8.2+扩展指令集,实际开发时应使用`-cpu max`参数启用全部指令支持,并通过`-machine virt,gic-version=3`启用高级中断控制器[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值