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