~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/lib/i386/relocator.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2009  Free Software Foundation, Inc.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include <grub/mm.h>
 
20
#include <grub/misc.h>
 
21
 
 
22
#include <grub/types.h>
 
23
#include <grub/err.h>
 
24
#include <grub/term.h>
 
25
 
 
26
#include <grub/i386/relocator.h>
 
27
#include <grub/relocator_private.h>
 
28
 
 
29
extern grub_uint8_t grub_relocator_forward_start;
 
30
extern grub_uint8_t grub_relocator_forward_end;
 
31
extern grub_uint8_t grub_relocator_backward_start;
 
32
extern grub_uint8_t grub_relocator_backward_end;
 
33
 
 
34
extern void *grub_relocator_backward_dest;
 
35
extern void *grub_relocator_backward_src;
 
36
extern grub_size_t grub_relocator_backward_chunk_size;
 
37
 
 
38
extern void *grub_relocator_forward_dest;
 
39
extern void *grub_relocator_forward_src;
 
40
extern grub_size_t grub_relocator_forward_chunk_size;
 
41
 
 
42
extern grub_uint8_t grub_relocator16_start;
 
43
extern grub_uint8_t grub_relocator16_end;
 
44
extern grub_uint16_t grub_relocator16_cs;
 
45
extern grub_uint16_t grub_relocator16_ip;
 
46
extern grub_uint16_t grub_relocator16_ds;
 
47
extern grub_uint16_t grub_relocator16_es;
 
48
extern grub_uint16_t grub_relocator16_fs;
 
49
extern grub_uint16_t grub_relocator16_gs;
 
50
extern grub_uint16_t grub_relocator16_ss;
 
51
extern grub_uint16_t grub_relocator16_sp;
 
52
extern grub_uint32_t grub_relocator16_edx;
 
53
 
 
54
extern grub_uint8_t grub_relocator32_start;
 
55
extern grub_uint8_t grub_relocator32_end;
 
56
extern grub_uint32_t grub_relocator32_eax;
 
57
extern grub_uint32_t grub_relocator32_ebx;
 
58
extern grub_uint32_t grub_relocator32_ecx;
 
59
extern grub_uint32_t grub_relocator32_edx;
 
60
extern grub_uint32_t grub_relocator32_eip;
 
61
extern grub_uint32_t grub_relocator32_esp;
 
62
extern grub_uint32_t grub_relocator32_ebp;
 
63
extern grub_uint32_t grub_relocator32_esi;
 
64
extern grub_uint32_t grub_relocator32_edi;
 
65
 
 
66
extern grub_uint8_t grub_relocator64_start;
 
67
extern grub_uint8_t grub_relocator64_end;
 
68
extern grub_uint64_t grub_relocator64_rax;
 
69
extern grub_uint64_t grub_relocator64_rbx;
 
70
extern grub_uint64_t grub_relocator64_rcx;
 
71
extern grub_uint64_t grub_relocator64_rdx;
 
72
extern grub_uint64_t grub_relocator64_rip;
 
73
extern grub_uint64_t grub_relocator64_rip_addr;
 
74
extern grub_uint64_t grub_relocator64_rsp;
 
75
extern grub_uint64_t grub_relocator64_rsi;
 
76
extern grub_addr_t grub_relocator64_cr3;
 
77
 
 
78
#define RELOCATOR_SIZEOF(x)     (&grub_relocator##x##_end - &grub_relocator##x##_start)
 
79
 
 
80
grub_size_t grub_relocator_align = 1;
 
81
grub_size_t grub_relocator_forward_size;
 
82
grub_size_t grub_relocator_backward_size;
 
83
#ifdef __x86_64__
 
84
grub_size_t grub_relocator_jumper_size = 12;
 
85
#else
 
86
grub_size_t grub_relocator_jumper_size = 7;
 
87
#endif
 
88
 
 
89
void
 
90
grub_cpu_relocator_init (void)
 
91
{
 
92
  grub_relocator_forward_size = RELOCATOR_SIZEOF(_forward);
 
93
  grub_relocator_backward_size = RELOCATOR_SIZEOF(_backward);
 
94
}
 
95
 
 
96
void
 
97
grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
 
98
{
 
99
  grub_uint8_t *ptr;
 
100
  ptr = rels;
 
101
#ifdef __x86_64__
 
102
  /* movq imm64, %rax (for relocator) */
 
103
  *(grub_uint8_t *) ptr = 0x48;
 
104
  ptr++;
 
105
  *(grub_uint8_t *) ptr = 0xb8;
 
106
  ptr++;
 
107
  *(grub_uint64_t *) ptr = addr;
 
108
  ptr += sizeof (grub_uint64_t);
 
109
#else
 
110
  /* movl imm32, %eax (for relocator) */
 
111
  *(grub_uint8_t *) ptr = 0xb8;
 
112
  ptr++;
 
113
  *(grub_uint32_t *) ptr = addr;
 
114
  ptr += sizeof (grub_uint32_t);
 
115
#endif
 
116
  /* jmp $eax/$rax */
 
117
  *(grub_uint8_t *) ptr = 0xff;
 
118
  ptr++;
 
119
  *(grub_uint8_t *) ptr = 0xe0;
 
120
  ptr++;
 
121
}
 
122
 
 
123
void
 
124
grub_cpu_relocator_backward (void *ptr, void *src, void *dest,
 
125
                             grub_size_t size)
 
126
{
 
127
  grub_relocator_backward_dest = dest;
 
128
  grub_relocator_backward_src = src;
 
129
  grub_relocator_backward_chunk_size = size;
 
130
 
 
131
  grub_memmove (ptr,
 
132
                &grub_relocator_backward_start,
 
133
                RELOCATOR_SIZEOF (_backward));
 
134
}
 
