[PATCH 31/39] bootmeth: Refactor to put options in a common file

Simon Glass sjg at chromium.org
Tue Nov 19 14:18:36 CET 2024


The fallback feature is implemented for extlinux but not for PXE. Move
the code into common files so we can keep both pieces in sync.

Tidy up the comment for set_property() while we are here, fixing the
return value and some missing hyphens.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 boot/Makefile            |  4 +--
 boot/bootmeth_extlinux.c | 74 --------------------------------------
 boot/ext_pxe_common.c    | 76 ++++++++++++++++++++++++++++++++++++++++
 include/bootmeth.h       |  8 ++---
 include/extlinux.h       | 29 +++++++++++++++
 5 files changed, 111 insertions(+), 80 deletions(-)
 create mode 100644 boot/ext_pxe_common.c

diff --git a/boot/Makefile b/boot/Makefile
index 9ccdc2dc8f4..882cd760027 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -27,8 +27,8 @@ obj-$(CONFIG_$(PHASE_)BOOTSTD) += bootstd-uclass.o
 obj-$(CONFIG_$(PHASE_)BOOTSTD_MENU) += bootflow_menu.o
 obj-$(CONFIG_$(PHASE_)BOOTSTD_PROG) += prog_boot.o
 
-obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX) += bootmeth_extlinux.o
-obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX_PXE) += bootmeth_pxe.o
+obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX) += ext_pxe_common.o bootmeth_extlinux.o
+obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX_PXE) += ext_pxe_common.o bootmeth_pxe.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_EFILOADER) += bootmeth_efi.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_CROS) += bootm.o bootm_os.o bootmeth_cros.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_QFW) += bootmeth_qfw.o
diff --git a/boot/bootmeth_extlinux.c b/boot/bootmeth_extlinux.c
index 2f8e579540b..ce11077ee35 100644
--- a/boot/bootmeth_extlinux.c
+++ b/boot/bootmeth_extlinux.c
@@ -21,48 +21,6 @@
 #include <mmc.h>
 #include <pxe_utils.h>
 
