2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009 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>
31 #include <grub/kernel.h>
32 #include <grub/misc.h>
33 #include <grub/cache.h>
34 #include <grub/util/misc.h>
36 #include <grub/term.h>
37 #include <grub/time.h>
38 #include <grub/machine/time.h>
40 /* Include malloc.h, only if memalign is available. It is known that
41 memalign is declared in malloc.h in all systems, if present. */
55 grub_util_warn (const char *fmt
, ...)
59 fprintf (stderr
, "%s: warn: ", progname
);
61 vfprintf (stderr
, fmt
, ap
);
68 grub_util_info (const char *fmt
, ...)
74 fprintf (stderr
, "%s: info: ", progname
);
76 vfprintf (stderr
, fmt
, ap
);
84 grub_util_error (const char *fmt
, ...)
88 fprintf (stderr
, "%s: error: ", progname
);
90 vfprintf (stderr
, fmt
, ap
);
97 grub_err_printf (const char *fmt
, ...)
103 ret
= vfprintf (stderr
, fmt
, ap
);
110 xmalloc (size_t size
)
116 grub_util_error ("out of memory");
122 xrealloc (void *ptr
, size_t size
)
124 ptr
= realloc (ptr
, size
);
126 grub_util_error ("out of memory");
132 xstrdup (const char *str
)
138 dup
= (char *) xmalloc (len
+ 1);
139 memcpy (dup
, str
, len
+ 1);
145 grub_util_get_path (const char *dir
, const char *file
)
149 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
150 sprintf (path
, "%s/%s", dir
, file
);
155 grub_util_get_fp_size (FILE *fp
)
159 if (fflush (fp
) == EOF
)
160 grub_util_error ("fflush failed");
162 if (fstat (fileno (fp
), &st
) == -1)
163 grub_util_error ("fstat failed");
169 grub_util_get_image_size (const char *path
)
173 grub_util_info ("getting the size of %s", path
);
175 if (stat (path
, &st
) == -1)
176 grub_util_error ("cannot stat %s", path
);
182 grub_util_read_at (void *img
, size_t size
, off_t offset
, FILE *fp
)
184 if (fseeko (fp
, offset
, SEEK_SET
) == -1)
185 grub_util_error ("seek failed");
187 if (fread (img
, 1, size
, fp
) != size
)
188 grub_util_error ("read failed");
192 grub_util_read_image (const char *path
)
198 grub_util_info ("reading %s", path
);
200 size
= grub_util_get_image_size (path
);
201 img
= (char *) xmalloc (size
);
203 fp
= fopen (path
, "rb");
205 grub_util_error ("cannot open %s", path
);
207 grub_util_read_at (img
, size
, 0, fp
);
215 grub_util_load_image (const char *path
, char *buf
)
220 grub_util_info ("reading %s", path
);
222 size
= grub_util_get_image_size (path
);
224 fp
= fopen (path
, "rb");
226 grub_util_error ("cannot open %s", path
);
228 if (fread (buf
, 1, size
, fp
) != size
)
229 grub_util_error ("cannot read %s", path
);
235 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
)
237 grub_util_info ("writing 0x%x bytes at offset 0x%x", size
, offset
);
238 if (fseeko (out
, offset
, SEEK_SET
) == -1)
239 grub_util_error ("seek failed");
240 if (fwrite (img
, 1, size
, out
) != size
)
241 grub_util_error ("write failed");
245 grub_util_write_image (const char *img
, size_t size
, FILE *out
)
247 grub_util_info ("writing 0x%x bytes", size
);
248 if (fwrite (img
, 1, size
, out
) != size
)
249 grub_util_error ("write failed");
253 grub_malloc (grub_size_t size
)
255 return xmalloc (size
);
259 grub_free (void *ptr
)
265 grub_realloc (void *ptr
, grub_size_t size
)
267 return xrealloc (ptr
, size
);
271 grub_memalign (grub_size_t align
, grub_size_t size
)
275 #if defined(HAVE_POSIX_MEMALIGN)
276 if (posix_memalign (&p
, align
, size
) != 0)
278 #elif defined(HAVE_MEMALIGN)
279 p
= memalign (align
, size
);
283 grub_util_error ("grub_memalign is not supported");
287 grub_util_error ("out of memory");
292 /* Some functions that we don't use. */
294 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
295 grub_size_t size
__attribute__ ((unused
)))
300 grub_register_exported_symbols (void)
315 gettimeofday (&tv
, 0);
317 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
318 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
319 * GRUB_TICKS_PER_SECOND
/ 1000000));
323 grub_get_time_ms (void)
327 gettimeofday (&tv
, 0);
329 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
335 grub_millisleep (grub_uint32_t ms
)
343 grub_millisleep (grub_uint32_t ms
)
347 ts
.tv_sec
= ms
/ 1000;
348 ts
.tv_nsec
= (ms
% 1000) * 1000000;
349 nanosleep (&ts
, NULL
);
355 grub_arch_sync_caches (void *address
__attribute__ ((unused
)),
356 grub_size_t len
__attribute__ ((unused
)))
360 #ifndef HAVE_ASPRINTF
363 asprintf (char **buf
, const char *fmt
, ...)
368 /* Should be large enough. */
369 *buf
= xmalloc (512);
372 status
= vsprintf (*buf
, fmt
, ap
);
386 int fsync (int fno
__attribute__ ((unused
)))
397 grub_util_get_disk_size (char *name
)
400 grub_int64_t size
= -1LL;
402 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
403 0, OPEN_EXISTING
, 0, 0);
405 if (hd
== INVALID_HANDLE_VALUE
)
408 if (((name
[0] == '/') || (name
[0] == '\\')) &&
409 ((name
[1] == '/') || (name
[1] == '\\')) &&
411 ((name
[3] == '/') || (name
[3] == '\\')) &&
412 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
417 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
418 0, 0, &g
, sizeof (g
), &nr
, 0))
421 size
= g
.Cylinders
.QuadPart
;
422 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
428 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);