Skip to content

Commit

Permalink
nvmem: u-boot-env: use nvmem device helpers
Browse files Browse the repository at this point in the history
[ Upstream commit a832556 ]

Use nvmem_dev_size() and nvmem_device_read() to make this driver less
mtd dependent.

Signed-off-by: Rafał Miłecki <[email protected]>
Reviewed-by: Miquel Raynal <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
Stable-dep-of: 8679e8b ("nvmem: u-boot-env: error if NVMEM device is too small")
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
Rafał Miłecki authored and gregkh committed Sep 18, 2024
1 parent ae91c9c commit 2eea394
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions drivers/nvmem/u-boot-env.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,34 @@ static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf,

static int u_boot_env_parse(struct u_boot_env *priv)
{
struct nvmem_device *nvmem = priv->nvmem;
struct device *dev = priv->dev;
size_t crc32_data_offset;
size_t crc32_data_len;
size_t crc32_offset;
size_t data_offset;
size_t data_len;
size_t dev_size;
uint32_t crc32;
uint32_t calc;
size_t bytes;
uint8_t *buf;
int bytes;
int err;

buf = kcalloc(1, priv->mtd->size, GFP_KERNEL);
dev_size = nvmem_dev_size(nvmem);

buf = kcalloc(1, dev_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) {
dev_err(dev, "Failed to read from mtd: %d\n", err);
bytes = nvmem_device_read(nvmem, 0, dev_size, buf);
if (bytes < 0) {
err = bytes;
goto err_kfree;
} else if (bytes != dev_size) {
err = -EIO;
goto err_kfree;
}

Expand All @@ -169,8 +176,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 = dev_size - crc32_data_offset;
data_len = dev_size - data_offset;

calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L;
if (calc != crc32) {
Expand All @@ -179,7 +186,7 @@ static int u_boot_env_parse(struct u_boot_env *priv)
goto err_kfree;
}

buf[priv->mtd->size - 1] = '\0';
buf[dev_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);
Expand Down

0 comments on commit 2eea394

Please sign in to comment.