Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 1 | #include "bios_parser.hpp" |
| 2 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 3 | #include "bios_table.hpp" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 4 | #include "utils.hpp" |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 5 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 6 | #include <cassert> |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 7 | #include <filesystem> |
| 8 | #include <fstream> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 9 | #include <iostream> |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 10 | #include <nlohmann/json.hpp> |
| 11 | #include <optional> |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 12 | #include <set> |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 13 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 14 | #include "libpldm/bios.h" |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 15 | #include "libpldm/bios_table.h" |
| 16 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 17 | namespace bios_parser |
| 18 | { |
| 19 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 20 | using namespace pldm::utils; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 21 | using Json = nlohmann::json; |
| 22 | namespace fs = std::filesystem; |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 23 | using namespace pldm::responder::bios; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 24 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 25 | const std::vector<Json> emptyJsonList{}; |
| 26 | const Json emptyJson{}; |
| 27 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 28 | using AttrType = uint8_t; |
| 29 | using Table = std::vector<uint8_t>; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 30 | using BIOSJsonName = std::string; |
| 31 | using AttrLookup = std::map<AttrName, std::optional<DBusMapping>>; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 32 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 33 | const std::set<std::string> SupportedDbusPropertyTypes = { |
| 34 | "bool", "uint8_t", "int16_t", "uint16_t", "int32_t", |
| 35 | "uint32_t", "int64_t", "uint64_t", "double", "string"}; |
| 36 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 37 | using BIOSStringHandler = |
| 38 | std::function<int(const Json& entry, Strings& strings)>; |
| 39 | using AttrLookupHandler = std::function<int(const Json& entry, AttrLookup)>; |
| 40 | using typeHandler = std::function<int(const Json& entry)>; |
| 41 | |
| 42 | Strings BIOSStrings; |
| 43 | AttrLookup BIOSAttrLookup; |
| 44 | |
| 45 | const Strings& getStrings() |
| 46 | { |
| 47 | return BIOSStrings; |
| 48 | } |
| 49 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 50 | int parseBIOSJsonFile(const fs::path& dirPath, const std::string& fileName, |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 51 | Json& fileData) |
| 52 | { |
| 53 | int rc = 0; |
| 54 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 55 | fs::path filePath = dirPath / fileName; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 56 | |
| 57 | std::ifstream jsonFile(filePath); |
| 58 | if (!jsonFile.is_open()) |
| 59 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 60 | std::cerr << "BIOS config file does not exist, FILE=" |
| 61 | << filePath.c_str() << "\n"; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 62 | rc = -1; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | fileData = Json::parse(jsonFile, nullptr, false); |
| 67 | if (fileData.is_discarded()) |
| 68 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 69 | std::cerr << "Parsing config file failed, FILE=" << filePath.c_str() |
| 70 | << "\n"; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 71 | rc = -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return rc; |
| 76 | } |
| 77 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 78 | namespace bios_enum |
| 79 | { |
| 80 | |
| 81 | namespace internal |
| 82 | { |
| 83 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 84 | using Value = std::string; |
| 85 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 86 | /** @brief Map of DBus property value to attribute value |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 87 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 88 | using DbusValToValMap = std::map<PropertyValue, Value>; |
| 89 | |
| 90 | /** @brief Map containing the DBus property value to attribute value map for the |
| 91 | * BIOS enumeration type attributes |
| 92 | */ |
| 93 | std::map<AttrName, DbusValToValMap> dbusValToValMaps; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 94 | |
| 95 | /** @brief Map containing the possible and the default values for the BIOS |
| 96 | * enumeration type attributes. |
| 97 | */ |
| 98 | AttrValuesMap valueMap; |
| 99 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 100 | /** @brief Populate the mapping between D-Bus property value and attribute value |
| 101 | * for the BIOS enumeration attribute. |
| 102 | * |
| 103 | * @param[in] type - type of the D-Bus property |
| 104 | * @param[in] dBusValues - json array of D-Bus property values |
| 105 | * @param[in] pv - Possible values for the BIOS enumeration attribute |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 106 | * |
| 107 | */ |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 108 | DbusValToValMap populateMapping(const std::string& type, const Json& dBusValues, |
| 109 | const PossibleValues& pv) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 110 | { |
| 111 | size_t pos = 0; |
| 112 | PropertyValue value; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 113 | DbusValToValMap valueMap; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 114 | for (auto it = dBusValues.begin(); it != dBusValues.end(); ++it, ++pos) |
| 115 | { |
| 116 | if (type == "uint8_t") |
| 117 | { |
| 118 | value = static_cast<uint8_t>(it.value()); |
| 119 | } |
| 120 | else if (type == "uint16_t") |
| 121 | { |
| 122 | value = static_cast<uint16_t>(it.value()); |
| 123 | } |
| 124 | else if (type == "uint32_t") |
| 125 | { |
| 126 | value = static_cast<uint32_t>(it.value()); |
| 127 | } |
| 128 | else if (type == "uint64_t") |
| 129 | { |
| 130 | value = static_cast<uint64_t>(it.value()); |
| 131 | } |
| 132 | else if (type == "int16_t") |
| 133 | { |
| 134 | value = static_cast<int16_t>(it.value()); |
| 135 | } |
| 136 | else if (type == "int32_t") |
| 137 | { |
| 138 | value = static_cast<int32_t>(it.value()); |
| 139 | } |
| 140 | else if (type == "int64_t") |
| 141 | { |
| 142 | value = static_cast<int64_t>(it.value()); |
| 143 | } |
| 144 | else if (type == "bool") |
| 145 | { |
| 146 | value = static_cast<bool>(it.value()); |
| 147 | } |
| 148 | else if (type == "double") |
| 149 | { |
| 150 | value = static_cast<double>(it.value()); |
| 151 | } |
| 152 | else if (type == "string") |
| 153 | { |
| 154 | value = static_cast<std::string>(it.value()); |
| 155 | } |
| 156 | else |
| 157 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 158 | std::cerr << "Unknown D-Bus property type, TYPE=" << type.c_str() |
| 159 | << "\n"; |
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 | valueMap.emplace(value, pv[pos]); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 163 | } |
| 164 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 165 | return valueMap; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 166 | } |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 167 | } // namespace internal |
| 168 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 169 | int setupBIOSStrings(const Json& entry, Strings& strings) |
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 | Json pvs = entry.value("possible_values", emptyJsonList); |
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 | for (auto& pv : pvs) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 174 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 175 | strings.emplace_back(std::move(pv.get<std::string>())); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 176 | } |
| 177 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 178 | return 0; |
| 179 | } |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 180 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 181 | int setup(const Json& entry) |
| 182 | { |
| 183 | PossibleValues possibleValues; |
| 184 | DefaultValues defaultValues; |
| 185 | |
| 186 | std::string attrName = entry.value("attribute_name", ""); |
| 187 | Json pv = entry["possible_values"]; |
| 188 | for (auto& val : pv) |
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 | possibleValues.emplace_back(std::move(val)); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 191 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 192 | Json dv = entry["default_values"]; |
| 193 | for (auto& val : dv) |
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 | defaultValues.emplace_back(std::move(val)); |
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 | if (entry.count("dbus") != 0) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 198 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 199 | auto dbusEntry = entry.value("dbus", emptyJson); |
| 200 | std::string propertyType = dbusEntry.value("property_type", ""); |
| 201 | Json propValues = dbusEntry["property_values"]; |
| 202 | internal::dbusValToValMaps.emplace( |
| 203 | attrName, internal::populateMapping(propertyType, propValues, |
| 204 | possibleValues)); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 205 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 206 | // Defaulting all the types of attributes to BIOSEnumeration |
| 207 | internal::valueMap.emplace(std::move(attrName), |
| 208 | std::make_tuple(entry.count("dbus") == 0, |
| 209 | std::move(possibleValues), |
| 210 | std::move(defaultValues))); |
| 211 | return 0; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | const AttrValuesMap& getValues() |
| 215 | { |
| 216 | return internal::valueMap; |
| 217 | } |
| 218 | |
| 219 | CurrentValues getAttrValue(const AttrName& attrName) |
| 220 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 221 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 222 | CurrentValues currentValues; |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 223 | PropertyValue propValue; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 224 | |
| 225 | if (dBusMap == std::nullopt) |
| 226 | { |
| 227 | const auto& valueEntry = internal::valueMap.at(attrName); |
| 228 | const auto& [readOnly, possibleValues, currentValues] = valueEntry; |
| 229 | return currentValues; |
| 230 | } |
| 231 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 232 | const auto& dbusValToValMap = internal::dbusValToValMaps.at(attrName); |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 233 | propValue = pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 234 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 235 | dBusMap->interface.c_str()); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 236 | |
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 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 246 | int setAttrValue(const AttrName& attrName, |
| 247 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 248 | const pldm_bios_attr_table_entry* attrEntry, |
| 249 | const BIOSStringTable& stringTable) |
| 250 | { |
| 251 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 252 | if (dBusMap == std::nullopt) |
| 253 | { |
| 254 | return PLDM_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | uint8_t pvNum = pldm_bios_table_attr_entry_enum_decode_pv_num(attrEntry); |
| 258 | std::vector<uint16_t> pvHdls(pvNum, 0); |
| 259 | pldm_bios_table_attr_entry_enum_decode_pv_hdls(attrEntry, pvHdls.data(), |
| 260 | pvNum); |
| 261 | |
| 262 | uint8_t defNum = |
| 263 | pldm_bios_table_attr_value_entry_enum_decode_number(attrValueEntry); |
| 264 | |
| 265 | assert(defNum == 1); |
| 266 | |
| 267 | std::vector<uint8_t> currHdls(1, 0); |
| 268 | pldm_bios_table_attr_value_entry_enum_decode_handles( |
| 269 | attrValueEntry, currHdls.data(), currHdls.size()); |
| 270 | |
| 271 | auto valueString = stringTable.findString(pvHdls[currHdls[0]]); |
| 272 | |
| 273 | const auto& dbusValToValMap = internal::dbusValToValMaps.at(attrName); |
| 274 | |
| 275 | auto it = std::find_if(dbusValToValMap.begin(), dbusValToValMap.end(), |
| 276 | [&valueString](const auto& typePair) { |
| 277 | return typePair.second == valueString; |
| 278 | }); |
| 279 | if (it == dbusValToValMap.end()) |
| 280 | { |
| 281 | std::cerr << "Invalid Enum Value\n"; |
| 282 | return PLDM_ERROR; |
| 283 | } |
| 284 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 285 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), it->first); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 286 | |
| 287 | return PLDM_SUCCESS; |
| 288 | } |
| 289 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 290 | } // namespace bios_enum |
| 291 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 292 | namespace bios_string |
| 293 | { |
| 294 | |
| 295 | /** @brief BIOS string types |
| 296 | */ |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 297 | enum BIOSStringEncoding |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 298 | { |
| 299 | UNKNOWN = 0x00, |
| 300 | ASCII = 0x01, |
| 301 | HEX = 0x02, |
| 302 | UTF_8 = 0x03, |
| 303 | UTF_16LE = 0x04, |
| 304 | UTF_16BE = 0x05, |
| 305 | VENDOR_SPECIFIC = 0xFF |
| 306 | }; |
| 307 | |
| 308 | const std::map<std::string, uint8_t> strTypeMap{ |
| 309 | {"Unknown", UNKNOWN}, |
| 310 | {"ASCII", ASCII}, |
| 311 | {"Hex", HEX}, |
| 312 | {"UTF-8", UTF_8}, |
| 313 | {"UTF-16LE", UTF_16LE}, |
| 314 | {"UTF-16LE", UTF_16LE}, |
| 315 | {"Vendor Specific", VENDOR_SPECIFIC}}; |
| 316 | |
| 317 | namespace internal |
| 318 | { |
| 319 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 320 | /** @brief Map containing the possible and the default values for the BIOS |
| 321 | * string type attributes. |
| 322 | */ |
| 323 | AttrValuesMap valueMap; |
| 324 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 325 | } // namespace internal |
| 326 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 327 | int setup(const Json& jsonEntry) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 328 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 329 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 330 | std::string attr = jsonEntry.value("attribute_name", ""); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 331 | // Transfer string type from string to enum |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 332 | std::string strTypeTmp = jsonEntry.value("string_type", "Unknown"); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 333 | auto iter = strTypeMap.find(strTypeTmp); |
| 334 | if (iter == strTypeMap.end()) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 335 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 336 | std::cerr << "Wrong string type, STRING_TYPE=" << strTypeTmp.c_str() |
| 337 | << " ATTRIBUTE_NAME=" << attr.c_str() << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 338 | return -1; |
| 339 | } |
| 340 | uint8_t strType = iter->second; |
| 341 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 342 | uint16_t minStrLen = jsonEntry.value("minimum_string_length", 0); |
| 343 | uint16_t maxStrLen = jsonEntry.value("maximum_string_length", 0); |
| 344 | uint16_t defaultStrLen = jsonEntry.value("default_string_length", 0); |
| 345 | std::string defaultStr = jsonEntry.value("default_string", ""); |
| 346 | |
| 347 | pldm_bios_table_attr_entry_string_info info = { |
| 348 | 0, /* name handle */ |
| 349 | false, /* read only */ |
| 350 | strType, minStrLen, maxStrLen, defaultStrLen, defaultStr.data(), |
| 351 | }; |
| 352 | |
| 353 | const char* errmsg; |
| 354 | auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg); |
| 355 | if (rc != PLDM_SUCCESS) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 356 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 357 | std::cerr << "Wrong filed for string attribute, ATTRIBUTE_NAME=" |
| 358 | << attr.c_str() << " ERRMSG=" << errmsg |
| 359 | << " MINIMUM_STRING_LENGTH=" << minStrLen |
| 360 | << " MAXIMUM_STRING_LENGTH=" << maxStrLen |
| 361 | << " DEFAULT_STRING_LENGTH=" << defaultStrLen |
| 362 | << " DEFAULT_STRING=" << defaultStr.data() << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 363 | return -1; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 364 | } |
| 365 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 366 | // Defaulting all the types of attributes to BIOSString |
| 367 | internal::valueMap.emplace( |
| 368 | std::move(attr), |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 369 | std::make_tuple(jsonEntry.count("dbus") == 0, strType, minStrLen, |
| 370 | maxStrLen, defaultStrLen, std::move(defaultStr))); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 371 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 372 | return 0; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | const AttrValuesMap& getValues() |
| 376 | { |
| 377 | return internal::valueMap; |
| 378 | } |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 379 | |
| 380 | std::string getAttrValue(const AttrName& attrName) |
| 381 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 382 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 383 | std::variant<std::string> propValue; |
| 384 | |
| 385 | if (dBusMap == std::nullopt) |
| 386 | { // return default string |
| 387 | const auto& valueEntry = internal::valueMap.at(attrName); |
| 388 | return std::get<DefaultStr>(valueEntry); |
| 389 | } |
| 390 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 391 | return pldm::utils::DBusHandler().getDbusProperty<std::string>( |
John Wang | 8b85f52 | 2019-10-17 19:28:19 +0800 | [diff] [blame] | 392 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 393 | dBusMap->interface.c_str()); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 394 | } |
| 395 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 396 | std::string stringToUtf8(BIOSStringEncoding stringType, |
| 397 | const std::vector<uint8_t>& data) |
| 398 | { |
| 399 | switch (stringType) |
| 400 | { |
| 401 | case ASCII: |
| 402 | case UTF_8: |
| 403 | case HEX: |
| 404 | return std::string(data.begin(), data.end()); |
| 405 | case UTF_16BE: |
| 406 | case UTF_16LE: // TODO |
| 407 | return std::string(data.begin(), data.end()); |
| 408 | case VENDOR_SPECIFIC: |
| 409 | throw std::invalid_argument("Vendor Specific is unsupported"); |
| 410 | case UNKNOWN: |
| 411 | throw std::invalid_argument("Unknown String Type"); |
| 412 | } |
| 413 | throw std::invalid_argument("String Type Error"); |
| 414 | } |
| 415 | |
| 416 | int setAttrValue(const AttrName& attrName, |
| 417 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 418 | const pldm_bios_attr_table_entry* attrEntry, |
| 419 | const BIOSStringTable&) |
| 420 | { |
| 421 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 422 | if (dBusMap == std::nullopt) |
| 423 | { |
| 424 | return PLDM_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | auto stringType = |
| 428 | pldm_bios_table_attr_entry_string_decode_string_type(attrEntry); |
| 429 | |
| 430 | variable_field currentString{}; |
| 431 | pldm_bios_table_attr_value_entry_string_decode_string(attrValueEntry, |
| 432 | ¤tString); |
| 433 | std::vector<uint8_t> data(currentString.ptr, |
| 434 | currentString.ptr + currentString.length); |
| 435 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 436 | PropertyValue value = |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 437 | stringToUtf8(static_cast<BIOSStringEncoding>(stringType), data); |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 438 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), value); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 439 | |
| 440 | return PLDM_SUCCESS; |
| 441 | } |
| 442 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 443 | } // namespace bios_string |
| 444 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 445 | namespace bios_integer |
| 446 | { |
| 447 | |
| 448 | AttrValuesMap valueMap; |
| 449 | |
| 450 | int setup(const Json& jsonEntry) |
| 451 | { |
| 452 | |
| 453 | std::string attr = jsonEntry.value("attribute_name", ""); |
| 454 | // Transfer string type from string to enum |
| 455 | |
| 456 | uint64_t lowerBound = jsonEntry.value("lower_bound", 0); |
| 457 | uint64_t upperBound = jsonEntry.value("upper_bound", 0); |
| 458 | uint32_t scalarIncrement = jsonEntry.value("scalar_increment", 1); |
| 459 | uint64_t defaultValue = jsonEntry.value("default_value", 0); |
| 460 | pldm_bios_table_attr_entry_integer_info info = { |
| 461 | 0, /* name handle*/ |
| 462 | false, /* read only */ |
| 463 | lowerBound, upperBound, scalarIncrement, defaultValue, |
| 464 | }; |
| 465 | const char* errmsg = nullptr; |
| 466 | auto rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg); |
| 467 | if (rc != PLDM_SUCCESS) |
| 468 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 469 | std::cerr << "Wrong filed for integer attribute, ATTRIBUTE_NAME=" |
| 470 | << attr.c_str() << " ERRMSG=" << errmsg |
| 471 | << " LOWER_BOUND=" << lowerBound |
| 472 | << " UPPER_BOUND=" << upperBound |
| 473 | << " DEFAULT_VALUE=" << defaultValue |
| 474 | << " SCALAR_INCREMENT=" << scalarIncrement << "\n"; |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | valueMap.emplace(std::move(attr), |
| 479 | std::make_tuple(jsonEntry.count("dbus") == 0, lowerBound, |
| 480 | upperBound, scalarIncrement, |
| 481 | defaultValue)); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | const AttrValuesMap& getValues() |
| 487 | { |
| 488 | return valueMap; |
| 489 | } |
| 490 | |
| 491 | uint64_t getAttrValue(const AttrName& attrName) |
| 492 | { |
| 493 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 494 | std::variant<std::string> propValue; |
| 495 | |
| 496 | if (dBusMap == std::nullopt) |
| 497 | { // return default string |
| 498 | const auto& valueEntry = valueMap.at(attrName); |
| 499 | return std::get<AttrDefaultValue>(valueEntry); |
| 500 | } |
| 501 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 502 | return pldm::utils::DBusHandler().getDbusProperty<uint64_t>( |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 503 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 504 | dBusMap->interface.c_str()); |
| 505 | } |
| 506 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 507 | int setAttrValue(const AttrName& attrName, |
| 508 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 509 | const pldm_bios_attr_table_entry*, const BIOSStringTable&) |
| 510 | { |
| 511 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 512 | if (dBusMap == std::nullopt) |
| 513 | { |
| 514 | return PLDM_SUCCESS; |
| 515 | } |
| 516 | |
| 517 | uint64_t currentValue = |
| 518 | pldm_bios_table_attr_value_entry_integer_decode_cv(attrValueEntry); |
| 519 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 520 | PropertyValue value = static_cast<uint64_t>(currentValue); |
| 521 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), value); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 522 | |
| 523 | return PLDM_SUCCESS; |
| 524 | } |
| 525 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 526 | } // namespace bios_integer |
| 527 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 528 | const std::map<BIOSJsonName, BIOSStringHandler> BIOSStringHandlers = { |
| 529 | {bIOSEnumJson, bios_enum::setupBIOSStrings}, |
| 530 | }; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 531 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 532 | const std::map<BIOSJsonName, typeHandler> BIOSTypeHandlers = { |
| 533 | {bIOSEnumJson, bios_enum::setup}, |
| 534 | {bIOSStrJson, bios_string::setup}, |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 535 | {bIOSIntegerJson, bios_integer::setup}, |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | void setupBIOSStrings(const BIOSJsonName& jsonName, const Json& entry, |
| 539 | Strings& strings) |
| 540 | { |
| 541 | strings.emplace_back(entry.value("attribute_name", "")); |
| 542 | auto iter = BIOSStringHandlers.find(jsonName); |
| 543 | if (iter != BIOSStringHandlers.end()) |
| 544 | { |
| 545 | iter->second(entry, strings); |
| 546 | } |
| 547 | } |
| 548 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 549 | void setupBIOSAttrLookup(const Json& jsonEntry, AttrLookup& lookup) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 550 | { |
| 551 | std::optional<DBusMapping> dBusMap; |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 552 | std::string attrName = jsonEntry.value("attribute_name", ""); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 553 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 554 | if (jsonEntry.count("dbus") != 0) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 555 | { |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 556 | auto dBusEntry = jsonEntry.value("dbus", emptyJson); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 557 | std::string objectPath = dBusEntry.value("object_path", ""); |
| 558 | std::string interface = dBusEntry.value("interface", ""); |
| 559 | std::string propertyName = dBusEntry.value("property_name", ""); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 560 | std::string propertyType = dBusEntry.value("property_type", ""); |
| 561 | if (!objectPath.empty() && !interface.empty() && |
| 562 | !propertyName.empty() && |
| 563 | (SupportedDbusPropertyTypes.find(propertyType) != |
| 564 | SupportedDbusPropertyTypes.end())) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 565 | { |
| 566 | dBusMap = std::optional<DBusMapping>( |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 567 | {objectPath, interface, propertyName, propertyType}); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 568 | } |
| 569 | else |
| 570 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 571 | std::cerr << "Invalid dbus config, OBJPATH=" |
| 572 | << dBusMap->objectPath.c_str() |
| 573 | << " INTERFACE=" << dBusMap->interface.c_str() |
| 574 | << " PROPERTY_NAME=" << dBusMap->propertyName.c_str() |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 575 | << " PROPERTY_TYPE=" << dBusMap->propertyType.c_str() |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 576 | << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | lookup.emplace(attrName, dBusMap); |
| 580 | } |
| 581 | |
| 582 | int setupBIOSType(const BIOSJsonName& jsonName, const Json& entry) |
| 583 | { |
| 584 | auto iter = BIOSTypeHandlers.find(jsonName); |
| 585 | if (iter != BIOSTypeHandlers.end()) |
| 586 | { |
| 587 | iter->second(entry); |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 592 | const std::vector<BIOSJsonName> BIOSConfigFiles = {bIOSEnumJson, bIOSStrJson, |
| 593 | bIOSIntegerJson}; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 594 | |
| 595 | int setupConfig(const char* dirPath) |
| 596 | { |
| 597 | if (!BIOSStrings.empty() && !BIOSAttrLookup.empty()) |
| 598 | { |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | fs::path dir(dirPath); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 603 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 604 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 605 | std::cerr << "BIOS config directory does not exist or empty, DIR=" |
| 606 | << dirPath << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 607 | return -1; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 608 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 609 | for (auto jsonName : BIOSConfigFiles) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 610 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 611 | Json json; |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 612 | if (parseBIOSJsonFile(dir, jsonName, json) < 0) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 613 | { |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 614 | continue; |
| 615 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 616 | auto entries = json.value("entries", emptyJsonList); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 617 | for (auto& entry : entries) |
| 618 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 619 | setupBIOSStrings(jsonName, entry, BIOSStrings); |
| 620 | setupBIOSAttrLookup(entry, BIOSAttrLookup); |
| 621 | setupBIOSType(jsonName, entry); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 622 | } |
| 623 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 624 | if (BIOSStrings.empty()) |
| 625 | { // means there is no attribute |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 626 | std::cerr << "No attribute is found in the config directory, DIR=" |
| 627 | << dirPath << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 628 | return -1; |
| 629 | } |
| 630 | return 0; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 631 | } |
| 632 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 633 | using setAttrValueHandler = std::function<int( |
| 634 | const AttrName&, const pldm_bios_attr_val_table_entry*, |
| 635 | const pldm_bios_attr_table_entry*, const BIOSStringTable&)>; |
| 636 | |
| 637 | const std::map<AttrType, setAttrValueHandler> SetAttrValueMap{{ |
| 638 | {PLDM_BIOS_STRING, bios_string::setAttrValue}, |
| 639 | {PLDM_BIOS_STRING_READ_ONLY, bios_string::setAttrValue}, |
| 640 | {PLDM_BIOS_ENUMERATION, bios_enum::setAttrValue}, |
| 641 | {PLDM_BIOS_ENUMERATION_READ_ONLY, bios_enum::setAttrValue}, |
| 642 | {PLDM_BIOS_INTEGER, bios_integer::setAttrValue}, |
| 643 | {PLDM_BIOS_INTEGER_READ_ONLY, bios_integer::setAttrValue}, |
| 644 | |
| 645 | }}; |
| 646 | |
| 647 | int setAttributeValueOnDbus(const variable_field* attributeData, |
| 648 | const BIOSTable& biosAttributeTable, |
| 649 | const BIOSStringTable& stringTable) |
| 650 | { |
| 651 | Table attributeTable; |
| 652 | biosAttributeTable.load(attributeTable); |
| 653 | auto attrValueEntry = |
| 654 | reinterpret_cast<const pldm_bios_attr_val_table_entry*>( |
| 655 | attributeData->ptr); |
| 656 | |
| 657 | auto attrType = |
| 658 | pldm_bios_table_attr_value_entry_decode_attribute_type(attrValueEntry); |
| 659 | auto attrHandle = pldm_bios_table_attr_value_entry_decode_attribute_handle( |
| 660 | attrValueEntry); |
| 661 | |
| 662 | auto attrEntry = pldm_bios_table_attr_find_by_handle( |
| 663 | attributeTable.data(), attributeTable.size(), attrHandle); |
| 664 | |
| 665 | assert(attrEntry != nullptr); |
| 666 | |
| 667 | auto attrNameHandle = |
| 668 | pldm_bios_table_attr_entry_decode_string_handle(attrEntry); |
| 669 | |
| 670 | auto attrName = stringTable.findString(attrNameHandle); |
| 671 | |
| 672 | try |
| 673 | { |
| 674 | auto rc = SetAttrValueMap.at(attrType)(attrName, attrValueEntry, |
| 675 | attrEntry, stringTable); |
| 676 | return rc; |
| 677 | } |
| 678 | catch (const std::exception& e) |
| 679 | { |
| 680 | std::cerr << "setAttributeValueOnDbus Error: " << e.what() << std::endl; |
| 681 | return PLDM_ERROR; |
| 682 | } |
| 683 | } |
| 684 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 685 | } // namespace bios_parser |