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 256
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);
218 ao_storage_failure(uint32_t pos, char *format, ...)
221 printf("TEST FAILURE AT %08x: ", pos);
228 ao_storage_check_block(uint32_t pos, uint8_t value)
233 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
234 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
235 ao_storage_failure(pos + offset, "read failed\n");
238 for (byte = 0; byte < sizeof (storage_data); byte++)
239 if (storage_data[byte] != value) {
240 ao_storage_failure(pos + offset + byte,
241 "want %02x got %02x\n",
242 value, storage_data[byte]);
250 ao_storage_fill_block(uint32_t pos, uint8_t value)
255 for (byte = 0; byte < sizeof (storage_data); byte++)
256 storage_data[byte] = value;
257 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
258 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
259 ao_storage_failure(pos + offset, "write failed\n");
267 ao_storage_check_incr_block(uint32_t pos)
272 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
273 if (!ao_storage_read(pos + offset, storage_data, sizeof (storage_data))) {
274 ao_storage_failure(pos + offset, "read failed\n");
277 for (byte = 0; byte < sizeof (storage_data); byte++) {
278 uint8_t value = offset + byte;
279 if (storage_data[byte] != value) {
280 ao_storage_failure(pos + offset + byte,
281 "want %02x got %02x\n",
282 value, storage_data[byte]);
291 ao_storage_fill_incr_block(uint32_t pos)
296 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_data)) {
297 for (byte = 0; byte < sizeof (storage_data); byte++)
298 storage_data[byte] = offset + byte;
299 if (!ao_storage_write(pos + offset, storage_data, sizeof (storage_data))) {
300 ao_storage_failure(pos + offset, "write failed\n");
308 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
310 return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
314 ao_storage_incr_check_block(uint32_t pos)
316 return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
320 ao_storage_test_block(uint32_t pos)
322 ao_storage_erase(pos, ao_storage_block);
323 printf(" erase"); flush();
324 if (!ao_storage_check_block(pos, 0xff))
326 printf(" zero"); flush();
327 if (!ao_storage_fill_check_block(pos, 0x00))
329 ao_storage_erase(pos, ao_storage_block);
330 printf(" 0xaa"); flush();
331 if (!ao_storage_fill_check_block(pos, 0xaa))
333 ao_storage_erase(pos, ao_storage_block);
334 printf(" 0x55"); flush();
335 if (!ao_storage_fill_check_block(pos, 0x55))
337 ao_storage_erase(pos, ao_storage_block);
338 printf(" increment"); flush();
339 if (!ao_storage_incr_check_block(pos))
341 ao_storage_erase(pos, ao_storage_block);
342 printf(" pass\n"); flush();
347 ao_storage_test(void)
352 if (!ao_match_word("DoIt"))
354 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
355 printf("Testing block 0x%08x:", pos); flush();
356 if (!ao_storage_test_block(pos))
359 printf("Test complete\n");
363 ao_storage_fill(void)
368 if (!ao_match_word("DoIt"))
370 printf("erase "); flush();
371 ao_storage_erase(0, ao_storage_log_max);
372 for (pos = 0; pos < sizeof (storage_data); pos++)
373 storage_data[pos] = (uint8_t) pos;
374 for (pos = 0; pos < ao_storage_log_max; pos += sizeof (storage_data)) {
375 if ((pos & 0xffff) == 0) {
376 printf("Fill 0x%x\n", pos); flush();
378 ao_storage_write(pos, storage_data, sizeof (storage_data));
380 printf("Fill complete\n");
382 #endif /* AO_STORAGE_TEST */
385 ao_storage_info(void)
388 printf("Storage size: %ld\n", (long) ao_storage_total);
389 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
390 ao_storage_device_info();
393 const struct ao_cmds ao_storage_cmds[] = {
394 { ao_storage_info, "f\0Show storage" },
395 { ao_storage_dump, "e <block>\0Dump flash" },
396 #if HAS_STORAGE_DEBUG
397 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
399 { ao_storage_zap, "z <block>\0Erase <block>" },
400 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
402 { ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
403 { ao_storage_fill, "F <key>\0Fill flash with data. <key> is doit with D&I" },
409 ao_storage_init(void)
411 ao_storage_device_init();
412 ao_cmd_register(&ao_storage_cmds[0]);