2004-09-11 11:42:43 +00:00
|
|
|
/* fshelp.c -- Filesystem helper functions */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2008-05-29 13:01:43 +00:00
|
|
|
* Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc.
|
2004-09-11 11:42:43 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2004-09-11 11:42:43 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2004-09-11 11:42:43 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2004-09-11 11:42:43 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2004-09-11 11:42:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/fshelp.h>
|
2011-04-11 21:01:51 +00:00
|
|
|
#include <grub/dl.h>
|
2012-02-08 18:26:01 +00:00
|
|
|
#include <grub/i18n.h>
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
typedef int (*iterate_dir_func) (grub_fshelp_node_t dir,
|
|
|
|
grub_fshelp_iterate_dir_hook_t hook,
|
|
|
|
void *data);
|
|
|
|
typedef char *(*read_symlink_func) (grub_fshelp_node_t node);
|
|
|
|
|
|
|
|
/* Context for grub_fshelp_find_file. */
|
|
|
|
struct grub_fshelp_find_file_ctx
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
const char *path;
|
|
|
|
grub_fshelp_node_t rootnode, currroot, currnode, oldnode;
|
|
|
|
enum grub_fshelp_filetype foundtype;
|
|
|
|
int symlinknest;
|
|
|
|
char *name;
|
|
|
|
enum grub_fshelp_filetype type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper for find_file_iter. */
|
|
|
|
static void
|
|
|
|
free_node (grub_fshelp_node_t node, struct grub_fshelp_find_file_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (node != ctx->rootnode && node != ctx->currroot)
|
|
|
|
grub_free (node);
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Helper for grub_fshelp_find_file. */
|
|
|
|
static int
|
|
|
|
find_file_iter (const char *filename, enum grub_fshelp_filetype filetype,
|
|
|
|
grub_fshelp_node_t node, void *data)
|
|
|
|
{
|
|
|
|
struct grub_fshelp_find_file_ctx *ctx = data;
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
if (filetype == GRUB_FSHELP_UNKNOWN ||
|
|
|
|
(grub_strcmp (ctx->name, filename) &&
|
|
|
|
(! (filetype & GRUB_FSHELP_CASE_INSENSITIVE) ||
|
|
|
|
grub_strcasecmp (ctx->name, filename))))
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
grub_free (node);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* The node is found, stop iterating over the nodes. */
|
|
|
|
ctx->type = filetype & ~GRUB_FSHELP_CASE_INSENSITIVE;
|
|
|
|
ctx->oldnode = ctx->currnode;
|
|
|
|
ctx->currnode = node;
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
static grub_err_t
|
|
|
|
find_file (const char *currpath, grub_fshelp_node_t currroot,
|
|
|
|
grub_fshelp_node_t *currfound,
|
|
|
|
iterate_dir_func iterate_dir, read_symlink_func read_symlink,
|
|
|
|
struct grub_fshelp_find_file_ctx *ctx)
|
|
|
|
{
|
|
|
|
char fpath[grub_strlen (currpath) + 1];
|
|
|
|
char *next;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx->currroot = currroot;
|
|
|
|
ctx->name = fpath;
|
|
|
|
ctx->type = GRUB_FSHELP_DIR;
|
|
|
|
ctx->currnode = currroot;
|
|
|
|
ctx->oldnode = currroot;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
grub_strncpy (fpath, currpath, grub_strlen (currpath) + 1);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Remove all leading slashes. */
|
|
|
|
while (*ctx->name == '/')
|
|
|
|
ctx->name++;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
if (! *ctx->name)
|
|
|
|
{
|
|
|
|
*currfound = ctx->currnode;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int found;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Extract the actual part from the pathname. */
|
|
|
|
next = grub_strchr (ctx->name, '/');
|
|
|
|
if (next)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Remove all leading slashes. */
|
|
|
|
while (*next == '/')
|
|
|
|
*(next++) = '\0';
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* At this point it is expected that the current node is a
|
|
|
|
directory, check if this is true. */
|
|
|
|
if (ctx->type != GRUB_FSHELP_DIR)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
free_node (ctx->currnode, ctx);
|
|
|
|
ctx->currnode = 0;
|
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Iterate over the directory. */
|
|
|
|
found = iterate_dir (ctx->currnode, find_file_iter, ctx);
|
|
|
|
if (! found)
|
|
|
|
{
|
|
|
|
free_node (ctx->currnode, ctx);
|
|
|
|
ctx->currnode = 0;
|
|
|
|
if (grub_errno)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read in the symlink and follow it. */
|
|
|
|
if (ctx->type == GRUB_FSHELP_SYMLINK)
|
|
|
|
{
|
|
|
|
char *symlink;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Test if the symlink does not loop. */
|
|
|
|
if (++ctx->symlinknest == 8)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
free_node (ctx->currnode, ctx);
|
|
|
|
free_node (ctx->oldnode, ctx);
|
|
|
|
ctx->currnode = 0;
|
|
|
|
return grub_error (GRUB_ERR_SYMLINK_LOOP,
|
|
|
|
N_("too deep nesting of symlinks"));
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
symlink = read_symlink (ctx->currnode);
|
|
|
|
free_node (ctx->currnode, ctx);
|
|
|
|
ctx->currnode = 0;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
if (!symlink)
|
|
|
|
{
|
|
|
|
free_node (ctx->oldnode, ctx);
|
|
|
|
return grub_errno;
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* The symlink is an absolute path, go back to the root inode. */
|
|
|
|
if (symlink[0] == '/')
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
free_node (ctx->oldnode, ctx);
|
|
|
|
ctx->oldnode = ctx->rootnode;
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Lookup the node the symlink points to. */
|
|
|
|
find_file (symlink, ctx->oldnode, &ctx->currnode,
|
|
|
|
iterate_dir, read_symlink, ctx);
|
|
|
|
ctx->type = ctx->foundtype;
|
|
|
|
grub_free (symlink);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
if (grub_errno)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
free_node (ctx->oldnode, ctx);
|
|
|
|
return grub_errno;
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2013-01-21 01:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->oldnode != ctx->currnode)
|
|
|
|
free_node (ctx->oldnode, ctx);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Found the node! */
|
|
|
|
if (! next || *next == '\0')
|
|
|
|
{
|
|
|
|
*currfound = ctx->currnode;
|
|
|
|
ctx->foundtype = ctx->type;
|
|
|
|
return 0;
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx->name = next;
|
2004-09-11 11:42:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
|
|
|
|
ctx->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup the node PATH. The node ROOTNODE describes the root of the
|
|
|
|
directory tree. The node found is returned in FOUNDNODE, which is
|
|
|
|
either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to
|
|
|
|
iterate over all directory entries in the current node.
|
|
|
|
READ_SYMLINK is used to read the symlink if a node is a symlink.
|
|
|
|
EXPECTTYPE is the type node that is expected by the called, an
|
|
|
|
error is generated if the node is not of the expected type. */
|
|
|
|
grub_err_t
|
|
|
|
grub_fshelp_find_file (const char *path, grub_fshelp_node_t rootnode,
|
|
|
|
grub_fshelp_node_t *foundnode,
|
|
|
|
iterate_dir_func iterate_dir,
|
|
|
|
read_symlink_func read_symlink,
|
|
|
|
enum grub_fshelp_filetype expecttype)
|
|
|
|
{
|
|
|
|
struct grub_fshelp_find_file_ctx ctx = {
|
|
|
|
.path = path,
|
|
|
|
.rootnode = rootnode,
|
|
|
|
.foundtype = GRUB_FSHELP_DIR,
|
|
|
|
.symlinknest = 0
|
|
|
|
};
|
|
|
|
grub_err_t err;
|
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
if (!path || path[0] != '/')
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), path);
|
2004-09-11 11:42:43 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
err = find_file (path, rootnode, foundnode, iterate_dir, read_symlink, &ctx);
|
2004-09-11 11:42:43 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
/* Check if the node that was found was of the expected type. */
|
2013-01-21 01:33:46 +00:00
|
|
|
if (expecttype == GRUB_FSHELP_REG && ctx.foundtype != expecttype)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
|
2013-01-21 01:33:46 +00:00
|
|
|
else if (expecttype == GRUB_FSHELP_DIR && ctx.foundtype != expecttype)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read LEN bytes from the file NODE on disk DISK into the buffer BUF,
|
|
|
|
beginning with the block POS. READ_HOOK should be set before
|
|
|
|
reading a block from the file. GET_BLOCK is used to translate file
|
|
|
|
blocks to disk blocks. The file is FILESIZE bytes big and the
|
|
|
|
blocks have a size of LOG2BLOCKSIZE (in log2). */
|
|
|
|
grub_ssize_t
|
|
|
|
grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
|
2007-08-02 18:40:37 +00:00
|
|
|
void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
|
2008-05-20 05:00:53 +00:00
|
|
|
unsigned offset,
|
|
|
|
unsigned length),
|
|
|
|
grub_off_t pos, grub_size_t len, char *buf,
|
|
|
|
grub_disk_addr_t (*get_block) (grub_fshelp_node_t node,
|
|
|
|
grub_disk_addr_t block),
|
2012-05-09 11:02:59 +00:00
|
|
|
grub_off_t filesize, int log2blocksize,
|
|
|
|
grub_disk_addr_t blocks_start)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2008-05-20 05:00:53 +00:00
|
|
|
grub_disk_addr_t i, blockcnt;
|
2004-09-11 11:42:43 +00:00
|
|
|
int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
|
|
|
|
|
2007-03-19 00:04:29 +00:00
|
|
|
/* Adjust LEN so it we can't read past the end of the file. */
|
2008-05-20 05:00:53 +00:00
|
|
|
if (pos + len > filesize)
|
|
|
|
len = filesize - pos;
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2008-05-20 05:00:53 +00:00
|
|
|
blockcnt = ((len + pos) + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS);
|
2004-09-11 11:42:43 +00:00
|
|
|
|
2008-05-20 05:00:53 +00:00
|
|
|
for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++)
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
2008-05-20 05:00:53 +00:00
|
|
|
grub_disk_addr_t blknr;
|
|
|
|
int blockoff = pos & (blocksize - 1);
|
2004-09-11 11:42:43 +00:00
|
|
|
int blockend = blocksize;
|
|
|
|
|
|
|
|
int skipfirst = 0;
|
|
|
|
|
|
|
|
blknr = get_block (node, i);
|
|
|
|
if (grub_errno)
|
|
|
|
return -1;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
blknr = blknr << log2blocksize;
|
|
|
|
|
|
|
|
/* Last block. */
|
|
|
|
if (i == blockcnt - 1)
|
|
|
|
{
|
2008-05-20 05:00:53 +00:00
|
|
|
blockend = (len + pos) & (blocksize - 1);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
/* The last portion is exactly blocksize. */
|
2007-03-19 00:04:29 +00:00
|
|
|
if (! blockend)
|
2004-09-11 11:42:43 +00:00
|
|
|
blockend = blocksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First block. */
|
2008-05-20 05:00:53 +00:00
|
|
|
if (i == (pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS)))
|
2004-09-11 11:42:43 +00:00
|
|
|
{
|
|
|
|
skipfirst = blockoff;
|
|
|
|
blockend -= skipfirst;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-09-11 11:42:43 +00:00
|
|
|
/* If the block number is 0 this block is not stored on disk but
|
|
|
|
is zero filled instead. */
|
|
|
|
if (blknr)
|
|
|
|
{
|
2009-06-10 21:04:23 +00:00
|
|
|
disk->read_hook = read_hook;
|
2005-12-25 15:59:50 +00:00
|
|
|
|
2012-05-09 11:02:59 +00:00
|
|
|
grub_disk_read (disk, blknr + blocks_start, skipfirst,
|
2004-09-11 11:42:43 +00:00
|
|
|
blockend, buf);
|
|
|
|
disk->read_hook = 0;
|
|
|
|
if (grub_errno)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2008-02-02 14:15:31 +00:00
|
|
|
grub_memset (buf, 0, blockend);
|
2004-09-11 11:42:43 +00:00
|
|
|
|
|
|
|
buf += blocksize - skipfirst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2005-12-25 15:59:50 +00:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
grub_fshelp_log2blksize (unsigned int blksize, unsigned int *pow)
|
|
|
|
{
|
|
|
|
int mod;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2005-12-25 15:59:50 +00:00
|
|
|
*pow = 0;
|
|
|
|
while (blksize > 1)
|
|
|
|
{
|
2007-03-19 00:04:29 +00:00
|
|
|
mod = blksize - ((blksize >> 1) << 1);
|
2005-12-25 15:59:50 +00:00
|
|
|
blksize >>= 1;
|
|
|
|
|
|
|
|
/* Check if it really is a power of two. */
|
|
|
|
if (mod)
|
|
|
|
return grub_error (GRUB_ERR_BAD_NUMBER,
|
|
|
|
"the blocksize is not a power of two");
|
|
|
|
(*pow)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|