flash_eraseall: shrink

function                                             old     new   delta
flash_eraseall_main                                 1044    1006     -38
This commit is contained in:
Denis Vlasenko 2009-02-19 01:17:12 +00:00
parent 02468c2063
commit 962e3656f6

View File

@ -16,9 +16,8 @@
#define OPTION_J (1 << 0) #define OPTION_J (1 << 0)
#define OPTION_Q (1 << 1) #define OPTION_Q (1 << 1)
#define _bitNAND (2) #define IS_NAND (1 << 2)
#define isNAND (1 << _bitNAND) #define BBTEST (1 << 3)
#define bbtest (1 << 3)
struct globals { struct globals {
/* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */ /* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */
@ -42,7 +41,7 @@ static uint32_t crc32(uint32_t val, const void *ss, int len,
static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
{ {
printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.", printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
meminfo->erasesize / 1024, erase->start, (unsigned)meminfo->erasesize / 1024, erase->start,
(unsigned long long) erase->start * 100 / meminfo->size); (unsigned long long) erase->start * 100 / meminfo->size);
fflush(stdout); fflush(stdout);
} }
@ -52,7 +51,7 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
{ {
struct jffs2_unknown_node cleanmarker; struct jffs2_unknown_node cleanmarker;
mtd_info_t meminfo; mtd_info_t meminfo;
int fd, clmpos = 0, clmlen = 8; int fd, clmpos, clmlen;
erase_info_t erase; erase_info_t erase;
struct stat st; struct stat st;
unsigned int flags; unsigned int flags;
@ -60,9 +59,9 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
INIT_G(); INIT_G();
opt_complementary = "=1"; opt_complementary = "=1";
flags = getopt32(argv, "jq"); flags = BBTEST | getopt32(argv, "jq");
mtd_name = *(argv + optind); mtd_name = argv[optind];
xstat(mtd_name, &st); xstat(mtd_name, &st);
if (!S_ISCHR(st.st_mode)) if (!S_ISCHR(st.st_mode))
bb_error_msg_and_die("%s: not a char device", mtd_name); bb_error_msg_and_die("%s: not a char device", mtd_name);
@ -70,19 +69,21 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
fd = xopen(mtd_name, O_RDWR); fd = xopen(mtd_name, O_RDWR);
xioctl(fd, MEMGETINFO, &meminfo); xioctl(fd, MEMGETINFO, &meminfo);
erase.length = meminfo.erasesize; erase.length = meminfo.erasesize;
flags |= bbtest | ((meminfo.type == MTD_NANDFLASH) << _bitNAND); if (meminfo.type == MTD_NANDFLASH)
flags |= IS_NAND;
clmpos = 0;
clmlen = 8;
if (flags & OPTION_J) { if (flags & OPTION_J) {
uint32_t *crc32_table; uint32_t *crc32_table;
crc32_table = crc32_filltable(NULL, 0); crc32_table = crc32_filltable(NULL, 0);
cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
if (!(flags & isNAND)) if (!(flags & IS_NAND))
cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
else { else {
struct nand_oobinfo oobinfo; struct nand_oobinfo oobinfo;
@ -91,13 +92,12 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
/* Check for autoplacement */ /* Check for autoplacement */
if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
/* Get the position of the free bytes */ /* 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]; clmpos = oobinfo.oobfree[0][0];
clmlen = oobinfo.oobfree[0][1]; clmlen = oobinfo.oobfree[0][1];
if (clmlen > 8) if (clmlen > 8)
clmlen = 8; clmlen = 8;
if (clmlen == 0)
bb_error_msg_and_die("Autoplacement selected and no empty space in oob");
} else { } else {
/* Legacy mode */ /* Legacy mode */
switch (meminfo.oobsize) { switch (meminfo.oobsize) {
@ -107,24 +107,27 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
break; break;
case 16: case 16:
clmpos = 8; clmpos = 8;
clmlen = 8; /*clmlen = 8;*/
break; break;
case 64: case 64:
clmpos = 16; clmpos = 16;
clmlen = 8; /*clmlen = 8;*/
break; break;
} }
} }
cleanmarker.totlen = cpu_to_je32(8); cleanmarker.totlen = cpu_to_je32(8);
} }
cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4,
crc32_table)); crc32_table));
} }
/* Don't want to destroy progress indicator by bb_error_msg's */
applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
for (erase.start = 0; erase.start < meminfo.size; for (erase.start = 0; erase.start < meminfo.size;
erase.start += meminfo.erasesize) { erase.start += meminfo.erasesize) {
if (flags & bbtest) { if (flags & BBTEST) {
int ret; int ret;
loff_t offset = erase.start; loff_t offset = erase.start;
@ -133,18 +136,17 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
if (!(flags & OPTION_Q)) if (!(flags & OPTION_Q))
bb_info_msg("\nSkipping bad block at 0x%08x", erase.start); bb_info_msg("\nSkipping bad block at 0x%08x", erase.start);
continue; continue;
} else if (ret < 0) { }
if (ret < 0) {
/* Black block table is not available on certain flash /* Black block table is not available on certain flash
* types e.g. NOR * types e.g. NOR
*/ */
if (errno == EOPNOTSUPP) { if (errno == EOPNOTSUPP) {
flags =~ bbtest; flags = ~BBTEST;
if (flags & isNAND) if (flags & IS_NAND)
bb_error_msg_and_die("%s: Bad block check not available", bb_error_msg_and_die("bad block check not available");
mtd_name);
} else { } else {
bb_perror_msg_and_die("\n%s: MTD %s failure", bb_perror_msg_and_die("MEMGETBADBLOCK error");
mtd_name, "get bad block ");
} }
} }
} }
@ -159,22 +161,24 @@ int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
continue; continue;
/* write cleanmarker */ /* write cleanmarker */
if (flags & isNAND) { if (flags & IS_NAND) {
struct mtd_oob_buf oob; struct mtd_oob_buf oob;
oob.ptr = (unsigned char *) &cleanmarker; oob.ptr = (unsigned char *) &cleanmarker;
oob.start = erase.start + clmpos; oob.start = erase.start + clmpos;
oob.length = clmlen; oob.length = clmlen;
xioctl (fd, MEMWRITEOOB, &oob); xioctl(fd, MEMWRITEOOB, &oob);
} else { } else {
if (lseek (fd, erase.start, SEEK_SET) < 0) { xlseek(fd, erase.start, SEEK_SET);
bb_perror_msg("\n%s: MTD %s failure", mtd_name, "seek"); /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
bb_perror_msg("MTD %s failure", "seek");
continue; continue;
} } */
if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) { xwrite(fd, &cleanmarker, sizeof(cleanmarker));
bb_perror_msg("\n%s: MTD %s failure", mtd_name, "write"); /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
bb_perror_msg("MTD %s failure", "write");
continue; continue;
} } */
} }
if (!(flags & OPTION_Q)) if (!(flags & OPTION_Q))
printf(" Cleanmarker written at %x.", erase.start); printf(" Cleanmarker written at %x.", erase.start);