1 /*
2  * Copyright (C) 2022 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 #pragma once
18 #include <com/android/trusty/device_tree/BnDeviceTree.h>
19 
20 using android::sp;
21 using android::binder::Status;
22 
23 namespace com {
24 namespace android {
25 namespace trusty {
26 namespace device_tree {
27 
28 class PropIterator : public BnPropIterator {
29 public:
PropIterator(int prop_offset,const unsigned char * dtb)30     PropIterator(int prop_offset, const unsigned char* dtb)
31             : BnPropIterator(),
32               mInitialPropOffset(prop_offset),
33               mCurrentPropOffset(-1),
34               mDtb(dtb) {}
35 
36     Status get_next_prop(Property* prop);
37 
38 private:
39     int mInitialPropOffset;
40     int mCurrentPropOffset;
41     const unsigned char* mDtb;
42 };
43 
44 class Node : public BnNode {
45 public:
Node(int offset,const unsigned char * dtb)46     Node(int offset, const unsigned char* dtb)
47             : BnNode(), mNodeOffset(offset), mDtb(dtb) {}
48 
49     Status get_name(std::string* node_name);
50 
51     Status get_subnode(const std::string& node_name, sp<INode>* node);
52 
53     Status get_subnodes(sp<INodeIterator>* node_iter);
54 
55     Status get_props(sp<IPropIterator>* prop_iter);
56 
57     Status get_prop(const std::string& prop_name, Property* prop);
58 
59 private:
60     int mNodeOffset;
61     const unsigned char* mDtb;
62 };
63 
64 class NodeIterator : public BnNodeIterator {
65 public:
66     // Creates an iterator over the subnodes of the node at the initial
67     // offset
NodeIterator(int initial_offset,const unsigned char * dtb)68     NodeIterator(int initial_offset, const unsigned char* dtb)
69             : BnNodeIterator(),
70               mCompatibleStrs(std::nullopt),
71               mInitialNodeOffset(initial_offset),
72               mCurrentNodeOffset(-1),
73               mDtb(dtb) {}
74 
75     // Creates an iterator over the nodes matching one of the compatible
76     // strings
NodeIterator(const std::vector<std::string> & compatible,int initial_offset,const unsigned char * dtb)77     NodeIterator(const std::vector<std::string>& compatible,
78                  int initial_offset,
79                  const unsigned char* dtb)
80             : BnNodeIterator(),
81               mCompatibleStrs(std::move(compatible)),
82               mInitialNodeOffset(initial_offset),
83               mCurrentNodeOffset(-1),
84               mDtb(dtb) {}
85 
86     Status get_next_node(sp<INode>* node);
87 
88 private:
89     std::optional<std::vector<std::string>> mCompatibleStrs;
90     int mInitialNodeOffset;
91     int mCurrentNodeOffset;
92     const unsigned char* mDtb;
93 };
94 
95 class DeviceTree : public BnDeviceTree {
96 public:
97     DeviceTree(const unsigned char* dtb, size_t dtb_size);
98 
99     // Get an iterator over nodes with one of the given compatible strings.
100     Status get_compatible_nodes_from_list(
101             const std::vector<std::string>& compatible_strs,
102             sp<INodeIterator>* node_iter);
103 
104 private:
105     const unsigned char* mDtb;
106 };
107 }  // namespace device_tree
108 }  // namespace trusty
109 }  // namespace android
110 }  // namespace com
111