]>
Commit | Line | Data |
---|---|---|
dc7c9a1a WD |
1 | /* |
2 | * Driver for NAND support, Rick Bronson | |
3 | * borrowed heavily from: | |
4 | * (c) 1999 Machine Vision Holdings, Inc. | |
5 | * (c) 1999, 2000 David Woodhouse <[email protected]> | |
384cc687 | 6 | * |
c9f7351b BG |
7 | * Ported 'dynenv' to 'nand env.oob' command |
8 | * (C) 2010 Nanometrics, Inc. | |
9 | * 'dynenv' -- Dynamic environment offset in NAND OOB | |
10 | * (C) Copyright 2006-2007 OpenMoko, Inc. | |
384cc687 WD |
11 | * Added 16-bit nand support |
12 | * (C) 2004 Texas Instruments | |
ea533c26 | 13 | * |
418396e2 | 14 | * Copyright 2010, 2012 Freescale Semiconductor |
ea533c26 SW |
15 | * The portions of this file whose copyright is held by Freescale and which |
16 | * are not considered a derived work of GPL v2-only code may be distributed | |
17 | * and/or modified under the terms of the GNU General Public License as | |
18 | * published by the Free Software Foundation; either version 2 of the | |
19 | * License, or (at your option) any later version. | |
4ea84c5d BG |
20 | * |
21 | * The function nand_biterror() in this file is inspired from | |
22 | * mtd-utils/nand-utils/nandflipbits.c which was released under GPLv2 | |
23 | * only | |
dc7c9a1a WD |
24 | */ |
25 | ||
52f24238 | 26 | #include <bootstage.h> |
8e8ccfe1 | 27 | #include <image.h> |
90526e9f | 28 | #include <asm/cache.h> |
cfa460ad | 29 | #include <linux/mtd/mtd.h> |
1cefed1e | 30 | #include <linux/mtd/rawnand.h> |
addb2e16 | 31 | #include <command.h> |
24b852a7 | 32 | #include <console.h> |
c7694dd4 | 33 | #include <env.h> |
addb2e16 BS |
34 | #include <watchdog.h> |
35 | #include <malloc.h> | |
d2e0a9a6 | 36 | #include <mapmem.h> |
addb2e16 | 37 | #include <asm/byteorder.h> |
addb2e16 BS |
38 | #include <jffs2/jffs2.h> |
39 | #include <nand.h> | |
40 | ||
eb446ef6 MR |
41 | #include "legacy-mtd-utils.h" |
42 | ||
0c8a8491 | 43 | #if defined(CONFIG_CMD_MTDPARTS) |
856f0544 | 44 | |
445093d1 | 45 | /* partition handling routines */ |
856f0544 | 46 | int mtdparts_init(void); |
856f0544 | 47 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
4b070809 | 48 | u8 *part_num, struct part_info **part); |
856f0544 SR |
49 | #endif |
50 | ||
4ea84c5d BG |
51 | #define MAX_NUM_PAGES 64 |
52 | ||
53 | static int nand_biterror(struct mtd_info *mtd, ulong off, int bit) | |
54 | { | |
55 | int ret = 0; | |
56 | int page = 0; | |
57 | ulong block_off; | |
58 | u_char *datbuf[MAX_NUM_PAGES]; /* Data and OOB */ | |
59 | u_char data; | |
60 | int pages_per_blk = mtd->erasesize / mtd->writesize; | |
61 | struct erase_info einfo; | |
62 | ||
63 | if (pages_per_blk > MAX_NUM_PAGES) { | |
64 | printf("Too many pages in one erase block\n"); | |
65 | return 1; | |
66 | } | |
67 | ||
68 | if (bit < 0 || bit > 7) { | |
69 | printf("bit position 0 to 7 is allowed\n"); | |
70 | return 1; | |
71 | } | |
72 | ||
73 | /* Allocate memory */ | |
74 | memset(datbuf, 0, sizeof(datbuf)); | |
75 | for (page = 0; page < pages_per_blk ; page++) { | |
76 | datbuf[page] = malloc(mtd->writesize + mtd->oobsize); | |
77 | if (!datbuf[page]) { | |
78 | printf("No memory for page buffer\n"); | |
79 | ret = -ENOMEM; | |
80 | goto free_memory; | |
81 | } | |
82 | } | |
83 | ||
84 | /* Align to erase block boundary */ | |
85 | block_off = off & (~(mtd->erasesize - 1)); | |
86 | ||
87 | /* Read out memory as first step */ | |
88 | for (page = 0; page < pages_per_blk ; page++) { | |
89 | struct mtd_oob_ops ops; | |
90 | loff_t addr = (loff_t)block_off; | |
91 | ||
92 | memset(&ops, 0, sizeof(ops)); | |
93 | ops.datbuf = datbuf[page]; | |
94 | ops.oobbuf = datbuf[page] + mtd->writesize; | |
95 | ops.len = mtd->writesize; | |
96 | ops.ooblen = mtd->oobsize; | |
97 | ops.mode = MTD_OPS_RAW; | |
98 | ret = mtd_read_oob(mtd, addr, &ops); | |
99 | if (ret < 0) { | |
100 | printf("Error (%d) reading page %08lx\n", | |
101 | ret, block_off); | |
102 | ret = 1; | |
103 | goto free_memory; | |
104 | } | |
105 | block_off += mtd->writesize; | |
106 | } | |
107 | ||
108 | /* Erase the block */ | |
109 | memset(&einfo, 0, sizeof(einfo)); | |
110 | einfo.mtd = mtd; | |
111 | /* Align to erase block boundary */ | |
112 | einfo.addr = (loff_t)(off & (~(mtd->erasesize - 1))); | |
113 | einfo.len = mtd->erasesize; | |
114 | ret = mtd_erase(mtd, &einfo); | |
115 | if (ret < 0) { | |
116 | printf("Error (%d) nand_erase_nand page %08llx\n", | |
117 | ret, einfo.addr); | |
118 | ret = 1; | |
119 | goto free_memory; | |
120 | } | |
121 | ||
122 | /* Twist a bit in data part */ | |
123 | block_off = off & (mtd->erasesize - 1); | |
124 | data = datbuf[block_off / mtd->writesize][block_off % mtd->writesize]; | |
125 | data ^= (1 << bit); | |
126 | datbuf[block_off / mtd->writesize][block_off % mtd->writesize] = data; | |
127 | ||
128 | printf("Flip data at 0x%lx with xor 0x%02x (bit=%d) to value=0x%02x\n", | |
129 | off, (1 << bit), bit, data); | |
130 | ||
131 | /* Write back twisted data and unmodified OOB */ | |
132 | /* Align to erase block boundary */ | |
133 | block_off = off & (~(mtd->erasesize - 1)); | |
134 | for (page = 0; page < pages_per_blk; page++) { | |
135 | struct mtd_oob_ops ops; | |
136 | loff_t addr = (loff_t)block_off; | |
137 | ||
138 | memset(&ops, 0, sizeof(ops)); | |
139 | ops.datbuf = datbuf[page]; | |
140 | ops.oobbuf = datbuf[page] + mtd->writesize; | |
141 | ops.len = mtd->writesize; | |
142 | ops.ooblen = mtd->oobsize; | |
143 | ops.mode = MTD_OPS_RAW; | |
144 | ret = mtd_write_oob(mtd, addr, &ops); | |
145 | if (ret < 0) { | |
146 | printf("Error (%d) write page %08lx\n", ret, block_off); | |
147 | ret = 1; | |
148 | goto free_memory; | |
149 | } | |
150 | block_off += mtd->writesize; | |
151 | } | |
152 | ||
153 | free_memory: | |
154 | for (page = 0; page < pages_per_blk ; page++) { | |
155 | if (datbuf[page]) | |
156 | free(datbuf[page]); | |
157 | } | |
158 | return ret; | |
159 | } | |
160 | ||
151c06ec SW |
161 | static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob, |
162 | int repeat) | |
addb2e16 BS |
163 | { |
164 | int i; | |
9ad754fe | 165 | u_char *datbuf, *oobbuf, *p; |
8c5659a6 | 166 | static loff_t last; |
e40520b5 | 167 | int ret = 0; |
8c5659a6 SW |
168 | |
169 | if (repeat) | |
151c06ec | 170 | off = last + mtd->writesize; |
8c5659a6 SW |
171 | |
172 | last = off; | |
addb2e16 | 173 | |
151c06ec | 174 | datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize); |
e40520b5 | 175 | if (!datbuf) { |
addb2e16 BS |
176 | puts("No memory for page buffer\n"); |
177 | return 1; | |
178 | } | |
e40520b5 | 179 | |
151c06ec | 180 | oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize); |
e40520b5 MY |
181 | if (!oobbuf) { |
182 | puts("No memory for page buffer\n"); | |
183 | ret = 1; | |
184 | goto free_dat; | |
185 | } | |
151c06ec | 186 | off &= ~(mtd->writesize - 1); |
cfa460ad | 187 | loff_t addr = (loff_t) off; |
9ad754fe WJ |
188 | struct mtd_oob_ops ops; |
189 | memset(&ops, 0, sizeof(ops)); | |
190 | ops.datbuf = datbuf; | |
cfdae12f | 191 | ops.oobbuf = oobbuf; |
151c06ec SW |
192 | ops.len = mtd->writesize; |
193 | ops.ooblen = mtd->oobsize; | |
dfe64e2c | 194 | ops.mode = MTD_OPS_RAW; |
151c06ec | 195 | i = mtd_read_oob(mtd, addr, &ops); |
addb2e16 | 196 | if (i < 0) { |
e870690b | 197 | printf("Error (%d) reading page %08lx\n", i, off); |
e40520b5 MY |
198 | ret = 1; |
199 | goto free_all; | |
addb2e16 | 200 | } |
e870690b | 201 | printf("Page %08lx dump:\n", off); |
4b070809 | 202 | |
7d25cd34 | 203 | if (!only_oob) { |
151c06ec | 204 | i = mtd->writesize >> 4; |
7d25cd34 MY |
205 | p = datbuf; |
206 | ||
207 | while (i--) { | |
9ad754fe WJ |
208 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" |
209 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", | |
210 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], | |
dfbf617f SW |
211 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
212 | p[15]); | |
7d25cd34 MY |
213 | p += 16; |
214 | } | |
addb2e16 | 215 | } |
7d25cd34 | 216 | |
addb2e16 | 217 | puts("OOB:\n"); |
151c06ec | 218 | i = mtd->oobsize >> 3; |
cfdae12f | 219 | p = oobbuf; |
addb2e16 | 220 | while (i--) { |
cfa460ad WJ |
221 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", |
222 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); | |
addb2e16 BS |
223 | p += 8; |
224 | } | |
e40520b5 MY |
225 | |
226 | free_all: | |
9ad754fe | 227 | free(oobbuf); |
e40520b5 MY |
228 | free_dat: |
229 | free(datbuf); | |
addb2e16 | 230 | |
e40520b5 | 231 | return ret; |
addb2e16 BS |
232 | } |
233 | ||
234 | /* ------------------------------------------------------------------------- */ | |
235 | ||
ea533c26 SW |
236 | static int set_dev(int dev) |
237 | { | |
ad92dff2 M |
238 | struct mtd_info *mtd = get_nand_dev_by_index(dev); |
239 | ||
240 | if (!mtd) | |
241 | return -ENODEV; | |
ea533c26 SW |
242 | |
243 | if (nand_curr_device == dev) | |
244 | return 0; | |
245 | ||
ad92dff2 | 246 | printf("Device %d: %s", dev, mtd->name); |
ea533c26 SW |
247 | puts("... is now current device\n"); |
248 | nand_curr_device = dev; | |
249 | ||
250 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE | |
6308e71b | 251 | board_nand_select_device(mtd_to_nand(mtd), dev); |
ea533c26 SW |
252 | #endif |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
50657c27 NM |
257 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
258 | static void print_status(ulong start, ulong end, ulong erasesize, int status) | |
259 | { | |
e70bfa29 JH |
260 | /* |
261 | * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is | |
262 | * not the same as others. Instead of bit 1 being lock, it is | |
263 | * #lock_tight. To make the driver support either format, ignore bit 1 | |
264 | * and use only bit 0 and bit 2. | |
265 | */ | |
50657c27 NM |
266 | printf("%08lx - %08lx: %08lx blocks %s%s%s\n", |
267 | start, | |
268 | end - 1, | |
269 | (end - start) / erasesize, | |
270 | ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""), | |
e70bfa29 | 271 | (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""), |
50657c27 NM |
272 | ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); |
273 | } | |
274 | ||
151c06ec | 275 | static void do_nand_status(struct mtd_info *mtd) |
50657c27 NM |
276 | { |
277 | ulong block_start = 0; | |
278 | ulong off; | |
279 | int last_status = -1; | |
280 | ||
17cb4b8f | 281 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
50657c27 | 282 | /* check the WP bit */ |
151c06ec | 283 | nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
50657c27 | 284 | printf("device is %swrite protected\n", |
151c06ec SW |
285 | (nand_chip->read_byte(mtd) & 0x80 ? |
286 | "NOT " : "")); | |
50657c27 | 287 | |
151c06ec SW |
288 | for (off = 0; off < mtd->size; off += mtd->erasesize) { |
289 | int s = nand_get_lock_status(mtd, off); | |
50657c27 NM |
290 | |
291 | /* print message only if status has changed */ | |
292 | if (s != last_status && off != 0) { | |
151c06ec | 293 | print_status(block_start, off, mtd->erasesize, |
50657c27 NM |
294 | last_status); |
295 | block_start = off; | |
296 | } | |
297 | last_status = s; | |
298 | } | |
299 | /* Print the last block info */ | |
151c06ec | 300 | print_status(block_start, off, mtd->erasesize, last_status); |
50657c27 NM |
301 | } |
302 | #endif | |
303 | ||
c9f7351b BG |
304 | #ifdef CONFIG_ENV_OFFSET_OOB |
305 | unsigned long nand_env_oob_offset; | |
306 | ||
09140113 | 307 | int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[]) |
c9f7351b BG |
308 | { |
309 | int ret; | |
310 | uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)]; | |
ad92dff2 | 311 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
c9f7351b BG |
312 | char *cmd = argv[1]; |
313 | ||
8b7d5124 | 314 | if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) { |
ea533c26 SW |
315 | puts("no devices available\n"); |
316 | return 1; | |
317 | } | |
318 | ||
319 | set_dev(0); | |
320 | ||
c9f7351b | 321 | if (!strcmp(cmd, "get")) { |
151c06ec | 322 | ret = get_nand_env_oob(mtd, &nand_env_oob_offset); |
53504a27 | 323 | if (ret) |
c9f7351b | 324 | return 1; |
53504a27 SW |
325 | |
326 | printf("0x%08lx\n", nand_env_oob_offset); | |
c9f7351b | 327 | } else if (!strcmp(cmd, "set")) { |
ea533c26 SW |
328 | loff_t addr; |
329 | loff_t maxsize; | |
c9f7351b | 330 | struct mtd_oob_ops ops; |
ea533c26 | 331 | int idx = 0; |
c9f7351b BG |
332 | |
333 | if (argc < 3) | |
334 | goto usage; | |
335 | ||
ad92dff2 | 336 | mtd = get_nand_dev_by_index(idx); |
c39d6a0e | 337 | /* We don't care about size, or maxsize. */ |
09c32807 | 338 | if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize, |
ad92dff2 | 339 | MTD_DEV_TYPE_NAND, mtd->size)) { |
09c32807 HS |
340 | puts("Offset or partition name expected\n"); |
341 | return 1; | |
342 | } | |
343 | if (set_dev(idx)) { | |
ea533c26 SW |
344 | puts("Offset or partition name expected\n"); |
345 | return 1; | |
346 | } | |
347 | ||
348 | if (idx != 0) { | |
349 | puts("Partition not on first NAND device\n"); | |
c9f7351b BG |
350 | return 1; |
351 | } | |
352 | ||
151c06ec | 353 | if (mtd->oobavail < ENV_OFFSET_SIZE) { |
53504a27 SW |
354 | printf("Insufficient available OOB bytes:\n" |
355 | "%d OOB bytes available but %d required for " | |
356 | "env.oob support\n", | |
151c06ec | 357 | mtd->oobavail, ENV_OFFSET_SIZE); |
c9f7351b BG |
358 | return 1; |
359 | } | |
360 | ||
151c06ec | 361 | if ((addr & (mtd->erasesize - 1)) != 0) { |
c9f7351b BG |
362 | printf("Environment offset must be block-aligned\n"); |
363 | return 1; | |
364 | } | |
365 | ||
366 | ops.datbuf = NULL; | |
367 | ops.mode = MTD_OOB_AUTO; | |
368 | ops.ooboffs = 0; | |
369 | ops.ooblen = ENV_OFFSET_SIZE; | |
370 | ops.oobbuf = (void *) oob_buf; | |
371 | ||
372 | oob_buf[0] = ENV_OOB_MARKER; | |
151c06ec | 373 | oob_buf[1] = addr / mtd->erasesize; |
c9f7351b | 374 | |
151c06ec | 375 | ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops); |
53504a27 | 376 | if (ret) { |
c9f7351b BG |
377 | printf("Error writing OOB block 0\n"); |
378 | return ret; | |
379 | } | |
53504a27 | 380 | |
151c06ec | 381 | ret = get_nand_env_oob(mtd, &nand_env_oob_offset); |
53504a27 SW |
382 | if (ret) { |
383 | printf("Error reading env offset in OOB\n"); | |
384 | return ret; | |
385 | } | |
386 | ||
387 | if (addr != nand_env_oob_offset) { | |
388 | printf("Verification of env offset in OOB failed: " | |
ea533c26 SW |
389 | "0x%08llx expected but got 0x%08lx\n", |
390 | (unsigned long long)addr, nand_env_oob_offset); | |
53504a27 SW |
391 | return 1; |
392 | } | |
c9f7351b BG |
393 | } else { |
394 | goto usage; | |
395 | } | |
396 | ||
397 | return ret; | |
398 | ||
399 | usage: | |
4c12eeb8 | 400 | return CMD_RET_USAGE; |
c9f7351b BG |
401 | } |
402 | ||
403 | #endif | |
404 | ||
ce80ddc1 | 405 | static void nand_print_and_set_info(int idx) |
672ed2ae | 406 | { |
ad92dff2 M |
407 | struct mtd_info *mtd; |
408 | struct nand_chip *chip; | |
409 | ||
410 | mtd = get_nand_dev_by_index(idx); | |
411 | if (!mtd) | |
412 | return; | |
ce80ddc1 | 413 | |
ad92dff2 | 414 | chip = mtd_to_nand(mtd); |
672ed2ae WG |
415 | printf("Device %d: ", idx); |
416 | if (chip->numchips > 1) | |
417 | printf("%dx ", chip->numchips); | |
418 | printf("%s, sector size %u KiB\n", | |
151c06ec | 419 | mtd->name, mtd->erasesize >> 10); |
308bd746 MT |
420 | printf(" Page size %8d b\n", mtd->writesize); |
421 | printf(" OOB size %8d b\n", mtd->oobsize); | |
422 | printf(" Erase size %8d b\n", mtd->erasesize); | |
423 | printf(" ecc strength %8d bits\n", mtd->ecc_strength); | |
424 | printf(" ecc step size %8d b\n", mtd->ecc_step_size); | |
425 | printf(" subpagesize %8d b\n", chip->subpagesize); | |
426 | printf(" options 0x%08x\n", chip->options); | |
427 | printf(" bbt options 0x%08x\n", chip->bbt_options); | |
ce80ddc1 MV |
428 | |
429 | /* Set geometry info */ | |
018f5303 SG |
430 | env_set_hex("nand_writesize", mtd->writesize); |
431 | env_set_hex("nand_oobsize", mtd->oobsize); | |
432 | env_set_hex("nand_erasesize", mtd->erasesize); | |
672ed2ae WG |
433 | } |
434 | ||
d2e0a9a6 | 435 | static int raw_access(struct mtd_info *mtd, void *buf, loff_t off, |
2dc3c483 | 436 | ulong count, int read, int no_verify) |
418396e2 SW |
437 | { |
438 | int ret = 0; | |
418396e2 SW |
439 | |
440 | while (count--) { | |
441 | /* Raw access */ | |
442 | mtd_oob_ops_t ops = { | |
d2e0a9a6 SA |
443 | .datbuf = buf, |
444 | .oobbuf = buf + mtd->writesize, | |
151c06ec SW |
445 | .len = mtd->writesize, |
446 | .ooblen = mtd->oobsize, | |
dfe64e2c | 447 | .mode = MTD_OPS_RAW |
418396e2 SW |
448 | }; |
449 | ||
6b94f118 | 450 | if (read) { |
151c06ec | 451 | ret = mtd_read_oob(mtd, off, &ops); |
6b94f118 | 452 | } else { |
151c06ec | 453 | ret = mtd_write_oob(mtd, off, &ops); |
2dc3c483 | 454 | if (!ret && !no_verify) |
151c06ec | 455 | ret = nand_verify_page_oob(mtd, &ops, off); |
6b94f118 | 456 | } |
418396e2 SW |
457 | |
458 | if (ret) { | |
459 | printf("%s: error at offset %llx, ret %d\n", | |
460 | __func__, (long long)off, ret); | |
461 | break; | |
462 | } | |
463 | ||
d2e0a9a6 | 464 | buf += mtd->writesize + mtd->oobsize; |
151c06ec | 465 | off += mtd->writesize; |
418396e2 SW |
466 | } |
467 | ||
468 | return ret; | |
469 | } | |
470 | ||
e834402f | 471 | /* Adjust a chip/partition size down for bad blocks so we don't |
9b80aa8e | 472 | * read/write past the end of a chip/partition by accident. |
e834402f HC |
473 | */ |
474 | static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev) | |
475 | { | |
476 | /* We grab the nand info object here fresh because this is usually | |
477 | * called after arg_off_size() which can change the value of dev. | |
478 | */ | |
ad92dff2 | 479 | struct mtd_info *mtd = get_nand_dev_by_index(dev); |
e834402f HC |
480 | loff_t maxoffset = offset + *size; |
481 | int badblocks = 0; | |
482 | ||
483 | /* count badblocks in NAND from offset to offset + size */ | |
151c06ec SW |
484 | for (; offset < maxoffset; offset += mtd->erasesize) { |
485 | if (nand_block_isbad(mtd, offset)) | |
e834402f HC |
486 | badblocks++; |
487 | } | |
488 | /* adjust size if any bad blocks found */ | |
489 | if (badblocks) { | |
151c06ec | 490 | *size -= badblocks * mtd->erasesize; |
e834402f HC |
491 | printf("size adjusted to 0x%llx (%d bad blocks)\n", |
492 | (unsigned long long)*size, badblocks); | |
493 | } | |
494 | } | |
495 | ||
09140113 SG |
496 | static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc, |
497 | char *const argv[]) | |
addb2e16 | 498 | { |
ea533c26 SW |
499 | int i, ret = 0; |
500 | ulong addr; | |
c39d6a0e | 501 | loff_t off, size, maxsize; |
addb2e16 | 502 | char *cmd, *s; |
151c06ec | 503 | struct mtd_info *mtd; |
6d0f6bcf JCPV |
504 | #ifdef CONFIG_SYS_NAND_QUIET |
505 | int quiet = CONFIG_SYS_NAND_QUIET; | |
c750d2e6 | 506 | #else |
2255b2d2 | 507 | int quiet = 0; |
c750d2e6 | 508 | #endif |
00caae6d | 509 | const char *quiet_str = env_get("quiet"); |
ea533c26 | 510 | int dev = nand_curr_device; |
8c5659a6 | 511 | int repeat = flag & CMD_FLAG_REPEAT; |
addb2e16 BS |
512 | |
513 | /* at least two arguments please */ | |
514 | if (argc < 2) | |
515 | goto usage; | |
516 | ||
2255b2d2 SR |
517 | if (quiet_str) |
518 | quiet = simple_strtoul(quiet_str, NULL, 0) != 0; | |
519 | ||
addb2e16 BS |
520 | cmd = argv[1]; |
521 | ||
8c5659a6 SW |
522 | /* Only "dump" is repeatable. */ |
523 | if (repeat && strcmp(cmd, "dump")) | |
524 | return 0; | |
525 | ||
addb2e16 BS |
526 | if (strcmp(cmd, "info") == 0) { |
527 | ||
528 | putc('\n'); | |
ad92dff2 M |
529 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
530 | nand_print_and_set_info(i); | |
addb2e16 BS |
531 | return 0; |
532 | } | |
533 | ||
534 | if (strcmp(cmd, "device") == 0) { | |
addb2e16 | 535 | if (argc < 3) { |
672ed2ae | 536 | putc('\n'); |
ea533c26 | 537 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE) |
672ed2ae | 538 | puts("no devices available\n"); |
addb2e16 | 539 | else |
ce80ddc1 | 540 | nand_print_and_set_info(dev); |
addb2e16 BS |
541 | return 0; |
542 | } | |
43a2b0e7 | 543 | |
0b1284eb | 544 | dev = (int)dectoul(argv[2], NULL); |
ea533c26 | 545 | set_dev(dev); |
43a2b0e7 | 546 | |
addb2e16 BS |
547 | return 0; |
548 | } | |
549 | ||
c9f7351b BG |
550 | #ifdef CONFIG_ENV_OFFSET_OOB |
551 | /* this command operates only on the first nand device */ | |
ea533c26 SW |
552 | if (strcmp(cmd, "env.oob") == 0) |
553 | return do_nand_env_oob(cmdtp, argc - 1, argv + 1); | |
c9f7351b BG |
554 | #endif |
555 | ||
ea533c26 SW |
556 | /* The following commands operate on the current device, unless |
557 | * overridden by a partition specifier. Note that if somehow the | |
558 | * current device is invalid, it will have to be changed to a valid | |
559 | * one before these commands can run, even if a partition specifier | |
560 | * for another device is to be used. | |
561 | */ | |
ad92dff2 M |
562 | mtd = get_nand_dev_by_index(dev); |
563 | if (!mtd) { | |
addb2e16 BS |
564 | puts("\nno devices available\n"); |
565 | return 1; | |
566 | } | |
addb2e16 BS |
567 | |
568 | if (strcmp(cmd, "bad") == 0) { | |
ea533c26 | 569 | printf("\nDevice %d bad blocks:\n", dev); |
cfb82f7c ARS |
570 | for (off = 0; off < mtd->size; off += mtd->erasesize) { |
571 | ret = nand_block_isbad(mtd, off); | |
572 | if (ret) | |
573 | printf(" 0x%08llx%s\n", (unsigned long long)off, | |
574 | ret == 2 ? "\t (bbt reserved)" : ""); | |
575 | } | |
addb2e16 BS |
576 | return 0; |
577 | } | |
578 | ||
856f0544 SR |
579 | /* |
580 | * Syntax is: | |
581 | * 0 1 2 3 4 | |
582 | * nand erase [clean] [off size] | |
583 | */ | |
30486322 | 584 | if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) { |
2255b2d2 | 585 | nand_erase_options_t opts; |
856f0544 | 586 | /* "clean" at index 2 means request to write cleanmarker */ |
3043c045 | 587 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
60899816 MV |
588 | int scrub_yes = argc > 2 && !strcmp("-y", argv[2]); |
589 | int o = (clean || scrub_yes) ? 3 : 2; | |
30486322 | 590 | int scrub = !strncmp(cmd, "scrub", 5); |
30486322 SW |
591 | int spread = 0; |
592 | int args = 2; | |
60899816 MV |
593 | const char *scrub_warn = |
594 | "Warning: " | |
595 | "scrub option will erase all factory set bad blocks!\n" | |
596 | " " | |
597 | "There is no reliable way to recover them.\n" | |
598 | " " | |
599 | "Use this command only for testing purposes if you\n" | |
600 | " " | |
601 | "are sure of what you are doing!\n" | |
602 | "\nReally scrub this NAND flash? <y/N>\n"; | |
30486322 SW |
603 | |
604 | if (cmd[5] != 0) { | |
605 | if (!strcmp(&cmd[5], ".spread")) { | |
606 | spread = 1; | |
607 | } else if (!strcmp(&cmd[5], ".part")) { | |
30486322 SW |
608 | args = 1; |
609 | } else if (!strcmp(&cmd[5], ".chip")) { | |
30486322 SW |
610 | args = 0; |
611 | } else { | |
612 | goto usage; | |
613 | } | |
614 | } | |
615 | ||
616 | /* | |
617 | * Don't allow missing arguments to cause full chip/partition | |
618 | * erases -- easy to do accidentally, e.g. with a misspelled | |
619 | * variable name. | |
620 | */ | |
621 | if (argc != o + args) | |
622 | goto usage; | |
2255b2d2 | 623 | |
30486322 | 624 | printf("\nNAND %s: ", cmd); |
856f0544 | 625 | /* skip first two or three arguments, look for offset and size */ |
09c32807 HS |
626 | if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size, |
627 | &maxsize, MTD_DEV_TYPE_NAND, | |
ad92dff2 | 628 | mtd->size) != 0) |
09c32807 HS |
629 | return 1; |
630 | ||
631 | if (set_dev(dev)) | |
856f0544 | 632 | return 1; |
2255b2d2 | 633 | |
ad92dff2 | 634 | mtd = get_nand_dev_by_index(dev); |
ea533c26 | 635 | |
2255b2d2 SR |
636 | memset(&opts, 0, sizeof(opts)); |
637 | opts.offset = off; | |
638 | opts.length = size; | |
639 | opts.jffs2 = clean; | |
640 | opts.quiet = quiet; | |
30486322 | 641 | opts.spread = spread; |
2255b2d2 SR |
642 | |
643 | if (scrub) { | |
a5dffa4b | 644 | if (scrub_yes) { |
60899816 | 645 | opts.scrub = 1; |
a5dffa4b PA |
646 | } else { |
647 | puts(scrub_warn); | |
648 | if (confirm_yesno()) { | |
6b94b496 | 649 | opts.scrub = 1; |
a5dffa4b | 650 | } else { |
6b94b496 | 651 | puts("scrub aborted\n"); |
46aabcc4 | 652 | return 1; |
6b94b496 | 653 | } |
2255b2d2 SR |
654 | } |
655 | } | |
151c06ec | 656 | ret = nand_erase_opts(mtd, &opts); |
addb2e16 BS |
657 | printf("%s\n", ret ? "ERROR" : "OK"); |
658 | ||
659 | return ret == 0 ? 0 : 1; | |
660 | } | |
661 | ||
662 | if (strncmp(cmd, "dump", 4) == 0) { | |
663 | if (argc < 3) | |
664 | goto usage; | |
665 | ||
7e5f460e | 666 | off = (int)hextoul(argv[2], NULL); |
151c06ec | 667 | ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat); |
addb2e16 BS |
668 | |
669 | return ret == 0 ? 1 : 0; | |
addb2e16 BS |
670 | } |
671 | ||
addb2e16 | 672 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
ea533c26 | 673 | size_t rwsize; |
418396e2 | 674 | ulong pagecount = 1; |
2255b2d2 | 675 | int read; |
ced199dc | 676 | int raw = 0; |
2dc3c483 | 677 | int no_verify = 0; |
d2e0a9a6 | 678 | void *buf; |
2255b2d2 | 679 | |
addb2e16 BS |
680 | if (argc < 4) |
681 | goto usage; | |
2255b2d2 | 682 | |
7e5f460e | 683 | addr = (ulong)hextoul(argv[2], NULL); |
addb2e16 | 684 | |
2255b2d2 | 685 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
856f0544 | 686 | printf("\nNAND %s: ", read ? "read" : "write"); |
4cbb651b | 687 | |
2255b2d2 | 688 | s = strchr(cmd, '.'); |
418396e2 | 689 | |
2dc3c483 | 690 | if (s && !strncmp(s, ".raw", 4)) { |
418396e2 SW |
691 | raw = 1; |
692 | ||
2dc3c483 BB |
693 | if (!strcmp(s, ".raw.noverify")) |
694 | no_verify = 1; | |
695 | ||
09c32807 HS |
696 | if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize, |
697 | MTD_DEV_TYPE_NAND, | |
ad92dff2 | 698 | mtd->size)) |
09c32807 HS |
699 | return 1; |
700 | ||
701 | if (set_dev(dev)) | |
418396e2 SW |
702 | return 1; |
703 | ||
ad92dff2 | 704 | mtd = get_nand_dev_by_index(dev); |
93d3232d | 705 | |
418396e2 SW |
706 | if (argc > 4 && !str2long(argv[4], &pagecount)) { |
707 | printf("'%s' is not a number\n", argv[4]); | |
708 | return 1; | |
709 | } | |
710 | ||
151c06ec | 711 | if (pagecount * mtd->writesize > size) { |
418396e2 SW |
712 | puts("Size exceeds partition or device limit\n"); |
713 | return -1; | |
714 | } | |
715 | ||
151c06ec | 716 | rwsize = pagecount * (mtd->writesize + mtd->oobsize); |
418396e2 | 717 | } else { |
09c32807 HS |
718 | if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off, |
719 | &size, &maxsize, | |
720 | MTD_DEV_TYPE_NAND, | |
ad92dff2 | 721 | mtd->size) != 0) |
09c32807 HS |
722 | return 1; |
723 | ||
724 | if (set_dev(dev)) | |
418396e2 SW |
725 | return 1; |
726 | ||
e834402f HC |
727 | /* size is unspecified */ |
728 | if (argc < 5) | |
729 | adjust_size_for_badblocks(&size, off, dev); | |
418396e2 SW |
730 | rwsize = size; |
731 | } | |
732 | ||
ad92dff2 | 733 | mtd = get_nand_dev_by_index(dev); |
d2e0a9a6 | 734 | buf = map_sysmem(addr, maxsize); |
93d3232d | 735 | |
984e03cd SW |
736 | if (!s || !strcmp(s, ".jffs2") || |
737 | !strcmp(s, ".e") || !strcmp(s, ".i")) { | |
dfbf617f | 738 | if (read) |
151c06ec | 739 | ret = nand_read_skip_bad(mtd, off, &rwsize, |
d2e0a9a6 | 740 | NULL, maxsize, buf); |
dfbf617f | 741 | else |
151c06ec | 742 | ret = nand_write_skip_bad(mtd, off, &rwsize, |
d2e0a9a6 | 743 | NULL, maxsize, buf, |
6b94f118 | 744 | WITH_WR_VERIFY); |
c9494866 BG |
745 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
746 | } else if (!strcmp(s, ".trimffs")) { | |
747 | if (read) { | |
748 | printf("Unknown nand command suffix '%s'\n", s); | |
d2e0a9a6 | 749 | unmap_sysmem(buf); |
c9494866 BG |
750 | return 1; |
751 | } | |
151c06ec | 752 | ret = nand_write_skip_bad(mtd, off, &rwsize, NULL, |
d2e0a9a6 | 753 | maxsize, buf, |
6b94f118 | 754 | WITH_DROP_FFS | WITH_WR_VERIFY); |
47fc18f1 | 755 | #endif |
dfc99e14 | 756 | } else if (!strcmp(s, ".oob")) { |
cfa460ad WJ |
757 | /* out-of-band data */ |
758 | mtd_oob_ops_t ops = { | |
d2e0a9a6 | 759 | .oobbuf = buf, |
ea533c26 | 760 | .ooblen = rwsize, |
dfe64e2c | 761 | .mode = MTD_OPS_RAW |
cfa460ad WJ |
762 | }; |
763 | ||
fb3659ac | 764 | if (read) |
151c06ec | 765 | ret = mtd_read_oob(mtd, off, &ops); |
fb3659ac | 766 | else |
151c06ec | 767 | ret = mtd_write_oob(mtd, off, &ops); |
418396e2 | 768 | } else if (raw) { |
d2e0a9a6 | 769 | ret = raw_access(mtd, buf, off, pagecount, read, |
2dc3c483 | 770 | no_verify); |
856f0544 | 771 | } else { |
984e03cd | 772 | printf("Unknown nand command suffix '%s'.\n", s); |
d2e0a9a6 | 773 | unmap_sysmem(buf); |
984e03cd | 774 | return 1; |
2255b2d2 SR |
775 | } |
776 | ||
d2e0a9a6 | 777 | unmap_sysmem(buf); |
ea533c26 | 778 | printf(" %zu bytes %s: %s\n", rwsize, |
2255b2d2 | 779 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
addb2e16 BS |
780 | |
781 | return ret == 0 ? 0 : 1; | |
782 | } | |
2255b2d2 | 783 | |
3287f6d3 BT |
784 | #ifdef CONFIG_CMD_NAND_TORTURE |
785 | if (strcmp(cmd, "torture") == 0) { | |
1866be7d MK |
786 | loff_t endoff; |
787 | unsigned int failed = 0, passed = 0; | |
788 | ||
3287f6d3 BT |
789 | if (argc < 3) |
790 | goto usage; | |
791 | ||
792 | if (!str2off(argv[2], &off)) { | |
793 | puts("Offset is not a valid number\n"); | |
794 | return 1; | |
795 | } | |
796 | ||
1866be7d MK |
797 | size = mtd->erasesize; |
798 | if (argc > 3) { | |
799 | if (!str2off(argv[3], &size)) { | |
800 | puts("Size is not a valid number\n"); | |
801 | return 1; | |
802 | } | |
803 | } | |
3287f6d3 | 804 | |
1866be7d MK |
805 | endoff = off + size; |
806 | if (endoff > mtd->size) { | |
807 | puts("Arguments beyond end of NAND\n"); | |
808 | return 1; | |
809 | } | |
810 | ||
811 | off = round_down(off, mtd->erasesize); | |
812 | endoff = round_up(endoff, mtd->erasesize); | |
813 | size = endoff - off; | |
814 | printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n", | |
815 | dev, off, size, mtd->erasesize); | |
816 | while (off < endoff) { | |
817 | ret = nand_torture(mtd, off); | |
818 | if (ret) { | |
819 | failed++; | |
820 | printf(" block at 0x%llx failed\n", off); | |
821 | } else { | |
822 | passed++; | |
823 | } | |
824 | off += mtd->erasesize; | |
825 | } | |
826 | printf(" Passed: %u, failed: %u\n", passed, failed); | |
827 | return failed != 0; | |
3287f6d3 BT |
828 | } |
829 | #endif | |
830 | ||
2255b2d2 | 831 | if (strcmp(cmd, "markbad") == 0) { |
8360b66b WD |
832 | argc -= 2; |
833 | argv += 2; | |
2255b2d2 | 834 | |
8360b66b WD |
835 | if (argc <= 0) |
836 | goto usage; | |
837 | ||
838 | while (argc > 0) { | |
7e5f460e | 839 | addr = hextoul(*argv, NULL); |
8360b66b | 840 | |
151c06ec | 841 | if (mtd_block_markbad(mtd, addr)) { |
8360b66b WD |
842 | printf("block 0x%08lx NOT marked " |
843 | "as bad! ERROR %d\n", | |
844 | addr, ret); | |
845 | ret = 1; | |
846 | } else { | |
847 | printf("block 0x%08lx successfully " | |
848 | "marked as bad\n", | |
849 | addr); | |
850 | } | |
851 | --argc; | |
852 | ++argv; | |
2255b2d2 | 853 | } |
8360b66b | 854 | return ret; |
2255b2d2 | 855 | } |
dfbf617f | 856 | |
2255b2d2 | 857 | if (strcmp(cmd, "biterr") == 0) { |
4ea84c5d BG |
858 | int bit; |
859 | ||
860 | if (argc != 4) | |
861 | goto usage; | |
862 | ||
863 | off = (int)simple_strtoul(argv[2], NULL, 16); | |
864 | bit = (int)simple_strtoul(argv[3], NULL, 10); | |
865 | ret = nand_biterror(mtd, off, bit); | |
866 | return ret; | |
2255b2d2 SR |
867 | } |
868 | ||
50657c27 | 869 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
2255b2d2 | 870 | if (strcmp(cmd, "lock") == 0) { |
50657c27 | 871 | int tight = 0; |
2255b2d2 SR |
872 | int status = 0; |
873 | if (argc == 3) { | |
874 | if (!strcmp("tight", argv[2])) | |
875 | tight = 1; | |
876 | if (!strcmp("status", argv[2])) | |
877 | status = 1; | |
878 | } | |
2255b2d2 | 879 | if (status) { |
151c06ec | 880 | do_nand_status(mtd); |
cfa460ad | 881 | } else { |
151c06ec | 882 | if (!nand_lock(mtd, tight)) { |
5e1dae5c WJ |
883 | puts("NAND flash successfully locked\n"); |
884 | } else { | |
885 | puts("Error locking NAND flash\n"); | |
886 | return 1; | |
887 | } | |
2255b2d2 SR |
888 | } |
889 | return 0; | |
890 | } | |
891 | ||
eee623a5 JH |
892 | if (strncmp(cmd, "unlock", 5) == 0) { |
893 | int allexcept = 0; | |
894 | ||
895 | s = strchr(cmd, '.'); | |
896 | ||
897 | if (s && !strcmp(s, ".allexcept")) | |
898 | allexcept = 1; | |
899 | ||
09c32807 HS |
900 | if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size, |
901 | &maxsize, MTD_DEV_TYPE_NAND, | |
ad92dff2 | 902 | mtd->size) < 0) |
09c32807 HS |
903 | return 1; |
904 | ||
905 | if (set_dev(dev)) | |
856f0544 | 906 | return 1; |
2255b2d2 | 907 | |
ad92dff2 M |
908 | mtd = get_nand_dev_by_index(dev); |
909 | ||
910 | if (!nand_unlock(mtd, off, size, allexcept)) { | |
5e1dae5c WJ |
911 | puts("NAND flash successfully unlocked\n"); |
912 | } else { | |
913 | puts("Error unlocking NAND flash, " | |
3043c045 | 914 | "write and erase will probably fail\n"); |
5e1dae5c WJ |
915 | return 1; |
916 | } | |
2255b2d2 SR |
917 | return 0; |
918 | } | |
50657c27 | 919 | #endif |
2255b2d2 | 920 | |
addb2e16 | 921 | usage: |
4c12eeb8 | 922 | return CMD_RET_USAGE; |
addb2e16 BS |
923 | } |
924 | ||
3616218b | 925 | U_BOOT_LONGHELP(nand, |
a89c33db WD |
926 | "info - show available NAND devices\n" |
927 | "nand device [dev] - show or set current device\n" | |
928 | "nand read - addr off|partition size\n" | |
929 | "nand write - addr off|partition size\n" | |
930 | " read/write 'size' bytes starting at offset 'off'\n" | |
931 | " to/from memory address 'addr', skipping bad blocks.\n" | |
418396e2 | 932 | "nand read.raw - addr off|partition [count]\n" |
2dc3c483 | 933 | "nand write.raw[.noverify] - addr off|partition [count]\n" |
418396e2 | 934 | " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n" |
c9494866 BG |
935 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
936 | "nand write.trimffs - addr off|partition size\n" | |
937 | " write 'size' bytes starting at offset 'off' from memory address\n" | |
938 | " 'addr', skipping bad blocks and dropping any pages at the end\n" | |
939 | " of eraseblocks that contain only 0xFF\n" | |
47fc18f1 | 940 | #endif |
eb3abce8 | 941 | "nand erase[.spread] [clean] off size - erase 'size' bytes " |
30486322 SW |
942 | "from offset 'off'\n" |
943 | " With '.spread', erase enough for given file size, otherwise,\n" | |
944 | " 'size' includes skipped bad blocks.\n" | |
945 | "nand erase.part [clean] partition - erase entire mtd partition'\n" | |
946 | "nand erase.chip [clean] - erase entire chip'\n" | |
a89c33db WD |
947 | "nand bad - show bad blocks\n" |
948 | "nand dump[.oob] off - dump page\n" | |
3287f6d3 | 949 | #ifdef CONFIG_CMD_NAND_TORTURE |
1866be7d MK |
950 | "nand torture off - torture one block at offset\n" |
951 | "nand torture off [size] - torture blocks from off to off+size\n" | |
3287f6d3 | 952 | #endif |
60899816 | 953 | "nand scrub [-y] off size | scrub.part partition | scrub.chip\n" |
30486322 | 954 | " really clean NAND erasing bad blocks (UNSAFE)\n" |
a89c33db | 955 | "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" |
4ea84c5d | 956 | "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)" |
50657c27 | 957 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
a89c33db WD |
958 | "\n" |
959 | "nand lock [tight] [status]\n" | |
960 | " bring nand to lock state or display locked pages\n" | |
eee623a5 | 961 | "nand unlock[.allexcept] [offset] [size] - unlock section" |
50657c27 | 962 | #endif |
c9f7351b BG |
963 | #ifdef CONFIG_ENV_OFFSET_OOB |
964 | "\n" | |
965 | "nand env.oob - environment offset in OOB of block 0 of" | |
966 | " first device.\n" | |
967 | "nand env.oob set off|partition - set enviromnent offset\n" | |
968 | "nand env.oob get - get environment offset" | |
969 | #endif | |
3616218b | 970 | ); |
088f1b19 KP |
971 | |
972 | U_BOOT_CMD( | |
973 | nand, CONFIG_SYS_MAXARGS, 1, do_nand, | |
974 | "NAND sub-system", nand_help_text | |
50657c27 | 975 | ); |
addb2e16 | 976 | |
09140113 | 977 | static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd, |
4b070809 | 978 | ulong offset, ulong addr, char *cmd) |
addb2e16 | 979 | { |
addb2e16 | 980 | int r; |
67d668bf | 981 | char *s; |
4ca79f47 | 982 | size_t cnt; |
c76c93a3 | 983 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
f3543e69 | 984 | struct legacy_img_hdr *hdr; |
21d29f7f | 985 | #endif |
09475f75 | 986 | #if defined(CONFIG_FIT) |
3bab76a2 | 987 | const void *fit_hdr = NULL; |
09475f75 | 988 | #endif |
10e03893 TK |
989 | |
990 | s = strchr(cmd, '.'); | |
991 | if (s != NULL && | |
65d8bc94 | 992 | (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { |
984e03cd | 993 | printf("Unknown nand load suffix '%s'\n", s); |
770605e4 | 994 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
984e03cd SW |
995 | return 1; |
996 | } | |
addb2e16 | 997 | |
151c06ec | 998 | printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset); |
addb2e16 | 999 | |
151c06ec SW |
1000 | cnt = mtd->writesize; |
1001 | r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, | |
1002 | (u_char *)addr); | |
addb2e16 | 1003 | if (r) { |
856f0544 | 1004 | puts("** Read error\n"); |
770605e4 | 1005 | bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ); |
addb2e16 BS |
1006 | return 1; |
1007 | } | |
770605e4 | 1008 | bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ); |
addb2e16 | 1009 | |
9a4daad0 | 1010 | switch (genimg_get_format ((void *)addr)) { |
c76c93a3 | 1011 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
d5934ad7 | 1012 | case IMAGE_FORMAT_LEGACY: |
f3543e69 | 1013 | hdr = (struct legacy_img_hdr *)addr; |
d5934ad7 | 1014 | |
770605e4 | 1015 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
d5934ad7 | 1016 | image_print_contents (hdr); |
addb2e16 | 1017 | |
d5934ad7 MB |
1018 | cnt = image_get_image_size (hdr); |
1019 | break; | |
21d29f7f | 1020 | #endif |
d5934ad7 MB |
1021 | #if defined(CONFIG_FIT) |
1022 | case IMAGE_FORMAT_FIT: | |
09475f75 | 1023 | fit_hdr = (const void *)addr; |
09475f75 MB |
1024 | puts ("Fit image detected...\n"); |
1025 | ||
1026 | cnt = fit_get_size (fit_hdr); | |
1027 | break; | |
d5934ad7 MB |
1028 | #endif |
1029 | default: | |
770605e4 | 1030 | bootstage_error(BOOTSTAGE_ID_NAND_TYPE); |
d5934ad7 | 1031 | puts ("** Unknown image type\n"); |
addb2e16 BS |
1032 | return 1; |
1033 | } | |
770605e4 | 1034 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
addb2e16 | 1035 | |
151c06ec SW |
1036 | r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, |
1037 | (u_char *)addr); | |
addb2e16 | 1038 | if (r) { |
856f0544 | 1039 | puts("** Read error\n"); |
770605e4 | 1040 | bootstage_error(BOOTSTAGE_ID_NAND_READ); |
addb2e16 BS |
1041 | return 1; |
1042 | } | |
770605e4 | 1043 | bootstage_mark(BOOTSTAGE_ID_NAND_READ); |
addb2e16 | 1044 | |
09475f75 MB |
1045 | #if defined(CONFIG_FIT) |
1046 | /* This cannot be done earlier, we need complete FIT image in RAM first */ | |
3bab76a2 | 1047 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
c5819701 | 1048 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
770605e4 | 1049 | bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ); |
3bab76a2 MB |
1050 | puts ("** Bad FIT image format\n"); |
1051 | return 1; | |
1052 | } | |
770605e4 | 1053 | bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK); |
3bab76a2 MB |
1054 | fit_print_contents (fit_hdr); |
1055 | } | |
09475f75 MB |
1056 | #endif |
1057 | ||
addb2e16 BS |
1058 | /* Loading ok, update default load address */ |
1059 | ||
bb872dd9 | 1060 | image_load_addr = addr; |
addb2e16 | 1061 | |
67d668bf | 1062 | return bootm_maybe_autostart(cmdtp, cmd); |
addb2e16 BS |
1063 | } |
1064 | ||
09140113 SG |
1065 | static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc, |
1066 | char *const argv[]) | |
856f0544 SR |
1067 | { |
1068 | char *boot_device = NULL; | |
1069 | int idx; | |
1070 | ulong addr, offset = 0; | |
ad92dff2 | 1071 | struct mtd_info *mtd; |
0c8a8491 | 1072 | #if defined(CONFIG_CMD_MTDPARTS) |
856f0544 SR |
1073 | struct mtd_device *dev; |
1074 | struct part_info *part; | |
1075 | u8 pnum; | |
1076 | ||
1077 | if (argc >= 2) { | |
1078 | char *p = (argc == 2) ? argv[1] : argv[2]; | |
1079 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && | |
1080 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { | |
1081 | if (dev->id->type != MTD_DEV_TYPE_NAND) { | |
1082 | puts("Not a NAND device\n"); | |
1083 | return 1; | |
1084 | } | |
1085 | if (argc > 3) | |
1086 | goto usage; | |
1087 | if (argc == 3) | |
7e5f460e | 1088 | addr = hextoul(argv[1], NULL); |
856f0544 | 1089 | else |
6d0f6bcf | 1090 | addr = CONFIG_SYS_LOAD_ADDR; |
ad92dff2 M |
1091 | |
1092 | mtd = get_nand_dev_by_index(dev->id->num); | |
1093 | return nand_load_image(cmdtp, mtd, part->offset, | |
1094 | addr, argv[0]); | |
856f0544 SR |
1095 | } |
1096 | } | |
1097 | #endif | |
1098 | ||
770605e4 | 1099 | bootstage_mark(BOOTSTAGE_ID_NAND_PART); |
856f0544 SR |
1100 | switch (argc) { |
1101 | case 1: | |
6d0f6bcf | 1102 | addr = CONFIG_SYS_LOAD_ADDR; |
00caae6d | 1103 | boot_device = env_get("bootdevice"); |
856f0544 SR |
1104 | break; |
1105 | case 2: | |
7e5f460e | 1106 | addr = hextoul(argv[1], NULL); |
00caae6d | 1107 | boot_device = env_get("bootdevice"); |
856f0544 SR |
1108 | break; |
1109 | case 3: | |
7e5f460e | 1110 | addr = hextoul(argv[1], NULL); |
856f0544 SR |
1111 | boot_device = argv[2]; |
1112 | break; | |
1113 | case 4: | |
7e5f460e | 1114 | addr = hextoul(argv[1], NULL); |
856f0544 | 1115 | boot_device = argv[2]; |
7e5f460e | 1116 | offset = hextoul(argv[3], NULL); |
856f0544 SR |
1117 | break; |
1118 | default: | |
0c8a8491 | 1119 | #if defined(CONFIG_CMD_MTDPARTS) |
856f0544 SR |
1120 | usage: |
1121 | #endif | |
770605e4 | 1122 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
4c12eeb8 | 1123 | return CMD_RET_USAGE; |
856f0544 | 1124 | } |
770605e4 | 1125 | bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX); |
856f0544 SR |
1126 | |
1127 | if (!boot_device) { | |
1128 | puts("\n** No boot device **\n"); | |
770605e4 | 1129 | bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
856f0544 SR |
1130 | return 1; |
1131 | } | |
770605e4 | 1132 | bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
addb2e16 | 1133 | |
7e5f460e | 1134 | idx = hextoul(boot_device, NULL); |
856f0544 | 1135 | |
ad92dff2 M |
1136 | mtd = get_nand_dev_by_index(idx); |
1137 | if (!mtd) { | |
856f0544 | 1138 | printf("\n** Device %d not available\n", idx); |
770605e4 | 1139 | bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE); |
856f0544 SR |
1140 | return 1; |
1141 | } | |
770605e4 | 1142 | bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE); |
856f0544 | 1143 | |
ad92dff2 | 1144 | return nand_load_image(cmdtp, mtd, offset, addr, argv[0]); |
856f0544 SR |
1145 | } |
1146 | ||
1147 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, | |
2fb2604d | 1148 | "boot from NAND device", |
a89c33db WD |
1149 | "[partition] | [[[loadAddr] dev] offset]" |
1150 | ); |