blob: 8ab2e32a66003409f200ee454d287f1c260699dc [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
27void PostCode::deleteAll()
28{
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080029 std::uintmax_t n = fs::remove_all(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080030 std::cerr << "clearPostCodes deleted " << n << " files in "
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080031 << postCodeListPath << std::endl;
32 fs::create_directories(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080033 postCodes.clear();
Ravi Tejab84fea42021-06-03 02:09:15 -050034 currentBootCycleIndex = 0;
35 currentBootCycleCount(0);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080036}
37
Manojkiran Eda84a4c192021-02-25 15:23:42 +053038std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +080039{
Manojkiran Eda84a4c192021-02-25 15:23:42 +053040 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080041 if (1 == index && !postCodes.empty())
42 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +053043 std::transform(postCodes.begin(), postCodes.end(),
44 std::back_inserter(codesVec),
45 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080046 }
47 else
48 {
49 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +080050
ZhikuiRen993d4dd2020-01-29 14:25:44 -080051 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080052 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Manojkiran Edade8d3a52021-12-05 12:51:07 +053053 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec),
54 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080055 }
56 return codesVec;
57}
58
Manojkiran Eda84a4c192021-02-25 15:23:42 +053059std::map<uint64_t, postcode_t>
60 PostCode::getPostCodesWithTimeStamp(uint16_t index)
ZhikuiRen993d4dd2020-01-29 14:25:44 -080061{
62 if (1 == index && !postCodes.empty())
63 {
Kuiying Wang3a044402019-02-19 15:00:11 +080064 return postCodes;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080065 }
66
67 uint16_t bootNum = getBootNum(index);
68 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080069 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +080070 return codes;
71}
ZhikuiRen993d4dd2020-01-29 14:25:44 -080072
Manojkiran Eda84a4c192021-02-25 15:23:42 +053073void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +080074{
ZhikuiRen993d4dd2020-01-29 14:25:44 -080075 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
76 auto postCodeTimeSteady = std::chrono::steady_clock::now();
77 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
78 std::chrono::system_clock::now().time_since_epoch())
79 .count();
80
81 if (postCodes.empty())
82 {
83 firstPostCodeTimeSteady = postCodeTimeSteady;
84 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
85 incrBootCycle();
86 }
87 else
88 {
89 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +080090 tsUS = firstPostCodeUsSinceEpoch +
91 std::chrono::duration_cast<std::chrono::microseconds>(
92 postCodeTimeSteady - firstPostCodeTimeSteady)
93 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -080094 }
95
96 postCodes.insert(std::make_pair(tsUS, code));
Bonnie Loc1819372022-10-27 17:14:55 +080097 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE)
98 {
99 postCodes.erase(postCodes.begin());
100 }
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800101 serialize(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800102
Alan Kuo0171dd62021-04-15 10:47:32 +0800103#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800104 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800105 std::ostringstream hexCode;
106 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
107 << std::get<0>(code);
108
109 std::ostringstream timeOffsetStr;
110 // Set Fixed-Point Notation
111 timeOffsetStr << std::fixed;
112 // Set precision to 4 digits
113 timeOffsetStr << std::setprecision(4);
114 // Add double to stream
115 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
116
117 phosphor::logging::log<phosphor::logging::level::INFO>(
118 "BIOS POST Code",
119 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800120 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800121 phosphor::logging::entry(
122 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
123 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
124#endif
125
Kuiying Wang3a044402019-02-19 15:00:11 +0800126 return;
127}
128
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800129fs::path PostCode::serialize(const fs::path& path)
Kuiying Wang3a044402019-02-19 15:00:11 +0800130{
131 try
132 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800133 std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800134 cereal::BinaryOutputArchive idxArchive(osIdx);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800135 idxArchive(currentBootCycleIndex);
136
137 uint16_t count = currentBootCycleCount();
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800138 std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800139 cereal::BinaryOutputArchive cntArchive(osCnt);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800140 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800141
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800142 std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex));
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800143 cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800144 oarchivePostCodes(postCodes);
145 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500146 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800147 {
148 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
149 return "";
150 }
151 catch (const fs::filesystem_error& e)
152 {
153 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
154 return "";
155 }
156 return path;
157}
158
159bool PostCode::deserialize(const fs::path& path, uint16_t& index)
160{
161 try
162 {
163 if (fs::exists(path))
164 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800165 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800166 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800167 iarchive(index);
168 return true;
169 }
170 return false;
171 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500172 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800173 {
174 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
175 return false;
176 }
177 catch (const fs::filesystem_error& e)
178 {
179 return false;
180 }
181
182 return false;
183}
184
185bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530186 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800187{
188 try
189 {
190 if (fs::exists(path))
191 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800192 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800193 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800194 iarchive(codes);
195 return true;
196 }
197 return false;
198 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500199 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800200 {
201 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
202 return false;
203 }
204 catch (const fs::filesystem_error& e)
205 {
206 return false;
207 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800208 return false;
209}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800210
211void PostCode::incrBootCycle()
212{
213 if (currentBootCycleIndex >= maxBootCycleNum())
214 {
215 currentBootCycleIndex = 1;
216 }
217 else
218 {
219 currentBootCycleIndex++;
220 }
221 currentBootCycleCount(std::min(
222 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
223}
224
225uint16_t PostCode::getBootNum(const uint16_t index) const
226{
227 // bootNum assumes the oldest archive is boot number 1
228 // and the current boot number equals bootCycleCount
229 // map bootNum back to bootIndex that was used to archive postcode
230 uint16_t bootNum = currentBootCycleIndex;
231 if (index > bootNum) // need to wrap around
232 {
233 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
234 }
235 else
236 {
237 bootNum = currentBootCycleIndex - index + 1;
238 }
239 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530240}