main.c 7.37 KB
Newer Older
1
/* main.c - the kernel main routine */
okuji's avatar
okuji committed
2
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2002,2003,2005,2006,2008,2009  Free Software Foundation, Inc.
okuji's avatar
okuji committed
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
18 19
 */

20 21 22 23 24 25 26 27
#include <grub/kernel.h>
#include <grub/misc.h>
#include <grub/symbol.h>
#include <grub/dl.h>
#include <grub/term.h>
#include <grub/file.h>
#include <grub/device.h>
#include <grub/env.h>
28
#include <grub/mm.h>
29 30 31
#include <grub/command.h>
#include <grub/reader.h>
#include <grub/parser.h>
okuji's avatar
okuji committed
32

33 34 35 36
#ifdef GRUB_MACHINE_PCBIOS
#include <grub/machine/memory.h>
#endif

37 38 39 40 41
grub_addr_t
grub_modules_get_end (void)
{
  struct grub_module_info *modinfo;

42
  modinfo = (struct grub_module_info *) grub_modbase;
43 44 45

  /* Check if there are any modules.  */
  if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
46
    return grub_modbase;
47

48
  return grub_modbase + modinfo->size;
49 50
}

51 52 53 54
/* Load all modules in core.  */
static void
grub_load_modules (void)
{
55 56 57 58 59 60
  struct grub_module_header *header;
  FOR_MODULES (header)
  {
    /* Not an ELF module, skip.  */
    if (header->type != OBJ_TYPE_ELF)
      continue;
61

62 63 64
    if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
			     (header->size - sizeof (struct grub_module_header))))
      grub_fatal ("%s", grub_errmsg);
okuji's avatar
okuji committed
65

66 67 68
    if (grub_errno)
      grub_print_error ();
  }
okuji's avatar
okuji committed
69 70
}

71 72
static char *load_config;

73 74 75
static void
grub_load_config (void)
{
76 77 78 79 80 81
  struct grub_module_header *header;
  FOR_MODULES (header)
  {
    /* Not an embedded config, skip.  */
    if (header->type != OBJ_TYPE_CONFIG)
      continue;
82 83 84 85 86 87 88 89 90 91 92

    load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
    if (!load_config)
      {
	grub_print_error ();
	break;
      }
    grub_memcpy (load_config, (char *) header +
		 sizeof (struct grub_module_header),
		 header->size - sizeof (struct grub_module_header));
    load_config[header->size - sizeof (struct grub_module_header)] = 0;
93 94
    break;
  }
95 96
}

97 98 99
/* Write hook for the environment variables of root. Remove surrounding
   parentheses, if any.  */
static char *
100 101
grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
		     const char *val)
102 103 104
{
  /* XXX Is it better to check the existence of the device?  */
  grub_size_t len = grub_strlen (val);
105

106 107 108 109 110 111
  if (val[0] == '(' && val[len - 1] == ')')
    return grub_strndup (val + 1, len - 2);

  return grub_strdup (val);
}

112
static void
113
grub_set_prefix_and_root (void)
114
{
115 116 117 118
  char *device = NULL;
  char *path = NULL;
  char *fwdevice = NULL;
  char *fwpath = NULL;
119
  char *prefix = NULL;
120 121 122 123 124
  struct grub_module_header *header;

  FOR_MODULES (header)
    if (header->type == OBJ_TYPE_PREFIX)
      prefix = (char *) header + sizeof (struct grub_module_header);
125

126
  grub_register_variable_hook ("root", 0, grub_env_write_root);
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  grub_machine_get_bootlocation (&fwdevice, &fwpath);

  if (fwdevice)
    {
      char *cmdpath;

      cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
      if (cmdpath)
	{
	  grub_env_set ("cmdpath", cmdpath);
	  grub_env_export ("cmdpath");
	  grub_free (cmdpath);
	}
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  if (prefix)
    {
      char *pptr = NULL;
      if (prefix[0] == '(')
	{
	  pptr = grub_strrchr (prefix, ')');
	  if (pptr)
	    {
	      device = grub_strndup (prefix + 1, pptr - prefix - 1);
	      pptr++;
	    }
	}
      if (!pptr)
	pptr = prefix;
      if (pptr[0])
	path = grub_strdup (pptr);
    }
160 161 162 163

  if (!device && fwdevice)
    device = fwdevice;
  else if (fwdevice && (device[0] == ',' || !device[0]))
164
    {
165 166 167
      /* We have a partition, but still need to fill in the drive.  */
      char *comma, *new_device;

168 169 170 171 172 173 174 175 176 177 178 179
      for (comma = fwdevice; *comma; )
	{
	  if (comma[0] == '\\' && comma[1] == ',')
	    {
	      comma += 2;
	      continue;
	    }
	  if (*comma == ',')
	    break;
	  comma++;
	}
      if (*comma)
180 181 182 183 184 185 186
	{
	  char *drive = grub_strndup (fwdevice, comma - fwdevice);
	  new_device = grub_xasprintf ("%s%s", drive, device);
	  grub_free (drive);
	}
      else
	new_device = grub_xasprintf ("%s%s", fwdevice, device);
187

188 189 190 191
      grub_free (fwdevice);
      grub_free (device);
      device = new_device;
    }
192 193
  else
    grub_free (fwdevice);
194
  if (fwpath && !path)
195 196 197 198 199 200 201 202 203 204
    {
      grub_size_t len = grub_strlen (fwpath);
      while (len > 1 && fwpath[len - 1] == '/')
	fwpath[--len] = 0;
      if (len >= sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1
	  && grub_memcmp (fwpath + len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1), GRUB_TARGET_CPU "-" GRUB_PLATFORM,
			  sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1) == 0)
	fwpath[len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1)] = 0;
      path = fwpath;
    }
