public inbox for driver-core@lists.linux.dev
 help / color / mirror / Atom feed
* [PATCH] firmware_loader: allow firmware_class.path to take multiple paths
@ 2026-03-18 12:25 Jeff Layton
  2026-03-18 12:39 ` Greg Kroah-Hartman
  2026-03-18 12:40 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 4+ messages in thread
From: Jeff Layton @ 2026-03-18 12:25 UTC (permalink / raw)
  To: Luis Chamberlain, Russ Weight, Danilo Krummrich,
	Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Michal Grzedzicki, driver-core, linux-kernel, Jeff Layton

Refactor fw_get_filesystem_firmware() by extracting the per-path
firmware loading logic into a new fw_try_firmware_path() helper.

Use this helper to parse fw_path_para for ';'-separated paths,
trying each one before falling through to the default firmware
search paths. This allows users to specify multiple custom firmware
directories via firmware_class.path, e.g.:

  firmware_class.path=/custom/path1;/custom/path2

Suggested-by: Michal Grzedzicki <mge@meta.com>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
This is something Michal had asked for last year, and I just got around
to implementing. Tested with a dummy module that calls
request_firmware().
---
 drivers/base/firmware_loader/main.c | 197 +++++++++++++++++++++---------------
 1 file changed, 118 insertions(+), 79 deletions(-)

diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
index a11b30dda23be563bd55f25474ceff2153ddd667..5328d28a2141e85265e1bcc20e995ce4c5517c36 100644
--- a/drivers/base/firmware_loader/main.c
+++ b/drivers/base/firmware_loader/main.c
@@ -470,7 +470,6 @@ static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
 /* direct firmware loading support */
 static char fw_path_para[256];
 static const char * const fw_path[] = {
-	fw_path_para,
 	"/lib/firmware/updates/" UTS_RELEASE,
 	"/lib/firmware/updates",
 	"/lib/firmware/" UTS_RELEASE,
@@ -480,10 +479,83 @@ static const char * const fw_path[] = {
 /*
  * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
  * from kernel command line because firmware_class is generally built in
- * kernel instead of module.
+ * kernel instead of module. Multiple paths can be separated by ';'.
  */
 module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
-MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
+MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path, multiple paths can be separated by ';'");
+
+static int
+fw_try_firmware_path(struct device *device, struct fw_priv *fw_priv,
+		     const char *suffix,
+		     int (*decompress)(struct device *dev,
+				       struct fw_priv *fw_priv,
+				       size_t in_size,
+				       const void *in_buffer),
+		     const char *dir, int dirlen,
+		     char *path, void **bufp, size_t msize)
+{
+	size_t file_size = 0;
+	size_t *file_size_ptr = NULL;
+	size_t size;
+	int len, rc;
+
+	len = snprintf(path, PATH_MAX, "%.*s/%s%s",
+		       dirlen, dir, fw_priv->fw_name, suffix);
+	if (len >= PATH_MAX)
+		return -ENAMETOOLONG;
+
+	fw_priv->size = 0;
+
+	/*
+	 * The total file size is only examined when doing a partial
+	 * read; the "full read" case needs to fail if the whole
+	 * firmware was not completely loaded.
+	 */
+	if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && *bufp)
+		file_size_ptr = &file_size;
+
+	/* load firmware files from the mount namespace of init */
+	rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
+					       bufp, msize,
+					       file_size_ptr,
+					       READING_FIRMWARE);
+	if (rc < 0) {
+		if (!(fw_priv->opt_flags & FW_OPT_NO_WARN)) {
+			if (rc != -ENOENT)
+				dev_warn(device,
+					 "loading %s failed with error %d\n",
+					 path, rc);
+			else
+				dev_dbg(device,
+					"loading %s failed for no such file or directory.\n",
+					path);
+		}
+		return rc;
+	}
+	size = rc;
+
+	dev_dbg(device, "Loading firmware from %s\n", path);
+	if (decompress) {
+		dev_dbg(device, "f/w decompressing %s\n",
+			fw_priv->fw_name);
+		rc = decompress(device, fw_priv, size, *bufp);
+		/* discard the superfluous original content */
+		vfree(*bufp);
+		*bufp = NULL;
+		if (rc) {
+			fw_free_paged_buf(fw_priv);
+			return rc;
+		}
+	} else {
+		dev_dbg(device, "direct-loading %s\n",
+			fw_priv->fw_name);
+		if (!fw_priv->data)
+			fw_priv->data = *bufp;
+		fw_priv->size = size;
+	}
+	fw_state_done(fw_priv);
+	return 0;
+}
 
 static int
 fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
@@ -493,10 +565,9 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
 					     size_t in_size,
 					     const void *in_buffer))
 {
-	size_t size;
-	int i, len, maxlen = 0;
+	int i;
 	int rc = -ENOENT;
-	char *path, *nt = NULL;
+	char *path;
 	size_t msize = INT_MAX;
 	void *buffer = NULL;
 
@@ -511,83 +582,51 @@ fw_get_filesystem_firmware(struct device *device, struct fw_priv *fw_priv,
 		return -ENOMEM;
 
 	wait_for_initramfs();
-	for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
-		size_t file_size = 0;
-		size_t *file_size_ptr = NULL;
-
-		/* skip the unset customized path */
-		if (!fw_path[i][0])
-			continue;
-
-		/* strip off \n from customized path */
-		maxlen = strlen(fw_path[i]);
-		if (i == 0) {
-			nt = strchr(fw_path[i], '\n');
-			if (nt)
-				maxlen = nt - fw_path[i];
-		}
-
-		len = snprintf(path, PATH_MAX, "%.*s/%s%s",
-			       maxlen, fw_path[i],
-			       fw_priv->fw_name, suffix);
-		if (len >= PATH_MAX) {
-			rc = -ENAMETOOLONG;
-			break;
-		}
 
-		fw_priv->size = 0;
-
-		/*
-		 * The total file size is only examined when doing a partial
-		 * read; the "full read" case needs to fail if the whole
-		 * firmware was not completely loaded.
-		 */
-		if ((fw_priv->opt_flags & FW_OPT_PARTIAL) && buffer)
-			file_size_ptr = &file_size;
-
-		/* load firmware files from the mount namespace of init */
-		rc = kernel_read_file_from_path_initns(path, fw_priv->offset,
-						       &buffer, msize,
-						       file_size_ptr,
-						       READING_FIRMWARE);
-		if (rc < 0) {
-			if (!(fw_priv->opt_flags & FW_OPT_NO_WARN)) {
-				if (rc != -ENOENT)
-					dev_warn(device,
-						 "loading %s failed with error %d\n",
-						 path, rc);
-				else
-					dev_dbg(device,
-						"loading %s failed for no such file or directory.\n",
-						path);
-			}
-			continue;
-		}
-		size = rc;
-		rc = 0;
-
-		dev_dbg(device, "Loading firmware from %s\n", path);
-		if (decompress) {
-			dev_dbg(device, "f/w decompressing %s\n",
-				fw_priv->fw_name);
-			rc = decompress(device, fw_priv, size, buffer);
-			/* discard the superfluous original content */
-			vfree(buffer);
-			buffer = NULL;
-			if (rc) {
-				fw_free_paged_buf(fw_priv);
-				continue;
+	/* Try each ';'-separated path in fw_path_para first */
+	if (fw_path_para[0]) {
+		const char *start = fw_path_para;
+		const char *end;
+
+		while (*start) {
+			int dirlen;
+
+			end = strchr(start, ';');
+			if (end)
+				dirlen = end - start;
+			else
+				dirlen = strlen(start);
+
+			/* strip trailing newline */
+			if (dirlen > 0 && start[dirlen - 1] == '\n')
+				dirlen--;
+
+			if (dirlen > 0) {
+				rc = fw_try_firmware_path(device, fw_priv,
+							  suffix, decompress,
+							  start, dirlen,
+							  path, &buffer,
+							  msize);
+				if (!rc)
+					goto done;
 			}
-		} else {
-			dev_dbg(device, "direct-loading %s\n",
-				fw_priv->fw_name);
-			if (!fw_priv->data)
-				fw_priv->data = buffer;
-			fw_priv->size = size;
+
+			if (!end)
+				break;
+			start = end + 1;
 		}
-		fw_state_done(fw_priv);
-		break;
 	}
+
+	/* Try default firmware paths */
+	for (i = 0; i < ARRAY_SIZE(fw_path); i++) {
+		rc = fw_try_firmware_path(device, fw_priv, suffix, decompress,
+					  fw_path[i], strlen(fw_path[i]),
+					  path, &buffer, msize);
+		if (!rc)
+			break;
+	}
+
+done:
 	__putname(path);
 
 	return rc;

---
base-commit: 2d1373e4246da3b58e1df058374ed6b101804e07
change-id: 20260317-fw-path-a094c30259a5

Best regards,
-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] firmware_loader: allow firmware_class.path to take multiple paths
  2026-03-18 12:25 [PATCH] firmware_loader: allow firmware_class.path to take multiple paths Jeff Layton
@ 2026-03-18 12:39 ` Greg Kroah-Hartman
  2026-03-18 12:40 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-03-18 12:39 UTC (permalink / raw)
  To: Jeff Layton
  Cc: Luis Chamberlain, Russ Weight, Danilo Krummrich,
	Rafael J. Wysocki, Michal Grzedzicki, driver-core, linux-kernel

