1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ 2 #include "dsp/base.h" 3 #include "msgbuf.h" 4 5 #include <libpldm/base.h> 6 #include <libpldm/file.h> 7 #include <libpldm/utils.h> 8 9 #include <assert.h> 10 #include <errno.h> 11 #include <stdbool.h> 12 #include <stdint.h> 13 #include <string.h> 14 #include <stdlib.h> 15 16 LIBPLDM_ABI_TESTING 17 int encode_pldm_file_df_open_req(uint8_t instance_id, 18 const struct pldm_file_df_open_req *req, 19 struct pldm_msg *msg, size_t payload_length) 20 { 21 PLDM_MSGBUF_DEFINE_P(buf); 22 int rc; 23 24 if (req == NULL || msg == NULL) { 25 return -EINVAL; 26 } 27 28 struct pldm_header_info header = { 0 }; 29 header.instance = instance_id; 30 header.msg_type = PLDM_REQUEST; 31 header.pldm_type = PLDM_FILE; 32 header.command = PLDM_FILE_CMD_DF_OPEN; 33 34 rc = pack_pldm_header_errno(&header, &(msg->hdr)); 35 if (rc) { 36 return rc; 37 } 38 39 rc = pldm_msgbuf_init_errno(buf, PLDM_DF_OPEN_REQ_BYTES, msg->payload, 40 payload_length); 41 if (rc) { 42 return rc; 43 } 44 45 pldm_msgbuf_insert(buf, req->file_identifier); 46 pldm_msgbuf_insert(buf, req->file_attribute.value); 47 48 return pldm_msgbuf_complete(buf); 49 } 50 51 LIBPLDM_ABI_TESTING 52 int decode_pldm_file_df_open_resp(const struct pldm_msg *msg, 53 size_t payload_length, 54 struct pldm_file_df_open_resp *resp) 55 { 56 PLDM_MSGBUF_DEFINE_P(buf); 57 int rc; 58 59 if (!msg || !resp) { 60 return -EINVAL; 61 } 62 63 rc = pldm_msg_has_error(msg, payload_length); 64 if (rc) { 65 resp->completion_code = rc; 66 return 0; 67 } 68 69 rc = pldm_msgbuf_init_errno(buf, PLDM_DF_OPEN_RESP_BYTES, msg->payload, 70 payload_length); 71 if (rc) { 72 return rc; 73 } 74 75 pldm_msgbuf_extract(buf, resp->completion_code); 76 pldm_msgbuf_extract(buf, resp->file_descriptor); 77 78 return pldm_msgbuf_complete_consumed(buf); 79 } 80 81 LIBPLDM_ABI_TESTING 82 int encode_pldm_file_df_close_req(uint8_t instance_id, 83 const struct pldm_file_df_close_req *req, 84 struct pldm_msg *msg, size_t payload_length) 85 { 86 PLDM_MSGBUF_DEFINE_P(buf); 87 int rc; 88 89 if (!req || !msg) { 90 return -EINVAL; 91 } 92 93 struct pldm_header_info header = { 0 }; 94 header.instance = instance_id; 95 header.msg_type = PLDM_REQUEST; 96 header.pldm_type = PLDM_FILE; 97 header.command = PLDM_FILE_CMD_DF_CLOSE; 98 99 rc = pack_pldm_header_errno(&header, &(msg->hdr)); 100 if (rc) { 101 return rc; 102 } 103 104 rc = pldm_msgbuf_init_errno(buf, PLDM_DF_CLOSE_REQ_BYTES, msg->payload, 105 payload_length); 106 if (rc) { 107 return rc; 108 } 109 110 pldm_msgbuf_insert(buf, req->file_descriptor); 111 pldm_msgbuf_insert(buf, req->df_close_options.value); 112 113 return pldm_msgbuf_complete(buf); 114 } 115 116 LIBPLDM_ABI_TESTING 117 int decode_pldm_file_df_close_resp(const struct pldm_msg *msg, 118 size_t payload_length, 119 struct pldm_file_df_close_resp *resp) 120 { 121 if (!msg || !resp) { 122 return -EINVAL; 123 } 124 125 resp->completion_code = pldm_msg_has_error(msg, payload_length); 126 127 return 0; 128 } 129 130 LIBPLDM_ABI_TESTING 131 int encode_pldm_file_df_heartbeat_req( 132 uint8_t instance_id, const struct pldm_file_df_heartbeat_req *req, 133 struct pldm_msg *msg, size_t payload_length) 134 { 135 PLDM_MSGBUF_DEFINE_P(buf); 136 int rc; 137 138 if (!req || !msg) { 139 return -EINVAL; 140 } 141 142 struct pldm_header_info header = { 0 }; 143 header.instance = instance_id; 144 header.msg_type = PLDM_REQUEST; 145 header.pldm_type = PLDM_FILE; 146 header.command = PLDM_FILE_CMD_DF_HEARTBEAT; 147 148 rc = pack_pldm_header_errno(&header, &(msg->hdr)); 149 if (rc) { 150 return rc; 151 } 152 153 rc = pldm_msgbuf_init_errno(buf, PLDM_DF_HEARTBEAT_REQ_BYTES, 154 msg->payload, payload_length); 155 if (rc) { 156 return rc; 157 } 158 159 pldm_msgbuf_insert(buf, req->file_descriptor); 160 pldm_msgbuf_insert(buf, req->requester_max_interval); 161 162 return pldm_msgbuf_complete(buf); 163 } 164 165 LIBPLDM_ABI_TESTING 166 int decode_pldm_file_df_heartbeat_resp(const struct pldm_msg *msg, 167 size_t payload_length, 168 struct pldm_file_df_heartbeat_resp *resp) 169 { 170 PLDM_MSGBUF_DEFINE_P(buf); 171 int rc; 172 173 if (!msg || !resp) { 174 return -EINVAL; 175 } 176 177 rc = pldm_msg_has_error(msg, payload_length); 178 if (rc) { 179 resp->completion_code = rc; 180 return 0; 181 } 182 183 rc = pldm_msgbuf_init_errno(buf, PLDM_DF_HEARTBEAT_RESP_BYTES, 184 msg->payload, payload_length); 185 if (rc) { 186 return rc; 187 } 188 189 pldm_msgbuf_extract(buf, resp->completion_code); 190 pldm_msgbuf_extract(buf, resp->responder_max_interval); 191 192 return pldm_msgbuf_complete_consumed(buf); 193 } 194