hostdisk.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/disk.h> /* DIOCGMEDIASIZE */
  42. # include <sys/param.h>
  43. # include <sys/sysctl.h>
  44. # include <sys/mount.h>
  45. # include <libgeom.h>
  46. grub_int64_t
  47. grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
  48. {
  49. unsigned long long nr;
  50. unsigned sector_size, log_sector_size;
  51. if (ioctl (fd, DIOCGMEDIASIZE, &nr))
  52. return -1;
  53. if (ioctl (fd, DIOCGSECTORSIZE, &sector_size))
  54. return -1;
  55. if (sector_size & (sector_size - 1) || !sector_size)
  56. return -1;
  57. for (log_sector_size = 0;
  58. (1 << log_sector_size) < sector_size;
  59. log_sector_size++);
  60. if (log_secsize)
  61. *log_secsize = log_sector_size;
  62. if (nr & (sector_size - 1))
  63. grub_util_error ("%s", _("unaligned device size"));
  64. return nr;
  65. }
  66. void
  67. grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
  68. {
  69. }
  70. grub_util_fd_t
  71. grub_util_fd_open (const char *os_dev, int flags)
  72. {
  73. grub_util_fd_t ret;
  74. int sysctl_flags, sysctl_oldflags;
  75. size_t sysctl_size = sizeof (sysctl_flags);
  76. #ifdef O_LARGEFILE
  77. flags |= O_LARGEFILE;
  78. #endif
  79. #ifdef O_BINARY
  80. flags |= O_BINARY;
  81. #endif
  82. if (sysctlbyname ("kern.geom.debugflags", &sysctl_oldflags, &sysctl_size, NULL, 0))
  83. {
  84. grub_error (GRUB_ERR_BAD_DEVICE, "cannot get current flags of sysctl kern.geom.debugflags");
  85. return GRUB_UTIL_FD_INVALID;
  86. }
  87. sysctl_flags = sysctl_oldflags | 0x10;
  88. if (! (sysctl_oldflags & 0x10)
  89. && sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_flags, sysctl_size))
  90. {
  91. if (errno == EPERM)
  92. /* Running as an unprivileged user; don't worry about restoring
  93. flags, although if we try to write to anything interesting such
  94. as the MBR then we may fail later. */
  95. sysctl_oldflags = 0x10;
  96. else
  97. {
  98. grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags of sysctl kern.geom.debugflags");
  99. return GRUB_UTIL_FD_INVALID;
  100. }
  101. }
  102. ret = open (os_dev, flags, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR);
  103. if (! (sysctl_oldflags & 0x10)
  104. && sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_oldflags, sysctl_size))
  105. {
  106. grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags back to the old value for sysctl kern.geom.debugflags");
  107. close (ret);
  108. return GRUB_UTIL_FD_INVALID;
  109. }
  110. return ret;
  111. }