2008-08-01 Bean <bean123ch@gmail.com>
* conf/common.rmk (pkglib_MODULES): Add bufio.mod. (bufio_mod_SOURCES): New macro. (bufio_mod_CFLAGS): Likewise. (bufio_mod_LDFLAGS): Likewise. * include/grub/bufio.h: New file. * io/bufio.c: Likewise. * video/png.c: Replace <grub/file.h> with <grub/bufio.h>. (grub_video_reader_png): Use grub_buffile_open to open file. * video/jpeg.c: Replace <grub/file.h> with <grub/bufio.h>. (grub_video_reader_jpeg): Use grub_buffile_open to open file. * video/tga.c: Replace <grub/file.h> with <grub/bufio.h>. (grub_video_reader_tga): Use grub_buffile_open to open file. * font/manager.c: Include <grub/bufio.h>. (add_font): Use grub_buffile_open to open file.
This commit is contained in:
parent
3d8383e7a5
commit
9175e93d11
9 changed files with 328 additions and 9 deletions
23
ChangeLog
23
ChangeLog
|
@ -1,3 +1,26 @@
|
|||
2008-08-01 Bean <bean123ch@gmail.com>
|
||||
|
||||
* conf/common.rmk (pkglib_MODULES): Add bufio.mod.
|
||||
(bufio_mod_SOURCES): New macro.
|
||||
(bufio_mod_CFLAGS): Likewise.
|
||||
(bufio_mod_LDFLAGS): Likewise.
|
||||
|
||||
* include/grub/bufio.h: New file.
|
||||
|
||||
* io/bufio.c: Likewise.
|
||||
|
||||
* video/png.c: Replace <grub/file.h> with <grub/bufio.h>.
|
||||
(grub_video_reader_png): Use grub_buffile_open to open file.
|
||||
|
||||
* video/jpeg.c: Replace <grub/file.h> with <grub/bufio.h>.
|
||||
(grub_video_reader_jpeg): Use grub_buffile_open to open file.
|
||||
|
||||
* video/tga.c: Replace <grub/file.h> with <grub/bufio.h>.
|
||||
(grub_video_reader_tga): Use grub_buffile_open to open file.
|
||||
|
||||
* font/manager.c: Include <grub/bufio.h>.
|
||||
(add_font): Use grub_buffile_open to open file.
|
||||
|
||||
2008-07-31 Robert Millan <rmh@aybabtu.com>
|
||||
|
||||
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): When loading
|
||||
|
|
|
@ -3275,7 +3275,7 @@ crc_mod_CFLAGS = $(COMMON_CFLAGS)
|
|||
crc_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
# Misc.
|
||||
pkglib_MODULES += gzio.mod elf.mod
|
||||
pkglib_MODULES += gzio.mod bufio.mod elf.mod
|
||||
|
||||
# For elf.mod.
|
||||
elf_mod_SOURCES = kern/elf.c
|
||||
|
@ -3390,3 +3390,60 @@ partmap-gzio_mod-io_gzio.lst: io/gzio.c $(io/gzio.c_DEPENDENCIES) genpartmaplist
|
|||
|
||||
gzio_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
gzio_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
# For bufio.mod.
|
||||
bufio_mod_SOURCES = io/bufio.c
|
||||
CLEANFILES += bufio.mod mod-bufio.o mod-bufio.c pre-bufio.o bufio_mod-io_bufio.o und-bufio.lst
|
||||
ifneq ($(bufio_mod_EXPORTS),no)
|
||||
CLEANFILES += def-bufio.lst
|
||||
DEFSYMFILES += def-bufio.lst
|
||||
endif
|
||||
MOSTLYCLEANFILES += bufio_mod-io_bufio.d
|
||||
UNDSYMFILES += und-bufio.lst
|
||||
|
||||
bufio.mod: pre-bufio.o mod-bufio.o $(TARGET_OBJ2ELF)
|
||||
-rm -f $@
|
||||
$(TARGET_CC) $(bufio_mod_LDFLAGS) $(TARGET_LDFLAGS) $(MODULE_LDFLAGS) -Wl,-r,-d -o $@ pre-bufio.o mod-bufio.o
|
||||
if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
|
||||
$(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -K _grub_mod_init -K _grub_mod_fini -R .note -R .comment $@
|
||||
|
||||
pre-bufio.o: $(bufio_mod_DEPENDENCIES) bufio_mod-io_bufio.o
|
||||
-rm -f $@
|
||||
$(TARGET_CC) $(bufio_mod_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ bufio_mod-io_bufio.o
|
||||
|
||||
mod-bufio.o: mod-bufio.c
|
||||
$(TARGET_CC) $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(bufio_mod_CFLAGS) -c -o $@ $<
|
||||
|
||||
mod-bufio.c: moddep.lst genmodsrc.sh
|
||||
sh $(srcdir)/genmodsrc.sh 'bufio' $< > $@ || (rm -f $@; exit 1)
|
||||
|
||||
ifneq ($(bufio_mod_EXPORTS),no)
|
||||
def-bufio.lst: pre-bufio.o
|
||||
$(NM) -g --defined-only -P -p $< | sed 's/^\([^ ]*\).*/\1 bufio/' > $@
|
||||
endif
|
||||
|
||||
und-bufio.lst: pre-bufio.o
|
||||
echo 'bufio' > $@
|
||||
$(NM) -u -P -p $< | cut -f1 -d' ' >> $@
|
||||
|
||||
bufio_mod-io_bufio.o: io/bufio.c $(io/bufio.c_DEPENDENCIES)
|
||||
$(TARGET_CC) -Iio -I$(srcdir)/io $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(bufio_mod_CFLAGS) -MD -c -o $@ $<
|
||||
-include bufio_mod-io_bufio.d
|
||||
|
||||
CLEANFILES += cmd-bufio_mod-io_bufio.lst fs-bufio_mod-io_bufio.lst partmap-bufio_mod-io_bufio.lst
|
||||
COMMANDFILES += cmd-bufio_mod-io_bufio.lst
|
||||
FSFILES += fs-bufio_mod-io_bufio.lst
|
||||
PARTMAPFILES += partmap-bufio_mod-io_bufio.lst
|
||||
|
||||
cmd-bufio_mod-io_bufio.lst: io/bufio.c $(io/bufio.c_DEPENDENCIES) gencmdlist.sh
|
||||
set -e; $(TARGET_CC) -Iio -I$(srcdir)/io $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(bufio_mod_CFLAGS) -E $< | sh $(srcdir)/gencmdlist.sh bufio > $@ || (rm -f $@; exit 1)
|
||||
|
||||
fs-bufio_mod-io_bufio.lst: io/bufio.c $(io/bufio.c_DEPENDENCIES) genfslist.sh
|
||||
set -e; $(TARGET_CC) -Iio -I$(srcdir)/io $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(bufio_mod_CFLAGS) -E $< | sh $(srcdir)/genfslist.sh bufio > $@ || (rm -f $@; exit 1)
|
||||
|
||||
partmap-bufio_mod-io_bufio.lst: io/bufio.c $(io/bufio.c_DEPENDENCIES) genpartmaplist.sh
|
||||
set -e; $(TARGET_CC) -Iio -I$(srcdir)/io $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(bufio_mod_CFLAGS) -E $< | sh $(srcdir)/genpartmaplist.sh bufio > $@ || (rm -f $@; exit 1)
|
||||
|
||||
|
||||
bufio_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
bufio_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
|
|
@ -382,7 +382,7 @@ crc_mod_CFLAGS = $(COMMON_CFLAGS)
|
|||
crc_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
# Misc.
|
||||
pkglib_MODULES += gzio.mod elf.mod
|
||||
pkglib_MODULES += gzio.mod bufio.mod elf.mod
|
||||
|
||||
# For elf.mod.
|
||||
elf_mod_SOURCES = kern/elf.c
|
||||
|
@ -393,3 +393,8 @@ elf_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
|||
gzio_mod_SOURCES = io/gzio.c
|
||||
gzio_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
gzio_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
# For bufio.mod.
|
||||
bufio_mod_SOURCES = io/bufio.c
|
||||
bufio_mod_CFLAGS = $(COMMON_CFLAGS)
|
||||
bufio_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <grub/types.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/font.h>
|
||||
#include <grub/bufio.h>
|
||||
|
||||
struct entry
|
||||
{
|
||||
|
@ -69,7 +70,7 @@ add_font (const char *filename)
|
|||
grub_uint32_t num, i;
|
||||
struct font *font = 0;
|
||||
|
||||
file = grub_file_open (filename);
|
||||
file = grub_buffile_open (filename, 0);
|
||||
if (! file)
|
||||
goto fail;
|
||||
|
||||
|
|
28
include/grub/bufio.h
Normal file
28
include/grub/bufio.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* bufio.h - prototypes for bufio */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2008 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GRUB_BUFIO_H
|
||||
#define GRUB_BUFIO_H 1
|
||||
|
||||
#include <grub/file.h>
|
||||
|
||||
grub_file_t grub_bufio_open (grub_file_t io, int size);
|
||||
grub_file_t grub_buffile_open (const char *name, int size);
|
||||
|
||||
#endif /* ! GRUB_BUFIO_H */
|
205
io/bufio.c
Normal file
205
io/bufio.c
Normal file
|
@ -0,0 +1,205 @@
|
|||
/* bufio.c - buffered io access */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2008 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/err.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/fs.h>
|
||||
#include <grub/bufio.h>
|
||||
|
||||
#define GRUB_BUFIO_DEF_SIZE 8192
|
||||
#define GRUB_BUFIO_MAX_SIZE 1048576
|
||||
|
||||
struct grub_bufio
|
||||
{
|
||||
grub_file_t file;
|
||||
grub_size_t block_size;
|
||||
grub_size_t buffer_len;
|
||||
char buffer[0];
|
||||
};
|
||||
typedef struct grub_bufio *grub_bufio_t;
|
||||
|
||||
static struct grub_fs grub_bufio_fs;
|
||||
|
||||
grub_file_t
|
||||
grub_bufio_open (grub_file_t io, int size)
|
||||
{
|
||||
grub_file_t file;
|
||||
grub_bufio_t bufio = 0;
|
||||
|
||||
file = (grub_file_t) grub_malloc (sizeof (*file));
|
||||
if (! file)
|
||||
return 0;
|
||||
|
||||
if (size == 0)
|
||||
size = GRUB_BUFIO_DEF_SIZE;
|
||||
else if (size > GRUB_BUFIO_MAX_SIZE)
|
||||
size = GRUB_BUFIO_MAX_SIZE;
|
||||
|
||||
if ((size < 0) || ((unsigned) size > io->size))
|
||||
size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
|
||||
io->size);
|
||||
|
||||
bufio = grub_malloc (sizeof (struct grub_bufio) + size);
|
||||
if (! bufio)
|
||||
{
|
||||
grub_free (file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bufio->file = io;
|
||||
bufio->block_size = size;
|
||||
bufio->buffer_len = 0;
|
||||
|
||||
file->device = io->device;
|
||||
file->offset = 0;
|
||||
file->size = io->size;
|
||||
file->data = bufio;
|
||||
file->read_hook = 0;
|
||||
file->fs = &grub_bufio_fs;
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
grub_file_t
|
||||
grub_buffile_open (const char *name, int size)
|
||||
{
|
||||
grub_file_t io, file;
|
||||
|
||||
io = grub_file_open (name);
|
||||
if (! io)
|
||||
return 0;
|
||||
|
||||
file = grub_bufio_open (io, size);
|
||||
if (! file)
|
||||
{
|
||||
grub_file_close (io);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
static grub_ssize_t
|
||||
grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
|
||||
{
|
||||
grub_size_t res = len;
|
||||
grub_bufio_t bufio = file->data;
|
||||
grub_uint32_t pos;
|
||||
|
||||
if ((file->offset >= bufio->file->offset) &&
|
||||
(file->offset < bufio->file->offset + bufio->buffer_len))
|
||||
{
|
||||
grub_size_t n;
|
||||
|
||||
pos = file->offset - bufio->file->offset;
|
||||
n = bufio->buffer_len - pos;
|
||||
if (n > len)
|
||||
n = len;
|
||||
|
||||
grub_memcpy (buf, &bufio->buffer[pos], n);
|
||||
len -= n;
|
||||
if (! len)
|
||||
return res;
|
||||
|
||||
buf += n;
|
||||
bufio->file->offset += bufio->buffer_len;
|
||||
pos = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
bufio->file->offset = grub_divmod64 (file->offset, bufio->block_size,
|
||||
&pos);
|
||||
bufio->file->offset *= bufio->block_size;
|
||||
}
|
||||
|
||||
if (pos + len >= bufio->block_size)
|
||||
{
|
||||
if (pos)
|
||||
{
|
||||
grub_size_t n;
|
||||
|
||||
bufio->file->fs->read (bufio->file, bufio->buffer,
|
||||
bufio->block_size);
|
||||
if (grub_errno)
|
||||
return -1;
|
||||
|
||||
n = bufio->block_size - pos;
|
||||
grub_memcpy (buf, &bufio->buffer[pos], n);
|
||||
len -= n;
|
||||
buf += n;
|
||||
bufio->file->offset += bufio->block_size;
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
while (len >= bufio->block_size)
|
||||
{
|
||||
bufio->file->fs->read (bufio->file, buf, bufio->block_size);
|
||||
if (grub_errno)
|
||||
return -1;
|
||||
|
||||
len -= bufio->block_size;
|
||||
buf += bufio->block_size;
|
||||
bufio->file->offset += bufio->block_size;
|
||||
}
|
||||
|
||||
if (! len)
|
||||
{
|
||||
bufio->buffer_len = 0;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
bufio->buffer_len = bufio->file->size - bufio->file->offset;
|
||||
if (bufio->buffer_len > bufio->block_size)
|
||||
bufio->buffer_len = bufio->block_size;
|
||||
|
||||
bufio->file->fs->read (bufio->file, bufio->buffer, bufio->buffer_len);
|
||||
if (grub_errno)
|
||||
return -1;
|
||||
|
||||
grub_memcpy (buf, &bufio->buffer[pos], len);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_bufio_close (grub_file_t file)
|
||||
{
|
||||
grub_bufio_t bufio = file->data;
|
||||
|
||||
grub_file_close (bufio->file);
|
||||
grub_free (bufio);
|
||||
|
||||
file->device = 0;
|
||||
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
static struct grub_fs grub_bufio_fs =
|
||||
{
|
||||
.name = "bufio",
|
||||
.dir = 0,
|
||||
.open = 0,
|
||||
.read = grub_bufio_read,
|
||||
.close = grub_bufio_close,
|
||||
.label = 0,
|
||||
.next = 0
|
||||
};
|
|
@ -23,7 +23,7 @@
|
|||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/arg.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/bufio.h>
|
||||
|
||||
/* Uncomment following define to enable JPEG debug. */
|
||||
//#define JPEG_DEBUG
|
||||
|
@ -664,7 +664,7 @@ grub_video_reader_jpeg (struct grub_video_bitmap **bitmap,
|
|||
grub_file_t file;
|
||||
struct grub_jpeg_data *data;
|
||||
|
||||
file = grub_file_open (filename);
|
||||
file = grub_buffile_open (filename, 0);
|
||||
if (!file)
|
||||
return grub_errno;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/arg.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/bufio.h>
|
||||
|
||||
/* Uncomment following define to enable PNG debug. */
|
||||
//#define PNG_DEBUG
|
||||
|
@ -840,7 +840,7 @@ grub_video_reader_png (struct grub_video_bitmap **bitmap,
|
|||
grub_file_t file;
|
||||
struct grub_png_data *data;
|
||||
|
||||
file = grub_file_open (filename);
|
||||
file = grub_buffile_open (filename, 0);
|
||||
if (!file)
|
||||
return grub_errno;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/arg.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/bufio.h>
|
||||
|
||||
/* Uncomment following define to enable TGA debug. */
|
||||
//#define TGA_DEBUG
|
||||
|
@ -319,7 +319,7 @@ grub_video_reader_tga (struct grub_video_bitmap **bitmap,
|
|||
struct grub_tga_header header;
|
||||
int has_alpha;
|
||||
|
||||
file = grub_file_open (filename);
|
||||
file = grub_buffile_open (filename, 0);
|
||||
if (! file)
|
||||
return grub_errno;
|
||||
|
||||
|
|
Loading…
Reference in a new issue