Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 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 <algorithm> |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | #include "util.hpp" |
| 21 | |
| 22 | using namespace std::literals::string_literals; |
| 23 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 24 | template <typename T> struct BusMeetsMSL |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 25 | { |
| 26 | std::string path; |
| 27 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 28 | BusMeetsMSL(const std::string& p) : path(p) |
| 29 | { |
| 30 | } |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 31 | |
| 32 | auto operator()(const T& arg) |
| 33 | { |
| 34 | // Query each service hosting |
| 35 | // xyz.openbmc_project.Inventory.Decorator.MeetsMinimumShipLevel. |
| 36 | |
| 37 | const auto& busName = arg.first; |
| 38 | return util::sdbusplus::getProperty<bool>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 39 | busName, path, |
| 40 | "xyz.openbmc_project.Inventory." |
| 41 | "Decorator.MeetsMinimumShipLevel"s, |
| 42 | "MeetsMinimumShipLevel"s); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 43 | } |
| 44 | }; |
| 45 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 46 | template <typename T> struct PathMeetsMSL |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 47 | { |
| 48 | auto operator()(const T& arg) |
| 49 | { |
| 50 | // A given path in the mapper response is composed of |
| 51 | // a map of services/interfaces. Validate each service |
| 52 | // that hosts the MSL interface meets the MSL. |
| 53 | |
| 54 | const auto& path = arg.first; |
| 55 | return std::all_of( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 56 | arg.second.begin(), arg.second.end(), |
| 57 | BusMeetsMSL<typename decltype(arg.second)::value_type>(path)); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
| 61 | int main(void) |
| 62 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 63 | auto mslVerificationRequired = util::sdbusplus::getProperty<bool>( |
| 64 | "/xyz/openbmc_project/control/minimum_ship_level_required"s, |
| 65 | "xyz.openbmc_project.Control.MinimumShipLevel"s, |
| 66 | "MinimumShipLevelRequired"s); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 67 | |
| 68 | if (!mslVerificationRequired) |
| 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | // Obtain references to all objects hosting |
| 74 | // xyz.openbmc_project.Inventory.Decorator.MeetsMinimumShipLevel |
| 75 | // with a mapper subtree query. For each object, validate that |
| 76 | // the minimum ship level has been met. |
| 77 | |
| 78 | using SubTreeType = |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 79 | std::map<std::string, std::map<std::string, std::vector<std::string>>>; |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 80 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 81 | auto subtree = util::sdbusplus::callMethodAndRead<SubTreeType>( |
| 82 | "xyz.openbmc_project.ObjectMapper"s, |
| 83 | "/xyz/openbmc_project/object_mapper"s, |
| 84 | "xyz.openbmc_project.ObjectMapper"s, "GetSubTree"s, "/"s, 0, |
| 85 | std::vector<std::string>{"xyz.openbmc_project.Inventory" |
| 86 | ".Decorator.MeetsMinimumShipLevel"s}); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 87 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 88 | auto result = std::all_of(subtree.begin(), subtree.end(), |
| 89 | PathMeetsMSL<SubTreeType::value_type>()); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 90 | |
| 91 | if (!result) |
| 92 | { |
| 93 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 94 | "The physical system configuration does not " |
| 95 | "satisfy the minimum ship level."); |
Brad Bishop | b1e329a | 2017-08-02 01:23:12 -0400 | [diff] [blame] | 96 | |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |