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