blob: 8b0a89605e9f8d661ed81775fe830b51e9bfc7fb [file] [log] [blame]
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <sys/ioctl.h>
24
25#include "cutils/log.h"
26#include "mtdutils.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080027#include "dump_image.h"
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080028
29#ifdef LOG_TAG
30#undef LOG_TAG
31#endif
32
33#define LOG_TAG "dump_image"
34
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080035#define BLOCK_SIZE 2048
36#define SPARE_SIZE (BLOCK_SIZE >> 5)
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080037
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080038static int die(const char *msg, ...) {
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080039 int err = errno;
40 va_list args;
41 va_start(args, msg);
42 char buf[1024];
43 vsnprintf(buf, sizeof(buf), msg, args);
44 va_end(args);
45
46 if (err != 0) {
47 strlcat(buf, ": ", sizeof(buf));
48 strlcat(buf, strerror(err), sizeof(buf));
49 }
50
51 fprintf(stderr, "%s\n", buf);
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080052 return 1;
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080053}
54
55/* Read a flash partition and write it to an image file. */
56
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080057int dump_image(char* partition_name, char* filename, dump_image_callback callback) {
58 MtdReadContext *in;
59 const MtdPartition *partition;
60 char buf[BLOCK_SIZE + SPARE_SIZE];
61 size_t partition_size;
62 size_t read_size;
63 size_t total;
64 int fd;
65 int wrote;
66 int len;
67
68 if (mtd_scan_partitions() <= 0)
69 return die("error scanning partitions");
70
71 partition = mtd_find_partition_by_name(partition_name);
72 if (partition == NULL)
73 return die("can't find %s partition", partition_name);
74
75 if (mtd_partition_info(partition, &partition_size, NULL, NULL)) {
76 return die("can't get info of partition %s", partition_name);
77 }
78
79 if (!strcmp(filename, "-")) {
80 fd = fileno(stdout);
81 }
82 else {
83 fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
84 }
85
86 if (fd < 0)
87 return die("error opening %s", filename);
88
89 in = mtd_read_partition(partition);
90 if (in == NULL) {
91 close(fd);
92 unlink(filename);
93 return die("error opening %s: %s\n", partition_name, strerror(errno));
94 }
95
96 total = 0;
97 while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) {
98 wrote = write(fd, buf, len);
99 if (wrote != len) {
100 close(fd);
101 unlink(filename);
102 return die("error writing %s", filename);
103 }
104 total += BLOCK_SIZE;
105 if (callback != NULL)
106 callback(total, partition_size);
107 }
108
109 mtd_read_close(in);
110
111 if (close(fd)) {
112 unlink(filename);
113 return die("error closing %s", filename);
114 }
115 return 0;
116}
117
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800118int main(int argc, char **argv)
119{
120 ssize_t (*read_func) (MtdReadContext *, char *, size_t);
121 MtdReadContext *in;
122 const MtdPartition *partition;
123 char buf[BLOCK_SIZE + SPARE_SIZE];
124 size_t partition_size;
125 size_t read_size;
126 size_t total;
127 int fd;
128 int wrote;
129 int len;
130
131 if (argc != 3) {
132 fprintf(stderr, "usage: %s partition file.img\n", argv[0]);
133 return 2;
134 }
135
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -0800136 return dump_image(argv[1], argv[2], NULL);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800137}