205 206
  else
    grub_free (fwpath);
207 208
  if (device)
    {
209
      char *prefix_set;
210
    
211 212
      prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
      if (prefix_set)
213
	{
214 215
	  grub_env_set ("prefix", prefix_set);
	  grub_free (prefix_set);
216
	}
217
      grub_env_set ("root", device);
218
    }
219 220 221 222

  grub_free (device);
  grub_free (path);
  grub_print_error ();
223 224 225 226
}

/* Load the normal mode module and execute the normal mode if possible.  */
static void
227
grub_load_normal_mode (void)
228
{
229
  /* Load the module.  */
230
  grub_dl_load ("normal");
231

232
  /* Print errors if any.  */
233
  grub_print_error ();
234 235 236
  grub_errno = 0;

  grub_command_execute ("normal", 0, 0);
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static void
reclaim_module_space (void)
{
  grub_addr_t modstart, modend;

  if (!grub_modbase)
    return;

#ifdef GRUB_MACHINE_PCBIOS
  modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
#else
  modstart = grub_modbase;
#endif
  modend = grub_modules_get_end ();
  grub_modbase = 0;

#if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
  grub_mm_init_region ((void *) modstart, modend - modstart);
#else
  (void) modstart;
  (void) modend;
#endif
}

okuji's avatar
okuji committed
263
/* The main routine.  */
264
void __attribute__ ((noreturn))
265
grub_main (void)
okuji's avatar
okuji committed
266 267
{
  /* First of all, initialize the machine.  */
268
  grub_machine_init ();
okuji's avatar
okuji committed
269

270 271
  grub_boot_time ("After machine init.");

okuji's avatar
okuji committed
272
  /* Hello.  */
273 274 275
  grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  grub_printf ("Welcome to GRUB!\n\n");
  grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
okuji's avatar
okuji committed
276

277 278
  grub_load_config ();

279 280
  grub_boot_time ("Before loading embedded modules.");

281
  /* Load pre-loaded modules and free the space.  */
282
  grub_register_exported_symbols ();
283 284 285
#ifdef GRUB_LINKER_HAVE_INIT
  grub_arch_dl_init_linker ();
#endif  
286
  grub_load_modules ();
okuji's avatar
okuji committed
287

288 289
  grub_boot_time ("After loading embedded modules.");

290 291
  /* It is better to set the root device as soon as possible,
     for convenience.  */
292
  grub_set_prefix_and_root ();
293 294
  grub_env_export ("root");
  grub_env_export ("prefix");
295

296 297 298
  /* Reclaim space used for modules.  */
  reclaim_module_space ();

299 300
  grub_boot_time ("After reclaiming module space.");

301
  grub_register_core_commands ();
302

303 304
  grub_boot_time ("Before execution of embedded config.");

305 306 307
  if (load_config)
    grub_parser_execute (load_config);

308 309
  grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");

310
  grub_load_normal_mode ();
311
  grub_rescue_run ();
okuji's avatar
okuji committed
312
}