135
 
 
136
void
 
137
grub_cpu_relocator_forward (void *ptr, void *src, void *dest,
 
138
                             grub_size_t size)
 
139
{
 
140
  grub_relocator_forward_dest = dest;
 
141
  grub_relocator_forward_src = src;
 
142
  grub_relocator_forward_chunk_size = size;
 
143
 
 
144
  grub_memmove (ptr,
 
145
                &grub_relocator_forward_start,
 
146
                RELOCATOR_SIZEOF (_forward));
 
147
}
 
148
 
 
149
grub_err_t
 
150
grub_relocator32_boot (struct grub_relocator *rel,
 
151
                       struct grub_relocator32_state state)
 
152
{
 
153
  grub_err_t err;
 
154
  void *relst;
 
155
  grub_relocator_chunk_t ch;
 
156
 
 
157
  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
 
158
                                          (0xffffffff - RELOCATOR_SIZEOF (32))
 
159
                                          + 1, RELOCATOR_SIZEOF (32), 16,
 
160
                                          GRUB_RELOCATOR_PREFERENCE_NONE);
 
161
  if (err)
 
162
    return err;
 
163
 
 
164
  grub_relocator32_eax = state.eax;
 
165
  grub_relocator32_ebx = state.ebx;
 
166
  grub_relocator32_ecx = state.ecx;
 
167
  grub_relocator32_edx = state.edx;
 
168
  grub_relocator32_eip = state.eip;
 
169
  grub_relocator32_esp = state.esp;
 
170
  grub_relocator32_ebp = state.ebp;
 
171
  grub_relocator32_esi = state.esi;
 
172
  grub_relocator32_edi = state.edi;
 
173
 
 
174
  grub_memmove (get_virtual_current_address (ch), &grub_relocator32_start,
 
175
                RELOCATOR_SIZEOF (32));
 
176
 
 
177
  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
 
178
                                       &relst, NULL);
 
179
  if (err)
 
180
    return err;
 
181
 
 
182
  asm volatile ("cli");
 
183
  ((void (*) (void)) relst) ();
 
184
 
 
185
  /* Not reached.  */
 
186
  return GRUB_ERR_NONE;
 
187
}
 
188
 
 
189
grub_err_t
 
190
grub_relocator16_boot (struct grub_relocator *rel,
 
191
                       struct grub_relocator16_state state)
 
192
{
 
193
  grub_err_t err;
 
194
  void *relst;
 
195
  grub_relocator_chunk_t ch;
 
196
 
 
197
  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
 
198
                                          0xa0000 - RELOCATOR_SIZEOF (16),
 
199
                                          RELOCATOR_SIZEOF (16), 16,
 
200
                                          GRUB_RELOCATOR_PREFERENCE_NONE);
 
201
  if (err)
 
202
    return err;
 
203
 
 
204
  grub_relocator16_cs = state.cs;  
 
205
  grub_relocator16_ip = state.ip;
 
206
 
 
207
  grub_relocator16_ds = state.ds;
 
208
  grub_relocator16_es = state.es;
 
209
  grub_relocator16_fs = state.fs;
 
210
  grub_relocator16_gs = state.gs;
 
211
 
 
212
  grub_relocator16_ss = state.ss;
 
213
  grub_relocator16_sp = state.sp;
 
214
 
 
215
  grub_relocator16_edx = state.edx;
 
216
 
 
217
  grub_memmove (get_virtual_current_address (ch), &grub_relocator16_start,
 
218
                RELOCATOR_SIZEOF (16));
 
219
 
 
220
  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
 
221
                                       &relst, NULL);
 
222
  if (err)
 
223
    return err;
 
224
 
 
225
  asm volatile ("cli");
 
226
  ((void (*) (void)) relst) ();
 
227
 
 
228
  /* Not reached.  */
 
229
  return GRUB_ERR_NONE;
 
230
}
 
231
 
 
232
grub_err_t
 
233
grub_relocator64_boot (struct grub_relocator *rel,
 
234
                       struct grub_relocator64_state state,
 
235
                       grub_addr_t min_addr, grub_addr_t max_addr)
 
236
{
 
237
  grub_err_t err;
 
238
  void *relst;
 
239
  grub_relocator_chunk_t ch;
 
240
 
 
241
  err = grub_relocator_alloc_chunk_align (rel, &ch, min_addr,
 
242
                                          max_addr - RELOCATOR_SIZEOF (64),
 
243
                                          RELOCATOR_SIZEOF (64), 16,
 
244
                                          GRUB_RELOCATOR_PREFERENCE_NONE);
 
245
  if (err)
 
246
    return err;
 
247
 
 
248
  grub_relocator64_rax = state.rax;
 
249
  grub_relocator64_rbx = state.rbx;
 
250
  grub_relocator64_rcx = state.rcx;
 
251
  grub_relocator64_rdx = state.rdx;
 
252
  grub_relocator64_rip = state.rip;
 
253
  grub_relocator64_rsp = state.rsp;
 
254
  grub_relocator64_rsi = state.rsi;
 
255
  grub_relocator64_cr3 = state.cr3;
 
256
 
 
257
  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_start,
 
258
                RELOCATOR_SIZEOF (64));
 
259
 
 
260
  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
 
261
                                       &relst, NULL);
 
262
  if (err)
 
263
    return err;
 
264
 
 
265
  asm volatile ("cli");
 
266
  ((void (*) (void)) relst) ();
 
267
 
 
268
  /* Not reached.  */
 
269
  return GRUB_ERR_NONE;
 
270
}