Linux内核移植初探

Linux内核移植初探,第1张

概述内核移植的梯度: 初级:根据芯片公司的参考配置,编译开发板内核并了解执行过程 中极:添加内核驱动的方式方法 高级:修改或添加BSP包 linux内核特性: 可移植性强、支持的硬件平台广泛;超强的网络功能;多任务多用户系统;模块化的设计 五大子系统: 进程管理;内存管理;文件系统;网络协议;设备管理 内核获取路径:芯片厂商、内核源码官方 linux内核的目录结构层次结构: 平台相关目录树:arch目

内核移植的梯度:

初级:根据芯片公司的参考配置,编译开发板内核并了解执行过程

中极:添加内核驱动的方式方法

高级:修改或添加BSP包

linux内核特性:

可移植性强、支持的硬件平台广泛;超强的网络功能;多任务多用户系统;模块化的设计

五大子系统:

进程管理;内存管理;文件系统;网络协议;设备管理

内核获取路径:芯片厂商、内核源码官方

linux内核的目录结构层次结构:

平台相关目录树:arch目录下

平台无关目录树:其它

crypto目录:算法、加密涉及的源码目录

documentation目录:内核官方文档

fs目录:文件系统的信息

ipc目录:进程间通信的机制

mm目录:内存

driver:驱动相关

内核源码开发的头文件命名规范

#include <asm/xxx.h>:与cpu体系结构(arch)相关的头文件
#include <linux/xxx.h>:平台无关的头文件
#include <plat/xxx.h>:与某款芯片公司相关的头文件
#include <mach/xxx.h>:与开发板配套的头文件

配置内核

1.配置哪些目录需要编译

2.配置哪些文件需要编译

配置方法:Makefile
主目录Makefile:

 包含体系结构下的Makefile

# Use liNUXINCLUDE when you must reference the include/ directory.# Needed to be compatible with the O= optionliNUXINCLUDE    := -I$(srctree)/arch/$(hdr-arch)/include                    -Iarch/$(hdr-arch)/include/generated -Iinclude                    $(if $(KBUILD_SRC),-I$(srctree)/include)                    -include include/generated/autoconf.h

   hdr-arch := $(SRCARCH)

#包含体系架构下的Makefileinclude $(srctree)/arch/$(SRCARCH)/Makefile
ARCH ?= armCROSS_COMPILE ?= /opt/FrIEndlyARM/toolschain/
4.5.1/bin/arm-linux-# Architecture as present in compile.hUTS_MACHINE := $(ARCH)SRCARCH := $(ARCH)# Additional ARCH settings for x86ifeq ($(ARCH),i386)SRCARCH := x86endififeq ($(ARCH),x86_64)SRCARCH := x86endif

各个子目录的Makefile
例:arch/arm/mach-s5pv210下的Makefile

# arch/arm/mach-s5pv210/Makefile## copyright (c) 2010 Samsung Electronics Co.,Ltd.#         http://www.samsung.com/## licensed under GPLv2obj-y                :=obj-m                :=obj-n                :=obj-                :=# Core support for S5PV210 systemobj-$(CONfig_cpu_S5PV210)    += cpu.o init.o clock.o dma.oobj-$(CONfig_cpu_S5PV210)    += setup-i2c0.oobj-$(CONfig_S5PV210_PM)    += pm.o sleep.oobj-$(CONfig_cpu_FREQ)        += cpufreq.o# machine supportobj-$(CONfig_MACH_AquilA)    += mach-aquila.oobj-$(CONfig_MACH_SMDKV210)    += mach-smdkv210.oobj-$(CONfig_MACH_SMDKC110)    += mach-smdkc110.oobj-$(CONfig_MACH_GONI)        += mach-goni.oobj-$(CONfig_MACH_TORBRECK)    += mach-torbreck.o# device supportobj-y                += dev-audio.oobj-$(CONfig_S3C64XX_DEV_SPI)    += dev-spi.oobj-$(CONfig_S5PV210_SETUP_FB_24BPP)    += setup-fb-24bpp.oobj-$(CONfig_S5PV210_SETUP_FIMC)    += setup-fimc.oobj-$(CONfig_S5PV210_SETUP_I2C1)     += setup-i2c1.oobj-$(CONfig_S5PV210_SETUP_I2C2)     += setup-i2c2.oobj-$(CONfig_S5PV210_SETUP_IDE)        += setup-IDe.oobj-$(CONfig_S5PV210_SETUP_KEYPAD)    += setup-keypad.oobj-$(CONfig_S5PV210_SETUP_SDHCI)       += setup-sdhci.oobj-$(CONfig_S5PV210_SETUP_SDHCI_GPIO)    += setup-sdhci-gpio.o
arch/arm/mach-s5pv210/Makefile

