blob: df0d08e54effc2bdfd8ab3f9ba4007e5b31d88cf [file] [log] [blame]
John Wangd9659342020-02-27 16:46:05 +08001#include "bios_config.hpp"
2
John Wang3be70852020-02-13 15:59:04 +08003#include "bios_enum_attribute.hpp"
John Wang95e6b3c2020-02-13 09:43:24 +08004#include "bios_integer_attribute.hpp"
John Wangd9659342020-02-27 16:46:05 +08005#include "bios_string_attribute.hpp"
Sampa Misra46ece062020-03-18 07:17:44 -05006#include "bios_table.hpp"
George Liu1b180d82020-07-23 14:01:58 +08007#include "common/bios_utils.hpp"
John Wangd9659342020-02-27 16:46:05 +08008
Riya Dixit49cfb132023-03-02 04:26:53 -06009#include <phosphor-logging/lg2.hpp>
George Liu1244acf2020-08-14 09:11:11 +080010#include <xyz/openbmc_project/BIOSConfig/Manager/server.hpp>
11
Archana Kakani62dd8ff2024-02-12 10:00:40 -060012#include <filesystem>
John Wangd9659342020-02-27 16:46:05 +080013#include <fstream>
John Wangd9659342020-02-27 16:46:05 +080014
Tom Joseph7f839f92020-09-21 10:20:44 +053015#ifdef OEM_IBM
16#include "oem/ibm/libpldmresponder/platform_oem_ibm.hpp"
17#endif
18
Riya Dixit49cfb132023-03-02 04:26:53 -060019PHOSPHOR_LOG2_USING;
20
Brad Bishop5079ac42021-08-19 18:35:06 -040021using namespace pldm::utils;
22
John Wangd9659342020-02-27 16:46:05 +080023namespace pldm
24{
25namespace responder
26{
27namespace bios
28{
29namespace
30{
George Liu1244acf2020-08-14 09:11:11 +080031using BIOSConfigManager =
32 sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager;
33
Archana Kakaniac713ee2024-05-20 01:27:53 -050034constexpr auto attributesJsonFile = "bios_attrs.json";
John Wangd9659342020-02-27 16:46:05 +080035
36constexpr auto stringTableFile = "stringTable";
37constexpr auto attrTableFile = "attributeTable";
38constexpr auto attrValueTableFile = "attributeValueTable";
39
40} // namespace
41
Sampa Misrac0c79482021-06-02 08:01:54 -050042BIOSConfig::BIOSConfig(
43 const char* jsonDir, const char* tableDir, DBusHandler* const dbusHandler,
Andrew Jeffery197033b2024-07-25 20:53:07 +093044 int /* fd */, uint8_t eid, pldm::InstanceIdDb* instanceIdDb,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050045 pldm::requester::Handler<pldm::requester::Request>* handler,
Archana Kakani62dd8ff2024-02-12 10:00:40 -060046 pldm::responder::platform_config::Handler* platformConfigHandler,
47 pldm::responder::bios::Callback requestPLDMServiceName) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040048 jsonDir(jsonDir), tableDir(tableDir), dbusHandler(dbusHandler), eid(eid),
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060049 instanceIdDb(instanceIdDb), handler(handler),
Archana Kakani62dd8ff2024-02-12 10:00:40 -060050 platformConfigHandler(platformConfigHandler),
51 requestPLDMServiceName(requestPLDMServiceName)
52{
53 fs::create_directories(tableDir);
54 removeTables();
Archana Kakani46f352e2024-03-17 08:21:08 -050055
56#ifdef SYSTEM_SPECIFIC_BIOS_JSON
57 checkSystemTypeAvailability();
58#else
59 initBIOSAttributes(sysType, false);
60#endif
61
Archana Kakani62dd8ff2024-02-12 10:00:40 -060062 listenPendingAttributes();
63}
Tom Joseph7f839f92020-09-21 10:20:44 +053064
Archana Kakani46f352e2024-03-17 08:21:08 -050065void BIOSConfig::checkSystemTypeAvailability()
John Wangd9659342020-02-27 16:46:05 +080066{
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060067 if (platformConfigHandler)
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050068 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060069 auto systemType = platformConfigHandler->getPlatformName();
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050070 if (systemType.has_value())
71 {
Archana Kakani46f352e2024-03-17 08:21:08 -050072 // Received System Type from Entity Manager
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050073 sysType = systemType.value();
Archana Kakani46f352e2024-03-17 08:21:08 -050074 initBIOSAttributes(sysType, true);
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050075 }
Archana Kakani62dd8ff2024-02-12 10:00:40 -060076 else
77 {
Archana Kakani46f352e2024-03-17 08:21:08 -050078 platformConfigHandler->registerSystemTypeCallback(
79 std::bind(&BIOSConfig::initBIOSAttributes, this,
80 std::placeholders::_1, std::placeholders::_2));
Archana Kakani62dd8ff2024-02-12 10:00:40 -060081 }
Sagar Srinivas11ce8d22022-07-28 11:32:34 -050082 }
Archana Kakani62dd8ff2024-02-12 10:00:40 -060083}
84
Archana Kakani46f352e2024-03-17 08:21:08 -050085void BIOSConfig::initBIOSAttributes(const std::string& systemType,
86 bool registerService)
Archana Kakani62dd8ff2024-02-12 10:00:40 -060087{
88 sysType = systemType;
89 fs::path dir{jsonDir / sysType};
Jayashankar Padath2ca14ce2025-09-18 02:52:19 -050090 if (!fs::exists(dir) && !fs::is_symlink(dir))
Archana Kakani62dd8ff2024-02-12 10:00:40 -060091 {
Jayashankar Padath2ca14ce2025-09-18 02:52:19 -050092 error("System specific bios attribute directory {DIR} does not exist",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050093 "DIR", dir);
Thu Nguyen3f5a9692024-07-16 13:16:54 +000094 if (registerService)
95 {
96 requestPLDMServiceName();
97 }
Archana Kakani62dd8ff2024-02-12 10:00:40 -060098 return;
99 }
John Wangd9659342020-02-27 16:46:05 +0800100 constructAttributes();
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600101 buildTables();
Archana Kakani46f352e2024-03-17 08:21:08 -0500102 if (registerService)
103 {
104 requestPLDMServiceName();
105 }
John Wangd9659342020-02-27 16:46:05 +0800106}
107
108void BIOSConfig::buildTables()
109{
John Wangd9659342020-02-27 16:46:05 +0800110 auto stringTable = buildAndStoreStringTable();
111 if (stringTable)
112 {
113 buildAndStoreAttrTables(*stringTable);
114 }
115}
116
117std::optional<Table> BIOSConfig::getBIOSTable(pldm_bios_table_types tableType)
118{
119 fs::path tablePath;
120 switch (tableType)
121 {
122 case PLDM_BIOS_STRING_TABLE:
123 tablePath = tableDir / stringTableFile;
124 break;
125 case PLDM_BIOS_ATTR_TABLE:
126 tablePath = tableDir / attrTableFile;
127 break;
128 case PLDM_BIOS_ATTR_VAL_TABLE:
129 tablePath = tableDir / attrValueTableFile;
130 break;
131 }
132 return loadTable(tablePath);
133}
134
Tom Joseph7f839f92020-09-21 10:20:44 +0530135int BIOSConfig::setBIOSTable(uint8_t tableType, const Table& table,
136 bool updateBaseBIOSTable)
George Liu1b180d82020-07-23 14:01:58 +0800137{
138 fs::path stringTablePath(tableDir / stringTableFile);
139 fs::path attrTablePath(tableDir / attrTableFile);
140 fs::path attrValueTablePath(tableDir / attrValueTableFile);
141
142 if (!pldm_bios_table_checksum(table.data(), table.size()))
143 {
144 return PLDM_INVALID_BIOS_TABLE_DATA_INTEGRITY_CHECK;
145 }
146
147 if (tableType == PLDM_BIOS_STRING_TABLE)
148 {
149 storeTable(stringTablePath, table);
150 }
151 else if (tableType == PLDM_BIOS_ATTR_TABLE)
152 {
153 BIOSTable biosStringTable(stringTablePath.c_str());
154 if (biosStringTable.isEmpty())
155 {
156 return PLDM_INVALID_BIOS_TABLE_TYPE;
157 }
158
159 auto rc = checkAttributeTable(table);
160 if (rc != PLDM_SUCCESS)
161 {
162 return rc;
163 }
164
165 storeTable(attrTablePath, table);
166 }
167 else if (tableType == PLDM_BIOS_ATTR_VAL_TABLE)
168 {
169 BIOSTable biosStringTable(stringTablePath.c_str());
170 BIOSTable biosStringValueTable(attrTablePath.c_str());
171 if (biosStringTable.isEmpty() || biosStringValueTable.isEmpty())
172 {
173 return PLDM_INVALID_BIOS_TABLE_TYPE;
174 }
175
176 auto rc = checkAttributeValueTable(table);
177 if (rc != PLDM_SUCCESS)
178 {
179 return rc;
180 }
181
182 storeTable(attrValueTablePath, table);
George Liu1b180d82020-07-23 14:01:58 +0800183 }
184 else
185 {
186 return PLDM_INVALID_BIOS_TABLE_TYPE;
187 }
188
Tom Joseph7f839f92020-09-21 10:20:44 +0530189 if ((tableType == PLDM_BIOS_ATTR_VAL_TABLE) && updateBaseBIOSTable)
George Liu1b180d82020-07-23 14:01:58 +0800190 {
George Liu1b180d82020-07-23 14:01:58 +0800191 updateBaseBIOSTableProperty();
192 }
193
194 return PLDM_SUCCESS;
195}
196
197int BIOSConfig::checkAttributeTable(const Table& table)
198{
199 using namespace pldm::bios::utils;
200 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
201 for (auto entry :
202 BIOSTableIter<PLDM_BIOS_ATTR_TABLE>(table.data(), table.size()))
203 {
204 auto attrNameHandle =
205 pldm_bios_table_attr_entry_decode_string_handle(entry);
206
207 auto stringEnty = pldm_bios_table_string_find_by_handle(
208 stringTable->data(), stringTable->size(), attrNameHandle);
209 if (stringEnty == nullptr)
210 {
211 return PLDM_INVALID_BIOS_ATTR_HANDLE;
212 }
213
214 auto attrType = static_cast<pldm_bios_attribute_type>(
215 pldm_bios_table_attr_entry_decode_attribute_type(entry));
216
217 switch (attrType)
218 {
219 case PLDM_BIOS_ENUMERATION:
220 case PLDM_BIOS_ENUMERATION_READ_ONLY:
221 {
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930222 uint8_t pvNum;
223 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery8c05ca22024-08-01 13:15:36 +0000224 pldm_bios_table_attr_entry_enum_decode_pv_num(entry, &pvNum);
George Liu1b180d82020-07-23 14:01:58 +0800225 std::vector<uint16_t> pvHandls(pvNum);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930226 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery24611052024-08-01 13:15:36 +0000227 pldm_bios_table_attr_entry_enum_decode_pv_hdls(
George Liu1b180d82020-07-23 14:01:58 +0800228 entry, pvHandls.data(), pvHandls.size());
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930229 uint8_t defNum;
Andrew Jefferyff3fb9e2024-08-01 13:15:36 +0000230 pldm_bios_table_attr_entry_enum_decode_def_num(entry, &defNum);
George Liu1b180d82020-07-23 14:01:58 +0800231 std::vector<uint8_t> defIndices(defNum);
232 pldm_bios_table_attr_entry_enum_decode_def_indices(
233 entry, defIndices.data(), defIndices.size());
234
235 for (size_t i = 0; i < pvHandls.size(); i++)
236 {
237 auto stringEntry = pldm_bios_table_string_find_by_handle(
238 stringTable->data(), stringTable->size(), pvHandls[i]);
239 if (stringEntry == nullptr)
240 {
241 return PLDM_INVALID_BIOS_ATTR_HANDLE;
242 }
243 }
244
245 for (size_t i = 0; i < defIndices.size(); i++)
246 {
247 auto stringEntry = pldm_bios_table_string_find_by_handle(
248 stringTable->data(), stringTable->size(),
249 pvHandls[defIndices[i]]);
250 if (stringEntry == nullptr)
251 {
252 return PLDM_INVALID_BIOS_ATTR_HANDLE;
253 }
254 }
255 break;
256 }
257 case PLDM_BIOS_INTEGER:
258 case PLDM_BIOS_INTEGER_READ_ONLY:
259 case PLDM_BIOS_STRING:
260 case PLDM_BIOS_STRING_READ_ONLY:
261 case PLDM_BIOS_PASSWORD:
262 case PLDM_BIOS_PASSWORD_READ_ONLY:
263 break;
264 default:
265 return PLDM_INVALID_BIOS_ATTR_HANDLE;
266 }
267 }
268
269 return PLDM_SUCCESS;
270}
271
272int BIOSConfig::checkAttributeValueTable(const Table& table)
273{
274 using namespace pldm::bios::utils;
275 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
276 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
277
278 baseBIOSTableMaps.clear();
279
280 for (auto tableEntry :
281 BIOSTableIter<PLDM_BIOS_ATTR_VAL_TABLE>(table.data(), table.size()))
282 {
283 AttributeName attributeName{};
284 AttributeType attributeType{};
285 ReadonlyStatus readonlyStatus{};
286 DisplayName displayName{};
287 Description description{};
288 MenuPath menuPath{};
289 CurrentValue currentValue{};
290 DefaultValue defaultValue{};
Sagar Srinivas7927f902023-10-09 07:53:00 -0500291 std::vector<ValueDisplayName> valueDisplayNames;
292 std::map<uint16_t, std::vector<std::string>> valueDisplayNamesMap;
George Liu1b180d82020-07-23 14:01:58 +0800293 Option options{};
294
295 auto attrValueHandle =
296 pldm_bios_table_attr_value_entry_decode_attribute_handle(
297 tableEntry);
298 auto attrType = static_cast<pldm_bios_attribute_type>(
299 pldm_bios_table_attr_value_entry_decode_attribute_type(tableEntry));
300
301 auto attrEntry = pldm_bios_table_attr_find_by_handle(
302 attrTable->data(), attrTable->size(), attrValueHandle);
303 if (attrEntry == nullptr)
304 {
305 return PLDM_INVALID_BIOS_ATTR_HANDLE;
306 }
307 auto attrHandle =
308 pldm_bios_table_attr_entry_decode_attribute_handle(attrEntry);
309 auto attrNameHandle =
310 pldm_bios_table_attr_entry_decode_string_handle(attrEntry);
311
312 auto stringEntry = pldm_bios_table_string_find_by_handle(
313 stringTable->data(), stringTable->size(), attrNameHandle);
314 if (stringEntry == nullptr)
315 {
316 return PLDM_INVALID_BIOS_ATTR_HANDLE;
317 }
318 auto strLength =
319 pldm_bios_table_string_entry_decode_string_length(stringEntry);
320 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930321 // Preconditions are upheld therefore no error check necessary
Andrew Jefferyfe1189c2024-08-01 13:15:36 +0000322 pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
323 buffer.size());
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930324
George Liu1b180d82020-07-23 14:01:58 +0800325 attributeName = std::string(buffer.data(), buffer.data() + strLength);
326
327 if (!biosAttributes.empty())
328 {
329 readonlyStatus =
George Liub1fbeec2020-09-04 09:59:46 +0800330 biosAttributes[attrHandle % biosAttributes.size()]->readOnly;
George Liu92bb4022020-09-03 14:58:24 +0800331 description =
332 biosAttributes[attrHandle % biosAttributes.size()]->helpText;
333 displayName =
334 biosAttributes[attrHandle % biosAttributes.size()]->displayName;
Sagar Srinivas7927f902023-10-09 07:53:00 -0500335 valueDisplayNamesMap =
336 biosAttributes[attrHandle % biosAttributes.size()]
337 ->valueDisplayNamesMap;
George Liu1b180d82020-07-23 14:01:58 +0800338 }
339
340 switch (attrType)
341 {
342 case PLDM_BIOS_ENUMERATION:
343 case PLDM_BIOS_ENUMERATION_READ_ONLY:
344 {
Sagar Srinivas7927f902023-10-09 07:53:00 -0500345 if (valueDisplayNamesMap.contains(attrHandle))
346 {
347 const std::vector<ValueDisplayName>& vdn =
348 valueDisplayNamesMap[attrHandle];
349 valueDisplayNames.insert(valueDisplayNames.end(),
350 vdn.begin(), vdn.end());
351 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400352 auto getValue =
353 [](uint16_t handle, const Table& table) -> std::string {
George Liu1b180d82020-07-23 14:01:58 +0800354 auto stringEntry = pldm_bios_table_string_find_by_handle(
355 table.data(), table.size(), handle);
356
357 auto strLength =
358 pldm_bios_table_string_entry_decode_string_length(
359 stringEntry);
360 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930361 // Preconditions are upheld therefore no error check
362 // necessary
Andrew Jefferyfe1189c2024-08-01 13:15:36 +0000363 pldm_bios_table_string_entry_decode_string(
George Liu1b180d82020-07-23 14:01:58 +0800364 stringEntry, buffer.data(), buffer.size());
365
366 return std::string(buffer.data(),
367 buffer.data() + strLength);
368 };
369
370 attributeType = "xyz.openbmc_project.BIOSConfig.Manager."
371 "AttributeType.Enumeration";
372
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930373 uint8_t pvNum;
374 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery8c05ca22024-08-01 13:15:36 +0000375 pldm_bios_table_attr_entry_enum_decode_pv_num(attrEntry,
376 &pvNum);
George Liu1b180d82020-07-23 14:01:58 +0800377 std::vector<uint16_t> pvHandls(pvNum);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930378 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery24611052024-08-01 13:15:36 +0000379 pldm_bios_table_attr_entry_enum_decode_pv_hdls(
George Liu1b180d82020-07-23 14:01:58 +0800380 attrEntry, pvHandls.data(), pvHandls.size());
381
382 // get possible_value
383 for (size_t i = 0; i < pvHandls.size(); i++)
384 {
385 options.push_back(
386 std::make_tuple("xyz.openbmc_project.BIOSConfig."
387 "Manager.BoundType.OneOf",
Sagar Srinivas7927f902023-10-09 07:53:00 -0500388 getValue(pvHandls[i], *stringTable),
389 valueDisplayNames[i]));
George Liu1b180d82020-07-23 14:01:58 +0800390 }
391
392 auto count =
393 pldm_bios_table_attr_value_entry_enum_decode_number(
394 tableEntry);
395 std::vector<uint8_t> handles(count);
396 pldm_bios_table_attr_value_entry_enum_decode_handles(
397 tableEntry, handles.data(), handles.size());
398
399 // get current_value
400 for (size_t i = 0; i < handles.size(); i++)
401 {
402 currentValue = getValue(pvHandls[handles[i]], *stringTable);
403 }
404
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930405 uint8_t defNum;
406 // Preconditions are upheld therefore no error check necessary
Andrew Jefferyff3fb9e2024-08-01 13:15:36 +0000407 pldm_bios_table_attr_entry_enum_decode_def_num(attrEntry,
408 &defNum);
George Liu1b180d82020-07-23 14:01:58 +0800409 std::vector<uint8_t> defIndices(defNum);
410 pldm_bios_table_attr_entry_enum_decode_def_indices(
411 attrEntry, defIndices.data(), defIndices.size());
412
413 // get default_value
414 for (size_t i = 0; i < defIndices.size(); i++)
415 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400416 defaultValue =
417 getValue(pvHandls[defIndices[i]], *stringTable);
George Liu1b180d82020-07-23 14:01:58 +0800418 }
419
420 break;
421 }
422 case PLDM_BIOS_INTEGER:
423 case PLDM_BIOS_INTEGER_READ_ONLY:
424 {
425 attributeType = "xyz.openbmc_project.BIOSConfig.Manager."
426 "AttributeType.Integer";
427 currentValue = static_cast<int64_t>(
428 pldm_bios_table_attr_value_entry_integer_decode_cv(
429 tableEntry));
430
431 uint64_t lower, upper, def;
432 uint32_t scalar;
433 pldm_bios_table_attr_entry_integer_decode(
434 attrEntry, &lower, &upper, &scalar, &def);
Sagar Srinivas7927f902023-10-09 07:53:00 -0500435 options.push_back(std::make_tuple(
436 "xyz.openbmc_project.BIOSConfig.Manager."
437 "BoundType.LowerBound",
438 static_cast<int64_t>(lower), attributeName));
439 options.push_back(std::make_tuple(
440 "xyz.openbmc_project.BIOSConfig.Manager."
441 "BoundType.UpperBound",
442 static_cast<int64_t>(upper), attributeName));
443 options.push_back(std::make_tuple(
444 "xyz.openbmc_project.BIOSConfig.Manager."
445 "BoundType.ScalarIncrement",
446 static_cast<int64_t>(scalar), attributeName));
George Liu1b180d82020-07-23 14:01:58 +0800447 defaultValue = static_cast<int64_t>(def);
448 break;
449 }
450 case PLDM_BIOS_STRING:
451 case PLDM_BIOS_STRING_READ_ONLY:
452 {
453 attributeType = "xyz.openbmc_project.BIOSConfig.Manager."
454 "AttributeType.String";
455 variable_field currentString;
456 pldm_bios_table_attr_value_entry_string_decode_string(
457 tableEntry, &currentString);
458 currentValue = std::string(
459 reinterpret_cast<const char*>(currentString.ptr),
460 currentString.length);
461 auto min = pldm_bios_table_attr_entry_string_decode_min_length(
462 attrEntry);
463 auto max = pldm_bios_table_attr_entry_string_decode_max_length(
464 attrEntry);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930465 uint16_t def;
466 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery53e342a2024-08-01 13:15:36 +0000467 pldm_bios_table_attr_entry_string_decode_def_string_length(
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930468 attrEntry, &def);
George Liu1b180d82020-07-23 14:01:58 +0800469 std::vector<char> defString(def + 1);
470 pldm_bios_table_attr_entry_string_decode_def_string(
471 attrEntry, defString.data(), defString.size());
472 options.push_back(
473 std::make_tuple("xyz.openbmc_project.BIOSConfig.Manager."
474 "BoundType.MinStringLength",
Sagar Srinivas7927f902023-10-09 07:53:00 -0500475 static_cast<int64_t>(min), attributeName));
George Liu1b180d82020-07-23 14:01:58 +0800476 options.push_back(
477 std::make_tuple("xyz.openbmc_project.BIOSConfig.Manager."
478 "BoundType.MaxStringLength",
Sagar Srinivas7927f902023-10-09 07:53:00 -0500479 static_cast<int64_t>(max), attributeName));
George Liu1b180d82020-07-23 14:01:58 +0800480 defaultValue = defString.data();
481 break;
482 }
483 case PLDM_BIOS_PASSWORD:
484 case PLDM_BIOS_PASSWORD_READ_ONLY:
485 {
486 attributeType = "xyz.openbmc_project.BIOSConfig.Manager."
487 "AttributeType.Password";
488 break;
489 }
490 default:
491 return PLDM_INVALID_BIOS_ATTR_HANDLE;
492 }
493 baseBIOSTableMaps.emplace(
494 std::move(attributeName),
495 std::make_tuple(attributeType, readonlyStatus, displayName,
496 description, menuPath, currentValue, defaultValue,
497 std::move(options)));
498 }
499
500 return PLDM_SUCCESS;
501}
502
503void BIOSConfig::updateBaseBIOSTableProperty()
504{
George Liu1b180d82020-07-23 14:01:58 +0800505 constexpr static auto dbusProperties = "org.freedesktop.DBus.Properties";
506
507 if (baseBIOSTableMaps.empty())
508 {
509 return;
510 }
511
512 try
513 {
514 auto& bus = dbusHandler->getBus();
Alexander Hansen364275c2025-11-11 12:31:42 +0100515 auto service = dbusHandler->getService(biosConfigPath,
516 BIOSConfigManager::interface);
George Liu1b180d82020-07-23 14:01:58 +0800517 auto method = bus.new_method_call(service.c_str(), biosConfigPath,
518 dbusProperties, "Set");
519 std::variant<BaseBIOSTable> value = baseBIOSTableMaps;
Archana Kakani97a0f482025-03-10 05:39:38 -0500520 if (oemBiosHandler)
521 {
522 oemBiosHandler->processOEMBaseBiosTable(baseBIOSTableMaps);
523 }
Alexander Hansen364275c2025-11-11 12:31:42 +0100524 method.append(BIOSConfigManager::interface,
525 BIOSConfigManager::property_names::base_bios_table,
526 value);
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000527 bus.call_noreply(method, dbusTimeout);
George Liu1b180d82020-07-23 14:01:58 +0800528 }
529 catch (const std::exception& e)
530 {
Riya Dixit89644442024-03-31 05:39:59 -0500531 error("Failed to update BaseBIOSTable property, error - {ERROR}",
532 "ERROR", e);
George Liu1b180d82020-07-23 14:01:58 +0800533 }
534}
535
John Wangd9659342020-02-27 16:46:05 +0800536void BIOSConfig::constructAttributes()
537{
Archana Kakaniac713ee2024-05-20 01:27:53 -0500538 info("Bios Attribute file path: {PATH}", "PATH",
539 (jsonDir / sysType / attributesJsonFile));
540 load(jsonDir / sysType / attributesJsonFile, [this](const Json& entry) {
541 std::string attrType = entry.at("attribute_type");
542 if (attrType == "string")
543 {
544 constructAttribute<BIOSStringAttribute>(entry);
545 }
546 else if (attrType == "integer")
547 {
548 constructAttribute<BIOSIntegerAttribute>(entry);
549 }
550 else if (attrType == "enum")
551 {
552 constructAttribute<BIOSEnumAttribute>(entry);
553 }
John Wang3be70852020-02-13 15:59:04 +0800554 });
John Wangd9659342020-02-27 16:46:05 +0800555}
556
557void BIOSConfig::buildAndStoreAttrTables(const Table& stringTable)
558{
559 BIOSStringTable biosStringTable(stringTable);
560
561 if (biosAttributes.empty())
562 {
563 return;
564 }
565
Tom Josephca7b2522020-11-18 12:27:11 +0530566 BaseBIOSTable biosTable{};
Tom Josephca7b2522020-11-18 12:27:11 +0530567
568 try
569 {
570 auto& bus = dbusHandler->getBus();
Alexander Hansen364275c2025-11-11 12:31:42 +0100571 auto service = dbusHandler->getService(biosConfigPath,
572 BIOSConfigManager::interface);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400573 auto method =
Alexander Hansen364275c2025-11-11 12:31:42 +0100574 bus.new_method_call(service.c_str(), biosConfigPath,
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400575 "org.freedesktop.DBus.Properties", "Get");
Alexander Hansen364275c2025-11-11 12:31:42 +0100576 method.append(BIOSConfigManager::interface,
577 BIOSConfigManager::property_names::base_bios_table);
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500578 auto reply = bus.call(method, dbusTimeout);
Tom Josephca7b2522020-11-18 12:27:11 +0530579 std::variant<BaseBIOSTable> varBiosTable{};
580 reply.read(varBiosTable);
581 biosTable = std::get<BaseBIOSTable>(varBiosTable);
582 }
583 // Failed to read the BaseBIOSTable, so update the BaseBIOSTable with the
584 // default values populated from the BIOS JSONs to keep PLDM and
585 // bios-settings-manager in sync
586 catch (const std::exception& e)
587 {
Riya Dixit89644442024-03-31 05:39:59 -0500588 error("Failed to read BaseBIOSTable property, error - {ERROR}", "ERROR",
589 e);
Tom Josephca7b2522020-11-18 12:27:11 +0530590 }
591
John Wangd9659342020-02-27 16:46:05 +0800592 Table attrTable, attrValueTable;
593
594 for (auto& attr : biosAttributes)
595 {
596 try
597 {
Tom Josephca7b2522020-11-18 12:27:11 +0530598 auto iter = biosTable.find(attr->name);
599 if (iter == biosTable.end())
600 {
601 attr->constructEntry(biosStringTable, attrTable, attrValueTable,
602 std::nullopt);
603 }
604 else
605 {
606 attr->constructEntry(
607 biosStringTable, attrTable, attrValueTable,
608 std::get<static_cast<uint8_t>(Index::currentValue)>(
609 iter->second));
610 }
John Wangd9659342020-02-27 16:46:05 +0800611 }
612 catch (const std::exception& e)
613 {
Riya Dixit89644442024-03-31 05:39:59 -0500614 error(
615 "Failed to construct table entry for attribute '{ATTRIBUTE}', error - {ERROR}",
616 "ATTRIBUTE", attr->name, "ERROR", e);
John Wangd9659342020-02-27 16:46:05 +0800617 }
618 }
619
620 table::appendPadAndChecksum(attrTable);
621 table::appendPadAndChecksum(attrValueTable);
George Liu1b180d82020-07-23 14:01:58 +0800622 setBIOSTable(PLDM_BIOS_ATTR_TABLE, attrTable);
623 setBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE, attrValueTable);
John Wangd9659342020-02-27 16:46:05 +0800624}
625
626std::optional<Table> BIOSConfig::buildAndStoreStringTable()
627{
628 std::set<std::string> strings;
Archana Kakaniac713ee2024-05-20 01:27:53 -0500629 load(jsonDir / sysType / attributesJsonFile, [&strings](const Json& entry) {
630 if (entry.at("attribute_type") == "enum")
John Wangd9659342020-02-27 16:46:05 +0800631 {
Archana Kakaniac713ee2024-05-20 01:27:53 -0500632 strings.emplace(entry.at("attribute_name"));
633 auto possibleValues = entry.at("possible_values");
634 for (auto& pv : possibleValues)
635 {
636 strings.emplace(pv);
637 }
638 }
639 else
640 {
641 strings.emplace(entry.at("attribute_name"));
John Wangd9659342020-02-27 16:46:05 +0800642 }
643 });
644
645 if (strings.empty())
646 {
647 return std::nullopt;
648 }
649
650 Table table;
651 for (const auto& elem : strings)
652 {
653 table::string::constructEntry(table, elem);
654 }
655
656 table::appendPadAndChecksum(table);
George Liu1b180d82020-07-23 14:01:58 +0800657 setBIOSTable(PLDM_BIOS_STRING_TABLE, table);
John Wangd9659342020-02-27 16:46:05 +0800658 return table;
659}
660
661void BIOSConfig::storeTable(const fs::path& path, const Table& table)
662{
663 BIOSTable biosTable(path.c_str());
664 biosTable.store(table);
665}
666
667std::optional<Table> BIOSConfig::loadTable(const fs::path& path)
668{
669 BIOSTable biosTable(path.c_str());
670 if (biosTable.isEmpty())
671 {
672 return std::nullopt;
673 }
674
675 Table table;
676 biosTable.load(table);
677 return table;
678}
679
680void BIOSConfig::load(const fs::path& filePath, ParseHandler handler)
681{
682 std::ifstream file;
683 Json jsonConf;
684 if (fs::exists(filePath))
685 {
686 try
687 {
688 file.open(filePath);
689 jsonConf = Json::parse(file);
690 auto entries = jsonConf.at("entries");
691 for (auto& entry : entries)
692 {
693 try
694 {
695 handler(entry);
696 }
697 catch (const std::exception& e)
698 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600699 error(
Riya Dixit89644442024-03-31 05:39:59 -0500700 "Failed to parse JSON config file at path '{PATH}', error - {ERROR}",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500701 "PATH", filePath, "ERROR", e);
John Wangd9659342020-02-27 16:46:05 +0800702 }
703 }
704 }
705 catch (const std::exception& e)
706 {
Riya Dixit89644442024-03-31 05:39:59 -0500707 error("Failed to parse JSON config file '{PATH}', error - {ERROR}",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500708 "PATH", filePath, "ERROR", e);
John Wangd9659342020-02-27 16:46:05 +0800709 }
710 }
711}
712
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600713std::string BIOSConfig::decodeStringFromStringEntry(
714 const pldm_bios_string_table_entry* stringEntry)
715{
716 auto strLength =
717 pldm_bios_table_string_entry_decode_string_length(stringEntry);
718 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930719 // Preconditions are upheld therefore no error check necessary
Andrew Jefferyfe1189c2024-08-01 13:15:36 +0000720 pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
721 buffer.size());
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600722 return std::string(buffer.data(), buffer.data() + strLength);
723}
724
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400725std::string BIOSConfig::displayStringHandle(
726 uint16_t handle, uint8_t index, const std::optional<Table>& attrTable,
727 const std::optional<Table>& stringTable)
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600728{
729 auto attrEntry = pldm_bios_table_attr_find_by_handle(
730 attrTable->data(), attrTable->size(), handle);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930731 uint8_t pvNum;
Andrew Jeffery8c05ca22024-08-01 13:15:36 +0000732 int rc = pldm_bios_table_attr_entry_enum_decode_pv_num(attrEntry, &pvNum);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930733 if (rc != PLDM_SUCCESS)
734 {
735 error(
Riya Dixit89644442024-03-31 05:39:59 -0500736 "Failed to decode BIOS table possible values for attribute entry, response code '{RC}'",
737 "RC", rc);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930738 throw std::runtime_error(
739 "Failed to decode BIOS table possible values for attribute entry");
740 }
741
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600742 std::vector<uint16_t> pvHandls(pvNum);
Andrew Jeffery488f19d2023-06-13 20:43:39 +0930743 // Preconditions are upheld therefore no error check necessary
Andrew Jeffery24611052024-08-01 13:15:36 +0000744 pldm_bios_table_attr_entry_enum_decode_pv_hdls(attrEntry, pvHandls.data(),
745 pvHandls.size());
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600746
747 std::string displayString = std::to_string(pvHandls[index]);
748
749 auto stringEntry = pldm_bios_table_string_find_by_handle(
750 stringTable->data(), stringTable->size(), pvHandls[index]);
751
752 auto decodedStr = decodeStringFromStringEntry(stringEntry);
753
754 return decodedStr + "(" + displayString + ")";
755}
756
757void BIOSConfig::traceBIOSUpdate(
758 const pldm_bios_attr_val_table_entry* attrValueEntry,
759 const pldm_bios_attr_table_entry* attrEntry, bool isBMC)
760{
761 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
762 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
763
Patrick Williams6da4f912023-05-10 07:50:53 -0500764 auto [attrHandle,
765 attrType] = table::attribute_value::decodeHeader(attrValueEntry);
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600766
767 auto attrHeader = table::attribute::decodeHeader(attrEntry);
768 BIOSStringTable biosStringTable(*stringTable);
769 auto attrName = biosStringTable.findString(attrHeader.stringHandle);
770
771 switch (attrType)
772 {
773 case PLDM_BIOS_ENUMERATION:
774 case PLDM_BIOS_ENUMERATION_READ_ONLY:
775 {
776 auto count = pldm_bios_table_attr_value_entry_enum_decode_number(
777 attrValueEntry);
778 std::vector<uint8_t> handles(count);
779 pldm_bios_table_attr_value_entry_enum_decode_handles(
780 attrValueEntry, handles.data(), handles.size());
781
782 for (uint8_t handle : handles)
783 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600784 auto nwVal = displayStringHandle(attrHandle, handle, attrTable,
785 stringTable);
786 auto chkBMC = isBMC ? "true" : "false";
787 info(
Riya Dixit89644442024-03-31 05:39:59 -0500788 "BIOS attribute '{ATTRIBUTE}' updated to value '{VALUE}' by BMC '{CHECK_BMC}'",
789 "ATTRIBUTE", attrName, "VALUE", nwVal, "CHECK_BMC", chkBMC);
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600790 }
791 break;
792 }
793 case PLDM_BIOS_INTEGER:
794 case PLDM_BIOS_INTEGER_READ_ONLY:
795 {
796 auto value =
797 table::attribute_value::decodeIntegerEntry(attrValueEntry);
Riya Dixit49cfb132023-03-02 04:26:53 -0600798 auto chkBMC = isBMC ? "true" : "false";
799 info(
Riya Dixit89644442024-03-31 05:39:59 -0500800 "BIOS attribute '{ATTRIBUTE}' updated to value '{VALUE}' by BMC '{CHECK_BMC}'",
801 "ATTRIBUTE", attrName, "VALUE", value, "CHECK_BMC", chkBMC);
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600802 break;
803 }
804 case PLDM_BIOS_STRING:
805 case PLDM_BIOS_STRING_READ_ONLY:
806 {
807 auto value =
808 table::attribute_value::decodeStringEntry(attrValueEntry);
Riya Dixit49cfb132023-03-02 04:26:53 -0600809 auto chkBMC = isBMC ? "true" : "false";
810 info(
Riya Dixit89644442024-03-31 05:39:59 -0500811 "BIOS attribute '{ATTRIBUTE}' updated to value '{VALUE}' by BMC '{CHECK_BMC}'",
812 "ATTRIBUTE", attrName, "VALUE", value, "CHECK_BMC", chkBMC);
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600813 break;
814 }
815 default:
816 break;
817 };
818}
819
John Wang8241b342020-06-05 10:49:17 +0800820int BIOSConfig::checkAttrValueToUpdate(
821 const pldm_bios_attr_val_table_entry* attrValueEntry,
822 const pldm_bios_attr_table_entry* attrEntry, Table&)
823
824{
Patrick Williams6da4f912023-05-10 07:50:53 -0500825 auto [attrHandle,
826 attrType] = table::attribute_value::decodeHeader(attrValueEntry);
John Wang8241b342020-06-05 10:49:17 +0800827
828 switch (attrType)
829 {
830 case PLDM_BIOS_ENUMERATION:
George Liu5bb9edb2021-08-05 20:10:32 +0800831 case PLDM_BIOS_ENUMERATION_READ_ONLY:
John Wang8241b342020-06-05 10:49:17 +0800832 {
833 auto value =
834 table::attribute_value::decodeEnumEntry(attrValueEntry);
Patrick Williams6da4f912023-05-10 07:50:53 -0500835 auto [pvHdls,
836 defIndex] = table::attribute::decodeEnumEntry(attrEntry);
Varsha Kaverappa6a4d1cf2021-11-24 21:15:42 -0600837 if (!(value.size() == 1))
838 {
839 return PLDM_ERROR_INVALID_LENGTH;
840 }
John Wang8241b342020-06-05 10:49:17 +0800841 if (value[0] >= pvHdls.size())
842 {
Riya Dixit89644442024-03-31 05:39:59 -0500843 error(
844 "Invalid index '{INDEX}' encountered for Enum type BIOS attribute",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500845 "INDEX", value[0]);
John Wang8241b342020-06-05 10:49:17 +0800846 return PLDM_ERROR_INVALID_DATA;
847 }
John Wang8241b342020-06-05 10:49:17 +0800848 return PLDM_SUCCESS;
849 }
850 case PLDM_BIOS_INTEGER:
George Liu5bb9edb2021-08-05 20:10:32 +0800851 case PLDM_BIOS_INTEGER_READ_ONLY:
John Wang8241b342020-06-05 10:49:17 +0800852 {
853 auto value =
854 table::attribute_value::decodeIntegerEntry(attrValueEntry);
Patrick Williams6da4f912023-05-10 07:50:53 -0500855 auto [lower, upper, scalar,
856 def] = table::attribute::decodeIntegerEntry(attrEntry);
John Wang8241b342020-06-05 10:49:17 +0800857
858 if (value < lower || value > upper)
859 {
Riya Dixit89644442024-03-31 05:39:59 -0500860 error(
861 "Out of range index '{ATTRIBUTE_VALUE}' encountered for Integer type BIOS attribute for the lower bound '{LOWER}', the upper bound '{UPPER}' and the scalar value '{SCALAR}'.",
862 "ATTRIBUTE_VALUE", value, "LOWER", lower, "UPPER", upper,
863 "SCALAR", scalar);
John Wang8241b342020-06-05 10:49:17 +0800864 return PLDM_ERROR_INVALID_DATA;
865 }
866 return PLDM_SUCCESS;
867 }
868 case PLDM_BIOS_STRING:
George Liu5bb9edb2021-08-05 20:10:32 +0800869 case PLDM_BIOS_STRING_READ_ONLY:
John Wang8241b342020-06-05 10:49:17 +0800870 {
871 auto stringConf = table::attribute::decodeStringEntry(attrEntry);
872 auto value =
873 table::attribute_value::decodeStringEntry(attrValueEntry);
874 if (value.size() < stringConf.minLength ||
875 value.size() > stringConf.maxLength)
876 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600877 error(
Riya Dixit89644442024-03-31 05:39:59 -0500878 "Invalid length '{LENGTH}' encountered for string type BIOS attribute value '{ATTRIBUTE_VALUE}' when minimum string entry length '{MIN_LEN}' and maximum string entry length '{MAX_LEN}'",
879 "ATTRIBUTE_VALUE", value, "LENGTH", value.size(), "MIN_LEN",
880 stringConf.minLength, "MAX_LEN", stringConf.maxLength);
John Wang8241b342020-06-05 10:49:17 +0800881 return PLDM_ERROR_INVALID_LENGTH;
882 }
883 return PLDM_SUCCESS;
884 }
885 default:
Riya Dixit89644442024-03-31 05:39:59 -0500886 error("ReadOnly or Unsupported type '{TYPE}'", "TYPE", attrType);
John Wang8241b342020-06-05 10:49:17 +0800887 return PLDM_ERROR;
888 };
889}
890
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600891int BIOSConfig::setAttrValue(const void* entry, size_t size, bool isBMC,
892 bool updateDBus, bool updateBaseBIOSTable)
John Wangd9659342020-02-27 16:46:05 +0800893{
894 auto attrValueTable = getBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE);
895 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
896 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
897 if (!attrValueTable || !attrTable || !stringTable)
898 {
899 return PLDM_BIOS_TABLE_UNAVAILABLE;
900 }
901
John Wangd9659342020-02-27 16:46:05 +0800902 auto attrValueEntry =
903 reinterpret_cast<const pldm_bios_attr_val_table_entry*>(entry);
904
905 auto attrValHeader = table::attribute_value::decodeHeader(attrValueEntry);
906
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400907 auto attrEntry =
908 table::attribute::findByHandle(*attrTable, attrValHeader.attrHandle);
John Wangd9659342020-02-27 16:46:05 +0800909 if (!attrEntry)
910 {
911 return PLDM_ERROR;
912 }
913
John Wang8241b342020-06-05 10:49:17 +0800914 auto rc = checkAttrValueToUpdate(attrValueEntry, attrEntry, *stringTable);
915 if (rc != PLDM_SUCCESS)
916 {
917 return rc;
918 }
919
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400920 auto destTable =
921 table::attribute_value::updateTable(*attrValueTable, entry, size);
John Wang8241b342020-06-05 10:49:17 +0800922
923 if (!destTable)
924 {
925 return PLDM_ERROR;
926 }
927
John Wangd9659342020-02-27 16:46:05 +0800928 try
929 {
930 auto attrHeader = table::attribute::decodeHeader(attrEntry);
931
932 BIOSStringTable biosStringTable(*stringTable);
933 auto attrName = biosStringTable.findString(attrHeader.stringHandle);
Patrick Williamsa6756622023-10-20 11:19:15 -0500934 auto iter = std::find_if(
935 biosAttributes.begin(), biosAttributes.end(),
936 [&attrName](const auto& attr) { return attr->name == attrName; });
John Wangd9659342020-02-27 16:46:05 +0800937
938 if (iter == biosAttributes.end())
939 {
940 return PLDM_ERROR;
941 }
George Liu6d6d1e82021-02-16 11:08:55 +0800942 if (updateDBus)
943 {
944 (*iter)->setAttrValueOnDbus(attrValueEntry, attrEntry,
945 biosStringTable);
946 }
John Wangd9659342020-02-27 16:46:05 +0800947 }
948 catch (const std::exception& e)
949 {
Riya Dixit89644442024-03-31 05:39:59 -0500950 error("Set attribute value error - {ERROR}", "ERROR", e);
John Wangd9659342020-02-27 16:46:05 +0800951 return PLDM_ERROR;
952 }
953
Tom Joseph7f839f92020-09-21 10:20:44 +0530954 setBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE, *destTable, updateBaseBIOSTable);
George Liu5c3192b2020-08-13 17:35:43 +0800955
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600956 traceBIOSUpdate(attrValueEntry, attrEntry, isBMC);
957
John Wangd9659342020-02-27 16:46:05 +0800958 return PLDM_SUCCESS;
959}
960
961void BIOSConfig::removeTables()
962{
963 try
964 {
965 fs::remove(tableDir / stringTableFile);
966 fs::remove(tableDir / attrTableFile);
967 fs::remove(tableDir / attrValueTableFile);
968 }
969 catch (const std::exception& e)
970 {
Riya Dixit89644442024-03-31 05:39:59 -0500971 error("Remove the tables error - {ERROR}", "ERROR", e);
John Wangd9659342020-02-27 16:46:05 +0800972 }
973}
974
Sampa Misra46ece062020-03-18 07:17:44 -0500975void BIOSConfig::processBiosAttrChangeNotification(
976 const DbusChObjProperties& chProperties, uint32_t biosAttrIndex)
977{
978 const auto& dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
979 const auto& propertyName = dBusMap->propertyName;
980 const auto& attrName = biosAttributes[biosAttrIndex]->name;
981
982 const auto it = chProperties.find(propertyName);
983 if (it == chProperties.end())
984 {
985 return;
986 }
987
988 PropertyValue newPropVal = it->second;
989 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
990 if (!stringTable.has_value())
991 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600992 error("BIOS string table unavailable");
Sampa Misra46ece062020-03-18 07:17:44 -0500993 return;
994 }
995 BIOSStringTable biosStringTable(*stringTable);
996 uint16_t attrNameHdl{};
997 try
998 {
999 attrNameHdl = biosStringTable.findHandle(attrName);
1000 }
Patrick Williams51330582021-10-06 12:48:56 -05001001 catch (const std::invalid_argument& e)
Sampa Misra46ece062020-03-18 07:17:44 -05001002 {
Riya Dixit89644442024-03-31 05:39:59 -05001003 error(
1004 "Missing handle for attribute '{ATTRIBUTE}' in BIOS String Table, error - '{ERROR}'",
1005 "ATTRIBUTE", attrName, "ERROR", e);
Sampa Misra46ece062020-03-18 07:17:44 -05001006 return;
1007 }
1008
1009 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
1010 if (!attrTable.has_value())
1011 {
Riya Dixit89644442024-03-31 05:39:59 -05001012 error("BIOS Attribute table not present");
Sampa Misra46ece062020-03-18 07:17:44 -05001013 return;
1014 }
1015 const struct pldm_bios_attr_table_entry* tableEntry =
1016 table::attribute::findByStringHandle(*attrTable, attrNameHdl);
1017 if (tableEntry == nullptr)
1018 {
Riya Dixit49cfb132023-03-02 04:26:53 -06001019 error(
Riya Dixit89644442024-03-31 05:39:59 -05001020 "Failed to find attribute {ATTRIBUTE} in BIOS Attribute table with attribute handle '{ATTR_HANDLE}'",
1021 "ATTRIBUTE", attrName, "ATTR_HANDLE", attrNameHdl);
Sampa Misra46ece062020-03-18 07:17:44 -05001022 return;
1023 }
1024
Patrick Williams6da4f912023-05-10 07:50:53 -05001025 auto [attrHdl, attrType,
1026 stringHdl] = table::attribute::decodeHeader(tableEntry);
Sampa Misra46ece062020-03-18 07:17:44 -05001027
1028 auto attrValueSrcTable = getBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE);
1029
1030 if (!attrValueSrcTable.has_value())
1031 {
Riya Dixit49cfb132023-03-02 04:26:53 -06001032 error("Attribute value table not present");
Sampa Misra46ece062020-03-18 07:17:44 -05001033 return;
1034 }
1035
1036 Table newValue;
1037 auto rc = biosAttributes[biosAttrIndex]->updateAttrVal(
1038 newValue, attrHdl, attrType, newPropVal);
1039 if (rc != PLDM_SUCCESS)
1040 {
Riya Dixit49cfb132023-03-02 04:26:53 -06001041 error(
Riya Dixit89644442024-03-31 05:39:59 -05001042 "Failed to update the attribute value table for attribute handle '{ATTR_HANDLE}' and attribute type '{TYPE}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -05001043 "ATTR_HANDLE", attrHdl, "TYPE", attrType);
Sampa Misra46ece062020-03-18 07:17:44 -05001044 return;
1045 }
1046 auto destTable = table::attribute_value::updateTable(
1047 *attrValueSrcTable, newValue.data(), newValue.size());
1048 if (destTable.has_value())
1049 {
1050 storeTable(tableDir / attrValueTableFile, *destTable);
1051 }
Sampa Misra0f262332021-02-15 00:13:51 -06001052
Sagar Srinivascac0ebb2021-11-23 07:50:28 -06001053 rc = setAttrValue(newValue.data(), newValue.size(), true, false);
Sampa Misra0f262332021-02-15 00:13:51 -06001054 if (rc != PLDM_SUCCESS)
1055 {
Riya Dixit89644442024-03-31 05:39:59 -05001056 error(
1057 "Failed to setAttrValue on base bios table and dbus, response code '{RC}'",
1058 "RC", rc);
Sampa Misra0f262332021-02-15 00:13:51 -06001059 }
Sampa Misra46ece062020-03-18 07:17:44 -05001060}
1061
George Liu1244acf2020-08-14 09:11:11 +08001062uint16_t BIOSConfig::findAttrHandle(const std::string& attrName)
1063{
1064 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
1065 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
1066
1067 BIOSStringTable biosStringTable(*stringTable);
1068 pldm::bios::utils::BIOSTableIter<PLDM_BIOS_ATTR_TABLE> attrTableIter(
1069 attrTable->data(), attrTable->size());
1070 auto stringHandle = biosStringTable.findHandle(attrName);
1071
1072 for (auto entry : pldm::bios::utils::BIOSTableIter<PLDM_BIOS_ATTR_TABLE>(
1073 attrTable->data(), attrTable->size()))
1074 {
1075 auto header = table::attribute::decodeHeader(entry);
1076 if (header.stringHandle == stringHandle)
1077 {
1078 return header.attrHandle;
1079 }
1080 }
1081
Manojkiran Eda2576aec2024-06-17 12:05:17 +05301082 throw std::invalid_argument("Unknown attribute Name");
George Liu1244acf2020-08-14 09:11:11 +08001083}
1084
1085void BIOSConfig::constructPendingAttribute(
1086 const PendingAttributes& pendingAttributes)
1087{
Tom Joseph7f839f92020-09-21 10:20:44 +05301088 std::vector<uint16_t> listOfHandles{};
1089
George Liu1244acf2020-08-14 09:11:11 +08001090 for (auto& attribute : pendingAttributes)
1091 {
1092 std::string attributeName = attribute.first;
1093 auto& [attributeType, attributevalue] = attribute.second;
1094
1095 auto iter = std::find_if(biosAttributes.begin(), biosAttributes.end(),
1096 [&attributeName](const auto& attr) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001097 return attr->name == attributeName;
1098 });
George Liu1244acf2020-08-14 09:11:11 +08001099
1100 if (iter == biosAttributes.end())
1101 {
Riya Dixit89644442024-03-31 05:39:59 -05001102 error("Wrong attribute name {NAME}", "NAME", attributeName);
George Liu1244acf2020-08-14 09:11:11 +08001103 continue;
1104 }
1105
1106 Table attrValueEntry(sizeof(pldm_bios_attr_val_table_entry), 0);
Pavithra Barithaya677a4552025-01-31 10:33:39 +05301107 auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
George Liu1244acf2020-08-14 09:11:11 +08001108
1109 auto handler = findAttrHandle(attributeName);
1110 auto type =
1111 BIOSConfigManager::convertAttributeTypeFromString(attributeType);
1112
1113 if (type != BIOSConfigManager::AttributeType::Enumeration &&
1114 type != BIOSConfigManager::AttributeType::String &&
1115 type != BIOSConfigManager::AttributeType::Integer)
1116 {
Riya Dixit89644442024-03-31 05:39:59 -05001117 error("Attribute type '{TYPE}' not supported", "TYPE",
1118 attributeType);
George Liu1244acf2020-08-14 09:11:11 +08001119 continue;
1120 }
1121
George Liu4876c542022-06-08 15:59:54 +08001122 const auto [attrType, readonlyStatus, displayName, description,
Patrick Williams6da4f912023-05-10 07:50:53 -05001123 menuPath, currentValue, defaultValue,
1124 option] = baseBIOSTableMaps.at(attributeName);
George Liu4876c542022-06-08 15:59:54 +08001125
George Liu1244acf2020-08-14 09:11:11 +08001126 entry->attr_handle = htole16(handler);
George Liu4876c542022-06-08 15:59:54 +08001127
1128 // Need to verify that the current value has really changed
1129 if (attributeType == attrType && attributevalue != currentValue)
1130 {
1131 listOfHandles.emplace_back(htole16(handler));
1132 }
Tom Joseph7f839f92020-09-21 10:20:44 +05301133
George Liu1244acf2020-08-14 09:11:11 +08001134 (*iter)->generateAttributeEntry(attributevalue, attrValueEntry);
1135
Sagar Srinivascac0ebb2021-11-23 07:50:28 -06001136 setAttrValue(attrValueEntry.data(), attrValueEntry.size(), true);
Tom Joseph7f839f92020-09-21 10:20:44 +05301137 }
1138
1139 if (listOfHandles.size())
1140 {
1141#ifdef OEM_IBM
1142 auto rc = pldm::responder::platform::sendBiosAttributeUpdateEvent(
Andrew Jefferya330b2f2023-05-04 14:55:37 +09301143 eid, instanceIdDb, listOfHandles, handler);
Tom Joseph7f839f92020-09-21 10:20:44 +05301144 if (rc != PLDM_SUCCESS)
1145 {
1146 return;
1147 }
1148#endif
George Liu1244acf2020-08-14 09:11:11 +08001149 }
1150}
1151
1152void BIOSConfig::listenPendingAttributes()
1153{
George Liu1244acf2020-08-14 09:11:11 +08001154 using namespace sdbusplus::bus::match::rules;
Patrick Williams84b790c2022-07-22 19:26:56 -05001155 auto updateBIOSMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu1244acf2020-08-14 09:11:11 +08001156 pldm::utils::DBusHandler::getBus(),
Alexander Hansen364275c2025-11-11 12:31:42 +01001157 propertiesChanged(biosConfigPath, BIOSConfigManager::interface),
Patrick Williams84b790c2022-07-22 19:26:56 -05001158 [this](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001159 using Value =
1160 std::variant<std::string, PendingAttributes, BaseBIOSTable>;
1161 using Properties = std::map<DbusProp, Value>;
George Liu1244acf2020-08-14 09:11:11 +08001162
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001163 Properties props{};
1164 std::string intf;
1165 msg.read(intf, props);
George Liu1244acf2020-08-14 09:11:11 +08001166
Alexander Hansen364275c2025-11-11 12:31:42 +01001167 auto valPropMap = props.find(
1168 BIOSConfigManager::property_names::pending_attributes);
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001169 if (valPropMap == props.end())
1170 {
1171 return;
1172 }
George Liu1244acf2020-08-14 09:11:11 +08001173
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001174 PendingAttributes pendingAttributes =
1175 std::get<PendingAttributes>(valPropMap->second);
1176 this->constructPendingAttribute(pendingAttributes);
1177 });
George Liu1244acf2020-08-14 09:11:11 +08001178
1179 biosAttrMatch.emplace_back(std::move(updateBIOSMatch));
1180}
1181
John Wangd9659342020-02-27 16:46:05 +08001182} // namespace bios
1183} // namespace responder
1184} // namespace pldm