disk.c 13.7 KB
Newer Older
okuji's avatar
okuji committed
1
/*
2
 *  GRUB  --  GRand Unified Bootloader
3
 *  Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010  Free Software Foundation, Inc.
okuji's avatar
okuji committed
4
 *
5
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
8 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
11 12 13 14 15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
17 18
 */

19 20 21 22
#include <grub/disk.h>
#include <grub/err.h>
#include <grub/mm.h>
#include <grub/types.h>
23
#include <grub/partition.h>
24
#include <grub/misc.h>
25
#include <grub/time.h>
26
#include <grub/file.h>
27
#include <grub/i18n.h>
28

29
#define	GRUB_CACHE_TIMEOUT	2
30 31

/* The last time the disk was used.  */
32
static grub_uint64_t grub_last_time = 0;
33

34
struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
okuji's avatar
okuji committed
35

36 37 38
void (*grub_disk_firmware_fini) (void);
int grub_disk_firmware_is_tainted;

39
#if DISK_CACHE_STATS
40 41
static unsigned long grub_disk_cache_hits;
static unsigned long grub_disk_cache_misses;
okuji's avatar
okuji committed
42 43

void
44
grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
okuji's avatar
okuji committed
45
{
46 47
  *hits = grub_disk_cache_hits;
  *misses = grub_disk_cache_misses;
okuji's avatar
okuji committed
48 49 50
}
#endif

51 52 53 54 55 56
grub_err_t (*grub_disk_write_weak) (grub_disk_t disk,
				    grub_disk_addr_t sector,
				    grub_off_t offset,
				    grub_size_t size,
				    const void *buf);
#include "disk_common.c"
okuji's avatar
okuji committed
57 58

void
59
grub_disk_cache_invalidate_all (void)
okuji's avatar
okuji committed
60 61 62
{
  unsigned i;

63
  for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
okuji's avatar
okuji committed
64
    {
65
      struct grub_disk_cache *cache = grub_disk_cache_table + i;
okuji's avatar
okuji committed
66 67 68

      if (cache->data && ! cache->lock)
	{
69
	  grub_free (cache->data);
okuji's avatar
okuji committed
70 71 72 73 74 75
	  cache->data = 0;
	}
    }
}

static char *
76
grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
77
		       grub_disk_addr_t sector)
okuji's avatar
okuji committed
78
{
79
  struct grub_disk_cache *cache;
80
  unsigned cache_index;
okuji's avatar
okuji committed
81

82 83
  cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
  cache = grub_disk_cache_table + cache_index;
okuji's avatar
okuji committed
84

85 86
  if (cache->dev_id == dev_id && cache->disk_id == disk_id
      && cache->sector == sector)
okuji's avatar
okuji committed
87 88
    {
      cache->lock = 1;
89
#if DISK_CACHE_STATS
90
      grub_disk_cache_hits++;
okuji's avatar
okuji committed
91 92 93 94
#endif
      return cache->data;
    }

95
#if DISK_CACHE_STATS
96
  grub_disk_cache_misses++;
okuji's avatar
okuji committed
97
#endif
98

okuji's avatar
okuji committed
99 100 101 102
  return 0;
}

static void
103
grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
104
			grub_disk_addr_t sector)
okuji's avatar
okuji committed
105
{
106
  struct grub_disk_cache *cache;
107
  unsigned cache_index;
okuji's avatar
okuji committed
108

109 110
  cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
  cache = grub_disk_cache_table + cache_index;
okuji's avatar
okuji committed
111

112 113
  if (cache->dev_id == dev_id && cache->disk_id == disk_id
      && cache->sector == sector)
okuji's avatar
okuji committed
114 115 116
    cache->lock = 0;
}

117
static grub_err_t
118
grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
119
		       grub_disk_addr_t sector, const char *data)
