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