blob: 8682e128b4d400aecc0da6815a4f9116bc636b22 [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 Bao358c2ec2016-11-28 11:48:43 -080069 unique_file f(ota_fopen(filename, "rb"));
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 Bao358c2ec2016-11-28 11:48:43 -0800121 unique_file dev(ota_fopen(partition, "rb"));
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 }
katao9a6f5202016-12-19 11:22:30 +0800335
336 if (ota_close(fd) != 0) {
337 printf("failed to close %s: %s\n", partition, strerror(errno));
338 return -1;
339 }
340
341 fd.reset(ota_open(partition, O_RDWR));
342 if (fd == -1) {
343 printf("failed to reopen %s for retry write && verify: %s\n", partition, strerror(errno));
344 return -1;
345 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800346 }
347
348 if (!success) {
349 printf("failed to verify after all attempts\n");
350 return -1;
351 }
352
Tao Bao3dc14cb2016-11-22 11:37:13 -0800353 if (ota_close(fd) == -1) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800354 printf("error closing %s: %s\n", partition, strerror(errno));
Tao Bao8fce75a2016-11-10 12:33:41 -0800355 return -1;
356 }
357 sync();
358
359 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800360}
361
Doug Zongker512536a2010-02-17 16:11:44 -0800362// Take a string 'str' of 40 hex digits and parse it into the 20
363// byte array 'digest'. 'str' may contain only the digest or be of
364// the form "<digest>:<anything>". Return 0 on success, -1 on any
365// error.
366int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800367 const char* ps = str;
368 uint8_t* pd = digest;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800369 for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800370 int digit;
371 if (*ps >= '0' && *ps <= '9') {
372 digit = *ps - '0';
373 } else if (*ps >= 'a' && *ps <= 'f') {
374 digit = *ps - 'a' + 10;
375 } else if (*ps >= 'A' && *ps <= 'F') {
376 digit = *ps - 'A' + 10;
377 } else {
378 return -1;
379 }
380 if (i % 2 == 0) {
381 *pd = digit << 4;
382 } else {
383 *pd |= digit;
384 ++pd;
385 }
Doug Zongker512536a2010-02-17 16:11:44 -0800386 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800387 if (*ps != '\0') return -1;
388 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800389}
390
Doug Zongkerc4351c72010-02-22 14:46:32 -0800391// Search an array of sha1 strings for one matching the given sha1.
392// Return the index of the match on success, or -1 if no match is
393// found.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700394int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800395 for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
396 uint8_t patch_sha1[SHA_DIGEST_LENGTH];
397 if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
398 memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
399 return i;
Doug Zongker512536a2010-02-17 16:11:44 -0800400 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800401 }
402 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800403}
404
405// Returns 0 if the contents of the file (argv[2]) or the cached file
406// match any of the sha1's on the command line (argv[3:]). Returns
407// nonzero otherwise.
Tianjie Xuaced5d92016-10-12 10:55:04 -0700408int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
Tao Bao8fce75a2016-11-10 12:33:41 -0800409 FileContents file;
Doug Zongker512536a2010-02-17 16:11:44 -0800410
Tao Bao8fce75a2016-11-10 12:33:41 -0800411 // It's okay to specify no sha1s; the check will pass if the
412 // LoadFileContents is successful. (Useful for reading
413 // partitions, where the filename encodes the sha1s; no need to
414 // check them twice.)
415 if (LoadFileContents(filename, &file) != 0 ||
416 (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
417 printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800418
Tao Bao8fce75a2016-11-10 12:33:41 -0800419 // If the source file is missing or corrupted, it might be because
420 // we were killed in the middle of patching it. A copy of it
421 // should have been made in CACHE_TEMP_SOURCE. If that file
422 // exists and matches the sha1 we're looking for, the check still
423 // passes.
424 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
425 printf("failed to load cache file\n");
426 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800427 }
Tao Bao8fce75a2016-11-10 12:33:41 -0800428
429 if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
430 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
431 return 1;
432 }
433 }
434 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800435}
436
437int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800438 ShowBSDiffLicense();
439 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800440}
441
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700442ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800443 int fd = *static_cast<int*>(token);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800444 ssize_t done = 0;
445 ssize_t wrote;
Tao Baoba9a42a2015-06-23 23:23:33 -0700446 while (done < len) {
Jed Estepa7b9a462015-12-15 16:04:53 -0800447 wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done));
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700448 if (wrote == -1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700449 printf("error writing %zd bytes: %s\n", (len-done), strerror(errno));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800450 return done;
451 }
452 done += wrote;
Doug Zongker512536a2010-02-17 16:11:44 -0800453 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800454 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800455}
456
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700457ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
Yabin Cuid483c202016-02-03 17:08:52 -0800458 std::string* s = static_cast<std::string*>(token);
459 s->append(reinterpret_cast<const char*>(data), len);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800460 return len;
Doug Zongker512536a2010-02-17 16:11:44 -0800461}
462
463// Return the amount of free space (in bytes) on the filesystem
464// containing filename. filename must exist. Return -1 on error.
465size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800466 struct statfs sf;
467 if (statfs(filename, &sf) != 0) {
468 printf("failed to statfs %s: %s\n", filename, strerror(errno));
469 return -1;
470 }
caozhiyuan3b497762015-05-19 17:21:00 +0800471 return sf.f_bsize * sf.f_bavail;
Doug Zongker512536a2010-02-17 16:11:44 -0800472}
473
Doug Zongkerc4351c72010-02-22 14:46:32 -0800474int CacheSizeCheck(size_t bytes) {
475 if (MakeFreeSpaceOnCache(bytes) < 0) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700476 printf("unable to make %zu bytes available on /cache\n", bytes);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800477 return 1;
478 } else {
479 return 0;
480 }
481}
482
Doug Zongkerc4351c72010-02-22 14:46:32 -0800483// This function applies binary patches to files in a way that is safe
Doug Zongker512536a2010-02-17 16:11:44 -0800484// (the original file is not touched until we have the desired
485// replacement for it) and idempotent (it's okay to run this program
486// multiple times).
487//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800488// - if the sha1 hash of <target_filename> is <target_sha1_string>,
489// does nothing and exits successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800490//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800491// - otherwise, if the sha1 hash of <source_filename> is one of the
492// entries in <patch_sha1_str>, the corresponding patch from
493// <patch_data> (which must be a VAL_BLOB) is applied to produce a
494// new file (the type of patch is automatically detected from the
Tao Baoabba55b2015-07-17 18:11:12 -0700495// blob data). If that new file has sha1 hash <target_sha1_str>,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800496// moves it to replace <target_filename>, and exits successfully.
497// Note that if <source_filename> and <target_filename> are not the
498// same, <source_filename> is NOT deleted on success.
499// <target_filename> may be the string "-" to mean "the same as
500// source_filename".
Doug Zongker512536a2010-02-17 16:11:44 -0800501//
502// - otherwise, or if any error is encountered, exits with non-zero
503// status.
504//
Doug Zongkerf291d852010-07-07 13:55:25 -0700505// <source_filename> may refer to a partition to read the source data.
Tao Baoabba55b2015-07-17 18:11:12 -0700506// See the comments for the LoadPartitionContents() function above
Doug Zongkerc4351c72010-02-22 14:46:32 -0800507// for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800508
Doug Zongkerc4351c72010-02-22 14:46:32 -0800509int applypatch(const char* source_filename,
510 const char* target_filename,
511 const char* target_sha1_str,
512 size_t target_size,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700513 const std::vector<std::string>& patch_sha1_str,
Tao Baofada91c2016-10-27 18:16:06 -0700514 const std::vector<std::unique_ptr<Value>>& patch_data,
515 const Value* bonus_data) {
Doug Zongkerbf80f492012-10-19 12:24:26 -0700516 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800517
Tao Baoba9a42a2015-06-23 23:23:33 -0700518 if (target_filename[0] == '-' && target_filename[1] == '\0') {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800519 target_filename = source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800520 }
521
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800522 uint8_t target_sha1[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800523 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
524 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800525 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800526 }
Doug Zongker512536a2010-02-17 16:11:44 -0800527
Doug Zongkerc4351c72010-02-22 14:46:32 -0800528 FileContents source_file;
Tao Baofada91c2016-10-27 18:16:06 -0700529 const Value* source_patch_value = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -0800530
Doug Zongkerc4351c72010-02-22 14:46:32 -0800531 // We try to load the target file into the source_file object.
Doug Zongkera1bc1482014-02-13 15:18:19 -0800532 if (LoadFileContents(target_filename, &source_file) == 0) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800533 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800534 // The early-exit case: the patch was already applied, this file
535 // has the desired hash, nothing for us to do.
Tao Baoabba55b2015-07-17 18:11:12 -0700536 printf("already %s\n", short_sha1(target_sha1).c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800537 return 0;
538 }
539 }
Doug Zongker512536a2010-02-17 16:11:44 -0800540
Yabin Cuid6c93af2016-02-10 16:41:10 -0800541 if (source_file.data.empty() ||
Doug Zongkerc4351c72010-02-22 14:46:32 -0800542 (target_filename != source_filename &&
543 strcmp(target_filename, source_filename) != 0)) {
544 // Need to load the source file: either we failed to load the
545 // target file, or we did but it's different from the source file.
Yabin Cuid6c93af2016-02-10 16:41:10 -0800546 source_file.data.clear();
Doug Zongkera1bc1482014-02-13 15:18:19 -0800547 LoadFileContents(source_filename, &source_file);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800548 }
549
Yabin Cuid6c93af2016-02-10 16:41:10 -0800550 if (!source_file.data.empty()) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700551 int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800552 if (to_use >= 0) {
Tao Baofada91c2016-10-27 18:16:06 -0700553 source_patch_value = patch_data[to_use].get();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800554 }
555 }
556
Tao Bao8fce75a2016-11-10 12:33:41 -0800557 FileContents copy_file;
558 const Value* copy_patch_value = nullptr;
Tao Baofada91c2016-10-27 18:16:06 -0700559 if (source_patch_value == nullptr) {
Yabin Cuid6c93af2016-02-10 16:41:10 -0800560 source_file.data.clear();
Doug Zongkerc4351c72010-02-22 14:46:32 -0800561 printf("source file is bad; trying copy\n");
562
Doug Zongkera1bc1482014-02-13 15:18:19 -0800563 if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800564 // fail.
565 printf("failed to read copy file\n");
566 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800567 }
568
Tianjie Xuaced5d92016-10-12 10:55:04 -0700569 int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
Doug Zongker8cd9e4f2010-08-12 17:38:09 -0700570 if (to_use >= 0) {
Tao Baofada91c2016-10-27 18:16:06 -0700571 copy_patch_value = patch_data[to_use].get();
Doug Zongker512536a2010-02-17 16:11:44 -0800572 }
573
Tao Baofada91c2016-10-27 18:16:06 -0700574 if (copy_patch_value == nullptr) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800575 // fail.
576 printf("copy file doesn't match source SHA-1s either\n");
577 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800578 }
Doug Zongker512536a2010-02-17 16:11:44 -0800579 }
580
Yabin Cuid6c93af2016-02-10 16:41:10 -0800581 return GenerateTarget(&source_file, source_patch_value,
582 &copy_file, copy_patch_value,
583 source_filename, target_filename,
584 target_sha1, target_size, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800585}
586
Tao Baoabba55b2015-07-17 18:11:12 -0700587/*
588 * This function flashes a given image to the target partition. It verifies
589 * the target cheksum first, and will return if target has the desired hash.
590 * It checks the checksum of the given source image before flashing, and
591 * verifies the target partition afterwards. The function is idempotent.
592 * Returns zero on success.
593 */
594int applypatch_flash(const char* source_filename, const char* target_filename,
595 const char* target_sha1_str, size_t target_size) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800596 printf("flash %s: ", target_filename);
Tao Baoabba55b2015-07-17 18:11:12 -0700597
Tao Bao6e02ea92016-11-17 11:24:07 -0800598 uint8_t target_sha1[SHA_DIGEST_LENGTH];
599 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
600 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
601 return 1;
602 }
Tao Baoabba55b2015-07-17 18:11:12 -0700603
Tao Bao6e02ea92016-11-17 11:24:07 -0800604 std::string target_str(target_filename);
605 std::vector<std::string> pieces = android::base::Split(target_str, ":");
606 if (pieces.size() != 2 || pieces[0] != "EMMC") {
607 printf("invalid target name \"%s\"", target_filename);
608 return 1;
609 }
Tao Baoabba55b2015-07-17 18:11:12 -0700610
Tao Bao6e02ea92016-11-17 11:24:07 -0800611 // Load the target into the source_file object to see if already applied.
612 pieces.push_back(std::to_string(target_size));
613 pieces.push_back(target_sha1_str);
614 std::string fullname = android::base::Join(pieces, ':');
615 FileContents source_file;
616 if (LoadPartitionContents(fullname, &source_file) == 0 &&
617 memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
618 // The early-exit case: the image was already applied, this partition
619 // has the desired hash, nothing for us to do.
620 printf("already %s\n", short_sha1(target_sha1).c_str());
Tao Baoabba55b2015-07-17 18:11:12 -0700621 return 0;
Tao Bao6e02ea92016-11-17 11:24:07 -0800622 }
623
624 if (LoadFileContents(source_filename, &source_file) == 0) {
625 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
626 // The source doesn't have desired checksum.
627 printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
628 printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
629 short_sha1(source_file.sha1).c_str());
630 return 1;
631 }
632 }
633
634 if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
635 printf("write of copied data to %s failed\n", target_filename);
636 return 1;
637 }
638 return 0;
Tao Baoabba55b2015-07-17 18:11:12 -0700639}
640
Doug Zongker1c43c972012-02-28 11:07:09 -0800641static int GenerateTarget(FileContents* source_file,
642 const Value* source_patch_value,
643 FileContents* copy_file,
644 const Value* copy_patch_value,
645 const char* source_filename,
646 const char* target_filename,
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800647 const uint8_t target_sha1[SHA_DIGEST_LENGTH],
Doug Zongkera3ccba62012-08-20 15:28:02 -0700648 size_t target_size,
649 const Value* bonus_data) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800650 // assume that target_filename (eg "/system/app/Foo.apk") is located
651 // on the same filesystem as its top-level directory ("/system").
652 // We need something that exists for calling statfs().
653 std::string target_fs = target_filename;
654 auto slash_pos = target_fs.find('/', 1);
655 if (slash_pos != std::string::npos) {
656 target_fs.resize(slash_pos);
657 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800658
Tao Bao6e02ea92016-11-17 11:24:07 -0800659 FileContents* source_to_use;
660 const Value* patch;
661 if (source_patch_value != nullptr) {
662 source_to_use = source_file;
663 patch = source_patch_value;
664 } else {
665 source_to_use = copy_file;
666 patch = copy_patch_value;
667 }
Yabin Cuid483c202016-02-03 17:08:52 -0800668
Tao Bao6e02ea92016-11-17 11:24:07 -0800669 if (patch->type != VAL_BLOB) {
670 printf("patch is not a blob\n");
671 return 1;
672 }
Yabin Cuid483c202016-02-03 17:08:52 -0800673
Tao Bao6e02ea92016-11-17 11:24:07 -0800674 const char* header = &patch->data[0];
675 size_t header_bytes_read = patch->data.size();
676 bool use_bsdiff = false;
677 if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
678 use_bsdiff = true;
679 } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
680 use_bsdiff = false;
681 } else {
682 printf("Unknown patch file format\n");
683 return 1;
684 }
Doug Zongker512536a2010-02-17 16:11:44 -0800685
Tao Bao6e02ea92016-11-17 11:24:07 -0800686 bool target_is_partition = (strncmp(target_filename, "EMMC:", 5) == 0);
687 const std::string tmp_target_filename = std::string(target_filename) + ".patch";
Doug Zongkerc4351c72010-02-22 14:46:32 -0800688
Tao Bao6e02ea92016-11-17 11:24:07 -0800689 int retry = 1;
690 bool made_copy = false;
691 SHA_CTX ctx;
692 std::string memory_sink_str; // Don't need to reserve space.
693 do {
694 // Is there enough room in the target filesystem to hold the patched file?
Doug Zongkerc4351c72010-02-22 14:46:32 -0800695
Yabin Cuid483c202016-02-03 17:08:52 -0800696 if (target_is_partition) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800697 // If the target is a partition, we're actually going to
698 // write the output to /tmp and then copy it to the
699 // partition. statfs() always returns 0 blocks free for
700 // /tmp, so instead we'll just assume that /tmp has enough
701 // space to hold the file.
702
703 // We still write the original source to cache, in case
704 // the partition write is interrupted.
705 if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
706 printf("not enough free space on /cache\n");
707 return 1;
708 }
709 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
710 printf("failed to back up source file\n");
711 return 1;
712 }
713 made_copy = true;
714 retry = 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800715 } else {
Tao Bao6e02ea92016-11-17 11:24:07 -0800716 bool enough_space = false;
717 if (retry > 0) {
718 size_t free_space = FreeSpaceForFile(target_fs.c_str());
719 enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum
720 (free_space > (target_size * 3 / 2)); // 50% margin of error
721 if (!enough_space) {
722 printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", target_size,
723 free_space, retry, enough_space);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800724 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800725 }
726
727 if (!enough_space) {
728 retry = 0;
729 }
730
731 if (!enough_space && source_patch_value != nullptr) {
732 // Using the original source, but not enough free space. First
733 // copy the source file to cache, then delete it from the original
734 // location.
735
736 if (strncmp(source_filename, "EMMC:", 5) == 0) {
737 // It's impossible to free space on the target filesystem by
738 // deleting the source if the source is a partition. If
739 // we're ever in a state where we need to do this, fail.
740 printf("not enough free space for target but source is partition\n");
741 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800742 }
Doug Zongker512536a2010-02-17 16:11:44 -0800743
Tao Bao6e02ea92016-11-17 11:24:07 -0800744 if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
745 printf("not enough free space on /cache\n");
746 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800747 }
Tao Bao6e02ea92016-11-17 11:24:07 -0800748
749 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
750 printf("failed to back up source file\n");
751 return 1;
752 }
753 made_copy = true;
754 unlink(source_filename);
755
756 size_t free_space = FreeSpaceForFile(target_fs.c_str());
757 printf("(now %zu bytes free for target) ", free_space);
758 }
Doug Zongker512536a2010-02-17 16:11:44 -0800759 }
760
Tao Bao6e02ea92016-11-17 11:24:07 -0800761 SinkFn sink = nullptr;
762 void* token = nullptr;
763 unique_fd output_fd;
764 if (target_is_partition) {
765 // We store the decoded output in memory.
766 sink = MemorySink;
767 token = &memory_sink_str;
768 } else {
769 // We write the decoded output to "<tgt-file>.patch".
770 output_fd.reset(ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
771 S_IRUSR | S_IWUSR));
772 if (output_fd == -1) {
773 printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(), strerror(errno));
774 return 1;
775 }
776 sink = FileSink;
777 token = &output_fd;
Tao Baoba9a42a2015-06-23 23:23:33 -0700778 }
Doug Zongker512536a2010-02-17 16:11:44 -0800779
Tao Bao6e02ea92016-11-17 11:24:07 -0800780 SHA1_Init(&ctx);
781
782 int result;
783 if (use_bsdiff) {
784 result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(), patch, 0,
785 sink, token, &ctx);
786 } else {
787 result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(), patch, sink,
788 token, &ctx, bonus_data);
789 }
790
791 if (!target_is_partition) {
792 if (ota_fsync(output_fd) != 0) {
793 printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
794 result = 1;
795 }
Tao Bao3dc14cb2016-11-22 11:37:13 -0800796 if (ota_close(output_fd) != 0) {
Tao Bao6e02ea92016-11-17 11:24:07 -0800797 printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
798 result = 1;
799 }
800 }
801
802 if (result != 0) {
803 if (retry == 0) {
804 printf("applying patch failed\n");
805 return 1;
806 } else {
807 printf("applying patch failed; retrying\n");
808 }
809 if (!target_is_partition) {
810 unlink(tmp_target_filename.c_str());
811 }
812 } else {
813 // succeeded; no need to retry
814 break;
815 }
816 } while (retry-- > 0);
817
818 uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
819 SHA1_Final(current_target_sha1, &ctx);
820 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
821 printf("patch did not produce expected sha1\n");
822 return 1;
823 } else {
824 printf("now %s\n", short_sha1(target_sha1).c_str());
825 }
826
827 if (target_is_partition) {
828 // Copy the temp file to the partition.
829 if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
830 memory_sink_str.size(), target_filename) != 0) {
831 printf("write of patched data to %s failed\n", target_filename);
832 return 1;
833 }
834 } else {
835 // Give the .patch file the same owner, group, and mode of the original source file.
836 if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
837 printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
838 return 1;
839 }
840 if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid,
841 source_to_use->st.st_gid) != 0) {
842 printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
843 return 1;
844 }
845
846 // Finally, rename the .patch file to replace the target file.
847 if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
848 printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
849 return 1;
850 }
851 }
852
853 // If this run of applypatch created the copy, and we're here, we can delete it.
854 if (made_copy) {
855 unlink(CACHE_TEMP_SOURCE);
856 }
857
858 // Success!
859 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800860}