Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 1 | #include "pdr.h" |
| 2 | #include "platform.h" |
| 3 | #include <assert.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | typedef struct pldm_pdr_record { |
| 8 | uint32_t record_handle; |
| 9 | uint32_t size; |
| 10 | uint8_t *data; |
| 11 | struct pldm_pdr_record *next; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 12 | bool is_remote; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 13 | } pldm_pdr_record; |
| 14 | |
| 15 | typedef struct pldm_pdr { |
| 16 | uint32_t record_count; |
| 17 | uint32_t size; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 18 | pldm_pdr_record *first; |
| 19 | pldm_pdr_record *last; |
| 20 | } pldm_pdr; |
| 21 | |
| 22 | static 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 | |
| 34 | static 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; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 48 | ++repo->record_count; |
| 49 | } |
| 50 | |
| 51 | static inline uint32_t get_new_record_handle(const pldm_pdr *repo) |
| 52 | { |
| 53 | assert(repo != NULL); |
Deepak Kodihalli | b984873 | 2020-04-21 23:34:01 -0500 | [diff] [blame] | 54 | uint32_t last_used_hdl = |
| 55 | repo->last != NULL ? repo->last->record_handle : 0; |
| 56 | assert(last_used_hdl != UINT32_MAX); |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 57 | |
Deepak Kodihalli | b984873 | 2020-04-21 23:34:01 -0500 | [diff] [blame] | 58 | return last_used_hdl + 1; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static pldm_pdr_record *make_new_record(const pldm_pdr *repo, |
| 62 | const uint8_t *data, uint32_t size, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 63 | uint32_t record_handle, bool is_remote) |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 64 | { |
| 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 Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 73 | record->is_remote = is_remote; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 74 | 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 Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 87 | hdr->record_handle = htole32(record->record_handle); |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | record->next = NULL; |
| 91 | |
| 92 | return record; |
| 93 | } |
| 94 | |
| 95 | uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 96 | uint32_t record_handle, bool is_remote) |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 97 | { |
| 98 | assert(size != 0); |
| 99 | assert(data != NULL); |
| 100 | |
| 101 | pldm_pdr_record *record = |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 102 | make_new_record(repo, data, size, record_handle, is_remote); |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 103 | add_record(repo, record); |
| 104 | |
| 105 | return record->record_handle; |
| 106 | } |
| 107 | |
| 108 | pldm_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; |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 114 | repo->first = NULL; |
| 115 | repo->last = NULL; |
| 116 | |
| 117 | return repo; |
| 118 | } |
| 119 | |
| 120 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 121 | { |
| 122 | assert(repo != NULL); |
| 123 | |
| 124 | pldm_pdr_record *record = repo->first; |
| 125 | while (record != NULL) { |
| 126 | pldm_pdr_record *next = record->next; |
| 127 | if (record->data) { |
| 128 | free(record->data); |
| 129 | record->data = NULL; |
| 130 | } |
| 131 | free(record); |
| 132 | record = next; |
| 133 | } |
| 134 | free(repo); |
| 135 | } |
| 136 | |
| 137 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 138 | uint32_t record_handle, |
| 139 | uint8_t **data, uint32_t *size, |
| 140 | uint32_t *next_record_handle) |
| 141 | { |
| 142 | assert(repo != NULL); |
| 143 | assert(data != NULL); |
| 144 | assert(size != NULL); |
| 145 | assert(next_record_handle != NULL); |
| 146 | |
| 147 | if (!record_handle && (repo->first != NULL)) { |
| 148 | record_handle = repo->first->record_handle; |
| 149 | } |
| 150 | pldm_pdr_record *record = repo->first; |
| 151 | while (record != NULL) { |
| 152 | if (record->record_handle == record_handle) { |
| 153 | *size = record->size; |
| 154 | *data = record->data; |
| 155 | *next_record_handle = |
| 156 | get_next_record_handle(repo, record); |
| 157 | return record; |
| 158 | } |
| 159 | record = record->next; |
| 160 | } |
| 161 | |
| 162 | *size = 0; |
| 163 | *next_record_handle = 0; |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | const pldm_pdr_record * |
| 168 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 169 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 170 | uint32_t *size, uint32_t *next_record_handle) |
| 171 | { |
| 172 | assert(repo != NULL); |
| 173 | assert(curr_record != NULL); |
| 174 | assert(data != NULL); |
| 175 | assert(size != NULL); |
| 176 | assert(next_record_handle != NULL); |
| 177 | |
| 178 | if (curr_record == repo->last) { |
| 179 | *data = NULL; |
| 180 | *size = 0; |
| 181 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 186 | *data = curr_record->next->data; |
| 187 | *size = curr_record->next->size; |
| 188 | return curr_record->next; |
| 189 | } |
| 190 | |
| 191 | const pldm_pdr_record * |
| 192 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 193 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 194 | uint32_t *size) |
| 195 | { |
| 196 | assert(repo != NULL); |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 197 | |
| 198 | pldm_pdr_record *record = repo->first; |
| 199 | if (curr_record != NULL) { |
| 200 | record = curr_record->next; |
| 201 | } |
| 202 | while (record != NULL) { |
| 203 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 204 | if (hdr->type == pdr_type) { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 205 | if (data && size) { |
| 206 | *size = record->size; |
| 207 | *data = record->data; |
| 208 | } |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 209 | return record; |
| 210 | } |
| 211 | record = record->next; |
| 212 | } |
| 213 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 214 | if (size) { |
| 215 | *size = 0; |
| 216 | } |
Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 221 | { |
| 222 | assert(repo != NULL); |
| 223 | |
| 224 | return repo->record_count; |
| 225 | } |
| 226 | |
| 227 | uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo) |
| 228 | { |
| 229 | assert(repo != NULL); |
| 230 | |
| 231 | return repo->size; |
| 232 | } |
| 233 | |
| 234 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo, |
| 235 | const pldm_pdr_record *record) |
| 236 | { |
| 237 | assert(repo != NULL); |
| 238 | assert(record != NULL); |
| 239 | |
| 240 | return record->record_handle; |
| 241 | } |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 242 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 243 | inline bool pldm_pdr_record_is_remote(const pldm_pdr_record *record) |
| 244 | { |
| 245 | assert(record != NULL); |
| 246 | |
| 247 | return record->is_remote; |
| 248 | } |
| 249 | |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 250 | uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle, |
| 251 | uint16_t fru_rsi, uint16_t entity_type, |
| 252 | uint16_t entity_instance_num, |
| 253 | uint16_t container_id) |
| 254 | { |
| 255 | uint32_t size = sizeof(struct pldm_pdr_hdr) + |
| 256 | sizeof(struct pldm_pdr_fru_record_set); |
| 257 | uint8_t data[size]; |
| 258 | |
| 259 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data; |
| 260 | hdr->version = 1; |
| 261 | hdr->record_handle = 0; |
| 262 | hdr->type = PLDM_PDR_FRU_RECORD_SET; |
| 263 | hdr->record_change_num = 0; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 264 | hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set)); |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 265 | struct pldm_pdr_fru_record_set *fru = |
| 266 | (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr + |
| 267 | sizeof(struct pldm_pdr_hdr)); |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 268 | fru->terminus_handle = htole16(terminus_handle); |
| 269 | fru->fru_rsi = htole16(fru_rsi); |
| 270 | fru->entity_type = htole16(entity_type); |
| 271 | fru->entity_instance_num = htole16(entity_instance_num); |
| 272 | fru->container_id = htole16(container_id); |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 273 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 274 | return pldm_pdr_add(repo, data, size, 0, false); |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi( |
| 278 | const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle, |
| 279 | uint16_t *entity_type, uint16_t *entity_instance_num, |
| 280 | uint16_t *container_id) |
| 281 | { |
| 282 | assert(terminus_handle != NULL); |
| 283 | assert(entity_type != NULL); |
| 284 | assert(entity_instance_num != NULL); |
| 285 | assert(container_id != NULL); |
| 286 | |
| 287 | uint8_t *data = NULL; |
| 288 | uint32_t size = 0; |
| 289 | const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type( |
| 290 | repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size); |
| 291 | while (curr_record != NULL) { |
| 292 | struct pldm_pdr_fru_record_set *fru = |
| 293 | (struct pldm_pdr_fru_record_set |
| 294 | *)(data + sizeof(struct pldm_pdr_hdr)); |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 295 | if (fru->fru_rsi == htole16(fru_rsi)) { |
| 296 | *terminus_handle = le16toh(fru->terminus_handle); |
| 297 | *entity_type = le16toh(fru->entity_type); |
| 298 | *entity_instance_num = |
| 299 | le16toh(fru->entity_instance_num); |
| 300 | *container_id = le16toh(fru->container_id); |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 301 | return curr_record; |
| 302 | } |
| 303 | data = NULL; |
| 304 | curr_record = pldm_pdr_find_record_by_type( |
| 305 | repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, &size); |
| 306 | } |
| 307 | |
| 308 | *terminus_handle = 0; |
| 309 | *entity_type = 0; |
| 310 | *entity_instance_num = 0; |
| 311 | *container_id = 0; |
| 312 | |
| 313 | return NULL; |
| 314 | } |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 315 | |
| 316 | typedef struct pldm_entity_association_tree { |
| 317 | pldm_entity_node *root; |
| 318 | uint16_t last_used_container_id; |
| 319 | } pldm_entity_association_tree; |
| 320 | |
| 321 | typedef struct pldm_entity_node { |
| 322 | pldm_entity entity; |
| 323 | pldm_entity_node *first_child; |
| 324 | pldm_entity_node *next_sibling; |
| 325 | uint8_t association_type; |
| 326 | } pldm_entity_node; |
| 327 | |
| 328 | static inline uint16_t next_container_id(pldm_entity_association_tree *tree) |
| 329 | { |
| 330 | assert(tree != NULL); |
| 331 | assert(tree->last_used_container_id != UINT16_MAX); |
| 332 | |
| 333 | return ++tree->last_used_container_id; |
| 334 | } |
| 335 | |
George Liu | d9855ab | 2021-05-13 09:41:31 +0800 | [diff] [blame^] | 336 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 337 | { |
| 338 | assert(node != NULL); |
| 339 | |
| 340 | return node->entity; |
| 341 | } |
| 342 | |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 343 | pldm_entity_association_tree *pldm_entity_association_tree_init() |
| 344 | { |
| 345 | pldm_entity_association_tree *tree = |
| 346 | malloc(sizeof(pldm_entity_association_tree)); |
| 347 | assert(tree != NULL); |
| 348 | tree->root = NULL; |
| 349 | tree->last_used_container_id = 0; |
| 350 | |
| 351 | return tree; |
| 352 | } |
| 353 | |
| 354 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 355 | uint16_t entity_type) |
| 356 | { |
| 357 | assert(start != NULL); |
| 358 | |
| 359 | /* Insert after the the last node that matches the input entity type, or |
| 360 | * at the end if no such match occurrs |
| 361 | */ |
| 362 | while (start->next_sibling != NULL) { |
| 363 | uint16_t this_type = start->entity.entity_type; |
| 364 | pldm_entity_node *next = start->next_sibling; |
| 365 | if (this_type == entity_type && |
| 366 | (this_type != next->entity.entity_type)) { |
| 367 | break; |
| 368 | } |
| 369 | start = start->next_sibling; |
| 370 | } |
| 371 | |
| 372 | return start; |
| 373 | } |
| 374 | |
| 375 | pldm_entity_node * |
| 376 | pldm_entity_association_tree_add(pldm_entity_association_tree *tree, |
| 377 | pldm_entity *entity, pldm_entity_node *parent, |
| 378 | uint8_t association_type) |
| 379 | { |
| 380 | assert(tree != NULL); |
| 381 | assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 382 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 383 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
| 384 | assert(node != NULL); |
| 385 | node->first_child = NULL; |
| 386 | node->next_sibling = NULL; |
| 387 | node->entity.entity_type = entity->entity_type; |
| 388 | node->entity.entity_instance_num = 1; |
| 389 | node->association_type = association_type; |
| 390 | |
| 391 | if (tree->root == NULL) { |
| 392 | assert(parent == NULL); |
| 393 | tree->root = node; |
| 394 | /* container_id 0 here indicates this is the top-most entry */ |
| 395 | node->entity.entity_container_id = 0; |
| 396 | } else if (parent != NULL && parent->first_child == NULL) { |
| 397 | parent->first_child = node; |
| 398 | node->entity.entity_container_id = next_container_id(tree); |
| 399 | } else { |
| 400 | pldm_entity_node *start = |
| 401 | parent == NULL ? tree->root : parent->first_child; |
| 402 | pldm_entity_node *prev = |
| 403 | find_insertion_at(start, entity->entity_type); |
| 404 | assert(prev != NULL); |
| 405 | pldm_entity_node *next = prev->next_sibling; |
| 406 | if (prev->entity.entity_type == entity->entity_type) { |
| 407 | assert(prev->entity.entity_instance_num != UINT16_MAX); |
| 408 | node->entity.entity_instance_num = |
| 409 | prev->entity.entity_instance_num + 1; |
| 410 | } |
| 411 | prev->next_sibling = node; |
| 412 | node->next_sibling = next; |
| 413 | node->entity.entity_container_id = |
| 414 | prev->entity.entity_container_id; |
| 415 | } |
| 416 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 417 | entity->entity_container_id = node->entity.entity_container_id; |
| 418 | |
| 419 | return node; |
| 420 | } |
| 421 | |
| 422 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 423 | { |
| 424 | if (node == NULL) { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | ++(*num); |
| 429 | get_num_nodes(node->next_sibling, num); |
| 430 | get_num_nodes(node->first_child, num); |
| 431 | } |
| 432 | |
| 433 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 434 | pldm_entity *entities, size_t *index) |
| 435 | { |
| 436 | if (node == NULL) { |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | pldm_entity *entity = &entities[*index]; |
| 441 | ++(*index); |
| 442 | entity->entity_type = node->entity.entity_type; |
| 443 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 444 | entity->entity_container_id = node->entity.entity_container_id; |
| 445 | |
| 446 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 447 | entity_association_tree_visit(node->first_child, entities, index); |
| 448 | } |
| 449 | |
| 450 | void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree, |
| 451 | pldm_entity **entities, size_t *size) |
| 452 | { |
| 453 | assert(tree != NULL); |
| 454 | |
| 455 | *size = 0; |
| 456 | if (tree->root == NULL) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | get_num_nodes(tree->root, size); |
| 461 | *entities = malloc(*size * sizeof(pldm_entity)); |
| 462 | size_t index = 0; |
| 463 | entity_association_tree_visit(tree->root, *entities, &index); |
| 464 | } |
| 465 | |
| 466 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 467 | { |
| 468 | if (node == NULL) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | entity_association_tree_destroy(node->next_sibling); |
| 473 | entity_association_tree_destroy(node->first_child); |
| 474 | free(node); |
| 475 | } |
| 476 | |
| 477 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 478 | { |
| 479 | assert(tree != NULL); |
| 480 | |
| 481 | entity_association_tree_destroy(tree->root); |
| 482 | free(tree); |
| 483 | } |
| 484 | |
| 485 | inline bool pldm_entity_is_node_parent(pldm_entity_node *node) |
| 486 | { |
| 487 | assert(node != NULL); |
| 488 | |
| 489 | return node->first_child != NULL; |
| 490 | } |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 491 | |
| 492 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 493 | uint8_t association_type) |
| 494 | { |
| 495 | assert(node != NULL); |
| 496 | assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 497 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 498 | |
| 499 | size_t count = 0; |
| 500 | pldm_entity_node *curr = node->first_child; |
| 501 | while (curr != NULL) { |
| 502 | if (curr->association_type == association_type) { |
| 503 | ++count; |
| 504 | } |
| 505 | curr = curr->next_sibling; |
| 506 | } |
| 507 | |
| 508 | assert(count < UINT8_MAX); |
| 509 | return count; |
| 510 | } |
| 511 | |
| 512 | static void _entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 513 | pldm_pdr *repo, uint16_t size, |
| 514 | uint8_t contained_count, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 515 | uint8_t association_type, |
| 516 | bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 517 | { |
| 518 | uint8_t pdr[size]; |
| 519 | uint8_t *start = pdr; |
| 520 | |
| 521 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 522 | hdr->version = 1; |
| 523 | hdr->record_handle = 0; |
| 524 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 525 | hdr->record_change_num = 0; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 526 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 527 | start += sizeof(struct pldm_pdr_hdr); |
| 528 | |
| 529 | uint16_t *container_id = (uint16_t *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 530 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 531 | start += sizeof(uint16_t); |
| 532 | *start = association_type; |
| 533 | start += sizeof(uint8_t); |
| 534 | |
| 535 | pldm_entity *entity = (pldm_entity *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 536 | entity->entity_type = htole16(curr->entity.entity_type); |
| 537 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 538 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 539 | start += sizeof(pldm_entity); |
| 540 | |
| 541 | *start = contained_count; |
| 542 | start += sizeof(uint8_t); |
| 543 | |
| 544 | pldm_entity_node *node = curr->first_child; |
| 545 | while (node != NULL) { |
| 546 | if (node->association_type == association_type) { |
| 547 | pldm_entity *entity = (pldm_entity *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 548 | entity->entity_type = htole16(node->entity.entity_type); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 549 | entity->entity_instance_num = |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 550 | htole16(node->entity.entity_instance_num); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 551 | entity->entity_container_id = |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 552 | htole16(node->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 553 | start += sizeof(pldm_entity); |
| 554 | } |
| 555 | node = node->next_sibling; |
| 556 | } |
| 557 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 558 | pldm_pdr_add(repo, pdr, size, 0, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static void entity_association_pdr_add_entry(pldm_entity_node *curr, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 562 | pldm_pdr *repo, bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 563 | { |
| 564 | uint8_t num_logical_children = |
| 565 | pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 566 | uint8_t num_physical_children = |
| 567 | pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
| 568 | |
| 569 | if (num_logical_children) { |
| 570 | uint16_t logical_pdr_size = |
| 571 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 572 | sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) + |
| 573 | (num_logical_children * sizeof(pldm_entity)); |
| 574 | _entity_association_pdr_add_entry( |
| 575 | curr, repo, logical_pdr_size, num_logical_children, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 576 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (num_physical_children) { |
| 580 | uint16_t physical_pdr_size = |
| 581 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 582 | sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) + |
| 583 | (num_physical_children * sizeof(pldm_entity)); |
| 584 | _entity_association_pdr_add_entry( |
| 585 | curr, repo, physical_pdr_size, num_physical_children, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 586 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 590 | static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 591 | bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 592 | { |
| 593 | if (curr == NULL) { |
| 594 | return; |
| 595 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 596 | entity_association_pdr_add_entry(curr, repo, is_remote); |
| 597 | entity_association_pdr_add(curr->next_sibling, repo, is_remote); |
| 598 | entity_association_pdr_add(curr->first_child, repo, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 602 | pldm_pdr *repo, bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 603 | { |
| 604 | assert(tree != NULL); |
| 605 | assert(repo != NULL); |
| 606 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 607 | entity_association_pdr_add(tree->root, repo, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 608 | } |
Deepak Kodihalli | 3b28ba8 | 2020-03-30 07:39:47 -0500 | [diff] [blame] | 609 | |
Deepak Kodihalli | 4a68093 | 2020-04-22 03:31:17 -0500 | [diff] [blame] | 610 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 611 | { |
| 612 | assert(repo != NULL); |
| 613 | bool removed = false; |
| 614 | |
| 615 | pldm_pdr_record *record = repo->first; |
| 616 | pldm_pdr_record *prev = NULL; |
| 617 | while (record != NULL) { |
| 618 | pldm_pdr_record *next = record->next; |
| 619 | if (record->is_remote == true) { |
| 620 | if (repo->first == record) { |
| 621 | repo->first = next; |
| 622 | } else { |
| 623 | prev->next = next; |
| 624 | } |
| 625 | if (repo->last == record) { |
| 626 | repo->last = prev; |
| 627 | } |
| 628 | if (record->data) { |
| 629 | free(record->data); |
| 630 | } |
| 631 | --repo->record_count; |
| 632 | repo->size -= record->size; |
| 633 | free(record); |
| 634 | removed = true; |
| 635 | } else { |
| 636 | prev = record; |
| 637 | } |
| 638 | record = next; |
| 639 | } |
| 640 | |
| 641 | if (removed == true) { |
| 642 | record = repo->first; |
| 643 | uint32_t record_handle = 0; |
| 644 | while (record != NULL) { |
| 645 | record->record_handle = ++record_handle; |
| 646 | if (record->data != NULL) { |
| 647 | struct pldm_pdr_hdr *hdr = |
| 648 | (struct pldm_pdr_hdr *)(record->data); |
| 649 | hdr->record_handle = |
| 650 | htole32(record->record_handle); |
| 651 | } |
| 652 | record = record->next; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Deepak Kodihalli | 3b28ba8 | 2020-03-30 07:39:47 -0500 | [diff] [blame] | 657 | void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity, |
| 658 | pldm_entity_node **out) |
| 659 | { |
| 660 | if (node == NULL) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | if (node->entity.entity_type == entity->entity_type && |
| 665 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 666 | entity->entity_container_id = node->entity.entity_container_id; |
| 667 | *out = node; |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | entity_association_tree_find(node->next_sibling, entity, out); |
| 672 | entity_association_tree_find(node->first_child, entity, out); |
| 673 | } |
| 674 | |
| 675 | pldm_entity_node * |
| 676 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 677 | pldm_entity *entity) |
| 678 | { |
| 679 | assert(tree != NULL); |
| 680 | |
| 681 | pldm_entity_node *node = NULL; |
| 682 | entity_association_tree_find(tree->root, entity, &node); |
| 683 | return node; |
| 684 | } |
| 685 | |
| 686 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 687 | size_t *num_entities, |
| 688 | pldm_entity **entities) |
| 689 | { |
| 690 | assert(pdr != NULL); |
| 691 | assert(pdr_len >= sizeof(struct pldm_pdr_hdr) + |
| 692 | sizeof(struct pldm_pdr_entity_association)); |
| 693 | |
| 694 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
| 695 | assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION); |
| 696 | |
| 697 | const uint8_t *start = (uint8_t *)pdr; |
| 698 | const uint8_t *end = |
| 699 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
| 700 | start += sizeof(struct pldm_pdr_hdr); |
| 701 | struct pldm_pdr_entity_association *entity_association_pdr = |
| 702 | (struct pldm_pdr_entity_association *)start; |
| 703 | *num_entities = entity_association_pdr->num_children + 1; |
| 704 | assert(*num_entities >= 2); |
| 705 | *entities = malloc(sizeof(pldm_entity) * *num_entities); |
| 706 | assert(*entities != NULL); |
| 707 | assert(start + sizeof(struct pldm_pdr_entity_association) + |
| 708 | sizeof(pldm_entity) * (*num_entities - 2) == |
| 709 | end); |
| 710 | (*entities)->entity_type = |
| 711 | le16toh(entity_association_pdr->container.entity_type); |
| 712 | (*entities)->entity_instance_num = |
| 713 | le16toh(entity_association_pdr->container.entity_instance_num); |
| 714 | (*entities)->entity_container_id = |
| 715 | le16toh(entity_association_pdr->container.entity_container_id); |
| 716 | pldm_entity *curr_entity = entity_association_pdr->children; |
| 717 | size_t i = 1; |
| 718 | while (i < *num_entities) { |
| 719 | (*entities + i)->entity_type = |
| 720 | le16toh(curr_entity->entity_type); |
| 721 | (*entities + i)->entity_instance_num = |
| 722 | le16toh(curr_entity->entity_instance_num); |
| 723 | (*entities + i)->entity_container_id = |
| 724 | le16toh(curr_entity->entity_container_id); |
| 725 | ++curr_entity; |
| 726 | ++i; |
| 727 | } |
| 728 | } |