2020-02-06-Linux设备驱动开发0-环境搭建

layouttitlesubtitledateauthorheader-imgcatalogtags
post
Linux设备驱动开发0-环境搭建
学习Linux驱动开发,当然得有一个自己的练兵台了!
2020-02-06
Tupelo Shen
img/post-bg-re-vs-ng2.jpg
true
Linux
Driver

1 环境介绍

主机环境:

Win10 64 + VirtualBox 6.0 + ubuntu16.04(32位)-4.10.0-28-generic

u-boot 版本:

u-boot-2015-04

Linux kernel版本:

linux-4.4.203

busybox版本:

busybox-1.31.1.tar.bz2

交叉编译工具链:

arm-linux-gnueabi-

qemu版本:

QEMU emulator version 4.1.93 (v4.2.0-rc3-dirty)

2 下载Linux内核

linux kernel下载地址 :

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/

方法1: 直接下载某一个版本的内核

wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.203.tar.xz

方法2: 获取最新的内核版本

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

3 安装交叉编译工具

ubuntu安装方法:

sudo apt-get install gcc-arm-linux-gnueabi

4 编译Linux内核

解压下载的内核tarball:

tar -xvf linux-4.4.203.tar.xz

清理编译环境的命令:

make mrproper

生成vexpress开发板的.config文件:

make CROSS_COMPILE=arm-linux-gnueabi- ARCH=arm vexpress_defconfig

个性化配置

make CROSS_COMPILE=arm-linux-gnueabi- ARCH=arm menuconfig

编译内核:

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

5 编译安装QEMU

具体的QEMU安装方法可以参考另一篇文章-Ubuntu-16.04编译Qemu

ERROR: glib-2.48 gthread-2.0 is required to compile QEMU
  1. 执行

     sudo apt-get install libglib2.0-dev
    
  2. 如果报下面的错

     The following packages have unmet dependencies:
      libglib2.0-dev : Depends: libglib2.0-0 (= 2.48.0-1ubuntu4) but 2.48.2-0ubuntu4.1 is to be installed
                       Depends: libglib2.0-bin (= 2.48.0-1ubuntu4) but 2.48.2-0ubuntu4.1 is to be installed
                       Depends: zlib1g-dev but it is not going to be installed
     E: Unable to correct problems, you have held broken packages.
    
  3. 逐个执行下面的代码

     sudo apt-get install libglib2.0-0=2.48.0-1ubuntu4
     sudo apt-get install libglib2.0-bin=2.48.0-1ubuntu4
     sudo apt-get install zlib1g= 1:1.2.8.dfsg-2ubuntu4
    
  4. 然后再执行1

6 测试QEMU和内核

运行下面的命令:

qemu-system-arm -M vexpress-a9 -m 512M -kernel /home/qemu4/qemu/linux/linux-4.4.203/arch/arm/boot/zImage -dtb  /home/qemu4/qemu/linux/linux-4.4.203/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "console=ttyAMA0"

# 请将/home/qemu4/qemu/linux/linux-4.4.203/修改为自己的目录

运行结果:

#0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb/smb:motherboard/smb: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.4.203 #1
Hardware name: ARM-Versatile Express
[<80015d70>] (unwind_backtrace) from [<800127a8>] (show_stack+0x10/0x14)
[<800127a8>] (show_stack) from [<80248be4>] (dump_stack+0x90/0xa4)
[<80248be4>] (dump_stack) from [<800a834c>] (panic+0x9c/0x1f4)
[<800a834c>] (panic) from [<806362c8>] (mount_block_root+0x1c8/0x268)
[<806362c8>] (mount_block_root) from [<8063648c>] (mount_root+0x124/0x12c)
[<8063648c>] (mount_root) from [<806365e4>] (prepare_namespace+0x150/0x198)
[<806365e4>] (prepare_namespace) from [<80635ed0>] (kernel_init_freeable+0x250/0x260)
[<80635ed0>] (kernel_init_freeable) from [<804afafc>] (kernel_init+0x8/0xe8)
[<804afafc>] (kernel_init) from [<8000f2d0>] (ret_from_fork+0x14/0x24)
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

在这儿之所以报Kernel panic,是因为我们现在还没有安装根文件系统,无法启动init进程所致。那么接下来我们就来研究根文件系统。

7 制作根文件系统

本文要制作的根文件系统rootfs=busybox(包含基本的linux命令)+运行库+几个字符设备。

7.1 根文件系统存放位置

其实依赖于每个开发板支持的存储设备,可以放到Nor Flash上,也可以放到SD卡,甚至外部磁盘上。最关键的一点是你要清楚知道开发板有什么存储设备。

本文直接使用SD卡做为存储空间,文件格式为ext3格式

7.2 下载、编译和安装busybox

下载:

wget https://busybox.net/downloads/busybox-1.31.1.tar.bz2

编译安装:

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

安装完成后,会在busybox目录下生成_install目录,该目录下的程序就是单板运行所需要的命令。

7.3 rootfs目录结构

  1. 创建rootfs目录,根文件系统内的文件全部放到其中。

     mkdir -p rootfs/{bin,dev,etc/init.d,lib,proc,sbin,sys,usr,mnt,tmp,var}
    
  2. 拷贝busybox到根目录下

     sudo cp busybox-1.20.2/_install/* -r rootfs/
    
  3. 从工具链中拷贝运行库到lib目录下

     sudo cp -P /usr/arm-linux-gnueabi/lib/* rootfs/lib/
    
  4. 创建4个tty端终设备

     sudo mknod rootfs/dev/tty1      c 4 1
     sudo mknod rootfs/dev/tty2      c 4 2
     sudo mknod rootfs/dev/tty3      c 4 3
     sudo mknod rootfs/dev/tty4      c 4 4
     sudo mknod rootfs/dev/console   c 5 1
     sudo mknod rootfs/dev/null      c 1 3
    
  5. 安装内核模块

     make modules_install ARCH=arm INSTALL_MOD_PATH=../../rootfs  //把.ko文件copy到lib/modules中
    

7.4 制作rootfs文件系统镜像

  1. 生成32M大小的镜像

     dd if=/dev/zero of=a9rootfs.ext3 bs=1M count=32
    
  2. 格式化成ext3文件系统

     mkfs.ext3 a9rootfs.ext3
    
  3. 将文件拷贝到镜像中

     sudo mkdir tmpfs
     sudo mount -t ext3 a9rootfs.ext3 tmpfs/ -o loop
     sudo cp -r rootfs/*  tmpfs/
     sudo umount tmpfs
    

8 系统启动运行

现在是时候运行QEMU,模拟vexpress-A9开发板了,启动命令如下:

qemu-system-arm -M vexpress-a9 -m 512M -kernel /home/qemu4/qemu/linux/linux-4.4.203/arch/arm/boot/zImage -dtb  /home/qemu4/qemu/linux/linux-4.4.203/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "root=/dev/mmcblk0  console=ttyAMA0" -sd a9rootfs.ext3

激动人心的时刻来了:

aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
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 2019-11-29 05:53:09 UTC (1575006789)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
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): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext3 filesystem) readonly on device 179:0.
Freeing unused kernel memory: 276K
random: nonblocking pool is initialized
can't run '/etc/init.d/rcS': No such file or directory

Please press Enter to activate this console.
/ # ls
bin         etc         linuxrc     sbin
dev         lib         lost+found  usr
/ #
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值