|
1 |
| -#include <storage/nonvolatile_storage.h> |
2 |
| -#include <libtock-sync/storage/nonvolatile_storage.h> |
| 1 | +#include <storage/isolated_nonvolatile_storage.h> |
| 2 | +#include <libtock-sync/storage/isolated_nonvolatile_storage.h> |
3 | 3 |
|
4 | 4 | #include <openthread/platform/flash.h>
|
5 | 5 |
|
6 | 6 | #include <stdio.h>
|
7 | 7 |
|
8 |
| -const uint32_t SWAP_SIZE = 1024; |
| 8 | +// TODO: The kernel currently has a 512 byte buffer |
| 9 | +// for isolated nonvolatile storage. This means that we can only |
| 10 | +// allow 512 bytes. 2x 512B swaps seems to be enough for openthread |
| 11 | +// but we may need to revist this in the future. |
| 12 | +const uint32_t SWAP_SIZE = 512; |
9 | 13 | const uint8_t SWAP_NUM = 2;
|
10 | 14 |
|
11 |
| -uint32_t otPlatFlashGetSwapSize(otInstance *aInstance){ |
| 15 | +uint32_t otPlatFlashGetSwapSize(otInstance *aInstance) { |
12 | 16 | OT_UNUSED_VARIABLE(aInstance);
|
13 | 17 | return SWAP_SIZE;
|
14 | 18 | }
|
15 | 19 |
|
16 |
| -void otPlatFlashInit(otInstance *aInstance) { |
| 20 | +void otPlatFlashInit(otInstance *aInstance) { |
17 | 21 | OT_UNUSED_VARIABLE(aInstance);
|
18 |
| -} |
19 | 22 |
|
20 |
| -void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) { |
21 |
| - OT_UNUSED_VARIABLE(aInstance); |
| 23 | + // Confirm we have enough space for the 2 swaps. |
| 24 | + uint64_t num_bytes; |
| 25 | + libtocksync_isolated_nonvolatile_storage_get_number_bytes(&num_bytes); |
22 | 26 |
|
23 |
| - uint32_t num_bytes; |
24 |
| - libtock_nonvolatile_storage_get_number_bytes(&num_bytes); |
| 27 | + assert(num_bytes >= SWAP_SIZE * SWAP_NUM); |
| 28 | +} |
25 | 29 |
|
26 |
| - // Write all zeroes to flash |
27 |
| - uint8_t zeroes[SWAP_SIZE]; |
| 30 | +void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) { |
| 31 | + uint8_t overwrite[SWAP_SIZE]; |
28 | 32 |
|
29 |
| - otPlatFlashWrite(aInstance, aSwapIndex, 0, zeroes, num_bytes); |
| 33 | + // Fill the swap area with 0xFF to erase it. |
| 34 | + for (uint32_t i = 0; i < SWAP_SIZE; i++) { |
| 35 | + overwrite[i] = 0xFF; |
| 36 | + } |
30 | 37 |
|
| 38 | + otPlatFlashWrite(aInstance, aSwapIndex, 0, overwrite, SWAP_SIZE); |
31 | 39 | }
|
32 | 40 |
|
33 | 41 | void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset,
|
34 | 42 | const void *aData, uint32_t aSize) {
|
35 | 43 | OT_UNUSED_VARIABLE(aInstance);
|
36 |
| - OT_UNUSED_VARIABLE(aSwapIndex); |
37 |
| - int ret; |
38 | 44 |
|
39 | 45 | assert(aSwapIndex < SWAP_NUM);
|
| 46 | + assert(aOffset + aSize <= SWAP_SIZE); |
40 | 47 | uint32_t offset = aSwapIndex ? SWAP_SIZE : 0;
|
41 | 48 | offset += aOffset;
|
42 | 49 |
|
43 |
| - int _written; |
44 |
| - ret = libtocksync_nonvolatile_storage_write(offset, aSize, (uint8_t*)aData, aSize, &_written); |
45 |
| - if (ret != RETURNCODE_SUCCESS) { |
46 |
| - return; |
47 |
| - } |
| 50 | + libtocksync_isolated_nonvolatile_storage_write(offset, (uint8_t*)aData, aSize); |
48 | 51 | }
|
49 | 52 |
|
50 | 53 | void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData,
|
51 | 54 | uint32_t aSize) {
|
52 | 55 | OT_UNUSED_VARIABLE(aInstance);
|
53 |
| - OT_UNUSED_VARIABLE(aSwapIndex); |
54 |
| - int ret; |
55 |
| - |
| 56 | + |
56 | 57 | assert(aSwapIndex < SWAP_NUM);
|
| 58 | + assert(aOffset + aSize <= SWAP_SIZE); |
57 | 59 | uint32_t offset = aSwapIndex ? SWAP_SIZE : 0;
|
58 | 60 | offset += aOffset;
|
59 |
| - |
60 |
| - int _read; |
61 |
| - ret = libtocksync_nonvolatile_storage_read(offset, aSize, (uint8_t*)aData, aSize, &_read); |
62 |
| - if (ret != RETURNCODE_SUCCESS) { |
63 |
| - return; |
64 |
| - } |
| 61 | + |
| 62 | + libtocksync_isolated_nonvolatile_storage_read(offset, (uint8_t*)aData, aSize); |
65 | 63 | }
|
0 commit comments