David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person |
| 5 | * obtaining a copy of this software and associated documentation |
| 6 | * files (the "Software"), to deal in the Software without |
| 7 | * restriction, including without limitation the rights to use, copy, |
| 8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #ifndef FAKE_AVB_OPS_H_ |
| 26 | #define FAKE_AVB_OPS_H_ |
| 27 | |
| 28 | #include <base/files/file_util.h> |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 29 | #include <map> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 30 | #include <string> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 31 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 32 | #include <libavb_ab/libavb_ab.h> |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 33 | #include <libavb_atx/libavb_atx.h> |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 34 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 35 | namespace avb { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 36 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 37 | // A delegate interface for ops callbacks. This allows tests to override default |
| 38 | // fake implementations. |
| 39 | class FakeAvbOpsDelegate { |
| 40 | public: |
| 41 | virtual ~FakeAvbOpsDelegate() {} |
| 42 | virtual AvbIOResult read_from_partition(const char* partition, |
| 43 | int64_t offset, |
| 44 | size_t num_bytes, |
| 45 | void* buffer, |
| 46 | size_t* out_num_read) = 0; |
| 47 | |
| 48 | virtual AvbIOResult write_to_partition(const char* partition, |
| 49 | int64_t offset, |
| 50 | size_t num_bytes, |
| 51 | const void* buffer) = 0; |
| 52 | |
| 53 | virtual AvbIOResult validate_vbmeta_public_key( |
| 54 | AvbOps* ops, |
| 55 | const uint8_t* public_key_data, |
| 56 | size_t public_key_length, |
| 57 | const uint8_t* public_key_metadata, |
| 58 | size_t public_key_metadata_length, |
| 59 | bool* out_key_is_trusted) = 0; |
| 60 | |
| 61 | virtual AvbIOResult read_rollback_index(AvbOps* ops, |
| 62 | size_t rollback_index_slot, |
| 63 | uint64_t* out_rollback_index) = 0; |
| 64 | |
| 65 | virtual AvbIOResult write_rollback_index(AvbOps* ops, |
| 66 | size_t rollback_index_slot, |
| 67 | uint64_t rollback_index) = 0; |
| 68 | |
| 69 | virtual AvbIOResult read_is_device_unlocked(AvbOps* ops, |
| 70 | bool* out_is_device_unlocked) = 0; |
| 71 | |
| 72 | virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops, |
| 73 | const char* partition, |
| 74 | char* guid_buf, |
| 75 | size_t guid_buf_size) = 0; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 76 | |
| 77 | virtual AvbIOResult read_permanent_attributes( |
| 78 | AvbAtxPermanentAttributes* attributes) = 0; |
| 79 | |
| 80 | virtual AvbIOResult read_permanent_attributes_hash( |
| 81 | uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0; |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Provides fake implementations of AVB ops. All instances of this class must be |
| 85 | // created on the same thread. |
| 86 | class FakeAvbOps : public FakeAvbOpsDelegate { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 87 | public: |
| 88 | FakeAvbOps(); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 89 | virtual ~FakeAvbOps(); |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 90 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 91 | static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) { |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame] | 92 | return reinterpret_cast<FakeAvbOps*>(ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 93 | } |
David Zeuthen | 574ed99 | 2017-01-05 15:43:11 -0500 | [diff] [blame] | 94 | static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) { |
| 95 | return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 96 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 97 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 98 | AvbOps* avb_ops() { |
| 99 | return &avb_ops_; |
| 100 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 101 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 102 | AvbABOps* avb_ab_ops() { |
| 103 | return &avb_ab_ops_; |
| 104 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 105 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 106 | AvbAtxOps* avb_atx_ops() { |
| 107 | return &avb_atx_ops_; |
| 108 | } |
| 109 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 110 | FakeAvbOpsDelegate* delegate() { |
| 111 | return delegate_; |
| 112 | } |
| 113 | |
| 114 | // Does not take ownership of |delegate|. |
| 115 | void set_delegate(FakeAvbOpsDelegate* delegate) { |
| 116 | delegate_ = delegate; |
| 117 | } |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 118 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 119 | void set_partition_dir(const base::FilePath& partition_dir) { |
| 120 | partition_dir_ = partition_dir; |
| 121 | } |
| 122 | |
| 123 | void set_expected_public_key(const std::string& expected_public_key) { |
| 124 | expected_public_key_ = expected_public_key; |
| 125 | } |
| 126 | |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 127 | void set_expected_public_key_metadata( |
| 128 | const std::string& expected_public_key_metadata) { |
| 129 | expected_public_key_metadata_ = expected_public_key_metadata; |
| 130 | } |
| 131 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 132 | void set_stored_rollback_indexes( |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 133 | const std::map<size_t, uint64_t>& stored_rollback_indexes) { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 134 | stored_rollback_indexes_ = stored_rollback_indexes; |
| 135 | } |
| 136 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 137 | std::map<size_t, uint64_t> get_stored_rollback_indexes() { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 138 | return stored_rollback_indexes_; |
| 139 | } |
| 140 | |
| 141 | void set_stored_is_device_unlocked(bool stored_is_device_unlocked) { |
| 142 | stored_is_device_unlocked_ = stored_is_device_unlocked; |
| 143 | } |
| 144 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 145 | void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) { |
| 146 | permanent_attributes_ = attributes; |
| 147 | } |
| 148 | |
| 149 | void set_permanent_attributes_hash(const std::string& hash) { |
| 150 | permanent_attributes_hash_ = hash; |
| 151 | } |
| 152 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 153 | // FakeAvbOpsDelegate methods. |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 154 | AvbIOResult read_from_partition(const char* partition, |
| 155 | int64_t offset, |
| 156 | size_t num_bytes, |
| 157 | void* buffer, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 158 | size_t* out_num_read) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 159 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 160 | AvbIOResult write_to_partition(const char* partition, |
| 161 | int64_t offset, |
| 162 | size_t num_bytes, |
| 163 | const void* buffer) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 164 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 165 | AvbIOResult validate_vbmeta_public_key(AvbOps* ops, |
| 166 | const uint8_t* public_key_data, |
| 167 | size_t public_key_length, |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 168 | const uint8_t* public_key_metadata, |
| 169 | size_t public_key_metadata_length, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 170 | bool* out_key_is_trusted) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 171 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 172 | AvbIOResult read_rollback_index(AvbOps* ops, |
| 173 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 174 | uint64_t* out_rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 175 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 176 | AvbIOResult write_rollback_index(AvbOps* ops, |
| 177 | size_t rollback_index_location, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 178 | uint64_t rollback_index) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 179 | |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 180 | AvbIOResult read_is_device_unlocked(AvbOps* ops, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 181 | bool* out_is_device_unlocked) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 182 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 183 | AvbIOResult get_unique_guid_for_partition(AvbOps* ops, |
| 184 | const char* partition, |
David Zeuthen | 507752b | 2016-09-29 16:31:18 -0400 | [diff] [blame] | 185 | char* guid_buf, |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 186 | size_t guid_buf_size) override; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 187 | |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 188 | AvbIOResult read_permanent_attributes( |
| 189 | AvbAtxPermanentAttributes* attributes) override; |
| 190 | |
| 191 | AvbIOResult read_permanent_attributes_hash( |
| 192 | uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override; |
| 193 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 194 | private: |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 195 | AvbOps avb_ops_; |
| 196 | AvbABOps avb_ab_ops_; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 197 | AvbAtxOps avb_atx_ops_; |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 198 | |
| 199 | FakeAvbOpsDelegate* delegate_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 200 | |
| 201 | base::FilePath partition_dir_; |
| 202 | |
| 203 | std::string expected_public_key_; |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 204 | std::string expected_public_key_metadata_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 205 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 206 | std::map<size_t, uint64_t> stored_rollback_indexes_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 207 | |
| 208 | bool stored_is_device_unlocked_; |
Darren Krahn | 147b08d | 2016-12-20 16:38:29 -0800 | [diff] [blame^] | 209 | |
| 210 | AvbAtxPermanentAttributes permanent_attributes_; |
| 211 | std::string permanent_attributes_hash_; |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 212 | }; |
| 213 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 214 | } // namespace avb |
| 215 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 216 | #endif /* FAKE_AVB_OPS_H_ */ |