grub/kern/disk.c

516 lines
12 KiB
C
Raw Normal View History

2002-12-27 08:53:07 +00:00
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2004 Free Software Foundation, Inc.
2002-12-27 08:53:07 +00:00
*
* GRUB is free software; you can redistribute it and/or modify
2002-12-27 08:53:07 +00:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* 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
* along with GRUB; if not, write to the Free Software
2002-12-27 08:53:07 +00:00
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <grub/disk.h>
#include <grub/err.h>
#include <grub/mm.h>
#include <grub/types.h>
2004-12-04 Marco Gerards <metgerards@student.han.nl> Modulize the partition map support and add support for the amiga partition map. * commands/ls.c: Include <grub/partition.h> instead of <grub/machine/partition.h>. * kern/disk.c: Likewise. * kern/rescue.c: Likewise. * loader/i386/pc/chainloader.c: Likewise. * normal/cmdline.c: Likewise. * kern/powerpc/ieee1275/init.c: Likewise. (grub_machine_init): Call `grub_pc_partition_map_init', `grub_amiga_partition_map_init' and `grub_apple_partition_map_init'. * conf/i386-pc.rmk (kernel_img_SOURCES): Remove `disk/i386/pc/partition.c'. Add `kern/partition.c'. (kernel_img_HEADERS): Remove `machine/partition.h'. Add `partition.h' and `pc_partition.h'. (grub_setup_SOURCES): Remove `disk/i386/pc/partition.c'. Add `kern/partition.c', `partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'. (grub_emu_SOURCES): Likewise. (pkgdata_MODULES): Add `amiga.mod', `apple.mod' and `pc.mod'. (amiga_mod_SOURCES, amiga_mod_CFLAGS, apple_mod_SOURCES) (apple_mod_CFLAGS, pc_mod_SOURCES, pc_mod_CFLAGS): New variables. * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Remove `disk/powerpc/ieee1275/partition.c'. Add `kern/partition.c', `partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'. (grubof_SOURCES): Likewise. * disk/i386/pc/partition.c: File removed. * disk/powerpc/ieee1275/partition.c: Likewise. * include/grub/powerpc/ieee1275/partition.h: Likewise. * include/grub/i386/pc/partition.h: Likewise. * kern/partition.c: New file. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * partmap/pc.c: Likewise. * include/grub/partition.h: Likewise.. * include/grub/pc_partition.h: Likewise. * util/grub-emu.c: Include <grub/partition.h> instead of <grub/machine/partition.h>. (main): Call `grub_pc_partition_map_init', `grub_amiga_partition_map_init' and `grub_apple_partition_map_init' and deinitialize afterwards. * util/i386/pc/biosdisk.c: Include `#include <grub/partition.h>' and `include <grub/pc_partition.h>' instead of `<grub/machine/partition.h>'. * util/i386/pc/grub-setup.c: Likewise. * util/i386/pc/biosdisk.c: Likewise. (grub_util_biosdisk_get_grub_dev): Only access the PC specific partition information in case of a PC partition. * util/i386/pc/grub-setup.c: Include `#include <grub/partition.h>' and `include <grub/pc_partition.h>' instead of `<grub/machine/partition.h>'. (setup): Only access the PC specific partition information in case of a PC partition.
2004-12-04 18:45:46 +00:00
#include <grub/partition.h>
#include <grub/misc.h>
#include <grub/machine/time.h>
#include <grub/file.h>
#define GRUB_CACHE_TIMEOUT 2
/* The last time the disk was used. */
static unsigned long grub_last_time = 0;
2002-12-27 08:53:07 +00:00
/* Disk cache. */
struct grub_disk_cache
2002-12-27 08:53:07 +00:00
{
unsigned long dev_id;
unsigned long disk_id;
2002-12-27 08:53:07 +00:00
unsigned long sector;
char *data;
int lock;
};
static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
2002-12-27 08:53:07 +00:00
#if 0
static unsigned long grub_disk_cache_hits;
static unsigned long grub_disk_cache_misses;
2002-12-27 08:53:07 +00:00
void
grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
2002-12-27 08:53:07 +00:00
{
*hits = grub_disk_cache_hits;
*misses = grub_disk_cache_misses;
2002-12-27 08:53:07 +00:00
}
#endif
static unsigned
grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
unsigned long sector)
2002-12-27 08:53:07 +00:00
{
return ((dev_id * 524287UL + disk_id * 2606459UL
+ (sector >> GRUB_DISK_CACHE_BITS))
% GRUB_DISK_CACHE_NUM);
2002-12-27 08:53:07 +00:00
}
static void
grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
unsigned long sector)
2002-12-27 08:53:07 +00:00
{
unsigned index;
struct grub_disk_cache *cache;
2002-12-27 08:53:07 +00:00
sector &= ~(GRUB_DISK_CACHE_SIZE - 1);
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
cache = grub_disk_cache_table + index;
2002-12-27 08:53:07 +00:00
if (cache->dev_id == dev_id && cache->disk_id == disk_id
&& cache->sector == sector && cache->data)
2002-12-27 08:53:07 +00:00
{
cache->lock = 1;
grub_free (cache->data);
2002-12-27 08:53:07 +00:00
cache->data = 0;
cache->lock = 0;
}
}
void
grub_disk_cache_invalidate_all (void)
2002-12-27 08:53:07 +00:00
{
unsigned i;
for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
2002-12-27 08:53:07 +00:00
{
struct grub_disk_cache *cache = grub_disk_cache_table + i;
2002-12-27 08:53:07 +00:00
if (cache->data && ! cache->lock)
{
grub_free (cache->data);
2002-12-27 08:53:07 +00:00
cache->data = 0;
}
}
}
static char *
grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
unsigned long sector)
2002-12-27 08:53:07 +00:00
{
struct grub_disk_cache *cache;
2002-12-27 08:53:07 +00:00
unsigned index;
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
cache = grub_disk_cache_table + index;
2002-12-27 08:53:07 +00:00
if (cache->dev_id == dev_id && cache->disk_id == disk_id
&& cache->sector == sector)
2002-12-27 08:53:07 +00:00
{
cache->lock = 1;
#if 0
grub_disk_cache_hits++;
2002-12-27 08:53:07 +00:00
#endif
return cache->data;
}
#if 0
grub_disk_cache_misses++;
2002-12-27 08:53:07 +00:00
#endif
return 0;
}
static void
grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
unsigned long sector)
2002-12-27 08:53:07 +00:00
{
struct grub_disk_cache *cache;
2002-12-27 08:53:07 +00:00
unsigned index;
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
cache = grub_disk_cache_table + index;
2002-12-27 08:53:07 +00:00
if (cache->dev_id == dev_id && cache->disk_id == disk_id
&& cache->sector == sector)
2002-12-27 08:53:07 +00:00
cache->lock = 0;
}
static grub_err_t
grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
unsigned long sector, const char *data)
2002-12-27 08:53:07 +00:00
{
unsigned index;
struct grub_disk_cache *cache;
2002-12-27 08:53:07 +00:00
grub_disk_cache_invalidate (dev_id, disk_id, sector);
2002-12-27 08:53:07 +00:00
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
cache = grub_disk_cache_table + index;
2002-12-27 08:53:07 +00:00
cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
2002-12-27 08:53:07 +00:00
if (! cache->data)
return grub_errno;
2002-12-27 08:53:07 +00:00
grub_memcpy (cache->data, data,
GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
cache->dev_id = dev_id;
cache->disk_id = disk_id;
2002-12-27 08:53:07 +00:00
cache->sector = sector;
return GRUB_ERR_NONE;
2002-12-27 08:53:07 +00:00
}
static grub_disk_dev_t grub_disk_dev_list;
2002-12-27 08:53:07 +00:00
void
grub_disk_dev_register (grub_disk_dev_t dev)
2002-12-27 08:53:07 +00:00
{
dev->next = grub_disk_dev_list;
grub_disk_dev_list = dev;
2002-12-27 08:53:07 +00:00
}
void
grub_disk_dev_unregister (grub_disk_dev_t dev)
2002-12-27 08:53:07 +00:00
{
grub_disk_dev_t *p, q;
2002-12-27 08:53:07 +00:00
for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
2002-12-27 08:53:07 +00:00
if (q == dev)
{
*p = q->next;
break;
}
}
2005-08-12 Yoshinori K. Okuji <okuji@enbug.org> * DISTLIST: Added normal/completion.c. * normal/completion.c: New file. * term/i386/pc/console.c (grub_console_getwh): New function. (grub_console_term): Assign grub_console_getwh to getwh. * normal/cmdline.c (grub_tab_complete): Removed. Now the same function is defined in normal/completion.c as grub_normal_do_completion. (grub_cmdline_get): Use grub_normal_do_completion instead of grub_tab_complete. * kern/partition.c (grub_partition_map_iterate): Return 1 if HOOK returns non-zero, otherwise return 0. (grub_partition_iterate): First, probe the partition map. Then, call ITERATE only for this partition map. * kern/misc.c (grub_strncmp): Rewritten. * kern/disk.c (grub_disk_dev_iterate): Return 1 if P->ITERATE returns non-zero. Otherwise return 0. * include/grub/partition.h (grub_partition_map_iterate): Return int instead of void. * include/grub/normal.h (grub_normal_do_completion): New prototype. * include/grub/misc.h (grub_strncmp): Change the type of N to grub_size_t. * include/grub/disk.h (grub_disk_dev_iterate): Return int instead of void. * normal/menu.c (draw_border): Cast GRUB_TERM_BORDER_WIDTH to unsigned explictly before comparing it with I. * kern/main.c (grub_env_write_root): Add the attribute unused into VAR. * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Added normal/completion.c. (normal_mod_SOURCES): Likewise. * conf/i386-pc.rmk (grub_emu_SOURCES): Likewise. (normal_mod_SOURCES): Likewise. * normal/command.c (grub_iterate_commands): If ITERATE returns non-zero, return one immediately.
2005-08-12 19:53:33 +00:00
int
grub_disk_dev_iterate (int (*hook) (const char *name))
2002-12-27 08:53:07 +00:00
{
grub_disk_dev_t p;
2002-12-27 08:53:07 +00:00
for (p = grub_disk_dev_list; p; p = p->next)
2002-12-27 08:53:07 +00:00
if ((p->iterate) (hook))
2005-08-12 Yoshinori K. Okuji <okuji@enbug.org> * DISTLIST: Added normal/completion.c. * normal/completion.c: New file. * term/i386/pc/console.c (grub_console_getwh): New function. (grub_console_term): Assign grub_console_getwh to getwh. * normal/cmdline.c (grub_tab_complete): Removed. Now the same function is defined in normal/completion.c as grub_normal_do_completion. (grub_cmdline_get): Use grub_normal_do_completion instead of grub_tab_complete. * kern/partition.c (grub_partition_map_iterate): Return 1 if HOOK returns non-zero, otherwise return 0. (grub_partition_iterate): First, probe the partition map. Then, call ITERATE only for this partition map. * kern/misc.c (grub_strncmp): Rewritten. * kern/disk.c (grub_disk_dev_iterate): Return 1 if P->ITERATE returns non-zero. Otherwise return 0. * include/grub/partition.h (grub_partition_map_iterate): Return int instead of void. * include/grub/normal.h (grub_normal_do_completion): New prototype. * include/grub/misc.h (grub_strncmp): Change the type of N to grub_size_t. * include/grub/disk.h (grub_disk_dev_iterate): Return int instead of void. * normal/menu.c (draw_border): Cast GRUB_TERM_BORDER_WIDTH to unsigned explictly before comparing it with I. * kern/main.c (grub_env_write_root): Add the attribute unused into VAR. * conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Added normal/completion.c. (normal_mod_SOURCES): Likewise. * conf/i386-pc.rmk (grub_emu_SOURCES): Likewise. (normal_mod_SOURCES): Likewise. * normal/command.c (grub_iterate_commands): If ITERATE returns non-zero, return one immediately.
2005-08-12 19:53:33 +00:00
return 1;
return 0;
2002-12-27 08:53:07 +00:00
}
grub_disk_t
grub_disk_open (const char *name)
2002-12-27 08:53:07 +00:00
{
char *p;
grub_disk_t disk;
grub_disk_dev_t dev;
2002-12-27 08:53:07 +00:00
char *raw = (char *) name;
unsigned long current_time;
2002-12-27 08:53:07 +00:00
disk = (grub_disk_t) grub_malloc (sizeof (*disk));
2002-12-27 08:53:07 +00:00
if (! disk)
return 0;
disk->dev = 0;
disk->read_hook = 0;
disk->partition = 0;
disk->data = 0;
disk->name = grub_strdup (name);
2002-12-27 08:53:07 +00:00
if (! disk->name)
goto fail;
p = grub_strchr (name, ',');
2002-12-27 08:53:07 +00:00
if (p)
{
grub_size_t len = p - name;
2002-12-27 08:53:07 +00:00
raw = grub_malloc (len + 1);
2002-12-27 08:53:07 +00:00
if (! raw)
goto fail;
grub_memcpy (raw, name, len);
2002-12-27 08:53:07 +00:00
raw[len] = '\0';
}
for (dev = grub_disk_dev_list; dev; dev = dev->next)
2002-12-27 08:53:07 +00:00
{
if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
break;
else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
grub_errno = GRUB_ERR_NONE;
2002-12-27 08:53:07 +00:00
else
goto fail;
}
if (! dev)
{
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
2002-12-27 08:53:07 +00:00
goto fail;
}
if (p && ! disk->has_partitions)
{
grub_error (GRUB_ERR_BAD_DEVICE, "no partition on this disk");
2002-12-27 08:53:07 +00:00
goto fail;
}
disk->dev = dev;
if (p)
disk->partition = grub_partition_probe (disk, p + 1);
2002-12-27 08:53:07 +00:00
/* The cache will be invalidated about 2 seconds after a device was
closed. */
current_time = grub_get_rtc ();
if (current_time > (grub_last_time
+ GRUB_CACHE_TIMEOUT * GRUB_TICKS_PER_SECOND))
grub_disk_cache_invalidate_all ();
grub_last_time = current_time;
2002-12-27 08:53:07 +00:00
fail:
if (raw && raw != name)
grub_free (raw);
2002-12-27 08:53:07 +00:00
if (grub_errno != GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
{
grub_disk_close (disk);
2002-12-27 08:53:07 +00:00
return 0;
}
return disk;
}
void
grub_disk_close (grub_disk_t disk)
2002-12-27 08:53:07 +00:00
{
if (disk->dev && disk->dev->close)
(disk->dev->close) (disk);
/* Reset the timer. */
grub_last_time = grub_get_rtc ();
grub_free (disk->partition);
grub_free ((void *) disk->name);
grub_free (disk);
2002-12-27 08:53:07 +00:00
}
static grub_err_t
grub_disk_check_range (grub_disk_t disk, unsigned long *sector,
unsigned long *offset, grub_ssize_t size)
2002-12-27 08:53:07 +00:00
{
*sector += *offset >> GRUB_DISK_SECTOR_BITS;
*offset &= GRUB_DISK_SECTOR_SIZE - 1;
2002-12-27 08:53:07 +00:00
if (disk->partition)
{
unsigned long start, len;
start = grub_partition_get_start (disk->partition);
len = grub_partition_get_len (disk->partition);
2002-12-27 08:53:07 +00:00
if (*sector >= len
|| len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
>> GRUB_DISK_SECTOR_BITS))
return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition");
2002-12-27 08:53:07 +00:00
*sector += start;
}
if (disk->total_sectors <= *sector
|| ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
>> GRUB_DISK_SECTOR_BITS) > disk->total_sectors - *sector)
return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
2002-12-27 08:53:07 +00:00
return GRUB_ERR_NONE;
2002-12-27 08:53:07 +00:00
}
/* Read data from the disk. */
grub_err_t
grub_disk_read (grub_disk_t disk, unsigned long sector,
2002-12-27 08:53:07 +00:00
unsigned long offset, unsigned long size, char *buf)
{
char *tmp_buf;
/* First of all, check if the region is within the disk. */
if (grub_disk_check_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
return grub_errno;
2002-12-27 08:53:07 +00:00
/* Allocate a temporary buffer. */
tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
2002-12-27 08:53:07 +00:00
if (! tmp_buf)
return grub_errno;
2002-12-27 08:53:07 +00:00
/* Until SIZE is zero... */
while (size)
{
char *data;
unsigned long start_sector;
unsigned long len;
unsigned long pos;
/* For reading bulk data. */
start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1);
pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
len = (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS) - pos - offset;
2002-12-27 08:53:07 +00:00
if (len > size)
len = size;
/* Fetch the cache. */
data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector);
2002-12-27 08:53:07 +00:00
if (data)
{
/* Just copy it! */
grub_memcpy (buf, data + pos + offset, len);
grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector);
2002-12-27 08:53:07 +00:00
}
else
{
/* Otherwise read data from the disk actually. */
if ((disk->dev->read) (disk, start_sector,
GRUB_DISK_CACHE_SIZE, tmp_buf)
!= GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
{
/* Uggh... Failed. Instead, just read necessary data. */
unsigned num;
grub_errno = GRUB_ERR_NONE;
2002-12-27 08:53:07 +00:00
/* If more data is required, no way. */
if (pos + size
>= (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS))
2002-12-27 08:53:07 +00:00
goto finish;
num = ((size + GRUB_DISK_SECTOR_SIZE - 1)
>> GRUB_DISK_SECTOR_BITS);
2002-12-27 08:53:07 +00:00
if ((disk->dev->read) (disk, sector, num, tmp_buf))
goto finish;
grub_memcpy (buf, tmp_buf + offset, size);
2002-12-27 08:53:07 +00:00
/* Call the read hook, if any. */
if (disk->read_hook)
while (size)
{
(disk->read_hook) (sector, offset,
((size > GRUB_DISK_SECTOR_SIZE)
? GRUB_DISK_SECTOR_SIZE
2002-12-27 08:53:07 +00:00
: size));
sector++;
size -= GRUB_DISK_SECTOR_SIZE - offset;
2002-12-27 08:53:07 +00:00
offset = 0;
}
/* This must be the end. */
goto finish;
}
/* Copy it and store it in the disk cache. */
grub_memcpy (buf, tmp_buf + pos + offset, len);
grub_disk_cache_store (disk->dev->id, disk->id,
start_sector, tmp_buf);
2002-12-27 08:53:07 +00:00
}
/* Call the read hook, if any. */
if (disk->read_hook)
{
unsigned long s = sector;
unsigned long l = len;
while (l)
{
(disk->read_hook) (s, offset,
((l > GRUB_DISK_SECTOR_SIZE)
? GRUB_DISK_SECTOR_SIZE
2002-12-27 08:53:07 +00:00
: l));
if (l < GRUB_DISK_SECTOR_SIZE - offset)
break;
2002-12-27 08:53:07 +00:00
s++;
l -= GRUB_DISK_SECTOR_SIZE - offset;
2002-12-27 08:53:07 +00:00
offset = 0;
}
}
sector = start_sector + GRUB_DISK_CACHE_SIZE;
2002-12-27 08:53:07 +00:00
buf += len;
size -= len;
offset = 0;
}
finish:
grub_free (tmp_buf);
2002-12-27 08:53:07 +00:00
return grub_errno;
2002-12-27 08:53:07 +00:00
}
grub_err_t
grub_disk_write (grub_disk_t disk, unsigned long sector,
2002-12-27 08:53:07 +00:00
unsigned long offset, unsigned long size, const char *buf)
{
if (grub_disk_check_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
return -1;
while (size)
{
if (offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
2002-12-27 08:53:07 +00:00
{
char tmp_buf[GRUB_DISK_SECTOR_SIZE];
2002-12-27 08:53:07 +00:00
unsigned long len;
if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf)
!= GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
goto finish;
len = GRUB_DISK_SECTOR_SIZE - offset;
2002-12-27 08:53:07 +00:00
if (len > size)
len = size;
grub_memcpy (tmp_buf + offset, buf, len);
2002-12-27 08:53:07 +00:00
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
2002-12-27 08:53:07 +00:00
if ((disk->dev->write) (disk, sector, 1, tmp_buf) != GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
goto finish;
sector++;
buf += len;
size -= len;
offset = 0;
}
else
{
unsigned long len;
unsigned long n;
len = size & ~(GRUB_DISK_SECTOR_SIZE - 1);
n = size >> GRUB_DISK_SECTOR_BITS;
2002-12-27 08:53:07 +00:00
if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE)
2002-12-27 08:53:07 +00:00
goto finish;
while (n--)
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
2002-12-27 08:53:07 +00:00
buf += len;
size -= len;
}
}
finish:
return grub_errno;
2002-12-27 08:53:07 +00:00
}