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" 17 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 25 #include <iomanip> 26 27 void PostCode::deleteAll() 28 { 29 std::uintmax_t n = fs::remove_all(postCodeListPath); 30 std::cerr << "clearPostCodes deleted " << n << " files in " 31 << postCodeListPath << std::endl; 32 fs::create_directories(postCodeListPath); 33 postCodes.clear(); 34 currentBootCycleIndex = 0; 35 currentBootCycleCount(0); 36 } 37 38 std::vector<postcode_t> PostCode::getPostCodes(uint16_t index) 39 { 40 std::vector<postcode_t> codesVec; 41 if (1 == index && !postCodes.empty()) 42 { 43 std::transform(postCodes.begin(), postCodes.end(), 44 std::back_inserter(codesVec), 45 [](const auto& kv) { return kv.second; }); 46 } 47 else 48 { 49 uint16_t bootNum = getBootNum(index); 50 51 decltype(postCodes) codes; 52 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes); 53 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec), 54 [](const auto& kv) { return kv.second; }); 55 } 56 return codesVec; 57 } 58 59 std::map<uint64_t, postcode_t> 60 PostCode::getPostCodesWithTimeStamp(uint16_t index) 61 { 62 if (1 == index && !postCodes.empty()) 63 { 64 return postCodes; 65 } 66 67 uint16_t bootNum = getBootNum(index); 68 decltype(postCodes) codes; 69 deserializePostCodes(postCodeListPath / std::to_string(bootNum), codes); 70 return codes; 71 } 72 73 void PostCode::savePostCodes(postcode_t code) 74 { 75 // steady_clock is a monotonic clock that is guaranteed to never be adjusted 76 auto postCodeTimeSteady = std::chrono::steady_clock::now(); 77 uint64_t tsUS = std::chrono::duration_cast<std::chrono::microseconds>( 78 std::chrono::system_clock::now().time_since_epoch()) 79 .count(); 80 81 if (postCodes.empty()) 82 { 83 firstPostCodeTimeSteady = postCodeTimeSteady; 84 firstPostCodeUsSinceEpoch = tsUS; // uS since epoch for 1st post code 85 incrBootCycle(); 86 } 87 else 88 { 89 // calculating tsUS so it is monotonic within the same boot 90 tsUS = firstPostCodeUsSinceEpoch + 91 std::chrono::duration_cast<std::chrono::microseconds>( 92 postCodeTimeSteady - firstPostCodeTimeSteady) 93 .count(); 94 } 95 96 postCodes.insert(std::make_pair(tsUS, code)); 97 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE) 98 { 99 postCodes.erase(postCodes.begin()); 100 } 101 serialize(postCodeListPath); 102 103 #ifdef ENABLE_BIOS_POST_CODE_LOG 104 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; 105 std::ostringstream hexCode; 106 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex 107 << std::get<0>(code); 108 109 std::ostringstream timeOffsetStr; 110 // Set Fixed-Point Notation 111 timeOffsetStr << std::fixed; 112 // Set precision to 4 digits 113 timeOffsetStr << std::setprecision(4); 114 // Add double to stream 115 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000; 116 117 phosphor::logging::log<phosphor::logging::level::INFO>( 118 "BIOS POST Code", 119 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", 120 "OpenBMC.0.2.BIOSPOSTCode"), 121 phosphor::logging::entry( 122 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, 123 timeOffsetStr.str().c_str(), hexCode.str().c_str())); 124 #endif 125 126 return; 127 } 128 129 fs::path PostCode::serialize(const fs::path& path) 130 { 131 try 132 { 133 std::ofstream osIdx(path / CurrentBootCycleIndexName, std::ios::binary); 134 cereal::BinaryOutputArchive idxArchive(osIdx); 135 idxArchive(currentBootCycleIndex); 136 137 uint16_t count = currentBootCycleCount(); 138 std::ofstream osCnt(path / CurrentBootCycleCountName, std::ios::binary); 139 cereal::BinaryOutputArchive cntArchive(osCnt); 140 cntArchive(count); 141 142 std::ofstream osPostCodes(path / std::to_string(currentBootCycleIndex)); 143 cereal::BinaryOutputArchive oarchivePostCodes(osPostCodes); 144 oarchivePostCodes(postCodes); 145 } 146 catch (const cereal::Exception& e) 147 { 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 159 bool PostCode::deserialize(const fs::path& path, uint16_t& index) 160 { 161 try 162 { 163 if (fs::exists(path)) 164 { 165 std::ifstream is(path, std::ios::in | std::ios::binary); 166 cereal::BinaryInputArchive iarchive(is); 167 iarchive(index); 168 return true; 169 } 170 return false; 171 } 172 catch (const cereal::Exception& e) 173 { 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 185 bool PostCode::deserializePostCodes(const fs::path& path, 186 std::map<uint64_t, postcode_t>& codes) 187 { 188 try 189 { 190 if (fs::exists(path)) 191 { 192 std::ifstream is(path, std::ios::in | std::ios::binary); 193 cereal::BinaryInputArchive iarchive(is); 194 iarchive(codes); 195 return true; 196 } 197 return false; 198 } 199 catch (const cereal::Exception& e) 200 { 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 } 208 return false; 209 } 210 211 void 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 225 uint16_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; 240 } 241