blob: 37623b825be3162733c2e8c3638ad45b1df95f3b [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
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070035#define LOG_TAG "erase_image"
Magnus Eriksson75b930f2010-03-10 18:29:38 +010036
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070037static int die(const char *msg, ...) {
Magnus Eriksson75b930f2010-03-10 18:29:38 +010038 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);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070052 return 3;
Magnus Eriksson75b930f2010-03-10 18:29:38 +010053}
54
Magnus Eriksson75b930f2010-03-10 18:29:38 +010055
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070056int erase_image(char* partition_name) {
Magnus Eriksson75b930f2010-03-10 18:29:38 +010057 MtdWriteContext *out;
58 size_t erased;
59 size_t total_size;
60 size_t erase_size;
61
Magnus Eriksson75b930f2010-03-10 18:29:38 +010062 if (mtd_scan_partitions() <= 0) die("error scanning partitions");
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070063 const MtdPartition *partition = mtd_find_partition_by_name(partition_name);
64 if (partition == NULL) return die("can't find %s partition", partition_name);
Magnus Eriksson75b930f2010-03-10 18:29:38 +010065
66 out = mtd_write_partition(partition);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070067 if (out == NULL) return die("could not estabilish write context for %s", partition_name);
Magnus Eriksson75b930f2010-03-10 18:29:38 +010068
69 // do the actual erase, -1 = full partition erase
70 erased = mtd_erase_blocks(out, -1);
71
72 // erased = bytes erased, if zero, something borked
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070073 if (!erased) return die("error erasing %s", partition_name);
Magnus Eriksson75b930f2010-03-10 18:29:38 +010074
75 return 0;
76}
Koushik K. Dutta16f0b492010-03-19 14:37:11 -070077
78
79/* Erase a mtd partition */
80
81int main(int argc, char **argv) {
82 if (argc != 2) {
83 fprintf(stderr, "usage: %s <partition>\n", argv[0]);
84 return 2;
85 }
86
87 return erase_image(argv[1]);
88}