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

« back to all changes in this revision

Viewing changes to loader/powerpc/ieee1275/linux.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
 
/* linux.c - boot Linux */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 2003,2004,2005,2007,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/elf.h>
21
 
#include <grub/elfload.h>
22
 
#include <grub/loader.h>
23
 
#include <grub/dl.h>
24
 
#include <grub/mm.h>
25
 
#include <grub/misc.h>
26
 
#include <grub/ieee1275/ieee1275.h>
27
 
#include <grub/machine/loader.h>
28
 
#include <grub/command.h>
29
 
#include <grub/i18n.h>
30
 
 
31
 
#define ELF32_LOADMASK (0xc0000000UL)
32
 
#define ELF64_LOADMASK (0xc000000000000000ULL)
33
 
 
34
 
static grub_dl_t my_mod;
35
 
 
36
 
static int loaded;
37
 
 
38
 
static grub_addr_t initrd_addr;
39
 
static grub_size_t initrd_size;
40
 
 
41
 
static grub_addr_t linux_addr;
42
 
static grub_size_t linux_size;
43
 
 
44
 
static char *linux_args;
45
 
 
46
 
typedef void (*kernel_entry_t) (void *, unsigned long, int (void *),
47
 
                                unsigned long, unsigned long);
48
 
 
49
 
static grub_err_t
50
 
grub_linux_boot (void)
51
 
{
52
 
  kernel_entry_t linuxmain;
53
 
  grub_ssize_t actual;
54
 
 
55
 
  /* Set the command line arguments.  */
56
 
  grub_ieee1275_set_property (grub_ieee1275_chosen, "bootargs", linux_args,
57
 
                              grub_strlen (linux_args) + 1, &actual);
58
 
 
59
 
  grub_dprintf ("loader", "Entry point: 0x%x\n", linux_addr);
60
 
  grub_dprintf ("loader", "Initrd at: 0x%x, size 0x%x\n", initrd_addr,
61
 
                initrd_size);
62
 
  grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
63
 
  grub_dprintf ("loader", "Jumping to Linux...\n");
64
 
 
65
 
  /* Boot the kernel.  */
66
 
  linuxmain = (kernel_entry_t) linux_addr;
67
 
  linuxmain ((void *) initrd_addr, initrd_size, grub_ieee1275_entry_fn, 0, 0);
68
 
 
69
 
  return GRUB_ERR_NONE;
70
 
}
71
 
 
72
 
static grub_err_t
73
 
grub_linux_release_mem (void)
74
 
{
75
 
  grub_free (linux_args);
76
 
  linux_args = 0;
77
 
 
78
 
  if (linux_addr && grub_ieee1275_release (linux_addr, linux_size))
79
 
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
80
 
 
81
 
  if (initrd_addr && grub_ieee1275_release (initrd_addr, initrd_size))
82
 
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
83
 
 
84
 
  linux_addr = 0;
85
 
  initrd_addr = 0;
86
 
 
87
 
  return GRUB_ERR_NONE;
88
 
}
89
 
 
90
 
static grub_err_t
91
 
grub_linux_unload (void)
92
 
{
93
 
  grub_err_t err;
94
 
 
95
 
  err = grub_linux_release_mem ();
96
 
  grub_dl_unref (my_mod);
97
 
 
98
 
  loaded = 0;
99
 
 
100
 
  return err;
101
 
}
102
 
 
103
 
static grub_err_t
104
 
grub_linux_load32 (grub_elf_t elf)
105
 
{
106
 
  Elf32_Addr entry;
107
 
  int found_addr = 0;
108
 
 
109
 
  /* Linux's entry point incorrectly contains a virtual address.  */
110
 
  entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK;
111
 
  if (entry == 0)
112
 
    entry = 0x01400000;
113
 
 
114
 
  linux_size = grub_elf32_size (elf, 0);
115
 
  if (linux_size == 0)
116
 
    return grub_errno;
117
 
  /* Pad it; the kernel scribbles over memory beyond its load address.  */
118
 
  linux_size += 0x100000;
119
 
 
120
 
  /* On some systems, firmware occupies the memory we're trying to use.
121
 
   * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
122
 
   * until we find an open area.  */
123
 
  for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
124
 
    {
125
 
      grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
126
 
                    linux_addr, linux_size);
127
 
      found_addr = grub_claimmap (linux_addr, linux_size);
128
 
      if (found_addr != -1)
129
 
        break;
130
 
    }
