Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 2 | /* eraseall.c -- erase the whole of a MTD device |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 3 | * |
| 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-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "libbb.h" |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 14 | #include <mtd/mtd-user.h> |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 15 | #include <linux/jffs2.h> |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 16 | |
| 17 | #define OPTION_J (1 << 0) |
| 18 | #define OPTION_Q (1 << 1) |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 19 | #define IS_NAND (1 << 2) |
| 20 | #define BBTEST (1 << 3) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 21 | |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 22 | /* mtd/jffs2-user.h used to have this atrocity: |
| 23 | extern int target_endian; |
| 24 | |
| 25 | #define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); }) |
| 26 | #define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); }) |
| 27 | |
| 28 | #define cpu_to_je16(x) ((jint16_t){t16(x)}) |
| 29 | #define cpu_to_je32(x) ((jint32_t){t32(x)}) |
| 30 | #define cpu_to_jemode(x) ((jmode_t){t32(x)}) |
| 31 | |
| 32 | #define je16_to_cpu(x) (t16((x).v16)) |
| 33 | #define je32_to_cpu(x) (t32((x).v32)) |
| 34 | #define jemode_to_cpu(x) (t32((x).m)) |
| 35 | |
| 36 | but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore) |
| 37 | */ |
| 38 | |
| 39 | /* We always use native endianness */ |
| 40 | #undef cpu_to_je16 |
| 41 | #undef cpu_to_je32 |
| 42 | #define cpu_to_je16(v) ((jint16_t){(v)}) |
| 43 | #define cpu_to_je32(v) ((jint32_t){(v)}) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 44 | |
| 45 | static uint32_t crc32(uint32_t val, const void *ss, int len, |
| 46 | uint32_t *crc32_table) |
| 47 | { |
| 48 | const unsigned char *s = ss; |
| 49 | while (--len >= 0) |
| 50 | val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8); |
| 51 | return val; |
| 52 | } |
| 53 | |
| 54 | static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) |
| 55 | { |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 56 | printf("\rErasing %u Kibyte @ %x - %2u%% complete.", |
| 57 | (unsigned)meminfo->erasesize / 1024, |
| 58 | erase->start, |
| 59 | (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size) |
| 60 | ); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 61 | fflush_all(); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 65 | int flash_eraseall_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 66 | { |
| 67 | struct jffs2_unknown_node cleanmarker; |
| 68 | mtd_info_t meminfo; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 69 | int fd, clmpos, clmlen; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 70 | erase_info_t erase; |
| 71 | struct stat st; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 72 | unsigned int flags; |
| 73 | char *mtd_name; |
| 74 | |
| 75 | opt_complementary = "=1"; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 76 | flags = BBTEST | getopt32(argv, "jq"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 78 | mtd_name = argv[optind]; |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 79 | fd = xopen(mtd_name, O_RDWR); |
| 80 | fstat(fd, &st); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 81 | if (!S_ISCHR(st.st_mode)) |
| 82 | bb_error_msg_and_die("%s: not a char device", mtd_name); |
| 83 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 84 | xioctl(fd, MEMGETINFO, &meminfo); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 85 | erase.length = meminfo.erasesize; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 86 | if (meminfo.type == MTD_NANDFLASH) |
| 87 | flags |= IS_NAND; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 89 | clmpos = 0; |
| 90 | clmlen = 8; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 91 | if (flags & OPTION_J) { |
| 92 | uint32_t *crc32_table; |
| 93 | |
| 94 | crc32_table = crc32_filltable(NULL, 0); |
| 95 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 96 | cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 97 | cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); |
| 98 | if (!(flags & IS_NAND)) |
| 99 | cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node)); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 100 | else { |
| 101 | struct nand_oobinfo oobinfo; |
| 102 | |
| 103 | xioctl(fd, MEMGETOOBSEL, &oobinfo); |
| 104 | |
| 105 | /* Check for autoplacement */ |
| 106 | if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { |
| 107 | /* Get the position of the free bytes */ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 108 | clmpos = oobinfo.oobfree[0][0]; |
| 109 | clmlen = oobinfo.oobfree[0][1]; |
| 110 | if (clmlen > 8) |
| 111 | clmlen = 8; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 112 | if (clmlen == 0) |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 113 | bb_error_msg_and_die("autoplacement selected and no empty space in oob"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 114 | } else { |
| 115 | /* Legacy mode */ |
| 116 | switch (meminfo.oobsize) { |
| 117 | case 8: |
| 118 | clmpos = 6; |
| 119 | clmlen = 2; |
| 120 | break; |
| 121 | case 16: |
| 122 | clmpos = 8; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 123 | /*clmlen = 8;*/ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 124 | break; |
| 125 | case 64: |
| 126 | clmpos = 16; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 127 | /*clmlen = 8;*/ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | } |
| 131 | cleanmarker.totlen = cpu_to_je32(8); |
| 132 | } |
| 133 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 134 | cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 135 | crc32_table)); |
| 136 | } |
| 137 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 138 | /* Don't want to destroy progress indicator by bb_error_msg's */ |
| 139 | applet_name = xasprintf("\n%s: %s", applet_name, mtd_name); |
| 140 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 141 | for (erase.start = 0; erase.start < meminfo.size; |
| 142 | erase.start += meminfo.erasesize) { |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 143 | if (flags & BBTEST) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 144 | int ret; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 145 | loff_t offset = erase.start; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 146 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 147 | ret = ioctl(fd, MEMGETBADBLOCK, &offset); |
| 148 | if (ret > 0) { |
| 149 | if (!(flags & OPTION_Q)) |
| 150 | bb_info_msg("\nSkipping bad block at 0x%08x", erase.start); |
| 151 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 152 | } |
| 153 | if (ret < 0) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 154 | /* Black block table is not available on certain flash |
| 155 | * types e.g. NOR |
| 156 | */ |
| 157 | if (errno == EOPNOTSUPP) { |
Denis Vlasenko | e261bba | 2009-02-24 16:47:03 +0000 | [diff] [blame] | 158 | flags &= ~BBTEST; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 159 | if (flags & IS_NAND) |
| 160 | bb_error_msg_and_die("bad block check not available"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 161 | } else { |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 162 | bb_perror_msg_and_die("MEMGETBADBLOCK error"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (!(flags & OPTION_Q)) |
| 168 | show_progress(&meminfo, &erase); |
| 169 | |
| 170 | xioctl(fd, MEMERASE, &erase); |
| 171 | |
| 172 | /* format for JFFS2 ? */ |
| 173 | if (!(flags & OPTION_J)) |
| 174 | continue; |
| 175 | |
| 176 | /* write cleanmarker */ |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 177 | if (flags & IS_NAND) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 178 | struct mtd_oob_buf oob; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 179 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 180 | oob.ptr = (unsigned char *) &cleanmarker; |
| 181 | oob.start = erase.start + clmpos; |
| 182 | oob.length = clmlen; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 183 | xioctl(fd, MEMWRITEOOB, &oob); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 184 | } else { |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 185 | xlseek(fd, erase.start, SEEK_SET); |
| 186 | /* if (lseek(fd, erase.start, SEEK_SET) < 0) { |
| 187 | bb_perror_msg("MTD %s failure", "seek"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 188 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 189 | } */ |
| 190 | xwrite(fd, &cleanmarker, sizeof(cleanmarker)); |
| 191 | /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) { |
| 192 | bb_perror_msg("MTD %s failure", "write"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 193 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 194 | } */ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 195 | } |
| 196 | if (!(flags & OPTION_Q)) |
| 197 | printf(" Cleanmarker written at %x.", erase.start); |
| 198 | } |
| 199 | if (!(flags & OPTION_Q)) { |
| 200 | show_progress(&meminfo, &erase); |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 201 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 204 | if (ENABLE_FEATURE_CLEAN_UP) |
| 205 | close(fd); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 206 | return EXIT_SUCCESS; |
| 207 | } |