Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / misc.c
blob93fa0b4bdc1bb12ea690253ae38b8b07aaeeb79f
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 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 <errno.h>
22 #include <setjmp.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 #include <time.h>
34 #include <grub/kernel.h>
35 #include <grub/dl.h>
36 #include <grub/misc.h>
37 #include <grub/cache.h>
38 #include <grub/emu/misc.h>
39 #include <grub/util/misc.h>
40 #include <grub/mm.h>
41 #include <grub/term.h>
42 #include <grub/time.h>
43 #include <grub/i18n.h>
44 #include <grub/script_sh.h>
46 #define ENABLE_RELOCATABLE 0
47 #include "progname.h"
49 /* Include malloc.h, only if memalign is available. It is known that
50 memalign is declared in malloc.h in all systems, if present. */
51 #ifdef HAVE_MEMALIGN
52 # include <malloc.h>
53 #endif
55 #ifdef __MINGW32__
56 #include <windows.h>
57 #include <winioctl.h>
58 #include "dirname.h"
59 #endif
61 #ifdef GRUB_UTIL
62 int
63 grub_err_printf (const char *fmt, ...)
65 va_list ap;
66 int ret;
68 va_start (ap, fmt);
69 ret = vfprintf (stderr, fmt, ap);
70 va_end (ap);
72 return ret;
74 #endif
76 char *
77 grub_util_get_path (const char *dir, const char *file)
79 char *path;
81 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
82 sprintf (path, "%s/%s", dir, file);
83 return path;
86 size_t
87 grub_util_get_image_size (const char *path)
89 struct stat st;
91 grub_util_info ("getting the size of %s", path);
93 if (stat (path, &st) == -1)
94 grub_util_error (_("cannot stat `%s': %s"), path, strerror (errno));
96 return st.st_size;
99 char *
100 grub_util_read_image (const char *path)
102 char *img;
103 FILE *fp;
104 size_t size;
106 grub_util_info ("reading %s", path);
108 size = grub_util_get_image_size (path);
109 img = (char *) xmalloc (size);
111 fp = fopen (path, "rb");
112 if (! fp)
113 grub_util_error (_("cannot open `%s': %s"), path,
114 strerror (errno));
116 if (fread (img, 1, size, fp) != size)
117 grub_util_error (_("cannot read `%s': %s"), path,
118 strerror (errno));
120 fclose (fp);
122 return img;
125 void
126 grub_util_load_image (const char *path, char *buf)
128 FILE *fp;
129 size_t size;
131 grub_util_info ("reading %s", path);
133 size = grub_util_get_image_size (path);
135 fp = fopen (path, "rb");
136 if (! fp)
137 grub_util_error (_("cannot open `%s': %s"), path,
138 strerror (errno));
140 if (fread (buf, 1, size, fp) != size)
141 grub_util_error (_("cannot read `%s': %s"), path,
142 strerror (errno));
144 fclose (fp);
147 void
148 grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out,
149 const char *name)
151 grub_util_info ("writing 0x%" PRIxGRUB_SIZE " bytes at offset 0x%llx",
152 size, (unsigned long long) offset);
153 if (fseeko (out, offset, SEEK_SET) == -1)
154 grub_util_error (_("cannot seek `%s': %s"),
155 name, strerror (errno));
156 if (fwrite (img, 1, size, out) != size)
157 grub_util_error (_("cannot write to `%s': %s"),
158 name, strerror (errno));
161 void
162 grub_util_write_image (const char *img, size_t size, FILE *out,
163 const char *name)
165 grub_util_info ("writing 0x%" PRIxGRUB_SIZE " bytes", size);
166 if (fwrite (img, 1, size, out) != size)
168 if (!name)
169 grub_util_error (_("cannot write to the stdout: %s"),
170 strerror (errno));
171 else
172 grub_util_error (_("cannot write to `%s': %s"),
173 name, strerror (errno));
177 grub_err_t
178 grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
180 return 0;
183 grub_err_t
184 grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
186 return 0;
189 grub_err_t
190 grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
192 return 0;
195 grub_err_t
196 grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
198 return 0;
201 grub_err_t
202 grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
204 return 0;
207 grub_err_t
208 grub_script_execute (struct grub_script *script)
210 if (script == 0 || script->cmd == 0)
211 return 0;
213 return script->cmd->exec (script->cmd);
217 grub_getkey (void)
219 return -1;
222 void
223 grub_refresh (void)
225 fflush (stdout);
228 static void
229 grub_xputs_real (const char *str)
231 fputs (str, stdout);
234 void (*grub_xputs) (const char *str) = grub_xputs_real;
237 grub_dl_ref (grub_dl_t mod)
239 (void) mod;
240 return 0;
244 grub_dl_unref (grub_dl_t mod)
246 (void) mod;
247 return 0;
250 /* Some functions that we don't use. */
251 void
252 grub_mm_init_region (void *addr __attribute__ ((unused)),
253 grub_size_t size __attribute__ ((unused)))
257 void
258 grub_register_exported_symbols (void)
262 #ifdef __MINGW32__
264 void
265 grub_millisleep (grub_uint32_t ms)
267 Sleep (ms);
270 #else
272 void
273 grub_millisleep (grub_uint32_t ms)
275 struct timespec ts;
277 ts.tv_sec = ms / 1000;
278 ts.tv_nsec = (ms % 1000) * 1000000;
279 nanosleep (&ts, NULL);
282 #endif
284 #ifdef __MINGW32__
286 void sync (void)
290 int fsync (int fno __attribute__ ((unused)))
292 return 0;
295 grub_int64_t
296 grub_util_get_disk_size (char *name)
298 HANDLE hd;
299 grub_int64_t size = -1LL;
301 strip_trailing_slashes(name);
302 hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
303 0, OPEN_EXISTING, 0, 0);
305 if (hd == INVALID_HANDLE_VALUE)
306 return size;
308 if (((name[0] == '/') || (name[0] == '\\')) &&
309 ((name[1] == '/') || (name[1] == '\\')) &&
310 (name[2] == '.') &&
311 ((name[3] == '/') || (name[3] == '\\')) &&
312 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
314 DWORD nr;
315 DISK_GEOMETRY g;
317 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
318 0, 0, &g, sizeof (g), &nr, 0))
319 goto fail;
321 size = g.Cylinders.QuadPart;
322 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
324 else
326 LARGE_INTEGER s;
328 s.LowPart = GetFileSize (hd, &s.HighPart);
329 size = s.QuadPart;
332 fail:
334 CloseHandle (hd);
336 return size;
339 #endif /* __MINGW32__ */
341 #ifdef GRUB_UTIL
342 void
343 grub_util_init_nls (void)
345 #if (defined(ENABLE_NLS) && ENABLE_NLS)
346 setlocale (LC_ALL, "");
347 bindtextdomain (PACKAGE, LOCALEDIR);
348 textdomain (PACKAGE);
349 #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
351 #endif