]> git.proxmox.com Git - grub2.git/blame - util/misc.c
* grub-core/genmod.sh.in: Strip before converting to ELF as strip
[grub2.git] / util / misc.c
CommitLineData
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>
93f3a1d8 29#include <sys/time.h>
2a54b559 30#include <time.h>
6a161fa9 31
36747a62 32#include <grub/kernel.h>
b77ab1aa 33#include <grub/dl.h>
36747a62 34#include <grub/misc.h>
35#include <grub/cache.h>
8c411768 36#include <grub/emu/misc.h>
4b13b216 37#include <grub/util/misc.h>
38#include <grub/mm.h>
39#include <grub/term.h>
042bd419 40#include <grub/time.h>
2f1a3acf 41#include <grub/i18n.h>
8c411768 42#include <grub/script_sh.h>
cd46aa6c 43#include <grub/emu/hostfile.h>
6a161fa9 44
98e6959d 45#define ENABLE_RELOCATABLE 0
7b780018
VS
46#ifdef GRUB_BUILD
47const char *program_name = GRUB_BUILD_PROGRAM_NAME;
48#else
8a4c07fd 49#include "progname.h"
7b780018 50#endif
8a4c07fd 51
2083672a 52#ifdef GRUB_UTIL
b86408f8 53int
54grub_err_printf (const char *fmt, ...)
55{
56 va_list ap;
57 int ret;
b39f9d20 58
b86408f8 59 va_start (ap, fmt);
60 ret = vfprintf (stderr, fmt, ap);
61 va_end (ap);
62
63 return ret;
64}
2083672a 65#endif
b86408f8 66
6a161fa9 67char *
4b13b216 68grub_util_get_path (const char *dir, const char *file)
6a161fa9 69{
70 char *path;
b39f9d20 71
6a161fa9 72 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
73 sprintf (path, "%s/%s", dir, file);
74 return path;
75}
76
77size_t
4b13b216 78grub_util_get_image_size (const char *path)
6a161fa9 79{
66a1b3ee
VS
80 FILE *f;
81 size_t ret;
b39f9d20 82
66a1b3ee 83 f = grub_util_fopen (path, "rb");
b39f9d20 84
66a1b3ee
VS
85 if (!f)
86 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
b39f9d20 87
66a1b3ee
VS
88 fseeko (f, 0, SEEK_END);
89
90 ret = ftello (f);
91
92 fclose (f);
93
94 return ret;
6a161fa9 95}
96
97char *
4b13b216 98grub_util_read_image (const char *path)
6a161fa9 99{
100 char *img;
101 FILE *fp;
102 size_t size;
b39f9d20 103
4b13b216 104 grub_util_info ("reading %s", path);
6a161fa9 105
4b13b216 106 size = grub_util_get_image_size (path);
6a161fa9 107 img = (char *) xmalloc (size);
108
bb338aaf 109 fp = grub_util_fopen (path, "rb");
6a161fa9 110 if (! fp)
0ae70393
VS
111 grub_util_error (_("cannot open `%s': %s"), path,
112 strerror (errno));
0b412211 113
0ae70393 114 if (fread (img, 1, size, fp) != size)
9c4b5c13 115 grub_util_error (_("cannot read `%s': %s"), path,
0ae70393 116 strerror (errno));
6a161fa9 117
1f5ab428 118 fclose (fp);
b39f9d20 119
6a161fa9 120 return img;
121}
122
1f5ab428 123void
4b13b216 124grub_util_load_image (const char *path, char *buf)
1f5ab428 125{
126 FILE *fp;
127 size_t size;
b39f9d20 128
4b13b216 129 grub_util_info ("reading %s", path);
1f5ab428 130
4b13b216 131 size = grub_util_get_image_size (path);
b39f9d20 132
bb338aaf 133 fp = grub_util_fopen (path, "rb");
1f5ab428 134 if (! fp)
0ae70393
VS
135 grub_util_error (_("cannot open `%s': %s"), path,
136 strerror (errno));
1f5ab428 137
138 if (fread (buf, 1, size, fp) != size)
9c4b5c13 139 grub_util_error (_("cannot read `%s': %s"), path,
0ae70393 140 strerror (errno));
1f5ab428 141
142 fclose (fp);
143}
144
6a161fa9 145void
0ae70393
VS
146grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out,
147 const char *name)
6a161fa9 148{
22f98db2
VS
149 grub_util_info ("writing 0x%llx bytes at offset 0x%llx",
150 (unsigned long long) size, (unsigned long long) offset);
828a2768 151 if (fseeko (out, offset, SEEK_SET) == -1)
9c4b5c13 152 grub_util_error (_("cannot seek `%s': %s"),
0ae70393 153 name, strerror (errno));
6a161fa9 154 if (fwrite (img, 1, size, out) != size)
9c4b5c13 155 grub_util_error (_("cannot write to `%s': %s"),
0ae70393 156 name, strerror (errno));
6a161fa9 157}
158
0b412211 159void
0ae70393
VS
160grub_util_write_image (const char *img, size_t size, FILE *out,
161 const char *name)
0b412211 162{
22f98db2 163 grub_util_info ("writing 0x%llx bytes", (unsigned long long) size);
4ca7004c 164 if (fwrite (img, 1, size, out) != size)
9c4b5c13
VS
165 {
166 if (!name)
167 grub_util_error (_("cannot write to the stdout: %s"),
168 strerror (errno));
169 else
170 grub_util_error (_("cannot write to `%s': %s"),
171 name, strerror (errno));
172 }
0b412211 173}
174
8c411768
BC
175grub_err_t
176grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
1cc73a62 177{
8c411768 178 return 0;
1f7315a3 179}
180
8c411768 181grub_err_t
16c7cb32 182grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
1f7315a3 183{
8c411768 184 return 0;
1cc73a62 185}
8e72a9c0 186
8c411768
BC
187grub_err_t
188grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
8e72a9c0 189{
8c411768
BC
190 return 0;
191}
8e72a9c0 192
8c411768
BC
193grub_err_t
194grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
195{
196 return 0;
197}
b39f9d20 198
8c411768
BC
199grub_err_t
200grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
201{
202 return 0;
8e72a9c0 203}
924b6140 204
8c411768
BC
205grub_err_t
206grub_script_execute (struct grub_script *script)
207{
208 if (script == 0 || script->cmd == 0)
209 return 0;
b39f9d20 210
8c411768 211 return script->cmd->exec (script->cmd);
042bd419 212}
213
8c411768
BC
214int
215grub_getkey (void)
2a54b559 216{
8c411768 217 return -1;
2a54b559 218}
219
b39f9d20 220void
8c411768 221grub_refresh (void)
924b6140 222{
8c411768 223 fflush (stdout);
924b6140 224}
6e5a42fe 225
297f0c2b
BC
226static void
227grub_xputs_real (const char *str)
228{
229 fputs (str, stdout);
230}
231
232void (*grub_xputs) (const char *str) = grub_xputs_real;
233
8c411768
BC
234int
235grub_dl_ref (grub_dl_t mod)
236{
237 (void) mod;
238 return 0;
239}
d6ceebf1
CW
240
241int
8c411768 242grub_dl_unref (grub_dl_t mod)
d6ceebf1 243{
8c411768
BC
244 (void) mod;
245 return 0;
246}
d6ceebf1 247
8c411768
BC
248/* Some functions that we don't use. */
249void
250grub_mm_init_region (void *addr __attribute__ ((unused)),
251 grub_size_t size __attribute__ ((unused)))
252{
d6ceebf1
CW
253}
254
8c411768 255void
4b13b216 256grub_register_exported_symbols (void)
8c411768
BC
257{
258}
3100cdc7
AB
259
260/* Used in comparison of arrays of strings with qsort */
261int
262grub_qsort_strcmp (const void *p1, const void *p2)
263{
264 return strcmp(*(char **)p1, *(char **)p2);
265}
266