Page 1 of 1

Ext2 image tool - reading files

Posted: Sun Mar 14, 2010 7:53 am
by thomasloven
I'm working on a tool for editing ext2 formated disk images.
So far I got following a path and reading a file working.

Did I get it right?

http://thomasloven.wordpress.com/2010/0 ... -system-2/

http://thomasloven.wordpress.com/2010/0 ... ng-a-file/

/Thomas

Re: Ext2 image tool - reading files

Posted: Mon Mar 15, 2010 11:02 pm
by KotuxGuy
Well, I didn't look through the whole article, but..

Code: Select all

struct superblock_t{
int num_inodes; //Number of inodes in the filesystem
int num_blocks; //Number of blocks in the filesystem
int num_reserved_blocks; //Number of reserved blocks
int num_free_blocks; //Number of unused blocks
int num_free_inodes; //Number of unused inodes
int group0_first_block; //Block where the first group starts
int block_size; //Size of a block
int fragment_size; //Size of a disk fragment
int blocks_per_group; //Number of blocks in a group
int fragments_per_group; //Number of fragments in a group
int inodes_per_group; //Number of inodes in a group
int last_mounted; //Time of last mount
int last_written; //Time of last write
short mount_count; //Number of mounts
short max_mounts; //Maximum number of mounts allowed
short signature; //Ext2 signature (0xef53)
short FS_state; //File system state flags
short error_method; //Method of handlig errors flag
short version_minor; //Minor version number
int last_consistency_check; //Time of last consistency check
int consistency_check_interval;//Time between forced consistency checks
int creator_OS; //Creating OS flag
int version_major; //Major version number
short SU_UID;//UID who can use reserved blocks
short SU_GID;//GID who can use reserved blocks
int first_unreserved_inode; //First non-reserved inode
short inode_struct_size ;//Size of each inode
short this_block_group; //This block group (not the same in every superblock)
int compatible_features; //Compatible features flag
int incompatible_features; //Incompatible features flag
int readonly_features; //Features while read-only flag
char FS_id[16];//File system ID
char volume_name[16]; //Volume name
char last_mount_path[64];//Path where last mounted
int algoritm_usage; //Algorithm usage bitmap
char num_file_blocks; //Number of blocks to preallocate for files
char num_dir_blocks; //Number of blocks to preallocate for directories
short unused0; //2 unused bytes
char journal_id[16]; //Journal ID
int journal_inode; //Journal inode
int journal_device; //Journal device
int orphan_inode_list_head; //Head of orphan inode list
char unused1[788]; //788 unused bytes
};
That should be :

Code: Select all

struct superblock_t{
int num_inodes; //Number of inodes in the filesystem
int num_blocks; //Number of blocks in the filesystem
int num_reserved_blocks; //Number of reserved blocks
int num_free_blocks; //Number of unused blocks
int num_free_inodes; //Number of unused inodes
int group0_first_block; //Block where the first group starts
int block_size; //Size of a block
int fragment_size; //Size of a disk fragment
int blocks_per_group; //Number of blocks in a group
int fragments_per_group; //Number of fragments in a group
int inodes_per_group; //Number of inodes in a group
int last_mounted; //Time of last mount
int last_written; //Time of last write
short mount_count; //Number of mounts
short max_mounts; //Maximum number of mounts allowed
short signature; //Ext2 signature (0xef53)
short FS_state; //File system state flags
short error_method; //Method of handlig errors flag
short version_minor; //Minor version number
int last_consistency_check; //Time of last consistency check
int consistency_check_interval;//Time between forced consistency checks
int creator_OS; //Creating OS flag
int version_major; //Major version number
short SU_UID;//UID who can use reserved blocks
short SU_GID;//GID who can use reserved blocks
int first_unreserved_inode; //First non-reserved inode
short inode_struct_size ;//Size of each inode
short this_block_group; //This block group (not the same in every superblock)
int compatible_features; //Compatible features flag
int incompatible_features; //Incompatible features flag
int readonly_features; //Features while read-only flag
char FS_id[16];//File system ID
char volume_name[16]; //Volume name
char last_mount_path[64];//Path where last mounted
int algoritm_usage; //Algorithm usage bitmap
char num_file_blocks; //Number of blocks to preallocate for files
char num_dir_blocks; //Number of blocks to preallocate for directories
short unused0; //2 unused bytes
char journal_id[16]; //Journal ID
int journal_inode; //Journal inode
int journal_device; //Journal device
int orphan_inode_list_head; //Head of orphan inode list
char unused1[788]; //788 unused bytes
} __attribute__( ( packed ) );
The problem is, without __attribute__( ( packed ) ), GCC ( or whatever compiler you're using ) will use word alignment on the structure.
Wildly oversimplified, word alignment makes all the variables in a structure align on a 4 byte boundary.
ext2( and all other FSes ) do not conform to word alignment. If you don't turn off word alignment on a structure intended for a FS, regardless of
whatever FS it is, other disk image tools( and other OSes ) won't be able to read your FS with word alignment.

I noticed a similar problem with other structures in your tool.

This link should be helpful with explaining word alignment: http://www.cs.umd.edu/class/sum2003/cms ... igned.html

Re: Ext2 image tool - reading files

Posted: Tue Mar 16, 2010 9:52 am
by thomasloven
Ah.
Of course you're right.
This actually explains some problems I've had. Thanks!

I also realized I forgot to explain how directories work, so that's added to the first part.

/Thomas

Re: Ext2 image tool - reading files

Posted: Tue Mar 16, 2010 5:55 pm
by KotuxGuy
Glad to help! :)

Re: Ext2 image tool - reading files

Posted: Wed Mar 17, 2010 1:16 am
by Solar
KotuxGuy wrote:The problem is, without __attribute__( ( packed ) ), GCC ( or whatever compiler you're using ) will use word alignment on the structure.
If you are like me, you frown at compiler-specific statements in your source code. In this case, you could also consider GCC's "-fpack-struct" command line option, which has the same effect (on a complete translation unit).

Re: Ext2 image tool - reading files

Posted: Wed Mar 17, 2010 5:19 am
by thomasloven
Also, source code has now been posted

http://thomasloven.wordpress.com/2010/0 ... le-source/