2004-04-04 Yoshinori K. Okuji <okuji@enbug.org>

All symbols prefixed with PUPA_ and pupa_ are renamed to GRUB_
	and grub_, respectively. Because the conversion is trivial and
	mechanical, I omit the details here. Please refer to the CVS
	if you need more information.
This commit is contained in:
okuji 2004-04-04 13:46:03 +00:00
parent 6a1425510d
commit 4b13b216f4
125 changed files with 6198 additions and 6181 deletions

518
fs/ext2.c
View file

@ -1,6 +1,6 @@
/* ext2.c - Second Extended filesystem */
/*
* PUPA -- Preliminary Universal Programming Architecture for GRUB
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
@ -31,139 +31,139 @@
#define FILETYPE_DIRECTORY 2
#define FILETYPE_SYMLINK 7
#include <pupa/err.h>
#include <pupa/file.h>
#include <pupa/mm.h>
#include <pupa/misc.h>
#include <pupa/disk.h>
#include <pupa/dl.h>
#include <pupa/types.h>
#include <grub/err.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/types.h>
/* Log2 size of ext2 block in 512 blocks. */
#define LOG2_EXT2_BLOCK_SIZE(data) \
(pupa_le_to_cpu32 (data->sblock.log2_block_size) + 1)
(grub_le_to_cpu32 (data->sblock.log2_block_size) + 1)
/* Log2 size of ext2 block in bytes. */
#define LOG2_BLOCK_SIZE(data) \
(pupa_le_to_cpu32 (data->sblock.log2_block_size) + 10)
(grub_le_to_cpu32 (data->sblock.log2_block_size) + 10)
/* The size of an ext2 block in bytes. */
#define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
/* The ext2 superblock. */
struct pupa_ext_sblock
struct grub_ext_sblock
{
pupa_uint32_t total_inodes;
pupa_uint32_t total_blocks;
pupa_uint32_t reserved_blocks;
pupa_uint32_t free_blocks;
pupa_uint32_t free_inodes;
pupa_uint32_t first_data_block;
pupa_uint32_t log2_block_size;
pupa_uint32_t log2_fragment_size;
pupa_uint32_t blocks_per_group;
pupa_uint32_t fragments_per_group;
pupa_uint32_t inodes_per_group;
pupa_uint32_t mtime;
pupa_uint32_t utime;
pupa_uint16_t mnt_count;
pupa_uint16_t max_mnt_count;
pupa_uint16_t magic;
pupa_uint16_t fs_state;
pupa_uint16_t error_handling;
pupa_uint16_t minor_revision_level;
pupa_uint32_t lastcheck;
pupa_uint32_t checkinterval;
pupa_uint32_t creator_os;
pupa_uint32_t revision_level;
pupa_uint16_t uid_reserved;
pupa_uint16_t gid_reserved;
pupa_uint32_t first_inode;
pupa_uint16_t inode_size;
pupa_uint16_t block_group_number;
pupa_uint32_t feature_compatibility;
pupa_uint32_t feature_incompat;
pupa_uint32_t feature_ro_compat;
pupa_uint32_t unique_id[4];
grub_uint32_t total_inodes;
grub_uint32_t total_blocks;
grub_uint32_t reserved_blocks;
grub_uint32_t free_blocks;
grub_uint32_t free_inodes;
grub_uint32_t first_data_block;
grub_uint32_t log2_block_size;
grub_uint32_t log2_fragment_size;
grub_uint32_t blocks_per_group;
grub_uint32_t fragments_per_group;
grub_uint32_t inodes_per_group;
grub_uint32_t mtime;
grub_uint32_t utime;
grub_uint16_t mnt_count;
grub_uint16_t max_mnt_count;
grub_uint16_t magic;
grub_uint16_t fs_state;
grub_uint16_t error_handling;
grub_uint16_t minor_revision_level;
grub_uint32_t lastcheck;
grub_uint32_t checkinterval;
grub_uint32_t creator_os;
grub_uint32_t revision_level;
grub_uint16_t uid_reserved;
grub_uint16_t gid_reserved;
grub_uint32_t first_inode;
grub_uint16_t inode_size;
grub_uint16_t block_group_number;
grub_uint32_t feature_compatibility;
grub_uint32_t feature_incompat;
grub_uint32_t feature_ro_compat;
grub_uint32_t unique_id[4];
char volume_name[16];
char last_mounted_on[64];
pupa_uint32_t compression_info;
grub_uint32_t compression_info;
};
/* The ext2 blockgroup. */
struct ext2_block_group
{
pupa_uint32_t block_id;
pupa_uint32_t inode_id;
pupa_uint32_t inode_table_id;
pupa_uint16_t free_blocks;
pupa_uint16_t free_inodes;
pupa_uint16_t pad;
pupa_uint32_t reserved[3];
grub_uint32_t block_id;
grub_uint32_t inode_id;
grub_uint32_t inode_table_id;
grub_uint16_t free_blocks;
grub_uint16_t free_inodes;
grub_uint16_t pad;
grub_uint32_t reserved[3];
};
/* The ext2 inode. */
struct pupa_ext2_inode
struct grub_ext2_inode
{
pupa_uint16_t mode;
pupa_uint16_t uid;
pupa_uint32_t size;
pupa_uint32_t atime;
pupa_uint32_t ctime;
pupa_uint32_t mtime;
pupa_uint32_t dtime;
pupa_uint16_t gid;
pupa_uint16_t nlinks;
pupa_uint32_t blockcnt; /* Blocks of 512 bytes!! */
pupa_uint32_t flags;
pupa_uint32_t osd1;
grub_uint16_t mode;
grub_uint16_t uid;
grub_uint32_t size;
grub_uint32_t atime;
grub_uint32_t ctime;
grub_uint32_t mtime;
grub_uint32_t dtime;
grub_uint16_t gid;
grub_uint16_t nlinks;
grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */
grub_uint32_t flags;
grub_uint32_t osd1;
union
{
struct datablocks
{
pupa_uint32_t dir_blocks[INDIRECT_BLOCKS];
pupa_uint32_t indir_block;
pupa_uint32_t double_indir_block;
pupa_uint32_t tripple_indir_block;
grub_uint32_t dir_blocks[INDIRECT_BLOCKS];
grub_uint32_t indir_block;
grub_uint32_t double_indir_block;
grub_uint32_t tripple_indir_block;
} blocks;
char symlink[60];
};
pupa_uint32_t version;
pupa_uint32_t acl;
pupa_uint32_t dir_acl;
pupa_uint32_t fragment_addr;
pupa_uint32_t osd2[3];
grub_uint32_t version;
grub_uint32_t acl;
grub_uint32_t dir_acl;
grub_uint32_t fragment_addr;
grub_uint32_t osd2[3];
};
/* The header of an ext2 directory entry. */
struct ext2_dirent
{
pupa_uint32_t inode;
pupa_uint16_t direntlen;
pupa_uint8_t namelen;
pupa_uint8_t filetype;
grub_uint32_t inode;
grub_uint16_t direntlen;
grub_uint8_t namelen;
grub_uint8_t filetype;
};
/* Information about a "mounted" ext2 filesystem. */
struct pupa_ext2_data
struct grub_ext2_data
{
struct pupa_ext_sblock sblock;
pupa_disk_t disk;
struct pupa_ext2_inode inode;
struct grub_ext_sblock sblock;
grub_disk_t disk;
struct grub_ext2_inode inode;
};
#ifndef PUPA_UTIL
static pupa_dl_t my_mod;
#ifndef GRUB_UTIL
static grub_dl_t my_mod;
#endif
/* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of
the mounted filesystem DATA. */
inline static pupa_err_t
pupa_ext2_blockgroup (struct pupa_ext2_data *data, int group,
inline static grub_err_t
grub_ext2_blockgroup (struct grub_ext2_data *data, int group,
struct ext2_block_group *blkgrp)
{
return pupa_disk_read (data->disk,
((pupa_le_to_cpu32 (data->sblock.first_data_block) + 1)
return grub_disk_read (data->disk,
((grub_le_to_cpu32 (data->sblock.first_data_block) + 1)
<< LOG2_EXT2_BLOCK_SIZE (data)),
group * sizeof (struct ext2_block_group),
sizeof (struct ext2_block_group), (char *) blkgrp);
@ -172,28 +172,28 @@ pupa_ext2_blockgroup (struct pupa_ext2_data *data, int group,
/* Return in BLOCK the on disk block number of block FILEBLOCK in the
opened file descibed by DATA. If this block is not stored on disk
in case of a sparse file return 0. */
static pupa_err_t
pupa_ext2_get_file_block (struct pupa_ext2_data *data,
static grub_err_t
grub_ext2_get_file_block (struct grub_ext2_data *data,
int fileblock, int *block)
{
int blknr;
struct pupa_ext2_inode *inode = &data->inode;
struct grub_ext2_inode *inode = &data->inode;
/* Direct blocks. */
if (fileblock < INDIRECT_BLOCKS)
blknr = pupa_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
blknr = grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]);
/* Indirect. */
else if (fileblock < INDIRECT_BLOCKS + EXT2_BLOCK_SIZE (data) / 4)
{
pupa_uint32_t indir[EXT2_BLOCK_SIZE (data) / 4];
grub_uint32_t indir[EXT2_BLOCK_SIZE (data) / 4];
if (pupa_disk_read (data->disk,
pupa_le_to_cpu32 (inode->blocks.indir_block)
if (grub_disk_read (data->disk,
grub_le_to_cpu32 (inode->blocks.indir_block)
<< LOG2_EXT2_BLOCK_SIZE (data),
0, EXT2_BLOCK_SIZE (data), (char *) indir))
return pupa_errno;
return grub_errno;
blknr = pupa_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]);
}
/* Double indirect. */
else if (fileblock < INDIRECT_BLOCKS + EXT2_BLOCK_SIZE (data) / 4
@ -202,29 +202,29 @@ pupa_ext2_get_file_block (struct pupa_ext2_data *data,
unsigned int perblock = EXT2_BLOCK_SIZE (data) / 4;
unsigned int rblock = fileblock - (INDIRECT_BLOCKS
+ EXT2_BLOCK_SIZE (data) / 4);
pupa_uint32_t indir[EXT2_BLOCK_SIZE (data) / 4];
grub_uint32_t indir[EXT2_BLOCK_SIZE (data) / 4];
if (pupa_disk_read (data->disk,
pupa_le_to_cpu32 (inode->blocks.double_indir_block)
if (grub_disk_read (data->disk,
grub_le_to_cpu32 (inode->blocks.double_indir_block)
<< LOG2_EXT2_BLOCK_SIZE (data),
0, EXT2_BLOCK_SIZE (data), (char *) indir))
return pupa_errno;
return grub_errno;
if (pupa_disk_read (data->disk,
pupa_le_to_cpu32 (indir[rblock / perblock])
if (grub_disk_read (data->disk,
grub_le_to_cpu32 (indir[rblock / perblock])
<< LOG2_EXT2_BLOCK_SIZE (data),
0, EXT2_BLOCK_SIZE (data), (char *) indir))
return pupa_errno;
return grub_errno;
blknr = pupa_le_to_cpu32 (indir[rblock % perblock]);
blknr = grub_le_to_cpu32 (indir[rblock % perblock]);
}
/* Tripple indirect. */
else
{
pupa_error (PUPA_ERR_NOT_IMPLEMENTED_YET,
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"ext2fs doesn't support tripple indirect blocks");
return pupa_errno;
return grub_errno;
}
*block = blknr;
@ -234,8 +234,8 @@ pupa_ext2_get_file_block (struct pupa_ext2_data *data,
/* Read LEN bytes from the file described by DATA starting with byte
POS. Return the amount of read bytes in READ. */
static pupa_ssize_t
pupa_ext2_read_file (struct pupa_ext2_data *data,
static grub_ssize_t
grub_ext2_read_file (struct grub_ext2_data *data,
void (*read_hook) (unsigned long sector,
unsigned offset, unsigned length),
int pos, unsigned int len, char *buf)
@ -244,8 +244,8 @@ pupa_ext2_read_file (struct pupa_ext2_data *data,
int blockcnt;
/* Adjust len so it we can't read past the end of the file. */
if (len > pupa_le_to_cpu32 (data->inode.size))
len = pupa_le_to_cpu32 (data->inode.size);
if (len > grub_le_to_cpu32 (data->inode.size))
len = grub_le_to_cpu32 (data->inode.size);
blockcnt = ((len + pos)
+ EXT2_BLOCK_SIZE (data) - 1) / EXT2_BLOCK_SIZE (data);
@ -258,8 +258,8 @@ pupa_ext2_read_file (struct pupa_ext2_data *data,
int skipfirst = 0;
pupa_ext2_get_file_block (data, i, &blknr);
if (pupa_errno)
grub_ext2_get_file_block (data, i, &blknr);
if (grub_errno)
return -1;
blknr = blknr << LOG2_EXT2_BLOCK_SIZE (data);
@ -286,14 +286,14 @@ pupa_ext2_read_file (struct pupa_ext2_data *data,
if (blknr)
{
data->disk->read_hook = read_hook;
pupa_disk_read (data->disk, blknr, skipfirst,
grub_disk_read (data->disk, blknr, skipfirst,
blockend, buf);
data->disk->read_hook = 0;
if (pupa_errno)
if (grub_errno)
return -1;
}
else
pupa_memset (buf, EXT2_BLOCK_SIZE (data) - skipfirst, 0);
grub_memset (buf, EXT2_BLOCK_SIZE (data) - skipfirst, 0);
buf += EXT2_BLOCK_SIZE (data) - skipfirst;
}
@ -303,12 +303,12 @@ pupa_ext2_read_file (struct pupa_ext2_data *data,
/* Read the inode INO for the file described by DATA into INODE. */
static pupa_err_t
pupa_ext2_read_inode (struct pupa_ext2_data *data,
int ino, struct pupa_ext2_inode *inode)
static grub_err_t
grub_ext2_read_inode (struct grub_ext2_data *data,
int ino, struct grub_ext2_inode *inode)
{
struct ext2_block_group blkgrp;
struct pupa_ext_sblock *sblock = &data->sblock;
struct grub_ext_sblock *sblock = &data->sblock;
int inodes_per_block;
unsigned int blkno;
@ -317,76 +317,76 @@ pupa_ext2_read_inode (struct pupa_ext2_data *data,
/* It is easier to calculate if the first inode is 0. */
ino--;
pupa_ext2_blockgroup (data, ino / pupa_le_to_cpu32 (sblock->inodes_per_group),
grub_ext2_blockgroup (data, ino / grub_le_to_cpu32 (sblock->inodes_per_group),
&blkgrp);
if (pupa_errno)
return pupa_errno;
if (grub_errno)
return grub_errno;
inodes_per_block = EXT2_BLOCK_SIZE (data) / 128;
blkno = (ino % pupa_le_to_cpu32 (sblock->inodes_per_group))
blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
/ inodes_per_block;
blkoff = (ino % pupa_le_to_cpu32 (sblock->inodes_per_group))
blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group))
% inodes_per_block;
/* Read the inode. */
if (pupa_disk_read (data->disk,
((pupa_le_to_cpu32 (blkgrp.inode_table_id) + blkno)
if (grub_disk_read (data->disk,
((grub_le_to_cpu32 (blkgrp.inode_table_id) + blkno)
<< LOG2_EXT2_BLOCK_SIZE (data)),
sizeof (struct pupa_ext2_inode) * blkoff,
sizeof (struct pupa_ext2_inode), (char *) inode))
return pupa_errno;
sizeof (struct grub_ext2_inode) * blkoff,
sizeof (struct grub_ext2_inode), (char *) inode))
return grub_errno;
return 0;
}
static struct pupa_ext2_data *
pupa_ext2_mount (pupa_disk_t disk)
static struct grub_ext2_data *
grub_ext2_mount (grub_disk_t disk)
{
struct pupa_ext2_data *data;
struct grub_ext2_data *data;
data = pupa_malloc (sizeof (struct pupa_ext2_data));
data = grub_malloc (sizeof (struct grub_ext2_data));
if (!data)
return 0;
/* Read the superblock. */
pupa_disk_read (disk, 1 * 2, 0, sizeof (struct pupa_ext_sblock),
grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext_sblock),
(char *) &data->sblock);
if (pupa_errno)
if (grub_errno)
goto fail;
/* Make sure this is an ext2 filesystem. */
if (pupa_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC)
if (grub_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC)
goto fail;
data->disk = disk;
return data;
fail:
pupa_error (PUPA_ERR_BAD_FS, "not an ext2 filesystem");
pupa_free (data);
grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem");
grub_free (data);
return 0;
}
/* Find the file with the pathname PATH on the filesystem described by
DATA. Return its inode number in INO. */
static pupa_err_t
pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
static grub_err_t
grub_ext2_find_file (struct grub_ext2_data *data, const char *path, int *ino)
{
int blocksize = EXT2_BLOCK_SIZE (data)
<< pupa_le_to_cpu32 (data->sblock.log2_block_size);
struct pupa_ext2_inode *inode = &data->inode;
<< grub_le_to_cpu32 (data->sblock.log2_block_size);
struct grub_ext2_inode *inode = &data->inode;
int currinode = 2;
int symlinkcnt = 0;
char fpath[EXT2_PATH_MAX];
char *name = fpath;
pupa_strncpy (fpath, path, EXT2_PATH_MAX);
grub_strncpy (fpath, path, EXT2_PATH_MAX);
if (!name || name[0] != '/')
{
pupa_error (PUPA_ERR_BAD_FILENAME, "bad filename");
return pupa_errno;
grub_error (GRUB_ERR_BAD_FILENAME, "bad filename");
return grub_errno;
}
/* Skip the first slash. */
@ -398,8 +398,8 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
}
/* Remove trailing "/". */
if (name[pupa_strlen (name) - 1] =='/')
name[pupa_strlen (name) - 1] = '\0';
if (name[grub_strlen (name) - 1] =='/')
name[grub_strlen (name) - 1] = '\0';
while (*name)
{
@ -408,29 +408,29 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
int namesize;
/* Extract the actual part from the pathname. */
next = pupa_strchr (name, '/');
next = grub_strchr (name, '/');
if (next)
{
next[0] = '\0';
next++;
}
namesize = pupa_strlen (name);
namesize = grub_strlen (name);
/* Open the file. */
pupa_ext2_read_inode (data, currinode, inode);
if (pupa_errno)
grub_ext2_read_inode (data, currinode, inode);
if (grub_errno)
goto fail;
/* Search the file. */
while (fpos < pupa_le_to_cpu32 (inode->size))
while (fpos < grub_le_to_cpu32 (inode->size))
{
struct ext2_dirent dirent;
/* Read the directory entry. */
pupa_ext2_read_file (data, 0, fpos, sizeof (struct ext2_dirent),
grub_ext2_read_file (data, 0, fpos, sizeof (struct ext2_dirent),
(char *) &dirent);
if (pupa_errno)
if (grub_errno)
goto fail;
if (dirent.namelen != 0)
@ -438,10 +438,10 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
char filename[dirent.namelen + 1];
/* Read the filename part of this directory entry. */
pupa_ext2_read_file (data, 0, fpos
grub_ext2_read_file (data, 0, fpos
+ sizeof (struct ext2_dirent),
dirent.namelen, filename);
if (pupa_errno)
if (grub_errno)
goto fail;
filename[dirent.namelen] = '\0';
@ -449,7 +449,7 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
/* Check if the current directory entry described the
file we are looking for. */
if (dirent.namelen == namesize
&& !pupa_strncmp (name, filename, namesize))
&& !grub_strncmp (name, filename, namesize))
{
/* If this is a symlink, follow it. */
if (dirent.filetype == FILETYPE_SYMLINK)
@ -459,38 +459,38 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
if (++symlinkcnt == EXT2_MAX_SYMLINKCNT)
{
pupa_error (PUPA_ERR_SYMLINK_LOOP,
grub_error (GRUB_ERR_SYMLINK_LOOP,
"too deep nesting of symlinks");
goto fail;
}
/* Read the symlink. */
pupa_ext2_read_inode (data,
pupa_le_to_cpu32 (dirent.inode),
grub_ext2_read_inode (data,
grub_le_to_cpu32 (dirent.inode),
inode);
/* If the filesize of the symlink is bigger than
60 the symlink is stored in a separate block,
otherwise it is stored in the inode. */
if (pupa_le_to_cpu32 (inode->size) <= 60)
pupa_strncpy (symlink,
if (grub_le_to_cpu32 (inode->size) <= 60)
grub_strncpy (symlink,
inode->symlink,
pupa_le_to_cpu32 (inode->size));
grub_le_to_cpu32 (inode->size));
else
{
pupa_ext2_read_file (data, 0, 0,
pupa_le_to_cpu32 (inode->size),
grub_ext2_read_file (data, 0, 0,
grub_le_to_cpu32 (inode->size),
symlink);
if (pupa_errno)
if (grub_errno)
goto fail;
}
symlink[pupa_le_to_cpu32 (inode->size)] = '\0';
symlink[grub_le_to_cpu32 (inode->size)] = '\0';
/* Check if the symlink is absolute or relative. */
if (symlink[0] == '/')
{
pupa_strncpy (fpath, symlink, EXT2_PATH_MAX);
grub_strncpy (fpath, symlink, EXT2_PATH_MAX);
name = fpath;
currinode = 2;
}
@ -500,20 +500,20 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
if (next)
{
bak = pupa_strdup (next);
bak = grub_strdup (next);
if (!bak)
goto fail;
}
/* Relative symlink, construct the new path. */
pupa_strcpy (fpath, symlink);
grub_strcpy (fpath, symlink);
name = fpath;
if (next)
{
pupa_strcat (name, "/");
pupa_strcat (name, bak);
pupa_free (bak);
grub_strcat (name, "/");
grub_strcat (name, bak);
grub_free (bak);
}
}
@ -523,12 +523,12 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
if (next)
{
currinode = pupa_le_to_cpu32 (dirent.inode);
currinode = grub_le_to_cpu32 (dirent.inode);
name = next;
if (dirent.filetype != FILETYPE_DIRECTORY)
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE,
grub_error (GRUB_ERR_BAD_FILE_TYPE,
"not a directory");
goto fail;
}
@ -536,59 +536,59 @@ pupa_ext2_find_file (struct pupa_ext2_data *data, const char *path, int *ino)
}
else /* Found it! */
{
*ino = pupa_le_to_cpu32 (dirent.inode);
*ino = grub_le_to_cpu32 (dirent.inode);
return 0;
}
}
}
/* Move to next directory entry. */
fpos += pupa_le_to_cpu16 (dirent.direntlen);
fpos += grub_le_to_cpu16 (dirent.direntlen);
}
/* The complete directory was read and no matching file was
found. */
if (fpos >= pupa_le_to_cpu32 (inode->size))
if (fpos >= grub_le_to_cpu32 (inode->size))
{
pupa_error (PUPA_ERR_FILE_NOT_FOUND, "file not found");
grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
goto fail;
}
}
fail:
return pupa_errno;
return grub_errno;
}
/* Open a file named NAME and initialize FILE. */
static pupa_err_t
pupa_ext2_open (struct pupa_file *file, const char *name)
static grub_err_t
grub_ext2_open (struct grub_file *file, const char *name)
{
struct pupa_ext2_data *data;
struct grub_ext2_data *data;
int ino;
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_ext2_mount (file->device->disk);
data = grub_ext2_mount (file->device->disk);
if (!data)
goto fail;
pupa_ext2_find_file (data, name, &ino);
if (pupa_errno)
grub_ext2_find_file (data, name, &ino);
if (grub_errno)
goto fail;
pupa_ext2_read_inode (data, ino, &data->inode);
if (pupa_errno)
grub_ext2_read_inode (data, ino, &data->inode);
if (grub_errno)
goto fail;
if (!(pupa_le_to_cpu16 (data->inode.mode) & 0100000))
if (!(grub_le_to_cpu16 (data->inode.mode) & 0100000))
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE, "not a regular file");
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file");
goto fail;
}
file->size = pupa_le_to_cpu32 (data->inode.size);
file->size = grub_le_to_cpu32 (data->inode.size);
file->data = data;
file->offset = 0;
@ -596,86 +596,86 @@ pupa_ext2_open (struct pupa_file *file, const char *name)
fail:
pupa_free (data);
grub_free (data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return pupa_errno;
return grub_errno;
}
static pupa_err_t
pupa_ext2_close (pupa_file_t file)
static grub_err_t
grub_ext2_close (grub_file_t file)
{
pupa_free (file->data);
grub_free (file->data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return PUPA_ERR_NONE;
return GRUB_ERR_NONE;
}
/* Read LEN bytes data from FILE into BUF. */
static pupa_ssize_t
pupa_ext2_read (pupa_file_t file, char *buf, pupa_ssize_t len)
static grub_ssize_t
grub_ext2_read (grub_file_t file, char *buf, grub_ssize_t len)
{
struct pupa_ext2_data *data =
(struct pupa_ext2_data *) file->data;
struct grub_ext2_data *data =
(struct grub_ext2_data *) file->data;
return pupa_ext2_read_file (data, file->read_hook, file->offset, len, buf);
return grub_ext2_read_file (data, file->read_hook, file->offset, len, buf);
}
static pupa_err_t
pupa_ext2_dir (pupa_device_t device, const char *path,
static grub_err_t
grub_ext2_dir (grub_device_t device, const char *path,
int (*hook) (const char *filename, int dir))
{
struct pupa_ext2_data *data = 0;;
struct grub_ext2_data *data = 0;;
int ino;
unsigned int fpos = 0;
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_ext2_mount (device->disk);
data = grub_ext2_mount (device->disk);
if (!data)
goto fail;
pupa_ext2_find_file (data, (char *) path, &ino);
if (pupa_errno)
grub_ext2_find_file (data, (char *) path, &ino);
if (grub_errno)
goto fail;
pupa_ext2_read_inode (data, ino, &data->inode);
if (pupa_errno)
grub_ext2_read_inode (data, ino, &data->inode);
if (grub_errno)
goto fail;
if (!(pupa_le_to_cpu16 (data->inode.mode) & 040000))
if (!(grub_le_to_cpu16 (data->inode.mode) & 040000))
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE, "not a directory");
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
goto fail;
}
/* Search the file. */
while (fpos < pupa_le_to_cpu32 (data->inode.size))
while (fpos < grub_le_to_cpu32 (data->inode.size))
{
struct ext2_dirent dirent;
pupa_ext2_read_file (data, 0, fpos, sizeof (struct ext2_dirent),
grub_ext2_read_file (data, 0, fpos, sizeof (struct ext2_dirent),
(char *) &dirent);
if (pupa_errno)
if (grub_errno)
goto fail;
if (dirent.namelen != 0)
{
char filename[dirent.namelen + 1];
pupa_ext2_read_file (data, 0, fpos + sizeof (struct ext2_dirent),
grub_ext2_read_file (data, 0, fpos + sizeof (struct ext2_dirent),
dirent.namelen, filename);
if (pupa_errno)
if (grub_errno)
goto fail;
filename[dirent.namelen] = '\0';
@ -683,78 +683,78 @@ pupa_ext2_dir (pupa_device_t device, const char *path,
hook (filename, dirent.filetype == FILETYPE_DIRECTORY);
}
fpos += pupa_le_to_cpu16 (dirent.direntlen);
fpos += grub_le_to_cpu16 (dirent.direntlen);
}
fail:
pupa_free (data);
grub_free (data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return pupa_errno;
return grub_errno;
}
static pupa_err_t
pupa_ext2_label (pupa_device_t device, char **label)
static grub_err_t
grub_ext2_label (grub_device_t device, char **label)
{
struct pupa_ext2_data *data;
pupa_disk_t disk = device->disk;
struct grub_ext2_data *data;
grub_disk_t disk = device->disk;
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_ext2_mount (disk);
data = grub_ext2_mount (disk);
if (data)
*label = pupa_strndup (data->sblock.volume_name, 14);
*label = grub_strndup (data->sblock.volume_name, 14);
else
*label = 0;
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
pupa_free (data);
grub_free (data);
return pupa_errno;
return grub_errno;
}
static struct pupa_fs pupa_ext2_fs =
static struct grub_fs grub_ext2_fs =
{
.name = "ext2",
.dir = pupa_ext2_dir,
.open = pupa_ext2_open,
.read = pupa_ext2_read,
.close = pupa_ext2_close,
.label = pupa_ext2_label,
.dir = grub_ext2_dir,
.open = grub_ext2_open,
.read = grub_ext2_read,
.close = grub_ext2_close,
.label = grub_ext2_label,
.next = 0
};
#ifdef PUPA_UTIL
#ifdef GRUB_UTIL
void
pupa_ext2_init (void)
grub_ext2_init (void)
{
pupa_fs_register (&pupa_ext2_fs);
grub_fs_register (&grub_ext2_fs);
}
void
pupa_ext2_fini (void)
grub_ext2_fini (void)
{
pupa_fs_unregister (&pupa_ext2_fs);
grub_fs_unregister (&grub_ext2_fs);
}
#else /* ! PUPA_UTIL */
PUPA_MOD_INIT
#else /* ! GRUB_UTIL */
GRUB_MOD_INIT
{
pupa_fs_register (&pupa_ext2_fs);
grub_fs_register (&grub_ext2_fs);
my_mod = mod;
}
PUPA_MOD_FINI
GRUB_MOD_FINI
{
pupa_fs_unregister (&pupa_ext2_fs);
grub_fs_unregister (&grub_ext2_fs);
}
#endif /* ! PUPA_UTIL */
#endif /* ! GRUB_UTIL */

500
fs/fat.c
View file

@ -1,6 +1,6 @@
/* fat.c - FAT filesystem */
/*
* PUPA -- Preliminary Universal Programming Architecture for GRUB
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2000,2001,2002,2003,2004 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
@ -18,115 +18,115 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <pupa/fs.h>
#include <pupa/disk.h>
#include <pupa/file.h>
#include <pupa/types.h>
#include <pupa/misc.h>
#include <pupa/mm.h>
#include <pupa/err.h>
#include <pupa/dl.h>
#include <grub/fs.h>
#include <grub/disk.h>
#include <grub/file.h>
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#define PUPA_FAT_DIR_ENTRY_SIZE 32
#define GRUB_FAT_DIR_ENTRY_SIZE 32
#define PUPA_FAT_ATTR_READ_ONLY 0x01
#define PUPA_FAT_ATTR_HIDDEN 0x02
#define PUPA_FAT_ATTR_SYSTEM 0x04
#define PUPA_FAT_ATTR_VOLUME_ID 0x08
#define PUPA_FAT_ATTR_DIRECTORY 0x10
#define PUPA_FAT_ATTR_ARCHIVE 0x20
#define GRUB_FAT_ATTR_READ_ONLY 0x01
#define GRUB_FAT_ATTR_HIDDEN 0x02
#define GRUB_FAT_ATTR_SYSTEM 0x04
#define GRUB_FAT_ATTR_VOLUME_ID 0x08
#define GRUB_FAT_ATTR_DIRECTORY 0x10
#define GRUB_FAT_ATTR_ARCHIVE 0x20
#define PUPA_FAT_ATTR_LONG_NAME (PUPA_FAT_ATTR_READ_ONLY \
| PUPA_FAT_ATTR_HIDDEN \
| PUPA_FAT_ATTR_SYSTEM \
| PUPA_FAT_ATTR_VOLUME_ID)
#define PUPA_FAT_ATTR_VALID (PUPA_FAT_ATTR_READ_ONLY \
| PUPA_FAT_ATTR_HIDDEN \
| PUPA_FAT_ATTR_SYSTEM \
| PUPA_FAT_ATTR_DIRECTORY \
| PUPA_FAT_ATTR_ARCHIVE)
#define GRUB_FAT_ATTR_LONG_NAME (GRUB_FAT_ATTR_READ_ONLY \
| GRUB_FAT_ATTR_HIDDEN \
| GRUB_FAT_ATTR_SYSTEM \
| GRUB_FAT_ATTR_VOLUME_ID)
#define GRUB_FAT_ATTR_VALID (GRUB_FAT_ATTR_READ_ONLY \
| GRUB_FAT_ATTR_HIDDEN \
| GRUB_FAT_ATTR_SYSTEM \
| GRUB_FAT_ATTR_DIRECTORY \
| GRUB_FAT_ATTR_ARCHIVE)
struct pupa_fat_bpb
struct grub_fat_bpb
{
pupa_uint8_t jmp_boot[3];
pupa_uint8_t oem_name[8];
pupa_uint16_t bytes_per_sector;
pupa_uint8_t sectors_per_cluster;
pupa_uint16_t num_reserved_sectors;
pupa_uint8_t num_fats;
pupa_uint16_t num_root_entries;
pupa_uint16_t num_total_sectors_16;
pupa_uint8_t media;
pupa_uint16_t sectors_per_fat_16;
pupa_uint16_t sectors_per_track;
pupa_uint16_t num_heads;
pupa_uint32_t num_hidden_sectors;
pupa_uint32_t num_total_sectors_32;
grub_uint8_t jmp_boot[3];
grub_uint8_t oem_name[8];
grub_uint16_t bytes_per_sector;
grub_uint8_t sectors_per_cluster;
grub_uint16_t num_reserved_sectors;
grub_uint8_t num_fats;
grub_uint16_t num_root_entries;
grub_uint16_t num_total_sectors_16;
grub_uint8_t media;
grub_uint16_t sectors_per_fat_16;
grub_uint16_t sectors_per_track;
grub_uint16_t num_heads;
grub_uint32_t num_hidden_sectors;
grub_uint32_t num_total_sectors_32;
/* The following fields are only used by FAT32. */
pupa_uint32_t sectors_per_fat_32;
pupa_uint16_t extended_flags;
pupa_uint16_t fs_version;
pupa_uint32_t root_cluster;
pupa_uint16_t fs_info;
pupa_uint16_t backup_boot_sector;
grub_uint32_t sectors_per_fat_32;
grub_uint16_t extended_flags;
grub_uint16_t fs_version;
grub_uint32_t root_cluster;
grub_uint16_t fs_info;
grub_uint16_t backup_boot_sector;
} __attribute__ ((packed));
struct pupa_fat_dir_entry
struct grub_fat_dir_entry
{
pupa_uint8_t name[11];
pupa_uint8_t attr;
pupa_uint8_t nt_reserved;
pupa_uint8_t c_time_tenth;
pupa_uint16_t c_time;
pupa_uint16_t c_date;
pupa_uint16_t a_date;
pupa_uint16_t first_cluster_high;
pupa_uint16_t w_time;
pupa_uint16_t w_date;
pupa_uint16_t first_cluster_low;
pupa_uint32_t file_size;
grub_uint8_t name[11];
grub_uint8_t attr;
grub_uint8_t nt_reserved;
grub_uint8_t c_time_tenth;
grub_uint16_t c_time;
grub_uint16_t c_date;
grub_uint16_t a_date;
grub_uint16_t first_cluster_high;
grub_uint16_t w_time;
grub_uint16_t w_date;
grub_uint16_t first_cluster_low;
grub_uint32_t file_size;
} __attribute__ ((packed));
struct pupa_fat_long_name_entry
struct grub_fat_long_name_entry
{
pupa_uint8_t id;
pupa_uint16_t name1[5];
pupa_uint8_t attr;
pupa_uint8_t reserved;
pupa_uint8_t checksum;
pupa_uint16_t name2[6];
pupa_uint16_t first_cluster;
pupa_uint16_t name3[2];
grub_uint8_t id;
grub_uint16_t name1[5];
grub_uint8_t attr;
grub_uint8_t reserved;
grub_uint8_t checksum;
grub_uint16_t name2[6];
grub_uint16_t first_cluster;
grub_uint16_t name3[2];
} __attribute__ ((packed));
struct pupa_fat_data
struct grub_fat_data
{
int logical_sector_bits;
pupa_uint32_t num_sectors;
grub_uint32_t num_sectors;
pupa_uint16_t fat_sector;
pupa_uint32_t sectors_per_fat;
grub_uint16_t fat_sector;
grub_uint32_t sectors_per_fat;
int fat_size;
pupa_uint32_t root_cluster;
pupa_uint32_t root_sector;
pupa_uint32_t num_root_sectors;
grub_uint32_t root_cluster;
grub_uint32_t root_sector;
grub_uint32_t num_root_sectors;
int cluster_bits;
pupa_uint32_t cluster_eof_mark;
pupa_uint32_t cluster_sector;
pupa_uint32_t num_clusters;
grub_uint32_t cluster_eof_mark;
grub_uint32_t cluster_sector;
grub_uint32_t num_clusters;
pupa_uint8_t attr;
pupa_ssize_t file_size;
pupa_uint32_t file_cluster;
pupa_uint32_t cur_cluster_num;
pupa_uint32_t cur_cluster;
grub_uint8_t attr;
grub_ssize_t file_size;
grub_uint32_t file_cluster;
grub_uint32_t cur_cluster_num;
grub_uint32_t cur_cluster;
};
#ifndef PUPA_UTIL
static pupa_dl_t my_mod;
#ifndef GRUB_UTIL
static grub_dl_t my_mod;
#endif
static int
@ -146,30 +146,30 @@ fat_log2 (unsigned x)
return i;
}
static struct pupa_fat_data *
pupa_fat_mount (pupa_disk_t disk)
static struct grub_fat_data *
grub_fat_mount (grub_disk_t disk)
{
struct pupa_fat_bpb bpb;
struct pupa_fat_data *data = 0;
pupa_uint32_t first_fat, magic;
struct grub_fat_bpb bpb;
struct grub_fat_data *data = 0;
grub_uint32_t first_fat, magic;
if (! disk)
goto fail;
data = (struct pupa_fat_data *) pupa_malloc (sizeof (*data));
data = (struct grub_fat_data *) grub_malloc (sizeof (*data));
if (! data)
goto fail;
/* Read the BPB. */
if (pupa_disk_read (disk, 0, 0, sizeof (bpb), (char *) &bpb))
if (grub_disk_read (disk, 0, 0, sizeof (bpb), (char *) &bpb))
goto fail;
/* Get the sizes of logical sectors and clusters. */
data->logical_sector_bits =
fat_log2 (pupa_le_to_cpu16 (bpb.bytes_per_sector));
if (data->logical_sector_bits < PUPA_DISK_SECTOR_BITS)
fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS)
goto fail;
data->logical_sector_bits -= PUPA_DISK_SECTOR_BITS;
data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
if (data->cluster_bits < 0)
@ -177,22 +177,22 @@ pupa_fat_mount (pupa_disk_t disk)
data->cluster_bits += data->logical_sector_bits;
/* Get information about FATs. */
data->fat_sector = (pupa_le_to_cpu16 (bpb.num_reserved_sectors)
data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors)
<< data->logical_sector_bits);
if (data->fat_sector == 0)
goto fail;
data->sectors_per_fat = ((bpb.sectors_per_fat_16
? pupa_le_to_cpu16 (bpb.sectors_per_fat_16)
: pupa_le_to_cpu32 (bpb.sectors_per_fat_32))
? grub_le_to_cpu16 (bpb.sectors_per_fat_16)
: grub_le_to_cpu32 (bpb.sectors_per_fat_32))
<< data->logical_sector_bits);
if (data->sectors_per_fat == 0)
goto fail;
/* Get the number of sectors in this volume. */
data->num_sectors = ((bpb.num_total_sectors_16
? pupa_le_to_cpu16 (bpb.num_total_sectors_16)
: pupa_le_to_cpu32 (bpb.num_total_sectors_32))
? grub_le_to_cpu16 (bpb.num_total_sectors_16)
: grub_le_to_cpu32 (bpb.num_total_sectors_32))
<< data->logical_sector_bits);
if (data->num_sectors == 0)
goto fail;
@ -203,10 +203,10 @@ pupa_fat_mount (pupa_disk_t disk)
data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat;
data->num_root_sectors
= ((((pupa_uint32_t) pupa_le_to_cpu16 (bpb.num_root_entries)
* PUPA_FAT_DIR_ENTRY_SIZE
+ pupa_le_to_cpu16 (bpb.bytes_per_sector) - 1)
>> (data->logical_sector_bits + PUPA_DISK_SECTOR_BITS))
= ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
* GRUB_FAT_DIR_ENTRY_SIZE
+ grub_le_to_cpu16 (bpb.bytes_per_sector) - 1)
>> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS))
<< (data->logical_sector_bits));
data->cluster_sector = data->root_sector + data->num_root_sectors;
@ -220,9 +220,9 @@ pupa_fat_mount (pupa_disk_t disk)
if (! bpb.sectors_per_fat_16)
{
/* FAT32. */
pupa_uint16_t flags = pupa_le_to_cpu16 (bpb.extended_flags);
grub_uint16_t flags = grub_le_to_cpu16 (bpb.extended_flags);
data->root_cluster = pupa_le_to_cpu32 (bpb.root_cluster);
data->root_cluster = grub_le_to_cpu32 (bpb.root_cluster);
data->fat_size = 32;
data->cluster_eof_mark = 0x0ffffff8;
@ -263,14 +263,14 @@ pupa_fat_mount (pupa_disk_t disk)
if (data->num_sectors <= data->fat_sector)
goto fail;
if (pupa_disk_read (disk,
if (grub_disk_read (disk,
data->fat_sector,
0,
sizeof (first_fat),
(char *) &first_fat))
goto fail;
first_fat = pupa_le_to_cpu32 (first_fat);
first_fat = grub_le_to_cpu32 (first_fat);
if (data->fat_size == 32)
{
@ -294,26 +294,26 @@ pupa_fat_mount (pupa_disk_t disk)
/* Start from the root directory. */
data->file_cluster = data->root_cluster;
data->cur_cluster_num = ~0UL;
data->attr = PUPA_FAT_ATTR_DIRECTORY;
data->attr = GRUB_FAT_ATTR_DIRECTORY;
return data;
fail:
pupa_free (data);
pupa_error (PUPA_ERR_BAD_FS, "not a fat filesystem");
grub_free (data);
grub_error (GRUB_ERR_BAD_FS, "not a fat filesystem");
return 0;
}
/* Convert UTF-16 (little endian) to UTF8. */
static pupa_uint8_t *
pupa_fat_utf16_to_utf8 (pupa_uint8_t *dest, pupa_uint16_t *src,
pupa_size_t size)
static grub_uint8_t *
grub_fat_utf16_to_utf8 (grub_uint8_t *dest, grub_uint16_t *src,
grub_size_t size)
{
pupa_uint32_t code_high = 0;
grub_uint32_t code_high = 0;
while (size--)
{
pupa_uint32_t code = pupa_le_to_cpu16 (*src++);
grub_uint32_t code = grub_le_to_cpu16 (*src++);
if (code_high)
{
@ -366,27 +366,27 @@ pupa_fat_utf16_to_utf8 (pupa_uint8_t *dest, pupa_uint16_t *src,
return dest;
}
static pupa_ssize_t
pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
static grub_ssize_t
grub_fat_read_data (grub_disk_t disk, struct grub_fat_data *data,
void (*read_hook) (unsigned long sector,
unsigned offset, unsigned length),
pupa_ssize_t offset, pupa_ssize_t len, char *buf)
grub_ssize_t offset, grub_ssize_t len, char *buf)
{
pupa_ssize_t size;
pupa_uint32_t logical_cluster;
grub_ssize_t size;
grub_uint32_t logical_cluster;
unsigned logical_cluster_bits;
pupa_ssize_t ret = 0;
grub_ssize_t ret = 0;
unsigned long sector;
/* This is a special case. FAT12 and FAT16 doesn't have the root directory
in clusters. */
if (data->file_cluster == ~0UL)
{
size = (data->num_root_sectors << PUPA_DISK_SECTOR_BITS) - offset;
size = (data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
if (size > len)
size = len;
if (pupa_disk_read (disk, data->root_sector, offset, size, buf))
if (grub_disk_read (disk, data->root_sector, offset, size, buf))
return -1;
return size;
@ -395,7 +395,7 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
/* Calculate the logical cluster number and offset. */
logical_cluster_bits = (data->cluster_bits
+ data->logical_sector_bits
+ PUPA_DISK_SECTOR_BITS);
+ GRUB_DISK_SECTOR_BITS);
logical_cluster = offset >> logical_cluster_bits;
offset &= (1 << logical_cluster_bits) - 1;
@ -410,7 +410,7 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
while (logical_cluster > data->cur_cluster_num)
{
/* Find next cluster. */
pupa_uint32_t next_cluster;
grub_uint32_t next_cluster;
unsigned long fat_offset;
switch (data->fat_size)
@ -428,12 +428,12 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
}
/* Read the FAT. */
if (pupa_disk_read (disk, data->fat_sector, fat_offset,
if (grub_disk_read (disk, data->fat_sector, fat_offset,
(data->fat_size + 7) >> 3,
(char *) &next_cluster))
return -1;
next_cluster = pupa_le_to_cpu32 (next_cluster);
next_cluster = grub_le_to_cpu32 (next_cluster);
switch (data->fat_size)
{
case 16:
@ -448,7 +448,7 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
}
#if 0
pupa_printf ("%s:%d: fat_size=%d, next_cluster=%u\n",
grub_printf ("%s:%d: fat_size=%d, next_cluster=%u\n",
__FILE__, __LINE__, data->fat_size, next_cluster);
#endif
@ -458,7 +458,7 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
if (next_cluster < 2 || next_cluster >= data->num_clusters)
{
pupa_error (PUPA_ERR_BAD_FS, "invalid cluster %u",
grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u",
next_cluster);
return -1;
}
@ -476,9 +476,9 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
size = len;
disk->read_hook = read_hook;
pupa_disk_read (disk, sector, offset, size, buf);
grub_disk_read (disk, sector, offset, size, buf);
disk->read_hook = 0;
if (pupa_errno)
if (grub_errno)
return -1;
len -= size;
@ -495,22 +495,22 @@ pupa_fat_read_data (pupa_disk_t disk, struct pupa_fat_data *data,
next path. If there is no next path or an error occurs, return NULL.
If HOOK is specified, call it with each file name. */
static char *
pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
grub_fat_find_dir (grub_disk_t disk, struct grub_fat_data *data,
const char *path,
int (*hook) (const char *filename, int dir))
{
struct pupa_fat_dir_entry dir;
struct grub_fat_dir_entry dir;
char *dirname, *dirp;
char *filename, *filep = 0;
pupa_uint16_t *unibuf;
grub_uint16_t *unibuf;
int slot = -1, slots = -1;
int checksum = -1;
pupa_ssize_t offset = -sizeof(dir);
grub_ssize_t offset = -sizeof(dir);
int call_hook;
if (! (data->attr & PUPA_FAT_ATTR_DIRECTORY))
if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY))
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE, "not a directory");
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
return 0;
}
@ -518,32 +518,32 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
while (*path == '/')
path++;
dirp = pupa_strchr (path, '/');
dirp = grub_strchr (path, '/');
if (dirp)
{
unsigned len = dirp - path;
dirname = pupa_malloc (len + 1);
dirname = grub_malloc (len + 1);
if (! dirname)
return 0;
pupa_memcpy (dirname, path, len);
grub_memcpy (dirname, path, len);
dirname[len] = '\0';
}
else
/* This is actually a file. */
dirname = pupa_strdup (path);
dirname = grub_strdup (path);
call_hook = (! dirp && hook);
/* Allocate space enough to hold a long name. */
filename = pupa_malloc (0x40 * 13 * 4 + 1);
unibuf = (pupa_uint16_t *) pupa_malloc (0x40 * 13 * 2);
filename = grub_malloc (0x40 * 13 * 4 + 1);
unibuf = (grub_uint16_t *) grub_malloc (0x40 * 13 * 2);
if (! filename || ! unibuf)
{
pupa_free (filename);
pupa_free (unibuf);
pupa_free (dirname);
grub_free (filename);
grub_free (unibuf);
grub_free (dirname);
return 0;
}
@ -555,23 +555,23 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
offset += sizeof (dir);
/* Read a directory entry. */
if ((pupa_fat_read_data (disk, data, 0,
if ((grub_fat_read_data (disk, data, 0,
offset, sizeof (dir), (char *) &dir)
!= sizeof (dir))
|| dir.name[0] == 0)
{
if (pupa_errno == PUPA_ERR_NONE && ! call_hook)
pupa_error (PUPA_ERR_FILE_NOT_FOUND, "file not found");
if (grub_errno == GRUB_ERR_NONE && ! call_hook)
grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
break;
}
/* Handle long name entries. */
if (dir.attr == PUPA_FAT_ATTR_LONG_NAME)
if (dir.attr == GRUB_FAT_ATTR_LONG_NAME)
{
struct pupa_fat_long_name_entry *long_name
= (struct pupa_fat_long_name_entry *) &dir;
pupa_uint8_t id = long_name->id;
struct grub_fat_long_name_entry *long_name
= (struct grub_fat_long_name_entry *) &dir;
grub_uint8_t id = long_name->id;
if (id & 0x40)
{
@ -587,14 +587,14 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
}
slot--;
pupa_memcpy (unibuf + slot * 13, long_name->name1, 5 * 2);
pupa_memcpy (unibuf + slot * 13 + 5, long_name->name2, 6 * 2);
pupa_memcpy (unibuf + slot * 13 + 11, long_name->name3, 2 * 2);
grub_memcpy (unibuf + slot * 13, long_name->name1, 5 * 2);
grub_memcpy (unibuf + slot * 13 + 5, long_name->name2, 6 * 2);
grub_memcpy (unibuf + slot * 13 + 11, long_name->name3, 2 * 2);
continue;
}
/* Check if this entry is valid. */
if (dir.name[0] == 0xe5 || (dir.attr & ~PUPA_FAT_ATTR_VALID))
if (dir.name[0] == 0xe5 || (dir.attr & ~GRUB_FAT_ATTR_VALID))
continue;
/* This is a workaround for Japanese. */
@ -603,28 +603,28 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
if (checksum != -1 && slot == 0)
{
pupa_uint8_t sum;
grub_uint8_t sum;
for (sum = 0, i = 0; i < sizeof (dir.name); i++)
sum = ((sum >> 1) | (sum << 7)) + dir.name[i];
if (sum == checksum)
{
*pupa_fat_utf16_to_utf8 (filename, unibuf, slots * 13) = '\0';
*grub_fat_utf16_to_utf8 (filename, unibuf, slots * 13) = '\0';
if (*dirname == '\0' && call_hook)
{
if (hook (filename, dir.attr & PUPA_FAT_ATTR_DIRECTORY))
if (hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY))
break;
checksum = -1;
continue;
}
if (pupa_strcmp (dirname, filename) == 0)
if (grub_strcmp (dirname, filename) == 0)
{
if (call_hook)
hook (filename, dir.attr & PUPA_FAT_ATTR_DIRECTORY);
hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY);
break;
}
@ -636,13 +636,13 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
/* Convert the 8.3 file name. */
filep = filename;
for (i = 0; i < 8 && dir.name[i] && ! pupa_isspace (dir.name[i]); i++)
*filep++ = pupa_tolower (dir.name[i]);
for (i = 0; i < 8 && dir.name[i] && ! grub_isspace (dir.name[i]); i++)
*filep++ = grub_tolower (dir.name[i]);
*filep = '.';
for (i = 8; i < 11 && dir.name[i] && ! pupa_isspace (dir.name[i]); i++)
*++filep = pupa_tolower (dir.name[i]);
for (i = 8; i < 11 && dir.name[i] && ! grub_isspace (dir.name[i]); i++)
*++filep = grub_tolower (dir.name[i]);
if (*filep != '.')
filep++;
@ -651,174 +651,174 @@ pupa_fat_find_dir (pupa_disk_t disk, struct pupa_fat_data *data,
if (*dirname == '\0' && call_hook)
{
if (hook (filename, dir.attr & PUPA_FAT_ATTR_DIRECTORY))
if (hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY))
break;
}
else if (pupa_strcmp (dirname, filename) == 0)
else if (grub_strcmp (dirname, filename) == 0)
{
if (call_hook)
hook (filename, dir.attr & PUPA_FAT_ATTR_DIRECTORY);
hook (filename, dir.attr & GRUB_FAT_ATTR_DIRECTORY);
break;
}
}
pupa_free (filename);
pupa_free (dirname);
grub_free (filename);
grub_free (dirname);
data->attr = dir.attr;
data->file_size = pupa_le_to_cpu32 (dir.file_size);
data->file_cluster = ((pupa_le_to_cpu16 (dir.first_cluster_high) << 16)
| pupa_le_to_cpu16 (dir.first_cluster_low));
data->file_size = grub_le_to_cpu32 (dir.file_size);
data->file_cluster = ((grub_le_to_cpu16 (dir.first_cluster_high) << 16)
| grub_le_to_cpu16 (dir.first_cluster_low));
data->cur_cluster_num = ~0UL;
return dirp;
}
static pupa_err_t
pupa_fat_dir (pupa_device_t device, const char *path,
static grub_err_t
grub_fat_dir (grub_device_t device, const char *path,
int (*hook) (const char *filename, int dir))
{
struct pupa_fat_data *data = 0;
pupa_disk_t disk = device->disk;
struct grub_fat_data *data = 0;
grub_disk_t disk = device->disk;
char *p = (char *) path;
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_fat_mount (disk);
data = grub_fat_mount (disk);
if (! data)
goto fail;
do
{
p = pupa_fat_find_dir (disk, data, p, hook);
p = grub_fat_find_dir (disk, data, p, hook);
}
while (p && pupa_errno == PUPA_ERR_NONE);
while (p && grub_errno == GRUB_ERR_NONE);
fail:
pupa_free (data);
grub_free (data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return pupa_errno;
return grub_errno;
}
static pupa_err_t
pupa_fat_open (pupa_file_t file, const char *name)
static grub_err_t
grub_fat_open (grub_file_t file, const char *name)
{
struct pupa_fat_data *data = 0;
struct grub_fat_data *data = 0;
char *p = (char *) name;
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_fat_mount (file->device->disk);
data = grub_fat_mount (file->device->disk);
if (! data)
goto fail;
do
{
p = pupa_fat_find_dir (file->device->disk, data, p, 0);
if (pupa_errno != PUPA_ERR_NONE)
p = grub_fat_find_dir (file->device->disk, data, p, 0);
if (grub_errno != GRUB_ERR_NONE)
goto fail;
}
while (p);
if (data->attr & PUPA_FAT_ATTR_DIRECTORY)
if (data->attr & GRUB_FAT_ATTR_DIRECTORY)
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE, "not a file");
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file");
goto fail;
}
file->data = data;
file->size = data->file_size;
return PUPA_ERR_NONE;
return GRUB_ERR_NONE;
fail:
pupa_free (data);
grub_free (data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return pupa_errno;
return grub_errno;
}
static pupa_ssize_t
pupa_fat_read (pupa_file_t file, char *buf, pupa_ssize_t len)
static grub_ssize_t
grub_fat_read (grub_file_t file, char *buf, grub_ssize_t len)
{
return pupa_fat_read_data (file->device->disk, file->data, file->read_hook,
return grub_fat_read_data (file->device->disk, file->data, file->read_hook,
file->offset, len, buf);
}
static pupa_err_t
pupa_fat_close (pupa_file_t file)
static grub_err_t
grub_fat_close (grub_file_t file)
{
pupa_free (file->data);
grub_free (file->data);
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
return pupa_errno;
return grub_errno;
}
static pupa_err_t
pupa_fat_label (pupa_device_t device, char **label)
static grub_err_t
grub_fat_label (grub_device_t device, char **label)
{
struct pupa_fat_data *data;
pupa_disk_t disk = device->disk;
pupa_ssize_t offset = -sizeof(struct pupa_fat_dir_entry);
struct grub_fat_data *data;
grub_disk_t disk = device->disk;
grub_ssize_t offset = -sizeof(struct grub_fat_dir_entry);
#ifndef PUPA_UTIL
pupa_dl_ref (my_mod);
#ifndef GRUB_UTIL
grub_dl_ref (my_mod);
#endif
data = pupa_fat_mount (disk);
data = grub_fat_mount (disk);
if (! data)
goto fail;
if (! (data->attr & PUPA_FAT_ATTR_DIRECTORY))
if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY))
{
pupa_error (PUPA_ERR_BAD_FILE_TYPE, "not a directory");
grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
return 0;
}
while (1)
{
struct pupa_fat_dir_entry dir;
struct grub_fat_dir_entry dir;
/* Adjust the offset. */
offset += sizeof (dir);
/* Read a directory entry. */
if ((pupa_fat_read_data (disk, data, 0,
if ((grub_fat_read_data (disk, data, 0,
offset, sizeof (dir), (char *) &dir)
!= sizeof (dir))
|| dir.name[0] == 0)
{
if (pupa_errno != PUPA_ERR_NONE)
if (grub_errno != GRUB_ERR_NONE)
goto fail;
else
{
*label = 0;
return PUPA_ERR_NONE;
return GRUB_ERR_NONE;
}
}
if (dir.attr == PUPA_FAT_ATTR_VOLUME_ID)
if (dir.attr == GRUB_FAT_ATTR_VOLUME_ID)
{
*label = pupa_strndup (dir.name, 11);
return PUPA_ERR_NONE;
*label = grub_strndup (dir.name, 11);
return GRUB_ERR_NONE;
}
}
@ -826,47 +826,47 @@ pupa_fat_label (pupa_device_t device, char **label)
fail:
#ifndef PUPA_UTIL
pupa_dl_unref (my_mod);
#ifndef GRUB_UTIL
grub_dl_unref (my_mod);
#endif
pupa_free (data);
grub_free (data);
return pupa_errno;
return grub_errno;
}
static struct pupa_fs pupa_fat_fs =
static struct grub_fs grub_fat_fs =
{
.name = "fat",
.dir = pupa_fat_dir,
.open = pupa_fat_open,
.read = pupa_fat_read,
.close = pupa_fat_close,
.label = pupa_fat_label,
.dir = grub_fat_dir,
.open = grub_fat_open,
.read = grub_fat_read,
.close = grub_fat_close,
.label = grub_fat_label,
.next = 0
};
#ifdef PUPA_UTIL
#ifdef GRUB_UTIL
void
pupa_fat_init (void)
grub_fat_init (void)
{
pupa_fs_register (&pupa_fat_fs);
grub_fs_register (&grub_fat_fs);
}
void
pupa_fat_fini (void)
grub_fat_fini (void)
{
pupa_fs_unregister (&pupa_fat_fs);
grub_fs_unregister (&grub_fat_fs);
}
#else /* ! PUPA_UTIL */
PUPA_MOD_INIT
#else /* ! GRUB_UTIL */
GRUB_MOD_INIT
{
pupa_fs_register (&pupa_fat_fs);
grub_fs_register (&grub_fat_fs);
my_mod = mod;
}
PUPA_MOD_FINI
GRUB_MOD_FINI
{
pupa_fs_unregister (&pupa_fat_fs);
grub_fs_unregister (&grub_fat_fs);
}
#endif /* ! PUPA_UTIL */
#endif /* ! GRUB_UTIL */