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