From 606b6e5900a18d78bec49be94f61c1d6e9e21d09 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Thu, 20 Oct 2016 11:36:10 +0800 Subject: [PATCH] Refine settings driver (#847) - write the actual setting's length into flash - add getAlignLength() for address alignment --- src/core/common/settings.cpp | 39 ++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/core/common/settings.cpp b/src/core/common/settings.cpp index 3d3fc49f6..9d63d62ea 100644 --- a/src/core/common/settings.cpp +++ b/src/core/common/settings.cpp @@ -76,6 +76,11 @@ struct settingsBlock uint16_t reserved; } OT_TOOL_PACKED_END; +static uint16_t getAlignLength(uint16_t length) +{ + return (length + 3) & 0xfffc; +} + static void setSettingsFlag(uint32_t aBase, uint32_t aFlag) { otPlatFlashWrite(aBase, reinterpret_cast(&aFlag), sizeof(aFlag)); @@ -132,7 +137,7 @@ static uint32_t swapSettingsBlock(otInstance *aInstance) if (!(addBlock.block.flag & kBlockAddCompleteFlag) && (addBlock.block.flag & kBlockDeleteFlag)) { - uint32_t address = swapAddress + addBlock.block.length; + uint32_t address = swapAddress + getAlignLength(addBlock.block.length); while (address < (oldBase + usedSize)) { @@ -147,16 +152,16 @@ static uint32_t swapSettingsBlock(otInstance *aInstance) break; } - address += (block.length + sizeof(struct settingsBlock)); + address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } if (valid) { - otPlatFlashRead(swapAddress, addBlock.data, addBlock.block.length); + otPlatFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length)); otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize, reinterpret_cast(&addBlock), - addBlock.block.length + sizeof(struct settingsBlock)); - aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + addBlock.block.length); + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)); + aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); } } else if (addBlock.block.flag == 0xff) @@ -164,7 +169,7 @@ static uint32_t swapSettingsBlock(otInstance *aInstance) break; } - swapAddress += addBlock.block.length; + swapAddress += getAlignLength(addBlock.block.length); } setSettingsFlag(aInstance->mSettingsBaseAddress, static_cast(kSettingsInUse)); @@ -199,9 +204,10 @@ static ThreadError addSetting(otInstance *aInstance, uint16_t aKey, bool aIndex0 addBlock.block.flag &= (~kBlockAddBeginFlag); addBlock.block.length = static_cast(aValueLength); - if ((aInstance->mSettingsUsedSize + addBlock.block.length + sizeof(struct settingsBlock)) >= settingsSize) + if ((aInstance->mSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >= + settingsSize) { - VerifyOrExit(swapSettingsBlock(aInstance) >= (addBlock.block.length + sizeof(struct settingsBlock)), + VerifyOrExit(swapSettingsBlock(aInstance) >= (getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)), error = kThreadError_NoBufs); } @@ -209,22 +215,17 @@ static ThreadError addSetting(otInstance *aInstance, uint16_t aKey, bool aIndex0 reinterpret_cast(&addBlock.block), sizeof(struct settingsBlock)); + memset(addBlock.data, 0xff, kSettingsBlockDataSize); memcpy(addBlock.data, aValue, addBlock.block.length); - // padding - while (addBlock.block.length & 3) - { - addBlock.data[addBlock.block.length++] = 0xff; - } - otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize + sizeof(struct settingsBlock), - reinterpret_cast(addBlock.data), addBlock.block.length); + reinterpret_cast(addBlock.data), getAlignLength(addBlock.block.length)); addBlock.block.flag &= (~kBlockAddCompleteFlag); otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize, reinterpret_cast(&addBlock.block), sizeof(struct settingsBlock)); - aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + addBlock.block.length); + aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length)); exit: return error; @@ -268,7 +269,7 @@ void otPlatSettingsInit(otInstance *aInstance) if (!(block.flag & kBlockAddBeginFlag)) { - aInstance->mSettingsUsedSize += (block.length + sizeof(struct settingsBlock)); + aInstance->mSettingsUsedSize += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } else { @@ -336,7 +337,7 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, } } - address += (block.length + sizeof(struct settingsBlock)); + address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } exit: @@ -395,7 +396,7 @@ ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aInde } } - address += (block.length + sizeof(struct settingsBlock)); + address += (getAlignLength(block.length) + sizeof(struct settingsBlock)); } return error;