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 | |
---|
19 | #include <config-util.h> |
---|
20 | |
---|
21 | #include <grub/disk.h> |
---|
22 | #include <grub/partition.h> |
---|
23 | #include <grub/msdos_partition.h> |
---|
24 | #include <grub/types.h> |
---|
25 | #include <grub/err.h> |
---|
26 | #include <grub/emu/misc.h> |
---|
27 | #include <grub/emu/hostdisk.h> |
---|
28 | #include <grub/emu/getroot.h> |
---|
29 | #include <grub/misc.h> |
---|
30 | #include <grub/i18n.h> |
---|
31 | #include <grub/list.h> |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <string.h> |
---|
36 | #include <ctype.h> |
---|
37 | #include <assert.h> |
---|
38 | #include <unistd.h> |
---|
39 | #include <sys/types.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <fcntl.h> |
---|
42 | #include <errno.h> |
---|
43 | #include <limits.h> |
---|
44 | |
---|
45 | # include <sys/disk.h> /* DIOCGMEDIASIZE */ |
---|
46 | # include <sys/param.h> |
---|
47 | # include <sys/sysctl.h> |
---|
48 | # include <sys/mount.h> |
---|
49 | # include <libgeom.h> |
---|
50 | |
---|
51 | grub_int64_t |
---|
52 | grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize) |
---|
53 | { |
---|
54 | unsigned long long nr; |
---|
55 | unsigned sector_size, log_sector_size; |
---|
56 | |
---|
57 | if (ioctl (fd, DIOCGMEDIASIZE, &nr)) |
---|
58 | return -1; |
---|
59 | |
---|
60 | if (ioctl (fd, DIOCGSECTORSIZE, §or_size)) |
---|
61 | return -1; |
---|
62 | if (sector_size & (sector_size - 1) || !sector_size) |
---|
63 | return -1; |
---|
64 | for (log_sector_size = 0; |
---|
65 | (1 << log_sector_size) < sector_size; |
---|
66 | log_sector_size++); |
---|
67 | |
---|
68 | if (log_secsize) |
---|
69 | *log_secsize = log_sector_size; |
---|
70 | |
---|
71 | if (nr & (sector_size - 1)) |
---|
72 | grub_util_error ("%s", _("unaligned device size")); |
---|
73 | |
---|
74 | return nr; |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused))) |
---|
79 | { |
---|
80 | } |
---|
81 | |
---|
82 | grub_util_fd_t |
---|
83 | grub_util_fd_open (const char *os_dev, int flags) |
---|
84 | { |
---|
85 | grub_util_fd_t ret; |
---|
86 | int sysctl_flags, sysctl_oldflags; |
---|
87 | size_t sysctl_size = sizeof (sysctl_flags); |
---|
88 | |
---|
89 | #ifdef O_LARGEFILE |
---|
90 | flags |= O_LARGEFILE; |
---|
91 | #endif |
---|
92 | #ifdef O_BINARY |
---|
93 | flags |= O_BINARY; |
---|
94 | #endif |
---|
95 | |
---|
96 | if (sysctlbyname ("kern.geom.debugflags", &sysctl_oldflags, &sysctl_size, NULL, 0)) |
---|
97 | { |
---|
98 | grub_error (GRUB_ERR_BAD_DEVICE, "cannot get current flags of sysctl kern.geom.debugflags"); |
---|
99 | return GRUB_UTIL_FD_INVALID; |
---|
100 | } |
---|
101 | sysctl_flags = sysctl_oldflags | 0x10; |
---|
102 | if (! (sysctl_oldflags & 0x10) |
---|
103 | && sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_flags, sysctl_size)) |
---|
104 | { |
---|
105 | grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags of sysctl kern.geom.debugflags"); |
---|
106 | return GRUB_UTIL_FD_INVALID; |
---|
107 | } |
---|
108 | |
---|
109 | ret = open (os_dev, flags, S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR); |
---|
110 | |
---|
111 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
---|
112 | if (! (sysctl_oldflags & 0x10) |
---|
113 | && sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_oldflags, sysctl_size)) |
---|
114 | { |
---|
115 | grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags back to the old value for sysctl kern.geom.debugflags"); |
---|
116 | close (ret); |
---|
117 | return GRUB_UTIL_FD_INVALID; |
---|
118 | } |
---|
119 | #endif |
---|
120 | |
---|
121 | return ret; |
---|
122 | } |
---|