getroot.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/ioctl.h>
  44. # include <sys/disklabel.h> /* struct disklabel */
  45. # include <sys/disk.h> /* struct dkwedge_info */
  46. # ifdef HAVE_GETRAWPARTITION
  47. # include <util.h> /* getrawpartition */
  48. # endif /* HAVE_GETRAWPARTITION */
  49. #if defined(__NetBSD__)
  50. # include <sys/fdio.h>
  51. #endif
  52. #if defined(__OpenBSD__)
  53. # include <sys/dkio.h>
  54. #endif
  55. char *
  56. grub_util_part_to_disk (const char *os_dev, struct stat *st,
  57. int *is_part)
  58. {
  59. int rawpart = -1;
  60. if (! S_ISCHR (st->st_mode))
  61. {
  62. *is_part = 0;
  63. return xstrdup (os_dev);
  64. }
  65. # ifdef HAVE_GETRAWPARTITION
  66. rawpart = getrawpartition();
  67. # endif /* HAVE_GETRAWPARTITION */
  68. if (rawpart < 0)
  69. return xstrdup (os_dev);
  70. #if defined(__NetBSD__)
  71. /* NetBSD disk wedges are of the form "/dev/rdk.*". */
  72. if (strncmp ("/dev/rdk", os_dev, sizeof("/dev/rdk") - 1) == 0)
  73. {
  74. struct dkwedge_info dkw;
  75. int fd;
  76. fd = open (os_dev, O_RDONLY);
  77. if (fd == -1)
  78. {
  79. grub_error (GRUB_ERR_BAD_DEVICE,
  80. N_("cannot open `%s': %s"), os_dev,
  81. strerror (errno));
  82. return xstrdup (os_dev);
  83. }
  84. if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == -1)
  85. {
  86. grub_error (GRUB_ERR_BAD_DEVICE,
  87. "cannot get disk wedge info of `%s'", os_dev);
  88. close (fd);
  89. return xstrdup (os_dev);
  90. }
  91. *is_part = (dkw.dkw_offset != 0);
  92. close (fd);
  93. return xasprintf ("/dev/r%s%c", dkw.dkw_parent, 'a' + rawpart);
  94. }
  95. #endif
  96. /* NetBSD (disk label) partitions are of the form "/dev/r[a-z]+[0-9][a-z]". */
  97. if (strncmp ("/dev/r", os_dev, sizeof("/dev/r") - 1) == 0 &&
  98. (os_dev[sizeof("/dev/r") - 1] >= 'a' && os_dev[sizeof("/dev/r") - 1] <= 'z') &&
  99. strncmp ("fd", os_dev + sizeof("/dev/r") - 1, sizeof("fd") - 1) != 0) /* not a floppy device name */
  100. {
  101. char *path = xstrdup (os_dev);
  102. char *p;
  103. for (p = path + sizeof("/dev/r"); *p >= 'a' && *p <= 'z'; p++);
  104. if (grub_isdigit(*p))
  105. {
  106. p++;
  107. if ((*p >= 'a' && *p <= 'z') && (*(p+1) == '\0'))
  108. {
  109. if (*p != 'a' + rawpart)
  110. *is_part = 1;
  111. /* path matches the required regular expression and
  112. p points to its last character. */
  113. *p = 'a' + rawpart;
  114. }
  115. }
  116. return path;
  117. }
  118. return xstrdup (os_dev);
  119. }
  120. enum grub_dev_abstraction_types
  121. grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
  122. {
  123. return GRUB_DEV_ABSTRACTION_NONE;
  124. }
  125. int
  126. grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
  127. enum grub_dev_abstraction_types ab __attribute__ ((unused)))
  128. {
  129. return 0;
  130. }
  131. char *
  132. grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
  133. {
  134. return NULL;
  135. }
  136. grub_disk_addr_t
  137. grub_util_find_partition_start_os (const char *dev)
  138. {
  139. int fd;
  140. # if defined(__NetBSD__)
  141. struct dkwedge_info dkw;
  142. # endif /* defined(__NetBSD__) */
  143. struct disklabel label;
  144. int p_index;
  145. fd = grub_util_fd_open (dev, O_RDONLY);
  146. if (fd == -1)
  147. {
  148. grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
  149. dev, strerror (errno));
  150. return 0;
  151. }
  152. # if defined(__NetBSD__)
  153. /* First handle the case of disk wedges. */
  154. if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == 0)
  155. {
  156. close (fd);
  157. return (grub_disk_addr_t) dkw.dkw_offset;
  158. }
  159. # endif /* defined(__NetBSD__) */
  160. if (ioctl (fd, DIOCGDINFO, &label) == -1)
  161. {
  162. grub_error (GRUB_ERR_BAD_DEVICE,
  163. "cannot get disk label of `%s'", dev);
  164. close (fd);
  165. return 0;
  166. }
  167. close (fd);
  168. if (dev[0])
  169. p_index = dev[strlen(dev) - 1] - 'a';
  170. else
  171. p_index = -1;
  172. if (p_index >= label.d_npartitions || p_index < 0)
  173. {
  174. grub_error (GRUB_ERR_BAD_DEVICE,
  175. "no disk label entry for `%s'", dev);
  176. return 0;
  177. }
  178. return (grub_disk_addr_t) label.d_partitions[p_index].p_offset;
  179. }