blob: cdaa2d594241b26dea6d51e9592fa201adb9eaaf [file] [log] [blame]
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -05001#include "pldm_oem_ibm.hpp"
2
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -05003#include "../../pldm_cmd_helper.hpp"
4
5#include <endian.h>
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/file_io.h>
7#include <libpldm/host.h>
8#include <libpldm/pldm_types.h>
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -05009
10#include <iostream>
11#include <string>
12namespace pldmtool
13{
14
15namespace oem_ibm
16{
17namespace
18{
19
20using namespace pldmtool::helper;
21
22std::vector<std::unique_ptr<CommandInterface>> commands;
23
24const std::map<const char*, pldm_fileio_table_type> pldmFileIOTableTypes{
25 {"AttributeTable", PLDM_FILE_ATTRIBUTE_TABLE},
26};
27
28constexpr uint8_t CHKSUM_PADDING = 8;
29
30} // namespace
31
32class GetAlertStatus : public CommandInterface
33{
34 public:
35 ~GetAlertStatus() = default;
36 GetAlertStatus() = delete;
37 GetAlertStatus(const GetAlertStatus&) = delete;
38 GetAlertStatus(GetAlertStatus&&) = default;
39 GetAlertStatus& operator=(const GetAlertStatus&) = delete;
40 GetAlertStatus& operator=(GetAlertStatus&&) = default;
41
42 explicit GetAlertStatus(const char* type, const char* name, CLI::App* app) :
43 CommandInterface(type, name, app)
44 {
45 app->add_option(
46 "-i, --id", versionId,
47 "Version of the command/response format. 0x00 for this format")
48 ->required();
49 }
50
51 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
52 {
53 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
54 PLDM_GET_ALERT_STATUS_REQ_BYTES);
55 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
56
57 auto rc = encode_get_alert_status_req(instanceId, versionId, request,
58 PLDM_GET_ALERT_STATUS_REQ_BYTES);
59 return {rc, requestMsg};
60 }
61
62 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
63 {
64 uint8_t completionCode = 0;
65 uint32_t rack_entry = 0;
66 uint32_t pri_cec_node = 0;
67 auto rc = decode_get_alert_status_resp(responsePtr, payloadLength,
68 &completionCode, &rack_entry,
69 &pri_cec_node);
70
71 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
72 {
73 std::cerr << "Response Message Error: "
74 << "rc=" << rc << ",cc=" << (int)completionCode << "\n";
75 return;
76 }
77
Sridevi Rameshdd049902020-08-12 01:43:51 -050078 std::stringstream re;
79 std::stringstream pcn;
80 ordered_json data;
81 re << "0x" << std::setfill('0') << std::setw(8) << std::hex
82 << (int)rack_entry;
83 pcn << "0x" << std::setfill('0') << std::setw(8) << std::hex
84 << (int)pri_cec_node;
85 data["rack entry"] = re.str();
86 data["pri cec node"] = pcn.str();
87 pldmtool::helper::DisplayInJson(data);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -050088 }
89
90 private:
91 uint8_t versionId;
92};
93
94class GetFileTable : public CommandInterface
95{
96 public:
97 ~GetFileTable() = default;
98 GetFileTable() = delete;
99 GetFileTable(const GetFileTable&) = delete;
100 GetFileTable(GetFileTable&&) = default;
101 GetFileTable& operator=(const GetFileTable&) = delete;
102 GetFileTable& operator=(GetFileTable&&) = default;
103
104 using CommandInterface::CommandInterface;
105
106 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
107 {
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500108 return {PLDM_ERROR, {}};
109 }
110
Patrick Williams6da4f912023-05-10 07:50:53 -0500111 void parseResponseMsg(pldm_msg*, size_t) override {}
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500112 void exec()
113 {
114 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
115 PLDM_GET_FILE_TABLE_REQ_BYTES);
116
117 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
118
119 auto rc = encode_get_file_table_req(instanceId, 0, PLDM_GET_FIRSTPART,
120 0, request);
121 if (rc != PLDM_SUCCESS)
122 {
123 std::cerr << "PLDM: Request Message Error, rc =" << rc << std::endl;
124 return;
125 }
126
127 std::vector<uint8_t> responseMsg;
128 rc = pldmSendRecv(requestMsg, responseMsg);
129 if (rc != PLDM_SUCCESS)
130 {
131 std::cerr << "PLDM: Communication Error, rc =" << rc << std::endl;
132 return;
133 }
134
135 uint8_t cc = 0;
136 uint8_t transferFlag = 0;
137 uint32_t nextTransferHandle = 0;
138 size_t fileTableDataLength = 0;
139 uint8_t table_data_start_offset;
140 auto responsePtr =
141 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
142 auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
143
144 rc = decode_get_file_table_resp(
145 responsePtr, payloadLength, &cc, &nextTransferHandle, &transferFlag,
146 &table_data_start_offset, &fileTableDataLength);
147
148 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
149 {
150 std::cerr << "Response Message Error: "
151 << ", rc=" << rc << ", cc=" << (int)cc << std::endl;
152 return;
153 }
154
155 auto tableData = reinterpret_cast<uint8_t*>((responsePtr->payload) +
156 table_data_start_offset);
157 printFileAttrTable(tableData, fileTableDataLength);
158 }
159
160 void printFileAttrTable(uint8_t* data, size_t length)
161 {
162 if (data == NULL || length == 0)
163 {
164 return;
165 }
166
167 auto startptr = data;
168 auto endptr = startptr + length - CHKSUM_PADDING;
Sridevi Rameshdd049902020-08-12 01:43:51 -0500169 ordered_json kwVal;
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500170
171 while (startptr < endptr)
172 {
Sridevi Rameshdd049902020-08-12 01:43:51 -0500173 ordered_json fdata;
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500174 auto filetableData =
175 reinterpret_cast<pldm_file_attr_table_entry*>(startptr);
Sridevi Rameshdd049902020-08-12 01:43:51 -0500176 fdata["FileHandle"] = std::to_string(filetableData->file_handle);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500177 startptr += sizeof(filetableData->file_handle);
178
179 auto nameLength = filetableData->file_name_length;
Sridevi Rameshdd049902020-08-12 01:43:51 -0500180 fdata["FileNameLength"] = nameLength;
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500181 startptr += sizeof(filetableData->file_name_length);
182
Sridevi Rameshdd049902020-08-12 01:43:51 -0500183 fdata["FileName"] = (std::string(
Patrick Williams6da4f912023-05-10 07:50:53 -0500184 reinterpret_cast<const char*>(startptr), nameLength));
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500185 startptr += nameLength;
186
187 auto fileSize = *(reinterpret_cast<uint32_t*>(startptr));
Sridevi Rameshdd049902020-08-12 01:43:51 -0500188 fdata["FileSize"] = le32toh(fileSize);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500189 startptr += sizeof(fileSize);
190
191 auto fileTraits =
192 (*(reinterpret_cast<bitfield32_t*>(startptr))).value;
Sridevi Rameshdd049902020-08-12 01:43:51 -0500193 fdata["FileTraits"] = le32toh(fileTraits);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500194 startptr += sizeof(fileTraits);
Sridevi Rameshdd049902020-08-12 01:43:51 -0500195 kwVal.emplace_back(fdata);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500196 }
Sridevi Rameshdd049902020-08-12 01:43:51 -0500197 pldmtool::helper::DisplayInJson(kwVal);
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500198 }
199};
200
201void registerCommand(CLI::App& app)
202{
203 auto oem_ibm = app.add_subcommand("oem-ibm", "oem type command");
204 oem_ibm->require_subcommand(1);
205
206 auto getAlertStatus = oem_ibm->add_subcommand(
207 "GetAlertStatus", "get alert status descriptor");
208 commands.push_back(std::make_unique<GetAlertStatus>(
209 "oem_ibm", "getAlertStatus", getAlertStatus));
210
Patrick Williams6da4f912023-05-10 07:50:53 -0500211 auto getFileTable = oem_ibm->add_subcommand("GetFileTable",
212 "get file table");
Pavithra Barithayac4e80cc2020-05-26 07:00:26 -0500213
214 commands.push_back(std::make_unique<GetFileTable>("oem_ibm", "getFileTable",
215 getFileTable));
216}
217} // namespace oem_ibm
218} // namespace pldmtool