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