131
 
  if (found_addr == -1)
132
 
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
133
 
 
134
 
  /* Now load the segments into the area we claimed.  */
135
 
  auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load);
136
 
  grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load)
137
 
    {
138
 
      if (phdr->p_type != PT_LOAD)
139
 
        {
140
 
          *do_load = 0;
141
 
          return 0;
142
 
        }
143
 
      *do_load = 1;
144
 
 
145
 
      /* Linux's program headers incorrectly contain virtual addresses.
146
 
       * Translate those to physical, and offset to the area we claimed.  */
147
 
      *addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr;
148
 
      return 0;
149
 
    }
150
 
  return grub_elf32_load (elf, offset_phdr, 0, 0);
151
 
}
152
 
 
153
 
static grub_err_t
154
 
grub_linux_load64 (grub_elf_t elf)
155
 
{
156
 
  Elf64_Addr entry;
157
 
  int found_addr = 0;
158
 
 
159
 
  /* Linux's entry point incorrectly contains a virtual address.  */
160
 
  entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK;
161
 
  if (entry == 0)
162
 
    entry = 0x01400000;
163
 
 
164
 
  linux_size = grub_elf64_size (elf, 0);
165
 
  if (linux_size == 0)
166
 
    return grub_errno;
167
 
  /* Pad it; the kernel scribbles over memory beyond its load address.  */
168
 
  linux_size += 0x100000;
169
 
 
170
 
  /* On some systems, firmware occupies the memory we're trying to use.
171
 
   * Happily, Linux can be loaded anywhere (it relocates itself).  Iterate
172
 
   * until we find an open area.  */
173
 
  for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
174
 
    {
175
 
      grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
176
 
                    linux_addr, linux_size);
177
 
      found_addr = grub_claimmap (linux_addr, linux_size);
178
 
      if (found_addr != -1)
179
 
        break;
180
 
    }
181
 
  if (found_addr == -1)
182
 
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
183
 
 
184
 
  /* Now load the segments into the area we claimed.  */
185
 
  auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load);
186
 
  grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load)
187
 
    {
188
 
      if (phdr->p_type != PT_LOAD)
189
 
        {
190
 
          *do_load = 0;
191
 
          return 0;
192
 
        }
193
 
      *do_load = 1;
194
 
      /* Linux's program headers incorrectly contain virtual addresses.
195
 
       * Translate those to physical, and offset to the area we claimed.  */
196
 
      *addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr;
197
 
      return 0;
198
 
    }
199
 
  return grub_elf64_load (elf, offset_phdr, 0, 0);
200
 
}
201
 
 
202
 
static grub_err_t
203
 
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
204
 
                int argc, char *argv[])
205
 
