From patchwork Fri Jun 29 14:52:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 140563 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp953244ljj; Fri, 29 Jun 2018 08:02:50 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeE84o2jRr2ARSEexMITKuCTkMM0uxuCFX157Ttg0ipxWMSWyk0CttwDZ0W961TNJrJVQaB X-Received: by 2002:a37:9986:: with SMTP id b128-v6mr13084694qke.288.1530284570767; Fri, 29 Jun 2018 08:02:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530284570; cv=none; d=google.com; s=arc-20160816; b=T9OlAqj+6MUHtJT0nY/N/i/YuvHMAgx5RAzhnUM5CBr3TTn0Vs8zyzW1dFMaKXnjzY 54E69S2Mm2Z/uWniDOObP1cKvTOE0DvgonRkGKSoCrh7uk7fl66AZ8qKklIrPkLaADm3 r5M6Q/B+V8EdrMh5DPbVlVAZZ8Ym+5PA3gWES4O2KFq93FrXR2qkEBIOYDgZYbHzXVL1 Di+TJqUUBv5YEd7quRn7vs8JFnvssKnNK6NY4GdSsoyIGOLzmFRhd2Op62UL6T2MB/ti WX9EeQcUio/ktNTEhDG2y9C7x0lF2oHdqQ/b5UKxLofLzGkmi1edM1e5/V7L+k7JsGlc cPOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:arc-authentication-results; bh=xlEko0pGMoc1QclBaJr3qOGnG5NwTUd+NvjVMOHH64M=; b=iM1/zVObmVsz2aVT/jSHd5/l3cC+7pis8PjV9FcObMPnqHNtBycGCCe6k+yrz772wM t6LWDrvIAgKvhMyA0GRWcJkaKPEw2Jycqd+Gm3KlLKFz04PLAoTpocyM0gN7Z58Bt2Rb Nohza0dT8ocC1gQ5AEoNEpr2orbyuuD7qoMd9E7cMkPwtCjuLAONY1Qwuu11gx/X/sg+ 4USDxuIkr35Q4H6AmWmZ/RzT83atI6rlwMP+LeXZrIbPljYsch4AAI8Z2wh6pjFEa8R4 vwYXnD4szlB+NTp0KNPDsOFVWJoH2xLT7vJ9/mtDd0BTQFerkO+5Nq2S0vFQM470xWup yhPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id v84-v6si9377439qkl.344.2018.06.29.08.02.50 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 29 Jun 2018 08:02:50 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:42817 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fYuvC-0006KX-4O for patch@linaro.org; Fri, 29 Jun 2018 11:02:50 -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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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(+) -- 2.17.1 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); +/** + * 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; } +char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp) +{ + char *prefix = g_strdup_printf("%s@", name); + unsigned int path_len = 16, n = 0; + GSList *path_list = NULL, *iter; + const char *iter_name; + int offset, len, ret; + char **path_array; + + offset = fdt_next_node(fdt, -1, NULL); + + while (offset >= 0) { + iter_name = fdt_get_name(fdt, offset, &len); + if (!iter_name) { + offset = len; + break; + } + if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) { + 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_next_node(fdt, offset, NULL); + } + g_free(prefix); + + if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { + error_setg(errp, "%s: abort parsing dt for %s node units: %s", + __func__, name, 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; +} + char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, Error **errp) {