grub/kern/disk.c
marco_g db1771cfbe 2004-03-14 Marco Gerards <metgerards@student.han.nl>
* commands/boot.c: New file.
	* commands/cat.c: Likewise.
	* commands/cmp.c: Likewise.
	* commands/ls.c: Likewise.
	* commands/terminal.c: Likewise.
	* normal/command.c: Include <pupa/env.h> and <pupa/dl.h>.
	(pupa_register_command): Changed interface to match the new
	argument parser.
	(pupa_command_execute): Changed (almost rewritten) so it uses
	pupa_split_command.  Added support for setting variables using the
	syntax `foo=bar'.
	(rescue_command): Changed to work with the new argument parser.
	(terminal_command): Moved from here to commands/terminal.c.
	(set_command): New function.
	(unset_command): New function.
	(insmod_command): New function.
	(rmmod_command): New function.
	(lsmod_command): New function.
	(pupa_command_init): Don't initialize the command terminal
	anymore.  Initialize the commands set, unset, insmod, rmmod and
	lsmod.
	* conf/i386-pc.rmk (kernel_img_SOURCES): Add kern/env.c.
	(kernel_img_HEADERS): Add arg.h and env.h.
	(pupa_mkimage_LDFLAGS): Add kern/env.c.
	(pupa_emu_SOURCES): Add kern/env.c, commands/ls.c,
	commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c,
	normal/arg.c.
	(pkgdata_MODULES): Add ls.mod, boot.mod, cmp.mod, cat.mod and
	terminal.mod.
	(normal_mod_SOURCES): Add normal/arg.c and normal/arg.c.
	(boot_mod_SOURCES): New variable.
	(terminal_mod_SOURCES): Likewise.
	(ls_mod_SOURCES): Likewise.
	(cmp_mod_SOURCES): Likewise.
	(cat_mod_SOURCES): Likewise.

	* normal/arg.c: New file.
	* kern/env.c: Likewise.
	* include/pupa/arg.h: Likewise.
	* include/pupa/env.h: Likewise.
	* font/manager.c (font_command): Changed to match argument parsing
	interface changes.
	(PUPA_MOD_INIT): Likewise.
	* hello/hello.c (pupa_cmd_hello): Likewise.
	(PUPA_MOD_INIT): Likewise.
	* include/pupa/disk.h: Include <pupa/device.h>.
	(pupa_print_partinfo): New prototype.
	* include/pupa/dl.h (pupa_dl_set_prefix): Prototype removed.
	(pupa_dl_get_prefix): Likewise.
	* include/pupa/misc.h: Include <pupa/err.h>.
	(pupa_isgraph): New prototype.
	(pupa_isdigit): Likewise.
	(pupa_split_cmdline): Likewise.
	* include/pupa/normal.h: Include <pupa/arg.h>.
	(pupa_command): Changed the prototype of the member `func' to
	match the argument parsing interface.  Added member `options'.
	(pupa_register_command): Updated to match function.
	(pupa_arg_parse): New prototype.
	(pupa_hello_init) [PUPA_UTIL]: New prototype.
	(pupa_hello_fini) [PUPA_UTIL]: Likewise.
	(pupa_ls_init) [PUPA_UTIL]: Likewise.
	(pupa_ls_fini) [PUPA_UTIL]: Likewise.
	(pupa_cat_init) [PUPA_UTIL]: Likewise.
	(pupa_cat_fini) [PUPA_UTIL]: Likewise.
	(pupa_boot_init) [PUPA_UTIL]: Likewise.
	(pupa_boot_fini) [PUPA_UTIL]: Likewise.
	(pupa_cmp_init) [PUPA_UTIL]: Likewise.
	(pupa_cmp_fini) [PUPA_UTIL]: Likewise.
	(pupa_terminal_init) [PUPA_UTIL]: Likewise.
	(pupa_terminal_fini) [PUPA_UTIL]: Likewise.
	* kern/disk.c: Include <pupa/file.h>.
	(pupa_print_partinfo): New function.
	* kern/dl.c: Include <pupa/env.h>.
	(pupa_dl_dir): Variable removed.
	(pupa_dl_load): Use the environment variable `prefix' instead of
	the variable pupa_dl_dir.
	(pupa_dl_set_prefix): Function removed.
	(pupa_dl_get_prefix): Likewise.
	* kern/i386/pc/init.c: Include <pupa/env.h>.
	(pupa_machine_init): Use the environment variable `prefix' instead of
	using pupa_dl_set_prefix to set the prefix.
	* kern/main.c: Include <pupa/env.h>.
	(pupa_set_root_dev): Use the environment variable `prefix' instead of
	using pupa_dl_get_prefix to get the prefix.
	* kern/misc.c: Include <pupa/env.h>.
	(pupa_isdigit): New function.
	(pupa_isgraph): Likewise.
	(pupa_ftoa): Likewise.
	(pupa_vsprintf): Added support for printing values of the type
	`double'.  Make it possible to format variable output when using
	formatting like `%1.2%f'.
	(pupa_split_cmdline): New function.
	* kern/rescue.c: Include <pupa/env.h>.
	(next_word): Removed function.
	(pupa_rescue_cmd_prefix): Likewise.
	(pupa_rescue_cmd_set): New function.
	(pupa_rescue_cmd_unset): New function.
	(pupa_enter_rescue_mode): Use the `pupa_split_cmdline' function to
	split the command line instead of splitting it here.  Added
	support for setting variables using the syntax `foo=bar'.  Don't
	initialize the prefix command anymore.  Initialized the set and
	unset commands.
	* normal/cmdline.c: Include <pupa/env.h>.
	(pupa_tab_complete): Added prototypes for print_simple_completion,
	print_partition_completion, add_completion, iterate_commands,
	iterate_dev, iterate_part and iterate_dir. Moved code to print
	partition information from here to kern/disk.c.
	(pupa_cmdline_run): Don't check if the funtion exists anymore.
	* normal/main.c: Include <pupa/env.h>.
	(pupa_rescue_cmd_normal): Use the environment variable `prefix'
	instead of using pupa_dl_get_prefix to get the prefix.
	* term/i386/pc/vga.c: Include <pupa/arg.h>.
	(check_vga_mem): Cast pointers to `void *' to silence a gcc
	warning.
	(pupa_vga_putchar) [! DEBUG_VGA]: Removed for this case.
	(pupa_vga_setcolor): Declare unused variables with `__attribute__
	((unused))' to silence a gcc warning.
	(pupa_vga_setcolor): Likewise.
	(debug_command): Changed to match argument parsing
	interface changes.
	* util/pupa-emu.c: Include <pupa/env.h>.
	(options): Added 0's for unused fields to silence a gcc warning.
	(argp): Likewise.
	(main): Use the environment variable `prefix' instead of using
	pupa_dl_set_prefix to set the prefix.  Initialize the commands ls,
	boot, cmp, cat and terminal.  Finish the commands boot, cmp, cat
	and terminal.

	* util/i386/pc/getroot.c: Include <pupa/i386/pc/util/biosdisk.h>.
	* util/misc.c: Include <malloc.h>.
	(pupa_malloc): Rewritten so errors are correctly reported.
	(pupa_realloc): Likewise.
	(pupa_memalign): Likewise.
	(pupa_mm_init_region): Declare unused variables with
	`__attribute__ ((unused))' to silence a gcc warning.
	* normal/i386/setjmp.S: Remove tab at the end of the file to
	silence a gcc warning.
	* loader/i386/pc/linux.c (pupa_rescue_cmd_initrd): Declare unused
	variables with `__attribute__ ((unused))' to silence a gcc
	warning.
	* loader/i386/pc/multiboot.c (pupa_multiboot_unload): Make the
	local variable i unsigned to silence a gcc warning.

	* kern/term.c: Include <pupa/misc.h>.
	(pupa_more_lines): New variable.
	(pupa_more): Likewise.
	(pupa_putcode): When the pager is active pause at the end of every
	screen.
	(pupa_set_more): New function.
	* include/pupa/term.h (pupa_set_more): New prototype.
