2009-02-18 18:56:29 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2017-10-05 19:03:28 +05:30
|
|
|
/*
|
|
|
|
* eraseall.c -- erase the whole of a MTD device
|
2009-02-18 18:56:29 +05:30
|
|
|
*
|
|
|
|
* Ported to busybox from mtd-utils.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 Arcom Control System Ltd
|
|
|
|
*
|
|
|
|
* Renamed to flash_eraseall.c
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-02-18 18:53:46 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config FLASH_ERASEALL
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "flash_eraseall (5.9 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default n # doesn't build on Ubuntu 8.04
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: The flash_eraseall binary from mtd-utils as of git head c4c6a59eb.
|
|
|
|
//config: This utility is used to erase the whole MTD device.
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_FLASH_ERASEALL(APPLET(flash_eraseall, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
2017-08-07 19:30:25 +05:30
|
|
|
/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
|
2016-11-23 04:24:17 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FLASH_ERASEALL) += flash_eraseall.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define flash_eraseall_trivial_usage
|
2013-03-15 05:12:39 +05:30
|
|
|
//usage: "[-jNq] MTD_DEVICE"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define flash_eraseall_full_usage "\n\n"
|
|
|
|
//usage: "Erase an MTD device\n"
|
|
|
|
//usage: "\n -j Format the device for jffs2"
|
2013-03-15 05:12:39 +05:30
|
|
|
//usage: "\n -N Don't skip bad blocks"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -q Don't display progress messages"
|
|
|
|
|
2009-02-18 18:53:46 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include <mtd/mtd-user.h>
|
2009-11-27 17:56:17 +05:30
|
|
|
#include <linux/jffs2.h>
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2010-10-28 22:27:19 +05:30
|
|
|
#define OPTION_J (1 << 0)
|
2013-03-15 05:12:39 +05:30
|
|
|
#define OPTION_N (1 << 1)
|
|
|
|
#define OPTION_Q (1 << 2)
|
|
|
|
#define IS_NAND (1 << 3)
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2009-11-27 17:56:17 +05:30
|
|
|
/* mtd/jffs2-user.h used to have this atrocity:
|
|
|
|
extern int target_endian;
|
|
|
|
|
|
|
|
#define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); })
|
|
|
|
#define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); })
|
|
|
|
|
|
|
|
#define cpu_to_je16(x) ((jint16_t){t16(x)})
|
|
|
|
#define cpu_to_je32(x) ((jint32_t){t32(x)})
|
|
|
|
#define cpu_to_jemode(x) ((jmode_t){t32(x)})
|
|
|
|
|
|
|
|
#define je16_to_cpu(x) (t16((x).v16))
|
|
|
|
#define je32_to_cpu(x) (t32((x).v32))
|
|
|
|
#define jemode_to_cpu(x) (t32((x).m))
|
|
|
|
|
|
|
|
but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We always use native endianness */
|
|
|
|
#undef cpu_to_je16
|
|
|
|
#undef cpu_to_je32
|
|
|
|
#define cpu_to_je16(v) ((jint16_t){(v)})
|
|
|
|
#define cpu_to_je32(v) ((jint32_t){(v)})
|
2009-02-18 18:53:46 +05:30
|
|
|
|
|
|
|
static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
|
|
|
|
{
|
2009-11-27 17:56:17 +05:30
|
|
|
printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
|
|
|
|
(unsigned)meminfo->erasesize / 1024,
|
|
|
|
erase->start,
|
|
|
|
(unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
|
|
|
|
);
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2009-02-18 18:53:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2009-02-18 18:56:29 +05:30
|
|
|
int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
|
2009-02-18 18:53:46 +05:30
|
|
|
{
|
|
|
|
struct jffs2_unknown_node cleanmarker;
|
|
|
|
mtd_info_t meminfo;
|
2009-02-19 06:47:12 +05:30
|
|
|
int fd, clmpos, clmlen;
|
2009-02-18 18:53:46 +05:30
|
|
|
erase_info_t erase;
|
|
|
|
struct stat st;
|
|
|
|
unsigned int flags;
|
|
|
|
char *mtd_name;
|
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
flags = getopt32(argv, "^" "jNq" "\0" "=1");
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2009-02-19 06:47:12 +05:30
|
|
|
mtd_name = argv[optind];
|
2009-11-27 17:56:17 +05:30
|
|
|
fd = xopen(mtd_name, O_RDWR);
|
|
|
|
fstat(fd, &st);
|
2009-02-18 18:53:46 +05:30
|
|
|
if (!S_ISCHR(st.st_mode))
|
|
|
|
bb_error_msg_and_die("%s: not a char device", mtd_name);
|
|
|
|
|
|
|
|
xioctl(fd, MEMGETINFO, &meminfo);
|
|
|
|
erase.length = meminfo.erasesize;
|
2009-02-19 06:47:12 +05:30
|
|
|
if (meminfo.type == MTD_NANDFLASH)
|
|
|
|
flags |= IS_NAND;
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2009-02-19 06:47:12 +05:30
|
|
|
clmpos = 0;
|
|
|
|
clmlen = 8;
|
2009-02-18 18:53:46 +05:30
|
|
|
if (flags & OPTION_J) {
|
|
|
|
uint32_t *crc32_table;
|
|
|
|
|
2018-02-01 15:26:19 +05:30
|
|
|
crc32_table = crc32_new_table_le();
|
2009-02-18 18:53:46 +05:30
|
|
|
|
2009-02-19 06:47:12 +05:30
|
|
|
cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
|
|
|
|
cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
|
|
|
|
if (!(flags & IS_NAND))
|
|
|
|
cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
|
2009-02-18 18:53:46 +05:30
|
|
|
else {
|
|
|
|
struct nand_oobinfo oobinfo;
|
|
|
|
|
|
|
|
xioctl(fd, MEMGETOOBSEL, &oobinfo);
|
|
|
|
|
|
|
|
/* Check for autoplacement */
|
|
|
|
if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
|
|
|
|
/* Get the position of the free bytes */
|
|
|
|
clmpos = oobinfo.oobfree[0][0];
|
|
|
|
clmlen = oobinfo.oobfree[0][1];
|
|
|
|
if (clmlen > 8)
|
|
|
|
clmlen = 8;
|
2009-02-19 06:47:12 +05:30
|
|
|
if (clmlen == 0)
|
2009-04-29 17:32:57 +05:30
|
|
|
bb_error_msg_and_die("autoplacement selected and no empty space in oob");
|
2009-02-18 18:53:46 +05:30
|
|
|
} else {
|
|
|
|
/* Legacy mode */
|
|
|
|
switch (meminfo.oobsize) {
|
|
|
|
case 8:
|
|
|
|
clmpos = 6;
|
|
|
|
clmlen = 2;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
clmpos = 8;
|
2009-02-19 06:47:12 +05:30
|
|
|
/*clmlen = 8;*/
|
2009-02-18 18:53:46 +05:30
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
clmpos = 16;
|
2009-02-19 06:47:12 +05:30
|
|
|
/*clmlen = 8;*/
|
2009-02-18 18:53:46 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanmarker.totlen = cpu_to_je32(8);
|
|
|
|
}
|
|
|
|
|
2010-10-27 18:56:45 +05:30
|
|
|
cleanmarker.hdr_crc = cpu_to_je32(
|
|
|
|
crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
|
|
|
|
);
|
2009-02-18 18:53:46 +05:30
|
|
|
}
|
|
|
|
|
2009-02-19 06:47:12 +05:30
|
|
|
/* Don't want to destroy progress indicator by bb_error_msg's */
|
|
|
|
applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
|
|
|
|
|
2009-02-18 18:53:46 +05:30
|
|
|
for (erase.start = 0; erase.start < meminfo.size;
|
|
|
|
erase.start += meminfo.erasesize) {
|
2013-03-15 05:12:39 +05:30
|
|
|
if (!(flags & OPTION_N)) {
|
2009-02-18 18:53:46 +05:30
|
|
|
int ret;
|
|
|
|
loff_t offset = erase.start;
|
2009-02-18 18:56:29 +05:30
|
|
|
|
2009-02-18 18:53:46 +05:30
|
|
|
ret = ioctl(fd, MEMGETBADBLOCK, &offset);
|
|
|
|
if (ret > 0) {
|
|
|
|
if (!(flags & OPTION_Q))
|
2016-03-30 19:50:28 +05:30
|
|
|
printf("\nSkipping bad block at 0x%08x\n", erase.start);
|
2009-02-18 18:53:46 +05:30
|
|
|
continue;
|
2009-02-19 06:47:12 +05:30
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2009-02-18 18:53:46 +05:30
|
|
|
/* Black block table is not available on certain flash
|
|
|
|
* types e.g. NOR
|
|
|
|
*/
|
|
|
|
if (errno == EOPNOTSUPP) {
|
2013-03-15 05:12:39 +05:30
|
|
|
flags |= OPTION_N;
|
2009-02-19 06:47:12 +05:30
|
|
|
if (flags & IS_NAND)
|
|
|
|
bb_error_msg_and_die("bad block check not available");
|
2009-02-18 18:53:46 +05:30
|
|
|
} else {
|
2009-02-19 06:47:12 +05:30
|
|
|
bb_perror_msg_and_die("MEMGETBADBLOCK error");
|
2009-02-18 18:53:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & OPTION_Q))
|
|
|
|
show_progress(&meminfo, &erase);
|
|
|
|
|
|
|
|
xioctl(fd, MEMERASE, &erase);
|
|
|
|
|
|
|
|
/* format for JFFS2 ? */
|
|
|
|
if (!(flags & OPTION_J))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* write cleanmarker */
|
2009-02-19 06:47:12 +05:30
|
|
|
if (flags & IS_NAND) {
|
2009-02-18 18:53:46 +05:30
|
|
|
struct mtd_oob_buf oob;
|
2009-02-18 18:56:29 +05:30
|
|
|
|
2009-02-18 18:53:46 +05:30
|
|
|
oob.ptr = (unsigned char *) &cleanmarker;
|
|
|
|
oob.start = erase.start + clmpos;
|
|
|
|
oob.length = clmlen;
|
2009-02-19 06:47:12 +05:30
|
|
|
xioctl(fd, MEMWRITEOOB, &oob);
|
2009-02-18 18:53:46 +05:30
|
|
|
} else {
|
2009-02-19 06:47:12 +05:30
|
|
|
xlseek(fd, erase.start, SEEK_SET);
|
|
|
|
/* if (lseek(fd, erase.start, SEEK_SET) < 0) {
|
|
|
|
bb_perror_msg("MTD %s failure", "seek");
|
2009-02-18 18:53:46 +05:30
|
|
|
continue;
|
2009-02-19 06:47:12 +05:30
|
|
|
} */
|
|
|
|
xwrite(fd, &cleanmarker, sizeof(cleanmarker));
|
|
|
|
/* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
|
|
|
|
bb_perror_msg("MTD %s failure", "write");
|
2009-02-18 18:53:46 +05:30
|
|
|
continue;
|
2009-02-19 06:47:12 +05:30
|
|
|
} */
|
2009-02-18 18:53:46 +05:30
|
|
|
}
|
|
|
|
if (!(flags & OPTION_Q))
|
|
|
|
printf(" Cleanmarker written at %x.", erase.start);
|
|
|
|
}
|
|
|
|
if (!(flags & OPTION_Q)) {
|
|
|
|
show_progress(&meminfo, &erase);
|
2009-02-18 18:56:29 +05:30
|
|
|
bb_putchar('\n');
|
2009-02-18 18:53:46 +05:30
|
|
|
}
|
|
|
|
|
2009-02-18 18:56:29 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
2009-02-18 18:53:46 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|