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; |
George Liu | fe77eea | 2021-04-28 15:08:07 +0800 | [diff] [blame] | 323 | pldm_entity_node *parent; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 324 | pldm_entity_node *first_child; |
| 325 | pldm_entity_node *next_sibling; |
| 326 | uint8_t association_type; |
| 327 | } pldm_entity_node; |
| 328 | |
| 329 | static inline uint16_t next_container_id(pldm_entity_association_tree *tree) |
| 330 | { |
| 331 | assert(tree != NULL); |
| 332 | assert(tree->last_used_container_id != UINT16_MAX); |
| 333 | |
| 334 | return ++tree->last_used_container_id; |
| 335 | } |
| 336 | |
George Liu | d9855ab | 2021-05-13 09:41:31 +0800 | [diff] [blame] | 337 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 338 | { |
| 339 | assert(node != NULL); |
| 340 | |
| 341 | return node->entity; |
| 342 | } |
| 343 | |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 344 | pldm_entity_association_tree *pldm_entity_association_tree_init() |
| 345 | { |
| 346 | pldm_entity_association_tree *tree = |
| 347 | malloc(sizeof(pldm_entity_association_tree)); |
| 348 | assert(tree != NULL); |
| 349 | tree->root = NULL; |
| 350 | tree->last_used_container_id = 0; |
| 351 | |
| 352 | return tree; |
| 353 | } |
| 354 | |
| 355 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 356 | uint16_t entity_type) |
| 357 | { |
| 358 | assert(start != NULL); |
| 359 | |
| 360 | /* Insert after the the last node that matches the input entity type, or |
| 361 | * at the end if no such match occurrs |
| 362 | */ |
| 363 | while (start->next_sibling != NULL) { |
| 364 | uint16_t this_type = start->entity.entity_type; |
| 365 | pldm_entity_node *next = start->next_sibling; |
| 366 | if (this_type == entity_type && |
| 367 | (this_type != next->entity.entity_type)) { |
| 368 | break; |
| 369 | } |
| 370 | start = start->next_sibling; |
| 371 | } |
| 372 | |
| 373 | return start; |
| 374 | } |
| 375 | |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 376 | pldm_entity_node *pldm_entity_association_tree_add( |
| 377 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 378 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 379 | uint8_t association_type) |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 380 | { |
| 381 | assert(tree != NULL); |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 382 | assert(entity != NULL); |
| 383 | |
| 384 | if (entity_instance_number != 0xFFFF && parent != NULL) { |
| 385 | pldm_entity node; |
| 386 | node.entity_type = entity->entity_type; |
| 387 | node.entity_instance_num = entity_instance_number; |
| 388 | if (pldm_is_current_parent_child(parent, &node)) { |
| 389 | return NULL; |
| 390 | } |
| 391 | } |
| 392 | |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 393 | assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 394 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 395 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
| 396 | assert(node != NULL); |
George Liu | fe77eea | 2021-04-28 15:08:07 +0800 | [diff] [blame] | 397 | node->parent = NULL; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 398 | node->first_child = NULL; |
| 399 | node->next_sibling = NULL; |
| 400 | node->entity.entity_type = entity->entity_type; |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 401 | node->entity.entity_instance_num = |
| 402 | entity_instance_number != 0xFFFF ? entity_instance_number : 1; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 403 | node->association_type = association_type; |
| 404 | |
| 405 | if (tree->root == NULL) { |
| 406 | assert(parent == NULL); |
| 407 | tree->root = node; |
| 408 | /* container_id 0 here indicates this is the top-most entry */ |
| 409 | node->entity.entity_container_id = 0; |
| 410 | } else if (parent != NULL && parent->first_child == NULL) { |
| 411 | parent->first_child = node; |
George Liu | fe77eea | 2021-04-28 15:08:07 +0800 | [diff] [blame] | 412 | node->parent = parent; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 413 | node->entity.entity_container_id = next_container_id(tree); |
| 414 | } else { |
| 415 | pldm_entity_node *start = |
| 416 | parent == NULL ? tree->root : parent->first_child; |
| 417 | pldm_entity_node *prev = |
| 418 | find_insertion_at(start, entity->entity_type); |
| 419 | assert(prev != NULL); |
| 420 | pldm_entity_node *next = prev->next_sibling; |
| 421 | if (prev->entity.entity_type == entity->entity_type) { |
| 422 | assert(prev->entity.entity_instance_num != UINT16_MAX); |
| 423 | node->entity.entity_instance_num = |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 424 | entity_instance_number != 0xFFFF |
| 425 | ? entity_instance_number |
| 426 | : prev->entity.entity_instance_num + 1; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 427 | } |
| 428 | prev->next_sibling = node; |
George Liu | fe77eea | 2021-04-28 15:08:07 +0800 | [diff] [blame] | 429 | node->parent = prev->parent; |
Deepak Kodihalli | 0578705 | 2020-03-10 01:54:08 -0500 | [diff] [blame] | 430 | node->next_sibling = next; |
| 431 | node->entity.entity_container_id = |
| 432 | prev->entity.entity_container_id; |
| 433 | } |
| 434 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 435 | entity->entity_container_id = node->entity.entity_container_id; |
| 436 | |
| 437 | return node; |
| 438 | } |
| 439 | |
| 440 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 441 | { |
| 442 | if (node == NULL) { |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | ++(*num); |
| 447 | get_num_nodes(node->next_sibling, num); |
| 448 | get_num_nodes(node->first_child, num); |
| 449 | } |
| 450 | |
| 451 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 452 | pldm_entity *entities, size_t *index) |
| 453 | { |
| 454 | if (node == NULL) { |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | pldm_entity *entity = &entities[*index]; |
| 459 | ++(*index); |
| 460 | entity->entity_type = node->entity.entity_type; |
| 461 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 462 | entity->entity_container_id = node->entity.entity_container_id; |
| 463 | |
| 464 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 465 | entity_association_tree_visit(node->first_child, entities, index); |
| 466 | } |
| 467 | |
| 468 | void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree, |
| 469 | pldm_entity **entities, size_t *size) |
| 470 | { |
| 471 | assert(tree != NULL); |
| 472 | |
| 473 | *size = 0; |
| 474 | if (tree->root == NULL) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | get_num_nodes(tree->root, size); |
| 479 | *entities = malloc(*size * sizeof(pldm_entity)); |
| 480 | size_t index = 0; |
| 481 | entity_association_tree_visit(tree->root, *entities, &index); |
| 482 | } |
| 483 | |
| 484 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 485 | { |
| 486 | if (node == NULL) { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | entity_association_tree_destroy(node->next_sibling); |
| 491 | entity_association_tree_destroy(node->first_child); |
| 492 | free(node); |
| 493 | } |
| 494 | |
| 495 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 496 | { |
| 497 | assert(tree != NULL); |
| 498 | |
| 499 | entity_association_tree_destroy(tree->root); |
| 500 | free(tree); |
| 501 | } |
| 502 | |
| 503 | inline bool pldm_entity_is_node_parent(pldm_entity_node *node) |
| 504 | { |
| 505 | assert(node != NULL); |
| 506 | |
| 507 | return node->first_child != NULL; |
| 508 | } |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 509 | |
George Liu | fe77eea | 2021-04-28 15:08:07 +0800 | [diff] [blame] | 510 | inline pldm_entity_node *pldm_entity_get_parent(pldm_entity_node *node) |
| 511 | { |
| 512 | assert(node != NULL); |
| 513 | |
| 514 | return node->parent; |
| 515 | } |
| 516 | |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 517 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 518 | uint8_t association_type) |
| 519 | { |
| 520 | assert(node != NULL); |
| 521 | assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 522 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 523 | |
| 524 | size_t count = 0; |
| 525 | pldm_entity_node *curr = node->first_child; |
| 526 | while (curr != NULL) { |
| 527 | if (curr->association_type == association_type) { |
| 528 | ++count; |
| 529 | } |
| 530 | curr = curr->next_sibling; |
| 531 | } |
| 532 | |
| 533 | assert(count < UINT8_MAX); |
| 534 | return count; |
| 535 | } |
| 536 | |
George Liu | 6a6c3ab | 2021-06-22 16:12:42 +0800 | [diff] [blame] | 537 | bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node) |
| 538 | { |
| 539 | assert(parent != NULL); |
| 540 | assert(node != NULL); |
| 541 | |
| 542 | pldm_entity_node *curr = parent->first_child; |
| 543 | while (curr != NULL) { |
| 544 | if (node->entity_type == curr->entity.entity_type && |
| 545 | node->entity_instance_num == |
| 546 | curr->entity.entity_instance_num) { |
| 547 | |
| 548 | return true; |
| 549 | } |
| 550 | curr = curr->next_sibling; |
| 551 | } |
| 552 | |
| 553 | return false; |
| 554 | } |
| 555 | |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 556 | static void _entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 557 | pldm_pdr *repo, uint16_t size, |
| 558 | uint8_t contained_count, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 559 | uint8_t association_type, |
| 560 | bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 561 | { |
| 562 | uint8_t pdr[size]; |
| 563 | uint8_t *start = pdr; |
| 564 | |
| 565 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 566 | hdr->version = 1; |
| 567 | hdr->record_handle = 0; |
| 568 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 569 | hdr->record_change_num = 0; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 570 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 571 | start += sizeof(struct pldm_pdr_hdr); |
| 572 | |
| 573 | uint16_t *container_id = (uint16_t *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 574 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 575 | start += sizeof(uint16_t); |
| 576 | *start = association_type; |
| 577 | start += sizeof(uint8_t); |
| 578 | |
| 579 | pldm_entity *entity = (pldm_entity *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 580 | entity->entity_type = htole16(curr->entity.entity_type); |
| 581 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 582 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 583 | start += sizeof(pldm_entity); |
| 584 | |
| 585 | *start = contained_count; |
| 586 | start += sizeof(uint8_t); |
| 587 | |
| 588 | pldm_entity_node *node = curr->first_child; |
| 589 | while (node != NULL) { |
| 590 | if (node->association_type == association_type) { |
| 591 | pldm_entity *entity = (pldm_entity *)start; |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 592 | entity->entity_type = htole16(node->entity.entity_type); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 593 | entity->entity_instance_num = |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 594 | htole16(node->entity.entity_instance_num); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 595 | entity->entity_container_id = |
Deepak Kodihalli | fb4dd7b | 2020-03-17 03:27:22 -0500 | [diff] [blame] | 596 | htole16(node->entity.entity_container_id); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 597 | start += sizeof(pldm_entity); |
| 598 | } |
| 599 | node = node->next_sibling; |
| 600 | } |
| 601 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 602 | pldm_pdr_add(repo, pdr, size, 0, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void entity_association_pdr_add_entry(pldm_entity_node *curr, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 606 | pldm_pdr *repo, bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 607 | { |
| 608 | uint8_t num_logical_children = |
| 609 | pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 610 | uint8_t num_physical_children = |
| 611 | pldm_entity_get_num_children(curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
| 612 | |
| 613 | if (num_logical_children) { |
| 614 | uint16_t logical_pdr_size = |
| 615 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 616 | sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) + |
| 617 | (num_logical_children * sizeof(pldm_entity)); |
| 618 | _entity_association_pdr_add_entry( |
| 619 | curr, repo, logical_pdr_size, num_logical_children, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 620 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | if (num_physical_children) { |
| 624 | uint16_t physical_pdr_size = |
| 625 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 626 | sizeof(uint8_t) + sizeof(pldm_entity) + sizeof(uint8_t) + |
| 627 | (num_physical_children * sizeof(pldm_entity)); |
| 628 | _entity_association_pdr_add_entry( |
| 629 | curr, repo, physical_pdr_size, num_physical_children, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 630 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 634 | static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 635 | bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 636 | { |
| 637 | if (curr == NULL) { |
| 638 | return; |
| 639 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 640 | entity_association_pdr_add_entry(curr, repo, is_remote); |
| 641 | entity_association_pdr_add(curr->next_sibling, repo, is_remote); |
| 642 | entity_association_pdr_add(curr->first_child, repo, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 646 | pldm_pdr *repo, bool is_remote) |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 647 | { |
| 648 | assert(tree != NULL); |
| 649 | assert(repo != NULL); |
| 650 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 651 | entity_association_pdr_add(tree->root, repo, is_remote); |
Deepak Kodihalli | 0a738f0 | 2020-03-10 01:56:21 -0500 | [diff] [blame] | 652 | } |
Deepak Kodihalli | 3b28ba8 | 2020-03-30 07:39:47 -0500 | [diff] [blame] | 653 | |
Deepak Kodihalli | 4a68093 | 2020-04-22 03:31:17 -0500 | [diff] [blame] | 654 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 655 | { |
| 656 | assert(repo != NULL); |
| 657 | bool removed = false; |
| 658 | |
| 659 | pldm_pdr_record *record = repo->first; |
| 660 | pldm_pdr_record *prev = NULL; |
| 661 | while (record != NULL) { |
| 662 | pldm_pdr_record *next = record->next; |
| 663 | if (record->is_remote == true) { |
| 664 | if (repo->first == record) { |
| 665 | repo->first = next; |
| 666 | } else { |
| 667 | prev->next = next; |
| 668 | } |
| 669 | if (repo->last == record) { |
| 670 | repo->last = prev; |
| 671 | } |
| 672 | if (record->data) { |
| 673 | free(record->data); |
| 674 | } |
| 675 | --repo->record_count; |
| 676 | repo->size -= record->size; |
| 677 | free(record); |
| 678 | removed = true; |
| 679 | } else { |
| 680 | prev = record; |
| 681 | } |
| 682 | record = next; |
| 683 | } |
| 684 | |
| 685 | if (removed == true) { |
| 686 | record = repo->first; |
| 687 | uint32_t record_handle = 0; |
| 688 | while (record != NULL) { |
| 689 | record->record_handle = ++record_handle; |
| 690 | if (record->data != NULL) { |
| 691 | struct pldm_pdr_hdr *hdr = |
| 692 | (struct pldm_pdr_hdr *)(record->data); |
| 693 | hdr->record_handle = |
| 694 | htole32(record->record_handle); |
| 695 | } |
| 696 | record = record->next; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Deepak Kodihalli | 3b28ba8 | 2020-03-30 07:39:47 -0500 | [diff] [blame] | 701 | void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity, |
| 702 | pldm_entity_node **out) |
| 703 | { |
| 704 | if (node == NULL) { |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | if (node->entity.entity_type == entity->entity_type && |
| 709 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 710 | entity->entity_container_id = node->entity.entity_container_id; |
| 711 | *out = node; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | entity_association_tree_find(node->next_sibling, entity, out); |
| 716 | entity_association_tree_find(node->first_child, entity, out); |
| 717 | } |
| 718 | |
| 719 | pldm_entity_node * |
| 720 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 721 | pldm_entity *entity) |
| 722 | { |
| 723 | assert(tree != NULL); |
| 724 | |
| 725 | pldm_entity_node *node = NULL; |
| 726 | entity_association_tree_find(tree->root, entity, &node); |
| 727 | return node; |
| 728 | } |
| 729 | |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 730 | static void entity_association_tree_copy(pldm_entity_node *org_node, |
| 731 | pldm_entity_node **new_node) |
| 732 | { |
| 733 | if (org_node == NULL) { |
| 734 | return; |
| 735 | } |
| 736 | *new_node = malloc(sizeof(pldm_entity_node)); |
| 737 | (*new_node)->entity = org_node->entity; |
| 738 | (*new_node)->association_type = org_node->association_type; |
| 739 | (*new_node)->first_child = NULL; |
| 740 | (*new_node)->next_sibling = NULL; |
| 741 | entity_association_tree_copy(org_node->first_child, |
| 742 | &((*new_node)->first_child)); |
| 743 | entity_association_tree_copy(org_node->next_sibling, |
| 744 | &((*new_node)->next_sibling)); |
| 745 | } |
| 746 | |
| 747 | void pldm_entity_association_tree_copy_root( |
| 748 | pldm_entity_association_tree *org_tree, |
| 749 | pldm_entity_association_tree *new_tree) |
| 750 | { |
| 751 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 752 | entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 753 | } |
| 754 | |
| 755 | void pldm_entity_association_tree_destroy_root( |
| 756 | pldm_entity_association_tree *tree) |
| 757 | { |
| 758 | assert(tree != NULL); |
| 759 | entity_association_tree_destroy(tree->root); |
| 760 | tree->last_used_container_id = 0; |
| 761 | tree->root = NULL; |
| 762 | } |
| 763 | |
| 764 | bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree) |
| 765 | { |
| 766 | return ((tree->root == NULL) ? true : false); |
| 767 | } |
| 768 | |
Deepak Kodihalli | 3b28ba8 | 2020-03-30 07:39:47 -0500 | [diff] [blame] | 769 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 770 | size_t *num_entities, |
| 771 | pldm_entity **entities) |
| 772 | { |
| 773 | assert(pdr != NULL); |
| 774 | assert(pdr_len >= sizeof(struct pldm_pdr_hdr) + |
| 775 | sizeof(struct pldm_pdr_entity_association)); |
| 776 | |
| 777 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
| 778 | assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION); |
| 779 | |
| 780 | const uint8_t *start = (uint8_t *)pdr; |
| 781 | const uint8_t *end = |
| 782 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
| 783 | start += sizeof(struct pldm_pdr_hdr); |
| 784 | struct pldm_pdr_entity_association *entity_association_pdr = |
| 785 | (struct pldm_pdr_entity_association *)start; |
| 786 | *num_entities = entity_association_pdr->num_children + 1; |
| 787 | assert(*num_entities >= 2); |
| 788 | *entities = malloc(sizeof(pldm_entity) * *num_entities); |
| 789 | assert(*entities != NULL); |
| 790 | assert(start + sizeof(struct pldm_pdr_entity_association) + |
| 791 | sizeof(pldm_entity) * (*num_entities - 2) == |
| 792 | end); |
| 793 | (*entities)->entity_type = |
| 794 | le16toh(entity_association_pdr->container.entity_type); |
| 795 | (*entities)->entity_instance_num = |
| 796 | le16toh(entity_association_pdr->container.entity_instance_num); |
| 797 | (*entities)->entity_container_id = |
| 798 | le16toh(entity_association_pdr->container.entity_container_id); |
| 799 | pldm_entity *curr_entity = entity_association_pdr->children; |
| 800 | size_t i = 1; |
| 801 | while (i < *num_entities) { |
| 802 | (*entities + i)->entity_type = |
| 803 | le16toh(curr_entity->entity_type); |
| 804 | (*entities + i)->entity_instance_num = |
| 805 | le16toh(curr_entity->entity_instance_num); |
| 806 | (*entities + i)->entity_container_id = |
| 807 | le16toh(curr_entity->entity_container_id); |
| 808 | ++curr_entity; |
| 809 | ++i; |
| 810 | } |
| 811 | } |