blob: 7b1f83046572d1a30d4a09dd232964117a2242f7 [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
96arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
97{
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) {
139 if (!(str2long(argv[1], size))) {
140 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;
161 ulong addr, off, size;
162 char *cmd, *s;
163 nand_info_t *nand;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200164#ifdef CFG_NAND_QUIET
165 int quiet = CFG_NAND_QUIET;
166#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200167 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200168#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200169 const char *quiet_str = getenv("quiet");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100170
171 /* at least two arguments please */
172 if (argc < 2)
173 goto usage;
174
Stefan Roese2255b2d2006-10-10 12:36:02 +0200175 if (quiet_str)
176 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
177
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100178 cmd = argv[1];
179
180 if (strcmp(cmd, "info") == 0) {
181
182 putc('\n');
183 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
184 if (nand_info[i].name)
185 printf("Device %d: %s, sector size %lu KiB\n",
186 i, nand_info[i].name,
187 nand_info[i].erasesize >> 10);
188 }
189 return 0;
190 }
191
192 if (strcmp(cmd, "device") == 0) {
193
194 if (argc < 3) {
195 if ((nand_curr_device < 0) ||
196 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
197 puts("\nno devices available\n");
198 else
199 printf("\nDevice %d: %s\n", nand_curr_device,
200 nand_info[nand_curr_device].name);
201 return 0;
202 }
203 dev = (int)simple_strtoul(argv[2], NULL, 10);
204 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
205 puts("No such device\n");
206 return 1;
207 }
208 printf("Device %d: %s", dev, nand_info[dev].name);
209 puts("... is now current device\n");
210 nand_curr_device = dev;
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200211
212#ifdef CFG_NAND_SELECT_DEVICE
213 /*
214 * Select the chip in the board/cpu specific driver
215 */
216 board_nand_select_device(nand_info[dev].priv, dev);
217#endif
218
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100219 return 0;
220 }
221
222 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
223 strncmp(cmd, "dump", 4) != 0 &&
Stefan Roese2255b2d2006-10-10 12:36:02 +0200224 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
225 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
226 strcmp(cmd, "biterr") != 0 &&
227 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100228 goto usage;
229
230 /* the following commands operate on the current device */
231 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
232 !nand_info[nand_curr_device].name) {
233 puts("\nno devices available\n");
234 return 1;
235 }
236 nand = &nand_info[nand_curr_device];
237
238 if (strcmp(cmd, "bad") == 0) {
239 printf("\nDevice %d bad blocks:\n", nand_curr_device);
240 for (off = 0; off < nand->size; off += nand->erasesize)
241 if (nand_block_isbad(nand, off))
242 printf(" %08x\n", off);
243 return 0;
244 }
245
Stefan Roese856f0542006-10-28 15:55:52 +0200246 /*
247 * Syntax is:
248 * 0 1 2 3 4
249 * nand erase [clean] [off size]
250 */
Stefan Roese2255b2d2006-10-10 12:36:02 +0200251 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
252 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200253 /* "clean" at index 2 means request to write cleanmarker */
254 int clean = argc > 2 && !strcmp("clean", argv[2]);
255 int o = clean ? 3 : 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200256 int scrub = !strcmp(cmd, "scrub");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100257
Stefan Roese856f0542006-10-28 15:55:52 +0200258 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
259 /* skip first two or three arguments, look for offset and size */
260 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
261 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200262
263 memset(&opts, 0, sizeof(opts));
264 opts.offset = off;
265 opts.length = size;
266 opts.jffs2 = clean;
267 opts.quiet = quiet;
268
269 if (scrub) {
Stefan Roese856f0542006-10-28 15:55:52 +0200270 puts("Warning: "
271 "scrub option will erase all factory set "
272 "bad blocks!\n"
273 " "
274 "There is no reliable way to recover them.\n"
275 " "
276 "Use this command only for testing purposes "
277 "if you\n"
278 " "
279 "are sure of what you are doing!\n"
280 "\nReally scrub this NAND flash? <y/N>\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200281
282 if (getc() == 'y' && getc() == '\r') {
283 opts.scrub = 1;
284 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200285 puts("scrub aborted\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200286 return -1;
287 }
288 }
289 ret = nand_erase_opts(nand, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100290 printf("%s\n", ret ? "ERROR" : "OK");
291
292 return ret == 0 ? 0 : 1;
293 }
294
295 if (strncmp(cmd, "dump", 4) == 0) {
296 if (argc < 3)
297 goto usage;
298
299 s = strchr(cmd, '.');
300 off = (int)simple_strtoul(argv[2], NULL, 16);
301
302 if (s != NULL && strcmp(s, ".oob") == 0)
303 ret = nand_dump_oob(nand, off);
304 else
305 ret = nand_dump(nand, off);
306
307 return ret == 0 ? 1 : 0;
308
309 }
310
311 /* read write */
312 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200313 int read;
314
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100315 if (argc < 4)
316 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200317
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100318 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
319
Stefan Roese2255b2d2006-10-10 12:36:02 +0200320 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200321 printf("\nNAND %s: ", read ? "read" : "write");
322 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
323 return 1;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100324
Stefan Roese2255b2d2006-10-10 12:36:02 +0200325 s = strchr(cmd, '.');
326 if (s != NULL &&
327 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
328 if (read) {
329 /* read */
330 nand_read_options_t opts;
331 memset(&opts, 0, sizeof(opts));
332 opts.buffer = (u_char*) addr;
333 opts.length = size;
334 opts.offset = off;
335 opts.quiet = quiet;
336 ret = nand_read_opts(nand, &opts);
337 } else {
338 /* write */
339 nand_write_options_t opts;
340 memset(&opts, 0, sizeof(opts));
341 opts.buffer = (u_char*) addr;
342 opts.length = size;
343 opts.offset = off;
344 /* opts.forcejffs2 = 1; */
345 opts.pad = 1;
346 opts.blockalign = 1;
347 opts.quiet = quiet;
348 ret = nand_write_opts(nand, &opts);
349 }
Harald Welte62d4f432007-12-19 14:12:53 +0100350 } else if (s != NULL && !strcmp(s, ".oob")) {
351 /* read out-of-band data */
352 if (read)
Jean-Christophe PLAGNIOL-VILLARD0a1e03b2008-01-13 12:36:12 +0100353 ret = nand->read_oob(nand, off, size, (size_t *) &size,
Harald Welte62d4f432007-12-19 14:12:53 +0100354 (u_char *) addr);
355 else
Jean-Christophe PLAGNIOL-VILLARD0a1e03b2008-01-13 12:36:12 +0100356 ret = nand->write_oob(nand, off, size, (size_t *) &size,
Harald Welte62d4f432007-12-19 14:12:53 +0100357 (u_char *) addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200358 } else {
359 if (read)
360 ret = nand_read(nand, off, &size, (u_char *)addr);
361 else
362 ret = nand_write(nand, off, &size, (u_char *)addr);
Stefan Roese2255b2d2006-10-10 12:36:02 +0200363 }
364
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100365 printf(" %d bytes %s: %s\n", size,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200366 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100367
368 return ret == 0 ? 0 : 1;
369 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200370
Stefan Roese2255b2d2006-10-10 12:36:02 +0200371 if (strcmp(cmd, "markbad") == 0) {
372 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
373
374 int ret = nand->block_markbad(nand, addr);
375 if (ret == 0) {
376 printf("block 0x%08lx successfully marked as bad\n",
377 (ulong) addr);
378 return 0;
379 } else {
380 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
381 (ulong) addr, ret);
382 }
383 return 1;
384 }
385 if (strcmp(cmd, "biterr") == 0) {
386 /* todo */
387 return 1;
388 }
389
390 if (strcmp(cmd, "lock") == 0) {
391 int tight = 0;
392 int status = 0;
393 if (argc == 3) {
394 if (!strcmp("tight", argv[2]))
395 tight = 1;
396 if (!strcmp("status", argv[2]))
397 status = 1;
398 }
399
400 if (status) {
401 ulong block_start = 0;
402 ulong off;
403 int last_status = -1;
404
405 struct nand_chip *nand_chip = nand->priv;
406 /* check the WP bit */
407 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
408 printf("device is %swrite protected\n",
409 (nand_chip->read_byte(nand) & 0x80 ?
410 "NOT " : "" ) );
411
412 for (off = 0; off < nand->size; off += nand->oobblock) {
413 int s = nand_get_lock_status(nand, off);
414
415 /* print message only if status has changed
416 * or at end of chip
417 */
418 if (off == nand->size - nand->oobblock
419 || (s != last_status && off != 0)) {
420
421 printf("%08x - %08x: %8d pages %s%s%s\n",
422 block_start,
423 off-1,
424 (off-block_start)/nand->oobblock,
425 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
426 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
427 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
428 }
429
430 last_status = s;
431 }
432 } else {
433 if (!nand_lock(nand, tight)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200434 puts("NAND flash successfully locked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200435 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200436 puts("Error locking NAND flash\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200437 return 1;
438 }
439 }
440 return 0;
441 }
442
443 if (strcmp(cmd, "unlock") == 0) {
Stefan Roese856f0542006-10-28 15:55:52 +0200444 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
445 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200446
447 if (!nand_unlock(nand, off, size)) {
Stefan Roese856f0542006-10-28 15:55:52 +0200448 puts("NAND flash successfully unlocked\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200449 } else {
Stefan Roese856f0542006-10-28 15:55:52 +0200450 puts("Error unlocking NAND flash, "
451 "write and erase will probably fail\n");
Stefan Roese2255b2d2006-10-10 12:36:02 +0200452 return 1;
453 }
454 return 0;
455 }
456
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100457usage:
458 printf("Usage:\n%s\n", cmdtp->usage);
459 return 1;
460}
461
462U_BOOT_CMD(nand, 5, 1, do_nand,
463 "nand - NAND sub-system\n",
464 "info - show available NAND devices\n"
465 "nand device [dev] - show or set current device\n"
Stefan Roese856f0542006-10-28 15:55:52 +0200466 "nand read[.jffs2] - addr off|partition size\n"
Matthias Fuchs5bd7fe92007-09-11 17:04:00 +0200467 "nand write[.jffs2] - addr off|partition size - read/write `size' bytes starting\n"
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100468 " at offset `off' to/from memory address `addr'\n"
469 "nand erase [clean] [off size] - erase `size' bytes from\n"
470 " offset `off' (entire device if not specified)\n"
471 "nand bad - show bad blocks\n"
472 "nand dump[.oob] off - dump page\n"
473 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
474 "nand markbad off - mark bad block at offset (UNSAFE)\n"
Stefan Roese2255b2d2006-10-10 12:36:02 +0200475 "nand biterr off - make a bit error at offset (UNSAFE)\n"
476 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
477 "nand unlock [offset] [size] - unlock section\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100478
Stefan Roese856f0542006-10-28 15:55:52 +0200479static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
480 ulong offset, ulong addr, char *cmd)
481{
482 int r;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200483 char *ep, *s;
Stefan Roese856f0542006-10-28 15:55:52 +0200484 ulong cnt;
485 image_header_t *hdr;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200486 int jffs2 = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100487#if defined(CONFIG_FIT)
488 const void *fit_hdr;
489#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200490
491 s = strchr(cmd, '.');
492 if (s != NULL &&
493 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
494 jffs2 = 1;
Stefan Roese856f0542006-10-28 15:55:52 +0200495
496 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
497
498 cnt = nand->oobblock;
Thomas Knobloch10e03892007-07-06 14:58:39 +0200499 if (jffs2) {
500 nand_read_options_t opts;
501 memset(&opts, 0, sizeof(opts));
502 opts.buffer = (u_char*) addr;
503 opts.length = cnt;
504 opts.offset = offset;
505 opts.quiet = 1;
506 r = nand_read_opts(nand, &opts);
507 } else {
508 r = nand_read(nand, offset, &cnt, (u_char *) addr);
509 }
510
Stefan Roese856f0542006-10-28 15:55:52 +0200511 if (r) {
512 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200513 show_boot_progress (-56);
Stefan Roese856f0542006-10-28 15:55:52 +0200514 return 1;
515 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200516 show_boot_progress (56);
Stefan Roese856f0542006-10-28 15:55:52 +0200517
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100518 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100519 case IMAGE_FORMAT_LEGACY:
520 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200521
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100522 show_boot_progress (57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100523 image_print_contents (hdr);
524
525 cnt = image_get_image_size (hdr);
526 break;
527#if defined(CONFIG_FIT)
528 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100529 fit_hdr = (const void *)addr;
530 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100531 show_boot_progress (-150);
Marian Balakowicz09475f72008-03-12 10:33:01 +0100532 puts ("** Bad FIT image format\n");
533 return 1;
534 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100535 show_boot_progress (151);
Marian Balakowicz09475f72008-03-12 10:33:01 +0100536 puts ("Fit image detected...\n");
537
538 cnt = fit_get_size (fit_hdr);
539 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100540#endif
541 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100542 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100543 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200544 return 1;
545 }
546
Thomas Knobloch10e03892007-07-06 14:58:39 +0200547 if (jffs2) {
548 nand_read_options_t opts;
549 memset(&opts, 0, sizeof(opts));
550 opts.buffer = (u_char*) addr;
551 opts.length = cnt;
552 opts.offset = offset;
553 opts.quiet = 1;
554 r = nand_read_opts(nand, &opts);
555 } else {
556 r = nand_read(nand, offset, &cnt, (u_char *) addr);
557 }
Stefan Roese856f0542006-10-28 15:55:52 +0200558
Stefan Roese856f0542006-10-28 15:55:52 +0200559 if (r) {
560 puts("** Read error\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200561 show_boot_progress (-58);
Stefan Roese856f0542006-10-28 15:55:52 +0200562 return 1;
563 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200564 show_boot_progress (58);
Stefan Roese856f0542006-10-28 15:55:52 +0200565
Marian Balakowicz09475f72008-03-12 10:33:01 +0100566#if defined(CONFIG_FIT)
567 /* This cannot be done earlier, we need complete FIT image in RAM first */
568 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
569 fit_print_contents ((const void *)addr);
570#endif
571
Stefan Roese856f0542006-10-28 15:55:52 +0200572 /* Loading ok, update default load address */
573
574 load_addr = addr;
575
576 /* Check if we should attempt an auto-start */
577 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
578 char *local_args[2];
579 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
580
581 local_args[0] = cmd;
582 local_args[1] = NULL;
583
584 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
585
586 do_bootm(cmdtp, 0, 1, local_args);
587 return 1;
588 }
589 return 0;
590}
591
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100592int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
593{
594 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200595 int idx;
596 ulong addr, offset = 0;
Jon Loeligerc76fe472007-07-08 18:02:23 -0500597#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200598 struct mtd_device *dev;
599 struct part_info *part;
600 u8 pnum;
601
602 if (argc >= 2) {
603 char *p = (argc == 2) ? argv[1] : argv[2];
604 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
605 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
606 if (dev->id->type != MTD_DEV_TYPE_NAND) {
607 puts("Not a NAND device\n");
608 return 1;
609 }
610 if (argc > 3)
611 goto usage;
612 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200613 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200614 else
615 addr = CFG_LOAD_ADDR;
616 return nand_load_image(cmdtp, &nand_info[dev->id->num],
617 part->offset, addr, argv[0]);
618 }
619 }
620#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100621
Heiko Schocherfad63402007-07-13 09:54:17 +0200622 show_boot_progress(52);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100623 switch (argc) {
624 case 1:
625 addr = CFG_LOAD_ADDR;
626 boot_device = getenv("bootdevice");
627 break;
628 case 2:
629 addr = simple_strtoul(argv[1], NULL, 16);
630 boot_device = getenv("bootdevice");
631 break;
632 case 3:
633 addr = simple_strtoul(argv[1], NULL, 16);
634 boot_device = argv[2];
635 break;
636 case 4:
637 addr = simple_strtoul(argv[1], NULL, 16);
638 boot_device = argv[2];
639 offset = simple_strtoul(argv[3], NULL, 16);
640 break;
641 default:
Jon Loeligerc76fe472007-07-08 18:02:23 -0500642#if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
Stefan Roese856f0542006-10-28 15:55:52 +0200643usage:
644#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100645 printf("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200646 show_boot_progress(-53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100647 return 1;
648 }
649
Heiko Schocherfad63402007-07-13 09:54:17 +0200650 show_boot_progress(53);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100651 if (!boot_device) {
652 puts("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200653 show_boot_progress(-54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100654 return 1;
655 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200656 show_boot_progress(54);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657
Stefan Roese856f0542006-10-28 15:55:52 +0200658 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100659
Stefan Roese856f0542006-10-28 15:55:52 +0200660 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
661 printf("\n** Device %d not available\n", idx);
Heiko Schocherfad63402007-07-13 09:54:17 +0200662 show_boot_progress(-55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100663 return 1;
664 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200665 show_boot_progress(55);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100666
Stefan Roese856f0542006-10-28 15:55:52 +0200667 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100668}
669
670U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Stefan Roese856f0542006-10-28 15:55:52 +0200671 "nboot - boot from NAND device\n",
Thomas Knobloch10e03892007-07-06 14:58:39 +0200672 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100673
Jon Loeligerc76fe472007-07-08 18:02:23 -0500674#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100675
676#else /* CFG_NAND_LEGACY */
677/*
678 *
679 * Legacy NAND support - to be phased out
680 *
681 */
wdenkdc7c9a12003-03-26 06:55:25 +0000682#include <command.h>
683#include <malloc.h>
684#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +0000685#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +0000686
Heiko Schocherfad63402007-07-13 09:54:17 +0200687#ifdef CONFIG_show_boot_progress
wdenkdc7c9a12003-03-26 06:55:25 +0000688# include <status_led.h>
Heiko Schocherfad63402007-07-13 09:54:17 +0200689# define show_boot_progress(arg) show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000690#else
Heiko Schocherfad63402007-07-13 09:54:17 +0200691# define show_boot_progress(arg)
wdenkdc7c9a12003-03-26 06:55:25 +0000692#endif
693
Jon Loeligerc76fe472007-07-08 18:02:23 -0500694#if defined(CONFIG_CMD_NAND)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100695#include <linux/mtd/nand_legacy.h>
696#if 0
wdenkdc7c9a12003-03-26 06:55:25 +0000697#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +0000698#include <jffs2/jffs2.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100699#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000700
wdenk1f4bb372003-07-27 00:21:01 +0000701#ifdef CONFIG_OMAP1510
702void archflashwp(void *archdata, int wp);
703#endif
704
705#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
706
wdenka43278a2003-09-11 19:48:06 +0000707#undef NAND_DEBUG
wdenkdc7c9a12003-03-26 06:55:25 +0000708#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +0000709
710/* ****************** WARNING *********************
711 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
712 * erase (or at least attempt to erase) blocks that are marked
713 * bad. This can be very handy if you are _sure_ that the block
714 * is OK, say because you marked a good block bad to test bad
715 * block handling and you are done testing, or if you have
716 * accidentally marked blocks bad.
717 *
718 * Erasing factory marked bad blocks is a _bad_ idea. If the
719 * erase succeeds there is no reliable way to find them again,
720 * and attempting to program or erase bad blocks can affect
721 * the data in _other_ (good) blocks.
722 */
723#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +0000724
725#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +0000726#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +0000727
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100728/* bits for nand_legacy_rw() `cmd'; or together as needed */
wdenk7a8e9bed2003-05-31 18:35:21 +0000729#define NANDRW_READ 0x01
730#define NANDRW_WRITE 0x00
731#define NANDRW_JFFS2 0x02
wdenka3d991b2004-04-15 21:48:45 +0000732#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +0000733
wdenkdc7c9a12003-03-26 06:55:25 +0000734/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100735 * Imports from nand_legacy.c
wdenkdc7c9a12003-03-26 06:55:25 +0000736 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100737extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
738extern int curr_device;
739extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
740 size_t len, int clean);
741extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
742 size_t len, size_t *retlen, u_char *buf);
743extern void nand_print(struct nand_chip *nand);
744extern void nand_print_bad(struct nand_chip *nand);
745extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
746 size_t len, size_t *retlen, u_char *buf);
747extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
748 size_t len, size_t *retlen, const u_char *buf);
wdenkdc7c9a12003-03-26 06:55:25 +0000749
wdenkdc7c9a12003-03-26 06:55:25 +0000750
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100751int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
wdenkdc7c9a12003-03-26 06:55:25 +0000752{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100753 int rcode = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000754
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100755 switch (argc) {
756 case 0:
757 case 1:
758 printf ("Usage:\n%s\n", cmdtp->usage);
759 return 1;
760 case 2:
761 if (strcmp (argv[1], "info") == 0) {
762 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000763
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100764 putc ('\n');
wdenkdc7c9a12003-03-26 06:55:25 +0000765
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100766 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
767 if (nand_dev_desc[i].ChipID ==
768 NAND_ChipID_UNKNOWN)
769 continue; /* list only known devices */
770 printf ("Device %d: ", i);
771 nand_print (&nand_dev_desc[i]);
wdenk7a8e9bed2003-05-31 18:35:21 +0000772 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100773 return 0;
774
775 } else if (strcmp (argv[1], "device") == 0) {
776 if ((curr_device < 0)
777 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
778 puts ("\nno devices available\n");
779 return 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000780 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100781 printf ("\nDevice %d: ", curr_device);
782 nand_print (&nand_dev_desc[curr_device]);
783 return 0;
784
785 } else if (strcmp (argv[1], "bad") == 0) {
786 if ((curr_device < 0)
787 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
788 puts ("\nno devices available\n");
789 return 1;
790 }
791 printf ("\nDevice %d bad blocks:\n", curr_device);
792 nand_print_bad (&nand_dev_desc[curr_device]);
793 return 0;
794
795 }
796 printf ("Usage:\n%s\n", cmdtp->usage);
797 return 1;
798 case 3:
799 if (strcmp (argv[1], "device") == 0) {
800 int dev = (int) simple_strtoul (argv[2], NULL, 10);
801
802 printf ("\nDevice %d: ", dev);
803 if (dev >= CFG_MAX_NAND_DEVICE) {
804 puts ("unknown device\n");
805 return 1;
806 }
807 nand_print (&nand_dev_desc[dev]);
808 /*nand_print (dev); */
809
810 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
811 return 1;
812 }
813
814 curr_device = dev;
815
816 puts ("... is now current device\n");
817
818 return 0;
819 } else if (strcmp (argv[1], "erase") == 0
820 && strcmp (argv[2], "clean") == 0) {
821 struct nand_chip *nand = &nand_dev_desc[curr_device];
822 ulong off = 0;
823 ulong size = nand->totlen;
824 int ret;
825
826 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
827
828 ret = nand_legacy_erase (nand, off, size, 1);
829
830 printf ("%s\n", ret ? "ERROR" : "OK");
831
wdenk7a8e9bed2003-05-31 18:35:21 +0000832 return ret;
833 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100834
835 printf ("Usage:\n%s\n", cmdtp->usage);
836 return 1;
837 default:
838 /* at least 4 args */
839
840 if (strncmp (argv[1], "read", 4) == 0 ||
841 strncmp (argv[1], "write", 5) == 0) {
842 ulong addr = simple_strtoul (argv[2], NULL, 16);
843 ulong off = simple_strtoul (argv[3], NULL, 16);
844 ulong size = simple_strtoul (argv[4], NULL, 16);
845 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
846 NANDRW_READ : NANDRW_WRITE;
847 int ret, total;
848 char *cmdtail = strchr (argv[1], '.');
849
850 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
851 /* read out-of-band data */
852 if (cmd & NANDRW_READ) {
853 ret = nand_read_oob (nand_dev_desc + curr_device,
854 off, size, (size_t *) & total,
855 (u_char *) addr);
856 } else {
857 ret = nand_write_oob (nand_dev_desc + curr_device,
858 off, size, (size_t *) & total,
859 (u_char *) addr);
860 }
861 return ret;
862 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
863 cmd |= NANDRW_JFFS2; /* skip bad blocks */
864 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
865 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
866 if (cmd & NANDRW_READ)
867 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
868 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000869#ifdef SXNI855T
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100870 /* need ".e" same as ".j" for compatibility with older units */
871 else if (cmdtail && !strcmp (cmdtail, ".e"))
872 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenk7a8e9bed2003-05-31 18:35:21 +0000873#endif
stroesea842a6d2004-12-16 17:45:46 +0000874#ifdef CFG_NAND_SKIP_BAD_DOT_I
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100875 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
876 /* ".i" for image -> read skips bad block (no 0xff) */
877 else if (cmdtail && !strcmp (cmdtail, ".i")) {
878 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
879 if (cmd & NANDRW_READ)
880 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
881 }
stroesea842a6d2004-12-16 17:45:46 +0000882#endif /* CFG_NAND_SKIP_BAD_DOT_I */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100883 else if (cmdtail) {
884 printf ("Usage:\n%s\n", cmdtp->usage);
885 return 1;
886 }
887
888 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
889 (cmd & NANDRW_READ) ? "read" : "write",
890 curr_device, off, size);
891
892 ret = nand_legacy_rw (nand_dev_desc + curr_device,
893 cmd, off, size,
894 (size_t *) & total,
895 (u_char *) addr);
896
897 printf (" %d bytes %s: %s\n", total,
898 (cmd & NANDRW_READ) ? "read" : "written",
899 ret ? "ERROR" : "OK");
900
901 return ret;
902 } else if (strcmp (argv[1], "erase") == 0 &&
903 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
904 int clean = argc == 5;
905 ulong off =
906 simple_strtoul (argv[2 + clean], NULL, 16);
907 ulong size =
908 simple_strtoul (argv[3 + clean], NULL, 16);
909 int ret;
910
911 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
912 curr_device, off, size);
913
914 ret = nand_legacy_erase (nand_dev_desc + curr_device,
915 off, size, clean);
916
917 printf ("%s\n", ret ? "ERROR" : "OK");
918
919 return ret;
920 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000921 printf ("Usage:\n%s\n", cmdtp->usage);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100922 rcode = 1;
wdenk7a8e9bed2003-05-31 18:35:21 +0000923 }
wdenkdc7c9a12003-03-26 06:55:25 +0000924
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100925 return rcode;
wdenkdc7c9a12003-03-26 06:55:25 +0000926 }
wdenkdc7c9a12003-03-26 06:55:25 +0000927}
928
wdenk0d498392003-07-01 21:06:45 +0000929U_BOOT_CMD(
930 nand, 5, 1, do_nand,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200931 "nand - legacy NAND sub-system\n",
wdenkb0fce992003-06-29 21:03:46 +0000932 "info - show available NAND devices\n"
933 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000934 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000935 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
936 " at offset `off' to/from memory address `addr'\n"
937 "nand erase [clean] [off size] - erase `size' bytes from\n"
938 " offset `off' (entire device if not specified)\n"
939 "nand bad - show bad blocks\n"
940 "nand read.oob addr off size - read out-of-band data\n"
941 "nand write.oob addr off size - read out-of-band data\n"
942);
943
wdenkdc7c9a12003-03-26 06:55:25 +0000944int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
945{
946 char *boot_device = NULL;
947 char *ep;
948 int dev;
949 ulong cnt;
950 ulong addr;
951 ulong offset = 0;
952 image_header_t *hdr;
953 int rcode = 0;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100954#if defined(CONFIG_FIT)
955 const void *fit_hdr;
956#endif
957
Heiko Schocherfad63402007-07-13 09:54:17 +0200958 show_boot_progress (52);
wdenkdc7c9a12003-03-26 06:55:25 +0000959 switch (argc) {
960 case 1:
961 addr = CFG_LOAD_ADDR;
962 boot_device = getenv ("bootdevice");
963 break;
964 case 2:
965 addr = simple_strtoul(argv[1], NULL, 16);
966 boot_device = getenv ("bootdevice");
967 break;
968 case 3:
969 addr = simple_strtoul(argv[1], NULL, 16);
970 boot_device = argv[2];
971 break;
972 case 4:
973 addr = simple_strtoul(argv[1], NULL, 16);
974 boot_device = argv[2];
975 offset = simple_strtoul(argv[3], NULL, 16);
976 break;
977 default:
978 printf ("Usage:\n%s\n", cmdtp->usage);
Heiko Schocherfad63402007-07-13 09:54:17 +0200979 show_boot_progress (-53);
wdenkdc7c9a12003-03-26 06:55:25 +0000980 return 1;
981 }
982
Heiko Schocherfad63402007-07-13 09:54:17 +0200983 show_boot_progress (53);
wdenkdc7c9a12003-03-26 06:55:25 +0000984 if (!boot_device) {
985 puts ("\n** No boot device **\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200986 show_boot_progress (-54);
wdenkdc7c9a12003-03-26 06:55:25 +0000987 return 1;
988 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200989 show_boot_progress (54);
wdenkdc7c9a12003-03-26 06:55:25 +0000990
991 dev = simple_strtoul(boot_device, &ep, 16);
992
993 if ((dev >= CFG_MAX_NAND_DEVICE) ||
994 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
995 printf ("\n** Device %d not available\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +0200996 show_boot_progress (-55);
wdenkdc7c9a12003-03-26 06:55:25 +0000997 return 1;
998 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200999 show_boot_progress (55);
wdenkdc7c9a12003-03-26 06:55:25 +00001000
wdenk7a8e9bed2003-05-31 18:35:21 +00001001 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkdc7c9a12003-03-26 06:55:25 +00001002 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1003 offset);
1004
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001005 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1006 SECTORSIZE, NULL, (u_char *)addr)) {
wdenkdc7c9a12003-03-26 06:55:25 +00001007 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001008 show_boot_progress (-56);
wdenkdc7c9a12003-03-26 06:55:25 +00001009 return 1;
1010 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001011 show_boot_progress (56);
wdenkdc7c9a12003-03-26 06:55:25 +00001012
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001013 switch (genimg_get_format ((void *)addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001014 case IMAGE_FORMAT_LEGACY:
1015 hdr = (image_header_t *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +01001016 image_print_contents (hdr);
wdenkdc7c9a12003-03-26 06:55:25 +00001017
Marian Balakowicz09475f72008-03-12 10:33:01 +01001018 cnt = image_get_image_size (hdr);
1019 cnt -= SECTORSIZE;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001020 break;
1021#if defined(CONFIG_FIT)
1022 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001023 fit_hdr = (const void *)addr;
1024 if (!fit_check_format (fit_hdr)) {
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001025 show_boot_progress (-150);
Marian Balakowicz09475f72008-03-12 10:33:01 +01001026 puts ("** Bad FIT image format\n");
1027 return 1;
1028 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001029 show_boot_progress (151);
Marian Balakowicz09475f72008-03-12 10:33:01 +01001030 puts ("Fit image detected...\n");
1031
1032 cnt = fit_get_size (fit_hdr);
1033 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001034#endif
1035 default:
Marian Balakowicz09475f72008-03-12 10:33:01 +01001036 show_boot_progress (-57);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001037 puts ("** Unknown image type\n");
wdenkdc7c9a12003-03-26 06:55:25 +00001038 return 1;
1039 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001040 show_boot_progress (57);
wdenkdc7c9a12003-03-26 06:55:25 +00001041
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001042 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1043 offset + SECTORSIZE, cnt, NULL,
1044 (u_char *)(addr+SECTORSIZE))) {
wdenkdc7c9a12003-03-26 06:55:25 +00001045 printf ("** Read error on %d\n", dev);
Heiko Schocherfad63402007-07-13 09:54:17 +02001046 show_boot_progress (-58);
wdenkdc7c9a12003-03-26 06:55:25 +00001047 return 1;
1048 }
Heiko Schocherfad63402007-07-13 09:54:17 +02001049 show_boot_progress (58);
wdenkdc7c9a12003-03-26 06:55:25 +00001050
Marian Balakowicz09475f72008-03-12 10:33:01 +01001051#if defined(CONFIG_FIT)
1052 /* This cannot be done earlier, we need complete FIT image in RAM first */
1053 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
1054 fit_print_contents ((const void *)addr);
1055#endif
1056
wdenkdc7c9a12003-03-26 06:55:25 +00001057 /* Loading ok, update default load address */
1058
1059 load_addr = addr;
1060
1061 /* Check if we should attempt an auto-start */
1062 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1063 char *local_args[2];
1064 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1065
1066 local_args[0] = argv[0];
1067 local_args[1] = NULL;
1068
wdenk7a8e9bed2003-05-31 18:35:21 +00001069 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +00001070
1071 do_bootm (cmdtp, 0, 1, local_args);
1072 rcode = 1;
1073 }
1074 return rcode;
1075}
1076
wdenk0d498392003-07-01 21:06:45 +00001077U_BOOT_CMD(
1078 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +00001079 "nboot - boot from NAND device\n",
1080 "loadAddr dev\n"
1081);
1082
Jon Loeligerc76fe472007-07-08 18:02:23 -05001083#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001084
1085#endif /* CFG_NAND_LEGACY */