Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 2 | #ifndef PLDM_MSGBUF_PLATFORM_H |
| 3 | #define PLDM_MSGBUF_PLATFORM_H |
| 4 | |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 5 | #include "../compiler.h" |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 6 | #include "../msgbuf.h" |
| 7 | #include <libpldm/base.h> |
| 8 | #include <libpldm/platform.h> |
| 9 | |
Andrew Jeffery | 329176a | 2024-09-26 22:38:24 +0930 | [diff] [blame] | 10 | LIBPLDM_CC_NONNULL |
| 11 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 12 | pldm_msgbuf_extract_value_pdr_hdr(struct pldm_msgbuf *ctx, |
Andrew Jeffery | 329176a | 2024-09-26 22:38:24 +0930 | [diff] [blame] | 13 | struct pldm_value_pdr_hdr *hdr, size_t lower, |
| 14 | size_t upper) |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 15 | { |
Andrew Jeffery | 329176a | 2024-09-26 22:38:24 +0930 | [diff] [blame] | 16 | int rc; |
| 17 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 18 | pldm_msgbuf_extract(ctx, hdr->record_handle); |
| 19 | pldm_msgbuf_extract(ctx, hdr->version); |
| 20 | pldm_msgbuf_extract(ctx, hdr->type); |
| 21 | pldm_msgbuf_extract(ctx, hdr->record_change_num); |
Andrew Jeffery | 329176a | 2024-09-26 22:38:24 +0930 | [diff] [blame] | 22 | rc = pldm_msgbuf_extract(ctx, hdr->length); |
| 23 | if (rc) { |
| 24 | return rc; |
| 25 | } |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 26 | |
Andrew Jeffery | 329176a | 2024-09-26 22:38:24 +0930 | [diff] [blame] | 27 | if (hdr->length + sizeof(*ctx) < lower) { |
| 28 | return -EOVERFLOW; |
| 29 | } |
| 30 | |
| 31 | if (hdr->length > upper) { |
| 32 | return -EOVERFLOW; |
| 33 | } |
| 34 | |
| 35 | return 0; |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 36 | } |
| 37 | |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 38 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 39 | pldm_msgbuf_extract_sensor_data(struct pldm_msgbuf *ctx, |
| 40 | enum pldm_sensor_readings_data_type tag, |
| 41 | union_sensor_data_size *dst) |
| 42 | { |
| 43 | switch (tag) { |
| 44 | case PLDM_SENSOR_DATA_SIZE_UINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 45 | return pldm_msgbuf_extract(ctx, dst->value_u8); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 46 | case PLDM_SENSOR_DATA_SIZE_SINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 47 | return pldm_msgbuf_extract(ctx, dst->value_s8); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 48 | case PLDM_SENSOR_DATA_SIZE_UINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 49 | return pldm_msgbuf_extract(ctx, dst->value_u16); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 50 | case PLDM_SENSOR_DATA_SIZE_SINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 51 | return pldm_msgbuf_extract(ctx, dst->value_s16); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 52 | case PLDM_SENSOR_DATA_SIZE_UINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 53 | return pldm_msgbuf_extract(ctx, dst->value_u32); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 54 | case PLDM_SENSOR_DATA_SIZE_SINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 55 | return pldm_msgbuf_extract(ctx, dst->value_s32); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return -PLDM_ERROR_INVALID_DATA; |
| 59 | } |
| 60 | |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 61 | /* |
| 62 | * This API is bad, but it's because the caller's APIs are also bad. They should |
| 63 | * have used the approach used by callers of pldm_msgbuf_extract_sensor_data() |
| 64 | * above |
| 65 | */ |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 66 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 67 | pldm_msgbuf_extract_sensor_value(struct pldm_msgbuf *ctx, |
Thu Nguyen | cd07fec | 2023-11-10 16:15:09 +0700 | [diff] [blame] | 68 | enum pldm_sensor_readings_data_type tag, |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 69 | void *val) |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 70 | { |
| 71 | switch (tag) { |
| 72 | case PLDM_SENSOR_DATA_SIZE_UINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 73 | return pldm__msgbuf_extract_uint8(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 74 | case PLDM_SENSOR_DATA_SIZE_SINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 75 | return pldm__msgbuf_extract_int8(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 76 | case PLDM_SENSOR_DATA_SIZE_UINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 77 | return pldm__msgbuf_extract_uint16(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 78 | case PLDM_SENSOR_DATA_SIZE_SINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 79 | return pldm__msgbuf_extract_int16(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 80 | case PLDM_SENSOR_DATA_SIZE_UINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 81 | return pldm__msgbuf_extract_uint32(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 82 | case PLDM_SENSOR_DATA_SIZE_SINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 83 | return pldm__msgbuf_extract_int32(ctx, val); |
Andrew Jeffery | 840b140 | 2023-04-13 23:54:44 +0930 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return -PLDM_ERROR_INVALID_DATA; |
| 87 | } |
| 88 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 89 | #define pldm_msgbuf_extract_range_field_format(ctx, tag, dst) \ |
| 90 | pldm_msgbuf_extract_typecheck(union_range_field_format, \ |
| 91 | pldm__msgbuf_extract_range_field_format, \ |
| 92 | dst, ctx, tag, (void *)&(dst)) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 93 | LIBPLDM_CC_ALWAYS_INLINE int pldm__msgbuf_extract_range_field_format( |
| 94 | struct pldm_msgbuf *ctx, enum pldm_range_field_format tag, void *rff) |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 95 | { |
| 96 | switch (tag) { |
| 97 | case PLDM_RANGE_FIELD_FORMAT_UINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 98 | return pldm__msgbuf_extract_uint8( |
| 99 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 100 | value_u8)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 101 | case PLDM_RANGE_FIELD_FORMAT_SINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 102 | return pldm__msgbuf_extract_int8( |
| 103 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 104 | value_s8)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 105 | case PLDM_RANGE_FIELD_FORMAT_UINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 106 | return pldm__msgbuf_extract_uint16( |
| 107 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 108 | value_u16)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 109 | case PLDM_RANGE_FIELD_FORMAT_SINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 110 | return pldm__msgbuf_extract_int16( |
| 111 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 112 | value_s16)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 113 | case PLDM_RANGE_FIELD_FORMAT_UINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 114 | return pldm__msgbuf_extract_uint32( |
| 115 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 116 | value_u32)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 117 | case PLDM_RANGE_FIELD_FORMAT_SINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 118 | return pldm__msgbuf_extract_int32( |
| 119 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 120 | value_s32)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 121 | case PLDM_RANGE_FIELD_FORMAT_REAL32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 122 | return pldm__msgbuf_extract_real32( |
| 123 | ctx, ((char *)rff) + offsetof(union_range_field_format, |
| 124 | value_f32)); |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return -PLDM_ERROR_INVALID_DATA; |
| 128 | } |
| 129 | |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 130 | /* This API is bad, but it's because the caller's APIs are also bad */ |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 131 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 132 | pldm_msgbuf_extract_effecter_value(struct pldm_msgbuf *ctx, |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 133 | enum pldm_effecter_data_size tag, void *dst) |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 134 | { |
| 135 | switch (tag) { |
| 136 | case PLDM_EFFECTER_DATA_SIZE_UINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 137 | return pldm__msgbuf_extract_uint8(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 138 | case PLDM_EFFECTER_DATA_SIZE_SINT8: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 139 | return pldm__msgbuf_extract_int8(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 140 | case PLDM_EFFECTER_DATA_SIZE_UINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 141 | return pldm__msgbuf_extract_uint16(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 142 | case PLDM_EFFECTER_DATA_SIZE_SINT16: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 143 | return pldm__msgbuf_extract_int16(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 144 | case PLDM_EFFECTER_DATA_SIZE_UINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 145 | return pldm__msgbuf_extract_uint32(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 146 | case PLDM_EFFECTER_DATA_SIZE_SINT32: |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 147 | return pldm__msgbuf_extract_int32(ctx, dst); |
Andrew Jeffery | 3884c44 | 2023-04-12 11:13:24 +0930 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return -PLDM_ERROR_INVALID_DATA; |
| 151 | } |
| 152 | |
Thu Nguyen | d4878cd | 2023-11-09 10:18:33 +0700 | [diff] [blame] | 153 | #define pldm_msgbuf_extract_effecter_data(ctx, tag, dst) \ |
| 154 | pldm_msgbuf_extract_typecheck(union_effecter_data_size, \ |
| 155 | pldm__msgbuf_extract_range_field_format, \ |
| 156 | dst, ctx, tag, (void *)&(dst)) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 157 | LIBPLDM_CC_ALWAYS_INLINE int |
Thu Nguyen | d4878cd | 2023-11-09 10:18:33 +0700 | [diff] [blame] | 158 | pldm__msgbuf_extract_effecter_data(struct pldm_msgbuf *ctx, |
| 159 | enum pldm_effecter_data_size tag, void *ed) |
| 160 | { |
| 161 | switch (tag) { |
| 162 | case PLDM_EFFECTER_DATA_SIZE_UINT8: |
| 163 | return pldm__msgbuf_extract_uint8( |
| 164 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 165 | value_u8)); |
| 166 | case PLDM_EFFECTER_DATA_SIZE_SINT8: |
| 167 | return pldm__msgbuf_extract_int8( |
| 168 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 169 | value_s8)); |
| 170 | case PLDM_EFFECTER_DATA_SIZE_UINT16: |
| 171 | return pldm__msgbuf_extract_uint16( |
| 172 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 173 | value_u16)); |
| 174 | case PLDM_EFFECTER_DATA_SIZE_SINT16: |
| 175 | return pldm__msgbuf_extract_int16( |
| 176 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 177 | value_s16)); |
| 178 | case PLDM_EFFECTER_DATA_SIZE_UINT32: |
| 179 | return pldm__msgbuf_extract_uint32( |
| 180 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 181 | value_u32)); |
| 182 | case PLDM_EFFECTER_DATA_SIZE_SINT32: |
| 183 | return pldm__msgbuf_extract_int32( |
| 184 | ctx, ((char *)ed) + offsetof(union_effecter_data_size, |
| 185 | value_s32)); |
| 186 | } |
| 187 | |
| 188 | return -PLDM_ERROR_INVALID_DATA; |
| 189 | } |
| 190 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 191 | #ifdef __cplusplus |
| 192 | #include <type_traits> |
| 193 | |
| 194 | template <typename T> |
| 195 | static inline int pldm_msgbuf_typecheck_range_field_format( |
| 196 | struct pldm_msgbuf *ctx, enum pldm_range_field_format tag, void *_rff) |
| 197 | { |
| 198 | static_assert(std::is_same<union_range_field_format, T>::value); |
| 199 | return pldm__msgbuf_extract_range_field_format(ctx, tag, _rff); |
| 200 | } |
| 201 | #endif |
| 202 | |
Andrew Jeffery | 7992eb8 | 2023-04-06 16:13:53 +0930 | [diff] [blame] | 203 | #endif |