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_free (void *ptr
)
267 grub_realloc (void *ptr
, grub_size_t size
)
269 return xrealloc (ptr
, size
);
273 grub_memalign (grub_size_t align
, grub_size_t size
)
277 #if defined(HAVE_POSIX_MEMALIGN)
278 if (posix_memalign (&p
, align
, size
) != 0)
280 #elif defined(HAVE_MEMALIGN)
281 p
= memalign (align
, size
);
285 grub_util_error ("grub_memalign is not supported");
289 grub_util_error ("out of memory");
294 /* Some functions that we don't use. */
296 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
297 grub_size_t size
__attribute__ ((unused
)))
302 grub_register_exported_symbols (void)
317 gettimeofday (&tv
, 0);
319 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
320 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
321 * GRUB_TICKS_PER_SECOND
/ 1000000));
325 grub_get_time_ms (void)
329 gettimeofday (&tv
, 0);
331 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
337 grub_millisleep (grub_uint32_t ms
)
345 grub_millisleep (grub_uint32_t ms
)
349 ts
.tv_sec
= ms
/ 1000;
350 ts
.tv_nsec
= (ms
% 1000) * 1000000;
351 nanosleep (&ts
, NULL
);
357 grub_arch_sync_caches (void *address
__attribute__ ((unused
)),
358 grub_size_t len
__attribute__ ((unused
)))
362 #ifndef HAVE_ASPRINTF
365 asprintf (char **buf
, const char *fmt
, ...)
370 /* Should be large enough. */
371 *buf
= xmalloc (512);
374 status
= vsprintf (*buf
, fmt
, ap
);
388 int fsync (int fno
__attribute__ ((unused
)))
399 grub_util_get_disk_size (char *name
)
402 grub_int64_t size
= -1LL;
404 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
405 0, OPEN_EXISTING
, 0, 0);
407 if (hd
== INVALID_HANDLE_VALUE
)
410 if (((name
[0] == '/') || (name
[0] == '\\')) &&
411 ((name
[1] == '/') || (name
[1] == '\\')) &&
413 ((name
[3] == '/') || (name
[3] == '\\')) &&
414 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
419 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
420 0, 0, &g
, sizeof (g
), &nr
, 0))
423 size
= g
.Cylinders
.QuadPart
;
424 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
430 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);
441 #endif /* __MINGW32__ */
446 longjmp (main_env
, 1);
451 #ifdef GRUB_MACHINE_PCBIOS
452 int no_apm
__attribute__ ((unused
))