blob: 99f86312376571a3b90d12f558e527537f7bae46 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#include "bios.hpp"
2
George Liu83409572019-12-24 18:42:54 +08003#include "utils.hpp"
Sampa Misra032bd502019-03-06 05:03:22 -06004
Xiaochao Ma60227a02019-12-04 09:00:12 +08005#include <time.h>
6
Sampa Misra032bd502019-03-06 05:03:22 -06007#include <array>
8#include <chrono>
9#include <ctime>
Sampa Misraaa8ae722019-12-12 03:20:40 -060010#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060011#include <stdexcept>
12#include <string>
13#include <variant>
14#include <vector>
15
16namespace pldm
17{
18
Sampa Misra032bd502019-03-06 05:03:22 -060019namespace responder
20{
21
22namespace utils
23{
24
25void epochToBCDTime(uint64_t timeSec, uint8_t& seconds, uint8_t& minutes,
26 uint8_t& hours, uint8_t& day, uint8_t& month,
27 uint16_t& year)
28{
29 auto t = time_t(timeSec);
30 auto time = localtime(&t);
31
George Liu83409572019-12-24 18:42:54 +080032 seconds = pldm::utils::decimalToBcd(time->tm_sec);
33 minutes = pldm::utils::decimalToBcd(time->tm_min);
34 hours = pldm::utils::decimalToBcd(time->tm_hour);
35 day = pldm::utils::decimalToBcd(time->tm_mday);
36 month = pldm::utils::decimalToBcd(time->tm_mon +
37 1); // The number of months in the range
38 // 0 to 11.PLDM expects range 1 to 12
39 year = pldm::utils::decimalToBcd(time->tm_year +
40 1900); // The number of years since 1900
Sampa Misra032bd502019-03-06 05:03:22 -060041}
42
Xiaochao Ma60227a02019-12-04 09:00:12 +080043std::time_t timeToEpoch(uint8_t seconds, uint8_t minutes, uint8_t hours,
44 uint8_t day, uint8_t month, uint16_t year)
45{
46 struct std::tm stm;
47
48 stm.tm_year = year - 1900;
49 stm.tm_mon = month - 1;
50 stm.tm_mday = day;
51 stm.tm_hour = hours;
52 stm.tm_min = minutes;
53 stm.tm_sec = seconds;
54 stm.tm_isdst = -1;
55
56 // It will get the time in seconds since
57 // Epoch, 1970.1.1 00:00:00 +0000,UTC.
58 return timegm(&stm);
59}
60
Sampa Misra032bd502019-03-06 05:03:22 -060061} // namespace utils
62
Deepak Kodihallibc669f12019-11-28 08:52:07 -060063namespace bios
64{
65
John Wang6080aae2020-02-14 09:34:25 +080066using EpochTimeUS = uint64_t;
67
68DBusHandler dbusHandler;
69
70Handler::Handler() : biosConfig(BIOS_JSONS_DIR, BIOS_TABLES_DIR, &dbusHandler)
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060071{
John Wang6080aae2020-02-14 09:34:25 +080072 biosConfig.removeTables();
73 biosConfig.buildTables();
74
Xiaochao Ma60227a02019-12-04 09:00:12 +080075 handlers.emplace(PLDM_SET_DATE_TIME,
76 [this](const pldm_msg* request, size_t payloadLength) {
77 return this->setDateTime(request, payloadLength);
78 });
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060079 handlers.emplace(PLDM_GET_DATE_TIME,
80 [this](const pldm_msg* request, size_t payloadLength) {
81 return this->getDateTime(request, payloadLength);
82 });
83 handlers.emplace(PLDM_GET_BIOS_TABLE,
84 [this](const pldm_msg* request, size_t payloadLength) {
85 return this->getBIOSTable(request, payloadLength);
86 });
John Wang8721ed62019-12-05 14:44:43 +080087 handlers.emplace(PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
88 [this](const pldm_msg* request, size_t payloadLength) {
89 return this->getBIOSAttributeCurrentValueByHandle(
90 request, payloadLength);
91 });
John Wang42174882019-12-20 14:56:59 +080092 handlers.emplace(PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE,
93 [this](const pldm_msg* request, size_t payloadLength) {
94 return this->setBIOSAttributeCurrentValue(
95 request, payloadLength);
96 });
Deepak Kodihallid9fb1522019-11-28 10:06:34 -060097}
98
Deepak Kodihallibc669f12019-11-28 08:52:07 -060099Response Handler::getDateTime(const pldm_msg* request, size_t /*payloadLength*/)
Sampa Misra032bd502019-03-06 05:03:22 -0600100{
101 uint8_t seconds = 0;
102 uint8_t minutes = 0;
103 uint8_t hours = 0;
104 uint8_t day = 0;
105 uint8_t month = 0;
106 uint16_t year = 0;
107
108 constexpr auto timeInterface = "xyz.openbmc_project.Time.EpochTime";
George Liu408c3c42019-10-16 16:49:15 +0800109 constexpr auto hostTimePath = "/xyz/openbmc_project/time/host";
vkaverapa6575b82019-04-03 05:33:52 -0500110 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_DATE_TIME_RESP_BYTES, 0);
111 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liu782d37f2020-01-24 09:02:17 +0800112 EpochTimeUS timeUsec;
Sampa Misra032bd502019-03-06 05:03:22 -0600113
Sampa Misra032bd502019-03-06 05:03:22 -0600114 try
115 {
George Liu782d37f2020-01-24 09:02:17 +0800116 timeUsec = pldm::utils::DBusHandler().getDbusProperty<EpochTimeUS>(
117 hostTimePath, "Elapsed", timeInterface);
Sampa Misra032bd502019-03-06 05:03:22 -0600118 }
George Liu0e02c322020-01-01 09:41:51 +0800119 catch (const sdbusplus::exception::SdBusError& e)
Sampa Misra032bd502019-03-06 05:03:22 -0600120 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600121 std::cerr << "Error getting time, PATH=" << hostTimePath
122 << " TIME INTERACE=" << timeInterface << "\n";
Sampa Misra032bd502019-03-06 05:03:22 -0600123
George Liufb8611d2019-12-06 10:14:15 +0800124 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Sampa Misra032bd502019-03-06 05:03:22 -0600125 }
126
Sampa Misra032bd502019-03-06 05:03:22 -0600127 uint64_t timeSec = std::chrono::duration_cast<std::chrono::seconds>(
128 std::chrono::microseconds(timeUsec))
129 .count();
130
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600131 pldm::responder::utils::epochToBCDTime(timeSec, seconds, minutes, hours,
132 day, month, year);
Sampa Misra032bd502019-03-06 05:03:22 -0600133
George Liufb8611d2019-12-06 10:14:15 +0800134 auto rc = encode_get_date_time_resp(request->hdr.instance_id, PLDM_SUCCESS,
135 seconds, minutes, hours, day, month,
136 year, responsePtr);
137 if (rc != PLDM_SUCCESS)
138 {
139 return ccOnlyResponse(request, rc);
140 }
141
vkaverapa6575b82019-04-03 05:33:52 -0500142 return response;
Sampa Misra032bd502019-03-06 05:03:22 -0600143}
144
Xiaochao Ma60227a02019-12-04 09:00:12 +0800145Response Handler::setDateTime(const pldm_msg* request, size_t payloadLength)
146{
147 uint8_t seconds = 0;
148 uint8_t minutes = 0;
149 uint8_t hours = 0;
150 uint8_t day = 0;
151 uint8_t month = 0;
152 uint16_t year = 0;
153 std::time_t timeSec;
154
155 constexpr auto setTimeInterface = "xyz.openbmc_project.Time.EpochTime";
156 constexpr auto setTimePath = "/xyz/openbmc_project/time/host";
157 constexpr auto timeSetPro = "Elapsed";
158
159 auto rc = decode_set_date_time_req(request, payloadLength, &seconds,
160 &minutes, &hours, &day, &month, &year);
161 if (rc != PLDM_SUCCESS)
162 {
163 return ccOnlyResponse(request, rc);
164 }
165 timeSec = pldm::responder::utils::timeToEpoch(seconds, minutes, hours, day,
166 month, year);
167 uint64_t timeUsec = std::chrono::duration_cast<std::chrono::microseconds>(
168 std::chrono::seconds(timeSec))
169 .count();
George Liu1e44c732020-02-28 20:20:06 +0800170 PropertyValue value{timeUsec};
Xiaochao Ma60227a02019-12-04 09:00:12 +0800171 try
172 {
George Liu1e44c732020-02-28 20:20:06 +0800173 DBusMapping dbusMapping{setTimePath, setTimeInterface, timeSetPro,
174 "uint64_t"};
175 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
Xiaochao Ma60227a02019-12-04 09:00:12 +0800176 }
177 catch (std::exception& e)
178 {
179
180 std::cerr << "Error Setting time,PATH=" << setTimePath
181 << "TIME INTERFACE=" << setTimeInterface
182 << "ERROR=" << e.what() << "\n";
183
184 return ccOnlyResponse(request, PLDM_ERROR);
185 }
186
187 return ccOnlyResponse(request, PLDM_SUCCESS);
188}
189
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600190Response Handler::getBIOSTable(const pldm_msg* request, size_t payloadLength)
Sampa Misrab37be312019-07-03 02:26:41 -0500191{
John Wang6080aae2020-02-14 09:34:25 +0800192 uint32_t transferHandle{};
193 uint8_t transferOpFlag{};
194 uint8_t tableType{};
195
196 auto rc = decode_get_bios_table_req(request, payloadLength, &transferHandle,
197 &transferOpFlag, &tableType);
198 if (rc != PLDM_SUCCESS)
199 {
200 return ccOnlyResponse(request, rc);
201 }
202
203 auto table =
204 biosConfig.getBIOSTable(static_cast<pldm_bios_table_types>(tableType));
205 if (!table)
206 {
207 return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
208 }
209
210 Response response(sizeof(pldm_msg_hdr) +
211 PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES + table->size());
212 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
213
214 rc = encode_get_bios_table_resp(
215 request->hdr.instance_id, PLDM_SUCCESS, 0 /* nxtTransferHandle */,
216 PLDM_START_AND_END, table->data(), response.size(), responsePtr);
217 if (rc != PLDM_SUCCESS)
218 {
219 return ccOnlyResponse(request, rc);
220 }
Sampa Misrab37be312019-07-03 02:26:41 -0500221
222 return response;
223}
224
John Wang8721ed62019-12-05 14:44:43 +0800225Response Handler::getBIOSAttributeCurrentValueByHandle(const pldm_msg* request,
226 size_t payloadLength)
227{
228 uint32_t transferHandle;
229 uint8_t transferOpFlag;
230 uint16_t attributeHandle;
231
232 auto rc = decode_get_bios_attribute_current_value_by_handle_req(
233 request, payloadLength, &transferHandle, &transferOpFlag,
234 &attributeHandle);
235 if (rc != PLDM_SUCCESS)
236 {
237 return ccOnlyResponse(request, rc);
238 }
239
John Wang6080aae2020-02-14 09:34:25 +0800240 auto table = biosConfig.getBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE);
241 if (!table)
Deepak Kodihalli4976a682020-01-07 05:48:29 -0600242 {
243 return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
244 }
245
John Wang8721ed62019-12-05 14:44:43 +0800246 auto entry = pldm_bios_table_attr_value_find_by_handle(
John Wang6080aae2020-02-14 09:34:25 +0800247 table->data(), table->size(), attributeHandle);
John Wang8721ed62019-12-05 14:44:43 +0800248 if (entry == nullptr)
249 {
250 return ccOnlyResponse(request, PLDM_INVALID_BIOS_ATTR_HANDLE);
251 }
252
John Wang8e877e02020-02-03 16:06:55 +0800253 auto entryLength = pldm_bios_table_attr_value_entry_length(entry);
John Wang8721ed62019-12-05 14:44:43 +0800254 Response response(sizeof(pldm_msg_hdr) +
255 PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_MIN_RESP_BYTES +
John Wang8e877e02020-02-03 16:06:55 +0800256 entryLength,
John Wang8721ed62019-12-05 14:44:43 +0800257 0);
258 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800259 rc = encode_get_bios_current_value_by_handle_resp(
John Wang8e877e02020-02-03 16:06:55 +0800260 request->hdr.instance_id, PLDM_SUCCESS, 0, PLDM_START_AND_END,
261 reinterpret_cast<const uint8_t*>(entry), entryLength, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800262 if (rc != PLDM_SUCCESS)
263 {
264 return ccOnlyResponse(request, rc);
265 }
John Wang8721ed62019-12-05 14:44:43 +0800266
267 return response;
268}
269
John Wang42174882019-12-20 14:56:59 +0800270Response Handler::setBIOSAttributeCurrentValue(const pldm_msg* request,
271 size_t payloadLength)
272{
273 uint32_t transferHandle;
274 uint8_t transferOpFlag;
275 variable_field attributeField;
276
277 auto rc = decode_set_bios_attribute_current_value_req(
278 request, payloadLength, &transferHandle, &transferOpFlag,
279 &attributeField);
280 if (rc != PLDM_SUCCESS)
281 {
282 return ccOnlyResponse(request, rc);
283 }
284
John Wang6080aae2020-02-14 09:34:25 +0800285 rc = biosConfig.setAttrValue(attributeField.ptr, attributeField.length);
John Wang42174882019-12-20 14:56:59 +0800286
John Wang6080aae2020-02-14 09:34:25 +0800287 return ccOnlyResponse(request, rc);
John Wang42174882019-12-20 14:56:59 +0800288}
289
John Wangf719f3b2020-01-17 08:46:22 +0800290} // namespace bios
Sampa Misra032bd502019-03-06 05:03:22 -0600291} // namespace responder
292} // namespace pldm