{
206
 
  grub_elf_t elf = 0;
207
 
  int i;
208
 
  int size;
209
 
  char *dest;
210
 
 
211
 
  grub_dl_ref (my_mod);
212
 
 
213
 
  if (argc == 0)
214
 
    {
215
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
216
 
      goto out;
217
 
    }
218
 
 
219
 
  elf = grub_elf_open (argv[0]);
220
 
  if (! elf)
221
 
    goto out;
222
 
 
223
 
  if (elf->ehdr.ehdr32.e_type != ET_EXEC)
224
 
    {
225
 
      grub_error (GRUB_ERR_UNKNOWN_OS,
226
 
                  "this ELF file is not of the right type");
227
 
      goto out;
228
 
    }
229
 
 
230
 
  /* Release the previously used memory.  */
231
 
  grub_loader_unset ();
232
 
 
233
 
  if (grub_elf_is_elf32 (elf))
234
 
    grub_linux_load32 (elf);
235
 
  else
236
 
  if (grub_elf_is_elf64 (elf))
237
 
    grub_linux_load64 (elf);
238
 
  else
239
 
    {
240
 
      grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
241
 
      goto out;
242
 
    }
243
 
 
244
 
  size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
245
 
  for (i = 0; i < argc; i++)
246
 
    size += grub_strlen (argv[i]) + 1;
247
 
 
248
 
  linux_args = grub_malloc (size);
249
 
  if (! linux_args)
250
 
    goto out;
251
 
 
252
 
  /* Specify the boot file.  */
253
 
  dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
254
 
  dest = grub_stpcpy (dest, argv[0]);
255
 
 
256
 
  for (i = 1; i < argc; i++)
257
 
    {
258
 
      *dest++ = ' ';
259
 
      dest = grub_stpcpy (dest, argv[i]);
260
 
    }
261
 
 
262
 
out:
263
 
 
264
 
  if (elf)
265
 
    grub_elf_close (elf);
266
 
 
267
 
  if (grub_errno != GRUB_ERR_NONE)
268
 
    {
269
 
      grub_linux_release_mem ();
270
 
      grub_dl_unref (my_mod);
271
 
      loaded = 0;
272
 
    }
273
 
  else
274
 
    {
275
 
      grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
276
 
      initrd_addr = 0;
277
 
      loaded = 1;
278
 
    }
279
 
 
280
 
  return grub_errno;
281
 
}
282
 
 
283
 
static grub_err_t
284
 
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
285
 
                 int argc, char *argv[])
286
 
{
287
 
  grub_file_t file = 0;
288
 
  grub_ssize_t size;
289
 
  grub_addr_t first_addr;
290
 
  grub_addr_t addr;
291
 
  int found_addr = 0;
292
 
 
293
 
  if (argc == 0)
294
 
    {
295
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
296
 
      goto fail;
297
 
    }
298
 
 
299
 
  if (!loaded)
300
 
    {
301
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
302
 
      goto fail;
303
 
    }
304
 
 
305
 
  file = grub_file_open (argv[0]);
306
 
  if (! file)
307
 
    goto fail;
308
 
 
309
 
  first_addr = linux_addr + linux_size;
310
 
  size = grub_file_size (file);
311
 
 
312
 
  /* Attempt to claim at a series of addresses until successful in
313
 
     the same way that grub_rescue_cmd_linux does.  */
314
 
  for (addr = first_addr; addr < first_addr + 200 * 0x100000; addr += 0x100000)
315
 
    {
316
 
      grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
317
 
                    addr, size);
318
 
      found_addr = grub_claimmap (addr, size);
319
 
      if (found_addr != -1)
320
 
        break;
321
 
    }
322
 
 
323
 
  if (found_addr == -1)
324
 
    {
325
 
      grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot claim memory");
326
 
      goto fail;
327
 
    }
328
 
 
329
 
  grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
330
 
 
331
 
  if (grub_file_read (file, (void *) addr, size) != size)
332
 
    {
333
 
      grub_ieee1275_release (addr, size);
334
 
      grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
335
 
      goto fail;
336
 
    }
337
 
 
338
 
  initrd_addr = addr;
339
 
  initrd_size = size;
340
 
 
341
 
 fail:
342
 
  if (file)
343
 
    grub_file_close (file);
344
 
 
345
 
  return grub_errno;
346
 
}
347
 
 
348
 
static grub_command_t cmd_linux, cmd_initrd;
349
 
 
350
 
GRUB_MOD_INIT(linux)
351
 
{
352
 
  cmd_linux = grub_register_command ("linux", grub_cmd_linux,
353
 
                                     0, N_("Load Linux."));
354
 
  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
355
 
                                      0, N_("Load initrd."));
356
 
  my_mod = mod;
357
 
}
358
 
 
359
 
GRUB_MOD_FINI(linux)
360
 
{
361
 
  grub_unregister_command (cmd_linux);
362
 
  grub_unregister_command (cmd_initrd);
363
 
}