~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/osdep/linux/hostdisk.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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,2011,2012,2013  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
 
 
20
#include <config-util.h>
 
21
 
 
22
#include <grub/disk.h>
 
23
#include <grub/partition.h>
 
24
#include <grub/msdos_partition.h>
 
25
#include <grub/types.h>
 
26
#include <grub/err.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>
 
34
 
 
35
#include <stdio.h>
 
36
#include <stdlib.h>
 
37
#include <string.h>
 
38
#include <ctype.h>
 
39
#include <assert.h>
 
40
#include <unistd.h>
 
41
#include <sys/types.h>
 
42
#include <sys/stat.h>
 
43
#include <sys/wait.h>
 
44
#include <fcntl.h>
 
45
#include <errno.h>
 
46
#include <limits.h>
 
47
 
 
48
# include <sys/ioctl.h>         /* ioctl */
 
49
# include <sys/mount.h>
 
50
# ifndef BLKFLSBUF
 
51
#  define BLKFLSBUF     _IO (0x12,97)   /* flush buffer cache */
 
52
# endif /* ! BLKFLSBUF */
 
53
# include <sys/ioctl.h>         /* ioctl */
 
54
# ifndef HDIO_GETGEO
 
55
#  define HDIO_GETGEO   0x0301  /* get device geometry */
 
56
/* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is
 
57
   defined.  */
 
58
struct hd_geometry
 
59
{
 
60
  unsigned char heads;
 
61
  unsigned char sectors;
 
62
  unsigned short cylinders;
 
63
  unsigned long start;
 
64
};
 
65
# endif /* ! HDIO_GETGEO */
 
66
# ifndef BLKGETSIZE64
 
67
#  define BLKGETSIZE64  _IOR(0x12,114,size_t)    /* return device size */
 
68
# endif /* ! BLKGETSIZE64 */
 
69
 
 
70
 
 
71
grub_int64_t
 
72
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
 
73
{
 
74
  unsigned long long nr;
 
75
  unsigned sector_size, log_sector_size;
 
76
 
 
77
  if (ioctl (fd, BLKGETSIZE64, &nr))
 
78
    return -1;
 
79
 
 
80
  if (ioctl (fd, BLKSSZGET, &sector_size))
 
81
    return -1;
 
82
 
 
83
  if (sector_size & (sector_size - 1) || !sector_size)
 
84
    return -1;
 
85
  for (log_sector_size = 0;
 
86
       (1 << log_sector_size) < sector_size;
 
87
       log_sector_size++);
 
88
 
 
89
  if (log_secsize)
 
90
    *log_secsize = log_sector_size;
 
91
 
 
92
  if (nr & ((1 << log_sector_size) - 1))
 
93
    grub_util_error ("%s", _("unaligned device size"));
 
94
 
 
95
  return nr;
 
96
}
 
97
 
 
98
static char *
 
99
sysfs_partition_path (const char *dev, const char *entry)
 
100
{
 
101
  const char *argv[7];
 
102
  int fd;
 
103
  pid_t pid;
 
104
  FILE *udevadm;
 
105
  char *buf = NULL;
 
106
  size_t len = 0;
 
107
  char *path = NULL;
 
108
 
 
109
  argv[0] = "udevadm";
 
110
  argv[1] = "info";
 
111
  argv[2] = "--query";
 
112
  argv[3] = "path";
 
113
  argv[4] = "--name";
 
114
  argv[5] = dev;
 
115
  argv[6] = NULL;
 
116
 
 
117
  pid = grub_util_exec_pipe (argv, &fd);
 
118
 
 
119
  if (!pid)
 
120
    return NULL;
 
121
 
 
122
  /* Parent.  Read udevadm's output.  */
 
123
  udevadm = fdopen (fd, "r");
 
124
  if (!udevadm)
 
125
    {
 
126
      grub_util_warn (_("Unable to open stream from %s: %s"),
 
127
                      "udevadm", strerror (errno));
 
128
      close (fd);
 
129
      goto out;
 
130
    }
 
131
 
 
132
  if (getline (&buf, &len, udevadm) > 0)
 
133
    {
 
134
      char *newline;
 
135
 
 
136
      newline = strchr (buf, '\n');
 
137
      if (newline)
 
138
        *newline = '\0';
 
139
      path = xasprintf ("/sys%s/%s", buf, entry);
 
140
    }
 
141
 
 
142
out:
 
143
  if (udevadm)
 
144
    fclose (udevadm);
 
145
  waitpid (pid, NULL, 0);
 
146
  free (buf);
 
147
 
 
148
  return path;
 
149
}
 
150
 
 
151
static int
 
152
sysfs_partition_start (const char *dev, grub_disk_addr_t *start)
 
