~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

Viewing changes to loader/i386/pc/multiboot.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* multiboot.c - boot a multiboot OS image. */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
 
5
 *
 
6
 *  This program 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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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 this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
/* 
 
22
 *  FIXME: The following features from the Multiboot specification still
 
23
 *         need to be implemented:
 
24
 *  - VBE support
 
25
 *  - a.out support
 
26
 *  - boot device
 
27
 *  - symbol table
 
28
 *  - memory map
 
29
 *  - drives table
 
30
 *  - ROM configuration table
 
31
 *  - APM table
 
32
 */
 
33
 
 
34
#include <grub/loader.h>
 
35
#include <grub/machine/loader.h>
 
36
#include <grub/machine/multiboot.h>
 
37
#include <grub/machine/init.h>
 
38
#include <grub/elf.h>
 
39
#include <grub/file.h>
 
40
#include <grub/err.h>
 
41
#include <grub/rescue.h>
 
42
#include <grub/dl.h>
 
43
#include <grub/mm.h>
 
44
#include <grub/misc.h>
 
45
#include <grub/gzio.h>
 
46
 
 
47
static grub_dl_t my_mod;
 
48
static struct grub_multiboot_info *mbi;
 
49
static grub_addr_t entry;
 
50
 
 
51
static grub_err_t
 
52
grub_multiboot_boot (void)
 
53
{
 
54
  grub_multiboot_real_boot (entry, mbi);
 
55
 
 
56
  /* Not reached.  */
 
57
  return GRUB_ERR_NONE;
 
58
}
 
59
 
 
60
static grub_err_t
 
61
grub_multiboot_unload (void)
 
62
{
 
63
  if (mbi)
 
64
    {
 
65
      unsigned int i;
 
66
      for (i = 0; i < mbi->mods_count; i++)
 
67
        {
 
68
          grub_free ((void *)
 
69
                     ((struct grub_mod_list *) mbi->mods_addr)[i].mod_start);
 
70
          grub_free ((void *)
 
71
                     ((struct grub_mod_list *) mbi->mods_addr)[i].cmdline);
 
72
        }
 
73
      grub_free ((void *) mbi->mods_addr);
 
74
      grub_free ((void *) mbi->cmdline);
 
75
      grub_free (mbi);
 
76
    }
 
77
 
 
78
 
 
79
  mbi = 0;
 
80
  grub_dl_unref (my_mod);
 
81
 
 
82
  return GRUB_ERR_NONE;
 
83
}
 
84
 
 
85
/* Check if BUFFER contains ELF32.  */
 
86
static int
 
87
grub_multiboot_is_elf32 (void *buffer)
 
88
{
 
89
  Elf32_Ehdr *ehdr = (Elf32_Ehdr *) buffer;
 
90
  
 
91
  return ehdr->e_ident[EI_CLASS] == ELFCLASS32;
 
92
}
 
93
 
 
94
static grub_err_t
 
95
grub_multiboot_load_elf32 (grub_file_t file, void *buffer)
 
96
{
 
97
  Elf32_Ehdr *ehdr = (Elf32_Ehdr *) buffer;
 
98
  Elf32_Phdr *phdr;
 
99
  int i;
 
100
 
 
101
  if (ehdr->e_ident[EI_CLASS] != ELFCLASS32)
 
102
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
 
103
  
 
104
  if (grub_dl_check_header (ehdr, sizeof(Elf32_Ehdr)))
 
105
    return grub_error (GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
 
106
  
 
107
  if (ehdr->e_type != ET_EXEC)
 
108
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
 
109
  
 
110
  /* FIXME: Should we support program headers at strange locations?  */
 
111
  if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH)
 
112
    return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
 
113
  
 
114
  entry = ehdr->e_entry;
 
115
  
 
116
  /* Load every loadable segment in memory.  */
 
117
  for (i = 0; i < ehdr->e_phnum; i++)
 
118
    {
 
119
      phdr = (Elf32_Phdr *) ((char *) buffer + ehdr->e_phoff
 
120
                             + i * ehdr->e_phentsize);
 
121
      if (phdr->p_type == PT_LOAD)
 
122
        {
 
123
          /* The segment should fit in the area reserved for the OS.  */
 
124
          if ((phdr->p_paddr < grub_os_area_addr)
 
125
              || (phdr->p_paddr + phdr->p_memsz
 
126
                  > grub_os_area_addr + grub_os_area_size))
 
127
            return grub_error (GRUB_ERR_BAD_OS,
 
128
                               "segment doesn't fit in memory reserved for the OS");
 
129
 
 
130
          if (grub_file_seek (file, phdr->p_offset) == -1)
 
131
            return grub_error (GRUB_ERR_BAD_OS,
 
132
                               "invalid offset in program header");
 
133
          
 
134
          if (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz)
 
135
              != (grub_ssize_t) phdr->p_filesz)
 
136
            return grub_error (GRUB_ERR_BAD_OS,
 
137
                               "couldn't read segment from file");
 
138
 
 
139
          if (phdr->p_filesz < phdr->p_memsz)
 
140
            grub_memset ((char *) phdr->p_paddr + phdr->p_filesz, 0,
 
141
                         phdr->p_memsz - phdr->p_filesz);
 
142
        }
 
143
    }
 
