blob: 5bda69e819b0c4aa4ec16becba073866db3e7694 [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>
William Juulcfa460a2007-10-31 13:53:06 +010023#include <linux/mtd/mtd.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010024#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070025#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060026#include <env.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010027#include <watchdog.h>
28#include <malloc.h>
29#include <asm/byteorder.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010030#include <jffs2/jffs2.h>
31#include <nand.h>
32
Miquel Raynaleb446ef2019-10-25 19:39:29 +020033#include "legacy-mtd-utils.h"
34
Ladislav Michl0c8a8492009-04-21 02:26:31 +020035#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +020036
Wolfgang Denk445093d2009-11-17 21:27:39 +010037/* partition handling routines */
Stefan Roese856f0542006-10-28 15:55:52 +020038int mtdparts_init(void);
Stefan Roese856f0542006-10-28 15:55:52 +020039int find_dev_and_part(const char *id, struct mtd_device **dev,
Wolfgang Denk4b070802008-08-14 14:41:06 +020040 u8 *part_num, struct part_info **part);
Stefan Roese856f0542006-10-28 15:55:52 +020041#endif
42
Scott Wood151c06e2016-05-30 13:57:54 -050043static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
44 int repeat)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010045{
46 int i;
William Juul9ad754f2007-12-14 16:33:45 +010047 u_char *datbuf, *oobbuf, *p;
Scott Wood8c5659a2010-08-25 15:24:01 -050048 static loff_t last;
Masahiro Yamadae40520b2013-07-11 17:27:12 +090049 int ret = 0;
Scott Wood8c5659a2010-08-25 15:24:01 -050050
51 if (repeat)
Scott Wood151c06e2016-05-30 13:57:54 -050052 off = last + mtd->writesize;
Scott Wood8c5659a2010-08-25 15:24:01 -050053
54 last = off;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010055
Scott Wood151c06e2016-05-30 13:57:54 -050056 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090057 if (!datbuf) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058 puts("No memory for page buffer\n");
59 return 1;
60 }
Masahiro Yamadae40520b2013-07-11 17:27:12 +090061
Scott Wood151c06e2016-05-30 13:57:54 -050062 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090063 if (!oobbuf) {
64 puts("No memory for page buffer\n");
65 ret = 1;
66 goto free_dat;
67 }
Scott Wood151c06e2016-05-30 13:57:54 -050068 off &= ~(mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +010069 loff_t addr = (loff_t) off;
William Juul9ad754f2007-12-14 16:33:45 +010070 struct mtd_oob_ops ops;
71 memset(&ops, 0, sizeof(ops));
72 ops.datbuf = datbuf;
Tom Rinicfdae122012-02-23 15:47:46 -070073 ops.oobbuf = oobbuf;
Scott Wood151c06e2016-05-30 13:57:54 -050074 ops.len = mtd->writesize;
75 ops.ooblen = mtd->oobsize;
Sergey Lapindfe64e22013-01-14 03:46:50 +000076 ops.mode = MTD_OPS_RAW;
Scott Wood151c06e2016-05-30 13:57:54 -050077 i = mtd_read_oob(mtd, addr, &ops);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010078 if (i < 0) {
Stefan Roesee8706902008-07-10 10:10:54 +020079 printf("Error (%d) reading page %08lx\n", i, off);
Masahiro Yamadae40520b2013-07-11 17:27:12 +090080 ret = 1;
81 goto free_all;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010082 }
Stefan Roesee8706902008-07-10 10:10:54 +020083 printf("Page %08lx dump:\n", off);
Wolfgang Denk4b070802008-08-14 14:41:06 +020084
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090085 if (!only_oob) {
Scott Wood151c06e2016-05-30 13:57:54 -050086 i = mtd->writesize >> 4;
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090087 p = datbuf;
88
89 while (i--) {
William Juul9ad754f2007-12-14 16:33:45 +010090 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
91 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
92 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
Scott Wooddfbf6172008-06-12 13:20:16 -050093 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
94 p[15]);
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090095 p += 16;
96 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010097 }
Masahiro Yamada7d25cd32013-07-11 17:27:13 +090098
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010099 puts("OOB:\n");
Scott Wood151c06e2016-05-30 13:57:54 -0500100 i = mtd->oobsize >> 3;
Tom Rinicfdae122012-02-23 15:47:46 -0700101 p = oobbuf;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102 while (i--) {
William Juulcfa460a2007-10-31 13:53:06 +0100103 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
104 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100105 p += 8;
106 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100107
Masahiro Yamadae40520b2013-07-11 17:27:12 +0900108free_all:
109 free(oobbuf);
110free_dat:
111 free(datbuf);
112
113 return ret;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100114}
115
116/* ------------------------------------------------------------------------- */
117
Scott Woodea533c22010-08-02 13:04:24 -0500118static int set_dev(int dev)
119{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500120 struct mtd_info *mtd = get_nand_dev_by_index(dev);
121
122 if (!mtd)
123 return -ENODEV;
Scott Woodea533c22010-08-02 13:04:24 -0500124
125 if (nand_curr_device == dev)
126 return 0;
127
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500128 printf("Device %d: %s", dev, mtd->name);
Scott Woodea533c22010-08-02 13:04:24 -0500129 puts("... is now current device\n");
130 nand_curr_device = dev;
131
132#ifdef CONFIG_SYS_NAND_SELECT_DEVICE
Grygorii Strashko6308e712017-06-26 19:12:58 -0500133 board_nand_select_device(mtd_to_nand(mtd), dev);
Scott Woodea533c22010-08-02 13:04:24 -0500134#endif
135
136 return 0;
137}
138
Nishanth Menon50657c22008-12-13 09:43:06 -0600139#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
140static void print_status(ulong start, ulong end, ulong erasesize, int status)
141{
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500142 /*
143 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
144 * not the same as others. Instead of bit 1 being lock, it is
145 * #lock_tight. To make the driver support either format, ignore bit 1
146 * and use only bit 0 and bit 2.
147 */
Nishanth Menon50657c22008-12-13 09:43:06 -0600148 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
149 start,
150 end - 1,
151 (end - start) / erasesize,
152 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
Joe Hershbergere70bfa22012-08-22 16:49:45 -0500153 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
Nishanth Menon50657c22008-12-13 09:43:06 -0600154 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
155}
156
Scott Wood151c06e2016-05-30 13:57:54 -0500157static void do_nand_status(struct mtd_info *mtd)
Nishanth Menon50657c22008-12-13 09:43:06 -0600158{
159 ulong block_start = 0;
160 ulong off;
161 int last_status = -1;
162
Scott Wood17cb4b82016-05-30 13:57:56 -0500163 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Nishanth Menon50657c22008-12-13 09:43:06 -0600164 /* check the WP bit */
Scott Wood151c06e2016-05-30 13:57:54 -0500165 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Nishanth Menon50657c22008-12-13 09:43:06 -0600166 printf("device is %swrite protected\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500167 (nand_chip->read_byte(mtd) & 0x80 ?
168 "NOT " : ""));
Nishanth Menon50657c22008-12-13 09:43:06 -0600169
Scott Wood151c06e2016-05-30 13:57:54 -0500170 for (off = 0; off < mtd->size; off += mtd->erasesize) {
171 int s = nand_get_lock_status(mtd, off);
Nishanth Menon50657c22008-12-13 09:43:06 -0600172
173 /* print message only if status has changed */
174 if (s != last_status && off != 0) {
Scott Wood151c06e2016-05-30 13:57:54 -0500175 print_status(block_start, off, mtd->erasesize,
Nishanth Menon50657c22008-12-13 09:43:06 -0600176 last_status);
177 block_start = off;
178 }
179 last_status = s;
180 }
181 /* Print the last block info */
Scott Wood151c06e2016-05-30 13:57:54 -0500182 print_status(block_start, off, mtd->erasesize, last_status);
Nishanth Menon50657c22008-12-13 09:43:06 -0600183}
184#endif
185
Ben Gardinerc9f73512010-07-05 13:27:07 -0400186#ifdef CONFIG_ENV_OFFSET_OOB
187unsigned long nand_env_oob_offset;
188
Scott Woodea533c22010-08-02 13:04:24 -0500189int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
Ben Gardinerc9f73512010-07-05 13:27:07 -0400190{
191 int ret;
192 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500193 struct mtd_info *mtd = get_nand_dev_by_index(0);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400194 char *cmd = argv[1];
195
Scott Wood8b7d5122016-09-01 17:31:31 -0500196 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
Scott Woodea533c22010-08-02 13:04:24 -0500197 puts("no devices available\n");
198 return 1;
199 }
200
201 set_dev(0);
202
Ben Gardinerc9f73512010-07-05 13:27:07 -0400203 if (!strcmp(cmd, "get")) {
Scott Wood151c06e2016-05-30 13:57:54 -0500204 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500205 if (ret)
Ben Gardinerc9f73512010-07-05 13:27:07 -0400206 return 1;
Scott Wood53504a22010-07-12 18:17:40 -0500207
208 printf("0x%08lx\n", nand_env_oob_offset);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400209 } else if (!strcmp(cmd, "set")) {
Scott Woodea533c22010-08-02 13:04:24 -0500210 loff_t addr;
211 loff_t maxsize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400212 struct mtd_oob_ops ops;
Scott Woodea533c22010-08-02 13:04:24 -0500213 int idx = 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400214
215 if (argc < 3)
216 goto usage;
217
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500218 mtd = get_nand_dev_by_index(idx);
Tom Rinic39d6a02013-03-14 05:32:50 +0000219 /* We don't care about size, or maxsize. */
Heiko Schocher09c32802015-04-27 07:42:05 +0200220 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500221 MTD_DEV_TYPE_NAND, mtd->size)) {
Heiko Schocher09c32802015-04-27 07:42:05 +0200222 puts("Offset or partition name expected\n");
223 return 1;
224 }
225 if (set_dev(idx)) {
Scott Woodea533c22010-08-02 13:04:24 -0500226 puts("Offset or partition name expected\n");
227 return 1;
228 }
229
230 if (idx != 0) {
231 puts("Partition not on first NAND device\n");
Ben Gardinerc9f73512010-07-05 13:27:07 -0400232 return 1;
233 }
234
Scott Wood151c06e2016-05-30 13:57:54 -0500235 if (mtd->oobavail < ENV_OFFSET_SIZE) {
Scott Wood53504a22010-07-12 18:17:40 -0500236 printf("Insufficient available OOB bytes:\n"
237 "%d OOB bytes available but %d required for "
238 "env.oob support\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500239 mtd->oobavail, ENV_OFFSET_SIZE);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400240 return 1;
241 }
242
Scott Wood151c06e2016-05-30 13:57:54 -0500243 if ((addr & (mtd->erasesize - 1)) != 0) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400244 printf("Environment offset must be block-aligned\n");
245 return 1;
246 }
247
248 ops.datbuf = NULL;
249 ops.mode = MTD_OOB_AUTO;
250 ops.ooboffs = 0;
251 ops.ooblen = ENV_OFFSET_SIZE;
252 ops.oobbuf = (void *) oob_buf;
253
254 oob_buf[0] = ENV_OOB_MARKER;
Scott Wood151c06e2016-05-30 13:57:54 -0500255 oob_buf[1] = addr / mtd->erasesize;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400256
Scott Wood151c06e2016-05-30 13:57:54 -0500257 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500258 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400259 printf("Error writing OOB block 0\n");
260 return ret;
261 }
Scott Wood53504a22010-07-12 18:17:40 -0500262
Scott Wood151c06e2016-05-30 13:57:54 -0500263 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500264 if (ret) {
265 printf("Error reading env offset in OOB\n");
266 return ret;
267 }
268
269 if (addr != nand_env_oob_offset) {
270 printf("Verification of env offset in OOB failed: "
Scott Woodea533c22010-08-02 13:04:24 -0500271 "0x%08llx expected but got 0x%08lx\n",
272 (unsigned long long)addr, nand_env_oob_offset);
Scott Wood53504a22010-07-12 18:17:40 -0500273 return 1;
274 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400275 } else {
276 goto usage;
277 }
278
279 return ret;
280
281usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000282 return CMD_RET_USAGE;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400283}
284
285#endif
286
Marek Vasutce80ddc2011-09-22 03:57:26 +0200287static void nand_print_and_set_info(int idx)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100288{
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500289 struct mtd_info *mtd;
290 struct nand_chip *chip;
Marek Vasutce80ddc2011-09-22 03:57:26 +0200291
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500292 mtd = get_nand_dev_by_index(idx);
293 if (!mtd)
294 return;
295
296 chip = mtd_to_nand(mtd);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100297 printf("Device %d: ", idx);
298 if (chip->numchips > 1)
299 printf("%dx ", chip->numchips);
300 printf("%s, sector size %u KiB\n",
Scott Wood151c06e2016-05-30 13:57:54 -0500301 mtd->name, mtd->erasesize >> 10);
302 printf(" Page size %8d b\n", mtd->writesize);
303 printf(" OOB size %8d b\n", mtd->oobsize);
304 printf(" Erase size %8d b\n", mtd->erasesize);
Heiko Schocher5db73fe2015-04-12 10:18:09 +0200305 printf(" subpagesize %8d b\n", chip->subpagesize);
Lothar Waßmann66dc09c2017-07-10 08:44:24 +0200306 printf(" options 0x%08x\n", chip->options);
307 printf(" bbt options 0x%08x\n", chip->bbt_options);
Marek Vasutce80ddc2011-09-22 03:57:26 +0200308
309 /* Set geometry info */
Simon Glass018f5302017-08-03 12:22:10 -0600310 env_set_hex("nand_writesize", mtd->writesize);
311 env_set_hex("nand_oobsize", mtd->oobsize);
312 env_set_hex("nand_erasesize", mtd->erasesize);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100313}
314
Scott Wood151c06e2016-05-30 13:57:54 -0500315static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200316 ulong count, int read, int no_verify)
Scott Wood418396e2012-03-02 14:01:57 -0600317{
318 int ret = 0;
Scott Wood418396e2012-03-02 14:01:57 -0600319
320 while (count--) {
321 /* Raw access */
322 mtd_oob_ops_t ops = {
323 .datbuf = (u8 *)addr,
Scott Wood151c06e2016-05-30 13:57:54 -0500324 .oobbuf = ((u8 *)addr) + mtd->writesize,
325 .len = mtd->writesize,
326 .ooblen = mtd->oobsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000327 .mode = MTD_OPS_RAW
Scott Wood418396e2012-03-02 14:01:57 -0600328 };
329
Peter Tyser6b94f112015-02-03 11:58:13 -0600330 if (read) {
Scott Wood151c06e2016-05-30 13:57:54 -0500331 ret = mtd_read_oob(mtd, off, &ops);
Peter Tyser6b94f112015-02-03 11:58:13 -0600332 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500333 ret = mtd_write_oob(mtd, off, &ops);
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200334 if (!ret && !no_verify)
Scott Wood151c06e2016-05-30 13:57:54 -0500335 ret = nand_verify_page_oob(mtd, &ops, off);
Peter Tyser6b94f112015-02-03 11:58:13 -0600336 }
Scott Wood418396e2012-03-02 14:01:57 -0600337
338 if (ret) {
339 printf("%s: error at offset %llx, ret %d\n",
340 __func__, (long long)off, ret);
341 break;
342 }
343
Scott Wood151c06e2016-05-30 13:57:54 -0500344 addr += mtd->writesize + mtd->oobsize;
345 off += mtd->writesize;
Scott Wood418396e2012-03-02 14:01:57 -0600346 }
347
348 return ret;
349}
350
Harvey Chapmane8344022013-02-26 17:57:14 +0000351/* Adjust a chip/partition size down for bad blocks so we don't
Scott Wood9b80aa82013-06-20 12:45:31 -0500352 * read/write past the end of a chip/partition by accident.
Harvey Chapmane8344022013-02-26 17:57:14 +0000353 */
354static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
355{
356 /* We grab the nand info object here fresh because this is usually
357 * called after arg_off_size() which can change the value of dev.
358 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500359 struct mtd_info *mtd = get_nand_dev_by_index(dev);
Harvey Chapmane8344022013-02-26 17:57:14 +0000360 loff_t maxoffset = offset + *size;
361 int badblocks = 0;
362
363 /* count badblocks in NAND from offset to offset + size */
Scott Wood151c06e2016-05-30 13:57:54 -0500364 for (; offset < maxoffset; offset += mtd->erasesize) {
365 if (nand_block_isbad(mtd, offset))
Harvey Chapmane8344022013-02-26 17:57:14 +0000366 badblocks++;
367 }
368 /* adjust size if any bad blocks found */
369 if (badblocks) {
Scott Wood151c06e2016-05-30 13:57:54 -0500370 *size -= badblocks * mtd->erasesize;
Harvey Chapmane8344022013-02-26 17:57:14 +0000371 printf("size adjusted to 0x%llx (%d bad blocks)\n",
372 (unsigned long long)*size, badblocks);
373 }
374}
375
Kim Phillips088f1b12012-10-29 13:34:31 +0000376static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100377{
Scott Woodea533c22010-08-02 13:04:24 -0500378 int i, ret = 0;
379 ulong addr;
Tom Rinic39d6a02013-03-14 05:32:50 +0000380 loff_t off, size, maxsize;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100381 char *cmd, *s;
Scott Wood151c06e2016-05-30 13:57:54 -0500382 struct mtd_info *mtd;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200383#ifdef CONFIG_SYS_NAND_QUIET
384 int quiet = CONFIG_SYS_NAND_QUIET;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200385#else
Stefan Roese2255b2d2006-10-10 12:36:02 +0200386 int quiet = 0;
Matthias Fuchsc750d2e2007-09-12 12:36:53 +0200387#endif
Simon Glass00caae62017-08-03 12:22:12 -0600388 const char *quiet_str = env_get("quiet");
Scott Woodea533c22010-08-02 13:04:24 -0500389 int dev = nand_curr_device;
Scott Wood8c5659a2010-08-25 15:24:01 -0500390 int repeat = flag & CMD_FLAG_REPEAT;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100391
392 /* at least two arguments please */
393 if (argc < 2)
394 goto usage;
395
Stefan Roese2255b2d2006-10-10 12:36:02 +0200396 if (quiet_str)
397 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
398
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100399 cmd = argv[1];
400
Scott Wood8c5659a2010-08-25 15:24:01 -0500401 /* Only "dump" is repeatable. */
402 if (repeat && strcmp(cmd, "dump"))
403 return 0;
404
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100405 if (strcmp(cmd, "info") == 0) {
406
407 putc('\n');
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500408 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
409 nand_print_and_set_info(i);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100410 return 0;
411 }
412
413 if (strcmp(cmd, "device") == 0) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100414 if (argc < 3) {
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100415 putc('\n');
Scott Woodea533c22010-08-02 13:04:24 -0500416 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +0100417 puts("no devices available\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100418 else
Marek Vasutce80ddc2011-09-22 03:57:26 +0200419 nand_print_and_set_info(dev);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100420 return 0;
421 }
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200422
Scott Woodea533c22010-08-02 13:04:24 -0500423 dev = (int)simple_strtoul(argv[2], NULL, 10);
424 set_dev(dev);
Stefan Roese43a2b0e2006-10-20 14:28:52 +0200425
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100426 return 0;
427 }
428
Ben Gardinerc9f73512010-07-05 13:27:07 -0400429#ifdef CONFIG_ENV_OFFSET_OOB
430 /* this command operates only on the first nand device */
Scott Woodea533c22010-08-02 13:04:24 -0500431 if (strcmp(cmd, "env.oob") == 0)
432 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
Ben Gardinerc9f73512010-07-05 13:27:07 -0400433#endif
434
Scott Woodea533c22010-08-02 13:04:24 -0500435 /* The following commands operate on the current device, unless
436 * overridden by a partition specifier. Note that if somehow the
437 * current device is invalid, it will have to be changed to a valid
438 * one before these commands can run, even if a partition specifier
439 * for another device is to be used.
440 */
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500441 mtd = get_nand_dev_by_index(dev);
442 if (!mtd) {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100443 puts("\nno devices available\n");
444 return 1;
445 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100446
447 if (strcmp(cmd, "bad") == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500448 printf("\nDevice %d bad blocks:\n", dev);
Scott Wood151c06e2016-05-30 13:57:54 -0500449 for (off = 0; off < mtd->size; off += mtd->erasesize)
450 if (nand_block_isbad(mtd, off))
Scott Woodea533c22010-08-02 13:04:24 -0500451 printf(" %08llx\n", (unsigned long long)off);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100452 return 0;
453 }
454
Stefan Roese856f0542006-10-28 15:55:52 +0200455 /*
456 * Syntax is:
457 * 0 1 2 3 4
458 * nand erase [clean] [off size]
459 */
Scott Wood30486322010-08-25 14:43:29 -0500460 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
Stefan Roese2255b2d2006-10-10 12:36:02 +0200461 nand_erase_options_t opts;
Stefan Roese856f0542006-10-28 15:55:52 +0200462 /* "clean" at index 2 means request to write cleanmarker */
William Juul3043c042007-11-14 14:28:11 +0100463 int clean = argc > 2 && !strcmp("clean", argv[2]);
Marek Vasut60899812011-09-14 00:20:35 +0200464 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
465 int o = (clean || scrub_yes) ? 3 : 2;
Scott Wood30486322010-08-25 14:43:29 -0500466 int scrub = !strncmp(cmd, "scrub", 5);
Scott Wood30486322010-08-25 14:43:29 -0500467 int spread = 0;
468 int args = 2;
Marek Vasut60899812011-09-14 00:20:35 +0200469 const char *scrub_warn =
470 "Warning: "
471 "scrub option will erase all factory set bad blocks!\n"
472 " "
473 "There is no reliable way to recover them.\n"
474 " "
475 "Use this command only for testing purposes if you\n"
476 " "
477 "are sure of what you are doing!\n"
478 "\nReally scrub this NAND flash? <y/N>\n";
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100479
Scott Wood30486322010-08-25 14:43:29 -0500480 if (cmd[5] != 0) {
481 if (!strcmp(&cmd[5], ".spread")) {
482 spread = 1;
483 } else if (!strcmp(&cmd[5], ".part")) {
Scott Wood30486322010-08-25 14:43:29 -0500484 args = 1;
485 } else if (!strcmp(&cmd[5], ".chip")) {
Scott Wood30486322010-08-25 14:43:29 -0500486 args = 0;
487 } else {
488 goto usage;
489 }
490 }
491
492 /*
493 * Don't allow missing arguments to cause full chip/partition
494 * erases -- easy to do accidentally, e.g. with a misspelled
495 * variable name.
496 */
497 if (argc != o + args)
498 goto usage;
499
500 printf("\nNAND %s: ", cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200501 /* skip first two or three arguments, look for offset and size */
Heiko Schocher09c32802015-04-27 07:42:05 +0200502 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
503 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500504 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200505 return 1;
506
507 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200508 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200509
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500510 mtd = get_nand_dev_by_index(dev);
Scott Woodea533c22010-08-02 13:04:24 -0500511
Stefan Roese2255b2d2006-10-10 12:36:02 +0200512 memset(&opts, 0, sizeof(opts));
513 opts.offset = off;
514 opts.length = size;
515 opts.jffs2 = clean;
516 opts.quiet = quiet;
Scott Wood30486322010-08-25 14:43:29 -0500517 opts.spread = spread;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200518
519 if (scrub) {
Pierre Auberta5dffa42014-04-24 10:30:07 +0200520 if (scrub_yes) {
Marek Vasut60899812011-09-14 00:20:35 +0200521 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200522 } else {
523 puts(scrub_warn);
524 if (confirm_yesno()) {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100525 opts.scrub = 1;
Pierre Auberta5dffa42014-04-24 10:30:07 +0200526 } else {
Florian Fainelli6b94b492010-03-20 19:02:58 +0100527 puts("scrub aborted\n");
Masahiro Yamada46aabcc2013-07-11 17:29:57 +0900528 return 1;
Florian Fainelli6b94b492010-03-20 19:02:58 +0100529 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200530 }
531 }
Scott Wood151c06e2016-05-30 13:57:54 -0500532 ret = nand_erase_opts(mtd, &opts);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100533 printf("%s\n", ret ? "ERROR" : "OK");
534
535 return ret == 0 ? 0 : 1;
536 }
537
538 if (strncmp(cmd, "dump", 4) == 0) {
539 if (argc < 3)
540 goto usage;
541
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100542 off = (int)simple_strtoul(argv[2], NULL, 16);
Scott Wood151c06e2016-05-30 13:57:54 -0500543 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100544
545 return ret == 0 ? 1 : 0;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100546 }
547
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100548 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
Scott Woodea533c22010-08-02 13:04:24 -0500549 size_t rwsize;
Scott Wood418396e2012-03-02 14:01:57 -0600550 ulong pagecount = 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200551 int read;
Harvey Chapmanced199d2013-02-07 11:34:44 +0000552 int raw = 0;
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200553 int no_verify = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200554
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100555 if (argc < 4)
556 goto usage;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200557
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100558 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
559
Stefan Roese2255b2d2006-10-10 12:36:02 +0200560 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
Stefan Roese856f0542006-10-28 15:55:52 +0200561 printf("\nNAND %s: ", read ? "read" : "write");
William Juul4cbb6512007-11-08 10:39:53 +0100562
Stefan Roese2255b2d2006-10-10 12:36:02 +0200563 s = strchr(cmd, '.');
Scott Wood418396e2012-03-02 14:01:57 -0600564
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200565 if (s && !strncmp(s, ".raw", 4)) {
Scott Wood418396e2012-03-02 14:01:57 -0600566 raw = 1;
567
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200568 if (!strcmp(s, ".raw.noverify"))
569 no_verify = 1;
570
Heiko Schocher09c32802015-04-27 07:42:05 +0200571 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
572 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500573 mtd->size))
Heiko Schocher09c32802015-04-27 07:42:05 +0200574 return 1;
575
576 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600577 return 1;
578
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500579 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200580
Scott Wood418396e2012-03-02 14:01:57 -0600581 if (argc > 4 && !str2long(argv[4], &pagecount)) {
582 printf("'%s' is not a number\n", argv[4]);
583 return 1;
584 }
585
Scott Wood151c06e2016-05-30 13:57:54 -0500586 if (pagecount * mtd->writesize > size) {
Scott Wood418396e2012-03-02 14:01:57 -0600587 puts("Size exceeds partition or device limit\n");
588 return -1;
589 }
590
Scott Wood151c06e2016-05-30 13:57:54 -0500591 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
Scott Wood418396e2012-03-02 14:01:57 -0600592 } else {
Heiko Schocher09c32802015-04-27 07:42:05 +0200593 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
594 &size, &maxsize,
595 MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500596 mtd->size) != 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200597 return 1;
598
599 if (set_dev(dev))
Scott Wood418396e2012-03-02 14:01:57 -0600600 return 1;
601
Harvey Chapmane8344022013-02-26 17:57:14 +0000602 /* size is unspecified */
603 if (argc < 5)
604 adjust_size_for_badblocks(&size, off, dev);
Scott Wood418396e2012-03-02 14:01:57 -0600605 rwsize = size;
606 }
607
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500608 mtd = get_nand_dev_by_index(dev);
Rostislav Lisovy93d32322014-09-16 14:38:52 +0200609
Scott Wood984e03c2008-06-12 13:13:23 -0500610 if (!s || !strcmp(s, ".jffs2") ||
611 !strcmp(s, ".e") || !strcmp(s, ".i")) {
Scott Wooddfbf6172008-06-12 13:20:16 -0500612 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500613 ret = nand_read_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000614 NULL, maxsize,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200615 (u_char *)addr);
Scott Wooddfbf6172008-06-12 13:20:16 -0500616 else
Scott Wood151c06e2016-05-30 13:57:54 -0500617 ret = nand_write_skip_bad(mtd, off, &rwsize,
Tom Rinic39d6a02013-03-14 05:32:50 +0000618 NULL, maxsize,
Peter Tyser6b94f112015-02-03 11:58:13 -0600619 (u_char *)addr,
620 WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400621#ifdef CONFIG_CMD_NAND_TRIMFFS
622 } else if (!strcmp(s, ".trimffs")) {
623 if (read) {
624 printf("Unknown nand command suffix '%s'\n", s);
625 return 1;
626 }
Scott Wood151c06e2016-05-30 13:57:54 -0500627 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
Tom Rinic39d6a02013-03-14 05:32:50 +0000628 maxsize, (u_char *)addr,
Peter Tyser6b94f112015-02-03 11:58:13 -0600629 WITH_DROP_FFS | WITH_WR_VERIFY);
Ben Gardinerc9494862011-06-14 16:35:07 -0400630#endif
Mike Frysingerdfc99e12009-04-12 22:29:20 -0400631 } else if (!strcmp(s, ".oob")) {
William Juulcfa460a2007-10-31 13:53:06 +0100632 /* out-of-band data */
633 mtd_oob_ops_t ops = {
634 .oobbuf = (u8 *)addr,
Scott Woodea533c22010-08-02 13:04:24 -0500635 .ooblen = rwsize,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000636 .mode = MTD_OPS_RAW
William Juulcfa460a2007-10-31 13:53:06 +0100637 };
638
Harald Welte62d4f432007-12-19 14:12:53 +0100639 if (read)
Scott Wood151c06e2016-05-30 13:57:54 -0500640 ret = mtd_read_oob(mtd, off, &ops);
Harald Welte62d4f432007-12-19 14:12:53 +0100641 else
Scott Wood151c06e2016-05-30 13:57:54 -0500642 ret = mtd_write_oob(mtd, off, &ops);
Scott Wood418396e2012-03-02 14:01:57 -0600643 } else if (raw) {
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200644 ret = raw_access(mtd, addr, off, pagecount, read,
645 no_verify);
Stefan Roese856f0542006-10-28 15:55:52 +0200646 } else {
Scott Wood984e03c2008-06-12 13:13:23 -0500647 printf("Unknown nand command suffix '%s'.\n", s);
648 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200649 }
650
Scott Woodea533c22010-08-02 13:04:24 -0500651 printf(" %zu bytes %s: %s\n", rwsize,
Stefan Roese2255b2d2006-10-10 12:36:02 +0200652 read ? "read" : "written", ret ? "ERROR" : "OK");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100653
654 return ret == 0 ? 0 : 1;
655 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200656
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100657#ifdef CONFIG_CMD_NAND_TORTURE
658 if (strcmp(cmd, "torture") == 0) {
Max Krummenacher1866be72016-06-13 10:15:48 +0200659 loff_t endoff;
660 unsigned int failed = 0, passed = 0;
661
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100662 if (argc < 3)
663 goto usage;
664
665 if (!str2off(argv[2], &off)) {
666 puts("Offset is not a valid number\n");
667 return 1;
668 }
669
Max Krummenacher1866be72016-06-13 10:15:48 +0200670 size = mtd->erasesize;
671 if (argc > 3) {
672 if (!str2off(argv[3], &size)) {
673 puts("Size is not a valid number\n");
674 return 1;
675 }
676 }
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100677
Max Krummenacher1866be72016-06-13 10:15:48 +0200678 endoff = off + size;
679 if (endoff > mtd->size) {
680 puts("Arguments beyond end of NAND\n");
681 return 1;
682 }
683
684 off = round_down(off, mtd->erasesize);
685 endoff = round_up(endoff, mtd->erasesize);
686 size = endoff - off;
687 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
688 dev, off, size, mtd->erasesize);
689 while (off < endoff) {
690 ret = nand_torture(mtd, off);
691 if (ret) {
692 failed++;
693 printf(" block at 0x%llx failed\n", off);
694 } else {
695 passed++;
696 }
697 off += mtd->erasesize;
698 }
699 printf(" Passed: %u, failed: %u\n", passed, failed);
700 return failed != 0;
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100701 }
702#endif
703
Stefan Roese2255b2d2006-10-10 12:36:02 +0200704 if (strcmp(cmd, "markbad") == 0) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200705 argc -= 2;
706 argv += 2;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200707
Wolfgang Denk8360b662009-05-24 17:34:33 +0200708 if (argc <= 0)
709 goto usage;
710
711 while (argc > 0) {
712 addr = simple_strtoul(*argv, NULL, 16);
713
Scott Wood151c06e2016-05-30 13:57:54 -0500714 if (mtd_block_markbad(mtd, addr)) {
Wolfgang Denk8360b662009-05-24 17:34:33 +0200715 printf("block 0x%08lx NOT marked "
716 "as bad! ERROR %d\n",
717 addr, ret);
718 ret = 1;
719 } else {
720 printf("block 0x%08lx successfully "
721 "marked as bad\n",
722 addr);
723 }
724 --argc;
725 ++argv;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200726 }
Wolfgang Denk8360b662009-05-24 17:34:33 +0200727 return ret;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200728 }
Scott Wooddfbf6172008-06-12 13:20:16 -0500729
Stefan Roese2255b2d2006-10-10 12:36:02 +0200730 if (strcmp(cmd, "biterr") == 0) {
731 /* todo */
732 return 1;
733 }
734
Nishanth Menon50657c22008-12-13 09:43:06 -0600735#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Stefan Roese2255b2d2006-10-10 12:36:02 +0200736 if (strcmp(cmd, "lock") == 0) {
Nishanth Menon50657c22008-12-13 09:43:06 -0600737 int tight = 0;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200738 int status = 0;
739 if (argc == 3) {
740 if (!strcmp("tight", argv[2]))
741 tight = 1;
742 if (!strcmp("status", argv[2]))
743 status = 1;
744 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200745 if (status) {
Scott Wood151c06e2016-05-30 13:57:54 -0500746 do_nand_status(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100747 } else {
Scott Wood151c06e2016-05-30 13:57:54 -0500748 if (!nand_lock(mtd, tight)) {
William Juul5e1dae52007-11-09 13:32:30 +0100749 puts("NAND flash successfully locked\n");
750 } else {
751 puts("Error locking NAND flash\n");
752 return 1;
753 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200754 }
755 return 0;
756 }
757
Joe Hershbergereee623a2012-08-22 16:49:42 -0500758 if (strncmp(cmd, "unlock", 5) == 0) {
759 int allexcept = 0;
760
761 s = strchr(cmd, '.');
762
763 if (s && !strcmp(s, ".allexcept"))
764 allexcept = 1;
765
Heiko Schocher09c32802015-04-27 07:42:05 +0200766 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
767 &maxsize, MTD_DEV_TYPE_NAND,
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500768 mtd->size) < 0)
Heiko Schocher09c32802015-04-27 07:42:05 +0200769 return 1;
770
771 if (set_dev(dev))
Stefan Roese856f0542006-10-28 15:55:52 +0200772 return 1;
Stefan Roese2255b2d2006-10-10 12:36:02 +0200773
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500774 mtd = get_nand_dev_by_index(dev);
775
776 if (!nand_unlock(mtd, off, size, allexcept)) {
William Juul5e1dae52007-11-09 13:32:30 +0100777 puts("NAND flash successfully unlocked\n");
778 } else {
779 puts("Error unlocking NAND flash, "
William Juul3043c042007-11-14 14:28:11 +0100780 "write and erase will probably fail\n");
William Juul5e1dae52007-11-09 13:32:30 +0100781 return 1;
782 }
Stefan Roese2255b2d2006-10-10 12:36:02 +0200783 return 0;
784 }
Nishanth Menon50657c22008-12-13 09:43:06 -0600785#endif
Stefan Roese2255b2d2006-10-10 12:36:02 +0200786
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100787usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000788 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100789}
790
Kim Phillips088f1b12012-10-29 13:34:31 +0000791#ifdef CONFIG_SYS_LONGHELP
792static char nand_help_text[] =
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200793 "info - show available NAND devices\n"
794 "nand device [dev] - show or set current device\n"
795 "nand read - addr off|partition size\n"
796 "nand write - addr off|partition size\n"
797 " read/write 'size' bytes starting at offset 'off'\n"
798 " to/from memory address 'addr', skipping bad blocks.\n"
Scott Wood418396e2012-03-02 14:01:57 -0600799 "nand read.raw - addr off|partition [count]\n"
Boris Brezillon2dc3c4832016-06-15 10:42:18 +0200800 "nand write.raw[.noverify] - addr off|partition [count]\n"
Scott Wood418396e2012-03-02 14:01:57 -0600801 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
Ben Gardinerc9494862011-06-14 16:35:07 -0400802#ifdef CONFIG_CMD_NAND_TRIMFFS
803 "nand write.trimffs - addr off|partition size\n"
804 " write 'size' bytes starting at offset 'off' from memory address\n"
805 " 'addr', skipping bad blocks and dropping any pages at the end\n"
806 " of eraseblocks that contain only 0xFF\n"
807#endif
Daniel Hobieb3abce2011-05-19 19:28:54 +0200808 "nand erase[.spread] [clean] off size - erase 'size' bytes "
Scott Wood30486322010-08-25 14:43:29 -0500809 "from offset 'off'\n"
810 " With '.spread', erase enough for given file size, otherwise,\n"
811 " 'size' includes skipped bad blocks.\n"
812 "nand erase.part [clean] partition - erase entire mtd partition'\n"
813 "nand erase.chip [clean] - erase entire chip'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200814 "nand bad - show bad blocks\n"
815 "nand dump[.oob] off - dump page\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100816#ifdef CONFIG_CMD_NAND_TORTURE
Max Krummenacher1866be72016-06-13 10:15:48 +0200817 "nand torture off - torture one block at offset\n"
818 "nand torture off [size] - torture blocks from off to off+size\n"
Benoît Thébaudeau3287f6d2012-11-16 20:20:54 +0100819#endif
Marek Vasut60899812011-09-14 00:20:35 +0200820 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
Scott Wood30486322010-08-25 14:43:29 -0500821 " really clean NAND erasing bad blocks (UNSAFE)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200822 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
823 "nand biterr off - make a bit error at offset (UNSAFE)"
Nishanth Menon50657c22008-12-13 09:43:06 -0600824#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200825 "\n"
826 "nand lock [tight] [status]\n"
827 " bring nand to lock state or display locked pages\n"
Joe Hershbergereee623a2012-08-22 16:49:42 -0500828 "nand unlock[.allexcept] [offset] [size] - unlock section"
Nishanth Menon50657c22008-12-13 09:43:06 -0600829#endif
Ben Gardinerc9f73512010-07-05 13:27:07 -0400830#ifdef CONFIG_ENV_OFFSET_OOB
831 "\n"
832 "nand env.oob - environment offset in OOB of block 0 of"
833 " first device.\n"
834 "nand env.oob set off|partition - set enviromnent offset\n"
835 "nand env.oob get - get environment offset"
836#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000837 "";
838#endif
839
840U_BOOT_CMD(
841 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
842 "NAND sub-system", nand_help_text
Nishanth Menon50657c22008-12-13 09:43:06 -0600843);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100844
Scott Wood151c06e2016-05-30 13:57:54 -0500845static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200846 ulong offset, ulong addr, char *cmd)
Stefan Roese856f0542006-10-28 15:55:52 +0200847{
848 int r;
Mike Frysinger67d668b2011-06-05 13:43:02 +0000849 char *s;
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200850 size_t cnt;
Tom Rinic76c93a2019-05-23 07:14:07 -0400851#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Stefan Roese856f0542006-10-28 15:55:52 +0200852 image_header_t *hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200853#endif
Marian Balakowicz09475f72008-03-12 10:33:01 +0100854#if defined(CONFIG_FIT)
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200855 const void *fit_hdr = NULL;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100856#endif
Thomas Knobloch10e03892007-07-06 14:58:39 +0200857
858 s = strchr(cmd, '.');
859 if (s != NULL &&
Scott Wood65d8bc92009-03-17 12:06:04 -0500860 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
Scott Wood984e03c2008-06-12 13:13:23 -0500861 printf("Unknown nand load suffix '%s'\n", s);
Simon Glass770605e2012-02-13 13:51:18 +0000862 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Scott Wood984e03c2008-06-12 13:13:23 -0500863 return 1;
864 }
Stefan Roese856f0542006-10-28 15:55:52 +0200865
Scott Wood151c06e2016-05-30 13:57:54 -0500866 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
Stefan Roese856f0542006-10-28 15:55:52 +0200867
Scott Wood151c06e2016-05-30 13:57:54 -0500868 cnt = mtd->writesize;
869 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
870 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200871 if (r) {
872 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000873 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200874 return 1;
875 }
Simon Glass770605e2012-02-13 13:51:18 +0000876 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200877
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100878 switch (genimg_get_format ((void *)addr)) {
Tom Rinic76c93a2019-05-23 07:14:07 -0400879#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100880 case IMAGE_FORMAT_LEGACY:
881 hdr = (image_header_t *)addr;
Stefan Roese856f0542006-10-28 15:55:52 +0200882
Simon Glass770605e2012-02-13 13:51:18 +0000883 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100884 image_print_contents (hdr);
885
886 cnt = image_get_image_size (hdr);
887 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200888#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100889#if defined(CONFIG_FIT)
890 case IMAGE_FORMAT_FIT:
Marian Balakowicz09475f72008-03-12 10:33:01 +0100891 fit_hdr = (const void *)addr;
Marian Balakowicz09475f72008-03-12 10:33:01 +0100892 puts ("Fit image detected...\n");
893
894 cnt = fit_get_size (fit_hdr);
895 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100896#endif
897 default:
Simon Glass770605e2012-02-13 13:51:18 +0000898 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100899 puts ("** Unknown image type\n");
Stefan Roese856f0542006-10-28 15:55:52 +0200900 return 1;
901 }
Simon Glass770605e2012-02-13 13:51:18 +0000902 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
Stefan Roese856f0542006-10-28 15:55:52 +0200903
Scott Wood151c06e2016-05-30 13:57:54 -0500904 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
905 (u_char *)addr);
Stefan Roese856f0542006-10-28 15:55:52 +0200906 if (r) {
907 puts("** Read error\n");
Simon Glass770605e2012-02-13 13:51:18 +0000908 bootstage_error(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200909 return 1;
910 }
Simon Glass770605e2012-02-13 13:51:18 +0000911 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
Stefan Roese856f0542006-10-28 15:55:52 +0200912
Marian Balakowicz09475f72008-03-12 10:33:01 +0100913#if defined(CONFIG_FIT)
914 /* This cannot be done earlier, we need complete FIT image in RAM first */
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200915 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
916 if (!fit_check_format (fit_hdr)) {
Simon Glass770605e2012-02-13 13:51:18 +0000917 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200918 puts ("** Bad FIT image format\n");
919 return 1;
920 }
Simon Glass770605e2012-02-13 13:51:18 +0000921 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
Marian Balakowicz3bab76a2008-06-06 23:07:40 +0200922 fit_print_contents (fit_hdr);
923 }
Marian Balakowicz09475f72008-03-12 10:33:01 +0100924#endif
925
Stefan Roese856f0542006-10-28 15:55:52 +0200926 /* Loading ok, update default load address */
927
928 load_addr = addr;
929
Mike Frysinger67d668b2011-06-05 13:43:02 +0000930 return bootm_maybe_autostart(cmdtp, cmd);
Stefan Roese856f0542006-10-28 15:55:52 +0200931}
932
Kim Phillips088f1b12012-10-29 13:34:31 +0000933static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
934 char * const argv[])
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100935{
936 char *boot_device = NULL;
Stefan Roese856f0542006-10-28 15:55:52 +0200937 int idx;
938 ulong addr, offset = 0;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500939 struct mtd_info *mtd;
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200940#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200941 struct mtd_device *dev;
942 struct part_info *part;
943 u8 pnum;
944
945 if (argc >= 2) {
946 char *p = (argc == 2) ? argv[1] : argv[2];
947 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
948 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
949 if (dev->id->type != MTD_DEV_TYPE_NAND) {
950 puts("Not a NAND device\n");
951 return 1;
952 }
953 if (argc > 3)
954 goto usage;
955 if (argc == 3)
Thomas Knobloch10e03892007-07-06 14:58:39 +0200956 addr = simple_strtoul(argv[1], NULL, 16);
Stefan Roese856f0542006-10-28 15:55:52 +0200957 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200958 addr = CONFIG_SYS_LOAD_ADDR;
Mugunthan V Nad92dff2017-06-26 19:12:51 -0500959
960 mtd = get_nand_dev_by_index(dev->id->num);
961 return nand_load_image(cmdtp, mtd, part->offset,
962 addr, argv[0]);
Stefan Roese856f0542006-10-28 15:55:52 +0200963 }
964 }
965#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100966
Simon Glass770605e2012-02-13 13:51:18 +0000967 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100968 switch (argc) {
969 case 1:
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200970 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glass00caae62017-08-03 12:22:12 -0600971 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100972 break;
973 case 2:
974 addr = simple_strtoul(argv[1], NULL, 16);
Simon Glass00caae62017-08-03 12:22:12 -0600975 boot_device = env_get("bootdevice");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100976 break;
977 case 3:
978 addr = simple_strtoul(argv[1], NULL, 16);
979 boot_device = argv[2];
980 break;
981 case 4:
982 addr = simple_strtoul(argv[1], NULL, 16);
983 boot_device = argv[2];
984 offset = simple_strtoul(argv[3], NULL, 16);
985 break;
986 default:
Ladislav Michl0c8a8492009-04-21 02:26:31 +0200987#if defined(CONFIG_CMD_MTDPARTS)
Stefan Roese856f0542006-10-28 15:55:52 +0200988usage:
989#endif
Simon Glass770605e2012-02-13 13:51:18 +0000990 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000991 return CMD_RET_USAGE;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100992 }
Simon Glass770605e2012-02-13 13:51:18 +0000993 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100994
995 if (!boot_device) {
996 puts("\n** No boot device **\n");
Simon Glass770605e2012-02-13 13:51:18 +0000997 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100998 return 1;
999 }
Simon Glass770605e2012-02-13 13:51:18 +00001000 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001001
Stefan Roese856f0542006-10-28 15:55:52 +02001002 idx = simple_strtoul(boot_device, NULL, 16);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001003
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001004 mtd = get_nand_dev_by_index(idx);
1005 if (!mtd) {
Stefan Roese856f0542006-10-28 15:55:52 +02001006 printf("\n** Device %d not available\n", idx);
Simon Glass770605e2012-02-13 13:51:18 +00001007 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001008 return 1;
1009 }
Simon Glass770605e2012-02-13 13:51:18 +00001010 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001011
Mugunthan V Nad92dff2017-06-26 19:12:51 -05001012 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001013}
1014
1015U_BOOT_CMD(nboot, 4, 1, do_nandboot,
Peter Tyser2fb26042009-01-27 18:03:12 -06001016 "boot from NAND device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001017 "[partition] | [[[loadAddr] dev] offset]"
1018);