From 8aa57934e52f254064338cfb2a445af9a1f592f9 Mon Sep 17 00:00:00 2001 From: Marek Vasut Date: Mon, 22 Sep 2025 13:48:27 +0200 Subject: fw_loader: Split from fs_loader into separate library file The script based firmware loader does not use anything from the fs_loader implementation. Separate it into its own library source file and convert the mediatek PHY to use this separate code. This should reduce the amount of code that is pulled in alongside the firmware loader, as the FS loader is no longer included. Signed-off-by: Marek Vasut --- lib/fw_loader.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/fw_loader.c (limited to 'lib/fw_loader.c') diff --git a/lib/fw_loader.c b/lib/fw_loader.c new file mode 100644 index 00000000000..207f5fadd1e --- /dev/null +++ b/lib/fw_loader.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2025 Lucien Jheng + */ + +#include +#include +#include +#include +#include + +int request_firmware_into_buf_via_script(void *buf, size_t max_size, + const char *script_name, + size_t *retsize) +{ + char *args[2] = { "run", (char *)script_name }; + int ret, repeatable; + ulong addr, size; + + if (!buf || !script_name || !max_size) + return -EINVAL; + + /* Run the firmware loading script */ + ret = cmd_process(0, 2, args, &repeatable, NULL); + 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; + } + + if (retsize) + *retsize = size; + + memcpy(buf, (void *)addr, size); + + return 0; +} -- cgit v1.2.3 From 50fc92938623be4a1cf1993c703812643afc9cdd Mon Sep 17 00:00:00 2001 From: Marek Vasut Date: Mon, 22 Sep 2025 13:48:28 +0200 Subject: fw_loader: Prefix the FW loader variables with the script prefix Add the script name as a prefix to fw_addr and fw_size variables to make sure they are always unique and won't easily conflict with user variables. Signed-off-by: Marek Vasut --- lib/fw_loader.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib/fw_loader.c') diff --git a/lib/fw_loader.c b/lib/fw_loader.c index 207f5fadd1e..f776e09523a 100644 --- a/lib/fw_loader.c +++ b/lib/fw_loader.c @@ -13,6 +13,8 @@ int request_firmware_into_buf_via_script(void *buf, size_t max_size, const char *script_name, size_t *retsize) { + char env_addr[CONFIG_SYS_CBSIZE] = { 0 }; + char env_size[CONFIG_SYS_CBSIZE] = { 0 }; char *args[2] = { "run", (char *)script_name }; int ret, repeatable; ulong addr, size; @@ -28,13 +30,17 @@ int request_firmware_into_buf_via_script(void *buf, size_t max_size, return -EINVAL; } + /* Prefix the FW loader variables with the script prefix */ + snprintf(env_addr, sizeof(env_addr), "%s_addr", script_name); + snprintf(env_size, sizeof(env_size), "%s_size", script_name); + /* 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); + addr = env_get_hex(env_addr, 0); + size = env_get_hex(env_size, 0); /* Clear the variables set by the firmware loading script */ - env_set("fw_addr", NULL); - env_set("fw_size", NULL); + env_set(env_addr, NULL); + env_set(env_size, NULL); if (!addr || !size) { log_err("Firmware address (0x%lx) or size (0x%lx) are invalid.\n", -- cgit v1.2.3