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 4/8] libdt: factor out __of_cdev_find helper
Date: Wed, 31 May 2023 17:22:49 +0200	[thread overview]
Message-ID: <20230531152253.1407395-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230531152253.1407395-1-a.fatoum@pengutronix.de>

of_get_devicepath is an exported symbol by libdt, so we shouldn't change
its signature or its semantics. Yet, we will want to export more
information out of the udev'ification code in future. <dt/dt.h> already
declares an opaque struct cdev, so let's add a __of_cdev_find helper
that can populate it with the same information that of_get_devicepath
would return. In later commits we will define helpers that return
and process a struct cdev placed in dynamic memory.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 src/libdt.c | 82 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 52 insertions(+), 30 deletions(-)

diff --git a/src/libdt.c b/src/libdt.c
index 12d692d2b2cf..440fcbd32fb4 100644
--- a/src/libdt.c
+++ b/src/libdt.c
@@ -32,6 +32,12 @@
 #include <libudev.h>
 #include <dt.h>
 
+struct cdev {
+	char *devpath;
+	off_t offset;
+	size_t size;
+};
+
 static int pr_level = 5;
 
 void pr_level_set(int level)
@@ -2482,33 +2488,14 @@ static struct udev_device *of_find_device_by_uuid(struct udev_device *parent,
 	return NULL;
 }
 
-/*
- * of_get_devicepath - 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.
- */
-int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t *offset,
-		size_t *size)
+static int __of_cdev_find(struct device_node *partition_node, struct cdev *cdev)
 {
 	struct device_node *node;
 	struct udev_device *dev, *partdev, *mtd;
 	int ret;
 
-	*offset = 0;
-	*size = 0;
+	cdev->offset = 0;
+	cdev->size = 0;
 
 	/*
 	 * simplest case: This nodepath can directly be translated into
@@ -2520,8 +2507,8 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t
 	dev = of_find_device_by_node_path(partition_node->full_name);
 	if (dev) {
 		if (udev_device_is_eeprom(dev))
-			return udev_parse_eeprom(dev, devpath);
-		if (!udev_parse_mtd(dev, devpath, size))
+			return udev_parse_eeprom(dev, &cdev->devpath);
+		if (!udev_parse_mtd(dev, &cdev->devpath, &cdev->size))
 			return 0;
 
 		/*
@@ -2551,7 +2538,7 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t
 			while (*uuid)
 				*s++ = tolower(*uuid++);
 
-			*devpath = lc_uuid;
+			cdev->devpath = lc_uuid;
 
 			return 0;
 		}
@@ -2607,21 +2594,56 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t
 			return -ENODEV;
 
 		/* ...find the desired information by mtd udev_device */
-		return udev_parse_mtd(partdev, devpath, size);
+		return udev_parse_mtd(partdev, &cdev->devpath, &cdev->size);
 	}
 
 	if (udev_device_is_eeprom(dev)) {
-		ret = udev_parse_eeprom(dev, devpath);
+		ret = udev_parse_eeprom(dev, &cdev->devpath);
 		if (ret)
 			return ret;
 
-		return of_parse_partition(partition_node, offset, size);
+		return of_parse_partition(partition_node, &cdev->offset, &cdev->size);
 	} else {
-		ret = device_find_block_device(dev, devpath);
+		ret = device_find_block_device(dev, &cdev->devpath);
 		if (ret)
 			return ret;
-		return of_parse_partition(partition_node, offset, size);
+		return of_parse_partition(partition_node, &cdev->offset, &cdev->size);
 	}
 
 	return -EINVAL;
 }
+
+/*
+ * of_get_devicepath - 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.
+ */
+int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t *offset,
+		size_t *size)
+{
+	struct cdev cdev = {};
+	int ret;
+
+	ret = __of_cdev_find(partition_node, &cdev);
+	if (ret)
+		return ret;
+
+	*offset = cdev.offset;
+	*size = cdev.size;
+	*devpath = cdev.devpath;
+
+	return 0;
+}
-- 
2.39.2




  parent reply	other threads:[~2023-05-31 15:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 15:22 [OSS-Tools] [PATCH 0/8] state: allow lookup of barebox state partition by Type GUID Ahmad Fatoum
2023-05-31 15:22 ` [OSS-Tools] [PATCH 1/8] state: backend: direct: open block device in read-only mode if possible Ahmad Fatoum
2023-05-31 15:22 ` [OSS-Tools] [PATCH 2/8] libdt: factor out u64 sysattr parsing into helper Ahmad Fatoum
2023-06-02 13:16   ` Roland Hieber
2023-06-02 13:30     ` Roland Hieber
2023-05-31 15:22 ` [OSS-Tools] [PATCH 3/8] libdt: drop broken if-branch Ahmad Fatoum
2023-06-07  9:02   ` Uwe Kleine-König
2023-05-31 15:22 ` Ahmad Fatoum [this message]
2023-05-31 15:22 ` [OSS-Tools] [PATCH 5/8] libdt: use block device partition instead of parent if found Ahmad Fatoum
2023-06-05  8:37   ` Roland Hieber
2023-05-31 15:22 ` [OSS-Tools] [PATCH 6/8] state: align with barebox use of of_cdev_find Ahmad Fatoum
2023-05-31 15:22 ` [OSS-Tools] [PATCH 7/8] libdt: use of_find_device_by_uuid for partuuid lookup Ahmad Fatoum
2023-05-31 15:22 ` [OSS-Tools] [PATCH 8/8] state: allow lookup of barebox state partition by Type GUID Ahmad Fatoum

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=20230531152253.1407395-5-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