wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 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 <dwmw2@infradead.org> |
wdenk | 6d3c6d1 | 2005-04-03 22:35:21 +0000 | [diff] [blame] | 6 | * |
| 7 | * Added 16-bit nand support |
| 8 | * (C) 2004 Texas Instruments |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 12 | #include <linux/mtd/mtd.h> |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 13 | #include <command.h> |
| 14 | #include <watchdog.h> |
| 15 | #include <malloc.h> |
| 16 | #include <asm/byteorder.h> |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 17 | #include <jffs2/jffs2.h> |
| 18 | #include <nand.h> |
| 19 | |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 20 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 21 | |
| 22 | /* parition handling routines */ |
| 23 | int mtdparts_init(void); |
| 24 | int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num); |
| 25 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 26 | u8 *part_num, struct part_info **part); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 27 | #endif |
| 28 | |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 29 | static int nand_dump(nand_info_t *nand, ulong off, int only_oob) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 30 | { |
| 31 | int i; |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 32 | u_char *datbuf, *oobbuf, *p; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 33 | |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 34 | datbuf = malloc(nand->writesize + nand->oobsize); |
| 35 | oobbuf = malloc(nand->oobsize); |
| 36 | if (!datbuf || !oobbuf) { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 37 | puts("No memory for page buffer\n"); |
| 38 | return 1; |
| 39 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 40 | off &= ~(nand->writesize - 1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 41 | loff_t addr = (loff_t) off; |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 42 | struct mtd_oob_ops ops; |
| 43 | memset(&ops, 0, sizeof(ops)); |
| 44 | ops.datbuf = datbuf; |
| 45 | ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */ |
| 46 | ops.len = nand->writesize; |
| 47 | ops.ooblen = nand->oobsize; |
| 48 | ops.mode = MTD_OOB_RAW; |
| 49 | i = nand->read_oob(nand, addr, &ops); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 50 | if (i < 0) { |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 51 | printf("Error (%d) reading page %08lx\n", i, off); |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 52 | free(datbuf); |
| 53 | free(oobbuf); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 54 | return 1; |
| 55 | } |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 56 | printf("Page %08lx dump:\n", off); |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 57 | i = nand->writesize >> 4; |
| 58 | p = datbuf; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 59 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 60 | while (i--) { |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 61 | if (!only_oob) |
| 62 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" |
| 63 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 64 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 65 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
| 66 | p[15]); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 67 | p += 16; |
| 68 | } |
| 69 | puts("OOB:\n"); |
| 70 | i = nand->oobsize >> 3; |
| 71 | while (i--) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 72 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 73 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 74 | p += 8; |
| 75 | } |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 76 | free(datbuf); |
| 77 | free(oobbuf); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* ------------------------------------------------------------------------- */ |
| 83 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 84 | static inline int str2long(char *p, ulong *num) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 85 | { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 86 | char *endptr; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 87 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 88 | *num = simple_strtoul(p, &endptr, 16); |
| 89 | return (*p != '\0' && *endptr == '\0') ? 1 : 0; |
| 90 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 91 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 92 | static int |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 93 | arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 94 | { |
| 95 | int idx = nand_curr_device; |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 96 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 97 | struct mtd_device *dev; |
| 98 | struct part_info *part; |
| 99 | u8 pnum; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 100 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 101 | if (argc >= 1 && !(str2long(argv[0], off))) { |
| 102 | if ((mtdparts_init() == 0) && |
| 103 | (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) { |
| 104 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 105 | puts("not a NAND device\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | *off = part->offset; |
| 109 | if (argc >= 2) { |
Wolfgang Denk | 7f79454 | 2008-04-30 17:46:26 +0200 | [diff] [blame] | 110 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 111 | printf("'%s' is not a number\n", argv[1]); |
| 112 | return -1; |
| 113 | } |
| 114 | if (*size > part->size) |
| 115 | *size = part->size; |
| 116 | } else { |
| 117 | *size = part->size; |
| 118 | } |
| 119 | idx = dev->id->num; |
| 120 | *nand = nand_info[idx]; |
| 121 | goto out; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 122 | } |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 123 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 124 | #endif |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 125 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 126 | if (argc >= 1) { |
| 127 | if (!(str2long(argv[0], off))) { |
| 128 | printf("'%s' is not a number\n", argv[0]); |
| 129 | return -1; |
| 130 | } |
| 131 | } else { |
| 132 | *off = 0; |
| 133 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 134 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 135 | if (argc >= 2) { |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 136 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 137 | printf("'%s' is not a number\n", argv[1]); |
| 138 | return -1; |
| 139 | } |
| 140 | } else { |
| 141 | *size = nand->size - *off; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 144 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 145 | out: |
| 146 | #endif |
| 147 | printf("device %d ", idx); |
| 148 | if (*size == nand->size) |
| 149 | puts("whole chip\n"); |
| 150 | else |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 151 | printf("offset 0x%lx, size 0x%zx\n", *off, *size); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 152 | return 0; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 155 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
| 156 | static void print_status(ulong start, ulong end, ulong erasesize, int status) |
| 157 | { |
| 158 | printf("%08lx - %08lx: %08lx blocks %s%s%s\n", |
| 159 | start, |
| 160 | end - 1, |
| 161 | (end - start) / erasesize, |
| 162 | ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""), |
| 163 | ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""), |
| 164 | ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); |
| 165 | } |
| 166 | |
| 167 | static void do_nand_status(nand_info_t *nand) |
| 168 | { |
| 169 | ulong block_start = 0; |
| 170 | ulong off; |
| 171 | int last_status = -1; |
| 172 | |
| 173 | struct nand_chip *nand_chip = nand->priv; |
| 174 | /* check the WP bit */ |
| 175 | nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1); |
| 176 | printf("device is %swrite protected\n", |
| 177 | (nand_chip->read_byte(nand) & 0x80 ? |
| 178 | "NOT " : "")); |
| 179 | |
| 180 | for (off = 0; off < nand->size; off += nand->erasesize) { |
| 181 | int s = nand_get_lock_status(nand, off); |
| 182 | |
| 183 | /* print message only if status has changed */ |
| 184 | if (s != last_status && off != 0) { |
| 185 | print_status(block_start, off, nand->erasesize, |
| 186 | last_status); |
| 187 | block_start = off; |
| 188 | } |
| 189 | last_status = s; |
| 190 | } |
| 191 | /* Print the last block info */ |
| 192 | print_status(block_start, off, nand->erasesize, last_status); |
| 193 | } |
| 194 | #endif |
| 195 | |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 196 | static void nand_print_info(int idx) |
| 197 | { |
| 198 | nand_info_t *nand = &nand_info[idx]; |
| 199 | struct nand_chip *chip = nand->priv; |
| 200 | printf("Device %d: ", idx); |
| 201 | if (chip->numchips > 1) |
| 202 | printf("%dx ", chip->numchips); |
| 203 | printf("%s, sector size %u KiB\n", |
| 204 | nand->name, nand->erasesize >> 10); |
| 205 | } |
| 206 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 207 | int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 208 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 209 | int i, dev, ret = 0; |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 210 | ulong addr, off; |
| 211 | size_t size; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 212 | char *cmd, *s; |
| 213 | nand_info_t *nand; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_SYS_NAND_QUIET |
| 215 | int quiet = CONFIG_SYS_NAND_QUIET; |
Matthias Fuchs | d42fff2 | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 216 | #else |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 217 | int quiet = 0; |
Matthias Fuchs | d42fff2 | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 218 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 219 | const char *quiet_str = getenv("quiet"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 220 | |
| 221 | /* at least two arguments please */ |
| 222 | if (argc < 2) |
| 223 | goto usage; |
| 224 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 225 | if (quiet_str) |
| 226 | quiet = simple_strtoul(quiet_str, NULL, 0) != 0; |
| 227 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 228 | cmd = argv[1]; |
| 229 | |
| 230 | if (strcmp(cmd, "info") == 0) { |
| 231 | |
| 232 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 233 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 234 | if (nand_info[i].name) |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 235 | nand_print_info(i); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | if (strcmp(cmd, "device") == 0) { |
| 241 | |
| 242 | if (argc < 3) { |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 243 | putc('\n'); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 244 | if ((nand_curr_device < 0) || |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 245 | (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 246 | puts("no devices available\n"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 247 | else |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 248 | nand_print_info(nand_curr_device); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 252 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 253 | puts("No such device\n"); |
| 254 | return 1; |
| 255 | } |
| 256 | printf("Device %d: %s", dev, nand_info[dev].name); |
| 257 | puts("... is now current device\n"); |
| 258 | nand_curr_device = dev; |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 259 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 260 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 261 | /* |
| 262 | * Select the chip in the board/cpu specific driver |
| 263 | */ |
| 264 | board_nand_select_device(nand_info[dev].priv, dev); |
| 265 | #endif |
| 266 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 && |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 271 | strncmp(cmd, "dump", 4) != 0 && |
| 272 | strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 && |
| 273 | strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 && |
| 274 | strcmp(cmd, "biterr") != 0 && |
| 275 | strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 ) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 276 | goto usage; |
| 277 | |
| 278 | /* the following commands operate on the current device */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 279 | if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 280 | !nand_info[nand_curr_device].name) { |
| 281 | puts("\nno devices available\n"); |
| 282 | return 1; |
| 283 | } |
| 284 | nand = &nand_info[nand_curr_device]; |
| 285 | |
| 286 | if (strcmp(cmd, "bad") == 0) { |
| 287 | printf("\nDevice %d bad blocks:\n", nand_curr_device); |
| 288 | for (off = 0; off < nand->size; off += nand->erasesize) |
| 289 | if (nand_block_isbad(nand, off)) |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 290 | printf(" %08lx\n", off); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 294 | /* |
| 295 | * Syntax is: |
| 296 | * 0 1 2 3 4 |
| 297 | * nand erase [clean] [off size] |
| 298 | */ |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 299 | if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) { |
| 300 | nand_erase_options_t opts; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 301 | /* "clean" at index 2 means request to write cleanmarker */ |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 302 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 303 | int o = clean ? 3 : 2; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 304 | int scrub = !strcmp(cmd, "scrub"); |
| 305 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 306 | printf("\nNAND %s: ", scrub ? "scrub" : "erase"); |
| 307 | /* skip first two or three arguments, look for offset and size */ |
| 308 | if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0) |
| 309 | return 1; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 310 | |
| 311 | memset(&opts, 0, sizeof(opts)); |
| 312 | opts.offset = off; |
| 313 | opts.length = size; |
| 314 | opts.jffs2 = clean; |
| 315 | opts.quiet = quiet; |
| 316 | |
| 317 | if (scrub) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 318 | puts("Warning: " |
| 319 | "scrub option will erase all factory set " |
| 320 | "bad blocks!\n" |
| 321 | " " |
| 322 | "There is no reliable way to recover them.\n" |
| 323 | " " |
| 324 | "Use this command only for testing purposes " |
| 325 | "if you\n" |
| 326 | " " |
| 327 | "are sure of what you are doing!\n" |
| 328 | "\nReally scrub this NAND flash? <y/N>\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 329 | |
| 330 | if (getc() == 'y' && getc() == '\r') { |
| 331 | opts.scrub = 1; |
| 332 | } else { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 333 | puts("scrub aborted\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 334 | return -1; |
| 335 | } |
| 336 | } |
| 337 | ret = nand_erase_opts(nand, &opts); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 338 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 339 | |
| 340 | return ret == 0 ? 0 : 1; |
| 341 | } |
| 342 | |
| 343 | if (strncmp(cmd, "dump", 4) == 0) { |
| 344 | if (argc < 3) |
| 345 | goto usage; |
| 346 | |
| 347 | s = strchr(cmd, '.'); |
| 348 | off = (int)simple_strtoul(argv[2], NULL, 16); |
| 349 | |
| 350 | if (s != NULL && strcmp(s, ".oob") == 0) |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 351 | ret = nand_dump(nand, off, 1); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 352 | else |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 353 | ret = nand_dump(nand, off, 0); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 354 | |
| 355 | return ret == 0 ? 1 : 0; |
| 356 | |
| 357 | } |
| 358 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 359 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 360 | int read; |
| 361 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 362 | if (argc < 4) |
| 363 | goto usage; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 364 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 365 | addr = (ulong)simple_strtoul(argv[2], NULL, 16); |
| 366 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 367 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 368 | printf("\nNAND %s: ", read ? "read" : "write"); |
| 369 | if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0) |
| 370 | return 1; |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 371 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 372 | s = strchr(cmd, '.'); |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 373 | if (!s || !strcmp(s, ".jffs2") || |
| 374 | !strcmp(s, ".e") || !strcmp(s, ".i")) { |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 375 | if (read) |
| 376 | ret = nand_read_skip_bad(nand, off, &size, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 377 | (u_char *)addr); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 378 | else |
| 379 | ret = nand_write_skip_bad(nand, off, &size, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 380 | (u_char *)addr); |
Mike Frysinger | 9a2cbb9 | 2009-04-12 22:29:20 -0400 | [diff] [blame] | 381 | } else if (!strcmp(s, ".oob")) { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 382 | /* out-of-band data */ |
| 383 | mtd_oob_ops_t ops = { |
| 384 | .oobbuf = (u8 *)addr, |
| 385 | .ooblen = size, |
| 386 | .mode = MTD_OOB_RAW |
| 387 | }; |
| 388 | |
Harald Welte | 5a39510 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 389 | if (read) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 390 | ret = nand->read_oob(nand, off, &ops); |
Harald Welte | 5a39510 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 391 | else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 392 | ret = nand->write_oob(nand, off, &ops); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 393 | } else { |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 394 | printf("Unknown nand command suffix '%s'.\n", s); |
| 395 | return 1; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 396 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 397 | |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 398 | printf(" %zu bytes %s: %s\n", size, |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 399 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 400 | |
| 401 | return ret == 0 ? 0 : 1; |
| 402 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 403 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 404 | if (strcmp(cmd, "markbad") == 0) { |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 405 | argc -= 2; |
| 406 | argv += 2; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 407 | |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 408 | if (argc <= 0) |
| 409 | goto usage; |
| 410 | |
| 411 | while (argc > 0) { |
| 412 | addr = simple_strtoul(*argv, NULL, 16); |
| 413 | |
| 414 | if (nand->block_markbad(nand, addr)) { |
| 415 | printf("block 0x%08lx NOT marked " |
| 416 | "as bad! ERROR %d\n", |
| 417 | addr, ret); |
| 418 | ret = 1; |
| 419 | } else { |
| 420 | printf("block 0x%08lx successfully " |
| 421 | "marked as bad\n", |
| 422 | addr); |
| 423 | } |
| 424 | --argc; |
| 425 | ++argv; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 426 | } |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 427 | return ret; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 428 | } |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 429 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 430 | if (strcmp(cmd, "biterr") == 0) { |
| 431 | /* todo */ |
| 432 | return 1; |
| 433 | } |
| 434 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 435 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 436 | if (strcmp(cmd, "lock") == 0) { |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 437 | int tight = 0; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 438 | int status = 0; |
| 439 | if (argc == 3) { |
| 440 | if (!strcmp("tight", argv[2])) |
| 441 | tight = 1; |
| 442 | if (!strcmp("status", argv[2])) |
| 443 | status = 1; |
| 444 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 445 | if (status) { |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 446 | do_nand_status(nand); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 447 | } else { |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 448 | if (!nand_lock(nand, tight)) { |
| 449 | puts("NAND flash successfully locked\n"); |
| 450 | } else { |
| 451 | puts("Error locking NAND flash\n"); |
| 452 | return 1; |
| 453 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 454 | } |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | if (strcmp(cmd, "unlock") == 0) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 459 | if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0) |
| 460 | return 1; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 461 | |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 462 | if (!nand_unlock(nand, off, size)) { |
| 463 | puts("NAND flash successfully unlocked\n"); |
| 464 | } else { |
| 465 | puts("Error unlocking NAND flash, " |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 466 | "write and erase will probably fail\n"); |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 469 | return 0; |
| 470 | } |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 471 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 472 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 473 | usage: |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 474 | cmd_usage(cmdtp); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 478 | U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand, |
| 479 | "NAND sub-system", |
| 480 | "info - show available NAND devices\n" |
| 481 | "nand device [dev] - show or set current device\n" |
| 482 | "nand read - addr off|partition size\n" |
| 483 | "nand write - addr off|partition size\n" |
| 484 | " read/write 'size' bytes starting at offset 'off'\n" |
| 485 | " to/from memory address 'addr', skipping bad blocks.\n" |
| 486 | "nand erase [clean] [off size] - erase 'size' bytes from\n" |
| 487 | " offset 'off' (entire device if not specified)\n" |
| 488 | "nand bad - show bad blocks\n" |
| 489 | "nand dump[.oob] off - dump page\n" |
| 490 | "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n" |
| 491 | "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" |
| 492 | "nand biterr off - make a bit error at offset (UNSAFE)" |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 493 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 494 | "\n" |
| 495 | "nand lock [tight] [status]\n" |
| 496 | " bring nand to lock state or display locked pages\n" |
| 497 | "nand unlock [offset] [size] - unlock section" |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 498 | #endif |
| 499 | ); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 500 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 501 | static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 502 | ulong offset, ulong addr, char *cmd) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 503 | { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 504 | int r; |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 505 | char *ep, *s; |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 506 | size_t cnt; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 507 | image_header_t *hdr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 508 | #if defined(CONFIG_FIT) |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 509 | const void *fit_hdr = NULL; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 510 | #endif |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 511 | |
| 512 | s = strchr(cmd, '.'); |
| 513 | if (s != NULL && |
Scott Wood | d1c1f9c | 2009-03-17 12:06:04 -0500 | [diff] [blame] | 514 | (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 515 | printf("Unknown nand load suffix '%s'\n", s); |
| 516 | show_boot_progress(-53); |
| 517 | return 1; |
| 518 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 519 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 520 | printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 521 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 522 | cnt = nand->writesize; |
Ladislav Michl | aaee2a3 | 2009-03-13 14:38:19 +0100 | [diff] [blame] | 523 | r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 524 | if (r) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 525 | puts("** Read error\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 526 | show_boot_progress (-56); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 527 | return 1; |
| 528 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 529 | show_boot_progress (56); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 530 | |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 531 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 532 | case IMAGE_FORMAT_LEGACY: |
| 533 | hdr = (image_header_t *)addr; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 534 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 535 | show_boot_progress (57); |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 536 | image_print_contents (hdr); |
| 537 | |
| 538 | cnt = image_get_image_size (hdr); |
| 539 | break; |
| 540 | #if defined(CONFIG_FIT) |
| 541 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 542 | fit_hdr = (const void *)addr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 543 | puts ("Fit image detected...\n"); |
| 544 | |
| 545 | cnt = fit_get_size (fit_hdr); |
| 546 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 547 | #endif |
| 548 | default: |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 549 | show_boot_progress (-57); |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 550 | puts ("** Unknown image type\n"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 551 | return 1; |
| 552 | } |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 553 | show_boot_progress (57); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 554 | |
Ladislav Michl | aaee2a3 | 2009-03-13 14:38:19 +0100 | [diff] [blame] | 555 | r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 556 | if (r) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 557 | puts("** Read error\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 558 | show_boot_progress (-58); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 559 | return 1; |
| 560 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 561 | show_boot_progress (58); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 562 | |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 563 | #if defined(CONFIG_FIT) |
| 564 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 565 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 566 | if (!fit_check_format (fit_hdr)) { |
| 567 | show_boot_progress (-150); |
| 568 | puts ("** Bad FIT image format\n"); |
| 569 | return 1; |
| 570 | } |
| 571 | show_boot_progress (151); |
| 572 | fit_print_contents (fit_hdr); |
| 573 | } |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 574 | #endif |
| 575 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 576 | /* Loading ok, update default load address */ |
| 577 | |
| 578 | load_addr = addr; |
| 579 | |
| 580 | /* Check if we should attempt an auto-start */ |
| 581 | if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) { |
| 582 | char *local_args[2]; |
| 583 | extern int do_bootm(cmd_tbl_t *, int, int, char *[]); |
| 584 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 585 | local_args[0] = cmd; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 586 | local_args[1] = NULL; |
| 587 | |
| 588 | printf("Automatic boot of image at addr 0x%08lx ...\n", addr); |
| 589 | |
| 590 | do_bootm(cmdtp, 0, 1, local_args); |
| 591 | return 1; |
| 592 | } |
| 593 | return 0; |
| 594 | } |
| 595 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 596 | int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 597 | { |
| 598 | char *boot_device = NULL; |
| 599 | int idx; |
| 600 | ulong addr, offset = 0; |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 601 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 602 | struct mtd_device *dev; |
| 603 | struct part_info *part; |
| 604 | u8 pnum; |
| 605 | |
| 606 | if (argc >= 2) { |
| 607 | char *p = (argc == 2) ? argv[1] : argv[2]; |
| 608 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && |
| 609 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { |
| 610 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 611 | puts("Not a NAND device\n"); |
| 612 | return 1; |
| 613 | } |
| 614 | if (argc > 3) |
| 615 | goto usage; |
| 616 | if (argc == 3) |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 617 | addr = simple_strtoul(argv[1], NULL, 16); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 618 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 619 | addr = CONFIG_SYS_LOAD_ADDR; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 620 | return nand_load_image(cmdtp, &nand_info[dev->id->num], |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 621 | part->offset, addr, argv[0]); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | #endif |
| 625 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 626 | show_boot_progress(52); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 627 | switch (argc) { |
| 628 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 629 | addr = CONFIG_SYS_LOAD_ADDR; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 630 | boot_device = getenv("bootdevice"); |
| 631 | break; |
| 632 | case 2: |
| 633 | addr = simple_strtoul(argv[1], NULL, 16); |
| 634 | boot_device = getenv("bootdevice"); |
| 635 | break; |
| 636 | case 3: |
| 637 | addr = simple_strtoul(argv[1], NULL, 16); |
| 638 | boot_device = argv[2]; |
| 639 | break; |
| 640 | case 4: |
| 641 | addr = simple_strtoul(argv[1], NULL, 16); |
| 642 | boot_device = argv[2]; |
| 643 | offset = simple_strtoul(argv[3], NULL, 16); |
| 644 | break; |
| 645 | default: |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 646 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 647 | usage: |
| 648 | #endif |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 649 | cmd_usage(cmdtp); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 650 | show_boot_progress(-53); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 651 | return 1; |
| 652 | } |
| 653 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 654 | show_boot_progress(53); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 655 | if (!boot_device) { |
| 656 | puts("\n** No boot device **\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 657 | show_boot_progress(-54); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 658 | return 1; |
| 659 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 660 | show_boot_progress(54); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 661 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 662 | idx = simple_strtoul(boot_device, NULL, 16); |
| 663 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 664 | if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 665 | printf("\n** Device %d not available\n", idx); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 666 | show_boot_progress(-55); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 667 | return 1; |
| 668 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 669 | show_boot_progress(55); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 670 | |
| 671 | return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]); |
| 672 | } |
| 673 | |
| 674 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 675 | "boot from NAND device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 676 | "[partition] | [[[loadAddr] dev] offset]" |
| 677 | ); |