~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to util/grub-emu.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
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB; if not, write to the Free Software
 
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
 
 
20
#include <stdlib.h>
 
21
#include <malloc.h>
 
22
#include <sys/stat.h>
 
23
#include <argp.h>
 
24
#include <string.h>
 
25
#include <signal.h>
 
26
#include <sys/types.h>
 
27
#include <unistd.h>
 
28
 
 
29
#include <grub/mm.h>
 
30
#include <grub/setjmp.h>
 
31
#include <grub/fs.h>
 
32
#include <grub/i386/pc/util/biosdisk.h>
 
33
#include <grub/dl.h>
 
34
#include <grub/machine/console.h>
 
35
#include <grub/util/misc.h>
 
36
#include <grub/kernel.h>
 
37
#include <grub/normal.h>
 
38
#include <grub/util/getroot.h>
 
39
#include <grub/env.h>
 
40
#include <grub/partition.h>
 
41
 
 
42
#include <grub_modules_init.h>
 
43
 
 
44
#ifdef __NetBSD__
 
45
/* NetBSD uses /boot for its boot block.  */
 
46
# define DEFAULT_DIRECTORY      "/grub"
 
47
#else
 
48
# define DEFAULT_DIRECTORY      "/boot/grub"
 
49
#endif
 
50
 
 
51
#define DEFAULT_DEVICE_MAP      DEFAULT_DIRECTORY "/device.map"
 
52
 
 
53
/* Used for going back to the main function.  */
 
54
jmp_buf main_env;
 
55
 
 
56
grub_addr_t
 
57
grub_arch_modules_addr (void)
 
58
{
 
59
  return 0;
 
60
}
 
61
 
 
62
grub_err_t
 
63
grub_arch_dl_check_header (void *ehdr)
 
64
{
 
65
  (void) ehdr;
 
66
 
 
67
  return GRUB_ERR_BAD_MODULE;
 
68
}
 
69
 
 
70
grub_err_t
 
71
grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
 
72
{
 
73
  (void) mod;
 
74
  (void) ehdr;
 
75
 
 
76
  return GRUB_ERR_BAD_MODULE;
 
77
}
 
78
 
 
79
void
 
80
grub_machine_init (void)
 
81
{
 
82
  signal (SIGINT, SIG_IGN);
 
83
  grub_console_init ();
 
84
}
 
85
 
 
86
void
 
87
grub_machine_fini (void)
 
88
{
 
89
  grub_console_fini ();
 
90
}
 
91
 
 
92
 
 
93
const char *argp_program_version = PACKAGE_STRING;
 
94
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
 
95
static char doc[] = "GRUB emulator";
 
96
 
 
97
static struct argp_option options[] = {
 
98
  {"root-device", 'r', "DEV",  0, "use DEV as the root device [default=guessed]", 0},
 
99
  {"device-map",  'm', "FILE", 0, "use FILE as the device map", 0},
 
100
  {"directory",   'd', "DIR",  0, "use GRUB files in the directory DIR", 0},
 
101
  {"verbose",     'v', 0     , 0, "print verbose messages", 0},
 
102
  {"hold",        'H', "SECONDS", OPTION_ARG_OPTIONAL, "wait until a debugger will attach", 0},
 
103
  { 0, 0, 0, 0, 0, 0 }
 
104
};
 
105
 
 
106
struct arguments
 
107
{
 
108
  char *root_dev;
 
109
  char *dev_map;
 
110
  char *dir;
 
111
  int hold;
 
112
};
 
113
 
 
114
static error_t
 
115
parse_opt (int key, char *arg, struct argp_state *state)
 
116
{
 
117
  struct arguments *args = state->input;
 
118
  
 
119
  switch (key)
 
120
    {
 
121
    case 'r':
 
122
      args->root_dev = arg;
 
123
      break;
 
124
    case 'd':
 
125
      args->dir = arg;
 
126
      break;
 
127
    case 'm':
 
128
      args->dev_map = arg;
 
129
      break;
 
130
    case 'v':
 
131
      verbosity++;
 
132
      break;
 
133
    case 'H':
 
134
      args->hold = arg ? atoi (arg) : -1;
 
135
      break;
 
136
    case ARGP_KEY_END:
 
137
      break;
 
138
    default:
 
139
      return ARGP_ERR_UNKNOWN;
 
140
    }
 
141
  return 0;
 
142
}
 
143
 
 
144
static struct argp argp = {options, parse_opt, 0, doc, 0, 0, 0};
 
145
 
 
146
 
 
147
int
 
148
main (int argc, char *argv[])
 
149
{
 
150
  char *prefix = 0;
 
151
  char rootprefix[100];
 
152
  struct arguments args =
 
153
    {
 
154
      .dir = DEFAULT_DIRECTORY,
 
155
      .dev_map = DEFAULT_DEVICE_MAP,
 
156
      .hold = 0
 
157
    };
 
158
  
 
159
  progname = "grub-emu";
 
160
  
 
161
  argp_parse (&argp, argc, argv, 0, 0, &args);
 
162
 
 
163
  /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
 
164
  if (args.hold && verbosity > 0)
 
165
    printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
 
166
            progname, (int) getpid ());
 
167
  while (args.hold)
 
168
    {
 
169
      if (args.hold > 0)
 
170
        args.hold--;
 
171
 
 
172
      sleep (1);
 
173
    }
 
174
  
 
175
  /* Make sure that there is a root device.  */
 
176
  if (! args.root_dev)
 
177
    {
 
178
      args.root_dev = grub_guess_root_device (args.dir ? : DEFAULT_DIRECTORY);
 
179
      if (! args.root_dev)
 
180
        {
 
181
          grub_util_info ("guessing the root device failed, because of `%s'",
 
182
                          grub_errmsg);
 
183
          grub_util_error ("Cannot guess the root device. Specify the option ``--root-device''.");
 
184
        }
 
185
    }
 
186
 
 
187
  prefix = grub_get_prefix (args.dir ? : DEFAULT_DIRECTORY);
 
188
  sprintf (rootprefix, "%s%s", args.root_dev, prefix);
 
189
 
 
190
  grub_env_set ("prefix", rootprefix);
 
191
  
 
192
  /* XXX: This is a bit unportable.  */
 
193
  grub_util_biosdisk_init (args.dev_map);
 
194
 
 
195
  grub_init_all ();
 
196
 
 
197
  /* Start GRUB!  */
 
198
  if (setjmp (main_env) == 0)
 
199
    grub_main ();
 
200
 
 
201
  grub_fini_all ();
 
202
 
 
203
  grub_machine_fini ();
 
204
  
 
205
  return 0;
 
206
}