Skip to content

Commit 2a334a3

Browse files
authored
Merge pull request #295 from sameehj/fix
Fix transposed copy size and buffer offset in wh_FlashUnit_ReadBytes
2 parents 40549bd + 8875d00 commit 2a334a3

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/wh_flash_unit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ int wh_FlashUnit_ReadBytes(const whFlashCb* cb, void* context,
169169
if (offset_rem != 0) {
170170
ret = wh_FlashUnit_Read(cb, context, offset_units, 1, &buffer.unit);
171171
if (ret == 0) {
172-
uint32_t this_size = offset_rem;
172+
uint32_t this_size = WHFU_BYTES_PER_UNIT - offset_rem;
173173
if (data_len < this_size) this_size = data_len;
174-
memcpy(data, &buffer.bytes[WHFU_BYTES_PER_UNIT - offset_rem], this_size);
174+
memcpy(data, &buffer.bytes[offset_rem], this_size);
175175
data += this_size;
176176
data_len -= this_size;
177177
offset_units++;

test/wh_test_nvm_flash.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,47 @@ int whTest_Flash(const whFlashCb* fcb, void* fctx, const void* cfg)
301301
17 * WHFU_BYTES_PER_UNIT, 8, read_bytes));
302302
WH_TEST_RETURN_ON_FAIL(memcmp(write_bytes, read_bytes, 8));
303303

304+
/* Test unaligned ReadBytes (exercises the offset_rem != 0 path) */
305+
{
306+
uint8_t pattern[WHFU_BYTES_PER_UNIT * 4];
307+
uint8_t readback[WHFU_BYTES_PER_UNIT * 4];
308+
uint32_t base_unit = 20;
309+
uint32_t i;
310+
311+
for (i = 0; i < sizeof(pattern); i++) {
312+
pattern[i] = (uint8_t)(0x10 + i);
313+
}
314+
315+
/* Program 4 full units at base_unit */
316+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ProgramBytes(fcb, fctx,
317+
base_unit * WHFU_BYTES_PER_UNIT, sizeof(pattern), pattern));
318+
319+
/* offset_rem = 3: should read pattern[3..7] */
320+
memset(readback, 0, sizeof(readback));
321+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
322+
base_unit * WHFU_BYTES_PER_UNIT + 3, 5, readback));
323+
WH_TEST_ASSERT_RETURN(0 == memcmp(readback, &pattern[3], 5));
324+
325+
/* offset_rem = 1: should read pattern[1..10] */
326+
memset(readback, 0, sizeof(readback));
327+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
328+
base_unit * WHFU_BYTES_PER_UNIT + 1, 10, readback));
329+
WH_TEST_ASSERT_RETURN(0 == memcmp(readback, &pattern[1], 10));
330+
331+
/* offset_rem = 5: should read pattern[5..7] */
332+
memset(readback, 0, sizeof(readback));
333+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
334+
base_unit * WHFU_BYTES_PER_UNIT + 5, 3, readback));
335+
WH_TEST_ASSERT_RETURN(0 == memcmp(readback, &pattern[5], 3));
336+
337+
/* Full 3-phase read: leading partial + aligned middle + trailing
338+
* offset_rem = 2, len = 21: 6 leading + 8 aligned + 7 trailing */
339+
memset(readback, 0, sizeof(readback));
340+
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_ReadBytes(fcb, fctx,
341+
base_unit * WHFU_BYTES_PER_UNIT + 2, 21, readback));
342+
WH_TEST_ASSERT_RETURN(0 == memcmp(readback, &pattern[2], 21));
343+
}
344+
304345
/* Erase the first partition */
305346
WH_TEST_RETURN_ON_FAIL(wh_FlashUnit_Erase(fcb, fctx,
306347
0, partition_units));

0 commit comments

Comments
 (0)