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