blob: d0c3551c8938a591b6c5b8e65c00b190a76a6606 [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}},
Sampa Misra432e1872019-02-13 03:49:43 -060033};
34
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035namespace base
36{
37
38void registerHandlers()
39{
40 registerHandler(PLDM_BASE, PLDM_GET_PLDM_TYPES, std::move(getPLDMTypes));
41 registerHandler(PLDM_BASE, PLDM_GET_PLDM_COMMANDS,
42 std::move(getPLDMCommands));
43 registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION,
44 std::move(getPLDMVersion));
John Wang5c4f80d2019-07-29 11:12:18 +080045 registerHandler(PLDM_BASE, PLDM_GET_TID, std::move(getTID));
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050046}
47
48} // namespace base
49
vkaverapa6575b82019-04-03 05:33:52 -050050Response getPLDMTypes(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060051{
52 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060053 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060054 for (const auto& type : capabilities)
55 {
56 auto index = type.first / 8;
57 // <Type Number> = <Array Index> * 8 + <bit position>
58 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060059 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060060 }
61
vkaverapa6575b82019-04-03 05:33:52 -050062 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
63 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053064 encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(),
65 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050066
67 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060068}
69
vkaverapa6575b82019-04-03 05:33:52 -050070Response getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060071{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060072 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060073 Type type;
74
vkaverapa6575b82019-04-03 05:33:52 -050075 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
76 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060077
Zahed Hossain223a73d2019-07-04 12:46:18 -050078 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050079
80 if (rc != PLDM_SUCCESS)
81 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053082 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
83 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050084 return response;
85 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060086
87 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060088 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060089 if (capabilities.find(type) == capabilities.end())
90 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053091 encode_get_commands_resp(request->hdr.instance_id,
92 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050093 responsePtr);
94 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060095 }
96
97 for (const auto& cmd : capabilities.at(type))
98 {
99 auto index = cmd / 8;
100 // <Type Number> = <Array Index> * 8 + <bit position>
101 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600102 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600103 }
104
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530105 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
106 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500107
108 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600109}
110
vkaverapa6575b82019-04-03 05:33:52 -0500111Response getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600112{
113 uint32_t transferHandle;
114 Type type;
115 uint8_t transferFlag;
116
vkaverapa6575b82019-04-03 05:33:52 -0500117 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
118 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600119
Zahed Hossain223a73d2019-07-04 12:46:18 -0500120 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
121 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500122
123 if (rc != PLDM_SUCCESS)
124 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530125 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
126 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500127 return response;
128 }
Sampa Misra432e1872019-02-13 03:49:43 -0600129
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600130 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600131 auto search = versions.find(type);
132
133 if (search == versions.end())
134 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530135 encode_get_version_resp(request->hdr.instance_id,
136 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
137 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500138 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600139 }
140
141 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530142 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
143 PLDM_START_AND_END, &version, sizeof(pldm_version),
144 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500145
146 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600147}
148
John Wang5c4f80d2019-07-29 11:12:18 +0800149Response getTID(const pldm_msg* request, size_t payloadLength)
150{
151 // assigned 1 to the bmc as the PLDM terminus
152 uint8_t tid = 1;
153
154 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
155 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
156 encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
157 responsePtr);
158
159 return response;
160}
161
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600162} // namespace responder
163} // namespace pldm