Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 1 | #include "bios_parser.hpp" |
| 2 | |
| 3 | #include "libpldmresponder/utils.hpp" |
| 4 | |
| 5 | #include <filesystem> |
| 6 | #include <fstream> |
| 7 | #include <nlohmann/json.hpp> |
| 8 | #include <optional> |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | |
| 11 | namespace bios_parser |
| 12 | { |
| 13 | |
| 14 | using Json = nlohmann::json; |
| 15 | namespace fs = std::filesystem; |
| 16 | using namespace phosphor::logging; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 17 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 18 | const std::vector<Json> emptyJsonList{}; |
| 19 | const Json emptyJson{}; |
| 20 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 21 | struct DBusMapping |
| 22 | { |
| 23 | std::string objectPath; //!< D-Bus object path |
| 24 | std::string interface; //!< D-Bus interface |
| 25 | std::string propertyName; //!< D-Bus property name |
| 26 | }; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 27 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 28 | using AttrName = std::string; |
| 29 | using BIOSJsonName = std::string; |
| 30 | using AttrLookup = std::map<AttrName, std::optional<DBusMapping>>; |
| 31 | using BIOSStringHandler = |
| 32 | std::function<int(const Json& entry, Strings& strings)>; |
| 33 | using AttrLookupHandler = std::function<int(const Json& entry, AttrLookup)>; |
| 34 | using typeHandler = std::function<int(const Json& entry)>; |
| 35 | |
| 36 | Strings BIOSStrings; |
| 37 | AttrLookup BIOSAttrLookup; |
| 38 | |
| 39 | const Strings& getStrings() |
| 40 | { |
| 41 | return BIOSStrings; |
| 42 | } |
| 43 | |
| 44 | int parseBiosJsonFile(const fs::path& dirPath, const std::string& fileName, |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 45 | Json& fileData) |
| 46 | { |
| 47 | int rc = 0; |
| 48 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 49 | fs::path filePath = dirPath / fileName; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 50 | |
| 51 | std::ifstream jsonFile(filePath); |
| 52 | if (!jsonFile.is_open()) |
| 53 | { |
| 54 | log<level::ERR>("BIOS config file does not exist", |
| 55 | entry("FILE=%s", filePath.c_str())); |
| 56 | rc = -1; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | fileData = Json::parse(jsonFile, nullptr, false); |
| 61 | if (fileData.is_discarded()) |
| 62 | { |
| 63 | log<level::ERR>("Parsing config file failed", |
| 64 | entry("FILE=%s", filePath.c_str())); |
| 65 | rc = -1; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return rc; |
| 70 | } |
| 71 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 72 | namespace bios_enum |
| 73 | { |
| 74 | |
| 75 | namespace internal |
| 76 | { |
| 77 | |
| 78 | using PropertyValue = |
| 79 | std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, |
| 80 | uint64_t, double, std::string>; |
| 81 | using Value = std::string; |
| 82 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 83 | /** @brief Map of DBus property value to attribute value |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 84 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 85 | using DbusValToValMap = std::map<PropertyValue, Value>; |
| 86 | |
| 87 | /** @brief Map containing the DBus property value to attribute value map for the |
| 88 | * BIOS enumeration type attributes |
| 89 | */ |
| 90 | std::map<AttrName, DbusValToValMap> dbusValToValMaps; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 91 | |
| 92 | /** @brief Map containing the possible and the default values for the BIOS |
| 93 | * enumeration type attributes. |
| 94 | */ |
| 95 | AttrValuesMap valueMap; |
| 96 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 97 | /** @brief Populate the mapping between D-Bus property value and attribute value |
| 98 | * for the BIOS enumeration attribute. |
| 99 | * |
| 100 | * @param[in] type - type of the D-Bus property |
| 101 | * @param[in] dBusValues - json array of D-Bus property values |
| 102 | * @param[in] pv - Possible values for the BIOS enumeration attribute |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 103 | * |
| 104 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 105 | DbusValToValMap populateMapping(const std::string& type, const Json& dBusValues, |
| 106 | const PossibleValues& pv) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 107 | { |
| 108 | size_t pos = 0; |
| 109 | PropertyValue value; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 110 | DbusValToValMap valueMap; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 111 | for (auto it = dBusValues.begin(); it != dBusValues.end(); ++it, ++pos) |
| 112 | { |
| 113 | if (type == "uint8_t") |
| 114 | { |
| 115 | value = static_cast<uint8_t>(it.value()); |
| 116 | } |
| 117 | else if (type == "uint16_t") |
| 118 | { |
| 119 | value = static_cast<uint16_t>(it.value()); |
| 120 | } |
| 121 | else if (type == "uint32_t") |
| 122 | { |
| 123 | value = static_cast<uint32_t>(it.value()); |
| 124 | } |
| 125 | else if (type == "uint64_t") |
| 126 | { |
| 127 | value = static_cast<uint64_t>(it.value()); |
| 128 | } |
| 129 | else if (type == "int16_t") |
| 130 | { |
| 131 | value = static_cast<int16_t>(it.value()); |
| 132 | } |
| 133 | else if (type == "int32_t") |
| 134 | { |
| 135 | value = static_cast<int32_t>(it.value()); |
| 136 | } |
| 137 | else if (type == "int64_t") |
| 138 | { |
| 139 | value = static_cast<int64_t>(it.value()); |
| 140 | } |
| 141 | else if (type == "bool") |
| 142 | { |
| 143 | value = static_cast<bool>(it.value()); |
| 144 | } |
| 145 | else if (type == "double") |
| 146 | { |
| 147 | value = static_cast<double>(it.value()); |
| 148 | } |
| 149 | else if (type == "string") |
| 150 | { |
| 151 | value = static_cast<std::string>(it.value()); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | log<level::ERR>("Unknown D-Bus property type", |
| 156 | entry("TYPE=%s", type.c_str())); |
| 157 | } |
| 158 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 159 | valueMap.emplace(value, pv[pos]); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 160 | } |
| 161 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 162 | return valueMap; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace internal |
| 166 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 167 | int setupBIOSStrings(const Json& entry, Strings& strings) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 168 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 169 | Json pvs = entry.value("possible_values", emptyJsonList); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 170 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 171 | for (auto& pv : pvs) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 172 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 173 | strings.emplace_back(std::move(pv.get<std::string>())); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 174 | } |
| 175 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 178 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 179 | int setup(const Json& entry) |
| 180 | { |
| 181 | PossibleValues possibleValues; |
| 182 | DefaultValues defaultValues; |
| 183 | |
| 184 | std::string attrName = entry.value("attribute_name", ""); |
| 185 | Json pv = entry["possible_values"]; |
| 186 | for (auto& val : pv) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 187 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 188 | possibleValues.emplace_back(std::move(val)); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 189 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 190 | Json dv = entry["default_values"]; |
| 191 | for (auto& val : dv) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 192 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 193 | defaultValues.emplace_back(std::move(val)); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 194 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 195 | if (entry.count("dbus") != 0) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 196 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 197 | auto dbusEntry = entry.value("dbus", emptyJson); |
| 198 | std::string propertyType = dbusEntry.value("property_type", ""); |
| 199 | Json propValues = dbusEntry["property_values"]; |
| 200 | internal::dbusValToValMaps.emplace( |
| 201 | attrName, internal::populateMapping(propertyType, propValues, |
| 202 | possibleValues)); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 203 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 204 | // Defaulting all the types of attributes to BIOSEnumeration |
| 205 | internal::valueMap.emplace(std::move(attrName), |
| 206 | std::make_tuple(entry.count("dbus") == 0, |
| 207 | std::move(possibleValues), |
| 208 | std::move(defaultValues))); |
| 209 | return 0; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | const AttrValuesMap& getValues() |
| 213 | { |
| 214 | return internal::valueMap; |
| 215 | } |
| 216 | |
| 217 | CurrentValues getAttrValue(const AttrName& attrName) |
| 218 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 219 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 220 | CurrentValues currentValues; |
| 221 | internal::PropertyValue propValue; |
| 222 | |
| 223 | if (dBusMap == std::nullopt) |
| 224 | { |
| 225 | const auto& valueEntry = internal::valueMap.at(attrName); |
| 226 | const auto& [readOnly, possibleValues, currentValues] = valueEntry; |
| 227 | return currentValues; |
| 228 | } |
| 229 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 230 | const auto& dbusValToValMap = internal::dbusValToValMaps.at(attrName); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 231 | auto bus = sdbusplus::bus::new_default(); |
| 232 | auto service = pldm::responder::getService(bus, dBusMap.value().objectPath, |
| 233 | dBusMap.value().interface); |
| 234 | auto method = |
| 235 | bus.new_method_call(service.c_str(), dBusMap.value().objectPath.c_str(), |
| 236 | "org.freedesktop.DBus.Properties", "Get"); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 237 | method.append(dBusMap->interface, dBusMap->propertyName); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 238 | auto reply = bus.call(method); |
| 239 | reply.read(propValue); |
| 240 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 241 | auto iter = dbusValToValMap.find(propValue); |
| 242 | if (iter != dbusValToValMap.end()) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 243 | { |
| 244 | currentValues.push_back(iter->second); |
| 245 | } |
| 246 | |
| 247 | return currentValues; |
| 248 | } |
| 249 | |
| 250 | } // namespace bios_enum |
| 251 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 252 | namespace bios_string |
| 253 | { |
| 254 | |
| 255 | /** @brief BIOS string types |
| 256 | */ |
| 257 | enum BiosStringEncoding |
| 258 | { |
| 259 | UNKNOWN = 0x00, |
| 260 | ASCII = 0x01, |
| 261 | HEX = 0x02, |
| 262 | UTF_8 = 0x03, |
| 263 | UTF_16LE = 0x04, |
| 264 | UTF_16BE = 0x05, |
| 265 | VENDOR_SPECIFIC = 0xFF |
| 266 | }; |
| 267 | |
| 268 | const std::map<std::string, uint8_t> strTypeMap{ |
| 269 | {"Unknown", UNKNOWN}, |
| 270 | {"ASCII", ASCII}, |
| 271 | {"Hex", HEX}, |
| 272 | {"UTF-8", UTF_8}, |
| 273 | {"UTF-16LE", UTF_16LE}, |
| 274 | {"UTF-16LE", UTF_16LE}, |
| 275 | {"Vendor Specific", VENDOR_SPECIFIC}}; |
| 276 | |
| 277 | namespace internal |
| 278 | { |
| 279 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 280 | /** @brief Map containing the possible and the default values for the BIOS |
| 281 | * string type attributes. |
| 282 | */ |
| 283 | AttrValuesMap valueMap; |
| 284 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 285 | } // namespace internal |
| 286 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 287 | int setup(const Json& entry) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 288 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 289 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 290 | std::string attr = entry.value("attribute_name", ""); |
| 291 | // Transfer string type from string to enum |
| 292 | std::string strTypeTmp = entry.value("string_type", "Unknown"); |
| 293 | auto iter = strTypeMap.find(strTypeTmp); |
| 294 | if (iter == strTypeMap.end()) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 295 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 296 | log<level::ERR>( |
| 297 | "Wrong string type", |
| 298 | phosphor::logging::entry("STRING_TYPE=%s", strTypeTmp.c_str()), |
| 299 | phosphor::logging::entry("ATTRIBUTE_NAME=%s", attr.c_str())); |
| 300 | return -1; |
| 301 | } |
| 302 | uint8_t strType = iter->second; |
| 303 | |
| 304 | uint16_t minStrLen = entry.value("minimum_string_length", 0); |
| 305 | uint16_t maxStrLen = entry.value("maximum_string_length", 0); |
| 306 | if (maxStrLen - minStrLen < 0) |
| 307 | { |
| 308 | log<level::ERR>( |
| 309 | "Maximum string length is smaller than minimum string length", |
| 310 | phosphor::logging::entry("ATTRIBUTE_NAME=%s", attr.c_str())); |
| 311 | return -1; |
| 312 | } |
| 313 | uint16_t defaultStrLen = entry.value("default_string_length", 0); |
| 314 | std::string defaultStr = entry.value("default_string", ""); |
| 315 | if ((defaultStrLen == 0) && (defaultStr.size() > 0)) |
| 316 | { |
| 317 | log<level::ERR>( |
| 318 | "Default string length is 0, but default string is existing", |
| 319 | phosphor::logging::entry("ATTRIBUTE_NAME=%s", attr.c_str())); |
| 320 | return -1; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 321 | } |
| 322 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 323 | // Defaulting all the types of attributes to BIOSString |
| 324 | internal::valueMap.emplace( |
| 325 | std::move(attr), |
| 326 | std::make_tuple(entry.count("dbus") == 0, strType, minStrLen, maxStrLen, |
| 327 | defaultStrLen, std::move(defaultStr))); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 328 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 329 | return 0; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | const AttrValuesMap& getValues() |
| 333 | { |
| 334 | return internal::valueMap; |
| 335 | } |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 336 | |
| 337 | std::string getAttrValue(const AttrName& attrName) |
| 338 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 339 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 340 | std::variant<std::string> propValue; |
| 341 | |
| 342 | if (dBusMap == std::nullopt) |
| 343 | { // return default string |
| 344 | const auto& valueEntry = internal::valueMap.at(attrName); |
| 345 | return std::get<DefaultStr>(valueEntry); |
| 346 | } |
| 347 | |
| 348 | auto bus = sdbusplus::bus::new_default(); |
| 349 | auto service = pldm::responder::getService(bus, dBusMap->objectPath, |
| 350 | dBusMap->interface); |
| 351 | auto method = |
| 352 | bus.new_method_call(service.c_str(), dBusMap->objectPath.c_str(), |
| 353 | "org.freedesktop.DBus.Properties", "Get"); |
| 354 | method.append(dBusMap->interface, dBusMap->propertyName); |
| 355 | auto reply = bus.call(method); |
| 356 | reply.read(propValue); |
| 357 | |
| 358 | return std::get<std::string>(propValue); |
| 359 | } |
| 360 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 361 | } // namespace bios_string |
| 362 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 363 | const std::map<BIOSJsonName, BIOSStringHandler> BIOSStringHandlers = { |
| 364 | {bIOSEnumJson, bios_enum::setupBIOSStrings}, |
| 365 | }; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 366 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 367 | const std::map<BIOSJsonName, typeHandler> BIOSTypeHandlers = { |
| 368 | {bIOSEnumJson, bios_enum::setup}, |
| 369 | {bIOSStrJson, bios_string::setup}, |
| 370 | }; |
| 371 | |
| 372 | void setupBIOSStrings(const BIOSJsonName& jsonName, const Json& entry, |
| 373 | Strings& strings) |
| 374 | { |
| 375 | strings.emplace_back(entry.value("attribute_name", "")); |
| 376 | auto iter = BIOSStringHandlers.find(jsonName); |
| 377 | if (iter != BIOSStringHandlers.end()) |
| 378 | { |
| 379 | iter->second(entry, strings); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void setupBIOSAttrLookup(const Json& entry, AttrLookup& lookup) |
| 384 | { |
| 385 | std::optional<DBusMapping> dBusMap; |
| 386 | std::string attrName = entry.value("attribute_name", ""); |
| 387 | |
| 388 | if (entry.count("dbus") != 0) |
| 389 | { |
| 390 | auto dBusEntry = entry.value("dbus", emptyJson); |
| 391 | std::string objectPath = dBusEntry.value("object_path", ""); |
| 392 | std::string interface = dBusEntry.value("interface", ""); |
| 393 | std::string propertyName = dBusEntry.value("property_name", ""); |
| 394 | if (!objectPath.empty() && !interface.empty() && !propertyName.empty()) |
| 395 | { |
| 396 | dBusMap = std::optional<DBusMapping>( |
| 397 | {objectPath, interface, propertyName}); |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | log<level::ERR>( |
| 402 | "Invalid dbus config", |
| 403 | phosphor::logging::entry("OBJPATH=%s", |
| 404 | dBusMap->objectPath.c_str()), |
| 405 | phosphor::logging::entry("INTERFACE=%s", |
| 406 | dBusMap->interface.c_str()), |
| 407 | phosphor::logging::entry("PROPERTY_NAME=%s", |
| 408 | dBusMap->propertyName.c_str())); |
| 409 | } |
| 410 | } |
| 411 | lookup.emplace(attrName, dBusMap); |
| 412 | } |
| 413 | |
| 414 | int setupBIOSType(const BIOSJsonName& jsonName, const Json& entry) |
| 415 | { |
| 416 | auto iter = BIOSTypeHandlers.find(jsonName); |
| 417 | if (iter != BIOSTypeHandlers.end()) |
| 418 | { |
| 419 | iter->second(entry); |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | const std::vector<BIOSJsonName> BIOSConfigFiles = {bIOSEnumJson, bIOSStrJson}; |
| 425 | |
| 426 | int setupConfig(const char* dirPath) |
| 427 | { |
| 428 | if (!BIOSStrings.empty() && !BIOSAttrLookup.empty()) |
| 429 | { |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | fs::path dir(dirPath); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 434 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 435 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 436 | log<level::ERR>("BIOS config directory does not exist or empty", |
| 437 | entry("DIR=%s", dirPath)); |
| 438 | return -1; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 439 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 440 | for (auto jsonName : BIOSConfigFiles) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 441 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 442 | Json json; |
| 443 | if (parseBiosJsonFile(dir, jsonName, json) < 0) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 444 | { |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 445 | continue; |
| 446 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 447 | auto entries = json.value("entries", emptyJsonList); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 448 | for (auto& entry : entries) |
| 449 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 450 | setupBIOSStrings(jsonName, entry, BIOSStrings); |
| 451 | setupBIOSAttrLookup(entry, BIOSAttrLookup); |
| 452 | setupBIOSType(jsonName, entry); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 453 | } |
| 454 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 455 | if (BIOSStrings.empty()) |
| 456 | { // means there is no attribute |
| 457 | log<level::ERR>("No attribute is found in the config directory", |
| 458 | entry("DIR=%s", dirPath)); |
| 459 | return -1; |
| 460 | } |
| 461 | return 0; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | } // namespace bios_parser |