*: whitespace fixes
[platform/upstream/busybox.git] / miscutils / flash_eraseall.c
1 /* vi: set sw=4 ts=4: */
2 /* eraseall.c -- erase the whole of a MTD device
3  *
4  * Ported to busybox from mtd-utils.
5  *
6  * Copyright (C) 2000 Arcom Control System Ltd
7  *
8  * Renamed to flash_eraseall.c
9  *
10  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11  */
12
13 #include "libbb.h"
14 #include <mtd/mtd-user.h>
15 #include <linux/jffs2.h>
16
17 #define OPTION_J  (1 << 0)
18 #define OPTION_Q  (1 << 1)
19 #define IS_NAND   (1 << 2)
20 #define BBTEST    (1 << 3)
21
22 /* mtd/jffs2-user.h used to have this atrocity:
23 extern 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
36 but 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)})
44
45 static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
46 {
47         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         );
52         fflush_all();
53 }
54
55 int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
56 int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
57 {
58         struct jffs2_unknown_node cleanmarker;
59         mtd_info_t meminfo;
60         int fd, clmpos, clmlen;
61         erase_info_t erase;
62         struct stat st;
63         unsigned int flags;
64         char *mtd_name;
65
66         opt_complementary = "=1";
67         flags = BBTEST | getopt32(argv, "jq");
68
69         mtd_name = argv[optind];
70         fd = xopen(mtd_name, O_RDWR);
71         fstat(fd, &st);
72         if (!S_ISCHR(st.st_mode))
73                 bb_error_msg_and_die("%s: not a char device", mtd_name);
74
75         xioctl(fd, MEMGETINFO, &meminfo);
76         erase.length = meminfo.erasesize;
77         if (meminfo.type == MTD_NANDFLASH)
78                 flags |= IS_NAND;
79
80         clmpos = 0;
81         clmlen = 8;
82         if (flags & OPTION_J) {
83                 uint32_t *crc32_table;
84
85                 crc32_table = crc32_filltable(NULL, 0);
86
87                 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));
91                 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 */
99                                 clmpos = oobinfo.oobfree[0][0];
100                                 clmlen = oobinfo.oobfree[0][1];
101                                 if (clmlen > 8)
102                                         clmlen = 8;
103                                 if (clmlen == 0)
104                                         bb_error_msg_and_die("autoplacement selected and no empty space in oob");
105                         } 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;
114                                         /*clmlen = 8;*/
115                                         break;
116                                 case 64:
117                                         clmpos = 16;
118                                         /*clmlen = 8;*/
119                                         break;
120                                 }
121                         }
122                         cleanmarker.totlen = cpu_to_je32(8);
123                 }
124
125                 cleanmarker.hdr_crc = cpu_to_je32(
126                         crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
127                 );
128         }
129
130         /* Don't want to destroy progress indicator by bb_error_msg's */
131         applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
132
133         for (erase.start = 0; erase.start < meminfo.size;
134              erase.start += meminfo.erasesize) {
135                 if (flags & BBTEST) {
136                         int ret;
137                         loff_t offset = erase.start;
138
139                         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;
144                         }
145                         if (ret < 0) {
146                                 /* Black block table is not available on certain flash
147                                  * types e.g. NOR
148                                  */
149                                 if (errno == EOPNOTSUPP) {
150                                         flags &= ~BBTEST;
151                                         if (flags & IS_NAND)
152                                                 bb_error_msg_and_die("bad block check not available");
153                                 } else {
154                                         bb_perror_msg_and_die("MEMGETBADBLOCK error");
155                                 }
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 */
169                 if (flags & IS_NAND) {
170                         struct mtd_oob_buf oob;
171
172                         oob.ptr = (unsigned char *) &cleanmarker;
173                         oob.start = erase.start + clmpos;
174                         oob.length = clmlen;
175                         xioctl(fd, MEMWRITEOOB, &oob);
176                 } else {
177                         xlseek(fd, erase.start, SEEK_SET);
178                         /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
179                                 bb_perror_msg("MTD %s failure", "seek");
180                                 continue;
181                         } */
182                         xwrite(fd, &cleanmarker, sizeof(cleanmarker));
183                         /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
184                                 bb_perror_msg("MTD %s failure", "write");
185                                 continue;
186                         } */
187                 }
188                 if (!(flags & OPTION_Q))
189                         printf(" Cleanmarker written at %x.", erase.start);
190         }
191         if (!(flags & OPTION_Q)) {
192                 show_progress(&meminfo, &erase);
193                 bb_putchar('\n');
194         }
195
196         if (ENABLE_FEATURE_CLEAN_UP)
197                 close(fd);
198         return EXIT_SUCCESS;
199 }