hostdisk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <sys/stat.h>
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #include <limits.h>
  41. #include <time.h>
  42. #include <string.h>
  43. #include <dos/dos.h>
  44. #include <dos/filesystem.h>
  45. #include <dos/exall.h>
  46. #include <proto/dos.h>
  47. #include <devices/hardblocks.h>
  48. #include <devices/newstyle.h>
  49. #include <proto/exec.h>
  50. #include <proto/utility.h>
  51. #include <proto/partition.h>
  52. #include <devices/trackdisk.h>
  53. #include <exec/errors.h>
  54. #define BOUNCE_SIZE 1048576
  55. static ULONG *bounce;
  56. char *
  57. grub_canonicalize_file_name (const char *path)
  58. {
  59. char *ret;
  60. BPTR lck;
  61. if (path[0] == '/' && path[1] == '/' && path[2] == ':')
  62. return xstrdup (path);
  63. ret = xmalloc (2048);
  64. lck = Lock ((const unsigned char *) path, SHARED_LOCK);
  65. if (!lck || !NameFromLock (lck, (unsigned char *) ret, 2040))
  66. {
  67. free (ret);
  68. ret = xstrdup (path);
  69. }
  70. if (lck)
  71. UnLock (lck);
  72. return ret;
  73. }
  74. static grub_uint64_t
  75. grub_util_get_fd_size_volume (grub_util_fd_t fd __attribute__ ((unused)),
  76. const char *dev,
  77. unsigned *log_secsize)
  78. {
  79. struct DriveGeometry *geo;
  80. LONG err;
  81. unsigned sector_size, log_sector_size;
  82. if (!bounce)
  83. bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
  84. if (!bounce)
  85. grub_util_error ("out of memory");
  86. fd->ioreq->iotd_Req.io_Command = TD_GETGEOMETRY;
  87. fd->ioreq->iotd_Req.io_Length = sizeof (*geo);
  88. fd->ioreq->iotd_Req.io_Data = bounce;
  89. fd->ioreq->iotd_Req.io_Offset = 0;
  90. fd->ioreq->iotd_Req.io_Actual = 0;
  91. err = DoIO ((struct IORequest *) fd->ioreq);
  92. if (err)
  93. {
  94. grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
  95. return -1;
  96. }
  97. geo = (struct DriveGeometry *) bounce;
  98. sector_size = geo->dg_SectorSize;
  99. if (sector_size & (sector_size - 1) || !sector_size)
  100. return -1;
  101. log_sector_size = grub_log2ull (sector_size);
  102. if (log_secsize)
  103. *log_secsize = log_sector_size;
  104. return (grub_uint64_t) geo->dg_TotalSectors * (grub_uint64_t) geo->dg_SectorSize;
  105. }
  106. static grub_uint64_t
  107. grub_util_get_fd_size_file (grub_util_fd_t fd,
  108. const char *dev __attribute__ ((unused)),
  109. unsigned *log_secsize)
  110. {
  111. off_t oo, ro;
  112. *log_secsize = 9;
  113. /* FIXME: support 64-bit offsets. */
  114. oo = lseek (fd->fd, 0, SEEK_CUR);
  115. ro = lseek (fd->fd, 0, SEEK_END);
  116. lseek (fd->fd, oo, SEEK_SET);
  117. return ro;
  118. }
  119. int
  120. grub_util_fd_seek (grub_util_fd_t fd, grub_uint64_t off)
  121. {
  122. switch (fd->type)
  123. {
  124. case GRUB_UTIL_FD_FILE:
  125. if (lseek (fd->fd, 0, SEEK_SET) == (off_t) -1)
  126. return -1;
  127. fd->off = off;
  128. return 0;
  129. case GRUB_UTIL_FD_DISK:
  130. fd->off = off;
  131. return 0;
  132. }
  133. return -1;
  134. }
  135. grub_util_fd_t
  136. grub_util_fd_open (const char *dev, int flg)
  137. {
  138. grub_util_fd_t ret = xmalloc (sizeof (*ret));
  139. const char *p1, *p2;
  140. const char *dname;
  141. char *tmp;
  142. IPTR unit = 0;
  143. ULONG flags = 0;
  144. #ifdef O_LARGEFILE
  145. flg |= O_LARGEFILE;
  146. #endif
  147. #ifdef O_BINARY
  148. flg |= O_BINARY;
  149. #endif
  150. ret->off = 0;
  151. if (dev[0] != '/' || dev[1] != '/' || dev[2] != ':')
  152. {
  153. ret->type = GRUB_UTIL_FD_FILE;
  154. ret->fd = open (dev, flg, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR);
  155. if (ret->fd < 0)
  156. {
  157. free (ret);
  158. return NULL;
  159. }
  160. return ret;
  161. }
  162. p1 = strchr (dev + 3, '/');
  163. if (!p1)
  164. p1 = dev + strlen (dev);
  165. else
  166. {
  167. unit = grub_strtoul (p1 + 1, &p2, 16);
  168. if (p2 && *p2 == '/')
  169. flags = grub_strtoul (p2 + 1, 0, 16);
  170. }
  171. ret->mp = CreateMsgPort();
  172. if (!ret->mp)
  173. {
  174. free (ret);
  175. return NULL;
  176. }
  177. ret->ioreq = (struct IOExtTD *) CreateIORequest(ret->mp,
  178. sizeof(struct IOExtTD));
  179. if (!ret->ioreq)
  180. {
  181. free (ret);
  182. DeleteMsgPort (ret->mp);
  183. return NULL;
  184. }
  185. dname = dev + 3;
  186. ret->type = GRUB_UTIL_FD_DISK;
  187. tmp = xmalloc (p1 - dname + 1);
  188. memcpy (tmp, dname, p1 - dname);
  189. tmp[p1 - dname] = '\0';
  190. ret->is_floppy = (strcmp (tmp, TD_NAME) == 0);
  191. ret->is_64 = 1;
  192. if (OpenDevice ((unsigned char *) tmp, unit,
  193. (struct IORequest *) ret->ioreq, flags))
  194. {
  195. free (tmp);
  196. free (ret);
  197. DeleteMsgPort (ret->mp);
  198. return NULL;
  199. }
  200. free (tmp);
  201. return ret;
  202. }
  203. static ssize_t
  204. grub_util_fd_read_file (grub_util_fd_t fd, char *buf, size_t len)
  205. {
  206. ssize_t size = len;
  207. while (len)
  208. {
  209. ssize_t ret = read (fd->fd, buf, len);
  210. if (ret <= 0)
  211. {
  212. if (errno == EINTR)
  213. continue;
  214. else
  215. return ret;
  216. }
  217. fd->off += ret;
  218. len -= ret;
  219. buf += ret;
  220. }
  221. return size;
  222. }
  223. /* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
  224. error occurs, otherwise return LEN. */
  225. static ssize_t
  226. grub_util_fd_write_file (grub_util_fd_t fd, const char *buf, size_t len)
  227. {
  228. ssize_t size = len;
  229. while (len)
  230. {
  231. ssize_t ret = write (fd->fd, buf, len);
  232. if (ret <= 0)
  233. {
  234. if (errno == EINTR)
  235. continue;
  236. else
  237. return ret;
  238. }
  239. fd->off += ret;
  240. len -= ret;
  241. buf += ret;
  242. }
  243. return size;
  244. }
  245. static void
  246. stop_motor (grub_util_fd_t fd)
  247. {
  248. if (!fd->is_floppy)
  249. return;
  250. fd->ioreq->iotd_Req.io_Command = TD_MOTOR;
  251. fd->ioreq->iotd_Req.io_Length = 0;
  252. fd->ioreq->iotd_Req.io_Data = 0;
  253. fd->ioreq->iotd_Req.io_Offset = 0;
  254. fd->ioreq->iotd_Req.io_Actual = 0;
  255. DoIO ((struct IORequest *) fd->ioreq);
  256. }
  257. static ssize_t
  258. grub_util_fd_read_volume (grub_util_fd_t fd, char *buf, size_t len)
  259. {
  260. grub_uint64_t adj = 0;
  261. if (!bounce)
  262. bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
  263. if (!bounce)
  264. grub_util_error ("out of memory");
  265. while (len)
  266. {
  267. size_t cr = len;
  268. LONG err;
  269. if (cr > BOUNCE_SIZE)
  270. cr = BOUNCE_SIZE;
  271. retry:
  272. if (fd->is_64)
  273. fd->ioreq->iotd_Req.io_Command = NSCMD_TD_READ64;
  274. else
  275. fd->ioreq->iotd_Req.io_Command = CMD_READ;
  276. fd->ioreq->iotd_Req.io_Length = cr;
  277. fd->ioreq->iotd_Req.io_Data = bounce;
  278. fd->ioreq->iotd_Req.io_Offset = fd->off & 0xFFFFFFFF;
  279. fd->ioreq->iotd_Req.io_Actual = fd->off >> 32;
  280. err = DoIO ((struct IORequest *) fd->ioreq);
  281. if (err == IOERR_NOCMD && fd->is_64)
  282. {
  283. fd->is_64 = 0;
  284. goto retry;
  285. }
  286. if (err)
  287. {
  288. grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
  289. stop_motor (fd);
  290. return -1;
  291. }
  292. memcpy (buf, bounce, cr);
  293. adj += cr;
  294. len -= cr;
  295. buf += cr;
  296. }
  297. fd->off += adj;
  298. stop_motor (fd);
  299. return adj;
  300. }
  301. static ssize_t
  302. grub_util_fd_write_volume (grub_util_fd_t fd, const char *buf, size_t len)
  303. {
  304. grub_uint64_t adj = 0;
  305. if (!bounce)
  306. bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR);
  307. if (!bounce)
  308. grub_util_error ("out of memory");
  309. while (len)
  310. {
  311. size_t cr = len;
  312. LONG err;
  313. if (cr > BOUNCE_SIZE)
  314. cr = BOUNCE_SIZE;
  315. retry:
  316. if (fd->is_64)
  317. fd->ioreq->iotd_Req.io_Command = NSCMD_TD_WRITE64;
  318. else
  319. fd->ioreq->iotd_Req.io_Command = CMD_WRITE;
  320. fd->ioreq->iotd_Req.io_Length = cr;
  321. fd->ioreq->iotd_Req.io_Data = bounce;
  322. fd->ioreq->iotd_Req.io_Offset = fd->off & 0xFFFFFFFF;
  323. fd->ioreq->iotd_Req.io_Actual = fd->off >> 32;
  324. memcpy (bounce, buf, cr);
  325. err = DoIO ((struct IORequest *) fd->ioreq);
  326. if (err == IOERR_NOCMD && fd->is_64)
  327. {
  328. fd->is_64 = 0;
  329. goto retry;
  330. }
  331. if (err)
  332. {
  333. grub_util_info ("I/O failed with error %d", err);
  334. stop_motor (fd);
  335. return -1;
  336. }
  337. adj += cr;
  338. len -= cr;
  339. buf += cr;
  340. }
  341. fd->off += adj;
  342. stop_motor (fd);
  343. return adj;
  344. }
  345. ssize_t
  346. grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
  347. {
  348. switch (fd->type)
  349. {
  350. case GRUB_UTIL_FD_FILE:
  351. return grub_util_fd_read_file (fd, buf, len);
  352. case GRUB_UTIL_FD_DISK:
  353. return grub_util_fd_read_volume (fd, buf, len);
  354. }
  355. return -1;
  356. }
  357. ssize_t
  358. grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
  359. {
  360. switch (fd->type)
  361. {
  362. case GRUB_UTIL_FD_FILE:
  363. return grub_util_fd_write_file (fd, buf, len);
  364. case GRUB_UTIL_FD_DISK:
  365. return grub_util_fd_write_volume (fd, buf, len);
  366. }
  367. return -1;
  368. }
  369. grub_uint64_t
  370. grub_util_get_fd_size (grub_util_fd_t fd,
  371. const char *dev,
  372. unsigned *log_secsize)
  373. {
  374. switch (fd->type)
  375. {
  376. case GRUB_UTIL_FD_FILE:
  377. return grub_util_get_fd_size_file (fd, dev, log_secsize);
  378. case GRUB_UTIL_FD_DISK:
  379. return grub_util_get_fd_size_volume (fd, dev, log_secsize);
  380. }
  381. return -1;
  382. }
  383. int
  384. grub_util_fd_close (grub_util_fd_t fd)
  385. {
  386. switch (fd->type)
  387. {
  388. case GRUB_UTIL_FD_FILE:
  389. return close (fd->fd);
  390. case GRUB_UTIL_FD_DISK:
  391. CloseDevice ((struct IORequest *) fd->ioreq);
  392. DeleteIORequest((struct IORequest *) fd->ioreq);
  393. DeleteMsgPort (fd->mp);
  394. return 0;
  395. }
  396. return 0;
  397. }
  398. static int allow_fd_syncs = 1;
  399. static int
  400. grub_util_fd_sync_volume (grub_util_fd_t fd)
  401. {
  402. LONG err;
  403. fd->ioreq->iotd_Req.io_Command = CMD_UPDATE;
  404. fd->ioreq->iotd_Req.io_Length = 0;
  405. fd->ioreq->iotd_Req.io_Data = 0;
  406. fd->ioreq->iotd_Req.io_Offset = 0;
  407. fd->ioreq->iotd_Req.io_Actual = 0;
  408. err = DoIO ((struct IORequest *) fd->ioreq);
  409. if (err)
  410. {
  411. grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
  412. return -1;
  413. }
  414. return 0;
  415. }
  416. int
  417. grub_util_fd_sync (grub_util_fd_t fd)
  418. {
  419. if (allow_fd_syncs)
  420. {
  421. switch (fd->type)
  422. {
  423. case GRUB_UTIL_FD_FILE:
  424. return fsync (fd->fd);
  425. case GRUB_UTIL_FD_DISK:
  426. return grub_util_fd_sync_volume (fd);
  427. }
  428. }
  429. return 0;
  430. }
  431. int
  432. grub_util_file_sync (FILE *f)
  433. {
  434. if (fflush (f) != 0)
  435. return -1;
  436. if (!allow_fd_syncs)
  437. return 0;
  438. return fsync (fileno (f));
  439. }
  440. void
  441. grub_util_disable_fd_syncs (void)
  442. {
  443. allow_fd_syncs = 0;
  444. }
  445. void
  446. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  447. {
  448. }
  449. const char *
  450. grub_util_fd_strerror (void)
  451. {
  452. static char buf[201];
  453. LONG err = IoErr ();
  454. if (!err)
  455. return _("Success");
  456. memset (buf, '\0', sizeof (buf));
  457. Fault (err, (const unsigned char *) "", (STRPTR) buf, sizeof (buf));
  458. if (buf[0] == ':')
  459. return buf + 1;
  460. return buf;
  461. }
  462. FILE *
  463. grub_util_fopen (const char *path, const char *mode)
  464. {
  465. return fopen (path, mode);
  466. }
  467. int
  468. grub_util_is_directory (const char *path)
  469. {
  470. struct stat st;
  471. if (stat (path, &st) == -1)
  472. return 0;
  473. return S_ISDIR (st.st_mode);
  474. }
  475. int
  476. grub_util_is_regular (const char *path)
  477. {
  478. struct stat st;
  479. if (stat (path, &st) == -1)
  480. return 0;
  481. return S_ISREG (st.st_mode);
  482. }
  483. int
  484. grub_util_is_special_file (const char *path)
  485. {
  486. struct stat st;
  487. if (lstat (path, &st) == -1)
  488. return 1;
  489. return (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode));
  490. }
  491. static char *
  492. get_temp_name (void)
  493. {
  494. static int ctr = 0;
  495. char *t;
  496. struct stat st;
  497. while (1)
  498. {
  499. t = xasprintf ("T:grub.%d.%d.%d.%d", (int) getpid (), (int) getppid (),
  500. ctr++, time (0));
  501. if (stat (t, &st) == -1)
  502. return t;
  503. free (t);
  504. }
  505. }
  506. char *
  507. grub_util_make_temporary_file (void)
  508. {
  509. char *ret = get_temp_name ();
  510. FILE *f;
  511. f = grub_util_fopen (ret, "wb");
  512. if (f)
  513. fclose (f);
  514. return ret;
  515. }
  516. char *
  517. grub_util_make_temporary_dir (void)
  518. {
  519. char *ret = get_temp_name ();
  520. grub_util_mkdir (ret);
  521. return ret;
  522. }
  523. grub_uint32_t
  524. grub_util_get_mtime (const char *path)
  525. {
  526. struct stat st;
  527. if (stat (path, &st) == -1)
  528. return 0;
  529. return st.st_mtime;
  530. }