From patchwork Thu Feb 18 17:45:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8353041 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 4C73D9F727 for ; Thu, 18 Feb 2016 17:48:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 796E52022D for ; Thu, 18 Feb 2016 17:48:51 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7B22A2022A for ; Thu, 18 Feb 2016 17:48:49 +0000 (UTC) Received: from localhost ([::1]:43802 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWSh6-0007qq-V7 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Feb 2016 12:48:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37670) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWSeQ-00036S-3H for qemu-devel@nongnu.org; Thu, 18 Feb 2016 12:46:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWSeO-0002vA-9Y for qemu-devel@nongnu.org; Thu, 18 Feb 2016 12:46:02 -0500 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]:35128) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWSeN-0002uN-RF for qemu-devel@nongnu.org; Thu, 18 Feb 2016 12:46:00 -0500 Received: by mail-wm0-x22c.google.com with SMTP id c200so39871622wme.0 for ; Thu, 18 Feb 2016 09:45:59 -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=9JcvusmsSYmq2iVVrVRlwyEMNqlP92VK7fOHUPeayJM=; b=SjLM9sPEcb4MGhyudKeA8i1ljhbCt75D2lGkMm7gMOjvpo3Uycu36AyeaQ55GhxeCW gWA3YpwXPGugQ0IHWHYTgpafB99a+aHMWt/wWIUAEtApGhEQ7a3DaXoMr7Z6wZsVtref HNVbHSinX91FOjjA536fyx5qhY6m8BXoisrrQ= 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=9JcvusmsSYmq2iVVrVRlwyEMNqlP92VK7fOHUPeayJM=; b=MB4IiJ6/opS35vil48m7it9Zh3wT3H3lZIcLvG03K4SSc2/NyE2YdF2BBZgmINXUYr cti1SH6isHH1zTGLfkjP0Na8+5+BbvRTht0mFgBSN3XSa5GT7aO1FIPejelJv7f75W6c ORLwigiyjFYdLuXdzwpWqtb0ie7C97PdljGdgXnpfFztCFSDeaTKBrACHaUWmNZagvTJ D+RHnnHJiFwRoIi4xxpHDJcLOtfVKyadG0sCQ90BQwlJStVh6sZAuAVWVwOieqSp9mkp yZszed2auJEYPhkTHjLPMtuuTPAMyUgZ1KNOjcrunz0IDUuRAN3UqxpJfQPs4eteP/rN JD8A== X-Gm-Message-State: AG10YOSUVqmga2ZiFdLXVF1Cmvd+HMyEILRENKtgghvoKYsvrdb3Z9USssaJa1mEPBLZU750 X-Received: by 10.195.13.98 with SMTP id ex2mr8617418wjd.45.1455817559195; Thu, 18 Feb 2016 09:45:59 -0800 (PST) Received: from midway01-04-00.lavalab ([81.128.185.50]) by smtp.gmail.com with ESMTPSA id ct2sm7592997wjb.46.2016.02.18.09.45.56 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Feb 2016 09:45:57 -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, alex.williamson@redhat.com, david@gibson.dropbear.id.au Date: Thu, 18 Feb 2016 17:45:40 +0000 Message-Id: <1455817546-6564-3-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1455817546-6564-1-git-send-email-eric.auger@linaro.org> References: <1455817546-6564-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::22c 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 v7 2/8] 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This function returns the host device tree blob from sysfs (/proc/device-tree). It uses a recursive function inspired from dtc read_fstree. Signed-off-by: Eric Auger Reviewed-by: Peter Maydell --- v6 -> v7: - rebase on d38ea87ac54af64ef611de434d07c12dc0399216: "all: Clean up includes" - add Peter's R-b v5 -> v6: - fix some spelling mistakes - error_report + exit replaced by error_setg - const char *parent_node; - use g_strdup_printf instead of g_strjoin - add a doc comment for load_device_tree_from_sysfs v1 -> v2: - do not implement/expose read_fstree and load_device_tree_from_sysfs if CONFIG_LINUX is not defined (lstat is not implemeted in mingw) - correct indentation in read_fstree - use /proc/device-tree symlink instead of /sys/firmware/devicetree/base path (kernel.org/doc/Documentation/ABI/testing/sysfs-firmware-ofw) - use g_file_get_contents in read_fstree - introduce SYSFS_DT_BASEDIR macro and use strlen - exit on error in load_device_tree_from_sysfs - user error_setg RFC -> v1: - remove runtime dependency on dtc binary and introduce read_fstree --- device_tree.c | 100 +++++++++++++++++++++++++++++++++++++++++++ include/sysemu/device_tree.h | 8 ++++ 2 files changed, 108 insertions(+) diff --git a/device_tree.c b/device_tree.c index b1ad836..9e77c69 100644 --- a/device_tree.c +++ b/device_tree.c @@ -13,6 +13,10 @@ #include "qemu/osdep.h" +#ifdef CONFIG_LINUX +#include +#endif + #include "qemu-common.h" #include "qemu/error-report.h" #include "sysemu/device_tree.h" @@ -112,6 +116,102 @@ fail: return NULL; } +#ifdef CONFIG_LINUX + +#define SYSFS_DT_BASEDIR "/proc/device-tree" + +/** + * read_fstree: this function is inspired from dtc read_fstree + * @fdt: preallocated fdt blob buffer, to be populated + * @dirname: directory to scan under SYSFS_DT_BASEDIR + * the search is recursive and the tree is searched down to the + * leaves (property files). + * + * the function 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 = SYSFS_DT_BASEDIR; + const char *parent_node; + + if (strstr(dirname, root_dir) != dirname) { + error_setg(&error_fatal, "%s: %s must be searched within %s", + __func__, dirname, root_dir); + } + parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)]; + + d = opendir(dirname); + if (!d) { + error_setg(&error_fatal, "%s cannot open %s", __func__, dirname); + } + + while ((de = readdir(d)) != NULL) { + char *tmpnam; + + if (!g_strcmp0(de->d_name, ".") + || !g_strcmp0(de->d_name, "..")) { + continue; + } + + tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name); + + if (lstat(tmpnam, &st) < 0) { + error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam); + } + + if (S_ISREG(st.st_mode)) { + gchar *val; + gsize len; + + if (!g_file_get_contents(tmpnam, &val, &len, NULL)) { + error_setg(&error_fatal, "%s not able to extract info from %s", + __func__, tmpnam); + } + + if (strlen(parent_node) > 0) { + qemu_fdt_setprop(fdt, parent_node, + de->d_name, val, len); + } else { + qemu_fdt_setprop(fdt, "/", de->d_name, val, len); + } + 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, SYSFS_DT_BASEDIR); + if (fdt_check_header(host_fdt)) { + error_setg(&error_fatal, + "%s host device tree extracted into memory is invalid", + __func__); + } + return host_fdt; +} + +#endif /* CONFIG_LINUX */ + 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..62093ba 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -16,6 +16,14 @@ void *create_device_tree(int *sizep); void *load_device_tree(const char *filename_path, int *sizep); +#ifdef CONFIG_LINUX +/** + * load_device_tree_from_sysfs: reads the device tree information in the + * /proc/device-tree directory and return the corresponding binary blob + * buffer pointer. Asserts in case of error. + */ +void *load_device_tree_from_sysfs(void); +#endif int qemu_fdt_setprop(void *fdt, const char *node_path, const char *property, const void *val, int size);