blob: 92d039af8f52c6bf9ceb61bffbc58d713e52c1e8 [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
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>
wdenk384cc682005-04-03 22:35:21 +00006 *
Ben Gardinerc9f73512010-07-05 13:27:07 -04007 * 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.
wdenk384cc682005-04-03 22:35:21 +000011 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
Scott Woodea533c22010-08-02 13:04:24 -050013 *
Scott Wood418396e2012-03-02 14:01:57 -060014 * Copyright 2010, 2012 Freescale Semiconductor
Scott Woodea533c22010-08-02 13:04:24 -050015 * 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.
wdenkdc7c9a12003-03-26 06:55:25 +000020 */
21
22#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060023#include <bootstage.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070024#include <image.h>
Simon Glass90526e92020-05-10 11:39:56 -060025#include <asm/cache.h>
William Juulcfa460a2007-10-31 13:53:06 +010026#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010027#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070028#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060029#include <env.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010030#include <watchdog.h>
31#include <malloc.h>
32#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010033#include <jffs2/jffs2.h>
34#include <nand.h>
35
Miquel Raynaleb446ef2019-10-25 19:39:29 +020036#include "legacy-mtd-utils.h"
37
Ladislav Michl0c8a8492009-04-21 02:26:31 +020038#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020039
Wolfgang Denk445093d2009-11-17 21:27:39 +010040/* partition handling routines */
Stefan Roese856f0542006-10-28 15:55:52 +020041int mtdparts_init(void);
Stefan Roese856f0542006-10-28 15:55:52 +020042int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020043 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020044#endif
45
Scott Wood151c06e2016-05-30 13:57:54 -050046static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
47 int repeat)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010048{
49 int i;
William Juul9ad754f2007-12-14 16:33:45 +010050 u_char *datbuf, *oobbuf, *p;
Scott Wood8c5659a2010-08-25 15:24:01 -050051 static loff_t last;
Masahiro Yamadae40520b2013-07-11 17:27:12 +090052 int ret = 0;
Scott Wood8c5659a2010-08-25 15:24:01 -050053
54 if (repeat)
Scott Wood151c06e2016-05-30 13:57:54 -050055 off = last + mtd->writesize;
Scott Wood8c5659a2010-08-25 15:24:01 -050056
57 last = off;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058
Scott Wood151c06e2016-05-30 13:57:54 -050059 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090060 if (!datbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010061 puts("No memory for page buffer\n");
62 return 1;
63 }
Masahiro Yamadae40520b2013-07-11 17:27:12 +090064
Scott Wood151c06e2016-05-30 13:57:54 -050065 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090066 if (!oobbuf) {
67 puts("No memory for page buffer\n");
68 ret = 1;
69 goto free_dat;
70 }
Scott Wood151c06e2016-05-30 13:57:54 -050071 off &= ~(mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010072 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010073 struct mtd_oob_ops ops;
74 memset(&ops, 0, sizeof(ops));
75 ops.datbuf = datbuf;
Tom Rinicfdae122012-02-23 15:47:46 -070076 ops.oobbuf = oobbuf;
Scott Wood151c06e2016-05-30 13:57:54 -050077 ops.len = mtd->writesize;
78 ops.ooblen = mtd->oobsize;
Sergey Lapindfe64e22013-01-14 03:46:50 +000079 ops.mode = MTD_OPS_RAW;
Scott Wood151c06e2016-05-30 13:57:54 -050080 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010081 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020082 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090083 ret = 1;
84 goto free_all;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010085 }
Stefan Roesee8706902008-07-10 10:10:54 +020086 printf("Page %08lx dump:\n", off);
Wolfgang Denk4b070802008-08-14 14:41:06 +020087
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090088 if (!only_oob) {
Scott Wood151c06e2016-05-30 13:57:54 -050089 i = mtd->writesize >> 4;
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090090 p = datbuf;
91
92 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010093 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
94 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
95 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050096 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
97 p[15]);
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090098 p += 16;
99 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100100 }
Masahiro Yamada7d25cd32013-07-11 17:27:13 +0900101
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102 puts("OOB:\n");
Scott Wood151c06e2016-05-30 13:57:54 -0500103 i = mtd->oobsize >> 3;
Tom Rinicfdae122012-02-23 15:47:46 -0700104 p = oobbuf;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100105 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +0100106 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
107 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100108 p += 8;
109 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100110
Masahiro Yamadae40520b2013-07-11 17:27:12 +0900111free_all:
112 free(oobbuf);
113free_dat:
114 free(datbuf);
115
116 return ret;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100117}
118
119/* ------------------------------------------------------------------------- */
120
Scott Woodea533c22010-08-02 13:04:24 -0500121static int set_dev(int dev)
122{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500123 struct mtd_info *mtd = get_nand_dev_by_index(dev);
124
125 if (!mtd)
126 return -ENODEV;
Scott Woodea533c22010-08-02 13:04:24 -0500127
128 if (nand_curr_device == dev)
129 return 0;
130
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500131 printf("Device %d: %s", dev, mtd->name);
Scott Woodea533c22010-08-02 13:04:24 -0500132 puts("... is now current device\n");
133 nand_curr_device = dev;
134
135#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashko6308e712017-06-26 19:12:58 -0500136 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woodea533c22010-08-02 13:04:24 -0500137#endif
138
139 return 0;
140}
141
Nishanth Menon50657c22008-12-13 09:43:06 -0600142#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
143static void print_status(ulong start, ulong end, ulong erasesize, int status)
144{
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500145 /*
146 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
147 * not the same as others. Instead of bit 1 being lock, it is
148 * #lock_tight. To make the driver support either format, ignore bit 1
149 * and use only bit 0 and bit 2.
150 */
Nishanth Menon50657c22008-12-13 09:43:06 -0600151 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
152 start,
153 end - 1,
154 (end - start) / erasesize,
155 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500156 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menon50657c22008-12-13 09:43:06 -0600157 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
158}
159
Scott Wood151c06e2016-05-30 13:57:54 -0500160static void do_nand_status(struct mtd_info *mtd)
Nishanth Menon50657c22008-12-13 09:43:06 -0600161{
162 ulong block_start = 0;
163 ulong off;
164 int last_status = -1;
165
Scott Wood17cb4b82016-05-30 13:57:56 -0500166 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menon50657c22008-12-13 09:43:06 -0600167 /* check the WP bit */
Scott Wood151c06e2016-05-30 13:57:54 -0500168 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menon50657c22008-12-13 09:43:06 -0600169 printf("device is %swrite protected\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500170 (nand_chip->read_byte(mtd) & 0x80 ?
171 "NOT " : ""));
Nishanth Menon50657c22008-12-13 09:43:06 -0600172
Scott Wood151c06e2016-05-30 13:57:54 -0500173 for (off = 0; off < mtd->size; off += mtd->erasesize) {
174 int s = nand_get_lock_status(mtd, off);
Nishanth Menon50657c22008-12-13 09:43:06 -0600175
176 /* print message only if status has changed */
177 if (s != last_status && off != 0) {
Scott Wood151c06e2016-05-30 13:57:54 -0500178 print_status(block_start, off, mtd->erasesize,
Nishanth Menon50657c22008-12-13 09:43:06 -0600179 last_status);
180 block_start = off;
181 }
182 last_status = s;
183 }
184 /* Print the last block info */
Scott Wood151c06e2016-05-30 13:57:54 -0500185 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menon50657c22008-12-13 09:43:06 -0600186}
187#endif
188
Ben Gardinerc9f73512010-07-05 13:27:07 -0400189#ifdef CONFIG_ENV_OFFSET_OOB
190unsigned long nand_env_oob_offset;
191
Simon Glass09140112020-05-10 11:40:03 -0600192int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
Ben Gardinerc9f73512010-07-05 13:27:07 -0400193{
194 int ret;
195 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500196 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400197 char *cmd = argv[1];
198
Scott Wood8b7d5122016-09-01 17:31:31 -0500199 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woodea533c22010-08-02 13:04:24 -0500200 puts("no devices available\n");
201 return 1;
202 }
203
204 set_dev(0);
205
Ben Gardinerc9f73512010-07-05 13:27:07 -0400206 if (!strcmp(cmd, "get")) {
Scott Wood151c06e2016-05-30 13:57:54 -0500207 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500208 if (ret)
Ben Gardinerc9f73512010-07-05 13:27:07 -0400209 return 1;
Scott Wood53504a22010-07-12 18:17:40 -0500210
211 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400212 } else if (!strcmp(cmd, "set")) {
Scott Woodea533c22010-08-02 13:04:24 -0500213 loff_t addr;
214 loff_t maxsize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400215 struct mtd_oob_ops ops;
Scott Woodea533c22010-08-02 13:04:24 -0500216 int idx = 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400217
218 if (argc < 3)
219 goto usage;
220
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500221 mtd = get_nand_dev_by_index(idx);
Tom Rinic39d6a02013-03-14 05:32:50 +0000222 /* We don't care about size, or maxsize. */
Heiko Schocher09c32802015-04-27 07:42:05 +0200223 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500224 MTD_DEV_TYPE_NAND, mtd->size)) {
Heiko Schocher09c32802015-04-27 07:42:05 +0200225 puts("Offset or partition name expected\n");
226 return 1;
227 }
228 if (set_dev(idx)) {
Scott Woodea533c22010-08-02 13:04:24 -0500229 puts("Offset or partition name expected\n");
230 return 1;
231 }
232
233 if (idx != 0) {
234 puts("Partition not on first NAND device\n");
Ben Gardinerc9f73512010-07-05 13:27:07 -0400235 return 1;
236 }
237
Scott Wood151c06e2016-05-30 13:57:54 -0500238 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood53504a22010-07-12 18:17:40 -0500239 printf("Insufficient available OOB bytes:\n"
240 "%d OOB bytes available but %d required for "
241 "env.oob support\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500242 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400243 return 1;
244 }
245
Scott Wood151c06e2016-05-30 13:57:54 -0500246 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400247 printf("Environment offset must be block-aligned\n");
248 return 1;
249 }
250
251 ops.datbuf = NULL;
252 ops.mode = MTD_OOB_AUTO;
253 ops.ooboffs = 0;
254 ops.ooblen = ENV_OFFSET_SIZE;
255 ops.oobbuf = (void *) oob_buf;
256
257 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood151c06e2016-05-30 13:57:54 -0500258 oob_buf[1] = addr / mtd->erasesize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400259
Scott Wood151c06e2016-05-30 13:57:54 -0500260 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500261 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400262 printf("Error writing OOB block 0\n");
263 return ret;
264 }
Scott Wood53504a22010-07-12 18:17:40 -0500265
Scott Wood151c06e2016-05-30 13:57:54 -0500266 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500267 if (ret) {
268 printf("Error reading env offset in OOB\n");
269 return ret;
270 }
271
272 if (addr != nand_env_oob_offset) {
273 printf("Verification of env offset in OOB failed: "
Scott Woodea533c22010-08-02 13:04:24 -0500274 "0x%08llx expected but got 0x%08lx\n",
275 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500276 return 1;
277 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400278 } else {
279 goto usage;
280 }
281
282 return ret;
283
284usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000285 return CMD_RET_USAGE;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400286}
287
288#endif
289
Marek Vasutce80ddc2011-09-22 03:57:26 +0200290static void nand_print_and_set_info(int idx)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100291{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500292 struct mtd_info *mtd;
293 struct nand_chip *chip;
Marek Vasutce80ddc2011-09-22 03:57:26 +0200294
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500295 mtd = get_nand_dev_by_index(idx);
296 if (!mtd)
297 return;
298
299 chip = mtd_to_nand(mtd);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100300 printf("Device %d: ", idx);
301 if (chip->numchips > 1)
302 printf("%dx ", chip->numchips);
303 printf("%s, sector size %u KiB\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500304 mtd->name, mtd->erasesize >> 10);
305 printf(" Page size %8d b\n", mtd->writesize);
306 printf(" OOB size %8d b\n", mtd->oobsize);
307 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocher5db73fe2015-04-12 10:18:09 +0200308 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmann66dc09c2017-07-10 08:44:24 +0200309 printf(" options 0x%08x\n", chip->options);
310 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasutce80ddc2011-09-22 03:57:26 +0200311
312 /* Set geometry info */
Simon Glass018f5302017-08-03 12:22:10 -0600313 env_set_hex("nand_writesize", mtd->writesize);
314 env_set_hex("nand_oobsize", mtd->oobsize);
315 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100316}
317
Scott Wood151c06e2016-05-30 13:57:54 -0500318static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200319 ulong count, int read, int no_verify)
Scott Wood418396e2012-03-02 14:01:57 -0600320{
321 int ret = 0;
Scott Wood418396e2012-03-02 14:01:57 -0600322
323 while (count--) {
324 /* Raw access */
325 mtd_oob_ops_t ops = {
326 .datbuf = (u8 *)addr,
Scott Wood151c06e2016-05-30 13:57:54 -0500327 .oobbuf = ((u8 *)addr) + mtd->writesize,
328 .len = mtd->writesize,
329 .ooblen = mtd->oobsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000330 .mode = MTD_OPS_RAW
Scott Wood418396e2012-03-02 14:01:57 -0600331 };
332
Peter Tyser6b94f112015-02-03 11:58:13 -0600333 if (read) {
Scott Wood151c06e2016-05-30 13:57:54 -0500334 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser6b94f112015-02-03 11:58:13 -0600335 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500336 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200337 if (!ret && !no_verify)
Scott Wood151c06e2016-05-30 13:57:54 -0500338 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser6b94f112015-02-03 11:58:13 -0600339 }
Scott Wood418396e2012-03-02 14:01:57 -0600340
341 if (ret) {
342 printf("%s: error at offset %llx, ret %d\n",
343 __func__, (long long)off, ret);
344 break;
345 }
346
Scott Wood151c06e2016-05-30 13:57:54 -0500347 addr += mtd->writesize + mtd->oobsize;
348 off += mtd->writesize;
Scott Wood418396e2012-03-02 14:01:57 -0600349 }
350
351 return ret;
352}
353
Harvey Chapmane8344022013-02-26 17:57:14 +0000354/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood9b80aa82013-06-20 12:45:31 -0500355 * read/write past the end of a chip/partition by accident.
Harvey Chapmane8344022013-02-26 17:57:14 +0000356 */
357static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
358{
359 /* We grab the nand info object here fresh because this is usually
360 * called after arg_off_size() which can change the value of dev.
361 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500362 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapmane8344022013-02-26 17:57:14 +0000363 loff_t maxoffset = offset + *size;
364 int badblocks = 0;
365
366 /* count badblocks in NAND from offset to offset + size */
Scott Wood151c06e2016-05-30 13:57:54 -0500367 for (; offset < maxoffset; offset += mtd->erasesize) {
368 if (nand_block_isbad(mtd, offset))
Harvey Chapmane8344022013-02-26 17:57:14 +0000369 badblocks++;
370 }
371 /* adjust size if any bad blocks found */
372 if (badblocks) {
Scott Wood151c06e2016-05-30 13:57:54 -0500373 *size -= badblocks * mtd->erasesize;
Harvey Chapmane8344022013-02-26 17:57:14 +0000374 printf("size adjusted to 0x%llx (%d bad blocks)\n",
375 (unsigned long long)*size, badblocks);
376 }
377}
378
Simon Glass09140112020-05-10 11:40:03 -0600379static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
380 char *const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100381{
Scott Woodea533c22010-08-02 13:04:24 -0500382 int i, ret = 0;
383 ulong addr;
Tom Rinic39d6a02013-03-14 05:32:50 +0000384 loff_t off, size, maxsize;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100385 char *cmd, *s;
Scott Wood151c06e2016-05-30 13:57:54 -0500386 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200387#ifdef CONFIG_SYS_NAND_QUIET
388 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200389#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200390 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200391#endif
Simon Glass00caae62017-08-03 12:22:12 -0600392 const char *quiet_str = env_get("quiet");
Scott Woodea533c22010-08-02 13:04:24 -0500393 int dev = nand_curr_device;
Scott Wood8c5659a2010-08-25 15:24:01 -0500394 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100395
396 /* at least two arguments please */
397 if (argc < 2)
398 goto usage;
399
Stefan Roese2255b2d2006-10-10 12:36:02 +0200400 if (quiet_str)
401 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
402
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100403 cmd = argv[1];
404
Scott Wood8c5659a2010-08-25 15:24:01 -0500405 /* Only "dump" is repeatable. */
406 if (repeat && strcmp(cmd, "dump"))
407 return 0;
408
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100409 if (strcmp(cmd, "info") == 0) {
410
411 putc('\n');
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500412 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
413 nand_print_and_set_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100414 return 0;
415 }
416
417 if (strcmp(cmd, "device") == 0) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100418 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100419 putc('\n');
Scott Woodea533c22010-08-02 13:04:24 -0500420 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100421 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100422 else
Marek Vasutce80ddc2011-09-22 03:57:26 +0200423 nand_print_and_set_info(dev);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100424 return 0;
425 }
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200426
Scott Woodea533c22010-08-02 13:04:24 -0500427 dev = (int)simple_strtoul(argv[2], NULL, 10);
428 set_dev(dev);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200429
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100430 return 0;
431 }
432
Ben Gardinerc9f73512010-07-05 13:27:07 -0400433#ifdef CONFIG_ENV_OFFSET_OOB
434 /* this command operates only on the first nand device */
Scott Woodea533c22010-08-02 13:04:24 -0500435 if (strcmp(cmd, "env.oob") == 0)
436 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400437#endif
438
Scott Woodea533c22010-08-02 13:04:24 -0500439 /* The following commands operate on the current device, unless
440 * overridden by a partition specifier. Note that if somehow the
441 * current device is invalid, it will have to be changed to a valid
442 * one before these commands can run, even if a partition specifier
443 * for another device is to be used.
444 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500445 mtd = get_nand_dev_by_index(dev);
446 if (!mtd) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100447 puts("\nno devices available\n");
448 return 1;
449 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100450
451 if (strcmp(cmd, "bad") == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500452 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood151c06e2016-05-30 13:57:54 -0500453 for (off = 0; off < mtd->size; off += mtd->erasesize)
454 if (nand_block_isbad(mtd, off))
Scott Woodea533c22010-08-02 13:04:24 -0500455 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100456 return 0;
457 }
458
Stefan Roese856f0542006-10-28 15:55:52 +0200459 /*
460 * Syntax is:
461 * 0 1 2 3 4
462 * nand erase [clean] [off size]
463 */
Scott Wood30486322010-08-25 14:43:29 -0500464 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200465 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200466 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100467 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasut60899812011-09-14 00:20:35 +0200468 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
469 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood30486322010-08-25 14:43:29 -0500470 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood30486322010-08-25 14:43:29 -0500471 int spread = 0;
472 int args = 2;
Marek Vasut60899812011-09-14 00:20:35 +0200473 const char *scrub_warn =
474 "Warning: "
475 "scrub option will erase all factory set bad blocks!\n"
476 " "
477 "There is no reliable way to recover them.\n"
478 " "
479 "Use this command only for testing purposes if you\n"
480 " "
481 "are sure of what you are doing!\n"
482 "\nReally scrub this NAND flash? <y/N>\n";
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100483
Scott Wood30486322010-08-25 14:43:29 -0500484 if (cmd[5] != 0) {
485 if (!strcmp(&cmd[5], ".spread")) {
486 spread = 1;
487 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood30486322010-08-25 14:43:29 -0500488 args = 1;
489 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood30486322010-08-25 14:43:29 -0500490 args = 0;
491 } else {
492 goto usage;
493 }
494 }
495
496 /*
497 * Don't allow missing arguments to cause full chip/partition
498 * erases -- easy to do accidentally, e.g. with a misspelled
499 * variable name.
500 */
501 if (argc != o + args)
502 goto usage;
503
504 printf("\nNAND %s: ", cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200505 /* skip first two or three arguments, look for offset and size */
Heiko Schocher09c32802015-04-27 07:42:05 +0200506 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
507 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500508 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200509 return 1;
510
511 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200512 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200513
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500514 mtd = get_nand_dev_by_index(dev);
Scott Woodea533c22010-08-02 13:04:24 -0500515
Stefan Roese2255b2d2006-10-10 12:36:02 +0200516 memset(&opts, 0, sizeof(opts));
517 opts.offset = off;
518 opts.length = size;
519 opts.jffs2 = clean;
520 opts.quiet = quiet;
Scott Wood30486322010-08-25 14:43:29 -0500521 opts.spread = spread;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200522
523 if (scrub) {
Pierre Auberta5dffa42014-04-24 10:30:07 +0200524 if (scrub_yes) {
Marek Vasut60899812011-09-14 00:20:35 +0200525 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200526 } else {
527 puts(scrub_warn);
528 if (confirm_yesno()) {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100529 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200530 } else {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100531 puts("scrub aborted\n");
Masahiro Yamada46aabcc2013-07-11 17:29:57 +0900532 return 1;
Florian Fainelli6b94b492010-03-20 19:02:58 +0100533 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200534 }
535 }
Scott Wood151c06e2016-05-30 13:57:54 -0500536 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100537 printf("%s\n", ret ? "ERROR" : "OK");
538
539 return ret == 0 ? 0 : 1;
540 }
541
542 if (strncmp(cmd, "dump", 4) == 0) {
543 if (argc < 3)
544 goto usage;
545
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100546 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood151c06e2016-05-30 13:57:54 -0500547 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100548
549 return ret == 0 ? 1 : 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100550 }
551
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100552 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500553 size_t rwsize;
Scott Wood418396e2012-03-02 14:01:57 -0600554 ulong pagecount = 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200555 int read;
Harvey Chapmanced199d2013-02-07 11:34:44 +0000556 int raw = 0;
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200557 int no_verify = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200558
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100559 if (argc < 4)
560 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200561
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100562 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
563
Stefan Roese2255b2d2006-10-10 12:36:02 +0200564 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200565 printf("\nNAND %s: ", read ? "read" : "write");
William Juul4cbb6512007-11-08 10:39:53 +0100566
Stefan Roese2255b2d2006-10-10 12:36:02 +0200567 s = strchr(cmd, '.');
Scott Wood418396e2012-03-02 14:01:57 -0600568
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200569 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood418396e2012-03-02 14:01:57 -0600570 raw = 1;
571
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200572 if (!strcmp(s, ".raw.noverify"))
573 no_verify = 1;
574
Heiko Schocher09c32802015-04-27 07:42:05 +0200575 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
576 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500577 mtd->size))
Heiko Schocher09c32802015-04-27 07:42:05 +0200578 return 1;
579
580 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600581 return 1;
582
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500583 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200584
Scott Wood418396e2012-03-02 14:01:57 -0600585 if (argc > 4 && !str2long(argv[4], &pagecount)) {
586 printf("'%s' is not a number\n", argv[4]);
587 return 1;
588 }
589
Scott Wood151c06e2016-05-30 13:57:54 -0500590 if (pagecount * mtd->writesize > size) {
Scott Wood418396e2012-03-02 14:01:57 -0600591 puts("Size exceeds partition or device limit\n");
592 return -1;
593 }
594
Scott Wood151c06e2016-05-30 13:57:54 -0500595 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood418396e2012-03-02 14:01:57 -0600596 } else {
Heiko Schocher09c32802015-04-27 07:42:05 +0200597 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
598 &size, &maxsize,
599 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500600 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200601 return 1;
602
603 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600604 return 1;
605
Harvey Chapmane8344022013-02-26 17:57:14 +0000606 /* size is unspecified */
607 if (argc < 5)
608 adjust_size_for_badblocks(&size, off, dev);
Scott Wood418396e2012-03-02 14:01:57 -0600609 rwsize = size;
610 }
611
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500612 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200613
Scott Wood984e03c2008-06-12 13:13:23 -0500614 if (!s || !strcmp(s, ".jffs2") ||
615 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500616 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500617 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000618 NULL, maxsize,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200619 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500620 else
Scott Wood151c06e2016-05-30 13:57:54 -0500621 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000622 NULL, maxsize,
Peter Tyser6b94f112015-02-03 11:58:13 -0600623 (u_char *)addr,
624 WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400625#ifdef CONFIG_CMD_NAND_TRIMFFS
626 } else if (!strcmp(s, ".trimffs")) {
627 if (read) {
628 printf("Unknown nand command suffix '%s'\n", s);
629 return 1;
630 }
Scott Wood151c06e2016-05-30 13:57:54 -0500631 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rinic39d6a02013-03-14 05:32:50 +0000632 maxsize, (u_char *)addr,
Peter Tyser6b94f112015-02-03 11:58:13 -0600633 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400634#endif
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400635 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100636 /* out-of-band data */
637 mtd_oob_ops_t ops = {
638 .oobbuf = (u8 *)addr,
Scott Woodea533c22010-08-02 13:04:24 -0500639 .ooblen = rwsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000640 .mode = MTD_OPS_RAW
William Juulcfa460a2007-10-31 13:53:06 +0100641 };
642
Harald Welte62d4f432007-12-19 14:12:53 +0100643 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500644 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100645 else
Scott Wood151c06e2016-05-30 13:57:54 -0500646 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood418396e2012-03-02 14:01:57 -0600647 } else if (raw) {
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200648 ret = raw_access(mtd, addr, off, pagecount, read,
649 no_verify);
Stefan Roese856f0542006-10-28 15:55:52 +0200650 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500651 printf("Unknown nand command suffix '%s'.\n", s);
652 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200653 }
654
Scott Woodea533c22010-08-02 13:04:24 -0500655 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200656 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657
658 return ret == 0 ? 0 : 1;
659 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200660
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100661#ifdef CONFIG_CMD_NAND_TORTURE
662 if (strcmp(cmd, "torture") == 0) {
Max Krummenacher1866be72016-06-13 10:15:48 +0200663 loff_t endoff;
664 unsigned int failed = 0, passed = 0;
665
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100666 if (argc < 3)
667 goto usage;
668
669 if (!str2off(argv[2], &off)) {
670 puts("Offset is not a valid number\n");
671 return 1;
672 }
673
Max Krummenacher1866be72016-06-13 10:15:48 +0200674 size = mtd->erasesize;
675 if (argc > 3) {
676 if (!str2off(argv[3], &size)) {
677 puts("Size is not a valid number\n");
678 return 1;
679 }
680 }
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100681
Max Krummenacher1866be72016-06-13 10:15:48 +0200682 endoff = off + size;
683 if (endoff > mtd->size) {
684 puts("Arguments beyond end of NAND\n");
685 return 1;
686 }
687
688 off = round_down(off, mtd->erasesize);
689 endoff = round_up(endoff, mtd->erasesize);
690 size = endoff - off;
691 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
692 dev, off, size, mtd->erasesize);
693 while (off < endoff) {
694 ret = nand_torture(mtd, off);
695 if (ret) {
696 failed++;
697 printf(" block at 0x%llx failed\n", off);
698 } else {
699 passed++;
700 }
701 off += mtd->erasesize;
702 }
703 printf(" Passed: %u, failed: %u\n", passed, failed);
704 return failed != 0;
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100705 }
706#endif
707
Stefan Roese2255b2d2006-10-10 12:36:02 +0200708 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200709 argc -= 2;
710 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200711
Wolfgang Denk8360b662009-05-24 17:34:33 +0200712 if (argc <= 0)
713 goto usage;
714
715 while (argc > 0) {
716 addr = simple_strtoul(*argv, NULL, 16);
717
Scott Wood151c06e2016-05-30 13:57:54 -0500718 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200719 printf("block 0x%08lx NOT marked "
720 "as bad! ERROR %d\n",
721 addr, ret);
722 ret = 1;
723 } else {
724 printf("block 0x%08lx successfully "
725 "marked as bad\n",
726 addr);
727 }
728 --argc;
729 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200730 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200731 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200732 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500733
Stefan Roese2255b2d2006-10-10 12:36:02 +0200734 if (strcmp(cmd, "biterr") == 0) {
735 /* todo */
736 return 1;
737 }
738
Nishanth Menon50657c22008-12-13 09:43:06 -0600739#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200740 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600741 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200742 int status = 0;
743 if (argc == 3) {
744 if (!strcmp("tight", argv[2]))
745 tight = 1;
746 if (!strcmp("status", argv[2]))
747 status = 1;
748 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200749 if (status) {
Scott Wood151c06e2016-05-30 13:57:54 -0500750 do_nand_status(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100751 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500752 if (!nand_lock(mtd, tight)) {
William Juul5e1dae52007-11-09 13:32:30 +0100753 puts("NAND flash successfully locked\n");
754 } else {
755 puts("Error locking NAND flash\n");
756 return 1;
757 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200758 }
759 return 0;
760 }
761
Joe Hershbergereee623a2012-08-22 16:49:42 -0500762 if (strncmp(cmd, "unlock", 5) == 0) {
763 int allexcept = 0;
764
765 s = strchr(cmd, '.');
766
767 if (s && !strcmp(s, ".allexcept"))
768 allexcept = 1;
769
Heiko Schocher09c32802015-04-27 07:42:05 +0200770 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
771 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500772 mtd->size) < 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200773 return 1;
774
775 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200776 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200777
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500778 mtd = get_nand_dev_by_index(dev);
779
780 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul5e1dae52007-11-09 13:32:30 +0100781 puts("NAND flash successfully unlocked\n");
782 } else {
783 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100784 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100785 return 1;
786 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200787 return 0;
788 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600789#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200790
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100791usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000792 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100793}
794
Kim Phillips088f1b12012-10-29 13:34:31 +0000795#ifdef CONFIG_SYS_LONGHELP
796static char nand_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200797 "info - show available NAND devices\n"
798 "nand device [dev] - show or set current device\n"
799 "nand read - addr off|partition size\n"
800 "nand write - addr off|partition size\n"
801 " read/write 'size' bytes starting at offset 'off'\n"
802 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood418396e2012-03-02 14:01:57 -0600803 "nand read.raw - addr off|partition [count]\n"
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200804 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood418396e2012-03-02 14:01:57 -0600805 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerc9494862011-06-14 16:35:07 -0400806#ifdef CONFIG_CMD_NAND_TRIMFFS
807 "nand write.trimffs - addr off|partition size\n"
808 " write 'size' bytes starting at offset 'off' from memory address\n"
809 " 'addr', skipping bad blocks and dropping any pages at the end\n"
810 " of eraseblocks that contain only 0xFF\n"
811#endif
Daniel Hobieb3abce2011-05-19 19:28:54 +0200812 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood30486322010-08-25 14:43:29 -0500813 "from offset 'off'\n"
814 " With '.spread', erase enough for given file size, otherwise,\n"
815 " 'size' includes skipped bad blocks.\n"
816 "nand erase.part [clean] partition - erase entire mtd partition'\n"
817 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200818 "nand bad - show bad blocks\n"
819 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100820#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacher1866be72016-06-13 10:15:48 +0200821 "nand torture off - torture one block at offset\n"
822 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100823#endif
Marek Vasut60899812011-09-14 00:20:35 +0200824 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood30486322010-08-25 14:43:29 -0500825 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200826 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
827 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600828#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200829 "\n"
830 "nand lock [tight] [status]\n"
831 " bring nand to lock state or display locked pages\n"
Joe Hershbergereee623a2012-08-22 16:49:42 -0500832 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600833#endif
Ben Gardinerc9f73512010-07-05 13:27:07 -0400834#ifdef CONFIG_ENV_OFFSET_OOB
835 "\n"
836 "nand env.oob - environment offset in OOB of block 0 of"
837 " first device.\n"
838 "nand env.oob set off|partition - set enviromnent offset\n"
839 "nand env.oob get - get environment offset"
840#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000841 "";
842#endif
843
844U_BOOT_CMD(
845 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
846 "NAND sub-system", nand_help_text
Nishanth Menon50657c22008-12-13 09:43:06 -0600847);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100848
Simon Glass09140112020-05-10 11:40:03 -0600849static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200850 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200851{
852 int r;
Mike Frysinger67d668b2011-06-05 13:43:02 +0000853 char *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200854 size_t cnt;
Tom Rinic76c93a2019-05-23 07:14:07 -0400855#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Stefan Roese856f0542006-10-28 15:55:52 +0200856 image_header_t *hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200857#endif
Marian Balakowicz09475f72008-03-12 10:33:01 +0100858#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200859 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100860#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200861
862 s = strchr(cmd, '.');
863 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500864 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500865 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass770605e2012-02-13 13:51:18 +0000866 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood984e03c2008-06-12 13:13:23 -0500867 return 1;
868 }
Stefan Roese856f0542006-10-28 15:55:52 +0200869
Scott Wood151c06e2016-05-30 13:57:54 -0500870 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Stefan Roese856f0542006-10-28 15:55:52 +0200871
Scott Wood151c06e2016-05-30 13:57:54 -0500872 cnt = mtd->writesize;
873 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
874 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200875 if (r) {
876 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000877 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200878 return 1;
879 }
Simon Glass770605e2012-02-13 13:51:18 +0000880 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200881
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100882 switch (genimg_get_format ((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -0400883#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100884 case IMAGE_FORMAT_LEGACY:
885 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200886
Simon Glass770605e2012-02-13 13:51:18 +0000887 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100888 image_print_contents (hdr);
889
890 cnt = image_get_image_size (hdr);
891 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200892#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100893#if defined(CONFIG_FIT)
894 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100895 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100896 puts ("Fit image detected...\n");
897
898 cnt = fit_get_size (fit_hdr);
899 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100900#endif
901 default:
Simon Glass770605e2012-02-13 13:51:18 +0000902 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100903 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200904 return 1;
905 }
Simon Glass770605e2012-02-13 13:51:18 +0000906 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Stefan Roese856f0542006-10-28 15:55:52 +0200907
Scott Wood151c06e2016-05-30 13:57:54 -0500908 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
909 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200910 if (r) {
911 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000912 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200913 return 1;
914 }
Simon Glass770605e2012-02-13 13:51:18 +0000915 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200916
Marian Balakowicz09475f72008-03-12 10:33:01 +0100917#if defined(CONFIG_FIT)
918 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200919 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
920 if (!fit_check_format (fit_hdr)) {
Simon Glass770605e2012-02-13 13:51:18 +0000921 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200922 puts ("** Bad FIT image format\n");
923 return 1;
924 }
Simon Glass770605e2012-02-13 13:51:18 +0000925 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200926 fit_print_contents (fit_hdr);
927 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100928#endif
929
Stefan Roese856f0542006-10-28 15:55:52 +0200930 /* Loading ok, update default load address */
931
Simon Glassbb872dd2019-12-28 10:45:02 -0700932 image_load_addr = addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200933
Mike Frysinger67d668b2011-06-05 13:43:02 +0000934 return bootm_maybe_autostart(cmdtp, cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200935}
936
Simon Glass09140112020-05-10 11:40:03 -0600937static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
938 char *const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100939{
940 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200941 int idx;
942 ulong addr, offset = 0;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500943 struct mtd_info *mtd;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200944#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200945 struct mtd_device *dev;
946 struct part_info *part;
947 u8 pnum;
948
949 if (argc >= 2) {
950 char *p = (argc == 2) ? argv[1] : argv[2];
951 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
952 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
953 if (dev->id->type != MTD_DEV_TYPE_NAND) {
954 puts("Not a NAND device\n");
955 return 1;
956 }
957 if (argc > 3)
958 goto usage;
959 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200960 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200961 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200962 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500963
964 mtd = get_nand_dev_by_index(dev->id->num);
965 return nand_load_image(cmdtp, mtd, part->offset,
966 addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200967 }
968 }
969#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100970
Simon Glass770605e2012-02-13 13:51:18 +0000971 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100972 switch (argc) {
973 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200974 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass00caae62017-08-03 12:22:12 -0600975 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100976 break;
977 case 2:
978 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass00caae62017-08-03 12:22:12 -0600979 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100980 break;
981 case 3:
982 addr = simple_strtoul(argv[1], NULL, 16);
983 boot_device = argv[2];
984 break;
985 case 4:
986 addr = simple_strtoul(argv[1], NULL, 16);
987 boot_device = argv[2];
988 offset = simple_strtoul(argv[3], NULL, 16);
989 break;
990 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200991#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200992usage:
993#endif
Simon Glass770605e2012-02-13 13:51:18 +0000994 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000995 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100996 }
Simon Glass770605e2012-02-13 13:51:18 +0000997 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100998
999 if (!boot_device) {
1000 puts("\n** No boot device **\n");
Simon Glass770605e2012-02-13 13:51:18 +00001001 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001002 return 1;
1003 }
Simon Glass770605e2012-02-13 13:51:18 +00001004 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001005
Stefan Roese856f0542006-10-28 15:55:52 +02001006 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001007
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001008 mtd = get_nand_dev_by_index(idx);
1009 if (!mtd) {
Stefan Roese856f0542006-10-28 15:55:52 +02001010 printf("\n** Device %d not available\n", idx);
Simon Glass770605e2012-02-13 13:51:18 +00001011 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001012 return 1;
1013 }
Simon Glass770605e2012-02-13 13:51:18 +00001014 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001015
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001016 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001017}
1018
1019U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -06001020 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001021 "[partition] | [[[loadAddr] dev] offset]"
1022);