Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 1 | /* |
| 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. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 34 | #define BLOCK_SIZE 2048 |
| 35 | #define SPARE_SIZE (BLOCK_SIZE >> 5) |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 36 | |
Koushik K. Dutta | 981b0cd | 2010-02-22 08:53:34 -0800 | [diff] [blame] | 37 | static void die(const char *msg, ...) { |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 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 | |
| 56 | int 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. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 75 | die("error scanning partitions"); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 76 | |
| 77 | partition = mtd_find_partition_by_name(argv[1]); |
| 78 | if (partition == NULL) |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 79 | die("can't find %s partition", argv[1]); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 80 | |
| 81 | if (mtd_partition_info(partition, &partition_size, NULL, NULL)) { |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 82 | die("can't get info of partition %s", argv[1]); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (!strcmp(argv[2], "-")) { |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 86 | fd = fileno(stdout); |
| 87 | } |
| 88 | else { |
| 89 | fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (fd < 0) |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 93 | die("error opening %s", argv[2]); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 94 | |
| 95 | in = mtd_read_partition(partition); |
| 96 | if (in == NULL) { |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 97 | close(fd); |
| 98 | unlink(argv[2]); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 99 | die("error opening %s: %s\n", argv[1], strerror(errno)); |
| 100 | } |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 101 | |
| 102 | total = 0; |
Koushik K. Dutta | ff32e8c | 2010-02-24 13:16:43 -0800 | [diff] [blame] | 103 | while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) { |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 104 | wrote = write(fd, buf, len); |
| 105 | if (wrote != len) { |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 106 | close(fd); |
| 107 | unlink(argv[2]); |
| 108 | die("error writing %s", argv[2]); |
| 109 | } |
| 110 | total += BLOCK_SIZE; |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 113 | mtd_read_close(in); |
| 114 | |
| 115 | if (close(fd)) { |
Koushik K. Dutta | 4a8e7d2 | 2010-03-12 14:30:56 -0800 | [diff] [blame^] | 116 | unlink(argv[2]); |
| 117 | die("error closing %s", argv[2]); |
Koushik K. Dutta | 3ab130f | 2010-02-19 16:50:36 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |