hostdisk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 = xcalloc (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. log_sector_size = grub_log2ull (g.BytesPerSector);
  160. }
  161. else
  162. {
  163. ULARGE_INTEGER s;
  164. s.LowPart = GetFileSize (hd, &s.HighPart);
  165. size = s.QuadPart;
  166. }
  167. fail:
  168. if (log_secsize)
  169. *log_secsize = log_sector_size;
  170. free (name);
  171. return size;
  172. }
  173. void
  174. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  175. {
  176. }
  177. int
  178. grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off)
  179. {
  180. LARGE_INTEGER offset;
  181. offset.QuadPart = off;
  182. if (!SetFilePointerEx (fd, offset, NULL, FILE_BEGIN))
  183. return -1;
  184. return 0;
  185. }
  186. grub_util_fd_t
  187. grub_util_fd_open (const char *os_dev, int flags)
  188. {
  189. DWORD flg = 0, crt;
  190. LPTSTR dev = grub_util_get_windows_path (os_dev);
  191. grub_util_fd_t ret;
  192. if (flags & GRUB_UTIL_FD_O_WRONLY)
  193. flg |= GENERIC_WRITE;
  194. if (flags & GRUB_UTIL_FD_O_RDONLY)
  195. flg |= GENERIC_READ;
  196. if (flags & GRUB_UTIL_FD_O_CREATTRUNC)
  197. crt = CREATE_ALWAYS;
  198. else
  199. crt = OPEN_EXISTING;
  200. ret = CreateFile (dev, flg, FILE_SHARE_READ | FILE_SHARE_WRITE,
  201. 0, crt, 0, 0);
  202. free (dev);
  203. return ret;
  204. }
  205. ssize_t
  206. grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
  207. {
  208. DWORD real_read;
  209. if (!ReadFile(fd, buf, len, &real_read, NULL))
  210. {
  211. grub_util_info ("read err %x", (int) GetLastError ());
  212. return -1;
  213. }
  214. grub_util_info ("successful read");
  215. return real_read;
  216. }
  217. ssize_t
  218. grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
  219. {
  220. DWORD real_read;
  221. if (!WriteFile(fd, buf, len, &real_read, NULL))
  222. {
  223. grub_util_info ("write err %x", (int) GetLastError ());
  224. return -1;
  225. }
  226. grub_util_info ("successful write");
  227. return real_read;
  228. }
  229. static int allow_fd_syncs = 1;
  230. int
  231. grub_util_fd_sync (grub_util_fd_t fd)
  232. {
  233. if (allow_fd_syncs)
  234. {
  235. if (!FlushFileBuffers (fd))
  236. {
  237. grub_util_info ("flush err %x", (int) GetLastError ());
  238. return -1;
  239. }
  240. }
  241. return 0;
  242. }
  243. void
  244. grub_util_disable_fd_syncs (void)
  245. {
  246. allow_fd_syncs = 0;
  247. }
  248. int
  249. grub_util_fd_close (grub_util_fd_t fd)
  250. {
  251. if (!CloseHandle (fd))
  252. {
  253. grub_util_info ("close err %x", (int) GetLastError ());
  254. return -1;
  255. }
  256. return 0;
  257. }
  258. const char *
  259. grub_util_fd_strerror (void)
  260. {
  261. DWORD err = GetLastError ();
  262. LPTSTR tstr = NULL;
  263. static char *last;
  264. char *ret, *ptr;
  265. free (last);
  266. last = 0;
  267. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
  268. | FORMAT_MESSAGE_IGNORE_INSERTS,
  269. NULL, err, 0, (void *) &tstr,
  270. 0, NULL);
  271. if (!tstr)
  272. return "unknown error";
  273. ret = grub_util_tchar_to_utf8 (tstr);
  274. LocalFree (tstr);
  275. last = ret;
  276. for (ptr = ret + strlen (ret) - 1;
  277. ptr >= ret && (*ptr == '\n' || *ptr == '\r');
  278. ptr--);
  279. ptr[1] = '\0';
  280. return ret;
  281. }
  282. char *
  283. grub_canonicalize_file_name (const char *path)
  284. {
  285. char *ret;
  286. LPTSTR windows_path;
  287. ret = xmalloc (PATH_MAX);
  288. windows_path = grub_util_get_windows_path (path);
  289. if (!windows_path)
  290. return NULL;
  291. ret = grub_util_tchar_to_utf8 (windows_path);
  292. free (windows_path);
  293. return ret;
  294. }
  295. void
  296. grub_util_mkdir (const char *dir)
  297. {
  298. LPTSTR windows_name;
  299. windows_name = grub_util_get_windows_path (dir);
  300. CreateDirectory (windows_name, NULL);
  301. free (windows_name);
  302. }
  303. int
  304. grub_util_rename (const char *from, const char *to)
  305. {
  306. LPTSTR windows_from, windows_to;
  307. int ret;
  308. windows_from = grub_util_get_windows_path (from);
  309. windows_to = grub_util_get_windows_path (to);
  310. ret = !MoveFile (windows_from, windows_to);
  311. free (windows_from);
  312. free (windows_to);
  313. return ret;
  314. }
  315. struct grub_util_fd_dir
  316. {
  317. WIN32_FIND_DATA fd;
  318. HANDLE hnd;
  319. int is_end;
  320. char *last;
  321. };
  322. grub_util_fd_dir_t
  323. grub_util_fd_opendir (const char *name)
  324. {
  325. struct grub_util_fd_dir *ret;
  326. LPTSTR name_windows;
  327. LPTSTR pattern;
  328. ssize_t l;
  329. name_windows = grub_util_get_windows_path (name);
  330. for (l = 0; name_windows[l]; l++);
  331. for (l--; l >= 0 && (name_windows[l] == '\\' || name_windows[l] == '/'); l--);
  332. l++;
  333. pattern = xcalloc (l + 3, sizeof (pattern[0]));
  334. memcpy (pattern, name_windows, l * sizeof (pattern[0]));
  335. pattern[l] = '\\';
  336. pattern[l + 1] = '*';
  337. pattern[l + 2] = '\0';
  338. ret = xmalloc (sizeof (*ret));
  339. memset (ret, 0, sizeof (*ret));
  340. ret->hnd = FindFirstFile (pattern, &ret->fd);
  341. free (name_windows);
  342. free (pattern);
  343. if (ret->hnd == INVALID_HANDLE_VALUE)
  344. {
  345. DWORD err = GetLastError ();
  346. if (err == ERROR_FILE_NOT_FOUND)
  347. {
  348. ret->is_end = 1;
  349. return ret;
  350. }
  351. return NULL;
  352. }
  353. return ret;
  354. }
  355. void
  356. grub_util_fd_closedir (grub_util_fd_dir_t dirp)
  357. {
  358. if (dirp->hnd != INVALID_HANDLE_VALUE)
  359. CloseHandle (dirp->hnd);
  360. free (dirp->last);
  361. free (dirp);
  362. }
  363. grub_util_fd_dirent_t
  364. grub_util_fd_readdir (grub_util_fd_dir_t dirp)
  365. {
  366. char *ret;
  367. free (dirp->last);
  368. dirp->last = NULL;
  369. if (dirp->is_end)
  370. return NULL;
  371. ret = grub_util_tchar_to_utf8 (dirp->fd.cFileName);
  372. dirp->last = ret;
  373. if (!FindNextFile (dirp->hnd, &dirp->fd))
  374. dirp->is_end = 1;
  375. return (grub_util_fd_dirent_t) ret;
  376. }
  377. int
  378. grub_util_unlink (const char *name)
  379. {
  380. LPTSTR name_windows;
  381. int ret;
  382. name_windows = grub_util_get_windows_path (name);
  383. ret = !DeleteFile (name_windows);
  384. free (name_windows);
  385. return ret;
  386. }
  387. int
  388. grub_util_rmdir (const char *name)
  389. {
  390. LPTSTR name_windows;
  391. int ret;
  392. name_windows = grub_util_get_windows_path (name);
  393. ret = !RemoveDirectory (name_windows);
  394. free (name_windows);
  395. return ret;
  396. }
  397. #ifndef __CYGWIN__
  398. static char *
  399. get_temp_name (void)
  400. {
  401. TCHAR rt[1024];
  402. TCHAR *ptr;
  403. HCRYPTPROV hCryptProv;
  404. grub_uint8_t rnd[5];
  405. int i;
  406. GetTempPath (ARRAY_SIZE (rt) - 100, rt);
  407. if (!CryptAcquireContext (&hCryptProv,
  408. NULL,
  409. MS_DEF_PROV,
  410. PROV_RSA_FULL,
  411. CRYPT_VERIFYCONTEXT)
  412. || !CryptGenRandom (hCryptProv, 5, rnd))
  413. grub_util_error ("%s", _("couldn't retrieve random data"));
  414. CryptReleaseContext (hCryptProv, 0);
  415. for (ptr = rt; *ptr; ptr++);
  416. memcpy (ptr, TEXT("\\GRUB."), sizeof (TEXT("\\GRUB.")));
  417. ptr += sizeof ("\\GRUB.") - 1;
  418. for (i = 0; i < 8; i++)
  419. {
  420. grub_size_t b = i * 5;
  421. grub_uint8_t r;
  422. grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
  423. grub_size_t f2;
  424. if (f1 > 5)
  425. f1 = 5;
  426. f2 = 5 - f1;
  427. r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
  428. if (f2)
  429. r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
  430. if (r < 10)
  431. *ptr++ = '0' + r;
  432. else
  433. *ptr++ = 'a' + (r - 10);
  434. }
  435. *ptr = '\0';
  436. return grub_util_tchar_to_utf8 (rt);
  437. }
  438. char *
  439. grub_util_make_temporary_file (void)
  440. {
  441. char *ret = get_temp_name ();
  442. FILE *f;
  443. f = grub_util_fopen (ret, "wb");
  444. if (f)
  445. fclose (f);
  446. return ret;
  447. }
  448. char *
  449. grub_util_make_temporary_dir (void)
  450. {
  451. char *ret = get_temp_name ();
  452. grub_util_mkdir (ret);
  453. return ret;
  454. }
  455. #endif
  456. int
  457. grub_util_is_directory (const char *name)
  458. {
  459. LPTSTR name_windows;
  460. DWORD attr;
  461. name_windows = grub_util_get_windows_path (name);
  462. if (!name_windows)
  463. return 0;
  464. attr = GetFileAttributes (name_windows);
  465. grub_free (name_windows);
  466. return !!(attr & FILE_ATTRIBUTE_DIRECTORY);
  467. }
  468. int
  469. grub_util_is_regular (const char *name)
  470. {
  471. LPTSTR name_windows;
  472. DWORD attr;
  473. name_windows = grub_util_get_windows_path (name);
  474. if (!name_windows)
  475. return 0;
  476. attr = GetFileAttributes (name_windows);
  477. grub_free (name_windows);
  478. return !(attr & FILE_ATTRIBUTE_DIRECTORY)
  479. && !(attr & FILE_ATTRIBUTE_REPARSE_POINT) && attr;
  480. }
  481. grub_uint32_t
  482. grub_util_get_mtime (const char *path)
  483. {
  484. LPTSTR name_windows;
  485. BOOL b;
  486. WIN32_FILE_ATTRIBUTE_DATA attr;
  487. ULARGE_INTEGER us_ul;
  488. name_windows = grub_util_get_windows_path (path);
  489. if (!name_windows)
  490. return 0;
  491. b = GetFileAttributesEx (name_windows, GetFileExInfoStandard, &attr);
  492. grub_free (name_windows);
  493. if (!b)
  494. return 0;
  495. us_ul.LowPart = attr.ftLastWriteTime.dwLowDateTime;
  496. us_ul.HighPart = attr.ftLastWriteTime.dwHighDateTime;
  497. return (us_ul.QuadPart / 10000000)
  498. - 86400ULL * 365 * (1970 - 1601)
  499. - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
  500. }
  501. #ifdef __MINGW32__
  502. FILE *
  503. grub_util_fopen (const char *path, const char *mode)
  504. {
  505. LPTSTR tpath;
  506. FILE *ret;
  507. tpath = grub_util_get_windows_path (path);
  508. #if SIZEOF_TCHAR == 1
  509. ret = fopen (tpath, tmode);
  510. #else
  511. LPTSTR tmode;
  512. tmode = grub_util_utf8_to_tchar (mode);
  513. ret = _wfopen (tpath, tmode);
  514. free (tmode);
  515. #endif
  516. free (tpath);
  517. return ret;
  518. }
  519. int
  520. grub_util_file_sync (FILE *f)
  521. {
  522. HANDLE hnd;
  523. if (fflush (f) != 0)
  524. {
  525. grub_util_info ("fflush err %x", (int) GetLastError ());
  526. return -1;
  527. }
  528. if (!allow_fd_syncs)
  529. return 0;
  530. hnd = (HANDLE) _get_osfhandle (fileno (f));
  531. if (!FlushFileBuffers (hnd))
  532. {
  533. grub_util_info ("flush err %x", (int) GetLastError ());
  534. return -1;
  535. }
  536. return 0;
  537. }
  538. int
  539. grub_util_is_special_file (const char *name)
  540. {
  541. LPTSTR name_windows;
  542. DWORD attr;
  543. name_windows = grub_util_get_windows_path (name);
  544. if (!name_windows)
  545. return 1;
  546. attr = GetFileAttributes (name_windows);
  547. grub_free (name_windows);
  548. return !!(attr & FILE_ATTRIBUTE_REPARSE_POINT) || !attr;
  549. }
  550. #else
  551. void
  552. grub_util_file_sync (FILE *f)
  553. {
  554. fflush (f);
  555. if (!allow_fd_syncs)
  556. return;
  557. fsync (fileno (f));
  558. }
  559. FILE *
  560. grub_util_fopen (const char *path, const char *mode)
  561. {
  562. return fopen (path, mode);
  563. }
  564. #endif