misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include <unistd.h>
  32. #include <time.h>
  33. #ifdef HAVE_LIMITS_H
  34. #include <limits.h>
  35. #endif
  36. #include <grub/kernel.h>
  37. #include <grub/dl.h>
  38. #include <grub/misc.h>
  39. #include <grub/cache.h>
  40. #include <grub/util/misc.h>
  41. #include <grub/mm.h>
  42. #include <grub/term.h>
  43. #include <grub/time.h>
  44. #include <grub/i18n.h>
  45. #define ENABLE_RELOCATABLE 0
  46. #include "progname.h"
  47. /* Include malloc.h, only if memalign is available. It is known that
  48. memalign is declared in malloc.h in all systems, if present. */
  49. #ifdef HAVE_MEMALIGN
  50. # include <malloc.h>
  51. #endif
  52. #ifdef __CYGWIN__
  53. # include <sys/cygwin.h>
  54. # define DEV_CYGDRIVE_MAJOR 98
  55. #endif
  56. #ifdef __MINGW32__
  57. #include <windows.h>
  58. #include <winioctl.h>
  59. #endif
  60. int verbosity = 0;
  61. void
  62. grub_util_warn (const char *fmt, ...)
  63. {
  64. va_list ap;
  65. fprintf (stderr, _("%s: warn:"), program_name);
  66. fprintf (stderr, " ");
  67. va_start (ap, fmt);
  68. vfprintf (stderr, fmt, ap);
  69. va_end (ap);
  70. fprintf (stderr, ".\n");
  71. fflush (stderr);
  72. }
  73. void
  74. grub_util_info (const char *fmt, ...)
  75. {
  76. if (verbosity > 0)
  77. {
  78. va_list ap;
  79. fprintf (stderr, _("%s: info:"), program_name);
  80. fprintf (stderr, " ");
  81. va_start (ap, fmt);
  82. vfprintf (stderr, fmt, ap);
  83. va_end (ap);
  84. fprintf (stderr, ".\n");
  85. fflush (stderr);
  86. }
  87. }
  88. void
  89. grub_util_error (const char *fmt, ...)
  90. {
  91. va_list ap;
  92. fprintf (stderr, _("%s: error:"), program_name);
  93. fprintf (stderr, " ");
  94. va_start (ap, fmt);
  95. vfprintf (stderr, fmt, ap);
  96. va_end (ap);
  97. fprintf (stderr, ".\n");
  98. exit (1);
  99. }
  100. #ifdef GRUB_UTIL
  101. int
  102. grub_err_printf (const char *fmt, ...)
  103. {
  104. va_list ap;
  105. int ret;
  106. va_start (ap, fmt);
  107. ret = vfprintf (stderr, fmt, ap);
  108. va_end (ap);
  109. return ret;
  110. }
  111. #endif
  112. void *
  113. xmalloc (size_t size)
  114. {
  115. void *p;
  116. p = malloc (size);
  117. if (! p)
  118. grub_util_error ("out of memory");
  119. return p;
  120. }
  121. void *
  122. xrealloc (void *ptr, size_t size)
  123. {
  124. ptr = realloc (ptr, size);
  125. if (! ptr)
  126. grub_util_error ("out of memory");
  127. return ptr;
  128. }
  129. char *
  130. xstrdup (const char *str)
  131. {
  132. size_t len;
  133. char *newstr;
  134. len = strlen (str);
  135. newstr = (char *) xmalloc (len + 1);
  136. memcpy (newstr, str, len + 1);
  137. return newstr;
  138. }
  139. void *
  140. xmalloc_zero (size_t size)
  141. {
  142. void *p;
  143. p = xmalloc (size);
  144. memset (p, 0, size);
  145. return p;
  146. }
  147. void *
  148. grub_list_reverse (grub_list_t head)
  149. {
  150. grub_list_t prev;
  151. prev = 0;
  152. while (head)
  153. {
  154. grub_list_t temp;
  155. temp = head->next;
  156. head->next = prev;
  157. prev = head;
  158. head = temp;
  159. }
  160. return prev;
  161. }
  162. char *
  163. grub_util_get_path (const char *dir, const char *file)
  164. {
  165. char *path;
  166. path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
  167. sprintf (path, "%s/%s", dir, file);
  168. return path;
  169. }
  170. size_t
  171. grub_util_get_fp_size (FILE *fp)
  172. {
  173. struct stat st;
  174. if (fflush (fp) == EOF)
  175. grub_util_error ("fflush failed");
  176. if (fstat (fileno (fp), &st) == -1)
  177. grub_util_error ("fstat failed");
  178. return st.st_size;
  179. }
  180. size_t
  181. grub_util_get_image_size (const char *path)
  182. {
  183. struct stat st;
  184. grub_util_info ("getting the size of %s", path);
  185. if (stat (path, &st) == -1)
  186. grub_util_error ("cannot stat %s", path);
  187. return st.st_size;
  188. }
  189. void
  190. grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
  191. {
  192. if (fseeko (fp, offset, SEEK_SET) == -1)
  193. grub_util_error ("seek failed");
  194. if (fread (img, 1, size, fp) != size)
  195. grub_util_error ("read failed");
  196. }
  197. char *
  198. grub_util_read_image (const char *path)
  199. {
  200. char *img;
  201. FILE *fp;
  202. size_t size;
  203. grub_util_info ("reading %s", path);
  204. size = grub_util_get_image_size (path);
  205. img = (char *) xmalloc (size);
  206. fp = fopen (path, "rb");
  207. if (! fp)
  208. grub_util_error ("cannot open %s", path);
  209. grub_util_read_at (img, size, 0, fp);
  210. fclose (fp);
  211. return img;
  212. }
  213. void
  214. grub_util_load_image (const char *path, char *buf)
  215. {
  216. FILE *fp;
  217. size_t size;
  218. grub_util_info ("reading %s", path);
  219. size = grub_util_get_image_size (path);
  220. fp = fopen (path, "rb");
  221. if (! fp)
  222. grub_util_error ("cannot open %s", path);
  223. if (fread (buf, 1, size, fp) != size)
  224. grub_util_error ("cannot read %s", path);
  225. fclose (fp);
  226. }
  227. void
  228. grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
  229. {
  230. grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
  231. if (fseeko (out, offset, SEEK_SET) == -1)
  232. grub_util_error ("seek failed");
  233. if (fwrite (img, 1, size, out) != size)
  234. grub_util_error ("write failed");
  235. }
  236. void
  237. grub_util_write_image (const char *img, size_t size, FILE *out)
  238. {
  239. grub_util_info ("writing 0x%x bytes", size);
  240. if (fwrite (img, 1, size, out) != size)
  241. grub_util_error ("write failed");
  242. }
  243. char *
  244. grub_util_get_module_name (const char *str)
  245. {
  246. char *base;
  247. char *ext;
  248. base = strrchr (str, '/');
  249. if (! base)
  250. base = (char *) str;
  251. else
  252. base++;
  253. ext = strrchr (base, '.');
  254. if (ext && strcmp (ext, ".mod") == 0)
  255. {
  256. char *name;
  257. name = xmalloc (ext - base + 1);
  258. memcpy (name, base, ext - base);
  259. name[ext - base] = '\0';
  260. return name;
  261. }
  262. return xstrdup (base);
  263. }
  264. char *
  265. grub_util_get_module_path (const char *prefix, const char *str)
  266. {
  267. char *dir;
  268. char *base;
  269. char *ext;
  270. char *ret;
  271. ext = strrchr (str, '.');
  272. if (ext && strcmp (ext, ".mod") == 0)
  273. base = xstrdup (str);
  274. else
  275. {
  276. base = xmalloc (strlen (str) + 4 + 1);
  277. sprintf (base, "%s.mod", str);
  278. }
  279. dir = strchr (str, '/');
  280. if (dir)
  281. return base;
  282. ret = grub_util_get_path (prefix, base);
  283. free (base);
  284. return ret;
  285. }
  286. /* Some functions that we don't use. */
  287. void
  288. grub_mm_init_region (void *addr __attribute__ ((unused)),
  289. grub_size_t size __attribute__ ((unused)))
  290. {
  291. }
  292. #if GRUB_NO_MODULES
  293. void
  294. grub_register_exported_symbols (void)
  295. {
  296. }
  297. #endif
  298. void
  299. grub_exit (void)
  300. {
  301. exit (1);
  302. }
  303. grub_uint32_t
  304. grub_get_rtc (void)
  305. {
  306. struct timeval tv;
  307. gettimeofday (&tv, 0);
  308. return (tv.tv_sec * GRUB_TICKS_PER_SECOND
  309. + (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
  310. * GRUB_TICKS_PER_SECOND / 1000000));
  311. }
  312. grub_uint64_t
  313. grub_get_time_ms (void)
  314. {
  315. struct timeval tv;
  316. gettimeofday (&tv, 0);
  317. return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  318. }
  319. #ifdef __MINGW32__
  320. void
  321. grub_millisleep (grub_uint32_t ms)
  322. {
  323. Sleep (ms);
  324. }
  325. #else
  326. void
  327. grub_millisleep (grub_uint32_t ms)
  328. {
  329. struct timespec ts;
  330. ts.tv_sec = ms / 1000;
  331. ts.tv_nsec = (ms % 1000) * 1000000;
  332. nanosleep (&ts, NULL);
  333. }
  334. #endif
  335. #if !(defined (__i386__) || defined (__x86_64__)) && GRUB_NO_MODULES
  336. void
  337. grub_arch_sync_caches (void *address __attribute__ ((unused)),
  338. grub_size_t len __attribute__ ((unused)))
  339. {
  340. }
  341. #endif
  342. #ifndef HAVE_VASPRINTF
  343. int
  344. vasprintf (char **buf, const char *fmt, va_list ap)
  345. {
  346. /* Should be large enough. */
  347. *buf = xmalloc (512);
  348. return vsprintf (*buf, fmt, ap);
  349. }
  350. #endif
  351. #ifndef HAVE_ASPRINTF
  352. int
  353. asprintf (char **buf, const char *fmt, ...)
  354. {
  355. int status;
  356. va_list ap;
  357. va_start (ap, fmt);
  358. status = vasprintf (buf, fmt, ap);
  359. va_end (ap);
  360. return status;
  361. }
  362. #endif
  363. char *
  364. xasprintf (const char *fmt, ...)
  365. {
  366. va_list ap;
  367. char *result;
  368. va_start (ap, fmt);
  369. if (vasprintf (&result, fmt, ap) < 0)
  370. {
  371. if (errno == ENOMEM)
  372. grub_util_error ("out of memory");
  373. return NULL;
  374. }
  375. return result;
  376. }
  377. #ifdef __MINGW32__
  378. void sync (void)
  379. {
  380. }
  381. int fsync (int fno __attribute__ ((unused)))
  382. {
  383. return 0;
  384. }
  385. void sleep (int s)
  386. {
  387. Sleep (s * 1000);
  388. }
  389. grub_int64_t
  390. grub_util_get_disk_size (const char *name)
  391. {
  392. HANDLE hd;
  393. grub_int64_t size = -1LL;
  394. hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  395. 0, OPEN_EXISTING, 0, 0);
  396. if (hd == INVALID_HANDLE_VALUE)
  397. return size;
  398. if (((name[0] == '/') || (name[0] == '\\')) &&
  399. ((name[1] == '/') || (name[1] == '\\')) &&
  400. (name[2] == '.') &&
  401. ((name[3] == '/') || (name[3] == '\\')) &&
  402. (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
  403. {
  404. DWORD nr;
  405. DISK_GEOMETRY g;
  406. if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
  407. 0, 0, &g, sizeof (g), &nr, 0))
  408. goto fail;
  409. size = g.Cylinders.QuadPart;
  410. size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
  411. }
  412. else
  413. {
  414. LARGE_INTEGER s;
  415. s.LowPart = GetFileSize (hd, &s.HighPart);
  416. size = s.QuadPart;
  417. }
  418. fail:
  419. CloseHandle (hd);
  420. return size;
  421. }
  422. #endif /* __MINGW32__ */
  423. char *
  424. canonicalize_file_name (const char *path)
  425. {
  426. char *ret;
  427. #ifdef PATH_MAX
  428. ret = xmalloc (PATH_MAX);
  429. ret = realpath (path, ret);
  430. #else
  431. ret = realpath (path, NULL);
  432. #endif
  433. return ret;
  434. }
  435. #ifdef __CYGWIN__
  436. /* Convert POSIX path to Win32 path,
  437. remove drive letter, replace backslashes. */
  438. static char *
  439. get_win32_path (const char *path)
  440. {
  441. char winpath[PATH_MAX];
  442. if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, path, winpath, sizeof(winpath)))
  443. grub_util_error ("cygwin_conv_path() failed");
  444. int len = strlen (winpath);
  445. int offs = (len > 2 && winpath[1] == ':' ? 2 : 0);
  446. int i;
  447. for (i = offs; i < len; i++)
  448. if (winpath[i] == '\\')
  449. winpath[i] = '/';
  450. return xstrdup (winpath + offs);
  451. }
  452. #endif
  453. /* This function never prints trailing slashes (so that its output
  454. can be appended a slash unconditionally). */
  455. char *
  456. make_system_path_relative_to_its_root (const char *path)
  457. {
  458. struct stat st;
  459. char *p, *buf, *buf2, *buf3;
  460. uintptr_t offset = 0;
  461. dev_t num;
  462. size_t len;
  463. /* canonicalize. */
  464. p = canonicalize_file_name (path);
  465. if (p == NULL)
  466. grub_util_error ("failed to get canonical path of %s", path);
  467. len = strlen (p) + 1;
  468. buf = xstrdup (p);
  469. free (p);
  470. if (stat (buf, &st) < 0)
  471. grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
  472. buf2 = xstrdup (buf);
  473. num = st.st_dev;
  474. /* This loop sets offset to the number of chars of the root
  475. directory we're inspecting. */
  476. while (1)
  477. {
  478. p = strrchr (buf, '/');
  479. if (p == NULL)
  480. /* This should never happen. */
  481. grub_util_error ("FIXME: no / in buf. (make_system_path_relative_to_its_root)");
  482. if (p != buf)
  483. *p = 0;
  484. else
  485. *++p = 0;
  486. if (stat (buf, &st) < 0)
  487. grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
  488. /* buf is another filesystem; we found it. */
  489. if (st.st_dev != num)
  490. {
  491. /* offset == 0 means path given is the mount point.
  492. This works around special-casing of "/" in Un*x. This function never
  493. prints trailing slashes (so that its output can be appended a slash
  494. unconditionally). Each slash in is considered a preceding slash, and
  495. therefore the root directory is an empty string. */
  496. if (offset == 0)
  497. {
  498. free (buf);
  499. free (buf2);
  500. return xstrdup ("");
  501. }
  502. else
  503. break;
  504. }
  505. offset = p - buf;
  506. /* offset == 1 means root directory. */
  507. if (offset == 1)
  508. {
  509. /* Include leading slash. */
  510. offset = 0;
  511. break;
  512. }
  513. }
  514. free (buf);
  515. buf3 = xstrdup (buf2 + offset);
  516. free (buf2);
  517. #ifdef __CYGWIN__
  518. if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
  519. {
  520. /* Reached some mount point not below /cygdrive.
  521. GRUB does not know Cygwin's emulated mounts,
  522. convert to Win32 path. */
  523. grub_util_info ("Cygwin path = %s\n", buf3);
  524. char * temp = get_win32_path (buf3);
  525. free (buf3);
  526. buf3 = temp;
  527. }
  528. #endif
  529. /* Remove trailing slashes, return empty string if root directory. */
  530. len = strlen (buf3);
  531. while (len > 0 && buf3[len - 1] == '/')
  532. {
  533. buf3[len - 1] = '\0';
  534. len--;
  535. }
  536. return buf3;
  537. }
  538. #ifdef GRUB_UTIL
  539. void
  540. grub_util_init_nls (void)
  541. {
  542. #if (defined(ENABLE_NLS) && ENABLE_NLS)
  543. setlocale (LC_ALL, "");
  544. bindtextdomain (PACKAGE, LOCALEDIR);
  545. textdomain (PACKAGE);
  546. #endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */
  547. }
  548. #endif