]>
Commit | Line | Data |
---|---|---|
6a161fa9 | 1 | /* |
4b13b216 | 2 | * GRUB -- GRand Unified Bootloader |
2f1a3acf | 3 | * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. |
6a161fa9 | 4 | * |
5a79f472 | 5 | * GRUB is free software: you can redistribute it and/or modify |
6a161fa9 | 6 | * it under the terms of the GNU General Public License as published by |
5a79f472 | 7 | * the Free Software Foundation, either version 3 of the License, or |
6a161fa9 | 8 | * (at your option) any later version. |
9 | * | |
5a79f472 | 10 | * GRUB is distributed in the hope that it will be useful, |
6a161fa9 | 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 | |
5a79f472 | 16 | * along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
6a161fa9 | 17 | */ |
18 | ||
4889bdec | 19 | #include <config.h> |
20 | ||
50ceeb7c | 21 | #include <errno.h> |
211d06b5 | 22 | #include <setjmp.h> |
6a161fa9 | 23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <stdarg.h> | |
50ceeb7c | 26 | #include <stdint.h> |
6a161fa9 | 27 | #include <string.h> |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
93f3a1d8 | 30 | #include <sys/time.h> |
0b412211 | 31 | #include <unistd.h> |
2a54b559 | 32 | #include <time.h> |
6a161fa9 | 33 | |
36747a62 | 34 | #include <grub/kernel.h> |
b77ab1aa | 35 | #include <grub/dl.h> |
36747a62 | 36 | #include <grub/misc.h> |
37 | #include <grub/cache.h> | |
8c411768 | 38 | #include <grub/emu/misc.h> |
4b13b216 | 39 | #include <grub/util/misc.h> |
40 | #include <grub/mm.h> | |
41 | #include <grub/term.h> | |
042bd419 | 42 | #include <grub/time.h> |
2f1a3acf | 43 | #include <grub/i18n.h> |
8c411768 | 44 | #include <grub/script_sh.h> |
6a161fa9 | 45 | |
98e6959d | 46 | #define ENABLE_RELOCATABLE 0 |
8a4c07fd RM |
47 | #include "progname.h" |
48 | ||
4889bdec | 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 | ||
1f4147aa | 55 | #ifdef __MINGW32__ |
56 | #include <windows.h> | |
57 | #include <winioctl.h> | |
58 | #endif | |
59 | ||
2083672a | 60 | #ifdef GRUB_UTIL |
b86408f8 | 61 | int |
62 | grub_err_printf (const char *fmt, ...) | |
63 | { | |
64 | va_list ap; | |
65 | int ret; | |
b39f9d20 | 66 | |
b86408f8 | 67 | va_start (ap, fmt); |
68 | ret = vfprintf (stderr, fmt, ap); | |
69 | va_end (ap); | |
70 | ||
71 | return ret; | |
72 | } | |
2083672a | 73 | #endif |
b86408f8 | 74 | |
6a161fa9 | 75 | char * |
4b13b216 | 76 | grub_util_get_path (const char *dir, const char *file) |
6a161fa9 | 77 | { |
78 | char *path; | |
b39f9d20 | 79 | |
6a161fa9 | 80 | path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1); |
81 | sprintf (path, "%s/%s", dir, file); | |
82 | return path; | |
83 | } | |
84 | ||
0b412211 | 85 | size_t |
86 | grub_util_get_fp_size (FILE *fp) | |
87 | { | |
88 | struct stat st; | |
b39f9d20 | 89 | |
0b412211 | 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"); | |
b39f9d20 | 95 | |
0b412211 | 96 | return st.st_size; |
97 | } | |
98 | ||
6a161fa9 | 99 | size_t |
4b13b216 | 100 | grub_util_get_image_size (const char *path) |
6a161fa9 | 101 | { |
102 | struct stat st; | |
b39f9d20 | 103 | |
4b13b216 | 104 | grub_util_info ("getting the size of %s", path); |
b39f9d20 | 105 | |
6a161fa9 | 106 | if (stat (path, &st) == -1) |
4b13b216 | 107 | grub_util_error ("cannot stat %s", path); |
b39f9d20 | 108 | |
6a161fa9 | 109 | return st.st_size; |
110 | } | |
111 | ||
0b412211 | 112 | void |
113 | grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp) | |
114 | { | |
828a2768 | 115 | if (fseeko (fp, offset, SEEK_SET) == -1) |
116 | grub_util_error ("seek failed"); | |
0b412211 | 117 | |
118 | if (fread (img, 1, size, fp) != size) | |
119 | grub_util_error ("read failed"); | |
120 | } | |
121 | ||
6a161fa9 | 122 | char * |
4b13b216 | 123 | grub_util_read_image (const char *path) |
6a161fa9 | 124 | { |
125 | char *img; | |
126 | FILE *fp; | |
127 | size_t size; | |
b39f9d20 | 128 | |
4b13b216 | 129 | grub_util_info ("reading %s", path); |
6a161fa9 | 130 | |
4b13b216 | 131 | size = grub_util_get_image_size (path); |
6a161fa9 | 132 | img = (char *) xmalloc (size); |
133 | ||
134 | fp = fopen (path, "rb"); | |
135 | if (! fp) | |
4b13b216 | 136 | grub_util_error ("cannot open %s", path); |
0b412211 | 137 | |
138 | grub_util_read_at (img, size, 0, fp); | |
6a161fa9 | 139 | |
1f5ab428 | 140 | fclose (fp); |
b39f9d20 | 141 | |
6a161fa9 | 142 | return img; |
143 | } | |
144 | ||
1f5ab428 | 145 | void |
4b13b216 | 146 | grub_util_load_image (const char *path, char *buf) |
1f5ab428 | 147 | { |
148 | FILE *fp; | |
149 | size_t size; | |
b39f9d20 | 150 | |
4b13b216 | 151 | grub_util_info ("reading %s", path); |
1f5ab428 | 152 | |
4b13b216 | 153 | size = grub_util_get_image_size (path); |
b39f9d20 | 154 | |
1f5ab428 | 155 | fp = fopen (path, "rb"); |
156 | if (! fp) | |
4b13b216 | 157 | grub_util_error ("cannot open %s", path); |
1f5ab428 | 158 | |
159 | if (fread (buf, 1, size, fp) != size) | |
4b13b216 | 160 | grub_util_error ("cannot read %s", path); |
1f5ab428 | 161 | |
162 | fclose (fp); | |
163 | } | |
164 | ||
6a161fa9 | 165 | void |
0b412211 | 166 | grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out) |
6a161fa9 | 167 | { |
0b412211 | 168 | grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset); |
828a2768 | 169 | if (fseeko (out, offset, SEEK_SET) == -1) |
0b412211 | 170 | grub_util_error ("seek failed"); |
6a161fa9 | 171 | if (fwrite (img, 1, size, out) != size) |
4b13b216 | 172 | grub_util_error ("write failed"); |
6a161fa9 | 173 | } |
174 | ||
0b412211 | 175 | void |
176 | grub_util_write_image (const char *img, size_t size, FILE *out) | |
177 | { | |
4ca7004c | 178 | grub_util_info ("writing 0x%x bytes", size); |
179 | if (fwrite (img, 1, size, out) != size) | |
180 | grub_util_error ("write failed"); | |
0b412211 | 181 | } |
182 | ||
8c411768 BC |
183 | char * |
184 | grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused))) | |
1cc73a62 | 185 | { |
8c411768 | 186 | return 0; |
1cc73a62 | 187 | } |
188 | ||
8c411768 BC |
189 | grub_err_t |
190 | grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused))) | |
1cc73a62 | 191 | { |
8c411768 | 192 | return 0; |
1f7315a3 | 193 | } |
194 | ||
8c411768 | 195 | grub_err_t |
16c7cb32 | 196 | grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused))) |
1f7315a3 | 197 | { |
8c411768 | 198 | return 0; |
1cc73a62 | 199 | } |
8e72a9c0 | 200 | |
8c411768 BC |
201 | grub_err_t |
202 | grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused))) | |
8e72a9c0 | 203 | { |
8c411768 BC |
204 | return 0; |
205 | } | |
8e72a9c0 | 206 | |
8c411768 BC |
207 | grub_err_t |
208 | grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused))) | |
209 | { | |
210 | return 0; | |
211 | } | |
b39f9d20 | 212 | |
8c411768 BC |
213 | grub_err_t |
214 | grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused))) | |
215 | { | |
216 | return 0; | |
8e72a9c0 | 217 | } |
924b6140 | 218 | |
8c411768 BC |
219 | grub_err_t |
220 | grub_script_execute_menuentry (struct grub_script_cmd *cmd __attribute__ ((unused))) | |
042bd419 | 221 | { |
8c411768 BC |
222 | return 0; |
223 | } | |
042bd419 | 224 | |
8c411768 BC |
225 | grub_err_t |
226 | grub_script_execute (struct grub_script *script) | |
227 | { | |
228 | if (script == 0 || script->cmd == 0) | |
229 | return 0; | |
b39f9d20 | 230 | |
8c411768 | 231 | return script->cmd->exec (script->cmd); |
042bd419 | 232 | } |
233 | ||
1f4147aa | 234 | void |
8c411768 | 235 | grub_putchar (int c) |
1f4147aa | 236 | { |
8c411768 | 237 | putchar (c); |
1f4147aa | 238 | } |
239 | ||
8c411768 BC |
240 | int |
241 | grub_getkey (void) | |
2a54b559 | 242 | { |
8c411768 | 243 | return -1; |
2a54b559 | 244 | } |
245 | ||
b39f9d20 | 246 | void |
8c411768 | 247 | grub_refresh (void) |
924b6140 | 248 | { |
8c411768 | 249 | fflush (stdout); |
924b6140 | 250 | } |
6e5a42fe | 251 | |
297f0c2b BC |
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 | ||
8c411768 BC |
260 | int |
261 | grub_dl_ref (grub_dl_t mod) | |
262 | { | |
263 | (void) mod; | |
264 | return 0; | |
265 | } | |
d6ceebf1 CW |
266 | |
267 | int | |
8c411768 | 268 | grub_dl_unref (grub_dl_t mod) |
d6ceebf1 | 269 | { |
8c411768 BC |
270 | (void) mod; |
271 | return 0; | |
272 | } | |
d6ceebf1 | 273 | |
8c411768 BC |
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 | { | |
d6ceebf1 CW |
279 | } |
280 | ||
8c411768 | 281 | void |
4b13b216 | 282 | grub_register_exported_symbols (void) |
8c411768 BC |
283 | { |
284 | } | |
d6ceebf1 | 285 | |
1f4147aa | 286 | #ifdef __MINGW32__ |
6e5a42fe | 287 | |
1f4147aa | 288 | void |
289 | grub_millisleep (grub_uint32_t ms) | |
6e5a42fe | 290 | { |
1f4147aa | 291 | Sleep (ms); |
292 | } | |
6e5a42fe | 293 | |
1f4147aa | 294 | #else |
6e5a42fe | 295 | |
2a54b559 | 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); | |
6e5a42fe | 304 | } |
305 | ||
306 | #endif | |
307 | ||
308 | #ifdef __MINGW32__ | |
309 | ||
6e5a42fe | 310 | void sync (void) |
311 | { | |
312 | } | |
313 | ||
da4c0bb6 | 314 | int fsync (int fno __attribute__ ((unused))) |
315 | { | |
316 | return 0; | |
317 | } | |
318 | ||
6e5a42fe | 319 | grub_int64_t |
320 | grub_util_get_disk_size (char *name) | |
321 | { | |
322 | HANDLE hd; | |
323 | grub_int64_t size = -1LL; | |
324 | ||
325 | hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, | |
326 | 0, OPEN_EXISTING, 0, 0); | |
327 | ||
328 | if (hd == INVALID_HANDLE_VALUE) | |
329 | return size; | |
330 | ||
331 | if (((name[0] == '/') || (name[0] == '\\')) && | |
332 | ((name[1] == '/') || (name[1] == '\\')) && | |
333 | (name[2] == '.') && | |
334 | ((name[3] == '/') || (name[3] == '\\')) && | |
335 | (! strncasecmp (name + 4, "PHYSICALDRIVE", 13))) | |
336 | { | |
337 | DWORD nr; | |
338 | DISK_GEOMETRY g; | |
339 | ||
340 | if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY, | |
341 | 0, 0, &g, sizeof (g), &nr, 0)) | |
342 | goto fail; | |
343 | ||
344 | size = g.Cylinders.QuadPart; | |
345 | size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector; | |
346 | } | |
347 | else | |
348 | { | |
349 | LARGE_INTEGER s; | |
350 | ||
351 | s.LowPart = GetFileSize (hd, &s.HighPart); | |
352 | size = s.QuadPart; | |
353 | } | |
354 | ||
355 | fail: | |
356 | ||
357 | CloseHandle (hd); | |
358 | ||
359 | return size; | |
360 | } | |
361 | ||
211d06b5 | 362 | #endif /* __MINGW32__ */ |
50ceeb7c | 363 | |
2083672a | 364 | #ifdef GRUB_UTIL |
2f1a3acf YB |
365 | void |
366 | grub_util_init_nls (void) | |
367 | { | |
394a3120 | 368 | #if (defined(ENABLE_NLS) && ENABLE_NLS) |
2f1a3acf YB |
369 | setlocale (LC_ALL, ""); |
370 | bindtextdomain (PACKAGE, LOCALEDIR); | |
371 | textdomain (PACKAGE); | |
394a3120 | 372 | #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */ |
2f1a3acf | 373 | } |
2083672a | 374 | #endif |