blob: f2d4aa1536760dc3ac240d3a159d7d4e89a9e233 [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;
12} pldm_pdr_record;
13
14typedef struct pldm_pdr {
15 uint32_t record_count;
16 uint32_t size;
17 uint32_t last_used_record_handle;
18 pldm_pdr_record *first;
19 pldm_pdr_record *last;
20} pldm_pdr;
21
22static inline uint32_t get_next_record_handle(const pldm_pdr *repo,
23 const pldm_pdr_record *record)
24{
25 assert(repo != NULL);
26 assert(record != NULL);
27
28 if (record == repo->last) {
29 return 0;
30 }
31 return record->next->record_handle;
32}
33
34static void add_record(pldm_pdr *repo, pldm_pdr_record *record)
35{
36 assert(repo != NULL);
37 assert(record != NULL);
38
39 if (repo->first == NULL) {
40 assert(repo->last == NULL);
41 repo->first = record;
42 repo->last = record;
43 } else {
44 repo->last->next = record;
45 repo->last = record;
46 }
47 repo->size += record->size;
48 repo->last_used_record_handle = record->record_handle;
49 ++repo->record_count;
50}
51
52static inline uint32_t get_new_record_handle(const pldm_pdr *repo)
53{
54 assert(repo != NULL);
55 assert(repo->last_used_record_handle != UINT32_MAX);
56
57 return repo->last_used_record_handle + 1;
58}
59
60static pldm_pdr_record *make_new_record(const pldm_pdr *repo,
61 const uint8_t *data, uint32_t size,
62 uint32_t record_handle)
63{
64 assert(repo != NULL);
65 assert(size != 0);
66
67 pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record));
68 assert(record != NULL);
69 record->record_handle =
70 record_handle == 0 ? get_new_record_handle(repo) : record_handle;
71 record->size = size;
72 if (data != NULL) {
73 record->data = malloc(size);
74 assert(record->data != NULL);
75 memcpy(record->data, data, size);
76 /* If record handle is 0, that is an indication for this API to
77 * compute a new handle. For that reason, the computed handle
78 * needs to be populated in the PDR header. For a case where the
79 * caller supplied the record handle, it would exist in the
80 * header already.
81 */
82 if (!record_handle) {
83 struct pldm_pdr_hdr *hdr =
84 (struct pldm_pdr_hdr *)(record->data);
85 hdr->record_handle = record->record_handle;
86 }
87 }
88 record->next = NULL;
89
90 return record;
91}
92
93uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size,
94 uint32_t record_handle)
95{
96 assert(size != 0);
97 assert(data != NULL);
98
99 pldm_pdr_record *record =
100 make_new_record(repo, data, size, record_handle);
101 add_record(repo, record);
102
103 return record->record_handle;
104}
105
106pldm_pdr *pldm_pdr_init()
107{
108 pldm_pdr *repo = malloc(sizeof(pldm_pdr));
109 assert(repo != NULL);
110 repo->record_count = 0;
111 repo->size = 0;
112 repo->last_used_record_handle = 0;
113 repo->first = NULL;
114 repo->last = NULL;
115
116 return repo;
117}
118
119void pldm_pdr_destroy(pldm_pdr *repo)
120{
121 assert(repo != NULL);
122
123 pldm_pdr_record *record = repo->first;
124 while (record != NULL) {
125 pldm_pdr_record *next = record->next;
126 if (record->data) {
127 free(record->data);
128 record->data = NULL;
129 }
130 free(record);
131 record = next;
132 }
133 free(repo);
134}
135
136const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo,
137 uint32_t record_handle,
138 uint8_t **data, uint32_t *size,
139 uint32_t *next_record_handle)
140{
141 assert(repo != NULL);
142 assert(data != NULL);
143 assert(size != NULL);
144 assert(next_record_handle != NULL);
145
146 if (!record_handle && (repo->first != NULL)) {
147 record_handle = repo->first->record_handle;
148 }
149 pldm_pdr_record *record = repo->first;
150 while (record != NULL) {
151 if (record->record_handle == record_handle) {
152 *size = record->size;
153 *data = record->data;
154 *next_record_handle =
155 get_next_record_handle(repo, record);
156 return record;
157 }
158 record = record->next;
159 }
160
161 *size = 0;
162 *next_record_handle = 0;
163 return NULL;
164}
165
166const pldm_pdr_record *
167pldm_pdr_get_next_record(const pldm_pdr *repo,
168 const pldm_pdr_record *curr_record, uint8_t **data,
169 uint32_t *size, uint32_t *next_record_handle)
170{
171 assert(repo != NULL);
172 assert(curr_record != NULL);
173 assert(data != NULL);
174 assert(size != NULL);
175 assert(next_record_handle != NULL);
176
177 if (curr_record == repo->last) {
178 *data = NULL;
179 *size = 0;
180 *next_record_handle = get_next_record_handle(repo, curr_record);
181 return NULL;
182 }
183
184 *next_record_handle = get_next_record_handle(repo, curr_record->next);
185 *data = curr_record->next->data;
186 *size = curr_record->next->size;
187 return curr_record->next;
188}
189
190const pldm_pdr_record *
191pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type,
192 const pldm_pdr_record *curr_record, uint8_t **data,
193 uint32_t *size)
194{
195 assert(repo != NULL);
196 assert(data != NULL);
197 assert(size != NULL);
198
199 pldm_pdr_record *record = repo->first;
200 if (curr_record != NULL) {
201 record = curr_record->next;
202 }
203 while (record != NULL) {
204 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data;
205 if (hdr->type == pdr_type) {
206 *size = record->size;
207 *data = record->data;
208 return record;
209 }
210 record = record->next;
211 }
212
213 *size = 0;
214 return NULL;
215}
216
217uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo)
218{
219 assert(repo != NULL);
220
221 return repo->record_count;
222}
223
224uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo)
225{
226 assert(repo != NULL);
227
228 return repo->size;
229}
230
231uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo,
232 const pldm_pdr_record *record)
233{
234 assert(repo != NULL);
235 assert(record != NULL);
236
237 return record->record_handle;
238}
Deepak Kodihallidb914672020-02-07 02:47:45 -0600239
240uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle,
241 uint16_t fru_rsi, uint16_t entity_type,
242 uint16_t entity_instance_num,
243 uint16_t container_id)
244{
245 uint32_t size = sizeof(struct pldm_pdr_hdr) +
246 sizeof(struct pldm_pdr_fru_record_set);
247 uint8_t data[size];
248
249 struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data;
250 hdr->version = 1;
251 hdr->record_handle = 0;
252 hdr->type = PLDM_PDR_FRU_RECORD_SET;
253 hdr->record_change_num = 0;
254 hdr->length = sizeof(struct pldm_pdr_fru_record_set);
255 struct pldm_pdr_fru_record_set *fru =
256 (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr +
257 sizeof(struct pldm_pdr_hdr));
258 fru->terminus_handle = terminus_handle;
259 fru->fru_rsi = fru_rsi;
260 fru->entity_type = entity_type;
261 fru->entity_instance_num = entity_instance_num;
262 fru->container_id = container_id;
263
264 return pldm_pdr_add(repo, data, size, 0);
265}
266
267const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi(
268 const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle,
269 uint16_t *entity_type, uint16_t *entity_instance_num,
270 uint16_t *container_id)
271{
272 assert(terminus_handle != NULL);
273 assert(entity_type != NULL);
274 assert(entity_instance_num != NULL);
275 assert(container_id != NULL);
276
277 uint8_t *data = NULL;
278 uint32_t size = 0;
279 const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type(
280 repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size);
281 while (curr_record != NULL) {
282 struct pldm_pdr_fru_record_set *fru =
283 (struct pldm_pdr_fru_record_set
284 *)(data + sizeof(struct pldm_pdr_hdr));
285 if (fru->fru_rsi == fru_rsi) {
286 *terminus_handle = fru->terminus_handle;
287 *entity_type = fru->entity_type;
288 *entity_instance_num = fru->entity_instance_num;
289 *container_id = fru->container_id;
290 return curr_record;
291 }
292 data = NULL;
293 curr_record = pldm_pdr_find_record_by_type(
294 repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, &size);
295 }
296
297 *terminus_handle = 0;
298 *entity_type = 0;
299 *entity_instance_num = 0;
300 *container_id = 0;
301
302 return NULL;
303}
Deepak Kodihalli05787052020-03-10 01:54:08 -0500304
305typedef struct pldm_entity_association_tree {
306 pldm_entity_node *root;
307 uint16_t last_used_container_id;
308} pldm_entity_association_tree;
309
310typedef struct pldm_entity_node {
311 pldm_entity entity;
312 pldm_entity_node *first_child;
313 pldm_entity_node *next_sibling;
314 uint8_t association_type;
315} pldm_entity_node;
316
317static inline uint16_t next_container_id(pldm_entity_association_tree *tree)
318{
319 assert(tree != NULL);
320 assert(tree->last_used_container_id != UINT16_MAX);
321
322 return ++tree->last_used_container_id;
323}
324
325pldm_entity_association_tree *pldm_entity_association_tree_init()
326{
327 pldm_entity_association_tree *tree =
328 malloc(sizeof(pldm_entity_association_tree));
329 assert(tree != NULL);
330 tree->root = NULL;
331 tree->last_used_container_id = 0;
332
333 return tree;
334}
335
336static pldm_entity_node *find_insertion_at(pldm_entity_node *start,
337 uint16_t entity_type)
338{
339 assert(start != NULL);
340
341 /* Insert after the the last node that matches the input entity type, or
342 * at the end if no such match occurrs
343 */
344 while (start->next_sibling != NULL) {
345 uint16_t this_type = start->entity.entity_type;
346 pldm_entity_node *next = start->next_sibling;
347 if (this_type == entity_type &&
348 (this_type != next->entity.entity_type)) {
349 break;
350 }
351 start = start->next_sibling;
352 }
353
354 return start;
355}
356
357pldm_entity_node *
358pldm_entity_association_tree_add(pldm_entity_association_tree *tree,
359 pldm_entity *entity, pldm_entity_node *parent,
360 uint8_t association_type)
361{
362 assert(tree != NULL);
363 assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL ||
364 association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL);
365 pldm_entity_node *node = malloc(sizeof(pldm_entity_node));
366 assert(node != NULL);
367 node->first_child = NULL;
368 node->next_sibling = NULL;
369 node->entity.entity_type = entity->entity_type;
370 node->entity.entity_instance_num = 1;
371 node->association_type = association_type;
372
373 if (tree->root == NULL) {
374 assert(parent == NULL);
375 tree->root = node;
376 /* container_id 0 here indicates this is the top-most entry */
377 node->entity.entity_container_id = 0;
378 } else if (parent != NULL && parent->first_child == NULL) {
379 parent->first_child = node;
380 node->entity.entity_container_id = next_container_id(tree);
381 } else {
382 pldm_entity_node *start =
383 parent == NULL ? tree->root : parent->first_child;
384 pldm_entity_node *prev =
385 find_insertion_at(start, entity->entity_type);
386 assert(prev != NULL);
387 pldm_entity_node *next = prev->next_sibling;
388 if (prev->entity.entity_type == entity->entity_type) {
389 assert(prev->entity.entity_instance_num != UINT16_MAX);
390 node->entity.entity_instance_num =
391 prev->entity.entity_instance_num + 1;
392 }
393 prev->next_sibling = node;
394 node->next_sibling = next;
395 node->entity.entity_container_id =
396 prev->entity.entity_container_id;
397 }
398 entity->entity_instance_num = node->entity.entity_instance_num;
399 entity->entity_container_id = node->entity.entity_container_id;
400
401 return node;
402}
403
404static void get_num_nodes(pldm_entity_node *node, size_t *num)
405{
406 if (node == NULL) {
407 return;
408 }
409
410 ++(*num);
411 get_num_nodes(node->next_sibling, num);
412 get_num_nodes(node->first_child, num);
413}
414
415static void entity_association_tree_visit(pldm_entity_node *node,
416 pldm_entity *entities, size_t *index)
417{
418 if (node == NULL) {
419 return;
420 }
421
422 pldm_entity *entity = &entities[*index];
423 ++(*index);
424 entity->entity_type = node->entity.entity_type;
425 entity->entity_instance_num = node->entity.entity_instance_num;
426 entity->entity_container_id = node->entity.entity_container_id;
427
428 entity_association_tree_visit(node->next_sibling, entities, index);
429 entity_association_tree_visit(node->first_child, entities, index);
430}
431
432void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree,
433 pldm_entity **entities, size_t *size)
434{
435 assert(tree != NULL);
436
437 *size = 0;
438 if (tree->root == NULL) {
439 return;
440 }
441
442 get_num_nodes(tree->root, size);
443 *entities = malloc(*size * sizeof(pldm_entity));
444 size_t index = 0;
445 entity_association_tree_visit(tree->root, *entities, &index);
446}
447
448static void entity_association_tree_destroy(pldm_entity_node *node)
449{
450 if (node == NULL) {
451 return;
452 }
453
454 entity_association_tree_destroy(node->next_sibling);
455 entity_association_tree_destroy(node->first_child);
456 free(node);
457}
458
459void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree)
460{
461 assert(tree != NULL);
462
463 entity_association_tree_destroy(tree->root);
464 free(tree);
465}
466
467inline bool pldm_entity_is_node_parent(pldm_entity_node *node)
468{
469 assert(node != NULL);
470
471 return node->first_child != NULL;
472}