]> git.proxmox.com Git - grub2.git/blob - util/misc.c
cfbae609b711a03e40003d6f223ded0c244a4111
[grub2.git] / util / misc.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19 #include <config.h>
20
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>
33
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>
45
46 #define ENABLE_RELOCATABLE 0
47 #include "progname.h"
48
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
54
55 #ifdef __MINGW32__
56 #include <windows.h>
57 #include <winioctl.h>
58 #endif
59
60 #ifdef GRUB_UTIL
61 int
62 grub_err_printf (const char *fmt, ...)
63 {
64 va_list ap;
65 int ret;
66
67 va_start (ap, fmt);
68 ret = vfprintf (stderr, fmt, ap);
69 va_end (ap);
70
71 return ret;
72 }
73 #endif
74
75 char *
76 grub_util_get_path (const char *dir, const char *file)
77 {
78 char *path;
79
80 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
81 sprintf (path, "%s/%s", dir, file);
82 return path;
83 }
84
85 size_t
86 grub_util_get_fp_size (FILE *fp)
87 {
88 struct stat st;
89
90 if (fflush (fp) == EOF)
91 grub_util_error ("fflush failed");
92
93 if (fstat (fileno (fp), &st) == -1)
94 grub_util_error ("fstat failed");
95
96 return st.st_size;
97 }
98
99 size_t
100 grub_util_get_image_size (const char *path)
101 {
102 struct stat st;
103
104 grub_util_info ("getting the size of %s", path);
105
106 if (stat (path, &st) == -1)
107 grub_util_error ("cannot stat %s", path);
108
109 return st.st_size;
110 }
111
112 void
113 grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
114 {
115 if (fseeko (fp, offset, SEEK_SET) == -1)
116 grub_util_error ("seek failed");
117
118 if (fread (img, 1, size, fp) != size)
119 grub_util_error ("read failed");
120 }
121
122 char *
123 grub_util_read_image (const char *path)
124 {
125 char *img;
126 FILE *fp;
127 size_t size;
128
129 grub_util_info ("reading %s", path);
130
131 size = grub_util_get_image_size (path);
132 img = (char *) xmalloc (size);
133
134 fp = fopen (path, "rb");
135 if (! fp)
136 grub_util_error ("cannot open %s", path);
137
138 grub_util_read_at (img, size, 0, fp);
139
140 fclose (fp);
141
142 return img;
143 }
144
145 void
146 grub_util_load_image (const char *path, char *buf)
147 {
148 FILE *fp;
149 size_t size;
150
151 grub_util_info ("reading %s", path);
152
153 size = grub_util_get_image_size (path);
154
155 fp = fopen (path, "rb");
156 if (! fp)
157 grub_util_error ("cannot open %s", path);
158
159 if (fread (buf, 1, size, fp) != size)
160 grub_util_error ("cannot read %s", path);
161
162 fclose (fp);
163 }
164
165 void
166 grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
167 {
168 grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
169 if (fseeko (out, offset, SEEK_SET) == -1)
170 grub_util_error ("seek failed");
171 if (fwrite (img, 1, size, out) != size)
172 grub_util_error ("write failed");
173 }
174
175 void
176 grub_util_write_image (const char *img, size_t size, FILE *out)
177 {
178 grub_util_info ("writing 0x%x bytes", size);
179 if (fwrite (img, 1, size, out) != size)
180 grub_util_error ("write failed");
181 }
182
183 char *
184 grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused)))
185 {
186 return 0;
187 }
188
189 grub_err_t
190 grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
191 {
192 return 0;
193 }
194
195 grub_err_t
196 grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
197 {
198 return 0;
199 }
200
201 grub_err_t
202 grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
203 {
204 return 0;
205 }
206
207 grub_err_t
208 grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
209 {
210 return 0;
211 }
212
213 grub_err_t
214 grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
215 {
216 return 0;
217 }
218
219 grub_err_t
220 grub_script_execute_menuentry (struct grub_script_cmd *cmd __attribute__ ((unused)))
221 {
222 return 0;
223 }
224
225 grub_err_t
226 grub_script_execute (struct grub_script *script)
227 {
228 if (script == 0 || script->cmd == 0)
229 return 0;
230
231 return script->cmd->exec (script->cmd);
232 }
233
234 void
235 grub_putchar (int c)
236 {
237 putchar (c);
238 }
239
240 int
241 grub_getkey (void)
242 {
243 return -1;
244 }
245
246 void
247 grub_refresh (void)
248 {
249 fflush (stdout);
250 }
251
252 static void
253 grub_xputs_real (const char *str)
254 {
255 fputs (str, stdout);
256 }
257
258 void (*grub_xputs) (const char *str) = grub_xputs_real;
259
260 int
261 grub_dl_ref (grub_dl_t mod)
262 {
263 (void) mod;
264 return 0;
265 }
266
267 int
268 grub_dl_unref (grub_dl_t mod)
269 {
270 (void) mod;
271 return 0;
272 }
273
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)))
278 {
279 }
280
281 void
282 grub_register_exported_symbols (void)
283 {
284 }
285
286 #ifdef __MINGW32__
287
288 void
289 grub_millisleep (grub_uint32_t ms)
290 {
291 Sleep (ms);
292 }
293
294 #else
295
296 void
297 grub_millisleep (grub_uint32_t ms)
298 {
299 struct timespec ts;
300
301 ts.tv_sec = ms / 1000;
302 ts.tv_nsec = (ms % 1000) * 1000000;
303 nanosleep (&ts, NULL);
304 }
305
306 #endif
307
308 #ifdef __MINGW32__
309
310 void sync (void)
311 {
312 }
313
314 int fsync (int fno __attribute__ ((unused)))
315 {
316 return 0;
317 }
318
319 void sleep (int s)
320 {
321 Sleep (s * 1000);
322 }
323
324 grub_int64_t
325 grub_util_get_disk_size (char *name)
326 {
327 HANDLE hd;
328 grub_int64_t size = -1LL;
329
330 hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
331 0, OPEN_EXISTING, 0, 0);
332
333 if (hd == INVALID_HANDLE_VALUE)
334 return size;
335
336 if (((name[0] == '/') || (name[0] == '\\')) &&
337 ((name[1] == '/') || (name[1] == '\\')) &&
338 (name[2] == '.') &&
339 ((name[3] == '/') || (name[3] == '\\')) &&
340 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
341 {
342 DWORD nr;
343 DISK_GEOMETRY g;
344
345 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
346 0, 0, &g, sizeof (g), &nr, 0))
347 goto fail;
348
349 size = g.Cylinders.QuadPart;
350 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
351 }
352 else
353 {
354 LARGE_INTEGER s;
355
356 s.LowPart = GetFileSize (hd, &s.HighPart);
357 size = s.QuadPart;
358 }
359
360 fail:
361
362 CloseHandle (hd);
363
364 return size;
365 }
366
367 #endif /* __MINGW32__ */
368
369 #ifdef GRUB_UTIL
370 void
371 grub_util_init_nls (void)
372 {
373 #if (defined(ENABLE_NLS) && ENABLE_NLS)
374 setlocale (LC_ALL, "");
375 bindtextdomain (PACKAGE, LOCALEDIR);
376 textdomain (PACKAGE);
377 #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
378 }
379 #endif