1 /* vi: set sw=4 ts=4: */
2 /* eraseall.c -- erase the whole of a MTD device
4 * Ported to busybox from mtd-utils.
6 * Copyright (C) 2000 Arcom Control System Ltd
8 * Renamed to flash_eraseall.c
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <mtd/mtd-user.h>
15 #include <mtd/jffs2-user.h>
17 #define OPTION_J (1 << 0)
18 #define OPTION_Q (1 << 1)
19 #define IS_NAND (1 << 2)
20 #define BBTEST (1 << 3)
23 /* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */
26 #define G (*(struct globals*)&bb_common_bufsiz1)
27 #define target_endian (G.tgt_endian)
28 #define INIT_G() do { \
29 target_endian = __BYTE_ORDER; \
32 static uint32_t crc32(uint32_t val
, const void *ss
, int len
,
33 uint32_t *crc32_table
)
35 const unsigned char *s
= ss
;
37 val
= crc32_table
[(val
^ *s
++) & 0xff] ^ (val
>> 8);
41 static void show_progress(mtd_info_t
*meminfo
, erase_info_t
*erase
)
43 printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
44 (unsigned)meminfo
->erasesize
/ 1024, erase
->start
,
45 (unsigned long long) erase
->start
* 100 / meminfo
->size
);
49 int flash_eraseall_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
50 int flash_eraseall_main(int argc UNUSED_PARAM
, char **argv
)
52 struct jffs2_unknown_node cleanmarker
;
54 int fd
, clmpos
, clmlen
;
61 opt_complementary
= "=1";
62 flags
= BBTEST
| getopt32(argv
, "jq");
64 mtd_name
= argv
[optind
];
66 if (!S_ISCHR(st
.st_mode
))
67 bb_error_msg_and_die("%s: not a char device", mtd_name
);
69 fd
= xopen(mtd_name
, O_RDWR
);
71 xioctl(fd
, MEMGETINFO
, &meminfo
);
72 erase
.length
= meminfo
.erasesize
;
73 if (meminfo
.type
== MTD_NANDFLASH
)
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
);
85 if (!(flags
& IS_NAND
))
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 clmpos
= oobinfo
.oobfree
[0][0];
96 clmlen
= oobinfo
.oobfree
[0][1];
100 bb_error_msg_and_die("Autoplacement selected and no empty space in oob");
103 switch (meminfo
.oobsize
) {
118 cleanmarker
.totlen
= cpu_to_je32(8);
121 cleanmarker
.hdr_crc
= cpu_to_je32(crc32(0, &cleanmarker
, sizeof(struct jffs2_unknown_node
) - 4,
125 /* Don't want to destroy progress indicator by bb_error_msg's */
126 applet_name
= xasprintf("\n%s: %s", applet_name
, mtd_name
);
128 for (erase
.start
= 0; erase
.start
< meminfo
.size
;
129 erase
.start
+= meminfo
.erasesize
) {
130 if (flags
& BBTEST
) {
132 loff_t offset
= erase
.start
;
134 ret
= ioctl(fd
, MEMGETBADBLOCK
, &offset
);
136 if (!(flags
& OPTION_Q
))
137 bb_info_msg("\nSkipping bad block at 0x%08x", erase
.start
);
141 /* Black block table is not available on certain flash
144 if (errno
== EOPNOTSUPP
) {
147 bb_error_msg_and_die("bad block check not available");
149 bb_perror_msg_and_die("MEMGETBADBLOCK error");
154 if (!(flags
& OPTION_Q
))
155 show_progress(&meminfo
, &erase
);
157 xioctl(fd
, MEMERASE
, &erase
);
159 /* format for JFFS2 ? */
160 if (!(flags
& OPTION_J
))
163 /* write cleanmarker */
164 if (flags
& IS_NAND
) {
165 struct mtd_oob_buf oob
;
167 oob
.ptr
= (unsigned char *) &cleanmarker
;
168 oob
.start
= erase
.start
+ clmpos
;
170 xioctl(fd
, MEMWRITEOOB
, &oob
);
172 xlseek(fd
, erase
.start
, SEEK_SET
);
173 /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
174 bb_perror_msg("MTD %s failure", "seek");
177 xwrite(fd
, &cleanmarker
, sizeof(cleanmarker
));
178 /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
179 bb_perror_msg("MTD %s failure", "write");
183 if (!(flags
& OPTION_Q
))
184 printf(" Cleanmarker written at %x.", erase
.start
);
186 if (!(flags
& OPTION_Q
)) {
187 show_progress(&meminfo
, &erase
);
191 if (ENABLE_FEATURE_CLEAN_UP
)