blob: 00e02ddecf853b86059726794eae52c13b867d74 [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
2 * Copyright (C) 2017 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#include "VintfObject.h"
18
Yifan Hong143cfe62017-04-13 20:18:01 -070019#include "CompatibilityMatrix.h"
20#include "parse_xml.h"
21
Yifan Hong3daec812017-02-27 18:49:11 -080022#include <functional>
23#include <memory>
24#include <mutex>
25
26namespace android {
27namespace vintf {
28
29template <typename T>
30struct LockedUniquePtr {
31 std::unique_ptr<T> object;
32 std::mutex mutex;
33};
34
35static LockedUniquePtr<HalManifest> gDeviceManifest;
36static LockedUniquePtr<HalManifest> gFrameworkManifest;
37static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo;
38
39template <typename T, typename F>
40static const T *Get(
41 LockedUniquePtr<T> *ptr,
Yifan Hong143cfe62017-04-13 20:18:01 -070042 bool skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080043 const F &fetchAllInformation) {
44 std::unique_lock<std::mutex> _lock(ptr->mutex);
Yifan Hong143cfe62017-04-13 20:18:01 -070045 if (skipCache || ptr->object == nullptr) {
Yifan Hong3daec812017-02-27 18:49:11 -080046 ptr->object = std::make_unique<T>();
47 if (fetchAllInformation(ptr->object.get()) != OK) {
48 ptr->object = nullptr; // frees the old object
49 }
50 }
51 return ptr->object.get();
52}
53
54// static
Yifan Hong143cfe62017-04-13 20:18:01 -070055const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) {
56 return Get(&gDeviceManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080057 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
58 "/vendor/manifest.xml"));
59}
60
61// static
Yifan Hong143cfe62017-04-13 20:18:01 -070062const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) {
63 return Get(&gFrameworkManifest, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080064 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
65 "/system/manifest.xml"));
66}
67
68// static
Yifan Hong143cfe62017-04-13 20:18:01 -070069const RuntimeInfo *VintfObject::GetRuntimeInfo(bool skipCache) {
70 return Get(&gDeviceRuntimeInfo, skipCache,
Yifan Hong3daec812017-02-27 18:49:11 -080071 std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1));
72}
73
Yifan Hong143cfe62017-04-13 20:18:01 -070074namespace details {
75
76static status_t mountSystem() {
77 // TODO(b/36814503): mount system partition here for recovery mode.
78 return OK;
79}
80
81static status_t mountVendor() {
82 // TODO(b/36814503): mount vendor partition here for recovery mode.
83 return OK;
84}
85
86enum class ParseStatus {
87 OK,
88 PARSE_ERROR,
89 DUPLICATED_FWK_ENTRY,
90 DUPLICATED_DEV_ENTRY,
91};
92
93std::string toString(ParseStatus status) {
94 switch(status) {
95 case ParseStatus::OK: return "OK";
96 case ParseStatus::PARSE_ERROR: return "parse error";
97 case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework";
98 case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device";
99 }
100 return "";
101}
102
103template<typename T>
104ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse,
105 std::unique_ptr<T> *fwk, std::unique_ptr<T> *dev) {
106 std::unique_ptr<T> ret = std::make_unique<T>();
107 if (!parse(ret.get(), xml)) {
108 return ParseStatus::PARSE_ERROR;
109 }
110 if (ret->type() == SchemaType::FRAMEWORK) {
111 if (fwk->get() != nullptr) {
112 return ParseStatus::DUPLICATED_FWK_ENTRY;
113 }
114 *fwk = std::move(ret);
115 } else if (ret->type() == SchemaType::DEVICE) {
116 if (dev->get() != nullptr) {
117 return ParseStatus::DUPLICATED_DEV_ENTRY;
118 }
119 *dev = std::move(ret);
120 }
121 return ParseStatus::OK;
122}
123
124template<typename T, typename GetFunction>
125static status_t getMissing(const T *pkg, bool mount,
126 std::function<status_t(void)> mountFunction,
127 const T **updated,
128 GetFunction getFunction) {
129 if (pkg != nullptr) {
130 *updated = pkg;
131 } else {
132 if (mount) {
133 (void)mountFunction(); // ignore mount errors
134 }
135 *updated = getFunction();
136 }
137 return OK;
138}
139
140#define ADD_MESSAGE(__error__) \
141 if (error != nullptr) { \
142 *error += (__error__); \
143 } \
144
145struct PackageInfo {
146 struct Pair {
147 std::unique_ptr<HalManifest> manifest;
148 std::unique_ptr<CompatibilityMatrix> matrix;
149 };
150 Pair dev;
151 Pair fwk;
152};
153
154struct UpdatedInfo {
155 struct Pair {
156 const HalManifest *manifest;
157 const CompatibilityMatrix *matrix;
158 };
159 Pair dev;
160 Pair fwk;
161 const RuntimeInfo *runtimeInfo;
162};
163
164// Parse all information from package;
165// Get missing information from the device;
166// Do compatibility check.
167int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount,
168 std::function<status_t(void)> mountSystem,
169 std::function<status_t(void)> mountVendor,
170 std::function<const HalManifest *(bool)> GetFrameworkHalManifest,
171 std::function<const HalManifest *(bool)> GetDeviceHalManifest,
172 std::function<const RuntimeInfo *(bool)> GetRuntimeInfo,
173 std::string *error) {
174
175 status_t status;
176 ParseStatus parseStatus;
177 PackageInfo pkg; // All information from package.
178 UpdatedInfo updated; // All files and runtime info after the update.
179
180 if (xmls.empty()) {
181 ADD_MESSAGE("nothing to update");
182 return BAD_VALUE;
183 }
184
185 // parse all information from package
186 for (const auto &xml : xmls) {
187 parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest);
188 if (parseStatus == ParseStatus::OK) {
189 continue; // work on next one
190 }
191 if (parseStatus != ParseStatus::PARSE_ERROR) {
192 ADD_MESSAGE(toString(parseStatus) + " manifest");
193 return ALREADY_EXISTS;
194 }
195 parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix);
196 if (parseStatus == ParseStatus::OK) {
197 continue; // work on next one
198 }
199 if (parseStatus != ParseStatus::PARSE_ERROR) {
200 ADD_MESSAGE(toString(parseStatus) + " matrix");
201 return ALREADY_EXISTS;
202 }
203 ADD_MESSAGE(toString(parseStatus)); // parse error
204 return BAD_VALUE;
205 }
206
207 // get missing info from device
208 if ((status = getMissing(pkg.fwk.manifest.get(), mount, mountSystem, &updated.fwk.manifest,
209 std::bind(GetFrameworkHalManifest, true /* skipCache */))) != OK) {
210 return status;
211 }
212 if ((status = getMissing(pkg.dev.manifest.get(), mount, mountVendor, &updated.dev.manifest,
213 std::bind(GetDeviceHalManifest, true /* skipCache */))) != OK) {
214 return status;
215 }
216 updated.runtimeInfo = GetRuntimeInfo(true /* skipCache */);
217 // TODO(b/37321309) get matrices from the device as well.
218 updated.fwk.matrix = pkg.fwk.matrix.get();
219 updated.dev.matrix = pkg.dev.matrix.get();
220
221 // null checks for files and runtime info after the update
222 // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible.
223 if (updated.fwk.manifest == nullptr) {
224 ADD_MESSAGE("No framework manifest file from device or from update package");
225 return NO_INIT;
226 }
227 if (updated.dev.manifest == nullptr) {
228 ADD_MESSAGE("No device manifest file from device or from update package");
229 return NO_INIT;
230 }
231 if (updated.fwk.matrix == nullptr) {
232 ADD_MESSAGE("No framework matrix, skipping;");
233 // TODO(b/37321309) consider missing matricies as errors.
234 }
235 if (updated.dev.matrix == nullptr) {
236 ADD_MESSAGE("No device matrix, skipping;");
237 // TODO(b/37321309) consider missing matricies as errors.
238 }
239 if (updated.runtimeInfo == nullptr) {
240 ADD_MESSAGE("No runtime info from device");
241 return NO_INIT;
242 }
243
244 // compatiblity check.
245 // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors.
246 if (updated.dev.manifest && updated.fwk.matrix) {
247 if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) {
248 error->insert(0, "Device manifest and framework compatibility matrix "
249 "are incompatible: ");
250 return INCOMPATIBLE;
251 }
252 }
253 if (updated.fwk.manifest && updated.dev.matrix) {
254 if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) {
255 error->insert(0, "Framework manifest and device compatibility matrix "
256 "are incompatible: ");
257 return INCOMPATIBLE;
258 }
259 }
260 if (updated.runtimeInfo && updated.fwk.matrix) {
261 if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error)) {
262 error->insert(0, "Runtime info and framework compatibility matrix "
263 "are incompatible: ");
264 return INCOMPATIBLE;
265 }
266 }
267
268 return COMPATIBLE;
269}
270
271} // namespace details
272
Yifan Hongfbbf0472017-04-07 18:14:18 -0700273// static
274int32_t VintfObject::CheckCompatibility(
Yifan Hong143cfe62017-04-13 20:18:01 -0700275 const std::vector<std::string> &xmls,
276 bool mount, std::string *error) {
277 return details::checkCompatibility(xmls, mount,
278 &details::mountSystem, &details::mountVendor,
279 &VintfObject::GetFrameworkHalManifest,
280 &VintfObject::GetDeviceHalManifest,
281 &VintfObject::GetRuntimeInfo,
282 error);
Yifan Hongfbbf0472017-04-07 18:14:18 -0700283}
284
Yifan Hong3daec812017-02-27 18:49:11 -0800285
286} // namespace vintf
287} // namespace android