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 | |
| 18 | #include "iomanip" |
| 19 | |
| 20 | void PostCode::deleteAll() |
| 21 | { |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame^] | 22 | auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix + |
| 23 | std::to_string(postcodeDataHolderObj.node)); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 24 | std::uintmax_t n = fs::remove_all(dir); |
| 25 | std::cerr << "clearPostCodes deleted " << n << " files in " |
Manojkiran Eda | bd706d7 | 2021-12-21 05:09:47 +0530 | [diff] [blame^] | 26 | << postcodeDataHolderObj.PostCodeListPathPrefix + |
| 27 | std::to_string(postcodeDataHolderObj.node) |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 28 | << std::endl; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 29 | fs::create_directories(dir); |
| 30 | postCodes.clear(); |
Ravi Teja | b84fea4 | 2021-06-03 02:09:15 -0500 | [diff] [blame] | 31 | currentBootCycleIndex = 0; |
| 32 | currentBootCycleCount(0); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 35 | std::vector<postcode_t> PostCode::getPostCodes(uint16_t index) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 36 | { |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 37 | std::vector<postcode_t> codesVec; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 38 | if (1 == index && !postCodes.empty()) |
| 39 | { |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 40 | std::transform(postCodes.begin(), postCodes.end(), |
| 41 | std::back_inserter(codesVec), |
| 42 | [](const auto& kv) { return kv.second; }); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 43 | } |
| 44 | else |
| 45 | { |
| 46 | uint16_t bootNum = getBootNum(index); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 47 | |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 48 | decltype(postCodes) codes; |
| 49 | deserializePostCodes( |
| 50 | fs::path(strPostCodeListPath + std::to_string(bootNum)), codes); |
Manojkiran Eda | de8d3a5 | 2021-12-05 12:51:07 +0530 | [diff] [blame] | 51 | std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec), |
| 52 | [](const auto& kv) { return kv.second; }); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 53 | } |
| 54 | return codesVec; |
| 55 | } |
| 56 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 57 | std::map<uint64_t, postcode_t> |
| 58 | PostCode::getPostCodesWithTimeStamp(uint16_t index) |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 59 | { |
| 60 | if (1 == index && !postCodes.empty()) |
| 61 | { |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 62 | return postCodes; |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | uint16_t bootNum = getBootNum(index); |
| 66 | decltype(postCodes) codes; |
| 67 | deserializePostCodes( |
| 68 | fs::path(strPostCodeListPath + std::to_string(bootNum)), codes); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 69 | return codes; |
| 70 | } |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 71 | |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 72 | void PostCode::savePostCodes(postcode_t code) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 73 | { |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 74 | // 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 Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 89 | tsUS = firstPostCodeUsSinceEpoch + |
| 90 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 91 | postCodeTimeSteady - firstPostCodeTimeSteady) |
| 92 | .count(); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | postCodes.insert(std::make_pair(tsUS, code)); |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 96 | serialize(fs::path(strPostCodeListPath)); |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 97 | |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 98 | #ifdef ENABLE_BIOS_POST_CODE_LOG |
Alan Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 99 | uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 100 | 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 Kuo | f5e52db | 2021-12-23 10:57:05 +0800 | [diff] [blame] | 115 | "OpenBMC.0.2.BIOSPOSTCode"), |
Alan Kuo | 0171dd6 | 2021-04-15 10:47:32 +0800 | [diff] [blame] | 116 | phosphor::logging::entry( |
| 117 | "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, |
| 118 | timeOffsetStr.str().c_str(), hexCode.str().c_str())); |
| 119 | #endif |
| 120 | |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | fs::path PostCode::serialize(const std::string& path) |
| 125 | { |
| 126 | try |
| 127 | { |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 128 | 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 Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 138 | |
| 139 | std::ofstream osPostCodes( |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 140 | (path + std::to_string(currentBootCycleIndex))); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 141 | cereal::JSONOutputArchive oarchivePostCodes(osPostCodes); |
| 142 | oarchivePostCodes(postCodes); |
| 143 | } |
Patrick Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 144 | catch (const cereal::Exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 145 | { |
| 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 | |
| 157 | bool 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 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 false; |
| 174 | } |
| 175 | catch (const fs::filesystem_error& e) |
| 176 | { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | bool PostCode::deserializePostCodes(const fs::path& path, |
Manojkiran Eda | 84a4c19 | 2021-02-25 15:23:42 +0530 | [diff] [blame] | 184 | std::map<uint64_t, postcode_t>& codes) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 185 | { |
| 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 Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 197 | catch (const cereal::Exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 198 | { |
| 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 Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
ZhikuiRen | 993d4dd | 2020-01-29 14:25:44 -0800 | [diff] [blame] | 208 | |
| 209 | void 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 | |
| 223 | uint16_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 Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 238 | } |