From patchwork Fri Jun 24 14:27:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 640236 X-Patchwork-Delegate: panto@antoniou-consulting.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3rbghM1cc4z9sDC for ; Sat, 25 Jun 2016 00:29:39 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B2CA1A7531; Fri, 24 Jun 2016 16:29:11 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wNUWr6SkFEow; Fri, 24 Jun 2016 16:29:11 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 8BC8BA7532; Fri, 24 Jun 2016 16:28:41 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1AF1EA751C for ; Fri, 24 Jun 2016 16:28:28 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6wAvxTRdjyIY for ; Fri, 24 Jun 2016 16:28:28 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.free-electrons.com (down.free-electrons.com [37.187.137.238]) by theia.denx.de (Postfix) with ESMTP id 65F884BA7F for ; Fri, 24 Jun 2016 16:28:20 +0200 (CEST) Received: by mail.free-electrons.com (Postfix, from userid 110) id D3E8B1BB8; Fri, 24 Jun 2016 16:28:19 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.free-electrons.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT, URIBL_BLOCKED shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id D4D1C1814; Fri, 24 Jun 2016 16:28:01 +0200 (CEST) From: Maxime Ripard To: Pantelis Antoniou , Simon Glass Date: Fri, 24 Jun 2016 16:27:52 +0200 Message-Id: <20160624142757.32735-8-maxime.ripard@free-electrons.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160624142757.32735-1-maxime.ripard@free-electrons.com> References: <20160624142757.32735-1-maxime.ripard@free-electrons.com> Cc: Thomas Petazzoni , Tom Rini , u-boot@lists.denx.de, Alexander Kaplan , devicetree-compiler@vger.kernel.org Subject: [U-Boot] [PATCH v3 07/12] libfdt: Add fdt_setprop_inplace_by_index X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add a function to modify inplace a property starting from a given index. This is especially useful when the property is an array of values, and you want to update one of them without changing the DT size. Signed-off-by: Maxime Ripard Acked-by: Simon Glass --- include/libfdt.h | 34 +++++++++++++++++++++++++++++++--- lib/libfdt/fdt_wip.c | 13 ++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/libfdt.h b/include/libfdt.h index 4643be5adf39..2c8a42bcb667 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -1032,6 +1032,27 @@ int fdt_size_cells(const void *fdt, int nodeoffset); /* Write-in-place functions */ /**********************************************************************/ +/** + * fdt_setprop_inplace_namelen_by_index - change a property's value, + * but not its size + * @fdt: pointer to the device tree blob + * @nodeoffset: offset of the node whose property to change + * @name: name of the property to change + * @namelen: number of characters of name to consider + * @index: index of the property to change in the array + * @val: pointer to data to replace the property value with + * @len: length of the property value + * + * Identical to fdt_setprop_inplace(), but modifies the given property + * starting from the given index, and using only the first characters + * of the name. It is useful when you want to manipulate only one value of + * an array and you have a string that doesn't end with \0. + */ +int fdt_setprop_inplace_namelen_by_index(void *fdt, int nodeoffset, + const char *name, int namelen, + uint32_t index, const void *val, + int len); + /** * fdt_setprop_inplace - change a property's value, but not its size * @fdt: pointer to the device tree blob @@ -1060,8 +1081,13 @@ int fdt_size_cells(const void *fdt, int nodeoffset); * -FDT_ERR_BADSTRUCTURE, * -FDT_ERR_TRUNCATED, standard meanings */ -int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, - const void *val, int len); +static inline int fdt_setprop_inplace(void *fdt, int nodeoffset, + const char *name, const void *val, + int len) +{ + return fdt_setprop_inplace_namelen_by_index(fdt, nodeoffset, name, + strlen(name), 0, val, len); +} /** * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property @@ -1095,7 +1121,9 @@ static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset, const char *name, uint32_t val) { fdt32_t tmp = cpu_to_fdt32(val); - return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp)); + return fdt_setprop_inplace_namelen_by_index(fdt, nodeoffset, + name, strlen(name), + 0, &tmp, sizeof(tmp)); } /** diff --git a/lib/libfdt/fdt_wip.c b/lib/libfdt/fdt_wip.c index 9fe988655fe3..737769fa59e2 100644 --- a/lib/libfdt/fdt_wip.c +++ b/lib/libfdt/fdt_wip.c @@ -14,20 +14,23 @@ #include "libfdt_internal.h" -int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, - const void *val, int len) +int fdt_setprop_inplace_namelen_by_index(void *fdt, int nodeoffset, + const char *name, int namelen, + uint32_t index, const void *val, + int len) { void *propval; int proplen; - propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen); + propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen, + &proplen); if (!propval) return proplen; - if (proplen != len) + if (proplen < (len + index)) return -FDT_ERR_NOSPACE; - memcpy(propval, val, len); + memcpy(propval + index, val, len); return 0; }