blob: f72e8dc72b3bcb03b62d30a39db4fb7542e79fd5 [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
Sridevi Ramesh45e52542020-04-03 07:54:18 -050015#ifdef OEM_IBM
16#include "libpldm/file_io.h"
17#include "libpldm/host.h"
18#endif
19
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060020namespace pldm
21{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060022
23using Type = uint8_t;
24
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060025namespace responder
26{
27
28using Cmd = std::vector<uint8_t>;
29
30static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050031 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080032 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
33 PLDM_GET_PLDM_COMMANDS}},
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060034 {PLDM_PLATFORM, {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES}},
John Wang8721ed62019-12-05 14:44:43 +080035 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060036 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080037 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
38 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050039 {PLDM_FRU, {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE}},
40#ifdef OEM_IBM
41 {PLDM_OEM,
42 {PLDM_HOST_GET_ALERT_STATUS, PLDM_GET_FILE_TABLE, PLDM_READ_FILE,
43 PLDM_WRITE_FILE, PLDM_READ_FILE_INTO_MEMORY, PLDM_WRITE_FILE_FROM_MEMORY,
44 PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
45 PLDM_NEW_FILE_AVAILABLE, PLDM_READ_FILE_BY_TYPE, PLDM_WRITE_FILE_BY_TYPE,
46 PLDM_FILE_ACK}},
47#endif
48};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060049
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060050static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060051 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Tom Joseph0bead972020-01-28 10:54:03 +053052 {PLDM_PLATFORM, {0xF1, 0xF2, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050053 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Deepak Kodihalliec482972019-11-26 07:57:10 -060054 {PLDM_FRU, {0xF1, 0xF0, 0xF0, 0x00}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050055#ifdef OEM_IBM
56 {PLDM_OEM, {0xF1, 0xF0, 0xF0, 0x00}},
57#endif
Sampa Misra432e1872019-02-13 03:49:43 -060058};
59
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050060namespace base
61{
62
Deepak Kodihallibc669f12019-11-28 08:52:07 -060063Response Handler::getPLDMTypes(const pldm_msg* request,
64 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060065{
66 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060067 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060068 for (const auto& type : capabilities)
69 {
70 auto index = type.first / 8;
71 // <Type Number> = <Array Index> * 8 + <bit position>
72 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060073 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060074 }
75
vkaverapa6575b82019-04-03 05:33:52 -050076 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
77 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080078 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
79 types.data(), responsePtr);
80 if (rc != PLDM_SUCCESS)
81 {
82 return CmdHandler::ccOnlyResponse(request, rc);
83 }
vkaverapa6575b82019-04-03 05:33:52 -050084
85 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060086}
87
Deepak Kodihallibc669f12019-11-28 08:52:07 -060088Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060089{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060090 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060091 Type type;
92
vkaverapa6575b82019-04-03 05:33:52 -050093 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
94 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060095
Zahed Hossain223a73d2019-07-04 12:46:18 -050096 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050097
98 if (rc != PLDM_SUCCESS)
99 {
George Liufb8611d2019-12-06 10:14:15 +0800100 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500101 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600102
103 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600104 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600105 if (capabilities.find(type) == capabilities.end())
106 {
George Liufb8611d2019-12-06 10:14:15 +0800107 return CmdHandler::ccOnlyResponse(request,
108 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600109 }
110
111 for (const auto& cmd : capabilities.at(type))
112 {
113 auto index = cmd / 8;
114 // <Type Number> = <Array Index> * 8 + <bit position>
115 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600116 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600117 }
118
George Liufb8611d2019-12-06 10:14:15 +0800119 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
120 cmds.data(), responsePtr);
121 if (rc != PLDM_SUCCESS)
122 {
123 return ccOnlyResponse(request, rc);
124 }
vkaverapa6575b82019-04-03 05:33:52 -0500125
126 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600127}
128
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600129Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600130{
131 uint32_t transferHandle;
132 Type type;
133 uint8_t transferFlag;
134
vkaverapa6575b82019-04-03 05:33:52 -0500135 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
136 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600137
Zahed Hossain223a73d2019-07-04 12:46:18 -0500138 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
139 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500140
141 if (rc != PLDM_SUCCESS)
142 {
George Liufb8611d2019-12-06 10:14:15 +0800143 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500144 }
Sampa Misra432e1872019-02-13 03:49:43 -0600145
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600146 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600147 auto search = versions.find(type);
148
149 if (search == versions.end())
150 {
George Liufb8611d2019-12-06 10:14:15 +0800151 return CmdHandler::ccOnlyResponse(request,
152 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600153 }
154
155 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800156 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
157 PLDM_START_AND_END, &version,
158 sizeof(pldm_version), responsePtr);
159 if (rc != PLDM_SUCCESS)
160 {
161 return ccOnlyResponse(request, rc);
162 }
vkaverapa6575b82019-04-03 05:33:52 -0500163
164 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600165}
166
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600167Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800168{
169 // assigned 1 to the bmc as the PLDM terminus
170 uint8_t tid = 1;
171
172 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
173 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +0800174 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid,
175 responsePtr);
176 if (rc != PLDM_SUCCESS)
177 {
178 return ccOnlyResponse(request, rc);
179 }
John Wang5c4f80d2019-07-29 11:12:18 +0800180
181 return response;
182}
183
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600184} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600185} // namespace responder
186} // namespace pldm