misc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  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.
  9. *
  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.
  14. *
  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/>.
  17. */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <setjmp.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include <grub/kernel.h>
  30. #include <grub/dl.h>
  31. #include <grub/misc.h>
  32. #include <grub/cache.h>
  33. #include <grub/emu/misc.h>
  34. #include <grub/util/misc.h>
  35. #include <grub/mm.h>
  36. #include <grub/term.h>
  37. #include <grub/time.h>
  38. #include <grub/i18n.h>
  39. #include <grub/script_sh.h>
  40. #include <grub/emu/hostfile.h>
  41. #define ENABLE_RELOCATABLE 0
  42. #ifdef GRUB_BUILD
  43. const char *program_name = GRUB_BUILD_PROGRAM_NAME;
  44. #else
  45. #include "progname.h"
  46. #endif
  47. #ifdef GRUB_UTIL
  48. int
  49. grub_err_printf (const char *fmt, ...)
  50. {
  51. va_list ap;
  52. int ret;
  53. va_start (ap, fmt);
  54. ret = vfprintf (stderr, fmt, ap);
  55. va_end (ap);
  56. return ret;
  57. }
  58. #endif
  59. char *
  60. grub_util_get_path (const char *dir, const char *file)
  61. {
  62. char *path;
  63. path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
  64. sprintf (path, "%s/%s", dir, file);
  65. return path;
  66. }
  67. char *
  68. grub_util_read_image (const char *path)
  69. {
  70. char *img;
  71. FILE *fp;
  72. size_t size;
  73. grub_util_info ("reading %s", path);
  74. size = grub_util_get_image_size (path);
  75. img = (char *) xmalloc (size);
  76. fp = grub_util_fopen (path, "rb");
  77. if (! fp)
  78. grub_util_error (_("cannot open `%s': %s"), path,
  79. strerror (errno));
  80. if (fread (img, 1, size, fp) != size)
  81. grub_util_error (_("cannot read `%s': %s"), path,
  82. strerror (errno));
  83. fclose (fp);
  84. return img;
  85. }
  86. void
  87. grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out,
  88. const char *name)
  89. {
  90. grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes at offset 0x%"
  91. GRUB_HOST_PRIxLONG_LONG,
  92. (unsigned long long) size, (unsigned long long) offset);
  93. if (fseeko (out, offset, SEEK_SET) == -1)
  94. grub_util_error (_("cannot seek `%s': %s"),
  95. name, strerror (errno));
  96. if (fwrite (img, 1, size, out) != size)
  97. grub_util_error (_("cannot write to `%s': %s"),
  98. name, strerror (errno));
  99. }
  100. void
  101. grub_util_write_image (const char *img, size_t size, FILE *out,
  102. const char *name)
  103. {
  104. grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes", (unsigned long long) size);
  105. if (fwrite (img, 1, size, out) != size)
  106. {
  107. if (!name)
  108. grub_util_error (_("cannot write to the stdout: %s"),
  109. strerror (errno));
  110. else
  111. grub_util_error (_("cannot write to `%s': %s"),
  112. name, strerror (errno));
  113. }
  114. }
  115. grub_err_t
  116. grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
  117. {
  118. return 0;
  119. }
  120. grub_err_t
  121. grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
  122. {
  123. return 0;
  124. }
  125. grub_err_t
  126. grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
  127. {
  128. return 0;
  129. }
  130. grub_err_t
  131. grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
  132. {
  133. return 0;
  134. }
  135. grub_err_t
  136. grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
  137. {
  138. return 0;
  139. }
  140. grub_err_t
  141. grub_script_execute (struct grub_script *script)
  142. {
  143. if (script == 0 || script->cmd == 0)
  144. return 0;
  145. return script->cmd->exec (script->cmd);
  146. }
  147. int
  148. grub_getkey (void)
  149. {
  150. return -1;
  151. }
  152. void
  153. grub_refresh (void)
  154. {
  155. fflush (stdout);
  156. }
  157. static void
  158. grub_xputs_real (const char *str)
  159. {
  160. fputs (str, stdout);
  161. }
  162. void (*grub_xputs) (const char *str) = grub_xputs_real;
  163. int
  164. grub_dl_ref (grub_dl_t mod)
  165. {
  166. (void) mod;
  167. return 0;
  168. }
  169. int
  170. grub_dl_unref (grub_dl_t mod)
  171. {
  172. (void) mod;
  173. return 0;
  174. }
  175. /* Some functions that we don't use. */
  176. void
  177. grub_mm_init_region (void *addr __attribute__ ((unused)),
  178. grub_size_t size __attribute__ ((unused)))
  179. {
  180. }
  181. void
  182. grub_register_exported_symbols (void)
  183. {
  184. }
  185. /* Used in comparison of arrays of strings with qsort */
  186. int
  187. grub_qsort_strcmp (const void *p1, const void *p2)
  188. {
  189. return strcmp(*(char *const *)p1, *(char *const *)p2);
  190. }