2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A
2N/A#include <time.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <setjmp.h>
2N/A#include <sys/stat.h>
2N/A#include <string.h>
2N/A#include <signal.h>
2N/A#include <sys/types.h>
2N/A#include <unistd.h>
2N/A
2N/A#include <grub/mm.h>
2N/A#include <grub/setjmp.h>
2N/A#include <grub/fs.h>
2N/A#include <grub/emu/hostdisk.h>
2N/A#include <grub/time.h>
2N/A#include <grub/emu/console.h>
2N/A#include <grub/emu/misc.h>
2N/A#include <grub/kernel.h>
2N/A#include <grub/normal.h>
2N/A#include <grub/emu/getroot.h>
2N/A#include <grub/env.h>
2N/A#include <grub/partition.h>
2N/A#include <grub/i18n.h>
2N/A
2N/A#include "progname.h"
2N/A
2N/A#define ENABLE_RELOCATABLE 0
2N/A
2N/A/* Used for going back to the main function. */
2N/Astatic jmp_buf main_env;
2N/A
2N/A/* Store the prefix specified by an argument. */
2N/Astatic char *root_dev = NULL, *dir = NULL;
2N/A
2N/Aint grub_no_autoload;
2N/A
2N/Agrub_addr_t grub_modbase = 0;
2N/A
2N/Avoid
2N/Agrub_reboot (void)
2N/A{
2N/A longjmp (main_env, 1);
2N/A}
2N/A
2N/Avoid
2N/Agrub_machine_init (void)
2N/A{
2N/A}
2N/A
2N/Avoid
2N/Agrub_machine_get_bootlocation (char **device, char **path)
2N/A{
2N/A *device = root_dev;
2N/A *path = dir;
2N/A}
2N/A
2N/Avoid
2N/Agrub_machine_fini (void)
2N/A{
2N/A grub_console_fini ();
2N/A}
2N/A
2N/A
2N/A
2N/Astatic struct option options[] =
2N/A {
2N/A {"root-device", required_argument, 0, 'r'},
2N/A {"device-map", required_argument, 0, 'm'},
2N/A {"directory", required_argument, 0, 'd'},
2N/A {"hold", optional_argument, 0, 'H'},
2N/A {"help", no_argument, 0, 'h'},
2N/A {"version", no_argument, 0, 'V'},
2N/A {"verbose", no_argument, 0, 'v'},
2N/A { 0, 0, 0, 0 }
2N/A };
2N/A
2N/Astatic int
2N/Ausage (int status)
2N/A{
2N/A if (status)
2N/A fprintf (stderr,
2N/A _("Try `%s --help' for more information.\n"), program_name);
2N/A else
2N/A printf (
2N/A _("Usage: %s [OPTION]...\n"
2N/A "\n"
2N/A "GRUB emulator.\n"
2N/A "\n"
2N/A " -r, --root-device=DEV use DEV as the root device [default=host]\n"
2N/A " -m, --device-map=FILE use FILE as the device map [default=%s]\n"
2N/A " -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n"
2N/A " -v, --verbose print verbose messages\n"
2N/A " -H, --hold[=SECONDS] wait until a debugger will attach\n"
2N/A " -h, --help display this message and exit\n"
2N/A " -V, --version print version information and exit\n"
2N/A "\n"
2N/A "Report bugs to <%s>.\n"), program_name, DEFAULT_DEVICE_MAP, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
2N/A return status;
2N/A}
2N/A
2N/A
2N/Avoid grub_hostfs_init (void);
2N/Avoid grub_hostfs_fini (void);
2N/Avoid grub_host_init (void);
2N/Avoid grub_host_fini (void);
2N/Avoid grub_emu_init (void);
2N/A
2N/Aint
2N/Amain (int argc, char *argv[])
2N/A{
2N/A const char *dev_map = DEFAULT_DEVICE_MAP;
2N/A volatile int hold = 0;
2N/A int opt;
2N/A
2N/A set_program_name (argv[0]);
2N/A
2N/A dir = xstrdup (DEFAULT_DIRECTORY);
2N/A
2N/A while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1)
2N/A switch (opt)
2N/A {
2N/A case 'r':
2N/A free (root_dev);
2N/A root_dev = xstrdup (optarg);
2N/A break;
2N/A case 'd':
2N/A free (dir);
2N/A dir = xstrdup (optarg);
2N/A break;
2N/A case 'm':
2N/A dev_map = optarg;
2N/A break;
2N/A case 'v':
2N/A verbosity++;
2N/A grub_env_set("debug", "all");
2N/A break;
2N/A case 'H':
2N/A hold = (optarg ? atoi (optarg) : -1);
2N/A break;
2N/A case 'h':
2N/A return usage (0);
2N/A case 'V':
2N/A printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
2N/A return 0;
2N/A default:
2N/A return usage (1);
2N/A }
2N/A
2N/A if (optind < argc)
2N/A {
2N/A fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind]);
2N/A return usage (1);
2N/A }
2N/A
2N/A /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
2N/A if (hold && verbosity > 0)
2N/A printf (_("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n"),
2N/A program_name, (int) getpid ());
2N/A while (hold)
2N/A {
2N/A if (hold > 0)
2N/A hold--;
2N/A
2N/A sleep (1);
2N/A }
2N/A
2N/A signal (SIGINT, SIG_IGN);
2N/A grub_emu_init ();
2N/A grub_console_init ();
2N/A grub_host_init ();
2N/A
2N/A /* XXX: This is a bit unportable. */
2N/A grub_util_biosdisk_init (dev_map);
2N/A
2N/A grub_init_all ();
2N/A
2N/A grub_hostfs_init ();
2N/A
2N/A grub_emu_post_init ();
2N/A
2N/A /* Make sure that there is a root device. */
2N/A if (! root_dev)
2N/A root_dev = grub_strdup ("host");
2N/A
2N/A dir = xstrdup (dir);
2N/A
2N/A /* Start GRUB! */
2N/A if (setjmp (main_env) == 0)
2N/A grub_main ();
2N/A
2N/A grub_fini_all ();
2N/A grub_hostfs_fini ();
2N/A grub_host_fini ();
2N/A
2N/A grub_machine_fini ();
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A#ifdef __MINGW32__
2N/A
2N/Avoid
2N/Agrub_millisleep (grub_uint32_t ms)
2N/A{
2N/A Sleep (ms);
2N/A}
2N/A
2N/A#else
2N/A
2N/Avoid
2N/Agrub_millisleep (grub_uint32_t ms)
2N/A{
2N/A struct timespec ts;
2N/A
2N/A ts.tv_sec = ms / 1000;
2N/A ts.tv_nsec = (ms % 1000) * 1000000;
2N/A nanosleep (&ts, NULL);
2N/A}
2N/A
2N/A#endif