linux驱动学习(三)之uboot与内核编译

需要板子一起学习的可以这里购买(含资料):点击跳转

GEC6818内核源码下载:点击跳转

一、环境配置         

        由于GEC6818对应是64位系统,虚拟机中的linux系统也要是64位,比如:ubuntu16.04.rar
   
   但是,ubuntu16.04.rar系统,对虚拟机有要求:
   打开ubuntu16.04.vmx查看对虚拟机的要求:
   virtualHW.version = "12" ----- VM为12
   如果说电脑上的VM低于12,不要重新安装VM,
   virtualHW.version = "10"

二、源码拷贝

        把6818GEC.tar.gz拷贝到linux系统下,不能采用文件共享来在windows下编译uboot和内核

第一步:拷贝源码到linux的家目录下

第二步:解压源码

sudo tar zxvf 6818GEC.tar.gz

第三步:修改源码的权限

chmod 777 6818GEC

第四步:查看源码的目录内容

$ ls -l
total 40
drwxrwxr-x 15 gec gec 4096 Feb 15  2017 buildroot
drwxrwxr-x 20 gec gec 4096 Jul 17  2017 GEC6818uboot ---->开发板Uboot的源码
drwxrwxr-x 25 gec gec 4096 Jul 17  2017 kernel ---> 开发板内核的源码
drwxrwxr-x  3 gec gec 4096 Jul 18  2016 linux
-rwxrwxr-x  1 gec gec 6019 Jul 17  2017 mk ---> 脚本
drwxrwxr-x  5 gec gec 4096 Dec 21  2016 out ----> UBOOT和内核的输出
drwxrwxr-x  3 gec gec 4096 Jul 18  2016 prebuilts ---> 编译uboot与内核使用到的工具链
drwxrwxr-x  3 gec gec 4096 Jul 18  2016 prototype
drwxrwxr-x  3 gec gec 4096 Feb 14  2017 tools

三、mk脚本分析

1) 关于路径变量
    BS_DIR_TOP=$(cd `dirname $0` ; pwd) ----> gec@ubuntu:~/6818GEC$ ./mk 1 2 3 1  ./mk 相当于 $0
                              $() ----->提取内容,并且把内容赋值给变量BS_DIR_TOP
                                 gec@ubuntu:~/6818GEC$ pwd
                                /home/gec/6818GEC
                                BS_DIR_TOP=/home/gec/6818GEC
    BS_DIR_RELEASE=${BS_DIR_TOP}/out/release
    BS_DIR_TARGET=${BS_DIR_TOP}/out/target/product/GEC6818/
    BS_DIR_UBOOT=${BS_DIR_TOP}/GEC6818uboot  ----> uboot的路径
    BS_DIR_KERNEL=${BS_DIR_TOP}/kernel ----> 内核的路径
    BS_DIR_BUILDROOT=${BS_DIR_TOP}/buildroot
    
    以上都是变量进行赋值

2)关于路径变量
  BS_CROSS_TOOLCHAIN_BOOTLOADER=${BS_DIR_TOP}/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
  BS_CROSS_TOOLCHAIN_KERNEL=${BS_DIR_TOP}/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
  以上都是工具逻的路径

3) Target Config
    BS_CONFIG_BOOTLOADER_UBOOT=GEC6818_config  ---->编译uboot要使用到的配置文件
    BS_CONFIG_KERNEL=GEC6818_defconfig ---> 编译kernel要使用到的配置文件
        gec@ubuntu:~/6818GEC/kernel/arch/arm/configs$ ls
        GEC6818_defconfig 
    BS_CONFIG_FILESYSTEM=PRODUCT-GEC6818-userdebug
    BS_CONFIT_BUILDROOT=GEC6818_defconfig

4)编译各个模块的功能函数
    setup_environment()
    build_bootloader_uboot()
    build_kernel()
    
  5) 普通变量
    threads=4
    uboot=no ----> 默认情况下为no
    kernel=no
    system=no
    buildroot=no
  6) 分析shell语句
   if [ -z $1 ]; then
    uboot=yes
    kernel=yes
    system=yes
    buildroot=yes
  fi

7) case语句
   while [ "$1" ]; do
    case "$1" in
    -j=*)
        x=$1
        threads=${x#-j=}
        ;;
    -u|--uboot)
        uboot=yes
        ;;
    -k|--kernel)
        kernel=yes
        ;;
    -s|--system)
        system=yes
        ;;
    -b|--buildroot)
        buildroot=yes
        ;;
    -a|--all)
        uboot=yes
        kernel=yes
        system=yes
        buildroot=yes
        ;;
    如果$1 的值为 -u 或者 --uboot,则执行
       uboot=yes

