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