153
{
 
154
  char *path;
 
155
  FILE *fp;
 
156
  unsigned long long val;
 
157
  int ret = 0;
 
158
 
 
159
  path = sysfs_partition_path (dev, "start");
 
160
  if (!path)
 
161
    return 0;
 
162
 
 
163
  fp = grub_util_fopen (path, "r");
 
164
  if (!fp)
 
165
    goto out;
 
166
 
 
167
  if (fscanf (fp, "%llu", &val) == 1)
 
168
    {
 
169
      *start = (grub_disk_addr_t) val;
 
170
      ret = 1;
 
171
    }
 
172
 
 
173
out:
 
174
  free (path);
 
175
  if (fp)
 
176
    fclose (fp);
 
177
 
 
178
  return ret;
 
179
}
 
180
 
 
181
grub_disk_addr_t
 
182
grub_util_find_partition_start_os (const char *dev)
 
183
{
 
184
  grub_disk_addr_t start = 0;
 
185
  grub_util_fd_t fd;
 
186
  struct hd_geometry hdg;
 
187
 
 
188
  if (sysfs_partition_start (dev, &start))
 
189
    return start;
 
190
 
 
191
  fd = open (dev, O_RDONLY);
 
192
  if (fd == -1)
 
193
    {
 
194
      grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
 
195
                  dev, strerror (errno));
 
196
      return 0;
 
197
    }
 
198
 
 
199
  if (ioctl (fd, HDIO_GETGEO, &hdg))
 
200
    {
 
201
      grub_error (GRUB_ERR_BAD_DEVICE,
 
202
                  "cannot get disk geometry of `%s'", dev);
 
203
      close (fd);
 
204
      return 0;
 
205
    }
 
206
 
 
207
  close (fd);
 
208
 
 
209
  return hdg.start;
 
210
}
 
211
 
 
212
/* Cache of partition start sectors for each disk.  */
 
213
struct linux_partition_cache
 
214
{
 
215
  struct linux_partition_cache *next;
 
216
  struct linux_partition_cache **prev;
 
217
  char *dev;
 
218
  unsigned long start;
 
219
  int partno;
 
220
};
 
221
 
 
222
struct linux_partition_cache *linux_partition_cache_list;
 
223
 
 
224
/* Check if we have devfs support.  */
 
225
static int
 
226
have_devfs (void)
 
227
{
 
228
  static int dev_devfsd_exists = -1;
 
229
 
 
230
  if (dev_devfsd_exists < 0)
 
231
    {
 
232
      struct stat st;
 
233
 
 
234
      dev_devfsd_exists = stat ("/dev/.devfsd", &st) == 0;
 
235
    }
 
236
 
 
237
  return dev_devfsd_exists;
 
238
}
 
239
 
 
240
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
 
241
 
 
242
static int
 
243
grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
 
244
{
 
245
  size_t len = strlen (dev);
 
246
  const char *format;
 
247
  char *p;
 
248
  int i;
 
249
  char real_dev[PATH_MAX];
 
250
  struct linux_partition_cache *cache;
 
251
  int missing = 0;
 
252
 
 
253
  strcpy(real_dev, dev);
 
254
 
 
255
  if (have_devfs () && strcmp (real_dev + len - 5, "/disc") == 0)
 
256
    {
 
257
      p = real_dev + len - 4;
 
258
      format = "part%d";
 
259
    }
 
260
  else if (strncmp (real_dev, "/dev/disk/by-id/",
 
261
                    sizeof ("/dev/disk/by-id/") - 1) == 0)
 
262
    {
 
263
      p = real_dev + len;
 
264
      format = "-part%d";
 
265
    }
 
266
  else if (real_dev[len - 1] >= '0' && real_dev[len - 1] <= '9')
 
267
    {
 
268
      p = real_dev + len;
 
269
      format = "p%d";
 
270
    }
 
271
  else
 
272
    {
 
273
      p = real_dev + len;
 
274
      format = "%d";
 
275
    }
 
276
 
 
277
  for (cache = linux_partition_cache_list; cache; cache = cache->next)
 
278
    {
 
279
      if (strcmp (cache->dev, dev) == 0 && cache->start == sector)
 
280
        {
 
281
          sprintf (p, format, cache->partno);
 
282
          strcpy (dev, real_dev);
 
283
          return 1;
 
284
        }
 
285
    }
 
286
 
 
287
  for (i = 1; i < 10000; i++)
 
288
    {
 
289
      grub_util_fd_t fd;
 
290
      grub_disk_addr_t start;
 
291
      struct stat st;
 
292
 
 
293
      sprintf (p, format, i);
 
294
 
 
295
      fd = open (real_dev, O_RDONLY);
 
296
      if (fd == -1)
 
297
        {
 
298
          if (missing++ < 10)
 
299
            continue;
 
300
          else
 
301
            return 0;
 
302
        }
 
303
      missing = 0;
 
304
 
 
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;
 
311
 
 
312
      close (fd);
 
313
 
 
314
      if (start == sector)
 
315
        {
 
316
          struct linux_partition_cache *new_cache_item;
 
317
 
 
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));
 
324
 
 
325
          strcpy (dev, real_dev);
 
326
          return 1;
 
327
        }
 
