Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * eraseall.c -- erase the whole of a MTD device |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 4 | * |
| 5 | * Ported to busybox from mtd-utils. |
| 6 | * |
| 7 | * Copyright (C) 2000 Arcom Control System Ltd |
| 8 | * |
| 9 | * Renamed to flash_eraseall.c |
| 10 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 12 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 13 | //config:config FLASH_ERASEALL |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 14 | //config: bool "flash_eraseall (5.5 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 15 | //config: default n # doesn't build on Ubuntu 8.04 |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 17 | //config: The flash_eraseall binary from mtd-utils as of git head c4c6a59eb. |
| 18 | //config: This utility is used to erase the whole MTD device. |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 19 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 20 | //applet:IF_FLASH_ERASEALL(APPLET(flash_eraseall, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 21 | /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */ |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 22 | |
| 23 | //kbuild:lib-$(CONFIG_FLASH_ERASEALL) += flash_eraseall.o |
| 24 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define flash_eraseall_trivial_usage |
Alexander Shiyan | 45dc96c | 2013-03-15 00:42:39 +0100 | [diff] [blame] | 26 | //usage: "[-jNq] MTD_DEVICE" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | //usage:#define flash_eraseall_full_usage "\n\n" |
| 28 | //usage: "Erase an MTD device\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | //usage: "\n -j Format the device for jffs2" |
Alexander Shiyan | 45dc96c | 2013-03-15 00:42:39 +0100 | [diff] [blame] | 30 | //usage: "\n -N Don't skip bad blocks" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 31 | //usage: "\n -q Don't display progress messages" |
| 32 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 33 | #include "libbb.h" |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 34 | #include <mtd/mtd-user.h> |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 35 | #include <linux/jffs2.h> |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 36 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 37 | #define OPTION_J (1 << 0) |
Alexander Shiyan | 45dc96c | 2013-03-15 00:42:39 +0100 | [diff] [blame] | 38 | #define OPTION_N (1 << 1) |
| 39 | #define OPTION_Q (1 << 2) |
| 40 | #define IS_NAND (1 << 3) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 42 | /* mtd/jffs2-user.h used to have this atrocity: |
| 43 | extern int target_endian; |
| 44 | |
| 45 | #define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); }) |
| 46 | #define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); }) |
| 47 | |
| 48 | #define cpu_to_je16(x) ((jint16_t){t16(x)}) |
| 49 | #define cpu_to_je32(x) ((jint32_t){t32(x)}) |
| 50 | #define cpu_to_jemode(x) ((jmode_t){t32(x)}) |
| 51 | |
| 52 | #define je16_to_cpu(x) (t16((x).v16)) |
| 53 | #define je32_to_cpu(x) (t32((x).v32)) |
| 54 | #define jemode_to_cpu(x) (t32((x).m)) |
| 55 | |
| 56 | but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore) |
| 57 | */ |
| 58 | |
| 59 | /* We always use native endianness */ |
| 60 | #undef cpu_to_je16 |
| 61 | #undef cpu_to_je32 |
| 62 | #define cpu_to_je16(v) ((jint16_t){(v)}) |
| 63 | #define cpu_to_je32(v) ((jint32_t){(v)}) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 64 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 65 | static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) |
| 66 | { |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 67 | printf("\rErasing %u Kibyte @ %x - %2u%% complete.", |
| 68 | (unsigned)meminfo->erasesize / 1024, |
| 69 | erase->start, |
| 70 | (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size) |
| 71 | ); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 72 | fflush_all(); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 76 | int flash_eraseall_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 77 | { |
| 78 | struct jffs2_unknown_node cleanmarker; |
| 79 | mtd_info_t meminfo; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 80 | int fd, clmpos, clmlen; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 81 | erase_info_t erase; |
| 82 | struct stat st; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 83 | unsigned int flags; |
| 84 | char *mtd_name; |
| 85 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 86 | flags = getopt32(argv, "^" "jNq" "\0" "=1"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 88 | mtd_name = argv[optind]; |
Denys Vlasenko | 86cfb70 | 2009-11-27 13:26:17 +0100 | [diff] [blame] | 89 | fd = xopen(mtd_name, O_RDWR); |
| 90 | fstat(fd, &st); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 91 | if (!S_ISCHR(st.st_mode)) |
| 92 | bb_error_msg_and_die("%s: not a char device", mtd_name); |
| 93 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 94 | xioctl(fd, MEMGETINFO, &meminfo); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 95 | erase.length = meminfo.erasesize; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 96 | if (meminfo.type == MTD_NANDFLASH) |
| 97 | flags |= IS_NAND; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 99 | clmpos = 0; |
| 100 | clmlen = 8; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 101 | if (flags & OPTION_J) { |
| 102 | uint32_t *crc32_table; |
| 103 | |
Denys Vlasenko | ddacb03 | 2018-02-01 10:56:19 +0100 | [diff] [blame] | 104 | crc32_table = crc32_new_table_le(); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 106 | cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); |
| 107 | cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); |
| 108 | if (!(flags & IS_NAND)) |
| 109 | cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node)); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 110 | else { |
| 111 | struct nand_oobinfo oobinfo; |
| 112 | |
| 113 | xioctl(fd, MEMGETOOBSEL, &oobinfo); |
| 114 | |
| 115 | /* Check for autoplacement */ |
| 116 | if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { |
| 117 | /* Get the position of the free bytes */ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 118 | clmpos = oobinfo.oobfree[0][0]; |
| 119 | clmlen = oobinfo.oobfree[0][1]; |
| 120 | if (clmlen > 8) |
| 121 | clmlen = 8; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 122 | if (clmlen == 0) |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 123 | 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] | 124 | } else { |
| 125 | /* Legacy mode */ |
| 126 | switch (meminfo.oobsize) { |
| 127 | case 8: |
| 128 | clmpos = 6; |
| 129 | clmlen = 2; |
| 130 | break; |
| 131 | case 16: |
| 132 | clmpos = 8; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 133 | /*clmlen = 8;*/ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 134 | break; |
| 135 | case 64: |
| 136 | clmpos = 16; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 137 | /*clmlen = 8;*/ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | cleanmarker.totlen = cpu_to_je32(8); |
| 142 | } |
| 143 | |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 144 | cleanmarker.hdr_crc = cpu_to_je32( |
| 145 | crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table) |
| 146 | ); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 149 | /* Don't want to destroy progress indicator by bb_error_msg's */ |
| 150 | applet_name = xasprintf("\n%s: %s", applet_name, mtd_name); |
| 151 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 152 | for (erase.start = 0; erase.start < meminfo.size; |
| 153 | erase.start += meminfo.erasesize) { |
Alexander Shiyan | 45dc96c | 2013-03-15 00:42:39 +0100 | [diff] [blame] | 154 | if (!(flags & OPTION_N)) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 155 | int ret; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 156 | loff_t offset = erase.start; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 157 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 158 | ret = ioctl(fd, MEMGETBADBLOCK, &offset); |
| 159 | if (ret > 0) { |
| 160 | if (!(flags & OPTION_Q)) |
Denys Vlasenko | 066e76b | 2016-03-30 16:20:28 +0200 | [diff] [blame] | 161 | printf("\nSkipping bad block at 0x%08x\n", erase.start); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 162 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 163 | } |
| 164 | if (ret < 0) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 165 | /* Black block table is not available on certain flash |
| 166 | * types e.g. NOR |
| 167 | */ |
| 168 | if (errno == EOPNOTSUPP) { |
Alexander Shiyan | 45dc96c | 2013-03-15 00:42:39 +0100 | [diff] [blame] | 169 | flags |= OPTION_N; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 170 | if (flags & IS_NAND) |
| 171 | bb_error_msg_and_die("bad block check not available"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 172 | } else { |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 173 | bb_perror_msg_and_die("MEMGETBADBLOCK error"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (!(flags & OPTION_Q)) |
| 179 | show_progress(&meminfo, &erase); |
| 180 | |
| 181 | xioctl(fd, MEMERASE, &erase); |
| 182 | |
| 183 | /* format for JFFS2 ? */ |
| 184 | if (!(flags & OPTION_J)) |
| 185 | continue; |
| 186 | |
| 187 | /* write cleanmarker */ |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 188 | if (flags & IS_NAND) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 189 | struct mtd_oob_buf oob; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 190 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 191 | oob.ptr = (unsigned char *) &cleanmarker; |
| 192 | oob.start = erase.start + clmpos; |
| 193 | oob.length = clmlen; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 194 | xioctl(fd, MEMWRITEOOB, &oob); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 195 | } else { |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 196 | xlseek(fd, erase.start, SEEK_SET); |
| 197 | /* if (lseek(fd, erase.start, SEEK_SET) < 0) { |
| 198 | bb_perror_msg("MTD %s failure", "seek"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 199 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 200 | } */ |
| 201 | xwrite(fd, &cleanmarker, sizeof(cleanmarker)); |
| 202 | /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) { |
| 203 | bb_perror_msg("MTD %s failure", "write"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 204 | continue; |
Denis Vlasenko | 962e365 | 2009-02-19 01:17:12 +0000 | [diff] [blame] | 205 | } */ |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 206 | } |
| 207 | if (!(flags & OPTION_Q)) |
| 208 | printf(" Cleanmarker written at %x.", erase.start); |
| 209 | } |
| 210 | if (!(flags & OPTION_Q)) { |
| 211 | show_progress(&meminfo, &erase); |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 212 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 215 | if (ENABLE_FEATURE_CLEAN_UP) |
| 216 | close(fd); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 217 | return EXIT_SUCCESS; |
| 218 | } |