-/**
- * struct extlinux_plat - locate state for this bootmeth
- *
- * @use_fallback: true to boot with the fallback option
- * @info: Info passed to the extlinux_getfile() function
- * @ctx: holds the PXE context, if it should be saved
- */
-struct extlinux_plat {
-	bool use_fallback;
-	struct pxe_context ctx;
-	struct extlinux_info info;
-};
-
-enum extlinux_option_type {
-	EO_FALLBACK,
-	EO_INVALID
-};
-
-struct extlinux_option {
-	char *name;
-	enum extlinux_option_type option;
-};
-
-static const struct extlinux_option options[] = {
-	{"fallback", EO_FALLBACK},
-	{NULL, EO_INVALID}
-};
-
-static enum extlinux_option_type get_option(const char *option)
-{
-	int i = 0;
-
-	while (options[i].name) {
-		if (!strcmp(options[i].name, option))
-			return options[i].option;
-
-		i++;
-	}
-
-	return EO_INVALID;
-};
-
 static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
 {
 	if (IS_ENABLED(CONFIG_SANDBOX)) {
@@ -203,38 +161,6 @@ static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
 	return 0;
 }
 
-static int extlinux_set_property(struct udevice *dev, const char *property, const char *value)
-{
-	struct extlinux_plat *plat;
-	static enum extlinux_option_type option;
-
-	plat = dev_get_plat(dev);
-
-	option = get_option(property);
-	if (option == EO_INVALID) {
-		printf("Invalid option\n");
-		return -EINVAL;
-	}
-
-	switch (option) {
-	case EO_FALLBACK:
-		if (!strcmp(value, "1")) {
-			plat->use_fallback = true;
-		} else if (!strcmp(value, "0")) {
-			plat->use_fallback = false;
-		} else {
-			printf("Unexpected value '%s'\n", value);
-			return -EINVAL;
-		}
-		break;
-	default:
-		printf("Unrecognised property '%s'\n", property);
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
 static int extlinux_bootmeth_bind(struct udevice *dev)
 {
 	struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
diff --git a/boot/ext_pxe_common.c b/boot/ext_pxe_common.c
new file mode 100644
index 00000000000..e42865b84f5
--- /dev/null
+++ b/boot/ext_pxe_common.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Common functions for extlinux and PXE
+ *
+ * Copyright 2024 Collabora
+ * Written by Martyn Welch <martyn.welch at collabora.com>
+ */
+
+#define LOG_CATEGORY UCLASS_BOOTSTD
+
+#include <dm.h>
+#include <extlinux.h>
+#include <mapmem.h>
+#include <linux/string.h>
+
+enum extlinux_option_type {
+	EO_FALLBACK,
+	EO_INVALID
+};
+
+struct extlinux_option {
+	char *name;
+	enum extlinux_option_type option;
+};
+
+static const struct extlinux_option options[] = {
+	{"fallback", EO_FALLBACK},
+	{NULL, EO_INVALID}
+};
+
+static enum extlinux_option_type extlinux_get_option(const char *option)
+{
+	int i = 0;
+
+	while (options[i].name) {
+		if (!strcmp(options[i].name, option))
+			return options[i].option;
+
+		i++;
+	}
+
+	return EO_INVALID;
+};
+
+int extlinux_set_property(struct udevice *dev, const char *property,
+			  const char *value)
+{
+	struct extlinux_plat *plat;
+	static enum extlinux_option_type option;
+
+	plat = dev_get_plat(dev);
+
+	option = extlinux_get_option(property);
+	if (option == EO_INVALID) {
+		printf("Invalid option\n");
+		return -EINVAL;
+	}
+
+	switch (option) {
+	case EO_FALLBACK:
+		if (!strcmp(value, "1")) {
+			plat->use_fallback = true;
+		} else if (!strcmp(value, "0")) {
+			plat->use_fallback = false;
+		} else {
+			printf("Unexpected value '%s'\n", value);
+			return -EINVAL;
+		}
+		break;
+	default:
+		printf("Unrecognised property '%s'\n", property);
+		return -EINVAL;
+	}
+
+	return 0;
+}
diff --git a/include/bootmeth.h b/include/bootmeth.h
index 26de593a9a4..03301c90580 100644
--- a/include/bootmeth.h
+++ b/include/bootmeth.h
@@ -151,15 +151,15 @@ struct bootmeth_ops {
 	/**
 	 * set_property() - set the bootmeth property
 	 *
-	 * This allows the setting of boot method specific properties to enable
-	 * automated finer grain control of the boot process
+	 * This allows the setting of bootmeth-specific properties to enable
+	 * automated finer-grained control of the boot process
 	 *
 	 * @name: String containing the name of the relevant boot method
 	 * @property: String containing the name of the property to set
 	 * @value: String containing the value to be set for the specified
 	 *         property
-	 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is
-	 *      provided, -ENOENT if there are no bootmeth devices
+	 * Return: 0 if OK, -EINVAL if an unknown property or invalid value is
+	 *	provided
 	 */
 	int (*set_property)(struct udevice *dev, const char *property,
 			    const char *value);
diff --git a/include/extlinux.h b/include/extlinux.h
index 6747fe01dda..f97164954cc 100644
--- a/include/extlinux.h
+++ b/include/extlinux.h
@@ -7,6 +7,8 @@
 #ifndef __extlinux_h
 #define __extlinux_h
 
+#include <pxe_utils.h>
+
 #define EXTLINUX_FNAME	"extlinux/extlinux.conf"
 
 /**
@@ -20,4 +22,31 @@ struct extlinux_info {
 	struct bootflow *bflow;
 };
 
+/**
+ * struct extlinux_plat - locate state for this bootmeth
+ *
+ * @use_falllback: true to boot with the fallback option
+ * @ctx: holds the PXE context, if it should be saved
+ * @info: information used for the getfile() method
+ */
+struct extlinux_plat {
+	bool use_fallback;
+	struct pxe_context ctx;
+	struct extlinux_info info;
+};
+
+/**
+ * extlinux_set_property() - set an extlinux property
+ *
+ * This allows the setting of bootmeth-specific properties to enable
+ * automated finer-grained control of the boot process
+ *
+ * @name: String containing the name of the relevant boot method
+ * @property: String containing the name of the property to set
+ * @value: String containing the value to be set for the specified property
+ * Return: 0 if OK, -EINVAL if an unknown property or invalid value is provided
+ */
+int extlinux_set_property(struct udevice *dev, const char *property,
+			  const char *value);
+
 #endif
-- 
2.34.1



More information about the U-Boot mailing list