blob: 3e0c06ffd8b4f1c93a3f05ade42f978d6c2e2f32 [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 *
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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>
15#include <mtd/jffs2-user.h>
16
17#define OPTION_J (1 << 0)
18#define OPTION_Q (1 << 1)
Denis Vlasenko962e3652009-02-19 01:17:12 +000019#define IS_NAND (1 << 2)
20#define BBTEST (1 << 3)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000021
Bernhard Reutner-Fischer8af23f82009-02-18 13:42:51 +000022struct globals {
23 /* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */
24 int tgt_endian;
25};
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; \
30} while (0)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000031
32static uint32_t crc32(uint32_t val, const void *ss, int len,
33 uint32_t *crc32_table)
34{
35 const unsigned char *s = ss;
36 while (--len >= 0)
37 val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
38 return val;
39}
40
41static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
42{
43 printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
Denis Vlasenko962e3652009-02-19 01:17:12 +000044 (unsigned)meminfo->erasesize / 1024, erase->start,
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000045 (unsigned long long) erase->start * 100 / meminfo->size);
46 fflush(stdout);
47}
48
49int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +000050int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000051{
52 struct jffs2_unknown_node cleanmarker;
53 mtd_info_t meminfo;
Denis Vlasenko962e3652009-02-19 01:17:12 +000054 int fd, clmpos, clmlen;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000055 erase_info_t erase;
56 struct stat st;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000057 unsigned int flags;
58 char *mtd_name;
59
Bernhard Reutner-Fischer8af23f82009-02-18 13:42:51 +000060 INIT_G();
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000061 opt_complementary = "=1";
Denis Vlasenko962e3652009-02-19 01:17:12 +000062 flags = BBTEST | getopt32(argv, "jq");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000063
Denis Vlasenko962e3652009-02-19 01:17:12 +000064 mtd_name = argv[optind];
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000065 xstat(mtd_name, &st);
66 if (!S_ISCHR(st.st_mode))
67 bb_error_msg_and_die("%s: not a char device", mtd_name);
68
69 fd = xopen(mtd_name, O_RDWR);
70
71 xioctl(fd, MEMGETINFO, &meminfo);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000072 erase.length = meminfo.erasesize;
Denis Vlasenko962e3652009-02-19 01:17:12 +000073 if (meminfo.type == MTD_NANDFLASH)
74 flags |= IS_NAND;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000075
Denis Vlasenko962e3652009-02-19 01:17:12 +000076 clmpos = 0;
77 clmlen = 8;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000078 if (flags & OPTION_J) {
79 uint32_t *crc32_table;
80
81 crc32_table = crc32_filltable(NULL, 0);
82
Denis Vlasenko962e3652009-02-19 01:17:12 +000083 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));
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000087 else {
88 struct nand_oobinfo oobinfo;
89
90 xioctl(fd, MEMGETOOBSEL, &oobinfo);
91
92 /* Check for autoplacement */
93 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
94 /* Get the position of the free bytes */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +000095 clmpos = oobinfo.oobfree[0][0];
96 clmlen = oobinfo.oobfree[0][1];
97 if (clmlen > 8)
98 clmlen = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +000099 if (clmlen == 0)
100 bb_error_msg_and_die("Autoplacement selected and no empty space in oob");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000101 } else {
102 /* Legacy mode */
103 switch (meminfo.oobsize) {
104 case 8:
105 clmpos = 6;
106 clmlen = 2;
107 break;
108 case 16:
109 clmpos = 8;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000110 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000111 break;
112 case 64:
113 clmpos = 16;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000114 /*clmlen = 8;*/
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000115 break;
116 }
117 }
118 cleanmarker.totlen = cpu_to_je32(8);
119 }
120
Denis Vlasenko962e3652009-02-19 01:17:12 +0000121 cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4,
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000122 crc32_table));
123 }
124
Denis Vlasenko962e3652009-02-19 01:17:12 +0000125 /* Don't want to destroy progress indicator by bb_error_msg's */
126 applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
127
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000128 for (erase.start = 0; erase.start < meminfo.size;
129 erase.start += meminfo.erasesize) {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000130 if (flags & BBTEST) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000131 int ret;
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000132 loff_t offset = erase.start;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000133
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000134 ret = ioctl(fd, MEMGETBADBLOCK, &offset);
135 if (ret > 0) {
136 if (!(flags & OPTION_Q))
137 bb_info_msg("\nSkipping bad block at 0x%08x", erase.start);
138 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000139 }
140 if (ret < 0) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000141 /* Black block table is not available on certain flash
142 * types e.g. NOR
143 */
144 if (errno == EOPNOTSUPP) {
Denis Vlasenkoe261bba2009-02-24 16:47:03 +0000145 flags &= ~BBTEST;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000146 if (flags & IS_NAND)
147 bb_error_msg_and_die("bad block check not available");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000148 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000149 bb_perror_msg_and_die("MEMGETBADBLOCK error");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000150 }
151 }
152 }
153
154 if (!(flags & OPTION_Q))
155 show_progress(&meminfo, &erase);
156
157 xioctl(fd, MEMERASE, &erase);
158
159 /* format for JFFS2 ? */
160 if (!(flags & OPTION_J))
161 continue;
162
163 /* write cleanmarker */
Denis Vlasenko962e3652009-02-19 01:17:12 +0000164 if (flags & IS_NAND) {
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000165 struct mtd_oob_buf oob;
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000166
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000167 oob.ptr = (unsigned char *) &cleanmarker;
168 oob.start = erase.start + clmpos;
169 oob.length = clmlen;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000170 xioctl(fd, MEMWRITEOOB, &oob);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000171 } else {
Denis Vlasenko962e3652009-02-19 01:17:12 +0000172 xlseek(fd, erase.start, SEEK_SET);
173 /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
174 bb_perror_msg("MTD %s failure", "seek");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000175 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000176 } */
177 xwrite(fd, &cleanmarker, sizeof(cleanmarker));
178 /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
179 bb_perror_msg("MTD %s failure", "write");
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000180 continue;
Denis Vlasenko962e3652009-02-19 01:17:12 +0000181 } */
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000182 }
183 if (!(flags & OPTION_Q))
184 printf(" Cleanmarker written at %x.", erase.start);
185 }
186 if (!(flags & OPTION_Q)) {
187 show_progress(&meminfo, &erase);
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000188 bb_putchar('\n');
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000189 }
190
Bernhard Reutner-Fischer7d9d2512009-02-18 13:26:29 +0000191 if (ENABLE_FEATURE_CLEAN_UP)
192 close(fd);
Bernhard Reutner-Fischer0d22d172009-02-18 13:23:46 +0000193 return EXIT_SUCCESS;
194}