* Fix invalid delta bug in r_fs_partitions

- Thanks @earada for noticing it
* Fix 32-64bit pointer segfault caused by missing function signature
* Make partition msdos map be verbose when failing
This commit is contained in:
pancake 2011-07-12 00:31:57 +02:00
parent e9bbed187b
commit 35f139986e
6 changed files with 107 additions and 103 deletions

View File

@ -356,7 +356,7 @@ static void r_fs_find_name_aux (RFS* fs, const char *name, const char *glob, RLi
}
R_API RList *r_fs_find_name (RFS* fs, const char *name, const char *glob) {
RList *list = r_list_new ();
RList *list = r_list_new ();
list->free = free;
r_fs_find_name_aux (fs, name, glob, list);
return list;
@ -368,15 +368,16 @@ R_API RFSFile *r_fs_slurp(RFS* fs, const char *path) {
RList * roots = r_fs_root (fs, path);
RListIter *iter;
r_list_foreach (roots, iter, root) {
if (root && root->p) {
if (root->p->open && root->p->read && root->p->close) {
file = root->p->open (root, path);
if (file) root->p->read (file, 0, file->size); //file->data
else eprintf ("r_fs_slurp: cannot open file\n");
} else {
if (root->p->slurp) return root->p->slurp (root, path);
else eprintf ("r_fs_slurp: null root->p->slurp\n");
}
if (!root || !root->p)
continue;
if (root->p->open && root->p->read && root->p->close) {
file = root->p->open (root, path);
if (file) root->p->read (file, 0, file->size); //file->data
else eprintf ("r_fs_slurp: cannot open file\n");
} else {
if (root->p->slurp)
return root->p->slurp (root, path);
eprintf ("r_fs_slurp: null root->p->slurp\n");
}
}
free (roots);
@ -430,11 +431,12 @@ R_API RList *r_fs_partitions (RFS *fs, const char *ptype, ut64 delta) {
if (gpm) {
list = r_list_new ();
list->free = (RListFree)r_fs_partition_free;
grubfs_bind_io (NULL, 0);
struct grub_disk *disk = grubfs_disk (&fs->iob);
gpm->iterate (disk, parhook, 0);
return list;
}
if (ptype&&*ptype)
if (ptype && *ptype)
eprintf ("Unknown partition type '%s'.\n", ptype);
eprintf ("Supported types:\n");
for (i=0; partitions[i].name; i++)

View File

@ -63,12 +63,15 @@ static RList *FSP(_dir)(RFSRoot *root, const char *path, int view) {
static int do_nothing (const char *a, const struct grub_dirhook_info *b, void *c) { return 0; }
static int FSP(_mount)(RFSRoot *root) {
int ret;
GrubFS *gfs = grubfs_new (&FSIPTR, &root->iob);
root->ptr = gfs;
grubfs_bind_io (&root->iob, root->delta);
// XXX: null hook seems to be problematic on some filesystems
//return gfs->file->fs->dir (gfs->file->device, "/", NULL, 0)? R_FALSE:R_TRUE;
return gfs->file->fs->dir (gfs->file->device, "/", do_nothing, 0)? R_FALSE:R_TRUE;
ret = gfs->file->fs->dir (gfs->file->device, "/", do_nothing, 0)? R_FALSE:R_TRUE;
grubfs_bind_io (NULL, root->delta);
return ret;
}
static void FSP(_umount)(RFSRoot *root) {

View File

@ -498,8 +498,7 @@ grub_ext2_read_file (grub_fshelp_node_t node,
{
return grub_fshelp_read_file (node->data->disk, node, read_hook, closure,
flags, pos, len, buf, grub_ext2_read_block,
node->inode.size,
LOG2_EXT2_BLOCK_SIZE (node->data));
node->inode.size, LOG2_EXT2_BLOCK_SIZE (node->data));
}
@ -657,7 +656,7 @@ grub_ext2_iterate_dir (grub_fshelp_node_t dir,
{
struct ext2_dirent dirent;
grub_ext2_read_file (diro, 0, 0, 0, fpos, sizeof (struct ext2_dirent),
grub_ext2_read_file (diro, NULL, NULL, 0, fpos, sizeof (dirent),
(char *) &dirent);
if (grub_errno)
return 0;

View File

@ -21,8 +21,7 @@ static grub_err_t read_foo (struct grub_disk *disk, grub_disk_addr_t sector, gru
const int blocksize = 512; // unhardcode 512
int ret;
RIOBind *iob = disk->data;
if (bio)
iob = bio;
if (bio) iob = bio;
//printf ("io %p\n", file->root->iob.io);
ret = iob->read_at (iob->io, delta+(blocksize*sector),
(ut8*)buf, size*blocksize);

View File

@ -23,6 +23,7 @@
#include <grub/types.h>
#include <grub/symbol.h>
#include <grub/err.h>
#include <grub/disk.h>
typedef struct grub_fshelp_node *grub_fshelp_node_t;
@ -69,6 +70,7 @@ grub_err_t grub_fshelp_find_file (const char *path,
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,
void (*read_hook)
(grub_disk_addr_t sector,
@ -81,7 +83,6 @@ grub_ssize_t grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
(grub_fshelp_node_t node,
grub_disk_addr_t block),
grub_off_t filesize, int log2blocksize);
*/
unsigned int grub_fshelp_log2blksize (unsigned int blksize,
unsigned int *pow);

View File

@ -23,6 +23,7 @@
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/dl.h>
#include <grubfs.h>
struct grub_partition_map grub_msdos_partition_map;
@ -34,106 +35,105 @@ pc_partition_map_iterate (grub_disk_t disk,
void *closure),
void *closure)
{
struct grub_partition p;
struct grub_msdos_partition_mbr mbr;
int labeln = 0;
grub_disk_addr_t lastaddr;
grub_disk_addr_t ext_offset;
int i;
struct grub_msdos_partition_entry *e;
struct grub_partition p;
struct grub_msdos_partition_mbr mbr;
int labeln = 0;
grub_disk_addr_t lastaddr;
grub_disk_addr_t ext_offset;
p.offset = 0;
ext_offset = 0;
p.number = -1;
p.partmap = &grub_msdos_partition_map;
p.offset = 0;
ext_offset = 0;
p.number = -1;
p.partmap = &grub_msdos_partition_map;
/* Any value different than `p.offset' will satisfy the check during
first loop. */
lastaddr = !p.offset;
/* Any value different than `p.offset' will satisfy the check during
first loop. */
lastaddr = !p.offset;
while (1)
{
int i;
struct grub_msdos_partition_entry *e;
for (;;) {
/* Read the MBR. */
if (grub_disk_read (disk, p.offset, 0, sizeof (mbr), &mbr))
goto finish;
/* Read the MBR. */
if (grub_disk_read (disk, p.offset, 0, sizeof (mbr), &mbr))
goto finish;
/* This is our loop-detection algorithm. It works the following way:
It saves last position which was a power of two. Then it compares the
saved value with a current one. This way it's guaranteed that the loop
will be broken by at most third walk.
*/
if (labeln && lastaddr == p.offset) {
return grub_error (GRUB_ERR_BAD_PART_TABLE, "loop detected");
}
/* This is our loop-detection algorithm. It works the following way:
It saves last position which was a power of two. Then it compares the
saved value with a current one. This way it's guaranteed that the loop
will be broken by at most third walk.
*/
if (labeln && lastaddr == p.offset)
return grub_error (GRUB_ERR_BAD_PART_TABLE, "loop detected");
labeln++;
if ((labeln & (labeln - 1)) == 0)
lastaddr = p.offset;
labeln++;
if ((labeln & (labeln - 1)) == 0)
lastaddr = p.offset;
/* Check if it is valid. */
if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE)) {
fprintf (stderr, "msdos: no signature\n");
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
}
/* Check if it is valid. */
if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
for (i = 0; i < 4; i++)
if (mbr.entries[i].flag & 0x7f) {
fprintf (stderr, "msdos: bad boot flag\n");
return grub_error (GRUB_ERR_BAD_PART_TABLE, "bad boot flag");
}
for (i = 0; i < 4; i++)
if (mbr.entries[i].flag & 0x7f)
return grub_error (GRUB_ERR_BAD_PART_TABLE, "bad boot flag");
/* Analyze DOS partitions. */
for (p.index = 0; p.index < 4; p.index++) {
e = mbr.entries + p.index;
/* Analyze DOS partitions. */
for (p.index = 0; p.index < 4; p.index++)
{
e = mbr.entries + p.index;
p.start = p.offset + grub_le_to_cpu32 (e->start);
p.len = grub_le_to_cpu32 (e->length);
p.start = p.offset + grub_le_to_cpu32 (e->start);
p.len = grub_le_to_cpu32 (e->length);
p.msdostype = e->type;
grub_dprintf ("partition",
"partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
p.index, e->flag, e->type,
(unsigned long long) p.start,
(unsigned long long) p.len);
p.msdostype = e->type;
grub_dprintf ("partition",
"partition %d: flag 0x%x, type 0x%x, start 0x%llx, len 0x%llx\n",
p.index, e->flag, e->type,
(unsigned long long) p.start,
(unsigned long long) p.len);
/* If this is a GPT partition, this MBR is just a dummy. */
if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
/* If this is a GPT partition, this MBR is just a dummy. */
if (e->type == GRUB_PC_PARTITION_TYPE_GPT_DISK && p.index == 0)
return grub_error (GRUB_ERR_BAD_PART_TABLE, "dummy mbr");
/* If this partition is a normal one, call the hook. */
if (! grub_msdos_partition_is_empty (e->type)
&& ! grub_msdos_partition_is_extended (e->type))
{
p.number++;
/* If this partition is a normal one, call the hook. */
if (! grub_msdos_partition_is_empty (e->type)
&& ! grub_msdos_partition_is_extended (e->type))
{
p.number++;
if (hook (disk, &p, closure)) {
fprintf (stderr, "msdos: hook fail\n");
return grub_errno;
}
} else if (p.number < 4)
/* If this partition is a logical one, shouldn't increase the
partition number. */
p.number++;
}
if (hook (disk, &p, closure))
return grub_errno;
}
else if (p.number < 4)
/* If this partition is a logical one, shouldn't increase the
partition number. */
p.number++;
/* Find an extended partition. */
for (i = 0; i < 4; i++) {
e = mbr.entries + i;
if (grub_msdos_partition_is_extended (e->type)) {
p.offset = ext_offset + grub_le_to_cpu32 (e->start);
if (! ext_offset)
ext_offset = p.offset;
break;
}
}
/* If no extended partition, the end. */
if (i == 4)
break;
}
/* Find an extended partition. */
for (i = 0; i < 4; i++)
{
e = mbr.entries + i;
if (grub_msdos_partition_is_extended (e->type))
{
p.offset = ext_offset + grub_le_to_cpu32 (e->start);
if (! ext_offset)
ext_offset = p.offset;
break;
}
}
/* If no extended partition, the end. */
if (i == 4)
break;
}
finish:
return grub_errno;
finish:
return grub_errno;
}