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