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