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