From f821ba09d2c769ef7ffe75103ed399c72b7f3c08 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Fri, 2 Jan 2015 14:06:50 -0700 Subject: [PATCH] stm25p: fix seek error Ensure correct computation of m_log_info[ id ].read_addr when client code requests seek() to a cookie whose value is below the current virtual address range by a distance, in address value, greater than 255 sectors. The sector component of a virtual address is 16 bits, given STM25P_SECTOR_SIZE_LOG2 is 16 and sizeof(storage_cookie_t) is 32. However, in ClientResource.granted[]() in the S_SEEK case, the client's read_addr value can have its upper bits silently discarded because sector value are handled in uint8_t variables. The symptom of upper bit discards is that client code receives the contents of a full log many times until the client read_addr value finally increments to the point that allows satisfaction of the end of log condition, read_addr >= m_log_info[ client ].write_addr Example case exhibiting this behavior: read_addr = 0x59f0000 write_addr = 5a6dbf8 LogRead.seek(0x2a71400) The resulting address handling in ClientResource.granted[]() in the S_SEEK request case, around Stm25pLogP.nc:229: readSector = 0xa7; // from incoming cookie value, should be 0x2a7 writeSector = 0xa7; // should be 0x5a7 if (writeSector - readSector > numSectors): FALSE, should be TRUE Had the if expression above been TRUE, the read address would still be computed incorrectly due to 8-bit match in the if's 'then' clause. m_log_state[ id ].read_addr = 0x2a7000; // should be 0x59f000 --- tos/chips/stm25p/Stm25pLogP.nc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tos/chips/stm25p/Stm25pLogP.nc b/tos/chips/stm25p/Stm25pLogP.nc index e454de79..b63cf3f7 100644 --- a/tos/chips/stm25p/Stm25pLogP.nc +++ b/tos/chips/stm25p/Stm25pLogP.nc @@ -230,9 +230,9 @@ implementation { { // make sure the cookie is still within the range of valid data uint8_t numSectors = call Sector.getNumSectors[ id ](); - uint8_t readSector = + uint16_t readSector = (m_log_state[ id ].cookie >> STM25P_SECTOR_SIZE_LOG2); - uint8_t writeSector = + uint16_t writeSector = ((m_log_info[ id ].write_addr-1)>>STM25P_SECTOR_SIZE_LOG2)+1; // if cookie is overwritten, advance to beginning of log if ( (writeSector - readSector) > numSectors ) { -- 2.39.2