okuji's avatar
okuji committed
120
{
121
  unsigned cache_index;
122
  struct grub_disk_cache *cache;
123

124 125
  cache_index = grub_disk_cache_get_index (dev_id, disk_id, sector);
  cache = grub_disk_cache_table + cache_index;
126

127 128 129 130
  cache->lock = 1;
  grub_free (cache->data);
  cache->data = 0;
  cache->lock = 0;
131

132
  cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
okuji's avatar
okuji committed
133
  if (! cache->data)
134
    return grub_errno;
135

136 137
  grub_memcpy (cache->data, data,
	       GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
138 139
  cache->dev_id = dev_id;
  cache->disk_id = disk_id;
okuji's avatar
okuji committed
140 141
  cache->sector = sector;

142
  return GRUB_ERR_NONE;
okuji's avatar
okuji committed
143 144 145 146
}



147
grub_disk_dev_t grub_disk_dev_list;
okuji's avatar
okuji committed
148 149

void
150
grub_disk_dev_register (grub_disk_dev_t dev)
okuji's avatar
okuji committed
151
{
152 153
  dev->next = grub_disk_dev_list;
  grub_disk_dev_list = dev;
okuji's avatar
okuji committed
154 155 156
}

void
157
grub_disk_dev_unregister (grub_disk_dev_t dev)
okuji's avatar
okuji committed
158
{
159
  grub_disk_dev_t *p, q;
160

161
  for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
okuji's avatar
okuji committed
162 163 164 165 166 167 168
    if (q == dev)
      {
        *p = q->next;
	break;
      }
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/* Return the location of the first ',', if any, which is not
   escaped by a '\'.  */
static const char *
find_part_sep (const char *name)
{
  const char *p = name;
  char c;

  while ((c = *p++) != '\0')
    {
      if (c == '\\' && *p == ',')
	p++;
      else if (c == ',')
	return p - 1;
    }
  return NULL;
}

187 188
grub_disk_t
grub_disk_open (const char *name)
okuji's avatar
okuji committed
189
{
190
  const char *p;
191 192
  grub_disk_t disk;
  grub_disk_dev_t dev;
okuji's avatar
okuji committed
193
  char *raw = (char *) name;
194
  grub_uint64_t current_time;
195 196 197

  grub_dprintf ("disk", "Opening `%s'...\n", name);

198
  disk = (grub_disk_t) grub_zalloc (sizeof (*disk));
okuji's avatar
okuji committed
199 200
  if (! disk)
    return 0;
201
  disk->log_sector_size = GRUB_DISK_SECTOR_BITS;
202 203 204
  /* Default 1MiB of maximum agglomerate.  */
  disk->max_agglomerate = 1048576 >> (GRUB_DISK_SECTOR_BITS
				      + GRUB_DISK_CACHE_BITS);
okuji's avatar
okuji committed
205

206
  p = find_part_sep (name);
okuji's avatar
okuji committed
207 208
  if (p)
    {
209
      grub_size_t len = p - name;
210

211
      raw = grub_malloc (len + 1);
okuji's avatar
okuji committed
212 213 214
      if (! raw)
	goto fail;

215
      grub_memcpy (raw, name, len);
okuji's avatar
okuji committed
216
      raw[len] = '\0';
217
      disk->name = grub_strdup (raw);
okuji's avatar
okuji committed
218
    }
219 220 221 222 223
  else
    disk->name = grub_strdup (name);
  if (! disk->name)
    goto fail;

224
  for (dev = grub_disk_dev_list; dev; dev = dev->next)
okuji's avatar
okuji committed
225
    {
226
      if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
okuji's avatar
okuji committed
227
	break;
228 229
      else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
	grub_errno = GRUB_ERR_NONE;
okuji's avatar
okuji committed
230 231 232 233 234 235
      else
	goto fail;
    }

  if (! dev)
    {
236 237
      grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
		  name);
okuji's avatar
okuji committed
238 239
      goto fail;
    }
240 241 242 243 244 245 246 247
  if (disk->log_sector_size > GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS
      || disk->log_sector_size < GRUB_DISK_SECTOR_BITS)
    {
      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
		  "sector sizes of %d bytes aren't supported yet",
		  (1 << disk->log_sector_size));
      goto fail;
    }
okuji's avatar
okuji committed
248 249 250 251

  disk->dev = dev;

  if (p)
252 253 254 255
    {
      disk->partition = grub_partition_probe (disk, p + 1);
      if (! disk->partition)
	{
256 257
	  /* TRANSLATORS: It means that the specified partition e.g.
	     hd0,msdos1=/dev/sda1 doesn't exist.  */
258
	  grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("no such partition"));
259 260 261
	  goto fail;
	}
    }
okuji's avatar
okuji committed
262

263 264
  /* The cache will be invalidated about 2 seconds after a device was
     closed.  */
265
  current_time = grub_get_time_ms ();
266

267
  if (current_time > (grub_last_time
268
		      + GRUB_CACHE_TIMEOUT * 1000))
269
    grub_disk_cache_invalidate_all ();
270

271
  grub_last_time = current_time;
272

okuji's avatar
okuji committed
273
 fail:
274

okuji's avatar
okuji committed
275
  if (raw && raw != name)
276
    grub_free (raw);
okuji's avatar
okuji committed
277

278
  if (grub_errno != GRUB_ERR_NONE)
okuji's avatar
okuji committed
279
    {
280 281 282 283
      grub_error_push ();
      grub_dprintf ("disk", "Opening `%s' failed.\n", name);
      grub_error_pop ();

284
      grub_disk_close (disk);
okuji's avatar
okuji committed
285 286 287 288 289 290 291
      return 0;
    }

  return disk;
}

