hostdisk.c 12 KB

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