probe.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <string.h>
  39. /* Since OF path names can have "," characters in them, and GRUB
  40. internally uses "," to indicate partitions (unlike OF which uses
  41. ":" for this purpose) we escape such commas. */
  42. static char *
  43. escape_of_path (const char *orig_path)
  44. {
  45. char *new_path, *d, c;
  46. const char *p;
  47. if (!strchr (orig_path, ','))
  48. return (char *) xstrdup (orig_path);
  49. new_path = xmalloc (strlen (orig_path) * 2 + 1);
  50. p = orig_path;
  51. d = new_path;
  52. while ((c = *p++) != '\0')
  53. {
  54. if (c == ',')
  55. *d++ = '\\';
  56. *d++ = c;
  57. }
  58. *d = 0;
  59. return new_path;
  60. }
  61. char *
  62. grub_util_guess_bios_drive (const char *orig_path)
  63. {
  64. char *canon;
  65. char *ptr;
  66. canon = grub_canonicalize_file_name (orig_path);
  67. if (!canon)
  68. return NULL;
  69. ptr = strrchr (orig_path, '/');
  70. if (ptr)
  71. ptr++;
  72. else
  73. ptr = canon;
  74. if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
  75. {
  76. int num = ptr[2] - 'a';
  77. free (canon);
  78. return xasprintf ("hd%d", num);
  79. }
  80. if (ptr[0] == 'f' && ptr[1] == 'd')
  81. {
  82. int num = atoi (ptr + 2);
  83. free (canon);
  84. return xasprintf ("fd%d", num);
  85. }
  86. free (canon);
  87. return NULL;
  88. }
  89. char *
  90. grub_util_guess_efi_drive (const char *orig_path)
  91. {
  92. char *canon;
  93. char *ptr;
  94. canon = grub_canonicalize_file_name (orig_path);
  95. if (!canon)
  96. return NULL;
  97. ptr = strrchr (orig_path, '/');
  98. if (ptr)
  99. ptr++;
  100. else
  101. ptr = canon;
  102. if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
  103. {
  104. int num = ptr[2] - 'a';
  105. free (canon);
  106. return xasprintf ("hd%d", num);
  107. }
  108. if (ptr[0] == 'f' && ptr[1] == 'd')
  109. {
  110. int num = atoi (ptr + 2);
  111. free (canon);
  112. return xasprintf ("fd%d", num);
  113. }
  114. free (canon);
  115. return NULL;
  116. }
  117. char *
  118. grub_util_guess_baremetal_drive (const char *orig_path)
  119. {
  120. char *canon;
  121. char *ptr;
  122. canon = grub_canonicalize_file_name (orig_path);
  123. if (!canon)
  124. return NULL;
  125. ptr = strrchr (orig_path, '/');
  126. if (ptr)
  127. ptr++;
  128. else
  129. ptr = canon;
  130. if (ptr[0] == 'h' && ptr[1] == 'd')
  131. {
  132. int num = ptr[2] - 'a';
  133. free (canon);
  134. return xasprintf ("ata%d", num);
  135. }
  136. if (ptr[0] == 's' && ptr[1] == 'd')
  137. {
  138. int num = ptr[2] - 'a';
  139. free (canon);
  140. return xasprintf ("ahci%d", num);
  141. }
  142. free (canon);
  143. return NULL;
  144. }
  145. void
  146. grub_util_fprint_full_disk_name (FILE *f,
  147. const char *drive, grub_device_t dev)
  148. {
  149. char *dname = escape_of_path (drive);
  150. if (dev->disk->partition)
  151. {
  152. char *pname = grub_partition_get_name (dev->disk->partition);
  153. fprintf (f, "%s,%s", dname, pname);
  154. free (pname);
  155. }
  156. else
  157. fprintf (f, "%s", dname);
  158. free (dname);
  159. }