blob: 23044a957205c276481eb83c267936fe406c58c8 [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
Andrew Jefferyca248ce2023-07-07 10:38:30 +093037LIBPLDM_ABI_STABLE
Andrew Jeffery572a3952023-07-03 13:19:28 +093038int pldm_pdr_add_check(pldm_pdr *repo, const uint8_t *data, uint32_t size,
39 bool is_remote, uint16_t terminus_handle,
40 uint32_t *record_handle)
41{
Andrew Jefferyc6cc0282023-07-17 12:11:37 +093042 uint32_t curr;
43
Andrew Jeffery3b93d092023-07-17 13:01:50 +093044 if (!repo || !data || !size) {
Andrew Jeffery572a3952023-07-03 13:19:28 +093045 return -EINVAL;
46 }
Andrew Jeffery9c766792022-08-10 23:12:49 +093047
Andrew Jeffery3b93d092023-07-17 13:01:50 +093048 if (record_handle && *record_handle) {
Andrew Jefferyc6cc0282023-07-17 12:11:37 +093049 curr = *record_handle;
50 } else if (repo->last) {
51 curr = repo->last->record_handle;
52 if (curr == UINT32_MAX) {
53 return -EOVERFLOW;
54 }
55 curr += 1;
56 } else {
57 curr = 1;
58 }
59
Andrew Jeffery9c766792022-08-10 23:12:49 +093060 pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record));
Andrew Jeffery572a3952023-07-03 13:19:28 +093061 if (!record) {
62 return -ENOMEM;
63 }
Andrew Jefferya51ccc22023-06-28 21:57:46 +093064
Andrew Jeffery572a3952023-07-03 13:19:28 +093065 if (data) {
66 record->data = malloc(size);
Andrew Jeffery572a3952023-07-03 13:19:28 +093067 if (!record->data) {
68 free(record);
69 return -ENOMEM;
70 }
71 memcpy(record->data, data, size);
Andrew Jeffery2ca7e642023-06-28 22:29:07 +093072 }
73
Andrew Jeffery9c766792022-08-10 23:12:49 +093074 record->size = size;
75 record->is_remote = is_remote;
76 record->terminus_handle = terminus_handle;
Andrew Jefferyc6cc0282023-07-17 12:11:37 +093077 record->record_handle = curr;
Andrew Jeffery572a3952023-07-03 13:19:28 +093078
Andrew Jeffery3b93d092023-07-17 13:01:50 +093079 if (record_handle && !*record_handle && data) {
Andrew Jefferyc6cc0282023-07-17 12:11:37 +093080 /* If record handle is 0, that is an indication for this API to
81 * compute a new handle. For that reason, the computed handle
82 * needs to be populated in the PDR header. For a case where the
83 * caller supplied the record handle, it would exist in the
84 * header already.
85 */
86 struct pldm_pdr_hdr *hdr = (void *)record->data;
87 hdr->record_handle = htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +093088 }
Andrew Jeffery572a3952023-07-03 13:19:28 +093089
Andrew Jeffery9c766792022-08-10 23:12:49 +093090 record->next = NULL;
91
Andrew Jeffery8d231da2023-07-04 11:28:46 +093092 assert(!repo->first == !repo->last);
Andrew Jefferya51ccc22023-06-28 21:57:46 +093093 if (repo->first == NULL) {
Andrew Jefferya51ccc22023-06-28 21:57:46 +093094 repo->first = record;
95 repo->last = record;
96 } else {
97 repo->last->next = record;
98 repo->last = record;
99 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930100
Andrew Jefferya51ccc22023-06-28 21:57:46 +0930101 repo->size += record->size;
102 ++repo->record_count;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930103
Andrew Jeffery3b93d092023-07-17 13:01:50 +0930104 if (record_handle) {
105 *record_handle = record->record_handle;
106 }
Andrew Jeffery572a3952023-07-03 13:19:28 +0930107
108 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930109}
110
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930111LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930112pldm_pdr *pldm_pdr_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930113{
114 pldm_pdr *repo = malloc(sizeof(pldm_pdr));
Andrew Jefferya8bb22e2023-06-30 12:01:12 +0930115 if (!repo) {
116 return NULL;
117 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930118 repo->record_count = 0;
119 repo->size = 0;
120 repo->first = NULL;
121 repo->last = NULL;
122
123 return repo;
124}
125
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930126LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930127void pldm_pdr_destroy(pldm_pdr *repo)
128{
Andrew Jefferyfca1b602023-06-30 12:29:25 +0930129 if (!repo) {
130 return;
131 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930132
133 pldm_pdr_record *record = repo->first;
134 while (record != NULL) {
135 pldm_pdr_record *next = record->next;
136 if (record->data) {
137 free(record->data);
138 record->data = NULL;
139 }
140 free(record);
141 record = next;
142 }
143 free(repo);
144}
145
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930146LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930147const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo,
148 uint32_t record_handle,
149 uint8_t **data, uint32_t *size,
150 uint32_t *next_record_handle)
151{
Andrew Jeffery68b51302023-06-28 21:29:42 +0930152 if (!repo || !data || !size || !next_record_handle) {
153 return NULL;
154 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930155
156 if (!record_handle && (repo->first != NULL)) {
157 record_handle = repo->first->record_handle;
158 }
Andrew Jeffery68b51302023-06-28 21:29:42 +0930159
Andrew Jeffery9c766792022-08-10 23:12:49 +0930160 pldm_pdr_record *record = repo->first;
161 while (record != NULL) {
162 if (record->record_handle == record_handle) {
163 *size = record->size;
164 *data = record->data;
165 *next_record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930166 get_next_record_handle(repo, record);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930167 return record;
168 }
169 record = record->next;
170 }
171
172 *size = 0;
173 *next_record_handle = 0;
174 return NULL;
175}
176
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930177LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930178const pldm_pdr_record *
179pldm_pdr_get_next_record(const pldm_pdr *repo,
180 const pldm_pdr_record *curr_record, uint8_t **data,
181 uint32_t *size, uint32_t *next_record_handle)
182{
Andrew Jeffery68b51302023-06-28 21:29:42 +0930183 if (!repo || !curr_record || !data || !size || !next_record_handle) {
184 return NULL;
185 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930186
187 if (curr_record == repo->last) {
188 *data = NULL;
189 *size = 0;
190 *next_record_handle = get_next_record_handle(repo, curr_record);
191 return NULL;
192 }
193
194 *next_record_handle = get_next_record_handle(repo, curr_record->next);
195 *data = curr_record->next->data;
196 *size = curr_record->next->size;
197 return curr_record->next;
198}
199
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930200LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930201const pldm_pdr_record *
202pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type,
203 const pldm_pdr_record *curr_record, uint8_t **data,
204 uint32_t *size)
205{
Andrew Jefferyf85eeba2023-06-30 12:38:40 +0930206 if (!repo) {
207 return NULL;
208 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930209
210 pldm_pdr_record *record = repo->first;
211 if (curr_record != NULL) {
212 record = curr_record->next;
213 }
214 while (record != NULL) {
215 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data;
216 if (hdr->type == pdr_type) {
217 if (data && size) {
218 *size = record->size;
219 *data = record->data;
220 }
221 return record;
222 }
223 record = record->next;
224 }
225
226 if (size) {
227 *size = 0;
228 }
229 return NULL;
230}
231
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930232LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930233uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo)
234{
235 assert(repo != NULL);
236
237 return repo->record_count;
238}
239
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930240LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930241uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo)
242{
243 assert(repo != NULL);
244
245 return repo->size;
246}
247
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930248LIBPLDM_ABI_STABLE
Andrew Jeffery5565fcd2023-06-30 13:21:32 +0930249uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo
250 __attribute__((unused)),
Andrew Jeffery9c766792022-08-10 23:12:49 +0930251 const pldm_pdr_record *record)
252{
253 assert(repo != NULL);
254 assert(record != NULL);
255
256 return record->record_handle;
257}
258
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930259LIBPLDM_ABI_STABLE
260bool pldm_pdr_record_is_remote(const pldm_pdr_record *record)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930261{
262 assert(record != NULL);
263
264 return record->is_remote;
265}
266
Andrew Jefferya2c69112023-07-07 10:41:38 +0930267LIBPLDM_ABI_STABLE
Andrew Jefferyc821a702023-07-03 13:32:11 +0930268int pldm_pdr_add_fru_record_set_check(pldm_pdr *repo, uint16_t terminus_handle,
269 uint16_t fru_rsi, uint16_t entity_type,
270 uint16_t entity_instance_num,
271 uint16_t container_id,
272 uint32_t *bmc_record_handle)
273{
Andrew Jeffery73d67792023-07-17 15:22:18 +0930274 if (!repo || !bmc_record_handle) {
275 return -EINVAL;
276 }
277
Andrew Jeffery9c766792022-08-10 23:12:49 +0930278 uint32_t size = sizeof(struct pldm_pdr_hdr) +
279 sizeof(struct pldm_pdr_fru_record_set);
280 uint8_t data[size];
281
282 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data;
283 hdr->version = 1;
Andrew Jefferyc821a702023-07-03 13:32:11 +0930284 hdr->record_handle = *bmc_record_handle;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930285 hdr->type = PLDM_PDR_FRU_RECORD_SET;
286 hdr->record_change_num = 0;
287 hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set));
288 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930289 (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr +
290 sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930291 fru->terminus_handle = htole16(terminus_handle);
292 fru->fru_rsi = htole16(fru_rsi);
293 fru->entity_type = htole16(entity_type);
294 fru->entity_instance_num = htole16(entity_instance_num);
295 fru->container_id = htole16(container_id);
296
Andrew Jefferyc821a702023-07-03 13:32:11 +0930297 return pldm_pdr_add_check(repo, data, size, false, terminus_handle,
298 bmc_record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930299}
300
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930301LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930302const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930303 const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle,
304 uint16_t *entity_type, uint16_t *entity_instance_num,
305 uint16_t *container_id)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930306{
Andrew Jeffery01425e92023-06-30 13:47:40 +0930307 if (!repo || !terminus_handle || !entity_type || !entity_instance_num ||
308 !container_id) {
309 return NULL;
310 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930311
312 uint8_t *data = NULL;
313 uint32_t size = 0;
314 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930315 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930316 while (curr_record != NULL) {
317 struct pldm_pdr_fru_record_set *fru =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930318 (struct pldm_pdr_fru_record_set
319 *)(data + sizeof(struct pldm_pdr_hdr));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930320 if (fru->fru_rsi == htole16(fru_rsi)) {
321 *terminus_handle = le16toh(fru->terminus_handle);
322 *entity_type = le16toh(fru->entity_type);
323 *entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930324 le16toh(fru->entity_instance_num);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930325 *container_id = le16toh(fru->container_id);
326 return curr_record;
327 }
328 data = NULL;
329 curr_record = pldm_pdr_find_record_by_type(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930330 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data,
331 &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930332 }
333
334 *terminus_handle = 0;
335 *entity_type = 0;
336 *entity_instance_num = 0;
337 *container_id = 0;
338
339 return NULL;
340}
341
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930342LIBPLDM_ABI_STABLE
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930343/* NOLINTNEXTLINE(readability-identifier-naming) */
344void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle,
345 uint8_t tid, uint8_t tl_eid, bool valid_bit)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930346{
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930347 uint8_t *out_data = NULL;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930348 uint32_t size = 0;
349 const pldm_pdr_record *record;
350 record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR,
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930351 NULL, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930352
353 do {
354 if (record != NULL) {
355 struct pldm_terminus_locator_pdr *pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930356 (struct pldm_terminus_locator_pdr *)out_data;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930357 struct pldm_terminus_locator_type_mctp_eid *value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930358 (struct pldm_terminus_locator_type_mctp_eid *)
359 pdr->terminus_locator_value;
Andrew Jeffery6005f1c2023-04-05 20:02:52 +0930360 if (pdr->terminus_handle == terminus_handle &&
361 pdr->tid == tid && value->eid == tl_eid) {
362 pdr->validity = valid_bit;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930363 break;
364 }
365 }
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930366 record = pldm_pdr_find_record_by_type(repo,
367 PLDM_TERMINUS_LOCATOR_PDR,
368 record, &out_data, &size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930369 } while (record);
370}
371
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500372static bool pldm_record_handle_in_range(uint32_t record_handle,
373 uint32_t first_record_handle,
374 uint32_t last_record_handle)
375{
376 return record_handle >= first_record_handle &&
377 record_handle <= last_record_handle;
378}
379
380LIBPLDM_ABI_TESTING
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500381int pldm_pdr_find_child_container_id_index_range_exclude(
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500382 const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance,
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500383 uint8_t child_index, uint32_t range_exclude_start_handle,
384 uint32_t range_exclude_end_handle, uint16_t *container_id)
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500385{
386 pldm_pdr_record *record;
387 if (!repo) {
388 return -EINVAL;
389 }
390
391 for (record = repo->first; record; record = record->next) {
392 bool is_container_entity_instance_number;
393 struct pldm_pdr_entity_association *pdr;
394 bool is_container_entity_type;
395 struct pldm_entity *child;
396 struct pldm_pdr_hdr *hdr;
397 bool in_range;
398
399 // pldm_pdr_add() takes only uint8_t* data as an argument.
400 // The expectation here is the pldm_pdr_hdr is the first field of the record data
401 hdr = (struct pldm_pdr_hdr *)record->data;
402 if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
403 continue;
404 }
405 in_range = pldm_record_handle_in_range(
406 record->record_handle, range_exclude_start_handle,
407 range_exclude_end_handle);
408 if (in_range) {
409 continue;
410 }
411
412 // this cast is valid with respect to alignment because
413 // struct pldm_pdr_hdr is declared with __attribute__((packed))
414 pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr));
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500415 if (child_index >= pdr->num_children) {
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500416 continue;
417 }
Pavithra Barithaya8cf70452023-06-22 04:36:19 -0500418
419 child = (&pdr->children[child_index]);
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500420 is_container_entity_type = pdr->container.entity_type ==
421 entity_type;
422 is_container_entity_instance_number =
423 pdr->container.entity_instance_num == entity_instance;
424 if (is_container_entity_type &&
425 is_container_entity_instance_number) {
426 *container_id = le16toh(child->entity_container_id);
Pavithra Barithayaffd53422023-06-23 23:20:48 -0500427 return 0;
Pavithra Barithaya5dc02572023-05-19 09:24:36 -0500428 }
429 }
430 return -ENOKEY;
431}
432
Andrew Jeffery9c766792022-08-10 23:12:49 +0930433typedef struct pldm_entity_association_tree {
434 pldm_entity_node *root;
435 uint16_t last_used_container_id;
436} pldm_entity_association_tree;
437
438typedef struct pldm_entity_node {
439 pldm_entity entity;
440 pldm_entity parent;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600441 uint16_t remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930442 pldm_entity_node *first_child;
443 pldm_entity_node *next_sibling;
444 uint8_t association_type;
445} pldm_entity_node;
446
447static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
448{
449 assert(tree != NULL);
450 assert(tree->last_used_container_id != UINT16_MAX);
451
452 return ++tree->last_used_container_id;
453}
454
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930455LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930456pldm_entity pldm_entity_extract(pldm_entity_node *node)
457{
458 assert(node != NULL);
459
460 return node->entity;
461}
462
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600463LIBPLDM_ABI_TESTING
Andrew Jeffery15b88182023-06-30 13:29:17 +0930464uint16_t
465pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity)
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600466{
Andrew Jeffery15b88182023-06-30 13:29:17 +0930467 assert(entity != NULL);
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600468
Andrew Jeffery15b88182023-06-30 13:29:17 +0930469 return entity->remote_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600470}
471
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930472LIBPLDM_ABI_STABLE
Andrew Jeffery319304f2023-04-05 13:53:18 +0930473pldm_entity_association_tree *pldm_entity_association_tree_init(void)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930474{
475 pldm_entity_association_tree *tree =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930476 malloc(sizeof(pldm_entity_association_tree));
Andrew Jefferyc40037d2023-06-30 13:50:12 +0930477 if (!tree) {
478 return NULL;
479 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930480 tree->root = NULL;
481 tree->last_used_container_id = 0;
482
483 return tree;
484}
485
486static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
487 uint16_t entity_type)
488{
489 assert(start != NULL);
490
491 /* Insert after the the last node that matches the input entity type, or
492 * at the end if no such match occurrs
493 */
494 while (start->next_sibling != NULL) {
495 uint16_t this_type = start->entity.entity_type;
496 pldm_entity_node *next = start->next_sibling;
497 if (this_type == entity_type &&
498 (this_type != next->entity.entity_type)) {
499 break;
500 }
501 start = start->next_sibling;
502 }
503
504 return start;
505}
506
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930507LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930508pldm_entity_node *pldm_entity_association_tree_add(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930509 pldm_entity_association_tree *tree, pldm_entity *entity,
510 uint16_t entity_instance_number, pldm_entity_node *parent,
511 uint8_t association_type)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930512{
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500513 return pldm_entity_association_tree_add_entity(tree, entity,
514 entity_instance_number,
515 parent, association_type,
516 false, true, 0xFFFF);
517}
518
519LIBPLDM_ABI_TESTING
520pldm_entity_node *pldm_entity_association_tree_add_entity(
521 pldm_entity_association_tree *tree, pldm_entity *entity,
522 uint16_t entity_instance_number, pldm_entity_node *parent,
523 uint8_t association_type, bool is_remote, bool is_update_container_id,
524 uint16_t container_id)
525{
526 if ((!tree) || (!entity)) {
527 return NULL;
528 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930529
530 if (entity_instance_number != 0xFFFF && parent != NULL) {
531 pldm_entity node;
532 node.entity_type = entity->entity_type;
533 node.entity_instance_num = entity_instance_number;
534 if (pldm_is_current_parent_child(parent, &node)) {
535 return NULL;
536 }
537 }
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500538 if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL &&
539 association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) {
540 return NULL;
541 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930542 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500543 if (!node) {
544 return NULL;
545 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930546 node->first_child = NULL;
547 node->next_sibling = NULL;
548 node->parent.entity_type = 0;
549 node->parent.entity_instance_num = 0;
550 node->parent.entity_container_id = 0;
551 node->entity.entity_type = entity->entity_type;
552 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930553 entity_instance_number != 0xFFFF ? entity_instance_number : 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930554 node->association_type = association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600555 node->remote_container_id = 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930556 if (tree->root == NULL) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500557 if (parent != NULL) {
558 free(node);
559 return NULL;
560 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930561 tree->root = node;
562 /* container_id 0 here indicates this is the top-most entry */
563 node->entity.entity_container_id = 0;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600564 node->remote_container_id = node->entity.entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930565 } else if (parent != NULL && parent->first_child == NULL) {
566 parent->first_child = node;
567 node->parent = parent->entity;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500568
569 if (is_remote) {
570 node->remote_container_id = entity->entity_container_id;
571 }
572 if (is_update_container_id) {
573 if (container_id != 0xFFFF) {
574 node->entity.entity_container_id = container_id;
575 } else {
576 node->entity.entity_container_id =
577 next_container_id(tree);
578 }
579 } else {
580 node->entity.entity_container_id =
581 entity->entity_container_id;
582 }
583
584 if (!is_remote) {
585 node->remote_container_id =
586 node->entity.entity_container_id;
587 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930588 } else {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930589 pldm_entity_node *start = parent == NULL ? tree->root :
590 parent->first_child;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930591 pldm_entity_node *prev =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930592 find_insertion_at(start, entity->entity_type);
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500593 if (!prev) {
594 free(node);
595 return NULL;
596 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930597 pldm_entity_node *next = prev->next_sibling;
598 if (prev->entity.entity_type == entity->entity_type) {
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500599 if (prev->entity.entity_instance_num == UINT16_MAX) {
600 free(node);
601 return NULL;
602 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930603 node->entity.entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930604 entity_instance_number != 0xFFFF ?
605 entity_instance_number :
606 prev->entity.entity_instance_num + 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930607 }
608 prev->next_sibling = node;
609 node->parent = prev->parent;
610 node->next_sibling = next;
611 node->entity.entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930612 prev->entity.entity_container_id;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600613 node->remote_container_id = entity->entity_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930614 }
615 entity->entity_instance_num = node->entity.entity_instance_num;
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -0500616 if (is_update_container_id) {
617 entity->entity_container_id = node->entity.entity_container_id;
618 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930619 return node;
620}
621
622static void get_num_nodes(pldm_entity_node *node, size_t *num)
623{
624 if (node == NULL) {
625 return;
626 }
627
628 ++(*num);
629 get_num_nodes(node->next_sibling, num);
630 get_num_nodes(node->first_child, num);
631}
632
633static void entity_association_tree_visit(pldm_entity_node *node,
634 pldm_entity *entities, size_t *index)
635{
636 if (node == NULL) {
637 return;
638 }
639
640 pldm_entity *entity = &entities[*index];
641 ++(*index);
642 entity->entity_type = node->entity.entity_type;
643 entity->entity_instance_num = node->entity.entity_instance_num;
644 entity->entity_container_id = node->entity.entity_container_id;
645
646 entity_association_tree_visit(node->next_sibling, entities, index);
647 entity_association_tree_visit(node->first_child, entities, index);
648}
649
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930650LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930651void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
652 pldm_entity **entities, size_t *size)
653{
Andrew Jefferycd17e5c2023-06-30 14:06:18 +0930654 if (!tree || !entities || !size) {
655 return;
656 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930657
658 *size = 0;
659 if (tree->root == NULL) {
660 return;
661 }
662
663 get_num_nodes(tree->root, size);
664 *entities = malloc(*size * sizeof(pldm_entity));
Andrew Jefferycd17e5c2023-06-30 14:06:18 +0930665 if (!entities) {
666 return;
667 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930668 size_t index = 0;
669 entity_association_tree_visit(tree->root, *entities, &index);
670}
671
672static void entity_association_tree_destroy(pldm_entity_node *node)
673{
674 if (node == NULL) {
675 return;
676 }
677
678 entity_association_tree_destroy(node->next_sibling);
679 entity_association_tree_destroy(node->first_child);
680 free(node);
681}
682
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930683LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930684void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
685{
Andrew Jefferya89e0152023-06-30 14:24:05 +0930686 if (!tree) {
687 return;
688 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930689
690 entity_association_tree_destroy(tree->root);
691 free(tree);
692}
693
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930694LIBPLDM_ABI_STABLE
695bool pldm_entity_is_node_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930696{
697 assert(node != NULL);
698
699 return node->first_child != NULL;
700}
701
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930702LIBPLDM_ABI_STABLE
703pldm_entity pldm_entity_get_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930704{
705 assert(node != NULL);
706
707 return node->parent;
708}
709
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930710LIBPLDM_ABI_STABLE
711bool pldm_entity_is_exist_parent(pldm_entity_node *node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930712{
713 assert(node != NULL);
714
715 if (node->parent.entity_type == 0 &&
716 node->parent.entity_instance_num == 0 &&
717 node->parent.entity_container_id == 0) {
718 return false;
719 }
720
721 return true;
722}
723
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930724LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930725uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
726 uint8_t association_type)
727{
Andrew Jeffery6e8a2612023-06-30 15:36:48 +0930728 if (!node) {
729 return 0;
730 }
731
Andrew Jeffery6e8a2612023-06-30 15:36:48 +0930732 if (!(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
733 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL)) {
734 return 0;
735 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930736
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);
Andrew Jefferyc83ef862023-07-11 17:16:55 +0930747 return count < UINT8_MAX ? count : 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930748}
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{
Andrew Jeffery375d9fc2023-06-30 15:45:54 +0930753 if (!parent || !node) {
754 return false;
755 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930756
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
Andrew Jeffery65945992023-07-17 15:04:21 +0930770static int entity_association_pdr_add_children(
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500771 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
Andrew Jeffery65945992023-07-17 15:04:21 +0930815 return pldm_pdr_add_check(repo, pdr, size, is_remote, terminus_handle,
816 &record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930817}
818
Andrew Jeffery65945992023-07-17 15:04:21 +0930819static int entity_association_pdr_add_entry(pldm_entity_node *curr,
820 pldm_pdr *repo, bool is_remote,
821 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 Jeffery65945992023-07-17 15:04:21 +0930828 int rc;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930829
830 if (num_logical_children) {
831 uint16_t logical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930832 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
833 sizeof(uint8_t) + sizeof(pldm_entity) +
834 sizeof(uint8_t) +
835 (num_logical_children * sizeof(pldm_entity));
Andrew Jeffery65945992023-07-17 15:04:21 +0930836 rc = entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930837 curr, repo, logical_pdr_size, num_logical_children,
838 PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500839 terminus_handle, record_handle);
Andrew Jeffery65945992023-07-17 15:04:21 +0930840 if (rc < 0) {
841 return rc;
842 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930843 }
844
845 if (num_physical_children) {
846 uint16_t physical_pdr_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930847 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
848 sizeof(uint8_t) + sizeof(pldm_entity) +
849 sizeof(uint8_t) +
850 (num_physical_children * sizeof(pldm_entity));
Andrew Jeffery65945992023-07-17 15:04:21 +0930851 rc = entity_association_pdr_add_children(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930852 curr, repo, physical_pdr_size, num_physical_children,
853 PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500854 terminus_handle, record_handle);
Andrew Jeffery65945992023-07-17 15:04:21 +0930855 if (rc < 0) {
856 return rc;
857 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930858 }
Andrew Jeffery65945992023-07-17 15:04:21 +0930859
860 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930861}
862
Andrew Jefferyd09b1af2023-07-17 12:39:16 +0930863static bool is_present(pldm_entity entity, pldm_entity **entities,
864 size_t num_entities)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930865{
866 if (entities == NULL || num_entities == 0) {
867 return true;
868 }
869 size_t i = 0;
870 while (i < num_entities) {
871 if ((*entities + i)->entity_type == entity.entity_type) {
872 return true;
873 }
874 i++;
875 }
876 return false;
877}
878
Andrew Jeffery65945992023-07-17 15:04:21 +0930879static int entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo,
880 pldm_entity **entities,
881 size_t num_entities, bool is_remote,
882 uint16_t terminus_handle,
883 uint32_t record_handle)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930884{
Andrew Jeffery65945992023-07-17 15:04:21 +0930885 int rc;
886
Andrew Jeffery9c766792022-08-10 23:12:49 +0930887 if (curr == NULL) {
Andrew Jeffery65945992023-07-17 15:04:21 +0930888 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930889 }
Andrew Jeffery65945992023-07-17 15:04:21 +0930890
891 if (is_present(curr->entity, entities, num_entities)) {
892 rc = entity_association_pdr_add_entry(
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500893 curr, repo, is_remote, terminus_handle, record_handle);
Andrew Jeffery65945992023-07-17 15:04:21 +0930894 if (rc) {
895 return rc;
896 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930897 }
Andrew Jeffery65945992023-07-17 15:04:21 +0930898
899 rc = entity_association_pdr_add(curr->next_sibling, repo, entities,
900 num_entities, is_remote,
901 terminus_handle, record_handle);
902 if (rc) {
903 return rc;
904 }
905
906 return entity_association_pdr_add(curr->first_child, repo, entities,
907 num_entities, is_remote,
908 terminus_handle, record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930909}
910
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930911LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930912void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree,
913 pldm_pdr *repo, bool is_remote,
914 uint16_t terminus_handle)
915{
Andrew Jeffery65945992023-07-17 15:04:21 +0930916 int rc = pldm_entity_association_pdr_add_check(tree, repo, is_remote,
917 terminus_handle);
918 (void)rc;
919 assert(!rc);
920}
921
922LIBPLDM_ABI_TESTING
923int pldm_entity_association_pdr_add_check(pldm_entity_association_tree *tree,
924 pldm_pdr *repo, bool is_remote,
925 uint16_t terminus_handle)
926{
Andrew Jefferyc7883482023-06-30 15:52:04 +0930927 if (!tree || !repo) {
Andrew Jeffery65945992023-07-17 15:04:21 +0930928 return 0;
Andrew Jefferyc7883482023-06-30 15:52:04 +0930929 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930930
Andrew Jeffery65945992023-07-17 15:04:21 +0930931 return entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote,
932 terminus_handle, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930933}
934
Andrew Jeffery1354a6e2023-07-07 10:34:38 +0930935LIBPLDM_ABI_STABLE
Andrew Jefferycc394522023-07-03 12:49:31 +0930936int pldm_entity_association_pdr_add_from_node_check(
937 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
938 size_t num_entities, bool is_remote, uint16_t terminus_handle)
939{
940 return pldm_entity_association_pdr_add_from_node_with_record_handle(
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500941 node, repo, entities, num_entities, is_remote, terminus_handle,
942 0);
943}
944
945LIBPLDM_ABI_TESTING
946int pldm_entity_association_pdr_add_from_node_with_record_handle(
947 pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities,
948 size_t num_entities, bool is_remote, uint16_t terminus_handle,
949 uint32_t record_handle)
950{
951 if (!node || !repo || !entities) {
952 return -EINVAL;
953 }
954
Andrew Jeffery9c766792022-08-10 23:12:49 +0930955 entity_association_pdr_add(node, repo, entities, num_entities,
Pavithra Barithaya25ddbcc2023-05-19 08:28:59 -0500956 is_remote, terminus_handle, record_handle);
957
958 return 0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930959}
960
Andrew Jeffery643c4432023-07-17 15:36:03 +0930961static void find_entity_ref_in_tree(pldm_entity_node *tree_node,
962 pldm_entity entity, pldm_entity_node **node)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930963{
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600964 bool is_entity_container_id;
965 bool is_entity_instance_num;
966 bool is_type;
967
Andrew Jeffery9c766792022-08-10 23:12:49 +0930968 if (tree_node == NULL) {
969 return;
970 }
971
ArchanaKakani39bd2ea2023-02-02 02:39:18 -0600972 is_type = tree_node->entity.entity_type == entity.entity_type;
973 is_entity_instance_num = tree_node->entity.entity_instance_num ==
974 entity.entity_instance_num;
975 is_entity_container_id = tree_node->entity.entity_container_id ==
976 entity.entity_container_id;
977
978 if (is_type && is_entity_instance_num && is_entity_container_id) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930979 *node = tree_node;
980 return;
981 }
982
983 find_entity_ref_in_tree(tree_node->first_child, entity, node);
984 find_entity_ref_in_tree(tree_node->next_sibling, entity, node);
985}
986
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930987LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930988void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree,
989 pldm_entity entity, pldm_entity_node **node)
990{
Andrew Jefferyba47e832023-07-03 11:41:03 +0930991 if (!tree || !node) {
992 return;
993 }
994
Andrew Jeffery9c766792022-08-10 23:12:49 +0930995 find_entity_ref_in_tree(tree->root, entity, node);
996}
997
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930998LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930999void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo,
1000 uint16_t terminus_handle)
1001{
Andrew Jeffery438dd492023-07-03 11:51:43 +09301002 if (!repo) {
1003 return;
1004 }
1005
Andrew Jeffery9c766792022-08-10 23:12:49 +09301006 bool removed = false;
1007
1008 pldm_pdr_record *record = repo->first;
1009 pldm_pdr_record *prev = NULL;
1010 while (record != NULL) {
1011 pldm_pdr_record *next = record->next;
1012 if (record->terminus_handle == terminus_handle) {
1013 if (repo->first == record) {
1014 repo->first = next;
1015 } else {
1016 prev->next = next;
1017 }
1018 if (repo->last == record) {
1019 repo->last = prev;
1020 }
1021 if (record->data) {
1022 free(record->data);
1023 }
1024 --repo->record_count;
1025 repo->size -= record->size;
1026 free(record);
1027 removed = true;
1028 } else {
1029 prev = record;
1030 }
1031 record = next;
1032 }
1033
1034 if (removed == true) {
1035 record = repo->first;
1036 uint32_t record_handle = 0;
1037 while (record != NULL) {
1038 record->record_handle = ++record_handle;
1039 if (record->data != NULL) {
1040 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301041 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301042 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301043 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301044 }
1045 record = record->next;
1046 }
1047 }
1048}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301049
1050LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301051void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo)
1052{
Andrew Jeffery3e1d6592023-07-03 11:57:16 +09301053 if (!repo) {
1054 return;
1055 }
1056
Andrew Jeffery9c766792022-08-10 23:12:49 +09301057 bool removed = false;
1058
1059 pldm_pdr_record *record = repo->first;
1060 pldm_pdr_record *prev = NULL;
1061 while (record != NULL) {
1062 pldm_pdr_record *next = record->next;
1063 if (record->is_remote == true) {
1064 if (repo->first == record) {
1065 repo->first = next;
1066 } else {
1067 prev->next = next;
1068 }
1069 if (repo->last == record) {
1070 repo->last = prev;
1071 }
1072 if (record->data) {
1073 free(record->data);
1074 }
1075 --repo->record_count;
1076 repo->size -= record->size;
1077 free(record);
1078 removed = true;
1079 } else {
1080 prev = record;
1081 }
1082 record = next;
1083 }
1084
1085 if (removed == true) {
1086 record = repo->first;
1087 uint32_t record_handle = 0;
1088 while (record != NULL) {
1089 record->record_handle = ++record_handle;
1090 if (record->data != NULL) {
1091 struct pldm_pdr_hdr *hdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301092 (struct pldm_pdr_hdr *)(record->data);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301093 hdr->record_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301094 htole32(record->record_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301095 }
1096 record = record->next;
1097 }
1098 }
1099}
1100
Pavithra Barithaya4d694342023-05-19 08:04:41 -05001101LIBPLDM_ABI_TESTING
1102pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo,
1103 uint32_t first, uint32_t last)
1104{
1105 pldm_pdr_record *record = NULL;
1106 pldm_pdr_record *curr;
1107
1108 if (!repo) {
1109 return NULL;
1110 }
1111 for (curr = repo->first; curr; curr = curr->next) {
1112 if (first > curr->record_handle || last < curr->record_handle) {
1113 continue;
1114 }
1115 if (!record || curr->record_handle > record->record_handle) {
1116 record = curr;
1117 }
1118 }
1119
1120 return record;
1121}
1122
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001123static void entity_association_tree_find_if_remote(pldm_entity_node *node,
1124 pldm_entity *entity,
1125 pldm_entity_node **out,
1126 bool is_remote)
1127{
Pavithra Barithaya9947f9d2023-05-18 05:20:24 -05001128 if (node == NULL) {
1129 return;
1130 }
1131 bool is_entity_type;
1132 bool is_entity_instance_num;
1133
1134 is_entity_type = node->entity.entity_type == entity->entity_type;
1135 is_entity_instance_num = node->entity.entity_instance_num ==
1136 entity->entity_instance_num;
1137
1138 if (!is_remote ||
1139 node->remote_container_id == entity->entity_container_id) {
1140 if (is_entity_type && is_entity_instance_num) {
1141 entity->entity_container_id =
1142 node->entity.entity_container_id;
1143 *out = node;
1144 return;
1145 }
1146 }
1147 entity_association_tree_find_if_remote(node->next_sibling, entity, out,
1148 is_remote);
1149 entity_association_tree_find_if_remote(node->first_child, entity, out,
1150 is_remote);
1151}
1152
1153LIBPLDM_ABI_TESTING
1154pldm_entity_node *
1155pldm_entity_association_tree_find_if_remote(pldm_entity_association_tree *tree,
1156 pldm_entity *entity, bool is_remote)
1157{
1158 if (!tree || !entity) {
1159 return NULL;
1160 }
1161 pldm_entity_node *node = NULL;
1162 entity_association_tree_find_if_remote(tree->root, entity, &node,
1163 is_remote);
1164 return node;
1165}
1166
Andrew Jeffery54d91e82023-07-17 15:37:12 +09301167static void entity_association_tree_find(pldm_entity_node *node,
1168 pldm_entity *entity,
1169 pldm_entity_node **out)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301170{
1171 if (node == NULL) {
1172 return;
1173 }
1174
1175 if (node->entity.entity_type == entity->entity_type &&
1176 node->entity.entity_instance_num == entity->entity_instance_num) {
1177 entity->entity_container_id = node->entity.entity_container_id;
1178 *out = node;
1179 return;
1180 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301181 entity_association_tree_find(node->next_sibling, entity, out);
1182 entity_association_tree_find(node->first_child, entity, out);
1183}
1184
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301185LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301186pldm_entity_node *
1187pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
1188 pldm_entity *entity)
1189{
Andrew Jeffery94e364d2023-07-03 12:26:59 +09301190 if (!tree || !entity) {
1191 return NULL;
1192 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301193
1194 pldm_entity_node *node = NULL;
1195 entity_association_tree_find(tree->root, entity, &node);
1196 return node;
1197}
1198
1199static void entity_association_tree_copy(pldm_entity_node *org_node,
1200 pldm_entity_node **new_node)
1201{
1202 if (org_node == NULL) {
1203 return;
1204 }
1205 *new_node = malloc(sizeof(pldm_entity_node));
1206 (*new_node)->parent = org_node->parent;
1207 (*new_node)->entity = org_node->entity;
1208 (*new_node)->association_type = org_node->association_type;
ArchanaKakani39bd2ea2023-02-02 02:39:18 -06001209 (*new_node)->remote_container_id = org_node->remote_container_id;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301210 (*new_node)->first_child = NULL;
1211 (*new_node)->next_sibling = NULL;
1212 entity_association_tree_copy(org_node->first_child,
1213 &((*new_node)->first_child));
1214 entity_association_tree_copy(org_node->next_sibling,
1215 &((*new_node)->next_sibling));
1216}
1217
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301218LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301219void pldm_entity_association_tree_copy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301220 pldm_entity_association_tree *org_tree,
1221 pldm_entity_association_tree *new_tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301222{
1223 new_tree->last_used_container_id = org_tree->last_used_container_id;
1224 entity_association_tree_copy(org_tree->root, &(new_tree->root));
1225}
1226
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301227LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301228void pldm_entity_association_tree_destroy_root(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301229 pldm_entity_association_tree *tree)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301230{
Andrew Jeffery85d7a052023-07-03 12:29:24 +09301231 if (!tree) {
1232 return;
1233 }
1234
Andrew Jeffery9c766792022-08-10 23:12:49 +09301235 entity_association_tree_destroy(tree->root);
1236 tree->last_used_container_id = 0;
1237 tree->root = NULL;
1238}
1239
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301240LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301241bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree)
1242{
1243 return ((tree->root == NULL) ? true : false);
1244}
1245
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301246LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301247void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len,
1248 size_t *num_entities,
1249 pldm_entity **entities)
1250{
Andrew Jeffery3a5c46b2023-07-03 12:39:59 +09301251 if (!pdr || !num_entities || !entities) {
1252 return;
1253 }
1254#define PDR_MIN_SIZE \
1255 (sizeof(struct pldm_pdr_hdr) + \
1256 sizeof(struct pldm_pdr_entity_association))
Andrew Jeffery3a5c46b2023-07-03 12:39:59 +09301257 if (pdr_len < PDR_MIN_SIZE) {
1258 return;
1259 }
1260#undef PDR_MIN_SIZE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301261
1262 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
Andrew Jeffery918973f2023-07-11 17:11:03 +09301263 if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) {
1264 return;
1265 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301266
1267 const uint8_t *start = (uint8_t *)pdr;
Andrew Jeffery3a5c46b2023-07-03 12:39:59 +09301268 const uint8_t *end __attribute__((unused)) =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301269 start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301270 start += sizeof(struct pldm_pdr_hdr);
1271 struct pldm_pdr_entity_association *entity_association_pdr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301272 (struct pldm_pdr_entity_association *)start;
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301273 size_t l_num_entities = entity_association_pdr->num_children + 1;
Andrew Jeffery918973f2023-07-11 17:11:03 +09301274 if (l_num_entities < 2) {
1275 return;
1276 }
Andrew Jeffery918973f2023-07-11 17:11:03 +09301277 if (start + sizeof(struct pldm_pdr_entity_association) +
1278 sizeof(pldm_entity) * (l_num_entities - 2) !=
1279 end) {
1280 return;
1281 }
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301282 pldm_entity *l_entities = malloc(sizeof(pldm_entity) * l_num_entities);
Andrew Jeffery918973f2023-07-11 17:11:03 +09301283 if (!l_entities) {
1284 return;
1285 }
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301286 l_entities[0].entity_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301287 le16toh(entity_association_pdr->container.entity_type);
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301288 l_entities[0].entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301289 le16toh(entity_association_pdr->container.entity_instance_num);
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301290 l_entities[0].entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301291 le16toh(entity_association_pdr->container.entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301292 pldm_entity *curr_entity = entity_association_pdr->children;
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301293 for (size_t i = 1; i < l_num_entities; i++, curr_entity++) {
1294 l_entities[i].entity_type = le16toh(curr_entity->entity_type);
1295 l_entities[i].entity_instance_num =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301296 le16toh(curr_entity->entity_instance_num);
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301297 l_entities[i].entity_container_id =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301298 le16toh(curr_entity->entity_container_id);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301299 }
Andrew Jeffery0dbaa702023-07-11 16:58:25 +09301300
1301 *num_entities = l_num_entities;
1302 *entities = l_entities;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301303}