2004-03-13 13:59:25 +00:00

542 lines
12 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,2003 Free Software Foundation, Inc.
*
* 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>
#include <pupa/machine/time.h>
#include <pupa/file.h>
#define PUPA_CACHE_TIMEOUT 2
/* The last time the disk was used. */
static unsigned long pupa_last_time = 0;
/* 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;
unsigned long current_time;
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);
/* The cache will be invalidated about 2 seconds after a device was
closed. */
current_time = pupa_get_rtc ();
if (current_time > pupa_last_time + PUPA_CACHE_TIMEOUT * PUPA_TICKS_PER_SECOND)
pupa_disk_cache_invalidate_all ();
pupa_last_time = current_time;
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);
/* Reset the timer. */
pupa_last_time = pupa_get_rtc ();
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;
}
pupa_err_t
pupa_print_partinfo (pupa_device_t disk, char *partname)
{
pupa_fs_t fs = 0;
pupa_device_t part;
char devname[20];
pupa_sprintf (devname, "%s,%s", disk->disk->name, partname);
part = pupa_device_open (devname);
if (!part)
pupa_printf ("\tPartition num:%s, Filesystem cannot be accessed",
partname);
else
{
char *label;
fs = pupa_fs_probe (part);
/* Ignore all errors. */
pupa_errno = 0;
pupa_printf ("\tPartition num:%s, Filesystem type %s",
partname, fs ? fs->name : "Unknown");
if (fs)
{
(fs->label) (part, &label);
if (pupa_errno == PUPA_ERR_NONE)
{
if (label && pupa_strlen (label))
pupa_printf (", Label: %s", label);
pupa_free (label);
}
pupa_errno = PUPA_ERR_NONE;
}
pupa_device_close (part);
}
pupa_printf ("\n");
return pupa_errno;
}