1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2008 The Android Open Source Project
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun * You may obtain a copy of the License at
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun * limitations under the License.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <errno.h>
18*4882a593Smuzhiyun #include <fcntl.h>
19*4882a593Smuzhiyun #include <stdio.h>
20*4882a593Smuzhiyun #include <stdlib.h>
21*4882a593Smuzhiyun #include <string.h>
22*4882a593Smuzhiyun #include <unistd.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include "cutils/log.h"
25*4882a593Smuzhiyun #include "mtdutils.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define LOG_TAG "flash_image"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define HEADER_SIZE 2048 // size of header to compare for equality
30*4882a593Smuzhiyun
die(const char * msg,...)31*4882a593Smuzhiyun void die(const char *msg, ...)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun int err = errno;
34*4882a593Smuzhiyun va_list args;
35*4882a593Smuzhiyun va_start(args, msg);
36*4882a593Smuzhiyun char buf[1024];
37*4882a593Smuzhiyun vsnprintf(buf, sizeof(buf), msg, args);
38*4882a593Smuzhiyun va_end(args);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (err != 0) {
41*4882a593Smuzhiyun strlcat(buf, ": ", sizeof(buf));
42*4882a593Smuzhiyun strlcat(buf, strerror(err), sizeof(buf));
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun fprintf(stderr, "%s\n", buf);
46*4882a593Smuzhiyun LOGE("%s\n", buf);
47*4882a593Smuzhiyun exit(1);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Read an image file and write it to a flash partition. */
51*4882a593Smuzhiyun
main(int argc,char ** argv)52*4882a593Smuzhiyun int main(int argc, char **argv)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun const MtdPartition *ptn;
55*4882a593Smuzhiyun MtdWriteContext *write;
56*4882a593Smuzhiyun void *data;
57*4882a593Smuzhiyun unsigned sz;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (argc != 3) {
60*4882a593Smuzhiyun fprintf(stderr, "usage: %s partition file.img\n", argv[0]);
61*4882a593Smuzhiyun return 2;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (mtd_scan_partitions() <= 0) die("error scanning partitions");
65*4882a593Smuzhiyun const MtdPartition *partition = mtd_find_partition_by_name(argv[1]);
66*4882a593Smuzhiyun if (partition == NULL) die("can't find %s partition", argv[1]);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun // If the first part of the file matches the partition, skip writing
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun int fd = open(argv[2], O_RDONLY);
71*4882a593Smuzhiyun if (fd < 0) die("error opening %s", argv[2]);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun char header[HEADER_SIZE];
74*4882a593Smuzhiyun int headerlen = read(fd, header, sizeof(header));
75*4882a593Smuzhiyun if (headerlen <= 0) die("error reading %s header", argv[2]);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun MtdReadContext *in = mtd_read_partition(partition);
78*4882a593Smuzhiyun if (in == NULL) {
79*4882a593Smuzhiyun LOGW("error opening %s: %s\n", argv[1], strerror(errno));
80*4882a593Smuzhiyun // just assume it needs re-writing
81*4882a593Smuzhiyun } else {
82*4882a593Smuzhiyun char check[HEADER_SIZE];
83*4882a593Smuzhiyun int checklen = mtd_read_data(in, check, sizeof(check));
84*4882a593Smuzhiyun if (checklen <= 0) {
85*4882a593Smuzhiyun LOGW("error reading %s: %s\n", argv[1], strerror(errno));
86*4882a593Smuzhiyun // just assume it needs re-writing
87*4882a593Smuzhiyun } else if (checklen == headerlen && !memcmp(header, check, headerlen)) {
88*4882a593Smuzhiyun LOGI("header is the same, not flashing %s\n", argv[1]);
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun mtd_read_close(in);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun // Skip the header (we'll come back to it), write everything else
95*4882a593Smuzhiyun LOGI("flashing %s from %s\n", argv[1], argv[2]);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun MtdWriteContext *out = mtd_write_partition(partition);
98*4882a593Smuzhiyun if (out == NULL) die("error writing %s", argv[1]);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun char buf[HEADER_SIZE];
101*4882a593Smuzhiyun memset(buf, 0, headerlen);
102*4882a593Smuzhiyun int wrote = mtd_write_data(out, buf, headerlen);
103*4882a593Smuzhiyun if (wrote != headerlen) die("error writing %s", argv[1]);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun int len;
106*4882a593Smuzhiyun while ((len = read(fd, buf, sizeof(buf))) > 0) {
107*4882a593Smuzhiyun wrote = mtd_write_data(out, buf, len);
108*4882a593Smuzhiyun if (wrote != len) die("error writing %s", argv[1]);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun if (len < 0) die("error reading %s", argv[2]);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (mtd_write_close(out)) die("error closing %s", argv[1]);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun // Now come back and write the header last
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun out = mtd_write_partition(partition);
117*4882a593Smuzhiyun if (out == NULL) die("error re-opening %s", argv[1]);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun wrote = mtd_write_data(out, header, headerlen);
120*4882a593Smuzhiyun if (wrote != headerlen) die("error re-writing %s", argv[1]);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun // Need to write a complete block, so write the rest of the first block
123*4882a593Smuzhiyun size_t block_size;
124*4882a593Smuzhiyun if (mtd_partition_info(partition, NULL, &block_size, NULL))
125*4882a593Smuzhiyun die("error getting %s block size", argv[1]);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (lseek(fd, headerlen, SEEK_SET) != headerlen)
128*4882a593Smuzhiyun die("error rewinding %s", argv[2]);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun int left = block_size - headerlen;
131*4882a593Smuzhiyun while (left < 0) left += block_size;
132*4882a593Smuzhiyun while (left > 0) {
133*4882a593Smuzhiyun len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left);
134*4882a593Smuzhiyun if (len <= 0) die("error reading %s", argv[2]);
135*4882a593Smuzhiyun if (mtd_write_data(out, buf, len) != len)
136*4882a593Smuzhiyun die("error writing %s", argv[1]);
137*4882a593Smuzhiyun left -= len;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (mtd_write_close(out)) die("error closing %s", argv[1]);
141*4882a593Smuzhiyun return 0;
142*4882a593Smuzhiyun }
143