1
/* hostdisk.c - emulate biosdisk */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
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.
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.
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/>.
20
#include <config-util.h>
22
#include <grub/disk.h>
23
#include <grub/partition.h>
24
#include <grub/msdos_partition.h>
25
#include <grub/types.h>
27
#include <grub/emu/misc.h>
28
#include <grub/emu/hostdisk.h>
29
#include <grub/emu/getroot.h>
30
#include <grub/emu/exec.h>
31
#include <grub/misc.h>
32
#include <grub/i18n.h>
33
#include <grub/list.h>
41
#include <sys/types.h>
48
# include <sys/ioctl.h> /* ioctl */
49
# include <sys/mount.h>
51
# define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */
52
# endif /* ! BLKFLSBUF */
53
# include <sys/ioctl.h> /* ioctl */
55
# define HDIO_GETGEO 0x0301 /* get device geometry */
56
/* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is
61
unsigned char sectors;
62
unsigned short cylinders;
65
# endif /* ! HDIO_GETGEO */
67
# define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size */
68
# endif /* ! BLKGETSIZE64 */
72
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
74
unsigned long long nr;
75
unsigned sector_size, log_sector_size;
77
if (ioctl (fd, BLKGETSIZE64, &nr))
80
if (ioctl (fd, BLKSSZGET, §or_size))
83
if (sector_size & (sector_size - 1) || !sector_size)
85
for (log_sector_size = 0;
86
(1 << log_sector_size) < sector_size;
90
*log_secsize = log_sector_size;
92
if (nr & ((1 << log_sector_size) - 1))
93
grub_util_error ("%s", _("unaligned device size"));
99
sysfs_partition_path (const char *dev, const char *entry)
117
pid = grub_util_exec_pipe (argv, &fd);
122
/* Parent. Read udevadm's output. */
123
udevadm = fdopen (fd, "r");
126
grub_util_warn (_("Unable to open stream from %s: %s"),
127
"udevadm", strerror (errno));
132
if (getline (&buf, &len, udevadm) > 0)
136
newline = strchr (buf, '\n');
139
path = xasprintf ("/sys%s/%s", buf, entry);
145
waitpid (pid, NULL, 0);
152
sysfs_partition_start (const char *dev, grub_disk_addr_t *start)
156
unsigned long long val;
159
path = sysfs_partition_path (dev, "start");
163
fp = grub_util_fopen (path, "r");
167
if (fscanf (fp, "%llu", &val) == 1)
169
*start = (grub_disk_addr_t) val;
182
grub_util_find_partition_start_os (const char *dev)
184
grub_disk_addr_t start = 0;
186
struct hd_geometry hdg;
188
if (sysfs_partition_start (dev, &start))
191
fd = open (dev, O_RDONLY);
194
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
195
dev, strerror (errno));
199
if (ioctl (fd, HDIO_GETGEO, &hdg))
201
grub_error (GRUB_ERR_BAD_DEVICE,
202
"cannot get disk geometry of `%s'", dev);
212
/* Cache of partition start sectors for each disk. */
213
struct linux_partition_cache
215
struct linux_partition_cache *next;
216
struct linux_partition_cache **prev;
222
struct linux_partition_cache *linux_partition_cache_list;
224
/* Check if we have devfs support. */
228
static int dev_devfsd_exists = -1;
230
if (dev_devfsd_exists < 0)
234
dev_devfsd_exists = stat ("/dev/.devfsd", &st) == 0;
237
return dev_devfsd_exists;
240
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
243
grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
245
size_t len = strlen (dev);
249
char real_dev[PATH_MAX];
250
struct linux_partition_cache *cache;
253
strcpy(real_dev, dev);
255
if (have_devfs () && strcmp (real_dev + len - 5, "/disc") == 0)
257
p = real_dev + len - 4;
260
else if (strncmp (real_dev, "/dev/disk/by-id/",
261
sizeof ("/dev/disk/by-id/") - 1) == 0)
266
else if (real_dev[len - 1] >= '0' && real_dev[len - 1] <= '9')
277
for (cache = linux_partition_cache_list; cache; cache = cache->next)
279
if (strcmp (cache->dev, dev) == 0 && cache->start == sector)
281
sprintf (p, format, cache->partno);
282
strcpy (dev, real_dev);
287
for (i = 1; i < 10000; i++)
290
grub_disk_addr_t start;
293
sprintf (p, format, i);
295
fd = open (real_dev, O_RDONLY);
305
if (fstat (fd, &st) < 0
306
|| !grub_util_device_is_mapped_stat (&st)
307
|| !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &start))
308
start = grub_util_find_partition_start_os (real_dev);
309
/* We don't care about errors here. */
310
grub_errno = GRUB_ERR_NONE;
316
struct linux_partition_cache *new_cache_item;
318
new_cache_item = xmalloc (sizeof *new_cache_item);
319
new_cache_item->dev = xstrdup (dev);
320
new_cache_item->start = start;
321
new_cache_item->partno = i;
322
grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list),
323
GRUB_AS_LIST (new_cache_item));
325
strcpy (dev, real_dev);
333
#pragma GCC diagnostic error "-Wformat-nonliteral"
336
grub_hostdisk_flush_initial_buffer (const char *os_dev)
341
fd = open (os_dev, O_RDONLY);
342
if (fd >= 0 && fstat (fd, &st) >= 0 && S_ISBLK (st.st_mode))
343
ioctl (fd, BLKFLSBUF, 0);
349
grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
350
grub_disk_addr_t *max)
353
struct grub_util_hostdisk_data *data = disk->data;
358
flags |= O_LARGEFILE;
370
/* Linux has a bug that the disk cache for a whole disk is not consistent
371
with the one for a partition of the disk. */
373
int is_partition = 0;
375
grub_disk_addr_t part_start = 0;
377
part_start = grub_partition_get_start (disk->partition);
379
strcpy (dev, grub_util_biosdisk_get_osdev (disk));
381
&& strncmp (dev, "/dev/", 5) == 0)
383
if (sector >= part_start)
384
is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
386
*max = part_start - sector;
391
if (data->dev && strcmp (data->dev, dev) == 0 &&
392
data->access_mode == (flags & O_ACCMODE))
394
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
403
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
407
ioctl (data->fd, BLKFLSBUF, 0);
414
/* Open the partition. */
415
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
416
fd = open (dev, flags);
419
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
420
dev, strerror (errno));
424
data->dev = xstrdup (dev);
425
data->access_mode = (flags & O_ACCMODE);
429
ioctl (data->fd, BLKFLSBUF, 0);
434
*max = grub_util_get_fd_size (fd, dev, 0);
435
*max >>= disk->log_sector_size;
436
if (sector - part_start >= *max)
438
*max = disk->partition->len - (sector - part_start);
442
strcpy (dev, grub_util_biosdisk_get_osdev (disk));
445
sector -= part_start;
450
if (grub_util_fd_seek (fd, sector << disk->log_sector_size))
453
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
454
grub_util_biosdisk_get_osdev (disk), strerror (errno));