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