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 "iomanip" 19 20 PostCodeDataHolder* PostCodeDataHolder::instance = 0; 21 22 void PostCode::deleteAll() 23 { 24 auto dir = fs::path(postcodeDataHolderObj->PostCodeListPathPrefix + 25 std::to_string(postcodeDataHolderObj->node)); 26 std::uintmax_t n = fs::remove_all(dir); 27 std::cerr << "clearPostCodes deleted " << n << " files in " 28 << postcodeDataHolderObj->PostCodeListPathPrefix + 29 std::to_string(postcodeDataHolderObj->node) 30 << std::endl; 31 fs::create_directories(dir); 32 postCodes.clear(); 33 currentBootCycleIndex = 0; 34 currentBootCycleCount(0); 35 } 36 37 std::vector<postcode_t> PostCode::getPostCodes(uint16_t index) 38 { 39 std::vector<postcode_t> codesVec; 40 if (1 == index && !postCodes.empty()) 41 { 42 std::transform(postCodes.begin(), postCodes.end(), 43 std::back_inserter(codesVec), 44 [](const auto& kv) { return kv.second; }); 45 } 46 else 47 { 48 uint16_t bootNum = getBootNum(index); 49 50 decltype(postCodes) codes; 51 deserializePostCodes( 52 fs::path(strPostCodeListPath + 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( 70 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes); 71 return codes; 72 } 73 74 void PostCode::savePostCodes(postcode_t code) 75 { 76 // 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 91 tsUS = firstPostCodeUsSinceEpoch + 92 std::chrono::duration_cast<std::chrono::microseconds>( 93 postCodeTimeSteady - firstPostCodeTimeSteady) 94 .count(); 95 } 96 97 postCodes.insert(std::make_pair(tsUS, code)); 98 serialize(fs::path(strPostCodeListPath)); 99 100 #ifdef ENABLE_BIOS_POST_CODE_LOG 101 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; 102 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", 117 "OpenBMC.0.2.BIOSPOSTCode"), 118 phosphor::logging::entry( 119 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, 120 timeOffsetStr.str().c_str(), hexCode.str().c_str())); 121 #endif 122 123 return; 124 } 125 126 fs::path PostCode::serialize(const std::string& path) 127 { 128 try 129 { 130 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); 140 141 std::ofstream osPostCodes( 142 (path + std::to_string(currentBootCycleIndex))); 143 cereal::JSONOutputArchive 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.c_str(), std::ios::in | std::ios::binary); 166 cereal::JSONInputArchive 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.c_str(), std::ios::in | std::ios::binary); 193 cereal::JSONInputArchive 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