On Wed, Mar 18, 2026 at 08:25:54AM -0400, Jeff Layton wrote:
> Refactor fw_get_filesystem_firmware() by extracting the per-path
> firmware loading logic into a new fw_try_firmware_path() helper.
> 
> Use this helper to parse fw_path_para for ';'-separated paths,
> trying each one before falling through to the default firmware
> search paths. This allows users to specify multiple custom firmware
> directories via firmware_class.path, e.g.:
> 
>   firmware_class.path=/custom/path1;/custom/path2

Why are you using ';' and not ':' as the traditional path separation
character?  And where is this now documented?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] firmware_loader: allow firmware_class.path to take multiple paths
  2026-03-18 12:25 [PATCH] firmware_loader: allow firmware_class.path to take multiple paths Jeff Layton
  2026-03-18 12:39 ` Greg Kroah-Hartman
@ 2026-03-18 12:40 ` Greg Kroah-Hartman
  2026-03-18 12:56   ` Jeff Layton
  1 sibling, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-03-18 12:40 UTC (permalink / raw)
  To: Jeff Layton
  Cc: Luis Chamberlain, Russ Weight, Danilo Krummrich,
	Rafael J. Wysocki, Michal Grzedzicki, driver-core, linux-kernel

On Wed, Mar 18, 2026 at 08:25:54AM -0400, Jeff Layton wrote:
> Refactor fw_get_filesystem_firmware() by extracting the per-path
> firmware loading logic into a new fw_try_firmware_path() helper.
> 
> Use this helper to parse fw_path_para for ';'-separated paths,
> trying each one before falling through to the default firmware
> search paths. This allows users to specify multiple custom firmware
> directories via firmware_class.path, e.g.:
> 
>   firmware_class.path=/custom/path1;/custom/path2
> 
> Suggested-by: Michal Grzedzicki <mge@meta.com>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>
> ---
> This is something Michal had asked for last year, and I just got around
> to implementing. Tested with a dummy module that calls
> request_firmware().
> ---
>  drivers/base/firmware_loader/main.c | 197 +++++++++++++++++++++---------------
>  1 file changed, 118 insertions(+), 79 deletions(-)
> 
> diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
> index a11b30dda23be563bd55f25474ceff2153ddd667..5328d28a2141e85265e1bcc20e995ce4c5517c36 100644
> --- a/drivers/base/firmware_loader/main.c
> +++ b/drivers/base/firmware_loader/main.c
> @@ -470,7 +470,6 @@ static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
>  /* direct firmware loading support */
>  static char fw_path_para[256];
>  static const char * const fw_path[] = {
> -	fw_path_para,
>  	"/lib/firmware/updates/" UTS_RELEASE,
>  	"/lib/firmware/updates",
>  	"/lib/firmware/" UTS_RELEASE,
> @@ -480,10 +479,83 @@ static const char * const fw_path[] = {
>  /*
>   * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
>   * from kernel command line because firmware_class is generally built in
> - * kernel instead of module.
> + * kernel instead of module. Multiple paths can be separated by ';'.
>   */
>  module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
> -MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
> +MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path, multiple paths can be separated by ';'");

Ah, you documented it here, sorry, I missed that.

But still, why ';'?

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] firmware_loader: allow firmware_class.path to take multiple paths
  2026-03-18 12:40 ` Greg Kroah-Hartman
