blob: 64c314b4bd3e3c84b63a7e4795233ae2b9dd2c90 [file] [log] [blame]
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -06001#include "pdr.h"
2#include "platform.h"
3#include <assert.h>
4#include <stdlib.h>
5#include <string.h>
6
7typedef struct pldm_pdr_record {
8 uint32_t record_handle;
9 uint32_t size;
10 uint8_t *data;
11 struct pldm_pdr_record *next;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050012 bool is_remote;
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060013} pldm_pdr_record;
14
15typedef struct pldm_pdr {
16 uint32_t record_count;
17 uint32_t size;
18 uint32_t last_used_record_handle;
19 pldm_pdr_record *first;
20 pldm_pdr_record *last;
21} pldm_pdr;
22
23static inline uint32_t get_next_record_handle(const pldm_pdr *repo,
24 const pldm_pdr_record *record)
25{
26 assert(repo != NULL);
27 assert(record != NULL);
28
29 if (record == repo->last) {
30 return 0;
31 }
32 return record->next->record_handle;
33}
34
35static void add_record(pldm_pdr *repo, pldm_pdr_record *record)
36{
37 assert(repo != NULL);
38 assert(record != NULL);
39
40 if (repo->first == NULL) {
41 assert(repo->last == NULL);
42 repo->first = record;
43 repo->last = record;
44 } else {
45 repo->last->next = record;
46 repo->last = record;
47 }
48 repo->size += record->size;
49 repo->last_used_record_handle = record->record_handle;
50 ++repo->record_count;
51}
52
53static inline uint32_t get_new_record_handle(const pldm_pdr *repo)
54{
55 assert(repo != NULL);
56 assert(repo->last_used_record_handle != UINT32_MAX);
57
58 return repo->last_used_record_handle + 1;
59}
60
61static pldm_pdr_record *make_new_record(const pldm_pdr *repo,
62 const uint8_t *data, uint32_t size,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050063 uint32_t record_handle, bool is_remote)
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060064{
65 assert(repo != NULL);
66 assert(size != 0);
67
68 pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record));
69 assert(record != NULL);
70 record->record_handle =
71 record_handle == 0 ? get_new_record_handle(repo) : record_handle;
72 record->size = size;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050073 record->is_remote = is_remote;
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060074 if (data != NULL) {
75 record->data = malloc(size);
76 assert(record->data != NULL);
77 memcpy(record->data, data, size);
78 /* If record handle is 0, that is an indication for this API to
79 * compute a new handle. For that reason, the computed handle
80 * needs to be populated in the PDR header. For a case where the
81 * caller supplied the record handle, it would exist in the
82 * header already.
83 */
84 if (!record_handle) {
85 struct pldm_pdr_hdr *hdr =
86 (struct pldm_pdr_hdr *)(record->data);
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -050087 hdr->record_handle = htole32(record->record_handle);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060088 }
89 }
90 record->next = NULL;
91
92 return record;
93}
94
95uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050096 uint32_t record_handle, bool is_remote)
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -060097{
98 assert(size != 0);
99 assert(data != NULL);
100
101 pldm_pdr_record *record =
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500102 make_new_record(repo, data, size, record_handle, is_remote);
Deepak Kodihalli3b02ed82020-02-06 01:18:25 -0600103 add_record(repo, record);
104
105 return record->record_handle;
106}
107
108pldm_pdr *pldm_pdr_init()
109{
110 pldm_pdr *repo = malloc(sizeof(pldm_pdr));
111 assert(repo != NULL);
112 repo->record_count = 0;
113 repo->size = 0;
114 repo->last_used_record_handle = 0;
115 repo->first = NULL;
116 repo->last = NULL;
117
118 return repo;
119}
120
121void pldm_pdr_destroy(pldm_pdr *repo)
122{
123 assert(repo != NULL);
124
125 pldm_pdr_record *record = repo->first;
126 while (record != NULL) {
127 pldm_pdr_record *next = record->next;
128 if (record->data) {
129 free(record->data);
130 record->data = NULL;
131 }
132 free(record);
133 record = next;
134 }
135 free(repo);
136}
137
138const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo,
139 uint32_t record_handle,
140 uint8_t **data, uint32_t *size,
141 uint32_t *next_record_handle)
142{
143 assert(repo != NULL);
144 assert(data != NULL);
145 assert(size != NULL);
146 assert(next_record_handle != NULL);
147
148 if (!record_handle && (repo->first != NULL)) {
149 record_handle = repo->first->record_handle;
150 }
151 pldm_pdr_record *record = repo->first;
152 while (record != NULL) {
153 if (record->record_handle == record_handle) {
154 *size = record->size;
155 *data = record->data;
156 *next_record_handle =
157 get_next_record_handle(repo, record);
158 return record;
159 }
160 record = record->next;
161 }
162
163 *size = 0;
164 *next_record_handle = 0;
165 return NULL;
166}
167
168const pldm_pdr_record *
169pldm_pdr_get_next_record(const pldm_pdr *repo,
170 const pldm_pdr_record *curr_record, uint8_t **data,
171 uint32_t *size, uint32_t *next_record_handle)
172{
173 assert(repo != NULL);
174 assert(curr_record != NULL);
175 assert(data != NULL);
176 assert(size != NULL);
177 assert(next_record_handle != NULL);
178
179 if (curr_record == repo->last) {
180 *data = NULL;
181 *size = 0;
182 *next_record_handle = get_next_record_handle(repo, curr_record);
183 return NULL;
184 }
185
186 *next_record_handle = get_next_record_handle(repo, curr_record->next);
187 *data = curr_record->next->data;
188 *size = curr_record->next->size;
189 return curr_record->next;
190}
191
192const pldm_pdr_record *
193pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type,
194 const pldm_pdr_record *curr_record, uint8_t **data,
195 uint32_t *size)
196{
197 assert(repo != NULL);
198 assert(data != NULL);
199 assert(size != NULL);
200
201 pldm_pdr_record *record = repo->first;
202 if (curr_record != NULL) {
203 record = curr_record->next;
204 }
205 while (record != NULL) {
206 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data;
207 if (hdr->type == pdr_type) {
208 *size = record->size;
209 *data = record->data;
210 return record;
211 }
212 record = record->next;
213 }
214
215 *size = 0;
216 return NULL;
217}
218
219uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo)
220{
221 assert(repo != NULL);
222
223 return repo->record_count;
224}
225
226uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo)
227{
228 assert(repo != NULL);
229
230 return repo->size;
231}
232
233uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo,
234 const pldm_pdr_record *record)
235{
236 assert(repo != NULL);
237 assert(record != NULL);
238
239 return record->record_handle;
240}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600241
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500242inline bool pldm_pdr_record_is_remote(const pldm_pdr_record *record)
243{
244 assert(record != NULL);
245
246 return record->is_remote;
247}
248
Deepak Kodihallidb914672020-02-07 02:47:45 -0600249uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle,
250 uint16_t fru_rsi, uint16_t entity_type,
251 uint16_t entity_instance_num,
252 uint16_t container_id)
253{
254 uint32_t size = sizeof(struct pldm_pdr_hdr) +
255 sizeof(struct pldm_pdr_fru_record_set);
256 uint8_t data[size];
257
258 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data;
259 hdr->version = 1;
260 hdr->record_handle = 0;
261 hdr->type = PLDM_PDR_FRU_RECORD_SET;
262 hdr->record_change_num = 0;
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500263 hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set));
Deepak Kodihallidb914672020-02-07 02:47:45 -0600264 struct pldm_pdr_fru_record_set *fru =
265 (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr +
266 sizeof(struct pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500267 fru->terminus_handle = htole16(terminus_handle);
268 fru->fru_rsi = htole16(fru_rsi);
269 fru->entity_type = htole16(entity_type);
270 fru->entity_instance_num = htole16(entity_instance_num);
271 fru->container_id = htole16(container_id);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600272
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500273 return pldm_pdr_add(repo, data, size, 0, false);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600274}
275
276const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi(
277 const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle,
278 uint16_t *entity_type, uint16_t *entity_instance_num,
279 uint16_t *container_id)
280{
281 assert(terminus_handle != NULL);
282 assert(entity_type != NULL);
283 assert(entity_instance_num != NULL);
284 assert(container_id != NULL);
285
286 uint8_t *data = NULL;
287 uint32_t size = 0;
288 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
289 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
290 while (curr_record != NULL) {
291 struct pldm_pdr_fru_record_set *fru =
292 (struct pldm_pdr_fru_record_set
293 *)(data + sizeof(struct pldm_pdr_hdr));
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500294 if (fru->fru_rsi == htole16(fru_rsi)) {
295 *terminus_handle = le16toh(fru->terminus_handle);
296 *entity_type = le16toh(fru->entity_type);
297 *entity_instance_num =
298 le16toh(fru->entity_instance_num);
299 *container_id = le16toh(fru->container_id);
Deepak Kodihallidb914672020-02-07 02:47:45 -0600300 return curr_record;
301 }
302 data = NULL;
303 curr_record = pldm_pdr_find_record_by_type(
304 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, &size);
305 }
306
307 *terminus_handle = 0;
308 *entity_type = 0;
309 *entity_instance_num = 0;
310 *container_id = 0;
311
312 return NULL;
313}
Deepak Kodihalli05787052020-03-10 01:54:08 -0500314
315typedef struct pldm_entity_association_tree {
316 pldm_entity_node *root;
317 uint16_t last_used_container_id;
318} pldm_entity_association_tree;
319
320typedef struct pldm_entity_node {
321 pldm_entity entity;
322 pldm_entity_node *first_child;
323 pldm_entity_node *next_sibling;
324 uint8_t association_type;
325} pldm_entity_node;
326
327static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
328{
329 assert(tree != NULL);
330 assert(tree->last_used_container_id != UINT16_MAX);
331
332 return ++tree->last_used_container_id;
333}
334
335pldm_entity_association_tree *pldm_entity_association_tree_init()
336{
337 pldm_entity_association_tree *tree =
338 malloc(sizeof(pldm_entity_association_tree));
339 assert(tree != NULL);
340 tree->root = NULL;
341 tree->last_used_container_id = 0;
342
343 return tree;
344}
345
346static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
347 uint16_t entity_type)
348{
349 assert(start != NULL);
350
351 /* Insert after the the last node that matches the input entity type, or
352 * at the end if no such match occurrs
353 */
354 while (start->next_sibling != NULL) {
355 uint16_t this_type = start->entity.entity_type;
356 pldm_entity_node *next = start->next_sibling;
357 if (this_type == entity_type &&
358 (this_type != next->entity.entity_type)) {
359 break;
360 }
361 start = start->next_sibling;
362 }
363
364 return start;
365}
366
367pldm_entity_node *
368pldm_entity_association_tree_add(pldm_entity_association_tree *tree,
369 pldm_entity *entity, pldm_entity_node *parent,
370 uint8_t association_type)
371{
372 assert(tree != NULL);
373 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
374 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
375 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
376 assert(node != NULL);
377 node->first_child = NULL;
378 node->next_sibling = NULL;
379 node->entity.entity_type = entity->entity_type;
380 node->entity.entity_instance_num = 1;
381 node->association_type = association_type;
382
383 if (tree->root == NULL) {
384 assert(parent == NULL);
385 tree->root = node;
386 /* container_id 0 here indicates this is the top-most entry */
387 node->entity.entity_container_id = 0;
388 } else if (parent != NULL && parent->first_child == NULL) {
389 parent->first_child = node;
390 node->entity.entity_container_id = next_container_id(tree);
391 } else {
392 pldm_entity_node *start =
393 parent == NULL ? tree->root : parent->first_child;
394 pldm_entity_node *prev =
395 find_insertion_at(start, entity->entity_type);
396 assert(prev != NULL);
397 pldm_entity_node *next = prev->next_sibling;
398 if (prev->entity.entity_type == entity->entity_type) {
399 assert(prev->entity.entity_instance_num != UINT16_MAX);
400 node->entity.entity_instance_num =
401 prev->entity.entity_instance_num + 1;
402 }
403 prev->next_sibling = node;
404 node->next_sibling = next;
405 node->entity.entity_container_id =
406 prev->entity.entity_container_id;
407 }
408 entity->entity_instance_num = node->entity.entity_instance_num;
409 entity->entity_container_id = node->entity.entity_container_id;
410
411 return node;
412}
413
414static void get_num_nodes(pldm_entity_node *node, size_t *num)
415{
416 if (node == NULL) {
417 return;
418 }
419
420 ++(*num);
421 get_num_nodes(node->next_sibling, num);
422 get_num_nodes(node->first_child, num);
423}
424
425static void entity_association_tree_visit(pldm_entity_node *node,
426 pldm_entity *entities, size_t *index)
427{
428 if (node == NULL) {
429 return;
430 }
431
432 pldm_entity *entity = &entities[*index];
433 ++(*index);
434 entity->entity_type = node->entity.entity_type;
435 entity->entity_instance_num = node->entity.entity_instance_num;
436 entity->entity_container_id = node->entity.entity_container_id;
437
438 entity_association_tree_visit(node->next_sibling, entities, index);
439 entity_association_tree_visit(node->first_child, entities, index);
440}
441
442void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
443 pldm_entity **entities, size_t *size)
444{
445 assert(tree != NULL);
446
447 *size = 0;
448 if (tree->root == NULL) {
449 return;
450 }
451
452 get_num_nodes(tree->root, size);
453 *entities = malloc(*size * sizeof(pldm_entity));
454 size_t index = 0;
455 entity_association_tree_visit(tree->root, *entities, &index);
456}
457
458static void entity_association_tree_destroy(pldm_entity_node *node)
459{
460 if (node == NULL) {
461 return;
462 }
463
464 entity_association_tree_destroy(node->next_sibling);
465 entity_association_tree_destroy(node->first_child);
466 free(node);
467}
468
469void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
470{
471 assert(tree != NULL);
472
473 entity_association_tree_destroy(tree->root);
474 free(tree);
475}
476
477inline bool pldm_entity_is_node_parent(pldm_entity_node *node)
478{
479 assert(node != NULL);
480
481 return node->first_child != NULL;
482}
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500483
484uint8_t pldm_entity_get_num_children(pldm_entity_node *node,
485 uint8_t association_type)
486{
487 assert(node != NULL);
488 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
489 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
490
491 size_t count = 0;
492 pldm_entity_node *curr = node->first_child;
493 while (curr != NULL) {
494 if (curr->association_type == association_type) {
495 ++count;
496 }
497 curr = curr->next_sibling;
498 }
499
500 assert(count < UINT8_MAX);
501 return count;
502}
503
504static void _entity_association_pdr_add_entry(pldm_entity_node *curr,
505 pldm_pdr *repo, uint16_t size,
506 uint8_t contained_count,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500507 uint8_t association_type,
508 bool is_remote)
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500509{
510 uint8_t pdr[size];
511 uint8_t *start = pdr;
512
513 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start;
514 hdr->version = 1;
515 hdr->record_handle = 0;
516 hdr->type = PLDM_PDR_ENTITY_ASSOCIATION;
517 hdr->record_change_num = 0;
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500518 hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr));
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500519 start += sizeof(struct pldm_pdr_hdr);
520
521 uint16_t *container_id = (uint16_t *)start;
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500522 *container_id = htole16(curr->first_child->entity.entity_container_id);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500523 start += sizeof(uint16_t);
524 *start = association_type;
525 start += sizeof(uint8_t);
526
527 pldm_entity *entity = (pldm_entity *)start;
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500528 entity->entity_type = htole16(curr->entity.entity_type);
529 entity->entity_instance_num = htole16(curr->entity.entity_instance_num);
530 entity->entity_container_id = htole16(curr->entity.entity_container_id);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500531 start += sizeof(pldm_entity);
532
533 *start = contained_count;
534 start += sizeof(uint8_t);
535
536 pldm_entity_node *node = curr->first_child;
537 while (node != NULL) {
538 if (node->association_type == association_type) {
539 pldm_entity *entity = (pldm_entity *)start;
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500540 entity->entity_type = htole16(node->entity.entity_type);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500541 entity->entity_instance_num =
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500542 htole16(node->entity.entity_instance_num);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500543 entity->entity_container_id =
Deepak Kodihallifb4dd7b2020-03-17 03:27:22 -0500544 htole16(node->entity.entity_container_id);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500545 start += sizeof(pldm_entity);
546 }
547 node = node->next_sibling;
548 }
549
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500550 pldm_pdr_add(repo, pdr, size, 0, is_remote);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500551}
552
553static void entity_association_pdr_add_entry(pldm_entity_node *curr,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500554 pldm_pdr *repo, bool is_remote)
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500555{
556 uint8_t num_logical_children =
557 pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_LOGICAL);
558 uint8_t num_physical_children =
559 pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL);
560
561 if (num_logical_children) {
562 uint16_t logical_pdr_size =
563 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
564 sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) +
565 (num_logical_children * sizeof(pldm_entity));
566 _entity_association_pdr_add_entry(
567 curr, repo, logical_pdr_size, num_logical_children,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500568 PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500569 }
570
571 if (num_physical_children) {
572 uint16_t physical_pdr_size =
573 sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) +
574 sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) +
575 (num_physical_children * sizeof(pldm_entity));
576 _entity_association_pdr_add_entry(
577 curr, repo, physical_pdr_size, num_physical_children,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500578 PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500579 }
580}
581
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500582static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo,
583 bool is_remote)
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500584{
585 if (curr == NULL) {
586 return;
587 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500588 entity_association_pdr_add_entry(curr, repo, is_remote);
589 entity_association_pdr_add(curr->next_sibling, repo, is_remote);
590 entity_association_pdr_add(curr->first_child, repo, is_remote);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500591}
592
593void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500594 pldm_pdr *repo, bool is_remote)
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500595{
596 assert(tree != NULL);
597 assert(repo != NULL);
598
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500599 entity_association_pdr_add(tree->root, repo, is_remote);
Deepak Kodihalli0a738f02020-03-10 01:56:21 -0500600}
Deepak Kodihalli3b28ba82020-03-30 07:39:47 -0500601
602void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity,
603 pldm_entity_node **out)
604{
605 if (node == NULL) {
606 return;
607 }
608
609 if (node->entity.entity_type == entity->entity_type &&
610 node->entity.entity_instance_num == entity->entity_instance_num) {
611 entity->entity_container_id = node->entity.entity_container_id;
612 *out = node;
613 return;
614 }
615
616 entity_association_tree_find(node->next_sibling, entity, out);
617 entity_association_tree_find(node->first_child, entity, out);
618}
619
620pldm_entity_node *
621pldm_entity_association_tree_find(pldm_entity_association_tree *tree,
622 pldm_entity *entity)
623{
624 assert(tree != NULL);
625
626 pldm_entity_node *node = NULL;
627 entity_association_tree_find(tree->root, entity, &node);
628 return node;
629}
630
631void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len,
632 size_t *num_entities,
633 pldm_entity **entities)
634{
635 assert(pdr != NULL);
636 assert(pdr_len >= sizeof(struct pldm_pdr_hdr) +
637 sizeof(struct pldm_pdr_entity_association));
638
639 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr;
640 assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION);
641
642 const uint8_t *start = (uint8_t *)pdr;
643 const uint8_t *end =
644 start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length);
645 start += sizeof(struct pldm_pdr_hdr);
646 struct pldm_pdr_entity_association *entity_association_pdr =
647 (struct pldm_pdr_entity_association *)start;
648 *num_entities = entity_association_pdr->num_children + 1;
649 assert(*num_entities >= 2);
650 *entities = malloc(sizeof(pldm_entity) * *num_entities);
651 assert(*entities != NULL);
652 assert(start + sizeof(struct pldm_pdr_entity_association) +
653 sizeof(pldm_entity) * (*num_entities - 2) ==
654 end);
655 (*entities)->entity_type =
656 le16toh(entity_association_pdr->container.entity_type);
657 (*entities)->entity_instance_num =
658 le16toh(entity_association_pdr->container.entity_instance_num);
659 (*entities)->entity_container_id =
660 le16toh(entity_association_pdr->container.entity_container_id);
661 pldm_entity *curr_entity = entity_association_pdr->children;
662 size_t i = 1;
663 while (i < *num_entities) {
664 (*entities + i)->entity_type =
665 le16toh(curr_entity->entity_type);
666 (*entities + i)->entity_instance_num =
667 le16toh(curr_entity->entity_instance_num);
668 (*entities + i)->entity_container_id =
669 le16toh(curr_entity->entity_container_id);
670 ++curr_entity;
671 ++i;
672 }
673}