blob: e3a39275004f478809d1aca9a4a3e6f2ff3985cc [file] [log] [blame]
Aaron Durbin20686d82015-03-05 14:11:27 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin20686d82015-03-05 14:11:27 -060014 */
15
16#include <assert.h>
17#include <cbmem.h>
18#include <console/console.h>
19#include <imd.h>
20#include <stdlib.h>
21#include <string.h>
22
23/* For more details on implementation and usage please see the imd.h header. */
24
25static const uint32_t IMD_ROOT_PTR_MAGIC = 0xc0389481;
26static const uint32_t IMD_ENTRY_MAGIC = ~0xc0389481;
Aaron Durbincac50502015-03-24 23:14:46 -050027static const uint32_t SMALL_REGION_ID = CBMEM_ID_IMD_SMALL;
Aaron Durbin20686d82015-03-05 14:11:27 -060028static const size_t LIMIT_ALIGN = 4096;
29
30/* In-memory data structures. */
31struct imd_root_pointer {
32 uint32_t magic;
33 /* Relative to upper limit/offset. */
34 int32_t root_offset;
35} __attribute__((packed));
36
37struct imd_entry {
38 uint32_t magic;
39 /* start is located relative to imd_root */
40 int32_t start_offset;
41 uint32_t size;
42 uint32_t id;
43} __attribute__((packed));
44
45struct imd_root {
46 uint32_t max_entries;
47 uint32_t num_entries;
48 uint32_t flags;
49 uint32_t entry_align;
50 /* Used for fixing the size of an imd. Relative to the root. */
51 int32_t max_offset;
52 struct imd_entry entries[0];
53} __attribute__((packed));
54
55#define IMD_FLAG_LOCKED 1
56
57static void *relative_pointer(void *base, ssize_t offset)
58{
59 intptr_t b = (intptr_t)base;
60 b += offset;
61 return (void *)b;
62}
63
64static bool imd_root_pointer_valid(const struct imd_root_pointer *rp)
65{
66 return !!(rp->magic == IMD_ROOT_PTR_MAGIC);
67}
68
Aaron Durbincac50502015-03-24 23:14:46 -050069static struct imd_root *imdr_root(const struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -060070{
Aaron Durbincac50502015-03-24 23:14:46 -050071 return imdr->r;
Aaron Durbin20686d82015-03-05 14:11:27 -060072}
73
74/*
75 * The root pointer is relative to the upper limit of the imd. i.e. It sits
76 * just below the upper limit.
77 */
Aaron Durbincac50502015-03-24 23:14:46 -050078static struct imd_root_pointer *imdr_get_root_pointer(const struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -060079{
80 struct imd_root_pointer *rp;
81
Aaron Durbincac50502015-03-24 23:14:46 -050082 rp = relative_pointer((void *)imdr->limit, -sizeof(*rp));
Aaron Durbin20686d82015-03-05 14:11:27 -060083
84 return rp;
85}
86
87static void imd_link_root(struct imd_root_pointer *rp, struct imd_root *r)
88{
89 rp->magic = IMD_ROOT_PTR_MAGIC;
90 rp->root_offset = (int32_t)((intptr_t)r - (intptr_t)rp);
91}
92
Aaron Durbincac50502015-03-24 23:14:46 -050093static struct imd_entry *root_last_entry(struct imd_root *r)
94{
95 return &r->entries[r->num_entries - 1];
96}
97
98static size_t root_num_entries(size_t root_size)
99{
100 size_t entries_size;
101
102 entries_size = root_size;
103 entries_size -= sizeof(struct imd_root_pointer);
104 entries_size -= sizeof(struct imd_root);
105
106 return entries_size / sizeof(struct imd_entry);
107}
108
109static size_t imd_root_data_left(struct imd_root *r)
110{
111 struct imd_entry *last_entry;
112
113 last_entry = root_last_entry(r);
114
115 if (r->max_offset != 0)
116 return last_entry->start_offset - r->max_offset;
117
118 return ~(size_t)0;
119}
120
121static bool root_is_locked(const struct imd_root *r)
122{
123 return !!(r->flags & IMD_FLAG_LOCKED);
124}
125
Aaron Durbin20686d82015-03-05 14:11:27 -0600126static void imd_entry_assign(struct imd_entry *e, uint32_t id,
127 ssize_t offset, size_t size)
128{
129 e->magic = IMD_ENTRY_MAGIC;
130 e->start_offset = offset;
131 e->size = size;
132 e->id = id;
133}
134
Aaron Durbincac50502015-03-24 23:14:46 -0500135static void imdr_init(struct imdr *ir, void *upper_limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600136{
137 uintptr_t limit = (uintptr_t)upper_limit;
138 /* Upper limit is aligned down to 4KiB */
Aaron Durbincac50502015-03-24 23:14:46 -0500139 ir->limit = ALIGN_DOWN(limit, LIMIT_ALIGN);
140 ir->r = NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600141}
142
Aaron Durbincac50502015-03-24 23:14:46 -0500143static int imdr_create_empty(struct imdr *imdr, size_t root_size,
144 size_t entry_align)
Aaron Durbin20686d82015-03-05 14:11:27 -0600145{
146 struct imd_root_pointer *rp;
147 struct imd_root *r;
148 struct imd_entry *e;
149 ssize_t root_offset;
Aaron Durbin20686d82015-03-05 14:11:27 -0600150
Aaron Durbincac50502015-03-24 23:14:46 -0500151 if (!imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600152 return -1;
153
154 /* root_size and entry_align should be a power of 2. */
155 assert(IS_POWER_OF_2(root_size));
156 assert(IS_POWER_OF_2(entry_align));
157
Aaron Durbincac50502015-03-24 23:14:46 -0500158 if (!imdr->limit)
159 return -1;
160
Aaron Durbin20686d82015-03-05 14:11:27 -0600161 /*
Martin Roth2ed0aa22016-01-05 20:58:58 -0700162 * root_size needs to be large enough to accommodate root pointer and
Aaron Durbin20686d82015-03-05 14:11:27 -0600163 * root book keeping structure. The caller needs to ensure there's
164 * enough room for tracking individual allocations.
165 */
166 if (root_size < (sizeof(*rp) + sizeof(*r)))
167 return -1;
168
Lee Leahy73402172017-03-10 15:23:24 -0800169 /* For simplicity don't allow sizes or alignments to exceed LIMIT_ALIGN.
170 */
Aaron Durbin20686d82015-03-05 14:11:27 -0600171 if (root_size > LIMIT_ALIGN || entry_align > LIMIT_ALIGN)
172 return -1;
173
174 /* Additionally, don't handle an entry alignment > root_size. */
175 if (entry_align > root_size)
176 return -1;
177
Aaron Durbincac50502015-03-24 23:14:46 -0500178 rp = imdr_get_root_pointer(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600179
180 root_offset = -(ssize_t)root_size;
181 /* Set root pointer. */
Aaron Durbincac50502015-03-24 23:14:46 -0500182 imdr->r = relative_pointer((void *)imdr->limit, root_offset);
183 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600184 imd_link_root(rp, r);
185
186 memset(r, 0, sizeof(*r));
187 r->entry_align = entry_align;
188
189 /* Calculate size left for entries. */
Aaron Durbincac50502015-03-24 23:14:46 -0500190 r->max_entries = root_num_entries(root_size);
Aaron Durbin20686d82015-03-05 14:11:27 -0600191
192 /* Fill in first entry covering the root region. */
193 r->num_entries = 1;
194 e = &r->entries[0];
195 imd_entry_assign(e, CBMEM_ID_IMD_ROOT, 0, root_size);
196
197 printk(BIOS_DEBUG, "IMD: root @ %p %u entries.\n", r, r->max_entries);
198
199 return 0;
200}
201
Aaron Durbincac50502015-03-24 23:14:46 -0500202static int imdr_recover(struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -0600203{
204 struct imd_root_pointer *rp;
205 struct imd_root *r;
206 uintptr_t low_limit;
207 size_t i;
208
Aaron Durbincac50502015-03-24 23:14:46 -0500209 if (!imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600210 return -1;
211
Aaron Durbincac50502015-03-24 23:14:46 -0500212 rp = imdr_get_root_pointer(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600213
214 if (!imd_root_pointer_valid(rp))
215 return -1;
216
217 r = relative_pointer(rp, rp->root_offset);
218
219 /* Confirm the root and root pointer are just under the limit. */
220 if (ALIGN_UP((uintptr_t)&r->entries[r->max_entries], LIMIT_ALIGN) !=
Aaron Durbincac50502015-03-24 23:14:46 -0500221 imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600222 return -1;
223
224 if (r->num_entries > r->max_entries)
225 return -1;
226
227 /* Entry alignment should be power of 2. */
228 if (!IS_POWER_OF_2(r->entry_align))
229 return -1;
230
231 low_limit = (uintptr_t)relative_pointer(r, r->max_offset);
232
233 /* If no max_offset then lowest limit is 0. */
234 if (low_limit == (uintptr_t)r)
235 low_limit = 0;
236
237 for (i = 0; i < r->num_entries; i++) {
238 uintptr_t start_addr;
239 const struct imd_entry *e = &r->entries[i];
240
241 if (e->magic != IMD_ENTRY_MAGIC)
242 return -1;
243
244 start_addr = (uintptr_t)relative_pointer(r, e->start_offset);
245 if (start_addr < low_limit)
246 return -1;
Aaron Durbincac50502015-03-24 23:14:46 -0500247 if (start_addr >= imdr->limit ||
248 (start_addr + e->size) > imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600249 return -1;
250 }
251
252 /* Set root pointer. */
Aaron Durbincac50502015-03-24 23:14:46 -0500253 imdr->r = r;
Aaron Durbin20686d82015-03-05 14:11:27 -0600254
255 return 0;
256}
257
Aaron Durbincac50502015-03-24 23:14:46 -0500258static const struct imd_entry *imdr_entry_find(const struct imdr *imdr,
259 uint32_t id)
Aaron Durbin20686d82015-03-05 14:11:27 -0600260{
261 struct imd_root *r;
262 struct imd_entry *e;
Aaron Durbincac50502015-03-24 23:14:46 -0500263 size_t i;
Aaron Durbin20686d82015-03-05 14:11:27 -0600264
Aaron Durbincac50502015-03-24 23:14:46 -0500265 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600266
Aaron Durbincac50502015-03-24 23:14:46 -0500267 if (r == NULL)
268 return NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600269
Aaron Durbincac50502015-03-24 23:14:46 -0500270 e = NULL;
271 /* Skip first entry covering the root. */
272 for (i = 1; i < r->num_entries; i++) {
273 if (id != r->entries[i].id)
274 continue;
275 e = &r->entries[i];
276 break;
277 }
278
279 return e;
280}
281
282static int imdr_limit_size(struct imdr *imdr, size_t max_size)
283{
284 struct imd_root *r;
285 ssize_t smax_size;
286 size_t root_size;
287
288 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600289 if (r == NULL)
290 return -1;
291
Aaron Durbincac50502015-03-24 23:14:46 -0500292 root_size = imdr->limit - (uintptr_t)r;
Aaron Durbin20686d82015-03-05 14:11:27 -0600293
Aaron Durbincac50502015-03-24 23:14:46 -0500294 if (max_size < root_size)
295 return -1;
Aaron Durbin20686d82015-03-05 14:11:27 -0600296
Aaron Durbincac50502015-03-24 23:14:46 -0500297 /* Take into account the root size. */
298 smax_size = max_size - root_size;
299 smax_size = -smax_size;
Aaron Durbin20686d82015-03-05 14:11:27 -0600300
Aaron Durbincac50502015-03-24 23:14:46 -0500301 r->max_offset = smax_size;
Aaron Durbin20686d82015-03-05 14:11:27 -0600302
303 return 0;
304}
305
Aaron Durbincac50502015-03-24 23:14:46 -0500306static size_t imdr_entry_size(const struct imdr *imdr,
307 const struct imd_entry *e)
308{
309 return e->size;
310}
311
312static void *imdr_entry_at(const struct imdr *imdr, const struct imd_entry *e)
313{
314 return relative_pointer(imdr_root(imdr), e->start_offset);
315}
316
Aaron Durbin20686d82015-03-05 14:11:27 -0600317static struct imd_entry *imd_entry_add_to_root(struct imd_root *r, uint32_t id,
318 size_t size)
319{
320 struct imd_entry *entry;
321 struct imd_entry *last_entry;
322 ssize_t e_offset;
323 size_t used_size;
324
325 if (r->num_entries == r->max_entries)
326 return NULL;
327
328 /* Determine total size taken up by entry. */
329 used_size = ALIGN_UP(size, r->entry_align);
330
Aaron Durbin20686d82015-03-05 14:11:27 -0600331 /* See if size overflows imd total size. */
Aaron Durbincac50502015-03-24 23:14:46 -0500332 if (used_size > imd_root_data_left(r))
333 return NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600334
335 /*
336 * Determine if offset field overflows. All offsets should be lower
337 * than the previous one.
338 */
Aaron Durbincac50502015-03-24 23:14:46 -0500339 last_entry = root_last_entry(r);
Aaron Durbin20686d82015-03-05 14:11:27 -0600340 e_offset = last_entry->start_offset;
341 e_offset -= (ssize_t)used_size;
342 if (e_offset > last_entry->start_offset)
343 return NULL;
344
345 entry = root_last_entry(r) + 1;
346 r->num_entries++;
347
348 imd_entry_assign(entry, id, e_offset, size);
349
350 return entry;
351}
352
Aaron Durbincac50502015-03-24 23:14:46 -0500353static const struct imd_entry *imdr_entry_add(const struct imdr *imdr,
354 uint32_t id, size_t size)
Aaron Durbin20686d82015-03-05 14:11:27 -0600355{
356 struct imd_root *r;
357
Aaron Durbincac50502015-03-24 23:14:46 -0500358 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600359
360 if (r == NULL)
361 return NULL;
362
363 if (root_is_locked(r))
364 return NULL;
365
366 return imd_entry_add_to_root(r, id, size);
367}
368
Aaron Durbincac50502015-03-24 23:14:46 -0500369static bool imdr_has_entry(const struct imdr *imdr, const struct imd_entry *e)
370{
371 struct imd_root *r;
372 size_t idx;
373
374 r = imdr_root(imdr);
375 if (r == NULL)
376 return false;
377
378 /* Determine if the entry is within this root structure. */
379 idx = e - &r->entries[0];
380 if (idx >= r->num_entries)
381 return false;
382
383 return true;
384}
385
386static const struct imdr *imd_entry_to_imdr(const struct imd *imd,
387 const struct imd_entry *entry)
388{
389 if (imdr_has_entry(&imd->lg, entry))
390 return &imd->lg;
391
392 if (imdr_has_entry(&imd->sm, entry))
393 return &imd->sm;
394
395 return NULL;
396}
397
398/* Initialize imd handle. */
399void imd_handle_init(struct imd *imd, void *upper_limit)
400{
401 imdr_init(&imd->lg, upper_limit);
402 imdr_init(&imd->sm, NULL);
403}
404
405void imd_handle_init_partial_recovery(struct imd *imd)
406{
407 const struct imd_entry *e;
408 struct imd_root_pointer *rp;
409 struct imdr *imdr;
410
Aaron Durbin01562b62015-05-11 14:19:37 -0500411 if (imd->lg.limit == 0)
412 return;
413
Aaron Durbincac50502015-03-24 23:14:46 -0500414 imd_handle_init(imd, (void *)imd->lg.limit);
415
416 /* Initialize root pointer for the large regions. */
417 imdr = &imd->lg;
418 rp = imdr_get_root_pointer(imdr);
419 imdr->r = relative_pointer(rp, rp->root_offset);
420
421 e = imdr_entry_find(imdr, SMALL_REGION_ID);
422
423 if (e == NULL)
424 return;
425
426 imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e);
427 imd->sm.limit += imdr_entry_size(imdr, e);
428 imdr = &imd->sm;
429 rp = imdr_get_root_pointer(imdr);
430 imdr->r = relative_pointer(rp, rp->root_offset);
431}
432
433int imd_create_empty(struct imd *imd, size_t root_size, size_t entry_align)
434{
435 return imdr_create_empty(&imd->lg, root_size, entry_align);
436}
437
438int imd_create_tiered_empty(struct imd *imd,
439 size_t lg_root_size, size_t lg_entry_align,
440 size_t sm_root_size, size_t sm_entry_align)
441{
442 size_t sm_region_size;;
443 const struct imd_entry *e;
444 struct imdr *imdr;
445
446 imdr = &imd->lg;
447
448 if (imdr_create_empty(imdr, lg_root_size, lg_entry_align) != 0)
449 return -1;
450
451 /* Calculate the size of the small region to request. */
452 sm_region_size = root_num_entries(sm_root_size) * sm_entry_align;
453 sm_region_size += sm_root_size;
454 sm_region_size = ALIGN_UP(sm_region_size, lg_entry_align);
455
456 /* Add a new entry to the large region to cover the root and entries. */
457 e = imdr_entry_add(imdr, SMALL_REGION_ID, sm_region_size);
458
459 if (e == NULL)
460 goto fail;
461
462 imd->sm.limit = (uintptr_t)imdr_entry_at(imdr, e);
463 imd->sm.limit += sm_region_size;
464
465 if (imdr_create_empty(&imd->sm, sm_root_size, sm_entry_align) != 0 ||
466 imdr_limit_size(&imd->sm, sm_region_size))
467 goto fail;
468
469 return 0;
470fail:
471 imd_handle_init(imd, (void *)imdr->limit);
472 return -1;
473}
474
475int imd_recover(struct imd *imd)
476{
477 const struct imd_entry *e;
478 uintptr_t small_upper_limit;
479 struct imdr *imdr;
480
481 imdr = &imd->lg;
482 if (imdr_recover(imdr) != 0)
483 return -1;
484
485 /* Determine if small region is region is present. */
486 e = imdr_entry_find(imdr, SMALL_REGION_ID);
487
488 if (e == NULL)
489 return 0;
490
491 small_upper_limit = (uintptr_t)imdr_entry_at(imdr, e);
492 small_upper_limit += imdr_entry_size(imdr, e);
493
494 imd->sm.limit = small_upper_limit;
495
496 /* Tear down any changes on failure. */
497 if (imdr_recover(&imd->sm) != 0) {
498 imd_handle_init(imd, (void *)imd->lg.limit);
499 return -1;
500 }
501
502 return 0;
503}
504
505int imd_limit_size(struct imd *imd, size_t max_size)
506{
507 return imdr_limit_size(&imd->lg, max_size);
508}
509
510int imd_lockdown(struct imd *imd)
511{
512 struct imd_root *r;
513
514 r = imdr_root(&imd->lg);
515 if (r == NULL)
516 return -1;
517
518 r->flags |= IMD_FLAG_LOCKED;
519
520 r = imdr_root(&imd->sm);
521 if (r != NULL)
522 r->flags |= IMD_FLAG_LOCKED;
523
524 return 0;
525}
526
527int imd_region_used(struct imd *imd, void **base, size_t *size)
Aaron Durbin20686d82015-03-05 14:11:27 -0600528{
529 struct imd_root *r;
530 struct imd_entry *e;
Aaron Durbincac50502015-03-24 23:14:46 -0500531 void *low_addr;
532 size_t sz_used;
Aaron Durbin20686d82015-03-05 14:11:27 -0600533
Aaron Durbincac50502015-03-24 23:14:46 -0500534 if (!imd->lg.limit)
535 return -1;
536
537 r = imdr_root(&imd->lg);
Aaron Durbin20686d82015-03-05 14:11:27 -0600538
539 if (r == NULL)
Aaron Durbincac50502015-03-24 23:14:46 -0500540 return -1;
Aaron Durbin20686d82015-03-05 14:11:27 -0600541
Aaron Durbincac50502015-03-24 23:14:46 -0500542 /* Use last entry to obtain lowest address. */
543 e = root_last_entry(r);
544
545 low_addr = relative_pointer(r, e->start_offset);
546
547 /* Total size used is the last entry's base up to the limit. */
548 sz_used = imd->lg.limit - (uintptr_t)low_addr;
549
550 *base = low_addr;
551 *size = sz_used;
552
553 return 0;
554}
555
556const struct imd_entry *imd_entry_add(const struct imd *imd, uint32_t id,
557 size_t size)
558{
559 struct imd_root *r;
560 const struct imdr *imdr;
561 const struct imd_entry *e = NULL;
562
563 /*
564 * Determine if requested size is less than 1/4 of small data
565 * region is left.
566 */
567 imdr = &imd->sm;
568 r = imdr_root(imdr);
569
570 /* No small region. Use the large region. */
571 if (r == NULL)
572 return imdr_entry_add(&imd->lg, id, size);
573 else if (size <= r->entry_align || size <= imd_root_data_left(r) / 4)
574 e = imdr_entry_add(imdr, id, size);
575
576 /* Fall back on large region allocation. */
577 if (e == NULL)
578 e = imdr_entry_add(&imd->lg, id, size);
579
580 return e;
581}
582
583const struct imd_entry *imd_entry_find(const struct imd *imd, uint32_t id)
584{
585 const struct imd_entry *e;
586
587 /* Many of the smaller allocations are used a lot. Therefore, try
588 * the small region first. */
589 e = imdr_entry_find(&imd->sm, id);
590
591 if (e == NULL)
592 e = imdr_entry_find(&imd->lg, id);
Aaron Durbin20686d82015-03-05 14:11:27 -0600593
594 return e;
595}
596
597const struct imd_entry *imd_entry_find_or_add(const struct imd *imd,
598 uint32_t id, size_t size)
599{
600 const struct imd_entry *e;
601
602 e = imd_entry_find(imd, id);
603
604 if (e != NULL)
605 return e;
606
607 return imd_entry_add(imd, id, size);
608}
609
610size_t imd_entry_size(const struct imd *imd, const struct imd_entry *entry)
611{
Aaron Durbincac50502015-03-24 23:14:46 -0500612 return imdr_entry_size(NULL, entry);
Aaron Durbin20686d82015-03-05 14:11:27 -0600613}
614
615void *imd_entry_at(const struct imd *imd, const struct imd_entry *entry)
616{
Aaron Durbincac50502015-03-24 23:14:46 -0500617 const struct imdr *imdr;
Aaron Durbin20686d82015-03-05 14:11:27 -0600618
Aaron Durbincac50502015-03-24 23:14:46 -0500619 imdr = imd_entry_to_imdr(imd, entry);
Aaron Durbin20686d82015-03-05 14:11:27 -0600620
Aaron Durbincac50502015-03-24 23:14:46 -0500621 if (imdr == NULL)
Aaron Durbin20686d82015-03-05 14:11:27 -0600622 return NULL;
623
Aaron Durbincac50502015-03-24 23:14:46 -0500624 return imdr_entry_at(imdr, entry);
Aaron Durbin20686d82015-03-05 14:11:27 -0600625}
626
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500627uint32_t imd_entry_id(const struct imd *imd, const struct imd_entry *entry)
628{
629 return entry->id;
630}
631
Aaron Durbin20686d82015-03-05 14:11:27 -0600632int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry)
633{
634 struct imd_root *r;
Aaron Durbincac50502015-03-24 23:14:46 -0500635 const struct imdr *imdr;
Aaron Durbin20686d82015-03-05 14:11:27 -0600636
Aaron Durbincac50502015-03-24 23:14:46 -0500637 imdr = imd_entry_to_imdr(imd, entry);
638
639 if (imdr == NULL)
Lee Leahy35af5c42017-03-09 17:35:28 -0800640 return -1;
Aaron Durbincac50502015-03-24 23:14:46 -0500641
642 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600643
644 if (r == NULL)
645 return -1;
646
647 if (root_is_locked(r))
648 return -1;
649
650 if (entry != root_last_entry(r))
651 return -1;
652
653 r->num_entries--;
654
655 return 0;
656}
657
Aaron Durbincac50502015-03-24 23:14:46 -0500658static void imdr_print_entries(const struct imdr *imdr, const char *indent,
659 const struct imd_lookup *lookup, size_t size)
Aaron Durbin20686d82015-03-05 14:11:27 -0600660{
661 struct imd_root *r;
662 size_t i;
663 size_t j;
664
Aaron Durbincac50502015-03-24 23:14:46 -0500665 if (imdr == NULL)
666 return;
Aaron Durbin20686d82015-03-05 14:11:27 -0600667
Aaron Durbincac50502015-03-24 23:14:46 -0500668 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600669
670 for (i = 0; i < r->num_entries; i++) {
671 const char *name = NULL;
672 const struct imd_entry *e = &r->entries[i];
673
674 for (j = 0; j < size; j++) {
675 if (lookup[j].id == e->id) {
676 name = lookup[j].name;
677 break;
678 }
679 }
680
Aaron Durbincac50502015-03-24 23:14:46 -0500681 printk(BIOS_DEBUG, "%s", indent);
682
Aaron Durbin20686d82015-03-05 14:11:27 -0600683 if (name == NULL)
684 printk(BIOS_DEBUG, "%08x ", e->id);
685 else
686 printk(BIOS_DEBUG, "%s", name);
687 printk(BIOS_DEBUG, "%2zu. ", i);
Aaron Durbincac50502015-03-24 23:14:46 -0500688 printk(BIOS_DEBUG, "%p ", imdr_entry_at(imdr, e));
689 printk(BIOS_DEBUG, "%08zx\n", imdr_entry_size(imdr, e));
690 }
691}
692
693int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup,
694 size_t size)
695{
696 if (imdr_root(&imd->lg) == NULL)
697 return -1;
698
699 imdr_print_entries(&imd->lg, "", lookup, size);
700 if (imdr_root(&imd->sm) != NULL) {
701 printk(BIOS_DEBUG, "IMD small region:\n");
702 imdr_print_entries(&imd->sm, " ", lookup, size);
Aaron Durbin20686d82015-03-05 14:11:27 -0600703 }
704
705 return 0;
706}
Aaron Durbin1ca2d862015-09-30 12:26:54 -0500707
708int imd_cursor_init(const struct imd *imd, struct imd_cursor *cursor)
709{
710 if (imd == NULL || cursor == NULL)
711 return -1;
712
713 memset(cursor, 0, sizeof(*cursor));
714
715 cursor->imdr[0] = &imd->lg;
716 cursor->imdr[1] = &imd->sm;
717
718 return 0;
719}
720
721const struct imd_entry *imd_cursor_next(struct imd_cursor *cursor)
722{
723 struct imd_root *r;
724 const struct imd_entry *e;
725
726 if (cursor->current_imdr >= ARRAY_SIZE(cursor->imdr))
727 return NULL;
728
729 r = imdr_root(cursor->imdr[cursor->current_imdr]);
730
731 if (r == NULL)
732 return NULL;
733
734 if (cursor->current_entry >= r->num_entries) {
735 /* Try next imdr. */
736 cursor->current_imdr++;
737 cursor->current_entry = 0;
738 return imd_cursor_next(cursor);
739 }
740
741 e = &r->entries[cursor->current_entry];
742 cursor->current_entry++;
743
744 return e;
745}