blob: fc4ddf6612f26a4d40ea8b6c23ad7da113a0da6d [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
Zahed Hossain5e4e3832019-07-05 03:43:18 -050012#include "libpldm/bios.h"
13#include "libpldm/platform.h"
14
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060015namespace pldm
16{
17namespace responder
18{
19
20using Cmd = std::vector<uint8_t>;
21
22static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050023 {PLDM_BASE,
24 {PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES, PLDM_GET_PLDM_COMMANDS}},
25 {PLDM_PLATFORM, {PLDM_SET_STATE_EFFECTER_STATES}},
26 {PLDM_BIOS, {PLDM_GET_DATE_TIME}}};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060027
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060028static const std::map<Type, ver32_t> versions{
Sampa Misra432e1872019-02-13 03:49:43 -060029 {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}},
Zahed Hossain74c1c1e2019-06-18 02:13:48 -050030 {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}},
31 {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}},
Sampa Misra432e1872019-02-13 03:49:43 -060032};
33
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050034namespace base
35{
36
37void registerHandlers()
38{
39 registerHandler(PLDM_BASE, PLDM_GET_PLDM_TYPES, std::move(getPLDMTypes));
40 registerHandler(PLDM_BASE, PLDM_GET_PLDM_COMMANDS,
41 std::move(getPLDMCommands));
42 registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION,
43 std::move(getPLDMVersion));
44}
45
46} // namespace base
47
vkaverapa6575b82019-04-03 05:33:52 -050048Response getPLDMTypes(const pldm_msg* request, 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());
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053062 encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(),
63 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050064
65 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060066}
67
vkaverapa6575b82019-04-03 05:33:52 -050068Response getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060069{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060070 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060071 Type type;
72
vkaverapa6575b82019-04-03 05:33:52 -050073 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
74 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060075
Zahed Hossain223a73d2019-07-04 12:46:18 -050076 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -050077
78 if (rc != PLDM_SUCCESS)
79 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053080 encode_get_commands_resp(request->hdr.instance_id, rc, nullptr,
81 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -050082 return response;
83 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060084
85 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060086 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060087 if (capabilities.find(type) == capabilities.end())
88 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053089 encode_get_commands_resp(request->hdr.instance_id,
90 PLDM_ERROR_INVALID_PLDM_TYPE, nullptr,
vkaverapa6575b82019-04-03 05:33:52 -050091 responsePtr);
92 return response;
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
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530103 encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
104 cmds.data(), responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500105
106 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600107}
108
vkaverapa6575b82019-04-03 05:33:52 -0500109Response getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600110{
111 uint32_t transferHandle;
112 Type type;
113 uint8_t transferFlag;
114
vkaverapa6575b82019-04-03 05:33:52 -0500115 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
116 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600117
Zahed Hossain223a73d2019-07-04 12:46:18 -0500118 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
119 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500120
121 if (rc != PLDM_SUCCESS)
122 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530123 encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4,
124 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500125 return response;
126 }
Sampa Misra432e1872019-02-13 03:49:43 -0600127
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600128 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600129 auto search = versions.find(type);
130
131 if (search == versions.end())
132 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530133 encode_get_version_resp(request->hdr.instance_id,
134 PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4,
135 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500136 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600137 }
138
139 memcpy(&version, &(search->second), sizeof(version));
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530140 encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
141 PLDM_START_AND_END, &version, sizeof(pldm_version),
142 responsePtr);
vkaverapa6575b82019-04-03 05:33:52 -0500143
144 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600145}
146
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600147} // namespace responder
148} // namespace pldm