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