blob: 36ffa131459659f2cb8f91ac587592e49abb7a0a [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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
Doug Zongker39cf4172014-03-06 16:16:05 -080027#ifdef LOG_TAG
28#undef LOG_TAG
29#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#define LOG_TAG "flash_image"
31
32#define HEADER_SIZE 2048 // size of header to compare for equality
33
34void 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);
Steve Blocke6ef63f2012-01-08 12:24:35 +000048 ALOGE("%s\n", buf);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 exit(1);
50}
51
52/* Read an image file and write it to a flash partition. */
53
54int 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];
Elliott Hughes2f5feed2015-04-28 17:24:24 -070075 int headerlen = TEMP_FAILURE_RETRY(read(fd, header, sizeof(header)));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 if (headerlen <= 0) die("error reading %s header", argv[2]);
77
78 MtdReadContext *in = mtd_read_partition(partition);
79 if (in == NULL) {
Steve Block210f8872012-01-06 09:47:31 +000080 ALOGW("error opening %s: %s\n", argv[1], strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 // 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) {
Steve Block210f8872012-01-06 09:47:31 +000086 ALOGW("error reading %s: %s\n", argv[1], strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087 // just assume it needs re-writing
88 } else if (checklen == headerlen && !memcmp(header, check, headerlen)) {
Steve Blockadb5c2f2012-01-04 21:07:44 +000089 ALOGI("header is the same, not flashing %s\n", argv[1]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090 return 0;
91 }
92 mtd_read_close(in);
93 }
94
95 // Skip the header (we'll come back to it), write everything else
Steve Blockadb5c2f2012-01-04 21:07:44 +000096 ALOGI("flashing %s from %s\n", argv[1], argv[2]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097
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;
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700107 while ((len = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)))) > 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800108 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
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700128 if (TEMP_FAILURE_RETRY(lseek(fd, headerlen, SEEK_SET)) != headerlen)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800129 die("error rewinding %s", argv[2]);
130
131 int left = block_size - headerlen;
132 while (left < 0) left += block_size;
133 while (left > 0) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700134 len = TEMP_FAILURE_RETRY(read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 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}