diff mbox series

[fstools,v2,1/2] partname: check all parameters for overriding if needed

Message ID 20220702064858.2392-1-musashino.open@gmail.com
State Under Review
Delegated to: Daniel Golle
Headers show
Series [fstools,v2,1/2] partname: check all parameters for overriding if needed | expand

Commit Message

INAGAKI Hiroshi July 2, 2022, 6:48 a.m. UTC
In bootargs, the specific parameter is overridden if needed for booting
OpenWrt, such as "root=".
In this case, the last specific parameter in bootargs should be read
instead of the one passed by bootloader.

example (I-O DATA HDL2-AAX):

console=ttyS0,115200 mtdparts=spi0.0:960k(boot),64k(env),-(system) \
  initrd=0x3000040 rw root=/dev/md2 rootfstype=ext4 rw rootdelay=2 \
  init=/lib/sysvinit/init init=/sbin/init root=PARTLABEL=rootfs \
  rootfstype=squashfs,ext4

- "root=/dev/md2"        : passed by bootloader, for stock firmware
- "root=PARTLABEL=rootfs": passed by dtb, for OpenWrt

So, this patch adds checking support for all parameters in the specified
file and check all for root device ("root=" parameter).

Signed-off-by: INAGAKI Hiroshi <musashino.open@gmail.com>
---
v1 -> v2:

  (no patch)

v2 -> v3:

  - added patch to fix wrong detection of overridden root= parameter

 libfstools/partname.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

Comments

INAGAKI Hiroshi July 2, 2022, 12:34 p.m. UTC | #1
Ahhhh, this series is not v2, it's v3... I mistook in the subject...

