blob: 4d0c4f93a736258b3a89cb2a135e92e59d6d6bad [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());
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053059 encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(),
60 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050061
62 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060063}
64
Deepak Kodihallibc669f12019-11-28 08:52:07 -060065Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060066{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060067 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060068 Type type;
69
vkaverapa6575b82019-04-03 05:33:52 -050070 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
71 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060072
Zahed Hossain223a73d2019-07-04 12:46:18 -050073 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050074
75 if (rc != PLDM_SUCCESS)
76 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053077 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
78 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050079 return response;
80 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060081
82 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060083 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060084 if (capabilities.find(type) == capabilities.end())
85 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053086 encode_get_commands_resp(request->hdr.instance_id,
87 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050088 responsePtr);
89 return response;
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
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530100 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
101 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500102
103 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600104}
105
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600106Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600107{
108 uint32_t transferHandle;
109 Type type;
110 uint8_t transferFlag;
111
vkaverapa6575b82019-04-03 05:33:52 -0500112 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
113 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600114
Zahed Hossain223a73d2019-07-04 12:46:18 -0500115 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
116 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500117
118 if (rc != PLDM_SUCCESS)
119 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530120 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
121 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500122 return response;
123 }
Sampa Misra432e1872019-02-13 03:49:43 -0600124
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600125 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600126 auto search = versions.find(type);
127
128 if (search == versions.end())
129 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530130 encode_get_version_resp(request->hdr.instance_id,
131 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
132 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500133 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600134 }
135
136 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530137 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
138 PLDM_START_AND_END, &version, sizeof(pldm_version),
139 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500140
141 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600142}
143
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600144Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800145{
146 // assigned 1 to the bmc as the PLDM terminus
147 uint8_t tid = 1;
148
149 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
150 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
151 encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
152 responsePtr);
153
154 return response;
155}
156
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600157} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600158} // namespace responder
159} // namespace pldm