Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 2 | #include "msgbuf.h" |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 3 | #include <libpldm/pdr.h> |
| 4 | #include <libpldm/platform.h> |
| 5 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 6 | #include <assert.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 7 | #include <endian.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 11 | |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 12 | #define PDR_ENTITY_ASSOCIATION_MIN_SIZE \ |
| 13 | (sizeof(struct pldm_pdr_hdr) + \ |
| 14 | sizeof(struct pldm_pdr_entity_association)) |
| 15 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 16 | typedef struct pldm_pdr_record { |
| 17 | uint32_t record_handle; |
| 18 | uint32_t size; |
| 19 | uint8_t *data; |
| 20 | struct pldm_pdr_record *next; |
| 21 | bool is_remote; |
| 22 | uint16_t terminus_handle; |
| 23 | } pldm_pdr_record; |
| 24 | |
| 25 | typedef struct pldm_pdr { |
| 26 | uint32_t record_count; |
| 27 | uint32_t size; |
| 28 | pldm_pdr_record *first; |
| 29 | pldm_pdr_record *last; |
| 30 | } pldm_pdr; |
| 31 | |
| 32 | static inline uint32_t get_next_record_handle(const pldm_pdr *repo, |
| 33 | const pldm_pdr_record *record) |
| 34 | { |
| 35 | assert(repo != NULL); |
| 36 | assert(record != NULL); |
| 37 | |
| 38 | if (record == repo->last) { |
| 39 | return 0; |
| 40 | } |
| 41 | return record->next->record_handle; |
| 42 | } |
| 43 | |
Andrew Jeffery | ca248ce | 2023-07-07 10:38:30 +0930 | [diff] [blame] | 44 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 45 | int pldm_pdr_add_check(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
| 46 | bool is_remote, uint16_t terminus_handle, |
| 47 | uint32_t *record_handle) |
| 48 | { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 49 | uint32_t curr; |
| 50 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 51 | if (!repo || !data || !size) { |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 52 | return -EINVAL; |
| 53 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 54 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 55 | if (record_handle && *record_handle) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 56 | curr = *record_handle; |
| 57 | } else if (repo->last) { |
| 58 | curr = repo->last->record_handle; |
| 59 | if (curr == UINT32_MAX) { |
| 60 | return -EOVERFLOW; |
| 61 | } |
| 62 | curr += 1; |
| 63 | } else { |
| 64 | curr = 1; |
| 65 | } |
| 66 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 67 | pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record)); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 68 | if (!record) { |
| 69 | return -ENOMEM; |
| 70 | } |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 71 | |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 72 | if (data) { |
| 73 | record->data = malloc(size); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 74 | if (!record->data) { |
| 75 | free(record); |
| 76 | return -ENOMEM; |
| 77 | } |
| 78 | memcpy(record->data, data, size); |
Andrew Jeffery | 2ca7e64 | 2023-06-28 22:29:07 +0930 | [diff] [blame] | 79 | } |
| 80 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 81 | record->size = size; |
| 82 | record->is_remote = is_remote; |
| 83 | record->terminus_handle = terminus_handle; |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 84 | record->record_handle = curr; |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 85 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 86 | if (record_handle && !*record_handle && data) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 87 | /* If record handle is 0, that is an indication for this API to |
| 88 | * compute a new handle. For that reason, the computed handle |
| 89 | * needs to be populated in the PDR header. For a case where the |
| 90 | * caller supplied the record handle, it would exist in the |
| 91 | * header already. |
| 92 | */ |
| 93 | struct pldm_pdr_hdr *hdr = (void *)record->data; |
| 94 | hdr->record_handle = htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 95 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 96 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 97 | record->next = NULL; |
| 98 | |
Andrew Jeffery | 8d231da | 2023-07-04 11:28:46 +0930 | [diff] [blame] | 99 | assert(!repo->first == !repo->last); |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 100 | if (repo->first == NULL) { |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 101 | repo->first = record; |
| 102 | repo->last = record; |
| 103 | } else { |
| 104 | repo->last->next = record; |
| 105 | repo->last = record; |
| 106 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 107 | |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 108 | repo->size += record->size; |
| 109 | ++repo->record_count; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 110 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 111 | if (record_handle) { |
| 112 | *record_handle = record->record_handle; |
| 113 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 114 | |
| 115 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 116 | } |
| 117 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 118 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 119 | pldm_pdr *pldm_pdr_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 120 | { |
| 121 | pldm_pdr *repo = malloc(sizeof(pldm_pdr)); |
Andrew Jeffery | a8bb22e | 2023-06-30 12:01:12 +0930 | [diff] [blame] | 122 | if (!repo) { |
| 123 | return NULL; |
| 124 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 125 | repo->record_count = 0; |
| 126 | repo->size = 0; |
| 127 | repo->first = NULL; |
| 128 | repo->last = NULL; |
| 129 | |
| 130 | return repo; |
| 131 | } |
| 132 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 133 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 134 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 135 | { |
Andrew Jeffery | fca1b60 | 2023-06-30 12:29:25 +0930 | [diff] [blame] | 136 | if (!repo) { |
| 137 | return; |
| 138 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 139 | |
| 140 | pldm_pdr_record *record = repo->first; |
| 141 | while (record != NULL) { |
| 142 | pldm_pdr_record *next = record->next; |
| 143 | if (record->data) { |
| 144 | free(record->data); |
| 145 | record->data = NULL; |
| 146 | } |
| 147 | free(record); |
| 148 | record = next; |
| 149 | } |
| 150 | free(repo); |
| 151 | } |
| 152 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 153 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 154 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 155 | uint32_t record_handle, |
| 156 | uint8_t **data, uint32_t *size, |
| 157 | uint32_t *next_record_handle) |
| 158 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 159 | if (!repo || !data || !size || !next_record_handle) { |
| 160 | return NULL; |
| 161 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 162 | |
| 163 | if (!record_handle && (repo->first != NULL)) { |
| 164 | record_handle = repo->first->record_handle; |
| 165 | } |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 166 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 167 | pldm_pdr_record *record = repo->first; |
| 168 | while (record != NULL) { |
| 169 | if (record->record_handle == record_handle) { |
| 170 | *size = record->size; |
| 171 | *data = record->data; |
| 172 | *next_record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 173 | get_next_record_handle(repo, record); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 174 | return record; |
| 175 | } |
| 176 | record = record->next; |
| 177 | } |
| 178 | |
| 179 | *size = 0; |
| 180 | *next_record_handle = 0; |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 184 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 185 | const pldm_pdr_record * |
| 186 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 187 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 188 | uint32_t *size, uint32_t *next_record_handle) |
| 189 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 190 | if (!repo || !curr_record || !data || !size || !next_record_handle) { |
| 191 | return NULL; |
| 192 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 193 | |
| 194 | if (curr_record == repo->last) { |
| 195 | *data = NULL; |
| 196 | *size = 0; |
| 197 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 202 | *data = curr_record->next->data; |
| 203 | *size = curr_record->next->size; |
| 204 | return curr_record->next; |
| 205 | } |
| 206 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 207 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 208 | const pldm_pdr_record * |
| 209 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 210 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 211 | uint32_t *size) |
| 212 | { |
Andrew Jeffery | f85eeba | 2023-06-30 12:38:40 +0930 | [diff] [blame] | 213 | if (!repo) { |
| 214 | return NULL; |
| 215 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 216 | |
| 217 | pldm_pdr_record *record = repo->first; |
| 218 | if (curr_record != NULL) { |
| 219 | record = curr_record->next; |
| 220 | } |
| 221 | while (record != NULL) { |
| 222 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 223 | if (hdr->type == pdr_type) { |
| 224 | if (data && size) { |
| 225 | *size = record->size; |
| 226 | *data = record->data; |
| 227 | } |
| 228 | return record; |
| 229 | } |
| 230 | record = record->next; |
| 231 | } |
| 232 | |
| 233 | if (size) { |
| 234 | *size = 0; |
| 235 | } |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 239 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 240 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 241 | { |
| 242 | assert(repo != NULL); |
| 243 | |
| 244 | return repo->record_count; |
| 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_repo_size(const pldm_pdr *repo) |
| 249 | { |
| 250 | assert(repo != NULL); |
| 251 | |
| 252 | return repo->size; |
| 253 | } |
| 254 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 255 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 5565fcd | 2023-06-30 13:21:32 +0930 | [diff] [blame] | 256 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo |
| 257 | __attribute__((unused)), |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 258 | const pldm_pdr_record *record) |
| 259 | { |
| 260 | assert(repo != NULL); |
| 261 | assert(record != NULL); |
| 262 | |
| 263 | return record->record_handle; |
| 264 | } |
| 265 | |
Pavithra Barithaya | 274732f | 2024-07-05 16:03:01 +0530 | [diff] [blame^] | 266 | LIBPLDM_ABI_TESTING |
| 267 | uint16_t pldm_pdr_get_terminus_handle(const pldm_pdr *repo |
| 268 | __attribute__((unused)), |
| 269 | const pldm_pdr_record *record) |
| 270 | { |
| 271 | assert(repo != NULL); |
| 272 | assert(record != NULL); |
| 273 | |
| 274 | return record->terminus_handle; |
| 275 | } |
| 276 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 277 | LIBPLDM_ABI_STABLE |
| 278 | bool pldm_pdr_record_is_remote(const pldm_pdr_record *record) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 279 | { |
| 280 | assert(record != NULL); |
| 281 | |
| 282 | return record->is_remote; |
| 283 | } |
| 284 | |
Andrew Jeffery | a2c6911 | 2023-07-07 10:41:38 +0930 | [diff] [blame] | 285 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 286 | int pldm_pdr_add_fru_record_set_check(pldm_pdr *repo, uint16_t terminus_handle, |
| 287 | uint16_t fru_rsi, uint16_t entity_type, |
| 288 | uint16_t entity_instance_num, |
| 289 | uint16_t container_id, |
| 290 | uint32_t *bmc_record_handle) |
| 291 | { |
Andrew Jeffery | 73d6779 | 2023-07-17 15:22:18 +0930 | [diff] [blame] | 292 | if (!repo || !bmc_record_handle) { |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 296 | uint32_t size = sizeof(struct pldm_pdr_hdr) + |
| 297 | sizeof(struct pldm_pdr_fru_record_set); |
| 298 | uint8_t data[size]; |
| 299 | |
| 300 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data; |
| 301 | hdr->version = 1; |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 302 | hdr->record_handle = *bmc_record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 303 | hdr->type = PLDM_PDR_FRU_RECORD_SET; |
| 304 | hdr->record_change_num = 0; |
| 305 | hdr->length = htole16(sizeof(struct pldm_pdr_fru_record_set)); |
| 306 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 307 | (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr + |
| 308 | sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 309 | fru->terminus_handle = htole16(terminus_handle); |
| 310 | fru->fru_rsi = htole16(fru_rsi); |
| 311 | fru->entity_type = htole16(entity_type); |
| 312 | fru->entity_instance_num = htole16(entity_instance_num); |
| 313 | fru->container_id = htole16(container_id); |
| 314 | |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 315 | return pldm_pdr_add_check(repo, data, size, false, terminus_handle, |
| 316 | bmc_record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 317 | } |
| 318 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 319 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 320 | const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 321 | const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle, |
| 322 | uint16_t *entity_type, uint16_t *entity_instance_num, |
| 323 | uint16_t *container_id) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 324 | { |
Andrew Jeffery | 01425e9 | 2023-06-30 13:47:40 +0930 | [diff] [blame] | 325 | if (!repo || !terminus_handle || !entity_type || !entity_instance_num || |
| 326 | !container_id) { |
| 327 | return NULL; |
| 328 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 329 | |
| 330 | uint8_t *data = NULL; |
| 331 | uint32_t size = 0; |
| 332 | const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 333 | repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 334 | while (curr_record != NULL) { |
| 335 | struct pldm_pdr_fru_record_set *fru = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 336 | (struct pldm_pdr_fru_record_set |
| 337 | *)(data + sizeof(struct pldm_pdr_hdr)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 338 | if (fru->fru_rsi == htole16(fru_rsi)) { |
| 339 | *terminus_handle = le16toh(fru->terminus_handle); |
| 340 | *entity_type = le16toh(fru->entity_type); |
| 341 | *entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 342 | le16toh(fru->entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 343 | *container_id = le16toh(fru->container_id); |
| 344 | return curr_record; |
| 345 | } |
| 346 | data = NULL; |
| 347 | curr_record = pldm_pdr_find_record_by_type( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 348 | repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, |
| 349 | &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | *terminus_handle = 0; |
| 353 | *entity_type = 0; |
| 354 | *entity_instance_num = 0; |
| 355 | *container_id = 0; |
| 356 | |
| 357 | return NULL; |
| 358 | } |
| 359 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 360 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 361 | /* NOLINTNEXTLINE(readability-identifier-naming) */ |
| 362 | void pldm_pdr_update_TL_pdr(const pldm_pdr *repo, uint16_t terminus_handle, |
| 363 | uint8_t tid, uint8_t tl_eid, bool valid_bit) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 364 | { |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 365 | uint8_t *out_data = NULL; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 366 | uint32_t size = 0; |
| 367 | const pldm_pdr_record *record; |
| 368 | record = pldm_pdr_find_record_by_type(repo, PLDM_TERMINUS_LOCATOR_PDR, |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 369 | NULL, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 370 | |
| 371 | do { |
| 372 | if (record != NULL) { |
| 373 | struct pldm_terminus_locator_pdr *pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 374 | (struct pldm_terminus_locator_pdr *)out_data; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 375 | struct pldm_terminus_locator_type_mctp_eid *value = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 376 | (struct pldm_terminus_locator_type_mctp_eid *) |
| 377 | pdr->terminus_locator_value; |
Andrew Jeffery | 6005f1c | 2023-04-05 20:02:52 +0930 | [diff] [blame] | 378 | if (pdr->terminus_handle == terminus_handle && |
| 379 | pdr->tid == tid && value->eid == tl_eid) { |
| 380 | pdr->validity = valid_bit; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | } |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 384 | record = pldm_pdr_find_record_by_type(repo, |
| 385 | PLDM_TERMINUS_LOCATOR_PDR, |
| 386 | record, &out_data, &size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 387 | } while (record); |
| 388 | } |
| 389 | |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 390 | static bool pldm_record_handle_in_range(uint32_t record_handle, |
| 391 | uint32_t first_record_handle, |
| 392 | uint32_t last_record_handle) |
| 393 | { |
| 394 | return record_handle >= first_record_handle && |
| 395 | record_handle <= last_record_handle; |
| 396 | } |
| 397 | |
| 398 | LIBPLDM_ABI_TESTING |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 399 | int pldm_pdr_find_child_container_id_index_range_exclude( |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 400 | const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance, |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 401 | uint8_t child_index, uint32_t range_exclude_start_handle, |
| 402 | uint32_t range_exclude_end_handle, uint16_t *container_id) |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 403 | { |
| 404 | pldm_pdr_record *record; |
| 405 | if (!repo) { |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | for (record = repo->first; record; record = record->next) { |
| 410 | bool is_container_entity_instance_number; |
| 411 | struct pldm_pdr_entity_association *pdr; |
| 412 | bool is_container_entity_type; |
| 413 | struct pldm_entity *child; |
| 414 | struct pldm_pdr_hdr *hdr; |
| 415 | bool in_range; |
| 416 | |
| 417 | // pldm_pdr_add() takes only uint8_t* data as an argument. |
| 418 | // The expectation here is the pldm_pdr_hdr is the first field of the record data |
| 419 | hdr = (struct pldm_pdr_hdr *)record->data; |
| 420 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 421 | continue; |
| 422 | } |
| 423 | in_range = pldm_record_handle_in_range( |
| 424 | record->record_handle, range_exclude_start_handle, |
| 425 | range_exclude_end_handle); |
| 426 | if (in_range) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | // this cast is valid with respect to alignment because |
| 431 | // struct pldm_pdr_hdr is declared with __attribute__((packed)) |
| 432 | pdr = (void *)(record->data + sizeof(struct pldm_pdr_hdr)); |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 433 | if (child_index >= pdr->num_children) { |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 434 | continue; |
| 435 | } |
Pavithra Barithaya | 8cf7045 | 2023-06-22 04:36:19 -0500 | [diff] [blame] | 436 | |
| 437 | child = (&pdr->children[child_index]); |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 438 | is_container_entity_type = pdr->container.entity_type == |
| 439 | entity_type; |
| 440 | is_container_entity_instance_number = |
| 441 | pdr->container.entity_instance_num == entity_instance; |
| 442 | if (is_container_entity_type && |
| 443 | is_container_entity_instance_number) { |
| 444 | *container_id = le16toh(child->entity_container_id); |
Pavithra Barithaya | ffd5342 | 2023-06-23 23:20:48 -0500 | [diff] [blame] | 445 | return 0; |
Pavithra Barithaya | 5dc0257 | 2023-05-19 09:24:36 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | return -ENOKEY; |
| 449 | } |
| 450 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 451 | typedef struct pldm_entity_association_tree { |
| 452 | pldm_entity_node *root; |
| 453 | uint16_t last_used_container_id; |
| 454 | } pldm_entity_association_tree; |
| 455 | |
| 456 | typedef struct pldm_entity_node { |
| 457 | pldm_entity entity; |
| 458 | pldm_entity parent; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 459 | uint16_t remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 460 | pldm_entity_node *first_child; |
| 461 | pldm_entity_node *next_sibling; |
| 462 | uint8_t association_type; |
| 463 | } pldm_entity_node; |
| 464 | |
| 465 | static inline uint16_t next_container_id(pldm_entity_association_tree *tree) |
| 466 | { |
| 467 | assert(tree != NULL); |
| 468 | assert(tree->last_used_container_id != UINT16_MAX); |
| 469 | |
| 470 | return ++tree->last_used_container_id; |
| 471 | } |
| 472 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 473 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 474 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 475 | { |
| 476 | assert(node != NULL); |
| 477 | |
| 478 | return node->entity; |
| 479 | } |
| 480 | |
Pavithra Barithaya | def8e02 | 2023-08-16 00:31:06 -0500 | [diff] [blame] | 481 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 482 | uint16_t |
| 483 | pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity) |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 484 | { |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 485 | assert(entity != NULL); |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 486 | |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 487 | return entity->remote_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 488 | } |
| 489 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 490 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 491 | pldm_entity_association_tree *pldm_entity_association_tree_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 492 | { |
| 493 | pldm_entity_association_tree *tree = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 494 | malloc(sizeof(pldm_entity_association_tree)); |
Andrew Jeffery | c40037d | 2023-06-30 13:50:12 +0930 | [diff] [blame] | 495 | if (!tree) { |
| 496 | return NULL; |
| 497 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 498 | tree->root = NULL; |
| 499 | tree->last_used_container_id = 0; |
| 500 | |
| 501 | return tree; |
| 502 | } |
| 503 | |
| 504 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 505 | uint16_t entity_type) |
| 506 | { |
| 507 | assert(start != NULL); |
| 508 | |
| 509 | /* Insert after the the last node that matches the input entity type, or |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 510 | * at the end if no such match occurs |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 511 | */ |
| 512 | while (start->next_sibling != NULL) { |
| 513 | uint16_t this_type = start->entity.entity_type; |
| 514 | pldm_entity_node *next = start->next_sibling; |
| 515 | if (this_type == entity_type && |
| 516 | (this_type != next->entity.entity_type)) { |
| 517 | break; |
| 518 | } |
| 519 | start = start->next_sibling; |
| 520 | } |
| 521 | |
| 522 | return start; |
| 523 | } |
| 524 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 525 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 526 | pldm_entity_node *pldm_entity_association_tree_add( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 527 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 528 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 529 | uint8_t association_type) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 530 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 531 | return pldm_entity_association_tree_add_entity(tree, entity, |
| 532 | entity_instance_number, |
| 533 | parent, association_type, |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 534 | false, true, 0xffff); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Pavithra Barithaya | 1ade87f | 2023-07-28 04:03:01 -0500 | [diff] [blame] | 537 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 538 | pldm_entity_node *pldm_entity_association_tree_add_entity( |
| 539 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 540 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 541 | uint8_t association_type, bool is_remote, bool is_update_container_id, |
| 542 | uint16_t container_id) |
| 543 | { |
| 544 | if ((!tree) || (!entity)) { |
| 545 | return NULL; |
| 546 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 547 | |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 548 | if (entity_instance_number != 0xffff && parent != NULL) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 549 | pldm_entity node; |
| 550 | node.entity_type = entity->entity_type; |
| 551 | node.entity_instance_num = entity_instance_number; |
| 552 | if (pldm_is_current_parent_child(parent, &node)) { |
| 553 | return NULL; |
| 554 | } |
| 555 | } |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 556 | if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL && |
| 557 | association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) { |
| 558 | return NULL; |
| 559 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 560 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 561 | if (!node) { |
| 562 | return NULL; |
| 563 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 564 | node->first_child = NULL; |
| 565 | node->next_sibling = NULL; |
| 566 | node->parent.entity_type = 0; |
| 567 | node->parent.entity_instance_num = 0; |
| 568 | node->parent.entity_container_id = 0; |
| 569 | node->entity.entity_type = entity->entity_type; |
| 570 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 571 | entity_instance_number != 0xffff ? entity_instance_number : 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 572 | node->association_type = association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 573 | node->remote_container_id = 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 574 | if (tree->root == NULL) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 575 | if (parent != NULL) { |
| 576 | free(node); |
| 577 | return NULL; |
| 578 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 579 | tree->root = node; |
| 580 | /* container_id 0 here indicates this is the top-most entry */ |
| 581 | node->entity.entity_container_id = 0; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 582 | node->remote_container_id = node->entity.entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 583 | } else if (parent != NULL && parent->first_child == NULL) { |
Andrew Jeffery | 7062382 | 2023-08-10 16:05:27 +0930 | [diff] [blame] | 584 | /* Ensure next_container_id() will yield a valid ID */ |
| 585 | if (tree->last_used_container_id == UINT16_MAX) { |
| 586 | free(node); |
| 587 | return NULL; |
| 588 | } |
| 589 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 590 | parent->first_child = node; |
| 591 | node->parent = parent->entity; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 592 | |
| 593 | if (is_remote) { |
| 594 | node->remote_container_id = entity->entity_container_id; |
| 595 | } |
| 596 | if (is_update_container_id) { |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 597 | if (container_id != 0xffff) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 598 | node->entity.entity_container_id = container_id; |
| 599 | } else { |
| 600 | node->entity.entity_container_id = |
| 601 | next_container_id(tree); |
| 602 | } |
| 603 | } else { |
| 604 | node->entity.entity_container_id = |
| 605 | entity->entity_container_id; |
| 606 | } |
| 607 | |
| 608 | if (!is_remote) { |
| 609 | node->remote_container_id = |
| 610 | node->entity.entity_container_id; |
| 611 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 612 | } else { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 613 | pldm_entity_node *start = parent == NULL ? tree->root : |
| 614 | parent->first_child; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 615 | pldm_entity_node *prev = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 616 | find_insertion_at(start, entity->entity_type); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 617 | if (!prev) { |
| 618 | free(node); |
| 619 | return NULL; |
| 620 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 621 | pldm_entity_node *next = prev->next_sibling; |
| 622 | if (prev->entity.entity_type == entity->entity_type) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 623 | if (prev->entity.entity_instance_num == UINT16_MAX) { |
| 624 | free(node); |
| 625 | return NULL; |
| 626 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 627 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 628 | entity_instance_number != 0xffff ? |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 629 | entity_instance_number : |
| 630 | prev->entity.entity_instance_num + 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 631 | } |
| 632 | prev->next_sibling = node; |
| 633 | node->parent = prev->parent; |
| 634 | node->next_sibling = next; |
| 635 | node->entity.entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 636 | prev->entity.entity_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 637 | node->remote_container_id = entity->entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 638 | } |
| 639 | entity->entity_instance_num = node->entity.entity_instance_num; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 640 | if (is_update_container_id) { |
| 641 | entity->entity_container_id = node->entity.entity_container_id; |
| 642 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 643 | return node; |
| 644 | } |
| 645 | |
| 646 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 647 | { |
| 648 | if (node == NULL) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | ++(*num); |
| 653 | get_num_nodes(node->next_sibling, num); |
| 654 | get_num_nodes(node->first_child, num); |
| 655 | } |
| 656 | |
| 657 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 658 | pldm_entity *entities, size_t *index) |
| 659 | { |
| 660 | if (node == NULL) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | pldm_entity *entity = &entities[*index]; |
| 665 | ++(*index); |
| 666 | entity->entity_type = node->entity.entity_type; |
| 667 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 668 | entity->entity_container_id = node->entity.entity_container_id; |
| 669 | |
| 670 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 671 | entity_association_tree_visit(node->first_child, entities, index); |
| 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_visit(pldm_entity_association_tree *tree, |
| 676 | pldm_entity **entities, size_t *size) |
| 677 | { |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 678 | if (!tree || !entities || !size) { |
| 679 | return; |
| 680 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 681 | |
| 682 | *size = 0; |
| 683 | if (tree->root == NULL) { |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | get_num_nodes(tree->root, size); |
| 688 | *entities = malloc(*size * sizeof(pldm_entity)); |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 689 | if (!entities) { |
| 690 | return; |
| 691 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 692 | size_t index = 0; |
| 693 | entity_association_tree_visit(tree->root, *entities, &index); |
| 694 | } |
| 695 | |
| 696 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 697 | { |
| 698 | if (node == NULL) { |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | entity_association_tree_destroy(node->next_sibling); |
| 703 | entity_association_tree_destroy(node->first_child); |
| 704 | free(node); |
| 705 | } |
| 706 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 707 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 708 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 709 | { |
Andrew Jeffery | a89e015 | 2023-06-30 14:24:05 +0930 | [diff] [blame] | 710 | if (!tree) { |
| 711 | return; |
| 712 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 713 | |
| 714 | entity_association_tree_destroy(tree->root); |
| 715 | free(tree); |
| 716 | } |
| 717 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 718 | LIBPLDM_ABI_STABLE |
| 719 | bool pldm_entity_is_node_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->first_child != NULL; |
| 724 | } |
| 725 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 726 | LIBPLDM_ABI_STABLE |
| 727 | pldm_entity pldm_entity_get_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 728 | { |
| 729 | assert(node != NULL); |
| 730 | |
| 731 | return node->parent; |
| 732 | } |
| 733 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 734 | LIBPLDM_ABI_STABLE |
| 735 | bool pldm_entity_is_exist_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 736 | { |
| 737 | assert(node != NULL); |
| 738 | |
| 739 | if (node->parent.entity_type == 0 && |
| 740 | node->parent.entity_instance_num == 0 && |
| 741 | node->parent.entity_container_id == 0) { |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | return true; |
| 746 | } |
| 747 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 748 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 749 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 750 | uint8_t association_type) |
| 751 | { |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 752 | if (!node) { |
| 753 | return 0; |
| 754 | } |
| 755 | |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 756 | if (!(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 757 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL)) { |
| 758 | return 0; |
| 759 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 760 | |
| 761 | size_t count = 0; |
| 762 | pldm_entity_node *curr = node->first_child; |
| 763 | while (curr != NULL) { |
| 764 | if (curr->association_type == association_type) { |
| 765 | ++count; |
| 766 | } |
| 767 | curr = curr->next_sibling; |
| 768 | } |
| 769 | |
| 770 | assert(count < UINT8_MAX); |
Andrew Jeffery | c83ef86 | 2023-07-11 17:16:55 +0930 | [diff] [blame] | 771 | return count < UINT8_MAX ? count : 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 772 | } |
| 773 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 774 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 775 | bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node) |
| 776 | { |
Andrew Jeffery | 375d9fc | 2023-06-30 15:45:54 +0930 | [diff] [blame] | 777 | if (!parent || !node) { |
| 778 | return false; |
| 779 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 780 | |
| 781 | pldm_entity_node *curr = parent->first_child; |
| 782 | while (curr != NULL) { |
| 783 | if (node->entity_type == curr->entity.entity_type && |
| 784 | node->entity_instance_num == |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 785 | curr->entity.entity_instance_num) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 786 | return true; |
| 787 | } |
| 788 | curr = curr->next_sibling; |
| 789 | } |
| 790 | |
| 791 | return false; |
| 792 | } |
| 793 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 794 | static int entity_association_pdr_add_children( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 795 | pldm_entity_node *curr, pldm_pdr *repo, uint16_t size, |
| 796 | uint8_t contained_count, uint8_t association_type, bool is_remote, |
| 797 | uint16_t terminus_handle, uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 798 | { |
| 799 | uint8_t pdr[size]; |
| 800 | uint8_t *start = pdr; |
| 801 | |
| 802 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 803 | hdr->version = 1; |
Pavithra Barithaya | 39eb322 | 2023-11-17 03:30:27 -0600 | [diff] [blame] | 804 | hdr->record_handle = record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 805 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 806 | hdr->record_change_num = 0; |
| 807 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
| 808 | start += sizeof(struct pldm_pdr_hdr); |
| 809 | |
| 810 | uint16_t *container_id = (uint16_t *)start; |
| 811 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
| 812 | start += sizeof(uint16_t); |
| 813 | *start = association_type; |
| 814 | start += sizeof(uint8_t); |
| 815 | |
| 816 | pldm_entity *entity = (pldm_entity *)start; |
| 817 | entity->entity_type = htole16(curr->entity.entity_type); |
| 818 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 819 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
| 820 | start += sizeof(pldm_entity); |
| 821 | |
| 822 | *start = contained_count; |
| 823 | start += sizeof(uint8_t); |
| 824 | |
| 825 | pldm_entity_node *node = curr->first_child; |
| 826 | while (node != NULL) { |
| 827 | if (node->association_type == association_type) { |
| 828 | pldm_entity *entity = (pldm_entity *)start; |
| 829 | entity->entity_type = htole16(node->entity.entity_type); |
| 830 | entity->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 831 | htole16(node->entity.entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 832 | entity->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 833 | htole16(node->entity.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 834 | start += sizeof(pldm_entity); |
| 835 | } |
| 836 | node = node->next_sibling; |
| 837 | } |
| 838 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 839 | return pldm_pdr_add_check(repo, pdr, size, is_remote, terminus_handle, |
| 840 | &record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 841 | } |
| 842 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 843 | static int entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 844 | pldm_pdr *repo, bool is_remote, |
| 845 | uint16_t terminus_handle, |
| 846 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 847 | { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 848 | uint8_t num_logical_children = pldm_entity_get_num_children( |
| 849 | curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 850 | uint8_t num_physical_children = pldm_entity_get_num_children( |
| 851 | curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 852 | int rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 853 | |
| 854 | if (num_logical_children) { |
| 855 | uint16_t logical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 856 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 857 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 858 | sizeof(uint8_t) + |
| 859 | (num_logical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 860 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 861 | curr, repo, logical_pdr_size, num_logical_children, |
| 862 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 863 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 864 | if (rc < 0) { |
| 865 | return rc; |
| 866 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (num_physical_children) { |
| 870 | uint16_t physical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 871 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 872 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 873 | sizeof(uint8_t) + |
| 874 | (num_physical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 875 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 876 | curr, repo, physical_pdr_size, num_physical_children, |
| 877 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 878 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 879 | if (rc < 0) { |
| 880 | return rc; |
| 881 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 882 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 883 | |
| 884 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 885 | } |
| 886 | |
Andrew Jeffery | d09b1af | 2023-07-17 12:39:16 +0930 | [diff] [blame] | 887 | static bool is_present(pldm_entity entity, pldm_entity **entities, |
| 888 | size_t num_entities) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 889 | { |
| 890 | if (entities == NULL || num_entities == 0) { |
| 891 | return true; |
| 892 | } |
| 893 | size_t i = 0; |
| 894 | while (i < num_entities) { |
| 895 | if ((*entities + i)->entity_type == entity.entity_type) { |
| 896 | return true; |
| 897 | } |
| 898 | i++; |
| 899 | } |
| 900 | return false; |
| 901 | } |
| 902 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 903 | static int entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 904 | pldm_entity **entities, |
| 905 | size_t num_entities, bool is_remote, |
| 906 | uint16_t terminus_handle, |
| 907 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 908 | { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 909 | int rc; |
| 910 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 911 | if (curr == NULL) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 912 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 913 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 914 | |
| 915 | if (is_present(curr->entity, entities, num_entities)) { |
| 916 | rc = entity_association_pdr_add_entry( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 917 | curr, repo, is_remote, terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 918 | if (rc) { |
| 919 | return rc; |
| 920 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 921 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 922 | |
| 923 | rc = entity_association_pdr_add(curr->next_sibling, repo, entities, |
| 924 | num_entities, is_remote, |
| 925 | terminus_handle, record_handle); |
| 926 | if (rc) { |
| 927 | return rc; |
| 928 | } |
| 929 | |
| 930 | return entity_association_pdr_add(curr->first_child, repo, entities, |
| 931 | num_entities, is_remote, |
| 932 | terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 933 | } |
| 934 | |
Andrew Jeffery | 096685b | 2023-07-17 17:36:14 +0930 | [diff] [blame] | 935 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 936 | int pldm_entity_association_pdr_add_check(pldm_entity_association_tree *tree, |
| 937 | pldm_pdr *repo, bool is_remote, |
| 938 | uint16_t terminus_handle) |
| 939 | { |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 940 | if (!tree || !repo) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 941 | return 0; |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 942 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 943 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 944 | return entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote, |
| 945 | terminus_handle, 0); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 946 | } |
| 947 | |
Andrew Jeffery | 1354a6e | 2023-07-07 10:34:38 +0930 | [diff] [blame] | 948 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | cc39452 | 2023-07-03 12:49:31 +0930 | [diff] [blame] | 949 | int pldm_entity_association_pdr_add_from_node_check( |
| 950 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 951 | size_t num_entities, bool is_remote, uint16_t terminus_handle) |
| 952 | { |
| 953 | return pldm_entity_association_pdr_add_from_node_with_record_handle( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 954 | node, repo, entities, num_entities, is_remote, terminus_handle, |
| 955 | 0); |
| 956 | } |
| 957 | |
Pavithra Barithaya | 3a26705 | 2023-11-13 05:01:36 -0600 | [diff] [blame] | 958 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 959 | int pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 960 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 961 | size_t num_entities, bool is_remote, uint16_t terminus_handle, |
| 962 | uint32_t record_handle) |
| 963 | { |
| 964 | if (!node || !repo || !entities) { |
| 965 | return -EINVAL; |
| 966 | } |
| 967 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 968 | entity_association_pdr_add(node, repo, entities, num_entities, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 969 | is_remote, terminus_handle, record_handle); |
| 970 | |
| 971 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 972 | } |
| 973 | |
Andrew Jeffery | 643c443 | 2023-07-17 15:36:03 +0930 | [diff] [blame] | 974 | static void find_entity_ref_in_tree(pldm_entity_node *tree_node, |
| 975 | pldm_entity entity, pldm_entity_node **node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 976 | { |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 977 | bool is_entity_container_id; |
| 978 | bool is_entity_instance_num; |
| 979 | bool is_type; |
| 980 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 981 | if (tree_node == NULL) { |
| 982 | return; |
| 983 | } |
| 984 | |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 985 | is_type = tree_node->entity.entity_type == entity.entity_type; |
| 986 | is_entity_instance_num = tree_node->entity.entity_instance_num == |
| 987 | entity.entity_instance_num; |
| 988 | is_entity_container_id = tree_node->entity.entity_container_id == |
| 989 | entity.entity_container_id; |
| 990 | |
| 991 | if (is_type && is_entity_instance_num && is_entity_container_id) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 992 | *node = tree_node; |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | find_entity_ref_in_tree(tree_node->first_child, entity, node); |
| 997 | find_entity_ref_in_tree(tree_node->next_sibling, entity, node); |
| 998 | } |
| 999 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1000 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1001 | void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree, |
| 1002 | pldm_entity entity, pldm_entity_node **node) |
| 1003 | { |
Andrew Jeffery | ba47e83 | 2023-07-03 11:41:03 +0930 | [diff] [blame] | 1004 | if (!tree || !node) { |
| 1005 | return; |
| 1006 | } |
| 1007 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1008 | find_entity_ref_in_tree(tree->root, entity, node); |
| 1009 | } |
| 1010 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1011 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1012 | void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo, |
| 1013 | uint16_t terminus_handle) |
| 1014 | { |
Andrew Jeffery | 438dd49 | 2023-07-03 11:51:43 +0930 | [diff] [blame] | 1015 | if (!repo) { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1019 | bool removed = false; |
| 1020 | |
| 1021 | pldm_pdr_record *record = repo->first; |
| 1022 | pldm_pdr_record *prev = NULL; |
| 1023 | while (record != NULL) { |
| 1024 | pldm_pdr_record *next = record->next; |
| 1025 | if (record->terminus_handle == terminus_handle) { |
| 1026 | if (repo->first == record) { |
| 1027 | repo->first = next; |
| 1028 | } else { |
| 1029 | prev->next = next; |
| 1030 | } |
| 1031 | if (repo->last == record) { |
| 1032 | repo->last = prev; |
| 1033 | } |
| 1034 | if (record->data) { |
| 1035 | free(record->data); |
| 1036 | } |
| 1037 | --repo->record_count; |
| 1038 | repo->size -= record->size; |
| 1039 | free(record); |
| 1040 | removed = true; |
| 1041 | } else { |
| 1042 | prev = record; |
| 1043 | } |
| 1044 | record = next; |
| 1045 | } |
| 1046 | |
| 1047 | if (removed == true) { |
| 1048 | record = repo->first; |
| 1049 | uint32_t record_handle = 0; |
| 1050 | while (record != NULL) { |
| 1051 | record->record_handle = ++record_handle; |
| 1052 | if (record->data != NULL) { |
| 1053 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1054 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1055 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1056 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1057 | } |
| 1058 | record = record->next; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1062 | |
| 1063 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1064 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 1065 | { |
Andrew Jeffery | 3e1d659 | 2023-07-03 11:57:16 +0930 | [diff] [blame] | 1066 | if (!repo) { |
| 1067 | return; |
| 1068 | } |
| 1069 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1070 | bool removed = false; |
| 1071 | |
| 1072 | pldm_pdr_record *record = repo->first; |
| 1073 | pldm_pdr_record *prev = NULL; |
| 1074 | while (record != NULL) { |
| 1075 | pldm_pdr_record *next = record->next; |
| 1076 | if (record->is_remote == true) { |
| 1077 | if (repo->first == record) { |
| 1078 | repo->first = next; |
| 1079 | } else { |
| 1080 | prev->next = next; |
| 1081 | } |
| 1082 | if (repo->last == record) { |
| 1083 | repo->last = prev; |
| 1084 | } |
| 1085 | if (record->data) { |
| 1086 | free(record->data); |
| 1087 | } |
| 1088 | --repo->record_count; |
| 1089 | repo->size -= record->size; |
| 1090 | free(record); |
| 1091 | removed = true; |
| 1092 | } else { |
| 1093 | prev = record; |
| 1094 | } |
| 1095 | record = next; |
| 1096 | } |
| 1097 | |
| 1098 | if (removed == true) { |
| 1099 | record = repo->first; |
| 1100 | uint32_t record_handle = 0; |
| 1101 | while (record != NULL) { |
| 1102 | record->record_handle = ++record_handle; |
| 1103 | if (record->data != NULL) { |
| 1104 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1105 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1106 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1107 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1108 | } |
| 1109 | record = record->next; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
Pavithra Barithaya | 4a307bc | 2023-11-13 04:34:14 -0600 | [diff] [blame] | 1114 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 4d69434 | 2023-05-19 08:04:41 -0500 | [diff] [blame] | 1115 | pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo, |
| 1116 | uint32_t first, uint32_t last) |
| 1117 | { |
| 1118 | pldm_pdr_record *record = NULL; |
| 1119 | pldm_pdr_record *curr; |
| 1120 | |
| 1121 | if (!repo) { |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | for (curr = repo->first; curr; curr = curr->next) { |
| 1125 | if (first > curr->record_handle || last < curr->record_handle) { |
| 1126 | continue; |
| 1127 | } |
| 1128 | if (!record || curr->record_handle > record->record_handle) { |
| 1129 | record = curr; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | return record; |
| 1134 | } |
| 1135 | |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1136 | static void entity_association_tree_find_if_remote(pldm_entity_node *node, |
| 1137 | pldm_entity *entity, |
| 1138 | pldm_entity_node **out, |
| 1139 | bool is_remote) |
| 1140 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1141 | if (node == NULL) { |
| 1142 | return; |
| 1143 | } |
| 1144 | bool is_entity_type; |
| 1145 | bool is_entity_instance_num; |
| 1146 | |
| 1147 | is_entity_type = node->entity.entity_type == entity->entity_type; |
| 1148 | is_entity_instance_num = node->entity.entity_instance_num == |
| 1149 | entity->entity_instance_num; |
| 1150 | |
| 1151 | if (!is_remote || |
| 1152 | node->remote_container_id == entity->entity_container_id) { |
| 1153 | if (is_entity_type && is_entity_instance_num) { |
| 1154 | entity->entity_container_id = |
| 1155 | node->entity.entity_container_id; |
| 1156 | *out = node; |
| 1157 | return; |
| 1158 | } |
| 1159 | } |
| 1160 | entity_association_tree_find_if_remote(node->next_sibling, entity, out, |
| 1161 | is_remote); |
| 1162 | entity_association_tree_find_if_remote(node->first_child, entity, out, |
| 1163 | is_remote); |
| 1164 | } |
| 1165 | |
Pavithra Barithaya | 7570fae | 2023-07-28 02:15:16 -0500 | [diff] [blame] | 1166 | LIBPLDM_ABI_STABLE |
Sagar Srinivas | 302d9ff | 2023-08-01 02:11:30 -0500 | [diff] [blame] | 1167 | pldm_entity_node *pldm_entity_association_tree_find_with_locality( |
| 1168 | pldm_entity_association_tree *tree, pldm_entity *entity, bool is_remote) |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1169 | { |
| 1170 | if (!tree || !entity) { |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | pldm_entity_node *node = NULL; |
| 1174 | entity_association_tree_find_if_remote(tree->root, entity, &node, |
| 1175 | is_remote); |
| 1176 | return node; |
| 1177 | } |
| 1178 | |
Andrew Jeffery | 54d91e8 | 2023-07-17 15:37:12 +0930 | [diff] [blame] | 1179 | static void entity_association_tree_find(pldm_entity_node *node, |
| 1180 | pldm_entity *entity, |
| 1181 | pldm_entity_node **out) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1182 | { |
| 1183 | if (node == NULL) { |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | if (node->entity.entity_type == entity->entity_type && |
| 1188 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 1189 | entity->entity_container_id = node->entity.entity_container_id; |
| 1190 | *out = node; |
| 1191 | return; |
| 1192 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1193 | entity_association_tree_find(node->next_sibling, entity, out); |
| 1194 | entity_association_tree_find(node->first_child, entity, out); |
| 1195 | } |
| 1196 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1197 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1198 | pldm_entity_node * |
| 1199 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 1200 | pldm_entity *entity) |
| 1201 | { |
Andrew Jeffery | 94e364d | 2023-07-03 12:26:59 +0930 | [diff] [blame] | 1202 | if (!tree || !entity) { |
| 1203 | return NULL; |
| 1204 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1205 | |
| 1206 | pldm_entity_node *node = NULL; |
| 1207 | entity_association_tree_find(tree->root, entity, &node); |
| 1208 | return node; |
| 1209 | } |
| 1210 | |
| 1211 | static void entity_association_tree_copy(pldm_entity_node *org_node, |
| 1212 | pldm_entity_node **new_node) |
| 1213 | { |
| 1214 | if (org_node == NULL) { |
| 1215 | return; |
| 1216 | } |
| 1217 | *new_node = malloc(sizeof(pldm_entity_node)); |
| 1218 | (*new_node)->parent = org_node->parent; |
| 1219 | (*new_node)->entity = org_node->entity; |
| 1220 | (*new_node)->association_type = org_node->association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 1221 | (*new_node)->remote_container_id = org_node->remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1222 | (*new_node)->first_child = NULL; |
| 1223 | (*new_node)->next_sibling = NULL; |
| 1224 | entity_association_tree_copy(org_node->first_child, |
| 1225 | &((*new_node)->first_child)); |
| 1226 | entity_association_tree_copy(org_node->next_sibling, |
| 1227 | &((*new_node)->next_sibling)); |
| 1228 | } |
| 1229 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1230 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1231 | void pldm_entity_association_tree_copy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1232 | pldm_entity_association_tree *org_tree, |
| 1233 | pldm_entity_association_tree *new_tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1234 | { |
George Liu | c6c391d | 2023-11-09 10:13:34 +0800 | [diff] [blame] | 1235 | assert(org_tree != NULL); |
| 1236 | assert(new_tree != NULL); |
| 1237 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1238 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 1239 | entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 1240 | } |
| 1241 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1242 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1243 | void pldm_entity_association_tree_destroy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1244 | pldm_entity_association_tree *tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1245 | { |
Andrew Jeffery | 85d7a05 | 2023-07-03 12:29:24 +0930 | [diff] [blame] | 1246 | if (!tree) { |
| 1247 | return; |
| 1248 | } |
| 1249 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1250 | entity_association_tree_destroy(tree->root); |
| 1251 | tree->last_used_container_id = 0; |
| 1252 | tree->root = NULL; |
| 1253 | } |
| 1254 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1255 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1256 | bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree) |
| 1257 | { |
| 1258 | return ((tree->root == NULL) ? true : false); |
| 1259 | } |
| 1260 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1261 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1262 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 1263 | size_t *num_entities, |
| 1264 | pldm_entity **entities) |
| 1265 | { |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1266 | if (!pdr || !num_entities || !entities) { |
| 1267 | return; |
| 1268 | } |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1269 | if (pdr_len < PDR_ENTITY_ASSOCIATION_MIN_SIZE) { |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1270 | return; |
| 1271 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1272 | |
| 1273 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1274 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 1275 | return; |
| 1276 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1277 | |
| 1278 | const uint8_t *start = (uint8_t *)pdr; |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1279 | const uint8_t *end __attribute__((unused)) = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1280 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1281 | start += sizeof(struct pldm_pdr_hdr); |
| 1282 | struct pldm_pdr_entity_association *entity_association_pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1283 | (struct pldm_pdr_entity_association *)start; |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1284 | size_t l_num_entities = entity_association_pdr->num_children + 1; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1285 | if (l_num_entities < 2) { |
| 1286 | return; |
| 1287 | } |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1288 | if (start + sizeof(struct pldm_pdr_entity_association) + |
| 1289 | sizeof(pldm_entity) * (l_num_entities - 2) != |
| 1290 | end) { |
| 1291 | return; |
| 1292 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1293 | pldm_entity *l_entities = malloc(sizeof(pldm_entity) * l_num_entities); |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1294 | if (!l_entities) { |
| 1295 | return; |
| 1296 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1297 | l_entities[0].entity_type = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1298 | le16toh(entity_association_pdr->container.entity_type); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1299 | l_entities[0].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1300 | le16toh(entity_association_pdr->container.entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1301 | l_entities[0].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1302 | le16toh(entity_association_pdr->container.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1303 | pldm_entity *curr_entity = entity_association_pdr->children; |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1304 | for (size_t i = 1; i < l_num_entities; i++, curr_entity++) { |
| 1305 | l_entities[i].entity_type = le16toh(curr_entity->entity_type); |
| 1306 | l_entities[i].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1307 | le16toh(curr_entity->entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1308 | l_entities[i].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1309 | le16toh(curr_entity->entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1310 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1311 | |
| 1312 | *num_entities = l_num_entities; |
| 1313 | *entities = l_entities; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1314 | } |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1315 | |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 1316 | /* Find the position of record in pldm_pdr repo and place new_record in |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1317 | * the same position. |
| 1318 | */ |
| 1319 | static int pldm_pdr_replace_record(pldm_pdr *repo, pldm_pdr_record *record, |
| 1320 | pldm_pdr_record *prev, |
| 1321 | pldm_pdr_record *new_record) |
| 1322 | { |
| 1323 | assert(repo); |
| 1324 | assert(record); |
| 1325 | assert(prev); |
| 1326 | assert(new_record); |
| 1327 | |
| 1328 | if (repo->size < record->size) { |
| 1329 | return -EOVERFLOW; |
| 1330 | } |
| 1331 | |
| 1332 | if (repo->size + new_record->size < new_record->size) { |
| 1333 | return -EOVERFLOW; |
| 1334 | } |
| 1335 | |
| 1336 | if (repo->first == record) { |
| 1337 | repo->first = new_record; |
| 1338 | } else { |
| 1339 | prev->next = new_record; |
| 1340 | } |
| 1341 | new_record->next = record->next; |
| 1342 | |
| 1343 | if (repo->last == record) { |
| 1344 | repo->last = new_record; |
| 1345 | } |
| 1346 | |
| 1347 | repo->size = (repo->size - record->size) + new_record->size; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | /* Insert a new record to pldm_pdr repo to a position that comes after |
| 1352 | * pldm_pdr_record record. |
| 1353 | */ |
| 1354 | static int pldm_pdr_insert_record(pldm_pdr *repo, pldm_pdr_record *record, |
| 1355 | pldm_pdr_record *new_record) |
| 1356 | { |
| 1357 | assert(repo); |
| 1358 | assert(record); |
| 1359 | assert(new_record); |
| 1360 | |
| 1361 | if (repo->size + new_record->size < new_record->size) { |
| 1362 | return -EOVERFLOW; |
| 1363 | } |
| 1364 | |
| 1365 | if (repo->record_count == UINT32_MAX) { |
| 1366 | return -EOVERFLOW; |
| 1367 | } |
| 1368 | |
| 1369 | new_record->next = record->next; |
| 1370 | record->next = new_record; |
| 1371 | |
| 1372 | if (repo->last == record) { |
| 1373 | repo->last = new_record; |
| 1374 | } |
| 1375 | |
| 1376 | repo->size = repo->size + new_record->size; |
| 1377 | ++repo->record_count; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | /* Find the position of PDR when its record handle is known |
| 1382 | */ |
| 1383 | static bool pldm_pdr_find_record_by_handle(pldm_pdr_record **record, |
| 1384 | pldm_pdr_record **prev, |
| 1385 | uint32_t record_handle) |
| 1386 | { |
| 1387 | assert(record); |
| 1388 | assert(prev); |
| 1389 | |
| 1390 | while (*record != NULL) { |
| 1391 | if ((*record)->record_handle == record_handle) { |
| 1392 | return true; |
| 1393 | } |
| 1394 | *prev = *record; |
| 1395 | *record = (*record)->next; |
| 1396 | } |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | LIBPLDM_ABI_TESTING |
| 1401 | int pldm_entity_association_pdr_add_contained_entity_to_remote_pdr( |
| 1402 | pldm_pdr *repo, pldm_entity *entity, uint32_t pdr_record_handle) |
| 1403 | { |
| 1404 | if (!repo || !entity) { |
| 1405 | return -EINVAL; |
| 1406 | } |
| 1407 | |
| 1408 | pldm_pdr_record *record = repo->first; |
| 1409 | pldm_pdr_record *prev = repo->first; |
| 1410 | int rc = 0; |
| 1411 | uint16_t header_length = 0; |
| 1412 | uint8_t num_children = 0; |
| 1413 | struct pldm_msgbuf _src; |
| 1414 | struct pldm_msgbuf *src = &_src; |
| 1415 | struct pldm_msgbuf _dst; |
| 1416 | struct pldm_msgbuf *dst = &_dst; |
| 1417 | |
| 1418 | pldm_pdr_find_record_by_handle(&record, &prev, pdr_record_handle); |
| 1419 | |
| 1420 | if (!record) { |
| 1421 | return -EINVAL; |
| 1422 | } |
| 1423 | // Initialize msg buffer for record and record->data |
| 1424 | rc = pldm_msgbuf_init_errno(src, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1425 | record->data, record->size); |
| 1426 | if (rc) { |
| 1427 | return rc; |
| 1428 | } |
| 1429 | |
| 1430 | // check if adding another entity to record causes overflow before |
| 1431 | // allocating memory for new_record. |
| 1432 | if (record->size + sizeof(pldm_entity) < sizeof(pldm_entity)) { |
| 1433 | return -EOVERFLOW; |
| 1434 | } |
| 1435 | pldm_pdr_record *new_record = malloc(sizeof(pldm_pdr_record)); |
| 1436 | if (!new_record) { |
| 1437 | return -ENOMEM; |
| 1438 | } |
| 1439 | |
| 1440 | new_record->data = malloc(record->size + sizeof(pldm_entity)); |
| 1441 | if (!new_record->data) { |
| 1442 | rc = -ENOMEM; |
| 1443 | goto cleanup_new_record; |
| 1444 | } |
| 1445 | |
| 1446 | new_record->record_handle = record->record_handle; |
| 1447 | new_record->size = record->size + sizeof(struct pldm_entity); |
| 1448 | new_record->is_remote = record->is_remote; |
| 1449 | |
| 1450 | // Initialize new PDR record with data from original PDR record. |
| 1451 | // Start with adding the header of original PDR |
| 1452 | rc = pldm_msgbuf_init_errno(dst, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1453 | new_record->data, new_record->size); |
| 1454 | if (rc) { |
| 1455 | goto cleanup_new_record_data; |
| 1456 | } |
| 1457 | |
| 1458 | pldm_msgbuf_copy(dst, src, uint32_t, hdr_record_handle); |
| 1459 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_version); |
| 1460 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_type); |
| 1461 | pldm_msgbuf_copy(dst, src, uint16_t, hdr_record_change_num); |
| 1462 | // extract the header length from record and increment size with |
| 1463 | // size of pldm_entity before inserting the value into new_record. |
| 1464 | rc = pldm_msgbuf_extract(src, header_length); |
| 1465 | if (rc) { |
| 1466 | goto cleanup_new_record_data; |
| 1467 | } |
| 1468 | static_assert(UINT16_MAX < (SIZE_MAX - sizeof(pldm_entity)), |
| 1469 | "Fix the following bounds check."); |
| 1470 | if (header_length + sizeof(pldm_entity) > UINT16_MAX) { |
| 1471 | rc = -EOVERFLOW; |
| 1472 | goto cleanup_new_record_data; |
| 1473 | } |
| 1474 | header_length += sizeof(pldm_entity); |
| 1475 | pldm_msgbuf_insert(dst, header_length); |
| 1476 | pldm_msgbuf_copy(dst, src, uint16_t, container_id); |
| 1477 | pldm_msgbuf_copy(dst, src, uint8_t, association_type); |
| 1478 | pldm_msgbuf_copy(dst, src, uint16_t, entity_type); |
| 1479 | pldm_msgbuf_copy(dst, src, uint16_t, entity_instance_num); |
| 1480 | pldm_msgbuf_copy(dst, src, uint16_t, entity_container_id); |
| 1481 | // extract value of number of children from record and increment it |
| 1482 | // by 1 before insert the value to new record. |
| 1483 | rc = pldm_msgbuf_extract(src, num_children); |
| 1484 | if (rc) { |
| 1485 | goto cleanup_new_record_data; |
| 1486 | } |
| 1487 | if (num_children == UINT8_MAX) { |
| 1488 | rc = -EOVERFLOW; |
| 1489 | goto cleanup_new_record_data; |
| 1490 | } |
| 1491 | num_children += 1; |
| 1492 | pldm_msgbuf_insert(dst, num_children); |
| 1493 | //Add all children of original PDR to new PDR |
| 1494 | for (int i = 0; i < num_children - 1; i++) { |
| 1495 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_type); |
| 1496 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_instance_num); |
| 1497 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_container_id); |
| 1498 | } |
| 1499 | |
| 1500 | // Add new contained entity as a child of new PDR |
| 1501 | rc = pldm_msgbuf_destroy(src); |
| 1502 | if (rc) { |
| 1503 | goto cleanup_new_record_data; |
| 1504 | } |
| 1505 | rc = pldm_msgbuf_init_errno(src, sizeof(struct pldm_entity), entity, |
| 1506 | sizeof(struct pldm_entity)); |
| 1507 | if (rc) { |
| 1508 | goto cleanup_new_record_data; |
| 1509 | } |
| 1510 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_type); |
| 1511 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_instance_num); |
| 1512 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_container_id); |
| 1513 | |
| 1514 | rc = pldm_msgbuf_destroy(src); |
| 1515 | if (rc) { |
| 1516 | goto cleanup_new_record_data; |
| 1517 | } |
| 1518 | rc = pldm_msgbuf_destroy(dst); |
| 1519 | if (rc) { |
| 1520 | goto cleanup_new_record_data; |
| 1521 | } |
| 1522 | |
| 1523 | rc = pldm_pdr_replace_record(repo, record, prev, new_record); |
| 1524 | if (rc) { |
| 1525 | goto cleanup_new_record_data; |
| 1526 | } |
| 1527 | |
| 1528 | free(record->data); |
| 1529 | free(record); |
| 1530 | return rc; |
| 1531 | cleanup_new_record_data: |
| 1532 | free(new_record->data); |
| 1533 | cleanup_new_record: |
| 1534 | free(new_record); |
| 1535 | return rc; |
| 1536 | } |
| 1537 | |
| 1538 | LIBPLDM_ABI_TESTING |
| 1539 | int pldm_entity_association_pdr_create_new(pldm_pdr *repo, |
| 1540 | uint32_t pdr_record_handle, |
| 1541 | pldm_entity *parent, |
| 1542 | pldm_entity *entity, |
| 1543 | uint32_t *entity_record_handle) |
| 1544 | { |
| 1545 | if (!repo || !parent || !entity || !entity_record_handle) { |
| 1546 | return -EINVAL; |
| 1547 | } |
| 1548 | |
| 1549 | if (pdr_record_handle == UINT32_MAX) { |
| 1550 | return -EOVERFLOW; |
| 1551 | } |
| 1552 | |
| 1553 | bool pdr_added = false; |
| 1554 | uint16_t new_pdr_size; |
| 1555 | uint16_t container_id = 0; |
| 1556 | uint8_t *container_id_addr = NULL; |
| 1557 | struct pldm_msgbuf _dst; |
| 1558 | struct pldm_msgbuf *dst = &_dst; |
| 1559 | struct pldm_msgbuf _src_p; |
| 1560 | struct pldm_msgbuf *src_p = &_src_p; |
| 1561 | struct pldm_msgbuf _src_c; |
| 1562 | struct pldm_msgbuf *src_c = &_src_c; |
| 1563 | int rc = 0; |
| 1564 | |
| 1565 | pldm_pdr_record *prev = repo->first; |
| 1566 | pldm_pdr_record *record = repo->first; |
| 1567 | pdr_added = pldm_pdr_find_record_by_handle(&record, &prev, |
| 1568 | pdr_record_handle); |
| 1569 | if (!pdr_added) { |
| 1570 | return -ENOENT; |
| 1571 | } |
| 1572 | |
| 1573 | static_assert(PDR_ENTITY_ASSOCIATION_MIN_SIZE < UINT16_MAX, |
| 1574 | "Truncation ahead"); |
| 1575 | new_pdr_size = PDR_ENTITY_ASSOCIATION_MIN_SIZE; |
| 1576 | pldm_pdr_record *new_record = malloc(sizeof(pldm_pdr_record)); |
| 1577 | if (!new_record) { |
| 1578 | return -ENOMEM; |
| 1579 | } |
| 1580 | |
| 1581 | new_record->data = malloc(new_pdr_size); |
| 1582 | if (!new_record->data) { |
| 1583 | rc = -ENOMEM; |
| 1584 | goto cleanup_new_record; |
| 1585 | } |
| 1586 | |
| 1587 | // Initialise new PDR to be added with the header, size and handle. |
| 1588 | // Set the position of new PDR |
| 1589 | *entity_record_handle = pdr_record_handle + 1; |
| 1590 | new_record->record_handle = *entity_record_handle; |
| 1591 | new_record->size = new_pdr_size; |
| 1592 | new_record->is_remote = false; |
| 1593 | |
| 1594 | rc = pldm_msgbuf_init_errno(dst, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1595 | new_record->data, new_record->size); |
| 1596 | if (rc) { |
| 1597 | goto cleanup_new_record_data; |
| 1598 | } |
| 1599 | |
| 1600 | // header record handle |
| 1601 | pldm_msgbuf_insert(dst, *entity_record_handle); |
| 1602 | // header version |
| 1603 | pldm_msgbuf_insert_uint8(dst, 1); |
| 1604 | // header type |
| 1605 | pldm_msgbuf_insert_uint8(dst, PLDM_PDR_ENTITY_ASSOCIATION); |
| 1606 | // header change number |
| 1607 | pldm_msgbuf_insert_uint16(dst, 0); |
| 1608 | // header length |
| 1609 | pldm_msgbuf_insert_uint16(dst, |
| 1610 | (new_pdr_size - sizeof(struct pldm_pdr_hdr))); |
| 1611 | |
| 1612 | // Data for new PDR is obtained from parent PDR and new contained entity |
| 1613 | // is added as the child |
| 1614 | rc = pldm_msgbuf_init_errno(src_p, sizeof(struct pldm_entity), parent, |
| 1615 | sizeof(*parent)); |
| 1616 | if (rc) { |
| 1617 | goto cleanup_new_record_data; |
| 1618 | } |
| 1619 | |
| 1620 | rc = pldm_msgbuf_init_errno(src_c, sizeof(struct pldm_entity), entity, |
| 1621 | sizeof(*entity)); |
| 1622 | if (rc) { |
| 1623 | goto cleanup_new_record_data; |
| 1624 | } |
| 1625 | |
| 1626 | // extract pointer for container ID and save the address |
| 1627 | rc = pldm_msgbuf_span_required(dst, sizeof(container_id), |
| 1628 | (void **)&container_id_addr); |
| 1629 | if (rc) { |
| 1630 | goto cleanup_new_record_data; |
| 1631 | } |
| 1632 | pldm_msgbuf_insert_uint8(dst, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
| 1633 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_type); |
| 1634 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_instance_num); |
| 1635 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_container_id); |
| 1636 | // number of children |
| 1637 | pldm_msgbuf_insert_uint8(dst, 1); |
| 1638 | |
| 1639 | // Add new entity as child |
| 1640 | pldm_msgbuf_copy(dst, src_c, uint16_t, child_entity_type); |
| 1641 | pldm_msgbuf_copy(dst, src_c, uint16_t, child_entity_instance_num); |
| 1642 | // Extract and insert child entity container ID and add same value to |
| 1643 | // container ID of entity |
| 1644 | pldm_msgbuf_extract(src_c, container_id); |
| 1645 | pldm_msgbuf_insert(dst, container_id); |
| 1646 | container_id = htole16(container_id); |
| 1647 | memcpy(container_id_addr, &container_id, sizeof(uint16_t)); |
| 1648 | |
| 1649 | rc = pldm_msgbuf_destroy(dst); |
| 1650 | if (rc) { |
| 1651 | goto cleanup_new_record_data; |
| 1652 | } |
| 1653 | rc = pldm_msgbuf_destroy(src_p); |
| 1654 | if (rc) { |
| 1655 | goto cleanup_new_record_data; |
| 1656 | } |
| 1657 | rc = pldm_msgbuf_destroy(src_c); |
| 1658 | if (rc) { |
| 1659 | goto cleanup_new_record_data; |
| 1660 | } |
| 1661 | |
| 1662 | rc = pldm_pdr_insert_record(repo, record, new_record); |
| 1663 | if (rc) { |
| 1664 | goto cleanup_new_record_data; |
| 1665 | } |
| 1666 | |
| 1667 | return rc; |
| 1668 | cleanup_new_record_data: |
| 1669 | free(new_record->data); |
| 1670 | cleanup_new_record: |
| 1671 | free(new_record); |
| 1672 | return rc; |
| 1673 | } |