hostdisk.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <sys/ioctl.h>
  42. # include <sys/disklabel.h> /* struct disklabel */
  43. # include <sys/disk.h> /* struct dkwedge_info */
  44. # ifdef HAVE_GETRAWPARTITION
  45. # include <util.h> /* getrawpartition */
  46. # endif /* HAVE_GETRAWPARTITION */
  47. # if defined(__NetBSD__)
  48. # include <sys/fdio.h>
  49. # endif
  50. # if defined(__OpenBSD__)
  51. # include <sys/dkio.h>
  52. # endif
  53. #if defined(__NetBSD__)
  54. /* Adjust device driver parameters. This function should be called just
  55. after successfully opening the device. For now, it simply prevents the
  56. floppy driver from retrying operations on failure, as otherwise the
  57. driver takes a while to abort when there is no floppy in the drive. */
  58. static void
  59. configure_device_driver (grub_util_fd_t fd)
  60. {
  61. struct stat st;
  62. if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
  63. return;
  64. if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
  65. {
  66. int floppy_opts;
  67. if (ioctl (fd, FDIOCGETOPTS, &floppy_opts) == -1)
  68. return;
  69. floppy_opts |= FDOPT_NORETRY;
  70. if (ioctl (fd, FDIOCSETOPTS, &floppy_opts) == -1)
  71. return;
  72. }
  73. }
  74. grub_util_fd_t
  75. grub_util_fd_open (const char *os_dev, int flags)
  76. {
  77. grub_util_fd_t ret;
  78. #ifdef O_LARGEFILE
  79. flags |= O_LARGEFILE;
  80. #endif
  81. #ifdef O_BINARY
  82. flags |= O_BINARY;
  83. #endif
  84. ret = open (os_dev, flags, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR);
  85. if (ret >= 0)
  86. configure_device_driver (fd);
  87. return ret;
  88. }
  89. #endif
  90. grub_int64_t
  91. grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
  92. {
  93. struct disklabel label;
  94. unsigned sector_size, log_sector_size;
  95. #if defined(__NetBSD__)
  96. grub_hostdisk_configure_device_driver (fd);
  97. #endif
  98. if (ioctl (fd, DIOCGDINFO, &label) == -1)
  99. return -1;
  100. sector_size = label.d_secsize;
  101. if (sector_size & (sector_size - 1) || !sector_size)
  102. return -1;
  103. for (log_sector_size = 0;
  104. (1 << log_sector_size) < sector_size;
  105. log_sector_size++);
  106. if (log_secsize)
  107. *log_secsize = log_sector_size;
  108. return (grub_uint64_t) label.d_secperunit << log_sector_size;
  109. }
  110. void
  111. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  112. {
  113. }