blob: 4c46b9aeb452284f3d6c9d0edbba24264cbc34df [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#include "bios.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
Sampa Misra032bd502019-03-06 05:03:22 -06004
Xiaochao Ma60227a02019-12-04 09:00:12 +08005#include <time.h>
6
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
8
Sampa Misra032bd502019-03-06 05:03:22 -06009#include <array>
10#include <chrono>
11#include <ctime>
Sampa Misraaa8ae722019-12-12 03:20:40 -060012#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060013#include <stdexcept>
14#include <string>
15#include <variant>
16#include <vector>
17
Riya Dixit49cfb132023-03-02 04:26:53 -060018PHOSPHOR_LOG2_USING;
19
Brad Bishop5079ac42021-08-19 18:35:06 -040020using namespace pldm::utils;
21
Sampa Misra032bd502019-03-06 05:03:22 -060022namespace pldm
23{
Sampa Misra032bd502019-03-06 05:03:22 -060024namespace responder
25{
Sampa Misra032bd502019-03-06 05:03:22 -060026namespace utils
27{
Sampa Misra032bd502019-03-06 05:03:22 -060028void epochToBCDTime(uint64_t timeSec, uint8_t& seconds, uint8_t& minutes,
29 uint8_t& hours, uint8_t& day, uint8_t& month,
30 uint16_t& year)
31{
32 auto t = time_t(timeSec);
33 auto time = localtime(&t);
34
George Liu83409572019-12-24 18:42:54 +080035 seconds = pldm::utils::decimalToBcd(time->tm_sec);
36 minutes = pldm::utils::decimalToBcd(time->tm_min);
37 hours = pldm::utils::decimalToBcd(time->tm_hour);
38 day = pldm::utils::decimalToBcd(time->tm_mday);
39 month = pldm::utils::decimalToBcd(time->tm_mon +
40 1); // The number of months in the range
41 // 0 to 11.PLDM expects range 1 to 12
42 year = pldm::utils::decimalToBcd(time->tm_year +
43 1900); // The number of years since 1900
Sampa Misra032bd502019-03-06 05:03:22 -060044}
45
Xiaochao Ma60227a02019-12-04 09:00:12 +080046std::time_t timeToEpoch(uint8_t seconds, uint8_t minutes, uint8_t hours,
47 uint8_t day, uint8_t month, uint16_t year)
48{
49 struct std::tm stm;
50
51 stm.tm_year = year - 1900;
52 stm.tm_mon = month - 1;
53 stm.tm_mday = day;
54 stm.tm_hour = hours;
55 stm.tm_min = minutes;
56 stm.tm_sec = seconds;
57 stm.tm_isdst = -1;
58
59 // It will get the time in seconds since
60 // Epoch, 1970.1.1 00:00:00 +0000,UTC.
61 return timegm(&stm);
62}
63
Sampa Misra032bd502019-03-06 05:03:22 -060064} // namespace utils
65
Deepak Kodihallibc669f12019-11-28 08:52:07 -060066namespace bios
67{
John Wang6080aae2020-02-14 09:34:25 +080068using EpochTimeUS = uint64_t;
69
70DBusHandler dbusHandler;
71
Andrew Jefferya330b2f2023-05-04 14:55:37 +093072Handler::Handler(int fd, uint8_t eid, pldm::InstanceIdDb* instanceIdDb,
Sampa Misrac0c79482021-06-02 08:01:54 -050073 pldm::requester::Handler<pldm::requester::Request>* handler) :
Tom Joseph7f839f92020-09-21 10:20:44 +053074 biosConfig(BIOS_JSONS_DIR, BIOS_TABLES_DIR, &dbusHandler, fd, eid,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093075 instanceIdDb, handler)
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060076{
John Wang6080aae2020-02-14 09:34:25 +080077 biosConfig.removeTables();
78 biosConfig.buildTables();
79
Xiaochao Ma60227a02019-12-04 09:00:12 +080080 handlers.emplace(PLDM_SET_DATE_TIME,
81 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050082 return this->setDateTime(request, payloadLength);
83 });
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060084 handlers.emplace(PLDM_GET_DATE_TIME,
85 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050086 return this->getDateTime(request, payloadLength);
87 });
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060088 handlers.emplace(PLDM_GET_BIOS_TABLE,
89 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050090 return this->getBIOSTable(request, payloadLength);
91 });
George Liu1b180d82020-07-23 14:01:58 +080092 handlers.emplace(PLDM_SET_BIOS_TABLE,
93 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050094 return this->setBIOSTable(request, payloadLength);
95 });
John Wang8721ed62019-12-05 14:44:43 +080096 handlers.emplace(PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
97 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050098 return this->getBIOSAttributeCurrentValueByHandle(request,
99 payloadLength);
100 });
John Wang42174882019-12-20 14:56:59 +0800101 handlers.emplace(PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE,
102 [this](const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500103 return this->setBIOSAttributeCurrentValue(request, payloadLength);
104 });
Deepak Kodihallid9fb1522019-11-28 10:06:34 -0600105}
106
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600107Response Handler::getDateTime(const pldm_msg* request, size_t /*payloadLength*/)
Sampa Misra032bd502019-03-06 05:03:22 -0600108{
109 uint8_t seconds = 0;
110 uint8_t minutes = 0;
111 uint8_t hours = 0;
112 uint8_t day = 0;
113 uint8_t month = 0;
114 uint16_t year = 0;
115
116 constexpr auto timeInterface = "xyz.openbmc_project.Time.EpochTime";
George Liufd972642020-04-12 12:08:23 +0800117 constexpr auto bmcTimePath = "/xyz/openbmc_project/time/bmc";
vkaverapa6575b82019-04-03 05:33:52 -0500118 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES, 0);
119 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liu782d37f2020-01-24 09:02:17 +0800120 EpochTimeUS timeUsec;
Sampa Misra032bd502019-03-06 05:03:22 -0600121
Sampa Misra032bd502019-03-06 05:03:22 -0600122 try
123 {
George Liu782d37f2020-01-24 09:02:17 +0800124 timeUsec = pldm::utils::DBusHandler().getDbusProperty<EpochTimeUS>(
George Liufd972642020-04-12 12:08:23 +0800125 bmcTimePath, "Elapsed", timeInterface);
Sampa Misra032bd502019-03-06 05:03:22 -0600126 }
Patrick Williams84b790c2022-07-22 19:26:56 -0500127 catch (const sdbusplus::exception_t& e)
Sampa Misra032bd502019-03-06 05:03:22 -0600128 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600129 error(
130 "Error getting time, PATH={BMC_TIME_PATH} TIME INTERACE={TIME_INTERFACE}",
131 "BMC_TIME_PATH", bmcTimePath, "TIME_INTERFACE", timeInterface);
Sampa Misra032bd502019-03-06 05:03:22 -0600132
George Liufb8611d2019-12-06 10:14:15 +0800133 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Sampa Misra032bd502019-03-06 05:03:22 -0600134 }
135
Sampa Misra032bd502019-03-06 05:03:22 -0600136 uint64_t timeSec = std::chrono::duration_cast<std::chrono::seconds>(
137 std::chrono::microseconds(timeUsec))
138 .count();
139
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600140 pldm::responder::utils::epochToBCDTime(timeSec, seconds, minutes, hours,
141 day, month, year);
Sampa Misra032bd502019-03-06 05:03:22 -0600142
George Liufb8611d2019-12-06 10:14:15 +0800143 auto rc = encode_get_date_time_resp(request->hdr.instance_id, PLDM_SUCCESS,
144 seconds, minutes, hours, day, month,
145 year, responsePtr);
146 if (rc != PLDM_SUCCESS)
147 {
148 return ccOnlyResponse(request, rc);
149 }
150
vkaverapa6575b82019-04-03 05:33:52 -0500151 return response;
Sampa Misra032bd502019-03-06 05:03:22 -0600152}
153
Xiaochao Ma60227a02019-12-04 09:00:12 +0800154Response Handler::setDateTime(const pldm_msg* request, size_t payloadLength)
155{
156 uint8_t seconds = 0;
157 uint8_t minutes = 0;
158 uint8_t hours = 0;
159 uint8_t day = 0;
160 uint8_t month = 0;
161 uint16_t year = 0;
162 std::time_t timeSec;
163
Pavithra Barithaya297720a2022-09-12 05:50:38 -0500164 constexpr auto timeSyncPath = "/xyz/openbmc_project/time/sync_method";
165 constexpr auto timeSyncInterface =
166 "xyz.openbmc_project.Time.Synchronization";
167 constexpr auto timeSyncProperty = "TimeSyncMethod";
168
169 // The time is correct on BMC when in NTP mode, so we do not want to
170 // try and set the time again and cause potential time drifts.
171 try
172 {
173 auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
174 timeSyncPath, timeSyncProperty, timeSyncInterface);
175 const auto& mode = std::get<std::string>(propVal);
176
177 if (mode == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
178 {
179 return ccOnlyResponse(request, PLDM_SUCCESS);
180 }
181 }
182 catch (const std::exception& e)
183 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600184 error(
185 "Error getting the time sync property, PATH={TIME_SYNC_PATH} INTERFACE={SYNC_INTERFACE} PROPERTY={SYNC_PROP} ERROR={ERR_EXCEP}",
186 "TIME_SYNC_PATH", timeSyncPath, "SYNC_INTERFACE", timeSyncInterface,
187 "SYNC_PROP", timeSyncProperty, "ERR_EXCEP", e.what());
Pavithra Barithaya297720a2022-09-12 05:50:38 -0500188 }
189
Xiaochao Ma60227a02019-12-04 09:00:12 +0800190 constexpr auto setTimeInterface = "xyz.openbmc_project.Time.EpochTime";
George Liufd972642020-04-12 12:08:23 +0800191 constexpr auto setTimePath = "/xyz/openbmc_project/time/bmc";
Xiaochao Ma60227a02019-12-04 09:00:12 +0800192 constexpr auto timeSetPro = "Elapsed";
193
194 auto rc = decode_set_date_time_req(request, payloadLength, &seconds,
195 &minutes, &hours, &day, &month, &year);
196 if (rc != PLDM_SUCCESS)
197 {
198 return ccOnlyResponse(request, rc);
199 }
200 timeSec = pldm::responder::utils::timeToEpoch(seconds, minutes, hours, day,
201 month, year);
202 uint64_t timeUsec = std::chrono::duration_cast<std::chrono::microseconds>(
203 std::chrono::seconds(timeSec))
204 .count();
George Liu1e44c732020-02-28 20:20:06 +0800205 PropertyValue value{timeUsec};
Xiaochao Ma60227a02019-12-04 09:00:12 +0800206 try
207 {
George Liu1e44c732020-02-28 20:20:06 +0800208 DBusMapping dbusMapping{setTimePath, setTimeInterface, timeSetPro,
209 "uint64_t"};
210 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Xiaochao Ma60227a02019-12-04 09:00:12 +0800211 }
Patrick Williams51330582021-10-06 12:48:56 -0500212 catch (const std::exception& e)
Xiaochao Ma60227a02019-12-04 09:00:12 +0800213 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600214 error(
215 "Error Setting time,PATH={SET_TIME_PATH} TIME INTERFACE={TIME_INTERFACE} ERROR={ERR_EXCEP}",
216 "SET_TIME_PATH", setTimePath, "TIME_INTERFACE", setTimeInterface,
217 "ERR_EXCEP", e.what());
Xiaochao Ma60227a02019-12-04 09:00:12 +0800218 return ccOnlyResponse(request, PLDM_ERROR);
219 }
220
221 return ccOnlyResponse(request, PLDM_SUCCESS);
222}
223
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600224Response Handler::getBIOSTable(const pldm_msg* request, size_t payloadLength)
Sampa Misrab37be312019-07-03 02:26:41 -0500225{
John Wang6080aae2020-02-14 09:34:25 +0800226 uint32_t transferHandle{};
227 uint8_t transferOpFlag{};
228 uint8_t tableType{};
229
230 auto rc = decode_get_bios_table_req(request, payloadLength, &transferHandle,
231 &transferOpFlag, &tableType);
232 if (rc != PLDM_SUCCESS)
233 {
234 return ccOnlyResponse(request, rc);
235 }
236
237 auto table =
238 biosConfig.getBIOSTable(static_cast<pldm_bios_table_types>(tableType));
239 if (!table)
240 {
241 return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
242 }
243
244 Response response(sizeof(pldm_msg_hdr) +
245 PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + table->size());
246 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
247
248 rc = encode_get_bios_table_resp(
249 request->hdr.instance_id, PLDM_SUCCESS, 0 /* nxtTransferHandle */,
250 PLDM_START_AND_END, table->data(), response.size(), responsePtr);
251 if (rc != PLDM_SUCCESS)
252 {
253 return ccOnlyResponse(request, rc);
254 }
Sampa Misrab37be312019-07-03 02:26:41 -0500255
256 return response;
257}
258
George Liu1b180d82020-07-23 14:01:58 +0800259Response Handler::setBIOSTable(const pldm_msg* request, size_t payloadLength)
260{
261 uint32_t transferHandle{};
262 uint8_t transferOpFlag{};
263 uint8_t tableType{};
264 struct variable_field field;
265
266 auto rc = decode_set_bios_table_req(request, payloadLength, &transferHandle,
267 &transferOpFlag, &tableType, &field);
268 if (rc != PLDM_SUCCESS)
269 {
270 return ccOnlyResponse(request, rc);
271 }
272
273 Table table(field.ptr, field.ptr + field.length);
274 rc = biosConfig.setBIOSTable(tableType, table);
275 if (rc != PLDM_SUCCESS)
276 {
277 return ccOnlyResponse(request, rc);
278 }
279
280 Response response(sizeof(pldm_msg_hdr) + PLDM_SET_BIOS_TABLE_RESP_BYTES);
281 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
282
283 rc = encode_set_bios_table_resp(request->hdr.instance_id, PLDM_SUCCESS,
284 0 /* nxtTransferHandle */, responsePtr);
285 if (rc != PLDM_SUCCESS)
286 {
287 return ccOnlyResponse(request, rc);
288 }
289
290 return response;
291}
292
John Wang8721ed62019-12-05 14:44:43 +0800293Response Handler::getBIOSAttributeCurrentValueByHandle(const pldm_msg* request,
294 size_t payloadLength)
295{
296 uint32_t transferHandle;
297 uint8_t transferOpFlag;
298 uint16_t attributeHandle;
299
300 auto rc = decode_get_bios_attribute_current_value_by_handle_req(
301 request, payloadLength, &transferHandle, &transferOpFlag,
302 &attributeHandle);
303 if (rc != PLDM_SUCCESS)
304 {
305 return ccOnlyResponse(request, rc);
306 }
307
John Wang6080aae2020-02-14 09:34:25 +0800308 auto table = biosConfig.getBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE);
309 if (!table)
Deepak Kodihalli4976a682020-01-07 05:48:29 -0600310 {
311 return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
312 }
313
John Wang8721ed62019-12-05 14:44:43 +0800314 auto entry = pldm_bios_table_attr_value_find_by_handle(
John Wang6080aae2020-02-14 09:34:25 +0800315 table->data(), table->size(), attributeHandle);
John Wang8721ed62019-12-05 14:44:43 +0800316 if (entry == nullptr)
317 {
318 return ccOnlyResponse(request, PLDM_INVALID_BIOS_ATTR_HANDLE);
319 }
320
John Wang8e877e02020-02-03 16:06:55 +0800321 auto entryLength = pldm_bios_table_attr_value_entry_length(entry);
John Wang8721ed62019-12-05 14:44:43 +0800322 Response response(sizeof(pldm_msg_hdr) +
323 PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES +
John Wang8e877e02020-02-03 16:06:55 +0800324 entryLength,
John Wang8721ed62019-12-05 14:44:43 +0800325 0);
326 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800327 rc = encode_get_bios_current_value_by_handle_resp(
John Wang8e877e02020-02-03 16:06:55 +0800328 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
329 reinterpret_cast<const uint8_t*>(entry), entryLength, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800330 if (rc != PLDM_SUCCESS)
331 {
332 return ccOnlyResponse(request, rc);
333 }
John Wang8721ed62019-12-05 14:44:43 +0800334
335 return response;
336}
337
John Wang42174882019-12-20 14:56:59 +0800338Response Handler::setBIOSAttributeCurrentValue(const pldm_msg* request,
339 size_t payloadLength)
340{
341 uint32_t transferHandle;
342 uint8_t transferOpFlag;
343 variable_field attributeField;
344
345 auto rc = decode_set_bios_attribute_current_value_req(
346 request, payloadLength, &transferHandle, &transferOpFlag,
347 &attributeField);
348 if (rc != PLDM_SUCCESS)
349 {
350 return ccOnlyResponse(request, rc);
351 }
352
Sagar Srinivascac0ebb2021-11-23 07:50:28 -0600353 rc = biosConfig.setAttrValue(attributeField.ptr, attributeField.length,
354 false);
John Wang42174882019-12-20 14:56:59 +0800355
Xiaochao Ma7a0d0722020-05-29 09:07:33 +0800356 Response response(
357 sizeof(pldm_msg_hdr) + PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES, 0);
358 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
359
360 encode_set_bios_attribute_current_value_resp(request->hdr.instance_id, rc,
361 0, responsePtr);
362
363 return response;
John Wang42174882019-12-20 14:56:59 +0800364}
365
John Wangf719f3b2020-01-17 08:46:22 +0800366} // namespace bios
Sampa Misra032bd502019-03-06 05:03:22 -0600367} // namespace responder
368} // namespace pldm