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/>.
26 #include <sys/types.h>
32 #include <grub/kernel.h>
33 #include <grub/misc.h>
34 #include <grub/cache.h>
35 #include <grub/util/misc.h>
37 #include <grub/term.h>
38 #include <grub/time.h>
39 #include <grub/machine/time.h>
40 #include <grub/machine/machine.h>
42 /* Include malloc.h, only if memalign is available. It is known that
43 memalign is declared in malloc.h in all systems, if present. */
57 grub_util_warn (const char *fmt
, ...)
61 fprintf (stderr
, "%s: warn: ", progname
);
63 vfprintf (stderr
, fmt
, ap
);
70 grub_util_info (const char *fmt
, ...)
76 fprintf (stderr
, "%s: info: ", progname
);
78 vfprintf (stderr
, fmt
, ap
);
86 grub_util_error (const char *fmt
, ...)
90 fprintf (stderr
, "%s: error: ", progname
);
92 vfprintf (stderr
, fmt
, ap
);
99 grub_err_printf (const char *fmt
, ...)
105 ret
= vfprintf (stderr
, fmt
, ap
);
112 xmalloc (size_t size
)
118 grub_util_error ("out of memory");
124 xrealloc (void *ptr
, size_t size
)
126 ptr
= realloc (ptr
, size
);
128 grub_util_error ("out of memory");
134 xstrdup (const char *str
)
140 dup
= (char *) xmalloc (len
+ 1);
141 memcpy (dup
, str
, len
+ 1);
147 grub_util_get_path (const char *dir
, const char *file
)
151 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
152 sprintf (path
, "%s/%s", dir
, file
);
157 grub_util_get_fp_size (FILE *fp
)
161 if (fflush (fp
) == EOF
)
162 grub_util_error ("fflush failed");
164 if (fstat (fileno (fp
), &st
) == -1)
165 grub_util_error ("fstat failed");
171 grub_util_get_image_size (const char *path
)
175 grub_util_info ("getting the size of %s", path
);
177 if (stat (path
, &st
) == -1)
178 grub_util_error ("cannot stat %s", path
);
184 grub_util_read_at (void *img
, size_t size
, off_t offset
, FILE *fp
)
186 if (fseeko (fp
, offset
, SEEK_SET
) == -1)
187 grub_util_error ("seek failed");
189 if (fread (img
, 1, size
, fp
) != size
)
190 grub_util_error ("read failed");
194 grub_util_read_image (const char *path
)
200 grub_util_info ("reading %s", path
);
202 size
= grub_util_get_image_size (path
);
203 img
= (char *) xmalloc (size
);
205 fp
= fopen (path
, "rb");
207 grub_util_error ("cannot open %s", path
);
209 grub_util_read_at (img
, size
, 0, fp
);
217 grub_util_load_image (const char *path
, char *buf
)
222 grub_util_info ("reading %s", path
);
224 size
= grub_util_get_image_size (path
);
226 fp
= fopen (path
, "rb");
228 grub_util_error ("cannot open %s", path
);
230 if (fread (buf
, 1, size
, fp
) != size
)
231 grub_util_error ("cannot read %s", path
);
237 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
)
239 grub_util_info ("writing 0x%x bytes at offset 0x%x", size
, offset
);
240 if (fseeko (out
, offset
, SEEK_SET
) == -1)
241 grub_util_error ("seek failed");
242 if (fwrite (img
, 1, size
, out
) != size
)
243 grub_util_error ("write failed");
247 grub_util_write_image (const char *img
, size_t size
, FILE *out
)
249 grub_util_info ("writing 0x%x bytes", size
);
250 if (fwrite (img
, 1, size
, out
) != size
)
251 grub_util_error ("write failed");
255 grub_malloc (grub_size_t size
)
257 return xmalloc (size
);
261 grub_zalloc (grub_size_t size
)
265 ret
= xmalloc (size
);
266 memset (ret
, 0, size
);
271 grub_free (void *ptr
)
277 grub_realloc (void *ptr
, grub_size_t size
)
279 return xrealloc (ptr
, size
);
283 grub_memalign (grub_size_t align
, grub_size_t size
)
287 #if defined(HAVE_POSIX_MEMALIGN)
288 if (posix_memalign (&p
, align
, size
) != 0)
290 #elif defined(HAVE_MEMALIGN)
291 p
= memalign (align
, size
);
295 grub_util_error ("grub_memalign is not supported");
299 grub_util_error ("out of memory");
304 /* Some functions that we don't use. */
306 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
307 grub_size_t size
__attribute__ ((unused
)))
312 grub_register_exported_symbols (void)
327 gettimeofday (&tv
, 0);
329 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
330 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
331 * GRUB_TICKS_PER_SECOND
/ 1000000));
335 grub_get_time_ms (void)
339 gettimeofday (&tv
, 0);
341 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
347 grub_millisleep (grub_uint32_t ms
)
355 grub_millisleep (grub_uint32_t ms
)
359 ts
.tv_sec
= ms
/ 1000;
360 ts
.tv_nsec
= (ms
% 1000) * 1000000;
361 nanosleep (&ts
, NULL
);
367 grub_arch_sync_caches (void *address
__attribute__ ((unused
)),
368 grub_size_t len
__attribute__ ((unused
)))
372 #ifndef HAVE_ASPRINTF
375 asprintf (char **buf
, const char *fmt
, ...)
380 /* Should be large enough. */
381 *buf
= xmalloc (512);
384 status
= vsprintf (*buf
, fmt
, ap
);
398 int fsync (int fno
__attribute__ ((unused
)))
409 grub_util_get_disk_size (char *name
)
412 grub_int64_t size
= -1LL;
414 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
415 0, OPEN_EXISTING
, 0, 0);
417 if (hd
== INVALID_HANDLE_VALUE
)
420 if (((name
[0] == '/') || (name
[0] == '\\')) &&
421 ((name
[1] == '/') || (name
[1] == '\\')) &&
423 ((name
[3] == '/') || (name
[3] == '\\')) &&
424 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
429 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
430 0, 0, &g
, sizeof (g
), &nr
, 0))
433 size
= g
.Cylinders
.QuadPart
;
434 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
440 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);
451 #endif /* __MINGW32__ */