From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Wed, 31 May 2023 17:23:01 +0200 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q4NfC-004QQU-Ug for lore@lore.pengutronix.de; Wed, 31 May 2023 17:23:01 +0200 Received: from localhost ([127.0.0.1] helo=metis.ext.pengutronix.de) by metis.ext.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1q4Nf9-0005ke-15; Wed, 31 May 2023 17:22:59 +0200 Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q4Nf6-0005f7-Nk; Wed, 31 May 2023 17:22:56 +0200 Received: from [2a0a:edc0:0:1101:1d::54] (helo=dude05.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1q4Nf6-0049Ax-33; Wed, 31 May 2023 17:22:56 +0200 Received: from afa by dude05.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1q4Nf4-005uDy-B7; Wed, 31 May 2023 17:22:54 +0200 From: Ahmad Fatoum To: oss-tools@pengutronix.de Date: Wed, 31 May 2023 17:22:49 +0200 Message-Id: <20230531152253.1407395-5-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531152253.1407395-1-a.fatoum@pengutronix.de> References: <20230531152253.1407395-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [OSS-Tools] [PATCH 4/8] libdt: factor out __of_cdev_find helper X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false 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.
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 --- 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 #include +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