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 | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 2 | #include <libpldm/pdr.h> |
| 3 | #include <libpldm/platform.h> |
| 4 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | #include <assert.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 6 | #include <endian.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 9 | #include <errno.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 10 | |
| 11 | typedef struct pldm_pdr_record { |
| 12 | uint32_t record_handle; |
| 13 | uint32_t size; |
| 14 | uint8_t *data; |
| 15 | struct pldm_pdr_record *next; |
| 16 | bool is_remote; |
| 17 | uint16_t terminus_handle; |
| 18 | } pldm_pdr_record; |
| 19 | |
| 20 | typedef struct pldm_pdr { |
| 21 | uint32_t record_count; |
| 22 | uint32_t size; |
| 23 | pldm_pdr_record *first; |
| 24 | pldm_pdr_record *last; |
| 25 | } pldm_pdr; |
| 26 | |
| 27 | static inline uint32_t get_next_record_handle(const pldm_pdr *repo, |
| 28 | const pldm_pdr_record *record) |
| 29 | { |
| 30 | assert(repo != NULL); |
| 31 | assert(record != NULL); |
| 32 | |
| 33 | if (record == repo->last) { |
| 34 | return 0; |
| 35 | } |
| 36 | return record->next->record_handle; |
| 37 | } |
| 38 | |
Andrew Jeffery | ca248ce | 2023-07-07 10:38:30 +0930 | [diff] [blame] | 39 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 40 | int pldm_pdr_add_check(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
| 41 | bool is_remote, uint16_t terminus_handle, |
| 42 | uint32_t *record_handle) |
| 43 | { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 44 | uint32_t curr; |
| 45 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 46 | if (!repo || !data || !size) { |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 47 | return -EINVAL; |
| 48 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 49 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 50 | if (record_handle && *record_handle) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 51 | curr = *record_handle; |
| 52 | } else if (repo->last) { |
| 53 | curr = repo->last->record_handle; |
| 54 | if (curr == UINT32_MAX) { |
| 55 | return -EOVERFLOW; |
| 56 | } |
| 57 | curr += 1; |
| 58 | } else { |
| 59 | curr = 1; |
| 60 | } |
| 61 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 62 | pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record)); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 63 | if (!record) { |
| 64 | return -ENOMEM; |
| 65 | } |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 66 | |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 67 | if (data) { |
| 68 | record->data = malloc(size); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 69 | if (!record->data) { |
| 70 | free(record); |
| 71 | return -ENOMEM; |
| 72 | } |
| 73 | memcpy(record->data, data, size); |
Andrew Jeffery | 2ca7e64 | 2023-06-28 22:29:07 +0930 | [diff] [blame] | 74 | } |
| 75 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 76 | record->size = size; |
| 77 | record->is_remote = is_remote; |
| 78 | record->terminus_handle = terminus_handle; |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 79 | record->record_handle = curr; |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 80 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 81 | if (record_handle && !*record_handle && data) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 82 | /* If record handle is 0, that is an indication for this API to |
| 83 | * compute a new handle. For that reason, the computed handle |
| 84 | * needs to be populated in the PDR header. For a case where the |
| 85 | * caller supplied the record handle, it would exist in the |
| 86 | * header already. |
| 87 | */ |
| 88 | struct pldm_pdr_hdr *hdr = (void *)record->data; |
| 89 | hdr->record_handle = htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 90 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 91 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 92 | record->next = NULL; |
| 93 | |
Andrew Jeffery | 8d231da | 2023-07-04 11:28:46 +0930 | [diff] [blame] | 94 | assert(!repo->first == !repo->last); |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 95 | if (repo->first == NULL) { |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 96 | repo->first = record; |
| 97 | repo->last = record; |
| 98 | } else { |
| 99 | repo->last->next = record; |
| 100 | repo->last = record; |
| 101 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 102 | |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 103 | repo->size += record->size; |
| 104 | ++repo->record_count; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 105 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 106 | if (record_handle) { |
| 107 | *record_handle = record->record_handle; |
| 108 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 109 | |
| 110 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 111 | } |
| 112 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 113 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 114 | pldm_pdr *pldm_pdr_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 115 | { |
| 116 | pldm_pdr *repo = malloc(sizeof(pldm_pdr)); |
Andrew Jeffery | a8bb22e | 2023-06-30 12:01:12 +0930 | [diff] [blame] | 117 | if (!repo) { |
| 118 | return NULL; |
| 119 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 120 | repo->record_count = 0; |
| 121 | repo->size = 0; |
| 122 | repo->first = NULL; |
| 123 | repo->last = NULL; |
| 124 | |
| 125 | return repo; |
| 126 | } |
| 127 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 128 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 129 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 130 | { |
Andrew Jeffery | fca1b60 | 2023-06-30 12:29:25 +0930 | [diff] [blame] | 131 | if (!repo) { |
| 132 | return; |
| 133 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 134 | |
| 135 | pldm_pdr_record *record = repo->first; |
| 136 | while (record != NULL) { |
| 137 | pldm_pdr_record *next = record->next; |
| 138 | if (record->data) { |
| 139 | free(record->data); |
| 140 | record->data = NULL; |
| 141 | } |
| 142 | free(record); |
| 143 | record = next; |
| 144 | } |
| 145 | free(repo); |
| 146 | } |
| 147 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 148 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 149 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 150 | uint32_t record_handle, |
| 151 | uint8_t **data, uint32_t *size, |
| 152 | uint32_t *next_record_handle) |
| 153 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 154 | if (!repo || !data || !size || !next_record_handle) { |
| 155 | return NULL; |
| 156 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 157 | |
| 158 | if (!record_handle && (repo->first != NULL)) { |
| 159 | record_handle = repo->first->record_handle; |
| 160 | } |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 161 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 162 | pldm_pdr_record *record = repo->first; |
| 163 | while (record != NULL) { |
| 164 | if (record->record_handle == record_handle) { |
| 165 | *size = record->size; |
| 166 | *data = record->data; |
| 167 | *next_record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 168 | get_next_record_handle(repo, record); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 169 | return record; |
| 170 | } |
| 171 | record = record->next; |
| 172 | } |
| 173 | |
| 174 | *size = 0; |
| 175 | *next_record_handle = 0; |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 179 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 180 | const pldm_pdr_record * |
| 181 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 182 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 183 | uint32_t *size, uint32_t *next_record_handle) |
| 184 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 185 | if (!repo || !curr_record || !data || !size || !next_record_handle) { |
| 186 | return NULL; |
| 187 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 188 | |
| 189 | if (curr_record == repo->last) { |
| 190 | *data = NULL; |
| 191 | *size = 0; |
| 192 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 197 | *data = curr_record->next->data; |
| 198 | *size = curr_record->next->size; |
| 199 | return curr_record->next; |
| 200 | } |
| 201 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 202 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 203 | const pldm_pdr_record * |
| 204 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 205 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 206 | uint32_t *size) |
| 207 | { |
Andrew Jeffery | f85eeba | 2023-06-30 12:38:40 +0930 | [diff] [blame] | 208 | if (!repo) { |
| 209 | return NULL; |
| 210 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 211 | |
| 212 | pldm_pdr_record *record = repo->first; |
| 213 | if (curr_record != NULL) { |
| 214 | record = curr_record->next; |
| 215 | } |
| 216 | while (record != NULL) { |
| 217 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 218 | if (hdr->type == pdr_type) { |
| 219 | if (data && size) { |
| 220 | *size = record->size; |
| 221 | *data = record->data; |
| 222 | } |
| 223 | return record; |
| 224 | } |
| 225 | record = record->next; |
| 226 | } |
| 227 | |
| 228 | if (size) { |
| 229 | *size = 0; |
| 230 | } |
| 231 | return NULL; |
| 232 | } |
| 233 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 234 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 235 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 236 | { |
| 237 | assert(repo != NULL); |
| 238 | |
| 239 | return repo->record_count; |
| 240 | } |
| 241 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 242 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 243 | uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo) |
| 244 | { |
| 245 | assert(repo != NULL); |
| 246 | |
| 247 | return repo->size; |
| 248 | } |
| 249 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 250 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 5565fcd | 2023-06-30 13:21:32 +0930 | [diff] [blame] | 251 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo |
| 252 | __attribute__((unused)), |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 253 | const pldm_pdr_record *record) |
| 254 | { |
| 255 | assert(repo != NULL); |
| 256 | assert(record != NULL); |
| 257 | |
| 258 | return record->record_handle; |
| 259 | } |
| 260 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 261 | LIBPLDM_ABI_STABLE |
| 262 | bool pldm_pdr_record_is_remote(const pldm_pdr_record *record) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 263 | { |
| 264 | assert(record != NULL); |
| 265 | |
| 266 | return record->is_remote; |
| 267 | } |
| 268 | |
Andrew Jeffery | a2c6911 | 2023-07-07 10:41:38 +0930 | [diff] [blame] | 269 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 270 | int pldm_pdr_add_fru_record_set_check(pldm_pdr *repo, uint16_t terminus_handle, |
| 271 | uint16_t fru_rsi, uint16_t entity_type, |
| 272 | uint16_t entity_instance_num, |
| 273 | uint16_t container_id, |
| 274 | uint32_t *bmc_record_handle) |
| 275 | { |
Andrew Jeffery | 73d6779 | 2023-07-17 15:22:18 +0930 | [diff] [blame] | 276 | if (!repo || !bmc_record_handle) { |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 280 | uint32_t size = sizeof(struct pldm_pdr_hdr) + |
| 281 | sizeof(struct pldm_pdr_fru_record_set); |
| 282 | uint8_t data[size]; |
| 283 | |
| 284 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data; |
| 285 | hdr->version = 1; |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 286 | hdr->record_handle = *bmc_record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 287 | hdr->type = PLDM_PDR_FRU_RECORD_SET; |
| 288 | hdr->record_change_num = 0; |
| 289 | hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set)); |
| 290 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 291 | (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr + |
| 292 | sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 293 | fru->terminus_handle = htole16(terminus_handle); |
| 294 | fru->fru_rsi = htole16(fru_rsi); |
| 295 | fru->entity_type = htole16(entity_type); |
| 296 | fru->entity_instance_num = htole16(entity_instance_num); |
| 297 | fru->container_id = htole16(container_id); |
| 298 | |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 299 | return pldm_pdr_add_check(repo, data, size, false, terminus_handle, |
| 300 | bmc_record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 301 | } |
| 302 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 303 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 304 | const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 305 | const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle, |
| 306 | uint16_t *entity_type, uint16_t *entity_instance_num, |
| 307 | uint16_t *container_id) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 308 | { |
Andrew Jeffery | 01425e9 | 2023-06-30 13:47:40 +0930 | [diff] [blame] | 309 | if (!repo || !terminus_handle || !entity_type || !entity_instance_num || |
| 310 | !container_id) { |
| 311 | return NULL; |
| 312 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 313 | |
| 314 | uint8_t *data = NULL; |
| 315 | uint32_t size = 0; |
| 316 | const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 317 | repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 318 | while (curr_record != NULL) { |
| 319 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 320 | (struct pldm_pdr_fru_record_set |
| 321 | *)(data + sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 322 | if (fru->fru_rsi == htole16(fru_rsi)) { |
| 323 | *terminus_handle = le16toh(fru->terminus_handle); |
| 324 | *entity_type = le16toh(fru->entity_type); |
| 325 | *entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 326 | le16toh(fru->entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 327 | *container_id = le16toh(fru->container_id); |
| 328 | return curr_record; |
| 329 | } |
| 330 | data = NULL; |
| 331 | curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 332 | repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, |
| 333 | &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | *terminus_handle = 0; |
| 337 | *entity_type = 0; |
| 338 | *entity_instance_num = 0; |
| 339 | *container_id = 0; |
| 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 344 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 345 | /* NOLINTNEXTLINE(readability-identifier-naming) */ |
| 346 | void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle, |
| 347 | uint8_t tid, uint8_t tl_eid, bool valid_bit) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 348 | { |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 349 | uint8_t *out_data = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 350 | uint32_t size = 0; |
| 351 | const pldm_pdr_record *record; |
| 352 | record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR, |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 353 | NULL, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 354 | |
| 355 | do { |
| 356 | if (record != NULL) { |
| 357 | struct pldm_terminus_locator_pdr *pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 358 | (struct pldm_terminus_locator_pdr *)out_data; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 359 | struct pldm_terminus_locator_type_mctp_eid *value = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 360 | (struct pldm_terminus_locator_type_mctp_eid *) |
| 361 | pdr->terminus_locator_value; |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 362 | if (pdr->terminus_handle == terminus_handle && |
| 363 | pdr->tid == tid && value->eid == tl_eid) { |
| 364 | pdr->validity = valid_bit; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 368 | record = pldm_pdr_find_record_by_type(repo, |
| 369 | PLDM_TERMINUS_LOCATOR_PDR, |
| 370 | record, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 371 | } while (record); |
| 372 | } |
| 373 | |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 374 | static bool pldm_record_handle_in_range(uint32_t record_handle, |
| 375 | uint32_t first_record_handle, |
| 376 | uint32_t last_record_handle) |
| 377 | { |
| 378 | return record_handle >= first_record_handle && |
| 379 | record_handle <= last_record_handle; |
| 380 | } |
| 381 | |
| 382 | LIBPLDM_ABI_TESTING |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 383 | int pldm_pdr_find_child_container_id_index_range_exclude( |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 384 | const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance, |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 385 | uint8_t child_index, uint32_t range_exclude_start_handle, |
| 386 | uint32_t range_exclude_end_handle, uint16_t *container_id) |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 387 | { |
| 388 | pldm_pdr_record *record; |
| 389 | if (!repo) { |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | for (record = repo->first; record; record = record->next) { |
| 394 | bool is_container_entity_instance_number; |
| 395 | struct pldm_pdr_entity_association *pdr; |
| 396 | bool is_container_entity_type; |
| 397 | struct pldm_entity *child; |
| 398 | struct pldm_pdr_hdr *hdr; |
| 399 | bool in_range; |
| 400 | |
| 401 | // pldm_pdr_add() takes only uint8_t* data as an argument. |
| 402 | // The expectation here is the pldm_pdr_hdr is the first field of the record data |
| 403 | hdr = (struct pldm_pdr_hdr *)record->data; |
| 404 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 405 | continue; |
| 406 | } |
| 407 | in_range = pldm_record_handle_in_range( |
| 408 | record->record_handle, range_exclude_start_handle, |
| 409 | range_exclude_end_handle); |
| 410 | if (in_range) { |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | // this cast is valid with respect to alignment because |
| 415 | // struct pldm_pdr_hdr is declared with __attribute__((packed)) |
| 416 | pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr)); |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 417 | if (child_index >= pdr->num_children) { |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 418 | continue; |
| 419 | } |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 420 | |
| 421 | child = (&pdr->children[child_index]); |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 422 | is_container_entity_type = pdr->container.entity_type == |
| 423 | entity_type; |
| 424 | is_container_entity_instance_number = |
| 425 | pdr->container.entity_instance_num == entity_instance; |
| 426 | if (is_container_entity_type && |
| 427 | is_container_entity_instance_number) { |
| 428 | *container_id = le16toh(child->entity_container_id); |
Pavithra Barithaya | ffd5342 | 2023-06-23 23:20:48 -0500 | [diff] [blame] | 429 | return 0; |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | return -ENOKEY; |
| 433 | } |
| 434 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 435 | typedef struct pldm_entity_association_tree { |
| 436 | pldm_entity_node *root; |
| 437 | uint16_t last_used_container_id; |
| 438 | } pldm_entity_association_tree; |
| 439 | |
| 440 | typedef struct pldm_entity_node { |
| 441 | pldm_entity entity; |
| 442 | pldm_entity parent; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 443 | uint16_t remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 444 | pldm_entity_node *first_child; |
| 445 | pldm_entity_node *next_sibling; |
| 446 | uint8_t association_type; |
| 447 | } pldm_entity_node; |
| 448 | |
| 449 | static inline uint16_t next_container_id(pldm_entity_association_tree *tree) |
| 450 | { |
| 451 | assert(tree != NULL); |
| 452 | assert(tree->last_used_container_id != UINT16_MAX); |
| 453 | |
| 454 | return ++tree->last_used_container_id; |
| 455 | } |
| 456 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 457 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 458 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 459 | { |
| 460 | assert(node != NULL); |
| 461 | |
| 462 | return node->entity; |
| 463 | } |
| 464 | |
Pavithra Barithaya | def8e02 | 2023-08-16 00:31:06 -0500 | [diff] [blame] | 465 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 466 | uint16_t |
| 467 | pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity) |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 468 | { |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 469 | assert(entity != NULL); |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 470 | |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 471 | return entity->remote_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 472 | } |
| 473 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 474 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 475 | pldm_entity_association_tree *pldm_entity_association_tree_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 476 | { |
| 477 | pldm_entity_association_tree *tree = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 478 | malloc(sizeof(pldm_entity_association_tree)); |
Andrew Jeffery | c40037d | 2023-06-30 13:50:12 +0930 | [diff] [blame] | 479 | if (!tree) { |
| 480 | return NULL; |
| 481 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 482 | tree->root = NULL; |
| 483 | tree->last_used_container_id = 0; |
| 484 | |
| 485 | return tree; |
| 486 | } |
| 487 | |
| 488 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 489 | uint16_t entity_type) |
| 490 | { |
| 491 | assert(start != NULL); |
| 492 | |
| 493 | /* Insert after the the last node that matches the input entity type, or |
| 494 | * at the end if no such match occurrs |
| 495 | */ |
| 496 | while (start->next_sibling != NULL) { |
| 497 | uint16_t this_type = start->entity.entity_type; |
| 498 | pldm_entity_node *next = start->next_sibling; |
| 499 | if (this_type == entity_type && |
| 500 | (this_type != next->entity.entity_type)) { |
| 501 | break; |
| 502 | } |
| 503 | start = start->next_sibling; |
| 504 | } |
| 505 | |
| 506 | return start; |
| 507 | } |
| 508 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 509 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 510 | pldm_entity_node *pldm_entity_association_tree_add( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 511 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 512 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 513 | uint8_t association_type) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 514 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 515 | return pldm_entity_association_tree_add_entity(tree, entity, |
| 516 | entity_instance_number, |
| 517 | parent, association_type, |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 518 | false, true, 0xffff); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Pavithra Barithaya | 1ade87f | 2023-07-28 04:03:01 -0500 | [diff] [blame] | 521 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 522 | pldm_entity_node *pldm_entity_association_tree_add_entity( |
| 523 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 524 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 525 | uint8_t association_type, bool is_remote, bool is_update_container_id, |
| 526 | uint16_t container_id) |
| 527 | { |
| 528 | if ((!tree) || (!entity)) { |
| 529 | return NULL; |
| 530 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 531 | |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 532 | if (entity_instance_number != 0xffff && parent != NULL) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 533 | pldm_entity node; |
| 534 | node.entity_type = entity->entity_type; |
| 535 | node.entity_instance_num = entity_instance_number; |
| 536 | if (pldm_is_current_parent_child(parent, &node)) { |
| 537 | return NULL; |
| 538 | } |
| 539 | } |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 540 | if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL && |
| 541 | association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) { |
| 542 | return NULL; |
| 543 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 544 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 545 | if (!node) { |
| 546 | return NULL; |
| 547 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 548 | node->first_child = NULL; |
| 549 | node->next_sibling = NULL; |
| 550 | node->parent.entity_type = 0; |
| 551 | node->parent.entity_instance_num = 0; |
| 552 | node->parent.entity_container_id = 0; |
| 553 | node->entity.entity_type = entity->entity_type; |
| 554 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 555 | entity_instance_number != 0xffff ? entity_instance_number : 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 556 | node->association_type = association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 557 | node->remote_container_id = 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 558 | if (tree->root == NULL) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 559 | if (parent != NULL) { |
| 560 | free(node); |
| 561 | return NULL; |
| 562 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 563 | tree->root = node; |
| 564 | /* container_id 0 here indicates this is the top-most entry */ |
| 565 | node->entity.entity_container_id = 0; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 566 | node->remote_container_id = node->entity.entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 567 | } else if (parent != NULL && parent->first_child == NULL) { |
Andrew Jeffery | 7062382 | 2023-08-10 16:05:27 +0930 | [diff] [blame] | 568 | /* Ensure next_container_id() will yield a valid ID */ |
| 569 | if (tree->last_used_container_id == UINT16_MAX) { |
| 570 | free(node); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 574 | parent->first_child = node; |
| 575 | node->parent = parent->entity; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 576 | |
| 577 | if (is_remote) { |
| 578 | node->remote_container_id = entity->entity_container_id; |
| 579 | } |
| 580 | if (is_update_container_id) { |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 581 | if (container_id != 0xffff) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 582 | node->entity.entity_container_id = container_id; |
| 583 | } else { |
| 584 | node->entity.entity_container_id = |
| 585 | next_container_id(tree); |
| 586 | } |
| 587 | } else { |
| 588 | node->entity.entity_container_id = |
| 589 | entity->entity_container_id; |
| 590 | } |
| 591 | |
| 592 | if (!is_remote) { |
| 593 | node->remote_container_id = |
| 594 | node->entity.entity_container_id; |
| 595 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 596 | } else { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 597 | pldm_entity_node *start = parent == NULL ? tree->root : |
| 598 | parent->first_child; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 599 | pldm_entity_node *prev = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 600 | find_insertion_at(start, entity->entity_type); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 601 | if (!prev) { |
| 602 | free(node); |
| 603 | return NULL; |
| 604 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 605 | pldm_entity_node *next = prev->next_sibling; |
| 606 | if (prev->entity.entity_type == entity->entity_type) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 607 | if (prev->entity.entity_instance_num == UINT16_MAX) { |
| 608 | free(node); |
| 609 | return NULL; |
| 610 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 611 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 612 | entity_instance_number != 0xffff ? |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 613 | entity_instance_number : |
| 614 | prev->entity.entity_instance_num + 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 615 | } |
| 616 | prev->next_sibling = node; |
| 617 | node->parent = prev->parent; |
| 618 | node->next_sibling = next; |
| 619 | node->entity.entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 620 | prev->entity.entity_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 621 | node->remote_container_id = entity->entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 622 | } |
| 623 | entity->entity_instance_num = node->entity.entity_instance_num; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 624 | if (is_update_container_id) { |
| 625 | entity->entity_container_id = node->entity.entity_container_id; |
| 626 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 627 | return node; |
| 628 | } |
| 629 | |
| 630 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 631 | { |
| 632 | if (node == NULL) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | ++(*num); |
| 637 | get_num_nodes(node->next_sibling, num); |
| 638 | get_num_nodes(node->first_child, num); |
| 639 | } |
| 640 | |
| 641 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 642 | pldm_entity *entities, size_t *index) |
| 643 | { |
| 644 | if (node == NULL) { |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | pldm_entity *entity = &entities[*index]; |
| 649 | ++(*index); |
| 650 | entity->entity_type = node->entity.entity_type; |
| 651 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 652 | entity->entity_container_id = node->entity.entity_container_id; |
| 653 | |
| 654 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 655 | entity_association_tree_visit(node->first_child, entities, index); |
| 656 | } |
| 657 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 658 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 659 | void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree, |
| 660 | pldm_entity **entities, size_t *size) |
| 661 | { |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 662 | if (!tree || !entities || !size) { |
| 663 | return; |
| 664 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 665 | |
| 666 | *size = 0; |
| 667 | if (tree->root == NULL) { |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | get_num_nodes(tree->root, size); |
| 672 | *entities = malloc(*size * sizeof(pldm_entity)); |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 673 | if (!entities) { |
| 674 | return; |
| 675 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 676 | size_t index = 0; |
| 677 | entity_association_tree_visit(tree->root, *entities, &index); |
| 678 | } |
| 679 | |
| 680 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 681 | { |
| 682 | if (node == NULL) { |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | entity_association_tree_destroy(node->next_sibling); |
| 687 | entity_association_tree_destroy(node->first_child); |
| 688 | free(node); |
| 689 | } |
| 690 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 691 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 692 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 693 | { |
Andrew Jeffery | a89e015 | 2023-06-30 14:24:05 +0930 | [diff] [blame] | 694 | if (!tree) { |
| 695 | return; |
| 696 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 697 | |
| 698 | entity_association_tree_destroy(tree->root); |
| 699 | free(tree); |
| 700 | } |
| 701 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 702 | LIBPLDM_ABI_STABLE |
| 703 | bool pldm_entity_is_node_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 704 | { |
| 705 | assert(node != NULL); |
| 706 | |
| 707 | return node->first_child != NULL; |
| 708 | } |
| 709 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 710 | LIBPLDM_ABI_STABLE |
| 711 | pldm_entity pldm_entity_get_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 712 | { |
| 713 | assert(node != NULL); |
| 714 | |
| 715 | return node->parent; |
| 716 | } |
| 717 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 718 | LIBPLDM_ABI_STABLE |
| 719 | bool pldm_entity_is_exist_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 720 | { |
| 721 | assert(node != NULL); |
| 722 | |
| 723 | if (node->parent.entity_type == 0 && |
| 724 | node->parent.entity_instance_num == 0 && |
| 725 | node->parent.entity_container_id == 0) { |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | return true; |
| 730 | } |
| 731 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 732 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 733 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 734 | uint8_t association_type) |
| 735 | { |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 736 | if (!node) { |
| 737 | return 0; |
| 738 | } |
| 739 | |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 740 | if (!(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 741 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL)) { |
| 742 | return 0; |
| 743 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 744 | |
| 745 | size_t count = 0; |
| 746 | pldm_entity_node *curr = node->first_child; |
| 747 | while (curr != NULL) { |
| 748 | if (curr->association_type == association_type) { |
| 749 | ++count; |
| 750 | } |
| 751 | curr = curr->next_sibling; |
| 752 | } |
| 753 | |
| 754 | assert(count < UINT8_MAX); |
Andrew Jeffery | c83ef86 | 2023-07-11 17:16:55 +0930 | [diff] [blame] | 755 | return count < UINT8_MAX ? count : 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 756 | } |
| 757 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 758 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 759 | bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node) |
| 760 | { |
Andrew Jeffery | 375d9fc | 2023-06-30 15:45:54 +0930 | [diff] [blame] | 761 | if (!parent || !node) { |
| 762 | return false; |
| 763 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 764 | |
| 765 | pldm_entity_node *curr = parent->first_child; |
| 766 | while (curr != NULL) { |
| 767 | if (node->entity_type == curr->entity.entity_type && |
| 768 | node->entity_instance_num == |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 769 | curr->entity.entity_instance_num) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 770 | return true; |
| 771 | } |
| 772 | curr = curr->next_sibling; |
| 773 | } |
| 774 | |
| 775 | return false; |
| 776 | } |
| 777 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 778 | static int entity_association_pdr_add_children( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 779 | pldm_entity_node *curr, pldm_pdr *repo, uint16_t size, |
| 780 | uint8_t contained_count, uint8_t association_type, bool is_remote, |
| 781 | uint16_t terminus_handle, uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 782 | { |
| 783 | uint8_t pdr[size]; |
| 784 | uint8_t *start = pdr; |
| 785 | |
| 786 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 787 | hdr->version = 1; |
Pavithra Barithaya | 39eb322 | 2023-11-17 03:30:27 -0600 | [diff] [blame] | 788 | hdr->record_handle = record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 789 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 790 | hdr->record_change_num = 0; |
| 791 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
| 792 | start += sizeof(struct pldm_pdr_hdr); |
| 793 | |
| 794 | uint16_t *container_id = (uint16_t *)start; |
| 795 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
| 796 | start += sizeof(uint16_t); |
| 797 | *start = association_type; |
| 798 | start += sizeof(uint8_t); |
| 799 | |
| 800 | pldm_entity *entity = (pldm_entity *)start; |
| 801 | entity->entity_type = htole16(curr->entity.entity_type); |
| 802 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 803 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
| 804 | start += sizeof(pldm_entity); |
| 805 | |
| 806 | *start = contained_count; |
| 807 | start += sizeof(uint8_t); |
| 808 | |
| 809 | pldm_entity_node *node = curr->first_child; |
| 810 | while (node != NULL) { |
| 811 | if (node->association_type == association_type) { |
| 812 | pldm_entity *entity = (pldm_entity *)start; |
| 813 | entity->entity_type = htole16(node->entity.entity_type); |
| 814 | entity->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 815 | htole16(node->entity.entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 816 | entity->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 817 | htole16(node->entity.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 818 | start += sizeof(pldm_entity); |
| 819 | } |
| 820 | node = node->next_sibling; |
| 821 | } |
| 822 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 823 | return pldm_pdr_add_check(repo, pdr, size, is_remote, terminus_handle, |
| 824 | &record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 825 | } |
| 826 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 827 | static int entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 828 | pldm_pdr *repo, bool is_remote, |
| 829 | uint16_t terminus_handle, |
| 830 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 831 | { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 832 | uint8_t num_logical_children = pldm_entity_get_num_children( |
| 833 | curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 834 | uint8_t num_physical_children = pldm_entity_get_num_children( |
| 835 | curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 836 | int rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 837 | |
| 838 | if (num_logical_children) { |
| 839 | uint16_t logical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 840 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 841 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 842 | sizeof(uint8_t) + |
| 843 | (num_logical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 844 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 845 | curr, repo, logical_pdr_size, num_logical_children, |
| 846 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 847 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 848 | if (rc < 0) { |
| 849 | return rc; |
| 850 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (num_physical_children) { |
| 854 | uint16_t physical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 855 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 856 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 857 | sizeof(uint8_t) + |
| 858 | (num_physical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 859 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 860 | curr, repo, physical_pdr_size, num_physical_children, |
| 861 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 862 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 863 | if (rc < 0) { |
| 864 | return rc; |
| 865 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 866 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 867 | |
| 868 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 869 | } |
| 870 | |
Andrew Jeffery | d09b1af | 2023-07-17 12:39:16 +0930 | [diff] [blame] | 871 | static bool is_present(pldm_entity entity, pldm_entity **entities, |
| 872 | size_t num_entities) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 873 | { |
| 874 | if (entities == NULL || num_entities == 0) { |
| 875 | return true; |
| 876 | } |
| 877 | size_t i = 0; |
| 878 | while (i < num_entities) { |
| 879 | if ((*entities + i)->entity_type == entity.entity_type) { |
| 880 | return true; |
| 881 | } |
| 882 | i++; |
| 883 | } |
| 884 | return false; |
| 885 | } |
| 886 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 887 | static int entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 888 | pldm_entity **entities, |
| 889 | size_t num_entities, bool is_remote, |
| 890 | uint16_t terminus_handle, |
| 891 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 892 | { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 893 | int rc; |
| 894 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 895 | if (curr == NULL) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 896 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 897 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 898 | |
| 899 | if (is_present(curr->entity, entities, num_entities)) { |
| 900 | rc = entity_association_pdr_add_entry( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 901 | curr, repo, is_remote, terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 902 | if (rc) { |
| 903 | return rc; |
| 904 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 905 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 906 | |
| 907 | rc = entity_association_pdr_add(curr->next_sibling, repo, entities, |
| 908 | num_entities, is_remote, |
| 909 | terminus_handle, record_handle); |
| 910 | if (rc) { |
| 911 | return rc; |
| 912 | } |
| 913 | |
| 914 | return entity_association_pdr_add(curr->first_child, repo, entities, |
| 915 | num_entities, is_remote, |
| 916 | terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 917 | } |
| 918 | |
Andrew Jeffery | 096685b | 2023-07-17 17:36:14 +0930 | [diff] [blame] | 919 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 920 | int pldm_entity_association_pdr_add_check(pldm_entity_association_tree *tree, |
| 921 | pldm_pdr *repo, bool is_remote, |
| 922 | uint16_t terminus_handle) |
| 923 | { |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 924 | if (!tree || !repo) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 925 | return 0; |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 926 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 927 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 928 | return entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote, |
| 929 | terminus_handle, 0); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 930 | } |
| 931 | |
Andrew Jeffery | 1354a6e | 2023-07-07 10:34:38 +0930 | [diff] [blame] | 932 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | cc39452 | 2023-07-03 12:49:31 +0930 | [diff] [blame] | 933 | int pldm_entity_association_pdr_add_from_node_check( |
| 934 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 935 | size_t num_entities, bool is_remote, uint16_t terminus_handle) |
| 936 | { |
| 937 | return pldm_entity_association_pdr_add_from_node_with_record_handle( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 938 | node, repo, entities, num_entities, is_remote, terminus_handle, |
| 939 | 0); |
| 940 | } |
| 941 | |
Pavithra Barithaya | 3a26705 | 2023-11-13 05:01:36 -0600 | [diff] [blame] | 942 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 943 | int pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 944 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 945 | size_t num_entities, bool is_remote, uint16_t terminus_handle, |
| 946 | uint32_t record_handle) |
| 947 | { |
| 948 | if (!node || !repo || !entities) { |
| 949 | return -EINVAL; |
| 950 | } |
| 951 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 952 | entity_association_pdr_add(node, repo, entities, num_entities, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 953 | is_remote, terminus_handle, record_handle); |
| 954 | |
| 955 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 956 | } |
| 957 | |
Andrew Jeffery | 643c443 | 2023-07-17 15:36:03 +0930 | [diff] [blame] | 958 | static void find_entity_ref_in_tree(pldm_entity_node *tree_node, |
| 959 | pldm_entity entity, pldm_entity_node **node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 960 | { |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 961 | bool is_entity_container_id; |
| 962 | bool is_entity_instance_num; |
| 963 | bool is_type; |
| 964 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 965 | if (tree_node == NULL) { |
| 966 | return; |
| 967 | } |
| 968 | |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 969 | is_type = tree_node->entity.entity_type == entity.entity_type; |
| 970 | is_entity_instance_num = tree_node->entity.entity_instance_num == |
| 971 | entity.entity_instance_num; |
| 972 | is_entity_container_id = tree_node->entity.entity_container_id == |
| 973 | entity.entity_container_id; |
| 974 | |
| 975 | if (is_type && is_entity_instance_num && is_entity_container_id) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 976 | *node = tree_node; |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | find_entity_ref_in_tree(tree_node->first_child, entity, node); |
| 981 | find_entity_ref_in_tree(tree_node->next_sibling, entity, node); |
| 982 | } |
| 983 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 984 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 985 | void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree, |
| 986 | pldm_entity entity, pldm_entity_node **node) |
| 987 | { |
Andrew Jeffery | ba47e83 | 2023-07-03 11:41:03 +0930 | [diff] [blame] | 988 | if (!tree || !node) { |
| 989 | return; |
| 990 | } |
| 991 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 992 | find_entity_ref_in_tree(tree->root, entity, node); |
| 993 | } |
| 994 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 995 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 996 | void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo, |
| 997 | uint16_t terminus_handle) |
| 998 | { |
Andrew Jeffery | 438dd49 | 2023-07-03 11:51:43 +0930 | [diff] [blame] | 999 | if (!repo) { |
| 1000 | return; |
| 1001 | } |
| 1002 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1003 | bool removed = false; |
| 1004 | |
| 1005 | pldm_pdr_record *record = repo->first; |
| 1006 | pldm_pdr_record *prev = NULL; |
| 1007 | while (record != NULL) { |
| 1008 | pldm_pdr_record *next = record->next; |
| 1009 | if (record->terminus_handle == terminus_handle) { |
| 1010 | if (repo->first == record) { |
| 1011 | repo->first = next; |
| 1012 | } else { |
| 1013 | prev->next = next; |
| 1014 | } |
| 1015 | if (repo->last == record) { |
| 1016 | repo->last = prev; |
| 1017 | } |
| 1018 | if (record->data) { |
| 1019 | free(record->data); |
| 1020 | } |
| 1021 | --repo->record_count; |
| 1022 | repo->size -= record->size; |
| 1023 | free(record); |
| 1024 | removed = true; |
| 1025 | } else { |
| 1026 | prev = record; |
| 1027 | } |
| 1028 | record = next; |
| 1029 | } |
| 1030 | |
| 1031 | if (removed == true) { |
| 1032 | record = repo->first; |
| 1033 | uint32_t record_handle = 0; |
| 1034 | while (record != NULL) { |
| 1035 | record->record_handle = ++record_handle; |
| 1036 | if (record->data != NULL) { |
| 1037 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1038 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1039 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1040 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1041 | } |
| 1042 | record = record->next; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1046 | |
| 1047 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1048 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 1049 | { |
Andrew Jeffery | 3e1d659 | 2023-07-03 11:57:16 +0930 | [diff] [blame] | 1050 | if (!repo) { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1054 | bool removed = false; |
| 1055 | |
| 1056 | pldm_pdr_record *record = repo->first; |
| 1057 | pldm_pdr_record *prev = NULL; |
| 1058 | while (record != NULL) { |
| 1059 | pldm_pdr_record *next = record->next; |
| 1060 | if (record->is_remote == true) { |
| 1061 | if (repo->first == record) { |
| 1062 | repo->first = next; |
| 1063 | } else { |
| 1064 | prev->next = next; |
| 1065 | } |
| 1066 | if (repo->last == record) { |
| 1067 | repo->last = prev; |
| 1068 | } |
| 1069 | if (record->data) { |
| 1070 | free(record->data); |
| 1071 | } |
| 1072 | --repo->record_count; |
| 1073 | repo->size -= record->size; |
| 1074 | free(record); |
| 1075 | removed = true; |
| 1076 | } else { |
| 1077 | prev = record; |
| 1078 | } |
| 1079 | record = next; |
| 1080 | } |
| 1081 | |
| 1082 | if (removed == true) { |
| 1083 | record = repo->first; |
| 1084 | uint32_t record_handle = 0; |
| 1085 | while (record != NULL) { |
| 1086 | record->record_handle = ++record_handle; |
| 1087 | if (record->data != NULL) { |
| 1088 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1089 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1090 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1091 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1092 | } |
| 1093 | record = record->next; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
Pavithra Barithaya | 4a307bc | 2023-11-13 04:34:14 -0600 | [diff] [blame] | 1098 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 4d69434 | 2023-05-19 08:04:41 -0500 | [diff] [blame] | 1099 | pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo, |
| 1100 | uint32_t first, uint32_t last) |
| 1101 | { |
| 1102 | pldm_pdr_record *record = NULL; |
| 1103 | pldm_pdr_record *curr; |
| 1104 | |
| 1105 | if (!repo) { |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | for (curr = repo->first; curr; curr = curr->next) { |
| 1109 | if (first > curr->record_handle || last < curr->record_handle) { |
| 1110 | continue; |
| 1111 | } |
| 1112 | if (!record || curr->record_handle > record->record_handle) { |
| 1113 | record = curr; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | return record; |
| 1118 | } |
| 1119 | |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1120 | static void entity_association_tree_find_if_remote(pldm_entity_node *node, |
| 1121 | pldm_entity *entity, |
| 1122 | pldm_entity_node **out, |
| 1123 | bool is_remote) |
| 1124 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1125 | if (node == NULL) { |
| 1126 | return; |
| 1127 | } |
| 1128 | bool is_entity_type; |
| 1129 | bool is_entity_instance_num; |
| 1130 | |
| 1131 | is_entity_type = node->entity.entity_type == entity->entity_type; |
| 1132 | is_entity_instance_num = node->entity.entity_instance_num == |
| 1133 | entity->entity_instance_num; |
| 1134 | |
| 1135 | if (!is_remote || |
| 1136 | node->remote_container_id == entity->entity_container_id) { |
| 1137 | if (is_entity_type && is_entity_instance_num) { |
| 1138 | entity->entity_container_id = |
| 1139 | node->entity.entity_container_id; |
| 1140 | *out = node; |
| 1141 | return; |
| 1142 | } |
| 1143 | } |
| 1144 | entity_association_tree_find_if_remote(node->next_sibling, entity, out, |
| 1145 | is_remote); |
| 1146 | entity_association_tree_find_if_remote(node->first_child, entity, out, |
| 1147 | is_remote); |
| 1148 | } |
| 1149 | |
Pavithra Barithaya | 7570fae | 2023-07-28 02:15:16 -0500 | [diff] [blame] | 1150 | LIBPLDM_ABI_STABLE |
Sagar Srinivas | 302d9ff | 2023-08-01 02:11:30 -0500 | [diff] [blame] | 1151 | pldm_entity_node *pldm_entity_association_tree_find_with_locality( |
| 1152 | pldm_entity_association_tree *tree, pldm_entity *entity, bool is_remote) |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1153 | { |
| 1154 | if (!tree || !entity) { |
| 1155 | return NULL; |
| 1156 | } |
| 1157 | pldm_entity_node *node = NULL; |
| 1158 | entity_association_tree_find_if_remote(tree->root, entity, &node, |
| 1159 | is_remote); |
| 1160 | return node; |
| 1161 | } |
| 1162 | |
Andrew Jeffery | 54d91e8 | 2023-07-17 15:37:12 +0930 | [diff] [blame] | 1163 | static void entity_association_tree_find(pldm_entity_node *node, |
| 1164 | pldm_entity *entity, |
| 1165 | pldm_entity_node **out) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1166 | { |
| 1167 | if (node == NULL) { |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | if (node->entity.entity_type == entity->entity_type && |
| 1172 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 1173 | entity->entity_container_id = node->entity.entity_container_id; |
| 1174 | *out = node; |
| 1175 | return; |
| 1176 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1177 | entity_association_tree_find(node->next_sibling, entity, out); |
| 1178 | entity_association_tree_find(node->first_child, entity, out); |
| 1179 | } |
| 1180 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1181 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1182 | pldm_entity_node * |
| 1183 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 1184 | pldm_entity *entity) |
| 1185 | { |
Andrew Jeffery | 94e364d | 2023-07-03 12:26:59 +0930 | [diff] [blame] | 1186 | if (!tree || !entity) { |
| 1187 | return NULL; |
| 1188 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1189 | |
| 1190 | pldm_entity_node *node = NULL; |
| 1191 | entity_association_tree_find(tree->root, entity, &node); |
| 1192 | return node; |
| 1193 | } |
| 1194 | |
| 1195 | static void entity_association_tree_copy(pldm_entity_node *org_node, |
| 1196 | pldm_entity_node **new_node) |
| 1197 | { |
| 1198 | if (org_node == NULL) { |
| 1199 | return; |
| 1200 | } |
| 1201 | *new_node = malloc(sizeof(pldm_entity_node)); |
| 1202 | (*new_node)->parent = org_node->parent; |
| 1203 | (*new_node)->entity = org_node->entity; |
| 1204 | (*new_node)->association_type = org_node->association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 1205 | (*new_node)->remote_container_id = org_node->remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1206 | (*new_node)->first_child = NULL; |
| 1207 | (*new_node)->next_sibling = NULL; |
| 1208 | entity_association_tree_copy(org_node->first_child, |
| 1209 | &((*new_node)->first_child)); |
| 1210 | entity_association_tree_copy(org_node->next_sibling, |
| 1211 | &((*new_node)->next_sibling)); |
| 1212 | } |
| 1213 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1214 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1215 | void pldm_entity_association_tree_copy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1216 | pldm_entity_association_tree *org_tree, |
| 1217 | pldm_entity_association_tree *new_tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1218 | { |
George Liu | c6c391d | 2023-11-09 10:13:34 +0800 | [diff] [blame] | 1219 | assert(org_tree != NULL); |
| 1220 | assert(new_tree != NULL); |
| 1221 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1222 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 1223 | entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 1224 | } |
| 1225 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1226 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1227 | void pldm_entity_association_tree_destroy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1228 | pldm_entity_association_tree *tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1229 | { |
Andrew Jeffery | 85d7a05 | 2023-07-03 12:29:24 +0930 | [diff] [blame] | 1230 | if (!tree) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1234 | entity_association_tree_destroy(tree->root); |
| 1235 | tree->last_used_container_id = 0; |
| 1236 | tree->root = NULL; |
| 1237 | } |
| 1238 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1239 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1240 | bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree) |
| 1241 | { |
| 1242 | return ((tree->root == NULL) ? true : false); |
| 1243 | } |
| 1244 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1245 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1246 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 1247 | size_t *num_entities, |
| 1248 | pldm_entity **entities) |
| 1249 | { |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1250 | if (!pdr || !num_entities || !entities) { |
| 1251 | return; |
| 1252 | } |
| 1253 | #define PDR_MIN_SIZE \ |
| 1254 | (sizeof(struct pldm_pdr_hdr) + \ |
| 1255 | sizeof(struct pldm_pdr_entity_association)) |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1256 | if (pdr_len < PDR_MIN_SIZE) { |
| 1257 | return; |
| 1258 | } |
| 1259 | #undef PDR_MIN_SIZE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1260 | |
| 1261 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1262 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 1263 | return; |
| 1264 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1265 | |
| 1266 | const uint8_t *start = (uint8_t *)pdr; |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1267 | const uint8_t *end __attribute__((unused)) = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1268 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1269 | start += sizeof(struct pldm_pdr_hdr); |
| 1270 | struct pldm_pdr_entity_association *entity_association_pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1271 | (struct pldm_pdr_entity_association *)start; |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1272 | size_t l_num_entities = entity_association_pdr->num_children + 1; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1273 | if (l_num_entities < 2) { |
| 1274 | return; |
| 1275 | } |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1276 | if (start + sizeof(struct pldm_pdr_entity_association) + |
| 1277 | sizeof(pldm_entity) * (l_num_entities - 2) != |
| 1278 | end) { |
| 1279 | return; |
| 1280 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1281 | pldm_entity *l_entities = malloc(sizeof(pldm_entity) * l_num_entities); |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1282 | if (!l_entities) { |
| 1283 | return; |
| 1284 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1285 | l_entities[0].entity_type = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1286 | le16toh(entity_association_pdr->container.entity_type); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1287 | l_entities[0].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1288 | le16toh(entity_association_pdr->container.entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1289 | l_entities[0].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1290 | le16toh(entity_association_pdr->container.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1291 | pldm_entity *curr_entity = entity_association_pdr->children; |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1292 | for (size_t i = 1; i < l_num_entities; i++, curr_entity++) { |
| 1293 | l_entities[i].entity_type = le16toh(curr_entity->entity_type); |
| 1294 | l_entities[i].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1295 | le16toh(curr_entity->entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1296 | l_entities[i].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1297 | le16toh(curr_entity->entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1298 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1299 | |
| 1300 | *num_entities = l_num_entities; |
| 1301 | *entities = l_entities; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1302 | } |