Commit f6fdf2d1 authored by Jorge Ramirez-Ortiz's avatar Jorge Ramirez-Ortiz Committed by Greg Kroah-Hartman

nvmem: core: fix read buffer in place

[ Upstream commit 2fe518fe ]

When the bit_offset in the cell is zero, the pointer to the msb will
not be properly initialized (ie, will still be pointing to the first
byte in the buffer).

This being the case, if there are bits to clear in the msb, those will
be left untouched while the mask will incorrectly clear bit positions
on the first byte.

This commit also makes sure that any byte unused in the cell is
cleared.
Signed-off-by: default avatarJorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
Signed-off-by: default avatarSrinivas Kandagatla <srinivas.kandagatla@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 2a5fd4fa
...@@ -1166,7 +1166,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put); ...@@ -1166,7 +1166,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf) static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
{ {
u8 *p, *b; u8 *p, *b;
int i, bit_offset = cell->bit_offset; int i, extra, bit_offset = cell->bit_offset;
p = b = buf; p = b = buf;
if (bit_offset) { if (bit_offset) {
...@@ -1181,11 +1181,16 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf) ...@@ -1181,11 +1181,16 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
p = b; p = b;
*b++ >>= bit_offset; *b++ >>= bit_offset;
} }
} else {
/* point to the msb */
p += cell->bytes - 1;
}
/* result fits in less bytes */ /* result fits in less bytes */
if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE)) extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
while (--extra >= 0)
*p-- = 0; *p-- = 0;
}
/* clear msb bits if any leftover in the last byte */ /* clear msb bits if any leftover in the last byte */
*p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment