1 /* eraseall.c -- erase the whole of a MTD device
3 Ported to busybox from mtd-utils.
5 Copyright (C) 2000 Arcom Control System Ltd
7 Renamed to flash_eraseall.c
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 #include <mtd/mtd-user.h>
27 #include <mtd/jffs2-user.h>
29 #define OPTION_J (1 << 0)
30 #define OPTION_Q (1 << 1)
32 int target_endian = __BYTE_ORDER;
34 static uint32_t crc32(uint32_t val, const void *ss, int len,
35 uint32_t *crc32_table)
37 const unsigned char *s = ss;
39 val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
43 static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
45 printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
46 meminfo->erasesize / 1024, erase->start,
47 (unsigned long long) erase->start * 100 / meminfo->size);
51 int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
52 int flash_eraseall_main(int argc, char **argv)
54 struct jffs2_unknown_node cleanmarker;
56 int fd, clmpos = 0, clmlen = 8;
59 int isNAND, bbtest = 1;
63 opt_complementary = "=1";
64 flags = getopt32(argv, "jq");
66 mtd_name = *(argv + optind);
68 if (!S_ISCHR(st.st_mode))
69 bb_error_msg_and_die("%s: not a char device", mtd_name);
71 fd = xopen(mtd_name, O_RDWR);
73 xioctl(fd, MEMGETINFO, &meminfo);
75 erase.length = meminfo.erasesize;
76 isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0;
78 if (flags & OPTION_J) {
79 uint32_t *crc32_table;
81 crc32_table = crc32_filltable(NULL, 0);
83 cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
84 cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
86 cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node));
88 struct nand_oobinfo oobinfo;
90 xioctl(fd, MEMGETOOBSEL, &oobinfo);
92 /* Check for autoplacement */
93 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
94 /* Get the position of the free bytes */
95 if (!oobinfo.oobfree[0][1])
96 bb_error_msg_and_die("Autoplacement selected and no empty space in oob");
98 clmpos = oobinfo.oobfree[0][0];
99 clmlen = oobinfo.oobfree[0][1];
104 switch (meminfo.oobsize) {
119 cleanmarker.totlen = cpu_to_je32(8);
122 cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4,
126 for (erase.start = 0; erase.start < meminfo.size;
127 erase.start += meminfo.erasesize) {
131 loff_t offset = erase.start;
132 ret = ioctl(fd, MEMGETBADBLOCK, &offset);
134 if (!(flags & OPTION_Q))
135 bb_info_msg("\nSkipping bad block at 0x%08x", erase.start);
137 } else if (ret < 0) {
138 /* Black block table is not available on certain flash
141 if (errno == EOPNOTSUPP) {
144 bb_error_msg_and_die("%s: Bad block check not available",
147 bb_error_msg_and_die("\n%s: MTD get bad block failed: %s",
148 mtd_name, strerror(errno));
153 if (!(flags & OPTION_Q))
154 show_progress(&meminfo, &erase);
156 xioctl(fd, MEMERASE, &erase);
158 /* format for JFFS2 ? */
159 if (!(flags & OPTION_J))
162 /* write cleanmarker */
164 struct mtd_oob_buf oob;
165 oob.ptr = (unsigned char *) &cleanmarker;
166 oob.start = erase.start + clmpos;
168 xioctl (fd, MEMWRITEOOB, &oob);
170 if (lseek (fd, erase.start, SEEK_SET) < 0) {
171 bb_error_msg("\n%s: MTD lseek failure: %s", mtd_name, strerror(errno));
174 if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) {
175 bb_error_msg("\n%s: MTD write failure: %s", mtd_name, strerror(errno));
179 if (!(flags & OPTION_Q))
180 printf(" Cleanmarker written at %x.", erase.start);
182 if (!(flags & OPTION_Q)) {
183 show_progress(&meminfo, &erase);