@ 2026-03-18 12:56   ` Jeff Layton
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Layton @ 2026-03-18 12:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Luis Chamberlain, Russ Weight, Danilo Krummrich,
	Rafael J. Wysocki, Michal Grzedzicki, driver-core, linux-kernel

On Wed, 2026-03-18 at 13:40 +0100, Greg Kroah-Hartman wrote:
> On Wed, Mar 18, 2026 at 08:25:54AM -0400, Jeff Layton wrote:
> > Refactor fw_get_filesystem_firmware() by extracting the per-path
> > firmware loading logic into a new fw_try_firmware_path() helper.
> > 
> > Use this helper to parse fw_path_para for ';'-separated paths,
> > trying each one before falling through to the default firmware
> > search paths. This allows users to specify multiple custom firmware
> > directories via firmware_class.path, e.g.:
> > 
> >   firmware_class.path=/custom/path1;/custom/path2
> > 
> > Suggested-by: Michal Grzedzicki <mge@meta.com>
> > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > ---
> > This is something Michal had asked for last year, and I just got around
> > to implementing. Tested with a dummy module that calls
> > request_firmware().
> > ---
> >  drivers/base/firmware_loader/main.c | 197 +++++++++++++++++++++---------------
> >  1 file changed, 118 insertions(+), 79 deletions(-)
> > 
> > diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
> > index a11b30dda23be563bd55f25474ceff2153ddd667..5328d28a2141e85265e1bcc20e995ce4c5517c36 100644
> > --- a/drivers/base/firmware_loader/main.c
> > +++ b/drivers/base/firmware_loader/main.c
> > @@ -470,7 +470,6 @@ static int fw_decompress_xz(struct device *dev, struct fw_priv *fw_priv,
> >  /* direct firmware loading support */
> >  static char fw_path_para[256];
> >  static const char * const fw_path[] = {
> > -	fw_path_para,
> >  	"/lib/firmware/updates/" UTS_RELEASE,
> >  	"/lib/firmware/updates",
> >  	"/lib/firmware/" UTS_RELEASE,
> > @@ -480,10 +479,83 @@ static const char * const fw_path[] = {
> >  /*
> >   * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH'
> >   * from kernel command line because firmware_class is generally built in
> > - * kernel instead of module.
> > + * kernel instead of module. Multiple paths can be separated by ';'.
> >   */
> >  module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);
> > -MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path");
> > +MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path, multiple paths can be separated by ';'");
> 
> Ah, you documented it here, sorry, I missed that.
> 

If there's another spot we should document this, then happy to do so as
well. I didn't see much about this subsystem under Documentation/.

> But still, why ';'?
> 

Honestly, I don't know why, but I was thinking that ';' was the
traditional $PATH separator at the time. I'll change it to ':'. Expect
a v2 soon!

Thanks,
-- 
Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-18 12:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-18 12:25 [PATCH] firmware_loader: allow firmware_class.path to take multiple paths Jeff Layton
2026-03-18 12:39 ` Greg Kroah-Hartman
2026-03-18 12:40 ` Greg Kroah-Hartman
2026-03-18 12:56   ` Jeff Layton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox