blob: b06a64f213a3ca889283c96a8aa15cbcc900d8b8 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2009 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 Bao97555da2016-12-15 10:15:06 -080017// See imgdiff.cpp in this directory for a description of the patch file
Doug Zongker512536a2010-02-17 16:11:44 -080018// format.
19
Tao Bao97555da2016-12-15 10:15:06 -080020#include <applypatch/imgpatch.h>
21
22#include <errno.h>
Doug Zongker512536a2010-02-17 16:11:44 -080023#include <stdio.h>
Tao Bao97555da2016-12-15 10:15:06 -080024#include <string.h>
Mark Salyzynf3bb31c2014-03-14 09:39:48 -070025#include <sys/cdefs.h>
Doug Zongker512536a2010-02-17 16:11:44 -080026#include <sys/stat.h>
Doug Zongker512536a2010-02-17 16:11:44 -080027#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028
Tianjie Xua897f952017-05-17 22:41:55 -070029#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070030#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080031#include <vector>
32
Tianjie Xua897f952017-05-17 22:41:55 -070033#include <android-base/logging.h>
34#include <android-base/memory.h>
Tao Bao97555da2016-12-15 10:15:06 -080035#include <applypatch/applypatch.h>
36#include <applypatch/imgdiff.h>
37#include <openssl/sha.h>
38#include <zlib.h>
39
Tao Bao38d78d12017-10-09 11:03:38 -070040#include "edify/expr.h"
Tianjie Xu9e1ccd42018-04-25 17:19:20 -070041#include "otautil/print_sha1.h"
Tao Bao38d78d12017-10-09 11:03:38 -070042
Tianjie Xu12b90552017-03-07 14:44:14 -080043static inline int64_t Read8(const void *address) {
44 return android::base::get_unaligned<int64_t>(address);
45}
46
47static inline int32_t Read4(const void *address) {
48 return android::base::get_unaligned<int32_t>(address);
49}
Doug Zongker512536a2010-02-17 16:11:44 -080050
Tianjie Xua897f952017-05-17 22:41:55 -070051// This function is a wrapper of ApplyBSDiffPatch(). It has a custom sink function to deflate the
52// patched data and stream the deflated data to output.
53static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len,
Tao Bao1e0941f2017-11-10 11:49:53 -080054 const Value& patch, size_t patch_offset,
Tao Bao8b0b0f12018-04-19 21:02:13 -070055 const char* deflate_header, SinkFn sink) {
Tianjie Xua897f952017-05-17 22:41:55 -070056 size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32));
57 int level = Read4(deflate_header + 40);
58 int method = Read4(deflate_header + 44);
59 int window_bits = Read4(deflate_header + 48);
60 int mem_level = Read4(deflate_header + 52);
61 int strategy = Read4(deflate_header + 56);
62
Tao Baofdec1032017-10-24 12:25:41 -070063 z_stream strm;
64 strm.zalloc = Z_NULL;
65 strm.zfree = Z_NULL;
66 strm.opaque = Z_NULL;
67 strm.avail_in = 0;
68 strm.next_in = nullptr;
69 int ret = deflateInit2(&strm, level, method, window_bits, mem_level, strategy);
Tianjie Xua897f952017-05-17 22:41:55 -070070 if (ret != Z_OK) {
71 LOG(ERROR) << "Failed to init uncompressed data deflation: " << ret;
72 return false;
73 }
74
75 // Define a custom sink wrapper that feeds to bspatch. It deflates the available patch data on
76 // the fly and outputs the compressed data to the given sink.
77 size_t actual_target_length = 0;
78 size_t total_written = 0;
79 static constexpr size_t buffer_size = 32768;
Tianjie Xu9e1ccd42018-04-25 17:19:20 -070080 SHA_CTX sha_ctx;
81 SHA1_Init(&sha_ctx);
Tao Baofdec1032017-10-24 12:25:41 -070082 auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written,
Tianjie Xu9e1ccd42018-04-25 17:19:20 -070083 &ret, &sink, &sha_ctx](const uint8_t* data, size_t len) -> size_t {
Tianjie Xua897f952017-05-17 22:41:55 -070084 // The input patch length for an update never exceeds INT_MAX.
Tao Baofdec1032017-10-24 12:25:41 -070085 strm.avail_in = len;
86 strm.next_in = data;
Tianjie Xua897f952017-05-17 22:41:55 -070087 do {
88 std::vector<uint8_t> buffer(buffer_size);
Tao Baofdec1032017-10-24 12:25:41 -070089 strm.avail_out = buffer_size;
90 strm.next_out = buffer.data();
Tianjie Xua897f952017-05-17 22:41:55 -070091 if (actual_target_length + len < expected_target_length) {
Tao Baofdec1032017-10-24 12:25:41 -070092 ret = deflate(&strm, Z_NO_FLUSH);
Tianjie Xua897f952017-05-17 22:41:55 -070093 } else {
Tao Baofdec1032017-10-24 12:25:41 -070094 ret = deflate(&strm, Z_FINISH);
Tianjie Xua897f952017-05-17 22:41:55 -070095 }
96 if (ret != Z_OK && ret != Z_STREAM_END) {
97 LOG(ERROR) << "Failed to deflate stream: " << ret;
98 // zero length indicates an error in the sink function of bspatch().
99 return 0;
100 }
101
Tao Baofdec1032017-10-24 12:25:41 -0700102 size_t have = buffer_size - strm.avail_out;
Tianjie Xua897f952017-05-17 22:41:55 -0700103 total_written += have;
Tianjie Xu9e1ccd42018-04-25 17:19:20 -0700104
105 // TODO(b/67849209) Remove after debugging the unit test flakiness.
106 if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG &&
107 have != 0) {
108 SHA1_Update(&sha_ctx, data, len - strm.avail_in);
109 SHA_CTX temp_ctx;
110 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
111 uint8_t digest_so_far[SHA_DIGEST_LENGTH];
112 SHA1_Final(digest_so_far, &temp_ctx);
113 LOG(DEBUG) << "Processed " << actual_target_length + len - strm.avail_in
114 << " bytes input data in the sink function, sha1 so far: "
115 << short_sha1(digest_so_far);
116 }
117
Tianjie Xua897f952017-05-17 22:41:55 -0700118 if (sink(buffer.data(), have) != have) {
119 LOG(ERROR) << "Failed to write " << have << " compressed bytes to output.";
120 return 0;
121 }
Tao Baofdec1032017-10-24 12:25:41 -0700122 } while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
Tianjie Xua897f952017-05-17 22:41:55 -0700123
124 actual_target_length += len;
125 return len;
126 };
127
Tao Bao8b0b0f12018-04-19 21:02:13 -0700128 int bspatch_result = ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink);
Tao Baofdec1032017-10-24 12:25:41 -0700129 deflateEnd(&strm);
130
Tianjie Xu9e1ccd42018-04-25 17:19:20 -0700131 if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
132 uint8_t digest[SHA_DIGEST_LENGTH];
133 SHA1_Final(digest, &sha_ctx);
134 LOG(DEBUG) << "sha1 of " << actual_target_length << " bytes input data: " << short_sha1(digest);
135 }
Tao Baofdec1032017-10-24 12:25:41 -0700136 if (bspatch_result != 0) {
Tianjie Xua897f952017-05-17 22:41:55 -0700137 return false;
138 }
139
140 if (ret != Z_STREAM_END) {
141 LOG(ERROR) << "ret is expected to be Z_STREAM_END, but it's " << ret;
142 return false;
143 }
144
145 if (expected_target_length != actual_target_length) {
146 LOG(ERROR) << "target length is expected to be " << expected_target_length << ", but it's "
147 << actual_target_length;
148 return false;
149 }
150 LOG(DEBUG) << "bspatch writes " << total_written << " bytes in total to streaming output.";
151
152 return true;
153}
154
Tao Baof7eb7602017-03-27 15:12:48 -0700155int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data,
Tao Baoc0e1c462017-02-01 10:20:10 -0800156 size_t patch_size, SinkFn sink) {
Tao Bao97555da2016-12-15 10:15:06 -0800157 Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(patch_data), patch_size));
Tao Bao8b0b0f12018-04-19 21:02:13 -0700158 return ApplyImagePatch(old_data, old_size, patch, sink, nullptr);
Sen Jiang0cce9cd2016-01-22 20:49:07 +0800159}
160
Tao Bao1e0941f2017-11-10 11:49:53 -0800161int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700162 const Value* bonus_data) {
Tao Bao1e0941f2017-11-10 11:49:53 -0800163 if (patch.data.size() < 12) {
Tao Bao97555da2016-12-15 10:15:06 -0800164 printf("patch too short to contain header\n");
165 return -1;
166 }
167
Tao Bao1e0941f2017-11-10 11:49:53 -0800168 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. (IMGDIFF1, which is no longer
169 // supported, used CHUNK_NORMAL and CHUNK_GZIP.)
170 const char* const patch_header = patch.data.data();
171 if (memcmp(patch_header, "IMGDIFF2", 8) != 0) {
Tao Bao97555da2016-12-15 10:15:06 -0800172 printf("corrupt patch file header (magic number)\n");
173 return -1;
174 }
175
Tao Bao1e0941f2017-11-10 11:49:53 -0800176 int num_chunks = Read4(patch_header + 8);
177 size_t pos = 12;
Tao Bao97555da2016-12-15 10:15:06 -0800178 for (int i = 0; i < num_chunks; ++i) {
179 // each chunk's header record starts with 4 bytes.
Tao Bao1e0941f2017-11-10 11:49:53 -0800180 if (pos + 4 > patch.data.size()) {
Tao Bao97555da2016-12-15 10:15:06 -0800181 printf("failed to read chunk %d record\n", i);
182 return -1;
183 }
Tao Bao1e0941f2017-11-10 11:49:53 -0800184 int type = Read4(patch_header + pos);
Tao Bao97555da2016-12-15 10:15:06 -0800185 pos += 4;
186
187 if (type == CHUNK_NORMAL) {
Tao Bao1e0941f2017-11-10 11:49:53 -0800188 const char* normal_header = patch_header + pos;
Tao Bao97555da2016-12-15 10:15:06 -0800189 pos += 24;
Tao Bao1e0941f2017-11-10 11:49:53 -0800190 if (pos > patch.data.size()) {
Tao Bao97555da2016-12-15 10:15:06 -0800191 printf("failed to read chunk %d normal header data\n", i);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800192 return -1;
Tao Bao97555da2016-12-15 10:15:06 -0800193 }
Doug Zongker512536a2010-02-17 16:11:44 -0800194
Tianjie Xu12b90552017-03-07 14:44:14 -0800195 size_t src_start = static_cast<size_t>(Read8(normal_header));
196 size_t src_len = static_cast<size_t>(Read8(normal_header + 8));
197 size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16));
Tao Bao97555da2016-12-15 10:15:06 -0800198
Tao Baof7eb7602017-03-27 15:12:48 -0700199 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800200 printf("source data too short\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800201 return -1;
Tao Bao97555da2016-12-15 10:15:06 -0800202 }
Tao Bao8b0b0f12018-04-19 21:02:13 -0700203 if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink) != 0) {
Jinguang Dong391bb7b2017-04-26 10:40:45 +0800204 printf("Failed to apply bsdiff patch.\n");
205 return -1;
206 }
Tianjie Xuffed57a2018-04-23 17:51:45 -0700207
208 LOG(DEBUG) << "Processed chunk type normal";
Tao Bao97555da2016-12-15 10:15:06 -0800209 } else if (type == CHUNK_RAW) {
Tao Bao1e0941f2017-11-10 11:49:53 -0800210 const char* raw_header = patch_header + pos;
Tao Bao97555da2016-12-15 10:15:06 -0800211 pos += 4;
Tao Bao1e0941f2017-11-10 11:49:53 -0800212 if (pos > patch.data.size()) {
Tao Bao97555da2016-12-15 10:15:06 -0800213 printf("failed to read chunk %d raw header data\n", i);
214 return -1;
215 }
Doug Zongker512536a2010-02-17 16:11:44 -0800216
Tao Baof7eb7602017-03-27 15:12:48 -0700217 size_t data_len = static_cast<size_t>(Read4(raw_header));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800218
Tao Bao1e0941f2017-11-10 11:49:53 -0800219 if (pos + data_len > patch.data.size()) {
Tao Bao97555da2016-12-15 10:15:06 -0800220 printf("failed to read chunk %d raw data\n", i);
221 return -1;
222 }
Tao Bao1e0941f2017-11-10 11:49:53 -0800223 if (sink(reinterpret_cast<const unsigned char*>(patch_header + pos), data_len) != data_len) {
Tao Bao97555da2016-12-15 10:15:06 -0800224 printf("failed to write chunk %d raw data\n", i);
225 return -1;
226 }
227 pos += data_len;
Tianjie Xuffed57a2018-04-23 17:51:45 -0700228
229 LOG(DEBUG) << "Processed chunk type raw";
Tao Bao97555da2016-12-15 10:15:06 -0800230 } else if (type == CHUNK_DEFLATE) {
231 // deflate chunks have an additional 60 bytes in their chunk header.
Tao Bao1e0941f2017-11-10 11:49:53 -0800232 const char* deflate_header = patch_header + pos;
Tao Bao97555da2016-12-15 10:15:06 -0800233 pos += 60;
Tao Bao1e0941f2017-11-10 11:49:53 -0800234 if (pos > patch.data.size()) {
Tao Bao97555da2016-12-15 10:15:06 -0800235 printf("failed to read chunk %d deflate header data\n", i);
236 return -1;
237 }
238
Tianjie Xu12b90552017-03-07 14:44:14 -0800239 size_t src_start = static_cast<size_t>(Read8(deflate_header));
240 size_t src_len = static_cast<size_t>(Read8(deflate_header + 8));
241 size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16));
242 size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24));
Tao Bao97555da2016-12-15 10:15:06 -0800243
Tao Baof7eb7602017-03-27 15:12:48 -0700244 if (src_start + src_len > old_size) {
Tao Bao97555da2016-12-15 10:15:06 -0800245 printf("source data too short\n");
246 return -1;
247 }
248
249 // Decompress the source data; the chunk header tells us exactly
250 // how big we expect it to be when decompressed.
251
252 // Note: expanded_len will include the bonus data size if
253 // the patch was constructed with bonus data. The
254 // deflation will come up 'bonus_size' bytes short; these
255 // must be appended from the bonus_data value.
256 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0;
257
258 std::vector<unsigned char> expanded_source(expanded_len);
259
260 // inflate() doesn't like strm.next_out being a nullptr even with
261 // avail_out being zero (Z_STREAM_ERROR).
262 if (expanded_len != 0) {
263 z_stream strm;
264 strm.zalloc = Z_NULL;
265 strm.zfree = Z_NULL;
266 strm.opaque = Z_NULL;
267 strm.avail_in = src_len;
Tao Bao087bc0c2017-01-19 10:46:39 -0800268 strm.next_in = old_data + src_start;
Tao Bao97555da2016-12-15 10:15:06 -0800269 strm.avail_out = expanded_len;
270 strm.next_out = expanded_source.data();
271
272 int ret = inflateInit2(&strm, -15);
273 if (ret != Z_OK) {
274 printf("failed to init source inflation: %d\n", ret);
275 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800276 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800277
Tao Bao97555da2016-12-15 10:15:06 -0800278 // Because we've provided enough room to accommodate the output
279 // data, we expect one call to inflate() to suffice.
280 ret = inflate(&strm, Z_SYNC_FLUSH);
281 if (ret != Z_STREAM_END) {
282 printf("source inflation returned %d\n", ret);
283 return -1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800284 }
Tao Bao97555da2016-12-15 10:15:06 -0800285 // We should have filled the output buffer exactly, except
286 // for the bonus_size.
287 if (strm.avail_out != bonus_size) {
288 printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size);
289 return -1;
290 }
291 inflateEnd(&strm);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800292
Tao Bao97555da2016-12-15 10:15:06 -0800293 if (bonus_size) {
294 memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0],
295 bonus_size);
296 }
297 }
298
Tianjie Xua897f952017-05-17 22:41:55 -0700299 if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700300 patch_offset, deflate_header, sink)) {
Tianjie Xua897f952017-05-17 22:41:55 -0700301 LOG(ERROR) << "Fail to apply streaming bspatch.";
Tao Bao97555da2016-12-15 10:15:06 -0800302 return -1;
303 }
304
Tianjie Xuffed57a2018-04-23 17:51:45 -0700305 LOG(DEBUG) << "Processed chunk type deflate";
Tao Bao97555da2016-12-15 10:15:06 -0800306 } else {
307 printf("patch chunk %d is unknown type %d\n", i, type);
308 return -1;
309 }
310 }
311
312 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800313}