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