u-boot-2019 引导菜单分析

configs/ls2k_core.h 中定义了默认的菜单项及其他默认变量

#define CONFIG_EXTRA_ENV_SETTINGS                                       \
        CONSOLE_STDOUT_SETTINGS \
        "loadaddr=" __stringify(CONFIG_SYS_LOAD_ADDR) "\0" \
        "rd_start=0x86000000\0" \
        "rd_size=0x02000000\0" \
        "mtdids=" CONFIG_MTDIDS_DEFAULT "\0"                                    \
        "mtdparts=" CONFIG_MTDPARTS_DEFAULT "\0"                                \
        "splashpos=m,m\0" \
        "panel0=" "hz101wx" "\0" \
        "panel1=" "hz101wx" "\0" \
        "menucmd=bootmenu\0" \
        "bootmenu_0=0. U-Boot boot order=boot\0" \
        "bootmenu_1=1. Boot From SATA=" BOOT_SATA_DEFAULT "\0" \
        "bootmenu_2=2. Boot From SATA CFG=" BOOT_SATA_CFG_DEFAULT "\0" \
        "bootmenu_3=3. Boot From NAND=" BOOT_NAND_DEFAULT "\0" \
        "bootmenu_4=4. Update Kernel(tftp to nand)=loongson_update tftp kernel nand\0" \
        "bootmenu_5=5. Update Kernel(tftp to sata)=loongson_update tftp kernel sata\0" \
        "bootmenu_6=6. Update Rootfs(tftp to nand)=loongson_update tftp rootfs\0" \
        "bootmenu_7=7. Update U-Boot(tftp)=loongson_update tftp uboot\0" \
        "bootmenu_8=8. Update Kernel(usb to nand)=loongson_update usb kernel nand\0" \
        "bootmenu_9=9. Update Kernel(usb to sata)=loongson_update usb kernel sata\0" \
        "bootmenu_10=10. Update Rootfs(usb to nand)=loongson_update usb rootfs\0" \
        "bootmenu_11=11. Update U-Boot(usb)=loongson_update usb uboot\0" \
        "bootmenu_12=12. Update All(usb)=loongson_update usb all nand\0" \
        "bootmenu_13=13. Recover system=recover_cmd\0" \
        "bootmenu_delay=10\0" \

include/env_default.h 中定义了字符串数组 default_environment 其中的定义如下

#ifdef DEFAULT_ENV_INSTANCE_EMBEDDED
env_t embedded_environment __UBOOT_ENV_SECTION__(environment) = {
	ENV_CRC,	/* CRC Sum */
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
	1,		/* Flags: valid */
#endif
	{
#elif defined(DEFAULT_ENV_INSTANCE_STATIC)
static char default_environment[] = {
#else
const uchar default_environment[] = {
#endif
#ifdef	CONFIG_USE_BOOTARGS
	"bootargs="	CONFIG_BOOTARGS			"\0"
#endif
#ifdef	CONFIG_BOOTCOMMAND
	"bootcmd="	CONFIG_BOOTCOMMAND		"\0"
#endif
#ifdef	CONFIG_ENV_VARS_UBOOT_CONFIG
	"arch="		CONFIG_SYS_ARCH			"\0"
#ifdef CONFIG_SYS_CPU
	"cpu="		CONFIG_SYS_CPU			"\0"
#endif
#ifdef CONFIG_SYS_BOARD
	"board="	CONFIG_SYS_BOARD		"\0"
	"board_name="	CONFIG_SYS_BOARD		"\0"
#endif
#ifdef CONFIG_SYS_VENDOR
	"vendor="	CONFIG_SYS_VENDOR		"\0"
#endif
#ifdef CONFIG_SYS_SOC
	"soc="		CONFIG_SYS_SOC			"\0"
#endif
#endif
#if defined(CONFIG_BOOTCOUNT_BOOTLIMIT) && (CONFIG_BOOTCOUNT_BOOTLIMIT > 0)
	"bootlimit="	__stringify(CONFIG_BOOTCOUNT_BOOTLIMIT)"\0"
#endif
#ifdef	CONFIG_EXTRA_ENV_SETTINGS
	CONFIG_EXTRA_ENV_SETTINGS
#endif
	"\0"
#else /* CONFIG_USE_DEFAULT_ENV_FILE */
#include "generated/defaultenv_autogenerated.h"
#endif
#ifdef DEFAULT_ENV_INSTANCE_EMBEDDED
	}
#endif
};

菜单项显示及工作流程

do_bootmenu	cmd/bootmenu.c
	bootmenu_show 
		bootmenu_create					创建所有菜单项,是一个链表结构
			bootmenu_getoption			获取bootmenu_%d 变量,赋值给bootmenu_entry. 上面default_environment中定义了bootmenu_xx 变量
		menu_create							创建菜单
			bootmenu_print_entry			打印菜单项 回调函数
			bootmenu_choice_entry			等待用户选择菜单项  回调函数
				bootmenu_autoboot_loop			提示延时						
				bootmenu_loop					等待用户按键选择	
		menu_item_add						添加前面创建的菜单项加到此菜单中
		menu_default_set					设置默认菜单项
		menu_get_choice						获取用户选择的菜单项,基中包含命令及标题等信息
		
			menu_interactive_choice      common/menu.c
				menu_display				显示菜单项
					menu_display_statusline	打印菜单状态栏“U-Boot Boot Menu” 与 “Press UP/DOWN to move, ENTER to select"”	
				menu_item_by_key			调用bootmenu_print_entry 依次打印每个菜单项

common/menu.c 中定义的menumenu_item

struct menu {
	struct menu_item *default_item;
	int timeout;
	char *title;
	int prompt;
	void (*item_data_print)(void *);
	char *(*item_choice)(void *);
	void *item_choice_data;
	struct list_head items;
	int item_cnt;
};

struct menu_item {
	char *key;
	void *data;
	struct list_head list;
};

cmd/bootmenu.c 中定义中bootmenu_entrybootmenu_data

struct bootmenu_entry {
	unsigned short int num;		/* unique number 0 .. MAX_COUNT */
	char key[3];			/* key identifier of number */
	char *title;			/* title of entry */
	char *command;			/* hush command of entry */
	struct bootmenu_data *menu;	/* this bootmenu */
	struct bootmenu_entry *next;	/* next menu entry (num+1) */
};

struct bootmenu_data {
	int delay;			/* delay for autoboot */
	int active;			/* active menu entry */
	int count;			/* total count of menu entries */
	struct bootmenu_entry *first;	/* first menu entry */
};

整个关联起来之后 menu_item 中的data 对应的就是bootmenu_entrybootmenu_entry 是可以根据需要而自已定义相应的结构体数据。bootmenu_data对应的是menu中的item_choice_data

添加自已的菜单

struct menu *menu_create(char *title, int timeout, int prompt,
				void (*item_data_print)(void *),
				char *(*item_choice)(void *),
				void *item_choice_data);
int menu_default_set(struct menu *m, char *item_key);
int menu_get_choice(struct menu *m, void **choice);
int menu_item_add(struct menu *m, char *item_key, void *item_data);
int menu_destroy(struct menu *m);
void menu_display_statusline(struct menu *m);
int menu_default_choice(struct menu *m, void **choice);

操作菜单相关的接口都在 common/menu.c 中有实现。 要添加菜单只需实现item_data_printitem_choice 回调及item_choice_data 自定义的数据结构。 具体流程可以参考cmd/bootmenu.c中的do_bootmenu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值