mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: oss-tools@pengutronix.de
Subject: [OSS-Tools] [PATCH v2 6/8] state: align with barebox use of of_cdev_find
Date: Wed,  7 Jun 2023 14:16:26 +0200	[thread overview]
Message-ID: <20230607121628.3180139-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230607121628.3180139-1-a.fatoum@pengutronix.de>

As part of barebox-state by GPT partition type GUID lookup support,
state code in barebox needs direct interaction with the cdev, so it can
enumerate the child partitions. We have recently added __of_cdev_find
with internal linkage, so lets export of_cdev_find with external linkage
for outside use. Unlike __of_cdev_find, the new external function will
return dynamically allocated memory to avoid consumer code having to
know the struct cdev layout.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 src/barebox-state/state.c | 26 +++++++++++++++++++-------
 src/dt/common.h           |  8 ++++++++
 src/dt/dt.h               |  3 +++
 src/libdt-utils.sym       |  2 ++
 src/libdt.c               | 37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/src/barebox-state/state.c b/src/barebox-state/state.c
index 3174c84b8ded..29e04c3d7ea8 100644
--- a/src/barebox-state/state.c
+++ b/src/barebox-state/state.c
@@ -578,6 +578,20 @@ void state_release(struct state *state)
 	free(state);
 }
 
+#ifdef __BAREBOX__
+static char *cdev_to_devpath(struct cdev *cdev, off_t *offset, size_t *size)
+{
+	/*
+	 * We only accept partitions exactly mapping the barebox-state,
+	 * but dt-utils may need to set non-zero values here
+	 */
+	*offset = 0;
+	*size = 0;
+
+	return basprintf("/dev/%s", cdev->name);
+}
+#endif
+
 /*
  * state_new_from_node - create a new state instance from a device_node
  *
@@ -594,8 +608,9 @@ struct state *state_new_from_node(struct device_node *node, bool readonly)
 	const char *alias;
 	uint32_t stridesize;
 	struct device_node *partition_node;
-	off_t offset = 0;
-	size_t size = 0;
+	struct cdev *cdev;
+	off_t offset;
+	size_t size;
 
 	alias = of_alias_get(node);
 	if (!alias) {
@@ -614,11 +629,8 @@ struct state *state_new_from_node(struct device_node *node, bool readonly)
 		goto out_release_state;
 	}
 
-#ifdef __BAREBOX__
-	ret = of_find_path_by_node(partition_node, &state->backend_path, 0);
-#else
-	ret = of_get_devicepath(partition_node, &state->backend_path, &offset, &size);
-#endif
+	cdev = of_cdev_find(partition_node);
+	ret = PTR_ERR_OR_ZERO(cdev);
 	if (ret) {
 		if (ret != -EPROBE_DEFER)
 			dev_err(&state->dev, "state failed to parse path to backend: %s\n",
diff --git a/src/dt/common.h b/src/dt/common.h
index d16f1193fbe3..38dc61cd65fe 100644
--- a/src/dt/common.h
+++ b/src/dt/common.h
@@ -166,6 +166,14 @@ static inline void *ERR_CAST(const void *ptr)
 	return (void *) ptr;
 }
 
+static inline int PTR_ERR_OR_ZERO(const void *ptr)
+{
+	if (IS_ERR(ptr))
+		return PTR_ERR(ptr);
+	else
+		return 0;
+}
+
 static inline char *barebox_asprintf(const char *fmt, ...) __attribute__ ((format(__printf__, 1, 2)));
 static inline char *barebox_asprintf(const char *fmt, ...)
 {
diff --git a/src/dt/dt.h b/src/dt/dt.h
index d5e49eb32df9..f8bd3e56efed 100644
--- a/src/dt/dt.h
+++ b/src/dt/dt.h
@@ -231,6 +231,9 @@ void of_add_memory_bank(struct device_node *node, bool dump, int r,
 int of_get_devicepath(struct device_node *partition_node, char **devnode, off_t *offset,
 		size_t *size);
 
+struct cdev *of_cdev_find(struct device_node *partition_node);
+char *cdev_to_devpath(struct cdev *cdev, off_t *offset, size_t *size);
+
 #define for_each_node_by_name(dn, name) \
 	for (dn = of_find_node_by_name(NULL, name); dn; \
 	     dn = of_find_node_by_name(dn, name))
diff --git a/src/libdt-utils.sym b/src/libdt-utils.sym
index a749317fa024..f95c74305fb0 100644
--- a/src/libdt-utils.sym
+++ b/src/libdt-utils.sym
@@ -34,6 +34,8 @@ global:
 	of_get_child_by_name;
 	of_get_child_count;
 	of_get_devicepath;
+	of_cdev_find;
+	cdev_to_devpath;
 	of_get_next_available_child;
 	of_get_parent;
 	of_get_property;
diff --git a/src/libdt.c b/src/libdt.c
index 1b381ed8915b..ae98543400b6 100644
--- a/src/libdt.c
+++ b/src/libdt.c
@@ -2679,3 +2679,40 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t
 
 	return 0;
 }
+
+/*
+ * of_cdev_find - get information how to access device corresponding to a device_node
+ * @partition_node:	The device_node which shall be accessed
+ * @devpath:		Returns the devicepath under which the device is accessible
+ * @offset:		Returns the offset in the device
+ * @size:		Returns the size of the device
+ *
+ * This function takes a device_node which represents a partition.
+ * For this partition the function returns the device path and the offset
+ * and size in the device. For mtd devices the path will be /dev/mtdx, for
+ * EEPROMs it will be /sys/.../eeprom and for block devices it will be /dev/...
+ * For mtd devices the device path returned will be the partition itself.
+ * Since EEPROMs do not have partitions under Linux @offset and @size will
+ * describe the offset and size inside the full device. The same applies to
+ * block devices.
+ *
+ * returns 0 for success or negative error value on failure.
+ */
+struct cdev *of_cdev_find(struct device_node *partition_node)
+{
+	struct cdev cdev = {};
+	int ret;
+
+	ret = __of_cdev_find(partition_node, &cdev);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return xmemdup(&cdev, sizeof(cdev));
+}
+
+char *cdev_to_devpath(struct cdev *cdev, off_t *offset, size_t *size)
+{
+	*offset = cdev->offset;
+	*size = cdev->size;
+	return cdev->devpath;
+}
-- 
2.39.2




  parent reply	other threads:[~2023-06-07 12:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-07 12:16 [OSS-Tools] [PATCH v2 0/8] state: allow lookup of barebox state partition by Type GUID Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 1/8] state: backend: direct: open block device in read-only mode if possible Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 2/8] libdt: factor out u64 sysattr parsing into helper Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 3/8] libdt: drop broken if-branch Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 4/8] libdt: factor out __of_cdev_find helper Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 5/8] libdt: use block device partition instead of parent if found Ahmad Fatoum
2023-06-07 12:16 ` Ahmad Fatoum [this message]
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 7/8] libdt: use of_find_device_by_uuid for partuuid lookup Ahmad Fatoum
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 8/8] state: allow lookup of barebox state partition by Type GUID Ahmad Fatoum
2023-06-12 11:52 ` [OSS-Tools] [PATCH v2 0/8] " Ahmad Fatoum
2023-07-03 12:23 ` Roland Hieber

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=20230607121628.3180139-7-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=oss-tools@pengutronix.de \
    /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