2N/A/* bufio.c - buffered io access */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/types.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/fs.h>
2N/A#include <grub/bufio.h>
2N/A#include <grub/dl.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A#define GRUB_BUFIO_DEF_SIZE 8192
2N/A#define GRUB_BUFIO_MAX_SIZE 1048576
2N/A
2N/Astruct grub_bufio
2N/A{
2N/A grub_file_t file;
2N/A grub_size_t block_size;
2N/A grub_size_t buffer_len;
2N/A grub_off_t buffer_at;
2N/A char buffer[0];
2N/A};
2N/Atypedef struct grub_bufio *grub_bufio_t;
2N/A
2N/Astatic struct grub_fs grub_bufio_fs;
2N/A
2N/Agrub_file_t
2N/Agrub_bufio_open (grub_file_t io, int size)
2N/A{
2N/A grub_file_t file;
2N/A grub_bufio_t bufio = 0;
2N/A
2N/A file = (grub_file_t) grub_zalloc (sizeof (*file));
2N/A if (! file)
2N/A return 0;
2N/A
2N/A if (size == 0)
2N/A size = GRUB_BUFIO_DEF_SIZE;
2N/A else if (size > GRUB_BUFIO_MAX_SIZE)
2N/A size = GRUB_BUFIO_MAX_SIZE;
2N/A
2N/A if ((size < 0) || ((unsigned) size > io->size))
2N/A size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
2N/A io->size);
2N/A
2N/A bufio = grub_malloc (sizeof (struct grub_bufio) + size);
2N/A if (! bufio)
2N/A {
2N/A grub_free (file);
2N/A return 0;
2N/A }
2N/A
2N/A bufio->file = io;
2N/A bufio->block_size = size;
2N/A bufio->buffer_len = 0;
2N/A bufio->buffer_at = 0;
2N/A
2N/A file->device = io->device;
2N/A file->offset = 0;
2N/A file->size = io->size;
2N/A file->data = bufio;
2N/A file->read_hook = 0;
2N/A file->fs = &grub_bufio_fs;
2N/A file->not_easily_seekable = io->not_easily_seekable;
2N/A
2N/A return file;
2N/A}
2N/A
2N/Agrub_file_t
2N/Agrub_buffile_open (const char *name, int size)
2N/A{
2N/A grub_file_t io, file;
2N/A
2N/A io = grub_file_open (name);
2N/A if (! io)
2N/A return 0;
2N/A
2N/A file = grub_bufio_open (io, size);
2N/A if (! file)
2N/A {
2N/A grub_file_close (io);
2N/A return 0;
2N/A }
2N/A
2N/A return file;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A grub_size_t res = 0;
2N/A grub_off_t next_buf;
2N/A grub_bufio_t bufio = file->data;
2N/A grub_ssize_t really_read;
2N/A
2N/A if (file->size == GRUB_FILE_SIZE_UNKNOWN)
2N/A file->size = bufio->file->size;
2N/A
2N/A /* First part: use whatever we already have in the buffer. */
2N/A if ((file->offset >= bufio->buffer_at) &&
2N/A (file->offset < bufio->buffer_at + bufio->buffer_len))
2N/A {
2N/A grub_size_t n;
2N/A grub_uint64_t pos;
2N/A
2N/A pos = file->offset - bufio->buffer_at;
2N/A n = bufio->buffer_len - pos;
2N/A if (n > len)
2N/A n = len;
2N/A
2N/A grub_memcpy (buf, &bufio->buffer[pos], n);
2N/A len -= n;
2N/A res += n;
2N/A
2N/A buf += n;
2N/A }
2N/A if (len == 0)
2N/A return res;
2N/A
2N/A /* Need to read some more. */
2N/A next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1);
2N/A /* Now read between file->offset + res and bufio->buffer_at. */
2N/A if (file->offset + res < next_buf)
2N/A {
2N/A grub_size_t read_now;
2N/A read_now = next_buf - (file->offset + res);
2N/A grub_file_seek (bufio->file, file->offset + res);
2N/A really_read = grub_file_read (bufio->file, buf, read_now);
2N/A if (really_read < 0)
2N/A return -1;
2N/A if (file->size == GRUB_FILE_SIZE_UNKNOWN)
2N/A file->size = bufio->file->size;
2N/A len -= really_read;
2N/A buf += really_read;
2N/A res += really_read;
2N/A
2N/A /* Partial read. File ended unexpectedly. Save the last chunk in buffer.
2N/A */
2N/A if (really_read != (grub_ssize_t) read_now)
2N/A {
2N/A bufio->buffer_len = really_read;
2N/A if (bufio->buffer_len > bufio->block_size)
2N/A bufio->buffer_len = bufio->block_size;
2N/A bufio->buffer_at = file->offset + res - bufio->buffer_len;
2N/A grub_memcpy (&bufio->buffer[0], buf - bufio->buffer_len,
2N/A bufio->buffer_len);
2N/A return res;
2N/A }
2N/A }
2N/A
2N/A /* Read into buffer. */
2N/A grub_file_seek (bufio->file, next_buf);
2N/A really_read = grub_file_read (bufio->file, bufio->buffer,
2N/A bufio->block_size);
2N/A if (really_read < 0)
2N/A return -1;
2N/A bufio->buffer_at = next_buf;
2N/A bufio->buffer_len = really_read;
2N/A
2N/A if (file->size == GRUB_FILE_SIZE_UNKNOWN)
2N/A file->size = bufio->file->size;
2N/A
2N/A if (len > bufio->buffer_len)
2N/A len = bufio->buffer_len;
2N/A grub_memcpy (buf, &bufio->buffer[file->offset + res - next_buf], len);
2N/A res += len;
2N/A
2N/A return res;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_bufio_close (grub_file_t file)
2N/A{
2N/A grub_bufio_t bufio = file->data;
2N/A
2N/A grub_file_close (bufio->file);
2N/A grub_free (bufio);
2N/A
2N/A file->device = 0;
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic struct grub_fs grub_bufio_fs =
2N/A {
2N/A .name = "bufio",
2N/A .dir = 0,
2N/A .open = 0,
2N/A .read = grub_bufio_read,
2N/A .close = grub_bufio_close,
2N/A .label = 0,
2N/A .next = 0
2N/A };