blob: 0250407a513427bd8bb2c8c04d6ff2a0a7dfb14e [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -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
Tao Bao8fce75a2016-11-10 12:33:41 -080017#include "applypatch/applypatch.h"
18
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <errno.h>
Tao Baoba9a42a2015-06-23 23:23:33 -070020#include <fcntl.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021#include <libgen.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/statfs.h>
27#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028#include <unistd.h>
29
Yabin Cuid483c202016-02-03 17:08:52 -080030#include <memory>
31#include <string>
Tao Bao8fce75a2016-11-10 12:33:41 -080032#include <utility>
33#include <vector>
Yabin Cuid483c202016-02-03 17:08:52 -080034
Tao Bao8fce75a2016-11-10 12:33:41 -080035#include <android-base/parseint.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080036#include <android-base/strings.h>
Tao Bao8fce75a2016-11-10 12:33:41 -080037#include <openssl/sha.h>
Tao Baoaca8e892015-07-17 11:47:44 -070038
Doug Zongkerc4351c72010-02-22 14:46:32 -080039#include "edify/expr.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080040#include "ota_io.h"
Tao Baoe6aa3322015-08-05 15:20:27 -070041#include "print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080042
Tao Bao8fce75a2016-11-10 12:33:41 -080043static int LoadPartitionContents(const std::string& filename, FileContents* file);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070044static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
Doug Zongker1c43c972012-02-28 11:07:09 -080045static int GenerateTarget(FileContents* source_file,
46 const Value* source_patch_value,
47 FileContents* copy_file,
48 const Value* copy_patch_value,
49 const char* source_filename,
50 const char* target_filename,
Sen Jiangc48cb5e2016-02-04 16:23:21 +080051 const uint8_t target_sha1[SHA_DIGEST_LENGTH],
Doug Zongkera3ccba62012-08-20 15:28:02 -070052 size_t target_size,
53 const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080054
Tao Bao8fce75a2016-11-10 12:33:41 -080055// Read a file into memory; store the file contents and associated metadata in *file.
Hristo Bojinovdb314d62010-08-02 10:29:49 -070056// Return 0 on success.
Doug Zongkera1bc1482014-02-13 15:18:19 -080057int LoadFileContents(const char* filename, FileContents* file) {
Tao Bao8fce75a2016-11-10 12:33:41 -080058 // A special 'filename' beginning with "EMMC:" means to load the contents of a partition.
59 if (strncmp(filename, "EMMC:", 5) == 0) {
60 return LoadPartitionContents(filename, file);
61 }
Doug Zongker512536a2010-02-17 16:11:44 -080062
Tao Bao8fce75a2016-11-10 12:33:41 -080063 if (stat(filename, &file->st) == -1) {
64 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
65 return -1;
66 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080067
Tao Bao8fce75a2016-11-10 12:33:41 -080068 std::vector<unsigned char> data(file->st.st_size);
Tao Bao3dc14cb2016-11-22 11:37:13 -080069 std::unique_ptr<FILE, int (*)(FILE*)> f(ota_fopen(filename, "rb"), ota_fclose);
Tao Bao8fce75a2016-11-10 12:33:41 -080070 if (!f) {
71 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
72 return -1;
73 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080074
Tao Bao8fce75a2016-11-10 12:33:41 -080075 size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
76 if (bytes_read != data.size()) {
77 printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
78 return -1;
79 }
80 file->data = std::move(data);
81 SHA1(file->data.data(), file->data.size(), file->sha1);
82 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080083}
84
Elliott Hughes63a31922016-06-09 17:41:22 -070085// Load the contents of an EMMC partition into the provided
Doug Zongker512536a2010-02-17 16:11:44 -080086// FileContents. filename should be a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -070087// "EMMC:<partition_device>:...". The smallest size_n bytes for
Doug Zongkerf291d852010-07-07 13:55:25 -070088// which that prefix of the partition contents has the corresponding
89// sha1 hash will be loaded. It is acceptable for a size value to be
90// repeated with different sha1s. Will return 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -080091//
92// This complexity is needed because if an OTA installation is
93// interrupted, the partition might contain either the source or the
94// target data, which might be of different lengths. We need to know
Doug Zongkerf291d852010-07-07 13:55:25 -070095// the length in order to read from a partition (there is no
96// "end-of-file" marker), so the caller must specify the possible
97// lengths and the hash of the data, and we'll do the load expecting
98// to find one of those hashes.
Tao Bao8fce75a2016-11-10 12:33:41 -080099static int LoadPartitionContents(const std::string& filename, FileContents* file) {
100 std::vector<std::string> pieces = android::base::Split(filename, ":");
101 if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") {
102 printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
103 return -1;
104 }
105
106 size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
107 std::vector<std::pair<size_t, std::string>> pairs;
108 for (size_t i = 0; i < pair_count; ++i) {
109 size_t size;
110 if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
111 printf("LoadPartitionContents called with bad size \"%s\"\n", pieces[i * 2 + 2].c_str());
112 return -1;
113 }
114 pairs.push_back({ size, pieces[i * 2 + 3] });
115 }
116
117 // Sort the pairs array so that they are in order of increasing size.
118 std::sort(pairs.begin(), pairs.end());
119
120 const char* partition = pieces[1].c_str();
Tao Bao3dc14cb2016-11-22 11:37:13 -0800121 std::unique_ptr<FILE, int (*)(FILE*)> dev(ota_fopen(partition, "rb"), ota_fclose);
Tao Bao8fce75a2016-11-10 12:33:41 -0800122 if (!dev) {
123 printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
124 return -1;
125 }
126
127 SHA_CTX sha_ctx;
128 SHA1_Init(&sha_ctx);
129
130 // Allocate enough memory to hold the largest size.
131 std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
132 unsigned char* buffer_ptr = buffer.data();
133 size_t buffer_size = 0; // # bytes read so far
134 bool found = false;
135
136 for (const auto& pair : pairs) {
137 size_t current_size = pair.first;
138 const std::string& current_sha1 = pair.second;
139
140 // Read enough additional bytes to get us up to the next size. (Again,
141 // we're trying the possibilities in order of increasing size).
142 size_t next = current_size - buffer_size;
143 if (next > 0) {
144 size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
145 if (next != read) {
146 printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition);
Tao Baoaca8e892015-07-17 11:47:44 -0700147 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800148 }
149 SHA1_Update(&sha_ctx, buffer_ptr, read);
150 buffer_size += read;
151 buffer_ptr += read;
Tao Baoaca8e892015-07-17 11:47:44 -0700152 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700153
Tao Bao8fce75a2016-11-10 12:33:41 -0800154 // Duplicate the SHA context and finalize the duplicate so we can
155 // check it against this pair's expected hash.
156 SHA_CTX temp_ctx;
157 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
158 uint8_t sha_so_far[SHA_DIGEST_LENGTH];
159 SHA1_Final(sha_so_far, &temp_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800160
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800161 uint8_t parsed_sha[SHA_DIGEST_LENGTH];
Tao Bao8fce75a2016-11-10 12:33:41 -0800162 if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
163 printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
164 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800165 }
166
Tao Bao8fce75a2016-11-10 12:33:41 -0800167 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
168 // We have a match. Stop reading the partition; we'll return the data we've read so far.
169 printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
170 found = true;
171 break;
Doug Zongker512536a2010-02-17 16:11:44 -0800172 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800173 }
Doug Zongker512536a2010-02-17 16:11:44 -0800174
Tao Bao8fce75a2016-11-10 12:33:41 -0800175 if (!found) {
176 // Ran off the end of the list of (size, sha1) pairs without finding a match.
177 printf("contents of partition \"%s\" didn't match %s\n", partition, filename.c_str());
178 return -1;
179 }
Doug Zongker512536a2010-02-17 16:11:44 -0800180
Tao Bao8fce75a2016-11-10 12:33:41 -0800181 SHA1_Final(file->sha1, &sha_ctx);
Doug Zongker512536a2010-02-17 16:11:44 -0800182
Tao Bao8fce75a2016-11-10 12:33:41 -0800183 buffer.resize(buffer_size);
184 file->data = std::move(buffer);
185 // Fake some stat() info.
186 file->st.st_mode = 0644;
187 file->st.st_uid = 0;
188 file->st.st_gid = 0;
189
190 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800191}
192
193
194// Save the contents of the given FileContents object under the given
195// filename. Return 0 on success.
Doug Zongker1c43c972012-02-28 11:07:09 -0800196int SaveFileContents(const char* filename, const FileContents* file) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800197 unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
198 if (fd == -1) {
199 printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
200 return -1;
201 }
Doug Zongker512536a2010-02-17 16:11:44 -0800202
Tao Bao6e02ea92016-11-17 11:24:07 -0800203 ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
204 if (bytes_written != static_cast<ssize_t>(file->data.size())) {
205 printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
206 file->data.size(), strerror(errno));
207 return -1;
208 }
209 if (ota_fsync(fd) != 0) {
210 printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
211 return -1;
212 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800213 if (ota_close(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800214 printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
215 return -1;
216 }
Doug Zongker512536a2010-02-17 16:11:44 -0800217
Tao Bao6e02ea92016-11-17 11:24:07 -0800218 if (chmod(filename, file->st.st_mode) != 0) {
219 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
220 return -1;
221 }
222 if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
223 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
224 return -1;
225 }
Doug Zongker512536a2010-02-17 16:11:44 -0800226
Tao Bao6e02ea92016-11-17 11:24:07 -0800227 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800228}
229
Doug Zongkerf291d852010-07-07 13:55:25 -0700230// Write a memory buffer to 'target' partition, a string of the form
Elliott Hughes63a31922016-06-09 17:41:22 -0700231// "EMMC:<partition_device>[:...]". The target name
Tao Bao1ce7a2a2015-07-24 15:29:12 -0700232// might contain multiple colons, but WriteToPartition() only uses the first
233// two and ignores the rest. Return 0 on success.
Tao Bao6e02ea92016-11-17 11:24:07 -0800234int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
235 std::vector<std::string> pieces = android::base::Split(target, ":");
Tao Bao8fce75a2016-11-10 12:33:41 -0800236 if (pieces.size() < 2 || pieces[0] != "EMMC") {
Tao Bao6e02ea92016-11-17 11:24:07 -0800237 printf("WriteToPartition called with bad target (%s)\n", target.c_str());
Tao Bao8fce75a2016-11-10 12:33:41 -0800238 return -1;
239 }
240
241 const char* partition = pieces[1].c_str();
Tao Bao6e02ea92016-11-17 11:24:07 -0800242 unique_fd fd(ota_open(partition, O_RDWR));
243 if (fd == -1) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800244 printf("failed to open %s: %s\n", partition, strerror(errno));
245 return -1;
246 }
247
Tao Bao6e02ea92016-11-17 11:24:07 -0800248 size_t start = 0;
249 bool success = false;
Tao Bao8fce75a2016-11-10 12:33:41 -0800250 for (size_t attempt = 0; attempt < 2; ++attempt) {
251 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
252 printf("failed seek on %s: %s\n", partition, strerror(errno));
253 return -1;
254 }
255 while (start < len) {
256 size_t to_write = len - start;
257 if (to_write > 1 << 20) to_write = 1 << 20;
258
259 ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data + start, to_write));
260 if (written == -1) {
261 printf("failed write writing to %s: %s\n", partition, strerror(errno));
Tao Baoaca8e892015-07-17 11:47:44 -0700262 return -1;
Tao Bao8fce75a2016-11-10 12:33:41 -0800263 }
264 start += written;
Tao Baoaca8e892015-07-17 11:47:44 -0700265 }
Doug Zongkerf291d852010-07-07 13:55:25 -0700266
Tao Bao8fce75a2016-11-10 12:33:41 -0800267 if (ota_fsync(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800268 printf("failed to sync to %s: %s\n", partition, strerror(errno));
Tao Bao8fce75a2016-11-10 12:33:41 -0800269 return -1;
Doug Zongkerf291d852010-07-07 13:55:25 -0700270 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800271 if (ota_close(fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800272 printf("failed to close %s: %s\n", partition, strerror(errno));
Tao Bao8fce75a2016-11-10 12:33:41 -0800273 return -1;
Elliott Hughes63a31922016-06-09 17:41:22 -0700274 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800275
Tao Bao6e02ea92016-11-17 11:24:07 -0800276 fd.reset(ota_open(partition, O_RDONLY));
277 if (fd == -1) {
278 printf("failed to reopen %s for verify: %s\n", partition, strerror(errno));
Tao Bao8fce75a2016-11-10 12:33:41 -0800279 return -1;
280 }
281
282 // Drop caches so our subsequent verification read won't just be reading the cache.
Elliott Hughes63a31922016-06-09 17:41:22 -0700283 sync();
Tao Bao6e02ea92016-11-17 11:24:07 -0800284 unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
Tao Bao8fce75a2016-11-10 12:33:41 -0800285 if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
286 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
287 } else {
288 printf(" caches dropped\n");
289 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800290 ota_close(dc);
Tao Bao8fce75a2016-11-10 12:33:41 -0800291 sleep(1);
Elliott Hughes63a31922016-06-09 17:41:22 -0700292
Tao Bao6e02ea92016-11-17 11:24:07 -0800293 // Verify.
Tao Bao8fce75a2016-11-10 12:33:41 -0800294 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
295 printf("failed to seek back to beginning of %s: %s\n", partition, strerror(errno));
296 return -1;
297 }
298
299 unsigned char buffer[4096];
300 start = len;
301 for (size_t p = 0; p < len; p += sizeof(buffer)) {
302 size_t to_read = len - p;
303 if (to_read > sizeof(buffer)) {
304 to_read = sizeof(buffer);
305 }
306
307 size_t so_far = 0;
308 while (so_far < to_read) {
309 ssize_t read_count = TEMP_FAILURE_RETRY(ota_read(fd, buffer + so_far, to_read - so_far));
310 if (read_count == -1) {
311 printf("verify read error %s at %zu: %s\n", partition, p, strerror(errno));
312 return -1;
313 } else if (read_count == 0) {
314 printf("verify read reached unexpected EOF, %s at %zu\n", partition, p);
315 return -1;
316 }
317 if (static_cast<size_t>(read_count) < to_read) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800318 printf("short verify read %s at %zu: %zd %zu\n", partition, p, read_count, to_read);
Tao Bao8fce75a2016-11-10 12:33:41 -0800319 }
320 so_far += read_count;
321 }
322
323 if (memcmp(buffer, data + p, to_read) != 0) {
324 printf("verification failed starting at %zu\n", p);
325 start = p;
326 break;
327 }
328 }
329
330 if (start == len) {
331 printf("verification read succeeded (attempt %zu)\n", attempt + 1);
332 success = true;
333 break;
334 }
335 }
336
337 if (!success) {
338 printf("failed to verify after all attempts\n");
339 return -1;
340 }
341
Tao Bao3dc14cb2016-11-22 11:37:13 -0800342 if (ota_close(fd) == -1) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800343 printf("error closing %s: %s\n", partition, strerror(errno));
Tao Bao8fce75a2016-11-10 12:33:41 -0800344 return -1;
345 }
346 sync();
347
348 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800349}
350
Doug Zongker512536a2010-02-17 16:11:44 -0800351// Take a string 'str' of 40 hex digits and parse it into the 20
352// byte array 'digest'. 'str' may contain only the digest or be of
353// the form "<digest>:<anything>". Return 0 on success, -1 on any
354// error.
355int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800356 const char* ps = str;
357 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800358 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800359 int digit;
360 if (*ps >= '0' && *ps <= '9') {
361 digit = *ps - '0';
362 } else if (*ps >= 'a' && *ps <= 'f') {
363 digit = *ps - 'a' + 10;
364 } else if (*ps >= 'A' && *ps <= 'F') {
365 digit = *ps - 'A' + 10;
366 } else {
367 return -1;
368 }
369 if (i % 2 == 0) {
370 *pd = digit << 4;
371 } else {
372 *pd |= digit;
373 ++pd;
374 }
Doug Zongker512536a2010-02-17 16:11:44 -0800375 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800376 if (*ps != '\0') return -1;
377 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800378}
379
Doug Zongkerc4351c72010-02-22 14:46:32 -0800380// Search an array of sha1 strings for one matching the given sha1.
381// Return the index of the match on success, or -1 if no match is
382// found.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700383int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800384 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
385 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
386 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
387 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
388 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800389 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800390 }
391 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800392}
393
394// Returns 0 if the contents of the file (argv[2]) or the cached file
395// match any of the sha1's on the command line (argv[3:]). Returns
396// nonzero otherwise.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700397int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800398 FileContents file;
Doug Zongker512536a2010-02-17 16:11:44 -0800399
Tao Bao8fce75a2016-11-10 12:33:41 -0800400 // It's okay to specify no sha1s; the check will pass if the
401 // LoadFileContents is successful. (Useful for reading
402 // partitions, where the filename encodes the sha1s; no need to
403 // check them twice.)
404 if (LoadFileContents(filename, &file) != 0 ||
405 (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
406 printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800407
Tao Bao8fce75a2016-11-10 12:33:41 -0800408 // If the source file is missing or corrupted, it might be because
409 // we were killed in the middle of patching it. A copy of it
410 // should have been made in CACHE_TEMP_SOURCE. If that file
411 // exists and matches the sha1 we're looking for, the check still
412 // passes.
413 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
414 printf("failed to load cache file\n");
415 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800416 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800417
418 if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
419 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
420 return 1;
421 }
422 }
423 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800424}
425
426int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800427 ShowBSDiffLicense();
428 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800429}
430
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700431ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800432 int fd = *static_cast<int*>(token);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800433 ssize_t done = 0;
434 ssize_t wrote;
Tao Baoba9a42a2015-06-23 23:23:33 -0700435 while (done < len) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800436 wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700437 if (wrote == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700438 printf("error writing %zd bytes: %s\n", (len-done), strerror(errno));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800439 return done;
440 }
441 done += wrote;
Doug Zongker512536a2010-02-17 16:11:44 -0800442 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800443 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800444}
445
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700446ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800447 std::string* s = static_cast<std::string*>(token);
448 s->append(reinterpret_cast<const char*>(data), len);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800449 return len;
Doug Zongker512536a2010-02-17 16:11:44 -0800450}
451
452// Return the amount of free space (in bytes) on the filesystem
453// containing filename. filename must exist. Return -1 on error.
454size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800455 struct statfs sf;
456 if (statfs(filename, &sf) != 0) {
457 printf("failed to statfs %s: %s\n", filename, strerror(errno));
458 return -1;
459 }
caozhiyuan3b497762015-05-19 17:21:00 +0800460 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800461}
462
Doug Zongkerc4351c72010-02-22 14:46:32 -0800463int CacheSizeCheck(size_t bytes) {
464 if (MakeFreeSpaceOnCache(bytes) < 0) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700465 printf("unable to make %zu bytes available on /cache\n", bytes);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800466 return 1;
467 } else {
468 return 0;
469 }
470}
471
Doug Zongkerc4351c72010-02-22 14:46:32 -0800472// This function applies binary patches to files in a way that is safe
Doug Zongker512536a2010-02-17 16:11:44 -0800473// (the original file is not touched until we have the desired
474// replacement for it) and idempotent (it's okay to run this program
475// multiple times).
476//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800477// - if the sha1 hash of <target_filename> is <target_sha1_string>,
478// does nothing and exits successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800479//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800480// - otherwise, if the sha1 hash of <source_filename> is one of the
481// entries in <patch_sha1_str>, the corresponding patch from
482// <patch_data> (which must be a VAL_BLOB) is applied to produce a
483// new file (the type of patch is automatically detected from the
Tao Baoabba55b2015-07-17 18:11:12 -0700484// blob data). If that new file has sha1 hash <target_sha1_str>,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800485// moves it to replace <target_filename>, and exits successfully.
486// Note that if <source_filename> and <target_filename> are not the
487// same, <source_filename> is NOT deleted on success.
488// <target_filename> may be the string "-" to mean "the same as
489// source_filename".
Doug Zongker512536a2010-02-17 16:11:44 -0800490//
491// - otherwise, or if any error is encountered, exits with non-zero
492// status.
493//
Doug Zongkerf291d852010-07-07 13:55:25 -0700494// <source_filename> may refer to a partition to read the source data.
Tao Baoabba55b2015-07-17 18:11:12 -0700495// See the comments for the LoadPartitionContents() function above
Doug Zongkerc4351c72010-02-22 14:46:32 -0800496// for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800497
Doug Zongkerc4351c72010-02-22 14:46:32 -0800498int applypatch(const char* source_filename,
499 const char* target_filename,
500 const char* target_sha1_str,
501 size_t target_size,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700502 const std::vector<std::string>& patch_sha1_str,
Tao Baofada91c2016-10-27 18:16:06 -0700503 const std::vector<std::unique_ptr<Value>>& patch_data,
504 const Value* bonus_data) {
Doug Zongkerbf80f492012-10-19 12:24:26 -0700505 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800506
Tao Baoba9a42a2015-06-23 23:23:33 -0700507 if (target_filename[0] == '-' && target_filename[1] == '\0') {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800508 target_filename = source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800509 }
510
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800511 uint8_t target_sha1[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800512 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
513 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800514 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800515 }
Doug Zongker512536a2010-02-17 16:11:44 -0800516
Doug Zongkerc4351c72010-02-22 14:46:32 -0800517 FileContents source_file;
Tao Baofada91c2016-10-27 18:16:06 -0700518 const Value* source_patch_value = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -0800519
Doug Zongkerc4351c72010-02-22 14:46:32 -0800520 // We try to load the target file into the source_file object.
Doug Zongkera1bc1482014-02-13 15:18:19 -0800521 if (LoadFileContents(target_filename, &source_file) == 0) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800522 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800523 // The early-exit case: the patch was already applied, this file
524 // has the desired hash, nothing for us to do.
Tao Baoabba55b2015-07-17 18:11:12 -0700525 printf("already %s\n", short_sha1(target_sha1).c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800526 return 0;
527 }
528 }
Doug Zongker512536a2010-02-17 16:11:44 -0800529
Yabin Cuid6c93af2016-02-10 16:41:10 -0800530 if (source_file.data.empty() ||
Doug Zongkerc4351c72010-02-22 14:46:32 -0800531 (target_filename != source_filename &&
532 strcmp(target_filename, source_filename) != 0)) {
533 // Need to load the source file: either we failed to load the
534 // target file, or we did but it's different from the source file.
Yabin Cuid6c93af2016-02-10 16:41:10 -0800535 source_file.data.clear();
Doug Zongkera1bc1482014-02-13 15:18:19 -0800536 LoadFileContents(source_filename, &source_file);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800537 }
538
Yabin Cuid6c93af2016-02-10 16:41:10 -0800539 if (!source_file.data.empty()) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700540 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800541 if (to_use >= 0) {
Tao Baofada91c2016-10-27 18:16:06 -0700542 source_patch_value = patch_data[to_use].get();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800543 }
544 }
545
Tao Bao8fce75a2016-11-10 12:33:41 -0800546 FileContents copy_file;
547 const Value* copy_patch_value = nullptr;
Tao Baofada91c2016-10-27 18:16:06 -0700548 if (source_patch_value == nullptr) {
Yabin Cuid6c93af2016-02-10 16:41:10 -0800549 source_file.data.clear();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800550 printf("source file is bad; trying copy\n");
551
Doug Zongkera1bc1482014-02-13 15:18:19 -0800552 if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800553 // fail.
554 printf("failed to read copy file\n");
555 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800556 }
557
Tianjie Xuaced5d92016-10-12 10:55:04 -0700558 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
Doug Zongker8cd9e4f2010-08-12 17:38:09 -0700559 if (to_use >= 0) {
Tao Baofada91c2016-10-27 18:16:06 -0700560 copy_patch_value = patch_data[to_use].get();
Doug Zongker512536a2010-02-17 16:11:44 -0800561 }
562
Tao Baofada91c2016-10-27 18:16:06 -0700563 if (copy_patch_value == nullptr) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800564 // fail.
565 printf("copy file doesn't match source SHA-1s either\n");
566 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800567 }
Doug Zongker512536a2010-02-17 16:11:44 -0800568 }
569
Yabin Cuid6c93af2016-02-10 16:41:10 -0800570 return GenerateTarget(&source_file, source_patch_value,
571 &copy_file, copy_patch_value,
572 source_filename, target_filename,
573 target_sha1, target_size, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800574}
575
Tao Baoabba55b2015-07-17 18:11:12 -0700576/*
577 * This function flashes a given image to the target partition. It verifies
578 * the target cheksum first, and will return if target has the desired hash.
579 * It checks the checksum of the given source image before flashing, and
580 * verifies the target partition afterwards. The function is idempotent.
581 * Returns zero on success.
582 */
583int applypatch_flash(const char* source_filename, const char* target_filename,
584 const char* target_sha1_str, size_t target_size) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800585 printf("flash %s: ", target_filename);
Tao Baoabba55b2015-07-17 18:11:12 -0700586
Tao Bao6e02ea92016-11-17 11:24:07 -0800587 uint8_t target_sha1[SHA_DIGEST_LENGTH];
588 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
589 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
590 return 1;
591 }
Tao Baoabba55b2015-07-17 18:11:12 -0700592
Tao Bao6e02ea92016-11-17 11:24:07 -0800593 std::string target_str(target_filename);
594 std::vector<std::string> pieces = android::base::Split(target_str, ":");
595 if (pieces.size() != 2 || pieces[0] != "EMMC") {
596 printf("invalid target name \"%s\"", target_filename);
597 return 1;
598 }
Tao Baoabba55b2015-07-17 18:11:12 -0700599
Tao Bao6e02ea92016-11-17 11:24:07 -0800600 // Load the target into the source_file object to see if already applied.
601 pieces.push_back(std::to_string(target_size));
602 pieces.push_back(target_sha1_str);
603 std::string fullname = android::base::Join(pieces, ':');
604 FileContents source_file;
605 if (LoadPartitionContents(fullname, &source_file) == 0 &&
606 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
607 // The early-exit case: the image was already applied, this partition
608 // has the desired hash, nothing for us to do.
609 printf("already %s\n", short_sha1(target_sha1).c_str());
Tao Baoabba55b2015-07-17 18:11:12 -0700610 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800611 }
612
613 if (LoadFileContents(source_filename, &source_file) == 0) {
614 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
615 // The source doesn't have desired checksum.
616 printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
617 printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
618 short_sha1(source_file.sha1).c_str());
619 return 1;
620 }
621 }
622
623 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
624 printf("write of copied data to %s failed\n", target_filename);
625 return 1;
626 }
627 return 0;
Tao Baoabba55b2015-07-17 18:11:12 -0700628}
629
Doug Zongker1c43c972012-02-28 11:07:09 -0800630static int GenerateTarget(FileContents* source_file,
631 const Value* source_patch_value,
632 FileContents* copy_file,
633 const Value* copy_patch_value,
634 const char* source_filename,
635 const char* target_filename,
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800636 const uint8_t target_sha1[SHA_DIGEST_LENGTH],
Doug Zongkera3ccba62012-08-20 15:28:02 -0700637 size_t target_size,
638 const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800639 // assume that target_filename (eg "/system/app/Foo.apk") is located
640 // on the same filesystem as its top-level directory ("/system").
641 // We need something that exists for calling statfs().
642 std::string target_fs = target_filename;
643 auto slash_pos = target_fs.find('/', 1);
644 if (slash_pos != std::string::npos) {
645 target_fs.resize(slash_pos);
646 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800647
Tao Bao6e02ea92016-11-17 11:24:07 -0800648 FileContents* source_to_use;
649 const Value* patch;
650 if (source_patch_value != nullptr) {
651 source_to_use = source_file;
652 patch = source_patch_value;
653 } else {
654 source_to_use = copy_file;
655 patch = copy_patch_value;
656 }
Yabin Cuid483c202016-02-03 17:08:52 -0800657
Tao Bao6e02ea92016-11-17 11:24:07 -0800658 if (patch->type != VAL_BLOB) {
659 printf("patch is not a blob\n");
660 return 1;
661 }
Yabin Cuid483c202016-02-03 17:08:52 -0800662
Tao Bao6e02ea92016-11-17 11:24:07 -0800663 const char* header = &patch->data[0];
664 size_t header_bytes_read = patch->data.size();
665 bool use_bsdiff = false;
666 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
667 use_bsdiff = true;
668 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
669 use_bsdiff = false;
670 } else {
671 printf("Unknown patch file format\n");
672 return 1;
673 }
Doug Zongker512536a2010-02-17 16:11:44 -0800674
Tao Bao6e02ea92016-11-17 11:24:07 -0800675 bool target_is_partition = (strncmp(target_filename, "EMMC:", 5) == 0);
676 const std::string tmp_target_filename = std::string(target_filename) + ".patch";
Doug Zongkerc4351c72010-02-22 14:46:32 -0800677
Tao Bao6e02ea92016-11-17 11:24:07 -0800678 int retry = 1;
679 bool made_copy = false;
680 SHA_CTX ctx;
681 std::string memory_sink_str; // Don't need to reserve space.
682 do {
683 // Is there enough room in the target filesystem to hold the patched file?
Doug Zongkerc4351c72010-02-22 14:46:32 -0800684
Yabin Cuid483c202016-02-03 17:08:52 -0800685 if (target_is_partition) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800686 // If the target is a partition, we're actually going to
687 // write the output to /tmp and then copy it to the
688 // partition. statfs() always returns 0 blocks free for
689 // /tmp, so instead we'll just assume that /tmp has enough
690 // space to hold the file.
691
692 // We still write the original source to cache, in case
693 // the partition write is interrupted.
694 if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
695 printf("not enough free space on /cache\n");
696 return 1;
697 }
698 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
699 printf("failed to back up source file\n");
700 return 1;
701 }
702 made_copy = true;
703 retry = 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800704 } else {
Tao Bao6e02ea92016-11-17 11:24:07 -0800705 bool enough_space = false;
706 if (retry > 0) {
707 size_t free_space = FreeSpaceForFile(target_fs.c_str());
708 enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum
709 (free_space > (target_size * 3 / 2)); // 50% margin of error
710 if (!enough_space) {
711 printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", target_size,
712 free_space, retry, enough_space);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800713 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800714 }
715
716 if (!enough_space) {
717 retry = 0;
718 }
719
720 if (!enough_space && source_patch_value != nullptr) {
721 // Using the original source, but not enough free space. First
722 // copy the source file to cache, then delete it from the original
723 // location.
724
725 if (strncmp(source_filename, "EMMC:", 5) == 0) {
726 // It's impossible to free space on the target filesystem by
727 // deleting the source if the source is a partition. If
728 // we're ever in a state where we need to do this, fail.
729 printf("not enough free space for target but source is partition\n");
730 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800731 }
Doug Zongker512536a2010-02-17 16:11:44 -0800732
Tao Bao6e02ea92016-11-17 11:24:07 -0800733 if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
734 printf("not enough free space on /cache\n");
735 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800736 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800737
738 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
739 printf("failed to back up source file\n");
740 return 1;
741 }
742 made_copy = true;
743 unlink(source_filename);
744
745 size_t free_space = FreeSpaceForFile(target_fs.c_str());
746 printf("(now %zu bytes free for target) ", free_space);
747 }
Doug Zongker512536a2010-02-17 16:11:44 -0800748 }
749
Tao Bao6e02ea92016-11-17 11:24:07 -0800750 SinkFn sink = nullptr;
751 void* token = nullptr;
752 unique_fd output_fd;
753 if (target_is_partition) {
754 // We store the decoded output in memory.
755 sink = MemorySink;
756 token = &memory_sink_str;
757 } else {
758 // We write the decoded output to "<tgt-file>.patch".
759 output_fd.reset(ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
760 S_IRUSR | S_IWUSR));
761 if (output_fd == -1) {
762 printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(), strerror(errno));
763 return 1;
764 }
765 sink = FileSink;
766 token = &output_fd;
Tao Baoba9a42a2015-06-23 23:23:33 -0700767 }
Doug Zongker512536a2010-02-17 16:11:44 -0800768
Tao Bao6e02ea92016-11-17 11:24:07 -0800769 SHA1_Init(&ctx);
770
771 int result;
772 if (use_bsdiff) {
773 result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(), patch, 0,
774 sink, token, &ctx);
775 } else {
776 result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(), patch, sink,
777 token, &ctx, bonus_data);
778 }
779
780 if (!target_is_partition) {
781 if (ota_fsync(output_fd) != 0) {
782 printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
783 result = 1;
784 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800785 if (ota_close(output_fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800786 printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
787 result = 1;
788 }
789 }
790
791 if (result != 0) {
792 if (retry == 0) {
793 printf("applying patch failed\n");
794 return 1;
795 } else {
796 printf("applying patch failed; retrying\n");
797 }
798 if (!target_is_partition) {
799 unlink(tmp_target_filename.c_str());
800 }
801 } else {
802 // succeeded; no need to retry
803 break;
804 }
805 } while (retry-- > 0);
806
807 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
808 SHA1_Final(current_target_sha1, &ctx);
809 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
810 printf("patch did not produce expected sha1\n");
811 return 1;
812 } else {
813 printf("now %s\n", short_sha1(target_sha1).c_str());
814 }
815
816 if (target_is_partition) {
817 // Copy the temp file to the partition.
818 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
819 memory_sink_str.size(), target_filename) != 0) {
820 printf("write of patched data to %s failed\n", target_filename);
821 return 1;
822 }
823 } else {
824 // Give the .patch file the same owner, group, and mode of the original source file.
825 if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
826 printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
827 return 1;
828 }
829 if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid,
830 source_to_use->st.st_gid) != 0) {
831 printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
832 return 1;
833 }
834
835 // Finally, rename the .patch file to replace the target file.
836 if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
837 printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
838 return 1;
839 }
840 }
841
842 // If this run of applypatch created the copy, and we're here, we can delete it.
843 if (made_copy) {
844 unlink(CACHE_TEMP_SOURCE);
845 }
846
847 // Success!
848 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800849}