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

« back to all changes in this revision

Viewing changes to mmap/i386/pc/mmap.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
 
/* Mmap management. */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2009  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB 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, either version 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/machine/memory.h>
21
 
#include <grub/memory.h>
22
 
#include <grub/misc.h>
23
 
#include <grub/term.h>
24
 
#include <grub/loader.h>
25
 
 
26
 
#define min(a,b) (((a) < (b)) ? (a) : (b))
27
 
 
28
 
static void *hooktarget = 0;
29
 
 
30
 
extern grub_uint8_t grub_machine_mmaphook_start;
31
 
extern grub_uint8_t grub_machine_mmaphook_end;
32
 
extern grub_uint8_t grub_machine_mmaphook_int12;
33
 
extern grub_uint8_t grub_machine_mmaphook_int15;
34
 
 
35
 
static grub_uint16_t grub_machine_mmaphook_int12offset = 0;
36
 
static grub_uint16_t grub_machine_mmaphook_int12segment = 0;
37
 
extern grub_uint16_t grub_machine_mmaphook_int15offset;
38
 
extern grub_uint16_t grub_machine_mmaphook_int15segment;
39
 
 
40
 
extern grub_uint16_t grub_machine_mmaphook_mmap_num;
41
 
extern grub_uint16_t grub_machine_mmaphook_kblow;
42
 
extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
43
 
extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
44
 
 
45
 
struct grub_e820_mmap_entry
46
 
{
47
 
  grub_uint64_t addr;
48
 
  grub_uint64_t len;
49
 
  grub_uint32_t type;
50
 
} __attribute__((packed));
51
 
 
52
 
 
53
 
static grub_err_t
54
 
preboot (int noreturn __attribute__ ((unused)))
55
 
{
56
 
  struct grub_e820_mmap_entry *hookmmap, *hookmmapcur;
57
 
  auto int NESTED_FUNC_ATTR fill_hook (grub_uint64_t, grub_uint64_t,
58
 
                                       grub_uint32_t);
59
 
  int NESTED_FUNC_ATTR fill_hook (grub_uint64_t addr, grub_uint64_t size,
60
 
                                  grub_uint32_t type)
61
 
  {
62
 
    grub_dprintf ("mmap", "mmap chunk %llx-%llx:%x\n", addr, addr + size, type);
63
 
    hookmmapcur->addr = addr;
64
 
    hookmmapcur->len = size;
65
 
    hookmmapcur->type = type;
66
 
    hookmmapcur++;
67
 
    return 0;
68
 
  }
69
 
 
70
 
  if (! hooktarget)
71
 
    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
72
 
                       "no space is allocated for memory hook");
73
 
 
74
 
  grub_dprintf ("mmap", "installing preboot handlers\n");
75
 
 
76
 
  hookmmapcur = hookmmap = (struct grub_e820_mmap_entry *)
77
 
    ((grub_uint8_t *) hooktarget + (&grub_machine_mmaphook_end
78
 
                                    - &grub_machine_mmaphook_start));
79
 
 
80
 
  grub_mmap_iterate (fill_hook);
81
 
  grub_machine_mmaphook_mmap_num = hookmmapcur - hookmmap;
82
 
 
83
 
  grub_machine_mmaphook_kblow = grub_mmap_get_lower () >> 10;
84
 
  grub_machine_mmaphook_kbin16mb
85
 
    = min (grub_mmap_get_upper (),0x3f00000ULL) >> 10;
86
 
  grub_machine_mmaphook_64kbin4gb
87
 
    = min (grub_mmap_get_post64 (), 0xfc000000ULL) >> 16;
88
 
 
89
 
  /* Correct BDA. */
90
 
  *((grub_uint16_t *) 0x413) = grub_mmap_get_lower () >> 10;
91
 
 
92
 
  /* Save old interrupt handlers. */
93
 
  grub_machine_mmaphook_int12offset = *((grub_uint16_t *) 0x48);
94
 
  grub_machine_mmaphook_int12segment = *((grub_uint16_t *) 0x4a);
95
 
  grub_machine_mmaphook_int15offset = *((grub_uint16_t *) 0x54);
96
 
  grub_machine_mmaphook_int15segment = *((grub_uint16_t *) 0x56);
97
 
 
98
 
  grub_dprintf ("mmap", "hooktarget = %p\n", hooktarget);
99
 
 
100
 
  /* Install the interrupt handlers. */
101
 
  grub_memcpy (hooktarget, &grub_machine_mmaphook_start,
102
 
               &grub_machine_mmaphook_end - &grub_machine_mmaphook_start);
103
 
 
104
 
  *((grub_uint16_t *) 0x4a) = PTR_TO_UINT32 (hooktarget) >> 4;
105
 
  *((grub_uint16_t *) 0x56) = PTR_TO_UINT32 (hooktarget) >> 4;
