tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / util / misc.c
blobd545212d90ec4b69a1150ede1f8a033a8812d019
1 /*
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/>.
19 #include <config.h>
21 #include <errno.h>
22 #include <setjmp.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <time.h>
32 #include <grub/kernel.h>
33 #include <grub/dl.h>
34 #include <grub/misc.h>
35 #include <grub/cache.h>
36 #include <grub/emu/misc.h>
37 #include <grub/util/misc.h>
38 #include <grub/mm.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
46 #ifdef GRUB_BUILD
47 const char *program_name = GRUB_BUILD_PROGRAM_NAME;
48 #else
49 #include "progname.h"
50 #endif
52 #ifdef GRUB_UTIL
53 int
54 grub_err_printf (const char *fmt, ...)
56 va_list ap;
57 int ret;
59 va_start (ap, fmt);
60 ret = vfprintf (stderr, fmt, ap);
61 va_end (ap);
63 return ret;
65 #endif
67 char *
68 grub_util_get_path (const char *dir, const char *file)
70 char *path;
72 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
73 sprintf (path, "%s/%s", dir, file);
74 return path;
77 char *
78 grub_util_read_image (const char *path)
80 char *img;
81 FILE *fp;
82 size_t size;
84 grub_util_info ("reading %s", path);
86 size = grub_util_get_image_size (path);
87 img = (char *) xmalloc (size);
89 fp = grub_util_fopen (path, "rb");
90 if (! fp)
91 grub_util_error (_("cannot open `%s': %s"), path,
92 strerror (errno));
94 if (fread (img, 1, size, fp) != size)
95 grub_util_error (_("cannot read `%s': %s"), path,
96 strerror (errno));
98 fclose (fp);
100 return img;
103 void
104 grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out,
105 const char *name)
107 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes at offset 0x%"
108 GRUB_HOST_PRIxLONG_LONG,
109 (unsigned long long) size, (unsigned long long) offset);
110 if (fseeko (out, offset, SEEK_SET) == -1)
111 grub_util_error (_("cannot seek `%s': %s"),
112 name, strerror (errno));
113 if (fwrite (img, 1, size, out) != size)
114 grub_util_error (_("cannot write to `%s': %s"),
115 name, strerror (errno));
118 void
119 grub_util_write_image (const char *img, size_t size, FILE *out,
120 const char *name)
122 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes", (unsigned long long) size);
123 if (fwrite (img, 1, size, out) != size)
125 if (!name)
126 grub_util_error (_("cannot write to the stdout: %s"),
127 strerror (errno));
128 else
129 grub_util_error (_("cannot write to `%s': %s"),
130 name, strerror (errno));
134 grub_err_t
135 grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
137 return 0;
140 grub_err_t
141 grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
143 return 0;
146 grub_err_t
147 grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
149 return 0;
152 grub_err_t
153 grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
155 return 0;
158 grub_err_t
159 grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
161 return 0;
164 grub_err_t
165 grub_script_execute (struct grub_script *script)
167 if (script == 0 || script->cmd == 0)
168 return 0;
170 return script->cmd->exec (script->cmd);
174 grub_getkey (void)
176 return -1;
179 void
180 grub_refresh (void)
182 fflush (stdout);
185 static void
186 grub_xputs_real (const char *str)
188 fputs (str, stdout);
191 void (*grub_xputs) (const char *str) = grub_xputs_real;
194 grub_dl_ref (grub_dl_t mod)
196 (void) mod;
197 return 0;
201 grub_dl_unref (grub_dl_t mod)
203 (void) mod;
204 return 0;
207 /* Some functions that we don't use. */
208 void
209 grub_mm_init_region (void *addr __attribute__ ((unused)),
210 grub_size_t size __attribute__ ((unused)))
214 void
215 grub_register_exported_symbols (void)
219 /* Used in comparison of arrays of strings with qsort */
221 grub_qsort_strcmp (const void *p1, const void *p2)
223 return strcmp(*(char *const *)p1, *(char *const *)p2);