2009-07-01 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / util / misc.c
blob2d0039028340aa7419a1bbf5874ed5d6ed4b6c2f
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_free (void *ptr)
263 free (ptr);
266 void *
267 grub_realloc (void *ptr, grub_size_t size)
269 return xrealloc (ptr, size);
272 void *
273 grub_memalign (grub_size_t align, grub_size_t size)
275 void *p;
277 #if defined(HAVE_POSIX_MEMALIGN)
278 if (posix_memalign (&p, align, size) != 0)
279 p = 0;
280 #elif defined(HAVE_MEMALIGN)
281 p = memalign (align, size);
282 #else
283 (void) align;
284 (void) size;
285 grub_util_error ("grub_memalign is not supported");
286 #endif
288 if (! p)
289 grub_util_error ("out of memory");
291 return p;
294 /* Some functions that we don't use. */
295 void
296 grub_mm_init_region (void *addr __attribute__ ((unused)),
297 grub_size_t size __attribute__ ((unused)))
301 void
302 grub_register_exported_symbols (void)
306 void
307 grub_exit (void)
309 exit (1);
312 grub_uint32_t
313 grub_get_rtc (void)
315 struct timeval tv;
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));
324 grub_uint64_t
325 grub_get_time_ms (void)
327 struct timeval tv;
329 gettimeofday (&tv, 0);
331 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
334 #ifdef __MINGW32__
336 void
337 grub_millisleep (grub_uint32_t ms)
339 Sleep (ms);
342 #else
344 void
345 grub_millisleep (grub_uint32_t ms)
347 struct timespec ts;
349 ts.tv_sec = ms / 1000;
350 ts.tv_nsec = (ms % 1000) * 1000000;
351 nanosleep (&ts, NULL);
354 #endif
356 void
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, ...)
367 int status;
368 va_list ap;
370 /* Should be large enough. */
371 *buf = xmalloc (512);
373 va_start (ap, fmt);
374 status = vsprintf (*buf, fmt, ap);
375 va_end (ap);
377 return status;
380 #endif
382 #ifdef __MINGW32__
384 void sync (void)
388 int fsync (int fno __attribute__ ((unused)))
390 return 0;
393 void sleep (int s)
395 Sleep (s * 1000);
398 grub_int64_t
399 grub_util_get_disk_size (char *name)
401 HANDLE hd;
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)
408 return size;
410 if (((name[0] == '/') || (name[0] == '\\')) &&
411 ((name[1] == '/') || (name[1] == '\\')) &&
412 (name[2] == '.') &&
413 ((name[3] == '/') || (name[3] == '\\')) &&
414 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
416 DWORD nr;
417 DISK_GEOMETRY g;
419 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
420 0, 0, &g, sizeof (g), &nr, 0))
421 goto fail;
423 size = g.Cylinders.QuadPart;
424 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
426 else
428 LARGE_INTEGER s;
430 s.LowPart = GetFileSize (hd, &s.HighPart);
431 size = s.QuadPart;
434 fail:
436 CloseHandle (hd);
438 return size;
441 #endif /* __MINGW32__ */
443 void
444 grub_reboot (void)
446 longjmp (main_env, 1);
449 void
450 grub_halt (
451 #ifdef GRUB_MACHINE_PCBIOS
452 int no_apm __attribute__ ((unused))
453 #endif
456 grub_reboot ();