From patchwork Thu Dec 17 12:29:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 558308 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 C75AE1401DE for ; Thu, 17 Dec 2015 23:30:15 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=ggMHSAfi; dkim-atps=neutral Received: from localhost ([::1]:52959 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9XhF-0000s9-Fp for incoming@patchwork.ozlabs.org; Thu, 17 Dec 2015 07:30:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44134) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9XgX-0008El-V1 for qemu-devel@nongnu.org; Thu, 17 Dec 2015 07:29:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a9XgW-0006H2-NF for qemu-devel@nongnu.org; Thu, 17 Dec 2015 07:29:29 -0500 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]:36252) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a9XgW-0006Gq-En for qemu-devel@nongnu.org; Thu, 17 Dec 2015 07:29:28 -0500 Received: by mail-wm0-x236.google.com with SMTP id p187so18892483wmp.1 for ; Thu, 17 Dec 2015 04:29:28 -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=vrtF2W3QZ2yvOVCcuVDW+pFnjprd1j0WUqLMbQ/QYlE=; b=ggMHSAfibYwn5aRReVsSLc+uCbFuJhhSdOJyFwXBKBpVA0NMaXJoplQLthGnF6GdvW XJT+2IRazbq7ueENi8OEz4Fu7eqy78zFVFmWfobpWNB/vQ2hLezyR94ZintFTEJn4EXi YDp9+iRGSEIX6SZpEGZhJliJ0pLLhmYneBALY= 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=vrtF2W3QZ2yvOVCcuVDW+pFnjprd1j0WUqLMbQ/QYlE=; b=EPDGgGFVuETj6U/Zpn6dsjU85kHAXilehAD/+K28IXZut55j09d5vMRIZSBzTUWoLC m3A83FUOp3Rsvmn3QcbNCBoUdg8BC6TimIGLzVVc9iGO27C6MTdEWXsqCr9Trt0XFCVP 22YyL8EPfMvuqp6e20jDJgnwPPa7e5Ov/Epbr+XxEDUlDvi/rDEACvS5k65cQHwvV6cd Pm/VzynTxJ6uozGX57hoZyApkuS2ItEM1/KFWQLcRCZgXJLEfLgvAtbN286VlOYEXwQo 66CyyYBO2PdrV6opdjGeHCqkG4R4XR6t2V14eDCj3qp62FN/SAW2YDPsu9AUjteamKOk CKaA== X-Gm-Message-State: ALoCoQksvqKpOSIKK7cJjE8a8lY3eh2zO3Ucfx2sqU+25p3V9x4eh6Fi1Sd4Bxe3fri8wyoswJshKIrdaNvSdafdQDY8HVYOWg== X-Received: by 10.28.3.133 with SMTP id 127mr3996584wmd.101.1450355367890; Thu, 17 Dec 2015 04:29:27 -0800 (PST) Received: from new-host-17.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id bh6sm10313963wjb.0.2015.12.17.04.29.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Dec 2015 04:29:26 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, qemu-devel@nongnu.org, peter.maydell@linaro.org, david@gibson.dropbear.id.au, alex.williamson@redhat.com Date: Thu, 17 Dec 2015 12:29:23 +0000 Message-Id: <1450355367-14818-3-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1450355367-14818-1-git-send-email-eric.auger@linaro.org> References: <1450355367-14818-1-git-send-email-eric.auger@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::236 Cc: thomas.lendacky@amd.com, thuth@redhat.com, b.reynal@virtualopensystems.com, patches@linaro.org, crosthwaitepeter@gmail.com, suravee.suthikulpanit@amd.com, pbonzini@redhat.com, alex.bennee@linaro.org, christoffer.dall@linaro.org Subject: [Qemu-devel] [PATCH 2/6] device_tree: introduce load_device_tree_from_sysfs 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 This function returns the host device tree blob from sysfs (/sys/firmware/devicetree/base). It uses a recursive function inspired from dtc read_fstree. Signed-off-by: Eric Auger --- RFC -> v1: - remove runtime dependency on dtc binary and introduce read_fstree --- device_tree.c | 102 +++++++++++++++++++++++++++++++++++++++++++ include/sysemu/device_tree.h | 1 + 2 files changed, 103 insertions(+) diff --git a/device_tree.c b/device_tree.c index a9f5f8e..e556a99 100644 --- a/device_tree.c +++ b/device_tree.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "qemu-common.h" #include "qemu/error-report.h" @@ -117,6 +118,107 @@ fail: return NULL; } +/** + * read_fstree: this function is inspired from dtc read_fstree + * @fdt: preallocated fdt blob buffer, to be populated + * @dirname: directory to scan under /sys/firmware/devicetree/base + * the search is recursive and the tree is search down to the + * leafs (property files). + * + * the function self-asserts in case of error + */ +static void read_fstree(void *fdt, const char *dirname) +{ + DIR *d; + struct dirent *de; + struct stat st; + const char *root_dir = "/sys/firmware/devicetree/base"; + char *parent_node; + + if (strstr(dirname, root_dir) != dirname) { + error_report("%s: %s must be searched within %s", + __func__, dirname, root_dir); + exit(1); + } + parent_node = (char *)&dirname[29]; + + d = opendir(dirname); + if (!d) { + error_report("%s cannot open %s", __func__, dirname); + exit(1); + } + + while ((de = readdir(d)) != NULL) { + char *tmpnam; + + if (!g_strcmp0(de->d_name, ".") + || !g_strcmp0(de->d_name, "..")) { + continue; + } + + tmpnam = g_strjoin("/", dirname, de->d_name, NULL); + + if (lstat(tmpnam, &st) < 0) { + error_report("%s cannot lstat %s", __func__, tmpnam); + exit(1); + } + + if (S_ISREG(st.st_mode)) { + int ret, size = st.st_size; + void *val = g_malloc0(size); + FILE *pfile; + + pfile = fopen(tmpnam, "r"); + if (!pfile) { + error_report("%s cannot open %s", __func__, tmpnam); + exit(1); + } + ret = fread(val, 1, size, pfile); + if (ferror(pfile) || ret < size) { + error_report("%s fail reading %s", __func__, tmpnam); + exit(1); + } + fclose(pfile); + + if (strlen(parent_node) > 0) { + qemu_fdt_setprop(fdt, parent_node, + de->d_name, val, size); + } else { + qemu_fdt_setprop(fdt, "/", de->d_name, val, size); + } + g_free(val); + } else if (S_ISDIR(st.st_mode)) { + char *node_name; + + node_name = g_strdup_printf("%s/%s", + parent_node, de->d_name); + qemu_fdt_add_subnode(fdt, node_name); + g_free(node_name); + read_fstree(fdt, tmpnam); + } + + g_free(tmpnam); + } + + closedir(d); +} + +/* load_device_tree_from_sysfs: extract the dt blob from host sysfs */ +void *load_device_tree_from_sysfs(void) +{ + void *host_fdt; + int host_fdt_size; + + host_fdt = create_device_tree(&host_fdt_size); + read_fstree(host_fdt, "/sys/firmware/devicetree/base"); + if (fdt_check_header(host_fdt)) { + error_report("%s host device tree extracted into memory is invalid", + __func__); + g_free(host_fdt); + } + return host_fdt; +} + static int findnode_nofail(void *fdt, const char *node_path) { int offset; diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h index 359e143..307e53d 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -16,6 +16,7 @@ void *create_device_tree(int *sizep); void *load_device_tree(const char *filename_path, int *sizep); +void *load_device_tree_from_sysfs(void); int qemu_fdt_setprop(void *fdt, const char *node_path, const char *property, const void *val, int size);