用 make menuconfig 图形化配置 uboot

U-Boot图形化配置及其原理

uboot可以通过mx6ull_alientek_emmc_defconfig来配置,或者通过文件mx6ull_alientek_emmc.h来配置;还有一种配置uboot的方法,就是图形化配置

1. U-Boot图形化配置
1.1 图形化配置简介

uboot或Linux内核可以通过输入make menuconfig 来打开图形化配置界面,menuconfig是一套图形化的配置工具,需要ncurses库支持。ncurses库提供零一系列的API函数供调用者生成基于文本的图形界面,因此需要先在Ubuntu中安装ncurses库

sudo apt-get install build-essential
sudo apt-get install libncurses5-dev

menuconfig重点会用到两个文件:“.config”和“Kconfig”,.config文件保存着uboot的配置项,使用menuconfig配置完uboot后该文件会被更新;Kconfig文件是图形界面的描述文件,即描述界面应该有什么内容,很多目录下都会有Kconfig文件

1.2 U-Boot图形化配置体验

在打开图形化配置界面前,需要先对uboot进行一次默认配置。之后使用“make menuconfig”命令打开图形化界面,打开后的界面如下示:
在这里插入图片描述

主界面上方的英文就是简单的操作说明,操作方法如下:

– 通过键盘上的向上和向下按键选择要配置的菜单,“Enter"按键进入
– 选中后按下"Y"键就会将相应的代码编译进uboot中,菜单前面变为<*>
– 选中后按下"N"键就会取消编译相应的代码
– 选中后按下"M"键就会将相应的代码编译为模块,菜单前面变为< M >
– 按两下"Esc"键退出,也就是返回到上一级
– 按下"?“键查看选中菜单的帮助信息
– 按下”/"键打开搜索框,可在搜索框输入要搜索的内容

在配置界面下方有五个按钮,其功能如下:

– < Select >:选中按钮,和enter按键功能相同
– < Exit >:退出按钮,和esc按键功能相同
– < Help >:帮助按钮,查看选中菜单的帮助信息
– < Save >:保存按钮,保存修改后的配置文件
– < Load >:加载按钮,加载指定的配置文件

下面以使能DNS命令为例,介绍如何通过图形化界面来配置uboot

  • 进入"Command line interface"配置项
    在这里插入图片描述

  • 进入"Network commands"网络相关命令配置项
    在这里插入图片描述

  • 选中dns,按下"Y"键将其编译到uboot中

在这里插入图片描述

  • 按两下esc键退出,如果有修改项目,在退出主界面时会提示是否需要保存

保存后可在uboot源码中的".config"文件中发现多了"CONFIG_CMD_DNS=y"这一行

在这里插入图片描述

  • 使用make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j16命令编译uboot

注意:此时不能用脚本来编译,因为该脚本文件在编译之前会清理工程,会删除掉.config文件,导致通过图形化界面配置的所有选项都被删除

  • 编译完成烧写到SD卡后,重启开发板进入uboot命令模式,设置dns服务器的IP地址
setenv dnsip 114.114.114.114
saveenv
  • 设置好后,使用dns命令即可查看百度官网的IP地址
dns www.baidu.com

在这里插入图片描述

2. menuconfig图形化配置原理
2.1 make menuconfig过程分析

当输入make menuconfig以后会匹配到顶层Makefile中的如下代码:

%config: scripts_basic outputmakefile FORCE
	$(Q)$(MAKE) $(build)=scripts/kconfig $@
#其中build=-f ./scripts/Makefile.build obj
###将上面第二行的规则展开后:
@make -f ./scripts/Makefile.build obj=scripts/kconfig menuconfig

Makefile.build会读取scripts/kconfig/Makefile中的内容,在
scripts/kconfig/Makefile中有如下代码:

menuconfig: $(obj)/mconf
	$< $(silent) $(Kconfig)
