~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to kern/main.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
/* main.c - the kernel main routine */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2002, 2003, 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
#include <grub/kernel.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/symbol.h>
 
25
#include <grub/dl.h>
 
26
#include <grub/term.h>
 
27
#include <grub/rescue.h>
 
28
#include <grub/file.h>
 
29
#include <grub/device.h>
 
30
#include <grub/env.h>
 
31
 
 
32
/* Load all modules in core.  */
 
33
static void
 
34
grub_load_modules (void)
 
35
{
 
36
  struct grub_module_info *modinfo;
 
37
  struct grub_module_header *header;
 
38
  grub_addr_t modbase;
 
39
 
 
40
  modbase = grub_arch_modules_addr ();
 
41
  modinfo = (struct grub_module_info *) modbase;
 
42
  
 
43
  /* Check if there are any modules.  */
 
44
  if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
 
45
    return;
 
46
 
 
47
  for (header = (struct grub_module_header *) (modbase + modinfo->offset);
 
48
       header < (struct grub_module_header *) (modbase + modinfo->size);
 
49
       header = (struct grub_module_header *) ((char *) header + header->size))
 
50
    {
 
51
      if (! grub_dl_load_core ((char *) header + header->offset,
 
52
                               (header->size - header->offset)))
 
53
        grub_fatal ("%s", grub_errmsg);
 
54
    }
 
55
 
 
56
  /* Add the region where modules reside into dynamic memory.  */
 
57
  grub_mm_init_region ((void *) modinfo, modinfo->size);
 
58
}
 
59
 
 
60
/* Write hook for the environment variables of root. Remove surrounding
 
61
   parentheses, if any.  */
 
62
static char *
 
63
grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
 
64
                     const char *val)
 
65
{
 
66
  /* XXX Is it better to check the existence of the device?  */
 
67
  grub_size_t len = grub_strlen (val);
 
68
  
 
69
  if (val[0] == '(' && val[len - 1] == ')')
 
70
    return grub_strndup (val + 1, len - 2);
 
71
 
 
72
  return grub_strdup (val);
 
73
}
 
74
 
 
75
/* Set the root device according to the dl prefix.  */
 
76
static void
 
77
grub_set_root_dev (void)
 
78
{
 
79
  const char *prefix;
 
80
 
 
81
  grub_register_variable_hook ("root", 0, grub_env_write_root);
 
82
  
 
83
  prefix = grub_env_get ("prefix");
 
84
  
 
85
  if (prefix)
 
86
    {
 
87
      char *dev;
 
88
 
 
89
      dev = grub_file_get_device_name (prefix);
 
90
      if (dev)
 
91
        {
 
92
          grub_env_set ("root", dev);
 
93
          grub_free (dev);
 
94
        }
 
95
    }
 
96
}
 
97
 
 
98
/* Load the normal mode module and execute the normal mode if possible.  */
 
99
static void
 
100
grub_load_normal_mode (void)
 
101
{
 
102
  /* Load the module.  */
 
103
  grub_dl_load ("normal");
 
104
  
 
105
  /* Ignore any error, because we have the rescue mode anyway.  */
 
106
  grub_errno = GRUB_ERR_NONE;
 
107
}
 
108
 
 
109
/* The main routine.  */
 
110
void
 
111
grub_main (void)
 
112
{
 
113
  /* First of all, initialize the machine.  */
 
114
  grub_machine_init ();
 
115
 
 
116
  /* Hello.  */
 
117
  grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
 
118
  grub_printf ("Welcome to GRUB!\n\n");
 
119
  grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
120
 
 
121
  /* It is better to set the root device as soon as possible,
 
122
     for convenience.  */
 
123
  grub_set_root_dev ();
 
124
 
 
125
  /* Load pre-loaded modules and free the space.  */
 
126
  grub_register_exported_symbols ();
 
127
  grub_load_modules ();
 
128
 
 
129
  /* Load the normal mode module.  */
 
130
  grub_load_normal_mode ();
 
131
  
 
132
  /* Enter the rescue mode.  */
 
133
  grub_enter_rescue_mode ();
 
134
}