2009-06-10 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / util / misc.c
blob939867b7f181ffe7d6943a356e0f606814ab08e8
1 /*
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/>.
19 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include <time.h>
31 #include <grub/kernel.h>
32 #include <grub/misc.h>
33 #include <grub/cache.h>
34 #include <grub/util/misc.h>
35 #include <grub/mm.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. */
42 #ifdef HAVE_MEMALIGN
43 # include <malloc.h>
44 #endif
46 #ifdef __MINGW32__
47 #include <windows.h>
48 #include <winioctl.h>
49 #endif
51 char *progname = 0;
52 int verbosity = 0;
54 void
55 grub_util_warn (const char *fmt, ...)
57 va_list ap;
59 fprintf (stderr, "%s: warn: ", progname);
60 va_start (ap, fmt);
61 vfprintf (stderr, fmt, ap);
62 va_end (ap);
63 fputc ('\n', stderr);
64 fflush (stderr);
67 void
68 grub_util_info (const char *fmt, ...)
70 if (verbosity > 0)
72 va_list ap;
74 fprintf (stderr, "%s: info: ", progname);
75 va_start (ap, fmt);
76 vfprintf (stderr, fmt, ap);
77 va_end (ap);
78 fputc ('\n', stderr);
79 fflush (stderr);
83 void
84 grub_util_error (const char *fmt, ...)
86 va_list ap;
88 fprintf (stderr, "%s: error: ", progname);
89 va_start (ap, fmt);
90 vfprintf (stderr, fmt, ap);
91 va_end (ap);
92 fputc ('\n', stderr);
93 exit (1);
96 int
97 grub_err_printf (const char *fmt, ...)
99 va_list ap;
100 int ret;
102 va_start (ap, fmt);
103 ret = vfprintf (stderr, fmt, ap);
104 va_end (ap);
106 return ret;
109 void *
110 xmalloc (size_t size)
112 void *p;
114 p = malloc (size);
115 if (! p)
116 grub_util_error ("out of memory");
118 return p;
121 void *
122 xrealloc (void *ptr, size_t size)
124 ptr = realloc (ptr, size);
125 if (! ptr)
126 grub_util_error ("out of memory");
128 return ptr;
131 char *
132 xstrdup (const char *str)
134 size_t len;
135 char *dup;
137 len = strlen (str);
138 dup = (char *) xmalloc (len + 1);
139 memcpy (dup, str, len + 1);
141 return dup;
144 char *
145 grub_util_get_path (const char *dir, const char *file)
147 char *path;
149 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
150 sprintf (path, "%s/%s", dir, file);
151 return path;
154 size_t
155 grub_util_get_fp_size (FILE *fp)
157 struct stat st;
159 if (fflush (fp) == EOF)
160 grub_util_error ("fflush failed");
162 if (fstat (fileno (fp), &st) == -1)
163 grub_util_error ("fstat failed");
165 return st.st_size;
168 size_t
169 grub_util_get_image_size (const char *path)
171 struct stat st;
173 grub_util_info ("getting the size of %s", path);
175 if (stat (path, &st) == -1)
176 grub_util_error ("cannot stat %s", path);
178 return st.st_size;
181 void
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");
191 char *
192 grub_util_read_image (const char *path)
194 char *img;
195 FILE *fp;
196 size_t size;
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");
204 if (! fp)
205 grub_util_error ("cannot open %s", path);
207 grub_util_read_at (img, size, 0, fp);
209 fclose (fp);
211 return img;
214 void
215 grub_util_load_image (const char *path, char *buf)
217 FILE *fp;
218 size_t size;
220 grub_util_info ("reading %s", path);
222 size = grub_util_get_image_size (path);
224 fp = fopen (path, "rb");
225 if (! fp)
226 grub_util_error ("cannot open %s", path);
228 if (fread (buf, 1, size, fp) != size)
229 grub_util_error ("cannot read %s", path);
231 fclose (fp);
234 void
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");
244 void
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");
252 void *
253 grub_malloc (grub_size_t size)
255 return xmalloc (size);
258 void
259 grub_free (void *ptr)
261 free (ptr);
264 void *
265 grub_realloc (void *ptr, grub_size_t size)
267 return xrealloc (ptr, size);
270 void *
271 grub_memalign (grub_size_t align, grub_size_t size)
273 void *p;
275 #if defined(HAVE_POSIX_MEMALIGN)
276 if (posix_memalign (&p, align, size) != 0)
277 p = 0;
278 #elif defined(HAVE_MEMALIGN)
279 p = memalign (align, size);
280 #else
281 (void) align;
282 (void) size;
283 grub_util_error ("grub_memalign is not supported");
284 #endif
286 if (! p)
287 grub_util_error ("out of memory");
289 return p;
292 /* Some functions that we don't use. */
293 void
294 grub_mm_init_region (void *addr __attribute__ ((unused)),
295 grub_size_t size __attribute__ ((unused)))
299 void
300 grub_register_exported_symbols (void)
304 void
305 grub_exit (void)
307 exit (1);
310 grub_uint32_t
311 grub_get_rtc (void)
313 struct timeval tv;
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));
322 grub_uint64_t
323 grub_get_time_ms (void)
325 struct timeval tv;
327 gettimeofday (&tv, 0);
329 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
332 #ifdef __MINGW32__
334 void
335 grub_millisleep (grub_uint32_t ms)
337 Sleep (ms);
340 #else
342 void
343 grub_millisleep (grub_uint32_t ms)
345 struct timespec ts;
347 ts.tv_sec = ms / 1000;
348 ts.tv_nsec = (ms % 1000) * 1000000;
349 nanosleep (&ts, NULL);
352 #endif
354 void
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, ...)
365 int status;
366 va_list ap;
368 /* Should be large enough. */
369 *buf = xmalloc (512);
371 va_start (ap, fmt);
372 status = vsprintf (*buf, fmt, ap);
373 va_end (ap);
375 return status;
378 #endif
380 #ifdef __MINGW32__
382 void sync (void)
386 int fsync (int fno __attribute__ ((unused)))
388 return 0;
391 void sleep (int s)
393 Sleep (s * 1000);
396 grub_int64_t
397 grub_util_get_disk_size (char *name)
399 HANDLE hd;
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)
406 return size;
408 if (((name[0] == '/') || (name[0] == '\\')) &&
409 ((name[1] == '/') || (name[1] == '\\')) &&
410 (name[2] == '.') &&
411 ((name[3] == '/') || (name[3] == '\\')) &&
412 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
414 DWORD nr;
415 DISK_GEOMETRY g;
417 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
418 0, 0, &g, sizeof (g), &nr, 0))
419 goto fail;
421 size = g.Cylinders.QuadPart;
422 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
424 else
426 LARGE_INTEGER s;
428 s.LowPart = GetFileSize (hd, &s.HighPart);
429 size = s.QuadPart;
432 fail:
434 CloseHandle (hd);
436 return size;
439 #endif