tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / grub-core / lib / ieee1275 / relocator.c
blob918392f1e54cdfb42670a16ab2c2b8098e34b08a
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB 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.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/relocator.h>
20 #include <grub/relocator_private.h>
21 #include <grub/memory.h>
22 #include <grub/ieee1275/ieee1275.h>
24 /* Helper for grub_relocator_firmware_get_max_events. */
25 static int
26 count (grub_uint64_t addr __attribute__ ((unused)),
27 grub_uint64_t len __attribute__ ((unused)),
28 grub_memory_type_t type __attribute__ ((unused)), void *data)
30 int *counter = data;
32 (*counter)++;
33 return 0;
36 unsigned
37 grub_relocator_firmware_get_max_events (void)
39 int counter = 0;
41 grub_machine_mmap_iterate (count, &counter);
42 return 2 * counter;
45 /* Context for grub_relocator_firmware_fill_events. */
46 struct grub_relocator_firmware_fill_events_ctx
48 struct grub_relocator_mmap_event *events;
49 int counter;
52 /* Helper for grub_relocator_firmware_fill_events. */
53 static int
54 grub_relocator_firmware_fill_events_iter (grub_uint64_t addr,
55 grub_uint64_t len,
56 grub_memory_type_t type, void *data)
58 struct grub_relocator_firmware_fill_events_ctx *ctx = data;
60 if (type != GRUB_MEMORY_AVAILABLE)
61 return 0;
63 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
65 if (addr + len <= 0x180000)
66 return 0;
68 if (addr < 0x180000)
70 len = addr + len - 0x180000;
71 addr = 0x180000;
75 ctx->events[ctx->counter].type = REG_FIRMWARE_START;
76 ctx->events[ctx->counter].pos = addr;
77 ctx->counter++;
78 ctx->events[ctx->counter].type = REG_FIRMWARE_END;
79 ctx->events[ctx->counter].pos = addr + len;
80 ctx->counter++;
82 return 0;
85 unsigned
86 grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
88 struct grub_relocator_firmware_fill_events_ctx ctx = {
89 .events = events,
90 .counter = 0
93 grub_machine_mmap_iterate (grub_relocator_firmware_fill_events_iter, &ctx);
94 return ctx.counter;
97 int
98 grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
100 grub_err_t err;
101 err = grub_claimmap (start, size);
102 grub_errno = 0;
103 return (err == 0);
106 void
107 grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
109 grub_ieee1275_release (start, size);