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];
25 static uint8_t storage_mutex;
28 ao_storage_read(ao_pos_t pos, void *v_buf, uint16_t len)
35 if (pos >= ao_storage_total || pos + len > ao_storage_total)
39 /* Compute portion of transfer within
42 this_off = (uint16_t) (pos & (ao_storage_unit - 1));
43 this_len = ao_storage_unit - this_off;
47 if (!ao_storage_device_read(pos, buf, this_len))
50 /* See how much is left */
59 ao_storage_write(ao_pos_t pos, void *v_buf, uint16_t 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 ao_storage_is_erased(uint32_t pos)
98 ao_mutex_get(&storage_mutex);
100 read_len = ao_storage_block;
102 uint32_t this_time = AO_STORAGE_DATA_SIZE;
103 if (this_time > read_len)
104 this_time = read_len;
105 if (!ao_storage_read(read_pos, storage_data, (uint16_t) this_time)) {
109 for (i = 0; i < this_time; i++)
110 if (storage_data[i] != AO_STORAGE_ERASED_BYTE) {
114 read_pos += this_time;
115 read_len -= this_time;
118 ao_mutex_put(&storage_mutex);
123 ao_storage_erase(uint32_t start_pos, uint32_t len)
125 /* Round 'len' up to ao_storage_block units */
126 len = ((len + ao_storage_block - 1) / ao_storage_block) * ao_storage_block;
129 * Start at the end of the area to erase so that the
130 * last block cleared is the first block; this will ensure
131 * that partially erased flight logs still appear in the list
132 * and can be re-erased.
134 uint32_t pos = start_pos + len - ao_storage_block;
138 #define MAX_TRIES 4 /* needs to be at least 2 */
139 for (tries = 0; tries < MAX_TRIES; tries++) {
140 if (ao_storage_is_erased(pos))
142 if (!ao_storage_device_erase(pos))
145 if (tries == MAX_TRIES)
147 pos -= ao_storage_block;
148 len -= ao_storage_block;
154 ao_storage_dump(void)
159 block = ao_cmd_hex();
160 if (ao_cmd_status != ao_cmd_success)
162 ao_mutex_get(&storage_mutex);
163 for (i = 0; ; i += AO_STORAGE_DATA_SIZE) {
164 if (ao_storage_read((block << 8) + i,
166 AO_STORAGE_DATA_SIZE)) {
167 for (k = 0; k < AO_STORAGE_DATA_SIZE; k += 8) {
168 ao_cmd_put16((uint16_t) i + k);
169 for (j = 0; j < 8; j++) {
171 ao_cmd_put8(storage_data[k + j]);
176 if (i == 256 - AO_STORAGE_DATA_SIZE)
179 ao_mutex_put(&storage_mutex);
182 #if HAS_STORAGE_DEBUG
184 /* not enough space for this today
187 ao_storage_store(void)
195 block = ao_cmd_hex();
197 addr = ((uint32_t) block << 8) | i;
199 if (ao_cmd_status != ao_cmd_success)
202 b = ao_cmd_hexbyte();
203 if (ao_cmd_status != ao_cmd_success)
205 ao_storage_write(addr, &b, 1);
214 uint32_t v = ao_cmd_hex();
215 if (ao_cmd_status != ao_cmd_success)
217 ao_storage_erase((uint32_t) v << 8, ao_storage_block);
221 ao_storage_zapall(void)
224 if (!ao_match_word("DoIt"))
226 ao_storage_erase(0, ao_storage_log_max);
231 #define AO_STORAGE_TEST_SIZE 256
232 static uint8_t storage_test[AO_STORAGE_TEST_SIZE];
235 ao_storage_failure(uint32_t pos, char *format, ...)
238 printf("TEST FAILURE AT %08x: ", pos);
245 ao_storage_check_block(uint32_t pos, uint8_t value)
250 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
251 if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
252 ao_storage_failure(pos + offset, "read failed\n");
255 for (byte = 0; byte < sizeof (storage_test); byte++)
256 if (storage_test[byte] != value) {
257 ao_storage_failure(pos + offset + byte,
258 "want %02x got %02x\n",
259 value, storage_test[byte]);
267 ao_storage_fill_block(uint32_t pos, uint8_t value)
272 for (byte = 0; byte < sizeof (storage_test); byte++)
273 storage_test[byte] = value;
274 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
275 if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
276 ao_storage_failure(pos + offset, "write failed\n");
284 ao_storage_check_incr_block(uint32_t pos)
289 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
290 if (!ao_storage_read(pos + offset, storage_test, sizeof (storage_test))) {
291 ao_storage_failure(pos + offset, "read failed\n");
294 for (byte = 0; byte < sizeof (storage_test); byte++) {
295 uint8_t value = offset + byte;
296 if (storage_test[byte] != value) {
297 ao_storage_failure(pos + offset + byte,
298 "want %02x got %02x\n",
299 value, storage_test[byte]);
308 ao_storage_fill_incr_block(uint32_t pos)
313 for (offset = 0; offset < ao_storage_block; offset += sizeof (storage_test)) {
314 for (byte = 0; byte < sizeof (storage_test); byte++)
315 storage_test[byte] = offset + byte;
316 if (!ao_storage_write(pos + offset, storage_test, sizeof (storage_test))) {
317 ao_storage_failure(pos + offset, "write failed\n");
325 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
327 return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
331 ao_storage_incr_check_block(uint32_t pos)
333 return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
337 ao_storage_test_block(uint32_t pos)
339 ao_storage_erase(pos, ao_storage_block);
340 printf(" erase"); flush();
341 if (!ao_storage_check_block(pos, 0xff))
343 printf(" zero"); flush();
344 if (!ao_storage_fill_check_block(pos, 0x00))
346 ao_storage_erase(pos, ao_storage_block);
347 printf(" 0xaa"); flush();
348 if (!ao_storage_fill_check_block(pos, 0xaa))
350 ao_storage_erase(pos, ao_storage_block);
351 printf(" 0x55"); flush();
352 if (!ao_storage_fill_check_block(pos, 0x55))
354 ao_storage_erase(pos, ao_storage_block);
355 printf(" increment"); flush();
356 if (!ao_storage_incr_check_block(pos))
358 ao_storage_erase(pos, ao_storage_block);
359 printf(" pass\n"); flush();
364 ao_storage_test(void)
369 if (!ao_match_word("DoIt"))
371 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block) {
372 printf("Testing block 0x%08x:", pos); flush();
373 if (!ao_storage_test_block(pos))
376 printf("Test complete\n");
380 ao_storage_fill(void)
385 if (!ao_match_word("DoIt"))
387 printf("erase "); flush();
388 ao_storage_erase(0, ao_storage_log_max);
389 for (pos = 0; pos < sizeof (storage_test); pos++)
390 storage_test[pos] = (uint8_t) pos;
391 for (pos = 0; pos < ao_storage_log_max; pos += sizeof (storage_test)) {
392 if ((pos & 0xffff) == 0) {
393 printf("Fill 0x%x\n", pos); flush();
395 ao_storage_write(pos, storage_test, sizeof (storage_test));
397 printf("Fill complete\n");
399 #endif /* AO_STORAGE_TEST */
402 ao_storage_info(void)
405 printf("Storage size: %ld\n", (long) ao_storage_total);
406 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
407 ao_storage_device_info();
410 const struct ao_cmds ao_storage_cmds[] = {
411 { ao_storage_info, "f\0Show storage" },
412 { ao_storage_dump, "e <block>\0Dump flash" },
413 #if HAS_STORAGE_DEBUG
414 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
416 { ao_storage_zap, "z <block>\0Erase <block>" },
417 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
419 { ao_storage_test, "V <key>\0Validate flash (destructive). <key> is doit with D&I" },
420 { ao_storage_fill, "F <key>\0Fill flash with data. <key> is doit with D&I" },
426 ao_storage_init(void)
428 ao_storage_device_init();
429 ao_cmd_register(&ao_storage_cmds[0]);