From: Christian Marangi <ansuelsmth@gmail.com>
To: "Rafał Miłecki" <rafal@milecki.pl>,
"Srinivas Kandagatla" <srinivas.kandagatla@linaro.org>,
"Rob Herring" <robh+dt@kernel.org>,
"Krzysztof Kozlowski" <krzysztof.kozlowski+dt@linaro.org>,
"Conor Dooley" <conor+dt@kernel.org>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Christian Marangi <ansuelsmth@gmail.com>
Subject: [PATCH 2/3] nvmem: u-boot-env: Permit to declare custom env-size
Date: Mon, 24 Jul 2023 10:26:31 +0200 [thread overview]
Message-ID: <20230724082632.21133-2-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20230724082632.21133-1-ansuelsmth@gmail.com>
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 <ansuelsmth@gmail.com>
---
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));
}
--
2.40.1
next prev parent reply other threads:[~2023-07-24 15:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-24 8:26 [PATCH 1/3] dt-bindings: nvmem: u-boot,env: Add support for u-boot,env-size Christian Marangi
2023-07-24 8:26 ` Christian Marangi [this message]
2023-07-24 8:26 ` [PATCH 3/3] nvmem: u-boot-env: Handle "reduced" ASCII address declaration Christian Marangi
2023-07-26 16:36 ` [PATCH 1/3] dt-bindings: nvmem: u-boot,env: Add support for u-boot,env-size Rob Herring
2023-07-27 19:04 ` Christian Marangi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230724082632.21133-2-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafal@milecki.pl \
--cc=robh+dt@kernel.org \
--cc=srinivas.kandagatla@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).