Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _APPLYPATCH_H |
| 18 | #define _APPLYPATCH_H |
| 19 | |
Sen Jiang | 696692a | 2016-02-02 16:01:23 +0800 | [diff] [blame] | 20 | #include <stdint.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 21 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 22 | #include <functional> |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 23 | #include <memory> |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 24 | #include <string> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 27 | #include <openssl/sha.h> |
| 28 | |
Tao Bao | 38d78d1 | 2017-10-09 11:03:38 -0700 | [diff] [blame] | 29 | // Forward declaration to avoid including "edify/expr.h" in the header. |
| 30 | struct Value; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 31 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 32 | struct FileContents { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 33 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 34 | std::vector<unsigned char> data; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 35 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 36 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 37 | using SinkFn = std::function<size_t(const unsigned char*, size_t)>; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 38 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 39 | // applypatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 40 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 41 | int ShowLicenses(); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 42 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 43 | // Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the |
| 44 | // digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 45 | int ParseSha1(const std::string& str, uint8_t* digest); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 46 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 47 | // Applies binary patches to eMMC target files in a way that is safe (the original file is not |
| 48 | // touched until we have the desired replacement for it) and idempotent (it's okay to run this |
| 49 | // program multiple times). |
| 50 | // |
| 51 | // - If the SHA-1 hash of 'target_filename' is 'target_sha1_string', does nothing and returns |
| 52 | // successfully. |
| 53 | // |
| 54 | // - Otherwise, if the SHA-1 hash of 'source_filename' is one of the entries in 'patch_sha1s', the |
| 55 | // corresponding patch from 'patch_data' (which must be a VAL_BLOB) is applied to produce a new |
| 56 | // file (the type of patch is automatically detected from the blob data). If that new file has |
| 57 | // SHA-1 hash 'target_sha1_str', moves it to replace 'target_filename', and exits successfully. |
| 58 | // Note that if 'source_filename' and 'target_filename' are not the same, 'source_filename' is |
| 59 | // NOT deleted on success. 'target_filename' may be the string "-" to mean |
| 60 | // "the same as 'source_filename'". |
| 61 | // |
| 62 | // - Otherwise, or if any error is encountered, exits with non-zero status. |
| 63 | // |
| 64 | // 'source_filename' must refer to an eMMC partition to read the source data. See the comments for |
| 65 | // the LoadPartitionContents() function for the format of such a filename. 'target_size' has become |
| 66 | // obsolete since we have dropped the support for patching non-eMMC targets (eMMC targets have the |
| 67 | // size embedded in the filename). |
| 68 | int applypatch(const char* source_filename, const char* target_filename, |
| 69 | const char* target_sha1_str, size_t target_size, |
| 70 | const std::vector<std::string>& patch_sha1s, |
| 71 | const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data); |
| 72 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 73 | // Returns 0 if the contents of the eMMC target or the cached file match any of the given SHA-1's. |
| 74 | // Returns nonzero otherwise. 'filename' must refer to an eMMC partition target. It would only use |
| 75 | // 'sha1s' to find a match on /cache if the hashes embedded in the filename fail to match. |
| 76 | int applypatch_check(const std::string& filename, const std::vector<std::string>& sha1s); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 77 | |
| 78 | // Flashes a given image to the target partition. It verifies the target cheksum first, and will |
| 79 | // return if target already has the desired hash. Otherwise it checks the checksum of the given |
| 80 | // source image before flashing, and verifies the target partition afterwards. The function is |
| 81 | // idempotent. Returns zero on success. |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 82 | int applypatch_flash(const char* source_filename, const char* target_filename, |
| 83 | const char* target_sha1_str, size_t target_size); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 84 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 85 | // Reads a file into memory; stores the file contents and associated metadata in *file. Returns 0 |
| 86 | // on success, or -1 on error. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 87 | int LoadFileContents(const std::string& filename, FileContents* file); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 88 | |
| 89 | // Saves the given FileContents object to the given filename. Returns 0 on success, or -1 on error. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 90 | int SaveFileContents(const std::string& filename, const FileContents* file); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 91 | |
Tao Bao | c8e7934 | 2016-12-28 10:11:22 -0800 | [diff] [blame] | 92 | // bspatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 93 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 94 | void ShowBSDiffLicense(); |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 95 | |
| 96 | // Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 97 | // data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns |
| 98 | // 0 on success. |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 99 | int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch, |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 100 | size_t patch_offset, SinkFn sink); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 101 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 102 | // imgpatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 103 | |
| 104 | // Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 105 | // the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on |
| 106 | // success. |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 107 | int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink, |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 108 | const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 109 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 110 | // freecache.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 111 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame^] | 112 | // Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately |
| 113 | // if so. Otherwise, it will try to free some space by removing older logs, checks again and |
| 114 | // returns the checking result. |
| 115 | bool CheckAndFreeSpaceOnCache(size_t bytes); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 116 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 117 | // Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the |
| 118 | // partition. |space_checker| should return the size of the free space, or -1 on error. |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 119 | bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 120 | const std::function<int64_t(const std::string&)>& space_checker); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 121 | #endif |