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