grub/kern/disk.c
marco_g a35eed7c3e 2003-10-27 Marco Gerards <metgerards@student.han.nl>
* README: In the pupa-mkimage example use _chain instead of chain
	and ext2 instead of fat.
	* TODO: Replace ext2fs with jfs as an example.  Add an item for
	adding journal playback for ext2fs.
	* conf/i386-pc.rmk (pupa_setup_SOURCES): Added fs/ext2.c.
	(pkgdata_MODULES): Added ext2.mod.
	(ext2_mod_SOURCES): New variable.
	(ext2_mod_CFLAGS): Likewise.
	* include/pupa/err.h (pupa_err_t): Added PUPA_ERR_SYMLINK_LOOP.
	* include/pupa/misc.h (pupa_strncpy): New prototype.
	(pupa_strcat): Likewise.
	(pupa_strncmp): Likewise.
	* kern/misc.c (pupa_strcat): Enable function.
	(pupa_strncpy): New function.
	(pupa_strncmp): Likewise.

	* kern/disk.c (pupa_disk_read): Set pupa_errno to PUPA_ERR_NONE
	when the read failed before retrying.
	* util/i386/pc/biosdisk.c (_LARGEFILE_SOURCE): Removed.
	(_FILE_OFFSET_BITS): Likewise.
	* configure.ac: Added AC_SYS_LARGEFILE.
2003-10-29 18:44:30 +00:00

