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 |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 13 | * |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 14 | * Copyright 2010, 2012 Freescale Semiconductor |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 15 | * The portions of this file whose copyright is held by Freescale and which |
| 16 | * are not considered a derived work of GPL v2-only code may be distributed |
| 17 | * and/or modified under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of the |
| 19 | * License, or (at your option) any later version. |
wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <common.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 23 | #include <linux/mtd/mtd.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 24 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 25 | #include <console.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 26 | #include <watchdog.h> |
| 27 | #include <malloc.h> |
| 28 | #include <asm/byteorder.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 29 | #include <jffs2/jffs2.h> |
| 30 | #include <nand.h> |
| 31 | |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 32 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 33 | |
Wolfgang Denk | 445093d | 2009-11-17 21:27:39 +0100 | [diff] [blame] | 34 | /* partition handling routines */ |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 35 | int mtdparts_init(void); |
| 36 | int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num); |
| 37 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 38 | u8 *part_num, struct part_info **part); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 41 | static int nand_dump(nand_info_t *nand, ulong off, int only_oob, int repeat) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 42 | { |
| 43 | int i; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 44 | u_char *datbuf, *oobbuf, *p; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 45 | static loff_t last; |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 46 | int ret = 0; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 47 | |
| 48 | if (repeat) |
| 49 | off = last + nand->writesize; |
| 50 | |
| 51 | last = off; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 52 | |
Thierry Reding | 62fd66f | 2012-07-30 20:21:55 +0000 | [diff] [blame] | 53 | datbuf = memalign(ARCH_DMA_MINALIGN, nand->writesize); |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 54 | if (!datbuf) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 55 | puts("No memory for page buffer\n"); |
| 56 | return 1; |
| 57 | } |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 58 | |
| 59 | oobbuf = memalign(ARCH_DMA_MINALIGN, nand->oobsize); |
| 60 | if (!oobbuf) { |
| 61 | puts("No memory for page buffer\n"); |
| 62 | ret = 1; |
| 63 | goto free_dat; |
| 64 | } |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 65 | off &= ~(nand->writesize - 1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 66 | loff_t addr = (loff_t) off; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 67 | struct mtd_oob_ops ops; |
| 68 | memset(&ops, 0, sizeof(ops)); |
| 69 | ops.datbuf = datbuf; |
Tom Rini | cfdae12 | 2012-02-23 15:47:46 -0700 | [diff] [blame] | 70 | ops.oobbuf = oobbuf; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 71 | ops.len = nand->writesize; |
| 72 | ops.ooblen = nand->oobsize; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 73 | ops.mode = MTD_OPS_RAW; |
| 74 | i = mtd_read_oob(nand, addr, &ops); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 75 | if (i < 0) { |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 76 | printf("Error (%d) reading page %08lx\n", i, off); |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 77 | ret = 1; |
| 78 | goto free_all; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 79 | } |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 80 | printf("Page %08lx dump:\n", off); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 81 | |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 82 | if (!only_oob) { |
| 83 | i = nand->writesize >> 4; |
| 84 | p = datbuf; |
| 85 | |
| 86 | while (i--) { |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 87 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" |
| 88 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 89 | 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] | 90 | p[8], p[9], p[10], p[11], p[12], p[13], p[14], |
| 91 | p[15]); |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 92 | p += 16; |
| 93 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 94 | } |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 95 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 96 | puts("OOB:\n"); |
| 97 | i = nand->oobsize >> 3; |
Tom Rini | cfdae12 | 2012-02-23 15:47:46 -0700 | [diff] [blame] | 98 | p = oobbuf; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 99 | while (i--) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 100 | printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 101 | 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] | 102 | p += 8; |
| 103 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 104 | |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 105 | free_all: |
| 106 | free(oobbuf); |
| 107 | free_dat: |
| 108 | free(datbuf); |
| 109 | |
| 110 | return ret; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* ------------------------------------------------------------------------- */ |
| 114 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 115 | static int set_dev(int dev) |
| 116 | { |
| 117 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || |
| 118 | !nand_info[dev].name) { |
| 119 | puts("No such device\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (nand_curr_device == dev) |
| 124 | return 0; |
| 125 | |
| 126 | printf("Device %d: %s", dev, nand_info[dev].name); |
| 127 | puts("... is now current device\n"); |
| 128 | nand_curr_device = dev; |
| 129 | |
| 130 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
| 131 | board_nand_select_device(nand_info[dev].priv, dev); |
| 132 | #endif |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 137 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
| 138 | static void print_status(ulong start, ulong end, ulong erasesize, int status) |
| 139 | { |
Joe Hershberger | e70bfa2 | 2012-08-22 16:49:45 -0500 | [diff] [blame] | 140 | /* |
| 141 | * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is |
| 142 | * not the same as others. Instead of bit 1 being lock, it is |
| 143 | * #lock_tight. To make the driver support either format, ignore bit 1 |
| 144 | * and use only bit 0 and bit 2. |
| 145 | */ |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 146 | printf("%08lx - %08lx: %08lx blocks %s%s%s\n", |
| 147 | start, |
| 148 | end - 1, |
| 149 | (end - start) / erasesize, |
| 150 | ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""), |
Joe Hershberger | e70bfa2 | 2012-08-22 16:49:45 -0500 | [diff] [blame] | 151 | (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""), |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 152 | ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); |
| 153 | } |
| 154 | |
| 155 | static void do_nand_status(nand_info_t *nand) |
| 156 | { |
| 157 | ulong block_start = 0; |
| 158 | ulong off; |
| 159 | int last_status = -1; |
| 160 | |
| 161 | struct nand_chip *nand_chip = nand->priv; |
| 162 | /* check the WP bit */ |
| 163 | nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1); |
| 164 | printf("device is %swrite protected\n", |
| 165 | (nand_chip->read_byte(nand) & 0x80 ? |
| 166 | "NOT " : "")); |
| 167 | |
| 168 | for (off = 0; off < nand->size; off += nand->erasesize) { |
| 169 | int s = nand_get_lock_status(nand, off); |
| 170 | |
| 171 | /* print message only if status has changed */ |
| 172 | if (s != last_status && off != 0) { |
| 173 | print_status(block_start, off, nand->erasesize, |
| 174 | last_status); |
| 175 | block_start = off; |
| 176 | } |
| 177 | last_status = s; |
| 178 | } |
| 179 | /* Print the last block info */ |
| 180 | print_status(block_start, off, nand->erasesize, last_status); |
| 181 | } |
| 182 | #endif |
| 183 | |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 184 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 185 | unsigned long nand_env_oob_offset; |
| 186 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 187 | int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[]) |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 188 | { |
| 189 | int ret; |
| 190 | uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)]; |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 191 | nand_info_t *nand = &nand_info[0]; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 192 | char *cmd = argv[1]; |
| 193 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 194 | if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !nand->name) { |
| 195 | puts("no devices available\n"); |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | set_dev(0); |
| 200 | |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 201 | if (!strcmp(cmd, "get")) { |
| 202 | ret = get_nand_env_oob(nand, &nand_env_oob_offset); |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 203 | if (ret) |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 204 | return 1; |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 205 | |
| 206 | printf("0x%08lx\n", nand_env_oob_offset); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 207 | } else if (!strcmp(cmd, "set")) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 208 | loff_t addr; |
| 209 | loff_t maxsize; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 210 | struct mtd_oob_ops ops; |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 211 | int idx = 0; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 212 | |
| 213 | if (argc < 3) |
| 214 | goto usage; |
| 215 | |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 216 | /* We don't care about size, or maxsize. */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 217 | if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize, |
| 218 | MTD_DEV_TYPE_NAND, nand_info[idx].size)) { |
| 219 | puts("Offset or partition name expected\n"); |
| 220 | return 1; |
| 221 | } |
| 222 | if (set_dev(idx)) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 223 | puts("Offset or partition name expected\n"); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | if (idx != 0) { |
| 228 | puts("Partition not on first NAND device\n"); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | if (nand->oobavail < ENV_OFFSET_SIZE) { |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 233 | printf("Insufficient available OOB bytes:\n" |
| 234 | "%d OOB bytes available but %d required for " |
| 235 | "env.oob support\n", |
| 236 | nand->oobavail, ENV_OFFSET_SIZE); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | if ((addr & (nand->erasesize - 1)) != 0) { |
| 241 | printf("Environment offset must be block-aligned\n"); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | ops.datbuf = NULL; |
| 246 | ops.mode = MTD_OOB_AUTO; |
| 247 | ops.ooboffs = 0; |
| 248 | ops.ooblen = ENV_OFFSET_SIZE; |
| 249 | ops.oobbuf = (void *) oob_buf; |
| 250 | |
| 251 | oob_buf[0] = ENV_OOB_MARKER; |
| 252 | oob_buf[1] = addr / nand->erasesize; |
| 253 | |
| 254 | ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops); |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 255 | if (ret) { |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 256 | printf("Error writing OOB block 0\n"); |
| 257 | return ret; |
| 258 | } |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 259 | |
| 260 | ret = get_nand_env_oob(nand, &nand_env_oob_offset); |
| 261 | if (ret) { |
| 262 | printf("Error reading env offset in OOB\n"); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | if (addr != nand_env_oob_offset) { |
| 267 | printf("Verification of env offset in OOB failed: " |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 268 | "0x%08llx expected but got 0x%08lx\n", |
| 269 | (unsigned long long)addr, nand_env_oob_offset); |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 270 | return 1; |
| 271 | } |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 272 | } else { |
| 273 | goto usage; |
| 274 | } |
| 275 | |
| 276 | return ret; |
| 277 | |
| 278 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 279 | return CMD_RET_USAGE; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | #endif |
| 283 | |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 284 | static void nand_print_and_set_info(int idx) |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 285 | { |
| 286 | nand_info_t *nand = &nand_info[idx]; |
| 287 | struct nand_chip *chip = nand->priv; |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 288 | |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 289 | printf("Device %d: ", idx); |
| 290 | if (chip->numchips > 1) |
| 291 | printf("%dx ", chip->numchips); |
| 292 | printf("%s, sector size %u KiB\n", |
| 293 | nand->name, nand->erasesize >> 10); |
Heiko Schocher | 5db73fe | 2015-04-12 10:18:09 +0200 | [diff] [blame] | 294 | printf(" Page size %8d b\n", nand->writesize); |
| 295 | printf(" OOB size %8d b\n", nand->oobsize); |
| 296 | printf(" Erase size %8d b\n", nand->erasesize); |
| 297 | printf(" subpagesize %8d b\n", chip->subpagesize); |
| 298 | printf(" options 0x%8x\n", chip->options); |
| 299 | printf(" bbt options 0x%8x\n", chip->bbt_options); |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 300 | |
| 301 | /* Set geometry info */ |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 302 | setenv_hex("nand_writesize", nand->writesize); |
| 303 | setenv_hex("nand_oobsize", nand->oobsize); |
| 304 | setenv_hex("nand_erasesize", nand->erasesize); |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 307 | static int raw_access(nand_info_t *nand, ulong addr, loff_t off, ulong count, |
| 308 | int read) |
| 309 | { |
| 310 | int ret = 0; |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 311 | |
| 312 | while (count--) { |
| 313 | /* Raw access */ |
| 314 | mtd_oob_ops_t ops = { |
| 315 | .datbuf = (u8 *)addr, |
| 316 | .oobbuf = ((u8 *)addr) + nand->writesize, |
| 317 | .len = nand->writesize, |
| 318 | .ooblen = nand->oobsize, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 319 | .mode = MTD_OPS_RAW |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 320 | }; |
| 321 | |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 322 | if (read) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 323 | ret = mtd_read_oob(nand, off, &ops); |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 324 | } else { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 325 | ret = mtd_write_oob(nand, off, &ops); |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 326 | if (!ret) |
| 327 | ret = nand_verify_page_oob(nand, &ops, off); |
| 328 | } |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 329 | |
| 330 | if (ret) { |
| 331 | printf("%s: error at offset %llx, ret %d\n", |
| 332 | __func__, (long long)off, ret); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | addr += nand->writesize + nand->oobsize; |
| 337 | off += nand->writesize; |
| 338 | } |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 343 | /* Adjust a chip/partition size down for bad blocks so we don't |
Scott Wood | 9b80aa8 | 2013-06-20 12:45:31 -0500 | [diff] [blame] | 344 | * read/write past the end of a chip/partition by accident. |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 345 | */ |
| 346 | static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev) |
| 347 | { |
| 348 | /* We grab the nand info object here fresh because this is usually |
| 349 | * called after arg_off_size() which can change the value of dev. |
| 350 | */ |
| 351 | nand_info_t *nand = &nand_info[dev]; |
| 352 | loff_t maxoffset = offset + *size; |
| 353 | int badblocks = 0; |
| 354 | |
| 355 | /* count badblocks in NAND from offset to offset + size */ |
| 356 | for (; offset < maxoffset; offset += nand->erasesize) { |
| 357 | if (nand_block_isbad(nand, offset)) |
| 358 | badblocks++; |
| 359 | } |
| 360 | /* adjust size if any bad blocks found */ |
| 361 | if (badblocks) { |
| 362 | *size -= badblocks * nand->erasesize; |
| 363 | printf("size adjusted to 0x%llx (%d bad blocks)\n", |
| 364 | (unsigned long long)*size, badblocks); |
| 365 | } |
| 366 | } |
| 367 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 368 | static 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] | 369 | { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 370 | int i, ret = 0; |
| 371 | ulong addr; |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 372 | loff_t off, size, maxsize; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 373 | char *cmd, *s; |
| 374 | nand_info_t *nand; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 375 | #ifdef CONFIG_SYS_NAND_QUIET |
| 376 | int quiet = CONFIG_SYS_NAND_QUIET; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 377 | #else |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 378 | int quiet = 0; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 379 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 380 | const char *quiet_str = getenv("quiet"); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 381 | int dev = nand_curr_device; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 382 | int repeat = flag & CMD_FLAG_REPEAT; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 383 | |
| 384 | /* at least two arguments please */ |
| 385 | if (argc < 2) |
| 386 | goto usage; |
| 387 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 388 | if (quiet_str) |
| 389 | quiet = simple_strtoul(quiet_str, NULL, 0) != 0; |
| 390 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 391 | cmd = argv[1]; |
| 392 | |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 393 | /* Only "dump" is repeatable. */ |
| 394 | if (repeat && strcmp(cmd, "dump")) |
| 395 | return 0; |
| 396 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 397 | if (strcmp(cmd, "info") == 0) { |
| 398 | |
| 399 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 400 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 401 | if (nand_info[i].name) |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 402 | nand_print_and_set_info(i); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 403 | } |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | if (strcmp(cmd, "device") == 0) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 408 | if (argc < 3) { |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 409 | putc('\n'); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 410 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE) |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 411 | puts("no devices available\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 412 | else |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 413 | nand_print_and_set_info(dev); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 414 | return 0; |
| 415 | } |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 416 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 417 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 418 | set_dev(dev); |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 419 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 423 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 424 | /* this command operates only on the first nand device */ |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 425 | if (strcmp(cmd, "env.oob") == 0) |
| 426 | return do_nand_env_oob(cmdtp, argc - 1, argv + 1); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 427 | #endif |
| 428 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 429 | /* The following commands operate on the current device, unless |
| 430 | * overridden by a partition specifier. Note that if somehow the |
| 431 | * current device is invalid, it will have to be changed to a valid |
| 432 | * one before these commands can run, even if a partition specifier |
| 433 | * for another device is to be used. |
| 434 | */ |
| 435 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || |
| 436 | !nand_info[dev].name) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 437 | puts("\nno devices available\n"); |
| 438 | return 1; |
| 439 | } |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 440 | nand = &nand_info[dev]; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 441 | |
| 442 | if (strcmp(cmd, "bad") == 0) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 443 | printf("\nDevice %d bad blocks:\n", dev); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 444 | for (off = 0; off < nand->size; off += nand->erasesize) |
| 445 | if (nand_block_isbad(nand, off)) |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 446 | printf(" %08llx\n", (unsigned long long)off); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 450 | /* |
| 451 | * Syntax is: |
| 452 | * 0 1 2 3 4 |
| 453 | * nand erase [clean] [off size] |
| 454 | */ |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 455 | if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) { |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 456 | nand_erase_options_t opts; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 457 | /* "clean" at index 2 means request to write cleanmarker */ |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 458 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 459 | int scrub_yes = argc > 2 && !strcmp("-y", argv[2]); |
| 460 | int o = (clean || scrub_yes) ? 3 : 2; |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 461 | int scrub = !strncmp(cmd, "scrub", 5); |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 462 | int spread = 0; |
| 463 | int args = 2; |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 464 | const char *scrub_warn = |
| 465 | "Warning: " |
| 466 | "scrub option will erase all factory set bad blocks!\n" |
| 467 | " " |
| 468 | "There is no reliable way to recover them.\n" |
| 469 | " " |
| 470 | "Use this command only for testing purposes if you\n" |
| 471 | " " |
| 472 | "are sure of what you are doing!\n" |
| 473 | "\nReally scrub this NAND flash? <y/N>\n"; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 474 | |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 475 | if (cmd[5] != 0) { |
| 476 | if (!strcmp(&cmd[5], ".spread")) { |
| 477 | spread = 1; |
| 478 | } else if (!strcmp(&cmd[5], ".part")) { |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 479 | args = 1; |
| 480 | } else if (!strcmp(&cmd[5], ".chip")) { |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 481 | args = 0; |
| 482 | } else { |
| 483 | goto usage; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Don't allow missing arguments to cause full chip/partition |
| 489 | * erases -- easy to do accidentally, e.g. with a misspelled |
| 490 | * variable name. |
| 491 | */ |
| 492 | if (argc != o + args) |
| 493 | goto usage; |
| 494 | |
| 495 | printf("\nNAND %s: ", cmd); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 496 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 497 | if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size, |
| 498 | &maxsize, MTD_DEV_TYPE_NAND, |
| 499 | nand_info[dev].size) != 0) |
| 500 | return 1; |
| 501 | |
| 502 | if (set_dev(dev)) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 503 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 504 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 505 | nand = &nand_info[dev]; |
| 506 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 507 | memset(&opts, 0, sizeof(opts)); |
| 508 | opts.offset = off; |
| 509 | opts.length = size; |
| 510 | opts.jffs2 = clean; |
| 511 | opts.quiet = quiet; |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 512 | opts.spread = spread; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 513 | |
| 514 | if (scrub) { |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 515 | if (scrub_yes) { |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 516 | opts.scrub = 1; |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 517 | } else { |
| 518 | puts(scrub_warn); |
| 519 | if (confirm_yesno()) { |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 520 | opts.scrub = 1; |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 521 | } else { |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 522 | puts("scrub aborted\n"); |
Masahiro Yamada | 46aabcc | 2013-07-11 17:29:57 +0900 | [diff] [blame] | 523 | return 1; |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 524 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | ret = nand_erase_opts(nand, &opts); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 528 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 529 | |
| 530 | return ret == 0 ? 0 : 1; |
| 531 | } |
| 532 | |
| 533 | if (strncmp(cmd, "dump", 4) == 0) { |
| 534 | if (argc < 3) |
| 535 | goto usage; |
| 536 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 537 | off = (int)simple_strtoul(argv[2], NULL, 16); |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 538 | ret = nand_dump(nand, off, !strcmp(&cmd[4], ".oob"), repeat); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 539 | |
| 540 | return ret == 0 ? 1 : 0; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 543 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 544 | size_t rwsize; |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 545 | ulong pagecount = 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 546 | int read; |
Harvey Chapman | ced199d | 2013-02-07 11:34:44 +0000 | [diff] [blame] | 547 | int raw = 0; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 548 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 549 | if (argc < 4) |
| 550 | goto usage; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 551 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 552 | addr = (ulong)simple_strtoul(argv[2], NULL, 16); |
| 553 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 554 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 555 | printf("\nNAND %s: ", read ? "read" : "write"); |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 556 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 557 | s = strchr(cmd, '.'); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 558 | |
Steve Sakoman | 8d75c89 | 2012-06-07 10:19:18 -0700 | [diff] [blame] | 559 | if (s && !strcmp(s, ".raw")) { |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 560 | raw = 1; |
| 561 | |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 562 | if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize, |
| 563 | MTD_DEV_TYPE_NAND, |
| 564 | nand_info[dev].size)) |
| 565 | return 1; |
| 566 | |
| 567 | if (set_dev(dev)) |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 568 | return 1; |
| 569 | |
Rostislav Lisovy | 93d3232 | 2014-09-16 14:38:52 +0200 | [diff] [blame] | 570 | nand = &nand_info[dev]; |
| 571 | |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 572 | if (argc > 4 && !str2long(argv[4], &pagecount)) { |
| 573 | printf("'%s' is not a number\n", argv[4]); |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | if (pagecount * nand->writesize > size) { |
| 578 | puts("Size exceeds partition or device limit\n"); |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | rwsize = pagecount * (nand->writesize + nand->oobsize); |
| 583 | } else { |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 584 | if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off, |
| 585 | &size, &maxsize, |
| 586 | MTD_DEV_TYPE_NAND, |
| 587 | nand_info[dev].size) != 0) |
| 588 | return 1; |
| 589 | |
| 590 | if (set_dev(dev)) |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 591 | return 1; |
| 592 | |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 593 | /* size is unspecified */ |
| 594 | if (argc < 5) |
| 595 | adjust_size_for_badblocks(&size, off, dev); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 596 | rwsize = size; |
| 597 | } |
| 598 | |
Rostislav Lisovy | 93d3232 | 2014-09-16 14:38:52 +0200 | [diff] [blame] | 599 | nand = &nand_info[dev]; |
| 600 | |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 601 | if (!s || !strcmp(s, ".jffs2") || |
| 602 | !strcmp(s, ".e") || !strcmp(s, ".i")) { |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 603 | if (read) |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 604 | ret = nand_read_skip_bad(nand, off, &rwsize, |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 605 | NULL, maxsize, |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 606 | (u_char *)addr); |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 607 | else |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 608 | ret = nand_write_skip_bad(nand, off, &rwsize, |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 609 | NULL, maxsize, |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 610 | (u_char *)addr, |
| 611 | WITH_WR_VERIFY); |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 612 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 613 | } else if (!strcmp(s, ".trimffs")) { |
| 614 | if (read) { |
| 615 | printf("Unknown nand command suffix '%s'\n", s); |
| 616 | return 1; |
| 617 | } |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 618 | ret = nand_write_skip_bad(nand, off, &rwsize, NULL, |
| 619 | maxsize, (u_char *)addr, |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 620 | WITH_DROP_FFS | WITH_WR_VERIFY); |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 621 | #endif |
Mike Frysinger | dfc99e1 | 2009-04-12 22:29:20 -0400 | [diff] [blame] | 622 | } else if (!strcmp(s, ".oob")) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 623 | /* out-of-band data */ |
| 624 | mtd_oob_ops_t ops = { |
| 625 | .oobbuf = (u8 *)addr, |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 626 | .ooblen = rwsize, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 627 | .mode = MTD_OPS_RAW |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 628 | }; |
| 629 | |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 630 | if (read) |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 631 | ret = mtd_read_oob(nand, off, &ops); |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 632 | else |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 633 | ret = mtd_write_oob(nand, off, &ops); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 634 | } else if (raw) { |
| 635 | ret = raw_access(nand, addr, off, pagecount, read); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 636 | } else { |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 637 | printf("Unknown nand command suffix '%s'.\n", s); |
| 638 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 641 | printf(" %zu bytes %s: %s\n", rwsize, |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 642 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 643 | |
| 644 | return ret == 0 ? 0 : 1; |
| 645 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 646 | |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 647 | #ifdef CONFIG_CMD_NAND_TORTURE |
| 648 | if (strcmp(cmd, "torture") == 0) { |
| 649 | if (argc < 3) |
| 650 | goto usage; |
| 651 | |
| 652 | if (!str2off(argv[2], &off)) { |
| 653 | puts("Offset is not a valid number\n"); |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | printf("\nNAND torture: device %d offset 0x%llx size 0x%x\n", |
| 658 | dev, off, nand->erasesize); |
| 659 | ret = nand_torture(nand, off); |
| 660 | printf(" %s\n", ret ? "Failed" : "Passed"); |
| 661 | |
| 662 | return ret == 0 ? 0 : 1; |
| 663 | } |
| 664 | #endif |
| 665 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 666 | if (strcmp(cmd, "markbad") == 0) { |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 667 | argc -= 2; |
| 668 | argv += 2; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 669 | |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 670 | if (argc <= 0) |
| 671 | goto usage; |
| 672 | |
| 673 | while (argc > 0) { |
| 674 | addr = simple_strtoul(*argv, NULL, 16); |
| 675 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 676 | if (mtd_block_markbad(nand, addr)) { |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 677 | printf("block 0x%08lx NOT marked " |
| 678 | "as bad! ERROR %d\n", |
| 679 | addr, ret); |
| 680 | ret = 1; |
| 681 | } else { |
| 682 | printf("block 0x%08lx successfully " |
| 683 | "marked as bad\n", |
| 684 | addr); |
| 685 | } |
| 686 | --argc; |
| 687 | ++argv; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 688 | } |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 689 | return ret; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 690 | } |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 691 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 692 | if (strcmp(cmd, "biterr") == 0) { |
| 693 | /* todo */ |
| 694 | return 1; |
| 695 | } |
| 696 | |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 697 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 698 | if (strcmp(cmd, "lock") == 0) { |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 699 | int tight = 0; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 700 | int status = 0; |
| 701 | if (argc == 3) { |
| 702 | if (!strcmp("tight", argv[2])) |
| 703 | tight = 1; |
| 704 | if (!strcmp("status", argv[2])) |
| 705 | status = 1; |
| 706 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 707 | if (status) { |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 708 | do_nand_status(nand); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 709 | } else { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 710 | if (!nand_lock(nand, tight)) { |
| 711 | puts("NAND flash successfully locked\n"); |
| 712 | } else { |
| 713 | puts("Error locking NAND flash\n"); |
| 714 | return 1; |
| 715 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
Joe Hershberger | eee623a | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 720 | if (strncmp(cmd, "unlock", 5) == 0) { |
| 721 | int allexcept = 0; |
| 722 | |
| 723 | s = strchr(cmd, '.'); |
| 724 | |
| 725 | if (s && !strcmp(s, ".allexcept")) |
| 726 | allexcept = 1; |
| 727 | |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 728 | if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size, |
| 729 | &maxsize, MTD_DEV_TYPE_NAND, |
| 730 | nand_info[dev].size) < 0) |
| 731 | return 1; |
| 732 | |
| 733 | if (set_dev(dev)) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 734 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 735 | |
Joe Hershberger | eee623a | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 736 | if (!nand_unlock(&nand_info[dev], off, size, allexcept)) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 737 | puts("NAND flash successfully unlocked\n"); |
| 738 | } else { |
| 739 | puts("Error unlocking NAND flash, " |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 740 | "write and erase will probably fail\n"); |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 741 | return 1; |
| 742 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 743 | return 0; |
| 744 | } |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 745 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 746 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 747 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 748 | return CMD_RET_USAGE; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 751 | #ifdef CONFIG_SYS_LONGHELP |
| 752 | static char nand_help_text[] = |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 753 | "info - show available NAND devices\n" |
| 754 | "nand device [dev] - show or set current device\n" |
| 755 | "nand read - addr off|partition size\n" |
| 756 | "nand write - addr off|partition size\n" |
| 757 | " read/write 'size' bytes starting at offset 'off'\n" |
| 758 | " to/from memory address 'addr', skipping bad blocks.\n" |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 759 | "nand read.raw - addr off|partition [count]\n" |
| 760 | "nand write.raw - addr off|partition [count]\n" |
| 761 | " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n" |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 762 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 763 | "nand write.trimffs - addr off|partition size\n" |
| 764 | " write 'size' bytes starting at offset 'off' from memory address\n" |
| 765 | " 'addr', skipping bad blocks and dropping any pages at the end\n" |
| 766 | " of eraseblocks that contain only 0xFF\n" |
| 767 | #endif |
Daniel Hobi | eb3abce | 2011-05-19 19:28:54 +0200 | [diff] [blame] | 768 | "nand erase[.spread] [clean] off size - erase 'size' bytes " |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 769 | "from offset 'off'\n" |
| 770 | " With '.spread', erase enough for given file size, otherwise,\n" |
| 771 | " 'size' includes skipped bad blocks.\n" |
| 772 | "nand erase.part [clean] partition - erase entire mtd partition'\n" |
| 773 | "nand erase.chip [clean] - erase entire chip'\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 774 | "nand bad - show bad blocks\n" |
| 775 | "nand dump[.oob] off - dump page\n" |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 776 | #ifdef CONFIG_CMD_NAND_TORTURE |
| 777 | "nand torture off - torture block at offset\n" |
| 778 | #endif |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 779 | "nand scrub [-y] off size | scrub.part partition | scrub.chip\n" |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 780 | " really clean NAND erasing bad blocks (UNSAFE)\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 781 | "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" |
| 782 | "nand biterr off - make a bit error at offset (UNSAFE)" |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 783 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 784 | "\n" |
| 785 | "nand lock [tight] [status]\n" |
| 786 | " bring nand to lock state or display locked pages\n" |
Joe Hershberger | eee623a | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 787 | "nand unlock[.allexcept] [offset] [size] - unlock section" |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 788 | #endif |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 789 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 790 | "\n" |
| 791 | "nand env.oob - environment offset in OOB of block 0 of" |
| 792 | " first device.\n" |
| 793 | "nand env.oob set off|partition - set enviromnent offset\n" |
| 794 | "nand env.oob get - get environment offset" |
| 795 | #endif |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 796 | ""; |
| 797 | #endif |
| 798 | |
| 799 | U_BOOT_CMD( |
| 800 | nand, CONFIG_SYS_MAXARGS, 1, do_nand, |
| 801 | "NAND sub-system", nand_help_text |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 802 | ); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 803 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 804 | 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] | 805 | ulong offset, ulong addr, char *cmd) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 806 | { |
| 807 | int r; |
Mike Frysinger | 67d668b | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 808 | char *s; |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 809 | size_t cnt; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 810 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 811 | image_header_t *hdr; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 812 | #endif |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 813 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 814 | const void *fit_hdr = NULL; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 815 | #endif |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 816 | |
| 817 | s = strchr(cmd, '.'); |
| 818 | if (s != NULL && |
Scott Wood | 65d8bc9 | 2009-03-17 12:06:04 -0500 | [diff] [blame] | 819 | (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 820 | printf("Unknown nand load suffix '%s'\n", s); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 821 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 822 | return 1; |
| 823 | } |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 824 | |
| 825 | printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset); |
| 826 | |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 827 | cnt = nand->writesize; |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 828 | r = nand_read_skip_bad(nand, offset, &cnt, NULL, nand->size, |
| 829 | (u_char *)addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 830 | if (r) { |
| 831 | puts("** Read error\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 832 | bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 833 | return 1; |
| 834 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 835 | bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 836 | |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 837 | switch (genimg_get_format ((void *)addr)) { |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 838 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 839 | case IMAGE_FORMAT_LEGACY: |
| 840 | hdr = (image_header_t *)addr; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 841 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 842 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 843 | image_print_contents (hdr); |
| 844 | |
| 845 | cnt = image_get_image_size (hdr); |
| 846 | break; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 847 | #endif |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 848 | #if defined(CONFIG_FIT) |
| 849 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 850 | fit_hdr = (const void *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 851 | puts ("Fit image detected...\n"); |
| 852 | |
| 853 | cnt = fit_get_size (fit_hdr); |
| 854 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 855 | #endif |
| 856 | default: |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 857 | bootstage_error(BOOTSTAGE_ID_NAND_TYPE); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 858 | puts ("** Unknown image type\n"); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 859 | return 1; |
| 860 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 861 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 862 | |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 863 | r = nand_read_skip_bad(nand, offset, &cnt, NULL, nand->size, |
| 864 | (u_char *)addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 865 | if (r) { |
| 866 | puts("** Read error\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 867 | bootstage_error(BOOTSTAGE_ID_NAND_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 868 | return 1; |
| 869 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 870 | bootstage_mark(BOOTSTAGE_ID_NAND_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 871 | |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 872 | #if defined(CONFIG_FIT) |
| 873 | /* 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] | 874 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
| 875 | if (!fit_check_format (fit_hdr)) { |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 876 | bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ); |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 877 | puts ("** Bad FIT image format\n"); |
| 878 | return 1; |
| 879 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 880 | bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK); |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 881 | fit_print_contents (fit_hdr); |
| 882 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 883 | #endif |
| 884 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 885 | /* Loading ok, update default load address */ |
| 886 | |
| 887 | load_addr = addr; |
| 888 | |
Mike Frysinger | 67d668b | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 889 | return bootm_maybe_autostart(cmdtp, cmd); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 892 | static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc, |
| 893 | char * const argv[]) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 894 | { |
| 895 | char *boot_device = NULL; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 896 | int idx; |
| 897 | ulong addr, offset = 0; |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 898 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 899 | struct mtd_device *dev; |
| 900 | struct part_info *part; |
| 901 | u8 pnum; |
| 902 | |
| 903 | if (argc >= 2) { |
| 904 | char *p = (argc == 2) ? argv[1] : argv[2]; |
| 905 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && |
| 906 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { |
| 907 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 908 | puts("Not a NAND device\n"); |
| 909 | return 1; |
| 910 | } |
| 911 | if (argc > 3) |
| 912 | goto usage; |
| 913 | if (argc == 3) |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 914 | addr = simple_strtoul(argv[1], NULL, 16); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 915 | else |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 916 | addr = CONFIG_SYS_LOAD_ADDR; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 917 | return nand_load_image(cmdtp, &nand_info[dev->id->num], |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 918 | part->offset, addr, argv[0]); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 922 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 923 | bootstage_mark(BOOTSTAGE_ID_NAND_PART); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 924 | switch (argc) { |
| 925 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 926 | addr = CONFIG_SYS_LOAD_ADDR; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 927 | boot_device = getenv("bootdevice"); |
| 928 | break; |
| 929 | case 2: |
| 930 | addr = simple_strtoul(argv[1], NULL, 16); |
| 931 | boot_device = getenv("bootdevice"); |
| 932 | break; |
| 933 | case 3: |
| 934 | addr = simple_strtoul(argv[1], NULL, 16); |
| 935 | boot_device = argv[2]; |
| 936 | break; |
| 937 | case 4: |
| 938 | addr = simple_strtoul(argv[1], NULL, 16); |
| 939 | boot_device = argv[2]; |
| 940 | offset = simple_strtoul(argv[3], NULL, 16); |
| 941 | break; |
| 942 | default: |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 943 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 944 | usage: |
| 945 | #endif |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 946 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 947 | return CMD_RET_USAGE; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 948 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 949 | bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 950 | |
| 951 | if (!boot_device) { |
| 952 | puts("\n** No boot device **\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 953 | bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 954 | return 1; |
| 955 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 956 | bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 957 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 958 | idx = simple_strtoul(boot_device, NULL, 16); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 959 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 960 | 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] | 961 | printf("\n** Device %d not available\n", idx); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 962 | bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 963 | return 1; |
| 964 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 965 | bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 966 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 967 | return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 971 | "boot from NAND device", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 972 | "[partition] | [[[loadAddr] dev] offset]" |
| 973 | ); |