#silent是这是静默编译的
###展开后###
menuconfig: scripts/kconfig/mconf
	scripts/kconfig/mconfKconfig

目标menuconfig依赖scripts/kconfig/mconf,因此scripts/kconfig/mconf.c文件会被编译,生成mconf可执行文件;目标menuconfig对应的规则为scripts/kconfig/mconfKconfig,也就是说mconf会调用uboot根目录下的Kconfig文件开始构建图形配置界面

2.2 Kconfig语法简介

对于Kconfig语法不需要太深入的去研究,了解其原理即可。打开uboot根目录下的顶层Kconfig,以这个文件为例来简单学习一下Kconfig语法

  • mainmenu:主菜单
##########顶层Kconfig代码段##########
mainmenu "U-Boot $UBOOTVERSION Configuration"
  • source命令调用其他目录下的Kconfig文件
##########顶层Kconfig代码段##########
source "arch/Kconfig"
......
source "common/Kconfig"
source "cmd/Kconfig"
source "dts/Kconfig"
source "net/Kconfig"
source "drivers/Kconfig"
source "fs/Kconfig"
source "lib/Kconfig"
source "test/Kconfig"

##以上子目录下的Kconfig文件在主菜单中生成各自的菜单项
  • menu/endmenu条目:menu用于生成菜单,endmenu菜单结束标志
##########顶层Kconfig代码段##########
menu "General setup"

config LOCALVERSION
	string "Local version - append to U-Boot release"
	help
	  Append an extra string to the end of your U-Boot version.
	  This will show up on your boot log, for example.
	  The string you set here will be appended after the contents of
	  any files with a filename matching localversion* in your
	  object and source tree, in that order.  Your total string can
	  be a maximum of 64 characters.
......
......
endmenu		# General setup

menu "Boot images"

config SUPPORT_SPL
	bool
......
......
endmenu		# Boot images

以上代码中有两个menu/endmenu代码块,这两个代码块就是两个子菜单

在这里插入图片描述

  • config条目:是菜单里的具体配置项
##########顶层Kconfig代码段##########
menu "General setup"

config LOCALVERSION
	string "Local version - append to U-Boot release"
	help			
	......
config LOCALVERSION_AUTO
	bool "Automatically append version information to the version string"
	default y		#表示该配置项默认值是y,即默认被选中
	help			#表示帮助信息,告知配置项的含义,按下h或?会弹出help的内容
	......
config CC_OPTIMIZE_FOR_SIZE
	bool "Optimize for size"
	default y
	help
	......
config SYS_MALLOC_F
	bool "Enable malloc() pool before relocation"
	default y if DM
	help
	......
config SYS_MALLOC_F_LEN
	hex "Size of malloc() pool before relocation"
	depends on SYS_MALLOC_F
	default 0x400
	help
	......
menuconfig EXPERT
	bool "Configure standard U-Boot features (expert users)"
	default y
	help
	......
if EXPERT
	config SYS_MALLOC_CLEAR_ON_INIT
	bool "Init with zeros the memory reserved for malloc (slow)"
	default y
	help
	......
endif
endmenu		# General setup

以上代码可以看出,在menu/endmenu代码块中有大量的"config XXXXX"代码块(config条目)。假如使能了XXXXX功能,那么就会在.config文件中生成CONFIG_XXXXX
常用的三种变量类型:bool、tristate和string
– bool,有两种值,y和n
– tristate,有三种值,y、n和m
– string,用来存储本地字符串

在这里插入图片描述

  • depends on和select
########## arch/Kconfig代码段 ##########
config SYS_GENERIC_BOARD
	bool
	depends on HAVE_GENERIC_BOARD
#depends on依赖:依赖项选中后,被依赖项才能被选中
choice
	prompt "Architecture select"
	default SANDBOX

config ARC
	bool "ARC architecture"
	select HAVE_PRIVATE_LIBGCC
	select HAVE_GENERIC_BOARD
	select SYS_GENERIC_BOARD
	select SUPPORT_OF_CONTROL
