menu: Make some parts of the menu available to other components
PATCH 1/2 menu: Make some parts of the menu available to other components
From: Frieder Schrempf <[email protected]>
In order to iterate over the menu entries and match for a specific name in the pxe boot, we need to properly export the needed structs and functions.
--- common/menu.c | 31 +------------------------------ include/menu.h | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 31 deletions(-)
common/menu.c +1 -30
@@ -12,36 +12,7 @@#include "menu.h" -/* -* Internally, each item in a menu is represented by a struct menu_item. -* -* These items will be alloc'd and initialized by menu_item_add and destroyed -* by menu_item_destroy, and the consumer of the interface never sees that -* this struct is used at all. -*/ -struct menu_item { -char *key; -void *data; -struct list_head list; -}; -/* -* The menu is composed of a list of items along with settings and callbacks -* provided by the user. An incomplete definition of this struct is available -* in menu.h, but the full definition is here to prevent consumers from -* relying on its contents. -*/ -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; -};/* * An iterator function for menu items. callback will be called for each item
@@ -51,7 +22,7 @@ struct menu {* used for search type operations. It is also safe for callback to remove the * item from the list of items. */ -static inline void *menu_items_iter (struct menu *m, +inline void *menu_items_iter (struct menu *m,void *(*callback)(struct menu *,struct menu_item *,void *),void *extra) {
include/menu.h +33 -1
@@ -6,8 +6,40 @@#ifndef __MENU_H__#define __MENU_H__ -struct menu ; +/* +* Internally, each item in a menu is represented by a struct menu_item. +* +* These items will be alloc'd and initialized by menu_item_add and destroyed +* by menu_item_destroy, and the consumer of the interface never sees that +* this struct is used at all. +*/ +struct menu_item { +char *key; +void *data; +struct list_head list; +}; + +/* +* The menu is composed of a list of items along with settings and callbacks +* provided by the user. An incomplete definition of this struct is available +* in menu.h, but the full definition is here to prevent consumers from +* relying on its contents. +*/ +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; +}; +void *menu_items_iter (struct menu *m, +void *(*callback)(struct menu *,struct menu_item *,void *), +void *extra); struct menu *menu_create(char *title, int timeout, int prompt, void (*item_data_print)(void *), char *(*item_choice)(void *),
Signature · 1 line
2.17.1
PATCH 2/2 pxe: Get default selection from board type if label matches
From: Frieder Schrempf <[email protected]>
In order to auto-select an option from the pxe boot menu, that matches the detected board, we check the board model string in the devicetree and set the default menu selection, if it matches the label of the menu entry and there is no default selection already set.
This is useful in combination with SPL that loads a FIT image with U-Boot and multiple DTBs. SPL can detect the board and choose the matching configuration in the FIT by using board_fit_config_name_match().
--- cmd/pxe_utils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
cmd/pxe_utils.c +62 -0
@@ -1219,6 +1219,61 @@ struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)return cfg; } +#ifdef CONFIG_OF_CONTROL +/* +* Check if an item's name matches a provided string, pointed to by extra. +* +* This is called via menu_items_iter, so it returns a pointer to the item if +* the name matches, and returns NULL otherwise. +*/ +static inline void *pxe_item_name_match (struct menu *m,struct menu_item *item, +void *extra) +{ +char *name = extra; +struct pxe_label *label; + +if (!name || !item->key) +return NULL ; + + label = (struct pxe_label *)item->data; + +if (strcmp (label->name, name) ==0 ) +return item; + +return NULL ; +} + +/* +* Find the first item with a name matching the given name, if any exists. +*/ +inline struct menu_item *menu_item_by_pxe_name (struct menu *m,char *name) +{ +return menu_items_iter (m, pxe_item_name_match, name); +} + +int pxe_runtime_select_menu_default (struct menu *m) +{ +DECLARE_GLOBAL_DATA_PTR ; +struct menu_item *item =NULL ; +const char *model; + + model =fdt_getprop (gd->fdt_blob,0 ,"model" ,NULL ); + +if (!model) +return 0 ; + + item =menu_item_by_pxe_name (m, model); + +if (item) { +printf ("Menu entry %s fits detected board. " \ +"Use as default selection...\n" , item->key); + m->default_item = item; + } + +return 0 ; +} +#endif +/* * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic * menu code.
@@ -1257,6 +1312,8 @@ static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)/* * After we've created items for each label in the menu, set the * menu's default label if one was specified. +* If OF_CONTROL is enabled and we don't have a default specified, +* we try to use an entry that matches the board/model name as default. */ if (default_num) { err =menu_default_set (m, default_num);
@@ -1268,6 +1325,11 @@ static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)printf ("Missing default: %s\n" , cfg->default_label); } +#ifdef CONFIG_OF_CONTROL + }else if (pxe_runtime_select_menu_default (m)) { +menu_destroy (m); +return NULL ; +#endif }return m;
Signature · 1 line
2.17.1
PATCH 2/2 pxe: Get default selection from board type if label matches
Quoted 8 lines · Schrempf Frieder
From: Frieder Schrempf <[email protected]>
In order to auto-select an option from the pxe boot menu, that matches the detected board, we check the board model string in the devicetree and set the default menu selection, if it matches the label of the menu entry and there is no default selection already set.
Quoted 6 lines
This is useful in combination with SPL that loads a FIT image with U-Boot and multiple DTBs. SPL can detect the board and choose the matching configuration in the FIT by using
board_fit_config_name_match().Signed-off-by Frieder Schrempf <[email protected]>
I forgot to fix some compiler warnings in this code, so this is probably more like a RFC until I send a fixed v2.
Quoted 92 lines
--- cmd/pxe_utils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)cmd/pxe_utils.c +62
-0@@ -1219,6 +1219,61 @@ struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)return cfg; } +#ifdef CONFIG_OF_CONTROL +/* +* Check if an item's name matches a provided string, pointed to by extra. +* +* This is called via menu_items_iter, so it returns a pointer to the item if +* the name matches, and returns NULL otherwise. +*/ +static inline void *pxe_item_name_match (struct menu *m,struct menu_item *item, +void *extra) +{ +char *name = extra; +struct pxe_label *label; + +if (!name || !item->key) +return NULL ; + + label = (struct pxe_label *)item->data; + +if (strcmp (label->name, name) ==0 ) +return item; + +return NULL ; +} + +/* +* Find the first item with a name matching the given name, if any exists. +*/ +inline struct menu_item *menu_item_by_pxe_name (struct menu *m,char *name) +{ +return menu_items_iter (m, pxe_item_name_match, name); +} + +int pxe_runtime_select_menu_default (struct menu *m) +{ +DECLARE_GLOBAL_DATA_PTR ; +struct menu_item *item =NULL ; +const char *model; + + model =fdt_getprop (gd->fdt_blob,0 ,"model" ,NULL ); + +if (!model) +return 0 ; + + item =menu_item_by_pxe_name (m, model); + +if (item) { +printf ("Menu entry %s fits detected board. " \ +"Use as default selection...\n" , item->key); + m->default_item = item; + } + +return 0 ; +} +#endif +/* * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic * menu code. @@ -1257,6 +1312,8 @@ static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)/* * After we've created items for each label in the menu, set the * menu's default label if one was specified. +* If OF_CONTROL is enabled and we don't have a default specified, +* we try to use an entry that matches the board/model name as default. */ if (default_num) { err =menu_default_set (m, default_num);@@ -1268,6 +1325,11 @@ static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)printf ("Missing default: %s\n" , cfg->default_label); } +#ifdef CONFIG_OF_CONTROL + }else if (pxe_runtime_select_menu_default (m)) { +menu_destroy (m); +return NULL ; +#endif }return m;
PATCH 1/2 menu: Make some parts of the menu available to other components
In reply to Schrempf FriederHi Schrempf,
Quoted 80 lines · Schrempf Frieder
From: Frieder Schrempf <[email protected]>
In order to iterate over the menu entries and match for a specific name in the pxe boot, we need to properly export the needed structs and functions.
Signed-off-by Frieder Schrempf <[email protected]>--- common/menu.c | 31 +------------------------------ include/menu.h | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 31 deletions(-)common/menu.c +1
-30@@ -12,36 +12,7 @@#include "menu.h" -/* -* Internally, each item in a menu is represented by a struct menu_item. -* -* These items will be alloc'd and initialized by menu_item_add and destroyed -* by menu_item_destroy, and the consumer of the interface never sees that -* this struct is used at all. -*/ -struct menu_item { -char *key; -void *data; -struct list_head list; -}; -/* -* The menu is composed of a list of items along with settings and callbacks -* provided by the user. An incomplete definition of this struct is available -* in menu.h, but the full definition is here to prevent consumers from -* relying on its contents. -*/ -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; -};/* * An iterator function for menu items. callback will be called for each item @@ -51,7 +22,7 @@ struct menu {* used for search type operations. It is also safe for callback to remove the * item from the list of items. */ -static inline void *menu_items_iter (struct menu *m, +inline void *menu_items_iter (struct menu *m,void *(*callback)(struct menu *,struct menu_item *,void *),void *extra) {include/menu.h +8
-1@@ -6,8 +6,40 @@#ifndef __MENU_H__#define __MENU_H__ -struct menu ; +/* +* Internally, each item in a menu is represented by a struct menu_item. +* +* These items will be alloc'd and initialized by menu_item_add and destroyed +* by menu_item_destroy, and the consumer of the interface never sees that +* this struct is used at all. +*/ +struct menu_item {
include/menu.h line 26
Quoted 10 lines
include/menu.h +10
-0+char *key; +void *data; +struct list_head list; +}; + +/* +* The menu is composed of a list of items along with settings and callbacks +* provided by the user. An incomplete definition of this struct is available +* in menu.h, but the full definition is here to prevent consumers from +* relying on its contents.
This comment doesn't seem to be true anymore.
Is it possible to add a function to get the info you need from the menu?
Quoted 20 lines
include/menu.h +15
-1+*/ +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; +}; +void *menu_items_iter (struct menu *m, +void *(*callback)(struct menu *,struct menu_item *,void *), +void *extra); struct menu *menu_create(char *title, int timeout, int prompt, void (*item_data_print)(void *), char *(*item_choice)(void *), --
Quoted 1 line
2.17.1
Regards,
Simon
PATCH 1/2 menu: Make some parts of the menu available to other components
Hi Simon,
Quoted 88 lines · Simon Glass
Hi Schrempf,
Quoted 84 lines · Schrempf Frieder
From: Frieder Schrempf <[email protected]>
In order to iterate over the menu entries and match for a specific name in the pxe boot, we need to properly export the needed structs and functions.
Signed-off-by Frieder Schrempf <[email protected]>--- common/menu.c | 31 +------------------------------ include/menu.h | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 31 deletions(-)common/menu.c +1
-30@@ -12,36 +12,7 @@#include "menu.h" -/* -* Internally, each item in a menu is represented by a struct menu_item. -* -* These items will be alloc'd and initialized by menu_item_add and destroyed -* by menu_item_destroy, and the consumer of the interface never sees that -* this struct is used at all. -*/ -struct menu_item { -char *key; -void *data; -struct list_head list; -}; -/* -* The menu is composed of a list of items along with settings and callbacks -* provided by the user. An incomplete definition of this struct is available -* in menu.h, but the full definition is here to prevent consumers from -* relying on its contents. -*/ -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; -};/* * An iterator function for menu items. callback will be called for each item @@ -51,7 +22,7 @@ struct menu {* used for search type operations. It is also safe for callback to remove the * item from the list of items. */ -static inline void *menu_items_iter (struct menu *m, +inline void *menu_items_iter (struct menu *m,void *(*callback)(struct menu *,struct menu_item *,void *),void *extra) {include/menu.h +12
-1@@ -6,8 +6,40 @@#ifndef __MENU_H__#define __MENU_H__ -struct menu ; +/* +* Internally, each item in a menu is represented by a struct menu_item. +* +* These items will be alloc'd and initialized by menu_item_add and destroyed +* by menu_item_destroy, and the consumer of the interface never sees that +* this struct is used at all. +*/ +struct menu_item { +char *key; +void *data; +struct list_head list; +};
Quoted 8 lines
include/menu.h line 26
Quoted 6 lines
include/menu.h +6
-0+ +/* +* The menu is composed of a list of items along with settings and callbacks +* provided by the user. An incomplete definition of this struct is available +* in menu.h, but the full definition is here to prevent consumers from +* relying on its contents. This comment doesn't seem to be true anymore.
Right.
Quoted 2 lines
Is it possible to add a function to get the info you need from the menu?
Unfortunately I didn't find a better solution, that doesn't expose the menu structures. The problem is that menu and pxe are not separated cleanly as the menu items contain references to struct pxe_label.
If I would add a function to iterate over the items to the menu code, I would need to make struct pxe_label known to the menu code, which doesn't seem nice, either.
Please let me know if you propose a different approach.
Thanks,
Frieder
Quoted 26 lines
Quoted 21 lines
include/menu.h +15
-1+*/ +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; +}; +void *menu_items_iter (struct menu *m, +void *(*callback)(struct menu *,struct menu_item *,void *), +void *extra); struct menu *menu_create(char *title, int timeout, int prompt, void (*item_data_print)(void *), char *(*item_choice)(void *), --Quoted 1 line
2.17.1
Regards,
Simon
PATCH 1/2 menu: Make some parts of the menu available to other components
Quoted 113 lines · Frieder Schrempf
Hi Simon,
Quoted 101 lines · Simon Glass
Hi Schrempf,
Quoted 97 lines · Schrempf Frieder
From: Frieder Schrempf <[email protected]>
In order to iterate over the menu entries and match for a specific name in the pxe boot, we need to properly export the needed structs and functions.
Signed-off-by Frieder Schrempf <[email protected]>---
common/menu.c | 31 +------------------------------include/menu.h| 34 +++++++++++++++++++++++++++++++++-2 files changed, 34 insertions(+), 31 deletions(-)common/menu.c +0
-2@@ -12,36 +12,7 @@ #include "menu.h" -/* -* Internally, each item in a menu is represented by a struct
menu_item.
- *
- * These items will be alloc'd and initialized bymenu_item_addand destroyed
- * bymenu_item_destroy, and the consumer of the interface never sees that
- * this struct is used at all.
- */common/menu.c +0
-7-struct menu_item { - char *key; - void *data; - struct list_head list; -}; -/* -* The menu is composed of a list of items along with settings and callbacks
- * provided by the user. An incomplete definition of this struct is available
- * inmenu.h, but the full definition is here to prevent consumers from
- * relying on its contents.
- */common/menu.c +0
-11-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; -};/*
* An iterator function for menu items. callback will be called for each itemcommon/menu.c +0
-0@@ -51,7 +22,7 @@ struct menu { * used for search type operations. It is also safe for callback toremove the
* item from the list of items.
*/common/menu.c +1
-1-static inline void *menu_items_iter(struct menu *m, +inline void *menu_items_iter(struct menu *m,void *(*callback)(struct menu *, struct
menu_item*, void *),
void *extra)
{include/menu.h +2
-1@@ -6,8 +6,40 @@ #ifndef __MENU_H__ #define __MENU_H__ -struct menu ; +/* +* Internally, each item in a menu is represented by a struct
menu_item.
+ *
+ * These items will be alloc'd and initialized bymenu_item_addand destroyed
+ * bymenu_item_destroy, and the consumer of the interface never sees that
+ * this struct is used at all.
+ */include/menu.h +7
-0+struct menu_item { + char *key; + void *data; + struct list_head list; +}; + +/* Quoted 8 lines
Quoted 6 lines
+ * The menu is composed of a list of items along with settings and callbacks
+ * provided by the user. An incomplete definition of this struct is available
+ * inmenu.h, but the full definition is here to prevent consumers from
+ * relying on its contents.This comment doesn't seem to be true anymore.
Quoted 8 lines
Right.
Quoted 2 lines
Is it possible to add a function to get the info you need from the menu?
Unfortunately I didn't find a better solution, that doesn't expose the menu structures. The problem is that menu and pxe are not separated cleanly as the menu items contain references to struct
pxe_label.
Actually, on second thought, if I don't use the existing iterator function in the menu code, I could keep all the menu code in menu.c and expose just one additional function.
I'll send a v2 with this approach.
Quoted 37 lines
If I would add a function to iterate over the items to the menu code, I would need to make struct
pxe_labelknown to the menu code, which doesn't seem nice, either.Please let me know if you propose a different approach.
Thanks,
FriederQuoted 27 lines
Quoted 16 lines
+ */
include/menu.h +13
-0+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; +}; +void *menu_items_iter(struct menu *m, + void *(*callback)(struct menu *, struct menu_item *,Quoted 7 lines
void *),
+ void *extra);
struct menu *menu_create(char *title, int timeout, int prompt, void (*item_data_print)(void *), char *(*item_choice)(void *),Signature · 1 line
2.17.1Regards,
Simon