]> git.sur5r.net Git - openocd/blob - src/flash/nand/fileio.c
65474e6eed6f9fbadaca15861584eebc2eeb5c6a
[openocd] / 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, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
22  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "core.h"
29 #include "fileio.h"
30
31 static struct nand_ecclayout nand_oob_16 = {
32         .eccbytes = 6,
33         .eccpos = {0, 1, 2, 3, 6, 7},
34         .oobfree = {
35                 {.offset = 8,
36                  .length = 8}
37         }
38 };
39
40 static struct nand_ecclayout nand_oob_64 = {
41         .eccbytes = 24,
42         .eccpos = {
43                 40, 41, 42, 43, 44, 45, 46, 47,
44                 48, 49, 50, 51, 52, 53, 54, 55,
45                 56, 57, 58, 59, 60, 61, 62, 63
46         },
47         .oobfree = {
48                 {.offset = 2,
49                  .length = 38}
50         }
51 };
52
53 void nand_fileio_init(struct nand_fileio_state *state)
54 {
55         memset(state, 0, sizeof(*state));
56         state->oob_format = NAND_OOB_NONE;
57 }
58
59 int nand_fileio_start(struct command_context *cmd_ctx,
60         struct nand_device *nand, const char *filename, int filemode,
61         struct nand_fileio_state *state)
62 {
63         if (state->address % nand->page_size) {
64                 command_print(cmd_ctx, "only page-aligned addresses are supported");
65                 return ERROR_COMMAND_SYNTAX_ERROR;
66         }
67
68         duration_start(&state->bench);
69
70         if (NULL != filename) {
71                 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
72                 if (ERROR_OK != retval) {
73                         const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
74                         command_print(cmd_ctx, "failed to open '%s' for %s access",
75                                 filename, msg);
76                         return retval;
77                 }
78                 state->file_opened = true;
79         }
80
81         if (!(state->oob_format & NAND_OOB_ONLY)) {
82                 state->page_size = nand->page_size;
83                 state->page = malloc(nand->page_size);
84         }
85
86         if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) {
87                 if (nand->page_size == 512) {
88                         state->oob_size = 16;
89                         state->eccpos = nand_oob_16.eccpos;
90                 } else if (nand->page_size == 2048)   {
91                         state->oob_size = 64;
92                         state->eccpos = nand_oob_64.eccpos;
93                 }
94                 state->oob = malloc(state->oob_size);
95         }
96
97         return ERROR_OK;
98 }
99 int nand_fileio_cleanup(struct nand_fileio_state *state)
100 {
101         if (state->file_opened)
102                 fileio_close(state->fileio);
103
104         if (state->oob) {
105                 free(state->oob);
106                 state->oob = NULL;
107         }
108         if (state->page) {
109                 free(state->page);
110                 state->page = NULL;
111         }
112         return ERROR_OK;
113 }
114 int nand_fileio_finish(struct nand_fileio_state *state)
115 {
116         nand_fileio_cleanup(state);
117         return duration_measure(&state->bench);
118 }
119
120 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
121         struct nand_device **dev, enum fileio_access filemode,
122         bool need_size, bool sw_ecc)
123 {
124         nand_fileio_init(state);
125
126         unsigned minargs = need_size ? 4 : 3;
127         if (CMD_ARGC < minargs)
128                 return ERROR_COMMAND_SYNTAX_ERROR;
129
130         struct nand_device *nand;
131         int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
132         if (ERROR_OK != retval)
133                 return retval;
134
135         if (NULL == nand->device) {
136                 command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
137                 return ERROR_NAND_DEVICE_NOT_PROBED;
138         }
139
140         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
141         if (need_size) {
142                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
143                 if (state->size % nand->page_size) {
144                         command_print(CMD_CTX, "only page-aligned sizes are supported");
145                         return ERROR_COMMAND_SYNTAX_ERROR;
146                 }
147         }
148
149         if (CMD_ARGC > minargs) {
150                 for (unsigned i = minargs; i < CMD_ARGC; i++) {
151                         if (!strcmp(CMD_ARGV[i], "oob_raw"))
152                                 state->oob_format |= NAND_OOB_RAW;
153                         else if (!strcmp(CMD_ARGV[i], "oob_only"))
154                                 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
155                         else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
156                                 state->oob_format |= NAND_OOB_SW_ECC;
157                         else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
158                                 state->oob_format |= NAND_OOB_SW_ECC_KW;
159                         else {
160                                 command_print(CMD_CTX, "unknown option: %s", CMD_ARGV[i]);
161                                 return ERROR_COMMAND_SYNTAX_ERROR;
162                         }
163                 }
164         }
165
166         retval = nand_fileio_start(CMD_CTX, nand, CMD_ARGV[1], filemode, state);
167         if (ERROR_OK != retval)
168                 return retval;
169
170         if (!need_size) {
171                 size_t filesize;
172                 retval = fileio_size(state->fileio, &filesize);
173                 if (retval != ERROR_OK)
174                         return retval;
175                 state->size = filesize;
176         }
177
178         *dev = nand;
179
180         return ERROR_OK;
181 }
182
183 /**
184  * @returns If no error occurred, returns number of bytes consumed;
185  * otherwise, returns a negative error code.)
186  */
187 int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
188 {
189         size_t total_read = 0;
190         size_t one_read;
191
192         if (NULL != s->page) {
193                 fileio_read(s->fileio, s->page_size, s->page, &one_read);
194                 if (one_read < s->page_size)
195                         memset(s->page + one_read, 0xff, s->page_size - one_read);
196                 total_read += one_read;
197         }
198
199         if (s->oob_format & NAND_OOB_SW_ECC) {
200                 uint8_t ecc[3];
201                 memset(s->oob, 0xff, s->oob_size);
202                 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
203                         nand_calculate_ecc(nand, s->page + i, ecc);
204                         s->oob[s->eccpos[j++]] = ecc[0];
205                         s->oob[s->eccpos[j++]] = ecc[1];
206                         s->oob[s->eccpos[j++]] = ecc[2];
207                 }
208         } else if (s->oob_format & NAND_OOB_SW_ECC_KW)   {
209                 /*
210                  * In this case eccpos is not used as
211                  * the ECC data is always stored contigously
212                  * at the end of the OOB area.  It consists
213                  * of 10 bytes per 512-byte data block.
214                  */
215                 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
216                 memset(s->oob, 0xff, s->oob_size);
217                 for (uint32_t i = 0; i < s->page_size; i += 512) {
218                         nand_calculate_ecc_kw(nand, s->page + i, ecc);
219                         ecc += 10;
220                 }
221         } else if (NULL != s->oob)   {
222                 fileio_read(s->fileio, s->oob_size, s->oob, &one_read);
223                 if (one_read < s->oob_size)
224                         memset(s->oob + one_read, 0xff, s->oob_size - one_read);
225                 total_read += one_read;
226         }
227         return total_read;
228 }