blob: 805abed88b56e15157e8ca0684c2bf97e4fd218c [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
12namespace pldm
13{
14namespace responder
15{
16
17using Cmd = std::vector<uint8_t>;
18
19static const std::map<Type, Cmd> capabilities{
20 {PLDM_BASE, {PLDM_GET_PLDM_TYPES, PLDM_GET_PLDM_COMMANDS}}};
21
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060022static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060023 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050024 {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}},
25 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060026};
27
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050028namespace base
29{
30
31void registerHandlers()
32{
33 registerHandler(PLDM_BASE, PLDM_GET_PLDM_TYPES, std::move(getPLDMTypes));
34 registerHandler(PLDM_BASE, PLDM_GET_PLDM_COMMANDS,
35 std::move(getPLDMCommands));
36 registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION,
37 std::move(getPLDMVersion));
38}
39
40} // namespace base
41
vkaverapa6575b82019-04-03 05:33:52 -050042Response getPLDMTypes(const pldm_msg* request, 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
vkaverapa6575b82019-04-03 05:33:52 -050062Response 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
vkaverapa6575b82019-04-03 05:33:52 -050070 auto rc = decode_get_commands_req(request->payload, payloadLength, &type,
71 &version);
72
73 if (rc != PLDM_SUCCESS)
74 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053075 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
76 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050077 return response;
78 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060079
80 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060081 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060082 if (capabilities.find(type) == capabilities.end())
83 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053084 encode_get_commands_resp(request->hdr.instance_id,
85 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050086 responsePtr);
87 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060088 }
89
90 for (const auto& cmd : capabilities.at(type))
91 {
92 auto index = cmd / 8;
93 // <Type Number> = <Array Index> * 8 + <bit position>
94 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060095 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060096 }
97
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053098 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
99 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500100
101 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600102}
103
vkaverapa6575b82019-04-03 05:33:52 -0500104Response getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600105{
106 uint32_t transferHandle;
107 Type type;
108 uint8_t transferFlag;
109
vkaverapa6575b82019-04-03 05:33:52 -0500110 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
111 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600112
vkaverapa6575b82019-04-03 05:33:52 -0500113 uint8_t rc = decode_get_version_req(request->payload, payloadLength,
114 &transferHandle, &transferFlag, &type);
115
116 if (rc != PLDM_SUCCESS)
117 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530118 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
119 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500120 return response;
121 }
Sampa Misra432e1872019-02-13 03:49:43 -0600122
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600123 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600124 auto search = versions.find(type);
125
126 if (search == versions.end())
127 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530128 encode_get_version_resp(request->hdr.instance_id,
129 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
130 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500131 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600132 }
133
134 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530135 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
136 PLDM_START_AND_END, &version, sizeof(pldm_version),
137 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500138
139 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600140}
141
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600142} // namespace responder
143} // namespace pldm