Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1 | #include "pdr.h" |
| 2 | #include "platform.h" |
| 3 | #include <assert.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 4 | #include <endian.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 7 | #include <errno.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 8 | |
| 9 | typedef struct pldm_pdr_record { |
| 10 | uint32_t record_handle; |
| 11 | uint32_t size; |
| 12 | uint8_t *data; |
| 13 | struct pldm_pdr_record *next; |
| 14 | bool is_remote; |
| 15 | uint16_t terminus_handle; |
| 16 | } pldm_pdr_record; |
| 17 | |
| 18 | typedef struct pldm_pdr { |
| 19 | uint32_t record_count; |
| 20 | uint32_t size; |
| 21 | pldm_pdr_record *first; |
| 22 | pldm_pdr_record *last; |
| 23 | } pldm_pdr; |
| 24 | |
| 25 | static inline uint32_t get_next_record_handle(const pldm_pdr *repo, |
| 26 | const pldm_pdr_record *record) |
| 27 | { |
| 28 | assert(repo != NULL); |
| 29 | assert(record != NULL); |
| 30 | |
| 31 | if (record == repo->last) { |
| 32 | return 0; |
| 33 | } |
| 34 | return record->next->record_handle; |
| 35 | } |
| 36 | |
| 37 | static void add_record(pldm_pdr *repo, pldm_pdr_record *record) |
| 38 | { |
| 39 | assert(repo != NULL); |
| 40 | assert(record != NULL); |
| 41 | |
| 42 | if (repo->first == NULL) { |
| 43 | assert(repo->last == NULL); |
| 44 | repo->first = record; |
| 45 | repo->last = record; |
| 46 | } else { |
| 47 | repo->last->next = record; |
| 48 | repo->last = record; |
| 49 | } |
| 50 | repo->size += record->size; |
| 51 | ++repo->record_count; |
| 52 | } |
| 53 | |
| 54 | static inline uint32_t get_new_record_handle(const pldm_pdr *repo) |
| 55 | { |
| 56 | assert(repo != NULL); |
| 57 | uint32_t last_used_hdl = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 58 | repo->last != NULL ? repo->last->record_handle : 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 59 | assert(last_used_hdl != UINT32_MAX); |
| 60 | |
| 61 | return last_used_hdl + 1; |
| 62 | } |
| 63 | |
| 64 | static pldm_pdr_record *make_new_record(const pldm_pdr *repo, |
| 65 | const uint8_t *data, uint32_t size, |
| 66 | uint32_t record_handle, bool is_remote, |
| 67 | uint16_t terminus_handle) |
| 68 | { |
| 69 | assert(repo != NULL); |
| 70 | assert(size != 0); |
| 71 | |
| 72 | pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record)); |
| 73 | assert(record != NULL); |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 74 | record->record_handle = record_handle == 0 ? |
| 75 | get_new_record_handle(repo) : |
| 76 | record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 77 | record->size = size; |
| 78 | record->is_remote = is_remote; |
| 79 | record->terminus_handle = terminus_handle; |
| 80 | if (data != NULL) { |
| 81 | record->data = malloc(size); |
| 82 | assert(record->data != NULL); |
| 83 | memcpy(record->data, data, size); |
| 84 | /* If record handle is 0, that is an indication for this API to |
| 85 | * compute a new handle. For that reason, the computed handle |
| 86 | * needs to be populated in the PDR header. For a case where the |
| 87 | * caller supplied the record handle, it would exist in the |
| 88 | * header already. |
| 89 | */ |
| 90 | if (!record_handle) { |
| 91 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 92 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 93 | hdr->record_handle = htole32(record->record_handle); |
| 94 | } |
| 95 | } |
| 96 | record->next = NULL; |
| 97 | |
| 98 | return record; |
| 99 | } |
| 100 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 101 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 102 | uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
| 103 | uint32_t record_handle, bool is_remote, |
| 104 | uint16_t terminus_handle) |
| 105 | { |
| 106 | assert(size != 0); |
| 107 | assert(data != NULL); |
| 108 | |
| 109 | pldm_pdr_record *record = make_new_record( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 110 | repo, data, size, record_handle, is_remote, terminus_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 111 | add_record(repo, record); |
| 112 | |
| 113 | return record->record_handle; |
| 114 | } |
| 115 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 116 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 117 | pldm_pdr *pldm_pdr_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 118 | { |
| 119 | pldm_pdr *repo = malloc(sizeof(pldm_pdr)); |
| 120 | assert(repo != NULL); |
| 121 | repo->record_count = 0; |
| 122 | repo->size = 0; |
| 123 | repo->first = NULL; |
| 124 | repo->last = NULL; |
| 125 | |
| 126 | return repo; |
| 127 | } |
| 128 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 129 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 130 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 131 | { |
| 132 | assert(repo != NULL); |
| 133 | |
| 134 | pldm_pdr_record *record = repo->first; |
| 135 | while (record != NULL) { |
| 136 | pldm_pdr_record *next = record->next; |
| 137 | if (record->data) { |
| 138 | free(record->data); |
| 139 | record->data = NULL; |
| 140 | } |
| 141 | free(record); |
| 142 | record = next; |
| 143 | } |
| 144 | free(repo); |
| 145 | } |
| 146 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 147 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 148 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 149 | uint32_t record_handle, |
| 150 | uint8_t **data, uint32_t *size, |
| 151 | uint32_t *next_record_handle) |
| 152 | { |
| 153 | assert(repo != NULL); |
| 154 | assert(data != NULL); |
| 155 | assert(size != NULL); |
| 156 | assert(next_record_handle != NULL); |
| 157 | |
| 158 | if (!record_handle && (repo->first != NULL)) { |
| 159 | record_handle = repo->first->record_handle; |
| 160 | } |
| 161 | pldm_pdr_record *record = repo->first; |
| 162 | while (record != NULL) { |
| 163 | if (record->record_handle == record_handle) { |
| 164 | *size = record->size; |
| 165 | *data = record->data; |
| 166 | *next_record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 167 | get_next_record_handle(repo, record); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 168 | return record; |
| 169 | } |
| 170 | record = record->next; |
| 171 | } |
| 172 | |
| 173 | *size = 0; |
| 174 | *next_record_handle = 0; |
| 175 | return NULL; |
| 176 | } |
| 177 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 178 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 179 | const pldm_pdr_record * |
| 180 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 181 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 182 | uint32_t *size, uint32_t *next_record_handle) |
| 183 | { |
| 184 | assert(repo != NULL); |
| 185 | assert(curr_record != NULL); |
| 186 | assert(data != NULL); |
| 187 | assert(size != NULL); |
| 188 | assert(next_record_handle != NULL); |
| 189 | |
| 190 | if (curr_record == repo->last) { |
| 191 | *data = NULL; |
| 192 | *size = 0; |
| 193 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 198 | *data = curr_record->next->data; |
| 199 | *size = curr_record->next->size; |
| 200 | return curr_record->next; |
| 201 | } |
| 202 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 203 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 204 | const pldm_pdr_record * |
| 205 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 206 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 207 | uint32_t *size) |
| 208 | { |
| 209 | assert(repo != NULL); |
| 210 | |
| 211 | pldm_pdr_record *record = repo->first; |
| 212 | if (curr_record != NULL) { |
| 213 | record = curr_record->next; |
| 214 | } |
| 215 | while (record != NULL) { |
| 216 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 217 | if (hdr->type == pdr_type) { |
| 218 | if (data && size) { |
| 219 | *size = record->size; |
| 220 | *data = record->data; |
| 221 | } |
| 222 | return record; |
| 223 | } |
| 224 | record = record->next; |
| 225 | } |
| 226 | |
| 227 | if (size) { |
| 228 | *size = 0; |
| 229 | } |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 233 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 234 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 235 | { |
| 236 | assert(repo != NULL); |
| 237 | |
| 238 | return repo->record_count; |
| 239 | } |
| 240 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 241 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 242 | uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo) |
| 243 | { |
| 244 | assert(repo != NULL); |
| 245 | |
| 246 | return repo->size; |
| 247 | } |
| 248 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 249 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 250 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo, |
| 251 | const pldm_pdr_record *record) |
| 252 | { |
| 253 | assert(repo != NULL); |
| 254 | assert(record != NULL); |
| 255 | |
| 256 | return record->record_handle; |
| 257 | } |
| 258 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 259 | LIBPLDM_ABI_STABLE |
| 260 | bool pldm_pdr_record_is_remote(const pldm_pdr_record *record) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 261 | { |
| 262 | assert(record != NULL); |
| 263 | |
| 264 | return record->is_remote; |
| 265 | } |
| 266 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 267 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 268 | uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle, |
| 269 | uint16_t fru_rsi, uint16_t entity_type, |
| 270 | uint16_t entity_instance_num, |
| 271 | uint16_t container_id, |
| 272 | uint32_t bmc_record_handle) |
| 273 | { |
| 274 | uint32_t size = sizeof(struct pldm_pdr_hdr) + |
| 275 | sizeof(struct pldm_pdr_fru_record_set); |
| 276 | uint8_t data[size]; |
| 277 | |
| 278 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data; |
| 279 | hdr->version = 1; |
| 280 | hdr->record_handle = bmc_record_handle; |
| 281 | hdr->type = PLDM_PDR_FRU_RECORD_SET; |
| 282 | hdr->record_change_num = 0; |
| 283 | hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set)); |
| 284 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 285 | (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr + |
| 286 | sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 287 | fru->terminus_handle = htole16(terminus_handle); |
| 288 | fru->fru_rsi = htole16(fru_rsi); |
| 289 | fru->entity_type = htole16(entity_type); |
| 290 | fru->entity_instance_num = htole16(entity_instance_num); |
| 291 | fru->container_id = htole16(container_id); |
| 292 | |
| 293 | return pldm_pdr_add(repo, data, size, bmc_record_handle, false, |
| 294 | terminus_handle); |
| 295 | } |
| 296 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 297 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 298 | const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 299 | const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle, |
| 300 | uint16_t *entity_type, uint16_t *entity_instance_num, |
| 301 | uint16_t *container_id) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 302 | { |
| 303 | assert(terminus_handle != NULL); |
| 304 | assert(entity_type != NULL); |
| 305 | assert(entity_instance_num != NULL); |
| 306 | assert(container_id != NULL); |
| 307 | |
| 308 | uint8_t *data = NULL; |
| 309 | uint32_t size = 0; |
| 310 | const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 311 | repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 312 | while (curr_record != NULL) { |
| 313 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 314 | (struct pldm_pdr_fru_record_set |
| 315 | *)(data + sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 316 | if (fru->fru_rsi == htole16(fru_rsi)) { |
| 317 | *terminus_handle = le16toh(fru->terminus_handle); |
| 318 | *entity_type = le16toh(fru->entity_type); |
| 319 | *entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 320 | le16toh(fru->entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 321 | *container_id = le16toh(fru->container_id); |
| 322 | return curr_record; |
| 323 | } |
| 324 | data = NULL; |
| 325 | curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 326 | repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, |
| 327 | &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | *terminus_handle = 0; |
| 331 | *entity_type = 0; |
| 332 | *entity_instance_num = 0; |
| 333 | *container_id = 0; |
| 334 | |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 338 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 339 | /* NOLINTNEXTLINE(readability-identifier-naming) */ |
| 340 | void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle, |
| 341 | uint8_t tid, uint8_t tl_eid, bool valid_bit) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 342 | { |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 343 | uint8_t *out_data = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 344 | uint32_t size = 0; |
| 345 | const pldm_pdr_record *record; |
| 346 | record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR, |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 347 | NULL, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 348 | |
| 349 | do { |
| 350 | if (record != NULL) { |
| 351 | struct pldm_terminus_locator_pdr *pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 352 | (struct pldm_terminus_locator_pdr *)out_data; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 353 | struct pldm_terminus_locator_type_mctp_eid *value = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 354 | (struct pldm_terminus_locator_type_mctp_eid *) |
| 355 | pdr->terminus_locator_value; |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 356 | if (pdr->terminus_handle == terminus_handle && |
| 357 | pdr->tid == tid && value->eid == tl_eid) { |
| 358 | pdr->validity = valid_bit; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | } |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 362 | record = pldm_pdr_find_record_by_type(repo, |
| 363 | PLDM_TERMINUS_LOCATOR_PDR, |
| 364 | record, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 365 | } while (record); |
| 366 | } |
| 367 | |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 368 | static bool pldm_record_handle_in_range(uint32_t record_handle, |
| 369 | uint32_t first_record_handle, |
| 370 | uint32_t last_record_handle) |
| 371 | { |
| 372 | return record_handle >= first_record_handle && |
| 373 | record_handle <= last_record_handle; |
| 374 | } |
| 375 | |
| 376 | LIBPLDM_ABI_TESTING |
| 377 | int pldm_pdr_find_container_id_range_exclude( |
| 378 | const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance, |
| 379 | uint32_t range_exclude_start_handle, uint32_t range_exclude_end_handle, |
| 380 | uint16_t *container_id) |
| 381 | { |
| 382 | pldm_pdr_record *record; |
| 383 | if (!repo) { |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | |
| 387 | for (record = repo->first; record; record = record->next) { |
| 388 | bool is_container_entity_instance_number; |
| 389 | struct pldm_pdr_entity_association *pdr; |
| 390 | bool is_container_entity_type; |
| 391 | struct pldm_entity *child; |
| 392 | struct pldm_pdr_hdr *hdr; |
| 393 | bool in_range; |
| 394 | |
| 395 | // pldm_pdr_add() takes only uint8_t* data as an argument. |
| 396 | // The expectation here is the pldm_pdr_hdr is the first field of the record data |
| 397 | hdr = (struct pldm_pdr_hdr *)record->data; |
| 398 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 399 | continue; |
| 400 | } |
| 401 | in_range = pldm_record_handle_in_range( |
| 402 | record->record_handle, range_exclude_start_handle, |
| 403 | range_exclude_end_handle); |
| 404 | if (in_range) { |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | // this cast is valid with respect to alignment because |
| 409 | // struct pldm_pdr_hdr is declared with __attribute__((packed)) |
| 410 | pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr)); |
| 411 | if (pdr->num_children == 0) { |
| 412 | continue; |
| 413 | } |
| 414 | child = (&pdr->children[0]); |
| 415 | is_container_entity_type = pdr->container.entity_type == |
| 416 | entity_type; |
| 417 | is_container_entity_instance_number = |
| 418 | pdr->container.entity_instance_num == entity_instance; |
| 419 | if (is_container_entity_type && |
| 420 | is_container_entity_instance_number) { |
| 421 | *container_id = le16toh(child->entity_container_id); |
Pavithra Barithaya | ffd5342 | 2023-06-23 23:20:48 -0500 | [diff] [blame^] | 422 | return 0; |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | return -ENOKEY; |
| 426 | } |
| 427 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 428 | typedef struct pldm_entity_association_tree { |
| 429 | pldm_entity_node *root; |
| 430 | uint16_t last_used_container_id; |
| 431 | } pldm_entity_association_tree; |
| 432 | |
| 433 | typedef struct pldm_entity_node { |
| 434 | pldm_entity entity; |
| 435 | pldm_entity parent; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 436 | uint16_t remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 437 | pldm_entity_node *first_child; |
| 438 | pldm_entity_node *next_sibling; |
| 439 | uint8_t association_type; |
| 440 | } pldm_entity_node; |
| 441 | |
| 442 | static inline uint16_t next_container_id(pldm_entity_association_tree *tree) |
| 443 | { |
| 444 | assert(tree != NULL); |
| 445 | assert(tree->last_used_container_id != UINT16_MAX); |
| 446 | |
| 447 | return ++tree->last_used_container_id; |
| 448 | } |
| 449 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 450 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 451 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 452 | { |
| 453 | assert(node != NULL); |
| 454 | |
| 455 | return node->entity; |
| 456 | } |
| 457 | |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 458 | LIBPLDM_ABI_TESTING |
| 459 | int pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity, |
| 460 | uint16_t *cid) |
| 461 | { |
| 462 | if (!entity) { |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | *cid = entity->remote_container_id; |
| 467 | return 0; |
| 468 | } |
| 469 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 470 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 471 | pldm_entity_association_tree *pldm_entity_association_tree_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 472 | { |
| 473 | pldm_entity_association_tree *tree = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 474 | malloc(sizeof(pldm_entity_association_tree)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 475 | assert(tree != NULL); |
| 476 | tree->root = NULL; |
| 477 | tree->last_used_container_id = 0; |
| 478 | |
| 479 | return tree; |
| 480 | } |
| 481 | |
| 482 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 483 | uint16_t entity_type) |
| 484 | { |
| 485 | assert(start != NULL); |
| 486 | |
| 487 | /* Insert after the the last node that matches the input entity type, or |
| 488 | * at the end if no such match occurrs |
| 489 | */ |
| 490 | while (start->next_sibling != NULL) { |
| 491 | uint16_t this_type = start->entity.entity_type; |
| 492 | pldm_entity_node *next = start->next_sibling; |
| 493 | if (this_type == entity_type && |
| 494 | (this_type != next->entity.entity_type)) { |
| 495 | break; |
| 496 | } |
| 497 | start = start->next_sibling; |
| 498 | } |
| 499 | |
| 500 | return start; |
| 501 | } |
| 502 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 503 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 504 | pldm_entity_node *pldm_entity_association_tree_add( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 505 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 506 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 507 | uint8_t association_type) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 508 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 509 | return pldm_entity_association_tree_add_entity(tree, entity, |
| 510 | entity_instance_number, |
| 511 | parent, association_type, |
| 512 | false, true, 0xFFFF); |
| 513 | } |
| 514 | |
| 515 | LIBPLDM_ABI_TESTING |
| 516 | pldm_entity_node *pldm_entity_association_tree_add_entity( |
| 517 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 518 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 519 | uint8_t association_type, bool is_remote, bool is_update_container_id, |
| 520 | uint16_t container_id) |
| 521 | { |
| 522 | if ((!tree) || (!entity)) { |
| 523 | return NULL; |
| 524 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 525 | |
| 526 | if (entity_instance_number != 0xFFFF && parent != NULL) { |
| 527 | pldm_entity node; |
| 528 | node.entity_type = entity->entity_type; |
| 529 | node.entity_instance_num = entity_instance_number; |
| 530 | if (pldm_is_current_parent_child(parent, &node)) { |
| 531 | return NULL; |
| 532 | } |
| 533 | } |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 534 | if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL && |
| 535 | association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) { |
| 536 | return NULL; |
| 537 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 538 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 539 | if (!node) { |
| 540 | return NULL; |
| 541 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 542 | node->first_child = NULL; |
| 543 | node->next_sibling = NULL; |
| 544 | node->parent.entity_type = 0; |
| 545 | node->parent.entity_instance_num = 0; |
| 546 | node->parent.entity_container_id = 0; |
| 547 | node->entity.entity_type = entity->entity_type; |
| 548 | node->entity.entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 549 | entity_instance_number != 0xFFFF ? entity_instance_number : 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 550 | node->association_type = association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 551 | node->remote_container_id = 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 552 | if (tree->root == NULL) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 553 | if (parent != NULL) { |
| 554 | free(node); |
| 555 | return NULL; |
| 556 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 557 | tree->root = node; |
| 558 | /* container_id 0 here indicates this is the top-most entry */ |
| 559 | node->entity.entity_container_id = 0; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 560 | node->remote_container_id = node->entity.entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 561 | } else if (parent != NULL && parent->first_child == NULL) { |
| 562 | parent->first_child = node; |
| 563 | node->parent = parent->entity; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 564 | |
| 565 | if (is_remote) { |
| 566 | node->remote_container_id = entity->entity_container_id; |
| 567 | } |
| 568 | if (is_update_container_id) { |
| 569 | if (container_id != 0xFFFF) { |
| 570 | node->entity.entity_container_id = container_id; |
| 571 | } else { |
| 572 | node->entity.entity_container_id = |
| 573 | next_container_id(tree); |
| 574 | } |
| 575 | } else { |
| 576 | node->entity.entity_container_id = |
| 577 | entity->entity_container_id; |
| 578 | } |
| 579 | |
| 580 | if (!is_remote) { |
| 581 | node->remote_container_id = |
| 582 | node->entity.entity_container_id; |
| 583 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 584 | } else { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 585 | pldm_entity_node *start = parent == NULL ? tree->root : |
| 586 | parent->first_child; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 587 | pldm_entity_node *prev = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 588 | find_insertion_at(start, entity->entity_type); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 589 | if (!prev) { |
| 590 | free(node); |
| 591 | return NULL; |
| 592 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 593 | pldm_entity_node *next = prev->next_sibling; |
| 594 | if (prev->entity.entity_type == entity->entity_type) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 595 | if (prev->entity.entity_instance_num == UINT16_MAX) { |
| 596 | free(node); |
| 597 | return NULL; |
| 598 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 599 | node->entity.entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 600 | entity_instance_number != 0xFFFF ? |
| 601 | entity_instance_number : |
| 602 | prev->entity.entity_instance_num + 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 603 | } |
| 604 | prev->next_sibling = node; |
| 605 | node->parent = prev->parent; |
| 606 | node->next_sibling = next; |
| 607 | node->entity.entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 608 | prev->entity.entity_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 609 | node->remote_container_id = entity->entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 610 | } |
| 611 | entity->entity_instance_num = node->entity.entity_instance_num; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 612 | if (is_update_container_id) { |
| 613 | entity->entity_container_id = node->entity.entity_container_id; |
| 614 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 615 | return node; |
| 616 | } |
| 617 | |
| 618 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 619 | { |
| 620 | if (node == NULL) { |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | ++(*num); |
| 625 | get_num_nodes(node->next_sibling, num); |
| 626 | get_num_nodes(node->first_child, num); |
| 627 | } |
| 628 | |
| 629 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 630 | pldm_entity *entities, size_t *index) |
| 631 | { |
| 632 | if (node == NULL) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | pldm_entity *entity = &entities[*index]; |
| 637 | ++(*index); |
| 638 | entity->entity_type = node->entity.entity_type; |
| 639 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 640 | entity->entity_container_id = node->entity.entity_container_id; |
| 641 | |
| 642 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 643 | entity_association_tree_visit(node->first_child, entities, index); |
| 644 | } |
| 645 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 646 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 647 | void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree, |
| 648 | pldm_entity **entities, size_t *size) |
| 649 | { |
| 650 | assert(tree != NULL); |
| 651 | |
| 652 | *size = 0; |
| 653 | if (tree->root == NULL) { |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | get_num_nodes(tree->root, size); |
| 658 | *entities = malloc(*size * sizeof(pldm_entity)); |
| 659 | size_t index = 0; |
| 660 | entity_association_tree_visit(tree->root, *entities, &index); |
| 661 | } |
| 662 | |
| 663 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 664 | { |
| 665 | if (node == NULL) { |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | entity_association_tree_destroy(node->next_sibling); |
| 670 | entity_association_tree_destroy(node->first_child); |
| 671 | free(node); |
| 672 | } |
| 673 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 674 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 675 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 676 | { |
| 677 | assert(tree != NULL); |
| 678 | |
| 679 | entity_association_tree_destroy(tree->root); |
| 680 | free(tree); |
| 681 | } |
| 682 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 683 | LIBPLDM_ABI_STABLE |
| 684 | bool pldm_entity_is_node_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 685 | { |
| 686 | assert(node != NULL); |
| 687 | |
| 688 | return node->first_child != NULL; |
| 689 | } |
| 690 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 691 | LIBPLDM_ABI_STABLE |
| 692 | pldm_entity pldm_entity_get_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 693 | { |
| 694 | assert(node != NULL); |
| 695 | |
| 696 | return node->parent; |
| 697 | } |
| 698 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 699 | LIBPLDM_ABI_STABLE |
| 700 | bool pldm_entity_is_exist_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 701 | { |
| 702 | assert(node != NULL); |
| 703 | |
| 704 | if (node->parent.entity_type == 0 && |
| 705 | node->parent.entity_instance_num == 0 && |
| 706 | node->parent.entity_container_id == 0) { |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | return true; |
| 711 | } |
| 712 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 713 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 714 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 715 | uint8_t association_type) |
| 716 | { |
| 717 | assert(node != NULL); |
| 718 | assert(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 719 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 720 | |
| 721 | size_t count = 0; |
| 722 | pldm_entity_node *curr = node->first_child; |
| 723 | while (curr != NULL) { |
| 724 | if (curr->association_type == association_type) { |
| 725 | ++count; |
| 726 | } |
| 727 | curr = curr->next_sibling; |
| 728 | } |
| 729 | |
| 730 | assert(count < UINT8_MAX); |
| 731 | return count; |
| 732 | } |
| 733 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 734 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 735 | bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node) |
| 736 | { |
| 737 | assert(parent != NULL); |
| 738 | assert(node != NULL); |
| 739 | |
| 740 | pldm_entity_node *curr = parent->first_child; |
| 741 | while (curr != NULL) { |
| 742 | if (node->entity_type == curr->entity.entity_type && |
| 743 | node->entity_instance_num == |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 744 | curr->entity.entity_instance_num) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 745 | return true; |
| 746 | } |
| 747 | curr = curr->next_sibling; |
| 748 | } |
| 749 | |
| 750 | return false; |
| 751 | } |
| 752 | |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 753 | static void entity_association_pdr_add_children( |
| 754 | pldm_entity_node *curr, pldm_pdr *repo, uint16_t size, |
| 755 | uint8_t contained_count, uint8_t association_type, bool is_remote, |
| 756 | uint16_t terminus_handle, uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 757 | { |
| 758 | uint8_t pdr[size]; |
| 759 | uint8_t *start = pdr; |
| 760 | |
| 761 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 762 | hdr->version = 1; |
| 763 | hdr->record_handle = 0; |
| 764 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 765 | hdr->record_change_num = 0; |
| 766 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
| 767 | start += sizeof(struct pldm_pdr_hdr); |
| 768 | |
| 769 | uint16_t *container_id = (uint16_t *)start; |
| 770 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
| 771 | start += sizeof(uint16_t); |
| 772 | *start = association_type; |
| 773 | start += sizeof(uint8_t); |
| 774 | |
| 775 | pldm_entity *entity = (pldm_entity *)start; |
| 776 | entity->entity_type = htole16(curr->entity.entity_type); |
| 777 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 778 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
| 779 | start += sizeof(pldm_entity); |
| 780 | |
| 781 | *start = contained_count; |
| 782 | start += sizeof(uint8_t); |
| 783 | |
| 784 | pldm_entity_node *node = curr->first_child; |
| 785 | while (node != NULL) { |
| 786 | if (node->association_type == association_type) { |
| 787 | pldm_entity *entity = (pldm_entity *)start; |
| 788 | entity->entity_type = htole16(node->entity.entity_type); |
| 789 | entity->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 790 | htole16(node->entity.entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 791 | entity->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 792 | htole16(node->entity.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 793 | start += sizeof(pldm_entity); |
| 794 | } |
| 795 | node = node->next_sibling; |
| 796 | } |
| 797 | |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 798 | pldm_pdr_add(repo, pdr, size, record_handle, is_remote, |
| 799 | terminus_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 803 | pldm_pdr *repo, bool is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 804 | uint16_t terminus_handle, |
| 805 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 806 | { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 807 | uint8_t num_logical_children = pldm_entity_get_num_children( |
| 808 | curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 809 | uint8_t num_physical_children = pldm_entity_get_num_children( |
| 810 | curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 811 | |
| 812 | if (num_logical_children) { |
| 813 | uint16_t logical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 814 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 815 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 816 | sizeof(uint8_t) + |
| 817 | (num_logical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 4edb708 | 2023-04-05 19:09:52 +0930 | [diff] [blame] | 818 | entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 819 | curr, repo, logical_pdr_size, num_logical_children, |
| 820 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 821 | terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | if (num_physical_children) { |
| 825 | uint16_t physical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 826 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 827 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 828 | sizeof(uint8_t) + |
| 829 | (num_physical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 4edb708 | 2023-04-05 19:09:52 +0930 | [diff] [blame] | 830 | entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 831 | curr, repo, physical_pdr_size, num_physical_children, |
| 832 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 833 | terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 837 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 838 | bool is_present(pldm_entity entity, pldm_entity **entities, size_t num_entities) |
| 839 | { |
| 840 | if (entities == NULL || num_entities == 0) { |
| 841 | return true; |
| 842 | } |
| 843 | size_t i = 0; |
| 844 | while (i < num_entities) { |
| 845 | if ((*entities + i)->entity_type == entity.entity_type) { |
| 846 | return true; |
| 847 | } |
| 848 | i++; |
| 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | static void entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 854 | pldm_entity **entities, |
| 855 | size_t num_entities, bool is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 856 | uint16_t terminus_handle, |
| 857 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 858 | { |
| 859 | if (curr == NULL) { |
| 860 | return; |
| 861 | } |
| 862 | bool to_add = true; |
| 863 | to_add = is_present(curr->entity, entities, num_entities); |
| 864 | if (to_add) { |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 865 | entity_association_pdr_add_entry( |
| 866 | curr, repo, is_remote, terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 867 | } |
| 868 | entity_association_pdr_add(curr->next_sibling, repo, entities, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 869 | num_entities, is_remote, terminus_handle, |
| 870 | record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 871 | entity_association_pdr_add(curr->first_child, repo, entities, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 872 | num_entities, is_remote, terminus_handle, |
| 873 | record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 874 | } |
| 875 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 876 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 877 | void pldm_entity_association_pdr_add(pldm_entity_association_tree *tree, |
| 878 | pldm_pdr *repo, bool is_remote, |
| 879 | uint16_t terminus_handle) |
| 880 | { |
| 881 | assert(tree != NULL); |
| 882 | assert(repo != NULL); |
| 883 | |
| 884 | entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 885 | terminus_handle, 0); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 886 | } |
| 887 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 888 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 889 | void pldm_entity_association_pdr_add_from_node( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 890 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 891 | size_t num_entities, bool is_remote, uint16_t terminus_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 892 | { |
| 893 | assert(repo != NULL); |
| 894 | |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 895 | pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 896 | node, repo, entities, num_entities, is_remote, terminus_handle, |
| 897 | 0); |
| 898 | } |
| 899 | |
| 900 | LIBPLDM_ABI_TESTING |
| 901 | int pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 902 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 903 | size_t num_entities, bool is_remote, uint16_t terminus_handle, |
| 904 | uint32_t record_handle) |
| 905 | { |
| 906 | if (!node || !repo || !entities) { |
| 907 | return -EINVAL; |
| 908 | } |
| 909 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 910 | entity_association_pdr_add(node, repo, entities, num_entities, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 911 | is_remote, terminus_handle, record_handle); |
| 912 | |
| 913 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 914 | } |
| 915 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 916 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 917 | void find_entity_ref_in_tree(pldm_entity_node *tree_node, pldm_entity entity, |
| 918 | pldm_entity_node **node) |
| 919 | { |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 920 | bool is_entity_container_id; |
| 921 | bool is_entity_instance_num; |
| 922 | bool is_type; |
| 923 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 924 | if (tree_node == NULL) { |
| 925 | return; |
| 926 | } |
| 927 | |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 928 | is_type = tree_node->entity.entity_type == entity.entity_type; |
| 929 | is_entity_instance_num = tree_node->entity.entity_instance_num == |
| 930 | entity.entity_instance_num; |
| 931 | is_entity_container_id = tree_node->entity.entity_container_id == |
| 932 | entity.entity_container_id; |
| 933 | |
| 934 | if (is_type && is_entity_instance_num && is_entity_container_id) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 935 | *node = tree_node; |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | find_entity_ref_in_tree(tree_node->first_child, entity, node); |
| 940 | find_entity_ref_in_tree(tree_node->next_sibling, entity, node); |
| 941 | } |
| 942 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 943 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 944 | void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree, |
| 945 | pldm_entity entity, pldm_entity_node **node) |
| 946 | { |
| 947 | assert(tree != NULL); |
| 948 | find_entity_ref_in_tree(tree->root, entity, node); |
| 949 | } |
| 950 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 951 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 952 | void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo, |
| 953 | uint16_t terminus_handle) |
| 954 | { |
| 955 | assert(repo != NULL); |
| 956 | bool removed = false; |
| 957 | |
| 958 | pldm_pdr_record *record = repo->first; |
| 959 | pldm_pdr_record *prev = NULL; |
| 960 | while (record != NULL) { |
| 961 | pldm_pdr_record *next = record->next; |
| 962 | if (record->terminus_handle == terminus_handle) { |
| 963 | if (repo->first == record) { |
| 964 | repo->first = next; |
| 965 | } else { |
| 966 | prev->next = next; |
| 967 | } |
| 968 | if (repo->last == record) { |
| 969 | repo->last = prev; |
| 970 | } |
| 971 | if (record->data) { |
| 972 | free(record->data); |
| 973 | } |
| 974 | --repo->record_count; |
| 975 | repo->size -= record->size; |
| 976 | free(record); |
| 977 | removed = true; |
| 978 | } else { |
| 979 | prev = record; |
| 980 | } |
| 981 | record = next; |
| 982 | } |
| 983 | |
| 984 | if (removed == true) { |
| 985 | record = repo->first; |
| 986 | uint32_t record_handle = 0; |
| 987 | while (record != NULL) { |
| 988 | record->record_handle = ++record_handle; |
| 989 | if (record->data != NULL) { |
| 990 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 991 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 992 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 993 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 994 | } |
| 995 | record = record->next; |
| 996 | } |
| 997 | } |
| 998 | } |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 999 | |
| 1000 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1001 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 1002 | { |
| 1003 | assert(repo != NULL); |
| 1004 | bool removed = false; |
| 1005 | |
| 1006 | pldm_pdr_record *record = repo->first; |
| 1007 | pldm_pdr_record *prev = NULL; |
| 1008 | while (record != NULL) { |
| 1009 | pldm_pdr_record *next = record->next; |
| 1010 | if (record->is_remote == true) { |
| 1011 | if (repo->first == record) { |
| 1012 | repo->first = next; |
| 1013 | } else { |
| 1014 | prev->next = next; |
| 1015 | } |
| 1016 | if (repo->last == record) { |
| 1017 | repo->last = prev; |
| 1018 | } |
| 1019 | if (record->data) { |
| 1020 | free(record->data); |
| 1021 | } |
| 1022 | --repo->record_count; |
| 1023 | repo->size -= record->size; |
| 1024 | free(record); |
| 1025 | removed = true; |
| 1026 | } else { |
| 1027 | prev = record; |
| 1028 | } |
| 1029 | record = next; |
| 1030 | } |
| 1031 | |
| 1032 | if (removed == true) { |
| 1033 | record = repo->first; |
| 1034 | uint32_t record_handle = 0; |
| 1035 | while (record != NULL) { |
| 1036 | record->record_handle = ++record_handle; |
| 1037 | if (record->data != NULL) { |
| 1038 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1039 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1040 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1041 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1042 | } |
| 1043 | record = record->next; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
Pavithra Barithaya | 4d69434 | 2023-05-19 08:04:41 -0500 | [diff] [blame] | 1048 | LIBPLDM_ABI_TESTING |
| 1049 | pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo, |
| 1050 | uint32_t first, uint32_t last) |
| 1051 | { |
| 1052 | pldm_pdr_record *record = NULL; |
| 1053 | pldm_pdr_record *curr; |
| 1054 | |
| 1055 | if (!repo) { |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | for (curr = repo->first; curr; curr = curr->next) { |
| 1059 | if (first > curr->record_handle || last < curr->record_handle) { |
| 1060 | continue; |
| 1061 | } |
| 1062 | if (!record || curr->record_handle > record->record_handle) { |
| 1063 | record = curr; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | return record; |
| 1068 | } |
| 1069 | |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1070 | static void entity_association_tree_find_if_remote(pldm_entity_node *node, |
| 1071 | pldm_entity *entity, |
| 1072 | pldm_entity_node **out, |
| 1073 | bool is_remote) |
| 1074 | { |
| 1075 | assert(out != NULL && *out == NULL); |
| 1076 | if (node == NULL) { |
| 1077 | return; |
| 1078 | } |
| 1079 | bool is_entity_type; |
| 1080 | bool is_entity_instance_num; |
| 1081 | |
| 1082 | is_entity_type = node->entity.entity_type == entity->entity_type; |
| 1083 | is_entity_instance_num = node->entity.entity_instance_num == |
| 1084 | entity->entity_instance_num; |
| 1085 | |
| 1086 | if (!is_remote || |
| 1087 | node->remote_container_id == entity->entity_container_id) { |
| 1088 | if (is_entity_type && is_entity_instance_num) { |
| 1089 | entity->entity_container_id = |
| 1090 | node->entity.entity_container_id; |
| 1091 | *out = node; |
| 1092 | return; |
| 1093 | } |
| 1094 | } |
| 1095 | entity_association_tree_find_if_remote(node->next_sibling, entity, out, |
| 1096 | is_remote); |
| 1097 | entity_association_tree_find_if_remote(node->first_child, entity, out, |
| 1098 | is_remote); |
| 1099 | } |
| 1100 | |
| 1101 | LIBPLDM_ABI_TESTING |
| 1102 | pldm_entity_node * |
| 1103 | pldm_entity_association_tree_find_if_remote(pldm_entity_association_tree *tree, |
| 1104 | pldm_entity *entity, bool is_remote) |
| 1105 | { |
| 1106 | if (!tree || !entity) { |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | pldm_entity_node *node = NULL; |
| 1110 | entity_association_tree_find_if_remote(tree->root, entity, &node, |
| 1111 | is_remote); |
| 1112 | return node; |
| 1113 | } |
| 1114 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1115 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1116 | void entity_association_tree_find(pldm_entity_node *node, pldm_entity *entity, |
| 1117 | pldm_entity_node **out) |
| 1118 | { |
| 1119 | if (node == NULL) { |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (node->entity.entity_type == entity->entity_type && |
| 1124 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 1125 | entity->entity_container_id = node->entity.entity_container_id; |
| 1126 | *out = node; |
| 1127 | return; |
| 1128 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1129 | entity_association_tree_find(node->next_sibling, entity, out); |
| 1130 | entity_association_tree_find(node->first_child, entity, out); |
| 1131 | } |
| 1132 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1133 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1134 | pldm_entity_node * |
| 1135 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 1136 | pldm_entity *entity) |
| 1137 | { |
| 1138 | assert(tree != NULL); |
| 1139 | |
| 1140 | pldm_entity_node *node = NULL; |
| 1141 | entity_association_tree_find(tree->root, entity, &node); |
| 1142 | return node; |
| 1143 | } |
| 1144 | |
| 1145 | static void entity_association_tree_copy(pldm_entity_node *org_node, |
| 1146 | pldm_entity_node **new_node) |
| 1147 | { |
| 1148 | if (org_node == NULL) { |
| 1149 | return; |
| 1150 | } |
| 1151 | *new_node = malloc(sizeof(pldm_entity_node)); |
| 1152 | (*new_node)->parent = org_node->parent; |
| 1153 | (*new_node)->entity = org_node->entity; |
| 1154 | (*new_node)->association_type = org_node->association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 1155 | (*new_node)->remote_container_id = org_node->remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1156 | (*new_node)->first_child = NULL; |
| 1157 | (*new_node)->next_sibling = NULL; |
| 1158 | entity_association_tree_copy(org_node->first_child, |
| 1159 | &((*new_node)->first_child)); |
| 1160 | entity_association_tree_copy(org_node->next_sibling, |
| 1161 | &((*new_node)->next_sibling)); |
| 1162 | } |
| 1163 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1164 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1165 | void pldm_entity_association_tree_copy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1166 | pldm_entity_association_tree *org_tree, |
| 1167 | pldm_entity_association_tree *new_tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1168 | { |
| 1169 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 1170 | entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 1171 | } |
| 1172 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1173 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1174 | void pldm_entity_association_tree_destroy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1175 | pldm_entity_association_tree *tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1176 | { |
| 1177 | assert(tree != NULL); |
| 1178 | entity_association_tree_destroy(tree->root); |
| 1179 | tree->last_used_container_id = 0; |
| 1180 | tree->root = NULL; |
| 1181 | } |
| 1182 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1183 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1184 | bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree) |
| 1185 | { |
| 1186 | return ((tree->root == NULL) ? true : false); |
| 1187 | } |
| 1188 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1189 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1190 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 1191 | size_t *num_entities, |
| 1192 | pldm_entity **entities) |
| 1193 | { |
| 1194 | assert(pdr != NULL); |
| 1195 | assert(pdr_len >= sizeof(struct pldm_pdr_hdr) + |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1196 | sizeof(struct pldm_pdr_entity_association)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1197 | |
| 1198 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
| 1199 | assert(hdr->type == PLDM_PDR_ENTITY_ASSOCIATION); |
| 1200 | |
| 1201 | const uint8_t *start = (uint8_t *)pdr; |
| 1202 | const uint8_t *end = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1203 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1204 | start += sizeof(struct pldm_pdr_hdr); |
| 1205 | struct pldm_pdr_entity_association *entity_association_pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1206 | (struct pldm_pdr_entity_association *)start; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1207 | *num_entities = entity_association_pdr->num_children + 1; |
| 1208 | assert(*num_entities >= 2); |
| 1209 | *entities = malloc(sizeof(pldm_entity) * *num_entities); |
| 1210 | assert(*entities != NULL); |
| 1211 | assert(start + sizeof(struct pldm_pdr_entity_association) + |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1212 | sizeof(pldm_entity) * (*num_entities - 2) == |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1213 | end); |
| 1214 | (*entities)->entity_type = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1215 | le16toh(entity_association_pdr->container.entity_type); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1216 | (*entities)->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1217 | le16toh(entity_association_pdr->container.entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1218 | (*entities)->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1219 | le16toh(entity_association_pdr->container.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1220 | pldm_entity *curr_entity = entity_association_pdr->children; |
| 1221 | size_t i = 1; |
| 1222 | while (i < *num_entities) { |
| 1223 | (*entities + i)->entity_type = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1224 | le16toh(curr_entity->entity_type); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1225 | (*entities + i)->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1226 | le16toh(curr_entity->entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1227 | (*entities + i)->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1228 | le16toh(curr_entity->entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1229 | ++curr_entity; |
| 1230 | ++i; |
| 1231 | } |
| 1232 | } |