Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [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 "flashutils.h" |
| 27 | |
| 28 | #ifdef LOG_TAG |
| 29 | #undef LOG_TAG |
| 30 | #endif |
| 31 | |
| 32 | #if 0 |
| 33 | |
| 34 | #define LOG_TAG "dump_image" |
| 35 | |
| 36 | #define BLOCK_SIZE 2048 |
| 37 | #define SPARE_SIZE (BLOCK_SIZE >> 5) |
| 38 | |
| 39 | static int die(const char *msg, ...) { |
| 40 | int err = errno; |
| 41 | va_list args; |
| 42 | va_start(args, msg); |
| 43 | char buf[1024]; |
| 44 | vsnprintf(buf, sizeof(buf), msg, args); |
| 45 | va_end(args); |
| 46 | |
| 47 | if (err != 0) { |
| 48 | strlcat(buf, ": ", sizeof(buf)); |
| 49 | strlcat(buf, strerror(err), sizeof(buf)); |
| 50 | } |
| 51 | |
| 52 | fprintf(stderr, "%s\n", buf); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | /* Read a flash partition and write it to an image file. */ |
| 57 | |
| 58 | int dump_image(char* partition_name, char* filename, dump_image_callback callback) { |
| 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 (mtd_scan_partitions() <= 0) |
| 70 | return die("error scanning partitions"); |
| 71 | |
| 72 | partition = mtd_find_partition_by_name(partition_name); |
| 73 | if (partition == NULL) |
| 74 | return die("can't find %s partition", partition_name); |
| 75 | |
| 76 | if (mtd_partition_info(partition, &partition_size, NULL, NULL)) { |
| 77 | return die("can't get info of partition %s", partition_name); |
| 78 | } |
| 79 | |
| 80 | if (!strcmp(filename, "-")) { |
| 81 | fd = fileno(stdout); |
| 82 | } |
| 83 | else { |
| 84 | fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 85 | } |
| 86 | |
| 87 | if (fd < 0) |
| 88 | return die("error opening %s", filename); |
| 89 | |
| 90 | in = mtd_read_partition(partition); |
| 91 | if (in == NULL) { |
| 92 | close(fd); |
| 93 | unlink(filename); |
| 94 | return die("error opening %s: %s\n", partition_name, strerror(errno)); |
| 95 | } |
| 96 | |
| 97 | total = 0; |
| 98 | while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) { |
| 99 | wrote = write(fd, buf, len); |
| 100 | if (wrote != len) { |
| 101 | close(fd); |
| 102 | unlink(filename); |
| 103 | return die("error writing %s", filename); |
| 104 | } |
| 105 | total += BLOCK_SIZE; |
| 106 | if (callback != NULL) |
| 107 | callback(total, partition_size); |
| 108 | } |
| 109 | |
| 110 | mtd_read_close(in); |
| 111 | |
| 112 | if (close(fd)) { |
| 113 | unlink(filename); |
| 114 | return die("error closing %s", filename); |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int main(int argc, char **argv) |
| 120 | { |
| 121 | ssize_t (*read_func) (MtdReadContext *, char *, size_t); |
| 122 | MtdReadContext *in; |
| 123 | const MtdPartition *partition; |
| 124 | char buf[BLOCK_SIZE + SPARE_SIZE]; |
| 125 | size_t partition_size; |
| 126 | size_t read_size; |
| 127 | size_t total; |
| 128 | int fd; |
| 129 | int wrote; |
| 130 | int len; |
| 131 | |
| 132 | if (argc != 3) { |
| 133 | fprintf(stderr, "usage: %s partition file.img\n", argv[0]); |
| 134 | return 2; |
| 135 | } |
| 136 | |
| 137 | return dump_image(argv[1], argv[2], NULL); |
| 138 | } |
| 139 | |
| 140 | #endif |
| 141 | |
| 142 | int main(int argc, char **argv) |
| 143 | { |
| 144 | if (argc != 3) { |
| 145 | fprintf(stderr, "usage: %s partition file.img\n", argv[0]); |
| 146 | return 2; |
| 147 | } |
| 148 | |
| 149 | return backup_raw_partition(NULL, argv[1], argv[2]); |
| 150 | } |