wdenk | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Driver for NAND support, Rick Bronson |
| 3 | * borrowed heavily from: |
| 4 | * (c) 1999 Machine Vision Holdings, Inc. |
| 5 | * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org> |
wdenk | 384cc68 | 2005-04-03 22:35:21 +0000 | [diff] [blame] | 6 | * |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 7 | * Ported 'dynenv' to 'nand env.oob' command |
| 8 | * (C) 2010 Nanometrics, Inc. |
| 9 | * 'dynenv' -- Dynamic environment offset in NAND OOB |
| 10 | * (C) Copyright 2006-2007 OpenMoko, Inc. |
wdenk | 384cc68 | 2005-04-03 22:35:21 +0000 | [diff] [blame] | 11 | * Added 16-bit nand support |
| 12 | * (C) 2004 Texas Instruments |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 13 | * |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 14 | * Copyright 2010, 2012 Freescale Semiconductor |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 15 | * The portions of this file whose copyright is held by Freescale and which |
| 16 | * are not considered a derived work of GPL v2-only code may be distributed |
| 17 | * and/or modified under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation; either version 2 of the |
| 19 | * License, or (at your option) any later version. |
Balamanikandan Gunasundar | 4ea84c5 | 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 | dc7c9a1 | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 27 | #include <bootstage.h> |
Simon Glass | 8e8ccfe | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 28 | #include <image.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 29 | #include <asm/cache.h> |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 30 | #include <linux/mtd/mtd.h> |
Tom Rini | 1cefed1 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 31 | #include <linux/mtd/rawnand.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 32 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 33 | #include <console.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 34 | #include <env.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 35 | #include <watchdog.h> |
| 36 | #include <malloc.h> |
| 37 | #include <asm/byteorder.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 38 | #include <jffs2/jffs2.h> |
| 39 | #include <nand.h> |
| 40 | |
Miquel Raynal | eb446ef | 2019-10-25 19:39:29 +0200 | [diff] [blame] | 41 | #include "legacy-mtd-utils.h" |
| 42 | |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 43 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 44 | |
Wolfgang Denk | 445093d | 2009-11-17 21:27:39 +0100 | [diff] [blame] | 45 | /* partition handling routines */ |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 46 | int mtdparts_init(void); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 47 | int find_dev_and_part(const char *id, struct mtd_device **dev, |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 48 | u8 *part_num, struct part_info **part); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Balamanikandan Gunasundar | 4ea84c5 | 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 | 151c06e | 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 | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 163 | { |
| 164 | int i; |
William Juul | 9ad754f | 2007-12-14 16:33:45 +0100 | [diff] [blame] | 165 | u_char *datbuf, *oobbuf, *p; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 166 | static loff_t last; |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 167 | int ret = 0; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 168 | |
| 169 | if (repeat) |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 170 | off = last + mtd->writesize; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 171 | |
| 172 | last = off; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 173 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 174 | datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize); |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 175 | if (!datbuf) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 176 | puts("No memory for page buffer\n"); |
| 177 | return 1; |
| 178 | } |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 179 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 180 | oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize); |
Masahiro Yamada | e40520b | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 186 | off &= ~(mtd->writesize - 1); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 187 | loff_t addr = (loff_t) off; |
William Juul | 9ad754f | 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 | cfdae12 | 2012-02-23 15:47:46 -0700 | [diff] [blame] | 191 | ops.oobbuf = oobbuf; |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 192 | ops.len = mtd->writesize; |
| 193 | ops.ooblen = mtd->oobsize; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 194 | ops.mode = MTD_OPS_RAW; |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 195 | i = mtd_read_oob(mtd, addr, &ops); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 196 | if (i < 0) { |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 197 | printf("Error (%d) reading page %08lx\n", i, off); |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 198 | ret = 1; |
| 199 | goto free_all; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 200 | } |
Stefan Roese | e870690 | 2008-07-10 10:10:54 +0200 | [diff] [blame] | 201 | printf("Page %08lx dump:\n", off); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 202 | |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 203 | if (!only_oob) { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 204 | i = mtd->writesize >> 4; |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 205 | p = datbuf; |
| 206 | |
| 207 | while (i--) { |
William Juul | 9ad754f | 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 | dfbf617 | 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 | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 213 | p += 16; |
| 214 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 215 | } |
Masahiro Yamada | 7d25cd3 | 2013-07-11 17:27:13 +0900 | [diff] [blame] | 216 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 217 | puts("OOB:\n"); |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 218 | i = mtd->oobsize >> 3; |
Tom Rini | cfdae12 | 2012-02-23 15:47:46 -0700 | [diff] [blame] | 219 | p = oobbuf; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 220 | while (i--) { |
William Juul | cfa460a | 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 | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 223 | p += 8; |
| 224 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 225 | |
Masahiro Yamada | e40520b | 2013-07-11 17:27:12 +0900 | [diff] [blame] | 226 | free_all: |
| 227 | free(oobbuf); |
| 228 | free_dat: |
| 229 | free(datbuf); |
| 230 | |
| 231 | return ret; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* ------------------------------------------------------------------------- */ |
| 235 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 236 | static int set_dev(int dev) |
| 237 | { |
Mugunthan V N | ad92dff | 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 | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 242 | |
| 243 | if (nand_curr_device == dev) |
| 244 | return 0; |
| 245 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 246 | printf("Device %d: %s", dev, mtd->name); |
Scott Wood | ea533c2 | 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 | 6308e71 | 2017-06-26 19:12:58 -0500 | [diff] [blame] | 251 | board_nand_select_device(mtd_to_nand(mtd), dev); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 252 | #endif |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Nishanth Menon | 50657c2 | 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 | e70bfa2 | 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 | 50657c2 | 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 | e70bfa2 | 2012-08-22 16:49:45 -0500 | [diff] [blame] | 271 | (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""), |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 272 | ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); |
| 273 | } |
| 274 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 275 | static void do_nand_status(struct mtd_info *mtd) |
Nishanth Menon | 50657c2 | 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 | 17cb4b8 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 281 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 282 | /* check the WP bit */ |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 283 | nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 284 | printf("device is %swrite protected\n", |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 285 | (nand_chip->read_byte(mtd) & 0x80 ? |
| 286 | "NOT " : "")); |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 287 | |
Scott Wood | 151c06e | 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 | 50657c2 | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 293 | print_status(block_start, off, mtd->erasesize, |
Nishanth Menon | 50657c2 | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 300 | print_status(block_start, off, mtd->erasesize, last_status); |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 301 | } |
| 302 | #endif |
| 303 | |
Ben Gardiner | c9f7351 | 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 | 0914011 | 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 | c9f7351 | 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 | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 311 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 312 | char *cmd = argv[1]; |
| 313 | |
Scott Wood | 8b7d512 | 2016-09-01 17:31:31 -0500 | [diff] [blame] | 314 | if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) { |
Scott Wood | ea533c2 | 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 | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 321 | if (!strcmp(cmd, "get")) { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 322 | ret = get_nand_env_oob(mtd, &nand_env_oob_offset); |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 323 | if (ret) |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 324 | return 1; |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 325 | |
| 326 | printf("0x%08lx\n", nand_env_oob_offset); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 327 | } else if (!strcmp(cmd, "set")) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 328 | loff_t addr; |
| 329 | loff_t maxsize; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 330 | struct mtd_oob_ops ops; |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 331 | int idx = 0; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 332 | |
| 333 | if (argc < 3) |
| 334 | goto usage; |
| 335 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 336 | mtd = get_nand_dev_by_index(idx); |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 337 | /* We don't care about size, or maxsize. */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 338 | if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize, |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 339 | MTD_DEV_TYPE_NAND, mtd->size)) { |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 340 | puts("Offset or partition name expected\n"); |
| 341 | return 1; |
| 342 | } |
| 343 | if (set_dev(idx)) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 344 | puts("Offset or partition name expected\n"); |
| 345 | return 1; |
| 346 | } |
| 347 | |
| 348 | if (idx != 0) { |
| 349 | puts("Partition not on first NAND device\n"); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 350 | return 1; |
| 351 | } |
| 352 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 353 | if (mtd->oobavail < ENV_OFFSET_SIZE) { |
Scott Wood | 53504a2 | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 357 | mtd->oobavail, ENV_OFFSET_SIZE); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 358 | return 1; |
| 359 | } |
| 360 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 361 | if ((addr & (mtd->erasesize - 1)) != 0) { |
Ben Gardiner | c9f7351 | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 373 | oob_buf[1] = addr / mtd->erasesize; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 374 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 375 | ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops); |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 376 | if (ret) { |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 377 | printf("Error writing OOB block 0\n"); |
| 378 | return ret; |
| 379 | } |
Scott Wood | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 380 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 381 | ret = get_nand_env_oob(mtd, &nand_env_oob_offset); |
Scott Wood | 53504a2 | 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 | ea533c2 | 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 | 53504a2 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 391 | return 1; |
| 392 | } |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 393 | } else { |
| 394 | goto usage; |
| 395 | } |
| 396 | |
| 397 | return ret; |
| 398 | |
| 399 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 400 | return CMD_RET_USAGE; |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | #endif |
| 404 | |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 405 | static void nand_print_and_set_info(int idx) |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 406 | { |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 407 | struct mtd_info *mtd; |
| 408 | struct nand_chip *chip; |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 409 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 410 | mtd = get_nand_dev_by_index(idx); |
| 411 | if (!mtd) |
| 412 | return; |
| 413 | |
| 414 | chip = mtd_to_nand(mtd); |
Wolfgang Grandegger | 672ed2a | 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 | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 419 | mtd->name, mtd->erasesize >> 10); |
| 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); |
Heiko Schocher | 5db73fe | 2015-04-12 10:18:09 +0200 | [diff] [blame] | 423 | printf(" subpagesize %8d b\n", chip->subpagesize); |
Lothar Waßmann | 66dc09c | 2017-07-10 08:44:24 +0200 | [diff] [blame] | 424 | printf(" options 0x%08x\n", chip->options); |
| 425 | printf(" bbt options 0x%08x\n", chip->bbt_options); |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 426 | |
| 427 | /* Set geometry info */ |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 428 | env_set_hex("nand_writesize", mtd->writesize); |
| 429 | env_set_hex("nand_oobsize", mtd->oobsize); |
| 430 | env_set_hex("nand_erasesize", mtd->erasesize); |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 433 | static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off, |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 434 | ulong count, int read, int no_verify) |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 435 | { |
| 436 | int ret = 0; |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 437 | |
| 438 | while (count--) { |
| 439 | /* Raw access */ |
| 440 | mtd_oob_ops_t ops = { |
| 441 | .datbuf = (u8 *)addr, |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 442 | .oobbuf = ((u8 *)addr) + mtd->writesize, |
| 443 | .len = mtd->writesize, |
| 444 | .ooblen = mtd->oobsize, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 445 | .mode = MTD_OPS_RAW |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 446 | }; |
| 447 | |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 448 | if (read) { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 449 | ret = mtd_read_oob(mtd, off, &ops); |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 450 | } else { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 451 | ret = mtd_write_oob(mtd, off, &ops); |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 452 | if (!ret && !no_verify) |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 453 | ret = nand_verify_page_oob(mtd, &ops, off); |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 454 | } |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 455 | |
| 456 | if (ret) { |
| 457 | printf("%s: error at offset %llx, ret %d\n", |
| 458 | __func__, (long long)off, ret); |
| 459 | break; |
| 460 | } |
| 461 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 462 | addr += mtd->writesize + mtd->oobsize; |
| 463 | off += mtd->writesize; |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 469 | /* Adjust a chip/partition size down for bad blocks so we don't |
Scott Wood | 9b80aa8 | 2013-06-20 12:45:31 -0500 | [diff] [blame] | 470 | * read/write past the end of a chip/partition by accident. |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 471 | */ |
| 472 | static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev) |
| 473 | { |
| 474 | /* We grab the nand info object here fresh because this is usually |
| 475 | * called after arg_off_size() which can change the value of dev. |
| 476 | */ |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 477 | struct mtd_info *mtd = get_nand_dev_by_index(dev); |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 478 | loff_t maxoffset = offset + *size; |
| 479 | int badblocks = 0; |
| 480 | |
| 481 | /* count badblocks in NAND from offset to offset + size */ |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 482 | for (; offset < maxoffset; offset += mtd->erasesize) { |
| 483 | if (nand_block_isbad(mtd, offset)) |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 484 | badblocks++; |
| 485 | } |
| 486 | /* adjust size if any bad blocks found */ |
| 487 | if (badblocks) { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 488 | *size -= badblocks * mtd->erasesize; |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 489 | printf("size adjusted to 0x%llx (%d bad blocks)\n", |
| 490 | (unsigned long long)*size, badblocks); |
| 491 | } |
| 492 | } |
| 493 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 494 | static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc, |
| 495 | char *const argv[]) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 496 | { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 497 | int i, ret = 0; |
| 498 | ulong addr; |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 499 | loff_t off, size, maxsize; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 500 | char *cmd, *s; |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 501 | struct mtd_info *mtd; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 502 | #ifdef CONFIG_SYS_NAND_QUIET |
| 503 | int quiet = CONFIG_SYS_NAND_QUIET; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 504 | #else |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 505 | int quiet = 0; |
Matthias Fuchs | c750d2e | 2007-09-12 12:36:53 +0200 | [diff] [blame] | 506 | #endif |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 507 | const char *quiet_str = env_get("quiet"); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 508 | int dev = nand_curr_device; |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 509 | int repeat = flag & CMD_FLAG_REPEAT; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 510 | |
| 511 | /* at least two arguments please */ |
| 512 | if (argc < 2) |
| 513 | goto usage; |
| 514 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 515 | if (quiet_str) |
| 516 | quiet = simple_strtoul(quiet_str, NULL, 0) != 0; |
| 517 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 518 | cmd = argv[1]; |
| 519 | |
Scott Wood | 8c5659a | 2010-08-25 15:24:01 -0500 | [diff] [blame] | 520 | /* Only "dump" is repeatable. */ |
| 521 | if (repeat && strcmp(cmd, "dump")) |
| 522 | return 0; |
| 523 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 524 | if (strcmp(cmd, "info") == 0) { |
| 525 | |
| 526 | putc('\n'); |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 527 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 528 | nand_print_and_set_info(i); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | if (strcmp(cmd, "device") == 0) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 533 | if (argc < 3) { |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 534 | putc('\n'); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 535 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE) |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 536 | puts("no devices available\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 537 | else |
Marek Vasut | ce80ddc | 2011-09-22 03:57:26 +0200 | [diff] [blame] | 538 | nand_print_and_set_info(dev); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 539 | return 0; |
| 540 | } |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 541 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 542 | dev = (int)dectoul(argv[2], NULL); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 543 | set_dev(dev); |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 544 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 548 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 549 | /* this command operates only on the first nand device */ |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 550 | if (strcmp(cmd, "env.oob") == 0) |
| 551 | return do_nand_env_oob(cmdtp, argc - 1, argv + 1); |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 552 | #endif |
| 553 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 554 | /* The following commands operate on the current device, unless |
| 555 | * overridden by a partition specifier. Note that if somehow the |
| 556 | * current device is invalid, it will have to be changed to a valid |
| 557 | * one before these commands can run, even if a partition specifier |
| 558 | * for another device is to be used. |
| 559 | */ |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 560 | mtd = get_nand_dev_by_index(dev); |
| 561 | if (!mtd) { |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 562 | puts("\nno devices available\n"); |
| 563 | return 1; |
| 564 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 565 | |
| 566 | if (strcmp(cmd, "bad") == 0) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 567 | printf("\nDevice %d bad blocks:\n", dev); |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 568 | for (off = 0; off < mtd->size; off += mtd->erasesize) |
| 569 | if (nand_block_isbad(mtd, off)) |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 570 | printf(" %08llx\n", (unsigned long long)off); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 574 | /* |
| 575 | * Syntax is: |
| 576 | * 0 1 2 3 4 |
| 577 | * nand erase [clean] [off size] |
| 578 | */ |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 579 | if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) { |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 580 | nand_erase_options_t opts; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 581 | /* "clean" at index 2 means request to write cleanmarker */ |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 582 | int clean = argc > 2 && !strcmp("clean", argv[2]); |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 583 | int scrub_yes = argc > 2 && !strcmp("-y", argv[2]); |
| 584 | int o = (clean || scrub_yes) ? 3 : 2; |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 585 | int scrub = !strncmp(cmd, "scrub", 5); |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 586 | int spread = 0; |
| 587 | int args = 2; |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 588 | const char *scrub_warn = |
| 589 | "Warning: " |
| 590 | "scrub option will erase all factory set bad blocks!\n" |
| 591 | " " |
| 592 | "There is no reliable way to recover them.\n" |
| 593 | " " |
| 594 | "Use this command only for testing purposes if you\n" |
| 595 | " " |
| 596 | "are sure of what you are doing!\n" |
| 597 | "\nReally scrub this NAND flash? <y/N>\n"; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 598 | |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 599 | if (cmd[5] != 0) { |
| 600 | if (!strcmp(&cmd[5], ".spread")) { |
| 601 | spread = 1; |
| 602 | } else if (!strcmp(&cmd[5], ".part")) { |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 603 | args = 1; |
| 604 | } else if (!strcmp(&cmd[5], ".chip")) { |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 605 | args = 0; |
| 606 | } else { |
| 607 | goto usage; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Don't allow missing arguments to cause full chip/partition |
| 613 | * erases -- easy to do accidentally, e.g. with a misspelled |
| 614 | * variable name. |
| 615 | */ |
| 616 | if (argc != o + args) |
| 617 | goto usage; |
| 618 | |
| 619 | printf("\nNAND %s: ", cmd); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 620 | /* skip first two or three arguments, look for offset and size */ |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 621 | if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size, |
| 622 | &maxsize, MTD_DEV_TYPE_NAND, |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 623 | mtd->size) != 0) |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 624 | return 1; |
| 625 | |
| 626 | if (set_dev(dev)) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 627 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 628 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 629 | mtd = get_nand_dev_by_index(dev); |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 630 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 631 | memset(&opts, 0, sizeof(opts)); |
| 632 | opts.offset = off; |
| 633 | opts.length = size; |
| 634 | opts.jffs2 = clean; |
| 635 | opts.quiet = quiet; |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 636 | opts.spread = spread; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 637 | |
| 638 | if (scrub) { |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 639 | if (scrub_yes) { |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 640 | opts.scrub = 1; |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 641 | } else { |
| 642 | puts(scrub_warn); |
| 643 | if (confirm_yesno()) { |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 644 | opts.scrub = 1; |
Pierre Aubert | a5dffa4 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 645 | } else { |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 646 | puts("scrub aborted\n"); |
Masahiro Yamada | 46aabcc | 2013-07-11 17:29:57 +0900 | [diff] [blame] | 647 | return 1; |
Florian Fainelli | 6b94b49 | 2010-03-20 19:02:58 +0100 | [diff] [blame] | 648 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 649 | } |
| 650 | } |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 651 | ret = nand_erase_opts(mtd, &opts); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 652 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 653 | |
| 654 | return ret == 0 ? 0 : 1; |
| 655 | } |
| 656 | |
| 657 | if (strncmp(cmd, "dump", 4) == 0) { |
| 658 | if (argc < 3) |
| 659 | goto usage; |
| 660 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 661 | off = (int)hextoul(argv[2], NULL); |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 662 | ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 663 | |
| 664 | return ret == 0 ? 1 : 0; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 665 | } |
| 666 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 667 | if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 668 | size_t rwsize; |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 669 | ulong pagecount = 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 670 | int read; |
Harvey Chapman | ced199d | 2013-02-07 11:34:44 +0000 | [diff] [blame] | 671 | int raw = 0; |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 672 | int no_verify = 0; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 673 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 674 | if (argc < 4) |
| 675 | goto usage; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 676 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 677 | addr = (ulong)hextoul(argv[2], NULL); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 678 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 679 | read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 680 | printf("\nNAND %s: ", read ? "read" : "write"); |
William Juul | 4cbb651 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 681 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 682 | s = strchr(cmd, '.'); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 683 | |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 684 | if (s && !strncmp(s, ".raw", 4)) { |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 685 | raw = 1; |
| 686 | |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 687 | if (!strcmp(s, ".raw.noverify")) |
| 688 | no_verify = 1; |
| 689 | |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 690 | if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize, |
| 691 | MTD_DEV_TYPE_NAND, |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 692 | mtd->size)) |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 693 | return 1; |
| 694 | |
| 695 | if (set_dev(dev)) |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 696 | return 1; |
| 697 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 698 | mtd = get_nand_dev_by_index(dev); |
Rostislav Lisovy | 93d3232 | 2014-09-16 14:38:52 +0200 | [diff] [blame] | 699 | |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 700 | if (argc > 4 && !str2long(argv[4], &pagecount)) { |
| 701 | printf("'%s' is not a number\n", argv[4]); |
| 702 | return 1; |
| 703 | } |
| 704 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 705 | if (pagecount * mtd->writesize > size) { |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 706 | puts("Size exceeds partition or device limit\n"); |
| 707 | return -1; |
| 708 | } |
| 709 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 710 | rwsize = pagecount * (mtd->writesize + mtd->oobsize); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 711 | } else { |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 712 | if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off, |
| 713 | &size, &maxsize, |
| 714 | MTD_DEV_TYPE_NAND, |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 715 | mtd->size) != 0) |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 716 | return 1; |
| 717 | |
| 718 | if (set_dev(dev)) |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 719 | return 1; |
| 720 | |
Harvey Chapman | e834402 | 2013-02-26 17:57:14 +0000 | [diff] [blame] | 721 | /* size is unspecified */ |
| 722 | if (argc < 5) |
| 723 | adjust_size_for_badblocks(&size, off, dev); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 724 | rwsize = size; |
| 725 | } |
| 726 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 727 | mtd = get_nand_dev_by_index(dev); |
Rostislav Lisovy | 93d3232 | 2014-09-16 14:38:52 +0200 | [diff] [blame] | 728 | |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 729 | if (!s || !strcmp(s, ".jffs2") || |
| 730 | !strcmp(s, ".e") || !strcmp(s, ".i")) { |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 731 | if (read) |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 732 | ret = nand_read_skip_bad(mtd, off, &rwsize, |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 733 | NULL, maxsize, |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 734 | (u_char *)addr); |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 735 | else |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 736 | ret = nand_write_skip_bad(mtd, off, &rwsize, |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 737 | NULL, maxsize, |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 738 | (u_char *)addr, |
| 739 | WITH_WR_VERIFY); |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 740 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 741 | } else if (!strcmp(s, ".trimffs")) { |
| 742 | if (read) { |
| 743 | printf("Unknown nand command suffix '%s'\n", s); |
| 744 | return 1; |
| 745 | } |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 746 | ret = nand_write_skip_bad(mtd, off, &rwsize, NULL, |
Tom Rini | c39d6a0 | 2013-03-14 05:32:50 +0000 | [diff] [blame] | 747 | maxsize, (u_char *)addr, |
Peter Tyser | 6b94f11 | 2015-02-03 11:58:13 -0600 | [diff] [blame] | 748 | WITH_DROP_FFS | WITH_WR_VERIFY); |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 749 | #endif |
Mike Frysinger | dfc99e1 | 2009-04-12 22:29:20 -0400 | [diff] [blame] | 750 | } else if (!strcmp(s, ".oob")) { |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 751 | /* out-of-band data */ |
| 752 | mtd_oob_ops_t ops = { |
| 753 | .oobbuf = (u8 *)addr, |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 754 | .ooblen = rwsize, |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 755 | .mode = MTD_OPS_RAW |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 756 | }; |
| 757 | |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 758 | if (read) |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 759 | ret = mtd_read_oob(mtd, off, &ops); |
Harald Welte | 62d4f43 | 2007-12-19 14:12:53 +0100 | [diff] [blame] | 760 | else |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 761 | ret = mtd_write_oob(mtd, off, &ops); |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 762 | } else if (raw) { |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 763 | ret = raw_access(mtd, addr, off, pagecount, read, |
| 764 | no_verify); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 765 | } else { |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 766 | printf("Unknown nand command suffix '%s'.\n", s); |
| 767 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Scott Wood | ea533c2 | 2010-08-02 13:04:24 -0500 | [diff] [blame] | 770 | printf(" %zu bytes %s: %s\n", rwsize, |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 771 | read ? "read" : "written", ret ? "ERROR" : "OK"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 772 | |
| 773 | return ret == 0 ? 0 : 1; |
| 774 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 775 | |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 776 | #ifdef CONFIG_CMD_NAND_TORTURE |
| 777 | if (strcmp(cmd, "torture") == 0) { |
Max Krummenacher | 1866be7 | 2016-06-13 10:15:48 +0200 | [diff] [blame] | 778 | loff_t endoff; |
| 779 | unsigned int failed = 0, passed = 0; |
| 780 | |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 781 | if (argc < 3) |
| 782 | goto usage; |
| 783 | |
| 784 | if (!str2off(argv[2], &off)) { |
| 785 | puts("Offset is not a valid number\n"); |
| 786 | return 1; |
| 787 | } |
| 788 | |
Max Krummenacher | 1866be7 | 2016-06-13 10:15:48 +0200 | [diff] [blame] | 789 | size = mtd->erasesize; |
| 790 | if (argc > 3) { |
| 791 | if (!str2off(argv[3], &size)) { |
| 792 | puts("Size is not a valid number\n"); |
| 793 | return 1; |
| 794 | } |
| 795 | } |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 796 | |
Max Krummenacher | 1866be7 | 2016-06-13 10:15:48 +0200 | [diff] [blame] | 797 | endoff = off + size; |
| 798 | if (endoff > mtd->size) { |
| 799 | puts("Arguments beyond end of NAND\n"); |
| 800 | return 1; |
| 801 | } |
| 802 | |
| 803 | off = round_down(off, mtd->erasesize); |
| 804 | endoff = round_up(endoff, mtd->erasesize); |
| 805 | size = endoff - off; |
| 806 | printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n", |
| 807 | dev, off, size, mtd->erasesize); |
| 808 | while (off < endoff) { |
| 809 | ret = nand_torture(mtd, off); |
| 810 | if (ret) { |
| 811 | failed++; |
| 812 | printf(" block at 0x%llx failed\n", off); |
| 813 | } else { |
| 814 | passed++; |
| 815 | } |
| 816 | off += mtd->erasesize; |
| 817 | } |
| 818 | printf(" Passed: %u, failed: %u\n", passed, failed); |
| 819 | return failed != 0; |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 820 | } |
| 821 | #endif |
| 822 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 823 | if (strcmp(cmd, "markbad") == 0) { |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 824 | argc -= 2; |
| 825 | argv += 2; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 826 | |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 827 | if (argc <= 0) |
| 828 | goto usage; |
| 829 | |
| 830 | while (argc > 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 831 | addr = hextoul(*argv, NULL); |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 832 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 833 | if (mtd_block_markbad(mtd, addr)) { |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 834 | printf("block 0x%08lx NOT marked " |
| 835 | "as bad! ERROR %d\n", |
| 836 | addr, ret); |
| 837 | ret = 1; |
| 838 | } else { |
| 839 | printf("block 0x%08lx successfully " |
| 840 | "marked as bad\n", |
| 841 | addr); |
| 842 | } |
| 843 | --argc; |
| 844 | ++argv; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 845 | } |
Wolfgang Denk | 8360b66 | 2009-05-24 17:34:33 +0200 | [diff] [blame] | 846 | return ret; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 847 | } |
Scott Wood | dfbf617 | 2008-06-12 13:20:16 -0500 | [diff] [blame] | 848 | |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 849 | if (strcmp(cmd, "biterr") == 0) { |
Balamanikandan Gunasundar | 4ea84c5 | 2021-06-29 12:46:11 +0530 | [diff] [blame] | 850 | int bit; |
| 851 | |
| 852 | if (argc != 4) |
| 853 | goto usage; |
| 854 | |
| 855 | off = (int)simple_strtoul(argv[2], NULL, 16); |
| 856 | bit = (int)simple_strtoul(argv[3], NULL, 10); |
| 857 | ret = nand_biterror(mtd, off, bit); |
| 858 | return ret; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 861 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 862 | if (strcmp(cmd, "lock") == 0) { |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 863 | int tight = 0; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 864 | int status = 0; |
| 865 | if (argc == 3) { |
| 866 | if (!strcmp("tight", argv[2])) |
| 867 | tight = 1; |
| 868 | if (!strcmp("status", argv[2])) |
| 869 | status = 1; |
| 870 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 871 | if (status) { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 872 | do_nand_status(mtd); |
William Juul | cfa460a | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 873 | } else { |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 874 | if (!nand_lock(mtd, tight)) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 875 | puts("NAND flash successfully locked\n"); |
| 876 | } else { |
| 877 | puts("Error locking NAND flash\n"); |
| 878 | return 1; |
| 879 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 880 | } |
| 881 | return 0; |
| 882 | } |
| 883 | |
Joe Hershberger | eee623a | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 884 | if (strncmp(cmd, "unlock", 5) == 0) { |
| 885 | int allexcept = 0; |
| 886 | |
| 887 | s = strchr(cmd, '.'); |
| 888 | |
| 889 | if (s && !strcmp(s, ".allexcept")) |
| 890 | allexcept = 1; |
| 891 | |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 892 | if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size, |
| 893 | &maxsize, MTD_DEV_TYPE_NAND, |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 894 | mtd->size) < 0) |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 895 | return 1; |
| 896 | |
| 897 | if (set_dev(dev)) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 898 | return 1; |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 899 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 900 | mtd = get_nand_dev_by_index(dev); |
| 901 | |
| 902 | if (!nand_unlock(mtd, off, size, allexcept)) { |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 903 | puts("NAND flash successfully unlocked\n"); |
| 904 | } else { |
| 905 | puts("Error unlocking NAND flash, " |
William Juul | 3043c04 | 2007-11-14 14:28:11 +0100 | [diff] [blame] | 906 | "write and erase will probably fail\n"); |
William Juul | 5e1dae5 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 907 | return 1; |
| 908 | } |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 909 | return 0; |
| 910 | } |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 911 | #endif |
Stefan Roese | 2255b2d | 2006-10-10 12:36:02 +0200 | [diff] [blame] | 912 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 913 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 914 | return CMD_RET_USAGE; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 917 | #ifdef CONFIG_SYS_LONGHELP |
| 918 | static char nand_help_text[] = |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 919 | "info - show available NAND devices\n" |
| 920 | "nand device [dev] - show or set current device\n" |
| 921 | "nand read - addr off|partition size\n" |
| 922 | "nand write - addr off|partition size\n" |
| 923 | " read/write 'size' bytes starting at offset 'off'\n" |
| 924 | " to/from memory address 'addr', skipping bad blocks.\n" |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 925 | "nand read.raw - addr off|partition [count]\n" |
Boris Brezillon | 2dc3c483 | 2016-06-15 10:42:18 +0200 | [diff] [blame] | 926 | "nand write.raw[.noverify] - addr off|partition [count]\n" |
Scott Wood | 418396e | 2012-03-02 14:01:57 -0600 | [diff] [blame] | 927 | " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n" |
Ben Gardiner | c949486 | 2011-06-14 16:35:07 -0400 | [diff] [blame] | 928 | #ifdef CONFIG_CMD_NAND_TRIMFFS |
| 929 | "nand write.trimffs - addr off|partition size\n" |
| 930 | " write 'size' bytes starting at offset 'off' from memory address\n" |
| 931 | " 'addr', skipping bad blocks and dropping any pages at the end\n" |
| 932 | " of eraseblocks that contain only 0xFF\n" |
| 933 | #endif |
Daniel Hobi | eb3abce | 2011-05-19 19:28:54 +0200 | [diff] [blame] | 934 | "nand erase[.spread] [clean] off size - erase 'size' bytes " |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 935 | "from offset 'off'\n" |
| 936 | " With '.spread', erase enough for given file size, otherwise,\n" |
| 937 | " 'size' includes skipped bad blocks.\n" |
| 938 | "nand erase.part [clean] partition - erase entire mtd partition'\n" |
| 939 | "nand erase.chip [clean] - erase entire chip'\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 940 | "nand bad - show bad blocks\n" |
| 941 | "nand dump[.oob] off - dump page\n" |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 942 | #ifdef CONFIG_CMD_NAND_TORTURE |
Max Krummenacher | 1866be7 | 2016-06-13 10:15:48 +0200 | [diff] [blame] | 943 | "nand torture off - torture one block at offset\n" |
| 944 | "nand torture off [size] - torture blocks from off to off+size\n" |
Benoît Thébaudeau | 3287f6d | 2012-11-16 20:20:54 +0100 | [diff] [blame] | 945 | #endif |
Marek Vasut | 6089981 | 2011-09-14 00:20:35 +0200 | [diff] [blame] | 946 | "nand scrub [-y] off size | scrub.part partition | scrub.chip\n" |
Scott Wood | 3048632 | 2010-08-25 14:43:29 -0500 | [diff] [blame] | 947 | " really clean NAND erasing bad blocks (UNSAFE)\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 948 | "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" |
Balamanikandan Gunasundar | 4ea84c5 | 2021-06-29 12:46:11 +0530 | [diff] [blame] | 949 | "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)" |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 950 | #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 951 | "\n" |
| 952 | "nand lock [tight] [status]\n" |
| 953 | " bring nand to lock state or display locked pages\n" |
Joe Hershberger | eee623a | 2012-08-22 16:49:42 -0500 | [diff] [blame] | 954 | "nand unlock[.allexcept] [offset] [size] - unlock section" |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 955 | #endif |
Ben Gardiner | c9f7351 | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 956 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 957 | "\n" |
| 958 | "nand env.oob - environment offset in OOB of block 0 of" |
| 959 | " first device.\n" |
| 960 | "nand env.oob set off|partition - set enviromnent offset\n" |
| 961 | "nand env.oob get - get environment offset" |
| 962 | #endif |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 963 | ""; |
| 964 | #endif |
| 965 | |
| 966 | U_BOOT_CMD( |
| 967 | nand, CONFIG_SYS_MAXARGS, 1, do_nand, |
| 968 | "NAND sub-system", nand_help_text |
Nishanth Menon | 50657c2 | 2008-12-13 09:43:06 -0600 | [diff] [blame] | 969 | ); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 970 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 971 | static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd, |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 972 | ulong offset, ulong addr, char *cmd) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 973 | { |
| 974 | int r; |
Mike Frysinger | 67d668b | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 975 | char *s; |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 976 | size_t cnt; |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 977 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 978 | image_header_t *hdr; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 979 | #endif |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 980 | #if defined(CONFIG_FIT) |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 981 | const void *fit_hdr = NULL; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 982 | #endif |
Thomas Knobloch | 10e0389 | 2007-07-06 14:58:39 +0200 | [diff] [blame] | 983 | |
| 984 | s = strchr(cmd, '.'); |
| 985 | if (s != NULL && |
Scott Wood | 65d8bc9 | 2009-03-17 12:06:04 -0500 | [diff] [blame] | 986 | (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 987 | printf("Unknown nand load suffix '%s'\n", s); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 988 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
Scott Wood | 984e03c | 2008-06-12 13:13:23 -0500 | [diff] [blame] | 989 | return 1; |
| 990 | } |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 991 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 992 | printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 993 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 994 | cnt = mtd->writesize; |
| 995 | r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, |
| 996 | (u_char *)addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 997 | if (r) { |
| 998 | puts("** Read error\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 999 | bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1000 | return 1; |
| 1001 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1002 | bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1003 | |
Marian Balakowicz | 9a4daad | 2008-02-29 14:58:34 +0100 | [diff] [blame] | 1004 | switch (genimg_get_format ((void *)addr)) { |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 1005 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1006 | case IMAGE_FORMAT_LEGACY: |
| 1007 | hdr = (image_header_t *)addr; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1008 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1009 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1010 | image_print_contents (hdr); |
| 1011 | |
| 1012 | cnt = image_get_image_size (hdr); |
| 1013 | break; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 1014 | #endif |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1015 | #if defined(CONFIG_FIT) |
| 1016 | case IMAGE_FORMAT_FIT: |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1017 | fit_hdr = (const void *)addr; |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1018 | puts ("Fit image detected...\n"); |
| 1019 | |
| 1020 | cnt = fit_get_size (fit_hdr); |
| 1021 | break; |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1022 | #endif |
| 1023 | default: |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1024 | bootstage_error(BOOTSTAGE_ID_NAND_TYPE); |
Marian Balakowicz | d5934ad | 2008-02-04 08:28:09 +0100 | [diff] [blame] | 1025 | puts ("** Unknown image type\n"); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1026 | return 1; |
| 1027 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1028 | bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1029 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 1030 | r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, |
| 1031 | (u_char *)addr); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1032 | if (r) { |
| 1033 | puts("** Read error\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1034 | bootstage_error(BOOTSTAGE_ID_NAND_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1035 | return 1; |
| 1036 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1037 | bootstage_mark(BOOTSTAGE_ID_NAND_READ); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1038 | |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1039 | #if defined(CONFIG_FIT) |
| 1040 | /* This cannot be done earlier, we need complete FIT image in RAM first */ |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 1041 | if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { |
Simon Glass | c581970 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1042 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1043 | bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ); |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 1044 | puts ("** Bad FIT image format\n"); |
| 1045 | return 1; |
| 1046 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1047 | bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK); |
Marian Balakowicz | 3bab76a | 2008-06-06 23:07:40 +0200 | [diff] [blame] | 1048 | fit_print_contents (fit_hdr); |
| 1049 | } |
Marian Balakowicz | 09475f7 | 2008-03-12 10:33:01 +0100 | [diff] [blame] | 1050 | #endif |
| 1051 | |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1052 | /* Loading ok, update default load address */ |
| 1053 | |
Simon Glass | bb872dd | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 1054 | image_load_addr = addr; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1055 | |
Mike Frysinger | 67d668b | 2011-06-05 13:43:02 +0000 | [diff] [blame] | 1056 | return bootm_maybe_autostart(cmdtp, cmd); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1059 | static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1060 | char *const argv[]) |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1061 | { |
| 1062 | char *boot_device = NULL; |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1063 | int idx; |
| 1064 | ulong addr, offset = 0; |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 1065 | struct mtd_info *mtd; |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 1066 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1067 | struct mtd_device *dev; |
| 1068 | struct part_info *part; |
| 1069 | u8 pnum; |
| 1070 | |
| 1071 | if (argc >= 2) { |
| 1072 | char *p = (argc == 2) ? argv[1] : argv[2]; |
| 1073 | if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && |
| 1074 | (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { |
| 1075 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
| 1076 | puts("Not a NAND device\n"); |
| 1077 | return 1; |
| 1078 | } |
| 1079 | if (argc > 3) |
| 1080 | goto usage; |
| 1081 | if (argc == 3) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1082 | addr = hextoul(argv[1], NULL); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1083 | else |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1084 | addr = CONFIG_SYS_LOAD_ADDR; |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 1085 | |
| 1086 | mtd = get_nand_dev_by_index(dev->id->num); |
| 1087 | return nand_load_image(cmdtp, mtd, part->offset, |
| 1088 | addr, argv[0]); |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | #endif |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1092 | |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1093 | bootstage_mark(BOOTSTAGE_ID_NAND_PART); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1094 | switch (argc) { |
| 1095 | case 1: |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1096 | addr = CONFIG_SYS_LOAD_ADDR; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1097 | boot_device = env_get("bootdevice"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1098 | break; |
| 1099 | case 2: |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1100 | addr = hextoul(argv[1], NULL); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1101 | boot_device = env_get("bootdevice"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1102 | break; |
| 1103 | case 3: |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1104 | addr = hextoul(argv[1], NULL); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1105 | boot_device = argv[2]; |
| 1106 | break; |
| 1107 | case 4: |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1108 | addr = hextoul(argv[1], NULL); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1109 | boot_device = argv[2]; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1110 | offset = hextoul(argv[3], NULL); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1111 | break; |
| 1112 | default: |
Ladislav Michl | 0c8a849 | 2009-04-21 02:26:31 +0200 | [diff] [blame] | 1113 | #if defined(CONFIG_CMD_MTDPARTS) |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1114 | usage: |
| 1115 | #endif |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1116 | bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1117 | return CMD_RET_USAGE; |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1118 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1119 | bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1120 | |
| 1121 | if (!boot_device) { |
| 1122 | puts("\n** No boot device **\n"); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1123 | bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1124 | return 1; |
| 1125 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1126 | bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1127 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1128 | idx = hextoul(boot_device, NULL); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1129 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 1130 | mtd = get_nand_dev_by_index(idx); |
| 1131 | if (!mtd) { |
Stefan Roese | 856f054 | 2006-10-28 15:55:52 +0200 | [diff] [blame] | 1132 | printf("\n** Device %d not available\n", idx); |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1133 | bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1134 | return 1; |
| 1135 | } |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 1136 | bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1137 | |
Mugunthan V N | ad92dff | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 1138 | return nand_load_image(cmdtp, mtd, offset, addr, argv[0]); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | U_BOOT_CMD(nboot, 4, 1, do_nandboot, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1142 | "boot from NAND device", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1143 | "[partition] | [[[loadAddr] dev] offset]" |
| 1144 | ); |