]>
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> |
ea4a7e35 GS |
33 | #ifdef HAVE_LIMITS_H |
34 | #include <limits.h> | |
35 | #endif | |
6a161fa9 | 36 | |
36747a62 | 37 | #include <grub/kernel.h> |
b77ab1aa | 38 | #include <grub/dl.h> |
36747a62 | 39 | #include <grub/misc.h> |
40 | #include <grub/cache.h> | |
4b13b216 | 41 | #include <grub/util/misc.h> |
42 | #include <grub/mm.h> | |
43 | #include <grub/term.h> | |
042bd419 | 44 | #include <grub/time.h> |
2f1a3acf | 45 | #include <grub/i18n.h> |
6a161fa9 | 46 | |
98e6959d | 47 | #define ENABLE_RELOCATABLE 0 |
8a4c07fd RM |
48 | #include "progname.h" |
49 | ||
4889bdec | 50 | /* Include malloc.h, only if memalign is available. It is known that |
51 | memalign is declared in malloc.h in all systems, if present. */ | |
52 | #ifdef HAVE_MEMALIGN | |
53 | # include <malloc.h> | |
54 | #endif | |
55 | ||
1f4147aa | 56 | #ifdef __MINGW32__ |
57 | #include <windows.h> | |
58 | #include <winioctl.h> | |
59 | #endif | |
60 | ||
2083672a | 61 | #ifdef GRUB_UTIL |
b86408f8 | 62 | int |
63 | grub_err_printf (const char *fmt, ...) | |
64 | { | |
65 | va_list ap; | |
66 | int ret; | |
b39f9d20 | 67 | |
b86408f8 | 68 | va_start (ap, fmt); |
69 | ret = vfprintf (stderr, fmt, ap); | |
70 | va_end (ap); | |
71 | ||
72 | return ret; | |
73 | } | |
2083672a | 74 | #endif |
b86408f8 | 75 | |
6a161fa9 | 76 | char * |
4b13b216 | 77 | grub_util_get_path (const char *dir, const char *file) |
6a161fa9 | 78 | { |
79 | char *path; | |
b39f9d20 | 80 | |
6a161fa9 | 81 | path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1); |
82 | sprintf (path, "%s/%s", dir, file); | |
83 | return path; | |
84 | } | |
85 | ||
0b412211 | 86 | size_t |
87 | grub_util_get_fp_size (FILE *fp) | |
88 | { | |
89 | struct stat st; | |
b39f9d20 | 90 | |
0b412211 | 91 | if (fflush (fp) == EOF) |
92 | grub_util_error ("fflush failed"); | |
93 | ||
94 | if (fstat (fileno (fp), &st) == -1) | |
95 | grub_util_error ("fstat failed"); | |
b39f9d20 | 96 | |
0b412211 | 97 | return st.st_size; |
98 | } | |
99 | ||
6a161fa9 | 100 | size_t |
4b13b216 | 101 | grub_util_get_image_size (const char *path) |
6a161fa9 | 102 | { |
103 | struct stat st; | |
b39f9d20 | 104 | |
4b13b216 | 105 | grub_util_info ("getting the size of %s", path); |
b39f9d20 | 106 | |
6a161fa9 | 107 | if (stat (path, &st) == -1) |
4b13b216 | 108 | grub_util_error ("cannot stat %s", path); |
b39f9d20 | 109 | |
6a161fa9 | 110 | return st.st_size; |
111 | } | |
112 | ||
0b412211 | 113 | void |
114 | grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp) | |
115 | { | |
828a2768 | 116 | if (fseeko (fp, offset, SEEK_SET) == -1) |
117 | grub_util_error ("seek failed"); | |
0b412211 | 118 | |
119 | if (fread (img, 1, size, fp) != size) | |
120 | grub_util_error ("read failed"); | |
121 | } | |
122 | ||
6a161fa9 | 123 | char * |
4b13b216 | 124 | grub_util_read_image (const char *path) |
6a161fa9 | 125 | { |
126 | char *img; | |
127 | FILE *fp; | |
128 | size_t size; | |
b39f9d20 | 129 | |
4b13b216 | 130 | grub_util_info ("reading %s", path); |
6a161fa9 | 131 | |
4b13b216 | 132 | size = grub_util_get_image_size (path); |
6a161fa9 | 133 | img = (char *) xmalloc (size); |
134 | ||
135 | fp = fopen (path, "rb"); | |
136 | if (! fp) | |
4b13b216 | 137 | grub_util_error ("cannot open %s", path); |
0b412211 | 138 | |
139 | grub_util_read_at (img, size, 0, fp); | |
6a161fa9 | 140 | |
1f5ab428 | 141 | fclose (fp); |
b39f9d20 | 142 | |
6a161fa9 | 143 | return img; |
144 | } | |
145 | ||
1f5ab428 | 146 | void |
4b13b216 | 147 | grub_util_load_image (const char *path, char *buf) |
1f5ab428 | 148 | { |
149 | FILE *fp; | |
150 | size_t size; | |
b39f9d20 | 151 | |
4b13b216 | 152 | grub_util_info ("reading %s", path); |
1f5ab428 | 153 | |
4b13b216 | 154 | size = grub_util_get_image_size (path); |
b39f9d20 | 155 | |
1f5ab428 | 156 | fp = fopen (path, "rb"); |
157 | if (! fp) | |
4b13b216 | 158 | grub_util_error ("cannot open %s", path); |
1f5ab428 | 159 | |
160 | if (fread (buf, 1, size, fp) != size) | |
4b13b216 | 161 | grub_util_error ("cannot read %s", path); |
1f5ab428 | 162 | |
163 | fclose (fp); | |
164 | } | |
165 | ||
6a161fa9 | 166 | void |
0b412211 | 167 | grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out) |
6a161fa9 | 168 | { |
0b412211 | 169 | grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset); |
828a2768 | 170 | if (fseeko (out, offset, SEEK_SET) == -1) |
0b412211 | 171 | grub_util_error ("seek failed"); |
6a161fa9 | 172 | if (fwrite (img, 1, size, out) != size) |
4b13b216 | 173 | grub_util_error ("write failed"); |
6a161fa9 | 174 | } |
175 | ||
0b412211 | 176 | void |
177 | grub_util_write_image (const char *img, size_t size, FILE *out) | |
178 | { | |
4ca7004c | 179 | grub_util_info ("writing 0x%x bytes", size); |
180 | if (fwrite (img, 1, size, out) != size) | |
181 | grub_util_error ("write failed"); | |
0b412211 | 182 | } |
183 | ||
1f7315a3 | 184 | /* Some functions that we don't use. */ |
1cc73a62 | 185 | void |
4b13b216 | 186 | grub_mm_init_region (void *addr __attribute__ ((unused)), |
187 | grub_size_t size __attribute__ ((unused))) | |
1cc73a62 | 188 | { |
1cc73a62 | 189 | } |
190 | ||
f38873b8 | 191 | #if GRUB_NO_MODULES |
1cc73a62 | 192 | void |
4b13b216 | 193 | grub_register_exported_symbols (void) |
1cc73a62 | 194 | { |
1f7315a3 | 195 | } |
f38873b8 | 196 | #endif |
1f7315a3 | 197 | |
1f4147aa | 198 | #ifdef __MINGW32__ |
199 | ||
200 | void | |
201 | grub_millisleep (grub_uint32_t ms) | |
202 | { | |
203 | Sleep (ms); | |
204 | } | |
205 | ||
206 | #else | |
207 | ||
2a54b559 | 208 | void |
209 | grub_millisleep (grub_uint32_t ms) | |
210 | { | |
211 | struct timespec ts; | |
212 | ||
213 | ts.tv_sec = ms / 1000; | |
214 | ts.tv_nsec = (ms % 1000) * 1000000; | |
215 | nanosleep (&ts, NULL); | |
216 | } | |
217 | ||
1f4147aa | 218 | #endif |
219 | ||
3dca5319 | 220 | #if !(defined (__i386__) || defined (__x86_64__)) && GRUB_NO_MODULES |
b39f9d20 | 221 | void |
924b6140 | 222 | grub_arch_sync_caches (void *address __attribute__ ((unused)), |
223 | grub_size_t len __attribute__ ((unused))) | |
224 | { | |
225 | } | |
8f891adc | 226 | #endif |
6e5a42fe | 227 | |
6e5a42fe | 228 | #ifdef __MINGW32__ |
229 | ||
6e5a42fe | 230 | void sync (void) |
231 | { | |
232 | } | |
233 | ||
da4c0bb6 | 234 | int fsync (int fno __attribute__ ((unused))) |
235 | { | |
236 | return 0; | |
237 | } | |
238 | ||
6e5a42fe | 239 | void sleep (int s) |
240 | { | |
241 | Sleep (s * 1000); | |
242 | } | |
243 | ||
244 | grub_int64_t | |
245 | grub_util_get_disk_size (char *name) | |
246 | { | |
247 | HANDLE hd; | |
248 | grub_int64_t size = -1LL; | |
249 | ||
250 | hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, | |
251 | 0, OPEN_EXISTING, 0, 0); | |
252 | ||
253 | if (hd == INVALID_HANDLE_VALUE) | |
254 | return size; | |
255 | ||
256 | if (((name[0] == '/') || (name[0] == '\\')) && | |
257 | ((name[1] == '/') || (name[1] == '\\')) && | |
258 | (name[2] == '.') && | |
259 | ((name[3] == '/') || (name[3] == '\\')) && | |
260 | (! strncasecmp (name + 4, "PHYSICALDRIVE", 13))) | |
261 | { | |
262 | DWORD nr; | |
263 | DISK_GEOMETRY g; | |
264 | ||
265 | if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY, | |
266 | 0, 0, &g, sizeof (g), &nr, 0)) | |
267 | goto fail; | |
268 | ||
269 | size = g.Cylinders.QuadPart; | |
270 | size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector; | |
271 | } | |
272 | else | |
273 | { | |
274 | LARGE_INTEGER s; | |
275 | ||
276 | s.LowPart = GetFileSize (hd, &s.HighPart); | |
277 | size = s.QuadPart; | |
278 | } | |
279 | ||
280 | fail: | |
281 | ||
282 | CloseHandle (hd); | |
283 | ||
284 | return size; | |
285 | } | |
286 | ||
211d06b5 | 287 | #endif /* __MINGW32__ */ |
50ceeb7c | 288 | |
2083672a | 289 | #ifdef GRUB_UTIL |
2f1a3acf YB |
290 | void |
291 | grub_util_init_nls (void) | |
292 | { | |
394a3120 | 293 | #if (defined(ENABLE_NLS) && ENABLE_NLS) |
2f1a3acf YB |
294 | setlocale (LC_ALL, ""); |
295 | bindtextdomain (PACKAGE, LOCALEDIR); | |
296 | textdomain (PACKAGE); | |
394a3120 | 297 | #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */ |
2f1a3acf | 298 | } |
2083672a | 299 | #endif |
4c7085f8 BC |
300 | |
301 | int | |
302 | grub_dl_ref (grub_dl_t mod) | |
303 | { | |
304 | (void) mod; | |
305 | return 0; | |
306 | } | |
307 | ||
308 | int | |
309 | grub_dl_unref (grub_dl_t mod) | |
310 | { | |
311 | (void) mod; | |
312 | return 0; | |
313 | } |