93f3a1d868
* commands/default.h: New file. * commands/timeout.h: Likewise. * normal/context.c: Likewise. * util/misc.c: Do not include sys/times.h. Include sys/time.h and grub/machine/time.h. (grub_get_rtc): Rewritten with gettimeofday. * util/grub-emu.c (main): Call grub_default_init and grub_timeout_init before grub_normal_init, and call grub_timeout_fini and grub_default_fini after grub_main. * util/console.c (grub_ncurses_checkkey): Return the read character or -1. * normal/menu.c (run_menu): Set MENU->TIMEOUT to -1 once it timeouts. * normal/main.c (read_config_file): Push MENU. If this fails, print an error and wait for a user input. Print an error only if GRUB_ERRNO is not GRUB_ERR_NONE. If a menu is empty or an error occurs, pop MENU. (grub_normal_execute): Pop and free MENU after grub_menu_run returns. * kern/loader.c (grub_loader_boot): Call grub_machine_fini. * include/grub/powerpc/ieee1275/time.h [GRUB_UTIL]: Do not include time.h. [GRUB_UTIL] (GRUB_TICKS_PER_SECOND): Use the same definition as without GRUB_UTIL. * include/grub/i386/pc/time.h [GRUB_UTIL]: Do not include time.h. [GRUB_UTIL] (GRUB_TICKS_PER_SECOND): Use the same definition as without GRUB_UTIL. * include/grub/normal.h (struct grub_menu_list): New struct. (grub_menu_list_t): New type. (struct grub_context): New struct. (grub_context_t): New type. (grub_register_command): Got rid of EXPORT_FUNC. (grub_unregister_command): Likewise. (grub_context_get): New prototype. (grub_context_get_current_menu): Likewise. (grub_context_push_menu): Likewise. (grub_context_pop_menu): Likewise. [GRUB_UTIL] (grub_default_init): Likewise. [GRUB_UTIL] (grub_default_fini): Likewise. [GRUB_UTIL] (grub_timeout_init): Likewise. [GRUB_UTIL] (grub_timeout_fini): Likewise. * conf/i386-pc.rmk (grub_emu_SOURCES): Added commands/default.c, commands/timeout.c and normal/context.c. (pkgdata_MODULES): Added default.mod and timeout.mod. (normal_mod_SOURCES): Added normal/context.c. (default_mod_SOURCES): New variable. (default_mod_CFLAGS): Likewise. (timeout_mod_SOURCES): Likewise. (timeout_mod_CFLAGS): Likewise. * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Copied from conf/i386-pc.rmk. (pkgdata_MODULES): Added default.mod and timeout.mod. (normal_mod_SOURCES): Added normal/context.c. (default_mod_SOURCES): New variable. (default_mod_CFLAGS): Likewise. (timeout_mod_SOURCES): Likewise. (timeout_mod_CFLAGS): Likewise. * Makefile.in (all-local): Added $(MKFILES).
273 lines
5.1 KiB
C
273 lines
5.1 KiB
C
/*
|
|
* GRUB -- GRand Unified Bootloader
|
|
* Copyright (C) 2002,2003,2005 Free Software Foundation, Inc.
|
|
*
|
|
* GRUB is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* 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
|
|
* along with GRUB; if not, write to the Free Software
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/time.h>
|
|
#include <malloc.h>
|
|
#include <unistd.h>
|
|
|
|
#include <grub/util/misc.h>
|
|
#include <grub/mm.h>
|
|
#include <grub/term.h>
|
|
#include <grub/machine/time.h>
|
|
|
|
char *progname = 0;
|
|
int verbosity = 0;
|
|
|
|
void
|
|
grub_util_info (const char *fmt, ...)
|
|
{
|
|
if (verbosity > 0)
|
|
{
|
|
va_list ap;
|
|
|
|
fprintf (stderr, "%s: info: ", progname);
|
|
va_start (ap, fmt);
|
|
vfprintf (stderr, fmt, ap);
|
|
va_end (ap);
|
|
fputc ('\n', stderr);
|
|
}
|
|
}
|
|
|
|
void
|
|
grub_util_error (const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
|
|
fprintf (stderr, "%s: error: ", progname);
|
|
va_start (ap, fmt);
|
|
vfprintf (stderr, fmt, ap);
|
|
va_end (ap);
|
|
fputc ('\n', stderr);
|
|
exit (1);
|
|
}
|
|
|
|
void *
|
|
xmalloc (size_t size)
|
|
{
|
|
void *p;
|
|
|
|
p = malloc (size);
|
|
if (! p)
|
|
grub_util_error ("out of memory");
|
|
|
|
return p;
|
|
}
|
|
|
|
void *
|
|
xrealloc (void *ptr, size_t size)
|
|
{
|
|
ptr = realloc (ptr, size);
|
|
if (! ptr)
|
|
grub_util_error ("out of memory");
|
|
|
|
return ptr;
|
|
}
|
|
|
|
char *
|
|
xstrdup (const char *str)
|
|
{
|
|
size_t len;
|
|
char *dup;
|
|
|
|
len = strlen (str);
|
|
dup = (char *) xmalloc (len + 1);
|
|
memcpy (dup, str, len + 1);
|
|
|
|
return dup;
|
|
}
|
|
|
|
char *
|
|
grub_util_get_path (const char *dir, const char *file)
|
|
{
|
|
char *path;
|
|
|
|
path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
|
|
sprintf (path, "%s/%s", dir, file);
|
|
return path;
|
|
}
|
|
|
|
size_t
|
|
grub_util_get_fp_size (FILE *fp)
|
|
{
|
|
struct stat st;
|
|
|
|
if (fflush (fp) == EOF)
|
|
grub_util_error ("fflush failed");
|
|
|
|
if (fstat (fileno (fp), &st) == -1)
|
|
grub_util_error ("fstat failed");
|
|
|
|
return st.st_size;
|
|
}
|
|
|
|
size_t
|
|
grub_util_get_image_size (const char *path)
|
|
{
|
|
struct stat st;
|
|
|
|
grub_util_info ("getting the size of %s", path);
|
|
|
|
if (stat (path, &st) == -1)
|
|
grub_util_error ("cannot stat %s", path);
|
|
|
|
return st.st_size;
|
|
}
|
|
|
|
void
|
|
grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
|
|
{
|
|
if (fseek (fp, offset, SEEK_SET) == -1)
|
|
grub_util_error ("fseek failed");
|
|
|
|
if (fread (img, 1, size, fp) != size)
|
|
grub_util_error ("read failed");
|
|
}
|
|
|
|
char *
|
|
grub_util_read_image (const char *path)
|
|
{
|
|
char *img;
|
|
FILE *fp;
|
|
size_t size;
|
|
|
|
grub_util_info ("reading %s", path);
|
|
|
|
size = grub_util_get_image_size (path);
|
|
img = (char *) xmalloc (size);
|
|
|
|
fp = fopen (path, "rb");
|
|
if (! fp)
|
|
grub_util_error ("cannot open %s", path);
|
|
|
|
grub_util_read_at (img, size, 0, fp);
|
|
|
|
fclose (fp);
|
|
|
|
return img;
|
|
}
|
|
|
|
void
|
|
grub_util_load_image (const char *path, char *buf)
|
|
{
|
|
FILE *fp;
|
|
size_t size;
|
|
|
|
grub_util_info ("reading %s", path);
|
|
|
|
size = grub_util_get_image_size (path);
|
|
|
|
fp = fopen (path, "rb");
|
|
if (! fp)
|
|
grub_util_error ("cannot open %s", path);
|
|
|
|
if (fread (buf, 1, size, fp) != size)
|
|
grub_util_error ("cannot read %s", path);
|
|
|
|
fclose (fp);
|
|
}
|
|
|
|
void
|
|
grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
|
|
{
|
|
grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
|
|
if (fseek (out, offset, SEEK_SET) == -1)
|
|
grub_util_error ("seek failed");
|
|
if (fwrite (img, 1, size, out) != size)
|
|
grub_util_error ("write failed");
|
|
}
|
|
|
|
void
|
|
grub_util_write_image (const char *img, size_t size, FILE *out)
|
|
{
|
|
grub_util_info ("writing 0x%x bytes", size);
|
|
if (fwrite (img, 1, size, out) != size)
|
|
grub_util_error ("write failed");
|
|
}
|
|
|
|
void *
|
|
grub_malloc (grub_size_t size)
|
|
{
|
|
return xmalloc (size);
|
|
}
|
|
|
|
void
|
|
grub_free (void *ptr)
|
|
{
|
|
free (ptr);
|
|
}
|
|
|
|
void *
|
|
grub_realloc (void *ptr, grub_size_t size)
|
|
{
|
|
return xrealloc (ptr, size);
|
|
}
|
|
|
|
void *
|
|
grub_memalign (grub_size_t align, grub_size_t size)
|
|
{
|
|
void *p;
|
|
|
|
p = memalign (align, size);
|
|
if (! p)
|
|
grub_util_error ("out of memory");
|
|
|
|
return p;
|
|
}
|
|
|
|
/* Some functions that we don't use. */
|
|
void
|
|
grub_mm_init_region (void *addr __attribute__ ((unused)),
|
|
grub_size_t size __attribute__ ((unused)))
|
|
{
|
|
}
|
|
|
|
void
|
|
grub_register_exported_symbols (void)
|
|
{
|
|
}
|
|
|
|
void
|
|
grub_stop (void)
|
|
{
|
|
exit (1);
|
|
}
|
|
|
|
grub_uint32_t
|
|
grub_get_rtc (void)
|
|
{
|
|
struct timeval tv;
|
|
|
|
gettimeofday (&tv, 0);
|
|
|
|
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
|
|
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
|
|
* GRUB_TICKS_PER_SECOND / 1000000));
|
|
}
|
|
|
|
void
|
|
grub_arch_sync_caches (void *address __attribute__ ((unused)),
|
|
grub_size_t len __attribute__ ((unused)))
|
|
{
|
|
}
|