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