blob: a78593c630027efbca02ff3a95408395e6917490 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include "pdr.h"
2#include "platform.h"
3#include <assert.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304#include <endian.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09305#include <stdlib.h>
6#include <string.h>
ArchanaKakani39bd2ea2023-02-02 02:39:18 -06007#include <errno.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09308
9typedef struct pldm_pdr_record {
10 uint32_t record_handle;
11 uint32_t size;
12 uint8_t *data;
13 struct pldm_pdr_record *next;
14 bool is_remote;
15 uint16_t terminus_handle;
16} pldm_pdr_record;
17
18typedef struct pldm_pdr {
19 uint32_t record_count;
20 uint32_t size;
21 pldm_pdr_record *first;
22 pldm_pdr_record *last;
23} pldm_pdr;
24
25static inline uint32_t get_next_record_handle(const pldm_pdr *repo,
26 const pldm_pdr_record *record)
27{
28 assert(repo != NULL);
29 assert(record != NULL);
30
31 if (record == repo->last) {
32 return 0;
33 }
34 return record->next->record_handle;
35}
36
37static void add_record(pldm_pdr *repo, pldm_pdr_record *record)
38{
39 assert(repo != NULL);
40 assert(record != NULL);
41
42 if (repo->first == NULL) {
43 assert(repo->last == NULL);
44 repo->first = record;
45 repo->last = record;
46 } else {
47 repo->last->next = record;
48 repo->last = record;
49 }
50 repo->size += record->size;
51 ++repo->record_count;
52}
53
54static inline uint32_t get_new_record_handle(const pldm_pdr *repo)
55{
56 assert(repo != NULL);
57 uint32_t last_used_hdl =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093058 repo->last != NULL ? repo->last->record_handle : 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +093059 assert(last_used_hdl != UINT32_MAX);
60
61 return last_used_hdl + 1;
62}
63
64static pldm_pdr_record *make_new_record(const pldm_pdr *repo,
65 const uint8_t *data, uint32_t size,
66 uint32_t record_handle, bool is_remote,
67 uint16_t terminus_handle)
68{
69 assert(repo != NULL);
70 assert(size != 0);
71
72 pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record));
73 assert(record != NULL);
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093074 record->record_handle = record_handle == 0 ?
75 get_new_record_handle(repo) :
76 record_handle;
Andrew Jeffery9c766792022-08-10 23:12:49 +093077 record->size = size;
78 record->is_remote = is_remote;
79 record->terminus_handle = terminus_handle;
80 if (data != NULL) {
81 record->data = malloc(size);
82 assert(record->data != NULL);
83 memcpy(record->data, data, size);
84 /* If record handle is 0, that is an indication for this API to
85 * compute a new handle. For that reason, the computed handle
86 * needs to be populated in the PDR header. For a case where the
87 * caller supplied the record handle, it would exist in the
88 * header already.
89 */
90 if (!record_handle) {
91 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093092 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +093093 hdr->record_handle = htole32(record->record_handle);
94 }
95 }
96 record->next = NULL;
97
98 return record;
99}
100
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930101LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930102uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size,
103 uint32_t record_handle, bool is_remote,
104 uint16_t terminus_handle)
105{
106 assert(size != 0);
107 assert(data != NULL);
108
109 pldm_pdr_record *record = make_new_record(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930110 repo, data, size, record_handle, is_remote, terminus_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930111 add_record(repo, record);
112
113 return record->record_handle;
114}
115
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930116LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930117pldm_pdr *pldm_pdr_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930118{
119 pldm_pdr *repo = malloc(sizeof(pldm_pdr));
120 assert(repo != NULL);
121 repo->record_count = 0;
122 repo->size = 0;
123 repo->first = NULL;
124 repo->last = NULL;
125
126 return repo;
127}
128
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930129LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930130void pldm_pdr_destroy(pldm_pdr *repo)
131{
132 assert(repo != NULL);
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 Jeffery9d2a1c62023-06-05 13:02:16 +0930147LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930148const 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{
153 assert(repo != NULL);
154 assert(data != NULL);
155 assert(size != NULL);
156 assert(next_record_handle != NULL);
157
158 if (!record_handle && (repo->first != NULL)) {
159 record_handle = repo->first->record_handle;
160 }
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 Jeffery37dd6a32023-05-12 16:04:06 +0930167 get_next_record_handle(repo, record);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930168 return record;
169 }
170 record = record->next;
171 }
172
173 *size = 0;
174 *next_record_handle = 0;
175 return NULL;
176}
177
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930178LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930179const pldm_pdr_record *
180pldm_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{
184 assert(repo != NULL);
185 assert(curr_record != NULL);
186 assert(data != NULL);
187 assert(size != NULL);
188 assert(next_record_handle != NULL);
189
190 if (curr_record == repo->last) {
191 *data = NULL;
192 *size = 0;
193 *next_record_handle = get_next_record_handle(repo, curr_record);
194 return NULL;
195 }
196
197 *next_record_handle = get_next_record_handle(repo, curr_record->next);
198 *data = curr_record->next->data;
199 *size = curr_record->next->size;
200 return curr_record->next;
201}
202
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930203LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930204const pldm_pdr_record *
205pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type,
206 const pldm_pdr_record *curr_record, uint8_t **data,
207 uint32_t *size)
208{
209 assert(repo != NULL);
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 Jeffery9d2a1c62023-06-05 13:02:16 +0930233LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930234uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo)
235{
236 assert(repo != NULL);
237
238 return repo->record_count;
239}
240
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930241LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930242uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo)
243{
244 assert(repo != NULL);
245
246 return repo->size;
247}
248
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930249LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930250uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo,
251 const pldm_pdr_record *record)
252{
253 assert(repo != NULL);
254 assert(record != NULL);
255
256 return record->record_handle;
257}
258
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930259LIBPLDM_ABI_STABLE
260bool pldm_pdr_record_is_remote(const pldm_pdr_record *record)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930261{
262 assert(record != NULL);
263
264 return record->is_remote;
265}
266
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930267LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930268uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle,
269 uint16_t fru_rsi, uint16_t entity_type,
270 uint16_t entity_instance_num,
271 uint16_t container_id,
272 uint32_t bmc_record_handle)
273{
274 uint32_t size = sizeof(struct pldm_pdr_hdr) +
275 sizeof(struct pldm_pdr_fru_record_set);
276 uint8_t data[size];
277
278 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data;
279 hdr->version = 1;
280 hdr->record_handle = bmc_record_handle;
281 hdr->type = PLDM_PDR_FRU_RECORD_SET;
282 hdr->record_change_num = 0;
283 hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set));
284 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930285 (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr +
286 sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930287 fru->terminus_handle = htole16(terminus_handle);
288 fru->fru_rsi = htole16(fru_rsi);
289 fru->entity_type = htole16(entity_type);
290 fru->entity_instance_num = htole16(entity_instance_num);
291 fru->container_id = htole16(container_id);
292
293 return pldm_pdr_add(repo, data, size, bmc_record_handle, false,
294 terminus_handle);
295}
296
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930297LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930298const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930299 const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle,
300 uint16_t *entity_type, uint16_t *entity_instance_num,
301 uint16_t *container_id)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930302{
303 assert(terminus_handle != NULL);
304 assert(entity_type != NULL);
305 assert(entity_instance_num != NULL);
306 assert(container_id != NULL);
307
308 uint8_t *data = NULL;
309 uint32_t size = 0;
310 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930311 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930312 while (curr_record != NULL) {
313 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930314 (struct pldm_pdr_fru_record_set
315 *)(data + sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930316 if (fru->fru_rsi == htole16(fru_rsi)) {
317 *terminus_handle = le16toh(fru->terminus_handle);
318 *entity_type = le16toh(fru->entity_type);
319 *entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930320 le16toh(fru->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930321 *container_id = le16toh(fru->container_id);
322 return curr_record;
323 }
324 data = NULL;
325 curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930326 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data,
327 &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930328 }
329
330 *terminus_handle = 0;
331 *entity_type = 0;
332 *entity_instance_num = 0;
333 *container_id = 0;
334
335 return NULL;
336}
337
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930338LIBPLDM_ABI_STABLE
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930339/* NOLINTNEXTLINE(readability-identifier-naming) */
340void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle,
341 uint8_t tid, uint8_t tl_eid, bool valid_bit)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930342{
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930343 uint8_t *out_data = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930344 uint32_t size = 0;
345 const pldm_pdr_record *record;
346 record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR,
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930347 NULL, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930348
349 do {
350 if (record != NULL) {
351 struct pldm_terminus_locator_pdr *pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930352 (struct pldm_terminus_locator_pdr *)out_data;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930353 struct pldm_terminus_locator_type_mctp_eid *value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930354 (struct pldm_terminus_locator_type_mctp_eid *)
355 pdr->terminus_locator_value;
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930356 if (pdr->terminus_handle == terminus_handle &&
357 pdr->tid == tid && value->eid == tl_eid) {
358 pdr->validity = valid_bit;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930359 break;
360 }
361 }
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930362 record = pldm_pdr_find_record_by_type(repo,
363 PLDM_TERMINUS_LOCATOR_PDR,
364 record, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930365 } while (record);
366}
367
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500368static bool pldm_record_handle_in_range(uint32_t record_handle,
369 uint32_t first_record_handle,
370 uint32_t last_record_handle)
371{
372 return record_handle >= first_record_handle &&
373 record_handle <= last_record_handle;
374}
375
376LIBPLDM_ABI_TESTING
377int pldm_pdr_find_container_id_range_exclude(
378 const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance,
379 uint32_t range_exclude_start_handle, uint32_t range_exclude_end_handle,
380 uint16_t *container_id)
381{
382 pldm_pdr_record *record;
383 if (!repo) {
384 return -EINVAL;
385 }
386
387 for (record = repo->first; record; record = record->next) {
388 bool is_container_entity_instance_number;
389 struct pldm_pdr_entity_association *pdr;
390 bool is_container_entity_type;
391 struct pldm_entity *child;
392 struct pldm_pdr_hdr *hdr;
393 bool in_range;
394
395 // pldm_pdr_add() takes only uint8_t* data as an argument.
396 // The expectation here is the pldm_pdr_hdr is the first field of the record data
397 hdr = (struct pldm_pdr_hdr *)record->data;
398 if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
399 continue;
400 }
401 in_range = pldm_record_handle_in_range(
402 record->record_handle, range_exclude_start_handle,
403 range_exclude_end_handle);
404 if (in_range) {
405 continue;
406 }
407
408 // this cast is valid with respect to alignment because
409 // struct pldm_pdr_hdr is declared with __attribute__((packed))
410 pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr));
411 if (pdr->num_children == 0) {
412 continue;
413 }
414 child = (&pdr->children[0]);
415 is_container_entity_type = pdr->container.entity_type ==
416 entity_type;
417 is_container_entity_instance_number =
418 pdr->container.entity_instance_num == entity_instance;
419 if (is_container_entity_type &&
420 is_container_entity_instance_number) {
421 *container_id = le16toh(child->entity_container_id);
422 }
423 }
424 return -ENOKEY;
425}
426
Andrew Jeffery9c766792022-08-10 23:12:49 +0930427typedef struct pldm_entity_association_tree {
428 pldm_entity_node *root;
429 uint16_t last_used_container_id;
430} pldm_entity_association_tree;
431
432typedef struct pldm_entity_node {
433 pldm_entity entity;
434 pldm_entity parent;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600435 uint16_t remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930436 pldm_entity_node *first_child;
437 pldm_entity_node *next_sibling;
438 uint8_t association_type;
439} pldm_entity_node;
440
441static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
442{
443 assert(tree != NULL);
444 assert(tree->last_used_container_id != UINT16_MAX);
445
446 return ++tree->last_used_container_id;
447}
448
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930449LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930450pldm_entity pldm_entity_extract(pldm_entity_node *node)
451{
452 assert(node != NULL);
453
454 return node->entity;
455}
456
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600457LIBPLDM_ABI_TESTING
458int pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity,
459 uint16_t *cid)
460{
461 if (!entity) {
462 return -EINVAL;
463 }
464
465 *cid = entity->remote_container_id;
466 return 0;
467}
468
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930469LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930470pldm_entity_association_tree *pldm_entity_association_tree_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930471{
472 pldm_entity_association_tree *tree =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930473 malloc(sizeof(pldm_entity_association_tree));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930474 assert(tree != NULL);
475 tree->root = NULL;
476 tree->last_used_container_id = 0;
477
478 return tree;
479}
480
481static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
482 uint16_t entity_type)
483{
484 assert(start != NULL);
485
486 /* Insert after the the last node that matches the input entity type, or
487 * at the end if no such match occurrs
488 */
489 while (start->next_sibling != NULL) {
490 uint16_t this_type = start->entity.entity_type;
491 pldm_entity_node *next = start->next_sibling;
492 if (this_type == entity_type &&
493 (this_type != next->entity.entity_type)) {
494 break;
495 }
496 start = start->next_sibling;
497 }
498
499 return start;
500}
501
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930502LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930503pldm_entity_node *pldm_entity_association_tree_add(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930504 pldm_entity_association_tree *tree, pldm_entity *entity,
505 uint16_t entity_instance_number, pldm_entity_node *parent,
506 uint8_t association_type)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930507{
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500508 return pldm_entity_association_tree_add_entity(tree, entity,
509 entity_instance_number,
510 parent, association_type,
511 false, true, 0xFFFF);
512}
513
514LIBPLDM_ABI_TESTING
515pldm_entity_node *pldm_entity_association_tree_add_entity(
516 pldm_entity_association_tree *tree, pldm_entity *entity,
517 uint16_t entity_instance_number, pldm_entity_node *parent,
518 uint8_t association_type, bool is_remote, bool is_update_container_id,
519 uint16_t container_id)
520{
521 if ((!tree) || (!entity)) {
522 return NULL;
523 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930524
525 if (entity_instance_number != 0xFFFF && parent != NULL) {
526 pldm_entity node;
527 node.entity_type = entity->entity_type;
528 node.entity_instance_num = entity_instance_number;
529 if (pldm_is_current_parent_child(parent, &node)) {
530 return NULL;
531 }
532 }
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500533 if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL &&
534 association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) {
535 return NULL;
536 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930537 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500538 if (!node) {
539 return NULL;
540 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930541 node->first_child = NULL;
542 node->next_sibling = NULL;
543 node->parent.entity_type = 0;
544 node->parent.entity_instance_num = 0;
545 node->parent.entity_container_id = 0;
546 node->entity.entity_type = entity->entity_type;
547 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930548 entity_instance_number != 0xFFFF ? entity_instance_number : 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930549 node->association_type = association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600550 node->remote_container_id = 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930551 if (tree->root == NULL) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500552 if (parent != NULL) {
553 free(node);
554 return NULL;
555 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930556 tree->root = node;
557 /* container_id 0 here indicates this is the top-most entry */
558 node->entity.entity_container_id = 0;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600559 node->remote_container_id = node->entity.entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930560 } else if (parent != NULL && parent->first_child == NULL) {
561 parent->first_child = node;
562 node->parent = parent->entity;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500563
564 if (is_remote) {
565 node->remote_container_id = entity->entity_container_id;
566 }
567 if (is_update_container_id) {
568 if (container_id != 0xFFFF) {
569 node->entity.entity_container_id = container_id;
570 } else {
571 node->entity.entity_container_id =
572 next_container_id(tree);
573 }
574 } else {
575 node->entity.entity_container_id =
576 entity->entity_container_id;
577 }
578
579 if (!is_remote) {
580 node->remote_container_id =
581 node->entity.entity_container_id;
582 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930583 } else {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930584 pldm_entity_node *start = parent == NULL ? tree->root :
585 parent->first_child;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930586 pldm_entity_node *prev =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930587 find_insertion_at(start, entity->entity_type);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500588 if (!prev) {
589 free(node);
590 return NULL;
591 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930592 pldm_entity_node *next = prev->next_sibling;
593 if (prev->entity.entity_type == entity->entity_type) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500594 if (prev->entity.entity_instance_num == UINT16_MAX) {
595 free(node);
596 return NULL;
597 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930598 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930599 entity_instance_number != 0xFFFF ?
600 entity_instance_number :
601 prev->entity.entity_instance_num + 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930602 }
603 prev->next_sibling = node;
604 node->parent = prev->parent;
605 node->next_sibling = next;
606 node->entity.entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930607 prev->entity.entity_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600608 node->remote_container_id = entity->entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930609 }
610 entity->entity_instance_num = node->entity.entity_instance_num;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500611 if (is_update_container_id) {
612 entity->entity_container_id = node->entity.entity_container_id;
613 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930614 return node;
615}
616
617static void get_num_nodes(pldm_entity_node *node, size_t *num)
618{
619 if (node == NULL) {
620 return;
621 }
622
623 ++(*num);
624 get_num_nodes(node->next_sibling, num);
625 get_num_nodes(node->first_child, num);
626}
627
628static void entity_association_tree_visit(pldm_entity_node *node,
629 pldm_entity *entities, size_t *index)
630{
631 if (node == NULL) {
632 return;
633 }
634
635 pldm_entity *entity = &entities[*index];
636 ++(*index);
637 entity->entity_type = node->entity.entity_type;
638 entity->entity_instance_num = node->entity.entity_instance_num;
639 entity->entity_container_id = node->entity.entity_container_id;
640
641 entity_association_tree_visit(node->next_sibling, entities, index);
642 entity_association_tree_visit(node->first_child, entities, index);
643}
644
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930645LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930646void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
647 pldm_entity **entities, size_t *size)
648{
649 assert(tree != NULL);
650
651 *size = 0;
652 if (tree->root == NULL) {
653 return;
654 }
655
656 get_num_nodes(tree->root, size);
657 *entities = malloc(*size * sizeof(pldm_entity));
658 size_t index = 0;
659 entity_association_tree_visit(tree->root, *entities, &index);
660}
661
662static void entity_association_tree_destroy(pldm_entity_node *node)
663{
664 if (node == NULL) {
665 return;
666 }
667
668 entity_association_tree_destroy(node->next_sibling);
669 entity_association_tree_destroy(node->first_child);
670 free(node);
671}
672
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930673LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930674void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
675{
676 assert(tree != NULL);
677
678 entity_association_tree_destroy(tree->root);
679 free(tree);
680}
681
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930682LIBPLDM_ABI_STABLE
683bool pldm_entity_is_node_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930684{
685 assert(node != NULL);
686
687 return node->first_child != NULL;
688}
689
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930690LIBPLDM_ABI_STABLE
691pldm_entity pldm_entity_get_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930692{
693 assert(node != NULL);
694
695 return node->parent;
696}
697
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930698LIBPLDM_ABI_STABLE
699bool pldm_entity_is_exist_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930700{
701 assert(node != NULL);
702
703 if (node->parent.entity_type == 0 &&
704 node->parent.entity_instance_num == 0 &&
705 node->parent.entity_container_id == 0) {
706 return false;
707 }
708
709 return true;
710}
711
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930712LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930713uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
714 uint8_t association_type)
715{
716 assert(node != NULL);
717 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
718 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
719
720 size_t count = 0;
721 pldm_entity_node *curr = node->first_child;
722 while (curr != NULL) {
723 if (curr->association_type == association_type) {
724 ++count;
725 }
726 curr = curr->next_sibling;
727 }
728
729 assert(count < UINT8_MAX);
730 return count;
731}
732
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930733LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930734bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node)
735{
736 assert(parent != NULL);
737 assert(node != NULL);
738
739 pldm_entity_node *curr = parent->first_child;
740 while (curr != NULL) {
741 if (node->entity_type == curr->entity.entity_type &&
742 node->entity_instance_num ==
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930743 curr->entity.entity_instance_num) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930744 return true;
745 }
746 curr = curr->next_sibling;
747 }
748
749 return false;
750}
751
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500752static void entity_association_pdr_add_children(
753 pldm_entity_node *curr, pldm_pdr *repo, uint16_t size,
754 uint8_t contained_count, uint8_t association_type, bool is_remote,
755 uint16_t terminus_handle, uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930756{
757 uint8_t pdr[size];
758 uint8_t *start = pdr;
759
760 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start;
761 hdr->version = 1;
762 hdr->record_handle = 0;
763 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
764 hdr->record_change_num = 0;
765 hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr));
766 start += sizeof(struct pldm_pdr_hdr);
767
768 uint16_t *container_id = (uint16_t *)start;
769 *container_id = htole16(curr->first_child->entity.entity_container_id);
770 start += sizeof(uint16_t);
771 *start = association_type;
772 start += sizeof(uint8_t);
773
774 pldm_entity *entity = (pldm_entity *)start;
775 entity->entity_type = htole16(curr->entity.entity_type);
776 entity->entity_instance_num = htole16(curr->entity.entity_instance_num);
777 entity->entity_container_id = htole16(curr->entity.entity_container_id);
778 start += sizeof(pldm_entity);
779
780 *start = contained_count;
781 start += sizeof(uint8_t);
782
783 pldm_entity_node *node = curr->first_child;
784 while (node != NULL) {
785 if (node->association_type == association_type) {
786 pldm_entity *entity = (pldm_entity *)start;
787 entity->entity_type = htole16(node->entity.entity_type);
788 entity->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930789 htole16(node->entity.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930790 entity->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930791 htole16(node->entity.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930792 start += sizeof(pldm_entity);
793 }
794 node = node->next_sibling;
795 }
796
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500797 pldm_pdr_add(repo, pdr, size, record_handle, is_remote,
798 terminus_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930799}
800
801static void entity_association_pdr_add_entry(pldm_entity_node *curr,
802 pldm_pdr *repo, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500803 uint16_t terminus_handle,
804 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930805{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930806 uint8_t num_logical_children = pldm_entity_get_num_children(
807 curr, PLDM_ENTITY_ASSOCIAION_LOGICAL);
808 uint8_t num_physical_children = pldm_entity_get_num_children(
809 curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930810
811 if (num_logical_children) {
812 uint16_t logical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930813 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
814 sizeof(uint8_t) + sizeof(pldm_entity) +
815 sizeof(uint8_t) +
816 (num_logical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930817 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930818 curr, repo, logical_pdr_size, num_logical_children,
819 PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500820 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930821 }
822
823 if (num_physical_children) {
824 uint16_t physical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930825 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
826 sizeof(uint8_t) + sizeof(pldm_entity) +
827 sizeof(uint8_t) +
828 (num_physical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930829 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930830 curr, repo, physical_pdr_size, num_physical_children,
831 PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500832 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930833 }
834}
835
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930836LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930837bool is_present(pldm_entity entity, pldm_entity **entities, size_t num_entities)
838{
839 if (entities == NULL || num_entities == 0) {
840 return true;
841 }
842 size_t i = 0;
843 while (i < num_entities) {
844 if ((*entities + i)->entity_type == entity.entity_type) {
845 return true;
846 }
847 i++;
848 }
849 return false;
850}
851
852static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo,
853 pldm_entity **entities,
854 size_t num_entities, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500855 uint16_t terminus_handle,
856 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930857{
858 if (curr == NULL) {
859 return;
860 }
861 bool to_add = true;
862 to_add = is_present(curr->entity, entities, num_entities);
863 if (to_add) {
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500864 entity_association_pdr_add_entry(
865 curr, repo, is_remote, terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930866 }
867 entity_association_pdr_add(curr->next_sibling, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500868 num_entities, is_remote, terminus_handle,
869 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930870 entity_association_pdr_add(curr->first_child, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500871 num_entities, is_remote, terminus_handle,
872 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930873}
874
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930875LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930876void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree,
877 pldm_pdr *repo, bool is_remote,
878 uint16_t terminus_handle)
879{
880 assert(tree != NULL);
881 assert(repo != NULL);
882
883 entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500884 terminus_handle, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930885}
886
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930887LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930888void pldm_entity_association_pdr_add_from_node(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930889 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
890 size_t num_entities, bool is_remote, uint16_t terminus_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930891{
892 assert(repo != NULL);
893
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500894 pldm_entity_association_pdr_add_from_node_with_record_handle(
895 node, repo, entities, num_entities, is_remote, terminus_handle,
896 0);
897}
898
899LIBPLDM_ABI_TESTING
900int pldm_entity_association_pdr_add_from_node_with_record_handle(
901 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
902 size_t num_entities, bool is_remote, uint16_t terminus_handle,
903 uint32_t record_handle)
904{
905 if (!node || !repo || !entities) {
906 return -EINVAL;
907 }
908
Andrew Jeffery9c766792022-08-10 23:12:49 +0930909 entity_association_pdr_add(node, repo, entities, num_entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500910 is_remote, terminus_handle, record_handle);
911
912 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930913}
914
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930915LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930916void find_entity_ref_in_tree(pldm_entity_node *tree_node, pldm_entity entity,
917 pldm_entity_node **node)
918{
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600919 bool is_entity_container_id;
920 bool is_entity_instance_num;
921 bool is_type;
922
Andrew Jeffery9c766792022-08-10 23:12:49 +0930923 if (tree_node == NULL) {
924 return;
925 }
926
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600927 is_type = tree_node->entity.entity_type == entity.entity_type;
928 is_entity_instance_num = tree_node->entity.entity_instance_num ==
929 entity.entity_instance_num;
930 is_entity_container_id = tree_node->entity.entity_container_id ==
931 entity.entity_container_id;
932
933 if (is_type && is_entity_instance_num && is_entity_container_id) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930934 *node = tree_node;
935 return;
936 }
937
938 find_entity_ref_in_tree(tree_node->first_child, entity, node);
939 find_entity_ref_in_tree(tree_node->next_sibling, entity, node);
940}
941
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930942LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930943void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree,
944 pldm_entity entity, pldm_entity_node **node)
945{
946 assert(tree != NULL);
947 find_entity_ref_in_tree(tree->root, entity, node);
948}
949
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930950LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930951void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo,
952 uint16_t terminus_handle)
953{
954 assert(repo != NULL);
955 bool removed = false;
956
957 pldm_pdr_record *record = repo->first;
958 pldm_pdr_record *prev = NULL;
959 while (record != NULL) {
960 pldm_pdr_record *next = record->next;
961 if (record->terminus_handle == terminus_handle) {
962 if (repo->first == record) {
963 repo->first = next;
964 } else {
965 prev->next = next;
966 }
967 if (repo->last == record) {
968 repo->last = prev;
969 }
970 if (record->data) {
971 free(record->data);
972 }
973 --repo->record_count;
974 repo->size -= record->size;
975 free(record);
976 removed = true;
977 } else {
978 prev = record;
979 }
980 record = next;
981 }
982
983 if (removed == true) {
984 record = repo->first;
985 uint32_t record_handle = 0;
986 while (record != NULL) {
987 record->record_handle = ++record_handle;
988 if (record->data != NULL) {
989 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930990 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930991 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930992 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930993 }
994 record = record->next;
995 }
996 }
997}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930998
999LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301000void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo)
1001{
1002 assert(repo != NULL);
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->is_remote == true) {
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 Jeffery37dd6a32023-05-12 16:04:06 +09301038 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301039 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301040 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301041 }
1042 record = record->next;
1043 }
1044 }
1045}
1046
Pavithra Barithaya4d694342023-05-19 08:04:41 -05001047LIBPLDM_ABI_TESTING
1048pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo,
1049 uint32_t first, uint32_t last)
1050{
1051 pldm_pdr_record *record = NULL;
1052 pldm_pdr_record *curr;
1053
1054 if (!repo) {
1055 return NULL;
1056 }
1057 for (curr = repo->first; curr; curr = curr->next) {
1058 if (first > curr->record_handle || last < curr->record_handle) {
1059 continue;
1060 }
1061 if (!record || curr->record_handle > record->record_handle) {
1062 record = curr;
1063 }
1064 }
1065
1066 return record;
1067}
1068
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001069static void entity_association_tree_find_if_remote(pldm_entity_node *node,
1070 pldm_entity *entity,
1071 pldm_entity_node **out,
1072 bool is_remote)
1073{
1074 assert(out != NULL && *out == NULL);
1075 if (node == NULL) {
1076 return;
1077 }
1078 bool is_entity_type;
1079 bool is_entity_instance_num;
1080
1081 is_entity_type = node->entity.entity_type == entity->entity_type;
1082 is_entity_instance_num = node->entity.entity_instance_num ==
1083 entity->entity_instance_num;
1084
1085 if (!is_remote ||
1086 node->remote_container_id == entity->entity_container_id) {
1087 if (is_entity_type && is_entity_instance_num) {
1088 entity->entity_container_id =
1089 node->entity.entity_container_id;
1090 *out = node;
1091 return;
1092 }
1093 }
1094 entity_association_tree_find_if_remote(node->next_sibling, entity, out,
1095 is_remote);
1096 entity_association_tree_find_if_remote(node->first_child, entity, out,
1097 is_remote);
1098}
1099
1100LIBPLDM_ABI_TESTING
1101pldm_entity_node *
1102pldm_entity_association_tree_find_if_remote(pldm_entity_association_tree *tree,
1103 pldm_entity *entity, bool is_remote)
1104{
1105 if (!tree || !entity) {
1106 return NULL;
1107 }
1108 pldm_entity_node *node = NULL;
1109 entity_association_tree_find_if_remote(tree->root, entity, &node,
1110 is_remote);
1111 return node;
1112}
1113
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301114LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301115void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity,
1116 pldm_entity_node **out)
1117{
1118 if (node == NULL) {
1119 return;
1120 }
1121
1122 if (node->entity.entity_type == entity->entity_type &&
1123 node->entity.entity_instance_num == entity->entity_instance_num) {
1124 entity->entity_container_id = node->entity.entity_container_id;
1125 *out = node;
1126 return;
1127 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301128 entity_association_tree_find(node->next_sibling, entity, out);
1129 entity_association_tree_find(node->first_child, entity, out);
1130}
1131
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301132LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301133pldm_entity_node *
1134pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
1135 pldm_entity *entity)
1136{
1137 assert(tree != NULL);
1138
1139 pldm_entity_node *node = NULL;
1140 entity_association_tree_find(tree->root, entity, &node);
1141 return node;
1142}
1143
1144static void entity_association_tree_copy(pldm_entity_node *org_node,
1145 pldm_entity_node **new_node)
1146{
1147 if (org_node == NULL) {
1148 return;
1149 }
1150 *new_node = malloc(sizeof(pldm_entity_node));
1151 (*new_node)->parent = org_node->parent;
1152 (*new_node)->entity = org_node->entity;
1153 (*new_node)->association_type = org_node->association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -06001154 (*new_node)->remote_container_id = org_node->remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301155 (*new_node)->first_child = NULL;
1156 (*new_node)->next_sibling = NULL;
1157 entity_association_tree_copy(org_node->first_child,
1158 &((*new_node)->first_child));
1159 entity_association_tree_copy(org_node->next_sibling,
1160 &((*new_node)->next_sibling));
1161}
1162
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301163LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301164void pldm_entity_association_tree_copy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301165 pldm_entity_association_tree *org_tree,
1166 pldm_entity_association_tree *new_tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301167{
1168 new_tree->last_used_container_id = org_tree->last_used_container_id;
1169 entity_association_tree_copy(org_tree->root, &(new_tree->root));
1170}
1171
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301172LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301173void pldm_entity_association_tree_destroy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301174 pldm_entity_association_tree *tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301175{
1176 assert(tree != NULL);
1177 entity_association_tree_destroy(tree->root);
1178 tree->last_used_container_id = 0;
1179 tree->root = NULL;
1180}
1181
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301182LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301183bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree)
1184{
1185 return ((tree->root == NULL) ? true : false);
1186}
1187
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301188LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301189void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len,
1190 size_t *num_entities,
1191 pldm_entity **entities)
1192{
1193 assert(pdr != NULL);
1194 assert(pdr_len >= sizeof(struct pldm_pdr_hdr) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301195 sizeof(struct pldm_pdr_entity_association));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301196
1197 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
1198 assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION);
1199
1200 const uint8_t *start = (uint8_t *)pdr;
1201 const uint8_t *end =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301202 start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301203 start += sizeof(struct pldm_pdr_hdr);
1204 struct pldm_pdr_entity_association *entity_association_pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301205 (struct pldm_pdr_entity_association *)start;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301206 *num_entities = entity_association_pdr->num_children + 1;
1207 assert(*num_entities >= 2);
1208 *entities = malloc(sizeof(pldm_entity) * *num_entities);
1209 assert(*entities != NULL);
1210 assert(start + sizeof(struct pldm_pdr_entity_association) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301211 sizeof(pldm_entity) * (*num_entities - 2) ==
Andrew Jeffery9c766792022-08-10 23:12:49 +09301212 end);
1213 (*entities)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301214 le16toh(entity_association_pdr->container.entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301215 (*entities)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301216 le16toh(entity_association_pdr->container.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301217 (*entities)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301218 le16toh(entity_association_pdr->container.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301219 pldm_entity *curr_entity = entity_association_pdr->children;
1220 size_t i = 1;
1221 while (i < *num_entities) {
1222 (*entities + i)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301223 le16toh(curr_entity->entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301224 (*entities + i)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301225 le16toh(curr_entity->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301226 (*entities + i)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301227 le16toh(curr_entity->entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301228 ++curr_entity;
1229 ++i;
1230 }
1231}