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 if (postCodes.size() > MAX_POST_CODE_SIZE_PER_CYCLE) 97 { 98 postCodes.erase(postCodes.begin()); 99 } 100 serialize(fs::path(strPostCodeListPath)); 101 102 #ifdef ENABLE_BIOS_POST_CODE_LOG 103 uint64_t usTimeOffset = tsUS - firstPostCodeUsSinceEpoch; 104 std::ostringstream hexCode; 105 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex 106 << std::get<0>(code); 107 108 std::ostringstream timeOffsetStr; 109 // Set Fixed-Point Notation 110 timeOffsetStr << std::fixed; 111 // Set precision to 4 digits 112 timeOffsetStr << std::setprecision(4); 113 // Add double to stream 114 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000; 115 116 phosphor::logging::log<phosphor::logging::level::INFO>( 117 "BIOS POST Code", 118 phosphor::logging::entry("REDFISH_MESSAGE_ID=%s", 119 "OpenBMC.0.2.BIOSPOSTCode"), 120 phosphor::logging::entry( 121 "REDFISH_MESSAGE_ARGS=%d,%s,%s", currentBootCycleIndex, 122 timeOffsetStr.str().c_str(), hexCode.str().c_str())); 123 #endif 124 125 return; 126 } 127 128 fs::path PostCode::serialize(const std::string& path) 129 { 130 try 131 { 132 fs::path idxPath(path + strCurrentBootCycleIndexName); 133 std::ofstream osIdx(idxPath.c_str(), std::ios::binary); 134 cereal::JSONOutputArchive idxArchive(osIdx); 135 idxArchive(currentBootCycleIndex); 136 137 uint16_t count = currentBootCycleCount(); 138 fs::path cntPath(path + strCurrentBootCycleCountName); 139 std::ofstream osCnt(cntPath.c_str(), std::ios::binary); 140 cereal::JSONOutputArchive cntArchive(osCnt); 141 cntArchive(count); 142 143 std::ofstream osPostCodes( 144 (path + std::to_string(currentBootCycleIndex))); 145 cereal::JSONOutputArchive oarchivePostCodes(osPostCodes); 146 oarchivePostCodes(postCodes); 147 } 148 catch (const cereal::Exception& e) 149 { 150 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 151 return ""; 152 } 153 catch (const fs::filesystem_error& e) 154 { 155 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 156 return ""; 157 } 158 return path; 159 } 160 161 bool PostCode::deserialize(const fs::path& path, uint16_t& index) 162 { 163 try 164 { 165 if (fs::exists(path)) 166 { 167 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); 168 cereal::JSONInputArchive iarchive(is); 169 iarchive(index); 170 return true; 171 } 172 return false; 173 } 174 catch (const cereal::Exception& e) 175 { 176 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 177 return false; 178 } 179 catch (const fs::filesystem_error& e) 180 { 181 return false; 182 } 183 184 return false; 185 } 186 187 bool PostCode::deserializePostCodes(const fs::path& path, 188 std::map<uint64_t, postcode_t>& codes) 189 { 190 try 191 { 192 if (fs::exists(path)) 193 { 194 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); 195 cereal::JSONInputArchive iarchive(is); 196 iarchive(codes); 197 return true; 198 } 199 return false; 200 } 201 catch (const cereal::Exception& e) 202 { 203 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 204 return false; 205 } 206 catch (const fs::filesystem_error& e) 207 { 208 return false; 209 } 210 return false; 211 } 212 213 void PostCode::incrBootCycle() 214 { 215 if (currentBootCycleIndex >= maxBootCycleNum()) 216 { 217 currentBootCycleIndex = 1; 218 } 219 else 220 { 221 currentBootCycleIndex++; 222 } 223 currentBootCycleCount(std::min( 224 maxBootCycleNum(), static_cast<uint16_t>(currentBootCycleCount() + 1))); 225 } 226 227 uint16_t PostCode::getBootNum(const uint16_t index) const 228 { 229 // bootNum assumes the oldest archive is boot number 1 230 // and the current boot number equals bootCycleCount 231 // map bootNum back to bootIndex that was used to archive postcode 232 uint16_t bootNum = currentBootCycleIndex; 233 if (index > bootNum) // need to wrap around 234 { 235 bootNum = (maxBootCycleNum() + currentBootCycleIndex) - index + 1; 236 } 237 else 238 { 239 bootNum = currentBootCycleIndex - index + 1; 240 } 241 return bootNum; 242 } 243