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>
22 #define AO_STORAGE_DATA_SIZE 128
24 static uint8_t storage_data[AO_STORAGE_DATA_SIZE];
27 ao_storage_read(ao_pos_t pos, void *v_buf, uint16_t len)
34 if (pos >= ao_storage_total || pos + len > ao_storage_total)
38 /* Compute portion of transfer within
41 this_off = (uint16_t) pos & (ao_storage_unit - 1);
42 this_len = ao_storage_unit - this_off;
46 if (!ao_storage_device_read(pos, buf, this_len))
49 /* See how much is left */
58 ao_storage_write(ao_pos_t pos, void *v_buf, uint16_t len)
65 if (pos >= ao_storage_total || pos + len > ao_storage_total)
69 /* Compute portion of transfer within
72 this_off = (uint16_t) pos & (ao_storage_unit - 1);
73 this_len = ao_storage_unit - this_off;
77 if (!ao_storage_device_write(pos, buf, this_len))
80 /* See how much is left */
89 ao_storage_is_erased(uint32_t pos)
96 read_len = ao_storage_block;
98 uint32_t this_time = AO_STORAGE_DATA_SIZE;
99 if (this_time > read_len)
100 this_time = read_len;
101 if (!ao_storage_read(read_pos, storage_data, this_time))
103 for (i = 0; i < this_time; i++)
104 if (storage_data[i] != 0xff)
106 read_pos += this_time;
107 read_len -= this_time;
113 ao_storage_erase(uint32_t start_pos, uint32_t len)
115 /* Round 'len' up to ao_storage_block units */
116 len = ((len + ao_storage_block - 1) / ao_storage_block) * ao_storage_block;
119 * Start at the end of the area to erase so that the
120 * last block cleared is the first block; this will ensure
121 * that partially erased flight logs still appear in the list
122 * and can be re-erased.
124 uint32_t pos = start_pos + len - ao_storage_block;
128 #define MAX_TRIES 4 /* needs to be at least 2 */
129 for (tries = 0; tries < MAX_TRIES; tries++) {
130 if (ao_storage_is_erased(pos))
132 if (!ao_storage_device_erase(pos))
135 if (tries == MAX_TRIES)
137 pos -= ao_storage_block;
138 len -= ao_storage_block;
144 ao_storage_dump(void)
149 block = ao_cmd_hex();
150 if (ao_cmd_status != ao_cmd_success)
152 for (i = 0; ; i += 8) {
153 if (ao_storage_read((block << 8) + i,
156 ao_cmd_put16((uint16_t) i);
157 for (j = 0; j < 8; j++) {
159 ao_cmd_put8(storage_data[j]);
168 #if HAS_STORAGE_DEBUG
170 /* not enough space for this today
173 ao_storage_store(void)
181 block = ao_cmd_hex();
183 addr = ((uint32_t) block << 8) | i;
185 if (ao_cmd_status != ao_cmd_success)
188 b = ao_cmd_hexbyte();
189 if (ao_cmd_status != ao_cmd_success)
191 ao_storage_write(addr, &b, 1);
200 uint32_t v = ao_cmd_hex();
201 if (ao_cmd_status != ao_cmd_success)
203 ao_storage_erase((uint32_t) v << 8, ao_storage_block);
207 ao_storage_zapall(void)
210 if (!ao_match_word("DoIt"))
212 ao_storage_erase(0, ao_storage_log_max);
217 #define AO_STORAGE_TEST_SIZE 256
218 static uint8_t storage_test[AO_STORAGE_TEST_SIZE];
221 ao_storage_failure(uint32_t pos, char *format, ...)
224 printf("TEST FAILURE AT %08x: ", pos);
231 ao_storage_check_block(uint32_t pos, uint8_t value)
236 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
237 if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
238 ao_storage_failure(pos + offset, "read failed\n");
241 for (byte = 0; byte < sizeof (storage_test); byte++)
242 if (storage_test[byte] != value) {
243 ao_storage_failure(pos + offset + byte,
244 "want %02x got %02x\n",
245 value, storage_test[byte]);
253 ao_storage_fill_block(uint32_t pos, uint8_t value)
258 for (byte = 0; byte < sizeof (storage_test); byte++)
259 storage_test[byte] = value;
260 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
261 if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
262 ao_storage_failure(pos + offset, "write failed\n");
270 ao_storage_check_incr_block(uint32_t pos)
275 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
276 if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
277 ao_storage_failure(pos + offset, "read failed\n");
280 for (byte = 0; byte < sizeof (storage_test); byte++) {
281 uint8_t value = offset + byte;
282 if (storage_test[byte] != value) {
283 ao_storage_failure(pos + offset + byte,
284 "want %02x got %02x\n",
285 value, storage_test[byte]);
294 ao_storage_fill_incr_block(uint32_t pos)
299 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
300 for (byte = 0; byte < sizeof (storage_test); byte++)
301 storage_test[byte] = offset + byte;
302 if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
303 ao_storage_failure(pos + offset, "write failed\n");
311 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
313 return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
317 ao_storage_incr_check_block(uint32_t pos)
319 return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
323 ao_storage_test_block(uint32_t pos)
325 ao_storage_erase(pos, ao_storage_block);
326 printf(" erase"); flush();
327 if (!ao_storage_check_block(pos, 0xff))
329 printf(" zero"); flush();
330 if (!ao_storage_fill_check_block(pos, 0x00))
332 ao_storage_erase(pos, ao_storage_block);
333 printf(" 0xaa"); flush();
334 if (!ao_storage_fill_check_block(pos, 0xaa))
336 ao_storage_erase(pos, ao_storage_block);
337 printf(" 0x55"); flush();
338 if (!ao_storage_fill_check_block(pos, 0x55))
340 ao_storage_erase(pos, ao_storage_block);
341 printf(" increment"); flush();
342 if (!ao_storage_incr_check_block(pos))
344 ao_storage_erase(pos, ao_storage_block);
345 printf(" pass\n"); flush();
350 ao_storage_test(void)
355 if (!ao_match_word("DoIt"))
357 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
358 printf("Testing block 0x%08x:", pos); flush();
359 if (!ao_storage_test_block(pos))
362 printf("Test complete\n");
366 ao_storage_fill(void)
371 if (!ao_match_word("DoIt"))
373 printf("erase "); flush();
374 ao_storage_erase(0, ao_storage_log_max);
375 for (pos = 0; pos < sizeof (storage_test); pos++)
376 storage_test[pos] = (uint8_t) pos;
377 for (pos = 0; pos < ao_storage_log_max; pos += sizeof (storage_test)) {
378 if ((pos & 0xffff) == 0) {
379 printf("Fill 0x%x\n", pos); flush();
381 ao_storage_write(pos, storage_test, sizeof (storage_test));
383 printf("Fill complete\n");
385 #endif /* AO_STORAGE_TEST */
388 ao_storage_info(void)
391 printf("Storage size: %ld\n", (long) ao_storage_total);
392 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
393 ao_storage_device_info();
396 const struct ao_cmds ao_storage_cmds[] = {
397 { ao_storage_info, "f\0Show storage" },
398 { ao_storage_dump, "e <block>\0Dump flash" },
399 #if HAS_STORAGE_DEBUG
400 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
402 { ao_storage_zap, "z <block>\0Erase <block>" },
403 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
405 { ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
406 { ao_storage_fill, "F <key>\0Fill flash with data. <key> is doit with D&I" },
412 ao_storage_init(void)
414 ao_storage_device_init();
415 ao_cmd_register(&ao_storage_cmds[0]);