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 | * |
Ben Gardiner | 5fc8b7f | 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 | 6d3c6d1 | 2005-04-03 22:35:21 +0000 | [diff] [blame] | 11 | * Added 16-bit nand support |
| 12 | * (C) 2004 Texas Instruments |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 16 | #include <linux/mtd/mtd.h> |
Bartlomiej Sieka | 582f1a3 | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 21 | #include <jffs2/jffs2.h> |
| 22 | #include <nand.h> |
| 23 | |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 24 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 25 | |
Wolfgang Denk | 161bfc5 | 2009-11-17 21:27:39 +0100 | [diff] [blame] | 26 | /* partition handling routines */ |
Stefan Roese | 198b23e | 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 | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 30 | u8 *part_num, struct part_info **part); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 31 | #endif |
| 32 | |
William Juul | e7ebe6e | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 34 | { |
| 35 | int i; |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 36 | u_char *datbuf, *oobbuf, *p; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 37 | |
William Juul | e7ebe6e | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 41 | puts("No memory for page buffer\n"); |
| 42 | return 1; |
| 43 | } |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 44 | off &= ~(nand->writesize - 1); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 45 | loff_t addr = (loff_t) off; |
William Juul | e7ebe6e | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 54 | if (i < 0) { |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 55 | printf("Error (%d) reading page %08lx\n", i, off); |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 56 | free(datbuf); |
| 57 | free(oobbuf); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 58 | return 1; |
| 59 | } |
Stefan Roese | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 60 | printf("Page %08lx dump:\n", off); |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 61 | i = nand->writesize >> 4; |
| 62 | p = datbuf; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 63 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 64 | while (i--) { |
William Juul | e7ebe6e | 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 | cc5f339 | 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 | 582f1a3 | 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 | 52c0796 | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 78 | p += 8; |
| 79 | } |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 80 | free(datbuf); |
| 81 | free(oobbuf); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* ------------------------------------------------------------------------- */ |
| 87 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 88 | static inline int str2long(char *p, ulong *num) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 89 | { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 90 | char *endptr; |
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 | *num = simple_strtoul(p, &endptr, 16); |
| 93 | return (*p != '\0' && *endptr == '\0') ? 1 : 0; |
| 94 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 95 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 96 | static int |
Wolfgang Denk | 6262d021 | 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 | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 98 | { |
| 99 | int idx = nand_curr_device; |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 100 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 101 | struct mtd_device *dev; |
| 102 | struct part_info *part; |
| 103 | u8 pnum; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 104 | |
Stefan Roese | 198b23e | 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 | 7f79454 | 2008-04-30 17:46:26 +0200 | [diff] [blame] | 114 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 198b23e | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 126 | } |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 127 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 128 | #endif |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 129 | |
Stefan Roese | 198b23e | 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; |
| 137 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 138 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 139 | if (argc >= 2) { |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 140 | if (!(str2long(argv[1], (ulong *)size))) { |
Stefan Roese | 198b23e | 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; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 148 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 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 | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 155 | printf("offset 0x%lx, size 0x%zx\n", *off, *size); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 156 | return 0; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Nishanth Menon | b20f840 | 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 | 5fc8b7f | 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 | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 204 | int argc, char * const argv[]) |
Ben Gardiner | 5fc8b7f | 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 | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 213 | if (ret) |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 214 | return 1; |
Scott Wood | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 215 | |
| 216 | printf("0x%08lx\n", nand_env_oob_offset); |
Ben Gardiner | 5fc8b7f | 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 | 0268da0 | 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 | 5fc8b7f | 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 | 0268da0 | 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 | 5fc8b7f | 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 | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 254 | if (ret) { |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 255 | printf("Error writing OOB block 0\n"); |
| 256 | return ret; |
| 257 | } |
Scott Wood | 0268da0 | 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 | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 271 | } else { |
| 272 | goto usage; |
| 273 | } |
| 274 | |
| 275 | return ret; |
| 276 | |
| 277 | usage: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 278 | return cmd_usage(cmdtp); |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | #endif |
| 282 | |
Wolfgang Grandegger | b325d7e | 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 | 6262d021 | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 295 | { |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 296 | int i, dev, ret = 0; |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 297 | ulong addr, off; |
| 298 | size_t size; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 299 | char *cmd, *s; |
| 300 | nand_info_t *nand; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 301 | #ifdef CONFIG_SYS_NAND_QUIET |
| 302 | int quiet = CONFIG_SYS_NAND_QUIET; |
Matthias Fuchs | d42fff2 | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 303 | #else |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 304 | int quiet = 0; |
Matthias Fuchs | d42fff2 | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 305 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 306 | const char *quiet_str = getenv("quiet"); |
Bartlomiej Sieka | 582f1a3 | 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 | d351b2b | 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 | 582f1a3 | 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 | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 320 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 321 | if (nand_info[i].name) |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 322 | nand_print_info(i); |
Bartlomiej Sieka | 582f1a3 | 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 | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 330 | putc('\n'); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 331 | if ((nand_curr_device < 0) || |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 332 | (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 333 | puts("no devices available\n"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 334 | else |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 335 | nand_print_info(nand_curr_device); |
Bartlomiej Sieka | 582f1a3 | 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 | 0383694 | 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 | 582f1a3 | 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 | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 346 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 347 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 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 | 582f1a3 | 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 | bfb8332 | 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 | 5fc8b7f | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 367 | goto usage; |
| 368 | |
Ben Gardiner | 5fc8b7f | 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 | 0268da0 | 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 | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 375 | #endif |
| 376 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 377 | /* the following commands operate on the current device */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 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 | 582f1a3 | 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 | 3167d07 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 389 | printf(" %08lx\n", off); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Stefan Roese | 198b23e | 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 | d351b2b | 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 | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 400 | /* "clean" at index 2 means request to write cleanmarker */ |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 401 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 402 | int o = clean ? 3 : 2; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 403 | int scrub = !strcmp(cmd, "scrub"); |
| 404 | |
Stefan Roese | 198b23e | 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 | d351b2b | 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 | 198b23e | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 428 | |
Florian Fainelli | adb8eb9 | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 437 | } else { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 438 | puts("scrub aborted\n"); |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 439 | return -1; |
| 440 | } |
| 441 | } |
| 442 | ret = nand_erase_opts(nand, &opts); |
Bartlomiej Sieka | 582f1a3 | 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 | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 456 | ret = nand_dump(nand, off, 1); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 457 | else |
William Juul | e7ebe6e | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 458 | ret = nand_dump(nand, off, 0); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 459 | |
| 460 | return ret == 0 ? 1 : 0; |
| 461 | |
| 462 | } |
| 463 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 464 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 465 | int read; |
| 466 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 467 | if (argc < 4) |
| 468 | goto usage; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 469 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 470 | addr = (ulong)simple_strtoul(argv[2], NULL, 16); |
| 471 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 472 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
Stefan Roese | 198b23e | 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 | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 476 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 477 | s = strchr(cmd, '.'); |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 478 | if (!s || !strcmp(s, ".jffs2") || |
| 479 | !strcmp(s, ".e") || !strcmp(s, ".i")) { |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 480 | if (read) |
| 481 | ret = nand_read_skip_bad(nand, off, &size, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 482 | (u_char *)addr); |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 483 | else |
| 484 | ret = nand_write_skip_bad(nand, off, &size, |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 485 | (u_char *)addr); |
Mike Frysinger | 9a2cbb9 | 2009-04-12 22:29:20 -0400 | [diff] [blame] | 486 | } else if (!strcmp(s, ".oob")) { |
William Juul | 52c0796 | 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 | 5a39510 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 494 | if (read) |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 495 | ret = nand->read_oob(nand, off, &ops); |
Harald Welte | 5a39510 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 496 | else |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 497 | ret = nand->write_oob(nand, off, &ops); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 498 | } else { |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 499 | printf("Unknown nand command suffix '%s'.\n", s); |
| 500 | return 1; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 501 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 502 | |
Mike Frysinger | d38f4a6 | 2009-01-18 19:46:06 -0500 | [diff] [blame] | 503 | printf(" %zu bytes %s: %s\n", size, |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 504 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 505 | |
| 506 | return ret == 0 ? 0 : 1; |
| 507 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 508 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 509 | if (strcmp(cmd, "markbad") == 0) { |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 510 | argc -= 2; |
| 511 | argv += 2; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 512 | |
Wolfgang Denk | 7a15ca1 | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 531 | } |
Wolfgang Denk | 7a15ca1 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 532 | return ret; |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 533 | } |
Scott Wood | cc5f339 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 534 | |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 535 | if (strcmp(cmd, "biterr") == 0) { |
| 536 | /* todo */ |
| 537 | return 1; |
| 538 | } |
| 539 | |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 540 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 541 | if (strcmp(cmd, "lock") == 0) { |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 542 | int tight = 0; |
Stefan Roese | d351b2b | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 550 | if (status) { |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 551 | do_nand_status(nand); |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 552 | } else { |
William Juul | 9e9c2c1 | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 559 | } |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | if (strcmp(cmd, "unlock") == 0) { |
Stefan Roese | 198b23e | 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 | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 566 | |
William Juul | 9e9c2c1 | 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 | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 571 | "write and erase will probably fail\n"); |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 572 | return 1; |
| 573 | } |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 576 | #endif |
Stefan Roese | d351b2b | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 577 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 578 | usage: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 579 | return cmd_usage(cmdtp); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 582 | U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand, |
| 583 | "NAND sub-system", |
| 584 | "info - show available NAND devices\n" |
| 585 | "nand device [dev] - show or set current device\n" |
| 586 | "nand read - addr off|partition size\n" |
| 587 | "nand write - addr off|partition size\n" |
| 588 | " read/write 'size' bytes starting at offset 'off'\n" |
| 589 | " to/from memory address 'addr', skipping bad blocks.\n" |
| 590 | "nand erase [clean] [off size] - erase 'size' bytes from\n" |
| 591 | " offset 'off' (entire device if not specified)\n" |
| 592 | "nand bad - show bad blocks\n" |
| 593 | "nand dump[.oob] off - dump page\n" |
| 594 | "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n" |
| 595 | "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" |
| 596 | "nand biterr off - make a bit error at offset (UNSAFE)" |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 597 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 598 | "\n" |
| 599 | "nand lock [tight] [status]\n" |
| 600 | " bring nand to lock state or display locked pages\n" |
| 601 | "nand unlock [offset] [size] - unlock section" |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 602 | #endif |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 603 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 604 | "\n" |
| 605 | "nand env.oob - environment offset in OOB of block 0 of" |
| 606 | " first device.\n" |
| 607 | "nand env.oob set off|partition - set enviromnent offset\n" |
| 608 | "nand env.oob get - get environment offset" |
| 609 | #endif |
Nishanth Menon | b20f840 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 610 | ); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 611 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 612 | 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] | 613 | ulong offset, ulong addr, char *cmd) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 614 | { |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 615 | int r; |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 616 | char *ep, *s; |
Wolfgang Denk | 9a11ccd | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 617 | size_t cnt; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 618 | image_header_t *hdr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 619 | #if defined(CONFIG_FIT) |
Marian Balakowicz | cefb402 | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 620 | const void *fit_hdr = NULL; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 621 | #endif |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 622 | |
| 623 | s = strchr(cmd, '.'); |
| 624 | if (s != NULL && |
Scott Wood | d1c1f9c | 2009-03-17 12:06:04 -0500 | [diff] [blame] | 625 | (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { |
Scott Wood | 12b1c95 | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 626 | printf("Unknown nand load suffix '%s'\n", s); |
| 627 | show_boot_progress(-53); |
| 628 | return 1; |
| 629 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 630 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 631 | printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 632 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 633 | cnt = nand->writesize; |
Ladislav Michl | aaee2a3 | 2009-03-13 14:38:19 +0100 | [diff] [blame] | 634 | r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 635 | if (r) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 636 | puts("** Read error\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 637 | show_boot_progress (-56); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 638 | return 1; |
| 639 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 640 | show_boot_progress (56); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 641 | |
Marian Balakowicz | d7c88a4 | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 642 | switch (genimg_get_format ((void *)addr)) { |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 643 | case IMAGE_FORMAT_LEGACY: |
| 644 | hdr = (image_header_t *)addr; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 645 | |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 646 | show_boot_progress (57); |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 647 | image_print_contents (hdr); |
| 648 | |
| 649 | cnt = image_get_image_size (hdr); |
| 650 | break; |
| 651 | #if defined(CONFIG_FIT) |
| 652 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 653 | fit_hdr = (const void *)addr; |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 654 | puts ("Fit image detected...\n"); |
| 655 | |
| 656 | cnt = fit_get_size (fit_hdr); |
| 657 | break; |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 658 | #endif |
| 659 | default: |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 660 | show_boot_progress (-57); |
Marian Balakowicz | dbdd16a | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 661 | puts ("** Unknown image type\n"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 662 | return 1; |
| 663 | } |
William Juul | bfb8332 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 664 | show_boot_progress (57); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 665 | |
Ladislav Michl | aaee2a3 | 2009-03-13 14:38:19 +0100 | [diff] [blame] | 666 | r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 667 | if (r) { |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 668 | puts("** Read error\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 669 | show_boot_progress (-58); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 670 | return 1; |
| 671 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 672 | show_boot_progress (58); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 673 | |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 674 | #if defined(CONFIG_FIT) |
| 675 | /* 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] | 676 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 677 | if (!fit_check_format (fit_hdr)) { |
| 678 | show_boot_progress (-150); |
| 679 | puts ("** Bad FIT image format\n"); |
| 680 | return 1; |
| 681 | } |
| 682 | show_boot_progress (151); |
| 683 | fit_print_contents (fit_hdr); |
| 684 | } |
Marian Balakowicz | e6bdd34 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 685 | #endif |
| 686 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 687 | /* Loading ok, update default load address */ |
| 688 | |
| 689 | load_addr = addr; |
| 690 | |
| 691 | /* Check if we should attempt an auto-start */ |
| 692 | if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) { |
| 693 | char *local_args[2]; |
| 694 | extern int do_bootm(cmd_tbl_t *, int, int, char *[]); |
| 695 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 696 | local_args[0] = cmd; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 697 | local_args[1] = NULL; |
| 698 | |
| 699 | printf("Automatic boot of image at addr 0x%08lx ...\n", addr); |
| 700 | |
| 701 | do_bootm(cmdtp, 0, 1, local_args); |
| 702 | return 1; |
| 703 | } |
| 704 | return 0; |
| 705 | } |
| 706 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 707 | int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 708 | { |
| 709 | char *boot_device = NULL; |
| 710 | int idx; |
| 711 | ulong addr, offset = 0; |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 712 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 713 | struct mtd_device *dev; |
| 714 | struct part_info *part; |
| 715 | u8 pnum; |
| 716 | |
| 717 | if (argc >= 2) { |
| 718 | char *p = (argc == 2) ? argv[1] : argv[2]; |
| 719 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && |
| 720 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { |
| 721 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 722 | puts("Not a NAND device\n"); |
| 723 | return 1; |
| 724 | } |
| 725 | if (argc > 3) |
| 726 | goto usage; |
| 727 | if (argc == 3) |
Thomas Knobloch | e84b777 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 728 | addr = simple_strtoul(argv[1], NULL, 16); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 729 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 730 | addr = CONFIG_SYS_LOAD_ADDR; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 731 | return nand_load_image(cmdtp, &nand_info[dev->id->num], |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 732 | part->offset, addr, argv[0]); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | #endif |
| 736 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 737 | show_boot_progress(52); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 738 | switch (argc) { |
| 739 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 740 | addr = CONFIG_SYS_LOAD_ADDR; |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 741 | boot_device = getenv("bootdevice"); |
| 742 | break; |
| 743 | case 2: |
| 744 | addr = simple_strtoul(argv[1], NULL, 16); |
| 745 | boot_device = getenv("bootdevice"); |
| 746 | break; |
| 747 | case 3: |
| 748 | addr = simple_strtoul(argv[1], NULL, 16); |
| 749 | boot_device = argv[2]; |
| 750 | break; |
| 751 | case 4: |
| 752 | addr = simple_strtoul(argv[1], NULL, 16); |
| 753 | boot_device = argv[2]; |
| 754 | offset = simple_strtoul(argv[3], NULL, 16); |
| 755 | break; |
| 756 | default: |
Ladislav Michl | 9d0928d | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 757 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 758 | usage: |
| 759 | #endif |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 760 | show_boot_progress(-53); |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 761 | return cmd_usage(cmdtp); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 764 | show_boot_progress(53); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 765 | if (!boot_device) { |
| 766 | puts("\n** No boot device **\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 767 | show_boot_progress(-54); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 768 | return 1; |
| 769 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 770 | show_boot_progress(54); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 771 | |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 772 | idx = simple_strtoul(boot_device, NULL, 16); |
| 773 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 774 | 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] | 775 | printf("\n** Device %d not available\n", idx); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 776 | show_boot_progress(-55); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 777 | return 1; |
| 778 | } |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 779 | show_boot_progress(55); |
Stefan Roese | 198b23e | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 780 | |
| 781 | return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]); |
| 782 | } |
| 783 | |
| 784 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 785 | "boot from NAND device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 786 | "[partition] | [[[loadAddr] dev] offset]" |
| 787 | ); |