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, |
| 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 Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 27 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 28 | static const std::map<Type, ver32_t> versions{ |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 29 | {PLDM_BASE, {0xF1, 0xF0, 0xF0, 0x00}}, |
Zahed Hossain | 74c1c1e | 2019-06-18 02:13:48 -0500 | [diff] [blame] | 30 | {PLDM_PLATFORM, {0xF1, 0xF1, 0xF1, 0x00}}, |
| 31 | {PLDM_BIOS, {0xF1, 0xF0, 0xF0, 0x00}}, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 34 | namespace base |
| 35 | { |
| 36 | |
| 37 | void 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 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 48 | Response getPLDMTypes(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 49 | { |
| 50 | // DSP0240 has this as a bitfield8[N], where N = 0 to 7 |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 51 | std::array<bitfield8_t, 8> types{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 52 | 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 Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 57 | types[index].byte |= 1 << bit; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 58 | } |
| 59 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 60 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0); |
| 61 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 62 | encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS, types.data(), |
| 63 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 64 | |
| 65 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 66 | } |
| 67 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 68 | Response getPLDMCommands(const pldm_msg* request, size_t payloadLength) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 69 | { |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 70 | ver32_t version{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 71 | Type type; |
| 72 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 73 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0); |
| 74 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 75 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 76 | auto rc = decode_get_commands_req(request, payloadLength, &type, &version); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 77 | |
| 78 | if (rc != PLDM_SUCCESS) |
| 79 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 80 | encode_get_commands_resp(request->hdr.instance_id, rc, nullptr, |
| 81 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 82 | return response; |
| 83 | } |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 84 | |
| 85 | // DSP0240 has this as a bitfield8[N], where N = 0 to 31 |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 86 | std::array<bitfield8_t, 32> cmds{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 87 | if (capabilities.find(type) == capabilities.end()) |
| 88 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 89 | encode_get_commands_resp(request->hdr.instance_id, |
| 90 | PLDM_ERROR_INVALID_PLDM_TYPE, nullptr, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 91 | responsePtr); |
| 92 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 93 | } |
| 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 Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 100 | cmds[index].byte |= 1 << bit; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 103 | encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS, |
| 104 | cmds.data(), responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 105 | |
| 106 | return response; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 107 | } |
| 108 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 109 | Response getPLDMVersion(const pldm_msg* request, size_t payloadLength) |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 110 | { |
| 111 | uint32_t transferHandle; |
| 112 | Type type; |
| 113 | uint8_t transferFlag; |
| 114 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 115 | Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0); |
| 116 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 117 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 118 | uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle, |
| 119 | &transferFlag, &type); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 120 | |
| 121 | if (rc != PLDM_SUCCESS) |
| 122 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 123 | encode_get_version_resp(request->hdr.instance_id, rc, 0, 0, nullptr, 4, |
| 124 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 125 | return response; |
| 126 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 127 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 128 | ver32_t version{}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 129 | auto search = versions.find(type); |
| 130 | |
| 131 | if (search == versions.end()) |
| 132 | { |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 133 | encode_get_version_resp(request->hdr.instance_id, |
| 134 | PLDM_ERROR_INVALID_PLDM_TYPE, 0, 0, nullptr, 4, |
| 135 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 136 | return response; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | memcpy(&version, &(search->second), sizeof(version)); |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 140 | encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0, |
| 141 | PLDM_START_AND_END, &version, sizeof(pldm_version), |
| 142 | responsePtr); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 143 | |
| 144 | return response; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 145 | } |
| 146 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 147 | } // namespace responder |
| 148 | } // namespace pldm |