Ext2: reading directory entries reads zeros.
Posted: Mon Sep 05, 2016 2:12 pm
I've tried everything, but nothing happens. Maybe it reads info from wrong address, so please correct me.
Code: Select all
//ext2_info: Outputs ext2 volume information from superblock.
void ext2_info(unsigned int ata_drive)
{
if(!is_valid_ext2(ata_drive))
{
fprintf(stderr, "[EXT2] Invalid ext2 filesystem on disk %d.", ata_drive);
return;
}
struct ext2_superblock *superblock=malloc(sizeof(struct ext2_superblock));
ata_read_bytes(ata_drive, (void*)superblock, sizeof(struct ext2_superblock), 1024);
printf("EXT2 %u.%u volume with ATA number %u and volume label \"%s\" information:\n", superblock->ver_major, superblock->ver_minor, ata_drive, superblock->volume_label);
printf("\tLast mounted on %s\n", superblock->last_mount_path);
uint32_t block_size=1024 << superblock->block_size;
uint32_t inode_size=1024 << superblock->inode_size;
printf("\tFree space: %d/%d bytes (%d/%d blocks)\n", (superblock->blocks_count-superblock->unallocated_blocks_count) * block_size, superblock->blocks_count * block_size, superblock->blocks_count-superblock->unallocated_blocks_count, superblock->blocks_count);
printf("\tFilesystem is %s\n", (superblock->fs_state==1) ? "clean" : "with errors");
//Parse inode 2 and list root directory.
uint32_t block_group_count = superblock->blocks_count / superblock->blocks_count_in_blk_group;
struct ext2_block_group_descriptor *bgd=malloc(sizeof(struct ext2_block_group_descriptor) * block_group_count);
ata_read_bytes(ata_drive, (void*)bgd, sizeof(struct ext2_block_group_descriptor) * block_group_count,
(block_size == 1024) ? block_size * (2+1) : block_size * (1+1));
//get inode function:
uint32_t inode = 2; //Root directory inode is defined to always be 2.
uint32_t block_group = (inode - 1) / superblock->inodes_count_in_blk_group;
uint32_t inode_index = (inode - 1) % superblock->inodes_count_in_blk_group;
uint32_t block = (inode_index * inode_size) / block_size;
uint32_t inodes_cnt = superblock->inodes_count_in_blk_group - bgd->unallocated_inodes_count;
printf("\tAllocating %u bytes,\n", sizeof(struct ext2_inode));
struct ext2_inode *inode_struct = malloc(sizeof(struct ext2_inode));
ata_read_bytes(ata_drive, (void*)inode_struct, sizeof(struct ext2_inode) * inodes_cnt, bgd[block_group].inode_table_address * block_size + inode_index * inode_size);
//inode_struct[inode_index].direct_block_pointers[0];
block=block;
printf("\tInode table is on %u, block number of root directory is %u, block size is %u\n", bgd[block_group].inode_table_address, inode_struct->direct_block_pointers[0], block_size);
struct ext2_directory_entry *dirent = malloc(sizeof(struct ext2_directory_entry));
uint16_t name_length = dirent->name_length_low | (dirent->type_or_name_length_high << 8);
ata_read_bytes(ata_drive, (void*)dirent, sizeof(struct ext2_directory_entry), inode_struct->direct_block_pointers[0] * block_size + sizeof(struct ext2_directory_entry));
dirent = realloc((void*)dirent, sizeof(struct ext2_directory_entry) + name_length);
char *filename = malloc(name_length);
ata_read_bytes(ata_drive, (void*)filename, name_length, inode_struct->direct_block_pointers[0] * block_size+sizeof(struct ext2_directory_entry) * 2);
printf("Filename: %s\n", filename);
printf("Read inode from 0x%08X\n", inode_struct->direct_block_pointers[0] * block_size + sizeof(struct ext2_directory_entry));
for(unsigned i=0; i<sizeof(struct ext2_directory_entry) + name_length; i++)
printf("0x%02x ", *((char*)dirent+i)); //Zeros!
//end
}