xref: /openbmc/phosphor-dbus-monitor/mslverify/verify.cpp (revision c5fe26a60b7a5f1c3c1a08d9cf2da71467356798)
1b1e329a6SBrad Bishop /**
2b1e329a6SBrad Bishop  * Copyright © 2017 IBM Corporation
3b1e329a6SBrad Bishop  *
4b1e329a6SBrad Bishop  * Licensed under the Apache License, Version 2.0 (the "License");
5b1e329a6SBrad Bishop  * you may not use this file except in compliance with the License.
6b1e329a6SBrad Bishop  * You may obtain a copy of the License at
7b1e329a6SBrad Bishop  *
8b1e329a6SBrad Bishop  *     http://www.apache.org/licenses/LICENSE-2.0
9b1e329a6SBrad Bishop  *
10b1e329a6SBrad Bishop  * Unless required by applicable law or agreed to in writing, software
11b1e329a6SBrad Bishop  * distributed under the License is distributed on an "AS IS" BASIS,
12b1e329a6SBrad Bishop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b1e329a6SBrad Bishop  * See the License for the specific language governing permissions and
14b1e329a6SBrad Bishop  * limitations under the License.
15b1e329a6SBrad Bishop  */
16b1e329a6SBrad Bishop 
173d6d3182SPatrick Venture #include "util.hpp"
183d6d3182SPatrick Venture 
19b1e329a6SBrad Bishop #include <algorithm>
20b1e329a6SBrad Bishop #include <map>
21b1e329a6SBrad Bishop #include <string>
22b1e329a6SBrad Bishop 
23b1e329a6SBrad Bishop using namespace std::literals::string_literals;
24b1e329a6SBrad Bishop 
253d6d3182SPatrick Venture template <typename T>
263d6d3182SPatrick Venture struct BusMeetsMSL
27b1e329a6SBrad Bishop {
28b1e329a6SBrad Bishop     std::string path;
29b1e329a6SBrad Bishop 
BusMeetsMSLBusMeetsMSL30*c5fe26a6SPatrick Williams     explicit BusMeetsMSL(const std::string& p) : path(p) {}
31b1e329a6SBrad Bishop 
operator ()BusMeetsMSL32b1e329a6SBrad Bishop     auto operator()(const T& arg)
33b1e329a6SBrad Bishop     {
34b1e329a6SBrad Bishop         // Query each service hosting
35b1e329a6SBrad Bishop         // xyz.openbmc_project.Inventory.Decorator.MeetsMinimumShipLevel.
36b1e329a6SBrad Bishop 
37b1e329a6SBrad Bishop         const auto& busName = arg.first;
38b1e329a6SBrad Bishop         return util::sdbusplus::getProperty<bool>(
39d1eac88dSBrad Bishop             busName, path,
40b1e329a6SBrad Bishop             "xyz.openbmc_project.Inventory."
41b1e329a6SBrad Bishop             "Decorator.MeetsMinimumShipLevel"s,
42b1e329a6SBrad Bishop             "MeetsMinimumShipLevel"s);
43b1e329a6SBrad Bishop     }
44b1e329a6SBrad Bishop };
45b1e329a6SBrad Bishop 
463d6d3182SPatrick Venture template <typename T>
473d6d3182SPatrick Venture struct PathMeetsMSL
48b1e329a6SBrad Bishop {
operator ()PathMeetsMSL49b1e329a6SBrad Bishop     auto operator()(const T& arg)
50b1e329a6SBrad Bishop     {
51b1e329a6SBrad Bishop         // A given path in the mapper response is composed of
52b1e329a6SBrad Bishop         // a map of services/interfaces.  Validate each service
53b1e329a6SBrad Bishop         // that hosts the MSL interface meets the MSL.
54b1e329a6SBrad Bishop 
55b1e329a6SBrad Bishop         const auto& path = arg.first;
56b1e329a6SBrad Bishop         return std::all_of(
57d1eac88dSBrad Bishop             arg.second.begin(), arg.second.end(),
58b1e329a6SBrad Bishop             BusMeetsMSL<typename decltype(arg.second)::value_type>(path));
59b1e329a6SBrad Bishop     }
60b1e329a6SBrad Bishop };
61b1e329a6SBrad Bishop 
main(void)62b1e329a6SBrad Bishop int main(void)
63b1e329a6SBrad Bishop {
64d1eac88dSBrad Bishop     auto mslVerificationRequired = util::sdbusplus::getProperty<bool>(
65b1e329a6SBrad Bishop         "/xyz/openbmc_project/control/minimum_ship_level_required"s,
66b1e329a6SBrad Bishop         "xyz.openbmc_project.Control.MinimumShipLevel"s,
67b1e329a6SBrad Bishop         "MinimumShipLevelRequired"s);
68b1e329a6SBrad Bishop 
69b1e329a6SBrad Bishop     if (!mslVerificationRequired)
70b1e329a6SBrad Bishop     {
71b1e329a6SBrad Bishop         return 0;
72b1e329a6SBrad Bishop     }
73b1e329a6SBrad Bishop 
74b1e329a6SBrad Bishop     // Obtain references to all objects hosting
75b1e329a6SBrad Bishop     // xyz.openbmc_project.Inventory.Decorator.MeetsMinimumShipLevel
76b1e329a6SBrad Bishop     // with a mapper subtree query.  For each object, validate that
77b1e329a6SBrad Bishop     // the minimum ship level has been met.
78b1e329a6SBrad Bishop 
79b1e329a6SBrad Bishop     using SubTreeType =
80d1eac88dSBrad Bishop         std::map<std::string, std::map<std::string, std::vector<std::string>>>;
81b1e329a6SBrad Bishop 
82d1eac88dSBrad Bishop     auto subtree = util::sdbusplus::callMethodAndRead<SubTreeType>(
83b1e329a6SBrad Bishop         "xyz.openbmc_project.ObjectMapper"s,
84b1e329a6SBrad Bishop         "/xyz/openbmc_project/object_mapper"s,
85d1eac88dSBrad Bishop         "xyz.openbmc_project.ObjectMapper"s, "GetSubTree"s, "/"s, 0,
86d1eac88dSBrad Bishop         std::vector<std::string>{"xyz.openbmc_project.Inventory"
87b1e329a6SBrad Bishop                                  ".Decorator.MeetsMinimumShipLevel"s});
88b1e329a6SBrad Bishop 
89d1eac88dSBrad Bishop     auto result = std::all_of(subtree.begin(), subtree.end(),
90b1e329a6SBrad Bishop                               PathMeetsMSL<SubTreeType::value_type>());
91b1e329a6SBrad Bishop 
92b1e329a6SBrad Bishop     if (!result)
93b1e329a6SBrad Bishop     {
9413e3df60SGeorge Liu         lg2::info(
9513e3df60SGeorge Liu             "The physical system configuration does not satisfy the minimum ship level.");
96b1e329a6SBrad Bishop 
97b1e329a6SBrad Bishop         return 1;
98b1e329a6SBrad Bishop     }
99b1e329a6SBrad Bishop 
100b1e329a6SBrad Bishop     return 0;
101b1e329a6SBrad Bishop }
102