blob: 2a9bd6cfefbcd1a50141cd70cbfd9609ce976b58 [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 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010012//config:config FLASH_ERASEALL
13//config: bool "flash_eraseall"
14//config: default n # doesn't build on Ubuntu 8.04
15//config: help
16//config: The flash_eraseall binary from mtd-utils as of git head c4c6a59eb.
17//config: This utility is used to erase the whole MTD device.
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000018
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010019//applet:IF_FLASH_ERASEALL(APPLET(flash_eraseall, BB_DIR_USR_SBIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_FLASH_ERASEALL) += flash_eraseall.o
22
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define flash_eraseall_trivial_usage
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010024//usage: "[-jNq] MTD_DEVICE"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define flash_eraseall_full_usage "\n\n"
26//usage: "Erase an MTD device\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage: "\n -j Format the device for jffs2"
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010028//usage: "\n -N Don't skip bad blocks"
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage: "\n -q Don't display progress messages"
30
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000031#include "libbb.h"
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000032#include <mtd/mtd-user.h>
Denys Vlasenko86cfb702009-11-27 13:26:17 +010033#include <linux/jffs2.h>
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000034
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020035#define OPTION_J (1 << 0)
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010036#define OPTION_N (1 << 1)
37#define OPTION_Q (1 << 2)
38#define IS_NAND (1 << 3)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000039
Denys Vlasenko86cfb702009-11-27 13:26:17 +010040/* mtd/jffs2-user.h used to have this atrocity:
41extern int target_endian;
42
43#define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); })
44#define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); })
45
46#define cpu_to_je16(x) ((jint16_t){t16(x)})
47#define cpu_to_je32(x) ((jint32_t){t32(x)})
48#define cpu_to_jemode(x) ((jmode_t){t32(x)})
49
50#define je16_to_cpu(x) (t16((x).v16))
51#define je32_to_cpu(x) (t32((x).v32))
52#define jemode_to_cpu(x) (t32((x).m))
53
54but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore)
55*/
56
57/* We always use native endianness */
58#undef cpu_to_je16
59#undef cpu_to_je32
60#define cpu_to_je16(v) ((jint16_t){(v)})
61#define cpu_to_je32(v) ((jint32_t){(v)})
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000062
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000063static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
64{
Denys Vlasenko86cfb702009-11-27 13:26:17 +010065 printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
66 (unsigned)meminfo->erasesize / 1024,
67 erase->start,
68 (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
69 );
Denys Vlasenko8131eea2009-11-02 14:19:51 +010070 fflush_all();
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000071}
72
73int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +000074int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000075{
76 struct jffs2_unknown_node cleanmarker;
77 mtd_info_t meminfo;
Denis Vlasenko962e3652009-02-19 01:17:12 +000078 int fd, clmpos, clmlen;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000079 erase_info_t erase;
80 struct stat st;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000081 unsigned int flags;
82 char *mtd_name;
83
84 opt_complementary = "=1";
Alexander Shiyan45dc96c2013-03-15 00:42:39 +010085 flags = getopt32(argv, "jNq");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000086
Denis Vlasenko962e3652009-02-19 01:17:12 +000087 mtd_name = argv[optind];
Denys Vlasenko86cfb702009-11-27 13:26:17 +010088 fd = xopen(mtd_name, O_RDWR);
89 fstat(fd, &st);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000090 if (!S_ISCHR(st.st_mode))
91 bb_error_msg_and_die("%s: not a char device", mtd_name);
92
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000093 xioctl(fd, MEMGETINFO, &meminfo);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000094 erase.length = meminfo.erasesize;
Denis Vlasenko962e3652009-02-19 01:17:12 +000095 if (meminfo.type == MTD_NANDFLASH)
96 flags |= IS_NAND;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000097
Denis Vlasenko962e3652009-02-19 01:17:12 +000098 clmpos = 0;
99 clmlen = 8;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000100 if (flags & OPTION_J) {
101 uint32_t *crc32_table;
102
103 crc32_table = crc32_filltable(NULL, 0);
104
Denis Vlasenko962e3652009-02-19 01:17:12 +0000105 cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
106 cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
107 if (!(flags & IS_NAND))
108 cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000109 else {
110 struct nand_oobinfo oobinfo;
111
112 xioctl(fd, MEMGETOOBSEL, &oobinfo);
113
114 /* Check for autoplacement */
115 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
116 /* Get the position of the free bytes */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000117 clmpos = oobinfo.oobfree[0][0];
118 clmlen = oobinfo.oobfree[0][1];
119 if (clmlen > 8)
120 clmlen = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000121 if (clmlen == 0)
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000122 bb_error_msg_and_die("autoplacement selected and no empty space in oob");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000123 } else {
124 /* Legacy mode */
125 switch (meminfo.oobsize) {
126 case 8:
127 clmpos = 6;
128 clmlen = 2;
129 break;
130 case 16:
131 clmpos = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000132 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000133 break;
134 case 64:
135 clmpos = 16;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000136 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000137 break;
138 }
139 }
140 cleanmarker.totlen = cpu_to_je32(8);
141 }
142
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200143 cleanmarker.hdr_crc = cpu_to_je32(
144 crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
145 );
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000146 }
147
Denis Vlasenko962e3652009-02-19 01:17:12 +0000148 /* Don't want to destroy progress indicator by bb_error_msg's */
149 applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
150
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000151 for (erase.start = 0; erase.start < meminfo.size;
152 erase.start += meminfo.erasesize) {
Alexander Shiyan45dc96c2013-03-15 00:42:39 +0100153 if (!(flags & OPTION_N)) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000154 int ret;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000155 loff_t offset = erase.start;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000156
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000157 ret = ioctl(fd, MEMGETBADBLOCK, &offset);
158 if (ret > 0) {
159 if (!(flags & OPTION_Q))
Denys Vlasenko066e76b2016-03-30 16:20:28 +0200160 printf("\nSkipping bad block at 0x%08x\n", erase.start);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000161 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000162 }
163 if (ret < 0) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000164 /* Black block table is not available on certain flash
165 * types e.g. NOR
166 */
167 if (errno == EOPNOTSUPP) {
Alexander Shiyan45dc96c2013-03-15 00:42:39 +0100168 flags |= OPTION_N;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000169 if (flags & IS_NAND)
170 bb_error_msg_and_die("bad block check not available");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000171 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000172 bb_perror_msg_and_die("MEMGETBADBLOCK error");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000173 }
174 }
175 }
176
177 if (!(flags & OPTION_Q))
178 show_progress(&meminfo, &erase);
179
180 xioctl(fd, MEMERASE, &erase);
181
182 /* format for JFFS2 ? */
183 if (!(flags & OPTION_J))
184 continue;
185
186 /* write cleanmarker */
Denis Vlasenko962e3652009-02-19 01:17:12 +0000187 if (flags & IS_NAND) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000188 struct mtd_oob_buf oob;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000189
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000190 oob.ptr = (unsigned char *) &cleanmarker;
191 oob.start = erase.start + clmpos;
192 oob.length = clmlen;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000193 xioctl(fd, MEMWRITEOOB, &oob);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000194 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000195 xlseek(fd, erase.start, SEEK_SET);
196 /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
197 bb_perror_msg("MTD %s failure", "seek");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000198 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000199 } */
200 xwrite(fd, &cleanmarker, sizeof(cleanmarker));
201 /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
202 bb_perror_msg("MTD %s failure", "write");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000203 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000204 } */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000205 }
206 if (!(flags & OPTION_Q))
207 printf(" Cleanmarker written at %x.", erase.start);
208 }
209 if (!(flags & OPTION_Q)) {
210 show_progress(&meminfo, &erase);
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000211 bb_putchar('\n');
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000212 }
213
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000214 if (ENABLE_FEATURE_CLEAN_UP)
215 close(fd);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000216 return EXIT_SUCCESS;
217}