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