diff mbox series

[2/3] nvmem: u-boot-env: Permit to declare custom env-size

Message ID 20230724082632.21133-2-ansuelsmth@gmail.com
State New
Headers show
Series [1/3] dt-bindings: nvmem: u-boot,env: Add support for u-boot,env-size | expand

Commit Message

Christian Marangi July 24, 2023, 8:26 a.m. UTC
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 mbox series

Patch

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));
 }