2002-12-27 08:53:07 +00:00
|
|
|
/* err.c - error handling routines */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2008-08-14 18:46:47 +00:00
|
|
|
* Copyright (C) 2002,2005,2007,2008 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2002-12-27 08:53:07 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2002-12-27 08:53:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2002-12-27 08:53:07 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2002-12-27 08:53:07 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/misc.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2005-12-23 22:59:12 +00:00
|
|
|
#define GRUB_MAX_ERRMSG 256
|
|
|
|
#define GRUB_ERROR_STACK_SIZE 10
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_err_t grub_errno;
|
|
|
|
char grub_errmsg[GRUB_MAX_ERRMSG];
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2005-12-23 22:59:12 +00:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
grub_err_t errno;
|
|
|
|
char errmsg[GRUB_MAX_ERRMSG];
|
|
|
|
} grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
|
|
|
|
|
|
|
|
static int grub_error_stack_pos;
|
|
|
|
static int grub_error_stack_assert;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_error (grub_err_t n, const char *fmt, ...)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_errno = n;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_vsprintf (grub_errmsg, fmt, ap);
|
2002-12-27 08:53:07 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_fatal (const char *fmt, ...)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_vprintf (fmt, ap);
|
2002-12-27 08:53:07 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
2006-04-23 13:37:36 +00:00
|
|
|
grub_abort ();
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 22:59:12 +00:00
|
|
|
void
|
|
|
|
grub_error_push (void)
|
|
|
|
{
|
|
|
|
/* Only add items to stack, if there is enough room. */
|
|
|
|
if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE)
|
|
|
|
{
|
|
|
|
/* Copy active error message to stack. */
|
|
|
|
grub_error_stack_items[grub_error_stack_pos].errno = grub_errno;
|
|
|
|
grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg,
|
|
|
|
grub_errmsg,
|
|
|
|
sizeof (grub_errmsg));
|
|
|
|
|
|
|
|
/* Advance to next error stack position. */
|
|
|
|
grub_error_stack_pos++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is no room for new error message. Discard new error message
|
|
|
|
and mark error stack assertion flag. */
|
|
|
|
grub_error_stack_assert = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow further operation of other components by resetting
|
|
|
|
active errno to GRUB_ERR_NONE. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
grub_error_pop (void)
|
|
|
|
{
|
|
|
|
if (grub_error_stack_pos > 0)
|
|
|
|
{
|
|
|
|
/* Pop error message from error stack to current active error. */
|
|
|
|
grub_error_stack_pos--;
|
|
|
|
|
|
|
|
grub_errno = grub_error_stack_items[grub_error_stack_pos].errno;
|
|
|
|
grub_memcpy (grub_errmsg,
|
|
|
|
grub_error_stack_items[grub_error_stack_pos].errmsg,
|
|
|
|
sizeof (grub_errmsg));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is no more items on error stack, reset to no error state. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_print_error (void)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2005-12-23 22:59:12 +00:00
|
|
|
/* Print error messages in reverse order. First print active error message
|
|
|
|
and then empty error stack. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2008-08-14 18:46:47 +00:00
|
|
|
grub_err_printf ("error: %s\n", grub_errmsg);
|
2005-12-23 22:59:12 +00:00
|
|
|
}
|
|
|
|
while (grub_error_pop ());
|
|
|
|
|
|
|
|
/* If there was an assert while using error stack, report about it. */
|
|
|
|
if (grub_error_stack_assert)
|
|
|
|
{
|
2008-08-14 18:46:47 +00:00
|
|
|
grub_err_printf ("assert: error stack overflow detected!\n");
|
2005-12-23 22:59:12 +00:00
|
|
|
grub_error_stack_assert = 0;
|
|
|
|
}
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|