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 | |
| 24 | #include "cutils/log.h" |
| 25 | #include "flashutils.h" |
| 26 | |
| 27 | #if 0 |
| 28 | #define LOG_TAG "flash_image" |
| 29 | |
| 30 | #define HEADER_SIZE 2048 // size of header to compare for equality |
| 31 | |
| 32 | void die(const char *msg, ...) { |
| 33 | int err = errno; |
| 34 | va_list args; |
| 35 | va_start(args, msg); |
| 36 | char buf[1024]; |
| 37 | vsnprintf(buf, sizeof(buf), msg, args); |
| 38 | va_end(args); |
| 39 | |
| 40 | if (err != 0) { |
| 41 | strlcat(buf, ": ", sizeof(buf)); |
| 42 | strlcat(buf, strerror(err), sizeof(buf)); |
| 43 | } |
| 44 | |
| 45 | fprintf(stderr, "%s\n", buf); |
| 46 | LOGE("%s\n", buf); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | /* Read an image file and write it to a flash partition. */ |
| 51 | |
| 52 | int main(int argc, char **argv) { |
| 53 | const MtdPartition *ptn; |
| 54 | MtdWriteContext *write; |
| 55 | void *data; |
| 56 | unsigned sz; |
| 57 | |
| 58 | if (argc != 3) { |
| 59 | fprintf(stderr, "usage: %s partition file.img\n", argv[0]); |
| 60 | return 2; |
| 61 | } |
| 62 | |
| 63 | if (mtd_scan_partitions() <= 0) die("error scanning partitions"); |
| 64 | const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); |
| 65 | if (partition == NULL) die("can't find %s partition", argv[1]); |
| 66 | |
| 67 | // If the first part of the file matches the partition, skip writing |
| 68 | |
| 69 | int fd = open(argv[2], O_RDONLY); |
| 70 | if (fd < 0) die("error opening %s", argv[2]); |
| 71 | |
| 72 | char header[HEADER_SIZE]; |
| 73 | int headerlen = read(fd, header, sizeof(header)); |
| 74 | if (headerlen <= 0) die("error reading %s header", argv[2]); |
| 75 | |
| 76 | MtdReadContext *in = mtd_read_partition(partition); |
| 77 | if (in == NULL) { |
| 78 | LOGW("error opening %s: %s\n", argv[1], strerror(errno)); |
| 79 | // just assume it needs re-writing |
| 80 | } else { |
| 81 | char check[HEADER_SIZE]; |
| 82 | int checklen = mtd_read_data(in, check, sizeof(check)); |
| 83 | if (checklen <= 0) { |
| 84 | LOGW("error reading %s: %s\n", argv[1], strerror(errno)); |
| 85 | // just assume it needs re-writing |
| 86 | } else if (checklen == headerlen && !memcmp(header, check, headerlen)) { |
| 87 | LOGI("header is the same, not flashing %s\n", argv[1]); |
| 88 | return 0; |
| 89 | } |
| 90 | mtd_read_close(in); |
| 91 | } |
| 92 | |
| 93 | // Skip the header (we'll come back to it), write everything else |
| 94 | LOGI("flashing %s from %s\n", argv[1], argv[2]); |
| 95 | |
| 96 | MtdWriteContext *out = mtd_write_partition(partition); |
| 97 | if (out == NULL) die("error writing %s", argv[1]); |
| 98 | |
| 99 | char buf[HEADER_SIZE]; |
| 100 | memset(buf, 0, headerlen); |
| 101 | int wrote = mtd_write_data(out, buf, headerlen); |
| 102 | if (wrote != headerlen) die("error writing %s", argv[1]); |
| 103 | |
| 104 | int len; |
| 105 | while ((len = read(fd, buf, sizeof(buf))) > 0) { |
| 106 | wrote = mtd_write_data(out, buf, len); |
| 107 | if (wrote != len) die("error writing %s", argv[1]); |
| 108 | } |
| 109 | if (len < 0) die("error reading %s", argv[2]); |
| 110 | |
| 111 | if (mtd_write_close(out)) die("error closing %s", argv[1]); |
| 112 | |
| 113 | // Now come back and write the header last |
| 114 | |
| 115 | out = mtd_write_partition(partition); |
| 116 | if (out == NULL) die("error re-opening %s", argv[1]); |
| 117 | |
| 118 | wrote = mtd_write_data(out, header, headerlen); |
| 119 | if (wrote != headerlen) die("error re-writing %s", argv[1]); |
| 120 | |
| 121 | // Need to write a complete block, so write the rest of the first block |
| 122 | size_t block_size; |
| 123 | if (mtd_partition_info(partition, NULL, &block_size, NULL)) |
| 124 | die("error getting %s block size", argv[1]); |
| 125 | |
| 126 | if (lseek(fd, headerlen, SEEK_SET) != headerlen) |
| 127 | die("error rewinding %s", argv[2]); |
| 128 | |
| 129 | int left = block_size - headerlen; |
| 130 | while (left < 0) left += block_size; |
| 131 | while (left > 0) { |
| 132 | len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left); |
| 133 | if (len <= 0) die("error reading %s", argv[2]); |
| 134 | if (mtd_write_data(out, buf, len) != len) |
| 135 | die("error writing %s", argv[1]); |
| 136 | left -= len; |
| 137 | } |
| 138 | |
| 139 | if (mtd_write_close(out)) die("error closing %s", argv[1]); |
| 140 | return 0; |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | int main(int argc, char **argv) |
| 145 | { |
| 146 | if (argc != 3) { |
| 147 | fprintf(stderr, "usage: %s partition file.img\n", argv[0]); |
| 148 | return 2; |
| 149 | } |
| 150 | |
| 151 | int ret = restore_raw_partition(NULL, argv[1], argv[2]); |
| 152 | if (ret != 0) |
| 153 | fprintf(stderr, "failed with error: %d\n", ret); |
| 154 | return ret; |
| 155 | } |