getroot.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <grub/charset.h>
  43. #include <grub/util/windows.h>
  44. #include <windows.h>
  45. #include <winioctl.h>
  46. TCHAR *
  47. grub_get_mount_point (const TCHAR *path)
  48. {
  49. const TCHAR *ptr;
  50. TCHAR *out;
  51. TCHAR letter = 0;
  52. size_t allocsize;
  53. for (ptr = path; *ptr; ptr++);
  54. allocsize = (ptr - path + 10) * 2;
  55. out = xmalloc (allocsize * sizeof (out[0]));
  56. /* When pointing to EFI system partition GetVolumePathName fails
  57. for ESP root and returns abberant information for everything
  58. else. Since GetVolumePathName shouldn't fail for any valid
  59. //?/X: we use it as indicator. */
  60. if ((path[0] == '/' || path[0] == '\\')
  61. && (path[1] == '/' || path[1] == '\\')
  62. && (path[2] == '?' || path[2] == '.')
  63. && (path[3] == '/' || path[3] == '\\')
  64. && path[4]
  65. && (path[5] == ':'))
  66. letter = path[4];
  67. if (path[0] && path[1] == ':')
  68. letter = path[0];
  69. if (letter)
  70. {
  71. TCHAR letterpath[10] = TEXT("\\\\?\\#:");
  72. letterpath[4] = letter;
  73. if (!GetVolumePathName (letterpath, out, allocsize))
  74. {
  75. if (path[1] == ':')
  76. {
  77. out[0] = path[0];
  78. out[1] = ':';
  79. out[2] = '\0';
  80. return out;
  81. }
  82. memcpy (out, path, sizeof (out[0]) * 6);
  83. out[6] = '\0';
  84. return out;
  85. }
  86. }
  87. if (!GetVolumePathName (path, out, allocsize))
  88. {
  89. free (out);
  90. return NULL;
  91. }
  92. return out;
  93. }
  94. char **
  95. grub_guess_root_devices (const char *dir)
  96. {
  97. char **os_dev = NULL;
  98. TCHAR *dirwindows, *mntpointwindows;
  99. TCHAR *ptr;
  100. TCHAR volumename[100];
  101. dirwindows = grub_util_get_windows_path (dir);
  102. if (!dirwindows)
  103. return 0;
  104. mntpointwindows = grub_get_mount_point (dirwindows);
  105. if (!mntpointwindows)
  106. {
  107. free (dirwindows);
  108. grub_util_info ("can't get volume path name: %d", (int) GetLastError ());
  109. return 0;
  110. }
  111. if (!mntpointwindows[0])
  112. {
  113. free (dirwindows);
  114. free (mntpointwindows);
  115. return 0;
  116. }
  117. for (ptr = mntpointwindows; *ptr; ptr++);
  118. if (*(ptr - 1) != '\\')
  119. {
  120. *ptr = '\\';
  121. *(ptr + 1) = '\0';
  122. }
  123. if (!GetVolumeNameForVolumeMountPoint (mntpointwindows,
  124. volumename,
  125. ARRAY_SIZE (volumename)))
  126. {
  127. TCHAR letter = 0;
  128. if ((mntpointwindows[0] == '/' || mntpointwindows[0] == '\\')
  129. && (mntpointwindows[1] == '/' || mntpointwindows[1] == '\\')
  130. && (mntpointwindows[2] == '?' || mntpointwindows[2] == '.')
  131. && (mntpointwindows[3] == '/' || mntpointwindows[3] == '\\')
  132. && mntpointwindows[4]
  133. && (mntpointwindows[5] == ':'))
  134. letter = mntpointwindows[4];
  135. if (mntpointwindows[0] && mntpointwindows[1] == ':')
  136. letter = mntpointwindows[0];
  137. if (!letter)
  138. {
  139. free (dirwindows);
  140. free (mntpointwindows);
  141. return 0;
  142. }
  143. volumename[0] = '\\';
  144. volumename[1] = '\\';
  145. volumename[2] = '?';
  146. volumename[3] = '\\';
  147. volumename[4] = letter;
  148. volumename[5] = ':';
  149. volumename[6] = '\0';
  150. }
  151. os_dev = xmalloc (2 * sizeof (os_dev[0]));
  152. for (ptr = volumename; *ptr; ptr++);
  153. while (ptr > volumename && *(ptr - 1) == '\\')
  154. *--ptr = '\0';
  155. os_dev[0] = grub_util_tchar_to_utf8 (volumename);
  156. free (dirwindows);
  157. free (mntpointwindows);
  158. if (!os_dev[0])
  159. {
  160. free (os_dev);
  161. return 0;
  162. }
  163. os_dev[1] = 0;
  164. return os_dev;
  165. }
  166. static int tcharncasecmp (LPCTSTR a, const char *b, size_t sz)
  167. {
  168. for (; sz; sz--, a++, b++)
  169. {
  170. char ac, bc;
  171. if(*a >= 0x80)
  172. return +1;
  173. if (*b & 0x80)
  174. return -1;
  175. if (*a == '\0' && *b == '\0')
  176. return 0;
  177. ac = *a;
  178. bc = *b;
  179. if (ac >= 'A' && ac <= 'Z')
  180. ac -= 'A' - 'a';
  181. if (bc >= 'A' && bc <= 'Z')
  182. bc -= 'A' - 'a';
  183. if (ac > bc)
  184. return +1;
  185. if (ac < bc)
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. char *
  191. grub_util_part_to_disk (const char *os_dev,
  192. struct stat *st __attribute__ ((unused)),
  193. int *is_part)
  194. {
  195. HANDLE hd;
  196. LPTSTR name = grub_util_get_windows_path (os_dev);
  197. VOLUME_DISK_EXTENTS exts;
  198. DWORD extsbytes;
  199. char *ret;
  200. if (((name[0] == '/') || (name[0] == '\\')) &&
  201. ((name[1] == '/') || (name[1] == '\\')) &&
  202. ((name[2] == '.') || (name[2] == '?')) &&
  203. ((name[3] == '/') || (name[3] == '\\'))
  204. && (tcharncasecmp (name + 4, "PhysicalDrive", sizeof ("PhysicalDrive") - 1) == 0
  205. || tcharncasecmp (name + 4, "Harddisk", sizeof ("Harddisk") - 1) == 0
  206. || ((name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
  207. && name[5] == ':' && name[6] == '\0')))
  208. {
  209. grub_util_info ("Matches full disk pattern");
  210. ret = grub_util_tchar_to_utf8 (name);
  211. free (name);
  212. return ret;
  213. }
  214. hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  215. 0, OPEN_EXISTING, 0, 0);
  216. if (hd == INVALID_HANDLE_VALUE)
  217. {
  218. grub_util_info ("CreateFile failed");
  219. ret = grub_util_tchar_to_utf8 (name);
  220. free (name);
  221. return ret;
  222. }
  223. if (!DeviceIoControl(hd, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
  224. NULL, 0, &exts, sizeof (exts), &extsbytes, NULL))
  225. {
  226. grub_util_info ("IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed");
  227. ret = grub_util_tchar_to_utf8 (name);
  228. CloseHandle (hd);
  229. free (name);
  230. return ret;
  231. }
  232. CloseHandle (hd);
  233. *is_part = 1;
  234. free (name);
  235. return xasprintf ("\\\\?\\PhysicalDrive%lu", (unsigned long) exts.Extents[0].DiskNumber);
  236. }
  237. enum grub_dev_abstraction_types
  238. grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
  239. {
  240. return GRUB_DEV_ABSTRACTION_NONE;
  241. }
  242. int
  243. grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
  244. enum grub_dev_abstraction_types ab __attribute__ ((unused)))
  245. {
  246. return 0;
  247. }
  248. char *
  249. grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
  250. {
  251. return NULL;
  252. }
  253. grub_disk_addr_t
  254. grub_util_find_partition_start_os (const char *os_dev)
  255. {
  256. HANDLE hd;
  257. LPTSTR name = grub_util_get_windows_path (os_dev);
  258. VOLUME_DISK_EXTENTS exts;
  259. DWORD extsbytes;
  260. char *ret;
  261. if (((name[0] == '/') || (name[0] == '\\')) &&
  262. ((name[1] == '/') || (name[1] == '\\')) &&
  263. ((name[2] == '.') || (name[2] == '?')) &&
  264. ((name[3] == '/') || (name[3] == '\\'))
  265. && (tcharncasecmp (name + 4, "PhysicalDrive", sizeof ("PhysicalDrive") - 1) == 0
  266. || tcharncasecmp (name + 4, "Harddisk", sizeof ("Harddisk") - 1) == 0
  267. || ((name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
  268. && name[5] == ':' && name[6] == '\0')))
  269. {
  270. ret = grub_util_tchar_to_utf8 (name);
  271. free (name);
  272. return 0;
  273. }
  274. hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  275. 0, OPEN_EXISTING, 0, 0);
  276. if (hd == INVALID_HANDLE_VALUE)
  277. {
  278. ret = grub_util_tchar_to_utf8 (name);
  279. free (name);
  280. return 0;
  281. }
  282. if (!DeviceIoControl(hd, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
  283. NULL, 0, &exts, sizeof (exts), &extsbytes, NULL))
  284. {
  285. ret = grub_util_tchar_to_utf8 (name);
  286. CloseHandle (hd);
  287. free (name);
  288. return 0;
  289. }
  290. CloseHandle (hd);
  291. free (name);
  292. return exts.Extents[0].StartingOffset.QuadPart / 512;
  293. }
  294. int
  295. grub_util_biosdisk_is_floppy (grub_disk_t disk)
  296. {
  297. int ret;
  298. const char *dname;
  299. LPTSTR name;
  300. dname = grub_util_biosdisk_get_osdev (disk);
  301. if (!dname)
  302. return 0;
  303. name = grub_util_get_windows_path (dname);
  304. ret = (((name[0] == '/') || (name[0] == '\\')) &&
  305. ((name[1] == '/') || (name[1] == '\\')) &&
  306. ((name[2] == '.') || (name[2] == '?')) &&
  307. ((name[3] == '/') || (name[3] == '\\'))
  308. && (name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
  309. && name[5] == ':' && name[6] == '\0');
  310. free (name);
  311. return ret;
  312. }