2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_storage.h>
23 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
26 return ao_storage_device_read(pos, buf, len);
32 if (pos >= ao_storage_total || pos + len > ao_storage_total)
36 /* Compute portion of transfer within
39 this_off = (uint16_t) pos & (ao_storage_unit - 1);
40 this_len = ao_storage_unit - this_off;
44 if (!ao_storage_device_read(pos, buf, this_len))
47 /* See how much is left */
57 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
60 return ao_storage_device_write(pos, buf, len);
66 if (pos >= ao_storage_total || pos + len > ao_storage_total)
70 /* Compute portion of transfer within
73 this_off = (uint16_t) pos & (ao_storage_unit - 1);
74 this_len = ao_storage_unit - this_off;
78 if (!ao_storage_device_write(pos, buf, this_len))
81 /* See how much is left */
90 static __xdata uint8_t storage_data[128];
93 ao_storage_dump(void) __reentrant
98 if (ao_cmd_status != ao_cmd_success)
100 for (i = 0; ; i += 8) {
101 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
104 ao_cmd_put16((uint16_t) i);
105 for (j = 0; j < 8; j++) {
107 ao_cmd_put8(storage_data[j]);
116 #if HAS_STORAGE_DEBUG
118 /* not enough space for this today
121 ao_storage_store(void) __reentrant
126 static __xdata uint8_t b;
130 block = ao_cmd_lex_i;
133 addr = ((uint32_t) block << 8) | i;
136 if (ao_cmd_status != ao_cmd_success)
140 if (ao_cmd_status != ao_cmd_success)
143 ao_storage_write(addr, &b, 1);
150 ao_storage_zap(void) __reentrant
153 if (ao_cmd_status != ao_cmd_success)
155 ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
159 ao_storage_zapall(void) __reentrant
164 if (!ao_match_word("DoIt"))
166 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
167 ao_storage_erase(pos);
173 ao_storage_failure(uint32_t pos, char *format, ...)
176 printf("TEST FAILURE AT %08x: ", pos);
183 ao_storage_check_block(uint32_t pos, uint8_t value)
188 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
189 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
190 ao_storage_failure(pos + offset, "read failed\n");
193 for (byte = 0; byte < sizeof (storage_data); byte++)
194 if (storage_data[byte] != value) {
195 ao_storage_failure(pos + offset + byte,
196 "want %02x got %02x\n",
197 value, storage_data[byte]);
205 ao_storage_fill_block(uint32_t pos, uint8_t value)
210 for (byte = 0; byte < sizeof (storage_data); byte++)
211 storage_data[byte] = value;
212 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
213 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
214 ao_storage_failure(pos + offset, "write failed\n");
222 ao_storage_check_incr_block(uint32_t pos)
227 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
228 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
229 ao_storage_failure(pos + offset, "read failed\n");
232 for (byte = 0; byte < sizeof (storage_data); byte++) {
233 uint8_t value = offset + byte;
234 if (storage_data[byte] != value) {
235 ao_storage_failure(pos + offset + byte,
236 "want %02x got %02x\n",
237 value, storage_data[byte]);
246 ao_storage_fill_incr_block(uint32_t pos)
251 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
252 for (byte = 0; byte < sizeof (storage_data); byte++)
253 storage_data[byte] = offset + byte;
254 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
255 ao_storage_failure(pos + offset, "write failed\n");
263 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
265 return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
269 ao_storage_incr_check_block(uint32_t pos)
271 return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
275 ao_storage_test_block(uint32_t pos) __reentrant
277 ao_storage_erase(pos);
278 printf(" erase"); flush();
279 if (!ao_storage_check_block(pos, 0xff))
281 printf(" zero"); flush();
282 if (!ao_storage_fill_check_block(pos, 0x00))
284 ao_storage_erase(pos);
285 printf(" 0xaa"); flush();
286 if (!ao_storage_fill_check_block(pos, 0xaa))
288 ao_storage_erase(pos);
289 printf(" 0x55"); flush();
290 if (!ao_storage_fill_check_block(pos, 0x55))
292 ao_storage_erase(pos);
293 printf(" increment"); flush();
294 if (!ao_storage_incr_check_block(pos))
296 ao_storage_erase(pos);
297 printf(" pass\n"); flush();
302 ao_storage_test(void) __reentrant
307 if (!ao_match_word("DoIt"))
309 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
310 printf("Testing block 0x%08x:", pos); flush();
311 if (!ao_storage_test_block(pos))
314 printf("Test complete\n");
316 #endif /* AO_STORAGE_TEST */
319 ao_storage_info(void) __reentrant
322 printf("Storage size: %ld\n", (long) ao_storage_total);
323 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
324 ao_storage_device_info();
327 __code struct ao_cmds ao_storage_cmds[] = {
328 { ao_storage_info, "f\0Show storage" },
329 { ao_storage_dump, "e <block>\0Dump flash" },
330 #if HAS_STORAGE_DEBUG
331 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
333 { ao_storage_zap, "z <block>\0Erase <block>" },
334 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
336 { ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
342 ao_storage_init(void)
344 ao_storage_device_init();
345 ao_cmd_register(&ao_storage_cmds[0]);