diff mbox series

[v2,2/2] pxe: Get default selection from board type if label matches

Message ID 20200205151244.12204-2-frieder.schrempf@kontron.de
State Superseded
Headers show
Series [v2,1/2] menu: Add a function to set the default by matching the item data | expand

Commit Message

Frieder Schrempf Feb. 5, 2020, 3:12 p.m. UTC
From: Frieder Schrempf <frieder.schrempf at kontron.de>

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().

Signed-off-by: Frieder Schrempf <frieder.schrempf at kontron.de>
---
Changes in v2:
* Don't use internal structs of menu, but instead call
  menu_set_default_by_item_data_match() that does the iteration work.
---
 cmd/pxe_utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

Comments

Simon Glass Feb. 5, 2020, 5:59 p.m. UTC | #1
Hi Schrempf,

On Wed, 5 Feb 2020 at 08:12, Schrempf Frieder
<frieder.schrempf at kontron.de> wrote:
>
> From: Frieder Schrempf <frieder.schrempf at kontron.de>
>
> 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().
>
> Signed-off-by: Frieder Schrempf <frieder.schrempf at kontron.de>
> ---
> Changes in v2:
> * Don't use internal structs of menu, but instead call
>   menu_set_default_by_item_data_match() that does the iteration work.
> ---
>  cmd/pxe_utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 47 insertions(+)
>
> diff --git a/cmd/pxe_utils.c b/cmd/pxe_utils.c
> index 53af04d7dc..62a1ee310d 100644
> --- a/cmd/pxe_utils.c
> +++ b/cmd/pxe_utils.c
> @@ -1220,6 +1220,46 @@ struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
>         return cfg;
>  }
>
> +#ifdef CONFIG_OF_CONTROL
> +int pxe_match_menu_label_with_str(void *data, void *str)
> +{
> +       struct pxe_label *label;
> +
> +       if (!data || !str)
> +               return 0;
> +
> +       label = (struct pxe_label *)data;
> +
> +       if (strcmp(label->name, str) == 0)
> +               return 1;
> +
> +       return 0;
> +}
> +
> +int pxe_runtime_select_menu_default(struct menu *m)
> +{
> +       DECLARE_GLOBAL_DATA_PTR;
> +       const char *model;
> +       char *key;
> +       int ret;
> +
> +       model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
> +
> +       if (!model)
> +               return 0;
> +
> +       ret = menu_set_default_by_item_data_match(m,
> +                       pxe_match_menu_label_with_str, (void *)model, &key);
> +       if (ret)
> +               return ret;
> +
> +       printf("Menu entry %s fits detected board. " \
> +              "Use as default selection...\n", key);
> +
> +       return 0;
> +}
> +#endif
> +
>  /*
>   * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
>   * menu code.
> @@ -1258,6 +1298,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);
> @@ -1269,6 +1311,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)) {

can you do:

   } else if (IS_ENABLED(CONFIG_OF_CONTROL) &&
pxe_runtime_select_menu_default...

?

Then you can drop your #ifdef above.

> +               menu_destroy(m);
> +               return NULL;
> +#endif
>         }
>
>         return m;
> --
> 2.17.1

Regards,
Simon
diff mbox series

Patch

diff --git a/cmd/pxe_utils.c b/cmd/pxe_utils.c
index 53af04d7dc..62a1ee310d 100644
--- a/cmd/pxe_utils.c
+++ b/cmd/pxe_utils.c
@@ -1220,6 +1220,46 @@  struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
 	return cfg;
 }
 
+#ifdef CONFIG_OF_CONTROL
+int pxe_match_menu_label_with_str(void *data, void *str)
+{
+	struct pxe_label *label;
+
+	if (!data || !str)
+		return 0;
+
+	label = (struct pxe_label *)data;
+
+	if (strcmp(label->name, str) == 0)
+		return 1;
+
+	return 0;
+}
+
+int pxe_runtime_select_menu_default(struct menu *m)
+{
+	DECLARE_GLOBAL_DATA_PTR;
+	const char *model;
+	char *key;
+	int ret;
+
+	model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
+
+	if (!model)
+		return 0;
+
+	ret = menu_set_default_by_item_data_match(m,
+			pxe_match_menu_label_with_str, (void *)model, &key);
+	if (ret)
+		return ret;
+
+	printf("Menu entry %s fits detected board. " \
+	       "Use as default selection...\n", key);
+
+	return 0;
+}
+#endif
+
 /*
  * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
  * menu code.
@@ -1258,6 +1298,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);
@@ -1269,6 +1311,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;