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