106
 
  *((grub_uint16_t *) 0x48) = &grub_machine_mmaphook_int12
107
 
    - &grub_machine_mmaphook_start;
108
 
  *((grub_uint16_t *) 0x54) = &grub_machine_mmaphook_int15
109
 
    - &grub_machine_mmaphook_start;
110
 
 
111
 
  return GRUB_ERR_NONE;
112
 
}
113
 
 
114
 
static grub_err_t
115
 
preboot_rest (void)
116
 
{
117
 
  /* Restore old interrupt handlers. */
118
 
  *((grub_uint16_t *) 0x48) = grub_machine_mmaphook_int12offset;
119
 
  *((grub_uint16_t *) 0x4a) = grub_machine_mmaphook_int12segment;
120
 
  *((grub_uint16_t *) 0x54) = grub_machine_mmaphook_int15offset;
121
 
  *((grub_uint16_t *) 0x56) = grub_machine_mmaphook_int15segment;
122
 
 
123
 
  return GRUB_ERR_NONE;
124
 
}
125
 
 
126
 
static grub_err_t
127
 
malloc_hook (void)
128
 
{
129
 
  static int reentry = 0;
130
 
  static int mmapregion = 0;
131
 
  static int slots_available = 0;
132
 
  int hooksize;
133
 
  int regcount = 0;
134
 
  auto int NESTED_FUNC_ATTR count_hook (grub_uint64_t, grub_uint64_t,
135
 
                                        grub_uint32_t);
136
 
  int NESTED_FUNC_ATTR count_hook (grub_uint64_t addr __attribute__ ((unused)),
137
 
                                   grub_uint64_t size __attribute__ ((unused)),
138
 
                                   grub_uint32_t type __attribute__ ((unused)))
139
 
  {
140
 
    regcount++;
141
 
    return 0;
142
 
  }
143
 
 
144
 
  if (reentry)
145
 
    return GRUB_ERR_NONE;
146
 
 
147
 
  grub_dprintf ("mmap", "registering\n");
148
 
 
149
 
  grub_mmap_iterate (count_hook);
150
 
 
151
 
  /* Mapping hook itself may introduce up to 2 additional regions. */
152
 
  regcount += 2;
153
 
 
154
 
  if (regcount <= slots_available)
155
 
    return GRUB_ERR_NONE;
156
 
 
157
 
  if (mmapregion)
158
 
    {
159
 
      grub_mmap_free_and_unregister (mmapregion);
160
 
      mmapregion = 0;
161
 
      hooktarget = 0;
162
 
    }
163
 
 
164
 
  hooksize = &grub_machine_mmaphook_end - &grub_machine_mmaphook_start
165
 
    + regcount * sizeof (struct grub_e820_mmap_entry);
166
 
  /* Allocate an integer number of KiB. */
167
 
  hooksize = ((hooksize - 1) | 0x3ff) + 1;
168
 
  slots_available = (hooksize - (&grub_machine_mmaphook_end
169
 
                                 - &grub_machine_mmaphook_start))
170
 
    / sizeof (struct grub_e820_mmap_entry);
171
 
 
172
 
  reentry = 1;
173
 
  hooktarget
174
 
    = grub_mmap_malign_and_register (16, hooksize, &mmapregion,
175
 
                                     GRUB_MACHINE_MEMORY_RESERVED,
176
 
                                     GRUB_MMAP_MALLOC_LOW);
177
 
  reentry = 0;
178
 
 
179
 
  if (! hooktarget)
180
 
    {
181
 
      slots_available = 0;
182
 
      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "no space for mmap hook");
183
 
    }
184
 
  return GRUB_ERR_NONE;
185
 
}
186
 
 
187
 
grub_err_t
188
 
grub_machine_mmap_register (grub_uint64_t start __attribute__ ((unused)),
189
 
                            grub_uint64_t size __attribute__ ((unused)),
190
 
                            int type __attribute__ ((unused)),
191
 
                            int handle  __attribute__ ((unused)))
192
 
{
193
 
  grub_err_t err;
194
 
  static void *preb_handle = 0;
195
 
 
196
 
  err = malloc_hook ();
197
 
  if (err)
198
 
    return err;
199
 
 
200
 
  if (! preb_handle)
201
 
    {
202
 
      grub_dprintf ("mmap", "adding preboot\n");
203
 
      preb_handle
204
 
        = grub_loader_register_preboot_hook (preboot, preboot_rest,
205
 
                                             GRUB_LOADER_PREBOOT_HOOK_PRIO_MEMORY);
206
 
      if (! preb_handle)
207
 
        return grub_errno;
208
 
    }
209
 
  return GRUB_ERR_NONE;
210
 
}
211
 
 
212
 
grub_err_t
213
 
grub_machine_mmap_unregister (int handle __attribute__ ((unused)))
214
 
{
215
 
  return GRUB_ERR_NONE;
216
 
}