8)  根据变量的值的状态调用相应的函数
   if [ "${uboot}" = yes ]; then
    build_bootloader_uboot || exit 1
    fi

    if [ "${kernel}" = yes ]; then
        build_kernel || exit 1
    fi
    
9)分析相关的函数
      build_bootloader_uboot
      
      build_kernel()
    {
        export PATH=${BS_DIR_UBOOT}/tools:$PATH  ---> 配置PATH
        # Compiler kernel
        cd ${BS_DIR_KERNEL} || return 1 ---> 进入到内核目录
        make ${BS_CONFIG_KERNEL} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} || return 1-->
        等 价于 make GEC6818_defconfig ARCH=arm CROSS_COMPILE=/home/gec/6818GEC/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
        make -j${threads} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} || return 1
        make -j${threads} ARCH=arm CROSS_COMPILE=${BS_CROSS_TOOLCHAIN_KERNEL} uImage || return 1 --->
        生成内核的镜像:uImage

        # Copy uImage to release directory
        #cp -v ${BS_DIR_KERNEL}/arch/arm/boot/uImage ${BS_DIR_RELEASE}

        #echo "^_^ kernel path: ${BS_DIR_RELEASE}/uImage"

        # generate boot.img
        cd ${BS_DIR_TOP} || return 1
        echo 'boot.img ->' ${BS_DIR_RELEASE}
        # Make boot.img with ext4 format, 64MB
        cp -v ${BS_DIR_KERNEL}/arch/arm/boot/uImage ${BS_DIR_TARGET}/boot --->
        把uImage拷贝到${BS_DIR_TARGET}/boot目录下
        
        mkuserimg.sh -s ${BS_DIR_TARGET}/boot ${BS_DIR_TARGET}/boot.img ext4 boot 67108864 --->
        通过mkuserimg.sh脚本中的命令,把${BS_DIR_TARGET}/boot目录的uImage文件,制作成
        ${BS_DIR_TARGET}/目录 下boot.img镜像文件

        cp -av ${BS_DIR_TARGET}/boot.img ${BS_DIR_RELEASE} || return 1; --->
        把编译好的boot.img拷贝到 ${BS_DIR_RELEASE}目录下
        return 0
    }
    
    综上分析得出:
    编译uboot:./mk -u
    编译内核:./mk -k 

四、使用mk脚本来编译内核

~/6818GEC$ ls

buildroot  GEC6818uboot  kernel  linux  mk  out  prebuilts  prototype  tools

~/6818GEC$ ./mk -k 

 内核编译输出的结果:
   

Kernel: arch/arm/boot/Image is ready ---> 内核Image
   Kernel: arch/arm/boot/zImage is ready ---> 内核zImage
   UIMAGE  arch/arm/boot/uImage ---->内核的最终镜像
   gec@ubuntu:~/6818GEC/kernel/arch/arm/boot$ ls
  bootp  compressed  dts  Image  install.sh  Makefile  uImage  zImage

    Image Name:   Linux-3.4.39-gec
    Created:      Sun Feb 17 19:14:47 2019
    Image Type:   ARM Linux Kernel Image (uncompressed)
    Data Size:    5532568 Bytes = 5402.90 kB = 5.28 MB
    Load Address: 40008000
    Entry Point:  40008000
      Image arch/arm/boot/uImage is ready
    boot.img -> /home/gec/6818GEC/out/release
    '/home/gec/6818GEC/kernel/arch/arm/boot/uImage' -> '/home/gec/6818GEC/out/target/product/GEC6818//boot/uImage'
    make_ext4fs -s -T -1 -l 67108864 -a boot /home/gec/6818GEC/out/target/product/GEC6818//boot.img /home/gec/6818GEC/out/target/product/GEC6818//boot
    Creating filesystem with parameters:
        Size: 67108864
        Block size: 4096
        Blocks per group: 32768
        Inodes per group: 4096
        Inode size: 256
        Journal blocks: 1024
        Label: 
        Blocks: 16384
        Block groups: 1
        Reserved block group size: 7
    Created filesystem with 18/4096 inodes and 4212/16384 blocks
    '/home/gec/6818GEC/out/target/product/GEC6818//boot.img' -> '/home/gec/6818GEC/out/release/boot.img'

    最终在6818GEC/out/release/boot.img找到镜像文件boot.img
    
    
