-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter_flash_storage.c
213 lines (166 loc) · 5.57 KB
/
parameter_flash_storage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <string.h>
#include "parameter_flash_storage.h"
#include "parameter_flash_storage_private.h"
#include "flash.h"
#include <parameter/parameter_msgpack.h>
#include <cmp/cmp.h>
#include <cmp_mem_access/cmp_mem_access.h>
#include <crc/crc32.h>
/* We cannot use a CRC start value of 0 because CRC(0, 0xffffffff) = 0xffffffff
* which makes empty flash pages valid. */
#define CRC_INITIAL_VALUE 0xdeadbeef
static size_t cmp_flash_writer(struct cmp_ctx_s *ctx, const void *data, size_t len)
{
cmp_mem_access_t *mem = (cmp_mem_access_t*)ctx->buf;
if (mem->index + len <= mem->size) {
flash_write(&mem->buf[mem->index], data, len);
mem->index += len;
return len;
} else {
return 0;
}
}
void parameter_flash_storage_erase(void *dst)
{
flash_unlock();
flash_sector_erase(dst);
flash_lock();
}
static void err_mark_false(void *arg, const char *id, const char *err)
{
(void) id;
(void) err;
bool *b = (bool *)arg;
*b = false;
}
void parameter_flash_storage_save(void *dst, size_t dst_len, parameter_namespace_t *ns)
{
cmp_ctx_t cmp;
cmp_mem_access_t mem;
size_t len;
bool success = true;
void *orig_dst = dst;
flash_unlock();
/* If there is no valid block, erase flash just to start from a pristine
* state. */
if (parameter_flash_storage_block_find_last_used(dst) == NULL) {
flash_sector_erase(dst);
}
/* Find first available flash block. */
dst = parameter_flash_storage_block_find_first_free(dst);
len = dst_len - (dst - orig_dst);
/* If the destination is too small to fit even the header, erase the block. */
if (len <= PARAMETER_FLASH_STORAGE_HEADER_SIZE) {
flash_sector_erase(orig_dst);
dst = orig_dst;
len = dst_len;
}
cmp_mem_access_init(&cmp,
&mem,
dst + PARAMETER_FLASH_STORAGE_HEADER_SIZE,
len - PARAMETER_FLASH_STORAGE_HEADER_SIZE);
/* Replace the RAM writer with the special writer for flash. */
cmp.write = cmp_flash_writer;
/* Tries to write the config. If there is an error the callback will set
* success to false. */
parameter_msgpack_write_cmp(ns, &cmp, err_mark_false, &success);
if (success == false) {
flash_sector_erase(orig_dst);
flash_lock();
return parameter_flash_storage_save(orig_dst, dst_len, ns);
}
len = cmp_mem_access_get_pos(&mem);
// On STM32F3s we have to make sure we wrote a multiple of 16 bits because
// the flash controller does not support single byte writes.
if (len % 2 == 1) {
uint8_t *end = dst + len + PARAMETER_FLASH_STORAGE_HEADER_SIZE + 1;
uint8_t data = 0x00;
flash_write(end, &data, 1);
len ++;
}
parameter_flash_storage_write_block_header(dst, len);
flash_lock();
}
bool parameter_flash_storage_load(parameter_namespace_t *ns, void *src)
{
int res;
uint32_t src_len;
src = parameter_flash_storage_block_find_last_used(src);
/* If no valid block was found signal an error. */
if (src == NULL) {
return false;
}
src_len = parameter_flash_storage_block_get_length(src);
bool successful = true;
res = parameter_msgpack_read(ns, src + PARAMETER_FLASH_STORAGE_HEADER_SIZE, src_len,
err_mark_false, &successful);
if (res != 0) {
return false;
}
return successful;
}
bool parameter_flash_storage_block_is_valid(void *p)
{
uint8_t *block = (uint8_t *)p;
uint32_t crc, length;
size_t offset = 0;
/* Extract length checksum. */
memcpy(&crc, &block[offset], sizeof(crc));
offset += sizeof(crc);
/* Extract length. */
memcpy(&length, &block[offset], sizeof(length));
offset += sizeof(length);
/* Check that the length is valid. */
if (crc != crc32(CRC_INITIAL_VALUE, &length, sizeof(length))) {
return false;
}
/* Extract data checksum. */
memcpy(&crc, &block[offset], sizeof(crc));
offset += sizeof(crc);
/* Check that the data checksum is valid. */
if (crc != crc32(CRC_INITIAL_VALUE, &block[offset], length)) {
return false;
}
return true;
}
void parameter_flash_storage_write_block_header(void *dst, uint32_t len)
{
uint32_t crc;
size_t offset = 0;
/* First write length checksum. */
crc = crc32(CRC_INITIAL_VALUE, &len, sizeof(uint32_t));
flash_write(dst + offset, &crc, sizeof(uint32_t));
offset += sizeof(uint32_t);
/* Then write the length itself. */
flash_write(dst + offset, &len, sizeof(uint32_t));
offset += sizeof(uint32_t);
/* Then write the data checksum. */
crc = crc32(CRC_INITIAL_VALUE, dst + PARAMETER_FLASH_STORAGE_HEADER_SIZE, len);
flash_write(dst + offset, &crc, sizeof(uint32_t));
}
uint32_t parameter_flash_storage_block_get_length(void *block)
{
uint32_t header[2];
memcpy(header, block, sizeof header);
return header[1];
}
void *parameter_flash_storage_block_find_last_used(void *p)
{
uint8_t *block = (uint8_t *)p;
uint8_t *last = NULL;
while (parameter_flash_storage_block_is_valid(block)) {
last = block;
block += parameter_flash_storage_block_get_length(block);
block += PARAMETER_FLASH_STORAGE_HEADER_SIZE;
}
return last;
}
void *parameter_flash_storage_block_find_first_free(void *p)
{
uint8_t *block = (uint8_t *)p;
while (parameter_flash_storage_block_is_valid(block)) {
block += parameter_flash_storage_block_get_length(block);
block += PARAMETER_FLASH_STORAGE_HEADER_SIZE;
}
return block;
}