blob: 23f0a4fe979626803290361b692b48379ac3fd17 [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>
Amithash Prasadb6616cd2025-05-29 15:56:51 -070024#include <phosphor-logging/commit.hpp>
25#include <sdbusplus/bus.hpp>
26#include <sdbusplus/exception.hpp>
Jonathan Doman8290e0f2022-11-23 15:04:17 -080027
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080028#include <iomanip>
ZhikuiRen993d4dd2020-01-29 14:25:44 -080029
Amithash Prasadb6616cd2025-05-29 15:56:51 -070030using nlohmann::json;
31
Bonnie Lo13cb8532022-12-15 17:00:40 +080032const static constexpr auto timeoutMicroSeconds = 1000000;
Amithash Prasadb6616cd2025-05-29 15:56:51 -070033/* systemd service to kick start a target. */
34constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
35constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
36constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
37
38void PostCodeEvent::raise() const
39{
40 json j = {{name, args}};
41 try
42 {
43 sdbusplus::exception::throw_via_json(j);
44 }
45 catch (sdbusplus::exception::generated_event_base& e)
46 {
47 auto path = lg2::commit(std::move(e));
48 std::cout << path.str << std::endl;
49 }
50}
51
52void from_json(const json& j, PostCodeEvent& event)
53{
54 j.at("name").get_to(event.name);
55 for (const auto& entry : j.at("arguments").items())
56 {
57 if (entry.value().is_string())
58 {
59 event.args[entry.key()] = entry.value().get<std::string>();
60 }
61 else if (entry.value().is_number_integer())
62 {
63 event.args[entry.key()] = entry.value().get<int>();
64 }
65 }
66}
67
Amithash Prasad7103f032025-07-09 18:52:44 -070068std::vector<uint8_t> decodeHexString(const std::string& hex)
69{
70 std::vector<uint8_t> out;
71 // The post-code is at least 1 byte. So, we need at
72 // least a 4 char string.
73 if (hex.size() < 4 || hex.size() % 2 != 0 || hex.substr(0, 2) != "0x")
74 {
75 throw std::runtime_error("Bad Hex String: " + hex);
76 }
77 for (size_t i = 2; i < hex.size(); i += 2)
78 {
79 std::string byteString = hex.substr(i, 2);
80 uint8_t byte = (uint8_t)std::strtol(byteString.c_str(), NULL, 16);
81 out.push_back(byte);
82 }
83 return out;
84}
85
Amithash Prasadb6616cd2025-05-29 15:56:51 -070086void from_json(const json& j, PostCodeHandler& handler)
87{
Amithash Prasad7103f032025-07-09 18:52:44 -070088 std::string primary;
89 j.at("name").get_to(handler.name);
90 j.at("description").get_to(handler.description);
91 j.at("primary").get_to(primary);
92 handler.primary = decodeHexString(primary);
Amithash Prasadb6616cd2025-05-29 15:56:51 -070093 if (j.contains("secondary"))
94 {
Amithash Prasad7103f032025-07-09 18:52:44 -070095 std::string secondary;
Amithash Prasadb6616cd2025-05-29 15:56:51 -070096 j.at("secondary").get_to(secondary);
Amithash Prasad7103f032025-07-09 18:52:44 -070097 handler.secondary = decodeHexString(secondary);
Amithash Prasadb6616cd2025-05-29 15:56:51 -070098 }
99 if (j.contains("targets"))
100 {
101 j.at("targets").get_to(handler.targets);
102 }
103 if (j.contains("event"))
104 {
105 PostCodeEvent event;
106 j.at("event").get_to(event);
107 handler.event = event;
108 }
109}
110
111const PostCodeHandler* PostCodeHandlers::find(postcode_t code)
112{
113 for (const auto& handler : handlers)
114 {
115 if (handler.primary == std::get<0>(code) &&
116 (!handler.secondary || *handler.secondary == std::get<1>(code)))
117 {
118 return &handler;
119 }
120 }
121 return nullptr;
122}
123
124void PostCodeHandlers::handle(postcode_t code)
125{
126 const PostCodeHandler* handler = find(code);
127 if (!handler)
128 {
129 return;
130 }
131 for (const auto& target : handler->targets)
132 {
133 auto bus = sdbusplus::bus::new_default();
134 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
135 SYSTEMD_INTERFACE, "StartUnit");
136 method.append(target);
137 method.append("replace");
138 bus.call_noreply(method);
139 }
140 if (handler->event)
141 {
142 (*(handler->event)).raise();
143 }
144}
145
146void PostCodeHandlers::load(const std::string& path)
147{
148 std::ifstream ifs(path);
149 handlers = json::parse(ifs).template get<std::vector<PostCodeHandler>>();
150 ifs.close();
151}
Bonnie Lo13cb8532022-12-15 17:00:40 +0800152
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800153void PostCode::deleteAll()
154{
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800155 std::uintmax_t n = fs::remove_all(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800156 std::cerr << "clearPostCodes deleted " << n << " files in "
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800157 << postCodeListPath << std::endl;
158 fs::create_directories(postCodeListPath);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800159 postCodes.clear();
Ravi Tejab84fea42021-06-03 02:09:15 -0500160 currentBootCycleIndex = 0;
161 currentBootCycleCount(0);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800162}
163
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530164std::vector<postcode_t> PostCode::getPostCodes(uint16_t index)
Kuiying Wang3a044402019-02-19 15:00:11 +0800165{
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530166 std::vector<postcode_t> codesVec;
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800167 if (1 == index && !postCodes.empty())
168 {
Manojkiran Edade8d3a52021-12-05 12:51:07 +0530169 std::transform(postCodes.begin(), postCodes.end(),
170 std::back_inserter(codesVec),
171 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800172 }
173 else
174 {
175 uint16_t bootNum = getBootNum(index);
Kuiying Wang3a044402019-02-19 15:00:11 +0800176
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800177 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800178 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Manojkiran Edade8d3a52021-12-05 12:51:07 +0530179 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec),
180 [](const auto& kv) { return kv.second; });
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800181 }
182 return codesVec;
183}
184
Patrick Williams4e1910b2025-02-01 08:22:19 -0500185std::map<uint64_t, postcode_t> PostCode::getPostCodesWithTimeStamp(
186 uint16_t index)
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800187{
188 if (1 == index && !postCodes.empty())
189 {
Kuiying Wang3a044402019-02-19 15:00:11 +0800190 return postCodes;
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800191 }
192
193 uint16_t bootNum = getBootNum(index);
194 decltype(postCodes) codes;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800195 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800196 return codes;
197}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800198
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530199void PostCode::savePostCodes(postcode_t code)
Kuiying Wang3a044402019-02-19 15:00:11 +0800200{
Bonnie Lo13cb8532022-12-15 17:00:40 +0800201 if (!timer)
202 {
Patrick Williams9c2e8712024-08-16 15:20:36 -0400203 timer = std::make_unique<sdbusplus::Timer>(event.get(), [this]() {
204 serialize(postCodeListPath);
205 });
Bonnie Lo13cb8532022-12-15 17:00:40 +0800206 }
207
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800208 // steady_clock is a monotonic clock that is guaranteed to never be adjusted
209 auto postCodeTimeSteady = std::chrono::steady_clock::now();
210 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>(
211 std::chrono::system_clock::now().time_since_epoch())
212 .count();
213
214 if (postCodes.empty())
215 {
216 firstPostCodeTimeSteady = postCodeTimeSteady;
217 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code
218 incrBootCycle();
219 }
220 else
221 {
222 // calculating tsUS so it is monotonic within the same boot
Alan Kuof5e52db2021-12-23 10:57:05 +0800223 tsUS = firstPostCodeUsSinceEpoch +
224 std::chrono::duration_cast<std::chrono::microseconds>(
225 postCodeTimeSteady - firstPostCodeTimeSteady)
226 .count();
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800227 }
228
229 postCodes.insert(std::make_pair(tsUS, code));
Bonnie Loc1819372022-10-27 17:14:55 +0800230 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE)
231 {
232 postCodes.erase(postCodes.begin());
233 }
Bonnie Lo13cb8532022-12-15 17:00:40 +0800234
235 if (!timer->isRunning())
236 {
237 timer->start(std::chrono::microseconds(timeoutMicroSeconds));
238 }
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800239
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800240 if (strlen(POSTCODE_DISPLAY_PATH) > 0)
241 {
Patrick Williams9c2e8712024-08-16 15:20:36 -0400242 std::string postCodeDisplayPath =
243 POSTCODE_DISPLAY_PATH + std::to_string(node);
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800244
245 std::ofstream postCodeDisplayFile(postCodeDisplayPath);
Potin Lai06b1dbe2024-09-16 15:09:06 +0800246 postCodeDisplayFile << "0x" << std::setfill('0') << std::hex;
247 for (const auto& byte : std::get<0>(code))
248 {
249 postCodeDisplayFile << std::setw(2) << static_cast<int>(byte);
250 }
Delphine CC Chiua4c19b02023-03-01 13:15:45 +0800251 postCodeDisplayFile.close();
252 }
253
Alan Kuo0171dd62021-04-15 10:47:32 +0800254#ifdef ENABLE_BIOS_POST_CODE_LOG
Alan Kuof5e52db2021-12-23 10:57:05 +0800255 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch;
Alan Kuo0171dd62021-04-15 10:47:32 +0800256 std::ostringstream hexCode;
Potin Lai06b1dbe2024-09-16 15:09:06 +0800257 hexCode << "0x" << std::setfill('0') << std::hex;
258 for (const auto& byte : std::get<0>(code))
259 {
260 hexCode << std::setw(2) << static_cast<int>(byte);
261 }
Alan Kuo0171dd62021-04-15 10:47:32 +0800262
263 std::ostringstream timeOffsetStr;
264 // Set Fixed-Point Notation
265 timeOffsetStr << std::fixed;
266 // Set precision to 4 digits
267 timeOffsetStr << std::setprecision(4);
268 // Add double to stream
269 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
270
271 phosphor::logging::log<phosphor::logging::level::INFO>(
272 "BIOS POST Code",
273 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s",
Alan Kuof5e52db2021-12-23 10:57:05 +0800274 "OpenBMC.0.2.BIOSPOSTCode"),
Alan Kuo0171dd62021-04-15 10:47:32 +0800275 phosphor::logging::entry(
276 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex,
277 timeOffsetStr.str().c_str(), hexCode.str().c_str()));
278#endif
Amithash Prasadb6616cd2025-05-29 15:56:51 -0700279 postCodeHandlers.handle(code);
Alan Kuo0171dd62021-04-15 10:47:32 +0800280
Kuiying Wang3a044402019-02-19 15:00:11 +0800281 return;
282}
283
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800284fs::path PostCode::serialize(const fs::path& path)
Kuiying Wang3a044402019-02-19 15:00:11 +0800285{
286 try
287 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800288 std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800289 cereal::BinaryOutputArchive idxArchive(osIdx);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800290 idxArchive(currentBootCycleIndex);
291
292 uint16_t count = currentBootCycleCount();
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800293 std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800294 cereal::BinaryOutputArchive cntArchive(osCnt);
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800295 cntArchive(count);
Kuiying Wang3a044402019-02-19 15:00:11 +0800296
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800297 std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex));
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800298 cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes);
Kuiying Wang3a044402019-02-19 15:00:11 +0800299 oarchivePostCodes(postCodes);
300 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500301 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800302 {
303 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
304 return "";
305 }
306 catch (const fs::filesystem_error& e)
307 {
308 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
309 return "";
310 }
311 return path;
312}
313
314bool PostCode::deserialize(const fs::path& path, uint16_t& index)
315{
316 try
317 {
318 if (fs::exists(path))
319 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800320 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800321 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800322 iarchive(index);
323 return true;
324 }
325 return false;
326 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500327 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800328 {
329 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
330 return false;
331 }
332 catch (const fs::filesystem_error& e)
333 {
334 return false;
335 }
336
337 return false;
338}
339
340bool PostCode::deserializePostCodes(const fs::path& path,
Manojkiran Eda84a4c192021-02-25 15:23:42 +0530341 std::map<uint64_t, postcode_t>& codes)
Kuiying Wang3a044402019-02-19 15:00:11 +0800342{
343 try
344 {
345 if (fs::exists(path))
346 {
Jonathan Domanc7fed5c2022-11-23 11:14:42 -0800347 std::ifstream is(path, std::ios::in | std::ios::binary);
Jonathan Doman8290e0f2022-11-23 15:04:17 -0800348 cereal::BinaryInputArchive iarchive(is);
Kuiying Wang3a044402019-02-19 15:00:11 +0800349 iarchive(codes);
350 return true;
351 }
352 return false;
353 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -0500354 catch (const cereal::Exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +0800355 {
356 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
357 return false;
358 }
359 catch (const fs::filesystem_error& e)
360 {
361 return false;
362 }
Kuiying Wang3a044402019-02-19 15:00:11 +0800363 return false;
364}
ZhikuiRen993d4dd2020-01-29 14:25:44 -0800365
366void PostCode::incrBootCycle()
367{
368 if (currentBootCycleIndex >= maxBootCycleNum())
369 {
370 currentBootCycleIndex = 1;
371 }
372 else
373 {
374 currentBootCycleIndex++;
375 }
376 currentBootCycleCount(std::min(
377 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1)));
378}
379
380uint16_t PostCode::getBootNum(const uint16_t index) const
381{
382 // bootNum assumes the oldest archive is boot number 1
383 // and the current boot number equals bootCycleCount
384 // map bootNum back to bootIndex that was used to archive postcode
385 uint16_t bootNum = currentBootCycleIndex;
386 if (index > bootNum) // need to wrap around
387 {
388 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1;
389 }
390 else
391 {
392 bootNum = currentBootCycleIndex - index + 1;
393 }
394 return bootNum;
Kumar Thangavelfd45f782020-09-01 22:59:00 +0530395}