From patchwork Thu Jan 7 15:50:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 59290 Delivered-To: patches@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp632499lbb; Thu, 7 Jan 2016 07:50:37 -0800 (PST) X-Received: by 10.194.243.6 with SMTP id wu6mr112385945wjc.14.1452181830256; Thu, 07 Jan 2016 07:50:30 -0800 (PST) Return-Path: Received: from mail-wm0-x22d.google.com (mail-wm0-x22d.google.com. [2a00:1450:400c:c09::22d]) by mx.google.com with ESMTPS id d188si20539808wmc.105.2016.01.07.07.50.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jan 2016 07:50:30 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22d as permitted sender) client-ip=2a00:1450:400c:c09::22d; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22d as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-wm0-x22d.google.com with SMTP id b14so130022749wmb.1 for ; Thu, 07 Jan 2016 07:50:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kV6N27qiuswlag2H1s2evzjNV+nHFfWWVrGZ4zJiJ3s=; b=Mu5PdS9fFfDMireLgbSS1zrMoHov9253DoSIJHPMX/2VGocJau62h+Gb0+tPJM7Au0 7IpHvg8Dg/PzsatfcvWTTAZ5Fv9KQwVgbngGoQ2/+TxMClGsiEcIzL+GDzpe3EF+vjQt u6guUUTtuP4eQfPxgyxAGVo9J8rjy3lvk+P1k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kV6N27qiuswlag2H1s2evzjNV+nHFfWWVrGZ4zJiJ3s=; b=C7LSOp0WBOOX+segVmv8z1i6ONhja3vvPXlX2LSK/ZEZshPS+oDatO7+hBX+kfiU0i 4ycfG+MBJr/ev356XtiBLat3nzFiv5QMaOZPAetYEqRatwUmSTU1T92Fnc8RLobZ+8+9 TUQo9AfP+cLheJOiP3oL9Yc/NDee78fahmAZJEPlF4Kv81MHMtrRqZEI5IhvIYfjpeWU Y6AAdmnahVIF8aF6rpGJJ1GZL4r0apjqRaGuYvfgUlFtV4QrUTgXny7gTDYbN9dutS6I Pu0fnVoNxR+teHvSuun/QZ8jQQwKhyJM8YuyVOUpKHhbPgSG7q0BMDCz+8a6lG0wQqWY Xnbg== X-Gm-Message-State: ALoCoQnFpOpqwHaDeKwz4pGslZZt+zjLlKtWtplIaFhvPcRI01Ry2pHolQ8y6nLkWP8XJCD78MRg4qH2KsIZpDPtU1udjB35SQ== X-Received: by 10.28.73.135 with SMTP id w129mr18628768wma.55.1452181830002; Thu, 07 Jan 2016 07:50:30 -0800 (PST) Return-Path: Received: from new-host-8.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id e198sm14267576wmd.0.2016.01.07.07.50.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 Jan 2016 07:50:28 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, david@gibson.dropbear.id.au, alex.williamson@redhat.com Cc: alex.bennee@linaro.org, thuth@redhat.com, crosthwaitepeter@gmail.com, patches@linaro.org, christoffer.dall@linaro.org, pbonzini@redhat.com, b.reynal@virtualopensystems.com, suravee.suthikulpanit@amd.com, thomas.lendacky@amd.com Subject: [PATCH v3 3/8] device_tree: introduce qemu_fdt_node_path Date: Thu, 7 Jan 2016 15:50:10 +0000 Message-Id: <1452181815-1948-4-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1452181815-1948-1-git-send-email-eric.auger@linaro.org> References: <1452181815-1948-1-git-send-email-eric.auger@linaro.org> This new helper routine returns the node path of a device referred to by its node name and compat string. Signed-off-by: Eric Auger --- v1 -> v2: - move doc comment in header file - do not use a fixed size buffer - break on errors in while loop - use strcmp instead of strncmp RFC -> v1: - improve error handling according to Alex' comments --- device_tree.c | 37 +++++++++++++++++++++++++++++++++++++ include/sysemu/device_tree.h | 14 ++++++++++++++ 2 files changed, 51 insertions(+) -- 1.9.1 diff --git a/device_tree.c b/device_tree.c index b262c2d..8441e01 100644 --- a/device_tree.c +++ b/device_tree.c @@ -231,6 +231,43 @@ static int findnode_nofail(void *fdt, const char *node_path) return offset; } +int qemu_fdt_node_path(void *fdt, const char *name, char *compat, + char **node_path) +{ + int offset, len, ret; + const char *iter_name; + unsigned int path_len = 16; + char *path; + + *node_path = NULL; + 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)) { + goto found; + } + offset = fdt_node_offset_by_compatible(fdt, offset, compat); + } + return offset; + +found: + 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); + } + if (!ret) { + *node_path = path; + } + return ret; +} + 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 fdf25a4..269cb1c 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -20,6 +20,20 @@ void *load_device_tree(const char *filename_path, int *sizep); void *load_device_tree_from_sysfs(void); #endif +/** + * qemu_fdt_node_path: return the node path of a device, given its + * node name and its compat string + * @fdt: pointer to the dt blob + * @name: device node name + * @compat: compatibility string of the device + * @node_path: returned node path + * + * upon success, the path is output at node_path address + * returns 0 on success, < 0 on failure + */ +int qemu_fdt_node_path(void *fdt, const char *name, char *compat, + char **node_path); + 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,