144
  
 
145
  return grub_errno;
 
146
}
 
147
 
 
148
/* Check if BUFFER contains ELF64.  */
 
149
static int
 
150
grub_multiboot_is_elf64 (void *buffer)
 
151
{
 
152
  Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
 
153
  
 
154
  return ehdr->e_ident[EI_CLASS] == ELFCLASS64;
 
155
}
 
156
 
 
157
static grub_err_t
 
158
grub_multiboot_load_elf64 (grub_file_t file, void *buffer)
 
159
{
 
160
  Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
 
161
  Elf64_Phdr *phdr;
 
162
  int i;
 
163
 
 
164
  if (ehdr->e_ident[EI_CLASS] != ELFCLASS64)
 
165
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
 
166
 
 
167
  if (ehdr->e_ident[EI_MAG0] != ELFMAG0
 
168
      || ehdr->e_ident[EI_MAG1] != ELFMAG1
 
169
      || ehdr->e_ident[EI_MAG2] != ELFMAG2
 
170
      || ehdr->e_ident[EI_MAG3] != ELFMAG3
 
171
      || ehdr->e_version != EV_CURRENT
 
172
      || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
 
173
      || ehdr->e_machine != EM_X86_64)
 
174
    return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
 
175
 
 
176
  if (ehdr->e_type != ET_EXEC)
 
177
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
 
178
 
 
179
  /* FIXME: Should we support program headers at strange locations?  */
 
180
  if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_MB_SEARCH)
 
181
    return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
 
182
 
 
183
  /* We still in 32-bit mode */
 
184
  if (ehdr->e_entry > 0xffffffff)
 
185
    return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
 
186
 
 
187
  entry = ehdr->e_entry;
 
188
 
 
189
  /* Load every loadable segment in memory.  */
 
190
  for (i = 0; i < ehdr->e_phnum; i++)
 
191
    {
 
192
      phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
 
193
                             + i * ehdr->e_phentsize);
 
194
      if (phdr->p_type == PT_LOAD)
 
195
        {
 
196
          /* The segment should fit in the area reserved for the OS.  */
 
197
          if ((phdr->p_paddr < (grub_uint64_t) grub_os_area_addr)
 
198
              || (phdr->p_paddr + phdr->p_memsz
 
199
                  > ((grub_uint64_t) grub_os_area_addr
 
200
                     + (grub_uint64_t) grub_os_area_size)))
 
201
            return grub_error (GRUB_ERR_BAD_OS,
 
202
                               "segment doesn't fit in memory reserved for the OS");
 
203
          
 
204
          if (grub_file_seek (file, phdr->p_offset) == -1)
 
205
            return grub_error (GRUB_ERR_BAD_OS,
 
206
                               "invalid offset in program header");
 
207
 
 
208
          if (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr),
 
209
                              phdr->p_filesz)
 
210
              != (grub_ssize_t) phdr->p_filesz)
 
211
            return grub_error (GRUB_ERR_BAD_OS,
 
212
                               "couldn't read segment from file");
 
213
          
 
214
          if (phdr->p_filesz < phdr->p_memsz)
 
215
            grub_memset (((char *) ((grub_uint32_t) phdr->p_paddr)
 
216
                          + phdr->p_filesz),
 
217
                         0,
 
218
                         phdr->p_memsz - phdr->p_filesz);
 
219
        }
 
220
    }
 
221
  
 
222
  return grub_errno;
 
223
}
 
224
 
 
225
/* Load ELF32 or ELF64.  */
 
226
static grub_err_t
 
227
grub_multiboot_load_elf (grub_file_t file, void *buffer)
 
