2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2002,2003,2005 Free Software Foundation, Inc.
5
* GRUB is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
#include <sys/types.h>
30
#include <grub/util/misc.h>
32
#include <grub/term.h>
33
#include <grub/machine/time.h>
39
grub_util_info (const char *fmt, ...)
45
fprintf (stderr, "%s: info: ", progname);
47
vfprintf (stderr, fmt, ap);
54
grub_util_error (const char *fmt, ...)
58
fprintf (stderr, "%s: error: ", progname);
60
vfprintf (stderr, fmt, ap);
73
grub_util_error ("out of memory");
79
xrealloc (void *ptr, size_t size)
81
ptr = realloc (ptr, size);
83
grub_util_error ("out of memory");
89
xstrdup (const char *str)
95
dup = (char *) xmalloc (len + 1);
96
memcpy (dup, str, len + 1);
102
grub_util_get_path (const char *dir, const char *file)
106
path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
107
sprintf (path, "%s/%s", dir, file);
112
grub_util_get_fp_size (FILE *fp)
116
if (fflush (fp) == EOF)
117
grub_util_error ("fflush failed");
119
if (fstat (fileno (fp), &st) == -1)
120
grub_util_error ("fstat failed");
126
grub_util_get_image_size (const char *path)
130
grub_util_info ("getting the size of %s", path);
132
if (stat (path, &st) == -1)
133
grub_util_error ("cannot stat %s", path);
139
grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
141
if (fseek (fp, offset, SEEK_SET) == -1)
142
grub_util_error ("fseek failed");
144
if (fread (img, 1, size, fp) != size)
145
grub_util_error ("read failed");
149
grub_util_read_image (const char *path)
155
grub_util_info ("reading %s", path);
157
size = grub_util_get_image_size (path);
158
img = (char *) xmalloc (size);
160
fp = fopen (path, "rb");
162
grub_util_error ("cannot open %s", path);
164
grub_util_read_at (img, size, 0, fp);
172
grub_util_load_image (const char *path, char *buf)
177
grub_util_info ("reading %s", path);
179
size = grub_util_get_image_size (path);
181
fp = fopen (path, "rb");
183
grub_util_error ("cannot open %s", path);
185
if (fread (buf, 1, size, fp) != size)
186
grub_util_error ("cannot read %s", path);
192
grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
194
grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
195
if (fseek (out, offset, SEEK_SET) == -1)
196
grub_util_error ("seek failed");
197
if (fwrite (img, 1, size, out) != size)
198
grub_util_error ("write failed");
202
grub_util_write_image (const char *img, size_t size, FILE *out)
204
grub_util_info ("writing 0x%x bytes", size);
205
if (fwrite (img, 1, size, out) != size)
206
grub_util_error ("write failed");
210
grub_malloc (grub_size_t size)
212
return xmalloc (size);
216
grub_free (void *ptr)
222
grub_realloc (void *ptr, grub_size_t size)
224
return xrealloc (ptr, size);
228
grub_memalign (grub_size_t align, grub_size_t size)
232
p = memalign (align, size);
234
grub_util_error ("out of memory");
239
/* Some functions that we don't use. */
241
grub_mm_init_region (void *addr __attribute__ ((unused)),
242
grub_size_t size __attribute__ ((unused)))
247
grub_register_exported_symbols (void)
262
gettimeofday (&tv, 0);
264
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
265
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
266
* GRUB_TICKS_PER_SECOND / 1000000));
270
grub_arch_sync_caches (void *address __attribute__ ((unused)),
271
grub_size_t len __attribute__ ((unused)))