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.oelm...@pengutronix.de> Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de> --- src/libdt.c | 62 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/libdt.c b/src/libdt.c index 6f1a9e309d53..7d03ac1d4394 100644 --- a/src/libdt.c +++ b/src/libdt.c @@ -2159,23 +2159,32 @@ 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 * * @dev: the udev_device to extract information from * @devpath: returns the devicepath under which the block device is accessible + * @start: start of partition in bytes + * @size: size of partition in bytes * - * returns 0 for success or negative error value on failure. + * returns 0 for success or negative error value on failure. Start + * is adjusted if a suitable partition is found within the parent block device. */ int device_find_block_device(struct udev_device *dev, - char **devpath) + char **devpath, off_t *start, size_t size) { 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(); @@ -2199,19 +2208,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 (!size) + break; + } else if (start && 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(*start, *start + size, + partstart, partstart + partsize)) + continue; + + best_match = part; + *start -= partstart; + break; } } - ret = -ENODEV; -out: + if (best_match) + *devpath = strdup(udev_device_get_devnode(best_match)); + udev_enumerate_unref(enumerate); udev_unref(udev); - return ret; + return best_match ? 0 : -ENODEV; } /* @@ -2428,7 +2461,7 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t return udev_parse_eeprom(dev, devpath); } else if (!udev_parse_mtd(dev, devpath, size)) { return 0; - } else if (device_find_block_device(dev, devpath)) { + } else if (device_find_block_device(dev, devpath, NULL, 0)) { return of_parse_partition(partition_node, offset, size); } @@ -2505,10 +2538,11 @@ int of_get_devicepath(struct device_node *partition_node, char **devpath, off_t return of_parse_partition(partition_node, offset, size); } else { - ret = device_find_block_device(dev, devpath); + ret = of_parse_partition(partition_node, offset, size); if (ret) return ret; - return of_parse_partition(partition_node, offset, size); + + return device_find_block_device(dev, devpath, offset, *size); } return -EINVAL; -- 2.39.2