blob: 82dba46fed91e46e33c904b6fe2243c9cb6bee1d [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
18#include "iomanip"
19
20void PostCode::deleteAll()
21{
Manojkiran Edabd706d72021-12-21 05:09:47 +053022 auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix +
23 std::to_string(postcodeDataHolderObj.node));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080024 std::uintmax_t n = fs::remove_all(dir);
25 std::cerr << "clearPostCodes deleted " << n << " files in "
Manojkiran Edabd706d72021-12-21 05:09:47 +053026 << postcodeDataHolderObj.PostCodeListPathPrefix +
27 std::to_string(postcodeDataHolderObj.node)
Kumar Thangavelfd45f782020-09-01 22:59:00 +053028 << std::endl;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080029 fs::create_directories(dir);
30 postCodes.clear();
Ravi Tejab84fea42021-06-03 02:09:15 -050031 currentBootCycleIndex = 0;
32 currentBootCycleCount(0);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080033}
34
Manojkiran Eda84a4c192021-02-25 15:23:42 +053035std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +080036{
Manojkiran Eda84a4c192021-02-25 15:23:42 +053037 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080038 if (1 == index && !postCodes.empty())
39 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +053040 std::transform(postCodes.begin(), postCodes.end(),
41 std::back_inserter(codesVec),
42 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080043 }
44 else
45 {
46 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +080047
ZhikuiRen993d4dd2020-01-29 14:25:44 -080048 decltype(postCodes) codes;
49 deserializePostCodes(
50 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
Manojkiran Edade8d3a52021-12-05 12:51:07 +053051 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec),
52 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080053 }
54 return codesVec;
55}
56
Manojkiran Eda84a4c192021-02-25 15:23:42 +053057std::map<uint64_t, postcode_t>
58 PostCode::getPostCodesWithTimeStamp(uint16_t index)
ZhikuiRen993d4dd2020-01-29 14:25:44 -080059{
60 if (1 == index && !postCodes.empty())
61 {
Kuiying Wang3a044402019-02-19 15:00:11 +080062 return postCodes;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080063 }
64
65 uint16_t bootNum = getBootNum(index);
66 decltype(postCodes) codes;
67 deserializePostCodes(
68 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +080069 return codes;
70}
ZhikuiRen993d4dd2020-01-29 14:25:44 -080071
Manojkiran Eda84a4c192021-02-25 15:23:42 +053072void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +080073{
ZhikuiRen993d4dd2020-01-29 14:25:44 -080074 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
75 auto postCodeTimeSteady = std::chrono::steady_clock::now();
76 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
77 std::chrono::system_clock::now().time_since_epoch())
78 .count();
79
80 if (postCodes.empty())
81 {
82 firstPostCodeTimeSteady = postCodeTimeSteady;
83 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
84 incrBootCycle();
85 }
86 else
87 {
88 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +080089 tsUS = firstPostCodeUsSinceEpoch +
90 std::chrono::duration_cast<std::chrono::microseconds>(
91 postCodeTimeSteady - firstPostCodeTimeSteady)
92 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -080093 }
94
95 postCodes.insert(std::make_pair(tsUS, code));
Bonnie Loc1819372022-10-27 17:14:55 +080096 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE)
97 {
98 postCodes.erase(postCodes.begin());
99 }
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530100 serialize(fs::path(strPostCodeListPath));
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800101
Alan Kuo0171dd62021-04-15 10:47:32 +0800102#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800103 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800104 std::ostringstream hexCode;
105 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
106 << std::get<0>(code);
107
108 std::ostringstream timeOffsetStr;
109 // Set Fixed-Point Notation
110 timeOffsetStr << std::fixed;
111 // Set precision to 4 digits
112 timeOffsetStr << std::setprecision(4);
113 // Add double to stream
114 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
115
116 phosphor::logging::log<phosphor::logging::level::INFO>(
117 "BIOS POST Code",
118 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800119 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800120 phosphor::logging::entry(
121 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
122 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
123#endif
124
Kuiying Wang3a044402019-02-19 15:00:11 +0800125 return;
126}
127
128fs::path PostCode::serialize(const std::string& path)
129{
130 try
131 {
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800132 fs::path idxPath(path + strCurrentBootCycleIndexName);
133 std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
134 cereal::JSONOutputArchive idxArchive(osIdx);
135 idxArchive(currentBootCycleIndex);
136
137 uint16_t count = currentBootCycleCount();
138 fs::path cntPath(path + strCurrentBootCycleCountName);
139 std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
140 cereal::JSONOutputArchive cntArchive(osCnt);
141 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800142
143 std::ofstream osPostCodes(
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800144 (path + std::to_string(currentBootCycleIndex)));
Kuiying Wang3a044402019-02-19 15:00:11 +0800145 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
146 oarchivePostCodes(postCodes);
147 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500148 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800149 {
150 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
151 return "";
152 }
153 catch (const fs::filesystem_error& e)
154 {
155 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
156 return "";
157 }
158 return path;
159}
160
161bool PostCode::deserialize(const fs::path& path, uint16_t& index)
162{
163 try
164 {
165 if (fs::exists(path))
166 {
167 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
168 cereal::JSONInputArchive iarchive(is);
169 iarchive(index);
170 return true;
171 }
172 return false;
173 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500174 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800175 {
176 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
177 return false;
178 }
179 catch (const fs::filesystem_error& e)
180 {
181 return false;
182 }
183
184 return false;
185}
186
187bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530188 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800189{
190 try
191 {
192 if (fs::exists(path))
193 {
194 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
195 cereal::JSONInputArchive iarchive(is);
196 iarchive(codes);
197 return true;
198 }
199 return false;
200 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500201 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800202 {
203 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
204 return false;
205 }
206 catch (const fs::filesystem_error& e)
207 {
208 return false;
209 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800210 return false;
211}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800212
213void PostCode::incrBootCycle()
214{
215 if (currentBootCycleIndex >= maxBootCycleNum())
216 {
217 currentBootCycleIndex = 1;
218 }
219 else
220 {
221 currentBootCycleIndex++;
222 }
223 currentBootCycleCount(std::min(
224 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
225}
226
227uint16_t PostCode::getBootNum(const uint16_t index) const
228{
229 // bootNum assumes the oldest archive is boot number 1
230 // and the current boot number equals bootCycleCount
231 // map bootNum back to bootIndex that was used to archive postcode
232 uint16_t bootNum = currentBootCycleIndex;
233 if (index > bootNum) // need to wrap around
234 {
235 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
236 }
237 else
238 {
239 bootNum = currentBootCycleIndex - index + 1;
240 }
241 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530242}