diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2009-02-18 13:26:29 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2009-02-18 13:26:29 +0000 |
commit | 7d9d25154359d6ce16edc59ed202cfa2e998fdcb (patch) | |
tree | 334be9578f4b78e44dbbd8ba8b5b3de44c167dce /miscutils/flash_eraseall.c | |
parent | 0d22d1755c2c55ad45b01458bbf2f09aab4308d2 (diff) | |
download | busybox-w32-7d9d25154359d6ce16edc59ed202cfa2e998fdcb.tar.gz busybox-w32-7d9d25154359d6ce16edc59ed202cfa2e998fdcb.tar.bz2 busybox-w32-7d9d25154359d6ce16edc59ed202cfa2e998fdcb.zip |
- make it compile and shrink it a little bit (~-110 bytes)
text data bss dec hex filename
1533 4 0 1537 601 miscutils/flash_eraseall.o.old
1415 4 0 1419 58b miscutils/flash_eraseall.o
- add some FIXMEs
Diffstat (limited to 'miscutils/flash_eraseall.c')
-rw-r--r-- | miscutils/flash_eraseall.c | 69 |
1 files changed, 33 insertions, 36 deletions
diff --git a/miscutils/flash_eraseall.c b/miscutils/flash_eraseall.c index 79028d780..e0e07ae69 100644 --- a/miscutils/flash_eraseall.c +++ b/miscutils/flash_eraseall.c | |||
@@ -1,35 +1,30 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* eraseall.c -- erase the whole of a MTD device | 2 | /* eraseall.c -- erase the whole of a MTD device |
2 | 3 | * | |
3 | Ported to busybox from mtd-utils. | 4 | * Ported to busybox from mtd-utils. |
4 | 5 | * | |
5 | Copyright (C) 2000 Arcom Control System Ltd | 6 | * Copyright (C) 2000 Arcom Control System Ltd |
6 | 7 | * | |
7 | Renamed to flash_eraseall.c | 8 | * Renamed to flash_eraseall.c |
8 | 9 | * | |
9 | This program is free software; you can redistribute it and/or modify | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
10 | it under the terms of the GNU General Public License as published by | ||
11 | the Free Software Foundation; either version 2 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | This program is distributed in the hope that it will be useful, | ||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | GNU General Public License for more details. | ||
18 | |||
19 | You should have received a copy of the GNU General Public License | ||
20 | along with this program; if not, write to the Free Software | ||
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
22 | */ | 11 | */ |
23 | 12 | ||
24 | #include "libbb.h" | 13 | #include "libbb.h" |
25 | |||
26 | #include <mtd/mtd-user.h> | 14 | #include <mtd/mtd-user.h> |
27 | #include <mtd/jffs2-user.h> | 15 | #include <mtd/jffs2-user.h> |
28 | 16 | ||
29 | #define OPTION_J (1 << 0) | 17 | #define OPTION_J (1 << 0) |
30 | #define OPTION_Q (1 << 1) | 18 | #define OPTION_Q (1 << 1) |
19 | #define _bitNAND (2) | ||
20 | #define isNAND (1 << _bitNAND) | ||
21 | #define bbtest (1 << 3) | ||
31 | 22 | ||
32 | int target_endian = __BYTE_ORDER; | 23 | /* This is used in the cpu_to_je/je_to_cpu macros in jffs2_user.h */ |
24 | /* FIXME: target_endian should be const! | ||
25 | * FIXME: Also it sounds more sensible to use our own existing SWAP_ macros. | ||
26 | */ | ||
27 | /* const */ int target_endian = __BYTE_ORDER; | ||
33 | 28 | ||
34 | static uint32_t crc32(uint32_t val, const void *ss, int len, | 29 | static uint32_t crc32(uint32_t val, const void *ss, int len, |
35 | uint32_t *crc32_table) | 30 | uint32_t *crc32_table) |
@@ -49,14 +44,13 @@ static void show_progress(mtd_info_t *meminfo, erase_info_t *erase) | |||
49 | } | 44 | } |
50 | 45 | ||
51 | int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 46 | int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
52 | int flash_eraseall_main(int argc, char **argv) | 47 | int flash_eraseall_main(int argc UNUSED_PARAM, char **argv) |
53 | { | 48 | { |
54 | struct jffs2_unknown_node cleanmarker; | 49 | struct jffs2_unknown_node cleanmarker; |
55 | mtd_info_t meminfo; | 50 | mtd_info_t meminfo; |
56 | int fd, clmpos = 0, clmlen = 8; | 51 | int fd, clmpos = 0, clmlen = 8; |
57 | erase_info_t erase; | 52 | erase_info_t erase; |
58 | struct stat st; | 53 | struct stat st; |
59 | int isNAND, bbtest = 1; | ||
60 | unsigned int flags; | 54 | unsigned int flags; |
61 | char *mtd_name; | 55 | char *mtd_name; |
62 | 56 | ||
@@ -73,7 +67,7 @@ int flash_eraseall_main(int argc, char **argv) | |||
73 | xioctl(fd, MEMGETINFO, &meminfo); | 67 | xioctl(fd, MEMGETINFO, &meminfo); |
74 | 68 | ||
75 | erase.length = meminfo.erasesize; | 69 | erase.length = meminfo.erasesize; |
76 | isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0; | 70 | flags |= bbtest | ((meminfo.type == MTD_NANDFLASH) << _bitNAND); |
77 | 71 | ||
78 | if (flags & OPTION_J) { | 72 | if (flags & OPTION_J) { |
79 | uint32_t *crc32_table; | 73 | uint32_t *crc32_table; |
@@ -82,7 +76,7 @@ int flash_eraseall_main(int argc, char **argv) | |||
82 | 76 | ||
83 | cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); | 77 | cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); |
84 | cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); | 78 | cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); |
85 | if (!isNAND) | 79 | if (!(flags & isNAND)) |
86 | cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); | 80 | cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node)); |
87 | else { | 81 | else { |
88 | struct nand_oobinfo oobinfo; | 82 | struct nand_oobinfo oobinfo; |
@@ -125,10 +119,10 @@ int flash_eraseall_main(int argc, char **argv) | |||
125 | 119 | ||
126 | for (erase.start = 0; erase.start < meminfo.size; | 120 | for (erase.start = 0; erase.start < meminfo.size; |
127 | erase.start += meminfo.erasesize) { | 121 | erase.start += meminfo.erasesize) { |
128 | if (bbtest) { | 122 | if (flags & bbtest) { |
129 | int ret; | 123 | int ret; |
130 | |||
131 | loff_t offset = erase.start; | 124 | loff_t offset = erase.start; |
125 | |||
132 | ret = ioctl(fd, MEMGETBADBLOCK, &offset); | 126 | ret = ioctl(fd, MEMGETBADBLOCK, &offset); |
133 | if (ret > 0) { | 127 | if (ret > 0) { |
134 | if (!(flags & OPTION_Q)) | 128 | if (!(flags & OPTION_Q)) |
@@ -139,13 +133,13 @@ int flash_eraseall_main(int argc, char **argv) | |||
139 | * types e.g. NOR | 133 | * types e.g. NOR |
140 | */ | 134 | */ |
141 | if (errno == EOPNOTSUPP) { | 135 | if (errno == EOPNOTSUPP) { |
142 | bbtest = 0; | 136 | flags =~ bbtest; |
143 | if (isNAND) | 137 | if (flags & isNAND) |
144 | bb_error_msg_and_die("%s: Bad block check not available", | 138 | bb_error_msg_and_die("%s: Bad block check not available", |
145 | mtd_name); | 139 | mtd_name); |
146 | } else { | 140 | } else { |
147 | bb_error_msg_and_die("\n%s: MTD get bad block failed: %s", | 141 | bb_perror_msg_and_die("\n%s: MTD %s failure", |
148 | mtd_name, strerror(errno)); | 142 | mtd_name, "get bad block "); |
149 | } | 143 | } |
150 | } | 144 | } |
151 | } | 145 | } |
@@ -160,19 +154,20 @@ int flash_eraseall_main(int argc, char **argv) | |||
160 | continue; | 154 | continue; |
161 | 155 | ||
162 | /* write cleanmarker */ | 156 | /* write cleanmarker */ |
163 | if (isNAND) { | 157 | if (flags & isNAND) { |
164 | struct mtd_oob_buf oob; | 158 | struct mtd_oob_buf oob; |
159 | |||
165 | oob.ptr = (unsigned char *) &cleanmarker; | 160 | oob.ptr = (unsigned char *) &cleanmarker; |
166 | oob.start = erase.start + clmpos; | 161 | oob.start = erase.start + clmpos; |
167 | oob.length = clmlen; | 162 | oob.length = clmlen; |
168 | xioctl (fd, MEMWRITEOOB, &oob); | 163 | xioctl (fd, MEMWRITEOOB, &oob); |
169 | } else { | 164 | } else { |
170 | if (lseek (fd, erase.start, SEEK_SET) < 0) { | 165 | if (lseek (fd, erase.start, SEEK_SET) < 0) { |
171 | bb_error_msg("\n%s: MTD lseek failure: %s", mtd_name, strerror(errno)); | 166 | bb_perror_msg("\n%s: MTD %s failure", mtd_name, "seek"); |
172 | continue; | 167 | continue; |
173 | } | 168 | } |
174 | if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) { | 169 | if (write (fd , &cleanmarker, sizeof (cleanmarker)) != sizeof (cleanmarker)) { |
175 | bb_error_msg("\n%s: MTD write failure: %s", mtd_name, strerror(errno)); | 170 | bb_perror_msg("\n%s: MTD %s failure", mtd_name, "write"); |
176 | continue; | 171 | continue; |
177 | } | 172 | } |
178 | } | 173 | } |
@@ -181,8 +176,10 @@ int flash_eraseall_main(int argc, char **argv) | |||
181 | } | 176 | } |
182 | if (!(flags & OPTION_Q)) { | 177 | if (!(flags & OPTION_Q)) { |
183 | show_progress(&meminfo, &erase); | 178 | show_progress(&meminfo, &erase); |
184 | printf("\n"); | 179 | bb_putchar('\n'); |
185 | } | 180 | } |
186 | 181 | ||
182 | if (ENABLE_FEATURE_CLEAN_UP) | ||
183 | close(fd); | ||
187 | return EXIT_SUCCESS; | 184 | return EXIT_SUCCESS; |
188 | } | 185 | } |