From nobody Wed Oct 29 09:11:12 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1525396943188629.9665616786318; Thu, 3 May 2018 18:22:23 -0700 (PDT) Received: from localhost ([::1]:59887 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEPQS-00019a-1A for importer@patchew.org; Thu, 03 May 2018 21:22:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50251) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEPPX-0000qu-Qe for qemu-devel@nongnu.org; Thu, 03 May 2018 21:21:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fEPPU-0005YN-M7 for qemu-devel@nongnu.org; Thu, 03 May 2018 21:21:23 -0400 Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]:42248) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fEPPU-0005Xc-Fz for qemu-devel@nongnu.org; Thu, 03 May 2018 21:21:20 -0400 Received: by mail-pg0-x243.google.com with SMTP id p9-v6so11441027pgc.9 for ; Thu, 03 May 2018 18:21:20 -0700 (PDT) Received: from localhost.localdomain (122-58-167-38-fibre.bb.spark.co.nz. [122.58.167.38]) by smtp.gmail.com with ESMTPSA id c3sm24404814pfn.62.2018.05.03.18.21.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 03 May 2018 18:21:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=FFxGO0/Db3fzUIIuJF/BtvJa7lKD5OF8H5Xmh4Nv/Jw=; b=Fx6Tqe3BY2IHAtOqdPFbJeUzsOCn+D1ma/tnXWVKgmWZ1JBhyoY7/o3nGfBtk+PvOR vdeEWRGvrElGhe8G8ridTX+HFksnN+dc6lUEylw7HuHurzO/S+Td0m3cVnXwOfb3+feh mjuc4NStQv2hN1XOOuaLs15dMF1ZYriJwi3d9VJiZY1HYB+e6ct4cxwfK/s57rHnxZUZ YJZWhtQLisXDCwciw7El2q+RvTKI+SFmL7OLfh/HkHHtksX+nZsb6im6EqrWeMq4flSh iuip6Oe6anz1gIbgZ9bHxtd/lTHZ8YNSgfSNPHi31H+f38ug6PcE5wSuulT93+seTtUo pZwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=FFxGO0/Db3fzUIIuJF/BtvJa7lKD5OF8H5Xmh4Nv/Jw=; b=CCnXvbLmAZeeiOK/PhC4xdbUKYdLU1RVqag7Nb3DUtJ2T5w2IqgG0JJa3al0APiiTq paYQGSFjhHQ7uMyNdCWA4TyOgUdniWvtfhgFqCOGHlev3HtGItUWINCYgo+hdLPIfno0 j8JpissrzCSmYufM3mzbfalvwvuRPxWy9btjDYksHb1v0gFKXEOVBQzqQZ36tFYiz76v TfYIQ+8Z2cTYgRWgQW/nPH/SuWZ6UPYQC42Dd6DWSEJRce9FIl4a/QDf12e1HTDsKn4z Zd+XvY7SAYq4uSG0llv4ortxNL0/ihywJAiHYgs5F9if/bD+rCl0kb2e2bsnwEZu5Ilm +QFw== X-Gm-Message-State: ALQs6tCSUuBdx9h06zITHf8SvNx4ekDJpt38WxiOoOuIqruVn5vzy3zt Phvd62Q2/+jHo/CZiTH6tD7kFKdirH0= X-Google-Smtp-Source: AB8JxZr9IiF7j5U0OX1t45nlmq5tJ+om2mB5mJDRJFCV7DpNpbdJ4iUkLWXnO01RSBWz5xe6bWIjWQ== X-Received: by 2002:a17:902:864b:: with SMTP id y11-v6mr24684510plt.86.1525396879058; Thu, 03 May 2018 18:21:19 -0700 (PDT) From: Michael Clark To: qemu-devel@nongnu.org Date: Fri, 4 May 2018 13:19:54 +1200 Message-Id: <1525396794-17042-1-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::243 Subject: [Qemu-devel] [PATCH] device_tree: Add qemu_fdt_totalsize function 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: , Cc: Peter Maydell , Michael Clark , Alistair Francis , Alexander Graf , Peter Crosthwaite Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently the device-tree create_device_tree function returns the size of the allocated device tree buffer however there is no way to get the actual amount of buffer space used by the device-tree. 14ec3cbd7c1e31dca4d23f028100c8f43e156573 increases the FDT_MAX_SIZE to 1 MiB. This creates an issue for boards that have less than 1 MiB in their ROM for device tree. While cpu_physical_memory_write will not write past the end of a buffer there is and a board is aware of its ROM buffer size, so can use min(fdt_size,rom_size); this provides no indication as to whether the device-tree may be truncated. qemu_fdt_totalsize allows a board to check that a dynamically created device tree will fit within its alloted ROM space. Add qemu_fdt_totalsize which uses the logic and public APIs from libfdt to calculate the device size: struct_offset + struct_size + strings_size + terminator. Cc: Peter Crosthwaite Cc: Alexander Graf Cc: Alistair Francis Cc: Peter Maydell --- device_tree.c | 6 ++++++ include/sysemu/device_tree.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/device_tree.c b/device_tree.c index 52c3358a5583..3a2166d61f37 100644 --- a/device_tree.c +++ b/device_tree.c @@ -215,6 +215,12 @@ void *load_device_tree_from_sysfs(void) =20 #endif /* CONFIG_LINUX */ =20 +size_t qemu_fdt_totalsize(void *fdt) +{ + return fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt) + + fdt_size_dt_strings(fdt) + sizeof(uint32_t) /* terminator */; +} + 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 e22e5bec9c3f..4af232dfdc65 100644 --- a/include/sysemu/device_tree.h +++ b/include/sysemu/device_tree.h @@ -26,6 +26,12 @@ void *load_device_tree_from_sysfs(void); #endif =20 /** + * qemu_fdt_total_size: returns the size required to store the current + * device tree versus the buffer size returned by create_device_tree + */ +size_t qemu_fdt_totalsize(void *fdt); + +/** * qemu_fdt_node_path: return the paths of nodes matching a given * name and compat string * @fdt: pointer to the dt blob --=20 2.7.0