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