flash/nor: use command_print() in command "flash banks"
[openocd.git] / src / flash / nand / fileio.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
4 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
5 * *
6 * Partially based on drivers/mtd/nand_ids.c from Linux. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "core.h"
27 #include "fileio.h"
28
29 static struct nand_ecclayout nand_oob_16 = {
30 .eccbytes = 6,
31 .eccpos = {0, 1, 2, 3, 6, 7},
32 .oobfree = {
33 {.offset = 8,
34 .length = 8}
35 }
36 };
37
38 static struct nand_ecclayout nand_oob_64 = {
39 .eccbytes = 24,
40 .eccpos = {
41 40, 41, 42, 43, 44, 45, 46, 47,
42 48, 49, 50, 51, 52, 53, 54, 55,
43 56, 57, 58, 59, 60, 61, 62, 63
44 },
45 .oobfree = {
46 {.offset = 2,
47 .length = 38}
48 }
49 };
50
51 void nand_fileio_init(struct nand_fileio_state *state)
52 {
53 memset(state, 0, sizeof(*state));
54 state->oob_format = NAND_OOB_NONE;
55 }
56
57 int nand_fileio_start(struct command_invocation *cmd,
58 struct nand_device *nand, const char *filename, int filemode,
59 struct nand_fileio_state *state)
60 {
61 if (state->address % nand->page_size) {
62 command_print(cmd, "only page-aligned addresses are supported");
63 return ERROR_COMMAND_SYNTAX_ERROR;
64 }
65
66 duration_start(&state->bench);
67
68 if (NULL != filename) {
69 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
70 if (ERROR_OK != retval) {
71 const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
72 command_print(cmd, "failed to open '%s' for %s access",
73 filename, msg);
74 return retval;
75 }
76 state->file_opened = true;
77 }
78
79 if (!(state->oob_format & NAND_OOB_ONLY)) {
80 state->page_size = nand->page_size;
81 state->page = malloc(nand->page_size);
82 }
83
84 if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) {
85 if (nand->page_size == 512) {
86 state->oob_size = 16;
87 state->eccpos = nand_oob_16.eccpos;
88 } else if (nand->page_size == 2048) {
89 state->oob_size = 64;
90 state->eccpos = nand_oob_64.eccpos;
91 }
92 state->oob = malloc(state->oob_size);
93 }
94
95 return ERROR_OK;
96 }
97 int nand_fileio_cleanup(struct nand_fileio_state *state)
98 {
99 if (state->file_opened)
100 fileio_close(state->fileio);
101
102 if (state->oob) {
103 free(state->oob);
104 state->oob = NULL;
105 }
106 if (state->page) {
107 free(state->page);
108 state->page = NULL;
109 }
110 return ERROR_OK;
111 }
112 int nand_fileio_finish(struct nand_fileio_state *state)
113 {
114 nand_fileio_cleanup(state);
115 return duration_measure(&state->bench);
116 }
117
118 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
119 struct nand_device **dev, enum fileio_access filemode,
120 bool need_size, bool sw_ecc)
121 {
122 nand_fileio_init(state);
123
124 unsigned minargs = need_size ? 4 : 3;
125 if (CMD_ARGC < minargs)
126 return ERROR_COMMAND_SYNTAX_ERROR;
127
128 struct nand_device *nand;
129 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
130 if (ERROR_OK != retval)
131 return retval;
132
133 if (NULL == nand->device) {
134 command_print(CMD, "#%s: not probed", CMD_ARGV[0]);
135 return ERROR_NAND_DEVICE_NOT_PROBED;
136 }
137
138 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
139 if (need_size) {
140 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
141 if (state->size % nand->page_size) {
142 command_print(CMD, "only page-aligned sizes are supported");
143 return ERROR_COMMAND_SYNTAX_ERROR;
144 }
145 }
146
147 if (CMD_ARGC > minargs) {
148 for (unsigned i = minargs; i < CMD_ARGC; i++) {
149 if (!strcmp(CMD_ARGV[i], "oob_raw"))
150 state->oob_format |= NAND_OOB_RAW;
151 else if (!strcmp(CMD_ARGV[i], "oob_only"))
152 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
153 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
154 state->oob_format |= NAND_OOB_SW_ECC;
155 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
156 state->oob_format |= NAND_OOB_SW_ECC_KW;
157 else {
158 command_print(CMD, "unknown option: %s", CMD_ARGV[i]);
159 return ERROR_COMMAND_SYNTAX_ERROR;
160 }
161 }
162 }
163
164 retval = nand_fileio_start(CMD, nand, CMD_ARGV[1], filemode, state);
165 if (ERROR_OK != retval)
166 return retval;
167
168 if (!need_size) {
169 size_t filesize;
170 retval = fileio_size(state->fileio, &filesize);
171 if (retval != ERROR_OK)
172 return retval;
173 state->size = filesize;
174 }
175
176 *dev = nand;
177
178 return ERROR_OK;
179 }
180
181 /**
182 * @returns If no error occurred, returns number of bytes consumed;
183 * otherwise, returns a negative error code.)
184 */
185 int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
186 {
187 size_t total_read = 0;
188 size_t one_read;
189
190 if (NULL != s->page) {
191 fileio_read(s->fileio, s->page_size, s->page, &one_read);
192 if (one_read < s->page_size)
193 memset(s->page + one_read, 0xff, s->page_size - one_read);
194 total_read += one_read;
195 }
196
197 if (s->oob_format & NAND_OOB_SW_ECC) {
198 uint8_t ecc[3];
199 memset(s->oob, 0xff, s->oob_size);
200 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
201 nand_calculate_ecc(nand, s->page + i, ecc);
202 s->oob[s->eccpos[j++]] = ecc[0];
203 s->oob[s->eccpos[j++]] = ecc[1];
204 s->oob[s->eccpos[j++]] = ecc[2];
205 }
206 } else if (s->oob_format & NAND_OOB_SW_ECC_KW) {
207 /*
208 * In this case eccpos is not used as
209 * the ECC data is always stored contigously
210 * at the end of the OOB area. It consists
211 * of 10 bytes per 512-byte data block.
212 */
213 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
214 memset(s->oob, 0xff, s->oob_size);
215 for (uint32_t i = 0; i < s->page_size; i += 512) {
216 nand_calculate_ecc_kw(nand, s->page + i, ecc);
217 ecc += 10;
218 }
219 } else if (NULL != s->oob) {
220 fileio_read(s->fileio, s->oob_size, s->oob, &one_read);
221 if (one_read < s->oob_size)
222 memset(s->oob + one_read, 0xff, s->oob_size - one_read);
223 total_read += one_read;
224 }
225 return total_read;
226 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)