From patchwork Fri Feb 19 17:40:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 585346 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DA276140775 for ; Sat, 20 Feb 2016 04:44:25 +1100 (AEDT) Received: from localhost ([::1]:54198 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWp6N-0003ZF-VY for incoming@patchwork.ozlabs.org; Fri, 19 Feb 2016 12:44:23 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44105) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWp2S-0004GE-ML for qemu-devel@nongnu.org; Fri, 19 Feb 2016 12:40:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWp2R-0006g5-M4 for qemu-devel@nongnu.org; Fri, 19 Feb 2016 12:40:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59814) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWp2R-0006fu-DE for qemu-devel@nongnu.org; Fri, 19 Feb 2016 12:40:19 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id B9B982F1497; Fri, 19 Feb 2016 17:40:18 +0000 (UTC) Received: from gimli.home (ovpn-113-102.phx2.redhat.com [10.3.113.102]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u1JHeICT011660; Fri, 19 Feb 2016 12:40:18 -0500 From: Alex Williamson To: qemu-devel@nongnu.org Date: Fri, 19 Feb 2016 10:40:17 -0700 Message-ID: <20160219174017.16497.33872.stgit@gimli.home> In-Reply-To: <20160219173708.16497.23357.stgit@gimli.home> References: <20160219173708.16497.23357.stgit@gimli.home> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Eric Auger Subject: [Qemu-devel] [PULL 08/14] device_tree: introduce qemu_fdt_node_path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Eric Auger This new helper routine returns a NULL terminated array of node paths matching a node name and a compat string. Signed-off-by: Eric Auger Reviewed-by: Peter Maydell Signed-off-by: Alex Williamson --- device_tree.c | 54 ++++++++++++++++++++++++++++++++++++++++++ include/sysemu/device_tree.h | 18 ++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/device_tree.c b/device_tree.c index 9e77c69..b29a433 100644 --- a/device_tree.c +++ b/device_tree.c @@ -226,6 +226,60 @@ static int findnode_nofail(void *fdt, const char *node_path) return offset; } +char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, + Error **errp) +{ + int offset, len, ret; + const char *iter_name; + unsigned int path_len = 16, n = 0; + GSList *path_list = NULL, *iter; + char **path_array; + + offset = fdt_node_offset_by_compatible(fdt, -1, compat); + + while (offset >= 0) { + iter_name = fdt_get_name(fdt, offset, &len); + if (!iter_name) { + offset = len; + break; + } + if (!strcmp(iter_name, name)) { + char *path; + + path = g_malloc(path_len); + while ((ret = fdt_get_path(fdt, offset, path, path_len)) + == -FDT_ERR_NOSPACE) { + path_len += 16; + path = g_realloc(path, path_len); + } + path_list = g_slist_prepend(path_list, path); + n++; + } + offset = fdt_node_offset_by_compatible(fdt, offset, compat); + } + + if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { + error_setg(errp, "%s: abort parsing dt for %s/%s: %s", + __func__, name, compat, fdt_strerror(offset)); + for (iter = path_list; iter; iter = iter->next) { + g_free(iter->data); + } + g_slist_free(path_list); + return NULL; + } + + path_array = g_new(char *, n + 1); + path_array[n--] = NULL; + + for (iter = path_list; iter; iter = iter->next) { + path_array[n--] = iter->data; + } + + g_slist_free(path_list); + + return path_array; +} + int qemu_fdt_setprop(void *fdt, const char *node_path, const char *property, const void *val, int size) { diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h index 62093ba..552df21 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -25,6 +25,24 @@ void *load_device_tree(const char *filename_path, int *sizep); void *load_device_tree_from_sysfs(void); #endif +/** + * qemu_fdt_node_path: return the paths of nodes matching a given + * name and compat string + * @fdt: pointer to the dt blob + * @name: node name + * @compat: compatibility string + * @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_path(void *fdt, const char *name, char *compat, + 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,