blob: 6473562d5cd562593ccb71eee22c174962551bb7 [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#include "post_code.hpp"
ZhikuiRen993d4dd2020-01-29 14:25:44 -080017
Jonathan Doman8290e0f2022-11-23 15:04:17 -080018#include <cereal/access.hpp>
19#include <cereal/archives/binary.hpp>
20#include <cereal/cereal.hpp>
21#include <cereal/types/map.hpp>
22#include <cereal/types/tuple.hpp>
23#include <cereal/types/vector.hpp>
24
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080025#include <iomanip>
ZhikuiRen993d4dd2020-01-29 14:25:44 -080026
Bonnie Lo13cb8532022-12-15 17:00:40 +080027const static constexpr auto timeoutMicroSeconds = 1000000;
28
ZhikuiRen993d4dd2020-01-29 14:25:44 -080029void PostCode::deleteAll()
30{
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080031 std::uintmax_t n = fs::remove_all(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080032 std::cerr << "clearPostCodes deleted " << n << " files in "
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080033 << postCodeListPath << std::endl;
34 fs::create_directories(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080035 postCodes.clear();
Ravi Tejab84fea42021-06-03 02:09:15 -050036 currentBootCycleIndex = 0;
37 currentBootCycleCount(0);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080038}
39
Manojkiran Eda84a4c192021-02-25 15:23:42 +053040std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +080041{
Manojkiran Eda84a4c192021-02-25 15:23:42 +053042 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080043 if (1 == index && !postCodes.empty())
44 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +053045 std::transform(postCodes.begin(), postCodes.end(),
46 std::back_inserter(codesVec),
47 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080048 }
49 else
50 {
51 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +080052
ZhikuiRen993d4dd2020-01-29 14:25:44 -080053 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080054 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Manojkiran Edade8d3a52021-12-05 12:51:07 +053055 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec),
56 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080057 }
58 return codesVec;
59}
60
Manojkiran Eda84a4c192021-02-25 15:23:42 +053061std::map<uint64_t, postcode_t>
62 PostCode::getPostCodesWithTimeStamp(uint16_t index)
ZhikuiRen993d4dd2020-01-29 14:25:44 -080063{
64 if (1 == index && !postCodes.empty())
65 {
Kuiying Wang3a044402019-02-19 15:00:11 +080066 return postCodes;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080067 }
68
69 uint16_t bootNum = getBootNum(index);
70 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080071 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +080072 return codes;
73}
ZhikuiRen993d4dd2020-01-29 14:25:44 -080074
Manojkiran Eda84a4c192021-02-25 15:23:42 +053075void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +080076{
Bonnie Lo13cb8532022-12-15 17:00:40 +080077 if (!timer)
78 {
Patrick Williams9c2e8712024-08-16 15:20:36 -040079 timer = std::make_unique<sdbusplus::Timer>(event.get(), [this]() {
80 serialize(postCodeListPath);
81 });
Bonnie Lo13cb8532022-12-15 17:00:40 +080082 }
83
ZhikuiRen993d4dd2020-01-29 14:25:44 -080084 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
85 auto postCodeTimeSteady = std::chrono::steady_clock::now();
86 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
87 std::chrono::system_clock::now().time_since_epoch())
88 .count();
89
90 if (postCodes.empty())
91 {
92 firstPostCodeTimeSteady = postCodeTimeSteady;
93 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
94 incrBootCycle();
95 }
96 else
97 {
98 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +080099 tsUS = firstPostCodeUsSinceEpoch +
100 std::chrono::duration_cast<std::chrono::microseconds>(
101 postCodeTimeSteady - firstPostCodeTimeSteady)
102 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800103 }
104
105 postCodes.insert(std::make_pair(tsUS, code));
Bonnie Loc1819372022-10-27 17:14:55 +0800106 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE)
107 {
108 postCodes.erase(postCodes.begin());
109 }
Bonnie Lo13cb8532022-12-15 17:00:40 +0800110
111 if (!timer->isRunning())
112 {
113 timer->start(std::chrono::microseconds(timeoutMicroSeconds));
114 }
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800115
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800116 if (strlen(POSTCODE_DISPLAY_PATH) > 0)
117 {
Patrick Williams9c2e8712024-08-16 15:20:36 -0400118 std::string postCodeDisplayPath =
119 POSTCODE_DISPLAY_PATH + std::to_string(node);
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800120
121 std::ofstream postCodeDisplayFile(postCodeDisplayPath);
Potin Lai06b1dbe2024-09-16 15:09:06 +0800122 postCodeDisplayFile << "0x" << std::setfill('0') << std::hex;
123 for (const auto& byte : std::get<0>(code))
124 {
125 postCodeDisplayFile << std::setw(2) << static_cast<int>(byte);
126 }
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800127 postCodeDisplayFile.close();
128 }
129
Alan Kuo0171dd62021-04-15 10:47:32 +0800130#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800131 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800132 std::ostringstream hexCode;
Potin Lai06b1dbe2024-09-16 15:09:06 +0800133 hexCode << "0x" << std::setfill('0') << std::hex;
134 for (const auto& byte : std::get<0>(code))
135 {
136 hexCode << std::setw(2) << static_cast<int>(byte);
137 }
Alan Kuo0171dd62021-04-15 10:47:32 +0800138
139 std::ostringstream timeOffsetStr;
140 // Set Fixed-Point Notation
141 timeOffsetStr << std::fixed;
142 // Set precision to 4 digits
143 timeOffsetStr << std::setprecision(4);
144 // Add double to stream
145 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
146
147 phosphor::logging::log<phosphor::logging::level::INFO>(
148 "BIOS POST Code",
149 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800150 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800151 phosphor::logging::entry(
152 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
153 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
154#endif
155
Kuiying Wang3a044402019-02-19 15:00:11 +0800156 return;
157}
158
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800159fs::path PostCode::serialize(const fs::path& path)
Kuiying Wang3a044402019-02-19 15:00:11 +0800160{
161 try
162 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800163 std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800164 cereal::BinaryOutputArchive idxArchive(osIdx);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800165 idxArchive(currentBootCycleIndex);
166
167 uint16_t count = currentBootCycleCount();
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800168 std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800169 cereal::BinaryOutputArchive cntArchive(osCnt);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800170 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800171
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800172 std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex));
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800173 cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800174 oarchivePostCodes(postCodes);
175 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500176 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800177 {
178 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
179 return "";
180 }
181 catch (const fs::filesystem_error& e)
182 {
183 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
184 return "";
185 }
186 return path;
187}
188
189bool PostCode::deserialize(const fs::path& path, uint16_t& index)
190{
191 try
192 {
193 if (fs::exists(path))
194 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800195 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800196 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800197 iarchive(index);
198 return true;
199 }
200 return false;
201 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500202 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800203 {
204 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
205 return false;
206 }
207 catch (const fs::filesystem_error& e)
208 {
209 return false;
210 }
211
212 return false;
213}
214
215bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530216 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800217{
218 try
219 {
220 if (fs::exists(path))
221 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800222 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800223 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800224 iarchive(codes);
225 return true;
226 }
227 return false;
228 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500229 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800230 {
231 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
232 return false;
233 }
234 catch (const fs::filesystem_error& e)
235 {
236 return false;
237 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800238 return false;
239}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800240
241void PostCode::incrBootCycle()
242{
243 if (currentBootCycleIndex >= maxBootCycleNum())
244 {
245 currentBootCycleIndex = 1;
246 }
247 else
248 {
249 currentBootCycleIndex++;
250 }
251 currentBootCycleCount(std::min(
252 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
253}
254
255uint16_t PostCode::getBootNum(const uint16_t index) const
256{
257 // bootNum assumes the oldest archive is boot number 1
258 // and the current boot number equals bootCycleCount
259 // map bootNum back to bootIndex that was used to archive postcode
260 uint16_t bootNum = currentBootCycleIndex;
261 if (index > bootNum) // need to wrap around
262 {
263 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
264 }
265 else
266 {
267 bootNum = currentBootCycleIndex - index + 1;
268 }
269 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530270}