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> *
6 * Partially based on drivers/mtd/nand_ids.c from Linux. *
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. *
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. *
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 ***************************************************************************/
29 static struct nand_ecclayout nand_oob_16
= {
31 .eccpos
= {0, 1, 2, 3, 6, 7},
38 static struct nand_ecclayout nand_oob_64
= {
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
51 void nand_fileio_init(struct nand_fileio_state
*state
)
53 memset(state
, 0, sizeof(*state
));
54 state
->oob_format
= NAND_OOB_NONE
;
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
)
61 if (state
->address
% nand
->page_size
) {
62 command_print(cmd
, "only page-aligned addresses are supported");
63 return ERROR_COMMAND_SYNTAX_ERROR
;
66 duration_start(&state
->bench
);
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",
76 state
->file_opened
= true;
79 if (!(state
->oob_format
& NAND_OOB_ONLY
)) {
80 state
->page_size
= nand
->page_size
;
81 state
->page
= malloc(nand
->page_size
);
84 if (state
->oob_format
& (NAND_OOB_RAW
| NAND_OOB_SW_ECC
| NAND_OOB_SW_ECC_KW
)) {
85 if (nand
->page_size
== 512) {
87 state
->eccpos
= nand_oob_16
.eccpos
;
88 } else if (nand
->page_size
== 2048) {
90 state
->eccpos
= nand_oob_64
.eccpos
;
92 state
->oob
= malloc(state
->oob_size
);
97 int nand_fileio_cleanup(struct nand_fileio_state
*state
)
99 if (state
->file_opened
)
100 fileio_close(state
->fileio
);
109 int nand_fileio_finish(struct nand_fileio_state
*state
)
111 nand_fileio_cleanup(state
);
112 return duration_measure(&state
->bench
);
115 COMMAND_HELPER(nand_fileio_parse_args
, struct nand_fileio_state
*state
,
116 struct nand_device
**dev
, enum fileio_access filemode
,
117 bool need_size
, bool sw_ecc
)
119 nand_fileio_init(state
);
121 unsigned minargs
= need_size
? 4 : 3;
122 if (CMD_ARGC
< minargs
)
123 return ERROR_COMMAND_SYNTAX_ERROR
;
125 struct nand_device
*nand
;
126 int retval
= CALL_COMMAND_HANDLER(nand_command_get_device
, 0, &nand
);
127 if (ERROR_OK
!= retval
)
130 if (NULL
== nand
->device
) {
131 command_print(CMD
, "#%s: not probed", CMD_ARGV
[0]);
132 return ERROR_NAND_DEVICE_NOT_PROBED
;
135 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], state
->address
);
137 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], state
->size
);
138 if (state
->size
% nand
->page_size
) {
139 command_print(CMD
, "only page-aligned sizes are supported");
140 return ERROR_COMMAND_SYNTAX_ERROR
;
144 if (CMD_ARGC
> minargs
) {
145 for (unsigned i
= minargs
; i
< CMD_ARGC
; i
++) {
146 if (!strcmp(CMD_ARGV
[i
], "oob_raw"))
147 state
->oob_format
|= NAND_OOB_RAW
;
148 else if (!strcmp(CMD_ARGV
[i
], "oob_only"))
149 state
->oob_format
|= NAND_OOB_RAW
| NAND_OOB_ONLY
;
150 else if (sw_ecc
&& !strcmp(CMD_ARGV
[i
], "oob_softecc"))
151 state
->oob_format
|= NAND_OOB_SW_ECC
;
152 else if (sw_ecc
&& !strcmp(CMD_ARGV
[i
], "oob_softecc_kw"))
153 state
->oob_format
|= NAND_OOB_SW_ECC_KW
;
155 command_print(CMD
, "unknown option: %s", CMD_ARGV
[i
]);
156 return ERROR_COMMAND_SYNTAX_ERROR
;
161 retval
= nand_fileio_start(CMD
, nand
, CMD_ARGV
[1], filemode
, state
);
162 if (ERROR_OK
!= retval
)
167 retval
= fileio_size(state
->fileio
, &filesize
);
168 if (retval
!= ERROR_OK
)
170 state
->size
= filesize
;
179 * @returns If no error occurred, returns number of bytes consumed;
180 * otherwise, returns a negative error code.)
182 int nand_fileio_read(struct nand_device
*nand
, struct nand_fileio_state
*s
)
184 size_t total_read
= 0;
187 if (NULL
!= s
->page
) {
188 fileio_read(s
->fileio
, s
->page_size
, s
->page
, &one_read
);
189 if (one_read
< s
->page_size
)
190 memset(s
->page
+ one_read
, 0xff, s
->page_size
- one_read
);
191 total_read
+= one_read
;
194 if (s
->oob_format
& NAND_OOB_SW_ECC
) {
196 memset(s
->oob
, 0xff, s
->oob_size
);
197 for (uint32_t i
= 0, j
= 0; i
< s
->page_size
; i
+= 256) {
198 nand_calculate_ecc(nand
, s
->page
+ i
, ecc
);
199 s
->oob
[s
->eccpos
[j
++]] = ecc
[0];
200 s
->oob
[s
->eccpos
[j
++]] = ecc
[1];
201 s
->oob
[s
->eccpos
[j
++]] = ecc
[2];
203 } else if (s
->oob_format
& NAND_OOB_SW_ECC_KW
) {
205 * In this case eccpos is not used as
206 * the ECC data is always stored contiguously
207 * at the end of the OOB area. It consists
208 * of 10 bytes per 512-byte data block.
210 uint8_t *ecc
= s
->oob
+ s
->oob_size
- s
->page_size
/ 512 * 10;
211 memset(s
->oob
, 0xff, s
->oob_size
);
212 for (uint32_t i
= 0; i
< s
->page_size
; i
+= 512) {
213 nand_calculate_ecc_kw(nand
, s
->page
+ i
, ecc
);
216 } else if (NULL
!= s
->oob
) {
217 fileio_read(s
->fileio
, s
->oob_size
, s
->oob
, &one_read
);
218 if (one_read
< s
->oob_size
)
219 memset(s
->oob
+ one_read
, 0xff, s
->oob_size
- one_read
);
220 total_read
+= one_read
;
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)