2002-12-27 08:53:07 +00:00
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-01 20:32:30 +00:00
|
|
|
* Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 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
|
|
|
*/
|
|
|
|
|
2006-06-13 22:50:01 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2009-11-08 00:49:15 +00:00
|
|
|
#include <errno.h>
|
2009-07-01 13:01:33 +00:00
|
|
|
#include <setjmp.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2009-11-08 00:49:15 +00:00
|
|
|
#include <stdint.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2005-02-27 21:19:06 +00:00
|
|
|
#include <sys/time.h>
|
2005-01-04 14:01:45 +00:00
|
|
|
#include <unistd.h>
|
2009-03-04 10:25:31 +00:00
|
|
|
#include <time.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2008-04-06 17:51:56 +00:00
|
|
|
#include <grub/kernel.h>
|
2010-02-07 16:52:11 +00:00
|
|
|
#include <grub/dl.h>
|
2008-04-06 17:51:56 +00:00
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/cache.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/util/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/term.h>
|
2008-08-07 23:48:13 +00:00
|
|
|
#include <grub/time.h>
|
2010-01-01 20:32:30 +00:00
|
|
|
#include <grub/i18n.h>
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2010-02-07 01:37:48 +00:00
|
|
|
#define ENABLE_RELOCATABLE 0
|
2009-11-25 23:10:02 +00:00
|
|
|
#include "progname.h"
|
|
|
|
|
2006-06-13 22:50:01 +00:00
|
|
|
/* Include malloc.h, only if memalign is available. It is known that
|
|
|
|
memalign is declared in malloc.h in all systems, if present. */
|
|
|
|
#ifdef HAVE_MEMALIGN
|
|
|
|
# include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-21 07:48:28 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winioctl.h>
|
|
|
|
#endif
|
|
|
|
|
2010-02-07 03:23:44 +00:00
|
|
|
#ifdef GRUB_UTIL
|
2008-08-14 18:46:47 +00:00
|
|
|
int
|
|
|
|
grub_err_printf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-08-14 18:46:47 +00:00
|
|
|
va_start (ap, fmt);
|
|
|
|
ret = vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-07 03:23:44 +00:00
|
|
|
#endif
|
2008-08-14 18:46:47 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_get_path (const char *dir, const char *file)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
char *path;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
|
|
|
|
sprintf (path, "%s/%s", dir, file);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
size_t
|
|
|
|
grub_util_get_fp_size (FILE *fp)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
if (fflush (fp) == EOF)
|
|
|
|
grub_util_error ("fflush failed");
|
|
|
|
|
|
|
|
if (fstat (fileno (fp), &st) == -1)
|
|
|
|
grub_util_error ("fstat failed");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
return st.st_size;
|
|
|
|
}
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
size_t
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_get_image_size (const char *path)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("getting the size of %s", path);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
if (stat (path, &st) == -1)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("cannot stat %s", path);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
return st.st_size;
|
|
|
|
}
|
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
void
|
|
|
|
grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
|
|
|
|
{
|
2008-04-02 01:49:18 +00:00
|
|
|
if (fseeko (fp, offset, SEEK_SET) == -1)
|
|
|
|
grub_util_error ("seek failed");
|
2005-01-04 14:01:45 +00:00
|
|
|
|
|
|
|
if (fread (img, 1, size, fp) != size)
|
|
|
|
grub_util_error ("read failed");
|
|
|
|
}
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_read_image (const char *path)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
|
|
|
char *img;
|
|
|
|
FILE *fp;
|
|
|
|
size_t size;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("reading %s", path);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
size = grub_util_get_image_size (path);
|
2002-12-27 08:53:07 +00:00
|
|
|
img = (char *) xmalloc (size);
|
|
|
|
|
|
|
|
fp = fopen (path, "rb");
|
|
|
|
if (! fp)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("cannot open %s", path);
|
2005-01-04 14:01:45 +00:00
|
|
|
|
|
|
|
grub_util_read_at (img, size, 0, fp);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
2003-01-31 03:26:56 +00:00
|
|
|
fclose (fp);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2003-01-31 03:26:56 +00:00
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_load_image (const char *path, char *buf)
|
2003-01-31 03:26:56 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
size_t size;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("reading %s", path);
|
2003-01-31 03:26:56 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
size = grub_util_get_image_size (path);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-31 03:26:56 +00:00
|
|
|
fp = fopen (path, "rb");
|
|
|
|
if (! fp)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("cannot open %s", path);
|
2003-01-31 03:26:56 +00:00
|
|
|
|
|
|
|
if (fread (buf, 1, size, fp) != size)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("cannot read %s", path);
|
2003-01-31 03:26:56 +00:00
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
void
|
2005-01-04 14:01:45 +00:00
|
|
|
grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
|
2002-12-27 08:53:07 +00:00
|
|
|
{
|
2005-01-04 14:01:45 +00:00
|
|
|
grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
|
2008-04-02 01:49:18 +00:00
|
|
|
if (fseeko (out, offset, SEEK_SET) == -1)
|
2005-01-04 14:01:45 +00:00
|
|
|
grub_util_error ("seek failed");
|
2002-12-27 08:53:07 +00:00
|
|
|
if (fwrite (img, 1, size, out) != size)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("write failed");
|
2002-12-27 08:53:07 +00:00
|
|
|
}
|
|
|
|
|
2005-01-04 14:01:45 +00:00
|
|
|
void
|
|
|
|
grub_util_write_image (const char *img, size_t size, FILE *out)
|
|
|
|
{
|
2005-01-05 13:33:16 +00:00
|
|
|
grub_util_info ("writing 0x%x bytes", size);
|
|
|
|
if (fwrite (img, 1, size, out) != size)
|
|
|
|
grub_util_error ("write failed");
|
2005-01-04 14:01:45 +00:00
|
|
|
}
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
/* Some functions that we don't use. */
|
2003-01-02 20:12:33 +00:00
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_mm_init_region (void *addr __attribute__ ((unused)),
|
|
|
|
grub_size_t size __attribute__ ((unused)))
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_register_exported_symbols (void)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2003-11-17 18:07:09 +00:00
|
|
|
}
|
|
|
|
|
2009-03-21 07:48:28 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_millisleep (grub_uint32_t ms)
|
|
|
|
{
|
|
|
|
Sleep (ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2009-03-04 10:25:31 +00:00
|
|
|
void
|
|
|
|
grub_millisleep (grub_uint32_t ms)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = ms / 1000;
|
|
|
|
ts.tv_nsec = (ms % 1000) * 1000000;
|
|
|
|
nanosleep (&ts, NULL);
|
|
|
|
}
|
|
|
|
|
2009-03-21 07:48:28 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-29 19:55:23 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
|
|
|
void sync (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-04-04 07:42:13 +00:00
|
|
|
int fsync (int fno __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-29 19:55:23 +00:00
|
|
|
void sleep (int s)
|
|
|
|
{
|
|
|
|
Sleep (s * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_int64_t
|
|
|
|
grub_util_get_disk_size (char *name)
|
|
|
|
{
|
|
|
|
HANDLE hd;
|
|
|
|
grub_int64_t size = -1LL;
|
|
|
|
|
|
|
|
hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
0, OPEN_EXISTING, 0, 0);
|
|
|
|
|
|
|
|
if (hd == INVALID_HANDLE_VALUE)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
if (((name[0] == '/') || (name[0] == '\\')) &&
|
|
|
|
((name[1] == '/') || (name[1] == '\\')) &&
|
|
|
|
(name[2] == '.') &&
|
|
|
|
((name[3] == '/') || (name[3] == '\\')) &&
|
|
|
|
(! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
|
|
|
|
{
|
|
|
|
DWORD nr;
|
|
|
|
DISK_GEOMETRY g;
|
|
|
|
|
|
|
|
if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
|
|
|
0, 0, &g, sizeof (g), &nr, 0))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
size = g.Cylinders.QuadPart;
|
|
|
|
size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LARGE_INTEGER s;
|
|
|
|
|
|
|
|
s.LowPart = GetFileSize (hd, &s.HighPart);
|
|
|
|
size = s.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
CloseHandle (hd);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-07-01 13:01:33 +00:00
|
|
|
#endif /* __MINGW32__ */
|
2009-11-08 00:49:15 +00:00
|
|
|
|
2010-02-07 03:23:44 +00:00
|
|
|
#ifdef GRUB_UTIL
|
2010-01-01 20:32:30 +00:00
|
|
|
void
|
|
|
|
grub_util_init_nls (void)
|
|
|
|
{
|
2010-03-27 11:19:32 +00:00
|
|
|
#if (defined(ENABLE_NLS) && ENABLE_NLS)
|
2010-01-01 20:32:30 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2010-03-27 11:19:32 +00:00
|
|
|
#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
|
2010-01-01 20:32:30 +00:00
|
|
|
}
|
2010-02-07 03:23:44 +00:00
|
|
|
#endif
|
2010-04-27 08:27:14 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
grub_dl_ref (grub_dl_t mod)
|
|
|
|
{
|
|
|
|
(void) mod;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
grub_dl_unref (grub_dl_t mod)
|
|
|
|
{
|
|
|
|
(void) mod;
|
|
|
|
return 0;
|
|
|
|
}
|