blob: 30f73e9b99dc2c929c3973b9c3405c3d46482269 [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
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080034#define BLOCK_SIZE 2048
35#define SPARE_SIZE (BLOCK_SIZE >> 5)
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080036
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)
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080075 die("error scanning partitions");
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080076
77 partition = mtd_find_partition_by_name(argv[1]);
78 if (partition == NULL)
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080079 die("can't find %s partition", argv[1]);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080080
81 if (mtd_partition_info(partition, &partition_size, NULL, NULL)) {
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080082 die("can't get info of partition %s", argv[1]);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080083 }
84
85 if (!strcmp(argv[2], "-")) {
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080086 fd = fileno(stdout);
87 }
88 else {
89 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080090 }
91
92 if (fd < 0)
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080093 die("error opening %s", argv[2]);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080094
95 in = mtd_read_partition(partition);
96 if (in == NULL) {
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -080097 close(fd);
98 unlink(argv[2]);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -080099 die("error opening %s: %s\n", argv[1], strerror(errno));
100 }
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800101
102 total = 0;
Koushik K. Duttaff32e8c2010-02-24 13:16:43 -0800103 while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) {
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800104 wrote = write(fd, buf, len);
105 if (wrote != len) {
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -0800106 close(fd);
107 unlink(argv[2]);
108 die("error writing %s", argv[2]);
109 }
110 total += BLOCK_SIZE;
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800111 }
112
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800113 mtd_read_close(in);
114
115 if (close(fd)) {
Koushik K. Dutta4a8e7d22010-03-12 14:30:56 -0800116 unlink(argv[2]);
117 die("error closing %s", argv[2]);
Koushik K. Dutta3ab130f2010-02-19 16:50:36 -0800118 }
119
120 return 0;
121}