From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Wed, 07 Jun 2023 14:16:36 +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 1q6s5d-00EJuC-Ra for lore@lore.pengutronix.de; Wed, 07 Jun 2023 14:16:36 +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 1q6s5Z-00069r-R2; Wed, 07 Jun 2023 14:16:33 +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 1q6s5X-00065p-PD; Wed, 07 Jun 2023 14:16:31 +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 1q6s5X-005jmT-4P; Wed, 07 Jun 2023 14:16:31 +0200 Received: from afa by dude05.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1q6s5V-00DNCf-L4; Wed, 07 Jun 2023 14:16:29 +0200 From: Ahmad Fatoum To: oss-tools@pengutronix.de Date: Wed, 7 Jun 2023 14:16:26 +0200 Message-Id: <20230607121628.3180139-7-a.fatoum@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230607121628.3180139-1-a.fatoum@pengutronix.de> References: <20230607121628.3180139-1-a.fatoum@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [OSS-Tools] [PATCH v2 6/8] state: align with barebox use of of_cdev_find 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 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 --- 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