getroot.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 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-util.h>
  19. #include <config.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <assert.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <dirent.h>
  27. #include <errno.h>
  28. #include <error.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #ifdef HAVE_LIMITS_H
  33. #include <limits.h>
  34. #endif
  35. #include <grub/types.h>
  36. #include <grub/util/misc.h>
  37. #include <grub/mm.h>
  38. #include <grub/misc.h>
  39. #include <grub/emu/misc.h>
  40. #include <grub/emu/hostdisk.h>
  41. #include <grub/emu/getroot.h>
  42. #include <sys/wait.h>
  43. # include <sys/types.h>
  44. # include <sys/mkdev.h>
  45. # include <sys/dkio.h>
  46. char *
  47. grub_util_part_to_disk (const char *os_dev,
  48. struct stat *st,
  49. int *is_part)
  50. {
  51. char *colon = grub_strrchr (os_dev, ':');
  52. if (! S_ISCHR (st->st_mode))
  53. {
  54. *is_part = 0;
  55. return xstrdup (os_dev);
  56. }
  57. if (grub_memcmp (os_dev, "/devices", sizeof ("/devices") - 1) == 0
  58. && colon)
  59. {
  60. char *ret = xmalloc (colon - os_dev + sizeof (":q,raw"));
  61. if (grub_strcmp (colon, ":q,raw") != 0)
  62. *is_part = 1;
  63. grub_memcpy (ret, os_dev, colon - os_dev);
  64. grub_memcpy (ret + (colon - os_dev), ":q,raw", sizeof (":q,raw"));
  65. return ret;
  66. }
  67. else
  68. return xstrdup (os_dev);
  69. }
  70. enum grub_dev_abstraction_types
  71. grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
  72. {
  73. return GRUB_DEV_ABSTRACTION_NONE;
  74. }
  75. int
  76. grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
  77. enum grub_dev_abstraction_types ab __attribute__ ((unused)))
  78. {
  79. return 0;
  80. }
  81. char *
  82. grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
  83. {
  84. return NULL;
  85. }
  86. grub_disk_addr_t
  87. grub_util_find_partition_start_os (const char *dev)
  88. {
  89. int fd;
  90. struct extpart_info pinfo;
  91. fd = open (dev, O_RDONLY);
  92. if (fd == -1)
  93. {
  94. grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
  95. dev, strerror (errno));
  96. return 0;
  97. }
  98. if (ioctl (fd, DKIOCEXTPARTINFO, &pinfo))
  99. {
  100. grub_error (GRUB_ERR_BAD_DEVICE,
  101. "cannot get disk geometry of `%s'", dev);
  102. close (fd);
  103. return 0;
  104. }
  105. close (fd);
  106. return pinfo.p_start;
  107. }