]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
misc: fs_loader: Add request_firmware_into_buf_via_script() for flexible firmware...
authorLucien.Jheng <lucienzx159@gmail.com>
Sun, 17 Aug 2025 15:02:03 +0000 (23:02 +0800)
committerTom Rini <trini@konsulko.com>
Tue, 26 Aug 2025 20:39:15 +0000 (14:39 -0600)
This commit introduces a new API,
request_firmware_into_buf_via_script(), to the fs_loader framework.
This function allows firmware to be loaded into memory using
a user-defined U-Boot script, providing greater flexibility for
firmware loading scenarios that cannot be handled by static file
paths or device/partition selection alone.

Key features:
- The API runs a specified U-Boot script (by name), which is responsible
  for loading the firmware into memory by any means (e.g., load from MMC, USB, network, etc.).
- The script must set two environment variables: 'fw_addr'
  (the memory address where the firmware is loaded) and
  'fw_size' (the size of the firmware in bytes).
- The function validates these variables, copies the firmware into a newly
  allocated buffer (using memdup), and returns the pointer
  via the provided double pointer argument.
- The maximum allowed firmware size is checked to prevent buffer overflows.
- The environment variables are cleared after use to avoid stale data.
- Detailed error messages are provided for all failure conditions to aid debugging.

Usage example:
1. Define a U-Boot script in the environment that loads the firmware
   and sets the required variables:
   => env set my_fw_script 'load mmc 0:1 ${loadaddr} firmware.bin &&
   env set fw_addr ${loadaddr} && env set fw_size ${filesize}'

2. In your code, call the new API:
   void *fw_buf = NULL;
   int ret = request_firmware_into_buf_via_script(&fw_buf, 0x46000000, "my_fw_script");
   if (ret < 0)
return ret;

This approach allows board integrators and users to customize the firmware
loading process without modifying the source code,
simply by changing the script in the U-Boot environment.

Signed-off-by: Lucien.Jheng <lucienzx159@gmail.com>
Reviewed-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
[trini: Fix printf of size_t needing to use %zx]
Signed-off-by: Tom Rini <trini@konsulko.com>
drivers/misc/fs_loader.c
include/fs_loader.h

index 66803f4b9975f4792c70adf16625cdf0e5bf1799..60296d55f23631d503bcee87ca2a7e020133b182 100644 (file)
@@ -228,6 +228,52 @@ int request_firmware_into_buf(struct udevice *dev,
        return ret;
 }
 
+int request_firmware_into_buf_via_script(void **buf, size_t max_size,
+                                        const char *script_name)
+{
+       ulong addr, size;
+       int ret;
+       char cmd[32];
+
+       if (!buf || !script_name || !max_size)
+               return -EINVAL;
+
+       /* Create command to run the firmware loading script */
+       snprintf(cmd, sizeof(cmd), "run %s", script_name);
+
+       /* Run the firmware loading script */
+       ret = run_command_list(cmd, -1, 0);
+       if (ret) {
+               log_err("Firmware loading script '%s' not defined or failed.\n",
+                       script_name);
+               return -EINVAL;
+       }
+
+       /* Find out where the firmware got loaded and how long it is */
+       addr = env_get_hex("fw_addr", 0);
+       size = env_get_hex("fw_size", 0);
+
+       /* Clear the variables set by the firmware loading script */
+       env_set("fw_addr", NULL);
+       env_set("fw_size", NULL);
+
+       if (!addr || !size) {
+               log_err("Firmware address (0x%lx) or size (0x%lx) are invalid.\n",
+                       addr, size);
+               return -EINVAL;
+       }
+
+       if (size > max_size) {
+               log_err("Loaded firmware size 0x%lx exceeded maximum allowed size 0x%zx.\n",
+                       size, max_size);
+               return -E2BIG;
+       }
+
+       memcpy(*buf, (void *)addr, size);
+
+       return 0;
+}
+
 static int fs_loader_of_to_plat(struct udevice *dev)
 {
        u32 phandlepart[2];
index 5eb5b7ab4a105348ab5c0f944bad309c0cfeb01a..a433be1eb99a7bf8a97832f833fdce673d7d451b 100644 (file)
@@ -64,4 +64,28 @@ 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_script() -
+ * Load firmware using a U-Boot script and copy to buffer
+ * @buf: Pointer to a pointer where the firmware buffer will be stored.
+ * @max_size: Maximum allowed size for the firmware to be loaded.
+ * @script_name: Name of the U-Boot script to execute for firmware loading.
+ *
+ * Executes a U-Boot script (@script_name) that loads firmware into
+ * memory and sets the environment variables 'fw_addr' (address) and
+ * 'fw_size' (size in bytes). On success, copies the firmware
+ * from the given address to user buffer @buf.
+ *
+ * The script must set these environment variables:
+ *   fw_addr - Address where firmware is loaded in memory
+ *   fw_size - Size of the firmware in bytes
+ *
+ * The script should be defined in the U-Boot environment, for example:
+ *   env set script_name 'load mmc 0:1 ${loadaddr} firmware.bin &&
+ *   env set fw_addr ${loadaddr} && env set fw_size ${filesize}
+ * Return: 0 on success, negative value on error.
+ */
+int request_firmware_into_buf_via_script(void **buf, size_t max_size,
+                                        const char *script_name);
 #endif