2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008 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 3 of the License, or
8 * (at your option) any later version.
10 * GRUB 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, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
30 #include <grub/kernel.h>
31 #include <grub/misc.h>
32 #include <grub/cache.h>
33 #include <grub/util/misc.h>
35 #include <grub/term.h>
36 #include <grub/time.h>
37 #include <grub/machine/time.h>
39 /* Include malloc.h, only if memalign is available. It is known that
40 memalign is declared in malloc.h in all systems, if present. */
49 grub_util_info (const char *fmt
, ...)
55 fprintf (stderr
, "%s: info: ", progname
);
57 vfprintf (stderr
, fmt
, ap
);
65 grub_util_error (const char *fmt
, ...)
69 fprintf (stderr
, "%s: error: ", progname
);
71 vfprintf (stderr
, fmt
, ap
);
78 grub_err_printf (const char *fmt
, ...)
84 ret
= vfprintf (stderr
, fmt
, ap
);
97 grub_util_error ("out of memory");
103 xrealloc (void *ptr
, size_t size
)
105 ptr
= realloc (ptr
, size
);
107 grub_util_error ("out of memory");
113 xstrdup (const char *str
)
119 dup
= (char *) xmalloc (len
+ 1);
120 memcpy (dup
, str
, len
+ 1);
126 grub_util_get_path (const char *dir
, const char *file
)
130 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
131 sprintf (path
, "%s/%s", dir
, file
);
136 grub_util_get_fp_size (FILE *fp
)
140 if (fflush (fp
) == EOF
)
141 grub_util_error ("fflush failed");
143 if (fstat (fileno (fp
), &st
) == -1)
144 grub_util_error ("fstat failed");
150 grub_util_get_image_size (const char *path
)
154 grub_util_info ("getting the size of %s", path
);
156 if (stat (path
, &st
) == -1)
157 grub_util_error ("cannot stat %s", path
);
163 grub_util_read_at (void *img
, size_t size
, off_t offset
, FILE *fp
)
165 if (fseeko (fp
, offset
, SEEK_SET
) == -1)
166 grub_util_error ("seek failed");
168 if (fread (img
, 1, size
, fp
) != size
)
169 grub_util_error ("read failed");
173 grub_util_read_image (const char *path
)
179 grub_util_info ("reading %s", path
);
181 size
= grub_util_get_image_size (path
);
182 img
= (char *) xmalloc (size
);
184 fp
= fopen (path
, "rb");
186 grub_util_error ("cannot open %s", path
);
188 grub_util_read_at (img
, size
, 0, fp
);
196 grub_util_load_image (const char *path
, char *buf
)
201 grub_util_info ("reading %s", path
);
203 size
= grub_util_get_image_size (path
);
205 fp
= fopen (path
, "rb");
207 grub_util_error ("cannot open %s", path
);
209 if (fread (buf
, 1, size
, fp
) != size
)
210 grub_util_error ("cannot read %s", path
);
216 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
)
218 grub_util_info ("writing 0x%x bytes at offset 0x%x", size
, offset
);
219 if (fseeko (out
, offset
, SEEK_SET
) == -1)
220 grub_util_error ("seek failed");
221 if (fwrite (img
, 1, size
, out
) != size
)
222 grub_util_error ("write failed");
226 grub_util_write_image (const char *img
, size_t size
, FILE *out
)
228 grub_util_info ("writing 0x%x bytes", size
);
229 if (fwrite (img
, 1, size
, out
) != size
)
230 grub_util_error ("write failed");
234 grub_malloc (grub_size_t size
)
236 return xmalloc (size
);
240 grub_free (void *ptr
)
246 grub_realloc (void *ptr
, grub_size_t size
)
248 return xrealloc (ptr
, size
);
252 grub_memalign (grub_size_t align
, grub_size_t size
)
256 #if defined(HAVE_POSIX_MEMALIGN)
257 if (posix_memalign (&p
, align
, size
) != 0)
259 #elif defined(HAVE_MEMALIGN)
260 p
= memalign (align
, size
);
264 grub_util_error ("grub_memalign is not supported");
268 grub_util_error ("out of memory");
273 /* Some functions that we don't use. */
275 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
276 grub_size_t size
__attribute__ ((unused
)))
281 grub_register_exported_symbols (void)
296 gettimeofday (&tv
, 0);
298 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
299 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
300 * GRUB_TICKS_PER_SECOND
/ 1000000));
304 grub_get_time_ms (void)
308 gettimeofday (&tv
, 0);
310 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
314 grub_arch_sync_caches (void *address
__attribute__ ((unused
)),
315 grub_size_t len
__attribute__ ((unused
)))
319 #ifndef HAVE_ASPRINTF
322 asprintf (char **buf
, const char *fmt
, ...)
327 /* Should be large enough. */
328 *buf
= xmalloc (512);
331 status
= vsprintf (*buf
, fmt
, ap
);
342 #include <winioctl.h>
354 grub_util_get_disk_size (char *name
)
357 grub_int64_t size
= -1LL;
359 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
360 0, OPEN_EXISTING
, 0, 0);
362 if (hd
== INVALID_HANDLE_VALUE
)
365 if (((name
[0] == '/') || (name
[0] == '\\')) &&
366 ((name
[1] == '/') || (name
[1] == '\\')) &&
368 ((name
[3] == '/') || (name
[3] == '\\')) &&
369 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
374 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
375 0, 0, &g
, sizeof (g
), &nr
, 0))
378 size
= g
.Cylinders
.QuadPart
;
379 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
385 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);