1 
2 /*
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "utils.h"
19 
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include <android-base/properties.h>
26 
27 using android::base::GetUintProperty;
28 
29 namespace android {
30 namespace vintf {
31 namespace testing {
32 
33 // Path to directory on target containing test data.
34 const string kDataDir = "/data/local/tmp/";
35 
36 // Name of file containing HAL hashes.
37 const string kHashFileName = "current.txt";
38 
39 // Map from package name to package root.
40 const map<string, string> kPackageRoot = {
41     {"android.frameworks", "frameworks/hardware/interfaces/"},
42     {"android.hardware", "hardware/interfaces/"},
43     {"android.hidl", "system/libhidl/transport/"},
44     {"android.system", "system/hardware/interfaces/"},
45 };
46 
47 // HALs that are allowed to be passthrough under Treble rules.
48 const set<string> kPassthroughHals = {
49     "android.hardware.graphics.mapper", "android.hardware.renderscript",
50     "android.hidl.memory",
51 };
52 
GetBoardApiLevel()53 uint64_t GetBoardApiLevel() {
54   uint64_t api_level = GetUintProperty<uint64_t>("ro.board.api_level", 0);
55   if (api_level != 0) {
56     return api_level;
57   }
58   api_level = GetUintProperty<uint64_t>("ro.board.first_api_level", 0);
59   if (api_level != 0) {
60     return api_level;
61   }
62   api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
63   if (api_level != 0) {
64     return api_level;
65   }
66   return GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
67 }
68 
69 // For a given interface returns package root if known. Returns empty string
70 // otherwise.
PackageRoot(const FQName & fq_iface_name)71 const string PackageRoot(const FQName &fq_iface_name) {
72   for (const auto &package_root : kPackageRoot) {
73     if (fq_iface_name.inPackage(package_root.first)) {
74       return package_root.second;
75     }
76   }
77   return "";
78 }
79 
80 // Returns true iff HAL interface is Android platform.
IsAndroidPlatformInterface(const FQName & fq_iface_name)81 bool IsAndroidPlatformInterface(const FQName &fq_iface_name) {
82   // Package roots are only known for Android platform packages.
83   return !PackageRoot(fq_iface_name).empty();
84 }
85 
86 // Returns the set of released hashes for a given HAL interface.
ReleasedHashes(const FQName & fq_iface_name)87 set<string> ReleasedHashes(const FQName &fq_iface_name) {
88   set<string> released_hashes{};
89   string err = "";
90 
91   string file_path = kDataDir + PackageRoot(fq_iface_name) + kHashFileName;
92   auto hashes = Hash::lookupHash(file_path, fq_iface_name.string(), &err);
93   released_hashes.insert(hashes.begin(), hashes.end());
94   return released_hashes;
95 }
96 
97 // Returns the partition that a HAL is associated with.
PartitionOfProcess(int32_t pid)98 Partition PartitionOfProcess(int32_t pid) {
99   auto partition = android::procpartition::getPartition(pid);
100 
101   // TODO(b/70033981): remove once ODM and Vendor manifests are distinguished
102   if (partition == Partition::ODM) {
103     partition = Partition::VENDOR;
104   }
105 
106   return partition;
107 }
108 
PartitionOfType(SchemaType type)109 Partition PartitionOfType(SchemaType type) {
110   switch (type) {
111     case SchemaType::DEVICE:
112       return Partition::VENDOR;
113     case SchemaType::FRAMEWORK:
114       return Partition::SYSTEM;
115   }
116   return Partition::UNKNOWN;
117 }
118 
119 }  // namespace testing
120 }  // namespace vintf
121 }  // namespace android
122 
123 namespace std {
PrintTo(const android::vintf::testing::HalManifestPtr & v,ostream * os)124 void PrintTo(const android::vintf::testing::HalManifestPtr &v, ostream *os) {
125   if (v == nullptr) {
126     *os << "nullptr";
127     return;
128   }
129   *os << to_string(v->type()) << " manifest";
130 }
131 }  // namespace std
132