Switch from GPLv2 to GPLv2+
[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 uint8_t
23 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
24 {
25         uint16_t this_len;
26         uint16_t this_off;
27
28         ao_storage_setup();
29         if (pos >= ao_storage_total || pos + len > ao_storage_total)
30                 return 0;
31         while (len) {
32
33                 /* Compute portion of transfer within
34                  * a single block
35                  */
36                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
37                 this_len = ao_storage_unit - this_off;
38                 if (this_len > len)
39                         this_len = len;
40
41                 if (!ao_storage_device_read(pos, buf, this_len))
42                         return 0;
43
44                 /* See how much is left */
45                 buf += this_len;
46                 len -= this_len;
47                 pos += this_len;
48         }
49         return 1;
50 }
51
52 uint8_t
53 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
54 {
55         uint16_t this_len;
56         uint16_t this_off;
57
58         ao_storage_setup();
59         if (pos >= ao_storage_total || pos + len > ao_storage_total)
60                 return 0;
61         while (len) {
62
63                 /* Compute portion of transfer within
64                  * a single block
65                  */
66                 this_off = (uint16_t) pos & (ao_storage_unit - 1);
67                 this_len = ao_storage_unit - this_off;
68                 if (this_len > len)
69                         this_len = len;
70
71                 if (!ao_storage_device_write(pos, buf, this_len))
72                         return 0;
73
74                 /* See how much is left */
75                 buf += this_len;
76                 len -= this_len;
77                 pos += this_len;
78         }
79         return 1;
80 }
81
82 static __xdata uint8_t storage_data[8];
83
84 static void
85 ao_storage_dump(void) __reentrant
86 {
87         uint8_t i, j;
88
89         ao_cmd_hex();
90         if (ao_cmd_status != ao_cmd_success)
91                 return;
92         for (i = 0; ; i += 8) {
93                 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
94                                   storage_data,
95                                   8)) {
96                         ao_cmd_put16((uint16_t) i);
97                         for (j = 0; j < 8; j++) {
98                                 putchar(' ');
99                                 ao_cmd_put8(storage_data[j]);
100                         }
101                         putchar ('\n');
102                 }
103                 if (i == 248)
104                         break;
105         }
106 }
107
108 #if HAS_STORAGE_DEBUG
109
110 /* not enough space for this today
111  */
112 static void
113 ao_storage_store(void) __reentrant
114 {
115         uint16_t block;
116         uint8_t i;
117         uint16_t len;
118         static __xdata uint8_t b;
119         uint32_t addr;
120
121         ao_cmd_hex();
122         block = ao_cmd_lex_i;
123         ao_cmd_hex();
124         i = ao_cmd_lex_i;
125         addr = ((uint32_t) block << 8) | i;
126         ao_cmd_hex();
127         len = ao_cmd_lex_i;
128         if (ao_cmd_status != ao_cmd_success)
129                 return;
130         while (len--) {
131                 ao_cmd_hex();
132                 if (ao_cmd_status != ao_cmd_success)
133                         return;
134                 b = ao_cmd_lex_i;
135                 ao_storage_write(addr, &b, 1);
136                 addr++;
137         }
138 }
139 #endif
140
141 void
142 ao_storage_zap(void) __reentrant
143 {
144         ao_cmd_hex();
145         if (ao_cmd_status != ao_cmd_success)
146                 return;
147         ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
148 }
149
150 void
151 ao_storage_zapall(void) __reentrant
152 {
153         uint32_t        pos;
154
155         ao_cmd_white();
156         if (!ao_match_word("DoIt"))
157                 return;
158         for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
159                 ao_storage_erase(pos);
160 }
161
162 void
163 ao_storage_info(void) __reentrant
164 {
165         ao_storage_setup();
166         printf("Storage size: %ld\n", (long) ao_storage_total);
167         printf("Storage erase unit: %ld\n", (long) ao_storage_block);
168         ao_storage_device_info();
169 }
170
171 __code struct ao_cmds ao_storage_cmds[] = {
172         { ao_storage_info, "f\0Show storage" },
173         { ao_storage_dump, "e <block>\0Dump flash" },
174 #if HAS_STORAGE_DEBUG
175         { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
176 #endif
177         { ao_storage_zap, "z <block>\0Erase <block>" },
178         { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
179         { 0, NULL },
180 };
181
182 void
183 ao_storage_init(void)
184 {
185         ao_storage_device_init();
186         ao_cmd_register(&ao_storage_cmds[0]);
187 }