228
{
 
229
  if (grub_multiboot_is_elf32 (buffer))
 
230
    return grub_multiboot_load_elf32 (file, buffer);
 
231
  else if (grub_multiboot_is_elf64 (buffer))
 
232
    return grub_multiboot_load_elf64 (file, buffer);
 
233
  
 
234
  return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
 
235
}
 
236
 
 
237
void
 
238
grub_rescue_cmd_multiboot (int argc, char *argv[])
 
239
{
 
240
  grub_file_t file = 0;
 
241
  char buffer[GRUB_MB_SEARCH], *cmdline = 0, *p;
 
242
  struct grub_multiboot_header *header;
 
243
  grub_ssize_t len;
 
244
  int i;
 
245
 
 
246
  grub_dl_ref (my_mod);
 
247
 
 
248
  grub_loader_unset ();
 
249
    
 
250
  if (argc == 0)
 
251
    {
 
252
      grub_error (GRUB_ERR_BAD_ARGUMENT, "No kernel specified");
 
253
      goto fail;
 
254
    }
 
255
 
 
256
  file = grub_gzfile_open (argv[0], 1);
 
257
  if (! file)
 
258
    {
 
259
      grub_error (GRUB_ERR_BAD_ARGUMENT, "Couldn't open file");
 
260
      goto fail;
 
261
    }
 
262
 
 
263
  len = grub_file_read (file, buffer, GRUB_MB_SEARCH);
 
264
  if (len < 32)
 
265
    {
 
266
      grub_error (GRUB_ERR_BAD_OS, "File too small");
 
267
      goto fail;
 
268
    }
 
269
 
 
270
  /* Look for the multiboot header in the buffer.  The header should
 
271
     be at least 12 bytes and aligned on a 4-byte boundary.  */
 
272
  for (header = (struct grub_multiboot_header *) buffer; 
 
273
       ((char *) header <= buffer + len - 12) || (header = 0);
 
274
       header = (struct grub_multiboot_header *) ((char *) header + 4))
 
275
    {
 
276
      if (header->magic == GRUB_MB_MAGIC 
 
277
          && !(header->magic + header->flags + header->checksum))
 
278
        break;
 
279
    }
 
280
  
 
281
  if (header == 0)
 
282
    {
 
283
      grub_error (GRUB_ERR_BAD_ARGUMENT, "No multiboot header found");
 
284
      goto fail;
 
285
    }
 
286
 
 
287
  if (header->flags & GRUB_MB_UNSUPPORTED)
 
288
    {
 
289
      grub_error (GRUB_ERR_UNKNOWN_OS,
 
290
                  "Unsupported flag: 0x%x", header->flags);
 
291
      goto fail;
 
292
    }
 
293
 
 
294
  if (grub_multiboot_load_elf (file, buffer) != GRUB_ERR_NONE)
 
295
    goto fail;
 
296
  
 
297
  mbi = grub_malloc (sizeof (struct grub_multiboot_info));
 
298
  if (! mbi)
 
299
    goto fail;
 
300
 
 
301
  mbi->flags = GRUB_MB_INFO_MEMORY;
 
302
 
 
303
  /* Convert from bytes to kilobytes.  */
 
304
  mbi->mem_lower = grub_lower_mem / 1024;
 
305
  mbi->mem_upper = grub_upper_mem / 1024;
 
306
 
 
307
  for (i = 0, len = 0; i < argc; i++)
 
308
    len += grub_strlen (argv[i]) + 1;
 
309
  
 
310
  cmdline = p = grub_malloc (len);
 
311
  if (! cmdline)
 
312
    goto fail;
 
313
  
 
314
  for (i = 0; i < argc; i++)
 
315
    {
 
316
      p = grub_stpcpy (p, argv[i]);
 
317
      *(p++) = ' ';
 
318
    }
 
319
  
 
320
  /* Remove the space after the last word.  */
 
321
  *(--p) = '\0';
 
322
  
 
323
  mbi->flags |= GRUB_MB_INFO_CMDLINE;
 
324
  mbi->cmdline = (grub_uint32_t) cmdline;
 
325
 
 
326
  mbi->flags |= GRUB_MB_INFO_BOOT_LOADER_NAME;
 
327
  mbi->boot_loader_name = (grub_uint32_t) grub_strdup (PACKAGE_STRING);
 
328
 
 
329
  grub_loader_set (grub_multiboot_boot, grub_multiboot_unload);
 
330
 
 
331
 fail:
 
332
  if (file)
 
333
    grub_file_close (file);
 
334
 
 
335
  if (grub_errno != GRUB_ERR_NONE)
 
336
    {
 
337
      grub_free (cmdline);
 
338
      grub_free (mbi);
 
339
      grub_dl_unref (my_mod);
 
340
    }
 
341
}
 
