blob: 107132406bc7dd297956fa0d64c5befad3753017 [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"
12#include "libpldm/platform.h"
13
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060014namespace pldm
15{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060016
17using Type = uint8_t;
18
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060019namespace responder
20{
21
22using Cmd = std::vector<uint8_t>;
23
24static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050025 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080026 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
27 PLDM_GET_PLDM_COMMANDS}},
Zahed Hossain5e4e3832019-07-05 03:43:18 -050028 {PLDM_PLATFORM, {PLDM_SET_STATE_EFFECTER_STATES}},
John Wang8721ed62019-12-05 14:44:43 +080029 {PLDM_BIOS,
30 {PLDM_GET_DATE_TIME, PLDM_GET_BIOS_TABLE,
31 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE}},
32};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060033
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060034static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060035 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050036 {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}},
37 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060038 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060039};
40
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041namespace base
42{
43
Deepak Kodihallibc669f12019-11-28 08:52:07 -060044Response Handler::getPLDMTypes(const pldm_msg* request,
45 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060046{
47 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060048 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060049 for (const auto& type : capabilities)
50 {
51 auto index = type.first / 8;
52 // <Type Number> = <Array Index> * 8 + <bit position>
53 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060054 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060055 }
56
vkaverapa6575b82019-04-03 05:33:52 -050057 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
58 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080059 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
60 types.data(), responsePtr);
61 if (rc != PLDM_SUCCESS)
62 {
63 return CmdHandler::ccOnlyResponse(request, rc);
64 }
vkaverapa6575b82019-04-03 05:33:52 -050065
66 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060067}
68
Deepak Kodihallibc669f12019-11-28 08:52:07 -060069Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060070{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060071 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060072 Type type;
73
vkaverapa6575b82019-04-03 05:33:52 -050074 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
75 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060076
Zahed Hossain223a73d2019-07-04 12:46:18 -050077 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050078
79 if (rc != PLDM_SUCCESS)
80 {
George Liufb8611d2019-12-06 10:14:15 +080081 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -050082 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060083
84 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060085 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060086 if (capabilities.find(type) == capabilities.end())
87 {
George Liufb8611d2019-12-06 10:14:15 +080088 return CmdHandler::ccOnlyResponse(request,
89 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060090 }
91
92 for (const auto& cmd : capabilities.at(type))
93 {
94 auto index = cmd / 8;
95 // <Type Number> = <Array Index> * 8 + <bit position>
96 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060097 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060098 }
99
George Liufb8611d2019-12-06 10:14:15 +0800100 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
101 cmds.data(), responsePtr);
102 if (rc != PLDM_SUCCESS)
103 {
104 return ccOnlyResponse(request, rc);
105 }
vkaverapa6575b82019-04-03 05:33:52 -0500106
107 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600108}
109
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600110Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600111{
112 uint32_t transferHandle;
113 Type type;
114 uint8_t transferFlag;
115
vkaverapa6575b82019-04-03 05:33:52 -0500116 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
117 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600118
Zahed Hossain223a73d2019-07-04 12:46:18 -0500119 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
120 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500121
122 if (rc != PLDM_SUCCESS)
123 {
George Liufb8611d2019-12-06 10:14:15 +0800124 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500125 }
Sampa Misra432e1872019-02-13 03:49:43 -0600126
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600127 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600128 auto search = versions.find(type);
129
130 if (search == versions.end())
131 {
George Liufb8611d2019-12-06 10:14:15 +0800132 return CmdHandler::ccOnlyResponse(request,
133 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600134 }
135
136 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800137 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
138 PLDM_START_AND_END, &version,
139 sizeof(pldm_version), responsePtr);
140 if (rc != PLDM_SUCCESS)
141 {
142 return ccOnlyResponse(request, rc);
143 }
vkaverapa6575b82019-04-03 05:33:52 -0500144
145 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600146}
147
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600148Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800149{
150 // assigned 1 to the bmc as the PLDM terminus
151 uint8_t tid = 1;
152
153 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
154 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800155 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
156 responsePtr);
157 if (rc != PLDM_SUCCESS)
158 {
159 return ccOnlyResponse(request, rc);
160 }
John Wang5c4f80d2019-07-29 11:12:18 +0800161
162 return response;
163}
164
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600165} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600166} // namespace responder
167} // namespace pldm