blob: 737e0e4ff7fe07a9a38023dbcb66edaa87ab479f [file] [log] [blame]
Kuiying Wang3a044402019-02-19 15:00:11 +08001/*
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 Eda84855ab2021-12-05 11:09:02 +053017#include <config.h>
Kuiying Wang3a044402019-02-19 15:00:11 +080018#include <fcntl.h>
19#include <unistd.h>
20
Kuiying Wang3a044402019-02-19 15:00:11 +080021#include <phosphor-logging/elog-errors.hpp>
Bonnie Lo13cb8532022-12-15 17:00:40 +080022#include <sdbusplus/timer.hpp>
ZhikuiRen993d4dd2020-01-29 14:25:44 -080023#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
Kuiying Wang3a044402019-02-19 15:00:11 +080024#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 Domaneddf9602022-11-23 09:29:41 -080028#include <chrono>
29#include <filesystem>
30#include <fstream>
31#include <iostream>
32
33const static constexpr char* CurrentBootCycleCountName =
ZhikuiRen993d4dd2020-01-29 14:25:44 -080034 "CurrentBootCycleCount";
Jonathan Domaneddf9602022-11-23 09:29:41 -080035const static constexpr char* CurrentBootCycleIndexName =
Kuiying Wang3a044402019-02-19 15:00:11 +080036 "CurrentBootCycleIndex";
Kumar Thangavelfd45f782020-09-01 22:59:00 +053037
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080038const static constexpr char* PostCodePath =
39 "/xyz/openbmc_project/state/boot/raw";
40const static constexpr char* PostCodeListPathPrefix =
41 "/var/lib/phosphor-post-code-manager/host";
42const static constexpr char* HostStatePathPrefix =
43 "/xyz/openbmc_project/state/host";
Kuiying Wang3a044402019-02-19 15:00:11 +080044
45struct EventDeleter
46{
Jonathan Domaneddf9602022-11-23 09:29:41 -080047 void operator()(sd_event* event) const
Kuiying Wang3a044402019-02-19 15:00:11 +080048 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +053049 sd_event_unref(event);
Kuiying Wang3a044402019-02-19 15:00:11 +080050 }
51};
52using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
Manojkiran Eda84a4c192021-02-25 15:23:42 +053053using primarycode_t = uint64_t;
54using secondarycode_t = std::vector<uint8_t>;
55using postcode_t = std::tuple<primarycode_t, secondarycode_t>;
Manojkiran Eda410ba292021-12-05 10:55:56 +053056namespace fs = std::filesystem;
Kuiying Wang3a044402019-02-19 15:00:11 +080057namespace StateServer = sdbusplus::xyz::openbmc_project::State::server;
58
59using post_code =
60 sdbusplus::xyz::openbmc_project::State::Boot::server::PostCode;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080061using delete_all =
62 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll;
Kuiying Wang3a044402019-02-19 15:00:11 +080063
ZhikuiRen993d4dd2020-01-29 14:25:44 -080064struct PostCode : sdbusplus::server::object_t<post_code, delete_all>
Kuiying Wang3a044402019-02-19 15:00:11 +080065{
Bonnie Lo13cb8532022-12-15 17:00:40 +080066 PostCode(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
67 int nodeIndex) :
68 sdbusplus::server::object_t<post_code, delete_all>(bus, path),
69 bus(bus), event(event), node(nodeIndex),
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080070 postCodeListPath(PostCodeListPathPrefix + std::to_string(node)),
Kuiying Wang3a044402019-02-19 15:00:11 +080071 propertiesChangedSignalRaw(
72 bus,
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080073 sdbusplus::bus::match::rules::propertiesChanged(
74 PostCodePath + std::to_string(node),
75 "xyz.openbmc_project.State.Boot.Raw"),
Jonathan Domaneddf9602022-11-23 09:29:41 -080076 [this](sdbusplus::message_t& msg) {
Patrick Williamse7e197b2023-05-10 07:50:39 -050077 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(std::get<postcode_t>(valPropMap->second));
85 }
Patrick Williams608a3922023-10-20 11:18:57 -050086 }),
87 propertiesChangedSignalCurrentHostState(
88 bus,
89 sdbusplus::bus::match::rules::propertiesChanged(
90 HostStatePathPrefix + std::to_string(node),
91 "xyz.openbmc_project.State.Host"),
92 [this](sdbusplus::message_t& msg) {
Patrick Williamse7e197b2023-05-10 07:50:39 -050093 std::string intfName;
94 std::map<std::string, std::variant<std::string>> msgData;
95 msg.read(intfName, msgData);
96 // Check if it was the Value property that changed.
97 auto valPropMap = msgData.find("CurrentHostState");
98 if (valPropMap != msgData.end())
99 {
100 StateServer::Host::HostState currentHostState =
101 StateServer::Host::convertHostStateFromString(
102 std::get<std::string>(valPropMap->second));
103 if (currentHostState == StateServer::Host::HostState::Off)
104 {
105 if (this->postCodes.empty())
Kuiying Wang3a044402019-02-19 15:00:11 +0800106 {
Patrick Williamse7e197b2023-05-10 07:50:39 -0500107 std::cerr << "HostState changed to OFF. Empty "
108 "postcode log, keep boot cycle at "
109 << this->currentBootCycleIndex << std::endl;
Kuiying Wang3a044402019-02-19 15:00:11 +0800110 }
Patrick Williamse7e197b2023-05-10 07:50:39 -0500111 else
112 {
113 this->postCodes.clear();
114 }
115 }
116 }
Patrick Williams608a3922023-10-20 11:18:57 -0500117 })
Kuiying Wang3a044402019-02-19 15:00:11 +0800118 {
119 phosphor::logging::log<phosphor::logging::level::INFO>(
120 "PostCode is created");
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800121 fs::create_directories(postCodeListPath);
122 deserialize(postCodeListPath / CurrentBootCycleIndexName,
123 currentBootCycleIndex);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800124 uint16_t count = 0;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800125 deserialize(postCodeListPath / CurrentBootCycleCountName, count);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800126 currentBootCycleCount(count);
Manojkiran Edaaed7b3d2021-06-19 09:39:30 +0530127 maxBootCycleNum(MAX_BOOT_CYCLE_COUNT);
Kuiying Wang3a044402019-02-19 15:00:11 +0800128 }
Jonathan Domaneddf9602022-11-23 09:29:41 -0800129 ~PostCode() {}
Kuiying Wang3a044402019-02-19 15:00:11 +0800130
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530131 std::vector<postcode_t> getPostCodes(uint16_t index) override;
132 std::map<uint64_t, postcode_t>
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800133 getPostCodesWithTimeStamp(uint16_t index) override;
134 void deleteAll() override;
Kuiying Wang3a044402019-02-19 15:00:11 +0800135
136 private:
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800137 void incrBootCycle();
138 uint16_t getBootNum(const uint16_t index) const;
139
Bonnie Lo13cb8532022-12-15 17:00:40 +0800140 std::unique_ptr<phosphor::Timer> timer;
Jonathan Domaneddf9602022-11-23 09:29:41 -0800141 sdbusplus::bus_t& bus;
Bonnie Lo13cb8532022-12-15 17:00:40 +0800142 EventPtr& event;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800143 int node;
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800144 std::chrono::time_point<std::chrono::steady_clock> firstPostCodeTimeSteady;
145 uint64_t firstPostCodeUsSinceEpoch;
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530146 std::map<uint64_t, postcode_t> postCodes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800147 fs::path postCodeListPath;
148 uint16_t currentBootCycleIndex = 0;
Kuiying Wang3a044402019-02-19 15:00:11 +0800149 sdbusplus::bus::match_t propertiesChangedSignalRaw;
150 sdbusplus::bus::match_t propertiesChangedSignalCurrentHostState;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800151
152 void savePostCodes(postcode_t code);
153 fs::path serialize(const fs::path& path);
Jonathan Domaneddf9602022-11-23 09:29:41 -0800154 bool deserialize(const fs::path& path, uint16_t& index);
155 bool deserializePostCodes(const fs::path& path,
156 std::map<uint64_t, postcode_t>& codes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800157};