blob: 8a932a880d5fdebfb70e1e440524bbf16089d116 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "libpldm/base.h"
2
3#include "base.hpp"
4
5#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -06006#include <cstring>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06007#include <map>
8#include <stdexcept>
9#include <vector>
10
Zahed Hossain5e4e3832019-07-05 03:43:18 -050011#include "libpldm/bios.h"
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060012#include "libpldm/fru.h"
Zahed Hossain5e4e3832019-07-05 03:43:18 -050013#include "libpldm/platform.h"
14
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060015namespace pldm
16{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060017
18using Type = uint8_t;
19
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060020namespace responder
21{
22
23using Cmd = std::vector<uint8_t>;
24
25static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050026 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080027 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
28 PLDM_GET_PLDM_COMMANDS}},
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060029 {PLDM_PLATFORM, {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES}},
John Wang8721ed62019-12-05 14:44:43 +080030 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060031 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
John Wang8721ed62019-12-05 14:44:43 +080032 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE}},
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060033 {PLDM_FRU, {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE}},
John Wang8721ed62019-12-05 14:44:43 +080034};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060035
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060036static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060037 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Tom Joseph0bead972020-01-28 10:54:03 +053038 {PLDM_PLATFORM, {0xF1, 0xF2, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050039 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060040 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060041};
42
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050043namespace base
44{
45
Deepak Kodihallibc669f12019-11-28 08:52:07 -060046Response Handler::getPLDMTypes(const pldm_msg* request,
47 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060048{
49 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060050 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060051 for (const auto& type : capabilities)
52 {
53 auto index = type.first / 8;
54 // <Type Number> = <Array Index> * 8 + <bit position>
55 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060056 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060057 }
58
vkaverapa6575b82019-04-03 05:33:52 -050059 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
60 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080061 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
62 types.data(), responsePtr);
63 if (rc != PLDM_SUCCESS)
64 {
65 return CmdHandler::ccOnlyResponse(request, rc);
66 }
vkaverapa6575b82019-04-03 05:33:52 -050067
68 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060069}
70
Deepak Kodihallibc669f12019-11-28 08:52:07 -060071Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060072{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060073 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060074 Type type;
75
vkaverapa6575b82019-04-03 05:33:52 -050076 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
77 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060078
Zahed Hossain223a73d2019-07-04 12:46:18 -050079 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050080
81 if (rc != PLDM_SUCCESS)
82 {
George Liufb8611d2019-12-06 10:14:15 +080083 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -050084 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060085
86 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060087 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060088 if (capabilities.find(type) == capabilities.end())
89 {
George Liufb8611d2019-12-06 10:14:15 +080090 return CmdHandler::ccOnlyResponse(request,
91 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060092 }
93
94 for (const auto& cmd : capabilities.at(type))
95 {
96 auto index = cmd / 8;
97 // <Type Number> = <Array Index> * 8 + <bit position>
98 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060099 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600100 }
101
George Liufb8611d2019-12-06 10:14:15 +0800102 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
103 cmds.data(), responsePtr);
104 if (rc != PLDM_SUCCESS)
105 {
106 return ccOnlyResponse(request, rc);
107 }
vkaverapa6575b82019-04-03 05:33:52 -0500108
109 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600110}
111
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600112Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600113{
114 uint32_t transferHandle;
115 Type type;
116 uint8_t transferFlag;
117
vkaverapa6575b82019-04-03 05:33:52 -0500118 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
119 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600120
Zahed Hossain223a73d2019-07-04 12:46:18 -0500121 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
122 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500123
124 if (rc != PLDM_SUCCESS)
125 {
George Liufb8611d2019-12-06 10:14:15 +0800126 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500127 }
Sampa Misra432e1872019-02-13 03:49:43 -0600128
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600129 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600130 auto search = versions.find(type);
131
132 if (search == versions.end())
133 {
George Liufb8611d2019-12-06 10:14:15 +0800134 return CmdHandler::ccOnlyResponse(request,
135 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600136 }
137
138 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800139 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
140 PLDM_START_AND_END, &version,
141 sizeof(pldm_version), responsePtr);
142 if (rc != PLDM_SUCCESS)
143 {
144 return ccOnlyResponse(request, rc);
145 }
vkaverapa6575b82019-04-03 05:33:52 -0500146
147 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600148}
149
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600150Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800151{
152 // assigned 1 to the bmc as the PLDM terminus
153 uint8_t tid = 1;
154
155 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
156 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800157 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
158 responsePtr);
159 if (rc != PLDM_SUCCESS)
160 {
161 return ccOnlyResponse(request, rc);
162 }
John Wang5c4f80d2019-07-29 11:12:18 +0800163
164 return response;
165}
166
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600167} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600168} // namespace responder
169} // namespace pldm