hostdisk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,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 <grub/disk.h>
  20. #include <grub/partition.h>
  21. #include <grub/msdos_partition.h>
  22. #include <grub/types.h>
  23. #include <grub/err.h>
  24. #include <grub/emu/misc.h>
  25. #include <grub/emu/hostdisk.h>
  26. #include <grub/emu/getroot.h>
  27. #include <grub/misc.h>
  28. #include <grub/i18n.h>
  29. #include <grub/list.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include <limits.h>
  40. #include <grub/util/windows.h>
  41. #include <grub/charset.h>
  42. #include <windows.h>
  43. #include <winioctl.h>
  44. #include <wincrypt.h>
  45. #ifdef __CYGWIN__
  46. #include <sys/cygwin.h>
  47. #endif
  48. #if SIZEOF_TCHAR == 1
  49. LPTSTR
  50. grub_util_utf8_to_tchar (const char *in)
  51. {
  52. return xstrdup (in);
  53. }
  54. char *
  55. grub_util_tchar_to_utf8 (LPCTSTR in)
  56. {
  57. return xstrdup (in);
  58. }
  59. #elif SIZEOF_TCHAR == 2
  60. LPTSTR
  61. grub_util_utf8_to_tchar (const char *in)
  62. {
  63. LPTSTR ret;
  64. size_t ssz = strlen (in);
  65. size_t tsz = 2 * (GRUB_MAX_UTF16_PER_UTF8 * ssz + 1);
  66. ret = xmalloc (tsz);
  67. tsz = grub_utf8_to_utf16 (ret, tsz,
  68. (const grub_uint8_t *) in, ssz, NULL);
  69. ret[tsz] = 0;
  70. return ret;
  71. }
  72. char *
  73. grub_util_tchar_to_utf8 (LPCTSTR in)
  74. {
  75. size_t ssz;
  76. for (ssz = 0; in[ssz]; ssz++);
  77. size_t tsz = GRUB_MAX_UTF8_PER_UTF16 * ssz + 1;
  78. grub_uint8_t *ret = xmalloc (tsz);
  79. *grub_utf16_to_utf8 (ret, in, ssz) = '\0';
  80. return (char *) ret;
  81. }
  82. #else
  83. #error "Unsupported TCHAR size"
  84. #endif
  85. static LPTSTR
  86. grub_util_get_windows_path_real (const char *path)
  87. {
  88. LPTSTR fpa;
  89. LPTSTR tpath;
  90. size_t alloc, len;
  91. tpath = grub_util_utf8_to_tchar (path);
  92. alloc = PATH_MAX;
  93. while (1)
  94. {
  95. fpa = xmalloc (alloc * sizeof (fpa[0]));
  96. len = GetFullPathName (tpath, alloc, fpa, NULL);
  97. if (len >= alloc)
  98. {
  99. free (fpa);
  100. alloc = 2 * (len + 2);
  101. continue;
  102. }
  103. if (len == 0)
  104. {
  105. free (fpa);
  106. return tpath;
  107. }
  108. free (tpath);
  109. return fpa;
  110. }
  111. }
  112. #ifdef __CYGWIN__
  113. LPTSTR
  114. grub_util_get_windows_path (const char *path)
  115. {
  116. LPTSTR winpath;
  117. /* Workaround cygwin bugs with //?/. */
  118. if ((path[0] == '\\' || path[0] == '/')
  119. && (path[1] == '\\' || path[1] == '/')
  120. && (path[2] == '?' || path[2] == '.')
  121. && (path[3] == '\\' || path[3] == '/'))
  122. return grub_util_get_windows_path_real (path);
  123. winpath = xmalloc (sizeof (winpath[0]) * PATH_MAX);
  124. memset (winpath, 0, sizeof (winpath[0]) * PATH_MAX);
  125. if (cygwin_conv_path ((sizeof (winpath[0]) == 1 ? CCP_POSIX_TO_WIN_A
  126. : CCP_POSIX_TO_WIN_W) | CCP_ABSOLUTE, path, winpath,
  127. sizeof (winpath[0]) * PATH_MAX))
  128. grub_util_error ("%s", _("cygwin_conv_path() failed"));
  129. return winpath;
  130. }
  131. #else
  132. LPTSTR
  133. grub_util_get_windows_path (const char *path)
  134. {
  135. return grub_util_get_windows_path_real (path);
  136. }
  137. #endif
  138. grub_uint64_t
  139. grub_util_get_fd_size (grub_util_fd_t hd, const char *name_in,
  140. unsigned *log_secsize)
  141. {
  142. grub_int64_t size = -1LL;
  143. int log_sector_size = 9;
  144. LPTSTR name = grub_util_get_windows_path (name_in);
  145. if (log_secsize)
  146. *log_secsize = log_sector_size;
  147. if (((name[0] == '/') || (name[0] == '\\')) &&
  148. ((name[1] == '/') || (name[1] == '\\')) &&
  149. ((name[2] == '.') || (name[2] == '?')) &&
  150. ((name[3] == '/') || (name[3] == '\\')))
  151. {
  152. DWORD nr;
  153. DISK_GEOMETRY g;
  154. if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
  155. 0, 0, &g, sizeof (g), &nr, 0))
  156. goto fail;
  157. size = g.Cylinders.QuadPart;
  158. size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
  159. for (log_sector_size = 0;
  160. (1 << log_sector_size) < g.BytesPerSector;
  161. log_sector_size++);
  162. }
  163. else
  164. {
  165. ULARGE_INTEGER s;
  166. s.LowPart = GetFileSize (hd, &s.HighPart);
  167. size = s.QuadPart;
  168. }
  169. fail:
  170. if (log_secsize)
  171. *log_secsize = log_sector_size;
  172. free (name);
  173. return size;
  174. }
  175. void
  176. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  177. {
  178. }
  179. int
  180. grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off)
  181. {
  182. LARGE_INTEGER offset;
  183. offset.QuadPart = off;
  184. if (!SetFilePointerEx (fd, offset, NULL, FILE_BEGIN))
  185. return -1;
  186. return 0;
  187. }
  188. grub_util_fd_t
  189. grub_util_fd_open (const char *os_dev, int flags)
  190. {
  191. DWORD flg = 0, crt;
  192. LPTSTR dev = grub_util_get_windows_path (os_dev);
  193. grub_util_fd_t ret;
  194. if (flags & GRUB_UTIL_FD_O_WRONLY)
  195. flg |= GENERIC_WRITE;
  196. if (flags & GRUB_UTIL_FD_O_RDONLY)
  197. flg |= GENERIC_READ;
  198. if (flags & GRUB_UTIL_FD_O_CREATTRUNC)
  199. crt = CREATE_ALWAYS;
  200. else
  201. crt = OPEN_EXISTING;
  202. ret = CreateFile (dev, flg, FILE_SHARE_READ | FILE_SHARE_WRITE,
  203. 0, crt, 0, 0);
  204. free (dev);
  205. return ret;
  206. }
  207. ssize_t
  208. grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
  209. {
  210. DWORD real_read;
  211. if (!ReadFile(fd, buf, len, &real_read, NULL))
  212. {
  213. grub_util_info ("read err %x", (int) GetLastError ());
  214. return -1;
  215. }
  216. grub_util_info ("successful read");
  217. return real_read;
  218. }
  219. ssize_t
  220. grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
  221. {
  222. DWORD real_read;
  223. if (!WriteFile(fd, buf, len, &real_read, NULL))
  224. {
  225. grub_util_info ("write err %x", (int) GetLastError ());
  226. return -1;
  227. }
  228. grub_util_info ("successful write");
  229. return real_read;
  230. }
  231. static int allow_fd_syncs = 1;
  232. void
  233. grub_util_fd_sync (grub_util_fd_t fd)
  234. {
  235. if (allow_fd_syncs)
  236. FlushFileBuffers (fd);
  237. }
  238. void
  239. grub_util_disable_fd_syncs (void)
  240. {
  241. allow_fd_syncs = 0;
  242. }
  243. void
  244. grub_util_fd_close (grub_util_fd_t fd)
  245. {
  246. CloseHandle (fd);
  247. }
  248. const char *
  249. grub_util_fd_strerror (void)
  250. {
  251. DWORD err = GetLastError ();
  252. LPTSTR tstr = NULL;
  253. static char *last;
  254. char *ret, *ptr;
  255. free (last);
  256. last = 0;
  257. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
  258. | FORMAT_MESSAGE_IGNORE_INSERTS,
  259. NULL, err, 0, (void *) &tstr,
  260. 0, NULL);
  261. if (!tstr)
  262. return "unknown error";
  263. ret = grub_util_tchar_to_utf8 (tstr);
  264. LocalFree (tstr);
  265. last = ret;
  266. for (ptr = ret + strlen (ret) - 1;
  267. ptr >= ret && (*ptr == '\n' || *ptr == '\r');
  268. ptr--);
  269. ptr[1] = '\0';
  270. return ret;
  271. }
  272. char *
  273. grub_canonicalize_file_name (const char *path)
  274. {
  275. char *ret;
  276. LPTSTR windows_path;
  277. ret = xmalloc (PATH_MAX);
  278. windows_path = grub_util_get_windows_path (path);
  279. if (!windows_path)
  280. return NULL;
  281. ret = grub_util_tchar_to_utf8 (windows_path);
  282. free (windows_path);
  283. return ret;
  284. }
  285. void
  286. grub_util_mkdir (const char *dir)
  287. {
  288. LPTSTR windows_name;
  289. windows_name = grub_util_get_windows_path (dir);
  290. CreateDirectory (windows_name, NULL);
  291. free (windows_name);
  292. }
  293. int
  294. grub_util_rename (const char *from, const char *to)
  295. {
  296. LPTSTR windows_from, windows_to;
  297. int ret;
  298. windows_from = grub_util_get_windows_path (from);
  299. windows_to = grub_util_get_windows_path (to);
  300. ret = !MoveFile (windows_from, windows_to);
  301. free (windows_from);
  302. free (windows_to);
  303. return ret;
  304. }
  305. struct grub_util_fd_dir
  306. {
  307. WIN32_FIND_DATA fd;
  308. HANDLE hnd;
  309. int is_end;
  310. char *last;
  311. };
  312. grub_util_fd_dir_t
  313. grub_util_fd_opendir (const char *name)
  314. {
  315. struct grub_util_fd_dir *ret;
  316. LPTSTR name_windows;
  317. LPTSTR pattern;
  318. ssize_t l;
  319. name_windows = grub_util_get_windows_path (name);
  320. for (l = 0; name_windows[l]; l++);
  321. for (l--; l >= 0 && (name_windows[l] == '\\' || name_windows[l] == '/'); l--);
  322. l++;
  323. pattern = xmalloc ((l + 3) * sizeof (pattern[0]));
  324. memcpy (pattern, name_windows, l * sizeof (pattern[0]));
  325. pattern[l] = '\\';
  326. pattern[l + 1] = '*';
  327. pattern[l + 2] = '\0';
  328. ret = xmalloc (sizeof (*ret));
  329. memset (ret, 0, sizeof (*ret));
  330. ret->hnd = FindFirstFile (pattern, &ret->fd);
  331. free (name_windows);
  332. free (pattern);
  333. if (ret->hnd == INVALID_HANDLE_VALUE)
  334. {
  335. DWORD err = GetLastError ();
  336. if (err == ERROR_FILE_NOT_FOUND)
  337. {
  338. ret->is_end = 1;
  339. return ret;
  340. }
  341. return NULL;
  342. }
  343. return ret;
  344. }
  345. void
  346. grub_util_fd_closedir (grub_util_fd_dir_t dirp)
  347. {
  348. if (dirp->hnd != INVALID_HANDLE_VALUE)
  349. CloseHandle (dirp->hnd);
  350. free (dirp->last);
  351. free (dirp);
  352. }
  353. grub_util_fd_dirent_t
  354. grub_util_fd_readdir (grub_util_fd_dir_t dirp)
  355. {
  356. char *ret;
  357. free (dirp->last);
  358. dirp->last = NULL;
  359. if (dirp->is_end)
  360. return NULL;
  361. ret = grub_util_tchar_to_utf8 (dirp->fd.cFileName);
  362. dirp->last = ret;
  363. if (!FindNextFile (dirp->hnd, &dirp->fd))
  364. dirp->is_end = 1;
  365. return (grub_util_fd_dirent_t) ret;
  366. }
  367. int
  368. grub_util_unlink (const char *name)
  369. {
  370. LPTSTR name_windows;
  371. int ret;
  372. name_windows = grub_util_get_windows_path (name);
  373. ret = !DeleteFile (name_windows);
  374. free (name_windows);
  375. return ret;
  376. }
  377. int
  378. grub_util_rmdir (const char *name)
  379. {
  380. LPTSTR name_windows;
  381. int ret;
  382. name_windows = grub_util_get_windows_path (name);
  383. ret = !RemoveDirectory (name_windows);
  384. free (name_windows);
  385. return ret;
  386. }
  387. #ifndef __CYGWIN__
  388. static char *
  389. get_temp_name (void)
  390. {
  391. TCHAR rt[1024];
  392. TCHAR *ptr;
  393. HCRYPTPROV hCryptProv;
  394. grub_uint8_t rnd[5];
  395. int i;
  396. GetTempPath (ARRAY_SIZE (rt) - 100, rt);
  397. if (!CryptAcquireContext (&hCryptProv,
  398. NULL,
  399. MS_DEF_PROV,
  400. PROV_RSA_FULL,
  401. CRYPT_VERIFYCONTEXT)
  402. || !CryptGenRandom (hCryptProv, 5, rnd))
  403. grub_util_error ("%s", _("couldn't retrieve random data"));
  404. CryptReleaseContext (hCryptProv, 0);
  405. for (ptr = rt; *ptr; ptr++);
  406. memcpy (ptr, TEXT("\\GRUB."), sizeof (TEXT("\\GRUB.")));
  407. ptr += sizeof ("\\GRUB.") - 1;
  408. for (i = 0; i < 8; i++)
  409. {
  410. grub_size_t b = i * 5;
  411. grub_uint8_t r;
  412. grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
  413. grub_size_t f2;
  414. if (f1 > 5)
  415. f1 = 5;
  416. f2 = 5 - f1;
  417. r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
  418. if (f2)
  419. r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
  420. if (r < 10)
  421. *ptr++ = '0' + r;
  422. else
  423. *ptr++ = 'a' + (r - 10);
  424. }
  425. *ptr = '\0';
  426. return grub_util_tchar_to_utf8 (rt);
  427. }
  428. char *
  429. grub_util_make_temporary_file (void)
  430. {
  431. char *ret = get_temp_name ();
  432. FILE *f;
  433. f = grub_util_fopen (ret, "wb");
  434. if (f)
  435. fclose (f);
  436. return ret;
  437. }
  438. char *
  439. grub_util_make_temporary_dir (void)
  440. {
  441. char *ret = get_temp_name ();
  442. grub_util_mkdir (ret);
  443. return ret;
  444. }
  445. #endif
  446. int
  447. grub_util_is_directory (const char *name)
  448. {
  449. LPTSTR name_windows;
  450. DWORD attr;
  451. name_windows = grub_util_get_windows_path (name);
  452. if (!name_windows)
  453. return 0;
  454. attr = GetFileAttributes (name_windows);
  455. grub_free (name_windows);
  456. return !!(attr & FILE_ATTRIBUTE_DIRECTORY);
  457. }
  458. int
  459. grub_util_is_regular (const char *name)
  460. {
  461. LPTSTR name_windows;
  462. DWORD attr;
  463. name_windows = grub_util_get_windows_path (name);
  464. if (!name_windows)
  465. return 0;
  466. attr = GetFileAttributes (name_windows);
  467. grub_free (name_windows);
  468. return !(attr & FILE_ATTRIBUTE_DIRECTORY)
  469. && !(attr & FILE_ATTRIBUTE_REPARSE_POINT) && attr;
  470. }
  471. grub_uint32_t
  472. grub_util_get_mtime (const char *path)
  473. {
  474. LPTSTR name_windows;
  475. BOOL b;
  476. WIN32_FILE_ATTRIBUTE_DATA attr;
  477. ULARGE_INTEGER us_ul;
  478. name_windows = grub_util_get_windows_path (path);
  479. if (!name_windows)
  480. return 0;
  481. b = GetFileAttributesEx (name_windows, GetFileExInfoStandard, &attr);
  482. grub_free (name_windows);
  483. if (!b)
  484. return 0;
  485. us_ul.LowPart = attr.ftLastWriteTime.dwLowDateTime;
  486. us_ul.HighPart = attr.ftLastWriteTime.dwHighDateTime;
  487. return (us_ul.QuadPart / 10000000)
  488. - 86400ULL * 365 * (1970 - 1601)
  489. - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
  490. }
  491. #ifdef __MINGW32__
  492. FILE *
  493. grub_util_fopen (const char *path, const char *mode)
  494. {
  495. LPTSTR tpath;
  496. FILE *ret;
  497. tpath = grub_util_get_windows_path (path);
  498. #if SIZEOF_TCHAR == 1
  499. ret = fopen (tpath, tmode);
  500. #else
  501. LPTSTR tmode;
  502. tmode = grub_util_utf8_to_tchar (mode);
  503. ret = _wfopen (tpath, tmode);
  504. free (tmode);
  505. #endif
  506. free (tpath);
  507. return ret;
  508. }
  509. void
  510. grub_util_file_sync (FILE *f)
  511. {
  512. HANDLE hnd;
  513. fflush (f);
  514. if (!allow_fd_syncs)
  515. return;
  516. hnd = (HANDLE) _get_osfhandle (fileno (f));
  517. FlushFileBuffers (hnd);
  518. }
  519. int
  520. grub_util_is_special_file (const char *name)
  521. {
  522. LPTSTR name_windows;
  523. DWORD attr;
  524. name_windows = grub_util_get_windows_path (name);
  525. if (!name_windows)
  526. return 1;
  527. attr = GetFileAttributes (name_windows);
  528. grub_free (name_windows);
  529. return !!(attr & FILE_ATTRIBUTE_REPARSE_POINT) || !attr;
  530. }
  531. #else
  532. void
  533. grub_util_file_sync (FILE *f)
  534. {
  535. fflush (f);
  536. if (!allow_fd_syncs)
  537. return;
  538. fsync (fileno (f));
  539. }
  540. FILE *
  541. grub_util_fopen (const char *path, const char *mode)
  542. {
  543. return fopen (path, mode);
  544. }
  545. #endif