GRUB-1.98 changes
[grub2/jjazz.git] / lib / relocator.c
blob6a5acc5486fb331bfff5109c857b735b99e1f96d
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009 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 #define MAX_OVERHEAD ((RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
20 + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) \
21 + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
22 + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN))
23 #define PRE_REGION_SIZE (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
25 void *
26 PREFIX (alloc) (grub_size_t size)
28 char *playground;
30 playground = grub_malloc (size + MAX_OVERHEAD);
31 if (!playground)
32 return 0;
34 *(grub_size_t *) playground = size;
36 return playground + PRE_REGION_SIZE;
39 void *
40 PREFIX (realloc) (void *relocator, grub_size_t size)
42 char *playground;
44 if (!relocator)
45 return PREFIX (alloc) (size);
47 playground = (char *) relocator - PRE_REGION_SIZE;
49 playground = grub_realloc (playground, size + MAX_OVERHEAD);
50 if (!playground)
51 return 0;
53 *(grub_size_t *) playground = size;
55 return playground + PRE_REGION_SIZE;
58 void
59 PREFIX(free) (void *relocator)
61 if (relocator)
62 grub_free ((char *) relocator - PRE_REGION_SIZE);
65 grub_err_t
66 PREFIX (boot) (void *relocator, grub_uint32_t dest,
67 struct grub_relocator32_state state)
69 grub_size_t size;
70 char *playground;
72 playground = (char *) relocator - PRE_REGION_SIZE;
73 size = *(grub_size_t *) playground;
75 grub_dprintf ("relocator",
76 "Relocator: source: %p, destination: 0x%x, size: 0x%lx\n",
77 relocator, (unsigned) dest, (unsigned long) size);
79 /* Very unlikely condition: Relocator may risk overwrite itself.
80 Just move it a bit up. */
81 if ((grub_addr_t) dest < (grub_addr_t) relocator
82 + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
83 && (grub_addr_t) dest + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
84 > (grub_addr_t) relocator)
86 void *relocator_new = ((grub_uint8_t *) relocator)
87 + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
88 + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN);
89 grub_dprintf ("relocator", "Overwrite condition detected moving "
90 "relocator from %p to %p\n", relocator, relocator_new);
91 grub_memmove (relocator_new, relocator,
92 (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
93 + size
94 + (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN));
95 relocator = relocator_new;
98 if ((grub_addr_t) dest >= (grub_addr_t) relocator)
100 int overhead;
101 overhead = dest -
102 ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN,
103 RELOCATOR_ALIGN);
104 grub_dprintf ("relocator",
105 "Backward relocator: code %p, source: %p, "
106 "destination: 0x%x, size: 0x%lx\n",
107 (char *) relocator - overhead,
108 (char *) relocator - overhead,
109 (unsigned) dest - overhead,
110 (unsigned long) size + overhead);
112 write_call_relocator_bw ((char *) relocator - overhead,
113 (char *) relocator - overhead,
114 dest - overhead, size + overhead, state);
116 else
118 int overhead;
120 overhead = ALIGN_UP (dest + size, RELOCATOR_ALIGN)
121 + RELOCATOR_SIZEOF (forward) - (dest + size);
122 grub_dprintf ("relocator",
123 "Forward relocator: code %p, source: %p, "
124 "destination: 0x%x, size: 0x%lx\n",
125 (char *) relocator + size + overhead
126 - RELOCATOR_SIZEOF (forward),
127 relocator, (unsigned) dest,
128 (unsigned long) size + overhead);
130 write_call_relocator_fw ((char *) relocator + size + overhead
131 - RELOCATOR_SIZEOF (forward),
132 relocator, dest, size + overhead, state);
135 /* Not reached. */
136 return GRUB_ERR_NONE;