void
292
grub_disk_close (grub_disk_t disk)
okuji's avatar
okuji committed
293
{
294
  grub_partition_t part;
295 296
  grub_dprintf ("disk", "Closing `%s'.\n", disk->name);

okuji's avatar
okuji committed
297 298 299
  if (disk->dev && disk->dev->close)
    (disk->dev->close) (disk);

300
  /* Reset the timer.  */
301
  grub_last_time = grub_get_time_ms ();
302

303 304 305 306 307 308
  while (disk->partition)
    {
      part = disk->partition->parent;
      grub_free (disk->partition);
      disk->partition = part;
    }
309 310
  grub_free ((void *) disk->name);
  grub_free (disk);
okuji's avatar
okuji committed
311 312
}

313 314 315 316
/* Small read (less than cache size and not pass across cache unit boundaries).
   sector is already adjusted and is divisible by cache unit size.
 */
static grub_err_t
317 318
grub_disk_read_small_real (grub_disk_t disk, grub_disk_addr_t sector,
			   grub_off_t offset, grub_size_t size, void *buf)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
{
  char *data;
  char *tmp_buf;

  /* Fetch the cache.  */
  data = grub_disk_cache_fetch (disk->dev->id, disk->id, sector);
  if (data)
    {
      /* Just copy it!  */
      grub_memcpy (buf, data + offset, size);
      grub_disk_cache_unlock (disk->dev->id, disk->id, sector);
      return GRUB_ERR_NONE;
    }

  /* Allocate a temporary buffer.  */
  tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
  if (! tmp_buf)
    return grub_errno;

  /* Otherwise read data from the disk actually.  */
  if (disk->total_sectors == GRUB_DISK_SIZE_UNKNOWN
      || sector + GRUB_DISK_CACHE_SIZE
      < (disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS)))
    {
      grub_err_t err;
      err = (disk->dev->read) (disk, transform_sector (disk, sector),
345 346 347
			       1U << (GRUB_DISK_CACHE_BITS
				      + GRUB_DISK_SECTOR_BITS
				      - disk->log_sector_size), tmp_buf);
348 349 350 351 352 353 354 355 356 357 358
      if (!err)
	{
	  /* Copy it and store it in the disk cache.  */
	  grub_memcpy (buf, tmp_buf + offset, size);
	  grub_disk_cache_store (disk->dev->id, disk->id,
				 sector, tmp_buf);
	  grub_free (tmp_buf);
	  return GRUB_ERR_NONE;
	}
    }

359
  grub_free (tmp_buf);
360 361 362 363 364 365 366 367 368
  grub_errno = GRUB_ERR_NONE;

  {
    /* Uggh... Failed. Instead, just read necessary data.  */
    unsigned num;
    grub_disk_addr_t aligned_sector;

    sector += (offset >> GRUB_DISK_SECTOR_BITS);
    offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
369 370
    aligned_sector = (sector & ~((1ULL << (disk->log_sector_size
					   - GRUB_DISK_SECTOR_BITS))
371 372
				 - 1));
    offset += ((sector - aligned_sector) << GRUB_DISK_SECTOR_BITS);
373
    num = ((size + offset + (1ULL << (disk->log_sector_size))
374 375 376 377 378 379 380 381 382 383 384 385
	    - 1) >> (disk->log_sector_size));

    tmp_buf = grub_malloc (num << disk->log_sector_size);
    if (!tmp_buf)
      return grub_errno;
    
    if ((disk->dev->read) (disk, transform_sector (disk, aligned_sector),
			   num, tmp_buf))
      {
	grub_error_push ();
	grub_dprintf ("disk", "%s read failed\n", disk->name);
	grub_error_pop ();
386
	grub_free (tmp_buf);
387 388 389
	return grub_errno;
      }
    grub_memcpy (buf, tmp_buf + offset, size);
390
    grub_free (tmp_buf);
391 392 393 394
    return GRUB_ERR_NONE;
  }
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static grub_err_t
grub_disk_read_small (grub_disk_t disk, grub_disk_addr_t sector,
		      grub_off_t offset, grub_size_t size, void *buf)
{
  grub_err_t err;

  err = grub_disk_read_small_real (disk, sector, offset, size, buf);
  if (err)
    return err;
  if (disk->read_hook)
    (disk->read_hook) (sector + (offset >> GRUB_DISK_SECTOR_BITS),
		       offset & (GRUB_DISK_SECTOR_SIZE - 1),
		       size, disk->read_hook_data);
  return GRUB_ERR_NONE;
}

okuji's avatar
okuji committed
411
/* Read data from the disk.  */
412
grub_err_t
413
grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
414
		grub_off_t offset, grub_size_t size, void *buf)
