[PATCH v3] bootm: add global.bootm.root_dev env var for booting via PARTUUID

Robert Karszniewicz r.karszniewicz at phytec.de
Mon Aug 10 08:20:08 EDT 2020


Introduces a new env var which specifies which device is the rootfs
device to be used in Linux, passed to Linux via bootargs, identified by
the rootfs partition's PARTUUID.

global.bootm.root_dev supplements global.bootm.appendroot, in that it
overrides appendroot's naïve default, which picks the partition that the
kernel resides on (global.bootm.image).

Example:
detect mmc2
global.bootm.image='/mnt/mmc2.0/zImage'
global.bootm.appendroot=1
global.bootm.root_dev=/dev/mmc2.1
boot mmc

Signed-off-by: Robert Karszniewicz <r.karszniewicz at phytec.de>
---
Changes from v2:
- correct commit message line lengths

Changes from v1:
- minor commit message change

Changes from RFC v2:
- replaced path_get_linux_rootarg("invalid") by ERR_PTR(-EINVAL)
- added error messages. (I've made them mutually exclusive to prevent
  NULL-pointer dereference)

 common/bootm.c  | 25 +++++++++++++++++++++++--
 include/bootm.h |  2 ++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index 8fec1ee34dd8..7f951172ccf5 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -64,6 +64,7 @@ void bootm_data_init_defaults(struct bootm_data *data)
 	getenv_ul("global.bootm.image.loadaddr", &data->os_address);
 	getenv_ul("global.bootm.initrd.loadaddr", &data->initrd_address);
 	data->initrd_file = getenv_nonempty("global.bootm.initrd");
+	data->root_dev = getenv_nonempty("global.bootm.root_dev");
 	data->verify = bootm_get_verify_mode();
 	data->appendroot = bootm_appendroot;
 	data->provide_machine_id = bootm_provide_machine_id;
@@ -655,7 +656,25 @@ int bootm_boot(struct bootm_data *bootm_data)
 	if (bootm_data->appendroot) {
 		char *rootarg;
 
-		rootarg = path_get_linux_rootarg(data->os_file);
+		if (bootm_data->root_dev) {
+			const char *root_dev_name = devpath_to_name(bootm_data->root_dev);
+			const struct cdev *root_cdev = cdev_by_name(root_dev_name);
+
+			if (root_cdev && root_cdev->partuuid[0] != 0) {
+				rootarg = basprintf("root=PARTUUID=%s", root_cdev->partuuid);
+			} else {
+				rootarg = ERR_PTR(-EINVAL);
+
+				if (!root_cdev)
+					pr_err("no cdev found for %s, cannot set root= option\n",
+						root_dev_name);
+				else if (!root_cdev->partuuid[0])
+					pr_err("%s doesn't have a PARTUUID, cannot set root= option\n",
+						root_dev_name);
+			}
+		} else {
+			rootarg = path_get_linux_rootarg(data->os_file);
+		}
 		if (!IS_ERR(rootarg)) {
 			printf("Adding \"%s\" to Kernel commandline\n", rootarg);
 			globalvar_add_simple("linux.bootargs.bootm.appendroot",
@@ -742,6 +761,7 @@ static int bootm_init(void)
 	globalvar_add_simple("bootm.image", NULL);
 	globalvar_add_simple("bootm.image.loadaddr", NULL);
 	globalvar_add_simple("bootm.oftree", NULL);
+	globalvar_add_simple("bootm.root_dev", NULL);
 	globalvar_add_simple("bootm.tee", NULL);
 	globalvar_add_simple_bool("bootm.appendroot", &bootm_appendroot);
 	globalvar_add_simple_bool("bootm.provide_machine_id", &bootm_provide_machine_id);
@@ -771,5 +791,6 @@ BAREBOX_MAGICVAR_NAMED(global_bootm_oftree, global.bootm.oftree, "bootm default
 BAREBOX_MAGICVAR_NAMED(global_bootm_tee, global.bootm.tee, "bootm default tee image");
 BAREBOX_MAGICVAR_NAMED(global_bootm_verify, global.bootm.verify, "bootm default verify level");
 BAREBOX_MAGICVAR_NAMED(global_bootm_verbose, global.bootm.verbose, "bootm default verbosity level (0=quiet)");
-BAREBOX_MAGICVAR_NAMED(global_bootm_appendroot, global.bootm.appendroot, "Add root= option to Kernel to mount rootfs from the device the Kernel comes from");
+BAREBOX_MAGICVAR_NAMED(global_bootm_appendroot, global.bootm.appendroot, "Add root= option to Kernel to mount rootfs from the device the Kernel comes from (default, device can be overridden via global.bootm.root_dev)");
+BAREBOX_MAGICVAR_NAMED(global_bootm_root_dev, global.bootm.root_dev, "bootm default root device (overrides default device in global.bootm.appendroot)");
 BAREBOX_MAGICVAR_NAMED(global_bootm_provide_machine_id, global.bootm.provide_machine_id, "If true, add systemd.machine_id= with value of global.machine_id to Kernel");
diff --git a/include/bootm.h b/include/bootm.h
index 7782de7a476d..3628a7eea5b9 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -18,6 +18,7 @@ struct bootm_data {
 	const char *initrd_file;
 	const char *oftree_file;
 	const char *tee_file;
+	const char *root_dev;
 	int verbose;
 	enum bootm_verify verify;
 	bool force;
@@ -25,6 +26,7 @@ struct bootm_data {
 	/*
 	 * appendroot - if true, try to add a suitable root= Kernel option to
 	 * mount the rootfs from the same device as the Kernel comes from.
+	 * The default rootfs device can be overridden with root_dev.
 	 */
 	bool appendroot;
 	/*
-- 
2.7.4




More information about the barebox mailing list