blob: 327e29e8fe29319d128e15e5027cd58ba0cda5b6 [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 {
79 timer = std::make_unique<phosphor::Timer>(
80 event.get(), [this]() { serialize(postCodeListPath); });
81 }
82
ZhikuiRen993d4dd2020-01-29 14:25:44 -080083 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
84 auto postCodeTimeSteady = std::chrono::steady_clock::now();
85 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
86 std::chrono::system_clock::now().time_since_epoch())
87 .count();
88
89 if (postCodes.empty())
90 {
91 firstPostCodeTimeSteady = postCodeTimeSteady;
92 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
93 incrBootCycle();
94 }
95 else
96 {
97 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +080098 tsUS = firstPostCodeUsSinceEpoch +
99 std::chrono::duration_cast<std::chrono::microseconds>(
100 postCodeTimeSteady - firstPostCodeTimeSteady)
101 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800102 }
103
104 postCodes.insert(std::make_pair(tsUS, code));
Bonnie Loc1819372022-10-27 17:14:55 +0800105 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE)
106 {
107 postCodes.erase(postCodes.begin());
108 }
Bonnie Lo13cb8532022-12-15 17:00:40 +0800109
110 if (!timer->isRunning())
111 {
112 timer->start(std::chrono::microseconds(timeoutMicroSeconds));
113 }
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800114
Alan Kuo0171dd62021-04-15 10:47:32 +0800115#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800116 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800117 std::ostringstream hexCode;
118 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
119 << std::get<0>(code);
120
121 std::ostringstream timeOffsetStr;
122 // Set Fixed-Point Notation
123 timeOffsetStr << std::fixed;
124 // Set precision to 4 digits
125 timeOffsetStr << std::setprecision(4);
126 // Add double to stream
127 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
128
129 phosphor::logging::log<phosphor::logging::level::INFO>(
130 "BIOS POST Code",
131 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800132 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800133 phosphor::logging::entry(
134 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
135 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
136#endif
137
Kuiying Wang3a044402019-02-19 15:00:11 +0800138 return;
139}
140
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800141fs::path PostCode::serialize(const fs::path& path)
Kuiying Wang3a044402019-02-19 15:00:11 +0800142{
143 try
144 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800145 std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800146 cereal::BinaryOutputArchive idxArchive(osIdx);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800147 idxArchive(currentBootCycleIndex);
148
149 uint16_t count = currentBootCycleCount();
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800150 std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800151 cereal::BinaryOutputArchive cntArchive(osCnt);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800152 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800153
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800154 std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex));
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800155 cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800156 oarchivePostCodes(postCodes);
157 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500158 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800159 {
160 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
161 return "";
162 }
163 catch (const fs::filesystem_error& e)
164 {
165 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
166 return "";
167 }
168 return path;
169}
170
171bool PostCode::deserialize(const fs::path& path, uint16_t& index)
172{
173 try
174 {
175 if (fs::exists(path))
176 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800177 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800178 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800179 iarchive(index);
180 return true;
181 }
182 return false;
183 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500184 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800185 {
186 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
187 return false;
188 }
189 catch (const fs::filesystem_error& e)
190 {
191 return false;
192 }
193
194 return false;
195}
196
197bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530198 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800199{
200 try
201 {
202 if (fs::exists(path))
203 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800204 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800205 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800206 iarchive(codes);
207 return true;
208 }
209 return false;
210 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500211 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800212 {
213 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
214 return false;
215 }
216 catch (const fs::filesystem_error& e)
217 {
218 return false;
219 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800220 return false;
221}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800222
223void PostCode::incrBootCycle()
224{
225 if (currentBootCycleIndex >= maxBootCycleNum())
226 {
227 currentBootCycleIndex = 1;
228 }
229 else
230 {
231 currentBootCycleIndex++;
232 }
233 currentBootCycleCount(std::min(
234 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
235}
236
237uint16_t PostCode::getBootNum(const uint16_t index) const
238{
239 // bootNum assumes the oldest archive is boot number 1
240 // and the current boot number equals bootCycleCount
241 // map bootNum back to bootIndex that was used to archive postcode
242 uint16_t bootNum = currentBootCycleIndex;
243 if (index > bootNum) // need to wrap around
244 {
245 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
246 }
247 else
248 {
249 bootNum = currentBootCycleIndex - index + 1;
250 }
251 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530252}