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