blob: 1fcbe55c62caf9b815c69c938b775fbd4a3c2121 [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
Kumar Thangavelfd45f782020-09-01 22:59:00 +053020PostCodeDataHolder* PostCodeDataHolder::instance = 0;
21
ZhikuiRen993d4dd2020-01-29 14:25:44 -080022void PostCode::deleteAll()
23{
Kumar Thangavelfd45f782020-09-01 22:59:00 +053024 auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix +
25 std::to_string(postcodeDataHolderObj->node));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080026 std::uintmax_t n = fs::remove_all(dir);
27 std::cerr << "clearPostCodes deleted " << n << " files in "
Kumar Thangavelfd45f782020-09-01 22:59:00 +053028 << postcodeDataHolderObj->PostCodeListPathPrefix +
29 std::to_string(postcodeDataHolderObj->node)
30 << std::endl;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080031 fs::create_directories(dir);
32 postCodes.clear();
Ravi Tejab84fea42021-06-03 02:09:15 -050033 currentBootCycleIndex = 0;
34 currentBootCycleCount(0);
ZhikuiRen993d4dd2020-01-29 14:25:44 -080035}
36
Manojkiran Eda84a4c192021-02-25 15:23:42 +053037std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +080038{
Manojkiran Eda84a4c192021-02-25 15:23:42 +053039 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -080040 if (1 == index && !postCodes.empty())
41 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +053042 std::transform(postCodes.begin(), postCodes.end(),
43 std::back_inserter(codesVec),
44 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -080045 }
46 else
47 {
48 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +080049
ZhikuiRen993d4dd2020-01-29 14:25:44 -080050 decltype(postCodes) codes;
51 deserializePostCodes(
52 fs::path(strPostCodeListPath + 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;
69 deserializePostCodes(
70 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +080071 return codes;
72}
ZhikuiRen993d4dd2020-01-29 14:25:44 -080073
Manojkiran Eda84a4c192021-02-25 15:23:42 +053074void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +080075{
ZhikuiRen993d4dd2020-01-29 14:25:44 -080076 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
77 auto postCodeTimeSteady = std::chrono::steady_clock::now();
78 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
79 std::chrono::system_clock::now().time_since_epoch())
80 .count();
81
82 if (postCodes.empty())
83 {
84 firstPostCodeTimeSteady = postCodeTimeSteady;
85 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
86 incrBootCycle();
87 }
88 else
89 {
90 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +080091 tsUS = firstPostCodeUsSinceEpoch +
92 std::chrono::duration_cast<std::chrono::microseconds>(
93 postCodeTimeSteady - firstPostCodeTimeSteady)
94 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -080095 }
96
97 postCodes.insert(std::make_pair(tsUS, code));
Kumar Thangavelfd45f782020-09-01 22:59:00 +053098 serialize(fs::path(strPostCodeListPath));
ZhikuiRen993d4dd2020-01-29 14:25:44 -080099
Alan Kuo0171dd62021-04-15 10:47:32 +0800100#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800101 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800102 std::ostringstream hexCode;
103 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
104 << std::get<0>(code);
105
106 std::ostringstream timeOffsetStr;
107 // Set Fixed-Point Notation
108 timeOffsetStr << std::fixed;
109 // Set precision to 4 digits
110 timeOffsetStr << std::setprecision(4);
111 // Add double to stream
112 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
113
114 phosphor::logging::log<phosphor::logging::level::INFO>(
115 "BIOS POST Code",
116 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800117 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800118 phosphor::logging::entry(
119 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
120 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
121#endif
122
Kuiying Wang3a044402019-02-19 15:00:11 +0800123 return;
124}
125
126fs::path PostCode::serialize(const std::string& path)
127{
128 try
129 {
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800130 fs::path idxPath(path + strCurrentBootCycleIndexName);
131 std::ofstream osIdx(idxPath.c_str(), std::ios::binary);
132 cereal::JSONOutputArchive idxArchive(osIdx);
133 idxArchive(currentBootCycleIndex);
134
135 uint16_t count = currentBootCycleCount();
136 fs::path cntPath(path + strCurrentBootCycleCountName);
137 std::ofstream osCnt(cntPath.c_str(), std::ios::binary);
138 cereal::JSONOutputArchive cntArchive(osCnt);
139 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800140
141 std::ofstream osPostCodes(
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800142 (path + std::to_string(currentBootCycleIndex)));
Kuiying Wang3a044402019-02-19 15:00:11 +0800143 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
144 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 {
165 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
166 cereal::JSONInputArchive iarchive(is);
167 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 {
192 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
193 cereal::JSONInputArchive iarchive(is);
194 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}