blob: 68596e11b002e0d389592c961e752e5de3bda993 [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
13#include "libbb.h"
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000014#include <mtd/mtd-user.h>
Denys Vlasenko86cfb702009-11-27 13:26:17 +010015#include <linux/jffs2.h>
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000016
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020017#define OPTION_J (1 << 0)
18#define OPTION_Q (1 << 1)
19#define IS_NAND (1 << 2)
20#define BBTEST (1 << 3)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000021
Denys Vlasenko86cfb702009-11-27 13:26:17 +010022/* mtd/jffs2-user.h used to have this atrocity:
23extern int target_endian;
24
25#define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); })
26#define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); })
27
28#define cpu_to_je16(x) ((jint16_t){t16(x)})
29#define cpu_to_je32(x) ((jint32_t){t32(x)})
30#define cpu_to_jemode(x) ((jmode_t){t32(x)})
31
32#define je16_to_cpu(x) (t16((x).v16))
33#define je32_to_cpu(x) (t32((x).v32))
34#define jemode_to_cpu(x) (t32((x).m))
35
36but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore)
37*/
38
39/* We always use native endianness */
40#undef cpu_to_je16
41#undef cpu_to_je32
42#define cpu_to_je16(v) ((jint16_t){(v)})
43#define cpu_to_je32(v) ((jint32_t){(v)})
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000044
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000045static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
46{
Denys Vlasenko86cfb702009-11-27 13:26:17 +010047 printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
48 (unsigned)meminfo->erasesize / 1024,
49 erase->start,
50 (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
51 );
Denys Vlasenko8131eea2009-11-02 14:19:51 +010052 fflush_all();
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000053}
54
55int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +000056int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000057{
58 struct jffs2_unknown_node cleanmarker;
59 mtd_info_t meminfo;
Denis Vlasenko962e3652009-02-19 01:17:12 +000060 int fd, clmpos, clmlen;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000061 erase_info_t erase;
62 struct stat st;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000063 unsigned int flags;
64 char *mtd_name;
65
66 opt_complementary = "=1";
Denis Vlasenko962e3652009-02-19 01:17:12 +000067 flags = BBTEST | getopt32(argv, "jq");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000068
Denis Vlasenko962e3652009-02-19 01:17:12 +000069 mtd_name = argv[optind];
Denys Vlasenko86cfb702009-11-27 13:26:17 +010070 fd = xopen(mtd_name, O_RDWR);
71 fstat(fd, &st);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000072 if (!S_ISCHR(st.st_mode))
73 bb_error_msg_and_die("%s: not a char device", mtd_name);
74
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000075 xioctl(fd, MEMGETINFO, &meminfo);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000076 erase.length = meminfo.erasesize;
Denis Vlasenko962e3652009-02-19 01:17:12 +000077 if (meminfo.type == MTD_NANDFLASH)
78 flags |= IS_NAND;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000079
Denis Vlasenko962e3652009-02-19 01:17:12 +000080 clmpos = 0;
81 clmlen = 8;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000082 if (flags & OPTION_J) {
83 uint32_t *crc32_table;
84
85 crc32_table = crc32_filltable(NULL, 0);
86
Denis Vlasenko962e3652009-02-19 01:17:12 +000087 cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
88 cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
89 if (!(flags & IS_NAND))
90 cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000091 else {
92 struct nand_oobinfo oobinfo;
93
94 xioctl(fd, MEMGETOOBSEL, &oobinfo);
95
96 /* Check for autoplacement */
97 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
98 /* Get the position of the free bytes */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000099 clmpos = oobinfo.oobfree[0][0];
100 clmlen = oobinfo.oobfree[0][1];
101 if (clmlen > 8)
102 clmlen = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000103 if (clmlen == 0)
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000104 bb_error_msg_and_die("autoplacement selected and no empty space in oob");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000105 } else {
106 /* Legacy mode */
107 switch (meminfo.oobsize) {
108 case 8:
109 clmpos = 6;
110 clmlen = 2;
111 break;
112 case 16:
113 clmpos = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000114 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000115 break;
116 case 64:
117 clmpos = 16;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000118 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000119 break;
120 }
121 }
122 cleanmarker.totlen = cpu_to_je32(8);
123 }
124
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200125 cleanmarker.hdr_crc = cpu_to_je32(
126 crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
127 );
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000128 }
129
Denis Vlasenko962e3652009-02-19 01:17:12 +0000130 /* Don't want to destroy progress indicator by bb_error_msg's */
131 applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
132
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000133 for (erase.start = 0; erase.start < meminfo.size;
134 erase.start += meminfo.erasesize) {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000135 if (flags & BBTEST) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000136 int ret;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000137 loff_t offset = erase.start;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000138
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000139 ret = ioctl(fd, MEMGETBADBLOCK, &offset);
140 if (ret > 0) {
141 if (!(flags & OPTION_Q))
142 bb_info_msg("\nSkipping bad block at 0x%08x", erase.start);
143 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000144 }
145 if (ret < 0) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000146 /* Black block table is not available on certain flash
147 * types e.g. NOR
148 */
149 if (errno == EOPNOTSUPP) {
Denis Vlasenkoe261bba2009-02-24 16:47:03 +0000150 flags &= ~BBTEST;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000151 if (flags & IS_NAND)
152 bb_error_msg_and_die("bad block check not available");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000153 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000154 bb_perror_msg_and_die("MEMGETBADBLOCK error");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000155 }
156 }
157 }
158
159 if (!(flags & OPTION_Q))
160 show_progress(&meminfo, &erase);
161
162 xioctl(fd, MEMERASE, &erase);
163
164 /* format for JFFS2 ? */
165 if (!(flags & OPTION_J))
166 continue;
167
168 /* write cleanmarker */
Denis Vlasenko962e3652009-02-19 01:17:12 +0000169 if (flags & IS_NAND) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000170 struct mtd_oob_buf oob;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000171
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000172 oob.ptr = (unsigned char *) &cleanmarker;
173 oob.start = erase.start + clmpos;
174 oob.length = clmlen;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000175 xioctl(fd, MEMWRITEOOB, &oob);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000176 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000177 xlseek(fd, erase.start, SEEK_SET);
178 /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
179 bb_perror_msg("MTD %s failure", "seek");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000180 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000181 } */
182 xwrite(fd, &cleanmarker, sizeof(cleanmarker));
183 /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
184 bb_perror_msg("MTD %s failure", "write");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000185 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000186 } */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000187 }
188 if (!(flags & OPTION_Q))
189 printf(" Cleanmarker written at %x.", erase.start);
190 }
191 if (!(flags & OPTION_Q)) {
192 show_progress(&meminfo, &erase);
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000193 bb_putchar('\n');
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000194 }
195
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000196 if (ENABLE_FEATURE_CLEAN_UP)
197 close(fd);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000198 return EXIT_SUCCESS;
199}