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> |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 24 | #include <ostream> |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 25 | #include <string> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 28 | #include <openssl/sha.h> |
| 29 | |
Tao Bao | 38d78d1 | 2017-10-09 11:03:38 -0700 | [diff] [blame] | 30 | // Forward declaration to avoid including "edify/expr.h" in the header. |
| 31 | struct Value; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 32 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 33 | struct FileContents { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 34 | uint8_t sha1[SHA_DIGEST_LENGTH]; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 35 | std::vector<unsigned char> data; |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 36 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 37 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 38 | using SinkFn = std::function<size_t(const unsigned char*, size_t)>; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 39 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 40 | // applypatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 41 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 42 | int ShowLicenses(); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 43 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 44 | // Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the |
| 45 | // 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] | 46 | int ParseSha1(const std::string& str, uint8_t* digest); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 47 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 48 | struct Partition { |
| 49 | Partition() = default; |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 50 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 51 | Partition(const std::string& name, size_t size, const std::string& hash) |
| 52 | : name(name), size(size), hash(hash) {} |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 53 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 54 | // Parses and returns the given string into a Partition object. The input string is of the form |
| 55 | // "EMMC:<device>:<size>:<hash>". Returns the parsed Partition, or an empty object on error. |
| 56 | static Partition Parse(const std::string& partition, std::string* err); |
| 57 | |
| 58 | std::string ToString() const; |
| 59 | |
| 60 | // Returns whether the current Partition object is valid. |
| 61 | explicit operator bool() const { |
| 62 | return !name.empty(); |
| 63 | } |
| 64 | |
| 65 | std::string name; |
| 66 | size_t size; |
| 67 | std::string hash; |
| 68 | }; |
| 69 | |
| 70 | std::ostream& operator<<(std::ostream& os, const Partition& partition); |
| 71 | |
| 72 | // Applies the given 'patch' to the 'source' Partition, verifies then writes the patching result to |
| 73 | // the 'target' Partition. While patching, it will backup the data on the source partition to |
| 74 | // /cache, so that the patching could be resumed on interruption even if both of the source and |
| 75 | // target partitions refer to the same device. The function is idempotent if called multiple times. |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 76 | // 'bonus' can be provided if the patch was generated with a bonus output, or nullptr. |
| 77 | // 'backup_source' indicates whether the source partition should be backed up prior to the update |
| 78 | // (e.g. when doing in-place update). Returns the patching result. |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 79 | bool PatchPartition(const Partition& target, const Partition& source, const Value& patch, |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 80 | const Value* bonus, bool backup_source); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 81 | |
| 82 | // Returns whether the contents of the eMMC target or the cached file match the embedded hash. |
| 83 | // It will look for the backup on /cache if the given partition doesn't match the checksum. |
| 84 | bool PatchPartitionCheck(const Partition& target, const Partition& source); |
| 85 | |
| 86 | // Checks whether the contents of the given partition has the desired hash. It will NOT look for |
| 87 | // the backup on /cache if the given partition doesn't have the expected checksum. |
| 88 | bool CheckPartition(const Partition& target); |
| 89 | |
| 90 | // Flashes a given image in 'source_filename' to the eMMC target partition. It verifies the target |
| 91 | // checksum first, and will return if target already has the desired hash. Otherwise it checks the |
| 92 | // checksum of the given source image, flashes, and verifies the target partition afterwards. The |
| 93 | // function is idempotent. Returns the flashing result. |
| 94 | bool FlashPartition(const Partition& target, const std::string& source_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 95 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 96 | // Reads a file into memory; stores the file contents and associated metadata in *file. |
| 97 | bool LoadFileContents(const std::string& filename, FileContents* file); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 98 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 99 | // Saves the given FileContents object to the given filename. |
| 100 | bool SaveFileContents(const std::string& filename, const FileContents* file); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 101 | |
Tao Bao | c8e7934 | 2016-12-28 10:11:22 -0800 | [diff] [blame] | 102 | // bspatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 103 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 104 | void ShowBSDiffLicense(); |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 105 | |
| 106 | // 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] | 107 | // data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns |
| 108 | // 0 on success. |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 109 | 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] | 110 | size_t patch_offset, SinkFn sink); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 111 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 112 | // imgpatch.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 113 | |
| 114 | // 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] | 115 | // the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on |
| 116 | // success. |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 117 | 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] | 118 | const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 119 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 120 | // freecache.cpp |
Tao Bao | 1e0941f | 2017-11-10 11:49:53 -0800 | [diff] [blame] | 121 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 122 | // Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately |
| 123 | // if so. Otherwise, it will try to free some space by removing older logs, checks again and |
| 124 | // returns the checking result. |
| 125 | bool CheckAndFreeSpaceOnCache(size_t bytes); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 126 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 127 | // Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the |
| 128 | // 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] | 129 | bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 130 | const std::function<int64_t(const std::string&)>& space_checker); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 131 | #endif |