okuji's avatar
okuji committed
415 416
{
  /* First of all, check if the region is within the disk.  */
417
  if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
418 419
    {
      grub_error_push ();
420 421
      grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
		    (unsigned long long) sector, grub_errmsg);
422 423 424
      grub_error_pop ();
      return grub_errno;
    }
okuji's avatar
okuji committed
425

426 427
  /* First read until first cache boundary.   */
  if (offset || (sector & (GRUB_DISK_CACHE_SIZE - 1)))
okuji's avatar
okuji committed
428
    {
429 430
      grub_disk_addr_t start_sector;
      grub_size_t pos;
431 432
      grub_err_t err;
      grub_size_t len;
okuji's avatar
okuji committed
433

434
      start_sector = sector & ~((grub_disk_addr_t) GRUB_DISK_CACHE_SIZE - 1);
435
      pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
436
      len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS)
437
	     - pos - offset);
okuji's avatar
okuji committed
438 439
      if (len > size)
	len = size;
440 441 442 443 444 445 446 447 448 449
      err = grub_disk_read_small (disk, start_sector,
				  offset + pos, len, buf);
      if (err)
	return err;
      buf = (char *) buf + len;
      size -= len;
      offset += len;
      sector += (offset >> GRUB_DISK_SECTOR_BITS);
      offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
    }
okuji's avatar
okuji committed
450

451 452 453 454 455 456 457 458 459
  /* Until SIZE is zero...  */
  while (size >= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS))
    {
      char *data = NULL;
      grub_disk_addr_t agglomerate;
      grub_err_t err;

      /* agglomerate read until we find a first cached entry.  */
      for (agglomerate = 0; agglomerate
460 461
	     < (size >> (GRUB_DISK_SECTOR_BITS + GRUB_DISK_CACHE_BITS))
	     && agglomerate < disk->max_agglomerate;
462
	   agglomerate++)
okuji's avatar
okuji committed
463
	{
464 465 466 467 468
	  data = grub_disk_cache_fetch (disk->dev->id, disk->id,
					sector + (agglomerate
						  << GRUB_DISK_CACHE_BITS));
	  if (data)
	    break;
okuji's avatar
okuji committed
469 470
	}

471 472 473 474 475 476 477 478 479
      if (data)
	{
	  grub_memcpy ((char *) buf
		       + (agglomerate << (GRUB_DISK_CACHE_BITS
					  + GRUB_DISK_SECTOR_BITS)),
		       data, GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
	  grub_disk_cache_unlock (disk->dev->id, disk->id,
				  sector + (agglomerate
					    << GRUB_DISK_CACHE_BITS));
okuji's avatar
okuji committed
480 481
	}

482
      if (agglomerate)
okuji's avatar
okuji committed
483
	{
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	  grub_disk_addr_t i;

	  err = (disk->dev->read) (disk, transform_sector (disk, sector),
				   agglomerate << (GRUB_DISK_CACHE_BITS
						   + GRUB_DISK_SECTOR_BITS
						   - disk->log_sector_size),
				   buf);
	  if (err)
	    return err;
	  
	  for (i = 0; i < agglomerate; i ++)
	    grub_disk_cache_store (disk->dev->id, disk->id,
				   sector + (i << GRUB_DISK_CACHE_BITS),
				   (char *) buf
				   + (i << (GRUB_DISK_CACHE_BITS
					    + GRUB_DISK_SECTOR_BITS)));

501 502 503 504 505

	  if (disk->read_hook)
	    (disk->read_hook) (sector, 0, agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS),
			       disk->read_hook_data);

506 507 508 509
	  sector += agglomerate << GRUB_DISK_CACHE_BITS;
	  size -= agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS);
	  buf = (char *) buf 
	    + (agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS));
okuji's avatar
okuji committed
510
	}
511

512
      if (data)
okuji's avatar
okuji committed
513
	{
514 515 516
	  if (disk->read_hook)
	    (disk->read_hook) (sector, 0, (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS),
			       disk->read_hook_data);
517 518 519
	  sector += GRUB_DISK_CACHE_SIZE;
	  buf = (char *) buf + (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
	  size -= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
okuji's avatar
okuji committed
520
	}
521
    }
522

523 524 525 526 527 528 529
  /* And now read the last part.  */
  if (size)
    {
      grub_err_t err;
      err = grub_disk_read_small (disk, sector, 0, size, buf);
      if (err)
	return err;
okuji's avatar
okuji committed
530
    }
531

532
  return grub_errno;
okuji's avatar
okuji committed
533 534
}

535
grub_uint64_t
536 537 538 539
grub_disk_get_size (grub_disk_t disk)
{
  if (disk->partition)
    return grub_partition_get_len (disk->partition);
540 541
  else if (disk->total_sectors != GRUB_DISK_SIZE_UNKNOWN)
    return disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
542
  else
543
    return GRUB_DISK_SIZE_UNKNOWN;
544
}