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> |
| 15 | #include <mtd/jffs2-user.h> |
| 16 | |
| 17 | #define OPTION_J (1 << 0) |
| 18 | #define OPTION_Q (1 << 1) |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 19 | #define _bitNAND (2) |
| 20 | #define isNAND (1 << _bitNAND) |
| 21 | #define bbtest (1 << 3) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 22 | |
Bernhard Reutner-Fischer | 8af23f8 | 2009-02-18 13:42:51 +0000 | [diff] [blame^] | 23 | struct globals { |
| 24 | /* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */ |
| 25 | int tgt_endian; |
| 26 | }; |
| 27 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 28 | #define target_endian (G.tgt_endian) |
| 29 | #define INIT_G() do { \ |
| 30 | target_endian = __BYTE_ORDER; \ |
| 31 | } while (0) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 32 | |
| 33 | static uint32_t crc32(uint32_t val, const void *ss, int len, |
| 34 | uint32_t *crc32_table) |
| 35 | { |
| 36 | const unsigned char *s = ss; |
| 37 | while (--len >= 0) |
| 38 | val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8); |
| 39 | return val; |
| 40 | } |
| 41 | |
| 42 | static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) |
| 43 | { |
| 44 | printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.", |
| 45 | meminfo->erasesize / 1024, erase->start, |
| 46 | (unsigned long long) erase->start * 100 / meminfo->size); |
| 47 | fflush(stdout); |
| 48 | } |
| 49 | |
| 50 | int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 51 | int flash_eraseall_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 52 | { |
| 53 | struct jffs2_unknown_node cleanmarker; |
| 54 | mtd_info_t meminfo; |
| 55 | int fd, clmpos = 0, clmlen = 8; |
| 56 | erase_info_t erase; |
| 57 | struct stat st; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 58 | unsigned int flags; |
| 59 | char *mtd_name; |
| 60 | |
Bernhard Reutner-Fischer | 8af23f8 | 2009-02-18 13:42:51 +0000 | [diff] [blame^] | 61 | INIT_G(); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 62 | opt_complementary = "=1"; |
| 63 | flags = getopt32(argv, "jq"); |
| 64 | |
| 65 | mtd_name = *(argv + optind); |
| 66 | xstat(mtd_name, &st); |
| 67 | if (!S_ISCHR(st.st_mode)) |
| 68 | bb_error_msg_and_die("%s: not a char device", mtd_name); |
| 69 | |
| 70 | fd = xopen(mtd_name, O_RDWR); |
| 71 | |
| 72 | xioctl(fd, MEMGETINFO, &meminfo); |
| 73 | |
| 74 | erase.length = meminfo.erasesize; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 75 | flags |= bbtest | ((meminfo.type == MTD_NANDFLASH) << _bitNAND); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 76 | |
| 77 | if (flags & OPTION_J) { |
| 78 | uint32_t *crc32_table; |
| 79 | |
| 80 | crc32_table = crc32_filltable(NULL, 0); |
| 81 | |
| 82 | cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); |
| 83 | cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 84 | if (!(flags & isNAND)) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 85 | cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); |
| 86 | else { |
| 87 | struct nand_oobinfo oobinfo; |
| 88 | |
| 89 | xioctl(fd, MEMGETOOBSEL, &oobinfo); |
| 90 | |
| 91 | /* Check for autoplacement */ |
| 92 | if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) { |
| 93 | /* Get the position of the free bytes */ |
| 94 | if (!oobinfo.oobfree[0][1]) |
| 95 | bb_error_msg_and_die("Autoplacement selected and no empty space in oob"); |
| 96 | |
| 97 | clmpos = oobinfo.oobfree[0][0]; |
| 98 | clmlen = oobinfo.oobfree[0][1]; |
| 99 | if (clmlen > 8) |
| 100 | clmlen = 8; |
| 101 | } else { |
| 102 | /* Legacy mode */ |
| 103 | switch (meminfo.oobsize) { |
| 104 | case 8: |
| 105 | clmpos = 6; |
| 106 | clmlen = 2; |
| 107 | break; |
| 108 | case 16: |
| 109 | clmpos = 8; |
| 110 | clmlen = 8; |
| 111 | break; |
| 112 | case 64: |
| 113 | clmpos = 16; |
| 114 | clmlen = 8; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | cleanmarker.totlen = cpu_to_je32(8); |
| 119 | } |
| 120 | |
| 121 | cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, |
| 122 | crc32_table)); |
| 123 | } |
| 124 | |
| 125 | for (erase.start = 0; erase.start < meminfo.size; |
| 126 | erase.start += meminfo.erasesize) { |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 127 | if (flags & bbtest) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 128 | int ret; |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 129 | loff_t offset = erase.start; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 130 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 131 | ret = ioctl(fd, MEMGETBADBLOCK, &offset); |
| 132 | if (ret > 0) { |
| 133 | if (!(flags & OPTION_Q)) |
| 134 | bb_info_msg("\nSkipping bad block at 0x%08x", erase.start); |
| 135 | continue; |
| 136 | } else if (ret < 0) { |
| 137 | /* Black block table is not available on certain flash |
| 138 | * types e.g. NOR |
| 139 | */ |
| 140 | if (errno == EOPNOTSUPP) { |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 141 | flags =~ bbtest; |
| 142 | if (flags & isNAND) |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 143 | bb_error_msg_and_die("%s: Bad block check not available", |
| 144 | mtd_name); |
| 145 | } else { |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 146 | bb_perror_msg_and_die("\n%s: MTD %s failure", |
| 147 | mtd_name, "get bad block "); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!(flags & OPTION_Q)) |
| 153 | show_progress(&meminfo, &erase); |
| 154 | |
| 155 | xioctl(fd, MEMERASE, &erase); |
| 156 | |
| 157 | /* format for JFFS2 ? */ |
| 158 | if (!(flags & OPTION_J)) |
| 159 | continue; |
| 160 | |
| 161 | /* write cleanmarker */ |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 162 | if (flags & isNAND) { |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 163 | struct mtd_oob_buf oob; |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 164 | |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 165 | oob.ptr = (unsigned char *) &cleanmarker; |
| 166 | oob.start = erase.start + clmpos; |
| 167 | oob.length = clmlen; |
| 168 | xioctl (fd, MEMWRITEOOB, &oob); |
| 169 | } else { |
| 170 | if (lseek (fd, erase.start, SEEK_SET) < 0) { |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 171 | bb_perror_msg("\n%s: MTD %s failure", mtd_name, "seek"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) { |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 175 | bb_perror_msg("\n%s: MTD %s failure", mtd_name, "write"); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 176 | continue; |
| 177 | } |
| 178 | } |
| 179 | if (!(flags & OPTION_Q)) |
| 180 | printf(" Cleanmarker written at %x.", erase.start); |
| 181 | } |
| 182 | if (!(flags & OPTION_Q)) { |
| 183 | show_progress(&meminfo, &erase); |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 184 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Bernhard Reutner-Fischer | 7d9d251 | 2009-02-18 13:26:29 +0000 | [diff] [blame] | 187 | if (ENABLE_FEATURE_CLEAN_UP) |
| 188 | close(fd); |
Bernhard Reutner-Fischer | 0d22d17 | 2009-02-18 13:23:46 +0000 | [diff] [blame] | 189 | return EXIT_SUCCESS; |
| 190 | } |