From patchwork Mon Jul 24 08:26:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 125073 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1882961vqg; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlHAgkE2yt877EMlTVbUsik1q140MJKAgNq+rPwDdN5eBW2z+j0UTYdnV5vQwaYU93X9mQgs X-Received: by 2002:a05:6358:27a8:b0:12b:da97:aba6 with SMTP id l40-20020a05635827a800b0012bda97aba6mr5259624rwb.24.1690213331079; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690213331; cv=none; d=google.com; s=arc-20160816; b=Xsj1kpD8g5Yprh/GInSz7LyE1Kenslk5ETVOYMWzDRRWa+p34j7oSRJfJgjpHE6ZBC bwJMH4YC4T3zlYIJX5mThvICa4DKdMtHNqzS/2Em8sqgyn+Su+KeUH4zGqYskQGc8KRW Q917AqeasIi979fJm8ueaD8LUTzOQaXWaKIk8SwP0LI61L5QeXSYdBf3WgRCSCRUUeHG BPMKSdYxqWCRWcTY6jBOAbQxjrMo34o3YSN9Hye+FOtp0XGP01YC5JiU9uhxsASIHDGB S2i2jpKbs3RueZwVAhQAmxd3CjX6qRh54e1dmi/x7hF3hHelgTR6gHwqkLfIiP7tJFNl bmYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; fh=LpK/+DU52e2A6LHcsmnq9BYHnjjjRVFCHa6LQ3LP2l8=; b=OkJMZMphgCOaZOG4eCEbCHeFlAay1IgAj2zim5kgO19ORBn+ZMsxBd8UWu3wTwqsum md2sUBhvecIneWCgZcB1gf4hCQJBiNKcffFLmd4ByAaeAgUKCbW9UstglmniGUUXf4iP X/GZ8Ex7RR9CWkZvecF424dukFjmXS5/UDHou/YrY0yiH8Xo/t+xW6+2RtjmAhAHoUI8 29vu/W4Fmdd7uPLtktIHp8EO7UQcSlxOoRW05HJVG+9s50oYofnwwvLLwOXjkPbyeNH9 XRztJOm7ft74VeRapBz4MxXs2LY45za9WYXxZgl5ImR6JKAsUfqCd4BTMPA555NtP7lT eHGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rMuYTyo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h2-20020a635302000000b00548e140a1a6si7402486pgb.66.2023.07.24.08.41.57; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rMuYTyo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231389AbjGXPcb (ORCPT + 99 others); Mon, 24 Jul 2023 11:32:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231352AbjGXPc0 (ORCPT ); Mon, 24 Jul 2023 11:32:26 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9101EE57; Mon, 24 Jul 2023 08:32:22 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fbc0609cd6so34803575e9.1; Mon, 24 Jul 2023 08:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690212741; x=1690817541; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; b=rMuYTyo8xrub4HIbmV+wNnF3zawKWt48DGl+hNOsmEvnykYpXH4QrT+L/D2lXI4/fd XrUZdfZ+c4oAQjQ+qqP1qc5ydHikAV181XknINdkQjAJozlQP87I6BHPqsi1r8Wo4sh7 DEFJnLOwQj5fx10GZ2RCO97YVdr+JX1gZCxheKeqVR+GreqTj9YjSaUUnITXEIcLFD/y 0vS7QgcHs8/eP5PATEDKZVmcBHHsvPMWnxHsB37cjEGJvUNxr7Ut4+0KldWH5g8zObE8 OG5ZTyLR20BVWVIVyEcato/71JCk9WGsuzyLpQi40ZOOpuNBenDPeLJ054/ORVLFnW9f TTLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690212741; x=1690817541; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; b=jSNT7JtkJ7JMLV6aG4tavcaZuL9j7MQkcv3EKzg2rQLfuwZyv4aDuRcRVMrHcNeMO4 8UWVlMkwdPw6Rn+eXR1YhSDfb3ygwUxwFvQRky7/+n5xhQnL39RwfZygvmWDNagKfqG8 YBHtkWGysjLavZf9Hzrv94zwFAu4mGDVaiVvsEjwgMhQMBzZT0KhsknMjw6ROZc10R/L +KCJLEJgGhA5qbYHAU6zFfwIstHccPyQ21/nm1yeh+8OTehoUScLCyk6G4q/K+8mQCOt 6dFbU646lsuY7MSIjZiZ01iKdRwVQmhtFdB1HGNiJRWa0gF46GrNWbiUWapGE9QtK1es v/uQ== X-Gm-Message-State: ABy/qLbbdtNUicwfkLXDvSYqr5WtjXmo7nhcVFBww21xhYqJN0MztnDt MnG4vEO5thHkFZEo/GYctrM= X-Received: by 2002:a05:600c:22cf:b0:3fb:ffef:d058 with SMTP id 15-20020a05600c22cf00b003fbffefd058mr5972219wmg.0.1690212740858; Mon, 24 Jul 2023 08:32:20 -0700 (PDT) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id l8-20020a7bc448000000b003fb40ec9475sm10676900wmi.11.2023.07.24.08.32.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 08:32:20 -0700 (PDT) From: Christian Marangi To: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [PATCH 2/3] nvmem: u-boot-env: Permit to declare custom env-size Date: Mon, 24 Jul 2023 10:26:31 +0200 Message-Id: <20230724082632.21133-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230724082632.21133-1-ansuelsmth@gmail.com> References: <20230724082632.21133-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772317134108104194 X-GMAIL-MSGID: 1772317134108104194 Permit to declare a custom size of the U-Boot env that differs than the partition size where the U-Boot env is located. U-Boot env is validated by calculating the CRC32 on the entire env and in some specific case, the env size might differ from the partition size resulting in wrong CRC32 calculation than the expected one saved at the start of the partition. This happens when U-Boot is compiled by hardcoding a specific env size but the env is actually placed in a bigger partition, resulting in needing to provide a custom value. To declare a custom env-size, the new property 'u-boot,env-size' is introduced to handle this special case. If the property is not declared, the mtd size is used instead. Signed-off-by: Christian Marangi --- drivers/nvmem/u-boot-env.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index ee9fd9989b6e..b64c37308789 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -24,6 +24,7 @@ enum u_boot_env_format { struct u_boot_env { struct device *dev; enum u_boot_env_format format; + unsigned int u_boot_env_size; struct mtd_info *mtd; @@ -149,14 +150,14 @@ static int u_boot_env_parse(struct u_boot_env *priv) uint8_t *buf; int err; - buf = kcalloc(1, priv->mtd->size, GFP_KERNEL); + buf = kcalloc(1, priv->u_boot_env_size, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto err_out; } - err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf); - if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) { + err = mtd_read(priv->mtd, 0, priv->u_boot_env_size, &bytes, buf); + if ((err && !mtd_is_bitflip(err)) || bytes != priv->u_boot_env_size) { dev_err(dev, "Failed to read from mtd: %d\n", err); goto err_kfree; } @@ -179,8 +180,8 @@ static int u_boot_env_parse(struct u_boot_env *priv) break; } crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset)); - crc32_data_len = priv->mtd->size - crc32_data_offset; - data_len = priv->mtd->size - data_offset; + crc32_data_len = priv->u_boot_env_size - crc32_data_offset; + data_len = priv->u_boot_env_size - data_offset; calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; if (calc != crc32) { @@ -189,7 +190,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) goto err_kfree; } - buf[priv->mtd->size - 1] = '\0'; + buf[priv->u_boot_env_size - 1] = '\0'; err = u_boot_env_add_cells(priv, buf, data_offset, data_len); if (err) dev_err(dev, "Failed to add cells: %d\n", err); @@ -224,6 +225,10 @@ static int u_boot_env_probe(struct platform_device *pdev) return PTR_ERR(priv->mtd); } + /* In absence of a custom env size, use the full mtd partition size */ + if (of_property_read_u32(np, "u-boot,env-size", &priv->u_boot_env_size)) + priv->u_boot_env_size = priv->mtd->size; + err = u_boot_env_parse(priv); if (err) return err; @@ -232,7 +237,7 @@ static int u_boot_env_probe(struct platform_device *pdev) config.cells = priv->cells; config.ncells = priv->ncells; config.priv = priv; - config.size = priv->mtd->size; + config.size = priv->u_boot_env_size; return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); }