blob: 8385c8ada619c03ed7dffa0757be8e693ef8069e [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "libpldm/base.h"
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/bios.h"
4#include "libpldm/fru.h"
5#include "libpldm/platform.h"
6
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06007#include "base.hpp"
8
9#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -060010#include <cstring>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060011#include <map>
12#include <stdexcept>
13#include <vector>
14
Sridevi Ramesh45e52542020-04-03 07:54:18 -050015#ifdef OEM_IBM
16#include "libpldm/file_io.h"
17#include "libpldm/host.h"
18#endif
19
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060020namespace pldm
21{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060022
23using Type = uint8_t;
24
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060025namespace responder
26{
27
28using Cmd = std::vector<uint8_t>;
29
30static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050031 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080032 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
33 PLDM_GET_PLDM_COMMANDS}},
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060034 {PLDM_PLATFORM, {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES}},
John Wang8721ed62019-12-05 14:44:43 +080035 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060036 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080037 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
38 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE}},
John Wang5bdde3a2020-06-16 15:02:33 +080039 {PLDM_FRU,
40 {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE,
41 PLDM_GET_FRU_RECORD_BY_OPTION}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050042#ifdef OEM_IBM
43 {PLDM_OEM,
44 {PLDM_HOST_GET_ALERT_STATUS, PLDM_GET_FILE_TABLE, PLDM_READ_FILE,
45 PLDM_WRITE_FILE, PLDM_READ_FILE_INTO_MEMORY, PLDM_WRITE_FILE_FROM_MEMORY,
46 PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
47 PLDM_NEW_FILE_AVAILABLE, PLDM_READ_FILE_BY_TYPE, PLDM_WRITE_FILE_BY_TYPE,
48 PLDM_FILE_ACK}},
49#endif
50};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060051
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060052static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060053 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Tom Joseph0bead972020-01-28 10:54:03 +053054 {PLDM_PLATFORM, {0xF1, 0xF2, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050055 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060056 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050057#ifdef OEM_IBM
58 {PLDM_OEM, {0xF1, 0xF0, 0xF0, 0x00}},
59#endif
Sampa Misra432e1872019-02-13 03:49:43 -060060};
61
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050062namespace base
63{
64
Deepak Kodihallibc669f12019-11-28 08:52:07 -060065Response Handler::getPLDMTypes(const pldm_msg* request,
66 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060067{
68 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060069 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060070 for (const auto& type : capabilities)
71 {
72 auto index = type.first / 8;
73 // <Type Number> = <Array Index> * 8 + <bit position>
74 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060075 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060076 }
77
vkaverapa6575b82019-04-03 05:33:52 -050078 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
79 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080080 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
81 types.data(), responsePtr);
82 if (rc != PLDM_SUCCESS)
83 {
84 return CmdHandler::ccOnlyResponse(request, rc);
85 }
vkaverapa6575b82019-04-03 05:33:52 -050086
87 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060088}
89
Deepak Kodihallibc669f12019-11-28 08:52:07 -060090Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060091{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060092 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060093 Type type;
94
vkaverapa6575b82019-04-03 05:33:52 -050095 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
96 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060097
Zahed Hossain223a73d2019-07-04 12:46:18 -050098 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050099
100 if (rc != PLDM_SUCCESS)
101 {
George Liufb8611d2019-12-06 10:14:15 +0800102 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500103 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600104
105 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600106 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600107 if (capabilities.find(type) == capabilities.end())
108 {
George Liufb8611d2019-12-06 10:14:15 +0800109 return CmdHandler::ccOnlyResponse(request,
110 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600111 }
112
113 for (const auto& cmd : capabilities.at(type))
114 {
115 auto index = cmd / 8;
116 // <Type Number> = <Array Index> * 8 + <bit position>
117 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600118 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600119 }
120
George Liufb8611d2019-12-06 10:14:15 +0800121 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
122 cmds.data(), responsePtr);
123 if (rc != PLDM_SUCCESS)
124 {
125 return ccOnlyResponse(request, rc);
126 }
vkaverapa6575b82019-04-03 05:33:52 -0500127
128 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600129}
130
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600131Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600132{
133 uint32_t transferHandle;
134 Type type;
135 uint8_t transferFlag;
136
vkaverapa6575b82019-04-03 05:33:52 -0500137 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
138 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600139
Zahed Hossain223a73d2019-07-04 12:46:18 -0500140 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
141 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500142
143 if (rc != PLDM_SUCCESS)
144 {
George Liufb8611d2019-12-06 10:14:15 +0800145 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500146 }
Sampa Misra432e1872019-02-13 03:49:43 -0600147
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600148 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600149 auto search = versions.find(type);
150
151 if (search == versions.end())
152 {
George Liufb8611d2019-12-06 10:14:15 +0800153 return CmdHandler::ccOnlyResponse(request,
154 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600155 }
156
157 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800158 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
159 PLDM_START_AND_END, &version,
160 sizeof(pldm_version), responsePtr);
161 if (rc != PLDM_SUCCESS)
162 {
163 return ccOnlyResponse(request, rc);
164 }
vkaverapa6575b82019-04-03 05:33:52 -0500165
166 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600167}
168
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600169Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800170{
171 // assigned 1 to the bmc as the PLDM terminus
172 uint8_t tid = 1;
173
174 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
175 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800176 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
177 responsePtr);
178 if (rc != PLDM_SUCCESS)
179 {
180 return ccOnlyResponse(request, rc);
181 }
John Wang5c4f80d2019-07-29 11:12:18 +0800182
183 return response;
184}
185
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600186} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600187} // namespace responder
188} // namespace pldm