Skip to contentSkip to conversation

misc: fs_loader: Add request_firmware_into_buf_via_env API for environment-based firmware loading

PATCH In review Lucien.Jheng 5 participants 6 messages Jul 20, 2025 – Jul 21, 2025 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fs_loader: Add request_firmware_into_buf_via_env API for environment-based firmware loading

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

---
Change log:
- Re-sending as previous version and add more maintainers to review.

 drivers/misc/fs_loader.c | 59 ++++++++++++++++++++++++++++++++++++++++
 include/fs_loader.h      | 18 ++++++++++++
 2 files changed, 77 insertions(+)
drivers/misc/fs_loader.c +59 -0
index 66803f4b997..a4f4448f5cc 100644
@@ -228,6 +228,65 @@ int request_firmware_into_buf(struct udevice *dev,
 	return ret;
 }
 
+/**
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf_via_env(struct udevice *dev,
+				      void *buf, u32 offset,
+				      unsigned int fw_index)
+{
+	char var_name[32];
+	size_t size;
+	int ret;
+
+	for (unsigned int index = 0; index < fw_index; index++) {
+		snprintf(var_name, sizeof(var_name), "fw_dir%u", index);
+		const char *name = env_get(var_name);
+
+		if (!name) {
+			printf("Error: env var %s not set.\n", var_name);
+			goto fail;
+		}
+
+		snprintf(var_name, sizeof(var_name), "fw_size%u", index);
+		size = env_get_hex(var_name, 0);
+		if (!size) {
+			printf("Error: env var %s is invalid or zero.\n", var_name);
+			goto fail;
+		}
+
+		ret = request_firmware_into_buf(dev, name, buf, size, offset);
+		if (ret < 0) {
+			printf("Error: Failed to load firmware %s, size 0x%zx.\n",
+			       name, size);
+			return ret;
+		}
+
+		buf = (void *)((uintptr_t)buf + size);
+	}
+
+	return 0;
+fail:
+	printf("Error: Failed to load firmware via environment variables.\n"
+	       "Set these environment variables for each firmware index:\n"
+	       "  fw_dirN   - path to firmware file (e.g., fw_dir0)\n"
+	       "  fw_sizeN  - size of firmware (e.g., fw_size0)\n"
+	       "Where N is the firmware index (0, 1, ...).\n");
+
+	return -EINVAL;
+}
+
 static int fs_loader_of_to_plat(struct udevice *dev)
 {
 	u32 phandlepart[2];
include/fs_loader.h +18 -0
index 5eb5b7ab4a1..0d130daacfc 100644
@@ -64,4 +64,22 @@ int request_firmware_into_buf(struct udevice *dev,
  * Return: 0 on success, negative value on error
  */
 int get_fs_loader(struct udevice **dev);
