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