481 lines
11 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* PUPA -- Preliminary Universal Programming Architecture for GRUB
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
* Copyright (C) 2003 Marco Gerards <metgerards@student.han.nl>
*
* PUPA is free software; you can redistribute it and/or modify
* 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 PUPA; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <pupa/disk.h>
#include <pupa/err.h>
#include <pupa/mm.h>
#include <pupa/types.h>
#include <pupa/machine/partition.h>
#include <pupa/misc.h>
/* Disk cache. */
struct pupa_disk_cache
{
unsigned long id;
unsigned long sector;
char *data;
int lock;
};
static struct pupa_disk_cache pupa_disk_cache_table[PUPA_DISK_CACHE_NUM];
#if 0
static unsigned long pupa_disk_cache_hits;
static unsigned long pupa_disk_cache_misses;
void
pupa_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
{
*hits = pupa_disk_cache_hits;
*misses = pupa_disk_cache_misses;
}
#endif
static unsigned
pupa_disk_cache_get_index (unsigned long id, unsigned long sector)
{
return ((id * 2606459 + (sector >> PUPA_DISK_CACHE_BITS))
% PUPA_DISK_CACHE_NUM);
}
static void
pupa_disk_cache_invalidate (unsigned long id, unsigned long sector)
{
unsigned index;
struct pupa_disk_cache *cache;
sector &= ~(PUPA_DISK_CACHE_SIZE - 1);
index = pupa_disk_cache_get_index (id, sector);
cache = pupa_disk_cache_table + index;
if (cache->id == id && cache->sector == sector && cache->data)
{
cache->lock = 1;
pupa_free (cache->data);
cache->data = 0;
cache->lock = 0;
}
}
void
pupa_disk_cache_invalidate_all (void)
{
unsigned i;
for (i = 0; i < PUPA_DISK_CACHE_NUM; i++)
{
struct pupa_disk_cache *cache = pupa_disk_cache_table + i;
if (cache->data && ! cache->lock)
{
pupa_free (cache->data);
cache->data = 0;
}
}
}
static char *
pupa_disk_cache_fetch (unsigned long id, unsigned long sector)
{
struct pupa_disk_cache *cache;
unsigned index;
index = pupa_disk_cache_get_index (id, sector);
cache = pupa_disk_cache_table + index;
if (cache->id == id && cache->sector == sector)
{
cache->lock = 1;
#if 0
pupa_disk_cache_hits++;
#endif
return cache->data;
}
#if 0
pupa_disk_cache_misses++;
#endif
return 0;
}
static void
pupa_disk_cache_unlock (unsigned long id, unsigned long sector)
{
struct pupa_disk_cache *cache;
unsigned index;
index = pupa_disk_cache_get_index (id, sector);
cache = pupa_disk_cache_table + index;
if (cache->id == id && cache->sector == sector)
cache->lock = 0;
}
static pupa_err_t
pupa_disk_cache_store (unsigned long id, unsigned long sector,
const char *data)
{
unsigned index;
struct pupa_disk_cache *cache;
pupa_disk_cache_invalidate (id, sector);
index = pupa_disk_cache_get_index (id, sector);
cache = pupa_disk_cache_table + index;
cache->data = pupa_malloc (PUPA_DISK_SECTOR_SIZE << PUPA_DISK_CACHE_BITS);
if (! cache->data)
return pupa_errno;
pupa_memcpy (cache->data, data,
PUPA_DISK_SECTOR_SIZE << PUPA_DISK_CACHE_BITS);
cache->id = id;
cache->sector = sector;
return PUPA_ERR_NONE;
}
static pupa_disk_dev_t pupa_disk_dev_list;
void
pupa_disk_dev_register (pupa_disk_dev_t dev)
{
dev->next = pupa_disk_dev_list;
pupa_disk_dev_list = dev;
}
void
pupa_disk_dev_unregister (pupa_disk_dev_t dev)
{
pupa_disk_dev_t *p, q;
for (p = &pupa_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
if (q == dev)
{
*p = q->next;
break;
}
}
void
pupa_disk_dev_iterate (int (*hook) (const char *name))
{
pupa_disk_dev_t p;
for (p = pupa_disk_dev_list; p; p = p->next)
if ((p->iterate) (hook))
break;
}
pupa_disk_t
pupa_disk_open (const char *name)
{
char *p;
pupa_disk_t disk;
pupa_disk_dev_t dev;
char *raw = (char *) name;
disk = (pupa_disk_t) pupa_malloc (sizeof (*disk));
if (! disk)
return 0;
disk->dev = 0;
disk->read_hook = 0;
disk->partition = 0;
disk->data = 0;
disk->name = pupa_strdup (name);
if (! disk->name)
goto fail;
p = pupa_strchr (name, ',');
if (p)
{
pupa_size_t len = p - name;
raw = pupa_malloc (len + 1);
if (! raw)
goto fail;
pupa_memcpy (raw, name, len);
raw[len] = '\0';
}
for (dev = pupa_disk_dev_list; dev; dev = dev->next)
{
if ((dev->open) (raw, disk) == PUPA_ERR_NONE)
break;
else if (pupa_errno == PUPA_ERR_UNKNOWN_DEVICE)
pupa_errno = PUPA_ERR_NONE;
else
goto fail;
}
if (! dev)
{
pupa_error (PUPA_ERR_UNKNOWN_DEVICE, "no such disk");
goto fail;
}
if (p && ! disk->has_partitions)
{
pupa_error (PUPA_ERR_BAD_DEVICE, "no partition on this disk");
goto fail;
}
disk->dev = dev;
if (p)
disk->partition = pupa_partition_probe (disk, p + 1);
fail:
if (raw && raw != name)
pupa_free (raw);
if (pupa_errno != PUPA_ERR_NONE)
{
pupa_disk_close (disk);
return 0;
}
return disk;
}
void
pupa_disk_close (pupa_disk_t disk)
{
if (disk->dev && disk->dev->close)
(disk->dev->close) (disk);
pupa_free (disk->partition);
pupa_free ((void *) disk->name);
pupa_free (disk);
}
static pupa_err_t
pupa_disk_check_range (pupa_disk_t disk, unsigned long *sector,
unsigned long *offset, pupa_ssize_t size)
{
*sector += *offset >> PUPA_DISK_SECTOR_BITS;
*offset &= PUPA_DISK_SECTOR_SIZE - 1;
if (disk->partition)
{
unsigned long start, len;
start = pupa_partition_get_start (disk->partition);
len = pupa_partition_get_len (disk->partition);
if (*sector >= len
|| len - *sector < ((*offset + size + PUPA_DISK_SECTOR_SIZE - 1)
>> PUPA_DISK_SECTOR_BITS))
return pupa_error (PUPA_ERR_OUT_OF_RANGE, "out of partition");
*sector += start;
}
if (disk->total_sectors <= *sector
|| ((*offset + size + PUPA_DISK_SECTOR_SIZE - 1)
>> PUPA_DISK_SECTOR_BITS) > disk->total_sectors - *sector)
return pupa_error (PUPA_ERR_OUT_OF_RANGE, "out of disk");
return PUPA_ERR_NONE;
}
/* Read data from the disk. */
pupa_err_t
pupa_disk_read (pupa_disk_t disk, unsigned long sector,
unsigned long offset, unsigned long size, char *buf)
{
char *tmp_buf;
/* First of all, check if the region is within the disk. */
if (pupa_disk_check_range (disk, &sector, &offset, size) != PUPA_ERR_NONE)
return pupa_errno;
/* Allocate a temporary buffer. */
tmp_buf = pupa_malloc (PUPA_DISK_SECTOR_SIZE << PUPA_DISK_CACHE_BITS);
if (! tmp_buf)
return pupa_errno;
/* 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 & ~(PUPA_DISK_CACHE_SIZE - 1);
pos = (sector - start_sector) << PUPA_DISK_SECTOR_BITS;
len = (PUPA_DISK_SECTOR_SIZE << PUPA_DISK_CACHE_BITS) - pos - offset;
if (len > size)
len = size;
/* Fetch the cache. */
data = pupa_disk_cache_fetch (disk->id, start_sector);
if (data)
{
/* Just copy it! */
pupa_memcpy (buf, data + pos + offset, len);
pupa_disk_cache_unlock (disk->id, start_sector);
}
else
{
/* Otherwise read data from the disk actually. */
if ((disk->dev->read) (disk, start_sector,
PUPA_DISK_CACHE_SIZE, tmp_buf)
!= PUPA_ERR_NONE)
{
/* Uggh... Failed. Instead, just read necessary data. */
unsigned num;
pupa_errno = PUPA_ERR_NONE;
/* If more data is required, no way. */
if (pos + size
>= (PUPA_DISK_SECTOR_SIZE << PUPA_DISK_CACHE_BITS))
goto finish;
num = ((size + PUPA_DISK_SECTOR_SIZE - 1)
>> PUPA_DISK_SECTOR_BITS);
if ((disk->dev->read) (disk, sector, num, tmp_buf))
goto finish;
pupa_memcpy (buf, tmp_buf + offset, size);
/* Call the read hook, if any. */
if (disk->read_hook)
while (size)
{
(disk->read_hook) (sector, offset,
((size > PUPA_DISK_SECTOR_SIZE)
? PUPA_DISK_SECTOR_SIZE
: size));
sector++;
size -= PUPA_DISK_SECTOR_SIZE - offset;
offset = 0;
}
/* This must be the end. */
goto finish;
}
/* Copy it and store it in the disk cache. */
pupa_memcpy (buf, tmp_buf + pos + offset, len);
pupa_disk_cache_store (disk->id, start_sector, tmp_buf);
}
/* 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 > PUPA_DISK_SECTOR_SIZE)
? PUPA_DISK_SECTOR_SIZE
: l));
if (l < PUPA_DISK_SECTOR_SIZE - offset)
break;
s++;
l -= PUPA_DISK_SECTOR_SIZE - offset;
offset = 0;
}
}
sector = start_sector + PUPA_DISK_CACHE_SIZE;
buf += len;
size -= len;
offset = 0;
}
finish:
pupa_free (tmp_buf);
return pupa_errno;
}
pupa_err_t
pupa_disk_write (pupa_disk_t disk, unsigned long sector,
unsigned long offset, unsigned long size, const char *buf)
{
if (pupa_disk_check_range (disk, &sector, &offset, size) != PUPA_ERR_NONE)
return -1;
while (size)
{
if (offset != 0 || (size < PUPA_DISK_SECTOR_SIZE && size != 0))
{
char tmp_buf[PUPA_DISK_SECTOR_SIZE];
unsigned long len;
if (pupa_disk_read (disk, sector, 0, PUPA_DISK_SECTOR_SIZE, tmp_buf)
!= PUPA_ERR_NONE)
goto finish;
len = PUPA_DISK_SECTOR_SIZE - offset;
if (len > size)
len = size;
pupa_memcpy (tmp_buf + offset, buf, len);
pupa_disk_cache_invalidate (disk->id, sector);
if ((disk->dev->write) (disk, sector, 1, tmp_buf) != PUPA_ERR_NONE)
goto finish;
sector++;
buf += len;
size -= len;
offset = 0;
}
else
{
unsigned long len;
unsigned long n;
len = size & ~(PUPA_DISK_SECTOR_SIZE - 1);
n = size >> PUPA_DISK_SECTOR_BITS;
if ((disk->dev->write) (disk, sector, n, buf) != PUPA_ERR_NONE)
goto finish;
while (n--)
pupa_disk_cache_invalidate (disk->id, sector++);
buf += len;
size -= len;
}
}
finish:
return pupa_errno;
}