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. */
63 grub_err_printf (const char *fmt
, ...)
69 ret
= vfprintf (stderr
, fmt
, ap
);
77 grub_util_get_path (const char *dir
, const char *file
)
81 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
82 sprintf (path
, "%s/%s", dir
, file
);
87 grub_util_get_image_size (const char *path
)
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
));
100 grub_util_read_image (const char *path
)
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");
113 grub_util_error (_("cannot open `%s': %s"), path
,
116 if (fread (img
, 1, size
, fp
) != size
)
117 grub_util_error (_("cannot read `%s': %s"), path
,
126 grub_util_load_image (const char *path
, char *buf
)
131 grub_util_info ("reading %s", path
);
133 size
= grub_util_get_image_size (path
);
135 fp
= fopen (path
, "rb");
137 grub_util_error (_("cannot open `%s': %s"), path
,
140 if (fread (buf
, 1, size
, fp
) != size
)
141 grub_util_error (_("cannot read `%s': %s"), path
,
148 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
,
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
));
162 grub_util_write_image (const char *img
, size_t size
, FILE *out
,
165 grub_util_info ("writing 0x%" PRIxGRUB_SIZE
" bytes", size
);
166 if (fwrite (img
, 1, size
, out
) != size
)
169 grub_util_error (_("cannot write to the stdout: %s"),
172 grub_util_error (_("cannot write to `%s': %s"),
173 name
, strerror (errno
));
178 grub_script_execute_cmdline (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
184 grub_script_execute_cmdlist (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
190 grub_script_execute_cmdif (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
196 grub_script_execute_cmdfor (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
202 grub_script_execute_cmdwhile (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
208 grub_script_execute (struct grub_script
*script
)
210 if (script
== 0 || script
->cmd
== 0)
213 return script
->cmd
->exec (script
->cmd
);
229 grub_xputs_real (const char *str
)
234 void (*grub_xputs
) (const char *str
) = grub_xputs_real
;
237 grub_dl_ref (grub_dl_t mod
)
244 grub_dl_unref (grub_dl_t mod
)
250 /* Some functions that we don't use. */
252 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
253 grub_size_t size
__attribute__ ((unused
)))
258 grub_register_exported_symbols (void)
265 grub_millisleep (grub_uint32_t ms
)
273 grub_millisleep (grub_uint32_t ms
)
277 ts
.tv_sec
= ms
/ 1000;
278 ts
.tv_nsec
= (ms
% 1000) * 1000000;
279 nanosleep (&ts
, NULL
);
290 int fsync (int fno
__attribute__ ((unused
)))
296 grub_util_get_disk_size (char *name
)
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
)
308 if (((name
[0] == '/') || (name
[0] == '\\')) &&
309 ((name
[1] == '/') || (name
[1] == '\\')) &&
311 ((name
[3] == '/') || (name
[3] == '\\')) &&
312 (! strncasecmp (name
+ 4, "PHYSICALDRIVE", 13)))
317 if (! DeviceIoControl (hd
, IOCTL_DISK_GET_DRIVE_GEOMETRY
,
318 0, 0, &g
, sizeof (g
), &nr
, 0))
321 size
= g
.Cylinders
.QuadPart
;
322 size
*= g
.TracksPerCylinder
* g
.SectorsPerTrack
* g
.BytesPerSector
;
328 s
.LowPart
= GetFileSize (hd
, &s
.HighPart
);
339 #endif /* __MINGW32__ */
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) */