#select方向依赖,“ARC”被选择后,四个select也会被选中
  • choice/endchoice:定义一组可选择项,将多个类似配置项组合在一起,供用户单选或多选
########## arch/Kconfig代码段 ##########
choice
	prompt "Architecture select"
	default SANDBOX

config ARC
	bool "ARC architecture"
	......
config ARM
	bool "ARM architecture"
	......
config AVR32
	bool "AVR32 architecture"
	......
config BLACKFIN
	bool "Blackfin architecture"
	......
config M68K
	bool "M68000 architecture"
	......
config MICROBLAZE
	bool "MicroBlaze architecture"
	......
config MIPS
	bool "MIPS architecture"
	......
config NDS32
	bool "NDS32 architecture"
	......
config NIOS2
	bool "Nios II architecture"
	......
config OPENRISC
	bool "OpenRISC architecture"

config PPC
	bool "PowerPC architecture"
	......
config SANDBOX
	bool "Sandbox"
	......
config SH
	bool "SuperH architecture"
	select HAVE_PRIVATE_LIBGCC

config SPARC
	bool "SPARC architecture"
	......
config X86
	bool "x86 architecture"
	......
endchoice

在这里插入图片描述

  • menuconfig:和menu类似,但是menuconfig是带选项的菜单,其一般用法如下
menuconfig MODULES		#定义一个可选的菜单MODULES
	bool "菜单"
if MODULES				#只有选中了,if里面的内容才会显示
......
endif # MODULES
##########顶层Kconfig代码段##########
menu "General setup"
......
menuconfig EXPERT
	bool "Configure standard U-Boot features (expert users)"
	default y
	help
	......

if EXPERT
	config SYS_MALLOC_CLEAR_ON_INIT
	bool "Init with zeros the memory reserved for malloc (slow)"
	default y
	help
	......
endif
endmenu		# General setup

以上代码实现了一个带选项的菜单EXPERT,只有被选中了,if/endif里的内容才会显示出来

在这里插入图片描述
在这里插入图片描述

  • comment:用于在图形化界面中显示一行注释
########## drviers/mtd/nand/Kconfig代码段##########
config NAND_ARASAN
	bool "Configure Arasan Nand"
	help
	  This enables Nand driver support for Arasan nand flash
	  controller. This uses the hardware ECC for read and
	  write operations.

comment "Generic NAND options"   #标注了一行注释

在这里插入图片描述

3. 添加自定义菜单

图形化配置工具的主要工作就是在.config文件里生成前缀为“CONFIG_”变量,这些变量一般都有值(y/m/n),在uboot源码里会根据这些变量来决定编译哪个文件

下面介绍如何添加一个自已的自定义菜单,自定义菜单要求:

– 在主界面中添加名为“My test menu”菜单项,菜单内部有一个配置项
– 配置项为“MY_TESTCONFIG”,处于菜单“My test menu”中
– 配置项的变量类型为bool,默认值为y
– 配置项菜单名字为“This is my test config”
– 配置项的帮助内容为“This is a empty config, just for testing!”

完成以上菜单要求,只需要在顶层Kconfig文件末尾加上如下代码即可

menu "My test menu"

config MY_TESTCONFIG
	bool "This is my test config"
	default y
	help
		This is a empty config,just for test!

endmenu		#my test menu

添加完成后,打开图形化配置界面
在这里插入图片描述
可见主菜单最后面出现零一个名为“My test menu”的子菜单,进入子菜单如下图示
在这里插入图片描述
按下help按键打开帮助文档,如下图示
在这里插入图片描述
打开.config文件,可以发现“CONFIG_MY_TESTCONFIG=y”,如下图示,至此在主菜单中添加自定义菜单的功能就实现了

在这里插入图片描述

  • 7
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安迪西嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值