blob: b9f5ff1c558226f2439768c93a9b79a5dc853e4a [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}},
29 {PLDM_BIOS, {PLDM_GET_DATE_TIME}}};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060030
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060031static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060032 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050033 {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}},
34 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060035 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060036};
37
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038namespace base
39{
40
Deepak Kodihallibc669f12019-11-28 08:52:07 -060041Response Handler::getPLDMTypes(const pldm_msg* request,
42 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060043{
44 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060045 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060046 for (const auto& type : capabilities)
47 {
48 auto index = type.first / 8;
49 // <Type Number> = <Array Index> * 8 + <bit position>
50 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060051 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060052 }
53
vkaverapa6575b82019-04-03 05:33:52 -050054 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
55 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053056 encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(),
57 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050058
59 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060060}
61
Deepak Kodihallibc669f12019-11-28 08:52:07 -060062Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060063{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060064 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060065 Type type;
66
vkaverapa6575b82019-04-03 05:33:52 -050067 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
68 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060069
Zahed Hossain223a73d2019-07-04 12:46:18 -050070 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050071
72 if (rc != PLDM_SUCCESS)
73 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053074 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
75 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050076 return response;
77 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060078
79 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060080 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060081 if (capabilities.find(type) == capabilities.end())
82 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053083 encode_get_commands_resp(request->hdr.instance_id,
84 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050085 responsePtr);
86 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060087 }
88
89 for (const auto& cmd : capabilities.at(type))
90 {
91 auto index = cmd / 8;
92 // <Type Number> = <Array Index> * 8 + <bit position>
93 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060094 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060095 }
96
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053097 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
98 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050099
100 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600101}
102
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600103Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600104{
105 uint32_t transferHandle;
106 Type type;
107 uint8_t transferFlag;
108
vkaverapa6575b82019-04-03 05:33:52 -0500109 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
110 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600111
Zahed Hossain223a73d2019-07-04 12:46:18 -0500112 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
113 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500114
115 if (rc != PLDM_SUCCESS)
116 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530117 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
118 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500119 return response;
120 }
Sampa Misra432e1872019-02-13 03:49:43 -0600121
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600122 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600123 auto search = versions.find(type);
124
125 if (search == versions.end())
126 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530127 encode_get_version_resp(request->hdr.instance_id,
128 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
129 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500130 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600131 }
132
133 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530134 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
135 PLDM_START_AND_END, &version, sizeof(pldm_version),
136 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500137
138 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600139}
140
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600141Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800142{
143 // assigned 1 to the bmc as the PLDM terminus
144 uint8_t tid = 1;
145
146 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
147 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
148 encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
149 responsePtr);
150
151 return response;
152}
153
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600154} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600155} // namespace responder
156} // namespace pldm