From 0d22d1755c2c55ad45b01458bbf2f09aab4308d2 Mon Sep 17 00:00:00 2001 From: Bernhard Reutner-Fischer Date: Wed, 18 Feb 2009 13:23:46 +0000 Subject: [PATCH] - add flash_eraseall This is the result after converting mtd-utils' flash_eraseall to BB. The functionality given by this patch almost the same except that this one does not support long options. I needed this tool a system which does not have a lot of flash for RFS and merging this into BB as the only way out. [bigeasy@]$ ./scripts/bloat-o-meter bb_wo_fl bb_w_fl function old new delta flash_eraseall_main - 1072 +1072 show_progress - 62 +62 packed_usage 25156 25176 +20 applet_names 1958 1973 +15 applet_main 2352 2360 +8 target_endian - 4 +4 applet_nameofs 588 590 +2 applet_install_loc 147 148 +1 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 5/0 up/down: 1184/0) Total: 1184 bytes [bigeasy@]$ size miscutils/flash_eraseall.o text data bss dec hex filename 1586 4 0 1590 636 miscutils/flash_eraseall.o Signed-off-by: Benedigt Spranger Signed-off-by: Sebastian Andrzej Siewior --- Makefile.flags | 1 + include/applets.h | 2 + include/usage.h | 8 ++ miscutils/Config.in | 7 ++ miscutils/Kbuild | 1 + miscutils/flash_eraseall.c | 188 +++++++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 miscutils/flash_eraseall.c diff --git a/Makefile.flags b/Makefile.flags index ad8d5e1f9..4c24ad690 100644 --- a/Makefile.flags +++ b/Makefile.flags @@ -23,6 +23,7 @@ CFLAGS += $(call cc-option,-Wwrite-strings,) CFLAGS += $(call cc-option,-Wundef,) CFLAGS += $(call cc-option,-Wstrict-prototypes,) CFLAGS += $(call cc-option,-Wunused -Wunused-parameter,) +CFLAGS += $(call cc-option,-Wunused-function -Wunused-value,) CFLAGS += $(call cc-option,-Wmissing-prototypes -Wmissing-declarations,) # warn about C99 declaration after statement CFLAGS += $(call cc-option,-Wdeclaration-after-statement,) diff --git a/include/applets.h b/include/applets.h index af21a0d4a..15dcbdf5e 100644 --- a/include/applets.h +++ b/include/applets.h @@ -155,6 +155,8 @@ USE_FDISK(APPLET(fdisk, _BB_DIR_SBIN, _BB_SUID_NEVER)) USE_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_NEVER, fgrep)) USE_FIND(APPLET_NOEXEC(find, find, _BB_DIR_USR_BIN, _BB_SUID_NEVER, find)) USE_FINDFS(APPLET(findfs, _BB_DIR_SBIN, _BB_SUID_MAYBE)) +//USE_FLASH_ERASEALL(APPLET_ODDNAME(flash_eraseall, flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_NEVER, flash_eraseall)) +USE_FLASH_ERASEALL(APPLET(flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) USE_FOLD(APPLET(fold, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) USE_FREE(APPLET(free, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) USE_FREERAMDISK(APPLET(freeramdisk, _BB_DIR_SBIN, _BB_SUID_NEVER)) diff --git a/include/usage.h b/include/usage.h index 52af47e58..0e606609d 100644 --- a/include/usage.h +++ b/include/usage.h @@ -1217,6 +1217,14 @@ "$ find / -name passwd\n" \ "/etc/passwd\n" +#define flash_eraseall_trivial_usage \ + "[-jq] MTD_DEVICE" +#define flash_eraseall_full_usage "\n\n" \ + "Erase an MTD device\n" \ + "\nOptions:" \ + "\n -j format the device for jffs2" \ + "\n -q don't display progress messages" + #define fold_trivial_usage \ "[-bs] [-w WIDTH] [FILE]" #define fold_full_usage "\n\n" \ diff --git a/miscutils/Config.in b/miscutils/Config.in index c8d7253d8..62be3e63e 100644 --- a/miscutils/Config.in +++ b/miscutils/Config.in @@ -250,6 +250,13 @@ config FBSPLASH "NN" (ASCII decimal number) - percentage to show on progress bar "exit" - well you guessed it +config FLASH_ERASEALL + bool "flash_eraseall" + default n + help + The flash_eraseall binary from mtd-utils as of git head c4c6a59eb. + This utility is used to erase the whole MTD device. + config IONICE bool "ionice" default n diff --git a/miscutils/Kbuild b/miscutils/Kbuild index 7665130e5..23d7d8d49 100644 --- a/miscutils/Kbuild +++ b/miscutils/Kbuild @@ -16,6 +16,7 @@ lib-$(CONFIG_DEVFSD) += devfsd.o lib-$(CONFIG_DEVMEM) += devmem.o lib-$(CONFIG_EJECT) += eject.o lib-$(CONFIG_FBSPLASH) += fbsplash.o +lib-$(CONFIG_FLASH_ERASEALL) += flash_eraseall.o lib-$(CONFIG_IONICE) += ionice.o lib-$(CONFIG_HDPARM) += hdparm.o lib-$(CONFIG_INOTIFYD) += inotifyd.o diff --git a/miscutils/flash_eraseall.c b/miscutils/flash_eraseall.c new file mode 100644 index 000000000..79028d780 --- /dev/null +++ b/miscutils/flash_eraseall.c @@ -0,0 +1,188 @@ +/* eraseall.c -- erase the whole of a MTD device + + Ported to busybox from mtd-utils. + + Copyright (C) 2000 Arcom Control System Ltd + + Renamed to flash_eraseall.c + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include "libbb.h" + +#include +#include + +#define OPTION_J (1 << 0) +#define OPTION_Q (1 << 1) + +int target_endian = __BYTE_ORDER; + +static uint32_t crc32(uint32_t val, const void *ss, int len, + uint32_t *crc32_table) +{ + const unsigned char *s = ss; + while (--len >= 0) + val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8); + return val; +} + +static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) +{ + printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.", + meminfo->erasesize / 1024, erase->start, + (unsigned long long) erase->start * 100 / meminfo->size); + fflush(stdout); +} + +int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int flash_eraseall_main(int argc, char **argv) +{ + struct jffs2_unknown_node cleanmarker; + mtd_info_t meminfo; + int fd, clmpos = 0, clmlen = 8; + erase_info_t erase; + struct stat st; + int isNAND, bbtest = 1; + unsigned int flags; + char *mtd_name; + + opt_complementary = "=1"; + flags = getopt32(argv, "jq"); + + mtd_name = *(argv + optind); + xstat(mtd_name, &st); + if (!S_ISCHR(st.st_mode)) + bb_error_msg_and_die("%s: not a char device", mtd_name); + + fd = xopen(mtd_name, O_RDWR); + + xioctl(fd, MEMGETINFO, &meminfo); + + erase.length = meminfo.erasesize; + isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0; + + if (flags & OPTION_J) { + uint32_t *crc32_table; + + crc32_table = crc32_filltable(NULL, 0); + + cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); + cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); + if (!isNAND) + cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); + 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 */ + if (!oobinfo.oobfree[0][1]) + bb_error_msg_and_die("Autoplacement selected and no empty space in oob"); + + clmpos = oobinfo.oobfree[0][0]; + clmlen = oobinfo.oobfree[0][1]; + if (clmlen > 8) + clmlen = 8; + } else { + /* Legacy mode */ + switch (meminfo.oobsize) { + case 8: + clmpos = 6; + clmlen = 2; + break; + case 16: + clmpos = 8; + clmlen = 8; + break; + case 64: + clmpos = 16; + clmlen = 8; + break; + } + } + cleanmarker.totlen = cpu_to_je32(8); + } + + cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, + crc32_table)); + } + + for (erase.start = 0; erase.start < meminfo.size; + erase.start += meminfo.erasesize) { + if (bbtest) { + int ret; + + loff_t offset = erase.start; + ret = ioctl(fd, MEMGETBADBLOCK, &offset); + if (ret > 0) { + if (!(flags & OPTION_Q)) + bb_info_msg("\nSkipping bad block at 0x%08x", erase.start); + continue; + } else if (ret < 0) { + /* Black block table is not available on certain flash + * types e.g. NOR + */ + if (errno == EOPNOTSUPP) { + bbtest = 0; + if (isNAND) + bb_error_msg_and_die("%s: Bad block check not available", + mtd_name); + } else { + bb_error_msg_and_die("\n%s: MTD get bad block failed: %s", + mtd_name, strerror(errno)); + } + } + } + + if (!(flags & OPTION_Q)) + show_progress(&meminfo, &erase); + + xioctl(fd, MEMERASE, &erase); + + /* format for JFFS2 ? */ + if (!(flags & OPTION_J)) + continue; + + /* write cleanmarker */ + if (isNAND) { + struct mtd_oob_buf oob; + oob.ptr = (unsigned char *) &cleanmarker; + oob.start = erase.start + clmpos; + oob.length = clmlen; + xioctl (fd, MEMWRITEOOB, &oob); + } else { + if (lseek (fd, erase.start, SEEK_SET) < 0) { + bb_error_msg("\n%s: MTD lseek failure: %s", mtd_name, strerror(errno)); + continue; + } + if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) { + bb_error_msg("\n%s: MTD write failure: %s", mtd_name, strerror(errno)); + continue; + } + } + if (!(flags & OPTION_Q)) + printf(" Cleanmarker written at %x.", erase.start); + } + if (!(flags & OPTION_Q)) { + show_progress(&meminfo, &erase); + printf("\n"); + } + + return EXIT_SUCCESS; +} -- 2.25.1