Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel 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 | #pragma once |
Manojkiran Eda | 84855ab | 2021-12-05 11:09:02 +0530 | [diff] [blame] | 17 | #include <config.h> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <cereal/access.hpp> |
| 22 | #include <cereal/archives/json.hpp> |
| 23 | #include <cereal/cereal.hpp> |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 24 | #include <cereal/types/map.hpp> |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 25 | #include <cereal/types/tuple.hpp> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 26 | #include <cereal/types/vector.hpp> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 27 | #include <phosphor-logging/elog-errors.hpp> |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 28 | #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 29 | #include <xyz/openbmc_project/Common/error.hpp> |
| 30 | #include <xyz/openbmc_project/State/Boot/PostCode/server.hpp> |
| 31 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| 32 | |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <filesystem> |
| 35 | #include <fstream> |
| 36 | #include <iostream> |
| 37 | |
| 38 | const static constexpr char* CurrentBootCycleCountName = |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 39 | "CurrentBootCycleCount"; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 40 | const static constexpr char* CurrentBootCycleIndexName = |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 41 | "CurrentBootCycleIndex"; |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 42 | |
| 43 | // Singleton holder to store host/node and other path information |
| 44 | class PostCodeDataHolder |
| 45 | { |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 46 | PostCodeDataHolder() {} |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 47 | |
| 48 | public: |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 49 | static PostCodeDataHolder& getInstance() |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 50 | { |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 51 | static PostCodeDataHolder instance; |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 52 | return instance; |
| 53 | } |
| 54 | |
| 55 | int node; |
| 56 | |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 57 | const static constexpr char* PostCodePath = |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 58 | "/xyz/openbmc_project/state/boot/raw"; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 59 | const static constexpr char* PropertiesIntf = |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 60 | "org.freedesktop.DBus.Properties"; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 61 | const static constexpr char* PostCodeListPathPrefix = |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 62 | "/var/lib/phosphor-post-code-manager/host"; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 63 | const static constexpr char* HostStatePathPrefix = |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 64 | "/xyz/openbmc_project/state/host"; |
| 65 | }; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 66 | |
| 67 | struct EventDeleter |
| 68 | { |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 69 | void operator()(sd_event* event) const |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 70 | { |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 71 | sd_event_unref(event); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 75 | using primarycode_t = uint64_t; |
| 76 | using secondarycode_t = std::vector<uint8_t>; |
| 77 | using postcode_t = std::tuple<primarycode_t, secondarycode_t>; |
Manojkiran Eda | 410ba29 | 2021-12-05 10:55:56 +0530 | [diff] [blame] | 78 | namespace fs = std::filesystem; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 79 | namespace StateServer = sdbusplus::xyz::openbmc_project::State::server; |
| 80 | |
| 81 | using post_code = |
| 82 | sdbusplus::xyz::openbmc_project::State::Boot::server::PostCode; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 83 | using delete_all = |
| 84 | sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 85 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 86 | struct PostCode : sdbusplus::server::object_t<post_code, delete_all> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 87 | { |
Bonnie Lo | 2018964 | 2022-11-10 13:32:42 +0800 | [diff] [blame] | 88 | PostCodeDataHolder& postcodeDataHolderObj = |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 89 | PostCodeDataHolder::getInstance(); |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 90 | |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 91 | PostCode(sdbusplus::bus_t& bus, const char* path, EventPtr& /*event*/) : |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 92 | sdbusplus::server::object_t<post_code, delete_all>(bus, path), bus(bus), |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 93 | propertiesChangedSignalRaw( |
| 94 | bus, |
| 95 | sdbusplus::bus::match::rules::type::signal() + |
| 96 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 97 | sdbusplus::bus::match::rules::path( |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 98 | postcodeDataHolderObj.PostCodePath + |
| 99 | std::to_string(postcodeDataHolderObj.node)) + |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 100 | sdbusplus::bus::match::rules::interface( |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 101 | postcodeDataHolderObj.PropertiesIntf), |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 102 | [this](sdbusplus::message_t& msg) { |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 103 | std::string objectName; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 104 | std::map<std::string, std::variant<postcode_t>> msgData; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 105 | msg.read(objectName, msgData); |
| 106 | // Check if it was the Value property that changed. |
| 107 | auto valPropMap = msgData.find("Value"); |
| 108 | { |
| 109 | if (valPropMap != msgData.end()) |
| 110 | { |
| 111 | this->savePostCodes( |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 112 | std::get<postcode_t>(valPropMap->second)); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | }), |
| 116 | propertiesChangedSignalCurrentHostState( |
| 117 | bus, |
| 118 | sdbusplus::bus::match::rules::type::signal() + |
| 119 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 120 | sdbusplus::bus::match::rules::path( |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 121 | postcodeDataHolderObj.HostStatePathPrefix + |
| 122 | std::to_string(postcodeDataHolderObj.node)) + |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 123 | sdbusplus::bus::match::rules::interface( |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 124 | postcodeDataHolderObj.PropertiesIntf), |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 125 | [this](sdbusplus::message_t& msg) { |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 126 | std::string objectName; |
Patrick Williams | 9d91a39 | 2020-05-13 17:59:26 -0500 | [diff] [blame] | 127 | std::map<std::string, std::variant<std::string>> msgData; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 128 | msg.read(objectName, msgData); |
| 129 | // Check if it was the Value property that changed. |
| 130 | auto valPropMap = msgData.find("CurrentHostState"); |
| 131 | { |
| 132 | if (valPropMap != msgData.end()) |
| 133 | { |
| 134 | StateServer::Host::HostState currentHostState = |
| 135 | StateServer::Host::convertHostStateFromString( |
Patrick Williams | 23e3bf0 | 2020-05-13 11:37:14 -0500 | [diff] [blame] | 136 | std::get<std::string>(valPropMap->second)); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 137 | if (currentHostState == |
| 138 | StateServer::Host::HostState::Off) |
| 139 | { |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 140 | if (this->postCodes.empty()) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 141 | { |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 142 | std::cerr << "HostState changed to OFF. Empty " |
| 143 | "postcode log, keep boot cycle at " |
| 144 | << this->currentBootCycleIndex |
| 145 | << std::endl; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 146 | } |
| 147 | else |
| 148 | { |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 149 | this->postCodes.clear(); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 150 | } |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | }) |
| 155 | { |
| 156 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 157 | "PostCode is created"); |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 158 | auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix + |
| 159 | std::to_string(postcodeDataHolderObj.node)); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 160 | fs::create_directories(dir); |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame] | 161 | strPostCodeListPath = postcodeDataHolderObj.PostCodeListPathPrefix + |
| 162 | std::to_string(postcodeDataHolderObj.node) + "/"; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 163 | strCurrentBootCycleIndexName = CurrentBootCycleIndexName; |
| 164 | uint16_t index = 0; |
| 165 | deserialize( |
| 166 | fs::path(strPostCodeListPath + strCurrentBootCycleIndexName), |
| 167 | index); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 168 | currentBootCycleIndex = index; |
| 169 | strCurrentBootCycleCountName = CurrentBootCycleCountName; |
| 170 | uint16_t count = 0; |
| 171 | deserialize( |
| 172 | fs::path(strPostCodeListPath + strCurrentBootCycleCountName), |
| 173 | count); |
| 174 | currentBootCycleCount(count); |
Manojkiran Eda | aed7b3d | 2021-06-19 09:39:30 +0530 | [diff] [blame] | 175 | maxBootCycleNum(MAX_BOOT_CYCLE_COUNT); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 176 | } |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 177 | ~PostCode() {} |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 178 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 179 | std::vector<postcode_t> getPostCodes(uint16_t index) override; |
| 180 | std::map<uint64_t, postcode_t> |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 181 | getPostCodesWithTimeStamp(uint16_t index) override; |
| 182 | void deleteAll() override; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 183 | |
| 184 | private: |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 185 | void incrBootCycle(); |
| 186 | uint16_t getBootNum(const uint16_t index) const; |
| 187 | |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 188 | sdbusplus::bus_t& bus; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 189 | std::chrono::time_point<std::chrono::steady_clock> firstPostCodeTimeSteady; |
| 190 | uint64_t firstPostCodeUsSinceEpoch; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 191 | std::map<uint64_t, postcode_t> postCodes; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 192 | std::string strPostCodeListPath; |
| 193 | std::string strCurrentBootCycleIndexName; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 194 | uint16_t currentBootCycleIndex; |
| 195 | std::string strCurrentBootCycleCountName; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 196 | void savePostCodes(postcode_t code); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 197 | sdbusplus::bus::match_t propertiesChangedSignalRaw; |
| 198 | sdbusplus::bus::match_t propertiesChangedSignalCurrentHostState; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 199 | fs::path serialize(const std::string& path); |
| 200 | bool deserialize(const fs::path& path, uint16_t& index); |
| 201 | bool deserializePostCodes(const fs::path& path, |
| 202 | std::map<uint64_t, postcode_t>& codes); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 203 | }; |