328
    }
 
329
 
 
330
  return 0;
 
331
}
 
332
 
 
333
#pragma GCC diagnostic error "-Wformat-nonliteral"
 
334
 
 
335
void
 
336
grub_hostdisk_flush_initial_buffer (const char *os_dev)
 
337
{
 
338
  grub_util_fd_t fd;
 
339
  struct stat st;
 
340
 
 
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);
 
344
  if (fd >= 0)
 
345
    close (fd);
 
346
}
 
347
 
 
348
int
 
349
grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
 
350
                          grub_disk_addr_t *max)
 
351
{
 
352
  grub_util_fd_t fd;
 
353
  struct grub_util_hostdisk_data *data = disk->data;
 
354
 
 
355
  *max = ~0ULL;
 
356
 
 
357
#ifdef O_LARGEFILE
 
358
  flags |= O_LARGEFILE;
 
359
#endif
 
360
#ifdef O_SYNC
 
361
  flags |= O_SYNC;
 
362
#endif
 
363
#ifdef O_FSYNC
 
364
  flags |= O_FSYNC;
 
365
#endif
 
366
#ifdef O_BINARY
 
367
  flags |= O_BINARY;
 
368
#endif
 
369
 
 
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.  */
 
372
  {
 
373
    int is_partition = 0;
 
374
    char dev[PATH_MAX];
 
375
    grub_disk_addr_t part_start = 0;
 
376
 
 
377
    part_start = grub_partition_get_start (disk->partition);
 
378
 
 
379
    strcpy (dev, grub_util_biosdisk_get_osdev (disk));
 
380
    if (disk->partition
 
381
        && strncmp (dev, "/dev/", 5) == 0)
 
382
      {
 
383
        if (sector >= part_start)
 
384
          is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
 
385
        else
 
386
          *max = part_start - sector;
 
387
      }
 
388
 
 
389
  reopen:
 
390
 
 
391
    if (data->dev && strcmp (data->dev, dev) == 0 &&
 
392
        data->access_mode == (flags & O_ACCMODE))
 
393
      {
 
394
        grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
 
395
        fd = data->fd;
 
396
      }
 
397
    else
 
398
      {
 
399
        free (data->dev);
 
400
        data->dev = 0;
 
401
        if (data->fd != -1)
 
402
          {
 
403
            if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
 
404
              {
 
405
                fsync (data->fd);
 
406
                if (data->is_disk)
 
407
                  ioctl (data->fd, BLKFLSBUF, 0);
 
408
              }
 
409
 
 
410
            close (data->fd);
 
411
            data->fd = -1;
 
412
          }
 
413
 
 
414
        /* Open the partition.  */
 
415
        grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
 
416
        fd = open (dev, flags);
 
417
        if (fd < 0)
 
418
          {
 
419
            grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
 
420
                        dev, strerror (errno));
 
421
            return -1;
 
422
          }
 
423
 
 
424
        data->dev = xstrdup (dev);
 
425
        data->access_mode = (flags & O_ACCMODE);
 
426
        data->fd = fd;
 
427
 
 
428
        if (data->is_disk)
 
429
          ioctl (data->fd, BLKFLSBUF, 0);
 
430
      }
 
431
 
 
432
    if (is_partition)
 
433
      {
 
434
        *max = grub_util_get_fd_size (fd, dev, 0);
 
435
        *max >>= disk->log_sector_size;
 
436
        if (sector - part_start >= *max)
 
437
          {
 
438
            *max = disk->partition->len - (sector - part_start);
 
439
            if (*max == 0)
 
440
              *max = ~0ULL;
 
441
            is_partition = 0;
 
442
            strcpy (dev, grub_util_biosdisk_get_osdev (disk));
 
443
            goto reopen;
 
444
          }
 
445
        sector -= part_start;
 
446
        *max -= sector;
 
447
      }
 
448
  }
 
449
 
 
450
  if (grub_util_fd_seek (fd, sector << disk->log_sector_size))
 
451
    {
 
452
      close (fd);
 
453
      grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
 
454
                  grub_util_biosdisk_get_osdev (disk), strerror (errno));
 
455
      return -1;
 
456
    }
 
457
 
 
458
  return fd;
 
459
}