2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
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 <config.h>
2N/A
2N/A#include <errno.h>
2N/A#include <setjmp.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <stdarg.h>
2N/A#include <stdint.h>
2N/A#include <string.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/time.h>
2N/A#include <unistd.h>
2N/A#include <time.h>
2N/A
2N/A#include <grub/kernel.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/cache.h>
2N/A#include <grub/emu/misc.h>
2N/A#include <grub/util/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/term.h>
2N/A#include <grub/time.h>
2N/A#include <grub/i18n.h>
2N/A#include <grub/script_sh.h>
2N/A
2N/A#ifdef __sun
2N/A#include <limits.h> /* for PATH_MAX */
2N/A#endif
2N/A
2N/A#define ENABLE_RELOCATABLE 0
2N/A#include "progname.h"
2N/A
2N/A/* Include malloc.h, only if memalign is available. It is known that
2N/A memalign is declared in malloc.h in all systems, if present. */
2N/A#ifdef HAVE_MEMALIGN
2N/A# include <malloc.h>
2N/A#endif
2N/A
2N/A#ifdef __MINGW32__
2N/A#include <windows.h>
2N/A#include <winioctl.h>
2N/A#include "dirname.h"
2N/A#endif
2N/A
2N/A#ifdef GRUB_UTIL
2N/Aint
2N/Agrub_err_printf (const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A int ret;
2N/A
2N/A va_start (ap, fmt);
2N/A ret = vfprintf (stderr, fmt, ap);
2N/A va_end (ap);
2N/A
2N/A return ret;
2N/A}
2N/A#endif
2N/A
2N/Achar *
2N/Agrub_util_get_path (const char *dir, const char *file)
2N/A{
2N/A char *path;
2N/A
2N/A path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
2N/A sprintf (path, "%s/%s", dir, file);
2N/A return path;
2N/A}
2N/A
2N/Asize_t
2N/Agrub_util_get_fp_size (FILE *fp)
2N/A{
2N/A struct stat st;
2N/A
2N/A if (fflush (fp) == EOF)
2N/A grub_util_error (_("fflush failed"));
2N/A
2N/A if (fstat (fileno (fp), &st) == -1)
2N/A grub_util_error (_("fstat failed"));
2N/A
2N/A return st.st_size;
2N/A}
2N/A
2N/Asize_t
2N/Agrub_util_get_image_size (const char *path)
2N/A{
2N/A struct stat st;
2N/A
2N/A grub_util_info ("getting the size of %s", path);
2N/A
2N/A if (stat (path, &st) == -1)
2N/A grub_util_error (_("cannot stat %s"), path);
2N/A
2N/A return st.st_size;
2N/A}
2N/A
2N/Avoid
2N/Agrub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
2N/A{
2N/A if (fseeko (fp, offset, SEEK_SET) == -1)
2N/A grub_util_error (_("seek failed"));
2N/A
2N/A if (fread (img, 1, size, fp) != size)
2N/A grub_util_error (_("read failed"));
2N/A}
2N/A
2N/Achar *
2N/Agrub_util_read_image (const char *path)
2N/A{
2N/A char *img;
2N/A FILE *fp;
2N/A size_t size;
2N/A
2N/A grub_util_info ("reading %s", path);
2N/A
2N/A size = grub_util_get_image_size (path);
2N/A img = (char *) xmalloc (size);
2N/A
2N/A fp = fopen (path, "rb");
2N/A if (! fp)
2N/A grub_util_error (_("cannot open %s"), path);
2N/A
2N/A grub_util_read_at (img, size, 0, fp);
2N/A
2N/A fclose (fp);
2N/A
2N/A return img;
2N/A}
2N/A
2N/Avoid
2N/Agrub_util_load_image (const char *path, char *buf)
2N/A{
2N/A FILE *fp;
2N/A size_t size;
2N/A
2N/A grub_util_info ("reading %s", path);
2N/A
2N/A size = grub_util_get_image_size (path);
2N/A
2N/A fp = fopen (path, "rb");
2N/A if (! fp)
2N/A grub_util_error (_("cannot open %s"), path);
2N/A
2N/A if (fread (buf, 1, size, fp) != size)
2N/A grub_util_error (_("cannot read %s"), path);
2N/A
2N/A fclose (fp);
2N/A}
2N/A
2N/Avoid
2N/Agrub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
2N/A{
2N/A grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
2N/A if (fseeko (out, offset, SEEK_SET) == -1)
2N/A grub_util_error (_("seek failed"));
2N/A if (fwrite (img, 1, size, out) != size)
2N/A grub_util_error (_("write failed"));
2N/A}
2N/A
2N/Avoid
2N/Agrub_util_write_image (const char *img, size_t size, FILE *out)
2N/A{
2N/A grub_util_info ("writing 0x%x bytes", size);
2N/A if (fwrite (img, 1, size, out) != size)
2N/A grub_util_error (_("write failed"));
2N/A}
2N/A
2N/Achar *
2N/Agrub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute_menuentry (struct grub_script_cmd *cmd __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_script_execute (struct grub_script *script)
2N/A{
2N/A if (script == 0 || script->cmd == 0)
2N/A return 0;
2N/A
2N/A return script->cmd->exec (script->cmd);
2N/A}
2N/A
2N/Avoid
2N/Agrub_putchar (int c)
2N/A{
2N/A putchar (c);
2N/A}
2N/A
2N/Aint
2N/Agrub_getkey (void)
2N/A{
2N/A return -1;
2N/A}
2N/A
2N/Avoid
2N/Agrub_refresh (void)
2N/A{
2N/A fflush (stdout);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_xputs_real (const char *str)
2N/A{
2N/A fputs (str, stdout);
2N/A}
2N/A
2N/Avoid (*grub_xputs) (const char *str) = grub_xputs_real;
2N/A
2N/Aint
2N/Agrub_dl_ref (grub_dl_t mod)
2N/A{
2N/A (void) mod;
2N/A return 0;
2N/A}
2N/A
2N/Aint
2N/Agrub_dl_unref (grub_dl_t mod)
2N/A{
2N/A (void) mod;
2N/A return 0;
2N/A}
2N/A
2N/A/* Some functions that we don't use. */
2N/Avoid
2N/Agrub_mm_init_region (void *addr __attribute__ ((unused)),
2N/A grub_size_t size __attribute__ ((unused)))
2N/A{
2N/A}
2N/A
2N/Avoid
2N/Agrub_register_exported_symbols (void)
2N/A{
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
2N/A
2N/A#ifdef __MINGW32__
2N/A
2N/Avoid sync (void)
2N/A{
2N/A}
2N/A
2N/Aint fsync (int fno __attribute__ ((unused)))
2N/A{
2N/A return 0;
2N/A}
2N/A
2N/Agrub_int64_t
2N/Agrub_util_get_disk_size (char *name)
2N/A{
2N/A HANDLE hd;
2N/A grub_int64_t size = -1LL;
2N/A
2N/A strip_trailing_slashes(name);
2N/A hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2N/A 0, OPEN_EXISTING, 0, 0);
2N/A
2N/A if (hd == INVALID_HANDLE_VALUE)
2N/A return size;
2N/A
2N/A if (((name[0] == '/') || (name[0] == '\\')) &&
2N/A ((name[1] == '/') || (name[1] == '\\')) &&
2N/A (name[2] == '.') &&
2N/A ((name[3] == '/') || (name[3] == '\\')) &&
2N/A (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
2N/A {
2N/A DWORD nr;
2N/A DISK_GEOMETRY g;
2N/A
2N/A if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
2N/A 0, 0, &g, sizeof (g), &nr, 0))
2N/A goto fail;
2N/A
2N/A size = g.Cylinders.QuadPart;
2N/A size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
2N/A }
2N/A else
2N/A {
2N/A LARGE_INTEGER s;
2N/A
2N/A s.LowPart = GetFileSize (hd, &s.HighPart);
2N/A size = s.QuadPart;
2N/A }
2N/A
2N/Afail:
2N/A
2N/A CloseHandle (hd);
2N/A
2N/A return size;
2N/A}
2N/A
2N/A#endif /* __MINGW32__ */
2N/A
2N/A#ifdef GRUB_UTIL
2N/Avoid
2N/Agrub_util_init_nls (void)
2N/A{
2N/A#if (defined(ENABLE_NLS) && ENABLE_NLS)
2N/A setlocale (LC_ALL, "");
2N/A bindtextdomain (PACKAGE, LOCALEDIR);
2N/A textdomain (PACKAGE);
2N/A#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
2N/A}
2N/A#endif