blob: 53775d442a5f2ee46fb7a6a99800653520823872 [file] [log] [blame]
David Zeuthen8b6973b2016-09-20 12:39:49 -04001/*
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 Krahn72d57902016-12-12 18:34:08 -080029#include <map>
David Zeuthen01ca9962017-05-23 15:28:17 -040030#include <set>
David Zeuthen8b6973b2016-09-20 12:39:49 -040031#include <string>
David Zeuthen8b6973b2016-09-20 12:39:49 -040032
David Zeuthenbaf59e22016-11-14 15:39:43 -050033#include <libavb_ab/libavb_ab.h>
Darren Krahn147b08d2016-12-20 16:38:29 -080034#include <libavb_atx/libavb_atx.h>
David Zeuthen8b6973b2016-09-20 12:39:49 -040035
Darren Krahn72d57902016-12-12 18:34:08 -080036namespace avb {
David Zeuthen8b6973b2016-09-20 12:39:49 -040037
Darren Krahn72d57902016-12-12 18:34:08 -080038// A delegate interface for ops callbacks. This allows tests to override default
39// fake implementations.
40class FakeAvbOpsDelegate {
41 public:
42 virtual ~FakeAvbOpsDelegate() {}
43 virtual AvbIOResult read_from_partition(const char* partition,
44 int64_t offset,
45 size_t num_bytes,
46 void* buffer,
47 size_t* out_num_read) = 0;
48
49 virtual AvbIOResult write_to_partition(const char* partition,
50 int64_t offset,
51 size_t num_bytes,
52 const void* buffer) = 0;
53
54 virtual AvbIOResult validate_vbmeta_public_key(
55 AvbOps* ops,
56 const uint8_t* public_key_data,
57 size_t public_key_length,
58 const uint8_t* public_key_metadata,
59 size_t public_key_metadata_length,
60 bool* out_key_is_trusted) = 0;
61
62 virtual AvbIOResult read_rollback_index(AvbOps* ops,
63 size_t rollback_index_slot,
64 uint64_t* out_rollback_index) = 0;
65
66 virtual AvbIOResult write_rollback_index(AvbOps* ops,
67 size_t rollback_index_slot,
68 uint64_t rollback_index) = 0;
69
70 virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
71 bool* out_is_device_unlocked) = 0;
72
73 virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
74 const char* partition,
75 char* guid_buf,
76 size_t guid_buf_size) = 0;
Darren Krahn147b08d2016-12-20 16:38:29 -080077
David Zeuthen27a291f2017-04-27 18:18:33 -040078 virtual AvbIOResult get_size_of_partition(AvbOps* ops,
79 const char* partition,
80 uint64_t* out_size) = 0;
81
Darren Krahn147b08d2016-12-20 16:38:29 -080082 virtual AvbIOResult read_permanent_attributes(
83 AvbAtxPermanentAttributes* attributes) = 0;
84
85 virtual AvbIOResult read_permanent_attributes_hash(
86 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0;
Darren Krahn72d57902016-12-12 18:34:08 -080087};
88
89// Provides fake implementations of AVB ops. All instances of this class must be
90// created on the same thread.
91class FakeAvbOps : public FakeAvbOpsDelegate {
David Zeuthen8b6973b2016-09-20 12:39:49 -040092 public:
93 FakeAvbOps();
Darren Krahn72d57902016-12-12 18:34:08 -080094 virtual ~FakeAvbOps();
David Zeuthen8b6973b2016-09-20 12:39:49 -040095
Darren Krahn72d57902016-12-12 18:34:08 -080096 static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
David Zeuthen574ed992017-01-05 15:43:11 -050097 return reinterpret_cast<FakeAvbOps*>(ops->user_data);
Darren Krahn72d57902016-12-12 18:34:08 -080098 }
David Zeuthen574ed992017-01-05 15:43:11 -050099 static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
100 return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
Darren Krahn72d57902016-12-12 18:34:08 -0800101 }
David Zeuthen8b6973b2016-09-20 12:39:49 -0400102
David Zeuthen4b6a6342017-01-03 15:19:56 -0500103 AvbOps* avb_ops() {
104 return &avb_ops_;
105 }
Darren Krahn72d57902016-12-12 18:34:08 -0800106
David Zeuthen4b6a6342017-01-03 15:19:56 -0500107 AvbABOps* avb_ab_ops() {
108 return &avb_ab_ops_;
109 }
Darren Krahn72d57902016-12-12 18:34:08 -0800110
Darren Krahn147b08d2016-12-20 16:38:29 -0800111 AvbAtxOps* avb_atx_ops() {
112 return &avb_atx_ops_;
113 }
114
Darren Krahn72d57902016-12-12 18:34:08 -0800115 FakeAvbOpsDelegate* delegate() {
116 return delegate_;
117 }
118
119 // Does not take ownership of |delegate|.
120 void set_delegate(FakeAvbOpsDelegate* delegate) {
121 delegate_ = delegate;
122 }
David Zeuthenbaf59e22016-11-14 15:39:43 -0500123
David Zeuthen8b6973b2016-09-20 12:39:49 -0400124 void set_partition_dir(const base::FilePath& partition_dir) {
125 partition_dir_ = partition_dir;
126 }
127
128 void set_expected_public_key(const std::string& expected_public_key) {
129 expected_public_key_ = expected_public_key;
130 }
131
David Zeuthen18666ab2016-11-15 11:18:05 -0500132 void set_expected_public_key_metadata(
133 const std::string& expected_public_key_metadata) {
134 expected_public_key_metadata_ = expected_public_key_metadata;
135 }
136
David Zeuthen8b6973b2016-09-20 12:39:49 -0400137 void set_stored_rollback_indexes(
Darren Krahn72d57902016-12-12 18:34:08 -0800138 const std::map<size_t, uint64_t>& stored_rollback_indexes) {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400139 stored_rollback_indexes_ = stored_rollback_indexes;
140 }
141
Darren Krahn72d57902016-12-12 18:34:08 -0800142 std::map<size_t, uint64_t> get_stored_rollback_indexes() {
David Zeuthen8b6973b2016-09-20 12:39:49 -0400143 return stored_rollback_indexes_;
144 }
145
146 void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
147 stored_is_device_unlocked_ = stored_is_device_unlocked;
148 }
149
Darren Krahn147b08d2016-12-20 16:38:29 -0800150 void set_permanent_attributes(const AvbAtxPermanentAttributes& attributes) {
151 permanent_attributes_ = attributes;
152 }
153
154 void set_permanent_attributes_hash(const std::string& hash) {
155 permanent_attributes_hash_ = hash;
156 }
157
David Zeuthen01ca9962017-05-23 15:28:17 -0400158 // Gets the partition names that were passed to the
159 // read_from_partition() operation.
160 std::set<std::string> get_partition_names_read_from();
161
Darren Krahn72d57902016-12-12 18:34:08 -0800162 // FakeAvbOpsDelegate methods.
David Zeuthen4b6a6342017-01-03 15:19:56 -0500163 AvbIOResult read_from_partition(const char* partition,
164 int64_t offset,
165 size_t num_bytes,
166 void* buffer,
Darren Krahn72d57902016-12-12 18:34:08 -0800167 size_t* out_num_read) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400168
David Zeuthen4b6a6342017-01-03 15:19:56 -0500169 AvbIOResult write_to_partition(const char* partition,
170 int64_t offset,
171 size_t num_bytes,
172 const void* buffer) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400173
David Zeuthen507752b2016-09-29 16:31:18 -0400174 AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
175 const uint8_t* public_key_data,
176 size_t public_key_length,
David Zeuthen18666ab2016-11-15 11:18:05 -0500177 const uint8_t* public_key_metadata,
178 size_t public_key_metadata_length,
Darren Krahn72d57902016-12-12 18:34:08 -0800179 bool* out_key_is_trusted) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400180
David Zeuthen4b6a6342017-01-03 15:19:56 -0500181 AvbIOResult read_rollback_index(AvbOps* ops,
182 size_t rollback_index_location,
Darren Krahn72d57902016-12-12 18:34:08 -0800183 uint64_t* out_rollback_index) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400184
David Zeuthen4b6a6342017-01-03 15:19:56 -0500185 AvbIOResult write_rollback_index(AvbOps* ops,
186 size_t rollback_index_location,
Darren Krahn72d57902016-12-12 18:34:08 -0800187 uint64_t rollback_index) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400188
David Zeuthen507752b2016-09-29 16:31:18 -0400189 AvbIOResult read_is_device_unlocked(AvbOps* ops,
Darren Krahn72d57902016-12-12 18:34:08 -0800190 bool* out_is_device_unlocked) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400191
David Zeuthen4b6a6342017-01-03 15:19:56 -0500192 AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
193 const char* partition,
David Zeuthen507752b2016-09-29 16:31:18 -0400194 char* guid_buf,
Darren Krahn72d57902016-12-12 18:34:08 -0800195 size_t guid_buf_size) override;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400196
David Zeuthen27a291f2017-04-27 18:18:33 -0400197 AvbIOResult get_size_of_partition(AvbOps* ops,
198 const char* partition,
199 uint64_t* out_size) override;
200
Darren Krahn147b08d2016-12-20 16:38:29 -0800201 AvbIOResult read_permanent_attributes(
202 AvbAtxPermanentAttributes* attributes) override;
203
204 AvbIOResult read_permanent_attributes_hash(
205 uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override;
206
David Zeuthen8b6973b2016-09-20 12:39:49 -0400207 private:
Darren Krahn72d57902016-12-12 18:34:08 -0800208 AvbOps avb_ops_;
209 AvbABOps avb_ab_ops_;
Darren Krahn147b08d2016-12-20 16:38:29 -0800210 AvbAtxOps avb_atx_ops_;
Darren Krahn72d57902016-12-12 18:34:08 -0800211
212 FakeAvbOpsDelegate* delegate_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400213
214 base::FilePath partition_dir_;
215
216 std::string expected_public_key_;
David Zeuthen18666ab2016-11-15 11:18:05 -0500217 std::string expected_public_key_metadata_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400218
Darren Krahn72d57902016-12-12 18:34:08 -0800219 std::map<size_t, uint64_t> stored_rollback_indexes_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400220
221 bool stored_is_device_unlocked_;
Darren Krahn147b08d2016-12-20 16:38:29 -0800222
223 AvbAtxPermanentAttributes permanent_attributes_;
224 std::string permanent_attributes_hash_;
David Zeuthen01ca9962017-05-23 15:28:17 -0400225
226 std::set<std::string> partition_names_read_from_;
David Zeuthen8b6973b2016-09-20 12:39:49 -0400227};
228
Darren Krahn72d57902016-12-12 18:34:08 -0800229} // namespace avb
230
David Zeuthen8b6973b2016-09-20 12:39:49 -0400231#endif /* FAKE_AVB_OPS_H_ */