John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <endian.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "bios.h" |
| 9 | #include "bios_table.h" |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 10 | #include "utils.h" |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 11 | |
| 12 | #define POINTER_CHECK(pointer) \ |
| 13 | do { \ |
| 14 | if (pointer == NULL) \ |
| 15 | return PLDM_ERROR_INVALID_DATA; \ |
| 16 | } while (0) |
| 17 | |
| 18 | #define ATTR_TYPE_EXPECT(type, expected) \ |
| 19 | do { \ |
| 20 | if (type != expected && type != (expected | 0x80)) \ |
| 21 | return PLDM_ERROR_INVALID_DATA; \ |
| 22 | } while (0) |
| 23 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 24 | #define BUFFER_SIZE_EXPECT(current_size, expected_size) \ |
| 25 | do { \ |
| 26 | if (current_size < expected_size) \ |
| 27 | return PLDM_ERROR_INVALID_LENGTH; \ |
| 28 | } while (0) |
| 29 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 30 | #define MEMBER_SIZE(type, member) sizeof(((struct type *)0)->member) |
| 31 | |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 32 | static void set_errmsg(const char **errmsg, const char *msg) |
| 33 | { |
| 34 | if (errmsg != NULL) |
| 35 | *errmsg = msg; |
| 36 | } |
| 37 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 38 | static uint16_t get_bios_string_handle() |
| 39 | { |
| 40 | static uint16_t handle = 0; |
| 41 | assert(handle != UINT16_MAX); |
| 42 | |
| 43 | return handle++; |
| 44 | } |
| 45 | |
| 46 | size_t pldm_bios_table_string_entry_encode_length(uint16_t string_length) |
| 47 | { |
| 48 | return sizeof(struct pldm_bios_string_table_entry) - |
| 49 | MEMBER_SIZE(pldm_bios_string_table_entry, name) + string_length; |
| 50 | } |
| 51 | |
| 52 | void pldm_bios_table_string_entry_encode(void *entry, size_t entry_length, |
| 53 | const char *str, uint16_t str_length) |
| 54 | { |
| 55 | size_t length = pldm_bios_table_string_entry_encode_length(str_length); |
| 56 | assert(length <= entry_length); |
| 57 | struct pldm_bios_string_table_entry *string_entry = entry; |
| 58 | string_entry->string_handle = htole16(get_bios_string_handle()); |
| 59 | string_entry->string_length = htole16(str_length); |
| 60 | memcpy(string_entry->name, str, str_length); |
| 61 | } |
| 62 | |
| 63 | int pldm_bios_table_string_entry_encode_check(void *entry, size_t entry_length, |
| 64 | const char *str, |
| 65 | uint16_t str_length) |
| 66 | { |
| 67 | if (str_length == 0) |
| 68 | return PLDM_ERROR_INVALID_DATA; |
| 69 | POINTER_CHECK(entry); |
| 70 | POINTER_CHECK(str); |
| 71 | size_t length = pldm_bios_table_string_entry_encode_length(str_length); |
| 72 | BUFFER_SIZE_EXPECT(entry_length, length); |
| 73 | pldm_bios_table_string_entry_encode(entry, entry_length, str, |
| 74 | str_length); |
| 75 | return PLDM_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | uint16_t pldm_bios_table_string_entry_decode_handle( |
| 79 | const struct pldm_bios_string_table_entry *entry) |
| 80 | { |
| 81 | return le16toh(entry->string_handle); |
| 82 | } |
| 83 | |
| 84 | uint16_t pldm_bios_table_string_entry_decode_string_length( |
| 85 | const struct pldm_bios_string_table_entry *entry) |
| 86 | { |
| 87 | return le16toh(entry->string_length); |
| 88 | } |
| 89 | |
| 90 | uint16_t pldm_bios_table_string_entry_decode_string( |
| 91 | const struct pldm_bios_string_table_entry *entry, char *buffer, size_t size) |
| 92 | { |
| 93 | uint16_t length = |
| 94 | pldm_bios_table_string_entry_decode_string_length(entry); |
| 95 | length = length < size ? length : size; |
| 96 | memcpy(buffer, entry->name, length); |
| 97 | buffer[length] = 0; |
| 98 | return length; |
| 99 | } |
| 100 | |
| 101 | int pldm_bios_table_string_entry_decode_string_check( |
| 102 | const struct pldm_bios_string_table_entry *entry, char *buffer, size_t size) |
| 103 | { |
| 104 | POINTER_CHECK(entry); |
| 105 | POINTER_CHECK(buffer); |
| 106 | size_t length = |
| 107 | pldm_bios_table_string_entry_decode_string_length(entry); |
| 108 | BUFFER_SIZE_EXPECT(size, length + 1); |
| 109 | pldm_bios_table_string_entry_decode_string(entry, buffer, size); |
| 110 | return PLDM_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | static size_t string_table_entry_length(const void *table_entry) |
| 114 | { |
| 115 | const struct pldm_bios_string_table_entry *entry = table_entry; |
| 116 | return sizeof(*entry) - sizeof(entry->name) + |
| 117 | pldm_bios_table_string_entry_decode_string_length(entry); |
| 118 | } |
| 119 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 120 | static uint16_t get_bios_attr_handle() |
| 121 | { |
| 122 | static uint16_t handle = 0; |
| 123 | assert(handle != UINT16_MAX); |
| 124 | |
| 125 | return handle++; |
| 126 | } |
| 127 | |
| 128 | static void attr_table_entry_encode_header(void *entry, size_t length, |
| 129 | uint8_t attr_type, |
| 130 | uint16_t string_handle) |
| 131 | { |
| 132 | struct pldm_bios_attr_table_entry *attr_entry = entry; |
| 133 | assert(sizeof(*attr_entry) <= length); |
| 134 | attr_entry->attr_handle = htole16(get_bios_attr_handle()); |
| 135 | attr_entry->attr_type = attr_type; |
| 136 | attr_entry->string_handle = htole16(string_handle); |
| 137 | } |
| 138 | |
| 139 | size_t pldm_bios_table_attr_entry_enum_encode_length(uint8_t pv_num, |
| 140 | uint8_t def_num) |
| 141 | { |
| 142 | return sizeof(struct pldm_bios_attr_table_entry) - |
| 143 | MEMBER_SIZE(pldm_bios_attr_table_entry, metadata) + |
| 144 | sizeof(pv_num) + pv_num * sizeof(uint16_t) + sizeof(def_num) + |
| 145 | def_num; |
| 146 | } |
| 147 | |
| 148 | void pldm_bios_table_attr_entry_enum_encode( |
| 149 | void *entry, size_t entry_length, |
| 150 | const struct pldm_bios_table_attr_entry_enum_info *info) |
| 151 | { |
| 152 | size_t length = pldm_bios_table_attr_entry_enum_encode_length( |
| 153 | info->pv_num, info->def_num); |
| 154 | assert(length <= entry_length); |
| 155 | uint8_t attr_type = info->read_only ? PLDM_BIOS_ENUMERATION_READ_ONLY |
| 156 | : PLDM_BIOS_ENUMERATION; |
| 157 | attr_table_entry_encode_header(entry, entry_length, attr_type, |
| 158 | info->name_handle); |
| 159 | struct pldm_bios_attr_table_entry *attr_entry = entry; |
| 160 | attr_entry->metadata[0] = info->pv_num; |
| 161 | uint16_t *pv_hdls = |
| 162 | (uint16_t *)(attr_entry->metadata + 1 /* sizeof(pv num) */); |
| 163 | size_t i; |
| 164 | for (i = 0; i < info->pv_num; i++) |
| 165 | pv_hdls[i] = htole16(info->pv_handle[i]); |
| 166 | attr_entry->metadata[1 + info->pv_num * sizeof(uint16_t)] = |
| 167 | info->def_num; |
| 168 | memcpy(attr_entry->metadata + 1 /* sizeof(pv num) */ + |
| 169 | info->pv_num * sizeof(uint16_t) + 1 /* sizeof(def num)*/, |
| 170 | info->def_index, info->def_num); |
| 171 | } |
| 172 | |
| 173 | int pldm_bios_table_attr_entry_enum_encode_check( |
| 174 | void *entry, size_t entry_length, |
| 175 | const struct pldm_bios_table_attr_entry_enum_info *info) |
| 176 | { |
| 177 | POINTER_CHECK(entry); |
| 178 | POINTER_CHECK(info); |
| 179 | size_t length = pldm_bios_table_attr_entry_enum_encode_length( |
| 180 | info->pv_num, info->def_num); |
| 181 | BUFFER_SIZE_EXPECT(entry_length, length); |
| 182 | pldm_bios_table_attr_entry_enum_encode(entry, entry_length, info); |
| 183 | return PLDM_SUCCESS; |
| 184 | } |
| 185 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 186 | #define ATTR_TYPE_EXPECT(type, expected) \ |
| 187 | do { \ |
| 188 | if (type != expected && type != (expected | 0x80)) \ |
| 189 | return PLDM_ERROR_INVALID_DATA; \ |
| 190 | } while (0) |
| 191 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 192 | uint8_t pldm_bios_table_attr_entry_enum_decode_pv_num( |
| 193 | const struct pldm_bios_attr_table_entry *entry) |
| 194 | { |
| 195 | return entry->metadata[0]; |
| 196 | } |
| 197 | |
| 198 | int pldm_bios_table_attr_entry_enum_decode_pv_num_check( |
| 199 | const struct pldm_bios_attr_table_entry *entry, uint8_t *pv_num) |
| 200 | { |
| 201 | POINTER_CHECK(entry); |
| 202 | POINTER_CHECK(pv_num); |
| 203 | ATTR_TYPE_EXPECT(entry->attr_type, PLDM_BIOS_ENUMERATION); |
| 204 | *pv_num = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 205 | return PLDM_SUCCESS; |
| 206 | } |
| 207 | |
| 208 | uint8_t pldm_bios_table_attr_entry_enum_decode_def_num( |
| 209 | const struct pldm_bios_attr_table_entry *entry) |
| 210 | { |
| 211 | uint8_t pv_num = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 212 | return entry->metadata[sizeof(uint8_t) /* pv_num */ + |
| 213 | sizeof(uint16_t) * pv_num]; |
| 214 | } |
| 215 | |
| 216 | int pldm_bios_table_attr_entry_enum_decode_def_num_check( |
| 217 | const struct pldm_bios_attr_table_entry *entry, uint8_t *def_num) |
| 218 | { |
| 219 | POINTER_CHECK(entry); |
| 220 | POINTER_CHECK(def_num); |
| 221 | ATTR_TYPE_EXPECT(entry->attr_type, PLDM_BIOS_ENUMERATION); |
| 222 | *def_num = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
| 223 | return PLDM_SUCCESS; |
| 224 | } |
| 225 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 226 | uint8_t pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 227 | const struct pldm_bios_attr_table_entry *entry, uint16_t *pv_hdls, |
| 228 | uint8_t pv_num) |
| 229 | { |
| 230 | uint8_t num = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 231 | num = num < pv_num ? num : pv_num; |
| 232 | size_t i; |
| 233 | for (i = 0; i < num; i++) { |
| 234 | uint16_t *hdl = (uint16_t *)(entry->metadata + sizeof(uint8_t) + |
| 235 | i * sizeof(uint16_t)); |
| 236 | pv_hdls[i] = le16toh(*hdl); |
| 237 | } |
| 238 | return num; |
| 239 | } |
| 240 | |
| 241 | int pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 242 | const struct pldm_bios_attr_table_entry *entry, uint16_t *pv_hdls, |
| 243 | uint8_t pv_num) |
| 244 | { |
| 245 | POINTER_CHECK(entry); |
| 246 | POINTER_CHECK(pv_hdls); |
| 247 | ATTR_TYPE_EXPECT(entry->attr_type, PLDM_BIOS_ENUMERATION); |
| 248 | uint8_t num = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 249 | if (num != pv_num) |
| 250 | return PLDM_ERROR_INVALID_DATA; |
| 251 | pldm_bios_table_attr_entry_enum_decode_pv_hdls(entry, pv_hdls, pv_num); |
| 252 | return PLDM_SUCCESS; |
| 253 | } |
| 254 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 255 | /** @brief Get length of an enum attribute entry |
| 256 | */ |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 257 | static size_t attr_table_entry_length_enum(const void *entry) |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 258 | { |
| 259 | uint8_t pv_num = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 260 | uint8_t def_num = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 261 | return pldm_bios_table_attr_entry_enum_encode_length(pv_num, def_num); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 262 | } |
| 263 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 264 | struct attr_table_string_entry_fields { |
| 265 | uint8_t string_type; |
| 266 | uint16_t min_length; |
| 267 | uint16_t max_length; |
| 268 | uint16_t def_length; |
| 269 | uint8_t def_string[1]; |
| 270 | } __attribute__((packed)); |
| 271 | |
| 272 | size_t pldm_bios_table_attr_entry_string_encode_length(uint16_t def_str_len) |
| 273 | { |
| 274 | return sizeof(struct pldm_bios_attr_table_entry) - |
| 275 | MEMBER_SIZE(pldm_bios_attr_table_entry, metadata) + |
| 276 | sizeof(struct attr_table_string_entry_fields) - |
| 277 | MEMBER_SIZE(attr_table_string_entry_fields, def_string) + |
| 278 | def_str_len; |
| 279 | } |
| 280 | |
| 281 | void pldm_bios_table_attr_entry_string_encode( |
| 282 | void *entry, size_t entry_length, |
| 283 | const struct pldm_bios_table_attr_entry_string_info *info) |
| 284 | { |
| 285 | size_t length = |
| 286 | pldm_bios_table_attr_entry_string_encode_length(info->def_length); |
| 287 | assert(length <= entry_length); |
| 288 | uint8_t attr_type = |
| 289 | info->read_only ? PLDM_BIOS_STRING_READ_ONLY : PLDM_BIOS_STRING; |
| 290 | attr_table_entry_encode_header(entry, entry_length, attr_type, |
| 291 | info->name_handle); |
| 292 | struct pldm_bios_attr_table_entry *attr_entry = entry; |
| 293 | struct attr_table_string_entry_fields *attr_fields = |
| 294 | (struct attr_table_string_entry_fields *)attr_entry->metadata; |
| 295 | attr_fields->string_type = info->string_type; |
| 296 | attr_fields->min_length = htole16(info->min_length); |
| 297 | attr_fields->max_length = htole16(info->max_length); |
| 298 | attr_fields->def_length = htole16(info->def_length); |
| 299 | if (info->def_length != 0 && info->def_string != NULL) |
| 300 | memcpy(attr_fields->def_string, info->def_string, |
| 301 | info->def_length); |
| 302 | } |
| 303 | |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 304 | #define PLDM_STRING_TYPE_MAX 5 |
| 305 | #define PLDM_STRING_TYPE_VENDOR 0xff |
| 306 | |
| 307 | int pldm_bios_table_attr_entry_string_info_check( |
| 308 | const struct pldm_bios_table_attr_entry_string_info *info, |
| 309 | const char **errmsg) |
| 310 | { |
| 311 | if (info->min_length > info->max_length) { |
| 312 | set_errmsg(errmsg, "MinimumStingLength should not be greater " |
| 313 | "than MaximumStringLength"); |
| 314 | return PLDM_ERROR_INVALID_DATA; |
| 315 | } |
| 316 | if (info->min_length == info->max_length && |
| 317 | info->def_length != info->min_length) { |
| 318 | set_errmsg(errmsg, "Wrong DefaultStringLength"); |
| 319 | return PLDM_ERROR_INVALID_DATA; |
| 320 | } |
| 321 | if (info->def_length > info->max_length || |
| 322 | info->def_length < info->min_length) { |
| 323 | set_errmsg(errmsg, "Wrong DefaultStringLength"); |
| 324 | return PLDM_ERROR_INVALID_DATA; |
| 325 | } |
| 326 | if (info->string_type > PLDM_STRING_TYPE_MAX && |
| 327 | info->string_type != PLDM_STRING_TYPE_VENDOR) { |
| 328 | set_errmsg(errmsg, "Wrong StringType"); |
| 329 | return PLDM_ERROR_INVALID_DATA; |
| 330 | } |
| 331 | if (info->def_length != strlen(info->def_string)) { |
| 332 | set_errmsg(errmsg, "Length of DefaultString should be equal to " |
| 333 | "DefaultStringLength"); |
| 334 | return PLDM_ERROR_INVALID_DATA; |
| 335 | } |
| 336 | |
| 337 | return PLDM_SUCCESS; |
| 338 | } |
| 339 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 340 | int pldm_bios_table_attr_entry_string_encode_check( |
| 341 | void *entry, size_t entry_length, |
| 342 | const struct pldm_bios_table_attr_entry_string_info *info) |
| 343 | { |
| 344 | POINTER_CHECK(entry); |
| 345 | POINTER_CHECK(info); |
| 346 | size_t length = |
| 347 | pldm_bios_table_attr_entry_string_encode_length(info->def_length); |
| 348 | BUFFER_SIZE_EXPECT(entry_length, length); |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 349 | if (pldm_bios_table_attr_entry_string_info_check(info, NULL) != |
| 350 | PLDM_SUCCESS) |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 351 | return PLDM_ERROR_INVALID_DATA; |
| 352 | pldm_bios_table_attr_entry_string_encode(entry, entry_length, info); |
| 353 | return PLDM_SUCCESS; |
| 354 | } |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 355 | |
| 356 | uint16_t pldm_bios_table_attr_entry_string_decode_def_string_length( |
| 357 | const struct pldm_bios_attr_table_entry *entry) |
| 358 | { |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 359 | struct attr_table_string_entry_fields *fields = |
| 360 | (struct attr_table_string_entry_fields *)entry->metadata; |
| 361 | return le16toh(fields->def_length); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 365 | const struct pldm_bios_attr_table_entry *entry, uint16_t *def_string_length) |
| 366 | { |
| 367 | POINTER_CHECK(entry); |
| 368 | POINTER_CHECK(def_string_length); |
| 369 | ATTR_TYPE_EXPECT(entry->attr_type, PLDM_BIOS_STRING); |
| 370 | *def_string_length = |
| 371 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
| 372 | return PLDM_SUCCESS; |
| 373 | } |
| 374 | |
| 375 | /** @brief Get length of a string attribute entry |
| 376 | */ |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 377 | static size_t attr_table_entry_length_string(const void *entry) |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 378 | { |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 379 | uint16_t def_str_len = |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 380 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 381 | return pldm_bios_table_attr_entry_string_encode_length(def_str_len); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 382 | } |
| 383 | |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 384 | struct attr_table_integer_entry_fields { |
| 385 | uint64_t lower_bound; |
| 386 | uint64_t upper_bound; |
| 387 | uint32_t scalar_increment; |
| 388 | uint64_t default_value; |
| 389 | } __attribute__((packed)); |
| 390 | |
| 391 | size_t pldm_bios_table_attr_entry_integer_encode_length() |
| 392 | { |
| 393 | return sizeof(struct pldm_bios_attr_table_entry) - 1 + |
| 394 | sizeof(struct attr_table_integer_entry_fields); |
| 395 | } |
| 396 | |
| 397 | void pldm_bios_table_attr_entry_integer_encode( |
| 398 | void *entry, size_t entry_length, |
| 399 | const struct pldm_bios_table_attr_entry_integer_info *info) |
| 400 | { |
| 401 | size_t length = pldm_bios_table_attr_entry_integer_encode_length(); |
| 402 | assert(length <= entry_length); |
| 403 | uint8_t attr_type = |
| 404 | info->read_only ? PLDM_BIOS_INTEGER_READ_ONLY : PLDM_BIOS_INTEGER; |
| 405 | attr_table_entry_encode_header(entry, entry_length, attr_type, |
| 406 | info->name_handle); |
| 407 | struct pldm_bios_attr_table_entry *attr_entry = entry; |
| 408 | struct attr_table_integer_entry_fields *attr_fields = |
| 409 | (struct attr_table_integer_entry_fields *)attr_entry->metadata; |
| 410 | attr_fields->lower_bound = htole64(info->lower_bound); |
| 411 | attr_fields->upper_bound = htole64(info->upper_bound); |
| 412 | attr_fields->scalar_increment = htole32(info->scalar_increment); |
| 413 | attr_fields->default_value = htole64(info->default_value); |
| 414 | } |
| 415 | |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 416 | int pldm_bios_table_attr_entry_integer_info_check( |
| 417 | const struct pldm_bios_table_attr_entry_integer_info *info, |
| 418 | const char **errmsg) |
| 419 | { |
| 420 | if (info->lower_bound == info->upper_bound) { |
| 421 | if (info->default_value != info->lower_bound) { |
| 422 | set_errmsg(errmsg, "Wrong DefaultValue"); |
| 423 | return PLDM_ERROR_INVALID_DATA; |
| 424 | } |
| 425 | if (info->scalar_increment != 0) { |
| 426 | set_errmsg(errmsg, "Wrong ScalarIncrement"); |
| 427 | return PLDM_ERROR_INVALID_DATA; |
| 428 | } |
| 429 | return PLDM_SUCCESS; |
| 430 | } |
| 431 | if (info->lower_bound > info->upper_bound) { |
| 432 | set_errmsg(errmsg, |
| 433 | "LowerBound should not be greater than UpperBound"); |
| 434 | return PLDM_ERROR_INVALID_DATA; |
| 435 | } |
| 436 | if (info->default_value > info->upper_bound || |
| 437 | info->default_value < info->lower_bound) { |
| 438 | set_errmsg(errmsg, "Wrong DefaultValue"); |
| 439 | return PLDM_ERROR_INVALID_DATA; |
| 440 | } |
| 441 | if (info->scalar_increment == 0) { |
| 442 | set_errmsg(errmsg, "ScalarIncrement should not be zero when " |
| 443 | "lower_bound != upper_bound"); |
| 444 | return PLDM_ERROR_INVALID_DATA; |
| 445 | } |
| 446 | if ((info->default_value - info->lower_bound) % |
| 447 | info->scalar_increment != |
| 448 | 0) { |
| 449 | set_errmsg(errmsg, "Wrong DefaultValue or ScalarIncrement"); |
| 450 | return PLDM_ERROR_INVALID_DATA; |
| 451 | } |
| 452 | return PLDM_SUCCESS; |
| 453 | } |
| 454 | |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 455 | int pldm_bios_table_attr_entry_integer_encode_check( |
| 456 | void *entry, size_t entry_length, |
| 457 | const struct pldm_bios_table_attr_entry_integer_info *info) |
| 458 | { |
| 459 | POINTER_CHECK(entry); |
| 460 | POINTER_CHECK(info); |
| 461 | size_t length = pldm_bios_table_attr_entry_integer_encode_length(); |
| 462 | BUFFER_SIZE_EXPECT(entry_length, length); |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 463 | if (pldm_bios_table_attr_entry_integer_info_check(info, NULL) != |
| 464 | PLDM_SUCCESS) |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 465 | return PLDM_ERROR_INVALID_DATA; |
| 466 | pldm_bios_table_attr_entry_integer_encode(entry, entry_length, info); |
| 467 | return PLDM_SUCCESS; |
| 468 | } |
| 469 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 470 | static size_t attr_table_entry_length_integer(const void *entry) |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 471 | { |
| 472 | (void)entry; |
| 473 | return pldm_bios_table_attr_entry_integer_encode_length(); |
| 474 | } |
| 475 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 476 | struct table_entry_length { |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 477 | uint8_t attr_type; |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 478 | size_t (*entry_length_handler)(const void *); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 479 | }; |
| 480 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 481 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 482 | |
John Wang | b6333cb | 2019-12-10 09:43:42 +0800 | [diff] [blame] | 483 | static const struct table_entry_length *find_table_entry_length_by_type( |
| 484 | uint8_t attr_type, const struct table_entry_length *handlers, size_t count) |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 485 | { |
| 486 | size_t i; |
| 487 | for (i = 0; i < count; i++) { |
| 488 | if (attr_type == handlers[i].attr_type) |
| 489 | return &handlers[i]; |
| 490 | } |
| 491 | return NULL; |
| 492 | } |
| 493 | |
John Wang | b6333cb | 2019-12-10 09:43:42 +0800 | [diff] [blame] | 494 | static const struct table_entry_length attr_table_entries[] = { |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 495 | {.attr_type = PLDM_BIOS_ENUMERATION, |
| 496 | .entry_length_handler = attr_table_entry_length_enum}, |
| 497 | {.attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY, |
| 498 | .entry_length_handler = attr_table_entry_length_enum}, |
| 499 | {.attr_type = PLDM_BIOS_STRING, |
| 500 | .entry_length_handler = attr_table_entry_length_string}, |
| 501 | {.attr_type = PLDM_BIOS_STRING_READ_ONLY, |
| 502 | .entry_length_handler = attr_table_entry_length_string}, |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 503 | {.attr_type = PLDM_BIOS_INTEGER, |
| 504 | .entry_length_handler = attr_table_entry_length_integer}, |
| 505 | {.attr_type = PLDM_BIOS_INTEGER_READ_ONLY, |
| 506 | .entry_length_handler = attr_table_entry_length_integer}, |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 507 | }; |
| 508 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 509 | static size_t attr_table_entry_length(const void *table_entry) |
| 510 | { |
| 511 | const struct pldm_bios_attr_table_entry *entry = table_entry; |
John Wang | b6333cb | 2019-12-10 09:43:42 +0800 | [diff] [blame] | 512 | const struct table_entry_length *attr_table_entry = |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 513 | find_table_entry_length_by_type(entry->attr_type, |
| 514 | attr_table_entries, |
| 515 | ARRAY_SIZE(attr_table_entries)); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 516 | assert(attr_table_entry != NULL); |
| 517 | assert(attr_table_entry->entry_length_handler != NULL); |
| 518 | |
| 519 | return attr_table_entry->entry_length_handler(entry); |
| 520 | } |
| 521 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 522 | size_t pldm_bios_table_attr_value_entry_encode_enum_length(uint8_t count) |
| 523 | { |
| 524 | return sizeof(struct pldm_bios_attr_val_table_entry) - 1 + |
| 525 | sizeof(count) + count; |
| 526 | } |
| 527 | |
| 528 | void pldm_bios_table_attr_value_entry_encode_enum( |
| 529 | void *entry, size_t entry_length, uint16_t attr_handle, uint8_t attr_type, |
| 530 | uint8_t count, uint8_t *handles) |
| 531 | { |
| 532 | size_t length = |
| 533 | pldm_bios_table_attr_value_entry_encode_enum_length(count); |
| 534 | assert(length <= entry_length); |
| 535 | |
| 536 | struct pldm_bios_attr_val_table_entry *table_entry = entry; |
| 537 | table_entry->attr_handle = htole16(attr_handle); |
| 538 | table_entry->attr_type = attr_type; |
| 539 | table_entry->value[0] = count; |
| 540 | if (count != 0) |
| 541 | memcpy(&table_entry->value[1], handles, count); |
| 542 | } |
| 543 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 544 | uint8_t pldm_bios_table_attr_value_entry_enum_decode_number( |
| 545 | const struct pldm_bios_attr_val_table_entry *entry) |
| 546 | { |
| 547 | return entry->value[0]; |
| 548 | } |
| 549 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 550 | int pldm_bios_table_attr_value_entry_encode_enum_check( |
| 551 | void *entry, size_t entry_length, uint16_t attr_handle, uint8_t attr_type, |
| 552 | uint8_t count, uint8_t *handles) |
| 553 | { |
| 554 | POINTER_CHECK(entry); |
| 555 | if (count != 0 && handles == NULL) |
| 556 | return PLDM_ERROR_INVALID_DATA; |
| 557 | ATTR_TYPE_EXPECT(attr_type, PLDM_BIOS_ENUMERATION); |
| 558 | size_t length = |
| 559 | pldm_bios_table_attr_value_entry_encode_enum_length(count); |
| 560 | BUFFER_SIZE_EXPECT(entry_length, length); |
| 561 | pldm_bios_table_attr_value_entry_encode_enum( |
| 562 | entry, entry_length, attr_handle, attr_type, count, handles); |
| 563 | return PLDM_SUCCESS; |
| 564 | } |
| 565 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 566 | static size_t attr_value_table_entry_length_enum(const void *entry) |
| 567 | { |
| 568 | uint8_t number = |
| 569 | pldm_bios_table_attr_value_entry_enum_decode_number(entry); |
| 570 | return pldm_bios_table_attr_value_entry_encode_enum_length(number); |
| 571 | } |
| 572 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 573 | size_t |
| 574 | pldm_bios_table_attr_value_entry_encode_string_length(uint16_t string_length) |
| 575 | { |
| 576 | return sizeof(struct pldm_bios_attr_val_table_entry) - 1 + |
| 577 | sizeof(string_length) + string_length; |
| 578 | } |
| 579 | |
| 580 | void pldm_bios_table_attr_value_entry_encode_string( |
| 581 | void *entry, size_t entry_length, uint16_t attr_handle, uint8_t attr_type, |
| 582 | uint16_t str_length, const char *str) |
| 583 | { |
| 584 | size_t length = |
| 585 | pldm_bios_table_attr_value_entry_encode_string_length(str_length); |
| 586 | assert(length <= entry_length); |
| 587 | |
| 588 | struct pldm_bios_attr_val_table_entry *table_entry = entry; |
| 589 | table_entry->attr_handle = htole16(attr_handle); |
| 590 | table_entry->attr_type = attr_type; |
| 591 | if (str_length != 0) |
| 592 | memcpy(table_entry->value + sizeof(str_length), str, |
| 593 | str_length); |
| 594 | str_length = htole16(str_length); |
| 595 | memcpy(table_entry->value, &str_length, sizeof(str_length)); |
| 596 | } |
| 597 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 598 | uint16_t pldm_bios_table_attr_value_entry_string_decode_length( |
| 599 | const struct pldm_bios_attr_val_table_entry *entry) |
| 600 | { |
| 601 | uint16_t str_length = 0; |
| 602 | memcpy(&str_length, entry->value, sizeof(str_length)); |
| 603 | return le16toh(str_length); |
| 604 | } |
| 605 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 606 | int pldm_bios_table_attr_value_entry_encode_string_check( |
| 607 | void *entry, size_t entry_length, uint16_t attr_handle, uint8_t attr_type, |
| 608 | uint16_t str_length, const char *str) |
| 609 | { |
| 610 | POINTER_CHECK(entry); |
| 611 | if (str_length != 0 && str == NULL) |
| 612 | return PLDM_ERROR_INVALID_DATA; |
| 613 | ATTR_TYPE_EXPECT(attr_type, PLDM_BIOS_STRING); |
| 614 | size_t length = |
| 615 | pldm_bios_table_attr_value_entry_encode_string_length(str_length); |
| 616 | BUFFER_SIZE_EXPECT(entry_length, length); |
| 617 | pldm_bios_table_attr_value_entry_encode_string( |
| 618 | entry, entry_length, attr_handle, attr_type, str_length, str); |
| 619 | return PLDM_SUCCESS; |
| 620 | } |
| 621 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 622 | static size_t attr_value_table_entry_length_string(const void *entry) |
| 623 | { |
| 624 | uint16_t str_length = |
| 625 | pldm_bios_table_attr_value_entry_string_decode_length(entry); |
| 626 | return pldm_bios_table_attr_value_entry_encode_string_length( |
| 627 | str_length); |
| 628 | } |
| 629 | |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 630 | size_t pldm_bios_table_attr_value_entry_encode_integer_length() |
| 631 | { |
| 632 | return sizeof(struct pldm_bios_attr_val_table_entry) - 1 + |
| 633 | sizeof(uint64_t); |
| 634 | } |
| 635 | void pldm_bios_table_attr_value_entry_encode_integer(void *entry, |
| 636 | size_t entry_length, |
| 637 | uint16_t attr_handle, |
| 638 | uint8_t attr_type, |
| 639 | uint64_t cv) |
| 640 | { |
| 641 | size_t length = |
| 642 | pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 643 | assert(length <= entry_length); |
| 644 | |
| 645 | struct pldm_bios_attr_val_table_entry *table_entry = entry; |
| 646 | table_entry->attr_handle = htole16(attr_handle); |
| 647 | table_entry->attr_type = attr_type; |
| 648 | cv = htole64(cv); |
| 649 | memcpy(table_entry->value, &cv, sizeof(uint64_t)); |
| 650 | } |
| 651 | |
| 652 | int pldm_bios_table_attr_value_entry_encode_integer_check(void *entry, |
| 653 | size_t entry_length, |
| 654 | uint16_t attr_handle, |
| 655 | uint8_t attr_type, |
| 656 | uint64_t cv) |
| 657 | { |
| 658 | POINTER_CHECK(entry); |
| 659 | size_t length = |
| 660 | pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 661 | ATTR_TYPE_EXPECT(attr_type, PLDM_BIOS_INTEGER); |
| 662 | BUFFER_SIZE_EXPECT(entry_length, length); |
| 663 | pldm_bios_table_attr_value_entry_encode_integer( |
| 664 | entry, entry_length, attr_handle, attr_type, cv); |
| 665 | return PLDM_SUCCESS; |
| 666 | } |
| 667 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 668 | static size_t attr_value_table_entry_length_integer(const void *entry) |
| 669 | { |
| 670 | (void)entry; |
| 671 | return pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 672 | } |
| 673 | |
John Wang | b6333cb | 2019-12-10 09:43:42 +0800 | [diff] [blame] | 674 | static const struct table_entry_length attr_value_table_entries[] = { |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 675 | {.attr_type = PLDM_BIOS_ENUMERATION, |
| 676 | .entry_length_handler = attr_value_table_entry_length_enum}, |
| 677 | {.attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY, |
| 678 | .entry_length_handler = attr_value_table_entry_length_enum}, |
| 679 | {.attr_type = PLDM_BIOS_STRING, |
| 680 | .entry_length_handler = attr_value_table_entry_length_string}, |
| 681 | {.attr_type = PLDM_BIOS_STRING_READ_ONLY, |
| 682 | .entry_length_handler = attr_value_table_entry_length_string}, |
| 683 | {.attr_type = PLDM_BIOS_INTEGER, |
| 684 | .entry_length_handler = attr_value_table_entry_length_integer}, |
| 685 | {.attr_type = PLDM_BIOS_INTEGER_READ_ONLY, |
| 686 | .entry_length_handler = attr_value_table_entry_length_integer}, |
| 687 | }; |
| 688 | |
| 689 | static size_t attr_value_table_entry_length(const void *table_entry) |
| 690 | { |
| 691 | const struct pldm_bios_attr_val_table_entry *entry = table_entry; |
John Wang | b6333cb | 2019-12-10 09:43:42 +0800 | [diff] [blame] | 692 | const struct table_entry_length *entry_length = |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 693 | find_table_entry_length_by_type( |
| 694 | entry->attr_type, attr_value_table_entries, |
| 695 | ARRAY_SIZE(attr_value_table_entries)); |
| 696 | assert(entry_length != NULL); |
| 697 | assert(entry_length->entry_length_handler != NULL); |
| 698 | |
| 699 | return entry_length->entry_length_handler(entry); |
| 700 | } |
| 701 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 702 | uint16_t pldm_bios_table_attr_value_entry_decode_handle( |
| 703 | const struct pldm_bios_attr_val_table_entry *entry) |
| 704 | { |
| 705 | return le16toh(entry->attr_handle); |
| 706 | } |
| 707 | |
| 708 | size_t pldm_bios_table_attr_value_entry_value_length( |
| 709 | const struct pldm_bios_attr_val_table_entry *entry) |
| 710 | { |
| 711 | size_t entry_length = attr_value_table_entry_length(entry); |
| 712 | size_t header_length = |
| 713 | MEMBER_SIZE(pldm_bios_attr_val_table_entry, attr_handle) + |
| 714 | MEMBER_SIZE(pldm_bios_attr_val_table_entry, attr_type); |
| 715 | assert(entry_length > header_length); |
| 716 | |
| 717 | return (entry_length - header_length); |
| 718 | } |
| 719 | |
| 720 | const uint8_t *pldm_bios_table_attr_value_entry_value( |
| 721 | const struct pldm_bios_attr_val_table_entry *entry) |
| 722 | { |
| 723 | return entry->value; |
| 724 | } |
| 725 | |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 726 | static size_t pad_size_get(size_t size_without_pad) |
| 727 | { |
| 728 | return ((size_without_pad % 4) ? (4 - size_without_pad % 4) : 0); |
| 729 | } |
| 730 | |
| 731 | static uint8_t *pad_append(uint8_t *table_end, size_t pad_size) |
| 732 | { |
| 733 | while (pad_size--) |
| 734 | *table_end++ = 0; |
| 735 | |
| 736 | return table_end; |
| 737 | } |
| 738 | |
| 739 | static uint8_t *checksum_append(uint8_t *table_end, uint32_t checksum) |
| 740 | { |
| 741 | checksum = htole32(checksum); |
| 742 | memcpy(table_end, &checksum, sizeof(checksum)); |
| 743 | |
| 744 | return table_end + sizeof(checksum); |
| 745 | } |
| 746 | |
| 747 | size_t pldm_bios_table_pad_checksum_size(size_t size_without_pad) |
| 748 | { |
| 749 | size_t size = pad_size_get(size_without_pad) + |
| 750 | sizeof(uint32_t) /*sizeof(checksum)*/; |
| 751 | return size; |
| 752 | } |
| 753 | |
| 754 | void pldm_bios_table_append_pad_checksum(void *table, size_t size, |
| 755 | size_t size_without_pad) |
| 756 | { |
| 757 | |
| 758 | size_t pad_checksum_size = |
| 759 | pldm_bios_table_pad_checksum_size(size_without_pad); |
| 760 | assert(size >= (size_without_pad + pad_checksum_size)); |
| 761 | |
| 762 | uint8_t *table_end = (uint8_t *)table + size_without_pad; |
| 763 | size_t pad_size = pad_size_get(size_without_pad); |
| 764 | table_end = pad_append(table_end, pad_size); |
| 765 | |
| 766 | uint32_t checksum = crc32(table, size_without_pad + pad_size); |
| 767 | checksum_append(table_end, checksum); |
| 768 | } |
| 769 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 770 | struct pldm_bios_table_iter { |
| 771 | const uint8_t *table_data; |
| 772 | size_t table_len; |
| 773 | size_t current_pos; |
| 774 | size_t (*entry_length_handler)(const void *table_entry); |
| 775 | }; |
| 776 | |
| 777 | struct pldm_bios_table_iter * |
| 778 | pldm_bios_table_iter_create(const void *table, size_t length, |
| 779 | enum pldm_bios_table_types type) |
| 780 | { |
| 781 | struct pldm_bios_table_iter *iter = malloc(sizeof(*iter)); |
| 782 | assert(iter != NULL); |
| 783 | iter->table_data = table; |
| 784 | iter->table_len = length; |
| 785 | iter->current_pos = 0; |
| 786 | iter->entry_length_handler = NULL; |
| 787 | switch (type) { |
| 788 | case PLDM_BIOS_STRING_TABLE: |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 789 | iter->entry_length_handler = string_table_entry_length; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 790 | break; |
| 791 | case PLDM_BIOS_ATTR_TABLE: |
| 792 | iter->entry_length_handler = attr_table_entry_length; |
| 793 | break; |
| 794 | case PLDM_BIOS_ATTR_VAL_TABLE: |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 795 | iter->entry_length_handler = attr_value_table_entry_length; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
| 799 | return iter; |
| 800 | } |
| 801 | |
| 802 | void pldm_bios_table_iter_free(struct pldm_bios_table_iter *iter) |
| 803 | { |
| 804 | free(iter); |
| 805 | } |
| 806 | |
| 807 | #define pad_and_check_max 7 |
| 808 | bool pldm_bios_table_iter_is_end(const struct pldm_bios_table_iter *iter) |
| 809 | { |
| 810 | if (iter->table_len - iter->current_pos <= pad_and_check_max) |
| 811 | return true; |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | void pldm_bios_table_iter_next(struct pldm_bios_table_iter *iter) |
| 816 | { |
| 817 | if (pldm_bios_table_iter_is_end(iter)) |
| 818 | return; |
| 819 | const void *entry = iter->table_data + iter->current_pos; |
| 820 | iter->current_pos += iter->entry_length_handler(entry); |
| 821 | } |
| 822 | |
| 823 | const void *pldm_bios_table_iter_value(struct pldm_bios_table_iter *iter) |
| 824 | { |
| 825 | return iter->table_data + iter->current_pos; |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 826 | } |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 827 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 828 | typedef bool (*equal_handler)(const void *entry, const void *key); |
| 829 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 830 | static const void * |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 831 | pldm_bios_table_entry_find_by_iter(struct pldm_bios_table_iter *iter, |
| 832 | const void *key, equal_handler equal) |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 833 | { |
| 834 | const void *entry; |
| 835 | while (!pldm_bios_table_iter_is_end(iter)) { |
| 836 | entry = pldm_bios_table_iter_value(iter); |
| 837 | if (equal(entry, key)) |
| 838 | return entry; |
| 839 | pldm_bios_table_iter_next(iter); |
| 840 | } |
| 841 | return NULL; |
| 842 | } |
| 843 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 844 | static const void * |
| 845 | pldm_bios_table_entry_find_from_table(const void *table, size_t length, |
| 846 | enum pldm_bios_table_types type, |
| 847 | equal_handler equal, const void *key) |
| 848 | { |
| 849 | struct pldm_bios_table_iter *iter = |
| 850 | pldm_bios_table_iter_create(table, length, type); |
| 851 | const void *entry = |
| 852 | pldm_bios_table_entry_find_by_iter(iter, key, equal); |
| 853 | pldm_bios_table_iter_free(iter); |
| 854 | return entry; |
| 855 | } |
| 856 | |
| 857 | static bool string_table_handle_equal(const void *entry, const void *key) |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 858 | { |
| 859 | const struct pldm_bios_string_table_entry *string_entry = entry; |
| 860 | uint16_t handle = *(uint16_t *)key; |
| 861 | if (pldm_bios_table_string_entry_decode_handle(string_entry) == handle) |
| 862 | return true; |
| 863 | return false; |
| 864 | } |
| 865 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 866 | const struct pldm_bios_string_table_entry * |
| 867 | pldm_bios_table_string_find_by_handle(const void *table, size_t length, |
| 868 | uint16_t handle) |
| 869 | { |
| 870 | return pldm_bios_table_entry_find_from_table( |
| 871 | table, length, PLDM_BIOS_STRING_TABLE, string_table_handle_equal, |
| 872 | &handle); |
| 873 | } |
| 874 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 875 | struct string_equal_arg { |
| 876 | uint16_t str_length; |
| 877 | const char *str; |
| 878 | }; |
| 879 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 880 | static bool string_table_string_equal(const void *entry, const void *key) |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 881 | { |
| 882 | const struct pldm_bios_string_table_entry *string_entry = entry; |
| 883 | const struct string_equal_arg *arg = key; |
| 884 | if (arg->str_length != |
| 885 | pldm_bios_table_string_entry_decode_string_length(string_entry)) |
| 886 | return false; |
| 887 | if (memcmp(string_entry->name, arg->str, arg->str_length) != 0) |
| 888 | return false; |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | const struct pldm_bios_string_table_entry * |
| 893 | pldm_bios_table_string_find_by_string(const void *table, size_t length, |
| 894 | const char *str) |
| 895 | { |
| 896 | uint16_t str_length = strlen(str); |
| 897 | struct string_equal_arg arg = {str_length, str}; |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 898 | return pldm_bios_table_entry_find_from_table( |
| 899 | table, length, PLDM_BIOS_STRING_TABLE, string_table_string_equal, |
| 900 | &arg); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 901 | } |
| 902 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 903 | static bool attr_value_table_handle_equal(const void *entry, const void *key) |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 904 | { |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 905 | uint16_t handle = *(uint16_t *)key; |
| 906 | return pldm_bios_table_attr_value_entry_decode_handle(entry) == handle; |
| 907 | } |
| 908 | |
| 909 | const struct pldm_bios_attr_val_table_entry * |
| 910 | pldm_bios_table_attr_value_find_by_handle(const void *table, size_t length, |
| 911 | uint16_t handle) |
| 912 | { |
| 913 | return pldm_bios_table_entry_find_from_table( |
| 914 | table, length, PLDM_BIOS_ATTR_VAL_TABLE, |
| 915 | attr_value_table_handle_equal, &handle); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 916 | } |