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