blob: ddaf6f6a631ad3f506b65e8a336439741ba18143 [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
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -080037static void die(const char *msg, ...) {
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080038 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 }
Koushik K. Duttaa37e9b12010-02-24 13:13:34 -0800100/* - comment out this outdated shit
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800101 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 }
Koushik K. Duttaa37e9b12010-02-24 13:13:34 -0800110*/
111
112// - use mtd_read_data only, clean this up later
113 read_size = BLOCK_SIZE;
114 read_func = mtd_read_data;
115
116// printf("debug: partition size: %d\n", partition_size);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800117
118 total = 0;
119 while ((len = read_func(in, buf, read_size)) > 0) {
120 wrote = write(fd, buf, len);
121 if (wrote != len) {
122 close(fd);
123 unlink(argv[2]);
124 die("error writing %s", argv[2]);
125 }
126 total += BLOCK_SIZE;
127 }
128
Koushik K. Duttaa37e9b12010-02-24 13:13:34 -0800129// printf("debug: bytes read: %d\n", total);
130
131/* packetlss - don't assume every eraseblock is ok
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800132 if (total != partition_size) {
133 close(fd);
134 unlink(argv[2]);
135 die("error reading %s", argv[1]);
136 }
Koushik K. Duttaa37e9b12010-02-24 13:13:34 -0800137*/
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800138 mtd_read_close(in);
139
140 if (close(fd)) {
141 unlink(argv[2]);
142 die("error closing %s", argv[2]);
143 }
144
145 return 0;
146}