blob: d95d214d914c5fbbd96d1ae88a77033e1ff23cb9 [file] [log] [blame]
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +00002/* eraseall.c -- erase the whole of a MTD device
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +00003 *
4 * Ported to busybox from mtd-utils.
5 *
6 * Copyright (C) 2000 Arcom Control System Ltd
7 *
8 * Renamed to flash_eraseall.c
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000011 */
12
Pere Orga5bc8c002011-04-11 03:29:49 +020013//usage:#define flash_eraseall_trivial_usage
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010014//usage: "[-jNq] MTD_DEVICE"
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage:#define flash_eraseall_full_usage "\n\n"
16//usage: "Erase an MTD device\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020017//usage: "\n -j Format the device for jffs2"
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010018//usage: "\n -N Don't skip bad blocks"
Pere Orga5bc8c002011-04-11 03:29:49 +020019//usage: "\n -q Don't display progress messages"
20
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000021#include "libbb.h"
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000022#include <mtd/mtd-user.h>
Denys Vlasenko86cfb702009-11-27 13:26:17 +010023#include <linux/jffs2.h>
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000024
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020025#define OPTION_J (1 << 0)
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010026#define OPTION_N (1 << 1)
27#define OPTION_Q (1 << 2)
28#define IS_NAND (1 << 3)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000029
Denys Vlasenko86cfb702009-11-27 13:26:17 +010030/* mtd/jffs2-user.h used to have this atrocity:
31extern int target_endian;
32
33#define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); })
34#define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); })
35
36#define cpu_to_je16(x) ((jint16_t){t16(x)})
37#define cpu_to_je32(x) ((jint32_t){t32(x)})
38#define cpu_to_jemode(x) ((jmode_t){t32(x)})
39
40#define je16_to_cpu(x) (t16((x).v16))
41#define je32_to_cpu(x) (t32((x).v32))
42#define jemode_to_cpu(x) (t32((x).m))
43
44but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore)
45*/
46
47/* We always use native endianness */
48#undef cpu_to_je16
49#undef cpu_to_je32
50#define cpu_to_je16(v) ((jint16_t){(v)})
51#define cpu_to_je32(v) ((jint32_t){(v)})
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000052
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000053static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
54{
Denys Vlasenko86cfb702009-11-27 13:26:17 +010055 printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
56 (unsigned)meminfo->erasesize / 1024,
57 erase->start,
58 (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
59 );
Denys Vlasenko8131eea2009-11-02 14:19:51 +010060 fflush_all();
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000061}
62
63int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +000064int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000065{
66 struct jffs2_unknown_node cleanmarker;
67 mtd_info_t meminfo;
Denis Vlasenko962e3652009-02-19 01:17:12 +000068 int fd, clmpos, clmlen;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000069 erase_info_t erase;
70 struct stat st;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000071 unsigned int flags;
72 char *mtd_name;
73
74 opt_complementary = "=1";
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010075 flags = getopt32(argv, "jNq");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000076
Denis Vlasenko962e3652009-02-19 01:17:12 +000077 mtd_name = argv[optind];
Denys Vlasenko86cfb702009-11-27 13:26:17 +010078 fd = xopen(mtd_name, O_RDWR);
79 fstat(fd, &st);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000080 if (!S_ISCHR(st.st_mode))
81 bb_error_msg_and_die("%s: not a char device", mtd_name);
82
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000083 xioctl(fd, MEMGETINFO, &meminfo);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000084 erase.length = meminfo.erasesize;
Denis Vlasenko962e3652009-02-19 01:17:12 +000085 if (meminfo.type == MTD_NANDFLASH)
86 flags |= IS_NAND;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000087
Denis Vlasenko962e3652009-02-19 01:17:12 +000088 clmpos = 0;
89 clmlen = 8;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000090 if (flags & OPTION_J) {
91 uint32_t *crc32_table;
92
93 crc32_table = crc32_filltable(NULL, 0);
94
Denis Vlasenko962e3652009-02-19 01:17:12 +000095 cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
96 cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
97 if (!(flags & IS_NAND))
98 cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000099 else {
100 struct nand_oobinfo oobinfo;
101
102 xioctl(fd, MEMGETOOBSEL, &oobinfo);
103
104 /* Check for autoplacement */
105 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
106 /* Get the position of the free bytes */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000107 clmpos = oobinfo.oobfree[0][0];
108 clmlen = oobinfo.oobfree[0][1];
109 if (clmlen > 8)
110 clmlen = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000111 if (clmlen == 0)
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000112 bb_error_msg_and_die("autoplacement selected and no empty space in oob");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000113 } else {
114 /* Legacy mode */
115 switch (meminfo.oobsize) {
116 case 8:
117 clmpos = 6;
118 clmlen = 2;
119 break;
120 case 16:
121 clmpos = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000122 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000123 break;
124 case 64:
125 clmpos = 16;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000126 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000127 break;
128 }
129 }
130 cleanmarker.totlen = cpu_to_je32(8);
131 }
132
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200133 cleanmarker.hdr_crc = cpu_to_je32(
134 crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
135 );
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000136 }
137
Denis Vlasenko962e3652009-02-19 01:17:12 +0000138 /* Don't want to destroy progress indicator by bb_error_msg's */
139 applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
140
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000141 for (erase.start = 0; erase.start < meminfo.size;
142 erase.start += meminfo.erasesize) {
Alexander Shiyan45dc96c2013-03-15 00:42:39 +0100143 if (!(flags & OPTION_N)) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000144 int ret;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000145 loff_t offset = erase.start;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000146
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000147 ret = ioctl(fd, MEMGETBADBLOCK, &offset);
148 if (ret > 0) {
149 if (!(flags & OPTION_Q))
Denys Vlasenko066e76b2016-03-30 16:20:28 +0200150 printf("\nSkipping bad block at 0x%08x\n", erase.start);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000151 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000152 }
153 if (ret < 0) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000154 /* Black block table is not available on certain flash
155 * types e.g. NOR
156 */
157 if (errno == EOPNOTSUPP) {
Alexander Shiyan45dc96c2013-03-15 00:42:39 +0100158 flags |= OPTION_N;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000159 if (flags & IS_NAND)
160 bb_error_msg_and_die("bad block check not available");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000161 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000162 bb_perror_msg_and_die("MEMGETBADBLOCK error");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000163 }
164 }
165 }
166
167 if (!(flags & OPTION_Q))
168 show_progress(&meminfo, &erase);
169
170 xioctl(fd, MEMERASE, &erase);
171
172 /* format for JFFS2 ? */
173 if (!(flags & OPTION_J))
174 continue;
175
176 /* write cleanmarker */
Denis Vlasenko962e3652009-02-19 01:17:12 +0000177 if (flags & IS_NAND) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000178 struct mtd_oob_buf oob;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000179
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000180 oob.ptr = (unsigned char *) &cleanmarker;
181 oob.start = erase.start + clmpos;
182 oob.length = clmlen;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000183 xioctl(fd, MEMWRITEOOB, &oob);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000184 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000185 xlseek(fd, erase.start, SEEK_SET);
186 /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
187 bb_perror_msg("MTD %s failure", "seek");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000188 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000189 } */
190 xwrite(fd, &cleanmarker, sizeof(cleanmarker));
191 /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
192 bb_perror_msg("MTD %s failure", "write");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000193 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000194 } */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000195 }
196 if (!(flags & OPTION_Q))
197 printf(" Cleanmarker written at %x.", erase.start);
198 }
199 if (!(flags & OPTION_Q)) {
200 show_progress(&meminfo, &erase);
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000201 bb_putchar('\n');
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000202 }
203
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000204 if (ENABLE_FEATURE_CLEAN_UP)
205 close(fd);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000206 return EXIT_SUCCESS;
207}