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