注意事项:
       问题一:在首次编译内核时,未先编译uboot,会出现错误
       "mkimage" command not found - U-Boot images will not be built
       解决办法:先编译u-boot,再重新编译内核
       ./mk -u 
       ./mk -k
 

五、烧写内核到开发板

第一步:把编译生成的boot.img单独拷贝,并且与fastboot.exe、adb.exe AdbWinUsbApi.dll,AdbWinApi.dll
  第二步:连接USB线到开发板与电脑
        大端的USB口接电脑
        小端的USB口接开发板
  第三步:启动开发板并且运行在u-boot命令行下,执行fastboot命令   

X6818# fastboot

Fastboot Partitions:
	 mmc.2: ubootpak, img : 0x200, 0x78000
	 mmc.2: 2ndboot, img : 0x200, 0x4000
	 mmc.2: bootloader, img : 0x8000, 0x70000
	 mmc.2: boot, fs : 0x100000, 0x4000000
	 mmc.2: system, fs : 0x4100000, 0x2f200000
	 mmc.2: cache, fs : 0x33300000, 0x1ac00000
	 mmc.2: misc, fs : 0x4e000000, 0x800000
	 mmc.2: recovery, fs : 0x4e900000, 0x1600000
	 mmc.2: userdata, fs : 0x50000000, 0x0
	Support fstype : 2nd  boot  factory  raw  fat  ext4  emmc  nand  ubi  ubifs 
	Reserved part  : partmap  mem  env  cmd 
	DONE: Logo bmp 311 by 300 (3bpp), len=280854 
	DRAW: 0x47000000 -> 0x46000000 
	Load USB Driver: android
	Core usb device tie configuration done
	OTG cable Connected!

在PC端的设备管理器上显示:Android Device

第四步:在PC端的命令行
    ctrl + r ---> cmd ---->弹出终端界面,并且进入到boot.img目录下

第五步:通过fastboot命令上传文件到开发板

fastboot flash boot boot.img

sending 'boot' (16848 KB)... OKAY
writing 'boot'... OKAY

第六步:重启动开发板
X6818# reset

六、使用make命令来编译kernel源码

第一步:进入到内核源码的目录下(~/6818GEC/kernel)

make clean -----> 清理一下项目

第二步:拷贝内核源码编译的配置文件(***_config)

cp arch/arm/configs/GEC6818_defconfig .config

第三步:执行make menuconfig 

make menuconfig ----->配置内核(以菜单来进行配置)

第四步:把执行make menuconfig修改后的.config拷贝回到对应的硬件平台配置目录下

cp .config arch/arm/configs/GEC6818_defconfig 

第五步:执行make 进行源码的编译

make  ARCH=arm CROSS_COMPILE=/home/gec/6818GEC/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-

七、内核源码的结构

Linux 内核源代码包含如下目录。
    ● arch :包含和硬件体系结构相关的代码,每种平台占一个相应的目录,如i386、arm、arm64、powerpc、mips 等。Linux 内核目前已经支持30 种左右的体系结构。在arch目录下,存放的是各个平台以及各个平台的芯片对Linux 内核进程调度、内存管理、中断等的支持,以及每个具体的SoC 和电路板的板级支持代码。
    ● block:块设备驱动程序I/O 调度。
    ● crypto:常用加密和散列算法(如AES、SHA 等),还有一些压缩和CRC 校验算法。
    ● documentation:内核各部分的通用解释和注释。
    ● drivers :设备驱动程序,每个不同的驱动占用一个子目录,如char、block、net、mtd、i2c 等。
    ● fs:所支持的各种文件系统,如EXT、FAT、NTFS、JFFS2 等。
    ● include:头文件,与系统相关的头文件放置在include/linux 子目录下。
    ● init:内核初始化代码。著名的start_kernel() 就位于init/main.c 文件中。
    ● ipc:进程间通信的代码。
    ● kernel :内核最核心的部分,包括进程调度、定时器等,而和平台相关的一部分代码
    放在arch/*/kernel 目录下。
    ● lib:库文件代码。
    ● mm:内存管理代码,和平台相关的一部分代码放在arch/*/mm 目录下。
    ● net:网络相关代码,实现各种常见的网络协议。
    ● scripts:用于配置内核的脚本文件。
    ● security:主要是一个SELinux 的模块。
    ● sound:ALSA、OSS 音频设备的驱动核心代码和常用设备驱动。
    ● usr:实现用于打包和压缩的cpio 等。
    ● include:内核API 级别头文件。
  
   

觉得有帮助的话,打赏一下呗。。

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值