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 | |
Amithash Prasad | b6616cd | 2025-05-29 15:56:51 -0700 | [diff] [blame^] | 21 | #include <nlohmann/json.hpp> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 22 | #include <phosphor-logging/elog-errors.hpp> |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 23 | #include <sdbusplus/timer.hpp> |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 25 | #include <xyz/openbmc_project/Common/error.hpp> |
| 26 | #include <xyz/openbmc_project/State/Boot/PostCode/server.hpp> |
| 27 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| 28 | |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 29 | #include <chrono> |
| 30 | #include <filesystem> |
| 31 | #include <fstream> |
| 32 | #include <iostream> |
| 33 | |
| 34 | const static constexpr char* CurrentBootCycleCountName = |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 35 | "CurrentBootCycleCount"; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 36 | const static constexpr char* CurrentBootCycleIndexName = |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 37 | "CurrentBootCycleIndex"; |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 38 | |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 39 | const static constexpr char* PostCodePath = |
| 40 | "/xyz/openbmc_project/state/boot/raw"; |
| 41 | const static constexpr char* PostCodeListPathPrefix = |
| 42 | "/var/lib/phosphor-post-code-manager/host"; |
| 43 | const static constexpr char* HostStatePathPrefix = |
| 44 | "/xyz/openbmc_project/state/host"; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 45 | |
| 46 | struct EventDeleter |
| 47 | { |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 48 | void operator()(sd_event* event) const |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 49 | { |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 50 | sd_event_unref(event); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 51 | } |
| 52 | }; |
| 53 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
Potin Lai | 06b1dbe | 2024-09-16 15:09:06 +0800 | [diff] [blame] | 54 | using primarycode_t = std::vector<uint8_t>; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 55 | using secondarycode_t = std::vector<uint8_t>; |
| 56 | using postcode_t = std::tuple<primarycode_t, secondarycode_t>; |
Manojkiran Eda | 410ba29 | 2021-12-05 10:55:56 +0530 | [diff] [blame] | 57 | namespace fs = std::filesystem; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 58 | namespace StateServer = sdbusplus::xyz::openbmc_project::State::server; |
| 59 | |
| 60 | using post_code = |
| 61 | sdbusplus::xyz::openbmc_project::State::Boot::server::PostCode; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 62 | using delete_all = |
| 63 | sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 64 | |
Amithash Prasad | b6616cd | 2025-05-29 15:56:51 -0700 | [diff] [blame^] | 65 | struct PostCodeEvent |
| 66 | { |
| 67 | std::string name; |
| 68 | nlohmann::json args; |
| 69 | void raise() const; |
| 70 | }; |
| 71 | |
| 72 | struct PostCodeHandler |
| 73 | { |
| 74 | primarycode_t primary; |
| 75 | std::optional<secondarycode_t> secondary; |
| 76 | std::vector<std::string> targets; |
| 77 | std::optional<PostCodeEvent> event; |
| 78 | }; |
| 79 | |
| 80 | struct PostCodeHandlers |
| 81 | { |
| 82 | std::vector<PostCodeHandler> handlers; |
| 83 | void handle(postcode_t code); |
| 84 | const PostCodeHandler* find(postcode_t code); |
| 85 | void load(const std::string& path); |
| 86 | }; |
| 87 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 88 | struct PostCode : sdbusplus::server::object_t<post_code, delete_all> |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 89 | { |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 90 | PostCode(sdbusplus::bus_t& bus, const char* path, EventPtr& event, |
Amithash Prasad | b6616cd | 2025-05-29 15:56:51 -0700 | [diff] [blame^] | 91 | int nodeIndex, PostCodeHandlers& handlers) : |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame] | 92 | sdbusplus::server::object_t<post_code, delete_all>(bus, path), bus(bus), |
| 93 | event(event), node(nodeIndex), |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 94 | postCodeListPath(PostCodeListPathPrefix + std::to_string(node)), |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 95 | propertiesChangedSignalRaw( |
| 96 | bus, |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 97 | sdbusplus::bus::match::rules::propertiesChanged( |
| 98 | PostCodePath + std::to_string(node), |
| 99 | "xyz.openbmc_project.State.Boot.Raw"), |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 100 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame] | 101 | std::string intfName; |
| 102 | std::map<std::string, std::variant<postcode_t>> msgData; |
| 103 | msg.read(intfName, msgData); |
| 104 | // Check if it was the Value property that changed. |
| 105 | auto valPropMap = msgData.find("Value"); |
| 106 | if (valPropMap != msgData.end()) |
| 107 | { |
| 108 | this->savePostCodes( |
| 109 | std::get<postcode_t>(valPropMap->second)); |
| 110 | } |
| 111 | }), |
Patrick Williams | 608a392 | 2023-10-20 11:18:57 -0500 | [diff] [blame] | 112 | propertiesChangedSignalCurrentHostState( |
| 113 | bus, |
| 114 | sdbusplus::bus::match::rules::propertiesChanged( |
| 115 | HostStatePathPrefix + std::to_string(node), |
| 116 | "xyz.openbmc_project.State.Host"), |
| 117 | [this](sdbusplus::message_t& msg) { |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame] | 118 | std::string intfName; |
| 119 | std::map<std::string, std::variant<std::string>> msgData; |
| 120 | msg.read(intfName, msgData); |
| 121 | // Check if it was the Value property that changed. |
| 122 | auto valPropMap = msgData.find("CurrentHostState"); |
| 123 | if (valPropMap != msgData.end()) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 124 | { |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame] | 125 | StateServer::Host::HostState currentHostState = |
| 126 | StateServer::Host::convertHostStateFromString( |
| 127 | std::get<std::string>(valPropMap->second)); |
| 128 | if (currentHostState == StateServer::Host::HostState::Off) |
| 129 | { |
| 130 | if (this->postCodes.empty()) |
| 131 | { |
| 132 | std::cerr |
| 133 | << "HostState changed to OFF. Empty " |
| 134 | "postcode log, keep boot cycle at " |
| 135 | << this->currentBootCycleIndex << std::endl; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | this->postCodes.clear(); |
| 140 | } |
| 141 | } |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 142 | } |
Amithash Prasad | b6616cd | 2025-05-29 15:56:51 -0700 | [diff] [blame^] | 143 | }), |
| 144 | postCodeHandlers(std::move(handlers)) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 145 | { |
| 146 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 147 | "PostCode is created"); |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 148 | fs::create_directories(postCodeListPath); |
| 149 | deserialize(postCodeListPath / CurrentBootCycleIndexName, |
| 150 | currentBootCycleIndex); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 151 | uint16_t count = 0; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 152 | deserialize(postCodeListPath / CurrentBootCycleCountName, count); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 153 | currentBootCycleCount(count); |
Manojkiran Eda | aed7b3d | 2021-06-19 09:39:30 +0530 | [diff] [blame] | 154 | maxBootCycleNum(MAX_BOOT_CYCLE_COUNT); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 155 | } |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 156 | ~PostCode() {} |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 157 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 158 | std::vector<postcode_t> getPostCodes(uint16_t index) override; |
Patrick Williams | 4e1910b | 2025-02-01 08:22:19 -0500 | [diff] [blame] | 159 | std::map<uint64_t, postcode_t> getPostCodesWithTimeStamp( |
| 160 | uint16_t index) override; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 161 | void deleteAll() override; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 162 | |
| 163 | private: |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 164 | void incrBootCycle(); |
| 165 | uint16_t getBootNum(const uint16_t index) const; |
| 166 | |
Patrick Williams | 2d74ceb | 2023-12-05 12:45:02 -0600 | [diff] [blame] | 167 | std::unique_ptr<sdbusplus::Timer> timer; |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 168 | sdbusplus::bus_t& bus; |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 169 | EventPtr& event; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 170 | int node; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 171 | std::chrono::time_point<std::chrono::steady_clock> firstPostCodeTimeSteady; |
| 172 | uint64_t firstPostCodeUsSinceEpoch; |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 173 | std::map<uint64_t, postcode_t> postCodes; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 174 | fs::path postCodeListPath; |
| 175 | uint16_t currentBootCycleIndex = 0; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 176 | sdbusplus::bus::match_t propertiesChangedSignalRaw; |
| 177 | sdbusplus::bus::match_t propertiesChangedSignalCurrentHostState; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 178 | |
| 179 | void savePostCodes(postcode_t code); |
| 180 | fs::path serialize(const fs::path& path); |
Jonathan Doman | eddf960 | 2022-11-23 09:29:41 -0800 | [diff] [blame] | 181 | bool deserialize(const fs::path& path, uint16_t& index); |
| 182 | bool deserializePostCodes(const fs::path& path, |
| 183 | std::map<uint64_t, postcode_t>& codes); |
Amithash Prasad | b6616cd | 2025-05-29 15:56:51 -0700 | [diff] [blame^] | 184 | PostCodeHandlers postCodeHandlers; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 185 | }; |