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>
32 #include <grub/kernel.h>
34 #include <grub/misc.h>
35 #include <grub/cache.h>
36 #include <grub/emu/misc.h>
37 #include <grub/util/misc.h>
39 #include <grub/term.h>
40 #include <grub/time.h>
41 #include <grub/i18n.h>
42 #include <grub/script_sh.h>
43 #include <grub/emu/hostfile.h>
45 #define ENABLE_RELOCATABLE 0
47 const char *program_name
= GRUB_BUILD_PROGRAM_NAME
;
54 grub_err_printf (const char *fmt
, ...)
60 ret
= vfprintf (stderr
, fmt
, ap
);
68 grub_util_get_path (const char *dir
, const char *file
)
72 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
73 sprintf (path
, "%s/%s", dir
, file
);
78 grub_util_get_image_size (const char *path
)
84 f
= grub_util_fopen (path
, "rb");
87 grub_util_error (_("cannot open `%s': %s"), path
, strerror (errno
));
89 fseeko (f
, 0, SEEK_END
);
93 grub_util_error (_("cannot open `%s': %s"), path
, strerror (errno
));
94 if (sz
!= (size_t) sz
)
95 grub_util_error (_("file `%s' is too big"), path
);
104 grub_util_read_image (const char *path
)
110 grub_util_info ("reading %s", path
);
112 size
= grub_util_get_image_size (path
);
113 img
= (char *) xmalloc (size
);
115 fp
= grub_util_fopen (path
, "rb");
117 grub_util_error (_("cannot open `%s': %s"), path
,
120 if (fread (img
, 1, size
, fp
) != size
)
121 grub_util_error (_("cannot read `%s': %s"), path
,
130 grub_util_load_image (const char *path
, char *buf
)
135 grub_util_info ("reading %s", path
);
137 size
= grub_util_get_image_size (path
);
139 fp
= grub_util_fopen (path
, "rb");
141 grub_util_error (_("cannot open `%s': %s"), path
,
144 if (fread (buf
, 1, size
, fp
) != size
)
145 grub_util_error (_("cannot read `%s': %s"), path
,
152 grub_util_write_image_at (const void *img
, size_t size
, off_t offset
, FILE *out
,
155 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG
" bytes at offset 0x%"
156 GRUB_HOST_PRIxLONG_LONG
,
157 (unsigned long long) size
, (unsigned long long) offset
);
158 if (fseeko (out
, offset
, SEEK_SET
) == -1)
159 grub_util_error (_("cannot seek `%s': %s"),
160 name
, strerror (errno
));
161 if (fwrite (img
, 1, size
, out
) != size
)
162 grub_util_error (_("cannot write to `%s': %s"),
163 name
, strerror (errno
));
167 grub_util_write_image (const char *img
, size_t size
, FILE *out
,
170 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG
" bytes", (unsigned long long) size
);
171 if (fwrite (img
, 1, size
, out
) != size
)
174 grub_util_error (_("cannot write to the stdout: %s"),
177 grub_util_error (_("cannot write to `%s': %s"),
178 name
, strerror (errno
));
183 grub_script_execute_cmdline (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
189 grub_script_execute_cmdlist (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
195 grub_script_execute_cmdif (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
201 grub_script_execute_cmdfor (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
207 grub_script_execute_cmdwhile (struct grub_script_cmd
*cmd
__attribute__ ((unused
)))
213 grub_script_execute (struct grub_script
*script
)
215 if (script
== 0 || script
->cmd
== 0)
218 return script
->cmd
->exec (script
->cmd
);
234 grub_xputs_real (const char *str
)
239 void (*grub_xputs
) (const char *str
) = grub_xputs_real
;
242 grub_dl_ref (grub_dl_t mod
)
249 grub_dl_unref (grub_dl_t mod
)
255 /* Some functions that we don't use. */
257 grub_mm_init_region (void *addr
__attribute__ ((unused
)),
258 grub_size_t size
__attribute__ ((unused
)))
263 grub_register_exported_symbols (void)
267 /* Used in comparison of arrays of strings with qsort */
269 grub_qsort_strcmp (const void *p1
, const void *p2
)
271 return strcmp(*(char *const *)p1
, *(char *const *)p2
);