blob: 6f97bdf8d93d83a22cb164aba6f8c4c51be59c7d [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"
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060012#include "libpldm/fru.h"
Zahed Hossain5e4e3832019-07-05 03:43:18 -050013#include "libpldm/platform.h"
14
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060015namespace pldm
16{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060017
18using Type = uint8_t;
19
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060020namespace responder
21{
22
23using Cmd = std::vector<uint8_t>;
24
25static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050026 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080027 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
28 PLDM_GET_PLDM_COMMANDS}},
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060029 {PLDM_PLATFORM, {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES}},
John Wang8721ed62019-12-05 14:44:43 +080030 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060031 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080032 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
33 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE}},
34 {PLDM_FRU,
35 {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE}}};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060036
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060037static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060038 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Tom Joseph0bead972020-01-28 10:54:03 +053039 {PLDM_PLATFORM, {0xF1, 0xF2, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050040 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060041 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060042};
43
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050044namespace base
45{
46
Deepak Kodihallibc669f12019-11-28 08:52:07 -060047Response Handler::getPLDMTypes(const pldm_msg* request,
48 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060049{
50 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060051 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060052 for (const auto& type : capabilities)
53 {
54 auto index = type.first / 8;
55 // <Type Number> = <Array Index> * 8 + <bit position>
56 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060057 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060058 }
59
vkaverapa6575b82019-04-03 05:33:52 -050060 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
61 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080062 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
63 types.data(), responsePtr);
64 if (rc != PLDM_SUCCESS)
65 {
66 return CmdHandler::ccOnlyResponse(request, rc);
67 }
vkaverapa6575b82019-04-03 05:33:52 -050068
69 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060070}
71
Deepak Kodihallibc669f12019-11-28 08:52:07 -060072Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060073{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060074 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060075 Type type;
76
vkaverapa6575b82019-04-03 05:33:52 -050077 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
78 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060079
Zahed Hossain223a73d2019-07-04 12:46:18 -050080 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050081
82 if (rc != PLDM_SUCCESS)
83 {
George Liufb8611d2019-12-06 10:14:15 +080084 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -050085 }
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 {
George Liufb8611d2019-12-06 10:14:15 +080091 return CmdHandler::ccOnlyResponse(request,
92 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060093 }
94
95 for (const auto& cmd : capabilities.at(type))
96 {
97 auto index = cmd / 8;
98 // <Type Number> = <Array Index> * 8 + <bit position>
99 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600100 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600101 }
102
George Liufb8611d2019-12-06 10:14:15 +0800103 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
104 cmds.data(), responsePtr);
105 if (rc != PLDM_SUCCESS)
106 {
107 return ccOnlyResponse(request, rc);
108 }
vkaverapa6575b82019-04-03 05:33:52 -0500109
110 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600111}
112
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600113Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600114{
115 uint32_t transferHandle;
116 Type type;
117 uint8_t transferFlag;
118
vkaverapa6575b82019-04-03 05:33:52 -0500119 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
120 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600121
Zahed Hossain223a73d2019-07-04 12:46:18 -0500122 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
123 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500124
125 if (rc != PLDM_SUCCESS)
126 {
George Liufb8611d2019-12-06 10:14:15 +0800127 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500128 }
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 {
George Liufb8611d2019-12-06 10:14:15 +0800135 return CmdHandler::ccOnlyResponse(request,
136 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600137 }
138
139 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800140 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
141 PLDM_START_AND_END, &version,
142 sizeof(pldm_version), responsePtr);
143 if (rc != PLDM_SUCCESS)
144 {
145 return ccOnlyResponse(request, rc);
146 }
vkaverapa6575b82019-04-03 05:33:52 -0500147
148 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600149}
150
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600151Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800152{
153 // assigned 1 to the bmc as the PLDM terminus
154 uint8_t tid = 1;
155
156 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
157 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800158 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
159 responsePtr);
160 if (rc != PLDM_SUCCESS)
161 {
162 return ccOnlyResponse(request, rc);
163 }
John Wang5c4f80d2019-07-29 11:12:18 +0800164
165 return response;
166}
167
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600168} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600169} // namespace responder
170} // namespace pldm