obj - y  :=   编译进内核
obj - m :=   以模块形式编译
obj - n  :=   不编译 
obj -     :=   不编译

 哪些文件需要编译?

  CONfig_XXX 

  配置单:

    在/arch/arm/configs默认目录下
  配置过程:

    1、导出需要的默认配置文件到主目录下并更名为.config
    2、交叉编译器的修改
    3、体系结构(System Type)的选择:S5PV210

ARCH ?= armCROSS_COMPILE ?= /opt/FrIEndlyARM/toolschain/4.5.1/bin/arm-linux-
主Makefile

    4、配置单(.config)增删改查--->使用make menuconfig实现图形化更改配置

                      Kconfig----> make menuconfig ---> .config ---->Makefile

Kconfig语法:
source:相当于include描述一包含关系
menu、endmenu:用来定义菜单如:

menu "System Info"end menu

执行make menuconfig 则:

@H_502_208@

.config中增加一条:CONfig_ABC = y
即config ----> CONfig_ABC =y---->Makefile中的obj-$(CONfig_ABC) += xx.o

通过make menuconfig中的-----相应选项的help----->找到相应选项的Kconfig---->Makefile中对应的文件 .c

  例如:
     找menuconfig下的Samsung SoC serial support定义的(Kconfig)位置:

            法一:选中这个选项键盘单击h按键
            法二:搜索grep -nR "Samsung SoC serial support"

    同样在Kconfig所在目录下的Makefile文件即可找到对应的.c文件                                              

通过Makefile  .o ----->找到Kconfig:同上↑

 

config:用来配置菜单子目录的内容

menu "System Info"config ABC     bool "This is a test config"
   help
     Support test configendmenu

执行make menuconfig 则:

 

Kconfig配置主线

# For a description of the Syntax of this configuration file,# see documentation/kbuild/kconfig-language.txt.#mainmenu "linux/$ARCH $KERNELVERSION Kernel Configuration"config SRCARCH    string    option env="SRCARCH"source "arch/$SRCARCH/Kconfig"
主Kconfig

      ---->source “arch/$SRCARCH/Kconfig”      

config ARM        bool        default y        select HAVE_AOUT        select HAVE_DMA_API_DEBUG        select HAVE_IDE        select HAVE_MEMBLOCK..................config HAVE_PWM        boolconfig MIGHT_HAVE_PCI        boolconfig SYS_SUPPORTS_APM_EMulATION        boolconfig HAVE_SCHED_CLOCK        boolconfig GENERIC_GPIO        bool............................menu "Power management options"source "kernel/power/Kconfig"config ARCH_SUSPEND_POSSIBLE        depends on !ARCH_S5P64X0 && !ARCH_S5PC100        depends on cpu_ARM920T || cpu_ARM926T || cpu_SA1100 ||                 cpu_V6 || cpu_V6K || cpu_V7 || cpu_XSC3 || cpu_XSCALE        def_bool yendmenusource "net/Kconfig"source "drivers/Kconfig"source "fs/Kconfig"source "arch/arm/Kconfig.deBUG"source "security/Kconfig"source "crypto/Kconfig"source "lib/Kconfig"
arch/arm/Kconfig  

        --->source "net/Kconfig"
   --->source "drivers/Kconfig"
   --->source "fs/Kconfig"
   --->source "arch/arm/Kconfig.deBUG"
   --->source "security/Kconfig"
   --->source "crypto/Kconfig"
   --->source "lib/Kconfig"

添加驱动到linux内核中的步骤:以myled.c字符驱动为例
1.可以在/drivers/char/目录下新建文件夹mydriver,然后复制myled.c到mydriver目录
2、在driver目录下创建Makefile文件

obj-$(CONfig_MYLED) += myled.o
Makefile

3、在上层的Makefile文件添加一句obj-y += mydriver/这样上层Makefile文件就会找到mydriver目录下的Makefile文件
4、在driver目录下创建Kconfig文件

menu "My Personal Device Driver"config MYLED        bool "Support myled device driver"    help           Support led driver for S5PV210endmenu

 5、在上层的Kconfig文件添加一句source "driver/char/mydriver/Kconfig"这样上层Kconfig文件就会找到mydriver目录下的Kconfig文件
 6、执行make menuconfig 选中My Personal Device Driver --->Support myled device driver之后执行make uImage 重新编译内核即可

内核编译过程

make :

  make Image     make zImage   make uImage (专为uboot启动准备的内核镜像 )      //编译的是obj-y    

  make modules   //编译的是obj-m

make uImage (vmlinux-->Image-->vmlinux-->zImage-->uImage) 

直接执行make uImage 报错解决办法:
  进入u-boot源码,在编译完成的uboot源码中进入tool目录,找到mkimage文件,将其复制到/bin根目录

vmlinux :OS elf file  ---OBJcopY拷贝生成Image
Image:未压缩,所以比较大
zImage:经过压缩的Image文件 

自定义BSP的过程

总结

以上是内存溢出为你收集整理的Linux内核移植初探全部内容,希望文章能够帮你解决Linux内核移植初探所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1026940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存