From nobody Thu Dec 18 19:31:06 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1530284676115146.1422320917909; Fri, 29 Jun 2018 08:04:36 -0700 (PDT) Received: from localhost ([::1]:42822 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fYuwt-0007Zb-DK for importer@patchew.org; Fri, 29 Jun 2018 11:04:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33720) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fYumh-0007NW-Bp for qemu-devel@nongnu.org; Fri, 29 Jun 2018 10:54:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fYume-0006IZ-Vq for qemu-devel@nongnu.org; Fri, 29 Jun 2018 10:54:03 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:43088) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fYume-00063g-Kd for qemu-devel@nongnu.org; Fri, 29 Jun 2018 10:54:00 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1fYumV-0004VM-MW for qemu-devel@nongnu.org; Fri, 29 Jun 2018 15:53:51 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 29 Jun 2018 15:52:56 +0100 Message-Id: <20180629145347.652-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180629145347.652-1-peter.maydell@linaro.org> References: <20180629145347.652-1-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PULL 04/55] device_tree: Add qemu_fdt_node_unit_path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Eric Auger This helper allows to retrieve the paths of nodes whose name match node-name or node-name@unit-address patterns. Signed-off-by: Eric Auger Message-id: 1530044492-24921-2-git-send-email-eric.auger@redhat.com Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- include/sysemu/device_tree.h | 16 +++++++++++ device_tree.c | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h index e22e5bec9c3..c16fd69bc0b 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -43,6 +43,22 @@ void *load_device_tree_from_sysfs(void); char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, Error **errp); =20 +/** + * qemu_fdt_node_unit_path: return the paths of nodes matching a given + * node-name, ie. node-name and node-name@unit-address + * @fdt: pointer to the dt blob + * @name: node name + * @errp: handle to an error object + * + * returns a newly allocated NULL-terminated array of node paths. + * Use g_strfreev() to free it. If one or more nodes were found, the + * array contains the path of each node and the last element equals to + * NULL. If there is no error but no matching node was found, the + * returned array contains a single element equal to NULL. If an error + * was encountered when parsing the blob, the function returns NULL + */ +char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp); + int qemu_fdt_setprop(void *fdt, const char *node_path, const char *property, const void *val, int size); int qemu_fdt_setprop_cell(void *fdt, const char *node_path, diff --git a/device_tree.c b/device_tree.c index 3553819257b..6d9c9726f66 100644 --- a/device_tree.c +++ b/device_tree.c @@ -232,6 +232,61 @@ static int findnode_nofail(void *fdt, const char *node= _path) return offset; } =20 +char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp) +{ + char *prefix =3D g_strdup_printf("%s@", name); + unsigned int path_len =3D 16, n =3D 0; + GSList *path_list =3D NULL, *iter; + const char *iter_name; + int offset, len, ret; + char **path_array; + + offset =3D fdt_next_node(fdt, -1, NULL); + + while (offset >=3D 0) { + iter_name =3D fdt_get_name(fdt, offset, &len); + if (!iter_name) { + offset =3D len; + break; + } + if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix= )) { + char *path; + + path =3D g_malloc(path_len); + while ((ret =3D fdt_get_path(fdt, offset, path, path_len)) + =3D=3D -FDT_ERR_NOSPACE) { + path_len +=3D 16; + path =3D g_realloc(path, path_len); + } + path_list =3D g_slist_prepend(path_list, path); + n++; + } + offset =3D fdt_next_node(fdt, offset, NULL); + } + g_free(prefix); + + if (offset < 0 && offset !=3D -FDT_ERR_NOTFOUND) { + error_setg(errp, "%s: abort parsing dt for %s node units: %s", + __func__, name, fdt_strerror(offset)); + for (iter =3D path_list; iter; iter =3D iter->next) { + g_free(iter->data); + } + g_slist_free(path_list); + return NULL; + } + + path_array =3D g_new(char *, n + 1); + path_array[n--] =3D NULL; + + for (iter =3D path_list; iter; iter =3D iter->next) { + path_array[n--] =3D iter->data; + } + + g_slist_free(path_list); + + return path_array; +} + char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, Error **errp) { --=20 2.17.1