+
+/**
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf_via_env(struct udevice *dev,
+				      void *buf, u32 offset,
+				      unsigned int fw_index);
 #endif

--
2.34.1

#1 lore raw

Am 20. Juli 2025 17:04:04 MESZ schrieb "Lucien.Jheng" <[email protected]>:

Quoted 6 lines

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

It is unclear where this function will be used. Is this a part of a larger patch series that you are planning to send?

Quoted 15 lines

---
Change log:
- Re-sending as previous version and add more maintainers to review.

drivers/misc/fs_loader.c | 59 ++++++++++++++++++++++++++++++++++++++++
include/fs_loader.h | 18 ++++++++++++

2 files changed, 77 insertions(+)
drivers/misc/fs_loader.c +0 -0
index 66803f4b997..a4f4448f5cc 100644

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 232

Quoted 5 lines
drivers/misc/fs_loader.c +2 -0
@@ -228,6 +228,65 @@ int request_firmware_into_buf(struct udevice *dev,
 	return ret;
 }
 
+/**
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.

request_firmware_into_buf_via_env() - ...

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 241

Quoted 9 lines
drivers/misc/fs_loader.c +9 -0
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.

Your patch series should comprise documentation of the new variables.

Quoted 30 lines
drivers/misc/fs_loader.c +30 -0
+ *
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf_via_env(struct udevice *dev,
+				      void *buf, u32 offset,
+				      unsigned int fw_index)
+{
+	char var_name[32];
+	size_t size;
+	int ret;
+
+	for (unsigned int index = 0; index < fw_index; index++) {
+		snprintf(var_name, sizeof(var_name), "fw_dir%u", index);
+		const char *name = env_get(var_name);
+
+		if (!name) {
+			printf("Error: env var %s not set.\n", var_name);
+			goto fail;
+		}
+
+		snprintf(var_name, sizeof(var_name), "fw_size%u", index);
+		size = env_get_hex(var_name, 0);
+		if (!size) {
+			printf("Error: env var %s is invalid or zero.\n", var_name);
+			goto fail;
+		}
+
+		ret = request_firmware_into_buf(dev, name, buf, size, offset);
+		if (ret < 0) {
+			printf("Error: Failed to load firmware %s, size 0x%zx.\n",

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 281

Quoted 10 lines
drivers/misc/fs_loader.c +10 -0
+			       name, size);
+			return ret;
+		}
+
+		buf = (void *)((uintptr_t)buf + size);
+	}
+
+	return 0;
+fail:
+	printf("Error: Failed to load firmware via environment variables.\n"

Please use log_err(). (Multiple instances)

Please remove the "Error: " prefix. The word "failed" clearly expresses that an error occurred.

The usage description below should be moved to the command that calls this function.

Best regards

Heinrich

Quoted 41 lines
drivers/misc/fs_loader.c +8 -0
+	       "Set these environment variables for each firmware index:\n"
+	       "  fw_dirN   - path to firmware file (e.g., fw_dir0)\n"
+	       "  fw_sizeN  - size of firmware (e.g., fw_size0)\n"
+	       "Where N is the firmware index (0, 1, ...).\n");
+
+	return -EINVAL;
+}
+
 static int fs_loader_of_to_plat(struct udevice *dev)
 {
 	u32 phandlepart[2];
include/fs_loader.h +18 -1
index 5eb5b7ab4a1..0d130daacfc 100644
@@ -64,4 +64,22 @@ int request_firmware_into_buf(struct udevice *dev,
 * Return: 0 on success, negative value on error
 */
 int get_fs_loader(struct udevice **dev);
