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