blob: b09a42480a542ccf7a5a82c32db854723419ca18 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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
27#include "cutils/log.h"
28#include "flashutils.h"
29
30#if 0
31
32#ifdef LOG_TAG
33#undef LOG_TAG
34#endif
35
36
37#define LOG_TAG "erase_image"
38
39static int die(const char *msg, ...) {
40 int err = errno;
41 va_list args;
42 va_start(args, msg);
43 char buf[1024];
44 vsnprintf(buf, sizeof(buf), msg, args);
45 va_end(args);
46
47 if (err != 0) {
48 strlcat(buf, ": ", sizeof(buf));
49 strlcat(buf, strerror(err), sizeof(buf));
50 }
51
52 fprintf(stderr, "%s\n", buf);
53 LOGE("%s\n", buf);
54 return 3;
55}
56
57
58int erase_image(char* partition_name) {
59 MtdWriteContext *out;
60 size_t erased;
61 size_t total_size;
62 size_t erase_size;
63
64 if (mtd_scan_partitions() <= 0) die("error scanning partitions");
65 const MtdPartition *partition = mtd_find_partition_by_name(partition_name);
66 if (partition == NULL) return die("can't find %s partition", partition_name);
67
68 out = mtd_write_partition(partition);
69 if (out == NULL) return die("could not estabilish write context for %s", partition_name);
70
71 // do the actual erase, -1 = full partition erase
72 erased = mtd_erase_blocks(out, -1);
73
74 // erased = bytes erased, if zero, something borked
75 if (!erased) return die("error erasing %s", partition_name);
76
77 return 0;
78}
79
80
81/* Erase a mtd partition */
82
83int main(int argc, char **argv) {
84 if (argc != 2) {
85 fprintf(stderr, "usage: %s <partition>\n", argv[0]);
86 return 2;
87 }
88
89 return erase_image(argv[1]);
90}
91
92#endif
93
94
95int main(int argc, char **argv)
96{
97 if (argc != 2) {
98 fprintf(stderr, "usage: %s partition\n", argv[0]);
99 return 2;
100 }
101
102 return erase_raw_partition(NULL, argv[1]);
103}