Deepak Kodihalli | 3b02ed8 | 2020-02-06 01:18:25 -0600 | [diff] [blame] | 1 | #include "pdr.h" |
| 2 | #include "platform.h" |
| 3 | #include <assert.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | typedef struct pldm_pdr_record { |
| 8 | uint32_t record_handle; |
| 9 | uint32_t size; |
| 10 | uint8_t *data; |
| 11 | struct pldm_pdr_record *next; |
| 12 | } pldm_pdr_record; |
| 13 | |
| 14 | typedef struct pldm_pdr { |
| 15 | uint32_t record_count; |
| 16 | uint32_t size; |
| 17 | uint32_t last_used_record_handle; |
| 18 | pldm_pdr_record *first; |
| 19 | pldm_pdr_record *last; |
| 20 | } pldm_pdr; |
| 21 | |
| 22 | static inline uint32_t get_next_record_handle(const pldm_pdr *repo, |
| 23 | const pldm_pdr_record *record) |
| 24 | { |
| 25 | assert(repo != NULL); |
| 26 | assert(record != NULL); |
| 27 | |
| 28 | if (record == repo->last) { |
| 29 | return 0; |
| 30 | } |
| 31 | return record->next->record_handle; |
| 32 | } |
| 33 | |
| 34 | static void add_record(pldm_pdr *repo, pldm_pdr_record *record) |
| 35 | { |
| 36 | assert(repo != NULL); |
| 37 | assert(record != NULL); |
| 38 | |
| 39 | if (repo->first == NULL) { |
| 40 | assert(repo->last == NULL); |
| 41 | repo->first = record; |
| 42 | repo->last = record; |
| 43 | } else { |
| 44 | repo->last->next = record; |
| 45 | repo->last = record; |
| 46 | } |
| 47 | repo->size += record->size; |
| 48 | repo->last_used_record_handle = record->record_handle; |
| 49 | ++repo->record_count; |
| 50 | } |
| 51 | |
| 52 | static inline uint32_t get_new_record_handle(const pldm_pdr *repo) |
| 53 | { |
| 54 | assert(repo != NULL); |
| 55 | assert(repo->last_used_record_handle != UINT32_MAX); |
| 56 | |
| 57 | return repo->last_used_record_handle + 1; |
| 58 | } |
| 59 | |
| 60 | static pldm_pdr_record *make_new_record(const pldm_pdr *repo, |
| 61 | const uint8_t *data, uint32_t size, |
| 62 | uint32_t record_handle) |
| 63 | { |
| 64 | assert(repo != NULL); |
| 65 | assert(size != 0); |
| 66 | |
| 67 | pldm_pdr_record *record = malloc(sizeof(pldm_pdr_record)); |
| 68 | assert(record != NULL); |
| 69 | record->record_handle = |
| 70 | record_handle == 0 ? get_new_record_handle(repo) : record_handle; |
| 71 | record->size = size; |
| 72 | if (data != NULL) { |
| 73 | record->data = malloc(size); |
| 74 | assert(record->data != NULL); |
| 75 | memcpy(record->data, data, size); |
| 76 | /* If record handle is 0, that is an indication for this API to |
| 77 | * compute a new handle. For that reason, the computed handle |
| 78 | * needs to be populated in the PDR header. For a case where the |
| 79 | * caller supplied the record handle, it would exist in the |
| 80 | * header already. |
| 81 | */ |
| 82 | if (!record_handle) { |
| 83 | struct pldm_pdr_hdr *hdr = |
| 84 | (struct pldm_pdr_hdr *)(record->data); |
| 85 | hdr->record_handle = record->record_handle; |
| 86 | } |
| 87 | } |
| 88 | record->next = NULL; |
| 89 | |
| 90 | return record; |
| 91 | } |
| 92 | |
| 93 | uint32_t pldm_pdr_add(pldm_pdr *repo, const uint8_t *data, uint32_t size, |
| 94 | uint32_t record_handle) |
| 95 | { |
| 96 | assert(size != 0); |
| 97 | assert(data != NULL); |
| 98 | |
| 99 | pldm_pdr_record *record = |
| 100 | make_new_record(repo, data, size, record_handle); |
| 101 | add_record(repo, record); |
| 102 | |
| 103 | return record->record_handle; |
| 104 | } |
| 105 | |
| 106 | pldm_pdr *pldm_pdr_init() |
| 107 | { |
| 108 | pldm_pdr *repo = malloc(sizeof(pldm_pdr)); |
| 109 | assert(repo != NULL); |
| 110 | repo->record_count = 0; |
| 111 | repo->size = 0; |
| 112 | repo->last_used_record_handle = 0; |
| 113 | repo->first = NULL; |
| 114 | repo->last = NULL; |
| 115 | |
| 116 | return repo; |
| 117 | } |
| 118 | |
| 119 | void pldm_pdr_destroy(pldm_pdr *repo) |
| 120 | { |
| 121 | assert(repo != NULL); |
| 122 | |
| 123 | pldm_pdr_record *record = repo->first; |
| 124 | while (record != NULL) { |
| 125 | pldm_pdr_record *next = record->next; |
| 126 | if (record->data) { |
| 127 | free(record->data); |
| 128 | record->data = NULL; |
| 129 | } |
| 130 | free(record); |
| 131 | record = next; |
| 132 | } |
| 133 | free(repo); |
| 134 | } |
| 135 | |
| 136 | const pldm_pdr_record *pldm_pdr_find_record(const pldm_pdr *repo, |
| 137 | uint32_t record_handle, |
| 138 | uint8_t **data, uint32_t *size, |
| 139 | uint32_t *next_record_handle) |
| 140 | { |
| 141 | assert(repo != NULL); |
| 142 | assert(data != NULL); |
| 143 | assert(size != NULL); |
| 144 | assert(next_record_handle != NULL); |
| 145 | |
| 146 | if (!record_handle && (repo->first != NULL)) { |
| 147 | record_handle = repo->first->record_handle; |
| 148 | } |
| 149 | pldm_pdr_record *record = repo->first; |
| 150 | while (record != NULL) { |
| 151 | if (record->record_handle == record_handle) { |
| 152 | *size = record->size; |
| 153 | *data = record->data; |
| 154 | *next_record_handle = |
| 155 | get_next_record_handle(repo, record); |
| 156 | return record; |
| 157 | } |
| 158 | record = record->next; |
| 159 | } |
| 160 | |
| 161 | *size = 0; |
| 162 | *next_record_handle = 0; |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | const pldm_pdr_record * |
| 167 | pldm_pdr_get_next_record(const pldm_pdr *repo, |
| 168 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 169 | uint32_t *size, uint32_t *next_record_handle) |
| 170 | { |
| 171 | assert(repo != NULL); |
| 172 | assert(curr_record != NULL); |
| 173 | assert(data != NULL); |
| 174 | assert(size != NULL); |
| 175 | assert(next_record_handle != NULL); |
| 176 | |
| 177 | if (curr_record == repo->last) { |
| 178 | *data = NULL; |
| 179 | *size = 0; |
| 180 | *next_record_handle = get_next_record_handle(repo, curr_record); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | *next_record_handle = get_next_record_handle(repo, curr_record->next); |
| 185 | *data = curr_record->next->data; |
| 186 | *size = curr_record->next->size; |
| 187 | return curr_record->next; |
| 188 | } |
| 189 | |
| 190 | const pldm_pdr_record * |
| 191 | pldm_pdr_find_record_by_type(const pldm_pdr *repo, uint8_t pdr_type, |
| 192 | const pldm_pdr_record *curr_record, uint8_t **data, |
| 193 | uint32_t *size) |
| 194 | { |
| 195 | assert(repo != NULL); |
| 196 | assert(data != NULL); |
| 197 | assert(size != NULL); |
| 198 | |
| 199 | pldm_pdr_record *record = repo->first; |
| 200 | if (curr_record != NULL) { |
| 201 | record = curr_record->next; |
| 202 | } |
| 203 | while (record != NULL) { |
| 204 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)record->data; |
| 205 | if (hdr->type == pdr_type) { |
| 206 | *size = record->size; |
| 207 | *data = record->data; |
| 208 | return record; |
| 209 | } |
| 210 | record = record->next; |
| 211 | } |
| 212 | |
| 213 | *size = 0; |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | uint32_t pldm_pdr_get_record_count(const pldm_pdr *repo) |
| 218 | { |
| 219 | assert(repo != NULL); |
| 220 | |
| 221 | return repo->record_count; |
| 222 | } |
| 223 | |
| 224 | uint32_t pldm_pdr_get_repo_size(const pldm_pdr *repo) |
| 225 | { |
| 226 | assert(repo != NULL); |
| 227 | |
| 228 | return repo->size; |
| 229 | } |
| 230 | |
| 231 | uint32_t pldm_pdr_get_record_handle(const pldm_pdr *repo, |
| 232 | const pldm_pdr_record *record) |
| 233 | { |
| 234 | assert(repo != NULL); |
| 235 | assert(record != NULL); |
| 236 | |
| 237 | return record->record_handle; |
| 238 | } |
Deepak Kodihalli | db91467 | 2020-02-07 02:47:45 -0600 | [diff] [blame] | 239 | |
| 240 | uint32_t pldm_pdr_add_fru_record_set(pldm_pdr *repo, uint16_t terminus_handle, |
| 241 | uint16_t fru_rsi, uint16_t entity_type, |
| 242 | uint16_t entity_instance_num, |
| 243 | uint16_t container_id) |
| 244 | { |
| 245 | uint32_t size = sizeof(struct pldm_pdr_hdr) + |
| 246 | sizeof(struct pldm_pdr_fru_record_set); |
| 247 | uint8_t data[size]; |
| 248 | |
| 249 | struct pldm_pdr_hdr *hdr = (struct pldm_pdr_hdr *)&data; |
| 250 | hdr->version = 1; |
| 251 | hdr->record_handle = 0; |
| 252 | hdr->type = PLDM_PDR_FRU_RECORD_SET; |
| 253 | hdr->record_change_num = 0; |
| 254 | hdr->length = sizeof(struct pldm_pdr_fru_record_set); |
| 255 | struct pldm_pdr_fru_record_set *fru = |
| 256 | (struct pldm_pdr_fru_record_set *)((uint8_t *)hdr + |
| 257 | sizeof(struct pldm_pdr_hdr)); |
| 258 | fru->terminus_handle = terminus_handle; |
| 259 | fru->fru_rsi = fru_rsi; |
| 260 | fru->entity_type = entity_type; |
| 261 | fru->entity_instance_num = entity_instance_num; |
| 262 | fru->container_id = container_id; |
| 263 | |
| 264 | return pldm_pdr_add(repo, data, size, 0); |
| 265 | } |
| 266 | |
| 267 | const pldm_pdr_record *pldm_pdr_fru_record_set_find_by_rsi( |
| 268 | const pldm_pdr *repo, uint16_t fru_rsi, uint16_t *terminus_handle, |
| 269 | uint16_t *entity_type, uint16_t *entity_instance_num, |
| 270 | uint16_t *container_id) |
| 271 | { |
| 272 | assert(terminus_handle != NULL); |
| 273 | assert(entity_type != NULL); |
| 274 | assert(entity_instance_num != NULL); |
| 275 | assert(container_id != NULL); |
| 276 | |
| 277 | uint8_t *data = NULL; |
| 278 | uint32_t size = 0; |
| 279 | const pldm_pdr_record *curr_record = pldm_pdr_find_record_by_type( |
| 280 | repo, PLDM_PDR_FRU_RECORD_SET, NULL, &data, &size); |
| 281 | while (curr_record != NULL) { |
| 282 | struct pldm_pdr_fru_record_set *fru = |
| 283 | (struct pldm_pdr_fru_record_set |
| 284 | *)(data + sizeof(struct pldm_pdr_hdr)); |
| 285 | if (fru->fru_rsi == fru_rsi) { |
| 286 | *terminus_handle = fru->terminus_handle; |
| 287 | *entity_type = fru->entity_type; |
| 288 | *entity_instance_num = fru->entity_instance_num; |
| 289 | *container_id = fru->container_id; |
| 290 | return curr_record; |
| 291 | } |
| 292 | data = NULL; |
| 293 | curr_record = pldm_pdr_find_record_by_type( |
| 294 | repo, PLDM_PDR_FRU_RECORD_SET, curr_record, &data, &size); |
| 295 | } |
| 296 | |
| 297 | *terminus_handle = 0; |
| 298 | *entity_type = 0; |
| 299 | *entity_instance_num = 0; |
| 300 | *container_id = 0; |
| 301 | |
| 302 | return NULL; |
| 303 | } |