mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 3/3] environment: use barebox environment from GPT partitions
Date: Mon, 19 Feb 2024 15:51:59 +0100	[thread overview]
Message-ID: <20240219145159.1962618-4-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20240219145159.1962618-1-s.hauer@pengutronix.de>

Documentation states that the barebox environment shall be put in a
partition with GUID 6c3737f2-07f8-45d1-ad45-15d260aab24d. So far
barebox doesn't use these partitions though, so let's change that.

With this patch barebox iterates over the currently registered block
devices and searches for partitions with the barebox environment GUID.
When one is found then it will be used for the environment. The device
barebox has booted from is preferred over other devices. Also this will
current serve as a fallback only. When board code has provided other
ideas in terms of calling default_environment_path_set() then that
will be used instead.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/environment.c | 65 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/common/environment.c b/common/environment.c
index a7432a84f3..583a2f0a03 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -26,6 +26,9 @@
 #include <environment.h>
 #include <globalvar.h>
 #include <libfile.h>
+#include <block.h>
+#include <efi/partition.h>
+#include <bootsource.h>
 #else
 #define EXPORT_SYMBOL(x)
 #endif
@@ -57,9 +60,69 @@ void default_environment_path_set(const char *path)
 	default_environment_path = xstrdup(path);
 }
 
+static guid_t partition_barebox_env_guid = PARTITION_BAREBOX_ENVIRONMENT_GUID;
+
+/*
+ * default_environment_path_search - look for environment partition
+ *
+ * This searches for a barebox environment partition on block devices. barebox
+ * environment partitions are recognized by the guid
+ * 6c3737f2-07f8-45d1-ad45-15d260aab24d. The device barebox itself has booted
+ * from is preferred over other devices.
+ *
+ * @return: The cdev providing the environment of found, NULL otherwise
+ */
+static struct cdev *default_environment_path_search(void)
+{
+	struct cdev *part;
+	struct device_node *boot_node;
+	int max_score = 0;
+	struct cdev *env_cdev = NULL;
+	struct block_device *blk;
+
+	boot_node = bootsource_of_node_get(NULL);
+
+	if (boot_node) {
+		struct device *dev;
+
+		dev = of_find_device_by_node(boot_node);
+		if (dev)
+			device_detect(dev);
+	}
+
+	for_each_block_device(blk) {
+		int score = 0;
+
+		part = cdev_find_child_by_gpt_typeuuid(&blk->cdev,
+						       &partition_barebox_env_guid);
+		if (IS_ERR(part))
+			continue;
+
+		score++;
+
+		if (boot_node && boot_node == blk->cdev.device_node)
+			score++;
+
+		if (score > max_score) {
+			max_score = score;
+			env_cdev = part;
+		}
+	}
+
+	return env_cdev;
+}
+
 const char *default_environment_path_get(void)
 {
-	if (!default_environment_path)
+	struct cdev *cdev;
+
+	if (default_environment_path)
+		return default_environment_path;
+
+	cdev = default_environment_path_search();
+	if (cdev)
+		default_environment_path = basprintf("/dev/%s", cdev->name);
+	else
 		default_environment_path = xstrdup("/dev/env0");
 
 	return default_environment_path;
-- 
2.39.2




  parent reply	other threads:[~2024-02-19 14:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19 14:51 [PATCH 0/3] use environment partitions in GPT Sascha Hauer
2024-02-19 14:51 ` [PATCH 1/3] partitions: efi: Allow to create barebox environment partition Sascha Hauer
2024-02-19 15:20   ` Ahmad Fatoum
2024-02-20 10:52     ` Sascha Hauer
2024-02-19 14:51 ` [PATCH 2/3] bootsource: add function to get device_node we booted from Sascha Hauer
2024-02-19 14:59   ` Marco Felsch
2024-02-19 15:21     ` Sascha Hauer
2024-02-20 10:56       ` Sascha Hauer
2024-02-19 14:51 ` Sascha Hauer [this message]
2024-02-19 15:06 ` [PATCH 0/3] use environment partitions in GPT Marco Felsch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240219145159.1962618-4-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox