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 5/8] libdt: use block device partition instead of parent if found
Date: Wed,  7 Jun 2023 14:16:25 +0200	[thread overview]
Message-ID: <20230607121628.3180139-6-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230607121628.3180139-1-a.fatoum@pengutronix.de>

Linux doesn't parse MTD fixed-partitions described in the device tree
for block devices and EEPROMs. For block devices, The dt-utils work around
this by doing the lookup in userspace and then arrive at the parent block
device along with an offset that is passed along. We can do better
though: If there's a partition block device that contains the region
we are interested in, we should just be using that.

This avoids the issue of triggering a reread of the partition table
after writing barebox state because udev is notified that we had
been opening the whole block device writable.

Tested-by: Ulrich Ölmann <u.oelmann@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 src/libdt.c | 60 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 45 insertions(+), 15 deletions(-)

diff --git a/src/libdt.c b/src/libdt.c
index c8a8ea43e452..1b381ed8915b 100644
--- a/src/libdt.c
+++ b/src/libdt.c
@@ -2212,23 +2212,29 @@ static int udev_device_parse_sysattr_u64(struct udev_device *dev, const char *at
 	return 0;
 }
 
+/* True if A completely contains B */
+static bool region_contains(loff_t starta, loff_t enda,
+			    loff_t startb, loff_t endb)
+{
+        return starta <= startb && enda >= endb;
+}
+
 /*
- * device_find_block_device - extract device path from udev block device
+ * cdev_from_block_device - Populate cdev from udev block device
  *
  * @dev:	the udev_device to extract information from
- * @devpath:	returns the devicepath under which the block device is accessible
+ * @cdev:	the cdev output parameter to populate
  *
  * returns 0 for success or negative error value on failure.
  */
-int device_find_block_device(struct udev_device *dev,
-		char **devpath)
+static int cdev_from_block_device(struct udev_device *dev,
+				  struct cdev *cdev)
 {
 
 	struct udev *udev;
 	struct udev_enumerate *enumerate;
 	struct udev_list_entry *devices, *dev_list_entry;
-	struct udev_device *part;
-	const char *outpath;
+	struct udev_device *part, *best_match = NULL;
 	int ret;
 
 	udev = udev_new();
@@ -2252,19 +2258,43 @@ int device_find_block_device(struct udev_device *dev,
 		if (!devtype)
 			continue;
 		if (!strcmp(devtype, "disk")) {
-			outpath = udev_device_get_devnode(part);
-			*devpath = strdup(outpath);
-			ret = 0;
-			goto out;
+			best_match = part;
+
+			/* Should we try to find a matching partition first? */
+			if (!cdev->size)
+				break;
+		} else if (cdev->size && !strcmp(devtype, "partition")) {
+			u64 partstart, partsize;
+
+			ret = udev_device_parse_sysattr_u64(part, "start", &partstart);
+			if (ret)
+				continue;
+
+			ret = udev_device_parse_sysattr_u64(part, "size", &partsize);
+			if (ret)
+				continue;
+
+			/* start/size sys attributes are always in 512-byte units */
+			partstart *= 512;
+			partsize *= 512;
+
+			if (!region_contains(partstart, partstart + partsize,
+					     cdev->offset, cdev->offset + cdev->size))
+				continue;
+
+			best_match = part;
+			cdev->offset -= partstart;
+			break;
 		}
 	}
-	ret = -ENODEV;
 
-out:
+	if (best_match)
+		cdev->devpath = strdup(udev_device_get_devnode(best_match));
+
 	udev_enumerate_unref(enumerate);
 	udev_unref(udev);
 
-	return ret;
+	return best_match ? 0 : -ENODEV;
 }
 
 /*
@@ -2606,10 +2636,10 @@ static int __of_cdev_find(struct device_node *partition_node, struct cdev *cdev)
 
 		return of_parse_partition(partition_node, &cdev->offset, &cdev->size);
 	} else {
-		ret = device_find_block_device(dev, &cdev->devpath);
+		ret = of_parse_partition(partition_node, &cdev->offset, &cdev->size);
 		if (ret)
 			return ret;
-		return of_parse_partition(partition_node, &cdev->offset, &cdev->size);
+		return cdev_from_block_device(dev, cdev);
 	}
 
 	return -EINVAL;
-- 
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 ` Ahmad Fatoum [this message]
2023-06-07 12:16 ` [OSS-Tools] [PATCH v2 6/8] state: align with barebox use of of_cdev_find Ahmad Fatoum
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-6-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