blob: bc96abc5a7d34ff7a967524268c7acb8aa7e8854 [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"
27
28#ifdef LOG_TAG
29#undef LOG_TAG
30#endif
31
32#define LOG_TAG "dump_image"
33
34#define BLOCK_SIZE 2048
35#define SPARE_SIZE (BLOCK_SIZE >> 5)
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 exit(1);
52}
53
54/* Read a flash partition and write it to an image file. */
55
56int main(int argc, char **argv)
57{
58 ssize_t (*read_func) (MtdReadContext *, char *, size_t);
59 MtdReadContext *in;
60 const MtdPartition *partition;
61 char buf[BLOCK_SIZE + SPARE_SIZE];
62 size_t partition_size;
63 size_t read_size;
64 size_t total;
65 int fd;
66 int wrote;
67 int len;
68
69 if (argc != 3) {
70 fprintf(stderr, "usage: %s partition file.img\n", argv[0]);
71 return 2;
72 }
73
74 if (mtd_scan_partitions() <= 0)
75 die("error scanning partitions");
76
77 partition = mtd_find_partition_by_name(argv[1]);
78 if (partition == NULL)
79 die("can't find %s partition", argv[1]);
80
81 if (mtd_partition_info(partition, &partition_size, NULL, NULL)) {
82 die("can't get info of partition %s", argv[1]);
83 }
84
85 if (!strcmp(argv[2], "-")) {
86 fd = fileno(stdout);
87 } else {
88 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666);
89 }
90
91 if (fd < 0)
92 die("error opening %s", argv[2]);
93
94 in = mtd_read_partition(partition);
95 if (in == NULL) {
96 close(fd);
97 unlink(argv[2]);
98 die("error opening %s: %s\n", argv[1], strerror(errno));
99 }
100
101 if (!strcmp(argv[1], "system") ||
102 !strcmp(argv[1], "cache") ||
103 !strcmp(argv[1], "userdata")) {
104 read_size = BLOCK_SIZE + SPARE_SIZE;
105 read_func = mtd_read_raw;
106 } else {
107 read_size = BLOCK_SIZE;
108 read_func = mtd_read_data;
109 }
110
111 total = 0;
112 while ((len = read_func(in, buf, read_size)) > 0) {
113 wrote = write(fd, buf, len);
114 if (wrote != len) {
115 close(fd);
116 unlink(argv[2]);
117 die("error writing %s", argv[2]);
118 }
119 total += BLOCK_SIZE;
120 }
121
122 if (total != partition_size) {
123 close(fd);
124 unlink(argv[2]);
125 die("error reading %s", argv[1]);
126 }
127
128 mtd_read_close(in);
129
130 if (close(fd)) {
131 unlink(argv[2]);
132 die("error closing %s", argv[2]);
133 }
134
135 return 0;
136}