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 void PostCode::deleteAll() 21 { 22 auto dir = fs::path(postcodeDataHolderObj.PostCodeListPathPrefix + 23 std::to_string(postcodeDataHolderObj.node)); 24 std::uintmax_t n = fs::remove_all(dir); 25 std::cerr << "clearPostCodes deleted " << n << " files in " 26 << postcodeDataHolderObj.PostCodeListPathPrefix + 27 std::to_string(postcodeDataHolderObj.node) 28 << std::endl; 29 fs::create_directories(dir); 30 postCodes.clear(); 31 currentBootCycleIndex = 0; 32 currentBootCycleCount(0); 33 } 34 35 std::vector<postcode_t> PostCode::getPostCodes(uint16_t index) 36 { 37 std::vector<postcode_t> codesVec; 38 if (1 == index && !postCodes.empty()) 39 { 40 std::transform(postCodes.begin(), postCodes.end(), 41 std::back_inserter(codesVec), 42 [](const auto& kv) { return kv.second; }); 43 } 44 else 45 { 46 uint16_t bootNum = getBootNum(index); 47 48 decltype(postCodes) codes; 49 deserializePostCodes( 50 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes); 51 std::transform(codes.begin(), codes.end(), std::back_inserter(codesVec), 52 [](const auto& kv) { return kv.second; }); 53 } 54 return codesVec; 55 } 56 57 std::map<uint64_t, postcode_t> 58 PostCode::getPostCodesWithTimeStamp(uint16_t index) 59 { 60 if (1 == index && !postCodes.empty()) 61 { 62 return postCodes; 63 } 64 65 uint16_t bootNum = getBootNum(index); 66 decltype(postCodes) codes; 67 deserializePostCodes( 68 fs::path(strPostCodeListPath + std::to_string(bootNum)), codes); 69 return codes; 70 } 71 72 void PostCode::savePostCodes(postcode_t code) 73 { 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 89 tsUS = firstPostCodeUsSinceEpoch + 90 std::chrono::duration_cast<std::chrono::microseconds>( 91 postCodeTimeSteady - firstPostCodeTimeSteady) 92 .count(); 93 } 94 95 postCodes.insert(std::make_pair(tsUS, code)); 96 serialize(fs::path(strPostCodeListPath)); 97 98 #ifdef ENABLE_BIOS_POST_CODE_LOG 99 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; 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", 115 "OpenBMC.0.2.BIOSPOSTCode"), 116 phosphor::logging::entry( 117 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, 118 timeOffsetStr.str().c_str(), hexCode.str().c_str())); 119 #endif 120 121 return; 122 } 123 124 fs::path PostCode::serialize(const std::string& path) 125 { 126 try 127 { 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); 138 139 std::ofstream osPostCodes( 140 (path + std::to_string(currentBootCycleIndex))); 141 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes); 142 oarchivePostCodes(postCodes); 143 } 144 catch (const cereal::Exception& e) 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 } 170 catch (const cereal::Exception& e) 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, 184 std::map<uint64_t, postcode_t>& codes) 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 } 197 catch (const cereal::Exception& e) 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 } 206 return false; 207 } 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; 238 } 239