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