xref: /aosp_15_r20/external/avb/test/fake_avb_ops.h (revision d289c2ba6de359471b23d594623b906876bc48a0)
1*d289c2baSAndroid Build Coastguard Worker /*
2*d289c2baSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d289c2baSAndroid Build Coastguard Worker  *
4*d289c2baSAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person
5*d289c2baSAndroid Build Coastguard Worker  * obtaining a copy of this software and associated documentation
6*d289c2baSAndroid Build Coastguard Worker  * files (the "Software"), to deal in the Software without
7*d289c2baSAndroid Build Coastguard Worker  * restriction, including without limitation the rights to use, copy,
8*d289c2baSAndroid Build Coastguard Worker  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*d289c2baSAndroid Build Coastguard Worker  * of the Software, and to permit persons to whom the Software is
10*d289c2baSAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
11*d289c2baSAndroid Build Coastguard Worker  *
12*d289c2baSAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*d289c2baSAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*d289c2baSAndroid Build Coastguard Worker  *
15*d289c2baSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*d289c2baSAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*d289c2baSAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*d289c2baSAndroid Build Coastguard Worker  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*d289c2baSAndroid Build Coastguard Worker  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*d289c2baSAndroid Build Coastguard Worker  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*d289c2baSAndroid Build Coastguard Worker  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*d289c2baSAndroid Build Coastguard Worker  * SOFTWARE.
23*d289c2baSAndroid Build Coastguard Worker  */
24*d289c2baSAndroid Build Coastguard Worker 
25*d289c2baSAndroid Build Coastguard Worker #ifndef FAKE_AVB_OPS_H_
26*d289c2baSAndroid Build Coastguard Worker #define FAKE_AVB_OPS_H_
27*d289c2baSAndroid Build Coastguard Worker 
28*d289c2baSAndroid Build Coastguard Worker #include <base/files/file_util.h>
29*d289c2baSAndroid Build Coastguard Worker #include <libavb_ab/libavb_ab.h>
30*d289c2baSAndroid Build Coastguard Worker #include <libavb_cert/libavb_cert.h>
31*d289c2baSAndroid Build Coastguard Worker 
32*d289c2baSAndroid Build Coastguard Worker #include <filesystem>
33*d289c2baSAndroid Build Coastguard Worker #include <map>
34*d289c2baSAndroid Build Coastguard Worker #include <set>
35*d289c2baSAndroid Build Coastguard Worker #include <string>
36*d289c2baSAndroid Build Coastguard Worker 
37*d289c2baSAndroid Build Coastguard Worker namespace avb {
38*d289c2baSAndroid Build Coastguard Worker 
39*d289c2baSAndroid Build Coastguard Worker // A delegate interface for ops callbacks. This allows tests to override default
40*d289c2baSAndroid Build Coastguard Worker // fake implementations. For convenience, test fixtures can inherit
41*d289c2baSAndroid Build Coastguard Worker // FakeAvbOpsDelegateWithDefaults and only override as needed.
42*d289c2baSAndroid Build Coastguard Worker class FakeAvbOpsDelegate {
43*d289c2baSAndroid Build Coastguard Worker  public:
~FakeAvbOpsDelegate()44*d289c2baSAndroid Build Coastguard Worker   virtual ~FakeAvbOpsDelegate() {}
45*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_from_partition(const char* partition,
46*d289c2baSAndroid Build Coastguard Worker                                           int64_t offset,
47*d289c2baSAndroid Build Coastguard Worker                                           size_t num_bytes,
48*d289c2baSAndroid Build Coastguard Worker                                           void* buffer,
49*d289c2baSAndroid Build Coastguard Worker                                           size_t* out_num_read) = 0;
50*d289c2baSAndroid Build Coastguard Worker 
51*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult get_preloaded_partition(
52*d289c2baSAndroid Build Coastguard Worker       const char* partition,
53*d289c2baSAndroid Build Coastguard Worker       size_t num_bytes,
54*d289c2baSAndroid Build Coastguard Worker       uint8_t** out_pointer,
55*d289c2baSAndroid Build Coastguard Worker       size_t* out_num_bytes_preloaded) = 0;
56*d289c2baSAndroid Build Coastguard Worker 
57*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult write_to_partition(const char* partition,
58*d289c2baSAndroid Build Coastguard Worker                                          int64_t offset,
59*d289c2baSAndroid Build Coastguard Worker                                          size_t num_bytes,
60*d289c2baSAndroid Build Coastguard Worker                                          const void* buffer) = 0;
61*d289c2baSAndroid Build Coastguard Worker 
62*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult validate_vbmeta_public_key(
63*d289c2baSAndroid Build Coastguard Worker       AvbOps* ops,
64*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_data,
65*d289c2baSAndroid Build Coastguard Worker       size_t public_key_length,
66*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_metadata,
67*d289c2baSAndroid Build Coastguard Worker       size_t public_key_metadata_length,
68*d289c2baSAndroid Build Coastguard Worker       bool* out_key_is_trusted) = 0;
69*d289c2baSAndroid Build Coastguard Worker 
70*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_rollback_index(AvbOps* ops,
71*d289c2baSAndroid Build Coastguard Worker                                           size_t rollback_index_slot,
72*d289c2baSAndroid Build Coastguard Worker                                           uint64_t* out_rollback_index) = 0;
73*d289c2baSAndroid Build Coastguard Worker 
74*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult write_rollback_index(AvbOps* ops,
75*d289c2baSAndroid Build Coastguard Worker                                            size_t rollback_index_slot,
76*d289c2baSAndroid Build Coastguard Worker                                            uint64_t rollback_index) = 0;
77*d289c2baSAndroid Build Coastguard Worker 
78*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_is_device_unlocked(AvbOps* ops,
79*d289c2baSAndroid Build Coastguard Worker                                               bool* out_is_device_unlocked) = 0;
80*d289c2baSAndroid Build Coastguard Worker 
81*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
82*d289c2baSAndroid Build Coastguard Worker                                                     const char* partition,
83*d289c2baSAndroid Build Coastguard Worker                                                     char* guid_buf,
84*d289c2baSAndroid Build Coastguard Worker                                                     size_t guid_buf_size) = 0;
85*d289c2baSAndroid Build Coastguard Worker 
86*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult get_size_of_partition(AvbOps* ops,
87*d289c2baSAndroid Build Coastguard Worker                                             const char* partition,
88*d289c2baSAndroid Build Coastguard Worker                                             uint64_t* out_size) = 0;
89*d289c2baSAndroid Build Coastguard Worker 
90*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_persistent_value(const char* name,
91*d289c2baSAndroid Build Coastguard Worker                                             size_t buffer_size,
92*d289c2baSAndroid Build Coastguard Worker                                             uint8_t* out_buffer,
93*d289c2baSAndroid Build Coastguard Worker                                             size_t* out_num_bytes_read) = 0;
94*d289c2baSAndroid Build Coastguard Worker 
95*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult write_persistent_value(const char* name,
96*d289c2baSAndroid Build Coastguard Worker                                              size_t value_size,
97*d289c2baSAndroid Build Coastguard Worker                                              const uint8_t* value) = 0;
98*d289c2baSAndroid Build Coastguard Worker 
99*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult validate_public_key_for_partition(
100*d289c2baSAndroid Build Coastguard Worker       AvbOps* ops,
101*d289c2baSAndroid Build Coastguard Worker       const char* partition,
102*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_data,
103*d289c2baSAndroid Build Coastguard Worker       size_t public_key_length,
104*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_metadata,
105*d289c2baSAndroid Build Coastguard Worker       size_t public_key_metadata_length,
106*d289c2baSAndroid Build Coastguard Worker       bool* out_is_trusted,
107*d289c2baSAndroid Build Coastguard Worker       uint32_t* out_rollback_index_location) = 0;
108*d289c2baSAndroid Build Coastguard Worker 
109*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_permanent_attributes(
110*d289c2baSAndroid Build Coastguard Worker       AvbCertPermanentAttributes* attributes) = 0;
111*d289c2baSAndroid Build Coastguard Worker 
112*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult read_permanent_attributes_hash(
113*d289c2baSAndroid Build Coastguard Worker       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) = 0;
114*d289c2baSAndroid Build Coastguard Worker 
115*d289c2baSAndroid Build Coastguard Worker   virtual void set_key_version(size_t rollback_index_location,
116*d289c2baSAndroid Build Coastguard Worker                                uint64_t key_version) = 0;
117*d289c2baSAndroid Build Coastguard Worker 
118*d289c2baSAndroid Build Coastguard Worker   virtual AvbIOResult get_random(size_t num_bytes, uint8_t* output) = 0;
119*d289c2baSAndroid Build Coastguard Worker };
120*d289c2baSAndroid Build Coastguard Worker 
121*d289c2baSAndroid Build Coastguard Worker // Provides fake implementations of AVB ops. All instances of this class must be
122*d289c2baSAndroid Build Coastguard Worker // created on the same thread.
123*d289c2baSAndroid Build Coastguard Worker class FakeAvbOps : public FakeAvbOpsDelegate {
124*d289c2baSAndroid Build Coastguard Worker  public:
125*d289c2baSAndroid Build Coastguard Worker   FakeAvbOps();
126*d289c2baSAndroid Build Coastguard Worker   virtual ~FakeAvbOps();
127*d289c2baSAndroid Build Coastguard Worker 
GetInstanceFromAvbOps(AvbOps * ops)128*d289c2baSAndroid Build Coastguard Worker   static FakeAvbOps* GetInstanceFromAvbOps(AvbOps* ops) {
129*d289c2baSAndroid Build Coastguard Worker     return reinterpret_cast<FakeAvbOps*>(ops->user_data);
130*d289c2baSAndroid Build Coastguard Worker   }
GetInstanceFromAvbABOps(AvbABOps * ab_ops)131*d289c2baSAndroid Build Coastguard Worker   static FakeAvbOps* GetInstanceFromAvbABOps(AvbABOps* ab_ops) {
132*d289c2baSAndroid Build Coastguard Worker     return reinterpret_cast<FakeAvbOps*>(ab_ops->ops->user_data);
133*d289c2baSAndroid Build Coastguard Worker   }
134*d289c2baSAndroid Build Coastguard Worker 
avb_ops()135*d289c2baSAndroid Build Coastguard Worker   AvbOps* avb_ops() {
136*d289c2baSAndroid Build Coastguard Worker     return &avb_ops_;
137*d289c2baSAndroid Build Coastguard Worker   }
138*d289c2baSAndroid Build Coastguard Worker 
avb_ab_ops()139*d289c2baSAndroid Build Coastguard Worker   AvbABOps* avb_ab_ops() {
140*d289c2baSAndroid Build Coastguard Worker     return &avb_ab_ops_;
141*d289c2baSAndroid Build Coastguard Worker   }
142*d289c2baSAndroid Build Coastguard Worker 
avb_cert_ops()143*d289c2baSAndroid Build Coastguard Worker   AvbCertOps* avb_cert_ops() {
144*d289c2baSAndroid Build Coastguard Worker     return &avb_cert_ops_;
145*d289c2baSAndroid Build Coastguard Worker   }
146*d289c2baSAndroid Build Coastguard Worker 
delegate()147*d289c2baSAndroid Build Coastguard Worker   FakeAvbOpsDelegate* delegate() {
148*d289c2baSAndroid Build Coastguard Worker     return delegate_;
149*d289c2baSAndroid Build Coastguard Worker   }
150*d289c2baSAndroid Build Coastguard Worker 
151*d289c2baSAndroid Build Coastguard Worker   // Does not take ownership of |delegate|.
set_delegate(FakeAvbOpsDelegate * delegate)152*d289c2baSAndroid Build Coastguard Worker   void set_delegate(FakeAvbOpsDelegate* delegate) {
153*d289c2baSAndroid Build Coastguard Worker     delegate_ = delegate;
154*d289c2baSAndroid Build Coastguard Worker   }
155*d289c2baSAndroid Build Coastguard Worker 
set_partition_dir(const std::filesystem::path & partition_dir)156*d289c2baSAndroid Build Coastguard Worker   void set_partition_dir(const std::filesystem::path& partition_dir) {
157*d289c2baSAndroid Build Coastguard Worker     partition_dir_ = partition_dir;
158*d289c2baSAndroid Build Coastguard Worker   }
159*d289c2baSAndroid Build Coastguard Worker 
set_expected_public_key(const std::string & expected_public_key)160*d289c2baSAndroid Build Coastguard Worker   void set_expected_public_key(const std::string& expected_public_key) {
161*d289c2baSAndroid Build Coastguard Worker     expected_public_key_ = expected_public_key;
162*d289c2baSAndroid Build Coastguard Worker   }
163*d289c2baSAndroid Build Coastguard Worker 
set_expected_public_key_for_partition(const std::string & partition_name,const std::string & expected_public_key,uint32_t rollback_index_location)164*d289c2baSAndroid Build Coastguard Worker   void set_expected_public_key_for_partition(
165*d289c2baSAndroid Build Coastguard Worker       const std::string& partition_name,
166*d289c2baSAndroid Build Coastguard Worker       const std::string& expected_public_key,
167*d289c2baSAndroid Build Coastguard Worker       uint32_t rollback_index_location) {
168*d289c2baSAndroid Build Coastguard Worker     expected_public_key_for_partition_map_[partition_name] =
169*d289c2baSAndroid Build Coastguard Worker         expected_public_key;
170*d289c2baSAndroid Build Coastguard Worker     rollback_index_location_for_partition_map_[partition_name] =
171*d289c2baSAndroid Build Coastguard Worker         rollback_index_location;
172*d289c2baSAndroid Build Coastguard Worker   }
173*d289c2baSAndroid Build Coastguard Worker 
set_expected_public_key_metadata(const std::string & expected_public_key_metadata)174*d289c2baSAndroid Build Coastguard Worker   void set_expected_public_key_metadata(
175*d289c2baSAndroid Build Coastguard Worker       const std::string& expected_public_key_metadata) {
176*d289c2baSAndroid Build Coastguard Worker     expected_public_key_metadata_ = expected_public_key_metadata;
177*d289c2baSAndroid Build Coastguard Worker   }
178*d289c2baSAndroid Build Coastguard Worker 
set_stored_rollback_indexes(const std::map<size_t,uint64_t> & stored_rollback_indexes)179*d289c2baSAndroid Build Coastguard Worker   void set_stored_rollback_indexes(
180*d289c2baSAndroid Build Coastguard Worker       const std::map<size_t, uint64_t>& stored_rollback_indexes) {
181*d289c2baSAndroid Build Coastguard Worker     stored_rollback_indexes_ = stored_rollback_indexes;
182*d289c2baSAndroid Build Coastguard Worker   }
183*d289c2baSAndroid Build Coastguard Worker 
get_stored_rollback_indexes()184*d289c2baSAndroid Build Coastguard Worker   std::map<size_t, uint64_t> get_stored_rollback_indexes() {
185*d289c2baSAndroid Build Coastguard Worker     return stored_rollback_indexes_;
186*d289c2baSAndroid Build Coastguard Worker   }
187*d289c2baSAndroid Build Coastguard Worker 
get_verified_rollback_indexes()188*d289c2baSAndroid Build Coastguard Worker   std::map<size_t, uint64_t> get_verified_rollback_indexes() {
189*d289c2baSAndroid Build Coastguard Worker     return verified_rollback_indexes_;
190*d289c2baSAndroid Build Coastguard Worker   }
191*d289c2baSAndroid Build Coastguard Worker 
set_stored_is_device_unlocked(bool stored_is_device_unlocked)192*d289c2baSAndroid Build Coastguard Worker   void set_stored_is_device_unlocked(bool stored_is_device_unlocked) {
193*d289c2baSAndroid Build Coastguard Worker     stored_is_device_unlocked_ = stored_is_device_unlocked;
194*d289c2baSAndroid Build Coastguard Worker   }
195*d289c2baSAndroid Build Coastguard Worker 
set_permanent_attributes(const AvbCertPermanentAttributes & attributes)196*d289c2baSAndroid Build Coastguard Worker   void set_permanent_attributes(const AvbCertPermanentAttributes& attributes) {
197*d289c2baSAndroid Build Coastguard Worker     permanent_attributes_ = attributes;
198*d289c2baSAndroid Build Coastguard Worker   }
199*d289c2baSAndroid Build Coastguard Worker 
set_permanent_attributes_hash(const std::string & hash)200*d289c2baSAndroid Build Coastguard Worker   void set_permanent_attributes_hash(const std::string& hash) {
201*d289c2baSAndroid Build Coastguard Worker     permanent_attributes_hash_ = hash;
202*d289c2baSAndroid Build Coastguard Worker   }
203*d289c2baSAndroid Build Coastguard Worker 
204*d289c2baSAndroid Build Coastguard Worker   // All AvbOps for partitions in the given set will fail with
205*d289c2baSAndroid Build Coastguard Worker   // AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION.
set_hidden_partitions(const std::set<std::string> & partitions)206*d289c2baSAndroid Build Coastguard Worker   void set_hidden_partitions(const std::set<std::string>& partitions) {
207*d289c2baSAndroid Build Coastguard Worker     hidden_partitions_ = partitions;
208*d289c2baSAndroid Build Coastguard Worker   }
209*d289c2baSAndroid Build Coastguard Worker 
210*d289c2baSAndroid Build Coastguard Worker   void enable_get_preloaded_partition();
211*d289c2baSAndroid Build Coastguard Worker 
212*d289c2baSAndroid Build Coastguard Worker   bool preload_partition(const std::string& partition,
213*d289c2baSAndroid Build Coastguard Worker                          const base::FilePath& path);
214*d289c2baSAndroid Build Coastguard Worker 
215*d289c2baSAndroid Build Coastguard Worker   bool preload_preallocated_partition(const std::string& partition,
216*d289c2baSAndroid Build Coastguard Worker                                       uint8_t* buffer,
217*d289c2baSAndroid Build Coastguard Worker                                       size_t size);
218*d289c2baSAndroid Build Coastguard Worker 
219*d289c2baSAndroid Build Coastguard Worker   // Gets the partition names that were passed to the
220*d289c2baSAndroid Build Coastguard Worker   // read_from_partition() operation.
221*d289c2baSAndroid Build Coastguard Worker   std::set<std::string> get_partition_names_read_from();
222*d289c2baSAndroid Build Coastguard Worker 
223*d289c2baSAndroid Build Coastguard Worker   // FakeAvbOpsDelegate methods.
224*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_from_partition(const char* partition,
225*d289c2baSAndroid Build Coastguard Worker                                   int64_t offset,
226*d289c2baSAndroid Build Coastguard Worker                                   size_t num_bytes,
227*d289c2baSAndroid Build Coastguard Worker                                   void* buffer,
228*d289c2baSAndroid Build Coastguard Worker                                   size_t* out_num_read) override;
229*d289c2baSAndroid Build Coastguard Worker 
230*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_preloaded_partition(const char* partition,
231*d289c2baSAndroid Build Coastguard Worker                                       size_t num_bytes,
232*d289c2baSAndroid Build Coastguard Worker                                       uint8_t** out_pointer,
233*d289c2baSAndroid Build Coastguard Worker                                       size_t* out_num_bytes_preloaded) override;
234*d289c2baSAndroid Build Coastguard Worker 
235*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_to_partition(const char* partition,
236*d289c2baSAndroid Build Coastguard Worker                                  int64_t offset,
237*d289c2baSAndroid Build Coastguard Worker                                  size_t num_bytes,
238*d289c2baSAndroid Build Coastguard Worker                                  const void* buffer) override;
239*d289c2baSAndroid Build Coastguard Worker 
240*d289c2baSAndroid Build Coastguard Worker   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
241*d289c2baSAndroid Build Coastguard Worker                                          const uint8_t* public_key_data,
242*d289c2baSAndroid Build Coastguard Worker                                          size_t public_key_length,
243*d289c2baSAndroid Build Coastguard Worker                                          const uint8_t* public_key_metadata,
244*d289c2baSAndroid Build Coastguard Worker                                          size_t public_key_metadata_length,
245*d289c2baSAndroid Build Coastguard Worker                                          bool* out_key_is_trusted) override;
246*d289c2baSAndroid Build Coastguard Worker 
247*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_rollback_index(AvbOps* ops,
248*d289c2baSAndroid Build Coastguard Worker                                   size_t rollback_index_location,
249*d289c2baSAndroid Build Coastguard Worker                                   uint64_t* out_rollback_index) override;
250*d289c2baSAndroid Build Coastguard Worker 
251*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_rollback_index(AvbOps* ops,
252*d289c2baSAndroid Build Coastguard Worker                                    size_t rollback_index_location,
253*d289c2baSAndroid Build Coastguard Worker                                    uint64_t rollback_index) override;
254*d289c2baSAndroid Build Coastguard Worker 
255*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_is_device_unlocked(AvbOps* ops,
256*d289c2baSAndroid Build Coastguard Worker                                       bool* out_is_device_unlocked) override;
257*d289c2baSAndroid Build Coastguard Worker 
258*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
259*d289c2baSAndroid Build Coastguard Worker                                             const char* partition,
260*d289c2baSAndroid Build Coastguard Worker                                             char* guid_buf,
261*d289c2baSAndroid Build Coastguard Worker                                             size_t guid_buf_size) override;
262*d289c2baSAndroid Build Coastguard Worker 
263*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_size_of_partition(AvbOps* ops,
264*d289c2baSAndroid Build Coastguard Worker                                     const char* partition,
265*d289c2baSAndroid Build Coastguard Worker                                     uint64_t* out_size) override;
266*d289c2baSAndroid Build Coastguard Worker 
267*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_persistent_value(const char* name,
268*d289c2baSAndroid Build Coastguard Worker                                     size_t buffer_size,
269*d289c2baSAndroid Build Coastguard Worker                                     uint8_t* out_buffer,
270*d289c2baSAndroid Build Coastguard Worker                                     size_t* out_num_bytes_read) override;
271*d289c2baSAndroid Build Coastguard Worker 
272*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_persistent_value(const char* name,
273*d289c2baSAndroid Build Coastguard Worker                                      size_t value_size,
274*d289c2baSAndroid Build Coastguard Worker                                      const uint8_t* value) override;
275*d289c2baSAndroid Build Coastguard Worker 
276*d289c2baSAndroid Build Coastguard Worker   AvbIOResult validate_public_key_for_partition(
277*d289c2baSAndroid Build Coastguard Worker       AvbOps* ops,
278*d289c2baSAndroid Build Coastguard Worker       const char* partition,
279*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_data,
280*d289c2baSAndroid Build Coastguard Worker       size_t public_key_length,
281*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_metadata,
282*d289c2baSAndroid Build Coastguard Worker       size_t public_key_metadata_length,
283*d289c2baSAndroid Build Coastguard Worker       bool* out_is_trusted,
284*d289c2baSAndroid Build Coastguard Worker       uint32_t* out_rollback_index_location) override;
285*d289c2baSAndroid Build Coastguard Worker 
286*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_permanent_attributes(
287*d289c2baSAndroid Build Coastguard Worker       AvbCertPermanentAttributes* attributes) override;
288*d289c2baSAndroid Build Coastguard Worker 
289*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_permanent_attributes_hash(
290*d289c2baSAndroid Build Coastguard Worker       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override;
291*d289c2baSAndroid Build Coastguard Worker 
292*d289c2baSAndroid Build Coastguard Worker   void set_key_version(size_t rollback_index_location,
293*d289c2baSAndroid Build Coastguard Worker                        uint64_t key_version) override;
294*d289c2baSAndroid Build Coastguard Worker 
295*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_random(size_t num_bytes, uint8_t* output) override;
296*d289c2baSAndroid Build Coastguard Worker 
297*d289c2baSAndroid Build Coastguard Worker  private:
298*d289c2baSAndroid Build Coastguard Worker   AvbOps avb_ops_;
299*d289c2baSAndroid Build Coastguard Worker   AvbABOps avb_ab_ops_;
300*d289c2baSAndroid Build Coastguard Worker   AvbCertOps avb_cert_ops_;
301*d289c2baSAndroid Build Coastguard Worker 
302*d289c2baSAndroid Build Coastguard Worker   FakeAvbOpsDelegate* delegate_;
303*d289c2baSAndroid Build Coastguard Worker 
304*d289c2baSAndroid Build Coastguard Worker   std::filesystem::path partition_dir_;
305*d289c2baSAndroid Build Coastguard Worker 
306*d289c2baSAndroid Build Coastguard Worker   std::string expected_public_key_;
307*d289c2baSAndroid Build Coastguard Worker   std::string expected_public_key_metadata_;
308*d289c2baSAndroid Build Coastguard Worker 
309*d289c2baSAndroid Build Coastguard Worker   std::map<std::string, std::string> expected_public_key_for_partition_map_;
310*d289c2baSAndroid Build Coastguard Worker 
311*d289c2baSAndroid Build Coastguard Worker   std::map<std::string, uint32_t> rollback_index_location_for_partition_map_;
312*d289c2baSAndroid Build Coastguard Worker 
313*d289c2baSAndroid Build Coastguard Worker   std::map<size_t, uint64_t> stored_rollback_indexes_;
314*d289c2baSAndroid Build Coastguard Worker   std::map<size_t, uint64_t> verified_rollback_indexes_;
315*d289c2baSAndroid Build Coastguard Worker 
316*d289c2baSAndroid Build Coastguard Worker   bool stored_is_device_unlocked_;
317*d289c2baSAndroid Build Coastguard Worker 
318*d289c2baSAndroid Build Coastguard Worker   AvbCertPermanentAttributes permanent_attributes_;
319*d289c2baSAndroid Build Coastguard Worker   std::string permanent_attributes_hash_;
320*d289c2baSAndroid Build Coastguard Worker 
321*d289c2baSAndroid Build Coastguard Worker   std::set<std::string> partition_names_read_from_;
322*d289c2baSAndroid Build Coastguard Worker   std::map<std::string, uint8_t*> preloaded_partitions_;
323*d289c2baSAndroid Build Coastguard Worker   std::map<std::string, std::pair<uint8_t*, size_t>>
324*d289c2baSAndroid Build Coastguard Worker       preallocated_preloaded_partitions_;
325*d289c2baSAndroid Build Coastguard Worker   std::set<std::string> hidden_partitions_;
326*d289c2baSAndroid Build Coastguard Worker 
327*d289c2baSAndroid Build Coastguard Worker   std::map<std::string, std::string> stored_values_;
328*d289c2baSAndroid Build Coastguard Worker };
329*d289c2baSAndroid Build Coastguard Worker 
330*d289c2baSAndroid Build Coastguard Worker // A delegate implementation that calls FakeAvbOps by default.
331*d289c2baSAndroid Build Coastguard Worker class FakeAvbOpsDelegateWithDefaults : public FakeAvbOpsDelegate {
332*d289c2baSAndroid Build Coastguard Worker  public:
read_from_partition(const char * partition,int64_t offset,size_t num_bytes,void * buffer,size_t * out_num_read)333*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_from_partition(const char* partition,
334*d289c2baSAndroid Build Coastguard Worker                                   int64_t offset,
335*d289c2baSAndroid Build Coastguard Worker                                   size_t num_bytes,
336*d289c2baSAndroid Build Coastguard Worker                                   void* buffer,
337*d289c2baSAndroid Build Coastguard Worker                                   size_t* out_num_read) override {
338*d289c2baSAndroid Build Coastguard Worker     return ops_.read_from_partition(
339*d289c2baSAndroid Build Coastguard Worker         partition, offset, num_bytes, buffer, out_num_read);
340*d289c2baSAndroid Build Coastguard Worker   }
341*d289c2baSAndroid Build Coastguard Worker 
get_preloaded_partition(const char * partition,size_t num_bytes,uint8_t ** out_pointer,size_t * out_num_bytes_preloaded)342*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_preloaded_partition(
343*d289c2baSAndroid Build Coastguard Worker       const char* partition,
344*d289c2baSAndroid Build Coastguard Worker       size_t num_bytes,
345*d289c2baSAndroid Build Coastguard Worker       uint8_t** out_pointer,
346*d289c2baSAndroid Build Coastguard Worker       size_t* out_num_bytes_preloaded) override {
347*d289c2baSAndroid Build Coastguard Worker     return ops_.get_preloaded_partition(
348*d289c2baSAndroid Build Coastguard Worker         partition, num_bytes, out_pointer, out_num_bytes_preloaded);
349*d289c2baSAndroid Build Coastguard Worker   }
350*d289c2baSAndroid Build Coastguard Worker 
write_to_partition(const char * partition,int64_t offset,size_t num_bytes,const void * buffer)351*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_to_partition(const char* partition,
352*d289c2baSAndroid Build Coastguard Worker                                  int64_t offset,
353*d289c2baSAndroid Build Coastguard Worker                                  size_t num_bytes,
354*d289c2baSAndroid Build Coastguard Worker                                  const void* buffer) override {
355*d289c2baSAndroid Build Coastguard Worker     return ops_.write_to_partition(partition, offset, num_bytes, buffer);
356*d289c2baSAndroid Build Coastguard Worker   }
357*d289c2baSAndroid Build Coastguard Worker 
validate_vbmeta_public_key(AvbOps * ops,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_key_is_trusted)358*d289c2baSAndroid Build Coastguard Worker   AvbIOResult validate_vbmeta_public_key(AvbOps* ops,
359*d289c2baSAndroid Build Coastguard Worker                                          const uint8_t* public_key_data,
360*d289c2baSAndroid Build Coastguard Worker                                          size_t public_key_length,
361*d289c2baSAndroid Build Coastguard Worker                                          const uint8_t* public_key_metadata,
362*d289c2baSAndroid Build Coastguard Worker                                          size_t public_key_metadata_length,
363*d289c2baSAndroid Build Coastguard Worker                                          bool* out_key_is_trusted) override {
364*d289c2baSAndroid Build Coastguard Worker     return ops_.validate_vbmeta_public_key(ops,
365*d289c2baSAndroid Build Coastguard Worker                                            public_key_data,
366*d289c2baSAndroid Build Coastguard Worker                                            public_key_length,
367*d289c2baSAndroid Build Coastguard Worker                                            public_key_metadata,
368*d289c2baSAndroid Build Coastguard Worker                                            public_key_metadata_length,
369*d289c2baSAndroid Build Coastguard Worker                                            out_key_is_trusted);
370*d289c2baSAndroid Build Coastguard Worker   }
371*d289c2baSAndroid Build Coastguard Worker 
validate_public_key_for_partition(AvbOps * ops,const char * partition,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_key_is_trusted,uint32_t * out_rollback_index_location)372*d289c2baSAndroid Build Coastguard Worker   AvbIOResult validate_public_key_for_partition(
373*d289c2baSAndroid Build Coastguard Worker       AvbOps* ops,
374*d289c2baSAndroid Build Coastguard Worker       const char* partition,
375*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_data,
376*d289c2baSAndroid Build Coastguard Worker       size_t public_key_length,
377*d289c2baSAndroid Build Coastguard Worker       const uint8_t* public_key_metadata,
378*d289c2baSAndroid Build Coastguard Worker       size_t public_key_metadata_length,
379*d289c2baSAndroid Build Coastguard Worker       bool* out_key_is_trusted,
380*d289c2baSAndroid Build Coastguard Worker       uint32_t* out_rollback_index_location) override {
381*d289c2baSAndroid Build Coastguard Worker     return ops_.validate_public_key_for_partition(ops,
382*d289c2baSAndroid Build Coastguard Worker                                                   partition,
383*d289c2baSAndroid Build Coastguard Worker                                                   public_key_data,
384*d289c2baSAndroid Build Coastguard Worker                                                   public_key_length,
385*d289c2baSAndroid Build Coastguard Worker                                                   public_key_metadata,
386*d289c2baSAndroid Build Coastguard Worker                                                   public_key_metadata_length,
387*d289c2baSAndroid Build Coastguard Worker                                                   out_key_is_trusted,
388*d289c2baSAndroid Build Coastguard Worker                                                   out_rollback_index_location);
389*d289c2baSAndroid Build Coastguard Worker   }
390*d289c2baSAndroid Build Coastguard Worker 
read_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t * out_rollback_index)391*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_rollback_index(AvbOps* ops,
392*d289c2baSAndroid Build Coastguard Worker                                   size_t rollback_index_slot,
393*d289c2baSAndroid Build Coastguard Worker                                   uint64_t* out_rollback_index) override {
394*d289c2baSAndroid Build Coastguard Worker     return ops_.read_rollback_index(
395*d289c2baSAndroid Build Coastguard Worker         ops, rollback_index_slot, out_rollback_index);
396*d289c2baSAndroid Build Coastguard Worker   }
397*d289c2baSAndroid Build Coastguard Worker 
write_rollback_index(AvbOps * ops,size_t rollback_index_slot,uint64_t rollback_index)398*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_rollback_index(AvbOps* ops,
399*d289c2baSAndroid Build Coastguard Worker                                    size_t rollback_index_slot,
400*d289c2baSAndroid Build Coastguard Worker                                    uint64_t rollback_index) override {
401*d289c2baSAndroid Build Coastguard Worker     return ops_.write_rollback_index(ops, rollback_index_slot, rollback_index);
402*d289c2baSAndroid Build Coastguard Worker   }
403*d289c2baSAndroid Build Coastguard Worker 
read_is_device_unlocked(AvbOps * ops,bool * out_is_device_unlocked)404*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_is_device_unlocked(AvbOps* ops,
405*d289c2baSAndroid Build Coastguard Worker                                       bool* out_is_device_unlocked) override {
406*d289c2baSAndroid Build Coastguard Worker     return ops_.read_is_device_unlocked(ops, out_is_device_unlocked);
407*d289c2baSAndroid Build Coastguard Worker   }
408*d289c2baSAndroid Build Coastguard Worker 
get_unique_guid_for_partition(AvbOps * ops,const char * partition,char * guid_buf,size_t guid_buf_size)409*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
410*d289c2baSAndroid Build Coastguard Worker                                             const char* partition,
411*d289c2baSAndroid Build Coastguard Worker                                             char* guid_buf,
412*d289c2baSAndroid Build Coastguard Worker                                             size_t guid_buf_size) override {
413*d289c2baSAndroid Build Coastguard Worker     return ops_.get_unique_guid_for_partition(
414*d289c2baSAndroid Build Coastguard Worker         ops, partition, guid_buf, guid_buf_size);
415*d289c2baSAndroid Build Coastguard Worker   }
416*d289c2baSAndroid Build Coastguard Worker 
get_size_of_partition(AvbOps * ops,const char * partition,uint64_t * out_size)417*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_size_of_partition(AvbOps* ops,
418*d289c2baSAndroid Build Coastguard Worker                                     const char* partition,
419*d289c2baSAndroid Build Coastguard Worker                                     uint64_t* out_size) override {
420*d289c2baSAndroid Build Coastguard Worker     return ops_.get_size_of_partition(ops, partition, out_size);
421*d289c2baSAndroid Build Coastguard Worker   }
422*d289c2baSAndroid Build Coastguard Worker 
read_persistent_value(const char * name,size_t buffer_size,uint8_t * out_buffer,size_t * out_num_bytes_read)423*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_persistent_value(const char* name,
424*d289c2baSAndroid Build Coastguard Worker                                     size_t buffer_size,
425*d289c2baSAndroid Build Coastguard Worker                                     uint8_t* out_buffer,
426*d289c2baSAndroid Build Coastguard Worker                                     size_t* out_num_bytes_read) override {
427*d289c2baSAndroid Build Coastguard Worker     return ops_.read_persistent_value(
428*d289c2baSAndroid Build Coastguard Worker         name, buffer_size, out_buffer, out_num_bytes_read);
429*d289c2baSAndroid Build Coastguard Worker   }
430*d289c2baSAndroid Build Coastguard Worker 
write_persistent_value(const char * name,size_t value_size,const uint8_t * value)431*d289c2baSAndroid Build Coastguard Worker   AvbIOResult write_persistent_value(const char* name,
432*d289c2baSAndroid Build Coastguard Worker                                      size_t value_size,
433*d289c2baSAndroid Build Coastguard Worker                                      const uint8_t* value) override {
434*d289c2baSAndroid Build Coastguard Worker     return ops_.write_persistent_value(name, value_size, value);
435*d289c2baSAndroid Build Coastguard Worker   }
436*d289c2baSAndroid Build Coastguard Worker 
read_permanent_attributes(AvbCertPermanentAttributes * attributes)437*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_permanent_attributes(
438*d289c2baSAndroid Build Coastguard Worker       AvbCertPermanentAttributes* attributes) override {
439*d289c2baSAndroid Build Coastguard Worker     return ops_.read_permanent_attributes(attributes);
440*d289c2baSAndroid Build Coastguard Worker   }
441*d289c2baSAndroid Build Coastguard Worker 
read_permanent_attributes_hash(uint8_t hash[AVB_SHA256_DIGEST_SIZE])442*d289c2baSAndroid Build Coastguard Worker   AvbIOResult read_permanent_attributes_hash(
443*d289c2baSAndroid Build Coastguard Worker       uint8_t hash[AVB_SHA256_DIGEST_SIZE]) override {
444*d289c2baSAndroid Build Coastguard Worker     return ops_.read_permanent_attributes_hash(hash);
445*d289c2baSAndroid Build Coastguard Worker   }
446*d289c2baSAndroid Build Coastguard Worker 
set_key_version(size_t rollback_index_location,uint64_t key_version)447*d289c2baSAndroid Build Coastguard Worker   void set_key_version(size_t rollback_index_location,
448*d289c2baSAndroid Build Coastguard Worker                        uint64_t key_version) override {
449*d289c2baSAndroid Build Coastguard Worker     ops_.set_key_version(rollback_index_location, key_version);
450*d289c2baSAndroid Build Coastguard Worker   }
451*d289c2baSAndroid Build Coastguard Worker 
get_random(size_t num_bytes,uint8_t * output)452*d289c2baSAndroid Build Coastguard Worker   AvbIOResult get_random(size_t num_bytes, uint8_t* output) override {
453*d289c2baSAndroid Build Coastguard Worker     return ops_.get_random(num_bytes, output);
454*d289c2baSAndroid Build Coastguard Worker   }
455*d289c2baSAndroid Build Coastguard Worker 
456*d289c2baSAndroid Build Coastguard Worker  protected:
457*d289c2baSAndroid Build Coastguard Worker   FakeAvbOps ops_;
458*d289c2baSAndroid Build Coastguard Worker };
459*d289c2baSAndroid Build Coastguard Worker 
460*d289c2baSAndroid Build Coastguard Worker }  // namespace avb
461*d289c2baSAndroid Build Coastguard Worker 
462*d289c2baSAndroid Build Coastguard Worker #endif /* FAKE_AVB_OPS_H_ */
463