2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / util / misc.c
blob37e75311e54e7c44b604a09294b7e23c48b9702f
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 <setjmp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30 #include <time.h>
32 #include <grub/kernel.h>
33 #include <grub/misc.h>
34 #include <grub/cache.h>
35 #include <grub/util/misc.h>
36 #include <grub/mm.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. */
44 #ifdef HAVE_MEMALIGN
45 # include <malloc.h>
46 #endif
48 #ifdef __MINGW32__
49 #include <windows.h>
50 #include <winioctl.h>
51 #endif
53 char *progname = 0;
54 int verbosity = 0;
56 void
57 grub_util_warn (const char *fmt, ...)
59 va_list ap;
61 fprintf (stderr, "%s: warn: ", progname);
62 va_start (ap, fmt);
63 vfprintf (stderr, fmt, ap);
64 va_end (ap);
65 fputc ('\n', stderr);
66 fflush (stderr);
69 void
70 grub_util_info (const char *fmt, ...)
72 if (verbosity > 0)
74 va_list ap;
76 fprintf (stderr, "%s: info: ", progname);
77 va_start (ap, fmt);
78 vfprintf (stderr, fmt, ap);
79 va_end (ap);
80 fputc ('\n', stderr);
81 fflush (stderr);
85 void
86 grub_util_error (const char *fmt, ...)
88 va_list ap;
90 fprintf (stderr, "%s: error: ", progname);
91 va_start (ap, fmt);
92 vfprintf (stderr, fmt, ap);
93 va_end (ap);
94 fputc ('\n', stderr);
95 exit (1);
98 int
99 grub_err_printf (const char *fmt, ...)
101 va_list ap;
102 int ret;
104 va_start (ap, fmt);
105 ret = vfprintf (stderr, fmt, ap);
106 va_end (ap);
108 return ret;
111 void *
112 xmalloc (size_t size)
114 void *p;
116 p = malloc (size);
117 if (! p)
118 grub_util_error ("out of memory");
120 return p;
123 void *
124 xrealloc (void *ptr, size_t size)
126 ptr = realloc (ptr, size);
127 if (! ptr)
128 grub_util_error ("out of memory");
130 return ptr;
133 char *
134 xstrdup (const char *str)
136 size_t len;
137 char *dup;
139 len = strlen (str);
140 dup = (char *) xmalloc (len + 1);
141 memcpy (dup, str, len + 1);
143 return dup;
146 char *
147 grub_util_get_path (const char *dir, const char *file)
149 char *path;
151 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
152 sprintf (path, "%s/%s", dir, file);
153 return path;
156 size_t
157 grub_util_get_fp_size (FILE *fp)
159 struct stat st;
161 if (fflush (fp) == EOF)
162 grub_util_error ("fflush failed");
164 if (fstat (fileno (fp), &st) == -1)
165 grub_util_error ("fstat failed");
167 return st.st_size;
170 size_t
171 grub_util_get_image_size (const char *path)
173 struct stat st;
175 grub_util_info ("getting the size of %s", path);
177 if (stat (path, &st) == -1)
178 grub_util_error ("cannot stat %s", path);
180 return st.st_size;
183 void
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");
193 char *
194 grub_util_read_image (const char *path)
196 char *img;
197 FILE *fp;
198 size_t size;
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");
206 if (! fp)
207 grub_util_error ("cannot open %s", path);
209 grub_util_read_at (img, size, 0, fp);
211 fclose (fp);
213 return img;
216 void
217 grub_util_load_image (const char *path, char *buf)
219 FILE *fp;
220 size_t size;
222 grub_util_info ("reading %s", path);
224 size = grub_util_get_image_size (path);
226 fp = fopen (path, "rb");
227 if (! fp)
228 grub_util_error ("cannot open %s", path);
230 if (fread (buf, 1, size, fp) != size)
231 grub_util_error ("cannot read %s", path);
233 fclose (fp);
236 void
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");
246 void
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");
254 void *
255 grub_malloc (grub_size_t size)
257 return xmalloc (size);
260 void *
261 grub_zalloc (grub_size_t size)
263 void *ret;
265 ret = xmalloc (size);
266 memset (ret, 0, size);
267 return ret;
270 void
271 grub_free (void *ptr)
273 free (ptr);
276 void *
277 grub_realloc (void *ptr, grub_size_t size)
279 return xrealloc (ptr, size);
282 void *
283 grub_memalign (grub_size_t align, grub_size_t size)
285 void *p;
287 #if defined(HAVE_POSIX_MEMALIGN)
288 if (posix_memalign (&p, align, size) != 0)
289 p = 0;
290 #elif defined(HAVE_MEMALIGN)
291 p = memalign (align, size);
292 #else
293 (void) align;
294 (void) size;
295 grub_util_error ("grub_memalign is not supported");
296 #endif
298 if (! p)
299 grub_util_error ("out of memory");
301 return p;
304 /* Some functions that we don't use. */
305 void
306 grub_mm_init_region (void *addr __attribute__ ((unused)),
307 grub_size_t size __attribute__ ((unused)))
311 void
312 grub_register_exported_symbols (void)
316 void
317 grub_exit (void)
319 exit (1);
322 grub_uint32_t
323 grub_get_rtc (void)
325 struct timeval tv;
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));
334 grub_uint64_t
335 grub_get_time_ms (void)
337 struct timeval tv;
339 gettimeofday (&tv, 0);
341 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
344 #ifdef __MINGW32__
346 void
347 grub_millisleep (grub_uint32_t ms)
349 Sleep (ms);
352 #else
354 void
355 grub_millisleep (grub_uint32_t ms)
357 struct timespec ts;
359 ts.tv_sec = ms / 1000;
360 ts.tv_nsec = (ms % 1000) * 1000000;
361 nanosleep (&ts, NULL);
364 #endif
366 void
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, ...)
377 int status;
378 va_list ap;
380 /* Should be large enough. */
381 *buf = xmalloc (512);
383 va_start (ap, fmt);
384 status = vsprintf (*buf, fmt, ap);
385 va_end (ap);
387 return status;
390 #endif
392 #ifdef __MINGW32__
394 void sync (void)
398 int fsync (int fno __attribute__ ((unused)))
400 return 0;
403 void sleep (int s)
405 Sleep (s * 1000);
408 grub_int64_t
409 grub_util_get_disk_size (char *name)
411 HANDLE hd;
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)
418 return size;
420 if (((name[0] == '/') || (name[0] == '\\')) &&
421 ((name[1] == '/') || (name[1] == '\\')) &&
422 (name[2] == '.') &&
423 ((name[3] == '/') || (name[3] == '\\')) &&
424 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
426 DWORD nr;
427 DISK_GEOMETRY g;
429 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
430 0, 0, &g, sizeof (g), &nr, 0))
431 goto fail;
433 size = g.Cylinders.QuadPart;
434 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
436 else
438 LARGE_INTEGER s;
440 s.LowPart = GetFileSize (hd, &s.HighPart);
441 size = s.QuadPart;
444 fail:
446 CloseHandle (hd);
448 return size;
451 #endif /* __MINGW32__ */