|
@@ -43,7 +43,7 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
|
43
|
43
|
uint8_t sum = 0;
|
44
|
44
|
unsigned int i;
|
45
|
45
|
|
46
|
|
- for ( i = 0 ; i < nvo->total_len ; i++ ) {
|
|
46
|
+ for ( i = 0 ; i < nvo->len ; i++ ) {
|
47
|
47
|
sum += *(data++);
|
48
|
48
|
}
|
49
|
49
|
return sum;
|
|
@@ -56,19 +56,14 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
|
56
|
56
|
* @ret rc Return status code
|
57
|
57
|
*/
|
58
|
58
|
static int nvo_load ( struct nvo_block *nvo ) {
|
59
|
|
- void *data = nvo->data;
|
60
|
|
- struct nvo_fragment *frag;
|
61
|
59
|
int rc;
|
62
|
60
|
|
63
|
|
- /* Read data a fragment at a time */
|
64
|
|
- for ( frag = nvo->fragments ; frag->len ; frag++ ) {
|
65
|
|
- if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
|
66
|
|
- frag->len ) ) != 0 ) {
|
67
|
|
- DBGC ( nvo, "NVO %p could not read %zd bytes at "
|
68
|
|
- "%#04x\n", nvo, frag->len, frag->address );
|
69
|
|
- return rc;
|
70
|
|
- }
|
71
|
|
- data += frag->len;
|
|
61
|
+ /* Read data */
|
|
62
|
+ if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
|
|
63
|
+ nvo->len ) ) != 0 ) {
|
|
64
|
+ DBGC ( nvo, "NVO %p could not read %zd bytes at %#04x: %s\n",
|
|
65
|
+ nvo, nvo->len, nvo->address, strerror ( rc ) );
|
|
66
|
+ return rc;
|
72
|
67
|
}
|
73
|
68
|
|
74
|
69
|
DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
|
|
@@ -82,23 +77,18 @@ static int nvo_load ( struct nvo_block *nvo ) {
|
82
|
77
|
* @ret rc Return status code
|
83
|
78
|
*/
|
84
|
79
|
static int nvo_save ( struct nvo_block *nvo ) {
|
85
|
|
- void *data = nvo->data;
|
86
|
|
- uint8_t *checksum = data;
|
87
|
|
- struct nvo_fragment *frag;
|
|
80
|
+ uint8_t *checksum = nvo->data;
|
88
|
81
|
int rc;
|
89
|
82
|
|
90
|
83
|
/* Recalculate checksum */
|
91
|
84
|
*checksum -= nvo_checksum ( nvo );
|
92
|
85
|
|
93
|
|
- /* Write data a fragment at a time */
|
94
|
|
- for ( frag = nvo->fragments ; frag->len ; frag++ ) {
|
95
|
|
- if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
|
96
|
|
- frag->len ) ) != 0 ) {
|
97
|
|
- DBGC ( nvo, "NVO %p could not write %zd bytes at "
|
98
|
|
- "%#04x\n", nvo, frag->len, frag->address );
|
99
|
|
- return rc;
|
100
|
|
- }
|
101
|
|
- data += frag->len;
|
|
86
|
+ /* Write data */
|
|
87
|
+ if ( ( rc = nvs_write ( nvo->nvs, nvo->address, nvo->data,
|
|
88
|
+ nvo->len ) ) != 0 ) {
|
|
89
|
+ DBGC ( nvo, "NVO %p could not write %zd bytes at %#04x: %s\n",
|
|
90
|
+ nvo, nvo->len, nvo->address, strerror ( rc ) );
|
|
91
|
+ return rc;
|
102
|
92
|
}
|
103
|
93
|
|
104
|
94
|
DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
|
|
@@ -120,7 +110,7 @@ static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
|
120
|
110
|
|
121
|
111
|
/* Steal one byte for the checksum */
|
122
|
112
|
options_data = ( nvo->data + 1 );
|
123
|
|
- options_len = ( nvo->total_len - 1 );
|
|
113
|
+ options_len = ( nvo->len - 1 );
|
124
|
114
|
|
125
|
115
|
/* If checksum fails, or options data starts with a zero,
|
126
|
116
|
* assume the whole block is invalid. This should capture the
|
|
@@ -130,7 +120,7 @@ static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
|
130
|
120
|
DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
|
131
|
121
|
"assuming empty\n", nvo, nvo_checksum ( nvo ),
|
132
|
122
|
options_data[0] );
|
133
|
|
- memset ( nvo->data, 0, nvo->total_len );
|
|
123
|
+ memset ( nvo->data, 0, nvo->len );
|
134
|
124
|
}
|
135
|
125
|
|
136
|
126
|
dhcpopt_init ( &nvo->dhcpopts, options_data, options_len,
|
|
@@ -198,13 +188,15 @@ static struct settings_operations nvo_settings_operations = {
|
198
|
188
|
*
|
199
|
189
|
* @v nvo Non-volatile options block
|
200
|
190
|
* @v nvs Underlying non-volatile storage device
|
201
|
|
- * @v fragments List of option-containing fragments, or NULL
|
|
191
|
+ * @v address Address within NVS device
|
|
192
|
+ * @v len Length of non-volatile options data
|
202
|
193
|
* @v refcnt Containing object reference counter, or NULL
|
203
|
194
|
*/
|
204
|
195
|
void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
|
205
|
|
- struct nvo_fragment *fragments, struct refcnt *refcnt ) {
|
|
196
|
+ size_t address, size_t len, struct refcnt *refcnt ) {
|
206
|
197
|
nvo->nvs = nvs;
|
207
|
|
- nvo->fragments = fragments;
|
|
198
|
+ nvo->address = address;
|
|
199
|
+ nvo->len = len;
|
208
|
200
|
settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 );
|
209
|
201
|
}
|
210
|
202
|
|
|
@@ -216,34 +208,17 @@ void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
|
216
|
208
|
* @ret rc Return status code
|
217
|
209
|
*/
|
218
|
210
|
int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
|
219
|
|
- struct nvo_fragment *fragment = nvo->fragments;
|
220
|
211
|
int rc;
|
221
|
212
|
|
222
|
|
- /* Calculate total length of all fragments, if applicable */
|
223
|
|
- if ( fragment ) {
|
224
|
|
- for ( ; fragment->len ; fragment++ )
|
225
|
|
- nvo->total_len += fragment->len;
|
226
|
|
- } else {
|
227
|
|
- nvo->total_len = nvo->nvs->size;
|
228
|
|
- }
|
229
|
|
-
|
230
|
|
- /* Allocate memory for options (and fragment list, if applicable) */
|
231
|
|
- nvo->data = zalloc ( nvo->total_len +
|
232
|
|
- ( fragment ? 0 : ( 2 * sizeof ( *fragment ) ) ) );
|
|
213
|
+ /* Allocate memory for options */
|
|
214
|
+ nvo->data = zalloc ( nvo->len );
|
233
|
215
|
if ( ! nvo->data ) {
|
234
|
216
|
DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
|
235
|
|
- nvo, nvo->total_len );
|
|
217
|
+ nvo, nvo->len );
|
236
|
218
|
rc = -ENOMEM;
|
237
|
219
|
goto err_malloc;
|
238
|
220
|
}
|
239
|
221
|
|
240
|
|
- /* Create fragment list, if applicable */
|
241
|
|
- if ( ! fragment ) {
|
242
|
|
- fragment = ( nvo->data + nvo->total_len );
|
243
|
|
- fragment->len = nvo->total_len;
|
244
|
|
- nvo->fragments = fragment;
|
245
|
|
- }
|
246
|
|
-
|
247
|
222
|
/* Read data from NVS */
|
248
|
223
|
if ( ( rc = nvo_load ( nvo ) ) != 0 )
|
249
|
224
|
goto err_load;
|