summaryrefslogtreecommitdiff
path: root/drivers/misc
diff options
context:
space:
mode:
authorMichel Alex <Alex.Michel@wiedemann-group.com>2023-10-26 05:47:41 +0000
committerTom Rini <trini@konsulko.com>2023-10-30 15:32:49 -0400
commit5ed1c55fb4c23dc9f14b9c8d1d687f8c0ee703ac (patch)
treeba75e840777ea0213e0c97fb72234d1ea4ed7769 /drivers/misc
parent9f339140047041d867b9cccfb67afcb87eb848bf (diff)
downloadu-boot-5ed1c55fb4c23dc9f14b9c8d1d687f8c0ee703ac.tar.gz
u-boot-5ed1c55fb4c23dc9f14b9c8d1d687f8c0ee703ac.tar.bz2
u-boot-5ed1c55fb4c23dc9f14b9c8d1d687f8c0ee703ac.zip
misc: i2c_eeprom: consider pagesize when writing to eeprom
Calculate the maximum length of the buffer when writing across the page boundary. If the buffer length (len) exceeds the page boundary (pagesize), split it. Use this length instead of comparing the length with the pagesize, because if the write start address (offset) is not at the beginning of a page and the page_offset + len is greater than the page boundary (pagesize), the write operation would overflow the current page and the behaviour can be undefined (e.g. at24). Signed-off-by: Alex Michel <alex.michel@wiedemann-group.com>
Diffstat (limited to 'drivers/misc')
-rw-r--r--drivers/misc/i2c_eeprom.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/drivers/misc/i2c_eeprom.c b/drivers/misc/i2c_eeprom.c
index e1d0b8f918..9111bd724c 100644
--- a/drivers/misc/i2c_eeprom.c
+++ b/drivers/misc/i2c_eeprom.c
@@ -60,6 +60,17 @@ static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
return dm_i2c_read(dev, offset, buf, size);
}
+static int i2c_eeprom_len(int offset, int len, int pagesize)
+{
+ int page_offset = offset & (pagesize - 1);
+ int maxlen = pagesize - page_offset;
+
+ if (len > maxlen)
+ len = maxlen;
+
+ return len;
+}
+
static int i2c_eeprom_std_write(struct udevice *dev, int offset,
const uint8_t *buf, int size)
{
@@ -67,7 +78,7 @@ static int i2c_eeprom_std_write(struct udevice *dev, int offset,
int ret;
while (size > 0) {
- int write_size = min_t(int, size, priv->pagesize);
+ int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
ret = dm_i2c_write(dev, offset, buf, write_size);
if (ret)