Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 1 | /* |
| 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 Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 19 | #include "CompatibilityMatrix.h" |
| 20 | #include "parse_xml.h" |
| 21 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <memory> |
| 24 | #include <mutex> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace vintf { |
| 28 | |
| 29 | template <typename T> |
| 30 | struct LockedUniquePtr { |
| 31 | std::unique_ptr<T> object; |
| 32 | std::mutex mutex; |
| 33 | }; |
| 34 | |
| 35 | static LockedUniquePtr<HalManifest> gDeviceManifest; |
| 36 | static LockedUniquePtr<HalManifest> gFrameworkManifest; |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 37 | static LockedUniquePtr<CompatibilityMatrix> gDeviceMatrix; |
| 38 | static LockedUniquePtr<CompatibilityMatrix> gFrameworkMatrix; |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 39 | static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo; |
| 40 | |
| 41 | template <typename T, typename F> |
| 42 | static const T *Get( |
| 43 | LockedUniquePtr<T> *ptr, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 44 | bool skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 45 | const F &fetchAllInformation) { |
| 46 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 47 | if (skipCache || ptr->object == nullptr) { |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 48 | ptr->object = std::make_unique<T>(); |
| 49 | if (fetchAllInformation(ptr->object.get()) != OK) { |
| 50 | ptr->object = nullptr; // frees the old object |
| 51 | } |
| 52 | } |
| 53 | return ptr->object.get(); |
| 54 | } |
| 55 | |
| 56 | // static |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 57 | const HalManifest *VintfObject::GetDeviceHalManifest(bool skipCache) { |
| 58 | return Get(&gDeviceManifest, skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 59 | std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1, |
| 60 | "/vendor/manifest.xml")); |
| 61 | } |
| 62 | |
| 63 | // static |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 64 | const HalManifest *VintfObject::GetFrameworkHalManifest(bool skipCache) { |
| 65 | return Get(&gFrameworkManifest, skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 66 | std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1, |
| 67 | "/system/manifest.xml")); |
| 68 | } |
| 69 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 70 | |
| 71 | // static |
| 72 | const CompatibilityMatrix *VintfObject::GetDeviceCompatibilityMatrix(bool skipCache) { |
| 73 | return Get(&gDeviceMatrix, skipCache, |
| 74 | std::bind(&CompatibilityMatrix::fetchAllInformation, std::placeholders::_1, |
| 75 | "/vendor/compatibility_matrix.xml")); |
| 76 | } |
| 77 | |
| 78 | // static |
| 79 | const CompatibilityMatrix *VintfObject::GetFrameworkCompatibilityMatrix(bool skipCache) { |
| 80 | return Get(&gFrameworkMatrix, skipCache, |
| 81 | std::bind(&CompatibilityMatrix::fetchAllInformation, std::placeholders::_1, |
| 82 | "/system/compatibility_matrix.xml")); |
| 83 | } |
| 84 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 85 | // static |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 86 | const RuntimeInfo *VintfObject::GetRuntimeInfo(bool skipCache) { |
| 87 | return Get(&gDeviceRuntimeInfo, skipCache, |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 88 | std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1)); |
| 89 | } |
| 90 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 91 | namespace details { |
| 92 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 93 | static status_t fakeMountUmount() { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | enum class ParseStatus { |
| 98 | OK, |
| 99 | PARSE_ERROR, |
| 100 | DUPLICATED_FWK_ENTRY, |
| 101 | DUPLICATED_DEV_ENTRY, |
| 102 | }; |
| 103 | |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 104 | static std::string toString(ParseStatus status) { |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 105 | switch(status) { |
| 106 | case ParseStatus::OK: return "OK"; |
| 107 | case ParseStatus::PARSE_ERROR: return "parse error"; |
| 108 | case ParseStatus::DUPLICATED_FWK_ENTRY: return "duplicated framework"; |
| 109 | case ParseStatus::DUPLICATED_DEV_ENTRY: return "duplicated device"; |
| 110 | } |
| 111 | return ""; |
| 112 | } |
| 113 | |
| 114 | template<typename T> |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 115 | static ParseStatus tryParse(const std::string &xml, const XmlConverter<T> &parse, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 116 | std::unique_ptr<T> *fwk, std::unique_ptr<T> *dev) { |
| 117 | std::unique_ptr<T> ret = std::make_unique<T>(); |
| 118 | if (!parse(ret.get(), xml)) { |
| 119 | return ParseStatus::PARSE_ERROR; |
| 120 | } |
| 121 | if (ret->type() == SchemaType::FRAMEWORK) { |
| 122 | if (fwk->get() != nullptr) { |
| 123 | return ParseStatus::DUPLICATED_FWK_ENTRY; |
| 124 | } |
| 125 | *fwk = std::move(ret); |
| 126 | } else if (ret->type() == SchemaType::DEVICE) { |
| 127 | if (dev->get() != nullptr) { |
| 128 | return ParseStatus::DUPLICATED_DEV_ENTRY; |
| 129 | } |
| 130 | *dev = std::move(ret); |
| 131 | } |
| 132 | return ParseStatus::OK; |
| 133 | } |
| 134 | |
| 135 | template<typename T, typename GetFunction> |
| 136 | static status_t getMissing(const T *pkg, bool mount, |
| 137 | std::function<status_t(void)> mountFunction, |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 138 | std::function<status_t(void)> umountFunction, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 139 | const T **updated, |
| 140 | GetFunction getFunction) { |
| 141 | if (pkg != nullptr) { |
| 142 | *updated = pkg; |
| 143 | } else { |
| 144 | if (mount) { |
| 145 | (void)mountFunction(); // ignore mount errors |
| 146 | } |
| 147 | *updated = getFunction(); |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 148 | if (mount) { |
| 149 | (void)umountFunction(); // ignore umount errors |
| 150 | } |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 151 | } |
| 152 | return OK; |
| 153 | } |
| 154 | |
| 155 | #define ADD_MESSAGE(__error__) \ |
| 156 | if (error != nullptr) { \ |
| 157 | *error += (__error__); \ |
| 158 | } \ |
| 159 | |
| 160 | struct PackageInfo { |
| 161 | struct Pair { |
| 162 | std::unique_ptr<HalManifest> manifest; |
| 163 | std::unique_ptr<CompatibilityMatrix> matrix; |
| 164 | }; |
| 165 | Pair dev; |
| 166 | Pair fwk; |
| 167 | }; |
| 168 | |
| 169 | struct UpdatedInfo { |
| 170 | struct Pair { |
| 171 | const HalManifest *manifest; |
| 172 | const CompatibilityMatrix *matrix; |
| 173 | }; |
| 174 | Pair dev; |
| 175 | Pair fwk; |
| 176 | const RuntimeInfo *runtimeInfo; |
| 177 | }; |
| 178 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 179 | // Checks given compatibility info against info on the device. If no |
| 180 | // compatability info is given then the device info will be checked against |
| 181 | // itself. |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 182 | int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount, |
| 183 | std::function<status_t(void)> mountSystem, |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 184 | std::function<status_t(void)> umountSystem, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 185 | std::function<status_t(void)> mountVendor, |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 186 | std::function<status_t(void)> umountVendor, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 187 | std::function<const HalManifest *(bool)> GetFrameworkHalManifest, |
| 188 | std::function<const HalManifest *(bool)> GetDeviceHalManifest, |
| 189 | std::function<const RuntimeInfo *(bool)> GetRuntimeInfo, |
| 190 | std::string *error) { |
| 191 | |
| 192 | status_t status; |
| 193 | ParseStatus parseStatus; |
| 194 | PackageInfo pkg; // All information from package. |
| 195 | UpdatedInfo updated; // All files and runtime info after the update. |
| 196 | |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 197 | // parse all information from package |
| 198 | for (const auto &xml : xmls) { |
| 199 | parseStatus = tryParse(xml, gHalManifestConverter, &pkg.fwk.manifest, &pkg.dev.manifest); |
| 200 | if (parseStatus == ParseStatus::OK) { |
| 201 | continue; // work on next one |
| 202 | } |
| 203 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 204 | ADD_MESSAGE(toString(parseStatus) + " manifest"); |
| 205 | return ALREADY_EXISTS; |
| 206 | } |
| 207 | parseStatus = tryParse(xml, gCompatibilityMatrixConverter, &pkg.fwk.matrix, &pkg.dev.matrix); |
| 208 | if (parseStatus == ParseStatus::OK) { |
| 209 | continue; // work on next one |
| 210 | } |
| 211 | if (parseStatus != ParseStatus::PARSE_ERROR) { |
| 212 | ADD_MESSAGE(toString(parseStatus) + " matrix"); |
| 213 | return ALREADY_EXISTS; |
| 214 | } |
| 215 | ADD_MESSAGE(toString(parseStatus)); // parse error |
| 216 | return BAD_VALUE; |
| 217 | } |
| 218 | |
| 219 | // get missing info from device |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 220 | if ((status = getMissing(pkg.fwk.manifest.get(), mount, mountSystem, umountSystem, |
| 221 | &updated.fwk.manifest, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 222 | std::bind(GetFrameworkHalManifest, true /* skipCache */))) != OK) { |
| 223 | return status; |
| 224 | } |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 225 | if ((status = getMissing(pkg.dev.manifest.get(), mount, mountVendor, umountVendor, |
| 226 | &updated.dev.manifest, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 227 | std::bind(GetDeviceHalManifest, true /* skipCache */))) != OK) { |
| 228 | return status; |
| 229 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 230 | if ((status = getMissing( |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 231 | pkg.fwk.matrix.get(), mount, mountSystem, umountSystem, &updated.fwk.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 232 | std::bind(VintfObject::GetFrameworkCompatibilityMatrix, true /* skipCache */))) != |
| 233 | OK) { |
| 234 | return status; |
| 235 | } |
| 236 | if ((status = getMissing( |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 237 | pkg.dev.matrix.get(), mount, mountVendor, umountVendor, &updated.dev.matrix, |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 238 | std::bind(VintfObject::GetDeviceCompatibilityMatrix, true /* skipCache */))) != OK) { |
| 239 | return status; |
| 240 | } |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 241 | updated.runtimeInfo = GetRuntimeInfo(true /* skipCache */); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 242 | |
| 243 | // null checks for files and runtime info after the update |
| 244 | // TODO(b/37321309) if a compat mat is missing, it is not matched and considered compatible. |
| 245 | if (updated.fwk.manifest == nullptr) { |
| 246 | ADD_MESSAGE("No framework manifest file from device or from update package"); |
| 247 | return NO_INIT; |
| 248 | } |
| 249 | if (updated.dev.manifest == nullptr) { |
| 250 | ADD_MESSAGE("No device manifest file from device or from update package"); |
| 251 | return NO_INIT; |
| 252 | } |
| 253 | if (updated.fwk.matrix == nullptr) { |
| 254 | ADD_MESSAGE("No framework matrix, skipping;"); |
| 255 | // TODO(b/37321309) consider missing matricies as errors. |
| 256 | } |
| 257 | if (updated.dev.matrix == nullptr) { |
| 258 | ADD_MESSAGE("No device matrix, skipping;"); |
| 259 | // TODO(b/37321309) consider missing matricies as errors. |
| 260 | } |
| 261 | if (updated.runtimeInfo == nullptr) { |
| 262 | ADD_MESSAGE("No runtime info from device"); |
| 263 | return NO_INIT; |
| 264 | } |
| 265 | |
| 266 | // compatiblity check. |
| 267 | // TODO(b/37321309) outer if checks can be removed if we consider missing matrices as errors. |
| 268 | if (updated.dev.manifest && updated.fwk.matrix) { |
| 269 | if (!updated.dev.manifest->checkCompatibility(*updated.fwk.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 270 | if (error) |
| 271 | error->insert(0, "Device manifest and framework compatibility matrix " |
| 272 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 273 | return INCOMPATIBLE; |
| 274 | } |
| 275 | } |
| 276 | if (updated.fwk.manifest && updated.dev.matrix) { |
| 277 | if (!updated.fwk.manifest->checkCompatibility(*updated.dev.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 278 | if (error) |
| 279 | error->insert(0, "Framework manifest and device compatibility matrix " |
| 280 | "are incompatible: "); |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 281 | return INCOMPATIBLE; |
| 282 | } |
| 283 | } |
| 284 | if (updated.runtimeInfo && updated.fwk.matrix) { |
| 285 | if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error)) { |
Yifan Hong | 0d4be12 | 2017-05-15 17:04:22 -0700 | [diff] [blame] | 286 | if (error) |
| 287 | error->insert(0, "Runtime info and framework compatibility matrix " |
| 288 | "are incompatible: "); |
Yifan Hong | c1ac82d | 2017-05-15 18:29:59 -0700 | [diff] [blame] | 289 | // TODO(b/38325029): OTA should check compatibility of kernel version |
| 290 | // and AVB version. |
| 291 | // return INCOMPATIBLE; |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | return COMPATIBLE; |
| 296 | } |
| 297 | |
| 298 | } // namespace details |
| 299 | |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 300 | // static |
| 301 | int32_t VintfObject::CheckCompatibility( |
Yifan Hong | 9532bd2 | 2017-04-14 15:30:52 -0700 | [diff] [blame] | 302 | const std::vector<std::string> &xmls, std::string *error) { |
| 303 | return details::checkCompatibility(xmls, false /* mount */, |
| 304 | &details::fakeMountUmount, &details::fakeMountUmount, |
| 305 | &details::fakeMountUmount, &details::fakeMountUmount, |
Yifan Hong | 143cfe6 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 306 | &VintfObject::GetFrameworkHalManifest, |
| 307 | &VintfObject::GetDeviceHalManifest, |
| 308 | &VintfObject::GetRuntimeInfo, |
| 309 | error); |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 312 | |
| 313 | } // namespace vintf |
| 314 | } // namespace android |