[Concept,12/32] boot: pxe: Add pxe_menu_init() function

Message ID 20260109231151.4056804-13-sjg@u-boot.org
State New
Headers
Series boot: pxe: Refactor into separate load/setup phases |

Commit Message

Simon Glass Jan. 9, 2026, 11:11 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Extract the pxe_menu allocation and initialisation from parse_pxefile()
into a new pxe_menu_init() function. This provides symmetry with
pxe_menu_uninit() and allows callers to create a menu structure without
going through the full parsing flow.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 boot/pxe_utils.c    | 20 +++++++++++++++-----
 include/pxe_utils.h | 11 +++++++++--
 2 files changed, 24 insertions(+), 7 deletions(-)
  

Patch

diff --git a/boot/pxe_utils.c b/boot/pxe_utils.c
index a824b1b5623..e0b7faddbd0 100644
--- a/boot/pxe_utils.c
+++ b/boot/pxe_utils.c
@@ -900,6 +900,20 @@  static int label_boot(struct pxe_context *ctx, struct pxe_label *label)
 	return 1;
 }
 
+struct pxe_menu *pxe_menu_init(void)
+{
+	struct pxe_menu *cfg;
+
+	cfg = malloc(sizeof(struct pxe_menu));
+	if (!cfg)
+		return NULL;
+
+	memset(cfg, '\0', sizeof(struct pxe_menu));
+	INIT_LIST_HEAD(&cfg->labels);
+
+	return cfg;
+}
+
 void pxe_menu_uninit(struct pxe_menu *cfg)
 {
 	struct list_head *pos, *n;
@@ -924,14 +938,10 @@  struct pxe_menu *parse_pxefile(struct pxe_context *ctx, unsigned long menucfg)
 	char *buf;
 	int r;
 
-	cfg = malloc(sizeof(struct pxe_menu));
+	cfg = pxe_menu_init();
 	if (!cfg)
 		return NULL;
 
-	memset(cfg, 0, sizeof(struct pxe_menu));
-
-	INIT_LIST_HEAD(&cfg->labels);
-
 	buf = map_sysmem(menucfg, 0);
 	r = parse_pxefile_top(ctx, buf, menucfg, cfg, 1);
 
diff --git a/include/pxe_utils.h b/include/pxe_utils.h
index 0de2f49aab5..0ea250300d6 100644
--- a/include/pxe_utils.h
+++ b/include/pxe_utils.h
@@ -179,11 +179,18 @@  struct pxe_context {
 };
 
 /**
- * pxe_menu_uninit() - Destroy an allocated pxe structure
+ * pxe_menu_init() - Allocate and initialise a pxe_menu structure
+ *
+ * Return: Allocated structure, or NULL on failure
+ */
+struct pxe_menu *pxe_menu_init(void);
+
+/**
+ * pxe_menu_uninit() - Free a pxe_menu structure
  *
  * Free the memory used by a pxe_menu and its labels
  *
- * @cfg: Config to destroy, previous returned from parse_pxefile()
+ * @cfg: Config to free, previously returned from pxe_menu_init()
  */
 void pxe_menu_uninit(struct pxe_menu *cfg);