[PATCH] nvmem: eeprom: at25: fix byte_len for FRAM

From: Ralph Siemsen
Date: Thu Oct 28 2021 - 09:50:07 EST


Commit fd307a4ad332 ("nvmem: prepare basics for FRAM support") added
support for FRAM devices such as the Cypress FM25V. During testing, it
was found that the FRAM detects properly, however reads and writes fail.
Upon further investigation, two problem were found in at25_probe() routine.

1) In the case of an FRAM device without platform data, eg.
fram == true && spi->dev.platform_data == NULL
the stack local variable "struct spi_eeprom chip" is never initialized.
This structure is copied into at25->chip, which is used subsequently.
Fix this by an explicit memset(), similar to existing at25_fw_to_chip().

2) The size of FRAM is computed from its ID register, and is stored into
the stack local "struct spi_eeprom chip" structure. This happens after
the same structure has been copied into at25->chip. As a result,
at25->chip.byte_len does not contain the correct length of the device.
In turn this can cause checks at beginning of at25_ee_read() to fail
(or equally, it could allow reads beyond the end of the device length).

Fixes: fd307a4ad332 ("nvmem: prepare basics for FRAM support")
Signed-off-by: Ralph Siemsen <ralph.siemsen@xxxxxxxxxx>
---
drivers/misc/eeprom/at25.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/misc/eeprom/at25.c b/drivers/misc/eeprom/at25.c
index 632325474233..65cfb10fa0b7 100644
--- a/drivers/misc/eeprom/at25.c
+++ b/drivers/misc/eeprom/at25.c
@@ -395,6 +395,8 @@ static int at25_probe(struct spi_device *spi)
err = at25_fw_to_chip(&spi->dev, &chip);
if (err)
return err;
+ } else {
+ memset(&chip, 0, sizeof(chip));
}
} else
chip = *(struct spi_eeprom *)spi->dev.platform_data;
@@ -432,6 +434,7 @@ static int at25_probe(struct spi_device *spi)
return -ENODEV;
}
chip.byte_len = int_pow(2, id[7] - 0x21 + 4) * 1024;
+ at25->chip.byte_len = chip.byte_len;

if (at25->chip.byte_len > 64 * 1024)
at25->chip.flags |= EE_ADDR3;
--
2.25.1