342
 
 
343
 
 
344
void
 
345
grub_rescue_cmd_module  (int argc, char *argv[])
 
346
{
 
347
  grub_file_t file = 0;
 
348
  grub_ssize_t size, len = 0;
 
349
  char *module = 0, *cmdline = 0, *p;
 
350
  int i;
 
351
 
 
352
  if (argc == 0)
 
353
    {
 
354
      grub_error (GRUB_ERR_BAD_ARGUMENT, "No module specified");
 
355
      goto fail;
 
356
    }
 
357
 
 
358
  if (!mbi)
 
359
    {
 
360
      grub_error (GRUB_ERR_BAD_ARGUMENT, 
 
361
                  "You need to load the multiboot kernel first");
 
362
      goto fail;
 
363
    }
 
364
 
 
365
  file = grub_gzfile_open (argv[0], 1);
 
366
  if (! file)
 
367
    goto fail;
 
368
 
 
369
  size = grub_file_size (file);
 
370
  module = grub_memalign (GRUB_MB_MOD_ALIGN, size);
 
371
  if (! module)
 
372
    goto fail;
 
373
 
 
374
  if (grub_file_read (file, module, size) != size)
 
375
    {
 
376
      grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
 
377
      goto fail;
 
378
    }
 
379
  
 
380
  for (i = 0; i < argc; i++)
 
381
    len += grub_strlen (argv[i]) + 1;
 
382
  
 
383
  cmdline = p = grub_malloc (len);
 
384
  if (! cmdline)
 
385
    goto fail;
 
386
  
 
387
  for (i = 0; i < argc; i++)
 
388
    {
 
389
      p = grub_stpcpy (p, argv[i]);
 
390
      *(p++) = ' ';
 
391
    }
 
392
  
 
393
  /* Remove the space after the last word.  */
 
394
  *(--p) = '\0';
 
395
 
 
396
  if (mbi->flags & GRUB_MB_INFO_MODS)
 
397
    {
 
398
      struct grub_mod_list *modlist = (struct grub_mod_list *) mbi->mods_addr;
 
399
 
 
400
      modlist = grub_realloc (modlist, (mbi->mods_count + 1) 
 
401
                                       * sizeof (struct grub_mod_list));
 
402
      if (! modlist)
 
403
        goto fail;
 
404
      mbi->mods_addr = (grub_uint32_t) modlist;
 
405
      modlist += mbi->mods_count;
 
406
      modlist->mod_start = (grub_uint32_t) module;
 
407
      modlist->mod_end = (grub_uint32_t) module + size;
 
408
      modlist->cmdline = (grub_uint32_t) cmdline;
 
409
      modlist->pad = 0;
 
410
      mbi->mods_count++;
 
411
    }
 
412
  else
 
413
    {
 
414
      struct grub_mod_list *modlist = grub_malloc (sizeof (struct grub_mod_list));
 
415
      if (! modlist)
 
416
        goto fail;
 
417
      modlist->mod_start = (grub_uint32_t) module;
 
418
      modlist->mod_end = (grub_uint32_t) module + size;
 
419
      modlist->cmdline = (grub_uint32_t) cmdline;
 
420
      modlist->pad = 0;
 
421
      mbi->mods_count = 1;
 
422
      mbi->mods_addr = (grub_uint32_t) modlist;
 
423
      mbi->flags |= GRUB_MB_INFO_MODS;
 
424
    }
 
425
 
 
426
 fail:
 
427
  if (file)
 
428
    grub_file_close (file);
 
429
 
 
430
  if (grub_errno != GRUB_ERR_NONE)
 
431
    {
 
432
      grub_free (module);
 
433
      grub_free (cmdline);
 
434
    }
 
435
}
 
436
 
 
437
 
 
438
GRUB_MOD_INIT(multiboot)
 
439
{
 
440
  grub_rescue_register_command ("multiboot", grub_rescue_cmd_multiboot,
 
441
                                "load a multiboot kernel");
 
442
  grub_rescue_register_command ("module", grub_rescue_cmd_module,
 
443
                                "load a multiboot module");
 
444
  my_mod = mod;
 
445
}
 
446
 
 
447
GRUB_MOD_FINI(multiboot)
 
448
{
 
449
  grub_rescue_unregister_command ("multiboot");
 
450
  grub_rescue_unregister_command ("module");
 
451
}