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