probe.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* grub-probe.c - probe device information for a given path */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,2008,2009,2010,2013 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/device.h>
  24. #include <grub/disk.h>
  25. #include <grub/file.h>
  26. #include <grub/fs.h>
  27. #include <grub/partition.h>
  28. #include <grub/msdos_partition.h>
  29. #include <grub/gpt_partition.h>
  30. #include <grub/emu/hostdisk.h>
  31. #include <grub/emu/getroot.h>
  32. #include <grub/term.h>
  33. #include <grub/env.h>
  34. #include <grub/diskfilter.h>
  35. #include <grub/i18n.h>
  36. #include <grub/emu/misc.h>
  37. #include <grub/util/ofpath.h>
  38. #include <grub/crypto.h>
  39. #include <grub/cryptodisk.h>
  40. #include <string.h>
  41. /* Since OF path names can have "," characters in them, and GRUB
  42. internally uses "," to indicate partitions (unlike OF which uses
  43. ":" for this purpose) we escape such commas. */
  44. static char *
  45. escape_of_path (const char *orig_path)
  46. {
  47. char *new_path, *d, c;
  48. const char *p;
  49. if (!strchr (orig_path, ','))
  50. return (char *) xstrdup (orig_path);
  51. new_path = xmalloc (strlen (orig_path) * 2 + 1);
  52. p = orig_path;
  53. d = new_path;
  54. while ((c = *p++) != '\0')
  55. {
  56. if (c == ',')
  57. *d++ = '\\';
  58. *d++ = c;
  59. }
  60. *d = 0;
  61. return new_path;
  62. }
  63. char *
  64. grub_util_guess_bios_drive (const char *orig_path)
  65. {
  66. char *canon;
  67. char *ptr;
  68. canon = grub_canonicalize_file_name (orig_path);
  69. if (!canon)
  70. return NULL;
  71. ptr = strrchr (orig_path, '/');
  72. if (ptr)
  73. ptr++;
  74. else
  75. ptr = canon;
  76. if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
  77. {
  78. int num = ptr[2] - 'a';
  79. free (canon);
  80. return xasprintf ("hd%d", num);
  81. }
  82. if (ptr[0] == 'f' && ptr[1] == 'd')
  83. {
  84. int num = atoi (ptr + 2);
  85. free (canon);
  86. return xasprintf ("fd%d", num);
  87. }
  88. free (canon);
  89. return NULL;
  90. }
  91. char *
  92. grub_util_guess_efi_drive (const char *orig_path)
  93. {
  94. char *canon;
  95. char *ptr;
  96. canon = grub_canonicalize_file_name (orig_path);
  97. if (!canon)
  98. return NULL;
  99. ptr = strrchr (orig_path, '/');
  100. if (ptr)
  101. ptr++;
  102. else
  103. ptr = canon;
  104. if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
  105. {
  106. int num = ptr[2] - 'a';
  107. free (canon);
  108. return xasprintf ("hd%d", num);
  109. }
  110. if (ptr[0] == 'f' && ptr[1] == 'd')
  111. {
  112. int num = atoi (ptr + 2);
  113. free (canon);
  114. return xasprintf ("fd%d", num);
  115. }
  116. free (canon);
  117. return NULL;
  118. }
  119. char *
  120. grub_util_guess_baremetal_drive (const char *orig_path)
  121. {
  122. char *canon;
  123. char *ptr;
  124. canon = grub_canonicalize_file_name (orig_path);
  125. if (!canon)
  126. return NULL;
  127. ptr = strrchr (orig_path, '/');
  128. if (ptr)
  129. ptr++;
  130. else
  131. ptr = canon;
  132. if (ptr[0] == 'h' && ptr[1] == 'd')
  133. {
  134. int num = ptr[2] - 'a';
  135. free (canon);
  136. return xasprintf ("ata%d", num);
  137. }
  138. if (ptr[0] == 's' && ptr[1] == 'd')
  139. {
  140. int num = ptr[2] - 'a';
  141. free (canon);
  142. return xasprintf ("ahci%d", num);
  143. }
  144. free (canon);
  145. return NULL;
  146. }
  147. void
  148. grub_util_fprint_full_disk_name (FILE *f,
  149. const char *drive, grub_device_t dev)
  150. {
  151. char *dname = escape_of_path (drive);
  152. if (dev->disk->partition)
  153. {
  154. char *pname = grub_partition_get_name (dev->disk->partition);
  155. fprintf (f, "%s,%s", dname, pname);
  156. free (pname);
  157. }
  158. else
  159. fprintf (f, "%s", dname);
  160. free (dname);
  161. }