+
+/**
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf_via_env(struct udevice *dev,
+				      void *buf, u32 offset,
+				      unsigned int fw_index);
 #endif
--

2.34.1

#2 lore raw
Quoted 9 lines · Heinrich Schuchardt

Am 20. Juli 2025 17:04:04 MESZ schrieb "Lucien.Jheng" <[email protected]>:

Quoted 6 lines

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

It is unclear where this function will be used. Is this a part of a larger patch series that you are planning to send?

I believe this will be used by the Airoha PHY driver.

#3 lore raw
In reply to Lucien.Jheng
Quoted 25 lines · Lucien.Jheng

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

---
Change log:
- Re-sending as previous version and add more maintainers to review.

  drivers/misc/fs_loader.c | 59 ++++++++++++++++++++++++++++++++++++++++
  include/fs_loader.h      | 18 ++++++++++++
  2 files changed, 77 insertions(+)
drivers/misc/fs_loader.c +1 -0
index 66803f4b997..a4f4448f5cc 100644
@@ -228,6 +228,65 @@ int request_firmware_into_buf(struct udevice *dev,
  	return ret;
  }
 
+/**

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 241

Quoted 10 lines
drivers/misc/fs_loader.c +10 -0
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.

To make this really scriptable, this code has to run a script which does the firmware loading and then points the U-Boot code to the buffer with data in it.

Instead of "fw_index", use "char *script_name" or some such. Then, run the script. The script should set up two environment variables, call them e.g. "fw_addr" and "fw_size" or some such . Then, this code should read the two environment variables, validate them, and possibly copy their content into $buf . Finally, the code should always unset fw_addr and fw_size .

Note that request_firmware_into_buf_via_env() should also have 'size' parameter which describes the maximum size of loaded firmware, so the code could verify whether the copy of firmware into $buf would write past the buffer and prevent that.

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 248

Quoted 6 lines
drivers/misc/fs_loader.c +6 -0
+ * Return: Size of total read, negative value when error.
+ */
+int request_firmware_into_buf_via_env(struct udevice *dev,
+				      void *buf, u32 offset,
+				      unsigned int fw_index)
+{

I'm sorry for the slow response.

[...]

#4 lore raw
Quoted 47 lines · Marek Vasut
Quoted 25 lines · Lucien.Jheng

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

---
Change log:
- Re-sending as previous version and add more maintainers to review.

  drivers/misc/fs_loader.c | 59 ++++++++++++++++++++++++++++++++++++++++
  include/fs_loader.h      | 18 ++++++++++++
  2 files changed, 77 insertions(+)
drivers/misc/fs_loader.c +1 -0
index 66803f4b997..a4f4448f5cc 100644
@@ -228,6 +228,65 @@ int request_firmware_into_buf(struct udevice *dev,
  	return ret;
  }
 
+/**

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 241

Quoted 10 lines
drivers/misc/fs_loader.c +10 -0
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.

To make this really scriptable, this code has to run a script which does the firmware loading and then points the U-Boot code to the buffer with data in it.

Instead of "fw_index", use "char *script_name" or some such. Then, run the script. The script should set up two environment variables, call them e.g. "fw_addr" and "fw_size" or some such . Then, this code should read the two environment variables, validate them, and possibly copy their content into $buf . Finally, the code should always unset fw_addr and fw_size .

Quoted 4 lines

Note that request_firmware_into_buf_via_env() should also have 'size' parameter which describes the maximum size of loaded firmware, so the code could verify whether the copy of firmware into $buf would write past the buffer and prevent that.

And for v4, can we please put this patch and the patch using it in the same series? I see there's some feedback right now from Frank on the other patch about how maybe this part should be implemented.

Signature · 1 line
Tom
#5 lore raw

Am 21. Juli 2025 17:33:48 MESZ schrieb Tom Rini <[email protected]>:

Quoted 48 lines · Marek Vasut
Quoted 47 lines · Lucien.Jheng
Quoted 25 lines

This commit adds request_firmware_into_buf_via_env() to the fs_loader driver, enabling firmware loading into a buffer
using environment variables for path and size.
It supports multiple entries via indexed variable names
(e.g., "fw_dir0", "fw_size0").
This provides flexible, scriptable firmware loading without hardcoded details.

---
Change log:
- Re-sending as previous version and add more maintainers to review.

  drivers/misc/fs_loader.c | 59 ++++++++++++++++++++++++++++++++++++++++
  include/fs_loader.h      | 18 ++++++++++++
  2 files changed, 77 insertions(+)
drivers/misc/fs_loader.c +1 -0
index 66803f4b997..a4f4448f5cc 100644
@@ -228,6 +228,65 @@ int request_firmware_into_buf(struct udevice *dev,
  	return ret;
  }
 
+/**

drivers/misc/fs_loader.c int request_firmware_into_buf(struct udevice *dev, · line 241

Quoted 10 lines
drivers/misc/fs_loader.c +10 -0
+ * request_firmware_into_buf_via_env - Load firmware using environment variables.
+ * @dev: An instance of a driver.
+ * @buf: Address of buffer to load firmware into.
+ * @offset: Offset of a file for start reading into buffer.
+ * @fw_index: Index of the firmware entry to load.
+ *
+ * This function loads firmware into the provided buffer using environment
+ * variables to determine the firmware file path, address, and size. It supports
+ * multiple firmware entries by using indexed environment variable names such as
+ * "fw_dir0", "fw_size0", etc.

To make this really scriptable, this code has to run a script which does the firmware loading and then points the U-Boot code to the buffer with data in it.

Instead of "fw_index", use "char *script_name" or some such. Then, run the script. The script should set up two environment variables, call them e.g. "fw_addr" and "fw_size" or some such . Then, this code should read the two environment variables, validate them, and possibly copy their content into $buf . Finally, the code should always unset fw_addr and fw_size .

Quoted 8 lines
Quoted 4 lines

Note that request_firmware_into_buf_via_env() should also have 'size' parameter which describes the maximum size of loaded firmware, so the code could verify whether the copy of firmware into $buf would write past the buffer and prevent that.

And for v4, can we please put this patch and the patch using it in the same series? I see there's some feedback right now from Frank on the other patch about how maybe this part should be implemented.

The airoha phy patch does not yet use the fs_loader,it uses only similar concept to be replaced by fw_loader. Currently it uses own
vars and loads directly without fs_loader as far as i see.

For fs loader it must be more generic,so basedir and vendor and filename maybe need to be separated. Or vendor is part of filename...but in this case it is again redundant in env (could be defined in driver),but not that easy to determine if file is in rootdir of e.g. usb. Then basedir and vendor is empty,but subsystem
and part set,filename from driver.

Possibly vendor can be optional in fsloader so it can check with or without. So driver can set it but fs_loader tries first with and as fallback without vendor string if file is not found there.

There are much complex subpaths (e.g.
mediatek/mt7996, i know, wifi firmware not
related for bootloader, but imho we should think about it that this can happen)

regards Frank