]> git.gag.com Git - fw/altos/blob - src/kernel/ao_storage.c
altosdroid: Place icons on screen instead of drawing path
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_storage.h>
20
21 uint8_t
22 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
23 {
24         uint16_t this_len;
25         uint16_t this_off;
26
27         ao_storage_setup();
28         if (pos >= ao_storage_total || pos + len > ao_storage_total)
29                 return 0;
30         while (len) {
31
32                 /* Compute portion of transfer within
33                  * a single block
34                  */
35                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
36                 this_len = ao_storage_unit - this_off;
37                 if (this_len > len)
38                         this_len = len;
39
40                 if (!ao_storage_device_read(pos, buf, this_len))
41                         return 0;
42
43                 /* See how much is left */
44                 buf += this_len;
45                 len -= this_len;
46                 pos += this_len;
47         }
48         return 1;
49 }
50
51 uint8_t
52 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
53 {
54         uint16_t this_len;
55         uint16_t this_off;
56
57         ao_storage_setup();
58         if (pos >= ao_storage_total || pos + len > ao_storage_total)
59                 return 0;
60         while (len) {
61
62                 /* Compute portion of transfer within
63                  * a single block
64                  */
65                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
66                 this_len = ao_storage_unit - this_off;
67                 if (this_len > len)
68                         this_len = len;
69
70                 if (!ao_storage_device_write(pos, buf, this_len))
71                         return 0;
72
73                 /* See how much is left */
74                 buf += this_len;
75                 len -= this_len;
76                 pos += this_len;
77         }
78         return 1;
79 }
80
81 static __xdata uint8_t storage_data[8];
82
83 static void
84 ao_storage_dump(void) __reentrant
85 {
86         uint8_t i, j;
87
88         ao_cmd_hex();
89         if (ao_cmd_status != ao_cmd_success)
90                 return;
91         for (i = 0; ; i += 8) {
92                 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
93                                   storage_data,
94                                   8)) {
95                         ao_cmd_put16((uint16_t) i);
96                         for (j = 0; j < 8; j++) {
97                                 putchar(' ');
98                                 ao_cmd_put8(storage_data[j]);
99                         }
100                         putchar ('\n');
101                 }
102                 if (i == 248)
103                         break;
104         }
105 }
106
107 #if HAS_STORAGE_DEBUG
108
109 /* not enough space for this today
110  */
111 static void
112 ao_storage_store(void) __reentrant
113 {
114         uint16_t block;
115         uint8_t i;
116         uint16_t len;
117         static __xdata uint8_t b;
118         uint32_t addr;
119
120         ao_cmd_hex();
121         block = ao_cmd_lex_i;
122         ao_cmd_hex();
123         i = ao_cmd_lex_i;
124         addr = ((uint32_t) block << 8) | i;
125         ao_cmd_hex();
126         len = ao_cmd_lex_i;
127         if (ao_cmd_status != ao_cmd_success)
128                 return;
129         while (len--) {
130                 ao_cmd_hex();
131                 if (ao_cmd_status != ao_cmd_success)
132                         return;
133                 b = ao_cmd_lex_i;
134                 ao_storage_write(addr, &b, 1);
135                 addr++;
136         }
137 }
138 #endif
139
140 void
141 ao_storage_zap(void) __reentrant
142 {
143         ao_cmd_hex();
144         if (ao_cmd_status != ao_cmd_success)
145                 return;
146         ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
147 }
148
149 void
150 ao_storage_zapall(void) __reentrant
151 {
152         uint32_t        pos;
153
154         ao_cmd_white();
155         if (!ao_match_word("DoIt"))
156                 return;
157         for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
158                 ao_storage_erase(pos);
159 }
160
161 void
162 ao_storage_info(void) __reentrant
163 {
164         ao_storage_setup();
165         printf("Storage size: %ld\n", (long) ao_storage_total);
166         printf("Storage erase unit: %ld\n", (long) ao_storage_block);
167         ao_storage_device_info();
168 }
169
170 __code struct ao_cmds ao_storage_cmds[] = {
171         { ao_storage_info, "f\0Show storage" },
172         { ao_storage_dump, "e <block>\0Dump flash" },
173 #if HAS_STORAGE_DEBUG
174         { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
175 #endif
176         { ao_storage_zap, "z <block>\0Erase <block>" },
177         { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
178         { 0, NULL },
179 };
180
181 void
182 ao_storage_init(void)
183 {
184         ao_storage_device_init();
185         ao_cmd_register(&ao_storage_cmds[0]);
186 }