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