Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | |
| 3 | #include "base.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 4 | #include "registration.hpp" |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 5 | |
| 6 | #include <array> |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 7 | #include <cstring> |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <stdexcept> |
| 10 | #include <vector> |
| 11 | |
Zahed Hossain | 5e4e383 | 2019-07-05 03:43:18 -0500 | [diff] [blame] | 12 | #include "libpldm/bios.h" |
| 13 | #include "libpldm/platform.h" |
| 14 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 15 | namespace pldm |
| 16 | { |
| 17 | namespace responder |
| 18 | { |
| 19 | |
| 20 | using Cmd = std::vector<uint8_t>; |
| 21 | |
| 22 | static const std::map<Type, Cmd> capabilities{ |
Zahed Hossain | 5e4e383 | 2019-07-05 03:43:18 -0500 | [diff] [blame] | 23 | {PLDM_BASE, |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 24 | {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES, |
| 25 | PLDM_GET_PLDM_COMMANDS}}, |
Zahed Hossain | 5e4e383 | 2019-07-05 03:43:18 -0500 | [diff] [blame] | 26 | {PLDM_PLATFORM, {PLDM_SET_STATE_EFFECTER_STATES}}, |
| 27 | {PLDM_BIOS, {PLDM_GET_DATE_TIME}}}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 28 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 29 | static const std::map<Type, ver32_t> versions{ |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 30 | {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}}, |
Zahed Hossain | 74c1c1e | 2019-06-18 02:13:48 -0500 | [diff] [blame] | 31 | {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}}, |
| 32 | {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}}, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 35 | namespace base |
| 36 | { |
| 37 | |
| 38 | void registerHandlers() |
| 39 | { |
| 40 | registerHandler(PLDM_BASE, PLDM_GET_PLDM_TYPES, std::move(getPLDMTypes)); |
| 41 | registerHandler(PLDM_BASE, PLDM_GET_PLDM_COMMANDS, |
| 42 | std::move(getPLDMCommands)); |
| 43 | registerHandler(PLDM_BASE, PLDM_GET_PLDM_VERSION, |
| 44 | std::move(getPLDMVersion)); |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 45 | registerHandler(PLDM_BASE, PLDM_GET_TID, std::move(getTID)); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | } // namespace base |
| 49 | |
Deepak Kodihalli | 3c275e1 | 2019-09-21 06:39:39 -0500 | [diff] [blame] | 50 | Response getPLDMTypes(const pldm_msg* request, size_t /*payloadLength*/) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 51 | { |
| 52 | // DSP0240 has this as a bitfield8[N], where N = 0 to 7 |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 53 | std::array<bitfield8_t, 8> types{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 54 | for (const auto& type : capabilities) |
| 55 | { |
| 56 | auto index = type.first / 8; |
| 57 | // <Type Number> = <Array Index> * 8 + <bit position> |
| 58 | auto bit = type.first - (index * 8); |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 59 | types[index].byte |= 1 << bit; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 60 | } |
| 61 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 62 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0); |
| 63 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 64 | encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(), |
| 65 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 66 | |
| 67 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 68 | } |
| 69 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 70 | Response getPLDMCommands(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 71 | { |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 72 | ver32_t version{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 73 | Type type; |
| 74 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 75 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0); |
| 76 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 77 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 78 | auto rc = decode_get_commands_req(request, payloadLength, &type, &version); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 79 | |
| 80 | if (rc != PLDM_SUCCESS) |
| 81 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 82 | encode_get_commands_resp(request->hdr.instance_id, rc, nullptr, |
| 83 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 84 | return response; |
| 85 | } |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 86 | |
| 87 | // DSP0240 has this as a bitfield8[N], where N = 0 to 31 |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 88 | std::array<bitfield8_t, 32> cmds{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 89 | if (capabilities.find(type) == capabilities.end()) |
| 90 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 91 | encode_get_commands_resp(request->hdr.instance_id, |
| 92 | PLDM_ERROR_INVALID_PLDM_TYPE, nullptr, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 93 | responsePtr); |
| 94 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | for (const auto& cmd : capabilities.at(type)) |
| 98 | { |
| 99 | auto index = cmd / 8; |
| 100 | // <Type Number> = <Array Index> * 8 + <bit position> |
| 101 | auto bit = cmd - (index * 8); |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 102 | cmds[index].byte |= 1 << bit; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 105 | encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS, |
| 106 | cmds.data(), responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 107 | |
| 108 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 109 | } |
| 110 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 111 | Response getPLDMVersion(const pldm_msg* request, size_t payloadLength) |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 112 | { |
| 113 | uint32_t transferHandle; |
| 114 | Type type; |
| 115 | uint8_t transferFlag; |
| 116 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 117 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0); |
| 118 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 119 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 120 | uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle, |
| 121 | &transferFlag, &type); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 122 | |
| 123 | if (rc != PLDM_SUCCESS) |
| 124 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 125 | encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4, |
| 126 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 127 | return response; |
| 128 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 129 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 130 | ver32_t version{}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 131 | auto search = versions.find(type); |
| 132 | |
| 133 | if (search == versions.end()) |
| 134 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 135 | encode_get_version_resp(request->hdr.instance_id, |
| 136 | PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4, |
| 137 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 138 | return response; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | memcpy(&version, &(search->second), sizeof(version)); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 142 | encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0, |
| 143 | PLDM_START_AND_END, &version, sizeof(pldm_version), |
| 144 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 145 | |
| 146 | return response; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 147 | } |
| 148 | |
Deepak Kodihalli | 3c275e1 | 2019-09-21 06:39:39 -0500 | [diff] [blame] | 149 | Response getTID(const pldm_msg* request, size_t /*payloadLength*/) |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 150 | { |
| 151 | // assigned 1 to the bmc as the PLDM terminus |
| 152 | uint8_t tid = 1; |
| 153 | |
| 154 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0); |
| 155 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 156 | encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS, tid, |
| 157 | responsePtr); |
| 158 | |
| 159 | return response; |
| 160 | } |
| 161 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 162 | } // namespace responder |
| 163 | } // namespace pldm |