summaryrefslogtreecommitdiff
path: root/src/kernel/ao_storage.c
blob: 400751de03ca5d8a95debcac49403fa32b681b61 (plain) (blame)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright © 2011 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <ao.h>
#include <ao_storage.h>

uint8_t
ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
{
#ifdef CC1111
	return ao_storage_device_read(pos, buf, len);
#else
	uint16_t this_len;
	uint16_t this_off;

	ao_storage_setup();
	if (pos >= ao_storage_total || pos + len > ao_storage_total)
		return 0;
	while (len) {

		/* Compute portion of transfer within
		 * a single block
		 */
		this_off = (uint16_t) pos & (ao_storage_unit - 1);
		this_len = ao_storage_unit - this_off;
		if (this_len > len)
			this_len = len;

		if (!ao_storage_device_read(pos, buf, this_len))
			return 0;

		/* See how much is left */
		buf += this_len;
		len -= this_len;
		pos += this_len;
	}
	return 1;
#endif
}

uint8_t
ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
{
#ifdef CC1111
	return ao_storage_device_write(pos, buf, len);
#else
	uint16_t this_len;
	uint16_t this_off;

	ao_storage_setup();
	if (pos >= ao_storage_total || pos + len > ao_storage_total)
		return 0;
	while (len) {

		/* Compute portion of transfer within
		 * a single block
		 */
		this_off = (uint16_t) pos & (ao_storage_unit - 1);
		this_len = ao_storage_unit - this_off;
		if (this_len > len)
			this_len = len;

		if (!ao_storage_device_write(pos, buf, this_len))
			return 0;

		/* See how much is left */
		buf += this_len;
		len -= this_len;
		pos += this_len;
	}
	return 1;
#endif
}

static __xdata uint8_t storage_data[128];

static void
ao_storage_dump(void) __reentrant
{
	uint8_t i, j;

	ao_cmd_hex();
	if (ao_cmd_status != ao_cmd_success)
		return;
	for (i = 0; ; i += 8) {
		if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
				  storage_data,
				  8)) {
			ao_cmd_put16((uint16_t) i);
			for (j = 0; j < 8; j++) {
				putchar(' ');
				ao_cmd_put8(storage_data[j]);
			}
			putchar ('\n');
		}
		if (i == 248)
			break;
	}
}

#if HAS_STORAGE_DEBUG

/* not enough space for this today
 */
static void
ao_storage_store(void) __reentrant
{
	uint16_t block;
	uint8_t i;
	uint16_t len;
	static __xdata uint8_t b;
	uint32_t addr;

	ao_cmd_hex();
	block = ao_cmd_lex_i;
	ao_cmd_hex();
	i = ao_cmd_lex_i;
	addr = ((uint32_t) block << 8) | i;
	ao_cmd_hex();
	len = ao_cmd_lex_i;
	if (ao_cmd_status != ao_cmd_success)
		return;
	while (len--) {
		ao_cmd_hex();
		if (ao_cmd_status != ao_cmd_success)
			return;
		b = ao_cmd_lex_i;
		ao_storage_write(addr, &b, 1);
		addr++;
	}
}
#endif

void
ao_storage_zap(void) __reentrant
{
	ao_cmd_hex();
	if (ao_cmd_status != ao_cmd_success)
		return;
	ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
}

void
ao_storage_zapall(void) __reentrant
{
	uint32_t	pos;

	ao_cmd_white();
	if (!ao_match_word("DoIt"))
		return;
	for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
		ao_storage_erase(pos);
}

#if AO_STORAGE_TEST

static void
ao_storage_failure(uint32_t pos, char *format, ...)
{
	va_list a;
	printf("TEST FAILURE AT %08x: ", pos);
	va_start(a, format);
	vprintf(format, a);
	va_end(a);
}

