1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */ 2 /* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */ 3 4 #ifndef _MLXFW_MFA2_TLV_H 5 #define _MLXFW_MFA2_TLV_H 6 7 #include <linux/kernel.h> 8 #include "mlxfw_mfa2_file.h" 9 10 struct mlxfw_mfa2_tlv { 11 u8 version; 12 u8 type; 13 __be16 len; 14 u8 data[]; 15 } __packed; 16 17 static inline const struct mlxfw_mfa2_tlv * 18 mlxfw_mfa2_tlv_get(const struct mlxfw_mfa2_file *mfa2_file, const void *ptr) 19 { 20 if (!mlxfw_mfa2_valid_ptr(mfa2_file, ptr) || 21 !mlxfw_mfa2_valid_ptr(mfa2_file, ptr + sizeof(struct mlxfw_mfa2_tlv))) 22 return NULL; 23 return ptr; 24 } 25 26 static inline const void * 27 mlxfw_mfa2_tlv_payload_get(const struct mlxfw_mfa2_file *mfa2_file, 28 const struct mlxfw_mfa2_tlv *tlv, u8 payload_type, 29 size_t payload_size, bool varsize) 30 { 31 void *tlv_top; 32 33 tlv_top = (void *) tlv + be16_to_cpu(tlv->len) - 1; 34 if (!mlxfw_mfa2_valid_ptr(mfa2_file, tlv) || 35 !mlxfw_mfa2_valid_ptr(mfa2_file, tlv_top)) 36 return NULL; 37 if (tlv->type != payload_type) 38 return NULL; 39 if (varsize && (be16_to_cpu(tlv->len) < payload_size)) 40 return NULL; 41 if (!varsize && (be16_to_cpu(tlv->len) != payload_size)) 42 return NULL; 43 44 return tlv->data; 45 } 46 47 #define MLXFW_MFA2_TLV(name, payload_type, tlv_type) \ 48 static inline const payload_type * \ 49 mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \ 50 const struct mlxfw_mfa2_tlv *tlv) \ 51 { \ 52 return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \ 53 tlv_type, sizeof(payload_type), \ 54 false); \ 55 } 56 57 #define MLXFW_MFA2_TLV_VARSIZE(name, payload_type, tlv_type) \ 58 static inline const payload_type * \ 59 mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \ 60 const struct mlxfw_mfa2_tlv *tlv) \ 61 { \ 62 return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \ 63 tlv_type, sizeof(payload_type), \ 64 true); \ 65 } 66 67 #endif 68