blob: a58a2cc5fea61df2ef33f241b0f2e06c25342b8e [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);
Andrew Jeffery01425e92023-06-30 13:47:40 +0930322 if (!repo || !terminus_handle || !entity_type || !entity_instance_num ||
323 !container_id) {
324 return NULL;
325 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930326
327 uint8_t *data = NULL;
328 uint32_t size = 0;
329 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930330 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930331 while (curr_record != NULL) {
332 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930333 (struct pldm_pdr_fru_record_set
334 *)(data + sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930335 if (fru->fru_rsi == htole16(fru_rsi)) {
336 *terminus_handle = le16toh(fru->terminus_handle);
337 *entity_type = le16toh(fru->entity_type);
338 *entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930339 le16toh(fru->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930340 *container_id = le16toh(fru->container_id);
341 return curr_record;
342 }
343 data = NULL;
344 curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930345 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data,
346 &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930347 }
348
349 *terminus_handle = 0;
350 *entity_type = 0;
351 *entity_instance_num = 0;
352 *container_id = 0;
353
354 return NULL;
355}
356
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930357LIBPLDM_ABI_STABLE
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930358/* NOLINTNEXTLINE(readability-identifier-naming) */
359void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle,
360 uint8_t tid, uint8_t tl_eid, bool valid_bit)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930361{
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930362 uint8_t *out_data = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930363 uint32_t size = 0;
364 const pldm_pdr_record *record;
365 record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR,
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930366 NULL, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930367
368 do {
369 if (record != NULL) {
370 struct pldm_terminus_locator_pdr *pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930371 (struct pldm_terminus_locator_pdr *)out_data;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930372 struct pldm_terminus_locator_type_mctp_eid *value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930373 (struct pldm_terminus_locator_type_mctp_eid *)
374 pdr->terminus_locator_value;
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930375 if (pdr->terminus_handle == terminus_handle &&
376 pdr->tid == tid && value->eid == tl_eid) {
377 pdr->validity = valid_bit;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930378 break;
379 }
380 }
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930381 record = pldm_pdr_find_record_by_type(repo,
382 PLDM_TERMINUS_LOCATOR_PDR,
383 record, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930384 } while (record);
385}
386
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500387static bool pldm_record_handle_in_range(uint32_t record_handle,
388 uint32_t first_record_handle,
389 uint32_t last_record_handle)
390{
391 return record_handle >= first_record_handle &&
392 record_handle <= last_record_handle;
393}
394
395LIBPLDM_ABI_TESTING
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500396int pldm_pdr_find_child_container_id_index_range_exclude(
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500397 const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance,
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500398 uint8_t child_index, uint32_t range_exclude_start_handle,
399 uint32_t range_exclude_end_handle, uint16_t *container_id)
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500400{
401 pldm_pdr_record *record;
402 if (!repo) {
403 return -EINVAL;
404 }
405
406 for (record = repo->first; record; record = record->next) {
407 bool is_container_entity_instance_number;
408 struct pldm_pdr_entity_association *pdr;
409 bool is_container_entity_type;
410 struct pldm_entity *child;
411 struct pldm_pdr_hdr *hdr;
412 bool in_range;
413
414 // pldm_pdr_add() takes only uint8_t* data as an argument.
415 // The expectation here is the pldm_pdr_hdr is the first field of the record data
416 hdr = (struct pldm_pdr_hdr *)record->data;
417 if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
418 continue;
419 }
420 in_range = pldm_record_handle_in_range(
421 record->record_handle, range_exclude_start_handle,
422 range_exclude_end_handle);
423 if (in_range) {
424 continue;
425 }
426
427 // this cast is valid with respect to alignment because
428 // struct pldm_pdr_hdr is declared with __attribute__((packed))
429 pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr));
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500430 if (child_index >= pdr->num_children) {
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500431 continue;
432 }
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500433
434 child = (&pdr->children[child_index]);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500435 is_container_entity_type = pdr->container.entity_type ==
436 entity_type;
437 is_container_entity_instance_number =
438 pdr->container.entity_instance_num == entity_instance;
439 if (is_container_entity_type &&
440 is_container_entity_instance_number) {
441 *container_id = le16toh(child->entity_container_id);
Pavithra Barithayaffd53422023-06-23 23:20:48 -0500442 return 0;
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500443 }
444 }
445 return -ENOKEY;
446}
447
Andrew Jeffery9c766792022-08-10 23:12:49 +0930448typedef struct pldm_entity_association_tree {
449 pldm_entity_node *root;
450 uint16_t last_used_container_id;
451} pldm_entity_association_tree;
452
453typedef struct pldm_entity_node {
454 pldm_entity entity;
455 pldm_entity parent;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600456 uint16_t remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930457 pldm_entity_node *first_child;
458 pldm_entity_node *next_sibling;
459 uint8_t association_type;
460} pldm_entity_node;
461
462static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
463{
464 assert(tree != NULL);
465 assert(tree->last_used_container_id != UINT16_MAX);
466
467 return ++tree->last_used_container_id;
468}
469
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930470LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930471pldm_entity pldm_entity_extract(pldm_entity_node *node)
472{
473 assert(node != NULL);
474
475 return node->entity;
476}
477
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600478LIBPLDM_ABI_TESTING
Andrew Jeffery15b88182023-06-30 13:29:17 +0930479uint16_t
480pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity)
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600481{
Andrew Jeffery15b88182023-06-30 13:29:17 +0930482 assert(entity != NULL);
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600483
Andrew Jeffery15b88182023-06-30 13:29:17 +0930484 return entity->remote_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600485}
486
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930487LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930488pldm_entity_association_tree *pldm_entity_association_tree_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930489{
490 pldm_entity_association_tree *tree =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930491 malloc(sizeof(pldm_entity_association_tree));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930492 assert(tree != NULL);
493 tree->root = NULL;
494 tree->last_used_container_id = 0;
495
496 return tree;
497}
498
499static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
500 uint16_t entity_type)
501{
502 assert(start != NULL);
503
504 /* Insert after the the last node that matches the input entity type, or
505 * at the end if no such match occurrs
506 */
507 while (start->next_sibling != NULL) {
508 uint16_t this_type = start->entity.entity_type;
509 pldm_entity_node *next = start->next_sibling;
510 if (this_type == entity_type &&
511 (this_type != next->entity.entity_type)) {
512 break;
513 }
514 start = start->next_sibling;
515 }
516
517 return start;
518}
519
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930520LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930521pldm_entity_node *pldm_entity_association_tree_add(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930522 pldm_entity_association_tree *tree, pldm_entity *entity,
523 uint16_t entity_instance_number, pldm_entity_node *parent,
524 uint8_t association_type)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930525{
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500526 return pldm_entity_association_tree_add_entity(tree, entity,
527 entity_instance_number,
528 parent, association_type,
529 false, true, 0xFFFF);
530}
531
532LIBPLDM_ABI_TESTING
533pldm_entity_node *pldm_entity_association_tree_add_entity(
534 pldm_entity_association_tree *tree, pldm_entity *entity,
535 uint16_t entity_instance_number, pldm_entity_node *parent,
536 uint8_t association_type, bool is_remote, bool is_update_container_id,
537 uint16_t container_id)
538{
539 if ((!tree) || (!entity)) {
540 return NULL;
541 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930542
543 if (entity_instance_number != 0xFFFF && parent != NULL) {
544 pldm_entity node;
545 node.entity_type = entity->entity_type;
546 node.entity_instance_num = entity_instance_number;
547 if (pldm_is_current_parent_child(parent, &node)) {
548 return NULL;
549 }
550 }
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500551 if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL &&
552 association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) {
553 return NULL;
554 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930555 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500556 if (!node) {
557 return NULL;
558 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930559 node->first_child = NULL;
560 node->next_sibling = NULL;
561 node->parent.entity_type = 0;
562 node->parent.entity_instance_num = 0;
563 node->parent.entity_container_id = 0;
564 node->entity.entity_type = entity->entity_type;
565 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930566 entity_instance_number != 0xFFFF ? entity_instance_number : 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930567 node->association_type = association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600568 node->remote_container_id = 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930569 if (tree->root == NULL) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500570 if (parent != NULL) {
571 free(node);
572 return NULL;
573 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930574 tree->root = node;
575 /* container_id 0 here indicates this is the top-most entry */
576 node->entity.entity_container_id = 0;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600577 node->remote_container_id = node->entity.entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930578 } else if (parent != NULL && parent->first_child == NULL) {
579 parent->first_child = node;
580 node->parent = parent->entity;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500581
582 if (is_remote) {
583 node->remote_container_id = entity->entity_container_id;
584 }
585 if (is_update_container_id) {
586 if (container_id != 0xFFFF) {
587 node->entity.entity_container_id = container_id;
588 } else {
589 node->entity.entity_container_id =
590 next_container_id(tree);
591 }
592 } else {
593 node->entity.entity_container_id =
594 entity->entity_container_id;
595 }
596
597 if (!is_remote) {
598 node->remote_container_id =
599 node->entity.entity_container_id;
600 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930601 } else {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930602 pldm_entity_node *start = parent == NULL ? tree->root :
603 parent->first_child;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930604 pldm_entity_node *prev =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930605 find_insertion_at(start, entity->entity_type);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500606 if (!prev) {
607 free(node);
608 return NULL;
609 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930610 pldm_entity_node *next = prev->next_sibling;
611 if (prev->entity.entity_type == entity->entity_type) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500612 if (prev->entity.entity_instance_num == UINT16_MAX) {
613 free(node);
614 return NULL;
615 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930616 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930617 entity_instance_number != 0xFFFF ?
618 entity_instance_number :
619 prev->entity.entity_instance_num + 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930620 }
621 prev->next_sibling = node;
622 node->parent = prev->parent;
623 node->next_sibling = next;
624 node->entity.entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930625 prev->entity.entity_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600626 node->remote_container_id = entity->entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930627 }
628 entity->entity_instance_num = node->entity.entity_instance_num;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500629 if (is_update_container_id) {
630 entity->entity_container_id = node->entity.entity_container_id;
631 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930632 return node;
633}
634
635static void get_num_nodes(pldm_entity_node *node, size_t *num)
636{
637 if (node == NULL) {
638 return;
639 }
640
641 ++(*num);
642 get_num_nodes(node->next_sibling, num);
643 get_num_nodes(node->first_child, num);
644}
645
646static void entity_association_tree_visit(pldm_entity_node *node,
647 pldm_entity *entities, size_t *index)
648{
649 if (node == NULL) {
650 return;
651 }
652
653 pldm_entity *entity = &entities[*index];
654 ++(*index);
655 entity->entity_type = node->entity.entity_type;
656 entity->entity_instance_num = node->entity.entity_instance_num;
657 entity->entity_container_id = node->entity.entity_container_id;
658
659 entity_association_tree_visit(node->next_sibling, entities, index);
660 entity_association_tree_visit(node->first_child, entities, index);
661}
662
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930663LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930664void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
665 pldm_entity **entities, size_t *size)
666{
667 assert(tree != NULL);
668
669 *size = 0;
670 if (tree->root == NULL) {
671 return;
672 }
673
674 get_num_nodes(tree->root, size);
675 *entities = malloc(*size * sizeof(pldm_entity));
676 size_t index = 0;
677 entity_association_tree_visit(tree->root, *entities, &index);
678}
679
680static void entity_association_tree_destroy(pldm_entity_node *node)
681{
682 if (node == NULL) {
683 return;
684 }
685
686 entity_association_tree_destroy(node->next_sibling);
687 entity_association_tree_destroy(node->first_child);
688 free(node);
689}
690
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930691LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930692void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
693{
694 assert(tree != NULL);
695
696 entity_association_tree_destroy(tree->root);
697 free(tree);
698}
699
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930700LIBPLDM_ABI_STABLE
701bool pldm_entity_is_node_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930702{
703 assert(node != NULL);
704
705 return node->first_child != NULL;
706}
707
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930708LIBPLDM_ABI_STABLE
709pldm_entity pldm_entity_get_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930710{
711 assert(node != NULL);
712
713 return node->parent;
714}
715
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930716LIBPLDM_ABI_STABLE
717bool pldm_entity_is_exist_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930718{
719 assert(node != NULL);
720
721 if (node->parent.entity_type == 0 &&
722 node->parent.entity_instance_num == 0 &&
723 node->parent.entity_container_id == 0) {
724 return false;
725 }
726
727 return true;
728}
729
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930730LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930731uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
732 uint8_t association_type)
733{
734 assert(node != NULL);
735 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
736 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
737
738 size_t count = 0;
739 pldm_entity_node *curr = node->first_child;
740 while (curr != NULL) {
741 if (curr->association_type == association_type) {
742 ++count;
743 }
744 curr = curr->next_sibling;
745 }
746
747 assert(count < UINT8_MAX);
748 return count;
749}
750
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930751LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930752bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node)
753{
754 assert(parent != NULL);
755 assert(node != NULL);
756
757 pldm_entity_node *curr = parent->first_child;
758 while (curr != NULL) {
759 if (node->entity_type == curr->entity.entity_type &&
760 node->entity_instance_num ==
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930761 curr->entity.entity_instance_num) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930762 return true;
763 }
764 curr = curr->next_sibling;
765 }
766
767 return false;
768}
769
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500770static void entity_association_pdr_add_children(
771 pldm_entity_node *curr, pldm_pdr *repo, uint16_t size,
772 uint8_t contained_count, uint8_t association_type, bool is_remote,
773 uint16_t terminus_handle, uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930774{
775 uint8_t pdr[size];
776 uint8_t *start = pdr;
777
778 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start;
779 hdr->version = 1;
780 hdr->record_handle = 0;
781 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
782 hdr->record_change_num = 0;
783 hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr));
784 start += sizeof(struct pldm_pdr_hdr);
785
786 uint16_t *container_id = (uint16_t *)start;
787 *container_id = htole16(curr->first_child->entity.entity_container_id);
788 start += sizeof(uint16_t);
789 *start = association_type;
790 start += sizeof(uint8_t);
791
792 pldm_entity *entity = (pldm_entity *)start;
793 entity->entity_type = htole16(curr->entity.entity_type);
794 entity->entity_instance_num = htole16(curr->entity.entity_instance_num);
795 entity->entity_container_id = htole16(curr->entity.entity_container_id);
796 start += sizeof(pldm_entity);
797
798 *start = contained_count;
799 start += sizeof(uint8_t);
800
801 pldm_entity_node *node = curr->first_child;
802 while (node != NULL) {
803 if (node->association_type == association_type) {
804 pldm_entity *entity = (pldm_entity *)start;
805 entity->entity_type = htole16(node->entity.entity_type);
806 entity->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930807 htole16(node->entity.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930808 entity->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930809 htole16(node->entity.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930810 start += sizeof(pldm_entity);
811 }
812 node = node->next_sibling;
813 }
814
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500815 pldm_pdr_add(repo, pdr, size, record_handle, is_remote,
816 terminus_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930817}
818
819static void entity_association_pdr_add_entry(pldm_entity_node *curr,
820 pldm_pdr *repo, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500821 uint16_t terminus_handle,
822 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930823{
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930824 uint8_t num_logical_children = pldm_entity_get_num_children(
825 curr, PLDM_ENTITY_ASSOCIAION_LOGICAL);
826 uint8_t num_physical_children = pldm_entity_get_num_children(
827 curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930828
829 if (num_logical_children) {
830 uint16_t logical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930831 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
832 sizeof(uint8_t) + sizeof(pldm_entity) +
833 sizeof(uint8_t) +
834 (num_logical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930835 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930836 curr, repo, logical_pdr_size, num_logical_children,
837 PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500838 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930839 }
840
841 if (num_physical_children) {
842 uint16_t physical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930843 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
844 sizeof(uint8_t) + sizeof(pldm_entity) +
845 sizeof(uint8_t) +
846 (num_physical_children * sizeof(pldm_entity));
Andrew Jeffery4edb7082023-04-05 19:09:52 +0930847 entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930848 curr, repo, physical_pdr_size, num_physical_children,
849 PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500850 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930851 }
852}
853
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930854LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930855bool is_present(pldm_entity entity, pldm_entity **entities, size_t num_entities)
856{
857 if (entities == NULL || num_entities == 0) {
858 return true;
859 }
860 size_t i = 0;
861 while (i < num_entities) {
862 if ((*entities + i)->entity_type == entity.entity_type) {
863 return true;
864 }
865 i++;
866 }
867 return false;
868}
869
870static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo,
871 pldm_entity **entities,
872 size_t num_entities, bool is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500873 uint16_t terminus_handle,
874 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930875{
876 if (curr == NULL) {
877 return;
878 }
879 bool to_add = true;
880 to_add = is_present(curr->entity, entities, num_entities);
881 if (to_add) {
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500882 entity_association_pdr_add_entry(
883 curr, repo, is_remote, terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930884 }
885 entity_association_pdr_add(curr->next_sibling, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500886 num_entities, is_remote, terminus_handle,
887 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930888 entity_association_pdr_add(curr->first_child, repo, entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500889 num_entities, is_remote, terminus_handle,
890 record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930891}
892
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930893LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930894void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree,
895 pldm_pdr *repo, bool is_remote,
896 uint16_t terminus_handle)
897{
898 assert(tree != NULL);
899 assert(repo != NULL);
900
901 entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500902 terminus_handle, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930903}
904
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930905LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930906void pldm_entity_association_pdr_add_from_node(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930907 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
908 size_t num_entities, bool is_remote, uint16_t terminus_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930909{
910 assert(repo != NULL);
911
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500912 pldm_entity_association_pdr_add_from_node_with_record_handle(
913 node, repo, entities, num_entities, is_remote, terminus_handle,
914 0);
915}
916
917LIBPLDM_ABI_TESTING
918int pldm_entity_association_pdr_add_from_node_with_record_handle(
919 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
920 size_t num_entities, bool is_remote, uint16_t terminus_handle,
921 uint32_t record_handle)
922{
923 if (!node || !repo || !entities) {
924 return -EINVAL;
925 }
926
Andrew Jeffery9c766792022-08-10 23:12:49 +0930927 entity_association_pdr_add(node, repo, entities, num_entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500928 is_remote, terminus_handle, record_handle);
929
930 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930931}
932
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930933LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930934void find_entity_ref_in_tree(pldm_entity_node *tree_node, pldm_entity entity,
935 pldm_entity_node **node)
936{
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600937 bool is_entity_container_id;
938 bool is_entity_instance_num;
939 bool is_type;
940
Andrew Jeffery9c766792022-08-10 23:12:49 +0930941 if (tree_node == NULL) {
942 return;
943 }
944
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600945 is_type = tree_node->entity.entity_type == entity.entity_type;
946 is_entity_instance_num = tree_node->entity.entity_instance_num ==
947 entity.entity_instance_num;
948 is_entity_container_id = tree_node->entity.entity_container_id ==
949 entity.entity_container_id;
950
951 if (is_type && is_entity_instance_num && is_entity_container_id) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930952 *node = tree_node;
953 return;
954 }
955
956 find_entity_ref_in_tree(tree_node->first_child, entity, node);
957 find_entity_ref_in_tree(tree_node->next_sibling, entity, node);
958}
959
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930960LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930961void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree,
962 pldm_entity entity, pldm_entity_node **node)
963{
964 assert(tree != NULL);
965 find_entity_ref_in_tree(tree->root, entity, node);
966}
967
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930968LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930969void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo,
970 uint16_t terminus_handle)
971{
972 assert(repo != NULL);
973 bool removed = false;
974
975 pldm_pdr_record *record = repo->first;
976 pldm_pdr_record *prev = NULL;
977 while (record != NULL) {
978 pldm_pdr_record *next = record->next;
979 if (record->terminus_handle == terminus_handle) {
980 if (repo->first == record) {
981 repo->first = next;
982 } else {
983 prev->next = next;
984 }
985 if (repo->last == record) {
986 repo->last = prev;
987 }
988 if (record->data) {
989 free(record->data);
990 }
991 --repo->record_count;
992 repo->size -= record->size;
993 free(record);
994 removed = true;
995 } else {
996 prev = record;
997 }
998 record = next;
999 }
1000
1001 if (removed == true) {
1002 record = repo->first;
1003 uint32_t record_handle = 0;
1004 while (record != NULL) {
1005 record->record_handle = ++record_handle;
1006 if (record->data != NULL) {
1007 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301008 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301009 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301010 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301011 }
1012 record = record->next;
1013 }
1014 }
1015}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301016
1017LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301018void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo)
1019{
1020 assert(repo != NULL);
1021 bool removed = false;
1022
1023 pldm_pdr_record *record = repo->first;
1024 pldm_pdr_record *prev = NULL;
1025 while (record != NULL) {
1026 pldm_pdr_record *next = record->next;
1027 if (record->is_remote == true) {
1028 if (repo->first == record) {
1029 repo->first = next;
1030 } else {
1031 prev->next = next;
1032 }
1033 if (repo->last == record) {
1034 repo->last = prev;
1035 }
1036 if (record->data) {
1037 free(record->data);
1038 }
1039 --repo->record_count;
1040 repo->size -= record->size;
1041 free(record);
1042 removed = true;
1043 } else {
1044 prev = record;
1045 }
1046 record = next;
1047 }
1048
1049 if (removed == true) {
1050 record = repo->first;
1051 uint32_t record_handle = 0;
1052 while (record != NULL) {
1053 record->record_handle = ++record_handle;
1054 if (record->data != NULL) {
1055 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301056 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301057 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301058 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301059 }
1060 record = record->next;
1061 }
1062 }
1063}
1064
Pavithra Barithaya4d694342023-05-19 08:04:41 -05001065LIBPLDM_ABI_TESTING
1066pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo,
1067 uint32_t first, uint32_t last)
1068{
1069 pldm_pdr_record *record = NULL;
1070 pldm_pdr_record *curr;
1071
1072 if (!repo) {
1073 return NULL;
1074 }
1075 for (curr = repo->first; curr; curr = curr->next) {
1076 if (first > curr->record_handle || last < curr->record_handle) {
1077 continue;
1078 }
1079 if (!record || curr->record_handle > record->record_handle) {
1080 record = curr;
1081 }
1082 }
1083
1084 return record;
1085}
1086
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001087static void entity_association_tree_find_if_remote(pldm_entity_node *node,
1088 pldm_entity *entity,
1089 pldm_entity_node **out,
1090 bool is_remote)
1091{
1092 assert(out != NULL && *out == NULL);
1093 if (node == NULL) {
1094 return;
1095 }
1096 bool is_entity_type;
1097 bool is_entity_instance_num;
1098
1099 is_entity_type = node->entity.entity_type == entity->entity_type;
1100 is_entity_instance_num = node->entity.entity_instance_num ==
1101 entity->entity_instance_num;
1102
1103 if (!is_remote ||
1104 node->remote_container_id == entity->entity_container_id) {
1105 if (is_entity_type && is_entity_instance_num) {
1106 entity->entity_container_id =
1107 node->entity.entity_container_id;
1108 *out = node;
1109 return;
1110 }
1111 }
1112 entity_association_tree_find_if_remote(node->next_sibling, entity, out,
1113 is_remote);
1114 entity_association_tree_find_if_remote(node->first_child, entity, out,
1115 is_remote);
1116}
1117
1118LIBPLDM_ABI_TESTING
1119pldm_entity_node *
1120pldm_entity_association_tree_find_if_remote(pldm_entity_association_tree *tree,
1121 pldm_entity *entity, bool is_remote)
1122{
1123 if (!tree || !entity) {
1124 return NULL;
1125 }
1126 pldm_entity_node *node = NULL;
1127 entity_association_tree_find_if_remote(tree->root, entity, &node,
1128 is_remote);
1129 return node;
1130}
1131
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301132LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301133void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity,
1134 pldm_entity_node **out)
1135{
1136 if (node == NULL) {
1137 return;
1138 }
1139
1140 if (node->entity.entity_type == entity->entity_type &&
1141 node->entity.entity_instance_num == entity->entity_instance_num) {
1142 entity->entity_container_id = node->entity.entity_container_id;
1143 *out = node;
1144 return;
1145 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301146 entity_association_tree_find(node->next_sibling, entity, out);
1147 entity_association_tree_find(node->first_child, entity, out);
1148}
1149
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301150LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301151pldm_entity_node *
1152pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
1153 pldm_entity *entity)
1154{
1155 assert(tree != NULL);
1156
1157 pldm_entity_node *node = NULL;
1158 entity_association_tree_find(tree->root, entity, &node);
1159 return node;
1160}
1161
1162static void entity_association_tree_copy(pldm_entity_node *org_node,
1163 pldm_entity_node **new_node)
1164{
1165 if (org_node == NULL) {
1166 return;
1167 }
1168 *new_node = malloc(sizeof(pldm_entity_node));
1169 (*new_node)->parent = org_node->parent;
1170 (*new_node)->entity = org_node->entity;
1171 (*new_node)->association_type = org_node->association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -06001172 (*new_node)->remote_container_id = org_node->remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301173 (*new_node)->first_child = NULL;
1174 (*new_node)->next_sibling = NULL;
1175 entity_association_tree_copy(org_node->first_child,
1176 &((*new_node)->first_child));
1177 entity_association_tree_copy(org_node->next_sibling,
1178 &((*new_node)->next_sibling));
1179}
1180
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301181LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301182void pldm_entity_association_tree_copy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301183 pldm_entity_association_tree *org_tree,
1184 pldm_entity_association_tree *new_tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301185{
1186 new_tree->last_used_container_id = org_tree->last_used_container_id;
1187 entity_association_tree_copy(org_tree->root, &(new_tree->root));
1188}
1189
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301190LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301191void pldm_entity_association_tree_destroy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301192 pldm_entity_association_tree *tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301193{
1194 assert(tree != NULL);
1195 entity_association_tree_destroy(tree->root);
1196 tree->last_used_container_id = 0;
1197 tree->root = NULL;
1198}
1199
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301200LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301201bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree)
1202{
1203 return ((tree->root == NULL) ? true : false);
1204}
1205
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301206LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301207void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len,
1208 size_t *num_entities,
1209 pldm_entity **entities)
1210{
1211 assert(pdr != NULL);
1212 assert(pdr_len >= sizeof(struct pldm_pdr_hdr) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301213 sizeof(struct pldm_pdr_entity_association));
Andrew Jeffery9c766792022-08-10 23:12:49 +09301214
1215 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
1216 assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION);
1217
1218 const uint8_t *start = (uint8_t *)pdr;
1219 const uint8_t *end =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301220 start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301221 start += sizeof(struct pldm_pdr_hdr);
1222 struct pldm_pdr_entity_association *entity_association_pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301223 (struct pldm_pdr_entity_association *)start;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301224 *num_entities = entity_association_pdr->num_children + 1;
1225 assert(*num_entities >= 2);
1226 *entities = malloc(sizeof(pldm_entity) * *num_entities);
1227 assert(*entities != NULL);
1228 assert(start + sizeof(struct pldm_pdr_entity_association) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301229 sizeof(pldm_entity) * (*num_entities - 2) ==
Andrew Jeffery9c766792022-08-10 23:12:49 +09301230 end);
1231 (*entities)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301232 le16toh(entity_association_pdr->container.entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301233 (*entities)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301234 le16toh(entity_association_pdr->container.entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301235 (*entities)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301236 le16toh(entity_association_pdr->container.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301237 pldm_entity *curr_entity = entity_association_pdr->children;
1238 size_t i = 1;
1239 while (i < *num_entities) {
1240 (*entities + i)->entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301241 le16toh(curr_entity->entity_type);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301242 (*entities + i)->entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301243 le16toh(curr_entity->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301244 (*entities + i)->entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301245 le16toh(curr_entity->entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301246 ++curr_entity;
1247 ++i;
1248 }
1249}