2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2002,2003,2004 Free Software Foundation, Inc.
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 2 of the License, or
8
* (at your option) any later version.
10
* This program 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.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include <grub/disk.h>
23
#include <grub/types.h>
24
#include <grub/partition.h>
25
#include <grub/misc.h>
26
#include <grub/machine/time.h>
27
#include <grub/file.h>
29
#define GRUB_CACHE_TIMEOUT 2
31
/* The last time the disk was used. */
32
static unsigned long grub_last_time = 0;
36
struct grub_disk_cache
39
unsigned long disk_id;
45
static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
48
static unsigned long grub_disk_cache_hits;
49
static unsigned long grub_disk_cache_misses;
52
grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
54
*hits = grub_disk_cache_hits;
55
*misses = grub_disk_cache_misses;
60
grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
63
return ((dev_id * 524287UL + disk_id * 2606459UL
64
+ (sector >> GRUB_DISK_CACHE_BITS))
65
% GRUB_DISK_CACHE_NUM);
69
grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
73
struct grub_disk_cache *cache;
75
sector &= ~(GRUB_DISK_CACHE_SIZE - 1);
76
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
77
cache = grub_disk_cache_table + index;
79
if (cache->dev_id == dev_id && cache->disk_id == disk_id
80
&& cache->sector == sector && cache->data)
83
grub_free (cache->data);
90
grub_disk_cache_invalidate_all (void)
94
for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
96
struct grub_disk_cache *cache = grub_disk_cache_table + i;
98
if (cache->data && ! cache->lock)
100
grub_free (cache->data);
107
grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
108
unsigned long sector)
110
struct grub_disk_cache *cache;
113
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
114
cache = grub_disk_cache_table + index;
116
if (cache->dev_id == dev_id && cache->disk_id == disk_id
117
&& cache->sector == sector)
121
grub_disk_cache_hits++;
127
grub_disk_cache_misses++;
134
grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
135
unsigned long sector)
137
struct grub_disk_cache *cache;
140
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
141
cache = grub_disk_cache_table + index;
143
if (cache->dev_id == dev_id && cache->disk_id == disk_id
144
&& cache->sector == sector)
149
grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
150
unsigned long sector, const char *data)
153
struct grub_disk_cache *cache;
155
grub_disk_cache_invalidate (dev_id, disk_id, sector);
157
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
158
cache = grub_disk_cache_table + index;
160
cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
164
grub_memcpy (cache->data, data,
165
GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
166
cache->dev_id = dev_id;
167
cache->disk_id = disk_id;
168
cache->sector = sector;
170
return GRUB_ERR_NONE;
175
static grub_disk_dev_t grub_disk_dev_list;
178
grub_disk_dev_register (grub_disk_dev_t dev)
180
dev->next = grub_disk_dev_list;
181
grub_disk_dev_list = dev;
185
grub_disk_dev_unregister (grub_disk_dev_t dev)
187
grub_disk_dev_t *p, q;
189
for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
198
grub_disk_dev_iterate (int (*hook) (const char *name))
202
for (p = grub_disk_dev_list; p; p = p->next)
203
if ((p->iterate) (hook))
210
grub_disk_open (const char *name)
215
char *raw = (char *) name;
216
unsigned long current_time;
218
disk = (grub_disk_t) grub_malloc (sizeof (*disk));
226
disk->name = grub_strdup (name);
230
p = grub_strchr (name, ',');
233
grub_size_t len = p - name;
235
raw = grub_malloc (len + 1);
239
grub_memcpy (raw, name, len);
243
for (dev = grub_disk_dev_list; dev; dev = dev->next)
245
if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
247
else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
248
grub_errno = GRUB_ERR_NONE;
255
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
259
if (p && ! disk->has_partitions)
261
grub_error (GRUB_ERR_BAD_DEVICE, "no partition on this disk");
268
disk->partition = grub_partition_probe (disk, p + 1);
270
/* The cache will be invalidated about 2 seconds after a device was
272
current_time = grub_get_rtc ();
274
if (current_time > (grub_last_time
275
+ GRUB_CACHE_TIMEOUT * GRUB_TICKS_PER_SECOND))
276
grub_disk_cache_invalidate_all ();
278
grub_last_time = current_time;
282
if (raw && raw != name)
285
if (grub_errno != GRUB_ERR_NONE)
287
grub_disk_close (disk);
295
grub_disk_close (grub_disk_t disk)
297
if (disk->dev && disk->dev->close)
298
(disk->dev->close) (disk);
300
/* Reset the timer. */
301
grub_last_time = grub_get_rtc ();
303
grub_free (disk->partition);
304
grub_free ((void *) disk->name);
309
grub_disk_check_range (grub_disk_t disk, unsigned long *sector,
310
unsigned long *offset, grub_ssize_t size)
312
*sector += *offset >> GRUB_DISK_SECTOR_BITS;
313
*offset &= GRUB_DISK_SECTOR_SIZE - 1;
317
unsigned long start, len;
319
start = grub_partition_get_start (disk->partition);
320
len = grub_partition_get_len (disk->partition);
323
|| len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
324
>> GRUB_DISK_SECTOR_BITS))
325
return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition");
330
if (disk->total_sectors <= *sector
331
|| ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
332
>> GRUB_DISK_SECTOR_BITS) > disk->total_sectors - *sector)
333
return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
335
return GRUB_ERR_NONE;
338
/* Read data from the disk. */
340
grub_disk_read (grub_disk_t disk, unsigned long sector,
341
unsigned long offset, unsigned long size, char *buf)
345
/* First of all, check if the region is within the disk. */
346
if (grub_disk_check_range (disk, §or, &offset, size) != GRUB_ERR_NONE)
349
/* Allocate a temporary buffer. */
350
tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
354
/* Until SIZE is zero... */
358
unsigned long start_sector;
362
/* For reading bulk data. */
363
start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1);
364
pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
365
len = (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS) - pos - offset;
369
/* Fetch the cache. */
370
data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector);
374
grub_memcpy (buf, data + pos + offset, len);
375
grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector);
379
/* Otherwise read data from the disk actually. */
380
if ((disk->dev->read) (disk, start_sector,
381
GRUB_DISK_CACHE_SIZE, tmp_buf)
384
/* Uggh... Failed. Instead, just read necessary data. */
387
grub_errno = GRUB_ERR_NONE;
389
/* If more data is required, no way. */
391
>= (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS))
394
num = ((size + GRUB_DISK_SECTOR_SIZE - 1)
395
>> GRUB_DISK_SECTOR_BITS);
396
if ((disk->dev->read) (disk, sector, num, tmp_buf))
399
grub_memcpy (buf, tmp_buf + offset, size);
401
/* Call the read hook, if any. */
405
(disk->read_hook) (sector, offset,
406
((size > GRUB_DISK_SECTOR_SIZE)
407
? GRUB_DISK_SECTOR_SIZE
410
size -= GRUB_DISK_SECTOR_SIZE - offset;
414
/* This must be the end. */
418
/* Copy it and store it in the disk cache. */
419
grub_memcpy (buf, tmp_buf + pos + offset, len);
420
grub_disk_cache_store (disk->dev->id, disk->id,
421
start_sector, tmp_buf);
424
/* Call the read hook, if any. */
427
unsigned long s = sector;
428
unsigned long l = len;
432
(disk->read_hook) (s, offset,
433
((l > GRUB_DISK_SECTOR_SIZE)
434
? GRUB_DISK_SECTOR_SIZE
437
if (l < GRUB_DISK_SECTOR_SIZE - offset)
441
l -= GRUB_DISK_SECTOR_SIZE - offset;
446
sector = start_sector + GRUB_DISK_CACHE_SIZE;
460
grub_disk_write (grub_disk_t disk, unsigned long sector,
461
unsigned long offset, unsigned long size, const char *buf)
463
if (grub_disk_check_range (disk, §or, &offset, size) != GRUB_ERR_NONE)
468
if (offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
470
char tmp_buf[GRUB_DISK_SECTOR_SIZE];
473
if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
477
len = GRUB_DISK_SECTOR_SIZE - offset;
481
grub_memcpy (tmp_buf + offset, buf, len);
483
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
485
if ((disk->dev->write) (disk, sector, 1, tmp_buf) != GRUB_ERR_NONE)
498
len = size & ~(GRUB_DISK_SECTOR_SIZE - 1);
499
n = size >> GRUB_DISK_SECTOR_BITS;
501
if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE)
505
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);