Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 1 | /* |
| 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" |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 17 | |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 18 | #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 Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 25 | #include <iomanip> |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 26 | |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 27 | const static constexpr auto timeoutMicroSeconds = 1000000; |
| 28 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 29 | void PostCode::deleteAll() |
| 30 | { |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 31 | std::uintmax_t n = fs::remove_all(postCodeListPath); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 32 | std::cerr << "clearPostCodes deleted " << n << " files in " |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 33 | << postCodeListPath << std::endl; |
| 34 | fs::create_directories(postCodeListPath); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 35 | postCodes.clear(); |
Ravi Teja | b84fea4 | 2021-06-03 02:09:15 -0500 | [diff] [blame] | 36 | currentBootCycleIndex = 0; |
| 37 | currentBootCycleCount(0); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 40 | std::vector<postcode_t> PostCode::getPostCodes(uint16_t index) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 41 | { |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 42 | std::vector<postcode_t> codesVec; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 43 | if (1 == index && !postCodes.empty()) |
| 44 | { |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 45 | std::transform(postCodes.begin(), postCodes.end(), |
| 46 | std::back_inserter(codesVec), |
| 47 | [](const auto& kv) { return kv.second; }); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 48 | } |
| 49 | else |
| 50 | { |
| 51 | uint16_t bootNum = getBootNum(index); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 52 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 53 | decltype(postCodes) codes; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 54 | deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes); |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 55 | std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec), |
| 56 | [](const auto& kv) { return kv.second; }); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 57 | } |
| 58 | return codesVec; |
| 59 | } |
| 60 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 61 | std::map<uint64_t, postcode_t> |
| 62 | PostCode::getPostCodesWithTimeStamp(uint16_t index) |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 63 | { |
| 64 | if (1 == index && !postCodes.empty()) |
| 65 | { |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 66 | return postCodes; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | uint16_t bootNum = getBootNum(index); |
| 70 | decltype(postCodes) codes; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 71 | deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 72 | return codes; |
| 73 | } |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 74 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 75 | void PostCode::savePostCodes(postcode_t code) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 76 | { |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 77 | if (!timer) |
| 78 | { |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame^] | 79 | timer = std::make_unique<sdbusplus::Timer>(event.get(), [this]() { |
| 80 | serialize(postCodeListPath); |
| 81 | }); |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 82 | } |
| 83 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 84 | // steady_clock is a monotonic clock that is guaranteed to never be adjusted |
| 85 | auto postCodeTimeSteady = std::chrono::steady_clock::now(); |
| 86 | uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>( |
| 87 | std::chrono::system_clock::now().time_since_epoch()) |
| 88 | .count(); |
| 89 | |
| 90 | if (postCodes.empty()) |
| 91 | { |
| 92 | firstPostCodeTimeSteady = postCodeTimeSteady; |
| 93 | firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code |
| 94 | incrBootCycle(); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | // calculating tsUS so it is monotonic within the same boot |
Alan Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 99 | tsUS = firstPostCodeUsSinceEpoch + |
| 100 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 101 | postCodeTimeSteady - firstPostCodeTimeSteady) |
| 102 | .count(); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | postCodes.insert(std::make_pair(tsUS, code)); |
Bonnie Lo | c181937 | 2022-10-27 17:14:55 +0800 | [diff] [blame] | 106 | if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE) |
| 107 | { |
| 108 | postCodes.erase(postCodes.begin()); |
| 109 | } |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 110 | |
| 111 | if (!timer->isRunning()) |
| 112 | { |
| 113 | timer->start(std::chrono::microseconds(timeoutMicroSeconds)); |
| 114 | } |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 115 | |
Delphine CC Chiu | a4c19b0 | 2023-03-01 13:15:45 +0800 | [diff] [blame] | 116 | if (strlen(POSTCODE_DISPLAY_PATH) > 0) |
| 117 | { |
Patrick Williams | 9c2e871 | 2024-08-16 15:20:36 -0400 | [diff] [blame^] | 118 | std::string postCodeDisplayPath = |
| 119 | POSTCODE_DISPLAY_PATH + std::to_string(node); |
Delphine CC Chiu | a4c19b0 | 2023-03-01 13:15:45 +0800 | [diff] [blame] | 120 | |
| 121 | std::ofstream postCodeDisplayFile(postCodeDisplayPath); |
| 122 | postCodeDisplayFile << "0x" << std::setfill('0') << std::setw(2) |
| 123 | << std::hex << std::get<0>(code); |
| 124 | postCodeDisplayFile.close(); |
| 125 | } |
| 126 | |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 127 | #ifdef ENABLE_BIOS_POST_CODE_LOG |
Alan Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 128 | uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 129 | std::ostringstream hexCode; |
| 130 | hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex |
| 131 | << std::get<0>(code); |
| 132 | |
| 133 | std::ostringstream timeOffsetStr; |
| 134 | // Set Fixed-Point Notation |
| 135 | timeOffsetStr << std::fixed; |
| 136 | // Set precision to 4 digits |
| 137 | timeOffsetStr << std::setprecision(4); |
| 138 | // Add double to stream |
| 139 | timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000; |
| 140 | |
| 141 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 142 | "BIOS POST Code", |
| 143 | phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", |
Alan Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 144 | "OpenBMC.0.2.BIOSPOSTCode"), |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 145 | phosphor::logging::entry( |
| 146 | "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, |
| 147 | timeOffsetStr.str().c_str(), hexCode.str().c_str())); |
| 148 | #endif |
| 149 | |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 153 | fs::path PostCode::serialize(const fs::path& path) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 154 | { |
| 155 | try |
| 156 | { |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 157 | std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary); |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 158 | cereal::BinaryOutputArchive idxArchive(osIdx); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 159 | idxArchive(currentBootCycleIndex); |
| 160 | |
| 161 | uint16_t count = currentBootCycleCount(); |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 162 | std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary); |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 163 | cereal::BinaryOutputArchive cntArchive(osCnt); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 164 | cntArchive(count); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 165 | |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 166 | std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex)); |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 167 | cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 168 | oarchivePostCodes(postCodes); |
| 169 | } |
Patrick Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 170 | catch (const cereal::Exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 171 | { |
| 172 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 173 | return ""; |
| 174 | } |
| 175 | catch (const fs::filesystem_error& e) |
| 176 | { |
| 177 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 178 | return ""; |
| 179 | } |
| 180 | return path; |
| 181 | } |
| 182 | |
| 183 | bool PostCode::deserialize(const fs::path& path, uint16_t& index) |
| 184 | { |
| 185 | try |
| 186 | { |
| 187 | if (fs::exists(path)) |
| 188 | { |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 189 | std::ifstream is(path, std::ios::in | std::ios::binary); |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 190 | cereal::BinaryInputArchive iarchive(is); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 191 | iarchive(index); |
| 192 | return true; |
| 193 | } |
| 194 | return false; |
| 195 | } |
Patrick Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 196 | catch (const cereal::Exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 197 | { |
| 198 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 199 | return false; |
| 200 | } |
| 201 | catch (const fs::filesystem_error& e) |
| 202 | { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | bool PostCode::deserializePostCodes(const fs::path& path, |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 210 | std::map<uint64_t, postcode_t>& codes) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 211 | { |
| 212 | try |
| 213 | { |
| 214 | if (fs::exists(path)) |
| 215 | { |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 216 | std::ifstream is(path, std::ios::in | std::ios::binary); |
Jonathan Doman | 8290e0f | 2022-11-23 15:04:17 -0800 | [diff] [blame] | 217 | cereal::BinaryInputArchive iarchive(is); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 218 | iarchive(codes); |
| 219 | return true; |
| 220 | } |
| 221 | return false; |
| 222 | } |
Patrick Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 223 | catch (const cereal::Exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 224 | { |
| 225 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 226 | return false; |
| 227 | } |
| 228 | catch (const fs::filesystem_error& e) |
| 229 | { |
| 230 | return false; |
| 231 | } |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 232 | return false; |
| 233 | } |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 234 | |
| 235 | void PostCode::incrBootCycle() |
| 236 | { |
| 237 | if (currentBootCycleIndex >= maxBootCycleNum()) |
| 238 | { |
| 239 | currentBootCycleIndex = 1; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | currentBootCycleIndex++; |
| 244 | } |
| 245 | currentBootCycleCount(std::min( |
| 246 | maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1))); |
| 247 | } |
| 248 | |
| 249 | uint16_t PostCode::getBootNum(const uint16_t index) const |
| 250 | { |
| 251 | // bootNum assumes the oldest archive is boot number 1 |
| 252 | // and the current boot number equals bootCycleCount |
| 253 | // map bootNum back to bootIndex that was used to archive postcode |
| 254 | uint16_t bootNum = currentBootCycleIndex; |
| 255 | if (index > bootNum) // need to wrap around |
| 256 | { |
| 257 | bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | bootNum = currentBootCycleIndex - index + 1; |
| 262 | } |
| 263 | return bootNum; |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 264 | } |