summaryrefslogtreecommitdiff
path: root/src/kernel/ao_storage.c
blob: 6eddae7f84dcdd754b8c46d54972d352f19fc754 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright © 2011 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <ao.h>
#include <ao_storage.h>

uint8_t
ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
{
	uint16_t this_len;
	uint16_t this_off;

	ao_storage_setup();
	if (pos >= ao_storage_total || pos + len > ao_storage_total)
		return 0;
	while (len) {

		/* Compute portion of transfer within
		 * a single block
		 */
		this_off = (uint16_t) pos & (ao_storage_unit - 1);
		this_len = ao_storage_unit - this_off;
		if (this_len > len)
			this_len = len;

		if (!ao_storage_device_read(pos, buf, this_len))
			return 0;

		/* See how much is left */
		buf += this_len;
		len -= this_len;
		pos += this_len;
	}
	return 1;
}

uint8_t
ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
{
	uint16_t this_len;
	uint16_t this_off;

	ao_storage_setup();
	if (pos >= ao_storage_total || pos + len > ao_storage_total)
		return 0;
	while (len) {

		/* Compute portion of transfer within
		 * a single block
		 */
		this_off = (uint16_t) pos & (ao_storage_unit - 1);
		this_len = ao_storage_unit - this_off;
		if (this_len > len)
			this_len = len;

		if (!ao_storage_device_write(pos, buf, this_len))
			return 0;

		/* See how much is left */
		buf += this_len;
		len -= this_len;
		pos += this_len;
	}
	return 1;
}

static __xdata uint8_t storage_data[8];

static void
ao_storage_dump(void) __reentrant
{
	uint8_t i, j;

	ao_cmd_hex();
	if (ao_cmd_status != ao_cmd_success)
		return;
	for (i = 0; ; i += 8) {
		if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
				  storage_data,
				  8)) {
			ao_cmd_put16((uint16_t) i);
			for (j = 0; j < 8; j++) {
				putchar(' ');
				ao_cmd_put8(storage_data[j]);
			}
			putchar ('\n');
		}
		if (i == 248)
			break;
	}
}

#if HAS_STORAGE_DEBUG

/* not enough space for this today
 */
static void
ao_storage_store(void) __reentrant
{
	uint16_t block;
	uint8_t i;
	uint16_t len;
	static __xdata uint8_t b;
	uint32_t addr;

	ao_cmd_hex();
	block = ao_cmd_lex_i;
	ao_cmd_hex();
	i = ao_cmd_lex_i;
	addr = ((uint32_t) block << 8) | i;
	ao_cmd_hex();
	len = ao_cmd_lex_i;
	if (ao_cmd_status != ao_cmd_success)
		return;
	while (len--) {
		ao_cmd_hex();
		if (ao_cmd_status != ao_cmd_success)
			return;
		b = ao_cmd_lex_i;
		ao_storage_write(addr, &b, 1);
		addr++;
	}
}
#endif

void
ao_storage_zap(void) __reentrant
{
	ao_cmd_hex();
	if (ao_cmd_status != ao_cmd_success)
		return;
	ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
}

void
ao_storage_zapall(void) __reentrant
{
	uint32_t	pos;

	ao_cmd_white();
	if (!ao_match_word("DoIt"))
		return;
	for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
		ao_storage_erase(pos);
}

void
ao_storage_info(void) __reentrant
{
	ao_storage_setup();
	printf("Storage size: %ld\n", (long) ao_storage_total);
	printf("Storage erase unit: %ld\n", (long) ao_storage_block);
	ao_storage_device_info();
}

__code struct ao_cmds ao_storage_cmds[] = {
	{ ao_storage_info, "f\0Show storage" },
	{ ao_storage_dump, "e <block>\0Dump flash" },
#if HAS_STORAGE_DEBUG
	{ ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
#endif
	{ ao_storage_zap, "z <block>\0Erase <block>" },
	{ ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
	{ 0, NULL },
};

void
ao_storage_init(void)
{
	ao_storage_device_init();
	ao_cmd_register(&ao_storage_cmds[0]);
}