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 */ |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 2 | #include "compiler.h" |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 3 | #include "msgbuf.h" |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 4 | #include <libpldm/pdr.h> |
| 5 | #include <libpldm/platform.h> |
| 6 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 7 | #include <assert.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 8 | #include <endian.h> |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 9 | #include <stdint.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 12 | #include <errno.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 13 | |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 14 | #define PDR_ENTITY_ASSOCIATION_MIN_SIZE \ |
| 15 | (sizeof(struct pldm_pdr_hdr) + \ |
| 16 | sizeof(struct pldm_pdr_entity_association)) |
| 17 | |
Varsha Kaverappa | 8b53ad9 | 2024-06-15 06:36:06 -0500 | [diff] [blame] | 18 | #define PDR_FRU_RECORD_SET_MIN_SIZE \ |
| 19 | (sizeof(struct pldm_pdr_hdr) + sizeof(struct pldm_pdr_fru_record_set)) |
| 20 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 21 | typedef struct pldm_pdr_record { |
| 22 | uint32_t record_handle; |
| 23 | uint32_t size; |
| 24 | uint8_t *data; |
| 25 | struct pldm_pdr_record *next; |
| 26 | bool is_remote; |
| 27 | uint16_t terminus_handle; |
| 28 | } pldm_pdr_record; |
| 29 | |
| 30 | typedef struct pldm_pdr { |
| 31 | uint32_t record_count; |
| 32 | uint32_t size; |
| 33 | pldm_pdr_record *first; |
| 34 | pldm_pdr_record *last; |
| 35 | } pldm_pdr; |
| 36 | |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 37 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 38 | static inline uint32_t get_next_record_handle(const pldm_pdr *repo, |
| 39 | const pldm_pdr_record *record) |
| 40 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 41 | if (record == repo->last) { |
| 42 | return 0; |
| 43 | } |
| 44 | return record->next->record_handle; |
| 45 | } |
| 46 | |
Andrew Jeffery | ca248ce | 2023-07-07 10:38:30 +0930 | [diff] [blame] | 47 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | fae3641 | 2024-06-20 06:35:51 +0000 | [diff] [blame] | 48 | int pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
| 49 | bool is_remote, uint16_t terminus_handle, |
| 50 | uint32_t *record_handle) |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 51 | { |
Varsha Kaverappa | 8b53ad9 | 2024-06-15 06:36:06 -0500 | [diff] [blame] | 52 | uint32_t curr = 0; |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 53 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 54 | if (!repo || !data || !size) { |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 55 | return -EINVAL; |
| 56 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 57 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 58 | if (record_handle && *record_handle) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 59 | curr = *record_handle; |
| 60 | } else if (repo->last) { |
| 61 | curr = repo->last->record_handle; |
| 62 | if (curr == UINT32_MAX) { |
| 63 | return -EOVERFLOW; |
| 64 | } |
| 65 | curr += 1; |
| 66 | } else { |
| 67 | curr = 1; |
| 68 | } |
| 69 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 70 | pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record)); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 71 | if (!record) { |
| 72 | return -ENOMEM; |
| 73 | } |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 74 | |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 75 | if (data) { |
| 76 | record->data = malloc(size); |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 77 | if (!record->data) { |
| 78 | free(record); |
| 79 | return -ENOMEM; |
| 80 | } |
| 81 | memcpy(record->data, data, size); |
Andrew Jeffery | 2ca7e64 | 2023-06-28 22:29:07 +0930 | [diff] [blame] | 82 | } |
| 83 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 84 | record->size = size; |
| 85 | record->is_remote = is_remote; |
| 86 | record->terminus_handle = terminus_handle; |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 87 | record->record_handle = curr; |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 88 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 89 | if (record_handle && !*record_handle && data) { |
Andrew Jeffery | c6cc028 | 2023-07-17 12:11:37 +0930 | [diff] [blame] | 90 | /* If record handle is 0, that is an indication for this API to |
| 91 | * compute a new handle. For that reason, the computed handle |
| 92 | * needs to be populated in the PDR header. For a case where the |
| 93 | * caller supplied the record handle, it would exist in the |
| 94 | * header already. |
| 95 | */ |
| 96 | struct pldm_pdr_hdr *hdr = (void *)record->data; |
| 97 | hdr->record_handle = htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 98 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 99 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 100 | record->next = NULL; |
| 101 | |
Andrew Jeffery | 8d231da | 2023-07-04 11:28:46 +0930 | [diff] [blame] | 102 | assert(!repo->first == !repo->last); |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 103 | if (repo->first == NULL) { |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 104 | repo->first = record; |
| 105 | repo->last = record; |
| 106 | } else { |
| 107 | repo->last->next = record; |
| 108 | repo->last = record; |
| 109 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 110 | |
Andrew Jeffery | a51ccc2 | 2023-06-28 21:57:46 +0930 | [diff] [blame] | 111 | repo->size += record->size; |
| 112 | ++repo->record_count; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 113 | |
Andrew Jeffery | 3b93d09 | 2023-07-17 13:01:50 +0930 | [diff] [blame] | 114 | if (record_handle) { |
| 115 | *record_handle = record->record_handle; |
| 116 | } |
Andrew Jeffery | 572a395 | 2023-07-03 13:19:28 +0930 | [diff] [blame] | 117 | |
| 118 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 119 | } |
| 120 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 121 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 122 | pldm_pdr *pldm_pdr_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 123 | { |
| 124 | pldm_pdr *repo = malloc(sizeof(pldm_pdr)); |
Andrew Jeffery | a8bb22e | 2023-06-30 12:01:12 +0930 | [diff] [blame] | 125 | if (!repo) { |
| 126 | return NULL; |
| 127 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 128 | repo->record_count = 0; |
| 129 | repo->size = 0; |
| 130 | repo->first = NULL; |
| 131 | repo->last = NULL; |
| 132 | |
| 133 | return repo; |
| 134 | } |
| 135 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 136 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 137 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 138 | { |
Andrew Jeffery | fca1b60 | 2023-06-30 12:29:25 +0930 | [diff] [blame] | 139 | if (!repo) { |
| 140 | return; |
| 141 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 142 | |
| 143 | pldm_pdr_record *record = repo->first; |
| 144 | while (record != NULL) { |
| 145 | pldm_pdr_record *next = record->next; |
| 146 | if (record->data) { |
| 147 | free(record->data); |
| 148 | record->data = NULL; |
| 149 | } |
| 150 | free(record); |
| 151 | record = next; |
| 152 | } |
| 153 | free(repo); |
| 154 | } |
| 155 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 156 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 157 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 158 | uint32_t record_handle, |
| 159 | uint8_t **data, uint32_t *size, |
| 160 | uint32_t *next_record_handle) |
| 161 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 162 | if (!repo || !data || !size || !next_record_handle) { |
| 163 | return NULL; |
| 164 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 165 | |
| 166 | if (!record_handle && (repo->first != NULL)) { |
| 167 | record_handle = repo->first->record_handle; |
| 168 | } |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 169 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 170 | pldm_pdr_record *record = repo->first; |
| 171 | while (record != NULL) { |
| 172 | if (record->record_handle == record_handle) { |
| 173 | *size = record->size; |
| 174 | *data = record->data; |
| 175 | *next_record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 176 | get_next_record_handle(repo, record); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 177 | return record; |
| 178 | } |
| 179 | record = record->next; |
| 180 | } |
| 181 | |
| 182 | *size = 0; |
| 183 | *next_record_handle = 0; |
| 184 | return NULL; |
| 185 | } |
| 186 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 187 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 188 | const pldm_pdr_record * |
| 189 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 190 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 191 | uint32_t *size, uint32_t *next_record_handle) |
| 192 | { |
Andrew Jeffery | 68b5130 | 2023-06-28 21:29:42 +0930 | [diff] [blame] | 193 | if (!repo || !curr_record || !data || !size || !next_record_handle) { |
| 194 | return NULL; |
| 195 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 196 | |
| 197 | if (curr_record == repo->last) { |
| 198 | *data = NULL; |
| 199 | *size = 0; |
| 200 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 205 | *data = curr_record->next->data; |
| 206 | *size = curr_record->next->size; |
| 207 | return curr_record->next; |
| 208 | } |
| 209 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 210 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 211 | const pldm_pdr_record * |
| 212 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 213 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 214 | uint32_t *size) |
| 215 | { |
Andrew Jeffery | f85eeba | 2023-06-30 12:38:40 +0930 | [diff] [blame] | 216 | if (!repo) { |
| 217 | return NULL; |
| 218 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 219 | |
| 220 | pldm_pdr_record *record = repo->first; |
| 221 | if (curr_record != NULL) { |
| 222 | record = curr_record->next; |
| 223 | } |
| 224 | while (record != NULL) { |
| 225 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 226 | if (hdr->type == pdr_type) { |
| 227 | if (data && size) { |
| 228 | *size = record->size; |
| 229 | *data = record->data; |
| 230 | } |
| 231 | return record; |
| 232 | } |
| 233 | record = record->next; |
| 234 | } |
| 235 | |
| 236 | if (size) { |
| 237 | *size = 0; |
| 238 | } |
| 239 | return NULL; |
| 240 | } |
| 241 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 242 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 243 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 244 | { |
| 245 | assert(repo != NULL); |
| 246 | |
| 247 | return repo->record_count; |
| 248 | } |
| 249 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 250 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 251 | uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo) |
| 252 | { |
| 253 | assert(repo != NULL); |
| 254 | |
| 255 | return repo->size; |
| 256 | } |
| 257 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 258 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 259 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo LIBPLDM_CC_UNUSED, |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 260 | const pldm_pdr_record *record) |
| 261 | { |
| 262 | assert(repo != NULL); |
| 263 | assert(record != NULL); |
| 264 | |
| 265 | return record->record_handle; |
| 266 | } |
| 267 | |
Pavithra Barithaya | 274732f | 2024-07-05 16:03:01 +0530 | [diff] [blame] | 268 | LIBPLDM_ABI_TESTING |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 269 | uint16_t pldm_pdr_get_terminus_handle(const pldm_pdr *repo LIBPLDM_CC_UNUSED, |
Pavithra Barithaya | 274732f | 2024-07-05 16:03:01 +0530 | [diff] [blame] | 270 | const pldm_pdr_record *record) |
| 271 | { |
| 272 | assert(repo != NULL); |
| 273 | assert(record != NULL); |
| 274 | |
| 275 | return record->terminus_handle; |
| 276 | } |
| 277 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 278 | LIBPLDM_ABI_STABLE |
| 279 | bool pldm_pdr_record_is_remote(const pldm_pdr_record *record) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 280 | { |
| 281 | assert(record != NULL); |
| 282 | |
| 283 | return record->is_remote; |
| 284 | } |
| 285 | |
Andrew Jeffery | a2c6911 | 2023-07-07 10:41:38 +0930 | [diff] [blame] | 286 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | e7f5511 | 2024-06-20 16:16:01 +0930 | [diff] [blame] | 287 | int pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle, |
| 288 | uint16_t fru_rsi, uint16_t entity_type, |
| 289 | uint16_t entity_instance_num, |
| 290 | uint16_t container_id, |
| 291 | uint32_t *bmc_record_handle) |
Andrew Jeffery | c821a70 | 2023-07-03 13:32:11 +0930 | [diff] [blame] | 292 | { |
Andrew Jeffery | 73d6779 | 2023-07-17 15:22:18 +0930 | [diff] [blame] | 293 | if (!repo || !bmc_record_handle) { |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
Andrew Jeffery | 5c49f16 | 2024-09-22 15:45:35 +0930 | [diff] [blame] | 297 | uint8_t data[sizeof(struct pldm_pdr_hdr) + |
| 298 | sizeof(struct pldm_pdr_fru_record_set)]; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 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 | 5c49f16 | 2024-09-22 15:45:35 +0930 | [diff] [blame] | 315 | return pldm_pdr_add(repo, data, sizeof(data), false, terminus_handle, |
Andrew Jeffery | fae3641 | 2024-06-20 06:35:51 +0000 | [diff] [blame] | 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 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 465 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 466 | pldm_entity pldm_entity_extract(pldm_entity_node *node) |
| 467 | { |
| 468 | assert(node != NULL); |
| 469 | |
| 470 | return node->entity; |
| 471 | } |
| 472 | |
Pavithra Barithaya | def8e02 | 2023-08-16 00:31:06 -0500 | [diff] [blame] | 473 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 474 | uint16_t |
| 475 | pldm_entity_node_get_remote_container_id(const pldm_entity_node *entity) |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 476 | { |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 477 | assert(entity != NULL); |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 478 | |
Andrew Jeffery | 15b8818 | 2023-06-30 13:29:17 +0930 | [diff] [blame] | 479 | return entity->remote_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 480 | } |
| 481 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 482 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 319304f | 2023-04-05 13:53:18 +0930 | [diff] [blame] | 483 | pldm_entity_association_tree *pldm_entity_association_tree_init(void) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 484 | { |
| 485 | pldm_entity_association_tree *tree = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 486 | malloc(sizeof(pldm_entity_association_tree)); |
Andrew Jeffery | c40037d | 2023-06-30 13:50:12 +0930 | [diff] [blame] | 487 | if (!tree) { |
| 488 | return NULL; |
| 489 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 490 | tree->root = NULL; |
| 491 | tree->last_used_container_id = 0; |
| 492 | |
| 493 | return tree; |
| 494 | } |
| 495 | |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 496 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 497 | static pldm_entity_node *find_insertion_at(pldm_entity_node *start, |
| 498 | uint16_t entity_type) |
| 499 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 500 | /* 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] | 501 | * at the end if no such match occurs |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 502 | */ |
| 503 | while (start->next_sibling != NULL) { |
| 504 | uint16_t this_type = start->entity.entity_type; |
| 505 | pldm_entity_node *next = start->next_sibling; |
| 506 | if (this_type == entity_type && |
| 507 | (this_type != next->entity.entity_type)) { |
| 508 | break; |
| 509 | } |
| 510 | start = start->next_sibling; |
| 511 | } |
| 512 | |
| 513 | return start; |
| 514 | } |
| 515 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 516 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 517 | pldm_entity_node *pldm_entity_association_tree_add( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 518 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 519 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 520 | uint8_t association_type) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 521 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 522 | return pldm_entity_association_tree_add_entity(tree, entity, |
| 523 | entity_instance_number, |
| 524 | parent, association_type, |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 525 | false, true, 0xffff); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Pavithra Barithaya | 1ade87f | 2023-07-28 04:03:01 -0500 | [diff] [blame] | 528 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 529 | pldm_entity_node *pldm_entity_association_tree_add_entity( |
| 530 | pldm_entity_association_tree *tree, pldm_entity *entity, |
| 531 | uint16_t entity_instance_number, pldm_entity_node *parent, |
| 532 | uint8_t association_type, bool is_remote, bool is_update_container_id, |
| 533 | uint16_t container_id) |
| 534 | { |
| 535 | if ((!tree) || (!entity)) { |
| 536 | return NULL; |
| 537 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 538 | |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 539 | if (entity_instance_number != 0xffff && parent != NULL) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 540 | pldm_entity node; |
| 541 | node.entity_type = entity->entity_type; |
| 542 | node.entity_instance_num = entity_instance_number; |
| 543 | if (pldm_is_current_parent_child(parent, &node)) { |
| 544 | return NULL; |
| 545 | } |
| 546 | } |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 547 | if (association_type != PLDM_ENTITY_ASSOCIAION_PHYSICAL && |
| 548 | association_type != PLDM_ENTITY_ASSOCIAION_LOGICAL) { |
| 549 | return NULL; |
| 550 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 551 | pldm_entity_node *node = malloc(sizeof(pldm_entity_node)); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 552 | if (!node) { |
| 553 | return NULL; |
| 554 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 555 | node->first_child = NULL; |
| 556 | node->next_sibling = NULL; |
| 557 | node->parent.entity_type = 0; |
| 558 | node->parent.entity_instance_num = 0; |
| 559 | node->parent.entity_container_id = 0; |
| 560 | node->entity.entity_type = entity->entity_type; |
| 561 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 562 | entity_instance_number != 0xffff ? entity_instance_number : 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 563 | node->association_type = association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 564 | node->remote_container_id = 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 565 | if (tree->root == NULL) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 566 | if (parent != NULL) { |
| 567 | free(node); |
| 568 | return NULL; |
| 569 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 570 | tree->root = node; |
| 571 | /* container_id 0 here indicates this is the top-most entry */ |
| 572 | node->entity.entity_container_id = 0; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 573 | node->remote_container_id = node->entity.entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 574 | } else if (parent != NULL && parent->first_child == NULL) { |
Andrew Jeffery | 7062382 | 2023-08-10 16:05:27 +0930 | [diff] [blame] | 575 | /* Ensure next_container_id() will yield a valid ID */ |
| 576 | if (tree->last_used_container_id == UINT16_MAX) { |
| 577 | free(node); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 581 | parent->first_child = node; |
| 582 | node->parent = parent->entity; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 583 | |
| 584 | if (is_remote) { |
| 585 | node->remote_container_id = entity->entity_container_id; |
| 586 | } |
| 587 | if (is_update_container_id) { |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 588 | if (container_id != 0xffff) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 589 | node->entity.entity_container_id = container_id; |
| 590 | } else { |
Andrew Jeffery | 0550777 | 2024-09-22 20:55:55 +0930 | [diff] [blame] | 591 | /* We will have returned above */ |
| 592 | assert(tree->last_used_container_id != |
| 593 | UINT16_MAX); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 594 | node->entity.entity_container_id = |
Andrew Jeffery | 0550777 | 2024-09-22 20:55:55 +0930 | [diff] [blame] | 595 | ++tree->last_used_container_id; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 596 | } |
| 597 | } else { |
| 598 | node->entity.entity_container_id = |
| 599 | entity->entity_container_id; |
| 600 | } |
| 601 | |
| 602 | if (!is_remote) { |
| 603 | node->remote_container_id = |
| 604 | node->entity.entity_container_id; |
| 605 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 606 | } else { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 607 | pldm_entity_node *start = parent == NULL ? tree->root : |
| 608 | parent->first_child; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 609 | pldm_entity_node *prev = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 610 | find_insertion_at(start, entity->entity_type); |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 611 | if (!prev) { |
| 612 | free(node); |
| 613 | return NULL; |
| 614 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 615 | pldm_entity_node *next = prev->next_sibling; |
| 616 | if (prev->entity.entity_type == entity->entity_type) { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 617 | if (prev->entity.entity_instance_num == UINT16_MAX) { |
| 618 | free(node); |
| 619 | return NULL; |
| 620 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 621 | node->entity.entity_instance_num = |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 622 | entity_instance_number != 0xffff ? |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 623 | entity_instance_number : |
| 624 | prev->entity.entity_instance_num + 1; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 625 | } |
| 626 | prev->next_sibling = node; |
| 627 | node->parent = prev->parent; |
| 628 | node->next_sibling = next; |
| 629 | node->entity.entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 630 | prev->entity.entity_container_id; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 631 | node->remote_container_id = entity->entity_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 632 | } |
| 633 | entity->entity_instance_num = node->entity.entity_instance_num; |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 634 | if (is_update_container_id) { |
| 635 | entity->entity_container_id = node->entity.entity_container_id; |
| 636 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 637 | return node; |
| 638 | } |
| 639 | |
| 640 | static void get_num_nodes(pldm_entity_node *node, size_t *num) |
| 641 | { |
| 642 | if (node == NULL) { |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | ++(*num); |
| 647 | get_num_nodes(node->next_sibling, num); |
| 648 | get_num_nodes(node->first_child, num); |
| 649 | } |
| 650 | |
| 651 | static void entity_association_tree_visit(pldm_entity_node *node, |
| 652 | pldm_entity *entities, size_t *index) |
| 653 | { |
| 654 | if (node == NULL) { |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | pldm_entity *entity = &entities[*index]; |
| 659 | ++(*index); |
| 660 | entity->entity_type = node->entity.entity_type; |
| 661 | entity->entity_instance_num = node->entity.entity_instance_num; |
| 662 | entity->entity_container_id = node->entity.entity_container_id; |
| 663 | |
| 664 | entity_association_tree_visit(node->next_sibling, entities, index); |
| 665 | entity_association_tree_visit(node->first_child, entities, index); |
| 666 | } |
| 667 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 668 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 669 | void pldm_entity_association_tree_visit(pldm_entity_association_tree *tree, |
| 670 | pldm_entity **entities, size_t *size) |
| 671 | { |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 672 | if (!tree || !entities || !size) { |
| 673 | return; |
| 674 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 675 | |
| 676 | *size = 0; |
| 677 | if (tree->root == NULL) { |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | get_num_nodes(tree->root, size); |
| 682 | *entities = malloc(*size * sizeof(pldm_entity)); |
Andrew Jeffery | cd17e5c | 2023-06-30 14:06:18 +0930 | [diff] [blame] | 683 | if (!entities) { |
| 684 | return; |
| 685 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 686 | size_t index = 0; |
| 687 | entity_association_tree_visit(tree->root, *entities, &index); |
| 688 | } |
| 689 | |
| 690 | static void entity_association_tree_destroy(pldm_entity_node *node) |
| 691 | { |
| 692 | if (node == NULL) { |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | entity_association_tree_destroy(node->next_sibling); |
| 697 | entity_association_tree_destroy(node->first_child); |
| 698 | free(node); |
| 699 | } |
| 700 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 701 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 702 | void pldm_entity_association_tree_destroy(pldm_entity_association_tree *tree) |
| 703 | { |
Andrew Jeffery | a89e015 | 2023-06-30 14:24:05 +0930 | [diff] [blame] | 704 | if (!tree) { |
| 705 | return; |
| 706 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 707 | |
| 708 | entity_association_tree_destroy(tree->root); |
| 709 | free(tree); |
| 710 | } |
| 711 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 712 | LIBPLDM_ABI_STABLE |
| 713 | bool pldm_entity_is_node_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 714 | { |
| 715 | assert(node != NULL); |
| 716 | |
| 717 | return node->first_child != NULL; |
| 718 | } |
| 719 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 720 | LIBPLDM_ABI_STABLE |
| 721 | pldm_entity pldm_entity_get_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 722 | { |
| 723 | assert(node != NULL); |
| 724 | |
| 725 | return node->parent; |
| 726 | } |
| 727 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 728 | LIBPLDM_ABI_STABLE |
| 729 | bool pldm_entity_is_exist_parent(pldm_entity_node *node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 730 | { |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 731 | if (!node) { |
| 732 | return false; |
| 733 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 734 | |
| 735 | if (node->parent.entity_type == 0 && |
| 736 | node->parent.entity_instance_num == 0 && |
| 737 | node->parent.entity_container_id == 0) { |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | return true; |
| 742 | } |
| 743 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 744 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 745 | uint8_t pldm_entity_get_num_children(pldm_entity_node *node, |
| 746 | uint8_t association_type) |
| 747 | { |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 748 | if (!node) { |
| 749 | return 0; |
| 750 | } |
| 751 | |
Andrew Jeffery | 6e8a261 | 2023-06-30 15:36:48 +0930 | [diff] [blame] | 752 | if (!(association_type == PLDM_ENTITY_ASSOCIAION_PHYSICAL || |
| 753 | association_type == PLDM_ENTITY_ASSOCIAION_LOGICAL)) { |
| 754 | return 0; |
| 755 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 756 | |
| 757 | size_t count = 0; |
| 758 | pldm_entity_node *curr = node->first_child; |
| 759 | while (curr != NULL) { |
| 760 | if (curr->association_type == association_type) { |
| 761 | ++count; |
| 762 | } |
| 763 | curr = curr->next_sibling; |
| 764 | } |
| 765 | |
| 766 | assert(count < UINT8_MAX); |
Andrew Jeffery | c83ef86 | 2023-07-11 17:16:55 +0930 | [diff] [blame] | 767 | return count < UINT8_MAX ? count : 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 768 | } |
| 769 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 770 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 771 | bool pldm_is_current_parent_child(pldm_entity_node *parent, pldm_entity *node) |
| 772 | { |
Andrew Jeffery | 375d9fc | 2023-06-30 15:45:54 +0930 | [diff] [blame] | 773 | if (!parent || !node) { |
| 774 | return false; |
| 775 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 776 | |
| 777 | pldm_entity_node *curr = parent->first_child; |
| 778 | while (curr != NULL) { |
| 779 | if (node->entity_type == curr->entity.entity_type && |
| 780 | node->entity_instance_num == |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 781 | curr->entity.entity_instance_num) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 782 | return true; |
| 783 | } |
| 784 | curr = curr->next_sibling; |
| 785 | } |
| 786 | |
| 787 | return false; |
| 788 | } |
| 789 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 790 | static int entity_association_pdr_add_children( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 791 | pldm_entity_node *curr, pldm_pdr *repo, uint16_t size, |
| 792 | uint8_t contained_count, uint8_t association_type, bool is_remote, |
| 793 | uint16_t terminus_handle, uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 794 | { |
Andrew Jeffery | 5c49f16 | 2024-09-22 15:45:35 +0930 | [diff] [blame] | 795 | uint8_t *start; |
| 796 | uint8_t *pdr; |
| 797 | int rc; |
| 798 | |
| 799 | pdr = calloc(1, size); |
| 800 | if (!pdr) { |
| 801 | return -ENOMEM; |
| 802 | } |
| 803 | |
| 804 | start = pdr; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 805 | |
| 806 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)start; |
| 807 | hdr->version = 1; |
Pavithra Barithaya | 39eb322 | 2023-11-17 03:30:27 -0600 | [diff] [blame] | 808 | hdr->record_handle = record_handle; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 809 | hdr->type = PLDM_PDR_ENTITY_ASSOCIATION; |
| 810 | hdr->record_change_num = 0; |
| 811 | hdr->length = htole16(size - sizeof(struct pldm_pdr_hdr)); |
| 812 | start += sizeof(struct pldm_pdr_hdr); |
| 813 | |
| 814 | uint16_t *container_id = (uint16_t *)start; |
| 815 | *container_id = htole16(curr->first_child->entity.entity_container_id); |
| 816 | start += sizeof(uint16_t); |
| 817 | *start = association_type; |
| 818 | start += sizeof(uint8_t); |
| 819 | |
| 820 | pldm_entity *entity = (pldm_entity *)start; |
| 821 | entity->entity_type = htole16(curr->entity.entity_type); |
| 822 | entity->entity_instance_num = htole16(curr->entity.entity_instance_num); |
| 823 | entity->entity_container_id = htole16(curr->entity.entity_container_id); |
| 824 | start += sizeof(pldm_entity); |
| 825 | |
| 826 | *start = contained_count; |
| 827 | start += sizeof(uint8_t); |
| 828 | |
| 829 | pldm_entity_node *node = curr->first_child; |
| 830 | while (node != NULL) { |
| 831 | if (node->association_type == association_type) { |
| 832 | pldm_entity *entity = (pldm_entity *)start; |
| 833 | entity->entity_type = htole16(node->entity.entity_type); |
| 834 | entity->entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 835 | htole16(node->entity.entity_instance_num); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 836 | entity->entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 837 | htole16(node->entity.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 838 | start += sizeof(pldm_entity); |
| 839 | } |
| 840 | node = node->next_sibling; |
| 841 | } |
| 842 | |
Andrew Jeffery | 5c49f16 | 2024-09-22 15:45:35 +0930 | [diff] [blame] | 843 | rc = pldm_pdr_add(repo, pdr, size, is_remote, terminus_handle, |
| 844 | &record_handle); |
| 845 | free(pdr); |
| 846 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 847 | } |
| 848 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 849 | static int entity_association_pdr_add_entry(pldm_entity_node *curr, |
| 850 | pldm_pdr *repo, bool is_remote, |
| 851 | uint16_t terminus_handle, |
| 852 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 853 | { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 854 | uint8_t num_logical_children = pldm_entity_get_num_children( |
| 855 | curr, PLDM_ENTITY_ASSOCIAION_LOGICAL); |
| 856 | uint8_t num_physical_children = pldm_entity_get_num_children( |
| 857 | curr, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 858 | int rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 859 | |
| 860 | if (num_logical_children) { |
| 861 | uint16_t logical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 862 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 863 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 864 | sizeof(uint8_t) + |
| 865 | (num_logical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 866 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 867 | curr, repo, logical_pdr_size, num_logical_children, |
| 868 | PLDM_ENTITY_ASSOCIAION_LOGICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 869 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 870 | if (rc < 0) { |
| 871 | return rc; |
| 872 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | if (num_physical_children) { |
| 876 | uint16_t physical_pdr_size = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 877 | sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t) + |
| 878 | sizeof(uint8_t) + sizeof(pldm_entity) + |
| 879 | sizeof(uint8_t) + |
| 880 | (num_physical_children * sizeof(pldm_entity)); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 881 | rc = entity_association_pdr_add_children( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 882 | curr, repo, physical_pdr_size, num_physical_children, |
| 883 | PLDM_ENTITY_ASSOCIAION_PHYSICAL, is_remote, |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 884 | terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 885 | if (rc < 0) { |
| 886 | return rc; |
| 887 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 888 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 889 | |
| 890 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 891 | } |
| 892 | |
Andrew Jeffery | d09b1af | 2023-07-17 12:39:16 +0930 | [diff] [blame] | 893 | static bool is_present(pldm_entity entity, pldm_entity **entities, |
| 894 | size_t num_entities) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 895 | { |
| 896 | if (entities == NULL || num_entities == 0) { |
| 897 | return true; |
| 898 | } |
| 899 | size_t i = 0; |
| 900 | while (i < num_entities) { |
| 901 | if ((*entities + i)->entity_type == entity.entity_type) { |
| 902 | return true; |
| 903 | } |
| 904 | i++; |
| 905 | } |
| 906 | return false; |
| 907 | } |
| 908 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 909 | static int entity_association_pdr_add(pldm_entity_node *curr, pldm_pdr *repo, |
| 910 | pldm_entity **entities, |
| 911 | size_t num_entities, bool is_remote, |
| 912 | uint16_t terminus_handle, |
| 913 | uint32_t record_handle) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 914 | { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 915 | int rc; |
| 916 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 917 | if (curr == NULL) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 918 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 919 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 920 | |
| 921 | if (is_present(curr->entity, entities, num_entities)) { |
| 922 | rc = entity_association_pdr_add_entry( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 923 | curr, repo, is_remote, terminus_handle, record_handle); |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 924 | if (rc) { |
| 925 | return rc; |
| 926 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 927 | } |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 928 | |
| 929 | rc = entity_association_pdr_add(curr->next_sibling, repo, entities, |
| 930 | num_entities, is_remote, |
| 931 | terminus_handle, record_handle); |
| 932 | if (rc) { |
| 933 | return rc; |
| 934 | } |
| 935 | |
| 936 | return entity_association_pdr_add(curr->first_child, repo, entities, |
| 937 | num_entities, is_remote, |
| 938 | terminus_handle, record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 939 | } |
| 940 | |
Andrew Jeffery | 096685b | 2023-07-17 17:36:14 +0930 | [diff] [blame] | 941 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | d72ea4b | 2024-06-24 21:33:33 +0930 | [diff] [blame] | 942 | int pldm_entity_association_pdr_add(pldm_entity_association_tree *tree, |
| 943 | pldm_pdr *repo, bool is_remote, |
| 944 | uint16_t terminus_handle) |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 945 | { |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 946 | if (!tree || !repo) { |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 947 | return 0; |
Andrew Jeffery | c788348 | 2023-06-30 15:52:04 +0930 | [diff] [blame] | 948 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 949 | |
Andrew Jeffery | 6594599 | 2023-07-17 15:04:21 +0930 | [diff] [blame] | 950 | return entity_association_pdr_add(tree->root, repo, NULL, 0, is_remote, |
| 951 | terminus_handle, 0); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 952 | } |
| 953 | |
Andrew Jeffery | 1354a6e | 2023-07-07 10:34:38 +0930 | [diff] [blame] | 954 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | aa49b71 | 2024-06-24 21:43:52 +0930 | [diff] [blame] | 955 | int pldm_entity_association_pdr_add_from_node( |
Andrew Jeffery | cc39452 | 2023-07-03 12:49:31 +0930 | [diff] [blame] | 956 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 957 | size_t num_entities, bool is_remote, uint16_t terminus_handle) |
| 958 | { |
| 959 | return pldm_entity_association_pdr_add_from_node_with_record_handle( |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 960 | node, repo, entities, num_entities, is_remote, terminus_handle, |
| 961 | 0); |
| 962 | } |
| 963 | |
Pavithra Barithaya | 3a26705 | 2023-11-13 05:01:36 -0600 | [diff] [blame] | 964 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 25ddbcc | 2023-05-19 08:28:59 -0500 | [diff] [blame] | 965 | int pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 966 | pldm_entity_node *node, pldm_pdr *repo, pldm_entity **entities, |
| 967 | size_t num_entities, bool is_remote, uint16_t terminus_handle, |
| 968 | uint32_t record_handle) |
| 969 | { |
| 970 | if (!node || !repo || !entities) { |
| 971 | return -EINVAL; |
| 972 | } |
| 973 | |
Archana Kakani | 74c9a54 | 2024-09-13 00:33:57 -0500 | [diff] [blame] | 974 | return entity_association_pdr_add(node, repo, entities, num_entities, |
| 975 | is_remote, terminus_handle, |
| 976 | record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 977 | } |
| 978 | |
Andrew Jeffery | 643c443 | 2023-07-17 15:36:03 +0930 | [diff] [blame] | 979 | static void find_entity_ref_in_tree(pldm_entity_node *tree_node, |
| 980 | pldm_entity entity, pldm_entity_node **node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 981 | { |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 982 | bool is_entity_container_id; |
| 983 | bool is_entity_instance_num; |
| 984 | bool is_type; |
| 985 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 986 | if (tree_node == NULL) { |
| 987 | return; |
| 988 | } |
| 989 | |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 990 | is_type = tree_node->entity.entity_type == entity.entity_type; |
| 991 | is_entity_instance_num = tree_node->entity.entity_instance_num == |
| 992 | entity.entity_instance_num; |
| 993 | is_entity_container_id = tree_node->entity.entity_container_id == |
| 994 | entity.entity_container_id; |
| 995 | |
| 996 | if (is_type && is_entity_instance_num && is_entity_container_id) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 997 | *node = tree_node; |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | find_entity_ref_in_tree(tree_node->first_child, entity, node); |
| 1002 | find_entity_ref_in_tree(tree_node->next_sibling, entity, node); |
| 1003 | } |
| 1004 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1005 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1006 | void pldm_find_entity_ref_in_tree(pldm_entity_association_tree *tree, |
| 1007 | pldm_entity entity, pldm_entity_node **node) |
| 1008 | { |
Andrew Jeffery | ba47e83 | 2023-07-03 11:41:03 +0930 | [diff] [blame] | 1009 | if (!tree || !node) { |
| 1010 | return; |
| 1011 | } |
| 1012 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1013 | find_entity_ref_in_tree(tree->root, entity, node); |
| 1014 | } |
| 1015 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1016 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1017 | void pldm_pdr_remove_pdrs_by_terminus_handle(pldm_pdr *repo, |
| 1018 | uint16_t terminus_handle) |
| 1019 | { |
Andrew Jeffery | 438dd49 | 2023-07-03 11:51:43 +0930 | [diff] [blame] | 1020 | if (!repo) { |
| 1021 | return; |
| 1022 | } |
| 1023 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1024 | bool removed = false; |
| 1025 | |
| 1026 | pldm_pdr_record *record = repo->first; |
| 1027 | pldm_pdr_record *prev = NULL; |
| 1028 | while (record != NULL) { |
| 1029 | pldm_pdr_record *next = record->next; |
| 1030 | if (record->terminus_handle == terminus_handle) { |
| 1031 | if (repo->first == record) { |
| 1032 | repo->first = next; |
| 1033 | } else { |
| 1034 | prev->next = next; |
| 1035 | } |
| 1036 | if (repo->last == record) { |
| 1037 | repo->last = prev; |
| 1038 | } |
| 1039 | if (record->data) { |
| 1040 | free(record->data); |
| 1041 | } |
| 1042 | --repo->record_count; |
| 1043 | repo->size -= record->size; |
| 1044 | free(record); |
| 1045 | removed = true; |
| 1046 | } else { |
| 1047 | prev = record; |
| 1048 | } |
| 1049 | record = next; |
| 1050 | } |
| 1051 | |
| 1052 | if (removed == true) { |
| 1053 | record = repo->first; |
| 1054 | uint32_t record_handle = 0; |
| 1055 | while (record != NULL) { |
| 1056 | record->record_handle = ++record_handle; |
| 1057 | if (record->data != NULL) { |
| 1058 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1059 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1060 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1061 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1062 | } |
| 1063 | record = record->next; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1067 | |
| 1068 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1069 | void pldm_pdr_remove_remote_pdrs(pldm_pdr *repo) |
| 1070 | { |
Andrew Jeffery | 3e1d659 | 2023-07-03 11:57:16 +0930 | [diff] [blame] | 1071 | if (!repo) { |
| 1072 | return; |
| 1073 | } |
| 1074 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1075 | bool removed = false; |
| 1076 | |
| 1077 | pldm_pdr_record *record = repo->first; |
| 1078 | pldm_pdr_record *prev = NULL; |
| 1079 | while (record != NULL) { |
| 1080 | pldm_pdr_record *next = record->next; |
| 1081 | if (record->is_remote == true) { |
| 1082 | if (repo->first == record) { |
| 1083 | repo->first = next; |
| 1084 | } else { |
| 1085 | prev->next = next; |
| 1086 | } |
| 1087 | if (repo->last == record) { |
| 1088 | repo->last = prev; |
| 1089 | } |
| 1090 | if (record->data) { |
| 1091 | free(record->data); |
| 1092 | } |
| 1093 | --repo->record_count; |
| 1094 | repo->size -= record->size; |
| 1095 | free(record); |
| 1096 | removed = true; |
| 1097 | } else { |
| 1098 | prev = record; |
| 1099 | } |
| 1100 | record = next; |
| 1101 | } |
| 1102 | |
| 1103 | if (removed == true) { |
| 1104 | record = repo->first; |
| 1105 | uint32_t record_handle = 0; |
| 1106 | while (record != NULL) { |
| 1107 | record->record_handle = ++record_handle; |
| 1108 | if (record->data != NULL) { |
| 1109 | struct pldm_pdr_hdr *hdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1110 | (struct pldm_pdr_hdr *)(record->data); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1111 | hdr->record_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1112 | htole32(record->record_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1113 | } |
| 1114 | record = record->next; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
Pavithra Barithaya | 4a307bc | 2023-11-13 04:34:14 -0600 | [diff] [blame] | 1119 | LIBPLDM_ABI_STABLE |
Pavithra Barithaya | 4d69434 | 2023-05-19 08:04:41 -0500 | [diff] [blame] | 1120 | pldm_pdr_record *pldm_pdr_find_last_in_range(const pldm_pdr *repo, |
| 1121 | uint32_t first, uint32_t last) |
| 1122 | { |
| 1123 | pldm_pdr_record *record = NULL; |
| 1124 | pldm_pdr_record *curr; |
| 1125 | |
| 1126 | if (!repo) { |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | for (curr = repo->first; curr; curr = curr->next) { |
| 1130 | if (first > curr->record_handle || last < curr->record_handle) { |
| 1131 | continue; |
| 1132 | } |
| 1133 | if (!record || curr->record_handle > record->record_handle) { |
| 1134 | record = curr; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return record; |
| 1139 | } |
| 1140 | |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1141 | static void entity_association_tree_find_if_remote(pldm_entity_node *node, |
| 1142 | pldm_entity *entity, |
| 1143 | pldm_entity_node **out, |
| 1144 | bool is_remote) |
| 1145 | { |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1146 | if (node == NULL) { |
| 1147 | return; |
| 1148 | } |
| 1149 | bool is_entity_type; |
| 1150 | bool is_entity_instance_num; |
| 1151 | |
| 1152 | is_entity_type = node->entity.entity_type == entity->entity_type; |
| 1153 | is_entity_instance_num = node->entity.entity_instance_num == |
| 1154 | entity->entity_instance_num; |
| 1155 | |
| 1156 | if (!is_remote || |
| 1157 | node->remote_container_id == entity->entity_container_id) { |
| 1158 | if (is_entity_type && is_entity_instance_num) { |
| 1159 | entity->entity_container_id = |
| 1160 | node->entity.entity_container_id; |
| 1161 | *out = node; |
| 1162 | return; |
| 1163 | } |
| 1164 | } |
| 1165 | entity_association_tree_find_if_remote(node->next_sibling, entity, out, |
| 1166 | is_remote); |
| 1167 | entity_association_tree_find_if_remote(node->first_child, entity, out, |
| 1168 | is_remote); |
| 1169 | } |
| 1170 | |
Pavithra Barithaya | 7570fae | 2023-07-28 02:15:16 -0500 | [diff] [blame] | 1171 | LIBPLDM_ABI_STABLE |
Sagar Srinivas | 302d9ff | 2023-08-01 02:11:30 -0500 | [diff] [blame] | 1172 | pldm_entity_node *pldm_entity_association_tree_find_with_locality( |
| 1173 | pldm_entity_association_tree *tree, pldm_entity *entity, bool is_remote) |
Pavithra Barithaya | 9947f9d | 2023-05-18 05:20:24 -0500 | [diff] [blame] | 1174 | { |
| 1175 | if (!tree || !entity) { |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | pldm_entity_node *node = NULL; |
| 1179 | entity_association_tree_find_if_remote(tree->root, entity, &node, |
| 1180 | is_remote); |
| 1181 | return node; |
| 1182 | } |
| 1183 | |
Andrew Jeffery | 54d91e8 | 2023-07-17 15:37:12 +0930 | [diff] [blame] | 1184 | static void entity_association_tree_find(pldm_entity_node *node, |
| 1185 | pldm_entity *entity, |
| 1186 | pldm_entity_node **out) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1187 | { |
| 1188 | if (node == NULL) { |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | if (node->entity.entity_type == entity->entity_type && |
| 1193 | node->entity.entity_instance_num == entity->entity_instance_num) { |
| 1194 | entity->entity_container_id = node->entity.entity_container_id; |
| 1195 | *out = node; |
| 1196 | return; |
| 1197 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1198 | entity_association_tree_find(node->next_sibling, entity, out); |
| 1199 | entity_association_tree_find(node->first_child, entity, out); |
| 1200 | } |
| 1201 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1202 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1203 | pldm_entity_node * |
| 1204 | pldm_entity_association_tree_find(pldm_entity_association_tree *tree, |
| 1205 | pldm_entity *entity) |
| 1206 | { |
Andrew Jeffery | 94e364d | 2023-07-03 12:26:59 +0930 | [diff] [blame] | 1207 | if (!tree || !entity) { |
| 1208 | return NULL; |
| 1209 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1210 | |
| 1211 | pldm_entity_node *node = NULL; |
| 1212 | entity_association_tree_find(tree->root, entity, &node); |
| 1213 | return node; |
| 1214 | } |
| 1215 | |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1216 | static int entity_association_tree_copy(pldm_entity_node *org_node, |
| 1217 | pldm_entity_node **new_node) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1218 | { |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1219 | int rc; |
| 1220 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1221 | if (org_node == NULL) { |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1222 | return 0; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1223 | } |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1224 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1225 | *new_node = malloc(sizeof(pldm_entity_node)); |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1226 | if (!*new_node) { |
| 1227 | return -ENOMEM; |
| 1228 | } |
| 1229 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1230 | (*new_node)->parent = org_node->parent; |
| 1231 | (*new_node)->entity = org_node->entity; |
| 1232 | (*new_node)->association_type = org_node->association_type; |
ArchanaKakani | 39bd2ea | 2023-02-02 02:39:18 -0600 | [diff] [blame] | 1233 | (*new_node)->remote_container_id = org_node->remote_container_id; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1234 | (*new_node)->first_child = NULL; |
| 1235 | (*new_node)->next_sibling = NULL; |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1236 | |
| 1237 | rc = entity_association_tree_copy(org_node->first_child, |
| 1238 | &((*new_node)->first_child)); |
| 1239 | if (rc) { |
| 1240 | goto cleanup; |
| 1241 | } |
| 1242 | |
| 1243 | rc = entity_association_tree_copy(org_node->next_sibling, |
| 1244 | &((*new_node)->next_sibling)); |
| 1245 | if (rc) { |
| 1246 | entity_association_tree_destroy((*new_node)->first_child); |
| 1247 | goto cleanup; |
| 1248 | } |
| 1249 | |
| 1250 | return 0; |
| 1251 | |
| 1252 | cleanup: |
| 1253 | free(*new_node); |
| 1254 | *new_node = NULL; |
| 1255 | return rc; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1256 | } |
| 1257 | |
Andrew Jeffery | 36324f6 | 2024-09-25 13:41:41 +0930 | [diff] [blame] | 1258 | LIBPLDM_ABI_DEPRECATED_UNSAFE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1259 | void pldm_entity_association_tree_copy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1260 | pldm_entity_association_tree *org_tree, |
| 1261 | pldm_entity_association_tree *new_tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1262 | { |
George Liu | c6c391d | 2023-11-09 10:13:34 +0800 | [diff] [blame] | 1263 | assert(org_tree != NULL); |
| 1264 | assert(new_tree != NULL); |
| 1265 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1266 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 1267 | entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 1268 | } |
| 1269 | |
Andrew Jeffery | 6058215 | 2024-09-22 21:16:38 +0930 | [diff] [blame] | 1270 | LIBPLDM_ABI_TESTING |
| 1271 | int pldm_entity_association_tree_copy_root_check( |
| 1272 | pldm_entity_association_tree *org_tree, |
| 1273 | pldm_entity_association_tree *new_tree) |
| 1274 | { |
| 1275 | if (!org_tree || !new_tree) { |
| 1276 | return -EINVAL; |
| 1277 | } |
| 1278 | |
| 1279 | new_tree->last_used_container_id = org_tree->last_used_container_id; |
| 1280 | return entity_association_tree_copy(org_tree->root, &(new_tree->root)); |
| 1281 | } |
| 1282 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1283 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1284 | void pldm_entity_association_tree_destroy_root( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1285 | pldm_entity_association_tree *tree) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1286 | { |
Andrew Jeffery | 85d7a05 | 2023-07-03 12:29:24 +0930 | [diff] [blame] | 1287 | if (!tree) { |
| 1288 | return; |
| 1289 | } |
| 1290 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1291 | entity_association_tree_destroy(tree->root); |
| 1292 | tree->last_used_container_id = 0; |
| 1293 | tree->root = NULL; |
| 1294 | } |
| 1295 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1296 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1297 | bool pldm_is_empty_entity_assoc_tree(pldm_entity_association_tree *tree) |
| 1298 | { |
| 1299 | return ((tree->root == NULL) ? true : false); |
| 1300 | } |
| 1301 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 1302 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1303 | void pldm_entity_association_pdr_extract(const uint8_t *pdr, uint16_t pdr_len, |
| 1304 | size_t *num_entities, |
| 1305 | pldm_entity **entities) |
| 1306 | { |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1307 | if (!pdr || !num_entities || !entities) { |
| 1308 | return; |
| 1309 | } |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1310 | if (pdr_len < PDR_ENTITY_ASSOCIATION_MIN_SIZE) { |
Andrew Jeffery | 3a5c46b | 2023-07-03 12:39:59 +0930 | [diff] [blame] | 1311 | return; |
| 1312 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1313 | |
| 1314 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)pdr; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1315 | if (hdr->type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 1316 | return; |
| 1317 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1318 | |
| 1319 | const uint8_t *start = (uint8_t *)pdr; |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 1320 | |
| 1321 | if (UINTPTR_MAX - (uintptr_t)start < |
| 1322 | (sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length))) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | if (pdr_len < (sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length))) { |
| 1327 | return; |
| 1328 | } |
| 1329 | |
| 1330 | const uint8_t *end = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1331 | start + sizeof(struct pldm_pdr_hdr) + le16toh(hdr->length); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1332 | start += sizeof(struct pldm_pdr_hdr); |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 1333 | |
| 1334 | if ((uintptr_t)end - (uintptr_t)start < |
| 1335 | sizeof(struct pldm_pdr_entity_association)) { |
| 1336 | return; |
| 1337 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1338 | struct pldm_pdr_entity_association *entity_association_pdr = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1339 | (struct pldm_pdr_entity_association *)start; |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 1340 | |
| 1341 | if (entity_association_pdr->num_children == UINT8_MAX) { |
| 1342 | return; |
| 1343 | } |
Andrew Jeffery | 8f33a1d | 2024-10-11 18:01:19 +1030 | [diff] [blame^] | 1344 | |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1345 | size_t l_num_entities = entity_association_pdr->num_children + 1; |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1346 | if (l_num_entities < 2) { |
| 1347 | return; |
| 1348 | } |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 1349 | |
Andrew Jeffery | 8f33a1d | 2024-10-11 18:01:19 +1030 | [diff] [blame^] | 1350 | if (UINT8_MAX < l_num_entities) { |
Andrew Jeffery | 9e56659 | 2024-10-02 16:38:51 +0930 | [diff] [blame] | 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | pldm_entity *l_entities = calloc(l_num_entities, sizeof(pldm_entity)); |
Andrew Jeffery | 918973f | 2023-07-11 17:11:03 +0930 | [diff] [blame] | 1355 | if (!l_entities) { |
| 1356 | return; |
| 1357 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1358 | l_entities[0].entity_type = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1359 | le16toh(entity_association_pdr->container.entity_type); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1360 | l_entities[0].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1361 | le16toh(entity_association_pdr->container.entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1362 | l_entities[0].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1363 | le16toh(entity_association_pdr->container.entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1364 | pldm_entity *curr_entity = entity_association_pdr->children; |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1365 | for (size_t i = 1; i < l_num_entities; i++, curr_entity++) { |
| 1366 | l_entities[i].entity_type = le16toh(curr_entity->entity_type); |
| 1367 | l_entities[i].entity_instance_num = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1368 | le16toh(curr_entity->entity_instance_num); |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1369 | l_entities[i].entity_container_id = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 1370 | le16toh(curr_entity->entity_container_id); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1371 | } |
Andrew Jeffery | 0dbaa70 | 2023-07-11 16:58:25 +0930 | [diff] [blame] | 1372 | |
| 1373 | *num_entities = l_num_entities; |
| 1374 | *entities = l_entities; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1375 | } |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1376 | |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 1377 | /* 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] | 1378 | * the same position. |
| 1379 | */ |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 1380 | LIBPLDM_CC_NONNULL |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1381 | static int pldm_pdr_replace_record(pldm_pdr *repo, pldm_pdr_record *record, |
| 1382 | pldm_pdr_record *prev, |
| 1383 | pldm_pdr_record *new_record) |
| 1384 | { |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1385 | if (repo->size < record->size) { |
| 1386 | return -EOVERFLOW; |
| 1387 | } |
| 1388 | |
| 1389 | if (repo->size + new_record->size < new_record->size) { |
| 1390 | return -EOVERFLOW; |
| 1391 | } |
| 1392 | |
| 1393 | if (repo->first == record) { |
| 1394 | repo->first = new_record; |
| 1395 | } else { |
| 1396 | prev->next = new_record; |
| 1397 | } |
| 1398 | new_record->next = record->next; |
| 1399 | |
| 1400 | if (repo->last == record) { |
| 1401 | repo->last = new_record; |
| 1402 | } |
| 1403 | |
| 1404 | repo->size = (repo->size - record->size) + new_record->size; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | /* Insert a new record to pldm_pdr repo to a position that comes after |
| 1409 | * pldm_pdr_record record. |
| 1410 | */ |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 1411 | LIBPLDM_CC_NONNULL |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1412 | static int pldm_pdr_insert_record(pldm_pdr *repo, pldm_pdr_record *record, |
| 1413 | pldm_pdr_record *new_record) |
| 1414 | { |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1415 | if (repo->size + new_record->size < new_record->size) { |
| 1416 | return -EOVERFLOW; |
| 1417 | } |
| 1418 | |
| 1419 | if (repo->record_count == UINT32_MAX) { |
| 1420 | return -EOVERFLOW; |
| 1421 | } |
| 1422 | |
| 1423 | new_record->next = record->next; |
| 1424 | record->next = new_record; |
| 1425 | |
| 1426 | if (repo->last == record) { |
| 1427 | repo->last = new_record; |
| 1428 | } |
| 1429 | |
| 1430 | repo->size = repo->size + new_record->size; |
| 1431 | ++repo->record_count; |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | /* Find the position of PDR when its record handle is known |
| 1436 | */ |
Andrew Jeffery | 890d37a | 2024-09-22 20:50:53 +0930 | [diff] [blame] | 1437 | LIBPLDM_CC_NONNULL |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1438 | static bool pldm_pdr_find_record_by_handle(pldm_pdr_record **record, |
| 1439 | pldm_pdr_record **prev, |
| 1440 | uint32_t record_handle) |
| 1441 | { |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1442 | while (*record != NULL) { |
| 1443 | if ((*record)->record_handle == record_handle) { |
| 1444 | return true; |
| 1445 | } |
| 1446 | *prev = *record; |
| 1447 | *record = (*record)->next; |
| 1448 | } |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | LIBPLDM_ABI_TESTING |
| 1453 | int pldm_entity_association_pdr_add_contained_entity_to_remote_pdr( |
| 1454 | pldm_pdr *repo, pldm_entity *entity, uint32_t pdr_record_handle) |
| 1455 | { |
| 1456 | if (!repo || !entity) { |
| 1457 | return -EINVAL; |
| 1458 | } |
| 1459 | |
| 1460 | pldm_pdr_record *record = repo->first; |
| 1461 | pldm_pdr_record *prev = repo->first; |
| 1462 | int rc = 0; |
| 1463 | uint16_t header_length = 0; |
| 1464 | uint8_t num_children = 0; |
| 1465 | struct pldm_msgbuf _src; |
| 1466 | struct pldm_msgbuf *src = &_src; |
| 1467 | struct pldm_msgbuf _dst; |
| 1468 | struct pldm_msgbuf *dst = &_dst; |
| 1469 | |
| 1470 | pldm_pdr_find_record_by_handle(&record, &prev, pdr_record_handle); |
| 1471 | |
| 1472 | if (!record) { |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | // Initialize msg buffer for record and record->data |
| 1476 | rc = pldm_msgbuf_init_errno(src, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1477 | record->data, record->size); |
| 1478 | if (rc) { |
| 1479 | return rc; |
| 1480 | } |
| 1481 | |
| 1482 | // check if adding another entity to record causes overflow before |
| 1483 | // allocating memory for new_record. |
| 1484 | if (record->size + sizeof(pldm_entity) < sizeof(pldm_entity)) { |
| 1485 | return -EOVERFLOW; |
| 1486 | } |
| 1487 | pldm_pdr_record *new_record = malloc(sizeof(pldm_pdr_record)); |
| 1488 | if (!new_record) { |
| 1489 | return -ENOMEM; |
| 1490 | } |
| 1491 | |
| 1492 | new_record->data = malloc(record->size + sizeof(pldm_entity)); |
| 1493 | if (!new_record->data) { |
| 1494 | rc = -ENOMEM; |
| 1495 | goto cleanup_new_record; |
| 1496 | } |
| 1497 | |
| 1498 | new_record->record_handle = record->record_handle; |
| 1499 | new_record->size = record->size + sizeof(struct pldm_entity); |
| 1500 | new_record->is_remote = record->is_remote; |
| 1501 | |
| 1502 | // Initialize new PDR record with data from original PDR record. |
| 1503 | // Start with adding the header of original PDR |
| 1504 | rc = pldm_msgbuf_init_errno(dst, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1505 | new_record->data, new_record->size); |
| 1506 | if (rc) { |
| 1507 | goto cleanup_new_record_data; |
| 1508 | } |
| 1509 | |
| 1510 | pldm_msgbuf_copy(dst, src, uint32_t, hdr_record_handle); |
| 1511 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_version); |
| 1512 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_type); |
| 1513 | pldm_msgbuf_copy(dst, src, uint16_t, hdr_record_change_num); |
| 1514 | // extract the header length from record and increment size with |
| 1515 | // size of pldm_entity before inserting the value into new_record. |
| 1516 | rc = pldm_msgbuf_extract(src, header_length); |
| 1517 | if (rc) { |
| 1518 | goto cleanup_new_record_data; |
| 1519 | } |
| 1520 | static_assert(UINT16_MAX < (SIZE_MAX - sizeof(pldm_entity)), |
| 1521 | "Fix the following bounds check."); |
| 1522 | if (header_length + sizeof(pldm_entity) > UINT16_MAX) { |
| 1523 | rc = -EOVERFLOW; |
| 1524 | goto cleanup_new_record_data; |
| 1525 | } |
| 1526 | header_length += sizeof(pldm_entity); |
| 1527 | pldm_msgbuf_insert(dst, header_length); |
| 1528 | pldm_msgbuf_copy(dst, src, uint16_t, container_id); |
| 1529 | pldm_msgbuf_copy(dst, src, uint8_t, association_type); |
| 1530 | pldm_msgbuf_copy(dst, src, uint16_t, entity_type); |
| 1531 | pldm_msgbuf_copy(dst, src, uint16_t, entity_instance_num); |
| 1532 | pldm_msgbuf_copy(dst, src, uint16_t, entity_container_id); |
| 1533 | // extract value of number of children from record and increment it |
| 1534 | // by 1 before insert the value to new record. |
| 1535 | rc = pldm_msgbuf_extract(src, num_children); |
| 1536 | if (rc) { |
| 1537 | goto cleanup_new_record_data; |
| 1538 | } |
| 1539 | if (num_children == UINT8_MAX) { |
| 1540 | rc = -EOVERFLOW; |
| 1541 | goto cleanup_new_record_data; |
| 1542 | } |
| 1543 | num_children += 1; |
| 1544 | pldm_msgbuf_insert(dst, num_children); |
| 1545 | //Add all children of original PDR to new PDR |
| 1546 | for (int i = 0; i < num_children - 1; i++) { |
| 1547 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_type); |
| 1548 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_instance_num); |
| 1549 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_container_id); |
| 1550 | } |
| 1551 | |
| 1552 | // Add new contained entity as a child of new PDR |
| 1553 | rc = pldm_msgbuf_destroy(src); |
| 1554 | if (rc) { |
| 1555 | goto cleanup_new_record_data; |
| 1556 | } |
| 1557 | rc = pldm_msgbuf_init_errno(src, sizeof(struct pldm_entity), entity, |
| 1558 | sizeof(struct pldm_entity)); |
| 1559 | if (rc) { |
| 1560 | goto cleanup_new_record_data; |
| 1561 | } |
| 1562 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_type); |
| 1563 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_instance_num); |
| 1564 | pldm_msgbuf_copy(dst, src, uint16_t, child_entity_container_id); |
| 1565 | |
| 1566 | rc = pldm_msgbuf_destroy(src); |
| 1567 | if (rc) { |
| 1568 | goto cleanup_new_record_data; |
| 1569 | } |
| 1570 | rc = pldm_msgbuf_destroy(dst); |
| 1571 | if (rc) { |
| 1572 | goto cleanup_new_record_data; |
| 1573 | } |
| 1574 | |
| 1575 | rc = pldm_pdr_replace_record(repo, record, prev, new_record); |
| 1576 | if (rc) { |
| 1577 | goto cleanup_new_record_data; |
| 1578 | } |
| 1579 | |
| 1580 | free(record->data); |
| 1581 | free(record); |
| 1582 | return rc; |
| 1583 | cleanup_new_record_data: |
| 1584 | free(new_record->data); |
| 1585 | cleanup_new_record: |
| 1586 | free(new_record); |
| 1587 | return rc; |
| 1588 | } |
| 1589 | |
| 1590 | LIBPLDM_ABI_TESTING |
| 1591 | int pldm_entity_association_pdr_create_new(pldm_pdr *repo, |
| 1592 | uint32_t pdr_record_handle, |
| 1593 | pldm_entity *parent, |
| 1594 | pldm_entity *entity, |
| 1595 | uint32_t *entity_record_handle) |
| 1596 | { |
| 1597 | if (!repo || !parent || !entity || !entity_record_handle) { |
| 1598 | return -EINVAL; |
| 1599 | } |
| 1600 | |
| 1601 | if (pdr_record_handle == UINT32_MAX) { |
| 1602 | return -EOVERFLOW; |
| 1603 | } |
| 1604 | |
| 1605 | bool pdr_added = false; |
| 1606 | uint16_t new_pdr_size; |
| 1607 | uint16_t container_id = 0; |
Varsha Kaverappa | b31e4c6 | 2024-06-25 06:45:46 -0500 | [diff] [blame] | 1608 | void *container_id_addr; |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1609 | struct pldm_msgbuf _dst; |
| 1610 | struct pldm_msgbuf *dst = &_dst; |
| 1611 | struct pldm_msgbuf _src_p; |
| 1612 | struct pldm_msgbuf *src_p = &_src_p; |
| 1613 | struct pldm_msgbuf _src_c; |
| 1614 | struct pldm_msgbuf *src_c = &_src_c; |
| 1615 | int rc = 0; |
| 1616 | |
| 1617 | pldm_pdr_record *prev = repo->first; |
| 1618 | pldm_pdr_record *record = repo->first; |
| 1619 | pdr_added = pldm_pdr_find_record_by_handle(&record, &prev, |
| 1620 | pdr_record_handle); |
| 1621 | if (!pdr_added) { |
| 1622 | return -ENOENT; |
| 1623 | } |
| 1624 | |
| 1625 | static_assert(PDR_ENTITY_ASSOCIATION_MIN_SIZE < UINT16_MAX, |
| 1626 | "Truncation ahead"); |
| 1627 | new_pdr_size = PDR_ENTITY_ASSOCIATION_MIN_SIZE; |
| 1628 | pldm_pdr_record *new_record = malloc(sizeof(pldm_pdr_record)); |
| 1629 | if (!new_record) { |
| 1630 | return -ENOMEM; |
| 1631 | } |
| 1632 | |
| 1633 | new_record->data = malloc(new_pdr_size); |
| 1634 | if (!new_record->data) { |
| 1635 | rc = -ENOMEM; |
| 1636 | goto cleanup_new_record; |
| 1637 | } |
| 1638 | |
| 1639 | // Initialise new PDR to be added with the header, size and handle. |
| 1640 | // Set the position of new PDR |
| 1641 | *entity_record_handle = pdr_record_handle + 1; |
| 1642 | new_record->record_handle = *entity_record_handle; |
| 1643 | new_record->size = new_pdr_size; |
| 1644 | new_record->is_remote = false; |
| 1645 | |
| 1646 | rc = pldm_msgbuf_init_errno(dst, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1647 | new_record->data, new_record->size); |
| 1648 | if (rc) { |
| 1649 | goto cleanup_new_record_data; |
| 1650 | } |
| 1651 | |
| 1652 | // header record handle |
| 1653 | pldm_msgbuf_insert(dst, *entity_record_handle); |
| 1654 | // header version |
| 1655 | pldm_msgbuf_insert_uint8(dst, 1); |
| 1656 | // header type |
| 1657 | pldm_msgbuf_insert_uint8(dst, PLDM_PDR_ENTITY_ASSOCIATION); |
| 1658 | // header change number |
| 1659 | pldm_msgbuf_insert_uint16(dst, 0); |
| 1660 | // header length |
| 1661 | pldm_msgbuf_insert_uint16(dst, |
| 1662 | (new_pdr_size - sizeof(struct pldm_pdr_hdr))); |
| 1663 | |
| 1664 | // Data for new PDR is obtained from parent PDR and new contained entity |
| 1665 | // is added as the child |
| 1666 | rc = pldm_msgbuf_init_errno(src_p, sizeof(struct pldm_entity), parent, |
| 1667 | sizeof(*parent)); |
| 1668 | if (rc) { |
| 1669 | goto cleanup_new_record_data; |
| 1670 | } |
| 1671 | |
| 1672 | rc = pldm_msgbuf_init_errno(src_c, sizeof(struct pldm_entity), entity, |
| 1673 | sizeof(*entity)); |
| 1674 | if (rc) { |
| 1675 | goto cleanup_new_record_data; |
| 1676 | } |
| 1677 | |
Varsha Kaverappa | b31e4c6 | 2024-06-25 06:45:46 -0500 | [diff] [blame] | 1678 | container_id_addr = NULL; |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1679 | // extract pointer for container ID and save the address |
| 1680 | rc = pldm_msgbuf_span_required(dst, sizeof(container_id), |
| 1681 | (void **)&container_id_addr); |
| 1682 | if (rc) { |
| 1683 | goto cleanup_new_record_data; |
| 1684 | } |
Varsha Kaverappa | b31e4c6 | 2024-06-25 06:45:46 -0500 | [diff] [blame] | 1685 | assert(container_id_addr); |
Varsha Kaverappa | 37552b9 | 2024-02-12 05:06:06 -0600 | [diff] [blame] | 1686 | pldm_msgbuf_insert_uint8(dst, PLDM_ENTITY_ASSOCIAION_PHYSICAL); |
| 1687 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_type); |
| 1688 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_instance_num); |
| 1689 | pldm_msgbuf_copy(dst, src_p, uint16_t, entity_container_id); |
| 1690 | // number of children |
| 1691 | pldm_msgbuf_insert_uint8(dst, 1); |
| 1692 | |
| 1693 | // Add new entity as child |
| 1694 | pldm_msgbuf_copy(dst, src_c, uint16_t, child_entity_type); |
| 1695 | pldm_msgbuf_copy(dst, src_c, uint16_t, child_entity_instance_num); |
| 1696 | // Extract and insert child entity container ID and add same value to |
| 1697 | // container ID of entity |
| 1698 | pldm_msgbuf_extract(src_c, container_id); |
| 1699 | pldm_msgbuf_insert(dst, container_id); |
| 1700 | container_id = htole16(container_id); |
| 1701 | memcpy(container_id_addr, &container_id, sizeof(uint16_t)); |
| 1702 | |
| 1703 | rc = pldm_msgbuf_destroy(dst); |
| 1704 | if (rc) { |
| 1705 | goto cleanup_new_record_data; |
| 1706 | } |
| 1707 | rc = pldm_msgbuf_destroy(src_p); |
| 1708 | if (rc) { |
| 1709 | goto cleanup_new_record_data; |
| 1710 | } |
| 1711 | rc = pldm_msgbuf_destroy(src_c); |
| 1712 | if (rc) { |
| 1713 | goto cleanup_new_record_data; |
| 1714 | } |
| 1715 | |
| 1716 | rc = pldm_pdr_insert_record(repo, record, new_record); |
| 1717 | if (rc) { |
| 1718 | goto cleanup_new_record_data; |
| 1719 | } |
| 1720 | |
| 1721 | return rc; |
| 1722 | cleanup_new_record_data: |
| 1723 | free(new_record->data); |
| 1724 | cleanup_new_record: |
| 1725 | free(new_record); |
| 1726 | return rc; |
| 1727 | } |
Varsha Kaverappa | b31e4c6 | 2024-06-25 06:45:46 -0500 | [diff] [blame] | 1728 | |
| 1729 | LIBPLDM_CC_NONNULL |
| 1730 | static bool pldm_entity_cmp(const struct pldm_entity *l, |
| 1731 | const struct pldm_entity *r) |
| 1732 | { |
| 1733 | return l->entity_type == r->entity_type && |
| 1734 | l->entity_instance_num == r->entity_instance_num && |
| 1735 | l->entity_container_id == r->entity_container_id; |
| 1736 | } |
| 1737 | |
| 1738 | /* Find record handle of a PDR record from PDR repo and |
| 1739 | * entity |
| 1740 | */ |
| 1741 | LIBPLDM_CC_NONNULL |
| 1742 | static int pldm_entity_association_find_record_handle_by_entity( |
| 1743 | pldm_pdr *repo, pldm_entity *entity, bool is_remote, |
| 1744 | uint32_t *record_handle) |
| 1745 | { |
| 1746 | uint8_t num_children = 0; |
| 1747 | uint8_t hdr_type = 0; |
| 1748 | int rc = 0; |
| 1749 | size_t skip_data_size = 0; |
| 1750 | pldm_pdr_record *record = repo->first; |
| 1751 | struct pldm_msgbuf _dst; |
| 1752 | struct pldm_msgbuf *dst = &_dst; |
| 1753 | |
| 1754 | while (record != NULL) { |
| 1755 | rc = pldm_msgbuf_init_errno(dst, |
| 1756 | PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1757 | record->data, record->size); |
| 1758 | if (rc) { |
| 1759 | return rc; |
| 1760 | } |
| 1761 | skip_data_size = sizeof(uint32_t) + sizeof(uint8_t); |
| 1762 | pldm_msgbuf_span_required(dst, skip_data_size, NULL); |
| 1763 | pldm_msgbuf_extract(dst, hdr_type); |
| 1764 | if (record->is_remote != is_remote || |
| 1765 | hdr_type != PLDM_PDR_ENTITY_ASSOCIATION) { |
| 1766 | goto cleanup; |
| 1767 | } |
| 1768 | skip_data_size = sizeof(uint16_t) + sizeof(uint16_t) + |
| 1769 | sizeof(uint16_t) + sizeof(uint8_t) + |
| 1770 | sizeof(struct pldm_entity); |
| 1771 | pldm_msgbuf_span_required(dst, skip_data_size, NULL); |
| 1772 | pldm_msgbuf_extract(dst, num_children); |
| 1773 | for (int i = 0; i < num_children; ++i) { |
| 1774 | struct pldm_entity e; |
| 1775 | |
| 1776 | if ((rc = pldm_msgbuf_extract(dst, e.entity_type)) || |
| 1777 | (rc = pldm_msgbuf_extract(dst, |
| 1778 | e.entity_instance_num)) || |
| 1779 | (rc = pldm_msgbuf_extract(dst, |
| 1780 | e.entity_container_id))) { |
| 1781 | return rc; |
| 1782 | } |
| 1783 | |
| 1784 | if (pldm_entity_cmp(entity, &e)) { |
| 1785 | *record_handle = record->record_handle; |
| 1786 | return 0; |
| 1787 | } |
| 1788 | } |
| 1789 | cleanup: |
| 1790 | rc = pldm_msgbuf_destroy(dst); |
| 1791 | if (rc) { |
| 1792 | return rc; |
| 1793 | } |
| 1794 | record = record->next; |
| 1795 | } |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | LIBPLDM_ABI_TESTING |
| 1800 | int pldm_entity_association_pdr_remove_contained_entity( |
| 1801 | pldm_pdr *repo, pldm_entity *entity, bool is_remote, |
| 1802 | uint32_t *pdr_record_handle) |
| 1803 | { |
| 1804 | uint16_t header_length = 0; |
| 1805 | uint8_t num_children = 0; |
| 1806 | struct pldm_msgbuf _src; |
| 1807 | struct pldm_msgbuf *src = &_src; |
| 1808 | struct pldm_msgbuf _dst; |
| 1809 | struct pldm_msgbuf *dst = &_dst; |
| 1810 | int rc; |
| 1811 | pldm_pdr_record *record; |
| 1812 | pldm_pdr_record *prev; |
| 1813 | |
| 1814 | if (!repo || !entity || !pdr_record_handle) { |
| 1815 | return -EINVAL; |
| 1816 | } |
| 1817 | record = repo->first; |
| 1818 | prev = repo->first; |
| 1819 | |
| 1820 | rc = pldm_entity_association_find_record_handle_by_entity( |
| 1821 | repo, entity, is_remote, pdr_record_handle); |
| 1822 | if (rc) { |
| 1823 | return rc; |
| 1824 | } |
| 1825 | pldm_pdr_find_record_by_handle(&record, &prev, *pdr_record_handle); |
| 1826 | if (!record) { |
| 1827 | return -EINVAL; |
| 1828 | } |
| 1829 | // Initialize msg buffer for record and record->data |
| 1830 | rc = pldm_msgbuf_init_errno(src, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1831 | record->data, record->size); |
| 1832 | if (rc) { |
| 1833 | return rc; |
| 1834 | } |
| 1835 | // check if removing an entity from record causes overflow before |
| 1836 | // allocating memory for new_record. |
| 1837 | if (record->size < sizeof(pldm_entity)) { |
| 1838 | return -EOVERFLOW; |
| 1839 | } |
| 1840 | pldm_pdr_record *new_record = malloc(sizeof(pldm_pdr_record)); |
| 1841 | if (!new_record) { |
| 1842 | return -ENOMEM; |
| 1843 | } |
| 1844 | new_record->data = malloc(record->size - sizeof(pldm_entity)); |
| 1845 | if (!new_record->data) { |
| 1846 | rc = -ENOMEM; |
| 1847 | goto cleanup_new_record; |
| 1848 | } |
| 1849 | new_record->record_handle = record->record_handle; |
| 1850 | new_record->size = record->size - sizeof(struct pldm_entity); |
| 1851 | new_record->is_remote = record->is_remote; |
| 1852 | |
| 1853 | // Initialize new PDR record with data from original PDR record. |
| 1854 | // Start with adding the header of original PDR |
| 1855 | rc = pldm_msgbuf_init_errno(dst, PDR_ENTITY_ASSOCIATION_MIN_SIZE, |
| 1856 | new_record->data, new_record->size); |
| 1857 | if (rc) { |
| 1858 | goto cleanup_new_record_data; |
| 1859 | } |
| 1860 | pldm_msgbuf_copy(dst, src, uint32_t, hdr_record_handle); |
| 1861 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_version); |
| 1862 | pldm_msgbuf_copy(dst, src, uint8_t, hdr_type); |
| 1863 | pldm_msgbuf_copy(dst, src, uint16_t, hdr_record_change_num); |
| 1864 | // extract the header length from record and decrement size with |
| 1865 | // size of pldm_entity before inserting the value into new_record. |
| 1866 | rc = pldm_msgbuf_extract(src, header_length); |
| 1867 | if (rc) { |
| 1868 | goto cleanup_new_record_data; |
| 1869 | } |
| 1870 | if (header_length < sizeof(pldm_entity)) { |
| 1871 | rc = -EOVERFLOW; |
| 1872 | goto cleanup_new_record_data; |
| 1873 | } |
| 1874 | header_length -= sizeof(pldm_entity); |
| 1875 | pldm_msgbuf_insert(dst, header_length); |
| 1876 | pldm_msgbuf_copy(dst, src, uint16_t, container_id); |
| 1877 | pldm_msgbuf_copy(dst, src, uint8_t, association_type); |
| 1878 | pldm_msgbuf_copy(dst, src, uint16_t, entity_type); |
| 1879 | pldm_msgbuf_copy(dst, src, uint16_t, entity_instance_num); |
| 1880 | pldm_msgbuf_copy(dst, src, uint16_t, entity_container_id); |
| 1881 | // extract value of number of children from record and decrement it |
| 1882 | // by 1 before insert the value to new record. |
| 1883 | rc = pldm_msgbuf_extract(src, num_children); |
| 1884 | if (rc) { |
| 1885 | goto cleanup_new_record_data; |
| 1886 | } |
| 1887 | if (num_children == 1) { |
| 1888 | prev->next = record->next; |
| 1889 | free(record->data); |
| 1890 | free(record); |
| 1891 | goto cleanup_new_record_data; |
| 1892 | } else if (num_children < 1) { |
| 1893 | rc = -EOVERFLOW; |
| 1894 | goto cleanup_new_record_data; |
| 1895 | } |
| 1896 | num_children -= 1; |
| 1897 | pldm_msgbuf_insert(dst, num_children); |
| 1898 | //Add all children of original PDR to new PDR |
| 1899 | for (int i = 0; i < num_children + 1; ++i) { |
| 1900 | struct pldm_entity e; |
| 1901 | |
| 1902 | if ((rc = pldm_msgbuf_extract(src, e.entity_type)) || |
| 1903 | (rc = pldm_msgbuf_extract(src, e.entity_instance_num)) || |
| 1904 | (rc = pldm_msgbuf_extract(src, e.entity_container_id))) { |
| 1905 | goto cleanup_new_record_data; |
| 1906 | } |
| 1907 | |
| 1908 | if (pldm_entity_cmp(entity, &e)) { |
| 1909 | continue; |
| 1910 | } |
| 1911 | |
| 1912 | pldm_msgbuf_insert(dst, e.entity_type); |
| 1913 | pldm_msgbuf_insert(dst, e.entity_instance_num); |
| 1914 | pldm_msgbuf_insert(dst, e.entity_container_id); |
| 1915 | } |
| 1916 | |
| 1917 | if ((rc = pldm_msgbuf_destroy(src)) || |
| 1918 | (rc = pldm_msgbuf_destroy(dst)) || |
| 1919 | (rc = pldm_pdr_replace_record(repo, record, prev, new_record))) { |
| 1920 | goto cleanup_new_record_data; |
| 1921 | } |
| 1922 | |
| 1923 | free(record->data); |
| 1924 | free(record); |
| 1925 | return rc; |
| 1926 | |
| 1927 | cleanup_new_record_data: |
| 1928 | free(new_record->data); |
| 1929 | cleanup_new_record: |
| 1930 | free(new_record); |
| 1931 | return rc; |
| 1932 | } |
Varsha Kaverappa | 8b53ad9 | 2024-06-15 06:36:06 -0500 | [diff] [blame] | 1933 | |
| 1934 | /* API to find the PDR record that is previous to a given PLDM PDR |
| 1935 | * record in a given PLDM PDR repository |
| 1936 | */ |
| 1937 | LIBPLDM_CC_NONNULL |
| 1938 | static pldm_pdr_record *pldm_pdr_get_prev_record(pldm_pdr *repo, |
| 1939 | pldm_pdr_record *record) |
| 1940 | { |
| 1941 | pldm_pdr_record *prev = NULL; |
| 1942 | pldm_pdr_record *curr = repo->first; |
| 1943 | |
| 1944 | while (curr != NULL) { |
| 1945 | if (curr->record_handle == record->record_handle) { |
| 1946 | break; |
| 1947 | } |
| 1948 | prev = curr; |
| 1949 | curr = curr->next; |
| 1950 | } |
| 1951 | return prev; |
| 1952 | } |
| 1953 | |
| 1954 | /* API to check if a PLDM PDR record is present in a PLDM PDR repository |
| 1955 | */ |
| 1956 | LIBPLDM_CC_NONNULL |
| 1957 | static bool is_prev_record_present(pldm_pdr *repo, pldm_pdr_record *record) |
| 1958 | { |
| 1959 | if (repo->first == record) { |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
| 1963 | return pldm_pdr_get_prev_record(repo, record) != NULL; |
| 1964 | } |
| 1965 | |
| 1966 | /* API to check if FRU RSI of record matches the given record set identifier. |
| 1967 | * Returns 1 if the provided FRU record matches the provided record set identifier, |
| 1968 | * 0 if it does not, otherwise -EINVAL if the arguments are invalid. |
| 1969 | */ |
| 1970 | LIBPLDM_CC_NONNULL |
| 1971 | static int pldm_pdr_record_matches_fru_rsi(const pldm_pdr_record *record, |
| 1972 | uint16_t rsi) |
| 1973 | { |
| 1974 | uint16_t record_fru_rsi = 0; |
| 1975 | uint8_t *skip_data = NULL; |
| 1976 | uint8_t skip_data_size = 0; |
| 1977 | struct pldm_msgbuf _dst; |
| 1978 | struct pldm_msgbuf *dst = &_dst; |
| 1979 | int rc = 0; |
| 1980 | |
| 1981 | rc = pldm_msgbuf_init_errno(dst, PDR_FRU_RECORD_SET_MIN_SIZE, |
| 1982 | record->data, record->size); |
| 1983 | if (rc) { |
| 1984 | return rc; |
| 1985 | } |
| 1986 | skip_data_size = sizeof(struct pldm_pdr_hdr) + sizeof(uint16_t); |
| 1987 | pldm_msgbuf_span_required(dst, skip_data_size, (void **)&skip_data); |
| 1988 | pldm_msgbuf_extract(dst, record_fru_rsi); |
| 1989 | |
| 1990 | rc = pldm_msgbuf_destroy(dst); |
| 1991 | if (rc) { |
| 1992 | return rc; |
| 1993 | } |
| 1994 | return record_fru_rsi == rsi; |
| 1995 | } |
| 1996 | |
| 1997 | /* API to remove PLDM PDR record from a PLDM PDR repository |
| 1998 | */ |
| 1999 | LIBPLDM_CC_NONNULL_ARGS(1, 2) |
| 2000 | static int pldm_pdr_remove_record(pldm_pdr *repo, pldm_pdr_record *record, |
| 2001 | pldm_pdr_record *prev) |
| 2002 | { |
| 2003 | if (!is_prev_record_present(repo, record)) { |
| 2004 | return -EINVAL; |
| 2005 | } |
| 2006 | |
| 2007 | assert(repo->size >= record->size); |
| 2008 | if (repo->size < record->size) { |
| 2009 | return -EOVERFLOW; |
| 2010 | } |
| 2011 | |
| 2012 | if (repo->first == record) { |
| 2013 | repo->first = record->next; |
| 2014 | } else { |
| 2015 | if (prev != NULL) { |
| 2016 | prev->next = record->next; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | if (repo->last == record) { |
| 2021 | repo->last = prev; |
| 2022 | if (prev != NULL) { |
| 2023 | prev->next = NULL; |
| 2024 | } |
| 2025 | } |
| 2026 | repo->record_count -= 1; |
| 2027 | repo->size -= record->size; |
| 2028 | free(record->data); |
| 2029 | free(record); |
| 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | LIBPLDM_ABI_TESTING |
| 2035 | int pldm_pdr_remove_fru_record_set_by_rsi(pldm_pdr *repo, uint16_t fru_rsi, |
| 2036 | bool is_remote, |
| 2037 | uint32_t *record_handle) |
| 2038 | { |
| 2039 | pldm_pdr_record *record; |
| 2040 | pldm_pdr_record *prev = NULL; |
| 2041 | size_t skip_data_size = sizeof(uint32_t) + sizeof(uint8_t); |
| 2042 | uint8_t hdr_type = 0; |
| 2043 | int rc = 0; |
| 2044 | int match; |
| 2045 | |
| 2046 | if (!repo || !record_handle) { |
| 2047 | return -EINVAL; |
| 2048 | } |
| 2049 | record = repo->first; |
| 2050 | |
| 2051 | while (record != NULL) { |
| 2052 | struct pldm_msgbuf _buf; |
| 2053 | struct pldm_msgbuf *buf = &_buf; |
| 2054 | rc = pldm_msgbuf_init_errno(buf, PDR_FRU_RECORD_SET_MIN_SIZE, |
| 2055 | record->data, record->size); |
| 2056 | if (rc) { |
| 2057 | return rc; |
| 2058 | } |
| 2059 | pldm_msgbuf_span_required(buf, skip_data_size, NULL); |
| 2060 | if ((rc = pldm_msgbuf_extract(buf, hdr_type))) { |
| 2061 | return rc; |
| 2062 | } |
| 2063 | if (record->is_remote != is_remote || |
| 2064 | hdr_type != PLDM_PDR_FRU_RECORD_SET) { |
| 2065 | goto cleanup; |
| 2066 | } |
| 2067 | match = pldm_pdr_record_matches_fru_rsi(record, fru_rsi); |
| 2068 | if (match < 0) { |
| 2069 | return match; |
| 2070 | } |
| 2071 | if (match) { |
| 2072 | *record_handle = record->record_handle; |
| 2073 | prev = pldm_pdr_get_prev_record(repo, record); |
| 2074 | return pldm_pdr_remove_record(repo, record, prev); |
| 2075 | } |
| 2076 | cleanup: |
| 2077 | rc = pldm_msgbuf_destroy(buf); |
| 2078 | if (rc) { |
| 2079 | return rc; |
| 2080 | } |
| 2081 | record = record->next; |
| 2082 | } |
| 2083 | return rc; |
| 2084 | } |