From patchwork Thu Nov 19 15:22:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 57021 Delivered-To: patches@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp3244764lbb; Thu, 19 Nov 2015 07:22:22 -0800 (PST) X-Received: by 10.28.126.197 with SMTP id z188mr11881648wmc.88.1447946537347; Thu, 19 Nov 2015 07:22:17 -0800 (PST) Return-Path: Received: from mail-wm0-x22b.google.com (mail-wm0-x22b.google.com. [2a00:1450:400c:c09::22b]) by mx.google.com with ESMTPS id kp6si11901687wjc.145.2015.11.19.07.22.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2015 07:22:17 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22b as permitted sender) client-ip=2a00:1450:400c:c09::22b; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22b as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro-org.20150623.gappssmtp.com Received: by wmdw130 with SMTP id w130so244787633wmd.0 for ; Thu, 19 Nov 2015 07:22:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hpTeZ1nLwus4mFC+VMfCT0FchFktG0QuPXzWyhLBgRQ=; b=Fhm08TrTTiOXfRWzklmySlx/qp1v4Y2M94/AXlKUM+g1/KGOHVbdRzmxCDKZALWBXn NHLi7uUY4NmsgTBGSy0uUSpM4j6GxZcaN8CFzCNc0AxmvcvWBdW4g1z597vmPoLFV9Vr 0cHHuFgNR5Xg+fbfveGOYXjCCB/ylH/BBb8hBQK9OhRclHciSVwyA3RqWGyqOWz7dkMf FT3K6LGwJ+BPzZWhiVgyw+PTWzAn5VZurPDnikmh/22jMVrM4oASSS55/xHUydsPZxZp MJgDCrDw906CKn2K3idle02IEq+yEiMzNGclXWcmjV1vKVEbuJlFjmUQsgTsB+luyzCu 5u6g== 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=hpTeZ1nLwus4mFC+VMfCT0FchFktG0QuPXzWyhLBgRQ=; b=MV0ys2nA5AT81Xe43C+xEdL2ssqKkIC1ukrYpwJO2fuU9XTYw9oDPBGElsom1nZh66 NAqYPNgT79eOwO7dAxo+Rkho72/1M4b2tA8zFpuFFvM3Zov5WiiouwiHtgEwZLXMlblH /j1j1QaQ2pMgTA1EcgJedhLEyZw1LwOkTsBHVhSwDq5TWgZiGVu2nX0xXKgl0jNvi7lH xLDqUm+p1NJxHQk6TmqRHzPB+DXlG4vP+5VRP2yEYQYNydB9d7tPbTJEavArD7qzHYJz 6yv5MolSEEXXx+ai4pth9BBXDwGHO4FQ6VzCSfWGShOMyTMMcpYRewpxnDDEkpMC+Hxn YPoQ== X-Gm-Message-State: ALoCoQnLxEpa9OC8EPCot3AqceSDBPnYGisj95yxQZT2INhwe94fhVK2AwXC7914yhAMbhiPjplH X-Received: by 10.28.23.83 with SMTP id 80mr1044061wmx.78.1447946537121; Thu, 19 Nov 2015 07:22:17 -0800 (PST) Return-Path: Received: from midway01-04-00.lavalab ([81.128.185.50]) by smtp.gmail.com with ESMTPSA id vr10sm8367221wjc.38.2015.11.19.07.22.16 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 19 Nov 2015 07:22:16 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, peter.maydell@linaro.org Cc: patches@linaro.org, christoffer.dall@linaro.org, pbonzini@redhat.com, b.reynal@virtualopensystems.com, suravee.suthikulpanit@amd.com, thomas.lendacky@amd.com Subject: [RESEND RFC 2/6] device_tree: introduce load_device_tree_from_sysfs Date: Thu, 19 Nov 2015 15:22:04 +0000 Message-Id: <1447946528-1533-3-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1447946528-1533-1-git-send-email-eric.auger@linaro.org> References: <1447946528-1533-1-git-send-email-eric.auger@linaro.org> This function returns the host device tree blob from sysfs (/sys/firmware/devicetree/base). This has a runtime dependency on the dtc binary. This functionality is useful for platform device passthrough where the host device tree needs to be parsed to feed information into the guest device tree. Signed-off-by: Eric Auger --- device_tree.c | 40 ++++++++++++++++++++++++++++++++++++++++ include/sysemu/device_tree.h | 1 + 2 files changed, 41 insertions(+) -- 1.8.3.2 diff --git a/device_tree.c b/device_tree.c index a9f5f8e..58a5329 100644 --- a/device_tree.c +++ b/device_tree.c @@ -117,6 +117,46 @@ fail: return NULL; } +/** + * load_device_tree_from_sysfs + * + * extract the dt blob from host sysfs + * this has a runtime dependency on the dtc binary + */ +void *load_device_tree_from_sysfs(void) +{ + char cmd[] = "dtc -I fs -O dtb /sys/firmware/devicetree/base"; + FILE *pipe; + void *fdt; + int ret, actual_dt_size; + + pipe = popen(cmd, "r"); + if (!pipe) { + error_report("%s: Error when executing dtc", __func__); + return NULL; + } + fdt = g_malloc0(FDT_MAX_SIZE); + actual_dt_size = fread(fdt, 1, FDT_MAX_SIZE, pipe); + pclose(pipe); + + if (actual_dt_size == 0) { + error_report("%s: could not copy host device tree in memory: %m", + __func__); + goto fail; + } + ret = fdt_check_header(fdt); + if (ret) { + error_report("%s: Host dt file loaded into memory is invalid: %s", + __func__, fdt_strerror(ret)); + goto fail; + } + return fdt; + +fail: + g_free(fdt); + return NULL; +} + 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);