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 | 8b85f52 | 2019-10-17 19:28:19 +0800 | [diff] [blame] | 233 | propValue = |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 234 | pldm::utils::DBusHandler().getDbusPropertyVariant<PropertyValue>( |
| 235 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 236 | dBusMap->interface.c_str()); |
| 237 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 238 | auto iter = dbusValToValMap.find(propValue); |
| 239 | if (iter != dbusValToValMap.end()) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 240 | { |
| 241 | currentValues.push_back(iter->second); |
| 242 | } |
| 243 | |
| 244 | return currentValues; |
| 245 | } |
| 246 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 247 | int setAttrValue(const AttrName& attrName, |
| 248 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 249 | const pldm_bios_attr_table_entry* attrEntry, |
| 250 | const BIOSStringTable& stringTable) |
| 251 | { |
| 252 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 253 | if (dBusMap == std::nullopt) |
| 254 | { |
| 255 | return PLDM_SUCCESS; |
| 256 | } |
| 257 | |
| 258 | uint8_t pvNum = pldm_bios_table_attr_entry_enum_decode_pv_num(attrEntry); |
| 259 | std::vector<uint16_t> pvHdls(pvNum, 0); |
| 260 | pldm_bios_table_attr_entry_enum_decode_pv_hdls(attrEntry, pvHdls.data(), |
| 261 | pvNum); |
| 262 | |
| 263 | uint8_t defNum = |
| 264 | pldm_bios_table_attr_value_entry_enum_decode_number(attrValueEntry); |
| 265 | |
| 266 | assert(defNum == 1); |
| 267 | |
| 268 | std::vector<uint8_t> currHdls(1, 0); |
| 269 | pldm_bios_table_attr_value_entry_enum_decode_handles( |
| 270 | attrValueEntry, currHdls.data(), currHdls.size()); |
| 271 | |
| 272 | auto valueString = stringTable.findString(pvHdls[currHdls[0]]); |
| 273 | |
| 274 | const auto& dbusValToValMap = internal::dbusValToValMaps.at(attrName); |
| 275 | |
| 276 | auto it = std::find_if(dbusValToValMap.begin(), dbusValToValMap.end(), |
| 277 | [&valueString](const auto& typePair) { |
| 278 | return typePair.second == valueString; |
| 279 | }); |
| 280 | if (it == dbusValToValMap.end()) |
| 281 | { |
| 282 | std::cerr << "Invalid Enum Value\n"; |
| 283 | return PLDM_ERROR; |
| 284 | } |
| 285 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 286 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), it->first); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 287 | |
| 288 | return PLDM_SUCCESS; |
| 289 | } |
| 290 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 291 | } // namespace bios_enum |
| 292 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 293 | namespace bios_string |
| 294 | { |
| 295 | |
| 296 | /** @brief BIOS string types |
| 297 | */ |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 298 | enum BIOSStringEncoding |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 299 | { |
| 300 | UNKNOWN = 0x00, |
| 301 | ASCII = 0x01, |
| 302 | HEX = 0x02, |
| 303 | UTF_8 = 0x03, |
| 304 | UTF_16LE = 0x04, |
| 305 | UTF_16BE = 0x05, |
| 306 | VENDOR_SPECIFIC = 0xFF |
| 307 | }; |
| 308 | |
| 309 | const std::map<std::string, uint8_t> strTypeMap{ |
| 310 | {"Unknown", UNKNOWN}, |
| 311 | {"ASCII", ASCII}, |
| 312 | {"Hex", HEX}, |
| 313 | {"UTF-8", UTF_8}, |
| 314 | {"UTF-16LE", UTF_16LE}, |
| 315 | {"UTF-16LE", UTF_16LE}, |
| 316 | {"Vendor Specific", VENDOR_SPECIFIC}}; |
| 317 | |
| 318 | namespace internal |
| 319 | { |
| 320 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 321 | /** @brief Map containing the possible and the default values for the BIOS |
| 322 | * string type attributes. |
| 323 | */ |
| 324 | AttrValuesMap valueMap; |
| 325 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 326 | } // namespace internal |
| 327 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 328 | int setup(const Json& jsonEntry) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 329 | { |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 330 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 331 | std::string attr = jsonEntry.value("attribute_name", ""); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 332 | // Transfer string type from string to enum |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 333 | std::string strTypeTmp = jsonEntry.value("string_type", "Unknown"); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 334 | auto iter = strTypeMap.find(strTypeTmp); |
| 335 | if (iter == strTypeMap.end()) |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 336 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 337 | std::cerr << "Wrong string type, STRING_TYPE=" << strTypeTmp.c_str() |
| 338 | << " ATTRIBUTE_NAME=" << attr.c_str() << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | uint8_t strType = iter->second; |
| 342 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 343 | uint16_t minStrLen = jsonEntry.value("minimum_string_length", 0); |
| 344 | uint16_t maxStrLen = jsonEntry.value("maximum_string_length", 0); |
| 345 | uint16_t defaultStrLen = jsonEntry.value("default_string_length", 0); |
| 346 | std::string defaultStr = jsonEntry.value("default_string", ""); |
| 347 | |
| 348 | pldm_bios_table_attr_entry_string_info info = { |
| 349 | 0, /* name handle */ |
| 350 | false, /* read only */ |
| 351 | strType, minStrLen, maxStrLen, defaultStrLen, defaultStr.data(), |
| 352 | }; |
| 353 | |
| 354 | const char* errmsg; |
| 355 | auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg); |
| 356 | if (rc != PLDM_SUCCESS) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 357 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 358 | std::cerr << "Wrong filed for string attribute, ATTRIBUTE_NAME=" |
| 359 | << attr.c_str() << " ERRMSG=" << errmsg |
| 360 | << " MINIMUM_STRING_LENGTH=" << minStrLen |
| 361 | << " MAXIMUM_STRING_LENGTH=" << maxStrLen |
| 362 | << " DEFAULT_STRING_LENGTH=" << defaultStrLen |
| 363 | << " DEFAULT_STRING=" << defaultStr.data() << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 364 | return -1; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 365 | } |
| 366 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 367 | // Defaulting all the types of attributes to BIOSString |
| 368 | internal::valueMap.emplace( |
| 369 | std::move(attr), |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 370 | std::make_tuple(jsonEntry.count("dbus") == 0, strType, minStrLen, |
| 371 | maxStrLen, defaultStrLen, std::move(defaultStr))); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 372 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 373 | return 0; |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | const AttrValuesMap& getValues() |
| 377 | { |
| 378 | return internal::valueMap; |
| 379 | } |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 380 | |
| 381 | std::string getAttrValue(const AttrName& attrName) |
| 382 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 383 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 384 | std::variant<std::string> propValue; |
| 385 | |
| 386 | if (dBusMap == std::nullopt) |
| 387 | { // return default string |
| 388 | const auto& valueEntry = internal::valueMap.at(attrName); |
| 389 | return std::get<DefaultStr>(valueEntry); |
| 390 | } |
| 391 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 392 | return pldm::utils::DBusHandler().getDbusProperty<std::string>( |
John Wang | 8b85f52 | 2019-10-17 19:28:19 +0800 | [diff] [blame] | 393 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 394 | dBusMap->interface.c_str()); |
Carol Wang | b503f9e | 2019-09-02 16:34:10 +0800 | [diff] [blame] | 395 | } |
| 396 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 397 | std::string stringToUtf8(BIOSStringEncoding stringType, |
| 398 | const std::vector<uint8_t>& data) |
| 399 | { |
| 400 | switch (stringType) |
| 401 | { |
| 402 | case ASCII: |
| 403 | case UTF_8: |
| 404 | case HEX: |
| 405 | return std::string(data.begin(), data.end()); |
| 406 | case UTF_16BE: |
| 407 | case UTF_16LE: // TODO |
| 408 | return std::string(data.begin(), data.end()); |
| 409 | case VENDOR_SPECIFIC: |
| 410 | throw std::invalid_argument("Vendor Specific is unsupported"); |
| 411 | case UNKNOWN: |
| 412 | throw std::invalid_argument("Unknown String Type"); |
| 413 | } |
| 414 | throw std::invalid_argument("String Type Error"); |
| 415 | } |
| 416 | |
| 417 | int setAttrValue(const AttrName& attrName, |
| 418 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 419 | const pldm_bios_attr_table_entry* attrEntry, |
| 420 | const BIOSStringTable&) |
| 421 | { |
| 422 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 423 | if (dBusMap == std::nullopt) |
| 424 | { |
| 425 | return PLDM_SUCCESS; |
| 426 | } |
| 427 | |
| 428 | auto stringType = |
| 429 | pldm_bios_table_attr_entry_string_decode_string_type(attrEntry); |
| 430 | |
| 431 | variable_field currentString{}; |
| 432 | pldm_bios_table_attr_value_entry_string_decode_string(attrValueEntry, |
| 433 | ¤tString); |
| 434 | std::vector<uint8_t> data(currentString.ptr, |
| 435 | currentString.ptr + currentString.length); |
| 436 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 437 | PropertyValue value = |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 438 | stringToUtf8(static_cast<BIOSStringEncoding>(stringType), data); |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 439 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), value); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 440 | |
| 441 | return PLDM_SUCCESS; |
| 442 | } |
| 443 | |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 444 | } // namespace bios_string |
| 445 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 446 | namespace bios_integer |
| 447 | { |
| 448 | |
| 449 | AttrValuesMap valueMap; |
| 450 | |
| 451 | int setup(const Json& jsonEntry) |
| 452 | { |
| 453 | |
| 454 | std::string attr = jsonEntry.value("attribute_name", ""); |
| 455 | // Transfer string type from string to enum |
| 456 | |
| 457 | uint64_t lowerBound = jsonEntry.value("lower_bound", 0); |
| 458 | uint64_t upperBound = jsonEntry.value("upper_bound", 0); |
| 459 | uint32_t scalarIncrement = jsonEntry.value("scalar_increment", 1); |
| 460 | uint64_t defaultValue = jsonEntry.value("default_value", 0); |
| 461 | pldm_bios_table_attr_entry_integer_info info = { |
| 462 | 0, /* name handle*/ |
| 463 | false, /* read only */ |
| 464 | lowerBound, upperBound, scalarIncrement, defaultValue, |
| 465 | }; |
| 466 | const char* errmsg = nullptr; |
| 467 | auto rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg); |
| 468 | if (rc != PLDM_SUCCESS) |
| 469 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 470 | std::cerr << "Wrong filed for integer attribute, ATTRIBUTE_NAME=" |
| 471 | << attr.c_str() << " ERRMSG=" << errmsg |
| 472 | << " LOWER_BOUND=" << lowerBound |
| 473 | << " UPPER_BOUND=" << upperBound |
| 474 | << " DEFAULT_VALUE=" << defaultValue |
| 475 | << " SCALAR_INCREMENT=" << scalarIncrement << "\n"; |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | valueMap.emplace(std::move(attr), |
| 480 | std::make_tuple(jsonEntry.count("dbus") == 0, lowerBound, |
| 481 | upperBound, scalarIncrement, |
| 482 | defaultValue)); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | const AttrValuesMap& getValues() |
| 488 | { |
| 489 | return valueMap; |
| 490 | } |
| 491 | |
| 492 | uint64_t getAttrValue(const AttrName& attrName) |
| 493 | { |
| 494 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 495 | std::variant<std::string> propValue; |
| 496 | |
| 497 | if (dBusMap == std::nullopt) |
| 498 | { // return default string |
| 499 | const auto& valueEntry = valueMap.at(attrName); |
| 500 | return std::get<AttrDefaultValue>(valueEntry); |
| 501 | } |
| 502 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 503 | return pldm::utils::DBusHandler().getDbusProperty<uint64_t>( |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 504 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 505 | dBusMap->interface.c_str()); |
| 506 | } |
| 507 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 508 | int setAttrValue(const AttrName& attrName, |
| 509 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 510 | const pldm_bios_attr_table_entry*, const BIOSStringTable&) |
| 511 | { |
| 512 | const auto& dBusMap = BIOSAttrLookup.at(attrName); |
| 513 | if (dBusMap == std::nullopt) |
| 514 | { |
| 515 | return PLDM_SUCCESS; |
| 516 | } |
| 517 | |
| 518 | uint64_t currentValue = |
| 519 | pldm_bios_table_attr_value_entry_integer_decode_cv(attrValueEntry); |
| 520 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 521 | PropertyValue value = static_cast<uint64_t>(currentValue); |
| 522 | pldm::utils::DBusHandler().setDbusProperty(dBusMap.value(), value); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 523 | |
| 524 | return PLDM_SUCCESS; |
| 525 | } |
| 526 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 527 | } // namespace bios_integer |
| 528 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 529 | const std::map<BIOSJsonName, BIOSStringHandler> BIOSStringHandlers = { |
| 530 | {bIOSEnumJson, bios_enum::setupBIOSStrings}, |
| 531 | }; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 532 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 533 | const std::map<BIOSJsonName, typeHandler> BIOSTypeHandlers = { |
| 534 | {bIOSEnumJson, bios_enum::setup}, |
| 535 | {bIOSStrJson, bios_string::setup}, |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 536 | {bIOSIntegerJson, bios_integer::setup}, |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | void setupBIOSStrings(const BIOSJsonName& jsonName, const Json& entry, |
| 540 | Strings& strings) |
| 541 | { |
| 542 | strings.emplace_back(entry.value("attribute_name", "")); |
| 543 | auto iter = BIOSStringHandlers.find(jsonName); |
| 544 | if (iter != BIOSStringHandlers.end()) |
| 545 | { |
| 546 | iter->second(entry, strings); |
| 547 | } |
| 548 | } |
| 549 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 550 | void setupBIOSAttrLookup(const Json& jsonEntry, AttrLookup& lookup) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 551 | { |
| 552 | std::optional<DBusMapping> dBusMap; |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 553 | std::string attrName = jsonEntry.value("attribute_name", ""); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 554 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 555 | if (jsonEntry.count("dbus") != 0) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 556 | { |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 557 | auto dBusEntry = jsonEntry.value("dbus", emptyJson); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 558 | std::string objectPath = dBusEntry.value("object_path", ""); |
| 559 | std::string interface = dBusEntry.value("interface", ""); |
| 560 | std::string propertyName = dBusEntry.value("property_name", ""); |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 561 | std::string propertyType = dBusEntry.value("property_type", ""); |
| 562 | if (!objectPath.empty() && !interface.empty() && |
| 563 | !propertyName.empty() && |
| 564 | (SupportedDbusPropertyTypes.find(propertyType) != |
| 565 | SupportedDbusPropertyTypes.end())) |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 566 | { |
| 567 | dBusMap = std::optional<DBusMapping>( |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 568 | {objectPath, interface, propertyName, propertyType}); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 569 | } |
| 570 | else |
| 571 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 572 | std::cerr << "Invalid dbus config, OBJPATH=" |
| 573 | << dBusMap->objectPath.c_str() |
| 574 | << " INTERFACE=" << dBusMap->interface.c_str() |
| 575 | << " PROPERTY_NAME=" << dBusMap->propertyName.c_str() |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 576 | << " PROPERTY_TYPE=" << dBusMap->propertyType.c_str() |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 577 | << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | lookup.emplace(attrName, dBusMap); |
| 581 | } |
| 582 | |
| 583 | int setupBIOSType(const BIOSJsonName& jsonName, const Json& entry) |
| 584 | { |
| 585 | auto iter = BIOSTypeHandlers.find(jsonName); |
| 586 | if (iter != BIOSTypeHandlers.end()) |
| 587 | { |
| 588 | iter->second(entry); |
| 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 593 | const std::vector<BIOSJsonName> BIOSConfigFiles = {bIOSEnumJson, bIOSStrJson, |
| 594 | bIOSIntegerJson}; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 595 | |
| 596 | int setupConfig(const char* dirPath) |
| 597 | { |
| 598 | if (!BIOSStrings.empty() && !BIOSAttrLookup.empty()) |
| 599 | { |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | fs::path dir(dirPath); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 604 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 605 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 606 | std::cerr << "BIOS config directory does not exist or empty, DIR=" |
| 607 | << dirPath << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 608 | return -1; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 609 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 610 | for (auto jsonName : BIOSConfigFiles) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 611 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 612 | Json json; |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 613 | if (parseBIOSJsonFile(dir, jsonName, json) < 0) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 614 | { |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 615 | continue; |
| 616 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 617 | auto entries = json.value("entries", emptyJsonList); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 618 | for (auto& entry : entries) |
| 619 | { |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 620 | setupBIOSStrings(jsonName, entry, BIOSStrings); |
| 621 | setupBIOSAttrLookup(entry, BIOSAttrLookup); |
| 622 | setupBIOSType(jsonName, entry); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 623 | } |
| 624 | } |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 625 | if (BIOSStrings.empty()) |
| 626 | { // means there is no attribute |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 627 | std::cerr << "No attribute is found in the config directory, DIR=" |
| 628 | << dirPath << "\n"; |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 629 | return -1; |
| 630 | } |
| 631 | return 0; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 632 | } |
| 633 | |
John Wang | 0df0e04 | 2020-01-16 10:01:36 +0800 | [diff] [blame] | 634 | using setAttrValueHandler = std::function<int( |
| 635 | const AttrName&, const pldm_bios_attr_val_table_entry*, |
| 636 | const pldm_bios_attr_table_entry*, const BIOSStringTable&)>; |
| 637 | |
| 638 | const std::map<AttrType, setAttrValueHandler> SetAttrValueMap{{ |
| 639 | {PLDM_BIOS_STRING, bios_string::setAttrValue}, |
| 640 | {PLDM_BIOS_STRING_READ_ONLY, bios_string::setAttrValue}, |
| 641 | {PLDM_BIOS_ENUMERATION, bios_enum::setAttrValue}, |
| 642 | {PLDM_BIOS_ENUMERATION_READ_ONLY, bios_enum::setAttrValue}, |
| 643 | {PLDM_BIOS_INTEGER, bios_integer::setAttrValue}, |
| 644 | {PLDM_BIOS_INTEGER_READ_ONLY, bios_integer::setAttrValue}, |
| 645 | |
| 646 | }}; |
| 647 | |
| 648 | int setAttributeValueOnDbus(const variable_field* attributeData, |
| 649 | const BIOSTable& biosAttributeTable, |
| 650 | const BIOSStringTable& stringTable) |
| 651 | { |
| 652 | Table attributeTable; |
| 653 | biosAttributeTable.load(attributeTable); |
| 654 | auto attrValueEntry = |
| 655 | reinterpret_cast<const pldm_bios_attr_val_table_entry*>( |
| 656 | attributeData->ptr); |
| 657 | |
| 658 | auto attrType = |
| 659 | pldm_bios_table_attr_value_entry_decode_attribute_type(attrValueEntry); |
| 660 | auto attrHandle = pldm_bios_table_attr_value_entry_decode_attribute_handle( |
| 661 | attrValueEntry); |
| 662 | |
| 663 | auto attrEntry = pldm_bios_table_attr_find_by_handle( |
| 664 | attributeTable.data(), attributeTable.size(), attrHandle); |
| 665 | |
| 666 | assert(attrEntry != nullptr); |
| 667 | |
| 668 | auto attrNameHandle = |
| 669 | pldm_bios_table_attr_entry_decode_string_handle(attrEntry); |
| 670 | |
| 671 | auto attrName = stringTable.findString(attrNameHandle); |
| 672 | |
| 673 | try |
| 674 | { |
| 675 | auto rc = SetAttrValueMap.at(attrType)(attrName, attrValueEntry, |
| 676 | attrEntry, stringTable); |
| 677 | return rc; |
| 678 | } |
| 679 | catch (const std::exception& e) |
| 680 | { |
| 681 | std::cerr << "setAttributeValueOnDbus Error: " << e.what() << std::endl; |
| 682 | return PLDM_ERROR; |
| 683 | } |
| 684 | } |
| 685 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 686 | } // namespace bios_parser |