blob: 02fc2b3363caf4cab11002fd5ebfde5a8cf50a11 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc.
18 */
19
20#include <assert.h>
21#include <cbmem.h>
22#include <console/console.h>
23#include <imd.h>
24#include <stdlib.h>
25#include <string.h>
26
27/* For more details on implementation and usage please see the imd.h header. */
28
29static const uint32_t IMD_ROOT_PTR_MAGIC = 0xc0389481;
30static const uint32_t IMD_ENTRY_MAGIC = ~0xc0389481;
Aaron Durbincac50502015-03-24 23:14:46 -050031static const uint32_t SMALL_REGION_ID = CBMEM_ID_IMD_SMALL;
Aaron Durbin20686d82015-03-05 14:11:27 -060032static const size_t LIMIT_ALIGN = 4096;
33
34/* In-memory data structures. */
35struct imd_root_pointer {
36 uint32_t magic;
37 /* Relative to upper limit/offset. */
38 int32_t root_offset;
39} __attribute__((packed));
40
41struct imd_entry {
42 uint32_t magic;
43 /* start is located relative to imd_root */
44 int32_t start_offset;
45 uint32_t size;
46 uint32_t id;
47} __attribute__((packed));
48
49struct imd_root {
50 uint32_t max_entries;
51 uint32_t num_entries;
52 uint32_t flags;
53 uint32_t entry_align;
54 /* Used for fixing the size of an imd. Relative to the root. */
55 int32_t max_offset;
56 struct imd_entry entries[0];
57} __attribute__((packed));
58
59#define IMD_FLAG_LOCKED 1
60
61static void *relative_pointer(void *base, ssize_t offset)
62{
63 intptr_t b = (intptr_t)base;
64 b += offset;
65 return (void *)b;
66}
67
68static bool imd_root_pointer_valid(const struct imd_root_pointer *rp)
69{
70 return !!(rp->magic == IMD_ROOT_PTR_MAGIC);
71}
72
Aaron Durbincac50502015-03-24 23:14:46 -050073static struct imd_root *imdr_root(const struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -060074{
Aaron Durbincac50502015-03-24 23:14:46 -050075 return imdr->r;
Aaron Durbin20686d82015-03-05 14:11:27 -060076}
77
78/*
79 * The root pointer is relative to the upper limit of the imd. i.e. It sits
80 * just below the upper limit.
81 */
Aaron Durbincac50502015-03-24 23:14:46 -050082static struct imd_root_pointer *imdr_get_root_pointer(const struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -060083{
84 struct imd_root_pointer *rp;
85
Aaron Durbincac50502015-03-24 23:14:46 -050086 rp = relative_pointer((void *)imdr->limit, -sizeof(*rp));
Aaron Durbin20686d82015-03-05 14:11:27 -060087
88 return rp;
89}
90
91static void imd_link_root(struct imd_root_pointer *rp, struct imd_root *r)
92{
93 rp->magic = IMD_ROOT_PTR_MAGIC;
94 rp->root_offset = (int32_t)((intptr_t)r - (intptr_t)rp);
95}
96
Aaron Durbincac50502015-03-24 23:14:46 -050097static struct imd_entry *root_last_entry(struct imd_root *r)
98{
99 return &r->entries[r->num_entries - 1];
100}
101
102static size_t root_num_entries(size_t root_size)
103{
104 size_t entries_size;
105
106 entries_size = root_size;
107 entries_size -= sizeof(struct imd_root_pointer);
108 entries_size -= sizeof(struct imd_root);
109
110 return entries_size / sizeof(struct imd_entry);
111}
112
113static size_t imd_root_data_left(struct imd_root *r)
114{
115 struct imd_entry *last_entry;
116
117 last_entry = root_last_entry(r);
118
119 if (r->max_offset != 0)
120 return last_entry->start_offset - r->max_offset;
121
122 return ~(size_t)0;
123}
124
125static bool root_is_locked(const struct imd_root *r)
126{
127 return !!(r->flags & IMD_FLAG_LOCKED);
128}
129
Aaron Durbin20686d82015-03-05 14:11:27 -0600130static void imd_entry_assign(struct imd_entry *e, uint32_t id,
131 ssize_t offset, size_t size)
132{
133 e->magic = IMD_ENTRY_MAGIC;
134 e->start_offset = offset;
135 e->size = size;
136 e->id = id;
137}
138
Aaron Durbincac50502015-03-24 23:14:46 -0500139static void imdr_init(struct imdr *ir, void *upper_limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600140{
141 uintptr_t limit = (uintptr_t)upper_limit;
142 /* Upper limit is aligned down to 4KiB */
Aaron Durbincac50502015-03-24 23:14:46 -0500143 ir->limit = ALIGN_DOWN(limit, LIMIT_ALIGN);
144 ir->r = NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600145}
146
Aaron Durbincac50502015-03-24 23:14:46 -0500147static int imdr_create_empty(struct imdr *imdr, size_t root_size,
148 size_t entry_align)
Aaron Durbin20686d82015-03-05 14:11:27 -0600149{
150 struct imd_root_pointer *rp;
151 struct imd_root *r;
152 struct imd_entry *e;
153 ssize_t root_offset;
Aaron Durbin20686d82015-03-05 14:11:27 -0600154
Aaron Durbincac50502015-03-24 23:14:46 -0500155 if (!imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600156 return -1;
157
158 /* root_size and entry_align should be a power of 2. */
159 assert(IS_POWER_OF_2(root_size));
160 assert(IS_POWER_OF_2(entry_align));
161
Aaron Durbincac50502015-03-24 23:14:46 -0500162 if (!imdr->limit)
163 return -1;
164
Aaron Durbin20686d82015-03-05 14:11:27 -0600165 /*
166 * root_size needs to be large enough to accomodate root pointer and
167 * root book keeping structure. The caller needs to ensure there's
168 * enough room for tracking individual allocations.
169 */
170 if (root_size < (sizeof(*rp) + sizeof(*r)))
171 return -1;
172
173 /* For simplicity don't allow sizes or alignments to exceed LIMIT_ALIGN. */
174 if (root_size > LIMIT_ALIGN || entry_align > LIMIT_ALIGN)
175 return -1;
176
177 /* Additionally, don't handle an entry alignment > root_size. */
178 if (entry_align > root_size)
179 return -1;
180
Aaron Durbincac50502015-03-24 23:14:46 -0500181 rp = imdr_get_root_pointer(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600182
183 root_offset = -(ssize_t)root_size;
184 /* Set root pointer. */
Aaron Durbincac50502015-03-24 23:14:46 -0500185 imdr->r = relative_pointer((void *)imdr->limit, root_offset);
186 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600187 imd_link_root(rp, r);
188
189 memset(r, 0, sizeof(*r));
190 r->entry_align = entry_align;
191
192 /* Calculate size left for entries. */
Aaron Durbincac50502015-03-24 23:14:46 -0500193 r->max_entries = root_num_entries(root_size);
Aaron Durbin20686d82015-03-05 14:11:27 -0600194
195 /* Fill in first entry covering the root region. */
196 r->num_entries = 1;
197 e = &r->entries[0];
198 imd_entry_assign(e, CBMEM_ID_IMD_ROOT, 0, root_size);
199
200 printk(BIOS_DEBUG, "IMD: root @ %p %u entries.\n", r, r->max_entries);
201
202 return 0;
203}
204
Aaron Durbincac50502015-03-24 23:14:46 -0500205static int imdr_recover(struct imdr *imdr)
Aaron Durbin20686d82015-03-05 14:11:27 -0600206{
207 struct imd_root_pointer *rp;
208 struct imd_root *r;
209 uintptr_t low_limit;
210 size_t i;
211
Aaron Durbincac50502015-03-24 23:14:46 -0500212 if (!imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600213 return -1;
214
Aaron Durbincac50502015-03-24 23:14:46 -0500215 rp = imdr_get_root_pointer(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600216
217 if (!imd_root_pointer_valid(rp))
218 return -1;
219
220 r = relative_pointer(rp, rp->root_offset);
221
222 /* Confirm the root and root pointer are just under the limit. */
223 if (ALIGN_UP((uintptr_t)&r->entries[r->max_entries], LIMIT_ALIGN) !=
Aaron Durbincac50502015-03-24 23:14:46 -0500224 imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600225 return -1;
226
227 if (r->num_entries > r->max_entries)
228 return -1;
229
230 /* Entry alignment should be power of 2. */
231 if (!IS_POWER_OF_2(r->entry_align))
232 return -1;
233
234 low_limit = (uintptr_t)relative_pointer(r, r->max_offset);
235
236 /* If no max_offset then lowest limit is 0. */
237 if (low_limit == (uintptr_t)r)
238 low_limit = 0;
239
240 for (i = 0; i < r->num_entries; i++) {
241 uintptr_t start_addr;
242 const struct imd_entry *e = &r->entries[i];
243
244 if (e->magic != IMD_ENTRY_MAGIC)
245 return -1;
246
247 start_addr = (uintptr_t)relative_pointer(r, e->start_offset);
248 if (start_addr < low_limit)
249 return -1;
Aaron Durbincac50502015-03-24 23:14:46 -0500250 if (start_addr >= imdr->limit ||
251 (start_addr + e->size) > imdr->limit)
Aaron Durbin20686d82015-03-05 14:11:27 -0600252 return -1;
253 }
254
255 /* Set root pointer. */
Aaron Durbincac50502015-03-24 23:14:46 -0500256 imdr->r = r;
Aaron Durbin20686d82015-03-05 14:11:27 -0600257
258 return 0;
259}
260
Aaron Durbincac50502015-03-24 23:14:46 -0500261static const struct imd_entry *imdr_entry_find(const struct imdr *imdr,
262 uint32_t id)
Aaron Durbin20686d82015-03-05 14:11:27 -0600263{
264 struct imd_root *r;
265 struct imd_entry *e;
Aaron Durbincac50502015-03-24 23:14:46 -0500266 size_t i;
Aaron Durbin20686d82015-03-05 14:11:27 -0600267
Aaron Durbincac50502015-03-24 23:14:46 -0500268 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600269
Aaron Durbincac50502015-03-24 23:14:46 -0500270 if (r == NULL)
271 return NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600272
Aaron Durbincac50502015-03-24 23:14:46 -0500273 e = NULL;
274 /* Skip first entry covering the root. */
275 for (i = 1; i < r->num_entries; i++) {
276 if (id != r->entries[i].id)
277 continue;
278 e = &r->entries[i];
279 break;
280 }
281
282 return e;
283}
284
285static int imdr_limit_size(struct imdr *imdr, size_t max_size)
286{
287 struct imd_root *r;
288 ssize_t smax_size;
289 size_t root_size;
290
291 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600292 if (r == NULL)
293 return -1;
294
Aaron Durbincac50502015-03-24 23:14:46 -0500295 root_size = imdr->limit - (uintptr_t)r;
Aaron Durbin20686d82015-03-05 14:11:27 -0600296
Aaron Durbincac50502015-03-24 23:14:46 -0500297 if (max_size < root_size)
298 return -1;
Aaron Durbin20686d82015-03-05 14:11:27 -0600299
Aaron Durbincac50502015-03-24 23:14:46 -0500300 /* Take into account the root size. */
301 smax_size = max_size - root_size;
302 smax_size = -smax_size;
Aaron Durbin20686d82015-03-05 14:11:27 -0600303
Aaron Durbincac50502015-03-24 23:14:46 -0500304 r->max_offset = smax_size;
Aaron Durbin20686d82015-03-05 14:11:27 -0600305
306 return 0;
307}
308
Aaron Durbincac50502015-03-24 23:14:46 -0500309static size_t imdr_entry_size(const struct imdr *imdr,
310 const struct imd_entry *e)
311{
312 return e->size;
313}
314
315static void *imdr_entry_at(const struct imdr *imdr, const struct imd_entry *e)
316{
317 return relative_pointer(imdr_root(imdr), e->start_offset);
318}
319
Aaron Durbin20686d82015-03-05 14:11:27 -0600320static struct imd_entry *imd_entry_add_to_root(struct imd_root *r, uint32_t id,
321 size_t size)
322{
323 struct imd_entry *entry;
324 struct imd_entry *last_entry;
325 ssize_t e_offset;
326 size_t used_size;
327
328 if (r->num_entries == r->max_entries)
329 return NULL;
330
331 /* Determine total size taken up by entry. */
332 used_size = ALIGN_UP(size, r->entry_align);
333
Aaron Durbin20686d82015-03-05 14:11:27 -0600334 /* See if size overflows imd total size. */
Aaron Durbincac50502015-03-24 23:14:46 -0500335 if (used_size > imd_root_data_left(r))
336 return NULL;
Aaron Durbin20686d82015-03-05 14:11:27 -0600337
338 /*
339 * Determine if offset field overflows. All offsets should be lower
340 * than the previous one.
341 */
Aaron Durbincac50502015-03-24 23:14:46 -0500342 last_entry = root_last_entry(r);
Aaron Durbin20686d82015-03-05 14:11:27 -0600343 e_offset = last_entry->start_offset;
344 e_offset -= (ssize_t)used_size;
345 if (e_offset > last_entry->start_offset)
346 return NULL;
347
348 entry = root_last_entry(r) + 1;
349 r->num_entries++;
350
351 imd_entry_assign(entry, id, e_offset, size);
352
353 return entry;
354}
355
Aaron Durbincac50502015-03-24 23:14:46 -0500356static const struct imd_entry *imdr_entry_add(const struct imdr *imdr,
357 uint32_t id, size_t size)
Aaron Durbin20686d82015-03-05 14:11:27 -0600358{
359 struct imd_root *r;
360
Aaron Durbincac50502015-03-24 23:14:46 -0500361 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600362
363 if (r == NULL)
364 return NULL;
365
366 if (root_is_locked(r))
367 return NULL;
368
369 return imd_entry_add_to_root(r, id, size);
370}
371
Aaron Durbincac50502015-03-24 23:14:46 -0500372static bool imdr_has_entry(const struct imdr *imdr, const struct imd_entry *e)
373{
374 struct imd_root *r;
375 size_t idx;
376
377 r = imdr_root(imdr);
378 if (r == NULL)
379 return false;
380
381 /* Determine if the entry is within this root structure. */
382 idx = e - &r->entries[0];
383 if (idx >= r->num_entries)
384 return false;
385
386 return true;
387}
388
389static const struct imdr *imd_entry_to_imdr(const struct imd *imd,
390 const struct imd_entry *entry)
391{
392 if (imdr_has_entry(&imd->lg, entry))
393 return &imd->lg;
394
395 if (imdr_has_entry(&imd->sm, entry))
396 return &imd->sm;
397
398 return NULL;
399}
400
401/* Initialize imd handle. */
402void imd_handle_init(struct imd *imd, void *upper_limit)
403{
404 imdr_init(&imd->lg, upper_limit);
405 imdr_init(&imd->sm, NULL);
406}
407
408void imd_handle_init_partial_recovery(struct imd *imd)
409{
410 const struct imd_entry *e;
411 struct imd_root_pointer *rp;
412 struct imdr *imdr;
413
414 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
627int imd_entry_remove(const struct imd *imd, const struct imd_entry *entry)
628{
629 struct imd_root *r;
Aaron Durbincac50502015-03-24 23:14:46 -0500630 const struct imdr *imdr;
Aaron Durbin20686d82015-03-05 14:11:27 -0600631
Aaron Durbincac50502015-03-24 23:14:46 -0500632 imdr = imd_entry_to_imdr(imd, entry);
633
634 if (imdr == NULL)
635 return - 1;
636
637 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600638
639 if (r == NULL)
640 return -1;
641
642 if (root_is_locked(r))
643 return -1;
644
645 if (entry != root_last_entry(r))
646 return -1;
647
648 r->num_entries--;
649
650 return 0;
651}
652
Aaron Durbincac50502015-03-24 23:14:46 -0500653static void imdr_print_entries(const struct imdr *imdr, const char *indent,
654 const struct imd_lookup *lookup, size_t size)
Aaron Durbin20686d82015-03-05 14:11:27 -0600655{
656 struct imd_root *r;
657 size_t i;
658 size_t j;
659
Aaron Durbincac50502015-03-24 23:14:46 -0500660 if (imdr == NULL)
661 return;
Aaron Durbin20686d82015-03-05 14:11:27 -0600662
Aaron Durbincac50502015-03-24 23:14:46 -0500663 r = imdr_root(imdr);
Aaron Durbin20686d82015-03-05 14:11:27 -0600664
665 for (i = 0; i < r->num_entries; i++) {
666 const char *name = NULL;
667 const struct imd_entry *e = &r->entries[i];
668
669 for (j = 0; j < size; j++) {
670 if (lookup[j].id == e->id) {
671 name = lookup[j].name;
672 break;
673 }
674 }
675
Aaron Durbincac50502015-03-24 23:14:46 -0500676 printk(BIOS_DEBUG, "%s", indent);
677
Aaron Durbin20686d82015-03-05 14:11:27 -0600678 if (name == NULL)
679 printk(BIOS_DEBUG, "%08x ", e->id);
680 else
681 printk(BIOS_DEBUG, "%s", name);
682 printk(BIOS_DEBUG, "%2zu. ", i);
Aaron Durbincac50502015-03-24 23:14:46 -0500683 printk(BIOS_DEBUG, "%p ", imdr_entry_at(imdr, e));
684 printk(BIOS_DEBUG, "%08zx\n", imdr_entry_size(imdr, e));
685 }
686}
687
688int imd_print_entries(const struct imd *imd, const struct imd_lookup *lookup,
689 size_t size)
690{
691 if (imdr_root(&imd->lg) == NULL)
692 return -1;
693
694 imdr_print_entries(&imd->lg, "", lookup, size);
695 if (imdr_root(&imd->sm) != NULL) {
696 printk(BIOS_DEBUG, "IMD small region:\n");
697 imdr_print_entries(&imd->sm, " ", lookup, size);
Aaron Durbin20686d82015-03-05 14:11:27 -0600698 }
699
700 return 0;
701}