blob: 1a3c1df03e01fe9fca9d1d5d39afffcc69fa121e [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
wdenk384cc682005-04-03 22:35:21 +00006 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkdc7c9a12003-03-26 06:55:25 +00009 */
10
11#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010012
13
14#ifndef CFG_NAND_LEGACY
15/*
16 *
17 * New NAND support
18 *
19 */
20#include <common.h>
21
Jon Loeligerc76fe472007-07-08 18:02:23 -050022#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010023
24#include <command.h>
25#include <watchdog.h>
26#include <malloc.h>
27#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010028#include <jffs2/jffs2.h>
29#include <nand.h>
30
Jon Loeligerc76fe472007-07-08 18:02:23 -050031#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +020032
33/* parition handling routines */
34int mtdparts_init(void);
35int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36int find_dev_and_part(const char *id, struct mtd_device **dev,
37 u8 *part_num, struct part_info **part);
38#endif
39
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010040extern nand_info_t nand_info[]; /* info for NAND chips */
41
42static int nand_dump_oob(nand_info_t *nand, ulong off)
43{
44 return 0;
45}
46
47static int nand_dump(nand_info_t *nand, ulong off)
48{
49 int i;
50 u_char *buf, *p;
51
52 buf = malloc(nand->oobblock + nand->oobsize);
53 if (!buf) {
54 puts("No memory for page buffer\n");
55 return 1;
56 }
57 off &= ~(nand->oobblock - 1);
58 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
59 if (i < 0) {
60 printf("Error (%d) reading page %08x\n", i, off);
61 free(buf);
62 return 1;
63 }
64 printf("Page %08x dump:\n", off);
65 i = nand->oobblock >> 4; p = buf;
66 while (i--) {
67 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
68 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
69 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
70 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
71 p += 16;
72 }
73 puts("OOB:\n");
74 i = nand->oobsize >> 3;
75 while (i--) {
76 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
77 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
78 p += 8;
79 }
80 free(buf);
81
82 return 0;
83}
84
85/* ------------------------------------------------------------------------- */
86
Stefan Roese856f0542006-10-28 15:55:52 +020087static inline int str2long(char *p, ulong *num)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010088{
Stefan Roese856f0542006-10-28 15:55:52 +020089 char *endptr;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010090
Stefan Roese856f0542006-10-28 15:55:52 +020091 *num = simple_strtoul(p, &endptr, 16);
92 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
93}
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010094
Stefan Roese856f0542006-10-28 15:55:52 +020095static int
Wolfgang Denk4ca79f42008-04-28 12:08:18 +020096arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
Stefan Roese856f0542006-10-28 15:55:52 +020097{
98 int idx = nand_curr_device;
Jon Loeligerc76fe472007-07-08 18:02:23 -050099#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200100 struct mtd_device *dev;
101 struct part_info *part;
102 u8 pnum;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100103
Stefan Roese856f0542006-10-28 15:55:52 +0200104 if (argc >= 1 && !(str2long(argv[0], off))) {
105 if ((mtdparts_init() == 0) &&
106 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
107 if (dev->id->type != MTD_DEV_TYPE_NAND) {
108 puts("not a NAND device\n");
109 return -1;
110 }
111 *off = part->offset;
112 if (argc >= 2) {
113 if (!(str2long(argv[1], size))) {
114 printf("'%s' is not a number\n", argv[1]);
115 return -1;
116 }
117 if (*size > part->size)
118 *size = part->size;
119 } else {
120 *size = part->size;
121 }
122 idx = dev->id->num;
123 *nand = nand_info[idx];
124 goto out;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100125 }
Stefan Roese856f0542006-10-28 15:55:52 +0200126 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100127#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100128
Stefan Roese856f0542006-10-28 15:55:52 +0200129 if (argc >= 1) {
130 if (!(str2long(argv[0], off))) {
131 printf("'%s' is not a number\n", argv[0]);
132 return -1;
133 }
134 } else {
135 *off = 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100136 }
137
Stefan Roese856f0542006-10-28 15:55:52 +0200138 if (argc >= 2) {
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200139 if (!(str2long(argv[1], (ulong *)size))) {
Stefan Roese856f0542006-10-28 15:55:52 +0200140 printf("'%s' is not a number\n", argv[1]);
141 return -1;
142 }
143 } else {
144 *size = nand->size - *off;
145 }
146
Jon Loeligerc76fe472007-07-08 18:02:23 -0500147#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200148out:
149#endif
150 printf("device %d ", idx);
151 if (*size == nand->size)
152 puts("whole chip\n");
153 else
154 printf("offset 0x%x, size 0x%x\n", *off, *size);
155 return 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100156}
157
158int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
159{
160 int i, dev, ret;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200161 ulong addr, off;
162 size_t size;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100163 char *cmd, *s;
164 nand_info_t *nand;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200165#ifdef CFG_NAND_QUIET
166 int quiet = CFG_NAND_QUIET;
167#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200168 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200169#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200170 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100171
172 /* at least two arguments please */
173 if (argc < 2)
174 goto usage;
175
Stefan Roese2255b2d2006-10-10 12:36:02 +0200176 if (quiet_str)
177 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
178
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100179 cmd = argv[1];
180
181 if (strcmp(cmd, "info") == 0) {
182
183 putc('\n');
184 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
185 if (nand_info[i].name)
186 printf("Device %d: %s, sector size %lu KiB\n",
187 i, nand_info[i].name,
188 nand_info[i].erasesize >> 10);
189 }
190 return 0;
191 }
192
193 if (strcmp(cmd, "device") == 0) {
194
195 if (argc < 3) {
196 if ((nand_curr_device < 0) ||
197 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
198 puts("\nno devices available\n");
199 else
200 printf("\nDevice %d: %s\n", nand_curr_device,
201 nand_info[nand_curr_device].name);
202 return 0;
203 }
204 dev = (int)simple_strtoul(argv[2], NULL, 10);
205 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
206 puts("No such device\n");
207 return 1;
208 }
209 printf("Device %d: %s", dev, nand_info[dev].name);
210 puts("... is now current device\n");
211 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200212
213#ifdef CFG_NAND_SELECT_DEVICE
214 /*
215 * Select the chip in the board/cpu specific driver
216 */
217 board_nand_select_device(nand_info[dev].priv, dev);
218#endif
219
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100220 return 0;
221 }
222
223 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
224 strncmp(cmd, "dump", 4) != 0 &&
Stefan Roese2255b2d2006-10-10 12:36:02 +0200225 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
226 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
227 strcmp(cmd, "biterr") != 0 &&
228 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100229 goto usage;
230
231 /* the following commands operate on the current device */
232 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
233 !nand_info[nand_curr_device].name) {
234 puts("\nno devices available\n");
235 return 1;
236 }
237 nand = &nand_info[nand_curr_device];
238
239 if (strcmp(cmd, "bad") == 0) {
240 printf("\nDevice %d bad blocks:\n", nand_curr_device);
241 for (off = 0; off < nand->size; off += nand->erasesize)
242 if (nand_block_isbad(nand, off))
243 printf(" %08x\n", off);
244 return 0;
245 }
246
Stefan Roese856f0542006-10-28 15:55:52 +0200247 /*
248 * Syntax is:
249 * 0 1 2 3 4
250 * nand erase [clean] [off size]
251 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200252 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
253 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200254 /* "clean" at index 2 means request to write cleanmarker */
255 int clean = argc > 2 && !strcmp("clean", argv[2]);
256 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200257 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100258
Stefan Roese856f0542006-10-28 15:55:52 +0200259 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
260 /* skip first two or three arguments, look for offset and size */
261 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
262 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200263
264 memset(&opts, 0, sizeof(opts));
265 opts.offset = off;
266 opts.length = size;
267 opts.jffs2 = clean;
268 opts.quiet = quiet;
269
270 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200271 puts("Warning: "
272 "scrub option will erase all factory set "
273 "bad blocks!\n"
274 " "
275 "There is no reliable way to recover them.\n"
276 " "
277 "Use this command only for testing purposes "
278 "if you\n"
279 " "
280 "are sure of what you are doing!\n"
281 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200282
283 if (getc() == 'y' && getc() == '\r') {
284 opts.scrub = 1;
285 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200286 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200287 return -1;
288 }
289 }
290 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100291 printf("%s\n", ret ? "ERROR" : "OK");
292
293 return ret == 0 ? 0 : 1;
294 }
295
296 if (strncmp(cmd, "dump", 4) == 0) {
297 if (argc < 3)
298 goto usage;
299
300 s = strchr(cmd, '.');
301 off = (int)simple_strtoul(argv[2], NULL, 16);
302
303 if (s != NULL && strcmp(s, ".oob") == 0)
304 ret = nand_dump_oob(nand, off);
305 else
306 ret = nand_dump(nand, off);
307
308 return ret == 0 ? 1 : 0;
309
310 }
311
312 /* read write */
313 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200314 int read;
315
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100316 if (argc < 4)
317 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200318
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100319 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
320
Stefan Roese2255b2d2006-10-10 12:36:02 +0200321 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200322 printf("\nNAND %s: ", read ? "read" : "write");
323 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
324 return 1;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100325
Stefan Roese2255b2d2006-10-10 12:36:02 +0200326 s = strchr(cmd, '.');
327 if (s != NULL &&
328 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
329 if (read) {
330 /* read */
331 nand_read_options_t opts;
332 memset(&opts, 0, sizeof(opts));
333 opts.buffer = (u_char*) addr;
334 opts.length = size;
335 opts.offset = off;
336 opts.quiet = quiet;
337 ret = nand_read_opts(nand, &opts);
338 } else {
339 /* write */
340 nand_write_options_t opts;
341 memset(&opts, 0, sizeof(opts));
342 opts.buffer = (u_char*) addr;
343 opts.length = size;
344 opts.offset = off;
345 /* opts.forcejffs2 = 1; */
346 opts.pad = 1;
347 opts.blockalign = 1;
348 opts.quiet = quiet;
349 ret = nand_write_opts(nand, &opts);
350 }
Harald Welte62d4f432007-12-19 14:12:53 +0100351 } else if (s != NULL && !strcmp(s, ".oob")) {
352 /* read out-of-band data */
353 if (read)
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200354 ret = nand->read_oob(nand, off, size, &size,
Harald Welte62d4f432007-12-19 14:12:53 +0100355 (u_char *) addr);
356 else
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200357 ret = nand->write_oob(nand, off, size, &size,
Harald Welte62d4f432007-12-19 14:12:53 +0100358 (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200359 } else {
360 if (read)
361 ret = nand_read(nand, off, &size, (u_char *)addr);
362 else
363 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200364 }
365
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100366 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200367 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100368
369 return ret == 0 ? 0 : 1;
370 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200371
Stefan Roese2255b2d2006-10-10 12:36:02 +0200372 if (strcmp(cmd, "markbad") == 0) {
373 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
374
375 int ret = nand->block_markbad(nand, addr);
376 if (ret == 0) {
377 printf("block 0x%08lx successfully marked as bad\n",
378 (ulong) addr);
379 return 0;
380 } else {
381 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
382 (ulong) addr, ret);
383 }
384 return 1;
385 }
386 if (strcmp(cmd, "biterr") == 0) {
387 /* todo */
388 return 1;
389 }
390
391 if (strcmp(cmd, "lock") == 0) {
392 int tight = 0;
393 int status = 0;
394 if (argc == 3) {
395 if (!strcmp("tight", argv[2]))
396 tight = 1;
397 if (!strcmp("status", argv[2]))
398 status = 1;
399 }
400
401 if (status) {
402 ulong block_start = 0;
403 ulong off;
404 int last_status = -1;
405
406 struct nand_chip *nand_chip = nand->priv;
407 /* check the WP bit */
408 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
409 printf("device is %swrite protected\n",
410 (nand_chip->read_byte(nand) & 0x80 ?
411 "NOT " : "" ) );
412
413 for (off = 0; off < nand->size; off += nand->oobblock) {
414 int s = nand_get_lock_status(nand, off);
415
416 /* print message only if status has changed
417 * or at end of chip
418 */
419 if (off == nand->size - nand->oobblock
420 || (s != last_status && off != 0)) {
421
422 printf("%08x - %08x: %8d pages %s%s%s\n",
423 block_start,
424 off-1,
425 (off-block_start)/nand->oobblock,
426 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
427 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
428 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
429 }
430
431 last_status = s;
432 }
433 } else {
434 if (!nand_lock(nand, tight)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200435 puts("NAND flash successfully locked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200436 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200437 puts("Error locking NAND flash\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200438 return 1;
439 }
440 }
441 return 0;
442 }
443
444 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200445 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
446 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200447
448 if (!nand_unlock(nand, off, size)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200449 puts("NAND flash successfully unlocked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200450 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200451 puts("Error unlocking NAND flash, "
452 "write and erase will probably fail\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200453 return 1;
454 }
455 return 0;
456 }
457
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100458usage:
459 printf("Usage:\n%s\n", cmdtp->usage);
460 return 1;
461}
462
463U_BOOT_CMD(nand, 5, 1, do_nand,
464 "nand - NAND sub-system\n",
465 "info - show available NAND devices\n"
466 "nand device [dev] - show or set current device\n"
Stefan Roese856f0542006-10-28 15:55:52 +0200467 "nand read[.jffs2] - addr off|partition size\n"
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200468 "nand write[.jffs2] - addr off|partition size - read/write `size' bytes starting\n"
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100469 " at offset `off' to/from memory address `addr'\n"
470 "nand erase [clean] [off size] - erase `size' bytes from\n"
471 " offset `off' (entire device if not specified)\n"
472 "nand bad - show bad blocks\n"
473 "nand dump[.oob] off - dump page\n"
474 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
475 "nand markbad off - mark bad block at offset (UNSAFE)\n"
Stefan Roese2255b2d2006-10-10 12:36:02 +0200476 "nand biterr off - make a bit error at offset (UNSAFE)\n"
477 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
478 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100479
Stefan Roese856f0542006-10-28 15:55:52 +0200480static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
481 ulong offset, ulong addr, char *cmd)
482{
483 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200484 char *ep, *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200485 size_t cnt;
Stefan Roese856f0542006-10-28 15:55:52 +0200486 image_header_t *hdr;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200487 int jffs2 = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100488#if defined(CONFIG_FIT)
489 const void *fit_hdr;
490#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200491
492 s = strchr(cmd, '.');
493 if (s != NULL &&
494 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
495 jffs2 = 1;
Stefan Roese856f0542006-10-28 15:55:52 +0200496
497 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
498
499 cnt = nand->oobblock;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200500 if (jffs2) {
501 nand_read_options_t opts;
502 memset(&opts, 0, sizeof(opts));
503 opts.buffer = (u_char*) addr;
504 opts.length = cnt;
505 opts.offset = offset;
506 opts.quiet = 1;
507 r = nand_read_opts(nand, &opts);
508 } else {
509 r = nand_read(nand, offset, &cnt, (u_char *) addr);
510 }
511
Stefan Roese856f0542006-10-28 15:55:52 +0200512 if (r) {
513 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200514 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200515 return 1;
516 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200517 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200518
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100519 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100520 case IMAGE_FORMAT_LEGACY:
521 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200522
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100523 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100524 image_print_contents (hdr);
525
526 cnt = image_get_image_size (hdr);
527 break;
528#if defined(CONFIG_FIT)
529 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100530 fit_hdr = (const void *)addr;
531 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100532 show_boot_progress (-150);
Marian Balakowicz09475f72008-03-12 10:33:01 +0100533 puts ("** Bad FIT image format\n");
534 return 1;
535 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100536 show_boot_progress (151);
Marian Balakowicz09475f72008-03-12 10:33:01 +0100537 puts ("Fit image detected...\n");
538
539 cnt = fit_get_size (fit_hdr);
540 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100541#endif
542 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100543 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100544 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200545 return 1;
546 }
547
Thomas Knobloch10e03892007-07-06 14:58:39 +0200548 if (jffs2) {
549 nand_read_options_t opts;
550 memset(&opts, 0, sizeof(opts));
551 opts.buffer = (u_char*) addr;
552 opts.length = cnt;
553 opts.offset = offset;
554 opts.quiet = 1;
555 r = nand_read_opts(nand, &opts);
556 } else {
557 r = nand_read(nand, offset, &cnt, (u_char *) addr);
558 }
Stefan Roese856f0542006-10-28 15:55:52 +0200559
Stefan Roese856f0542006-10-28 15:55:52 +0200560 if (r) {
561 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200562 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200563 return 1;
564 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200565 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200566
Marian Balakowicz09475f72008-03-12 10:33:01 +0100567#if defined(CONFIG_FIT)
568 /* This cannot be done earlier, we need complete FIT image in RAM first */
569 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
570 fit_print_contents ((const void *)addr);
571#endif
572
Stefan Roese856f0542006-10-28 15:55:52 +0200573 /* Loading ok, update default load address */
574
575 load_addr = addr;
576
577 /* Check if we should attempt an auto-start */
578 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
579 char *local_args[2];
580 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
581
582 local_args[0] = cmd;
583 local_args[1] = NULL;
584
585 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
586
587 do_bootm(cmdtp, 0, 1, local_args);
588 return 1;
589 }
590 return 0;
591}
592
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100593int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
594{
595 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200596 int idx;
597 ulong addr, offset = 0;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500598#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200599 struct mtd_device *dev;
600 struct part_info *part;
601 u8 pnum;
602
603 if (argc >= 2) {
604 char *p = (argc == 2) ? argv[1] : argv[2];
605 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
606 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
607 if (dev->id->type != MTD_DEV_TYPE_NAND) {
608 puts("Not a NAND device\n");
609 return 1;
610 }
611 if (argc > 3)
612 goto usage;
613 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200614 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200615 else
616 addr = CFG_LOAD_ADDR;
617 return nand_load_image(cmdtp, &nand_info[dev->id->num],
618 part->offset, addr, argv[0]);
619 }
620 }
621#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100622
Heiko Schocherfad63402007-07-13 09:54:17 +0200623 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100624 switch (argc) {
625 case 1:
626 addr = CFG_LOAD_ADDR;
627 boot_device = getenv("bootdevice");
628 break;
629 case 2:
630 addr = simple_strtoul(argv[1], NULL, 16);
631 boot_device = getenv("bootdevice");
632 break;
633 case 3:
634 addr = simple_strtoul(argv[1], NULL, 16);
635 boot_device = argv[2];
636 break;
637 case 4:
638 addr = simple_strtoul(argv[1], NULL, 16);
639 boot_device = argv[2];
640 offset = simple_strtoul(argv[3], NULL, 16);
641 break;
642 default:
Jon Loeligerc76fe472007-07-08 18:02:23 -0500643#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200644usage:
645#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100646 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200647 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100648 return 1;
649 }
650
Heiko Schocherfad63402007-07-13 09:54:17 +0200651 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100652 if (!boot_device) {
653 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200654 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100655 return 1;
656 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200657 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100658
Stefan Roese856f0542006-10-28 15:55:52 +0200659 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100660
Stefan Roese856f0542006-10-28 15:55:52 +0200661 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
662 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200663 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100664 return 1;
665 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200666 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100667
Stefan Roese856f0542006-10-28 15:55:52 +0200668 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100669}
670
671U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200672 "nboot - boot from NAND device\n",
Thomas Knobloch10e03892007-07-06 14:58:39 +0200673 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100674
Jon Loeligerc76fe472007-07-08 18:02:23 -0500675#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100676
677#else /* CFG_NAND_LEGACY */
678/*
679 *
680 * Legacy NAND support - to be phased out
681 *
682 */
wdenkdc7c9a12003-03-26 06:55:25 +0000683#include <command.h>
684#include <malloc.h>
685#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000686#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000687
Heiko Schocherfad63402007-07-13 09:54:17 +0200688#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000689# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200690# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000691#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200692# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000693#endif
694
Jon Loeligerc76fe472007-07-08 18:02:23 -0500695#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100696#include <linux/mtd/nand_legacy.h>
697#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000698#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000699#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100700#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000701
wdenk1f4bb372003-07-27 00:21:01 +0000702#ifdef CONFIG_OMAP1510
703void archflashwp(void *archdata, int wp);
704#endif
705
706#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
707
wdenka43278a2003-09-11 19:48:06 +0000708#undef NAND_DEBUG
wdenkdc7c9a12003-03-26 06:55:25 +0000709#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000710
711/* ****************** WARNING *********************
712 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
713 * erase (or at least attempt to erase) blocks that are marked
714 * bad. This can be very handy if you are _sure_ that the block
715 * is OK, say because you marked a good block bad to test bad
716 * block handling and you are done testing, or if you have
717 * accidentally marked blocks bad.
718 *
719 * Erasing factory marked bad blocks is a _bad_ idea. If the
720 * erase succeeds there is no reliable way to find them again,
721 * and attempting to program or erase bad blocks can affect
722 * the data in _other_ (good) blocks.
723 */
724#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000725
726#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000727#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000728
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100729/* bits for nand_legacy_rw() `cmd'; or together as needed */
wdenk7a8e9bed2003-05-31 18:35:21 +0000730#define NANDRW_READ 0x01
731#define NANDRW_WRITE 0x00
732#define NANDRW_JFFS2 0x02
wdenka3d991b2004-04-15 21:48:45 +0000733#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000734
wdenkdc7c9a12003-03-26 06:55:25 +0000735/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100736 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000737 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100738extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
739extern int curr_device;
740extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
741 size_t len, int clean);
742extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
743 size_t len, size_t *retlen, u_char *buf);
744extern void nand_print(struct nand_chip *nand);
745extern void nand_print_bad(struct nand_chip *nand);
746extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
747 size_t len, size_t *retlen, u_char *buf);
748extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
749 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000750
wdenkdc7c9a12003-03-26 06:55:25 +0000751
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100752int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000753{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100754 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000755
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100756 switch (argc) {
757 case 0:
758 case 1:
759 printf ("Usage:\n%s\n", cmdtp->usage);
760 return 1;
761 case 2:
762 if (strcmp (argv[1], "info") == 0) {
763 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000764
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100765 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000766
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100767 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
768 if (nand_dev_desc[i].ChipID ==
769 NAND_ChipID_UNKNOWN)
770 continue; /* list only known devices */
771 printf ("Device %d: ", i);
772 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000773 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100774 return 0;
775
776 } else if (strcmp (argv[1], "device") == 0) {
777 if ((curr_device < 0)
778 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
779 puts ("\nno devices available\n");
780 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000781 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100782 printf ("\nDevice %d: ", curr_device);
783 nand_print (&nand_dev_desc[curr_device]);
784 return 0;
785
786 } else if (strcmp (argv[1], "bad") == 0) {
787 if ((curr_device < 0)
788 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
789 puts ("\nno devices available\n");
790 return 1;
791 }
792 printf ("\nDevice %d bad blocks:\n", curr_device);
793 nand_print_bad (&nand_dev_desc[curr_device]);
794 return 0;
795
796 }
797 printf ("Usage:\n%s\n", cmdtp->usage);
798 return 1;
799 case 3:
800 if (strcmp (argv[1], "device") == 0) {
801 int dev = (int) simple_strtoul (argv[2], NULL, 10);
802
803 printf ("\nDevice %d: ", dev);
804 if (dev >= CFG_MAX_NAND_DEVICE) {
805 puts ("unknown device\n");
806 return 1;
807 }
808 nand_print (&nand_dev_desc[dev]);
809 /*nand_print (dev); */
810
811 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
812 return 1;
813 }
814
815 curr_device = dev;
816
817 puts ("... is now current device\n");
818
819 return 0;
820 } else if (strcmp (argv[1], "erase") == 0
821 && strcmp (argv[2], "clean") == 0) {
822 struct nand_chip *nand = &nand_dev_desc[curr_device];
823 ulong off = 0;
824 ulong size = nand->totlen;
825 int ret;
826
827 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
828
829 ret = nand_legacy_erase (nand, off, size, 1);
830
831 printf ("%s\n", ret ? "ERROR" : "OK");
832
wdenk7a8e9bed2003-05-31 18:35:21 +0000833 return ret;
834 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100835
836 printf ("Usage:\n%s\n", cmdtp->usage);
837 return 1;
838 default:
839 /* at least 4 args */
840
841 if (strncmp (argv[1], "read", 4) == 0 ||
842 strncmp (argv[1], "write", 5) == 0) {
Wolfgang Denkc71abba2008-04-28 14:55:12 +0200843 ulong addr = simple_strtoul (argv[2], NULL, 16);
844 off_t off = simple_strtoul (argv[3], NULL, 16);
845 size_t size = simple_strtoul (argv[4], NULL, 16);
846 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
847 NANDRW_READ : NANDRW_WRITE;
848 size_t total;
849 int ret;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100850 char *cmdtail = strchr (argv[1], '.');
851
852 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
853 /* read out-of-band data */
854 if (cmd & NANDRW_READ) {
855 ret = nand_read_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200856 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100857 (u_char *) addr);
858 } else {
859 ret = nand_write_oob (nand_dev_desc + curr_device,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200860 off, size, &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100861 (u_char *) addr);
862 }
863 return ret;
864 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
865 cmd |= NANDRW_JFFS2; /* skip bad blocks */
866 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
867 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
868 if (cmd & NANDRW_READ)
869 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
870 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000871#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100872 /* need ".e" same as ".j" for compatibility with older units */
873 else if (cmdtail && !strcmp (cmdtail, ".e"))
874 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000875#endif
stroesea842a6d2004-12-16 17:45:46 +0000876#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100877 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
878 /* ".i" for image -> read skips bad block (no 0xff) */
879 else if (cmdtail && !strcmp (cmdtail, ".i")) {
880 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
881 if (cmd & NANDRW_READ)
882 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
883 }
stroesea842a6d2004-12-16 17:45:46 +0000884#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100885 else if (cmdtail) {
886 printf ("Usage:\n%s\n", cmdtp->usage);
887 return 1;
888 }
889
890 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
891 (cmd & NANDRW_READ) ? "read" : "write",
892 curr_device, off, size);
893
894 ret = nand_legacy_rw (nand_dev_desc + curr_device,
895 cmd, off, size,
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200896 &total,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100897 (u_char *) addr);
898
899 printf (" %d bytes %s: %s\n", total,
900 (cmd & NANDRW_READ) ? "read" : "written",
901 ret ? "ERROR" : "OK");
902
903 return ret;
904 } else if (strcmp (argv[1], "erase") == 0 &&
905 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
906 int clean = argc == 5;
907 ulong off =
908 simple_strtoul (argv[2 + clean], NULL, 16);
909 ulong size =
910 simple_strtoul (argv[3 + clean], NULL, 16);
911 int ret;
912
913 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
914 curr_device, off, size);
915
916 ret = nand_legacy_erase (nand_dev_desc + curr_device,
917 off, size, clean);
918
919 printf ("%s\n", ret ? "ERROR" : "OK");
920
921 return ret;
922 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000923 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100924 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000925 }
wdenkdc7c9a12003-03-26 06:55:25 +0000926
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100927 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000928 }
wdenkdc7c9a12003-03-26 06:55:25 +0000929}
930
wdenk0d498392003-07-01 21:06:45 +0000931U_BOOT_CMD(
932 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200933 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000934 "info - show available NAND devices\n"
935 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000936 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000937 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
938 " at offset `off' to/from memory address `addr'\n"
939 "nand erase [clean] [off size] - erase `size' bytes from\n"
940 " offset `off' (entire device if not specified)\n"
941 "nand bad - show bad blocks\n"
942 "nand read.oob addr off size - read out-of-band data\n"
943 "nand write.oob addr off size - read out-of-band data\n"
944);
945
wdenkdc7c9a12003-03-26 06:55:25 +0000946int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
947{
948 char *boot_device = NULL;
949 char *ep;
950 int dev;
951 ulong cnt;
952 ulong addr;
953 ulong offset = 0;
954 image_header_t *hdr;
955 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100956#if defined(CONFIG_FIT)
957 const void *fit_hdr;
958#endif
959
Heiko Schocherfad63402007-07-13 09:54:17 +0200960 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000961 switch (argc) {
962 case 1:
963 addr = CFG_LOAD_ADDR;
964 boot_device = getenv ("bootdevice");
965 break;
966 case 2:
967 addr = simple_strtoul(argv[1], NULL, 16);
968 boot_device = getenv ("bootdevice");
969 break;
970 case 3:
971 addr = simple_strtoul(argv[1], NULL, 16);
972 boot_device = argv[2];
973 break;
974 case 4:
975 addr = simple_strtoul(argv[1], NULL, 16);
976 boot_device = argv[2];
977 offset = simple_strtoul(argv[3], NULL, 16);
978 break;
979 default:
980 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200981 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000982 return 1;
983 }
984
Heiko Schocherfad63402007-07-13 09:54:17 +0200985 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000986 if (!boot_device) {
987 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200988 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000989 return 1;
990 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200991 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000992
993 dev = simple_strtoul(boot_device, &ep, 16);
994
995 if ((dev >= CFG_MAX_NAND_DEVICE) ||
996 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
997 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200998 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +0000999 return 1;
1000 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001001 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +00001002
wdenk7a8e9bed2003-05-31 18:35:21 +00001003 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkdc7c9a12003-03-26 06:55:25 +00001004 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1005 offset);
1006
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001007 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1008 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001009 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001010 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001011 return 1;
1012 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001013 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001014
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001015 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001016 case IMAGE_FORMAT_LEGACY:
1017 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001018 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001019
Marian Balakowicz09475f72008-03-12 10:33:01 +01001020 cnt = image_get_image_size (hdr);
1021 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001022 break;
1023#if defined(CONFIG_FIT)
1024 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001025 fit_hdr = (const void *)addr;
1026 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001027 show_boot_progress (-150);
Marian Balakowicz09475f72008-03-12 10:33:01 +01001028 puts ("** Bad FIT image format\n");
1029 return 1;
1030 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001031 show_boot_progress (151);
Marian Balakowicz09475f72008-03-12 10:33:01 +01001032 puts ("Fit image detected...\n");
1033
1034 cnt = fit_get_size (fit_hdr);
1035 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001036#endif
1037 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001038 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001039 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001040 return 1;
1041 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001042 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001043
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001044 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1045 offset + SECTORSIZE, cnt, NULL,
1046 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001047 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001048 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001049 return 1;
1050 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001051 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001052
Marian Balakowicz09475f72008-03-12 10:33:01 +01001053#if defined(CONFIG_FIT)
1054 /* This cannot be done earlier, we need complete FIT image in RAM first */
1055 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
1056 fit_print_contents ((const void *)addr);
1057#endif
1058
wdenkdc7c9a12003-03-26 06:55:25 +00001059 /* Loading ok, update default load address */
1060
1061 load_addr = addr;
1062
1063 /* Check if we should attempt an auto-start */
1064 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1065 char *local_args[2];
1066 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1067
1068 local_args[0] = argv[0];
1069 local_args[1] = NULL;
1070
wdenk7a8e9bed2003-05-31 18:35:21 +00001071 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001072
1073 do_bootm (cmdtp, 0, 1, local_args);
1074 rcode = 1;
1075 }
1076 return rcode;
1077}
1078
wdenk0d498392003-07-01 21:06:45 +00001079U_BOOT_CMD(
1080 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +00001081 "nboot - boot from NAND device\n",
1082 "loadAddr dev\n"
1083);
1084
Jon Loeligerc76fe472007-07-08 18:02:23 -05001085#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001086
1087#endif /* CFG_NAND_LEGACY */