2002-12-27 08:53:07 +00:00
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2004-08-21 13:54:22 +00:00
|
|
|
|
* Copyright (C) 2002,2003,2004 Free Software Foundation, Inc.
|
2002-12-27 08:53:07 +00:00
|
|
|
|
*
|
2004-04-04 13:46:03 +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
|
2004-04-04 13:46:03 +00:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
#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>
|
2004-04-04 13:46:03 +00:00
|
|
|
|
#include <grub/misc.h>
|
|
|
|
|
#include <grub/machine/time.h>
|
|
|
|
|
#include <grub/file.h>
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
#define GRUB_CACHE_TIMEOUT 2
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
|
|
|
|
/* The last time the disk was used. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static unsigned long grub_last_time = 0;
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
/* Disk cache. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
struct grub_disk_cache
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-08-21 13:54:22 +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;
|
|
|
|
|
};
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
#if 0
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static unsigned long grub_disk_cache_hits;
|
|
|
|
|
static unsigned long grub_disk_cache_misses;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
*hits = grub_disk_cache_hits;
|
|
|
|
|
*misses = grub_disk_cache_misses;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static unsigned
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
|
|
|
|
|
unsigned long sector)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-08-21 13:54:22 +00:00
|
|
|
|
return ((dev_id * 524287UL + disk_id * 2606459UL
|
|
|
|
|
+ (sector >> GRUB_DISK_CACHE_BITS))
|
2004-04-04 13:46:03 +00:00
|
|
|
|
% GRUB_DISK_CACHE_NUM);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2004-08-21 13:54:22 +00:00
|
|
|
|
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;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
struct grub_disk_cache *cache;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
sector &= ~(GRUB_DISK_CACHE_SIZE - 1);
|
2004-08-21 13:54:22 +00:00
|
|
|
|
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
|
2004-04-04 13:46:03 +00:00
|
|
|
|
cache = grub_disk_cache_table + index;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +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;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_free (cache->data);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
cache->data = 0;
|
|
|
|
|
cache->lock = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_cache_invalidate_all (void)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +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)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_free (cache->data);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
cache->data = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
|
|
|
|
|
unsigned long sector)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
struct grub_disk_cache *cache;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
unsigned index;
|
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
|
2004-04-04 13:46:03 +00:00
|
|
|
|
cache = grub_disk_cache_table + index;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +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
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_cache_hits++;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
return cache->data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_cache_misses++;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
|
|
|
|
|
unsigned long sector)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
struct grub_disk_cache *cache;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
unsigned index;
|
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
|
2004-04-04 13:46:03 +00:00
|
|
|
|
cache = grub_disk_cache_table + index;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_err_t
|
2004-08-21 13:54:22 +00:00
|
|
|
|
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;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
struct grub_disk_cache *cache;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_invalidate (dev_id, disk_id, sector);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
index = grub_disk_cache_get_index (dev_id, disk_id, sector);
|
2004-04-04 13:46:03 +00:00
|
|
|
|
cache = grub_disk_cache_table + index;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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)
|
2004-04-04 13:46:03 +00:00
|
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_memcpy (cache->data, data,
|
|
|
|
|
GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
|
2004-08-21 13:54:22 +00:00
|
|
|
|
cache->dev_id = dev_id;
|
|
|
|
|
cache->disk_id = disk_id;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
cache->sector = sector;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
return GRUB_ERR_NONE;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_disk_dev_t grub_disk_dev_list;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_dev_register (grub_disk_dev_t dev)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
dev->next = grub_disk_dev_list;
|
|
|
|
|
grub_disk_dev_list = dev;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_dev_unregister (grub_disk_dev_t dev)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_dev_t *p, q;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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 19:53:33 +00:00
|
|
|
|
int
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_dev_iterate (int (*hook) (const char *name))
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_dev_t p;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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 19:53:33 +00:00
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
return 0;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_t
|
|
|
|
|
grub_disk_open (const char *name)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
|
|
|
|
char *p;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_t disk;
|
|
|
|
|
grub_disk_dev_t dev;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
char *raw = (char *) name;
|
2003-11-12 20:33:52 +00:00
|
|
|
|
unsigned long current_time;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
disk->name = grub_strdup (name);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
if (! disk->name)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
p = grub_strchr (name, ',');
|
2002-12-27 08:53:07 +00:00
|
|
|
|
if (p)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_size_t len = p - name;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
raw = grub_malloc (len + 1);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
if (! raw)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_memcpy (raw, name, len);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
raw[len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
for (dev = grub_disk_dev_list; dev; dev = dev->next)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
break;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
|
2002-12-27 08:53:07 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p && ! disk->has_partitions)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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)
|
2004-04-04 13:46:03 +00:00
|
|
|
|
disk->partition = grub_partition_probe (disk, p + 1);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2003-11-12 20:33:52 +00:00
|
|
|
|
/* The cache will be invalidated about 2 seconds after a device was
|
|
|
|
|
closed. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
current_time = grub_get_rtc ();
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
if (current_time > (grub_last_time
|
|
|
|
|
+ GRUB_CACHE_TIMEOUT * GRUB_TICKS_PER_SECOND))
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_cache_invalidate_all ();
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_last_time = current_time;
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
if (raw && raw != name)
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_free (raw);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_disk_close (disk);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return disk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2003-11-12 20:33:52 +00:00
|
|
|
|
/* Reset the timer. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_last_time = grub_get_rtc ();
|
2003-11-12 20:33:52 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_free (disk->partition);
|
|
|
|
|
grub_free ((void *) disk->name);
|
|
|
|
|
grub_free (disk);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +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
|
|
|
|
{
|
2004-04-04 13:46:03 +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;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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
|
2004-04-04 13:46:03 +00:00
|
|
|
|
|| 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
|
2004-04-04 13:46:03 +00:00
|
|
|
|
|| ((*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
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
return GRUB_ERR_NONE;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read data from the disk. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if (grub_disk_check_range (disk, §or, &offset, size) != GRUB_ERR_NONE)
|
|
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
/* Allocate a temporary buffer. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
if (! tmp_buf)
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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. */
|
2004-08-21 13:54:22 +00:00
|
|
|
|
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! */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_memcpy (buf, data + pos + offset, len);
|
2004-08-21 13:54:22 +00:00
|
|
|
|
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,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2003-10-29 18:44:30 +00:00
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
|
/* If more data is required, no way. */
|
|
|
|
|
if (pos + size
|
2004-04-04 13:46:03 +00:00
|
|
|
|
>= (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS))
|
2002-12-27 08:53:07 +00:00
|
|
|
|
goto finish;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
((size > GRUB_DISK_SECTOR_SIZE)
|
|
|
|
|
? GRUB_DISK_SECTOR_SIZE
|
2002-12-27 08:53:07 +00:00
|
|
|
|
: size));
|
|
|
|
|
sector++;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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. */
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_memcpy (buf, tmp_buf + pos + offset, len);
|
2004-08-21 13:54:22 +00:00
|
|
|
|
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,
|
2004-04-04 13:46:03 +00:00
|
|
|
|
((l > GRUB_DISK_SECTOR_SIZE)
|
|
|
|
|
? GRUB_DISK_SECTOR_SIZE
|
2002-12-27 08:53:07 +00:00
|
|
|
|
: l));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if (l < GRUB_DISK_SECTOR_SIZE - offset)
|
2003-01-02 20:12:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2002-12-27 08:53:07 +00:00
|
|
|
|
s++;
|
2004-04-04 13:46:03 +00:00
|
|
|
|
l -= GRUB_DISK_SECTOR_SIZE - offset;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
offset = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
sector = start_sector + GRUB_DISK_CACHE_SIZE;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
buf += len;
|
|
|
|
|
size -= len;
|
|
|
|
|
offset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_free (tmp_buf);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +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)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if (grub_disk_check_range (disk, §or, &offset, size) != GRUB_ERR_NONE)
|
2002-12-27 08:53:07 +00:00
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
while (size)
|
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
if (offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0))
|
2002-12-27 08:53:07 +00:00
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
|
char tmp_buf[GRUB_DISK_SECTOR_SIZE];
|
2002-12-27 08:53:07 +00:00
|
|
|
|
unsigned long len;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
len = GRUB_DISK_SECTOR_SIZE - offset;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
if (len > size)
|
|
|
|
|
len = size;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_memcpy (tmp_buf + offset, buf, len);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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;
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
len = size & ~(GRUB_DISK_SECTOR_SIZE - 1);
|
|
|
|
|
n = size >> GRUB_DISK_SECTOR_BITS;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +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--)
|
2004-08-21 13:54:22 +00:00
|
|
|
|
grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
|
2002-12-27 08:53:07 +00:00
|
|
|
|
|
|
|
|
|
buf += len;
|
|
|
|
|
size -= len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
return grub_errno;
|
2002-12-27 08:53:07 +00:00
|
|
|
|
}
|