2009-03-04 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / util / misc.c
blob8d7d0808b5804fcf2e55901d6859bfbb902ff28c
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 char *progname = 0;
47 int verbosity = 0;
49 void
50 grub_util_info (const char *fmt, ...)
52 if (verbosity > 0)
54 va_list ap;
56 fprintf (stderr, "%s: info: ", progname);
57 va_start (ap, fmt);
58 vfprintf (stderr, fmt, ap);
59 va_end (ap);
60 fputc ('\n', stderr);
61 fflush (stderr);
65 void
66 grub_util_error (const char *fmt, ...)
68 va_list ap;
70 fprintf (stderr, "%s: error: ", progname);
71 va_start (ap, fmt);
72 vfprintf (stderr, fmt, ap);
73 va_end (ap);
74 fputc ('\n', stderr);
75 exit (1);
78 int
79 grub_err_printf (const char *fmt, ...)
81 va_list ap;
82 int ret;
84 va_start (ap, fmt);
85 ret = vfprintf (stderr, fmt, ap);
86 va_end (ap);
88 return ret;
91 void *
92 xmalloc (size_t size)
94 void *p;
96 p = malloc (size);
97 if (! p)
98 grub_util_error ("out of memory");
100 return p;
103 void *
104 xrealloc (void *ptr, size_t size)
106 ptr = realloc (ptr, size);
107 if (! ptr)
108 grub_util_error ("out of memory");
110 return ptr;
113 char *
114 xstrdup (const char *str)
116 size_t len;
117 char *dup;
119 len = strlen (str);
120 dup = (char *) xmalloc (len + 1);
121 memcpy (dup, str, len + 1);
123 return dup;
126 char *
127 grub_util_get_path (const char *dir, const char *file)
129 char *path;
131 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
132 sprintf (path, "%s/%s", dir, file);
133 return path;
136 size_t
137 grub_util_get_fp_size (FILE *fp)
139 struct stat st;
141 if (fflush (fp) == EOF)
142 grub_util_error ("fflush failed");
144 if (fstat (fileno (fp), &st) == -1)
145 grub_util_error ("fstat failed");
147 return st.st_size;
150 size_t
151 grub_util_get_image_size (const char *path)
153 struct stat st;
155 grub_util_info ("getting the size of %s", path);
157 if (stat (path, &st) == -1)
158 grub_util_error ("cannot stat %s", path);
160 return st.st_size;
163 void
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");
173 char *
174 grub_util_read_image (const char *path)
176 char *img;
177 FILE *fp;
178 size_t size;
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");
186 if (! fp)
187 grub_util_error ("cannot open %s", path);
189 grub_util_read_at (img, size, 0, fp);
191 fclose (fp);
193 return img;
196 void
197 grub_util_load_image (const char *path, char *buf)
199 FILE *fp;
200 size_t size;
202 grub_util_info ("reading %s", path);
204 size = grub_util_get_image_size (path);
206 fp = fopen (path, "rb");
207 if (! fp)
208 grub_util_error ("cannot open %s", path);
210 if (fread (buf, 1, size, fp) != size)
211 grub_util_error ("cannot read %s", path);
213 fclose (fp);
216 void
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");
226 void
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");
234 void *
235 grub_malloc (grub_size_t size)
237 return xmalloc (size);
240 void
241 grub_free (void *ptr)
243 free (ptr);
246 void *
247 grub_realloc (void *ptr, grub_size_t size)
249 return xrealloc (ptr, size);
252 void *
253 grub_memalign (grub_size_t align, grub_size_t size)
255 void *p;
257 #if defined(HAVE_POSIX_MEMALIGN)
258 if (posix_memalign (&p, align, size) != 0)
259 p = 0;
260 #elif defined(HAVE_MEMALIGN)
261 p = memalign (align, size);
262 #else
263 (void) align;
264 (void) size;
265 grub_util_error ("grub_memalign is not supported");
266 #endif
268 if (! p)
269 grub_util_error ("out of memory");
271 return p;
274 /* Some functions that we don't use. */
275 void
276 grub_mm_init_region (void *addr __attribute__ ((unused)),
277 grub_size_t size __attribute__ ((unused)))
281 void
282 grub_register_exported_symbols (void)
286 void
287 grub_exit (void)
289 exit (1);
292 grub_uint32_t
293 grub_get_rtc (void)
295 struct timeval tv;
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));
304 grub_uint64_t
305 grub_get_time_ms (void)
307 struct timeval tv;
309 gettimeofday (&tv, 0);
311 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
314 void
315 grub_millisleep (grub_uint32_t ms)
317 struct timespec ts;
319 ts.tv_sec = ms / 1000;
320 ts.tv_nsec = (ms % 1000) * 1000000;
321 nanosleep (&ts, NULL);
324 void
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, ...)
335 int status;
336 va_list ap;
338 /* Should be large enough. */
339 *buf = xmalloc (512);
341 va_start (ap, fmt);
342 status = vsprintf (*buf, fmt, ap);
343 va_end (ap);
345 return status;
348 #endif
350 #ifdef __MINGW32__
352 #include <windows.h>
353 #include <winioctl.h>
355 void sync (void)
359 void sleep (int s)
361 Sleep (s * 1000);
364 grub_int64_t
365 grub_util_get_disk_size (char *name)
367 HANDLE hd;
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)
374 return size;
376 if (((name[0] == '/') || (name[0] == '\\')) &&
377 ((name[1] == '/') || (name[1] == '\\')) &&
378 (name[2] == '.') &&
379 ((name[3] == '/') || (name[3] == '\\')) &&
380 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
382 DWORD nr;
383 DISK_GEOMETRY g;
385 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
386 0, 0, &g, sizeof (g), &nr, 0))
387 goto fail;
389 size = g.Cylinders.QuadPart;
390 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
392 else
394 LARGE_INTEGER s;
396 s.LowPart = GetFileSize (hd, &s.HighPart);
397 size = s.QuadPart;
400 fail:
402 CloseHandle (hd);
404 return size;
407 #endif