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