b649e818758efbe27c9460e0d143e2609aba3012
[fw/altos] / src / kernel / ao_storage.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_storage.h>
21
22 #define AO_STORAGE_DATA_SIZE    128
23
24 static uint8_t storage_data[AO_STORAGE_DATA_SIZE];
25
26 uint8_t
27 ao_storage_read(ao_pos_t pos, void *v_buf, uint16_t len)
28 {
29         uint8_t *buf = v_buf;
30         uint16_t this_len;
31         uint16_t this_off;
32
33         ao_storage_setup();
34         if (pos >= ao_storage_total || pos + len > ao_storage_total)
35                 return 0;
36         while (len) {
37
38                 /* Compute portion of transfer within
39                  * a single block
40                  */
41                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
42                 this_len = ao_storage_unit - this_off;
43                 if (this_len > len)
44                         this_len = len;
45
46                 if (!ao_storage_device_read(pos, buf, this_len))
47                         return 0;
48
49                 /* See how much is left */
50                 buf += this_len;
51                 len -= this_len;
52                 pos += this_len;
53         }
54         return 1;
55 }
56
57 uint8_t
58 ao_storage_write(ao_pos_t pos, void *v_buf, uint16_t len) 
59 {
60         uint8_t *buf = v_buf;
61         uint16_t this_len;
62         uint16_t this_off;
63
64         ao_storage_setup();
65         if (pos >= ao_storage_total || pos + len > ao_storage_total)
66                 return 0;
67         while (len) {
68
69                 /* Compute portion of transfer within
70                  * a single block
71                  */
72                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
73                 this_len = ao_storage_unit - this_off;
74                 if (this_len > len)
75                         this_len = len;
76
77                 if (!ao_storage_device_write(pos, buf, this_len))
78                         return 0;
79
80                 /* See how much is left */
81                 buf += this_len;
82                 len -= this_len;
83                 pos += this_len;
84         }
85         return 1;
86 }
87
88 uint8_t
89 ao_storage_is_erased(uint32_t pos)
90 {
91         uint32_t        read_pos;
92         uint32_t        read_len;
93         uint32_t        i;
94
95         read_pos = pos;
96         read_len = ao_storage_block;
97         while (read_len) {
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))
102                         return 0;
103                 for (i = 0; i < this_time; i++)
104                         if (storage_data[i] != 0xff)
105                                 return 0;
106                 read_pos += this_time;
107                 read_len -= this_time;
108         }
109         return 1;
110 }
111
112 uint8_t
113 ao_storage_erase(uint32_t start_pos, uint32_t len)
114 {
115         /* Round 'len' up to ao_storage_block units */
116         len = ((len + ao_storage_block - 1) / ao_storage_block) * ao_storage_block;
117
118         /*
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.
123          */
124         uint32_t pos = start_pos + len - ao_storage_block;
125         while (len) {
126                 int tries;
127
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))
131                                 break;
132                         if (!ao_storage_device_erase(pos))
133                                 return 0;
134                 }
135                 if (tries == MAX_TRIES)
136                         return 0;
137                 pos -= ao_storage_block;
138                 len -= ao_storage_block;
139         }
140         return 1;
141 }
142
143 static void
144 ao_storage_dump(void) 
145 {
146         uint32_t block;
147         uint8_t i, j;
148
149         block = ao_cmd_hex();
150         if (ao_cmd_status != ao_cmd_success)
151                 return;
152         for (i = 0; ; i += 8) {
153                 if (ao_storage_read((block << 8) + i,
154                                     storage_data,
155                                     8)) {
156                         ao_cmd_put16((uint16_t) i);
157                         for (j = 0; j < 8; j++) {
158                                 putchar(' ');
159                                 ao_cmd_put8(storage_data[j]);
160                         }
161                         putchar ('\n');
162                 }
163                 if (i == 248)
164                         break;
165         }
166 }
167
168 #if HAS_STORAGE_DEBUG
169
170 /* not enough space for this today
171  */
172 static void
173 ao_storage_store(void) 
174 {
175         uint16_t block;
176         uint8_t i;
177         uint16_t len;
178         uint8_t b;
179         uint32_t addr;
180
181         block = ao_cmd_hex();
182         i = ao_cmd_hex();
183         addr = ((uint32_t) block << 8) | i;
184         len = ao_cmd_hex();
185         if (ao_cmd_status != ao_cmd_success)
186                 return;
187         while (len--) {
188                 b = ao_cmd_hexbyte();
189                 if (ao_cmd_status != ao_cmd_success)
190                         return;
191                 ao_storage_write(addr, &b, 1);
192                 addr++;
193         }
194 }
195 #endif
196
197 static void
198 ao_storage_zap(void) 
199 {
200         uint32_t v = ao_cmd_hex();
201         if (ao_cmd_status != ao_cmd_success)
202                 return;
203         ao_storage_erase((uint32_t) v << 8, ao_storage_block);
204 }
205
206 static void
207 ao_storage_zapall(void) 
208 {
209         ao_cmd_white();
210         if (!ao_match_word("DoIt"))
211                 return;
212         ao_storage_erase(0, ao_storage_log_max);
213 }
214
215 #if AO_STORAGE_TEST
216
217 static void
218 ao_storage_failure(uint32_t pos, char *format, ...)
219 {
220         va_list a;
221         printf("TEST FAILURE AT %08x: ", pos);
222         va_start(a, format);
223         vprintf(format, a);
224         va_end(a);
225 }
226
227 static uint8_t
228 ao_storage_check_block(uint32_t pos, uint8_t value)
229 {
230         uint32_t        offset;
231         uint32_t        byte;
232
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");
236                         return 0;
237                 }
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]);
243                                 return 0;
244                         }
245         }
246         return 1;
247 }
248
249 static uint8_t
250 ao_storage_fill_block(uint32_t pos, uint8_t value)
251 {
252         uint32_t        offset;
253         uint32_t        byte;
254
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");
260                         return 0;
261                 }
262         }
263         return 1;
264 }
265
266 static uint8_t
267 ao_storage_check_incr_block(uint32_t pos)
268 {
269         uint32_t        offset;
270         uint32_t        byte;
271
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");
275                         return 0;
276                 }
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]);
283                                 return 0;
284                         }
285                 }
286         }
287         return 1;
288 }
289
290 static uint8_t
291 ao_storage_fill_incr_block(uint32_t pos)
292 {
293         uint32_t        offset;
294         uint32_t        byte;
295
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");
301                         return 0;
302                 }
303         }
304         return 1;
305 }
306
307 static uint8_t
308 ao_storage_fill_check_block(uint32_t pos, uint8_t value)
309 {
310         return ao_storage_fill_block(pos, value) && ao_storage_check_block(pos, value);
311 }
312
313 static uint8_t
314 ao_storage_incr_check_block(uint32_t pos)
315 {
316         return ao_storage_fill_incr_block(pos) && ao_storage_check_incr_block(pos);
317 }
318
319 static uint8_t
320 ao_storage_test_block(uint32_t pos) 
321 {
322         ao_storage_erase(pos, ao_storage_block);
323         printf(" erase"); flush();
324         if (!ao_storage_check_block(pos, 0xff))
325                 return 0;
326         printf(" zero"); flush();
327         if (!ao_storage_fill_check_block(pos, 0x00))
328                 return 0;
329         ao_storage_erase(pos, ao_storage_block);
330         printf(" 0xaa"); flush();
331         if (!ao_storage_fill_check_block(pos, 0xaa))
332                 return 0;
333         ao_storage_erase(pos, ao_storage_block);
334         printf(" 0x55"); flush();
335         if (!ao_storage_fill_check_block(pos, 0x55))
336                 return 0;
337         ao_storage_erase(pos, ao_storage_block);
338         printf(" increment"); flush();
339         if (!ao_storage_incr_check_block(pos))
340                 return 0;
341         ao_storage_erase(pos, ao_storage_block);
342         printf(" pass\n"); flush();
343         return 1;
344 }
345
346 static void
347 ao_storage_test(void) 
348 {
349         uint32_t        pos;
350
351         ao_cmd_white();
352         if (!ao_match_word("DoIt"))
353                 return;
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))
357                         break;
358         }
359         printf("Test complete\n");
360 }
361
362 static void
363 ao_storage_fill(void)
364 {
365         uint32_t        pos;
366
367         ao_cmd_white();
368         if (!ao_match_word("DoIt"))
369                 return;
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();
377                 }
378                 ao_storage_write(pos, storage_data, sizeof (storage_data));
379         }
380         printf("Fill complete\n");
381 }
382 #endif /* AO_STORAGE_TEST */
383
384 static void
385 ao_storage_info(void) 
386 {
387         ao_storage_setup();
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();
391 }
392
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" },
398 #endif
399         { ao_storage_zap, "z <block>\0Erase <block>" },
400         { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
401 #if AO_STORAGE_TEST
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" },
404 #endif
405         { 0, NULL },
406 };
407
408 void
409 ao_storage_init(void)
410 {
411         ao_storage_device_init();
412         ao_cmd_register(&ao_storage_cmds[0]);
413 }