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/>.
28 #include <sys/types.h>
34 #include <grub/kernel.h>
36 #include <grub/misc.h>
37 #include <grub/cache.h>
38 #include <grub/emu/misc.h>
39 #include <grub/util/misc.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
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. */
62 grub_err_printf (const char *fmt
, ...)
68 ret
= vfprintf (stderr
, fmt
, ap
);
76 grub_util_get_path (const char *dir
, const char *file
)
80 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
81 sprintf (path
, "%s/%s", dir
, file
);
86 grub_util_get_fp_size (FILE *fp
)
90 if (fflush (fp
) == EOF
)
91 grub_util_error ("fflush failed");
93 if (fstat (fileno (fp
), &st
) == -1)
94 grub_util_error ("fstat failed");
100 grub_util_get_image_size (const char *path
)
104 grub_util_info ("getting the size of %s", path
);
106 if (stat (path
, &st
) == -1)
107 grub_util_error ("cannot stat %s", path
);
113 grub_util_read_at (void *img
, size_t size
, off_t offset
, FILE *fp
)
115 if (fseeko (fp
, offset
, SEEK_SET
) == -1)
116 grub_util_error ("seek failed");
118 if (fread (img
, 1, size
, fp
) != size
)
119 grub_util_error ("read failed");
123 grub_util_read_image (const char *path
)
129 grub_util_info ("reading %s", path
);
131 size
= grub_util_get_image_size (path
);
132 img
= (char *) xmalloc (size
);
134 fp
= fopen (path
, "rb");
136 grub_util_error ("cannot open %s", path
);
138 grub_util_read_at (img
, size
, 0, fp
);
146 grub_util_load_image (const char *path
, char *buf
)
151 grub_util_info ("reading %s", path
);
153 size
= grub_util_get_image_size (path
);
155 fp
= fopen (path
, "rb");
157 grub_util_error ("cannot open %s", path
);
159 if (fread (buf
, 1, size
, fp
) != size
)
160 grub_util_error ("cannot read %s", path
);
166 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
)
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");
176 grub_util_write_image (const char *img
, size_t size
, FILE *out
)
178 grub_util_info ("writing 0x%x bytes", size
);
179 if (fwrite (img
, 1, size
, out
) != size
)
180 grub_util_error ("write failed");
184 grub_script_execute_argument_to_string (struct grub_script_arg
*arg
__attribute__ ((unused
)))
190 grub_script_execute_cmdline (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
196 grub_script_execute_cmdlist (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
202 grub_script_execute_cmdif (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
208 grub_script_execute_cmdfor (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
214 grub_script_execute_cmdwhile (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
220 grub_script_execute_menuentry (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
226 grub_script_execute (struct grub_script
*script
)
228 if (script
== 0 || script
->cmd
== 0)
231 return script
->cmd
->exec (script
->cmd
);
253 grub_xputs_real (const char *str
)
258 void (*grub_xputs
) (const char *str
) = grub_xputs_real
;
261 grub_dl_ref (grub_dl_t mod
)
268 grub_dl_unref (grub_dl_t mod
)
274 /* Some functions that we don't use. */
276 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
277 grub_size_t size
__attribute__ ((unused
)))
282 grub_register_exported_symbols (void)
289 grub_millisleep (grub_uint32_t ms
)
297 grub_millisleep (grub_uint32_t ms
)
301 ts
.tv_sec
= ms
/ 1000;
302 ts
.tv_nsec
= (ms
% 1000) * 1000000;
303 nanosleep (&ts
, NULL
);
314 int fsync (int fno
__attribute__ ((unused
)))
325 grub_util_get_disk_size (char *name
)
328 grub_int64_t size
= -1LL;
330 hd
= CreateFile (name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
331 0, OPEN_EXISTING
, 0, 0);
333 if (hd
== INVALID_HANDLE_VALUE
)
336 if (((name
[0] == '/') || (name
[0] == '\\')) &&
337 ((name
[1] == '/') || (name
[1] == '\\')) &&
339 ((name
[3] == '/') || (name
[3] == '\\')) &&
340 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
345 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
346 0, 0, &g
, sizeof (g
), &nr
, 0))
349 size
= g
.Cylinders
.QuadPart
;
350 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
356 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);
367 #endif /* __MINGW32__ */
371 grub_util_init_nls (void)
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) */