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