blob: 3cd0f3a1576f201ccecfa396b8c976e072e17000 [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));
Andrew Jefferya8bb22e2023-06-30 12:01:12 +0930120 if (!repo) {
121 return NULL;
122 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930123 repo->record_count = 0;
124 repo->size = 0;
125 repo->first = NULL;
126 repo->last = NULL;
127
128 return repo;
129}
130
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930131LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930132void pldm_pdr_destroy(pldm_pdr *repo)
133{
Andrew Jefferyfca1b602023-06-30 12:29:25 +0930134 if (!repo) {
135 return;
136 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930137
138 pldm_pdr_record *record = repo->first;
139 while (record != NULL) {
140 pldm_pdr_record *next = record->next;
141 if (record->data) {
142 free(record->data);
143 record->data = NULL;
144 }
145 free(record);
146 record = next;
147 }
148 free(repo);
149}
150
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930151LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930152const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo,
153 uint32_t record_handle,
154 uint8_t **data, uint32_t *size,
155 uint32_t *next_record_handle)
156{
157 assert(repo != NULL);
158 assert(data != NULL);
159 assert(size != NULL);
160 assert(next_record_handle != NULL);
Andrew Jeffery68b51302023-06-28 21:29:42 +0930161 if (!repo || !data || !size || !next_record_handle) {
162 return NULL;
163 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930164
165 if (!record_handle && (repo->first != NULL)) {
166 record_handle = repo->first->record_handle;
167 }
Andrew Jeffery68b51302023-06-28 21:29:42 +0930168
Andrew Jeffery9c766792022-08-10 23:12:49 +0930169 pldm_pdr_record *record = repo->first;
170 while (record != NULL) {
171 if (record->record_handle == record_handle) {
172 *size = record->size;
173 *data = record->data;
174 *next_record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930175 get_next_record_handle(repo, record);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930176 return record;
177 }
178 record = record->next;
179 }
180
181 *size = 0;
182 *next_record_handle = 0;
183 return NULL;
184}
185
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930186LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930187const pldm_pdr_record *
188pldm_pdr_get_next_record(const pldm_pdr *repo,
189 const pldm_pdr_record *curr_record, uint8_t **data,
190 uint32_t *size, uint32_t *next_record_handle)
191{
192 assert(repo != NULL);
193 assert(curr_record != NULL);
194 assert(data != NULL);
195 assert(size != NULL);
196 assert(next_record_handle != NULL);
Andrew Jeffery68b51302023-06-28 21:29:42 +0930197 if (!repo || !curr_record || !data || !size || !next_record_handle) {
198 return NULL;
199 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930200
201 if (curr_record == repo->last) {
202 *data = NULL;
203 *size = 0;
204 *next_record_handle = get_next_record_handle(repo, curr_record);
205 return NULL;
206 }
207
208 *next_record_handle = get_next_record_handle(repo, curr_record->next);
209 *data = curr_record->next->data;
210 *size = curr_record->next->size;
211 return curr_record->next;
212}
213
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930214LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930215const pldm_pdr_record *
216pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type,
217 const pldm_pdr_record *curr_record, uint8_t **data,
218 uint32_t *size)
219{
220 assert(repo != NULL);
Andrew Jefferyf85eeba2023-06-30 12:38:40 +0930221 if (!repo) {
222 return NULL;
223 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930224
225 pldm_pdr_record *record = repo->first;
226 if (curr_record != NULL) {
227 record = curr_record->next;
228 }
229 while (record != NULL) {
230 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data;
231 if (hdr->type == pdr_type) {
232 if (data && size) {
233 *size = record->size;
234 *data = record->data;
235 }
236 return record;
237 }
238 record = record->next;
239 }
240
241 if (size) {
242 *size = 0;
243 }
244 return NULL;
245}
246
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930247LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930248uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo)
249{
250 assert(repo != NULL);
251
252 return repo->record_count;
253}
254
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930255LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930256uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo)
257{
258 assert(repo != NULL);
259
260 return repo->size;
261}
262
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930263LIBPLDM_ABI_STABLE
Andrew Jeffery5565fcd2023-06-30 13:21:32 +0930264uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo
265 __attribute__((unused)),
Andrew Jeffery9c766792022-08-10 23:12:49 +0930266 const pldm_pdr_record *record)
267{
268 assert(repo != NULL);
269 assert(record != NULL);
270
271 return record->record_handle;
272}
273
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930274LIBPLDM_ABI_STABLE
275bool pldm_pdr_record_is_remote(const pldm_pdr_record *record)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930276{
277 assert(record != NULL);
278
279 return record->is_remote;
280}
281
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930282LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930283uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle,
284 uint16_t fru_rsi, uint16_t entity_type,
285 uint16_t entity_instance_num,
286 uint16_t container_id,
287 uint32_t bmc_record_handle)
288{
289 uint32_t size = sizeof(struct pldm_pdr_hdr) +
290 sizeof(struct pldm_pdr_fru_record_set);
291 uint8_t data[size];
292
293 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data;
294 hdr->version = 1;
295 hdr->record_handle = bmc_record_handle;
296 hdr->type = PLDM_PDR_FRU_RECORD_SET;
297 hdr->record_change_num = 0;
298 hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set));
299 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930300 (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr +
301 sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930302 fru->terminus_handle = htole16(terminus_handle);
303 fru->fru_rsi = htole16(fru_rsi);
304 fru->entity_type = htole16(entity_type);
305 fru->entity_instance_num = htole16(entity_instance_num);
306 fru->container_id = htole16(container_id);
307
308 return pldm_pdr_add(repo, data, size, bmc_record_handle, false,
309 terminus_handle);
310}
311
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930312LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930313const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930314 const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle,
315 uint16_t *entity_type, uint16_t *entity_instance_num,
316 uint16_t *container_id)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930317{
318 assert(terminus_handle != NULL);
319 assert(entity_type != NULL);
320 assert(entity_instance_num != NULL);
321 assert(container_id != NULL);
322
323 uint8_t *data = NULL;
324 uint32_t size = 0;
325 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930326 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930327 while (curr_record != NULL) {
328 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930329 (struct pldm_pdr_fru_record_set
330 *)(data + sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930331 if (fru->fru_rsi == htole16(fru_rsi)) {
332 *terminus_handle = le16toh(fru->terminus_handle);
333 *entity_type = le16toh(fru->entity_type);
334 *entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930335 le16toh(fru->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930336 *container_id = le16toh(fru->container_id);
337 return curr_record;
338 }
339 data = NULL;
340 curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930341 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data,
342 &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930343 }
344
345 *terminus_handle = 0;
346 *entity_type = 0;
347 *entity_instance_num = 0;
348 *container_id = 0;
349
350 return NULL;
351}
352
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930353LIBPLDM_ABI_STABLE
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930354/* NOLINTNEXTLINE(readability-identifier-naming) */
355void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle,
356 uint8_t tid, uint8_t tl_eid, bool valid_bit)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930357{
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930358 uint8_t *out_data = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930359 uint32_t size = 0;
360 const pldm_pdr_record *record;
361 record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR,
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930362 NULL, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930363
364 do {
365 if (record != NULL) {
366 struct pldm_terminus_locator_pdr *pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930367 (struct pldm_terminus_locator_pdr *)out_data;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930368 struct pldm_terminus_locator_type_mctp_eid *value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930369 (struct pldm_terminus_locator_type_mctp_eid *)
370 pdr->terminus_locator_value;
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930371 if (pdr->terminus_handle == terminus_handle &&
372 pdr->tid == tid && value->eid == tl_eid) {
373 pdr->validity = valid_bit;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930374 break;
375 }
376 }
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930377 record = pldm_pdr_find_record_by_type(repo,
378 PLDM_TERMINUS_LOCATOR_PDR,
379 record, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930380 } while (record);
381}
382
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500383static bool pldm_record_handle_in_range(uint32_t record_handle,
384 uint32_t first_record_handle,
385 uint32_t last_record_handle)
386{
387 return record_handle >= first_record_handle &&
388 record_handle <= last_record_handle;
389}
390
391LIBPLDM_ABI_TESTING
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500392int pldm_pdr_find_child_container_id_index_range_exclude(
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500393 const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance,
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500394 uint8_t child_index, uint32_t range_exclude_start_handle,
395 uint32_t range_exclude_end_handle, uint16_t *container_id)
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500396{
397 pldm_pdr_record *record;
398 if (!repo) {
399 return -EINVAL;
400 }
401
402 for (record = repo->first; record; record = record->next) {
403 bool is_container_entity_instance_number;
404 struct pldm_pdr_entity_association *pdr;
405 bool is_container_entity_type;
406 struct pldm_entity *child;
407 struct pldm_pdr_hdr *hdr;
408 bool in_range;
409
410 // pldm_pdr_add() takes only uint8_t* data as an argument.
411 // The expectation here is the pldm_pdr_hdr is the first field of the record data
412 hdr = (struct pldm_pdr_hdr *)record->data;
413 if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
414 continue;
415 }
416 in_range = pldm_record_handle_in_range(
417 record->record_handle, range_exclude_start_handle,
418 range_exclude_end_handle);
419 if (in_range) {
420 continue;
421 }
422
423 // this cast is valid with respect to alignment because
424 // struct pldm_pdr_hdr is declared with __attribute__((packed))
425 pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr));
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500426 if (child_index >= pdr->num_children) {
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500427 continue;
428 }
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500429
430 child = (&pdr->children[child_index]);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500431 is_container_entity_type = pdr->container.entity_type ==
432 entity_type;
433 is_container_entity_instance_number =
434 pdr->container.entity_instance_num == entity_instance;
435 if (is_container_entity_type &&
436 is_container_entity_instance_number) {
437 *container_id = le16toh(child->entity_container_id);
Pavithra Barithayaffd53422023-06-23 23:20:48 -0500438 return 0;
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500439 }
440 }
441 return -ENOKEY;
442}
443
Andrew Jeffery9c766792022-08-10 23:12:49 +0930444typedef struct pldm_entity_association_tree {
445 pldm_entity_node *root;
446 uint16_t last_used_container_id;
447} pldm_entity_association_tree;
448
449typedef struct pldm_entity_node {
450 pldm_entity entity;
451 pldm_entity parent;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600452 uint16_t remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930453 pldm_entity_node *first_child;
454 pldm_entity_node *next_sibling;
455 uint8_t association_type;
456} pldm_entity_node;
457
458static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
459{
460 assert(tree != NULL);
461 assert(tree->last_used_container_id != UINT16_MAX);
462
463 return ++tree->last_used_container_id;
464}
465
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930466LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930467pldm_entity pldm_entity_extract(pldm_entity_node *node)
468{
469 assert(node != NULL);
470
471 return node->entity;
472}
473
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600474LIBPLDM_ABI_TESTING
475int pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity,
476 uint16_t *cid)
477{
478 if (!entity) {
479 return -EINVAL;
480 }
481
482 *cid = entity->remote_container_id;
483 return 0;
484}
485
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930486LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930487pldm_entity_association_tree *pldm_entity_association_tree_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930488{
489 pldm_entity_association_tree *tree =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930490 malloc(sizeof(pldm_entity_association_tree));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930491 assert(tree != NULL);
492 tree->root = NULL;
493 tree->last_used_container_id = 0;
494
495 return tree;
496}
497
498static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
499 uint16_t entity_type)
500{
501 assert(start != NULL);
502
503 /* Insert after the the last node that matches the input entity type, or
504 * at the end if no such match occurrs
505 */
506 while (start->next_sibling != NULL) {
507 uint16_t this_type = start->entity.entity_type;
508 pldm_entity_node *next = start->next_sibling;
509 if (this_type == entity_type &&
510 (this_type != next->entity.entity_type)) {
511 break;
512 }
513 start = start->next_sibling;
514 }
515
516 return start;
517}
518
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930519LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930520pldm_entity_node *pldm_entity_association_tree_add(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930521 pldm_entity_association_tree *tree, pldm_entity *entity,
522 uint16_t entity_instance_number, pldm_entity_node *parent,
523 uint8_t association_type)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930524{
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500525 return pldm_entity_association_tree_add_entity(tree, entity,
526 entity_instance_number,
527 parent, association_type,
528 false, true, 0xFFFF);
529}
530
531LIBPLDM_ABI_TESTING
532pldm_entity_node *pldm_entity_association_tree_add_entity(
533 pldm_entity_association_tree *tree, pldm_entity *entity,
534 uint16_t entity_instance_number, pldm_entity_node *parent,
535 uint8_t association_type, bool is_remote, bool is_update_container_id,
536 uint16_t container_id)
537{
538 if ((!tree) || (!entity)) {
539 return NULL;
540 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930541
542 if (entity_instance_number != 0xFFFF && parent != NULL) {
543 pldm_entity node;
544 node.entity_type = entity->entity_type;
545 node.entity_instance_num = entity_instance_number;
546 if (pldm_is_current_parent_child(parent, &node)) {
547 return NULL;
548 }
549 }
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500550 if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL &&
551 association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) {
552 return NULL;
553 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930554 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500555 if (!node) {
556 return NULL;
557 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930558 node->first_child = NULL;
559 node->next_sibling = NULL;
560 node->parent.entity_type = 0;
561 node->parent.entity_instance_num = 0;
562 node->parent.entity_container_id = 0;
563 node->entity.entity_type = entity->entity_type;
564 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930565 entity_instance_number != 0xFFFF ? entity_instance_number : 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930566 node->association_type = association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600567 node->remote_container_id = 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930568 if (tree->root == NULL) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500569 if (parent != NULL) {
570 free(node);
571 return NULL;
572 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930573 tree->root = node;
574 /* container_id 0 here indicates this is the top-most entry */
575 node->entity.entity_container_id = 0;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600576 node->remote_container_id = node->entity.entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930577 } else if (parent != NULL && parent->first_child == NULL) {
578 parent->first_child = node;
579 node->parent = parent->entity;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500580
581 if (is_remote) {
582 node->remote_container_id = entity->entity_container_id;
583 }
584 if (is_update_container_id) {
585 if (container_id != 0xFFFF) {
586 node->entity.entity_container_id = container_id;
587 } else {
588 node->entity.entity_container_id =
589 next_container_id(tree);
590 }
591 } else {
592 node->entity.entity_container_id =
593 entity->entity_container_id;
594 }
595
596 if (!is_remote) {
597 node->remote_container_id =
598 node->entity.entity_container_id;
599 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930600 } else {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930601 pldm_entity_node *start = parent == NULL ? tree->root :
602 parent->first_child;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930603 pldm_entity_node *prev =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930604 find_insertion_at(start, entity->entity_type);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500605 if (!prev) {
606 free(node);
607 return NULL;
608 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930609 pldm_entity_node *next = prev->next_sibling;
610 if (prev->entity.entity_type == entity->entity_type) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500611 if (prev->entity.entity_instance_num == UINT16_MAX) {
612 free(node);
613 return NULL;
614 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930615 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930616 entity_instance_number != 0xFFFF ?
617 entity_instance_number :
618 prev->entity.entity_instance_num + 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930619 }
620 prev->next_sibling = node;
621 node->parent = prev->parent;
622 node->next_sibling = next;
623 node->entity.entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930624 prev->entity.entity_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600625 node->remote_container_id = entity->entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930626 }
627 entity->entity_instance_num = node->entity.entity_instance_num;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500628 if (is_update_container_id) {
629 entity->entity_container_id = node->entity.entity_container_id;
630 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930631 return node;
632}
633
634static void get_num_nodes(pldm_entity_node *node, size_t *num)
635{
636 if (node == NULL) {
637 return;
638 }
639
640 ++(*num);
641 get_num_nodes(node->next_sibling, num);
642 get_num_nodes(node->first_child, num);
643}
644
645static void entity_association_tree_visit(pldm_entity_node *node,
646 pldm_entity *entities, size_t *index)
647{
648 if (node == NULL) {
649 return;
650 }
651
652 pldm_entity *entity = &entities[*index];
653 ++(*index);
654 entity->entity_type = node->entity.entity_type;
655 entity->entity_instance_num = node->entity.entity_instance_num;
656 entity->entity_container_id = node->entity.entity_container_id;
657
658 entity_association_tree_visit(node->next_sibling, entities, index);
659 entity_association_tree_visit(node->first_child, entities, index);
660}
661
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930662LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930663void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
664 pldm_entity **entities, size_t *size)
665{
666 assert(tree != NULL);
667
668 *size = 0;
669 if (tree->root == NULL) {
670 return;
671 }
672
673 get_num_nodes(tree->root, size);
674 *entities = malloc(*size * sizeof(pldm_entity));
675 size_t index = 0;
676 entity_association_tree_visit(tree->root, *entities, &index);
677}
678
679static 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 Jeffery9d2a1c62023-06-05 13:02:16 +0930690LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930691void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
692{
693 assert(tree != NULL);
694
695 entity_association_tree_destroy(tree->root);
696 free(tree);
697}
698
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930699LIBPLDM_ABI_STABLE
700bool pldm_entity_is_node_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930701{
702 assert(node != NULL);
703
704 return node->first_child != NULL;
705}
706
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930707LIBPLDM_ABI_STABLE
708pldm_entity pldm_entity_get_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930709{
710 assert(node != NULL);
711
712 return node->parent;
713}
714
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930715LIBPLDM_ABI_STABLE
716bool pldm_entity_is_exist_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930717{
718 assert(node != NULL);
719
720 if (node->parent.entity_type == 0 &&
721 node->parent.entity_instance_num == 0 &&
722 node->parent.entity_container_id == 0) {
723 return false;
724 }
725
726 return true;
727}
728
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930729LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930730uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
731 uint8_t association_type)
732{
733 assert(node != NULL);
734 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
735 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
736
737 size_t count = 0;
738 pldm_entity_node *curr = node->first_child;
739 while (curr != NULL) {
740 if (curr->association_type == association_type) {
741 ++count;
742 }
743 curr = curr->next_sibling;
744 }
745
746 assert(count < UINT8_MAX);
747 return count;
748}
749
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930750LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930751bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node)
752{
753 assert(parent != NULL);
754 assert(node != NULL);
755
756 pldm_entity_node *curr = parent->first_child;
757 while (curr != NULL) {
758 if (node->entity_type == curr->entity.entity_type &&
759 node->entity_instance_num ==
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930760 curr->entity.entity_instance_num) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930761 return true;
762 }
763 curr = curr->next_sibling;
764 }
765
766 return false;
767}
768
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500769static void entity_association_pdr_add_children(
770 pldm_entity_node *curr, pldm_pdr *repo, uint16_t size,
771 uint8_t contained_count, uint8_t association_type, bool is_remote,
772 uint16_t terminus_handle, uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930773{
774 uint8_t pdr[size];
775 uint8_t *start = pdr;
776
777 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start;
778 hdr->version = 1;
779 hdr->record_handle = 0;
780 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
781 hdr->record_change_num = 0;
782 hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr));
783 start += sizeof(struct pldm_pdr_hdr);
784
785 uint16_t *container_id = (uint16_t *)start;
786 *container_id = htole16(curr->first_child->entity.entity_container_id);
787 start += sizeof(uint16_t);
788 *start = association_type;
789 start += sizeof(uint8_t);
790
791 pldm_entity *entity = (pldm_entity *)start;
792 entity->entity_type = htole16(curr->entity.entity_type);
793 entity->entity_instance_num = htole16(curr->entity.entity_instance_num);
794 entity->entity_container_id = htole16(curr->entity.entity_container_id);
795 start += sizeof(pldm_entity);
796
797 *start = contained_count;
798 start += sizeof(uint8_t);
799
800 pldm_entity_node *node = curr->first_child;
801 while (node != NULL) {
802 if (node->association_type == association_type) {
803 pldm_entity *entity = (pldm_entity *)start;
804 entity->entity_type = htole16(node->entity.entity_type);
805 entity->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930806 htole16(node->entity.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930807 entity->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930808 htole16(node->entity.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930809 start += sizeof(pldm_entity);
810 }
811 node = node->next_sibling;
812 }
813
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500814 pldm_pdr_add(repo, pdr, size, record_handle, is_remote,
815 terminus_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930816}
817
818static void entity_association_pdr_add_entry(pldm_entity_node *curr,
819 pldm_pdr *repo, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500820 uint16_t terminus_handle,
821 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930822{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930823 uint8_t num_logical_children = pldm_entity_get_num_children(
824 curr, PLDM_ENTITY_ASSOCIAION_LOGICAL);
825 uint8_t num_physical_children = pldm_entity_get_num_children(
826 curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930827
828 if (num_logical_children) {
829 uint16_t logical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930830 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
831 sizeof(uint8_t) + sizeof(pldm_entity) +
832 sizeof(uint8_t) +
833 (num_logical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930834 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930835 curr, repo, logical_pdr_size, num_logical_children,
836 PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500837 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930838 }
839
840 if (num_physical_children) {
841 uint16_t physical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930842 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
843 sizeof(uint8_t) + sizeof(pldm_entity) +
844 sizeof(uint8_t) +
845 (num_physical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930846 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930847 curr, repo, physical_pdr_size, num_physical_children,
848 PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500849 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930850 }
851}
852
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930853LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930854bool is_present(pldm_entity entity, pldm_entity **entities, size_t num_entities)
855{
856 if (entities == NULL || num_entities == 0) {
857 return true;
858 }
859 size_t i = 0;
860 while (i < num_entities) {
861 if ((*entities + i)->entity_type == entity.entity_type) {
862 return true;
863 }
864 i++;
865 }
866 return false;
867}
868
869static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo,
870 pldm_entity **entities,
871 size_t num_entities, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500872 uint16_t terminus_handle,
873 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930874{
875 if (curr == NULL) {
876 return;
877 }
878 bool to_add = true;
879 to_add = is_present(curr->entity, entities, num_entities);
880 if (to_add) {
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500881 entity_association_pdr_add_entry(
882 curr, repo, is_remote, terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930883 }
884 entity_association_pdr_add(curr->next_sibling, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500885 num_entities, is_remote, terminus_handle,
886 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930887 entity_association_pdr_add(curr->first_child, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500888 num_entities, is_remote, terminus_handle,
889 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930890}
891
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930892LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930893void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree,
894 pldm_pdr *repo, bool is_remote,
895 uint16_t terminus_handle)
896{
897 assert(tree != NULL);
898 assert(repo != NULL);
899
900 entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500901 terminus_handle, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930902}
903
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930904LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930905void pldm_entity_association_pdr_add_from_node(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930906 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
907 size_t num_entities, bool is_remote, uint16_t terminus_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930908{
909 assert(repo != NULL);
910
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500911 pldm_entity_association_pdr_add_from_node_with_record_handle(
912 node, repo, entities, num_entities, is_remote, terminus_handle,
913 0);
914}
915
916LIBPLDM_ABI_TESTING
917int pldm_entity_association_pdr_add_from_node_with_record_handle(
918 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
919 size_t num_entities, bool is_remote, uint16_t terminus_handle,
920 uint32_t record_handle)
921{
922 if (!node || !repo || !entities) {
923 return -EINVAL;
924 }
925
Andrew Jeffery9c766792022-08-10 23:12:49 +0930926 entity_association_pdr_add(node, repo, entities, num_entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500927 is_remote, terminus_handle, record_handle);
928
929 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930930}
931
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930932LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930933void find_entity_ref_in_tree(pldm_entity_node *tree_node, pldm_entity entity,
934 pldm_entity_node **node)
935{
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600936 bool is_entity_container_id;
937 bool is_entity_instance_num;
938 bool is_type;
939
Andrew Jeffery9c766792022-08-10 23:12:49 +0930940 if (tree_node == NULL) {
941 return;
942 }
943
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600944 is_type = tree_node->entity.entity_type == entity.entity_type;
945 is_entity_instance_num = tree_node->entity.entity_instance_num ==
946 entity.entity_instance_num;
947 is_entity_container_id = tree_node->entity.entity_container_id ==
948 entity.entity_container_id;
949
950 if (is_type && is_entity_instance_num && is_entity_container_id) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930951 *node = tree_node;
952 return;
953 }
954
955 find_entity_ref_in_tree(tree_node->first_child, entity, node);
956 find_entity_ref_in_tree(tree_node->next_sibling, entity, node);
957}
958
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930959LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930960void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree,
961 pldm_entity entity, pldm_entity_node **node)
962{
963 assert(tree != NULL);
964 find_entity_ref_in_tree(tree->root, entity, node);
965}
966
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930967LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930968void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo,
969 uint16_t terminus_handle)
970{
971 assert(repo != NULL);
972 bool removed = false;
973
974 pldm_pdr_record *record = repo->first;
975 pldm_pdr_record *prev = NULL;
976 while (record != NULL) {
977 pldm_pdr_record *next = record->next;
978 if (record->terminus_handle == terminus_handle) {
979 if (repo->first == record) {
980 repo->first = next;
981 } else {
982 prev->next = next;
983 }
984 if (repo->last == record) {
985 repo->last = prev;
986 }
987 if (record->data) {
988 free(record->data);
989 }
990 --repo->record_count;
991 repo->size -= record->size;
992 free(record);
993 removed = true;
994 } else {
995 prev = record;
996 }
997 record = next;
998 }
999
1000 if (removed == true) {
1001 record = repo->first;
1002 uint32_t record_handle = 0;
1003 while (record != NULL) {
1004 record->record_handle = ++record_handle;
1005 if (record->data != NULL) {
1006 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301007 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301008 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301009 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301010 }
1011 record = record->next;
1012 }
1013 }
1014}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301015
1016LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301017void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo)
1018{
1019 assert(repo != NULL);
1020 bool removed = false;
1021
1022 pldm_pdr_record *record = repo->first;
1023 pldm_pdr_record *prev = NULL;
1024 while (record != NULL) {
1025 pldm_pdr_record *next = record->next;
1026 if (record->is_remote == true) {
1027 if (repo->first == record) {
1028 repo->first = next;
1029 } else {
1030 prev->next = next;
1031 }
1032 if (repo->last == record) {
1033 repo->last = prev;
1034 }
1035 if (record->data) {
1036 free(record->data);
1037 }
1038 --repo->record_count;
1039 repo->size -= record->size;
1040 free(record);
1041 removed = true;
1042 } else {
1043 prev = record;
1044 }
1045 record = next;
1046 }
1047
1048 if (removed == true) {
1049 record = repo->first;
1050 uint32_t record_handle = 0;
1051 while (record != NULL) {
1052 record->record_handle = ++record_handle;
1053 if (record->data != NULL) {
1054 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301055 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301056 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301057 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301058 }
1059 record = record->next;
1060 }
1061 }
1062}
1063
Pavithra Barithaya4d694342023-05-19 08:04:41 -05001064LIBPLDM_ABI_TESTING
1065pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo,
1066 uint32_t first, uint32_t last)
1067{
1068 pldm_pdr_record *record = NULL;
1069 pldm_pdr_record *curr;
1070
1071 if (!repo) {
1072 return NULL;
1073 }
1074 for (curr = repo->first; curr; curr = curr->next) {
1075 if (first > curr->record_handle || last < curr->record_handle) {
1076 continue;
1077 }
1078 if (!record || curr->record_handle > record->record_handle) {
1079 record = curr;
1080 }
1081 }
1082
1083 return record;
1084}
1085
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001086static void entity_association_tree_find_if_remote(pldm_entity_node *node,
1087 pldm_entity *entity,
1088 pldm_entity_node **out,
1089 bool is_remote)
1090{
1091 assert(out != NULL && *out == NULL);
1092 if (node == NULL) {
1093 return;
1094 }
1095 bool is_entity_type;
1096 bool is_entity_instance_num;
1097
1098 is_entity_type = node->entity.entity_type == entity->entity_type;
1099 is_entity_instance_num = node->entity.entity_instance_num ==
1100 entity->entity_instance_num;
1101
1102 if (!is_remote ||
1103 node->remote_container_id == entity->entity_container_id) {
1104 if (is_entity_type && is_entity_instance_num) {
1105 entity->entity_container_id =
1106 node->entity.entity_container_id;
1107 *out = node;
1108 return;
1109 }
1110 }
1111 entity_association_tree_find_if_remote(node->next_sibling, entity, out,
1112 is_remote);
1113 entity_association_tree_find_if_remote(node->first_child, entity, out,
1114 is_remote);
1115}
1116
1117LIBPLDM_ABI_TESTING
1118pldm_entity_node *
1119pldm_entity_association_tree_find_if_remote(pldm_entity_association_tree *tree,
1120 pldm_entity *entity, bool is_remote)
1121{
1122 if (!tree || !entity) {
1123 return NULL;
1124 }
1125 pldm_entity_node *node = NULL;
1126 entity_association_tree_find_if_remote(tree->root, entity, &node,
1127 is_remote);
1128 return node;
1129}
1130
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301131LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301132void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity,
1133 pldm_entity_node **out)
1134{
1135 if (node == NULL) {
1136 return;
1137 }
1138
1139 if (node->entity.entity_type == entity->entity_type &&
1140 node->entity.entity_instance_num == entity->entity_instance_num) {
1141 entity->entity_container_id = node->entity.entity_container_id;
1142 *out = node;
1143 return;
1144 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301145 entity_association_tree_find(node->next_sibling, entity, out);
1146 entity_association_tree_find(node->first_child, entity, out);
1147}
1148
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301149LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301150pldm_entity_node *
1151pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
1152 pldm_entity *entity)
1153{
1154 assert(tree != NULL);
1155
1156 pldm_entity_node *node = NULL;
1157 entity_association_tree_find(tree->root, entity, &node);
1158 return node;
1159}
1160
1161static void entity_association_tree_copy(pldm_entity_node *org_node,
1162 pldm_entity_node **new_node)
1163{
1164 if (org_node == NULL) {
1165 return;
1166 }
1167 *new_node = malloc(sizeof(pldm_entity_node));
1168 (*new_node)->parent = org_node->parent;
1169 (*new_node)->entity = org_node->entity;
1170 (*new_node)->association_type = org_node->association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -06001171 (*new_node)->remote_container_id = org_node->remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301172 (*new_node)->first_child = NULL;
1173 (*new_node)->next_sibling = NULL;
1174 entity_association_tree_copy(org_node->first_child,
1175 &((*new_node)->first_child));
1176 entity_association_tree_copy(org_node->next_sibling,
1177 &((*new_node)->next_sibling));
1178}
1179
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301180LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301181void pldm_entity_association_tree_copy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301182 pldm_entity_association_tree *org_tree,
1183 pldm_entity_association_tree *new_tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301184{
1185 new_tree->last_used_container_id = org_tree->last_used_container_id;
1186 entity_association_tree_copy(org_tree->root, &(new_tree->root));
1187}
1188
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301189LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301190void pldm_entity_association_tree_destroy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301191 pldm_entity_association_tree *tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301192{
1193 assert(tree != NULL);
1194 entity_association_tree_destroy(tree->root);
1195 tree->last_used_container_id = 0;
1196 tree->root = NULL;
1197}
1198
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301199LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301200bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree)
1201{
1202 return ((tree->root == NULL) ? true : false);
1203}
1204
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301205LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301206void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len,
1207 size_t *num_entities,
1208 pldm_entity **entities)
1209{
1210 assert(pdr != NULL);
1211 assert(pdr_len >= sizeof(struct pldm_pdr_hdr) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301212 sizeof(struct pldm_pdr_entity_association));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301213
1214 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
1215 assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION);
1216
1217 const uint8_t *start = (uint8_t *)pdr;
1218 const uint8_t *end =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301219 start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301220 start += sizeof(struct pldm_pdr_hdr);
1221 struct pldm_pdr_entity_association *entity_association_pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301222 (struct pldm_pdr_entity_association *)start;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301223 *num_entities = entity_association_pdr->num_children + 1;
1224 assert(*num_entities >= 2);
1225 *entities = malloc(sizeof(pldm_entity) * *num_entities);
1226 assert(*entities != NULL);
1227 assert(start + sizeof(struct pldm_pdr_entity_association) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301228 sizeof(pldm_entity) * (*num_entities - 2) ==
Andrew Jeffery9c766792022-08-10 23:12:49 +09301229 end);
1230 (*entities)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301231 le16toh(entity_association_pdr->container.entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301232 (*entities)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301233 le16toh(entity_association_pdr->container.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301234 (*entities)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301235 le16toh(entity_association_pdr->container.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301236 pldm_entity *curr_entity = entity_association_pdr->children;
1237 size_t i = 1;
1238 while (i < *num_entities) {
1239 (*entities + i)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301240 le16toh(curr_entity->entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301241 (*entities + i)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301242 le16toh(curr_entity->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301243 (*entities + i)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301244 le16toh(curr_entity->entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301245 ++curr_entity;
1246 ++i;
1247 }
1248}