hostdisk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* hostdisk.c - emulate biosdisk */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config-util.h>
  20. #include <grub/disk.h>
  21. #include <grub/partition.h>
  22. #include <grub/msdos_partition.h>
  23. #include <grub/types.h>
  24. #include <grub/err.h>
  25. #include <grub/emu/misc.h>
  26. #include <grub/emu/hostdisk.h>
  27. #include <grub/emu/getroot.h>
  28. #include <grub/misc.h>
  29. #include <grub/i18n.h>
  30. #include <grub/list.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <assert.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #include <errno.h>
  41. #include <limits.h>
  42. #ifdef __linux__
  43. # include <sys/ioctl.h> /* ioctl */
  44. # include <sys/mount.h>
  45. # ifndef BLKFLSBUF
  46. # define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */
  47. # endif /* ! BLKFLSBUF */
  48. #endif /* __linux__ */
  49. static struct
  50. {
  51. char *drive;
  52. char *device;
  53. int device_map;
  54. } map[256];
  55. static int
  56. unescape_cmp (const char *a, const char *b_escaped)
  57. {
  58. while (*a || *b_escaped)
  59. {
  60. if (*b_escaped == '\\' && b_escaped[1] != 0)
  61. b_escaped++;
  62. if (*a < *b_escaped)
  63. return -1;
  64. if (*a > *b_escaped)
  65. return +1;
  66. a++;
  67. b_escaped++;
  68. }
  69. if (*a)
  70. return +1;
  71. if (*b_escaped)
  72. return -1;
  73. return 0;
  74. }
  75. static int
  76. find_grub_drive (const char *name)
  77. {
  78. unsigned int i;
  79. if (name)
  80. {
  81. for (i = 0; i < ARRAY_SIZE (map); i++)
  82. if (map[i].drive && unescape_cmp (map[i].drive, name) == 0)
  83. return i;
  84. }
  85. return -1;
  86. }
  87. static int
  88. find_free_slot (void)
  89. {
  90. unsigned int i;
  91. for (i = 0; i < ARRAY_SIZE (map); i++)
  92. if (! map[i].drive)
  93. return i;
  94. return -1;
  95. }
  96. static int
  97. grub_util_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  98. grub_disk_pull_t pull)
  99. {
  100. unsigned i;
  101. if (pull != GRUB_DISK_PULL_NONE)
  102. return 0;
  103. for (i = 0; i < ARRAY_SIZE (map); i++)
  104. if (map[i].drive && hook (map[i].drive, hook_data))
  105. return 1;
  106. return 0;
  107. }
  108. static grub_err_t
  109. grub_util_biosdisk_open (const char *name, grub_disk_t disk)
  110. {
  111. int drive;
  112. struct grub_util_hostdisk_data *data;
  113. drive = find_grub_drive (name);
  114. grub_util_info ("drive = %d", drive);
  115. if (drive < 0)
  116. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  117. "no mapping exists for `%s'", name);
  118. disk->id = drive;
  119. disk->data = data = xmalloc (sizeof (struct grub_util_hostdisk_data));
  120. data->dev = NULL;
  121. data->access_mode = 0;
  122. data->fd = GRUB_UTIL_FD_INVALID;
  123. data->is_disk = 0;
  124. data->device_map = map[drive].device_map;
  125. /* Get the size. */
  126. {
  127. grub_util_fd_t fd;
  128. fd = grub_util_fd_open (map[drive].device, GRUB_UTIL_FD_O_RDONLY);
  129. if (!GRUB_UTIL_FD_IS_VALID(fd))
  130. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"),
  131. map[drive].device, grub_util_fd_strerror ());
  132. disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device,
  133. &disk->log_sector_size);
  134. disk->total_sectors >>= disk->log_sector_size;
  135. disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
  136. #if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
  137. {
  138. struct stat st;
  139. # if GRUB_DISK_DEVS_ARE_CHAR
  140. if (fstat (fd, &st) >= 0 && S_ISCHR (st.st_mode))
  141. # else
  142. if (fstat (fd, &st) >= 0 && S_ISBLK (st.st_mode))
  143. # endif
  144. data->is_disk = 1;
  145. }
  146. #endif
  147. grub_util_fd_close (fd);
  148. grub_util_info ("the size of %s is %" GRUB_HOST_PRIuLONG_LONG,
  149. name, (unsigned long long) disk->total_sectors);
  150. return GRUB_ERR_NONE;
  151. }
  152. }
  153. const char *
  154. grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
  155. {
  156. unsigned int i;
  157. char *canon;
  158. canon = grub_canonicalize_file_name (os_disk);
  159. if (!canon)
  160. canon = xstrdup (os_disk);
  161. for (i = 0; i < ARRAY_SIZE (map); i++)
  162. if (! map[i].device)
  163. break;
  164. else if (strcmp (map[i].device, canon) == 0)
  165. {
  166. free (canon);
  167. return map[i].drive;
  168. }
  169. if (!add)
  170. {
  171. free (canon);
  172. return NULL;
  173. }
  174. if (i == ARRAY_SIZE (map))
  175. /* TRANSLATORS: it refers to the lack of free slots. */
  176. grub_util_error ("%s", _("device count exceeds limit"));
  177. map[i].device = canon;
  178. map[i].drive = xmalloc (sizeof ("hostdisk/") + strlen (os_disk));
  179. strcpy (map[i].drive, "hostdisk/");
  180. strcpy (map[i].drive + sizeof ("hostdisk/") - 1, os_disk);
  181. map[i].device_map = 0;
  182. grub_hostdisk_flush_initial_buffer (os_disk);
  183. return map[i].drive;
  184. }
  185. #ifndef __linux__
  186. grub_util_fd_t
  187. grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
  188. grub_disk_addr_t *max)
  189. {
  190. grub_util_fd_t fd;
  191. struct grub_util_hostdisk_data *data = disk->data;
  192. *max = ~0ULL;
  193. flags |= GRUB_UTIL_FD_O_SYNC;
  194. if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
  195. data->access_mode == (flags & O_ACCMODE))
  196. {
  197. grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev);
  198. fd = data->fd;
  199. }
  200. else
  201. {
  202. free (data->dev);
  203. data->dev = 0;
  204. if (GRUB_UTIL_FD_IS_VALID(data->fd))
  205. {
  206. if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
  207. grub_util_fd_sync (data->fd);
  208. grub_util_fd_close (data->fd);
  209. data->fd = GRUB_UTIL_FD_INVALID;
  210. }
  211. fd = grub_util_fd_open (map[disk->id].device, flags);
  212. if (GRUB_UTIL_FD_IS_VALID(fd))
  213. {
  214. data->dev = xstrdup (map[disk->id].device);
  215. data->access_mode = (flags & O_ACCMODE);
  216. data->fd = fd;
  217. }
  218. }
  219. if (!GRUB_UTIL_FD_IS_VALID(data->fd))
  220. {
  221. grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
  222. map[disk->id].device, grub_util_fd_strerror ());
  223. return GRUB_UTIL_FD_INVALID;
  224. }
  225. if (grub_util_fd_seek (fd, sector << disk->log_sector_size))
  226. {
  227. grub_util_fd_close (fd);
  228. grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
  229. map[disk->id].device, grub_util_fd_strerror ());
  230. return GRUB_UTIL_FD_INVALID;
  231. }
  232. return fd;
  233. }
  234. #endif
  235. static grub_err_t
  236. grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
  237. grub_size_t size, char *buf)
  238. {
  239. while (size)
  240. {
  241. grub_util_fd_t fd;
  242. grub_disk_addr_t max = ~0ULL;
  243. fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_RDONLY, &max);
  244. if (!GRUB_UTIL_FD_IS_VALID (fd))
  245. return grub_errno;
  246. #ifdef __linux__
  247. if (sector == 0)
  248. /* Work around a bug in Linux ez remapping. Linux remaps all
  249. sectors that are read together with the MBR in one read. It
  250. should only remap the MBR, so we split the read in two
  251. parts. -jochen */
  252. max = 1;
  253. #endif /* __linux__ */
  254. if (max > size)
  255. max = size;
  256. if (grub_util_fd_read (fd, buf, max << disk->log_sector_size)
  257. != (ssize_t) (max << disk->log_sector_size))
  258. return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
  259. map[disk->id].device, grub_util_fd_strerror ());
  260. size -= max;
  261. buf += (max << disk->log_sector_size);
  262. sector += max;
  263. }
  264. return GRUB_ERR_NONE;
  265. }
  266. static grub_err_t
  267. grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
  268. grub_size_t size, const char *buf)
  269. {
  270. while (size)
  271. {
  272. grub_util_fd_t fd;
  273. grub_disk_addr_t max = ~0ULL;
  274. fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_WRONLY, &max);
  275. if (!GRUB_UTIL_FD_IS_VALID (fd))
  276. return grub_errno;
  277. #ifdef __linux__
  278. if (sector == 0)
  279. /* Work around a bug in Linux ez remapping. Linux remaps all
  280. sectors that are write together with the MBR in one write. It
  281. should only remap the MBR, so we split the write in two
  282. parts. -jochen */
  283. max = 1;
  284. #endif /* __linux__ */
  285. if (max > size)
  286. max = size;
  287. if (grub_util_fd_write (fd, buf, max << disk->log_sector_size)
  288. != (ssize_t) (max << disk->log_sector_size))
  289. return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"),
  290. map[disk->id].device, grub_util_fd_strerror ());
  291. size -= max;
  292. buf += (max << disk->log_sector_size);
  293. }
  294. return GRUB_ERR_NONE;
  295. }
  296. grub_err_t
  297. grub_util_biosdisk_flush (struct grub_disk *disk)
  298. {
  299. struct grub_util_hostdisk_data *data = disk->data;
  300. if (disk->dev->id != GRUB_DISK_DEVICE_BIOSDISK_ID)
  301. return GRUB_ERR_NONE;
  302. if (!GRUB_UTIL_FD_IS_VALID (data->fd))
  303. {
  304. grub_disk_addr_t max;
  305. data->fd = grub_util_fd_open_device (disk, 0, GRUB_UTIL_FD_O_RDONLY, &max);
  306. if (!GRUB_UTIL_FD_IS_VALID (data->fd))
  307. return grub_errno;
  308. }
  309. grub_util_fd_sync (data->fd);
  310. #ifdef __linux__
  311. if (data->is_disk)
  312. ioctl (data->fd, BLKFLSBUF, 0);
  313. #endif
  314. return GRUB_ERR_NONE;
  315. }
  316. static void
  317. grub_util_biosdisk_close (struct grub_disk *disk)
  318. {
  319. struct grub_util_hostdisk_data *data = disk->data;
  320. free (data->dev);
  321. if (GRUB_UTIL_FD_IS_VALID (data->fd))
  322. {
  323. if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
  324. grub_util_biosdisk_flush (disk);
  325. grub_util_fd_close (data->fd);
  326. }
  327. free (data);
  328. }
  329. static struct grub_disk_dev grub_util_biosdisk_dev =
  330. {
  331. .name = "hostdisk",
  332. .id = GRUB_DISK_DEVICE_HOSTDISK_ID,
  333. .disk_iterate = grub_util_biosdisk_iterate,
  334. .disk_open = grub_util_biosdisk_open,
  335. .disk_close = grub_util_biosdisk_close,
  336. .disk_read = grub_util_biosdisk_read,
  337. .disk_write = grub_util_biosdisk_write,
  338. .next = 0
  339. };
  340. static int
  341. grub_util_check_file_presence (const char *p)
  342. {
  343. #if !GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
  344. grub_util_fd_t h;
  345. h = grub_util_fd_open (p, GRUB_UTIL_FD_O_RDONLY);
  346. if (!GRUB_UTIL_FD_IS_VALID(h))
  347. return 0;
  348. grub_util_fd_close (h);
  349. return 1;
  350. #else
  351. struct stat st;
  352. if (stat (p, &st) == -1)
  353. return 0;
  354. return 1;
  355. #endif
  356. }
  357. static void
  358. read_device_map (const char *dev_map)
  359. {
  360. FILE *fp;
  361. char buf[1024]; /* XXX */
  362. int lineno = 0;
  363. if (!dev_map || dev_map[0] == '\0')
  364. {
  365. grub_util_info ("no device.map");
  366. return;
  367. }
  368. fp = grub_util_fopen (dev_map, "r");
  369. if (! fp)
  370. {
  371. grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno));
  372. return;
  373. }
  374. while (fgets (buf, sizeof (buf), fp))
  375. {
  376. char *p = buf;
  377. char *e;
  378. char *drive_e, *drive_p;
  379. int drive;
  380. lineno++;
  381. /* Skip leading spaces. */
  382. while (*p && grub_isspace (*p))
  383. p++;
  384. /* If the first character is `#' or NUL, skip this line. */
  385. if (*p == '\0' || *p == '#')
  386. continue;
  387. if (*p != '(')
  388. {
  389. char *tmp;
  390. tmp = xasprintf (_("missing `%c' symbol"), '(');
  391. grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
  392. }
  393. p++;
  394. /* Find a free slot. */
  395. drive = find_free_slot ();
  396. if (drive < 0)
  397. grub_util_error ("%s:%d: %s", dev_map, lineno, _("device count exceeds limit"));
  398. e = p;
  399. p = strchr (p, ')');
  400. if (! p)
  401. {
  402. char *tmp;
  403. tmp = xasprintf (_("missing `%c' symbol"), ')');
  404. grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
  405. }
  406. map[drive].drive = 0;
  407. if ((e[0] == 'f' || e[0] == 'h' || e[0] == 'c') && e[1] == 'd')
  408. {
  409. char *ptr;
  410. for (ptr = e + 2; ptr < p; ptr++)
  411. if (!grub_isdigit (*ptr))
  412. break;
  413. if (ptr == p)
  414. {
  415. map[drive].drive = xmalloc (p - e + sizeof ('\0'));
  416. strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
  417. map[drive].drive[p - e] = '\0';
  418. }
  419. if (*ptr == ',')
  420. {
  421. *p = 0;
  422. /* TRANSLATORS: Only one entry is ignored. However the suggestion
  423. is to correct/delete the whole file.
  424. device.map is a file indicating which
  425. devices are available at boot time. Fedora populated it with
  426. entries like (hd0,1) /dev/sda1 which would mean that every
  427. partition is a separate disk for BIOS. Such entries were
  428. inactive in GRUB due to its bug which is now gone. Without
  429. this additional check these entries would be harmful now.
  430. */
  431. grub_util_warn (_("the device.map entry `%s' is invalid. "
  432. "Ignoring it. Please correct or "
  433. "delete your device.map"), e);
  434. continue;
  435. }
  436. }
  437. drive_e = e;
  438. drive_p = p;
  439. map[drive].device_map = 1;
  440. p++;
  441. /* Skip leading spaces. */
  442. while (*p && grub_isspace (*p))
  443. p++;
  444. if (*p == '\0')
  445. grub_util_error ("%s:%d: %s", dev_map, lineno, _("filename expected"));
  446. /* NUL-terminate the filename. */
  447. e = p;
  448. while (*e && ! grub_isspace (*e))
  449. e++;
  450. *e = '\0';
  451. if (!grub_util_check_file_presence (p))
  452. {
  453. free (map[drive].drive);
  454. map[drive].drive = NULL;
  455. grub_util_info ("Cannot stat `%s', skipping", p);
  456. continue;
  457. }
  458. /* On Linux, the devfs uses symbolic links horribly, and that
  459. confuses the interface very much, so use realpath to expand
  460. symbolic links. */
  461. map[drive].device = grub_canonicalize_file_name (p);
  462. if (! map[drive].device)
  463. map[drive].device = xstrdup (p);
  464. if (!map[drive].drive)
  465. {
  466. char c;
  467. map[drive].drive = xmalloc (sizeof ("hostdisk/") + strlen (p));
  468. memcpy (map[drive].drive, "hostdisk/", sizeof ("hostdisk/") - 1);
  469. strcpy (map[drive].drive + sizeof ("hostdisk/") - 1, p);
  470. c = *drive_p;
  471. *drive_p = 0;
  472. /* TRANSLATORS: device.map is a filename. Not to be translated.
  473. device.map specifies disk correspondance overrides. Previously
  474. one could create any kind of device name with this. Due to
  475. some problems we decided to limit it to just a handful
  476. possibilities. */
  477. grub_util_warn (_("the drive name `%s' in device.map is incorrect. "
  478. "Using %s instead. "
  479. "Please use the form [hfc]d[0-9]* "
  480. "(E.g. `hd0' or `cd')"),
  481. drive_e, map[drive].drive);
  482. *drive_p = c;
  483. }
  484. grub_util_info ("adding `%s' -> `%s' from device.map", map[drive].drive,
  485. map[drive].device);
  486. grub_hostdisk_flush_initial_buffer (map[drive].device);
  487. }
  488. fclose (fp);
  489. }
  490. void
  491. grub_util_biosdisk_init (const char *dev_map)
  492. {
  493. read_device_map (dev_map);
  494. grub_disk_dev_register (&grub_util_biosdisk_dev);
  495. }
  496. void
  497. grub_util_biosdisk_fini (void)
  498. {
  499. unsigned i;
  500. for (i = 0; i < ARRAY_SIZE(map); i++)
  501. {
  502. if (map[i].drive)
  503. free (map[i].drive);
  504. if (map[i].device)
  505. free (map[i].device);
  506. map[i].drive = map[i].device = NULL;
  507. }
  508. grub_disk_dev_unregister (&grub_util_biosdisk_dev);
  509. }
  510. const char *
  511. grub_util_biosdisk_get_compatibility_hint (grub_disk_t disk)
  512. {
  513. if (disk->dev != &grub_util_biosdisk_dev || map[disk->id].device_map)
  514. return disk->name;
  515. return 0;
  516. }
  517. const char *
  518. grub_util_biosdisk_get_osdev (grub_disk_t disk)
  519. {
  520. if (disk->dev != &grub_util_biosdisk_dev)
  521. return 0;
  522. return map[disk->id].device;
  523. }
  524. static char *
  525. grub_util_path_concat_real (size_t n, int ext, va_list ap)
  526. {
  527. size_t totlen = 0;
  528. char **l = xcalloc (n + ext, sizeof (l[0]));
  529. char *r, *p, *pi;
  530. size_t i;
  531. int first = 1;
  532. for (i = 0; i < n + ext; i++)
  533. {
  534. l[i] = va_arg (ap, char *);
  535. if (l[i])
  536. totlen += strlen (l[i]) + 1;
  537. }
  538. r = xmalloc (totlen + 10);
  539. p = r;
  540. for (i = 0; i < n; i++)
  541. {
  542. pi = l[i];
  543. if (!pi)
  544. continue;
  545. while (*pi == '/')
  546. pi++;
  547. if ((p != r || (pi != l[i] && first)) && (p == r || *(p - 1) != '/'))
  548. *p++ = '/';
  549. first = 0;
  550. p = grub_stpcpy (p, pi);
  551. while (p != r && p != r + 1 && *(p - 1) == '/')
  552. p--;
  553. }
  554. if (ext && l[i])
  555. p = grub_stpcpy (p, l[i]);
  556. *p = '\0';
  557. free (l);
  558. return r;
  559. }
  560. char *
  561. grub_util_path_concat (size_t n, ...)
  562. {
  563. va_list ap;
  564. char *r;
  565. va_start (ap, n);
  566. r = grub_util_path_concat_real (n, 0, ap);
  567. va_end (ap);
  568. return r;
  569. }
  570. char *
  571. grub_util_path_concat_ext (size_t n, ...)
  572. {
  573. va_list ap;
  574. char *r;
  575. va_start (ap, n);
  576. r = grub_util_path_concat_real (n, 1, ap);
  577. va_end (ap);
  578. return r;
  579. }