blob: 111e637bd98531e237f49cf725dede417e0298ac [file] [log] [blame]
Magnus Eriksson75b930f2010-03-10 18:29:38 +01001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * Portions Copyright (C) 2010 Magnus Eriksson <packetlss@gmail.com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <errno.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include <fcntl.h>
26#include <mtd/mtd-user.h>
27
28#include "cutils/log.h"
29#include "mtdutils.h"
30
31#ifdef LOG_TAG
32#undef LOG_TAG
33#endif
34
35#define LOG_TAG "erase_flash"
36
37void die(const char *msg, ...) {
38 int err = errno;
39 va_list args;
40 va_start(args, msg);
41 char buf[1024];
42 vsnprintf(buf, sizeof(buf), msg, args);
43 va_end(args);
44
45 if (err != 0) {
46 strlcat(buf, ": ", sizeof(buf));
47 strlcat(buf, strerror(err), sizeof(buf));
48 }
49
50 fprintf(stderr, "%s\n", buf);
51 LOGE("%s\n", buf);
52 exit(1);
53}
54
55/* Erase a mtd partition */
56
57int main(int argc, char **argv) {
58 MtdWriteContext *out;
59 size_t erased;
60 size_t total_size;
61 size_t erase_size;
62
63 if (argc != 2) {
64 fprintf(stderr, "usage: %s <partition>\n", argv[0]);
65 return 2;
66 }
67
68 if (mtd_scan_partitions() <= 0) die("error scanning partitions");
69 const MtdPartition *partition = mtd_find_partition_by_name(argv[1]);
70 if (partition == NULL) die("can't find %s partition", argv[1]);
71
72 out = mtd_write_partition(partition);
73 if (out == NULL) die("could not estabilish write context for %s", argv[1]);
74
75 // do the actual erase, -1 = full partition erase
76 erased = mtd_erase_blocks(out, -1);
77
78 // erased = bytes erased, if zero, something borked
79 if (!erased) die("error erasing %s", argv[1]);
80
81 return 0;
82}