blob: b1ffab8d0767c5bd70a03169e411c51d66cacdc2 [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));
Kumar Thangavelfd45f782020-09-01 22:59:00 +053096 serialize(fs::path(strPostCodeListPath));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080097
Alan Kuo0171dd62021-04-15 10:47:32 +080098#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +080099 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800100 std::ostringstream hexCode;
101 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
102 << std::get<0>(code);
103
104 std::ostringstream timeOffsetStr;
105 // Set Fixed-Point Notation
106 timeOffsetStr << std::fixed;
107 // Set precision to 4 digits
108 timeOffsetStr << std::setprecision(4);
109 // Add double to stream
110 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
111
112 phosphor::logging::log<phosphor::logging::level::INFO>(
113 "BIOS POST Code",
114 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800115 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800116 phosphor::logging::entry(
117 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
118 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
119#endif
120
Kuiying Wang3a044402019-02-19 15:00:11 +0800121 return;
122}
123
124fs::path PostCode::serialize(const std::string& path)
125{
126 try
127 {
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800128 fs::path idxPath(path + strCurrentBootCycleIndexName);
129 std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
130 cereal::JSONOutputArchive idxArchive(osIdx);
131 idxArchive(currentBootCycleIndex);
132
133 uint16_t count = currentBootCycleCount();
134 fs::path cntPath(path + strCurrentBootCycleCountName);
135 std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
136 cereal::JSONOutputArchive cntArchive(osCnt);
137 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800138
139 std::ofstream osPostCodes(
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800140 (path + std::to_string(currentBootCycleIndex)));
Kuiying Wang3a044402019-02-19 15:00:11 +0800141 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
142 oarchivePostCodes(postCodes);
143 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500144 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800145 {
146 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
147 return "";
148 }
149 catch (const fs::filesystem_error& e)
150 {
151 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
152 return "";
153 }
154 return path;
155}
156
157bool PostCode::deserialize(const fs::path& path, uint16_t& index)
158{
159 try
160 {
161 if (fs::exists(path))
162 {
163 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
164 cereal::JSONInputArchive iarchive(is);
165 iarchive(index);
166 return true;
167 }
168 return false;
169 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500170 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800171 {
172 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
173 return false;
174 }
175 catch (const fs::filesystem_error& e)
176 {
177 return false;
178 }
179
180 return false;
181}
182
183bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530184 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800185{
186 try
187 {
188 if (fs::exists(path))
189 {
190 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
191 cereal::JSONInputArchive iarchive(is);
192 iarchive(codes);
193 return true;
194 }
195 return false;
196 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500197 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800198 {
199 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
200 return false;
201 }
202 catch (const fs::filesystem_error& e)
203 {
204 return false;
205 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800206 return false;
207}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800208
209void PostCode::incrBootCycle()
210{
211 if (currentBootCycleIndex >= maxBootCycleNum())
212 {
213 currentBootCycleIndex = 1;
214 }
215 else
216 {
217 currentBootCycleIndex++;
218 }
219 currentBootCycleCount(std::min(
220 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
221}
222
223uint16_t PostCode::getBootNum(const uint16_t index) const
224{
225 // bootNum assumes the oldest archive is boot number 1
226 // and the current boot number equals bootCycleCount
227 // map bootNum back to bootIndex that was used to archive postcode
228 uint16_t bootNum = currentBootCycleIndex;
229 if (index > bootNum) // need to wrap around
230 {
231 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
232 }
233 else
234 {
235 bootNum = currentBootCycleIndex - index + 1;
236 }
237 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530238}