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. */
50 grub_util_info (const char *fmt
, ...)
56 fprintf (stderr
, "%s: info: ", progname
);
58 vfprintf (stderr
, fmt
, ap
);
66 grub_util_error (const char *fmt
, ...)
70 fprintf (stderr
, "%s: error: ", progname
);
72 vfprintf (stderr
, fmt
, ap
);
79 grub_err_printf (const char *fmt
, ...)
85 ret
= vfprintf (stderr
, fmt
, ap
);
98 grub_util_error ("out of memory");
104 xrealloc (void *ptr
, size_t size
)
106 ptr
= realloc (ptr
, size
);
108 grub_util_error ("out of memory");
114 xstrdup (const char *str
)
120 dup
= (char *) xmalloc (len
+ 1);
121 memcpy (dup
, str
, len
+ 1);
127 grub_util_get_path (const char *dir
, const char *file
)
131 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
132 sprintf (path
, "%s/%s", dir
, file
);
137 grub_util_get_fp_size (FILE *fp
)
141 if (fflush (fp
) == EOF
)
142 grub_util_error ("fflush failed");
144 if (fstat (fileno (fp
), &st
) == -1)
145 grub_util_error ("fstat failed");
151 grub_util_get_image_size (const char *path
)
155 grub_util_info ("getting the size of %s", path
);
157 if (stat (path
, &st
) == -1)
158 grub_util_error ("cannot stat %s", path
);
164 grub_util_read_at (void *img
, size_t size
, off_t offset
, FILE *fp
)
166 if (fseeko (fp
, offset
, SEEK_SET
) == -1)
167 grub_util_error ("seek failed");
169 if (fread (img
, 1, size
, fp
) != size
)
170 grub_util_error ("read failed");
174 grub_util_read_image (const char *path
)
180 grub_util_info ("reading %s", path
);
182 size
= grub_util_get_image_size (path
);
183 img
= (char *) xmalloc (size
);
185 fp
= fopen (path
, "rb");
187 grub_util_error ("cannot open %s", path
);
189 grub_util_read_at (img
, size
, 0, fp
);
197 grub_util_load_image (const char *path
, char *buf
)
202 grub_util_info ("reading %s", path
);
204 size
= grub_util_get_image_size (path
);
206 fp
= fopen (path
, "rb");
208 grub_util_error ("cannot open %s", path
);
210 if (fread (buf
, 1, size
, fp
) != size
)
211 grub_util_error ("cannot read %s", path
);
217 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
)
219 grub_util_info ("writing 0x%x bytes at offset 0x%x", size
, offset
);
220 if (fseeko (out
, offset
, SEEK_SET
) == -1)
221 grub_util_error ("seek failed");
222 if (fwrite (img
, 1, size
, out
) != size
)
223 grub_util_error ("write failed");
227 grub_util_write_image (const char *img
, size_t size
, FILE *out
)
229 grub_util_info ("writing 0x%x bytes", size
);
230 if (fwrite (img
, 1, size
, out
) != size
)
231 grub_util_error ("write failed");
235 grub_malloc (grub_size_t size
)
237 return xmalloc (size
);
241 grub_free (void *ptr
)
247 grub_realloc (void *ptr
, grub_size_t size
)
249 return xrealloc (ptr
, size
);
253 grub_memalign (grub_size_t align
, grub_size_t size
)
257 #if defined(HAVE_POSIX_MEMALIGN)
258 if (posix_memalign (&p
, align
, size
) != 0)
260 #elif defined(HAVE_MEMALIGN)
261 p
= memalign (align
, size
);
265 grub_util_error ("grub_memalign is not supported");
269 grub_util_error ("out of memory");
274 /* Some functions that we don't use. */
276 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
277 grub_size_t size
__attribute__ ((unused
)))
282 grub_register_exported_symbols (void)
297 gettimeofday (&tv
, 0);
299 return (tv
.tv_sec
* GRUB_TICKS_PER_SECOND
300 + (((tv
.tv_sec
% GRUB_TICKS_PER_SECOND
) * 1000000 + tv
.tv_usec
)
301 * GRUB_TICKS_PER_SECOND
/ 1000000));
305 grub_get_time_ms (void)
309 gettimeofday (&tv
, 0);
311 return (tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000);
315 grub_millisleep (grub_uint32_t ms
)
319 ts
.tv_sec
= ms
/ 1000;
320 ts
.tv_nsec
= (ms
% 1000) * 1000000;
321 nanosleep (&ts
, NULL
);
325 grub_arch_sync_caches (void *address
__attribute__ ((unused
)),
326 grub_size_t len
__attribute__ ((unused
)))
330 #ifndef HAVE_ASPRINTF
333 asprintf (char **buf
, const char *fmt
, ...)
338 /* Should be large enough. */
339 *buf
= xmalloc (512);
342 status
= vsprintf (*buf
, fmt
, ap
);
353 #include <winioctl.h>
365 grub_util_get_disk_size (char *name
)
368 grub_int64_t size
= -1LL;
370 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
371 0, OPEN_EXISTING
, 0, 0);
373 if (hd
== INVALID_HANDLE_VALUE
)
376 if (((name
[0] == '/') || (name
[0] == '\\')) &&
377 ((name
[1] == '/') || (name
[1] == '\\')) &&
379 ((name
[3] == '/') || (name
[3] == '\\')) &&
380 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
385 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
386 0, 0, &g
, sizeof (g
), &nr
, 0))
389 size
= g
.Cylinders
.QuadPart
;
390 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
396 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);