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