blob: e94597a5d47cb47a706db31c3e27243e0417180d [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "libpldm/base.h"
2
3#include "base.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05004#include "registration.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06005
6#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -06007#include <cstring>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06008#include <map>
9#include <stdexcept>
10#include <vector>
11
Zahed Hossain5e4e3832019-07-05 03:43:18 -050012#include "libpldm/bios.h"
13#include "libpldm/platform.h"
14
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060015namespace pldm
16{
17namespace responder
18{
19
20using Cmd = std::vector<uint8_t>;
21
22static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050023 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080024 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
25 PLDM_GET_PLDM_COMMANDS}},
Zahed Hossain5e4e3832019-07-05 03:43:18 -050026 {PLDM_PLATFORM, {PLDM_SET_STATE_EFFECTER_STATES}},
27 {PLDM_BIOS, {PLDM_GET_DATE_TIME}}};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060028
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060029static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060030 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050031 {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}},
32 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060033 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060034};
35
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050036namespace base
37{
38
39void registerHandlers()
40{
41 registerHandler(PLDM_BASE, PLDM_GET_PLDM_TYPES, std::move(getPLDMTypes));
42 registerHandler(PLDM_BASE, PLDM_GET_PLDM_COMMANDS,
43 std::move(getPLDMCommands));
44 registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION,
45 std::move(getPLDMVersion));
John Wang5c4f80d2019-07-29 11:12:18 +080046 registerHandler(PLDM_BASE, PLDM_GET_TID, std::move(getTID));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050047}
48
49} // namespace base
50
Deepak Kodihalli3c275e12019-09-21 06:39:39 -050051Response getPLDMTypes(const pldm_msg* request, size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060052{
53 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060054 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060055 for (const auto& type : capabilities)
56 {
57 auto index = type.first / 8;
58 // <Type Number> = <Array Index> * 8 + <bit position>
59 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060060 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060061 }
62
vkaverapa6575b82019-04-03 05:33:52 -050063 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
64 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053065 encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(),
66 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050067
68 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060069}
70
vkaverapa6575b82019-04-03 05:33:52 -050071Response 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 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053083 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
84 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050085 return response;
86 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060087
88 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060089 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060090 if (capabilities.find(type) == capabilities.end())
91 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053092 encode_get_commands_resp(request->hdr.instance_id,
93 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050094 responsePtr);
95 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060096 }
97
98 for (const auto& cmd : capabilities.at(type))
99 {
100 auto index = cmd / 8;
101 // <Type Number> = <Array Index> * 8 + <bit position>
102 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600103 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600104 }
105
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530106 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
107 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500108
109 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600110}
111
vkaverapa6575b82019-04-03 05:33:52 -0500112Response 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 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530126 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
127 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500128 return response;
129 }
Sampa Misra432e1872019-02-13 03:49:43 -0600130
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600131 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600132 auto search = versions.find(type);
133
134 if (search == versions.end())
135 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530136 encode_get_version_resp(request->hdr.instance_id,
137 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
138 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500139 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600140 }
141
142 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530143 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
144 PLDM_START_AND_END, &version, sizeof(pldm_version),
145 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500146
147 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600148}
149
Deepak Kodihalli3c275e12019-09-21 06:39:39 -0500150Response 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());
157 encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
158 responsePtr);
159
160 return response;
161}
162
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600163} // namespace responder
164} // namespace pldm