On 2022/07/02 15:48, INAGAKI Hiroshi wrote:
> In bootargs, the specific parameter is overridden if needed for booting
> OpenWrt, such as "root=".
> In this case, the last specific parameter in bootargs should be read
> instead of the one passed by bootloader.
>
> example (I-O DATA HDL2-AAX):
>
> console=ttyS0,115200 mtdparts=spi0.0:960k(boot),64k(env),-(system) \
>    initrd=0x3000040 rw root=/dev/md2 rootfstype=ext4 rw rootdelay=2 \
>    init=/lib/sysvinit/init init=/sbin/init root=PARTLABEL=rootfs \
>    rootfstype=squashfs,ext4
>
> - "root=/dev/md2"        : passed by bootloader, for stock firmware
> - "root=PARTLABEL=rootfs": passed by dtb, for OpenWrt
>
> So, this patch adds checking support for all parameters in the specified
> file and check all for root device ("root=" parameter).
>
> Signed-off-by: INAGAKI Hiroshi <musashino.open@gmail.com>
> ---
> v1 -> v2:
>
>    (no patch)
>
> v2 -> v3:
>
>    - added patch to fix wrong detection of overridden root= parameter
>
>   libfstools/partname.c | 17 ++++++++++-------
>   1 file changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/libfstools/partname.c b/libfstools/partname.c
> index f59c52e..e282402 100644
> --- a/libfstools/partname.c
> +++ b/libfstools/partname.c
> @@ -62,9 +62,9 @@ static int partname_volume_init(struct volume *v)
>   }
>   
>   /* adapted from procd/utils.c -> should go to libubox */
> -static char* get_var_from_file(const char* filename, const char* name, char* out, int len)
> +static char* get_var_from_file(const char* filename, const char* name, char* out, int len, bool chk_all)
>   {
> -	char line[1024], *c, *sptr;
> +	char line[1024], *c, *sptr, *_out = NULL;
>   	int fd = open(filename, O_RDONLY);
>   	if (fd == -1)
>   		return NULL;
> @@ -89,10 +89,13 @@ static char* get_var_from_file(const char* filename, const char* name, char* out
>   
>   		strncpy(out, &sep[1], len);
>   		out[len-1] = '\0';
> -		return out;
> +		if (!chk_all)
> +			return out;
> +
> +		_out = out;
>   	}
>   
> -	return NULL;
> +	return _out;
>   }
>   
>   static char *rootdevname(char *devpath) {
> @@ -123,12 +126,12 @@ static struct volume *partname_volume_find(char *name)
>   	bool found = false;
>   	glob_t gl;
>   
> -	if (get_var_from_file("/proc/cmdline", "fstools_ignore_partname", rootparam, sizeof(rootparam))) {
> +	if (get_var_from_file("/proc/cmdline", "fstools_ignore_partname", rootparam, sizeof(rootparam), false)) {
>   		if (!strcmp("1", rootparam))
>   			return NULL;
>   	}
>   
> -	if (get_var_from_file("/proc/cmdline", "root", rootparam, sizeof(rootparam))) {
> +	if (get_var_from_file("/proc/cmdline", "root", rootparam, sizeof(rootparam), true)) {
>   		rootdev = rootdevname(rootparam);
>   		/* find partition on same device as rootfs */
>   		snprintf(ueventgstr, sizeof(ueventgstr), "%s/%s/*/uevent", block_dir_name, rootdev);
> @@ -139,7 +142,7 @@ static struct volume *partname_volume_find(char *name)
>   
>   	if (!glob(ueventgstr, GLOB_NOESCAPE, NULL, &gl))
>   		for (j = 0; j < gl.gl_pathc; j++) {
> -			if (!get_var_from_file(gl.gl_pathv[j], "PARTNAME", namebuf, sizeof(namebuf)))
> +			if (!get_var_from_file(gl.gl_pathv[j], "PARTNAME", namebuf, sizeof(namebuf), false))
>   				continue;
>   			if (!strncmp(namebuf, name, sizeof(namebuf))) {
>   				found = 1;
diff mbox series

Patch

diff --git a/libfstools/partname.c b/libfstools/partname.c
index f59c52e..e282402 100644
--- a/libfstools/partname.c
+++ b/libfstools/partname.c
@@ -62,9 +62,9 @@  static int partname_volume_init(struct volume *v)
 }
 
 /* adapted from procd/utils.c -> should go to libubox */
-static char* get_var_from_file(const char* filename, const char* name, char* out, int len)
+static char* get_var_from_file(const char* filename, const char* name, char* out, int len, bool chk_all)
 {
-	char line[1024], *c, *sptr;
+	char line[1024], *c, *sptr, *_out = NULL;
 	int fd = open(filename, O_RDONLY);
 	if (fd == -1)
 		return NULL;
@@ -89,10 +89,13 @@  static char* get_var_from_file(const char* filename, const char* name, char* out
 
 		strncpy(out, &sep[1], len);
 		out[len-1] = '\0';
-		return out;
+		if (!chk_all)
+			return out;
+
+		_out = out;
 	}
 
-	return NULL;
+	return _out;
 }
 
 static char *rootdevname(char *devpath) {
@@ -123,12 +126,12 @@  static struct volume *partname_volume_find(char *name)
 	bool found = false;
 	glob_t gl;
 
-	if (get_var_from_file("/proc/cmdline", "fstools_ignore_partname", rootparam, sizeof(rootparam))) {
+	if (get_var_from_file("/proc/cmdline", "fstools_ignore_partname", rootparam, sizeof(rootparam), false)) {
 		if (!strcmp("1", rootparam))
 			return NULL;
 	}
 
-	if (get_var_from_file("/proc/cmdline", "root", rootparam, sizeof(rootparam))) {
+	if (get_var_from_file("/proc/cmdline", "root", rootparam, sizeof(rootparam), true)) {
 		rootdev = rootdevname(rootparam);
 		/* find partition on same device as rootfs */
 		snprintf(ueventgstr, sizeof(ueventgstr), "%s/%s/*/uevent", block_dir_name, rootdev);
@@ -139,7 +142,7 @@  static struct volume *partname_volume_find(char *name)
 
 	if (!glob(ueventgstr, GLOB_NOESCAPE, NULL, &gl))
 		for (j = 0; j < gl.gl_pathc; j++) {
-			if (!get_var_from_file(gl.gl_pathv[j], "PARTNAME", namebuf, sizeof(namebuf)))
+			if (!get_var_from_file(gl.gl_pathv[j], "PARTNAME", namebuf, sizeof(namebuf), false))
 				continue;
 			if (!strncmp(namebuf, name, sizeof(namebuf))) {
 				found = 1;