static uint8_t
ao_storage_check_block(uint32_t pos, uint8_t value)
{
	uint32_t	offset;
	uint32_t	byte;

	for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
		if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
			ao_storage_failure(pos + offset, "read failed\n");
			return 0;
		}
		for (byte = 0; byte < sizeof (storage_data); byte++)
			if (storage_data[byte] != value) {
				ao_storage_failure(pos + offset + byte,
						   "want %02x got %02x\n",
						   value, storage_data[byte]);
				return 0;
			}
	}
	return 1;
}

static uint8_t
ao_storage_fill_block(uint32_t pos, uint8_t value)
{
	uint32_t	offset;
	uint32_t	byte;

	for (byte = 0; byte < sizeof (storage_data); byte++)
		storage_data[byte] = value;
	for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
		if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
			ao_storage_failure(pos + offset, "write failed\n");
			return 0;
		}
	}
	return 1;
}

static uint8_t
ao_storage_check_incr_block(uint32_t pos)
{
	uint32_t	offset;
	uint32_t	byte;

	for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
		if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
			ao_storage_failure(pos + offset, "read failed\n");
			return 0;
		}
		for (byte = 0; byte < sizeof (storage_data); byte++) {
			uint8_t value = offset + byte;
			if (storage_data[byte] != value) {
				ao_storage_failure(pos + offset + byte,
						   "want %02x got %02x\n",
						   value, storage_data[byte]);
				return 0;
			}
		}
	}
	return 1;
}

static uint8_t
ao_storage_fill_incr_block(uint32_t pos)
{
	uint32_t	offset;
	uint32_t	byte;

	for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
		for (byte = 0; byte < sizeof (storage_data); byte++)
			storage_data[byte] = offset + byte;
		if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
			ao_storage_failure(pos + offset, "write failed\n");
			return 0;
		}
	}
	return 1;
}

static uint8_t
ao_storage_fill_check_block(uint32_t pos, uint8_t value)
{
	return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
}

static uint8_t
ao_storage_incr_check_block(uint32_t pos)
{
	return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
}

static uint8_t
ao_storage_test_block(uint32_t pos) __reentrant
{
	ao_storage_erase(pos);
	printf(" erase"); flush();
	if (!ao_storage_check_block(pos, 0xff))
		return 0;
	printf(" zero"); flush();
	if (!ao_storage_fill_check_block(pos, 0x00))
		return 0;
	ao_storage_erase(pos);
	printf(" 0xaa"); flush();
	if (!ao_storage_fill_check_block(pos, 0xaa))
		return 0;
	ao_storage_erase(pos);
	printf(" 0x55"); flush();
	if (!ao_storage_fill_check_block(pos, 0x55))
		return 0;
	ao_storage_erase(pos);
	printf(" increment"); flush();
	if (!ao_storage_incr_check_block(pos))
		return 0;
	ao_storage_erase(pos);
	printf(" pass\n"); flush();
	return 1;
}

static void
ao_storage_test(void) __reentrant
{
	uint32_t	pos;

	ao_cmd_white();
	if (!ao_match_word("DoIt"))
		return;
	for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
		printf("Testing block 0x%08x:", pos); flush();
		if (!ao_storage_test_block(pos))
			break;
	}
	printf("Test complete\n");
}
#endif /* AO_STORAGE_TEST */

void
ao_storage_info(void) __reentrant
{
	ao_storage_setup();
	printf("Storage size: %ld\n", (long) ao_storage_total);
	printf("Storage erase unit: %ld\n", (long) ao_storage_block);
	ao_storage_device_info();
}

__code struct ao_cmds ao_storage_cmds[] = {
	{ ao_storage_info, "f\0Show storage" },
	{ ao_storage_dump, "e <block>\0Dump flash" },
#if HAS_STORAGE_DEBUG
	{ ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
#endif
	{ ao_storage_zap, "z <block>\0Erase <block>" },
	{ ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
#if AO_STORAGE_TEST
	{ ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
#endif
	{ 0, NULL },
};

void
ao_storage_init(void)
{
	ao_storage_device_init();
	ao_cmd_register(&ao_storage_cmds[0]);
}