hostdisk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. int
  233. grub_util_fd_sync (grub_util_fd_t fd)
  234. {
  235. if (allow_fd_syncs)
  236. {
  237. if (!FlushFileBuffers (fd))
  238. {
  239. grub_util_info ("flush err %x", (int) GetLastError ());
  240. return -1;
  241. }
  242. }
  243. return 0;
  244. }
  245. void
  246. grub_util_disable_fd_syncs (void)
  247. {
  248. allow_fd_syncs = 0;
  249. }
  250. int
  251. grub_util_fd_close (grub_util_fd_t fd)
  252. {
  253. if (!CloseHandle (fd))
  254. {
  255. grub_util_info ("close err %x", (int) GetLastError ());
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. const char *
  261. grub_util_fd_strerror (void)
  262. {
  263. DWORD err = GetLastError ();
  264. LPTSTR tstr = NULL;
  265. static char *last;
  266. char *ret, *ptr;
  267. free (last);
  268. last = 0;
  269. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
  270. | FORMAT_MESSAGE_IGNORE_INSERTS,
  271. NULL, err, 0, (void *) &tstr,
  272. 0, NULL);
  273. if (!tstr)
  274. return "unknown error";
  275. ret = grub_util_tchar_to_utf8 (tstr);
  276. LocalFree (tstr);
  277. last = ret;
  278. for (ptr = ret + strlen (ret) - 1;
  279. ptr >= ret && (*ptr == '\n' || *ptr == '\r');
  280. ptr--);
  281. ptr[1] = '\0';
  282. return ret;
  283. }
  284. char *
  285. grub_canonicalize_file_name (const char *path)
  286. {
  287. char *ret;
  288. LPTSTR windows_path;
  289. ret = xmalloc (PATH_MAX);
  290. windows_path = grub_util_get_windows_path (path);
  291. if (!windows_path)
  292. return NULL;
  293. ret = grub_util_tchar_to_utf8 (windows_path);
  294. free (windows_path);
  295. return ret;
  296. }
  297. void
  298. grub_util_mkdir (const char *dir)
  299. {
  300. LPTSTR windows_name;
  301. windows_name = grub_util_get_windows_path (dir);
  302. CreateDirectory (windows_name, NULL);
  303. free (windows_name);
  304. }
  305. int
  306. grub_util_rename (const char *from, const char *to)
  307. {
  308. LPTSTR windows_from, windows_to;
  309. int ret;
  310. windows_from = grub_util_get_windows_path (from);
  311. windows_to = grub_util_get_windows_path (to);
  312. ret = !MoveFile (windows_from, windows_to);
  313. free (windows_from);
  314. free (windows_to);
  315. return ret;
  316. }
  317. struct grub_util_fd_dir
  318. {
  319. WIN32_FIND_DATA fd;
  320. HANDLE hnd;
  321. int is_end;
  322. char *last;
  323. };
  324. grub_util_fd_dir_t
  325. grub_util_fd_opendir (const char *name)
  326. {
  327. struct grub_util_fd_dir *ret;
  328. LPTSTR name_windows;
  329. LPTSTR pattern;
  330. ssize_t l;
  331. name_windows = grub_util_get_windows_path (name);
  332. for (l = 0; name_windows[l]; l++);
  333. for (l--; l >= 0 && (name_windows[l] == '\\' || name_windows[l] == '/'); l--);
  334. l++;
  335. pattern = xmalloc ((l + 3) * sizeof (pattern[0]));
  336. memcpy (pattern, name_windows, l * sizeof (pattern[0]));
  337. pattern[l] = '\\';
  338. pattern[l + 1] = '*';
  339. pattern[l + 2] = '\0';
  340. ret = xmalloc (sizeof (*ret));
  341. memset (ret, 0, sizeof (*ret));
  342. ret->hnd = FindFirstFile (pattern, &ret->fd);
  343. free (name_windows);
  344. free (pattern);
  345. if (ret->hnd == INVALID_HANDLE_VALUE)
  346. {
  347. DWORD err = GetLastError ();
  348. if (err == ERROR_FILE_NOT_FOUND)
  349. {
  350. ret->is_end = 1;
  351. return ret;
  352. }
  353. return NULL;
  354. }
  355. return ret;
  356. }
  357. void
  358. grub_util_fd_closedir (grub_util_fd_dir_t dirp)
  359. {
  360. if (dirp->hnd != INVALID_HANDLE_VALUE)
  361. CloseHandle (dirp->hnd);
  362. free (dirp->last);
  363. free (dirp);
  364. }
  365. grub_util_fd_dirent_t
  366. grub_util_fd_readdir (grub_util_fd_dir_t dirp)
  367. {
  368. char *ret;
  369. free (dirp->last);
  370. dirp->last = NULL;
  371. if (dirp->is_end)
  372. return NULL;
  373. ret = grub_util_tchar_to_utf8 (dirp->fd.cFileName);
  374. dirp->last = ret;
  375. if (!FindNextFile (dirp->hnd, &dirp->fd))
  376. dirp->is_end = 1;
  377. return (grub_util_fd_dirent_t) ret;
  378. }
  379. int
  380. grub_util_unlink (const char *name)
  381. {
  382. LPTSTR name_windows;
  383. int ret;
  384. name_windows = grub_util_get_windows_path (name);
  385. ret = !DeleteFile (name_windows);
  386. free (name_windows);
  387. return ret;
  388. }
  389. int
  390. grub_util_rmdir (const char *name)
  391. {
  392. LPTSTR name_windows;
  393. int ret;
  394. name_windows = grub_util_get_windows_path (name);
  395. ret = !RemoveDirectory (name_windows);
  396. free (name_windows);
  397. return ret;
  398. }
  399. #ifndef __CYGWIN__
  400. static char *
  401. get_temp_name (void)
  402. {
  403. TCHAR rt[1024];
  404. TCHAR *ptr;
  405. HCRYPTPROV hCryptProv;
  406. grub_uint8_t rnd[5];
  407. int i;
  408. GetTempPath (ARRAY_SIZE (rt) - 100, rt);
  409. if (!CryptAcquireContext (&hCryptProv,
  410. NULL,
  411. MS_DEF_PROV,
  412. PROV_RSA_FULL,
  413. CRYPT_VERIFYCONTEXT)
  414. || !CryptGenRandom (hCryptProv, 5, rnd))
  415. grub_util_error ("%s", _("couldn't retrieve random data"));
  416. CryptReleaseContext (hCryptProv, 0);
  417. for (ptr = rt; *ptr; ptr++);
  418. memcpy (ptr, TEXT("\\GRUB."), sizeof (TEXT("\\GRUB.")));
  419. ptr += sizeof ("\\GRUB.") - 1;
  420. for (i = 0; i < 8; i++)
  421. {
  422. grub_size_t b = i * 5;
  423. grub_uint8_t r;
  424. grub_size_t f1 = GRUB_CHAR_BIT - b % GRUB_CHAR_BIT;
  425. grub_size_t f2;
  426. if (f1 > 5)
  427. f1 = 5;
  428. f2 = 5 - f1;
  429. r = (rnd[b / GRUB_CHAR_BIT] >> (b % GRUB_CHAR_BIT)) & ((1 << f1) - 1);
  430. if (f2)
  431. r |= (rnd[b / GRUB_CHAR_BIT + 1] & ((1 << f2) - 1)) << f1;
  432. if (r < 10)
  433. *ptr++ = '0' + r;
  434. else
  435. *ptr++ = 'a' + (r - 10);
  436. }
  437. *ptr = '\0';
  438. return grub_util_tchar_to_utf8 (rt);
  439. }
  440. char *
  441. grub_util_make_temporary_file (void)
  442. {
  443. char *ret = get_temp_name ();
  444. FILE *f;
  445. f = grub_util_fopen (ret, "wb");
  446. if (f)
  447. fclose (f);
  448. return ret;
  449. }
  450. char *
  451. grub_util_make_temporary_dir (void)
  452. {
  453. char *ret = get_temp_name ();
  454. grub_util_mkdir (ret);
  455. return ret;
  456. }
  457. #endif
  458. int
  459. grub_util_is_directory (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. }
  470. int
  471. grub_util_is_regular (const char *name)
  472. {
  473. LPTSTR name_windows;
  474. DWORD attr;
  475. name_windows = grub_util_get_windows_path (name);
  476. if (!name_windows)
  477. return 0;
  478. attr = GetFileAttributes (name_windows);
  479. grub_free (name_windows);
  480. return !(attr & FILE_ATTRIBUTE_DIRECTORY)
  481. && !(attr & FILE_ATTRIBUTE_REPARSE_POINT) && attr;
  482. }
  483. grub_uint32_t
  484. grub_util_get_mtime (const char *path)
  485. {
  486. LPTSTR name_windows;
  487. BOOL b;
  488. WIN32_FILE_ATTRIBUTE_DATA attr;
  489. ULARGE_INTEGER us_ul;
  490. name_windows = grub_util_get_windows_path (path);
  491. if (!name_windows)
  492. return 0;
  493. b = GetFileAttributesEx (name_windows, GetFileExInfoStandard, &attr);
  494. grub_free (name_windows);
  495. if (!b)
  496. return 0;
  497. us_ul.LowPart = attr.ftLastWriteTime.dwLowDateTime;
  498. us_ul.HighPart = attr.ftLastWriteTime.dwHighDateTime;
  499. return (us_ul.QuadPart / 10000000)
  500. - 86400ULL * 365 * (1970 - 1601)
  501. - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
  502. }
  503. #ifdef __MINGW32__
  504. FILE *
  505. grub_util_fopen (const char *path, const char *mode)
  506. {
  507. LPTSTR tpath;
  508. FILE *ret;
  509. tpath = grub_util_get_windows_path (path);
  510. #if SIZEOF_TCHAR == 1
  511. ret = fopen (tpath, tmode);
  512. #else
  513. LPTSTR tmode;
  514. tmode = grub_util_utf8_to_tchar (mode);
  515. ret = _wfopen (tpath, tmode);
  516. free (tmode);
  517. #endif
  518. free (tpath);
  519. return ret;
  520. }
  521. int
  522. grub_util_file_sync (FILE *f)
  523. {
  524. HANDLE hnd;
  525. if (fflush (f) != 0)
  526. {
  527. grub_util_info ("fflush err %x", (int) GetLastError ());
  528. return -1;
  529. }
  530. if (!allow_fd_syncs)
  531. return 0;
  532. hnd = (HANDLE) _get_osfhandle (fileno (f));
  533. if (!FlushFileBuffers (hnd))
  534. {
  535. grub_util_info ("flush err %x", (int) GetLastError ());
  536. return -1;
  537. }
  538. return 0;
  539. }
  540. int
  541. grub_util_is_special_file (const char *name)
  542. {
  543. LPTSTR name_windows;
  544. DWORD attr;
  545. name_windows = grub_util_get_windows_path (name);
  546. if (!name_windows)
  547. return 1;
  548. attr = GetFileAttributes (name_windows);
  549. grub_free (name_windows);
  550. return !!(attr & FILE_ATTRIBUTE_REPARSE_POINT) || !attr;
  551. }
  552. #else
  553. void
  554. grub_util_file_sync (FILE *f)
  555. {
  556. fflush (f);
  557. if (!allow_fd_syncs)
  558. return;
  559. fsync (fileno (f));
  560. }
  561. FILE *
  562. grub_util_fopen (const char *path, const char *mode)
  563. {
  564. return fopen (path, mode);
  565. }
  566. #endif