Skip to content

Commit

Permalink
implement byte-order swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverca22 committed Aug 20, 2021
1 parent a644add commit d7f8c5e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 52 deletions.
38 changes: 1 addition & 37 deletions lib/fs/ext2/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct dircookie {
uint cursor;
};

// TODO, move to ext2.c
static void ext2_dump_inode(struct ext2_inode *inode) {
#if LOCAL_TRACE
printf("mode: %d\n", inode->i_mode);
Expand Down Expand Up @@ -232,43 +233,6 @@ status_t ext2_opendir(fscookie *cookie, const char *name, dircookie **dcookie) {

*dcookie = dir;

#if 0
buf = malloc(EXT2_BLOCK_SIZE(ext2->sb));

file_blocknum = 0;
for (;;) {
/* read in the offset */
err = ext2_read_inode(ext2, &dir_inode, buf, file_blocknum * EXT2_BLOCK_SIZE(ext2->sb), EXT2_BLOCK_SIZE(ext2->sb));
if (err <= 0) {
free(buf);
return -1;
}

/* walk through the directory entries, looking for the one that matches */
struct ext2_dir_entry_2 *ent;
uint pos = 0;
while (pos < EXT2_BLOCK_SIZE(ext2->sb)) {
ent = (struct ext2_dir_entry_2 *)&buf[pos];

LTRACEF("ent %d:%d: inode 0x%x, reclen %d, namelen %d\n",
file_blocknum, pos, LE32(ent->inode), LE16(ent->rec_len), ent->name_len/* , ent->name*/);

/* sanity check the record length */
if (LE16(ent->rec_len) == 0)
break;

pos += ROUNDUP(LE16(ent->rec_len), 4);
}

file_blocknum++;

/* sanity check the directory. 4MB should be enough */
if (file_blocknum > 1024) {
free(buf);
return -1;
}
}
#endif
return 0;
}
status_t ext2_readdir(dircookie *cookie, struct dirent *ent_out) {
Expand Down
1 change: 1 addition & 0 deletions lib/fs/ext2/ext2_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct ext2_group_desc {

/*
* Structure of an inode on the disk
* endian_swap_inode and ext2_load_inode deal with byte-order
*/
struct ext2_inode {
uint16_t i_mode; /* File mode */
Expand Down
4 changes: 4 additions & 0 deletions lib/fs/ext2/ext4_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#define RO_COMPAT_EXTRA_ISIZE 0x040
#define RO_COMPAT_METADATA_CSUM 0x400

// all fields in little-endian, LE16 and LE32 must be used
// TODO? add a variant of endian_swap_inode
typedef struct {
uint16_t eh_magic;
uint16_t eh_entries;
Expand All @@ -13,6 +15,8 @@ typedef struct {
uint32_t eh_generation;
} ext4_extent_header;

// all fields in little-endian, LE16 and LE32 must be used
// TODO? add a variant of endian_swap_inode
typedef struct {
uint32_t ee_block;
uint16_t ee_len;
Expand Down
30 changes: 15 additions & 15 deletions lib/fs/ext2/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,32 @@ static blocknum_t file_block_to_fs_block(ext2_t *ext2, struct ext2_inode *inode,
if (inode->i_flags & 0x80000) { // inode is stored using extents
ext4_extent_header *eh = (ext4_extent_header*)&inode->i_block;
//printf("its an extent based object\n");
//printf("eh_magic: 0x%x\n", eh->eh_magic);
//printf("eh_entries: %d\n", eh->eh_entries);
//printf("eh_max: %d\n", eh->eh_max);
//printf("eh_depth: %d\n", eh->eh_depth);
//printf("eh_generation: %d\n", eh->eh_generation);
if (eh->eh_magic != 0xf30a) {
//printf("eh_magic: 0x%x\n", LE16(eh->eh_magic));
//printf("eh_entries: %d\n", LE16(eh->eh_entries));
//printf("eh_max: %d\n", LE16(eh->eh_max));
//printf("eh_depth: %d\n", LE16(eh->eh_depth));
//printf("eh_generation: %d\n", LE32(eh->eh_generation));
if (LE16(eh->eh_magic) != 0xf30a) {
puts("extent header magic invalid");
return 0;
}
block = 0; // TODO
if (eh->eh_depth == 0) {
if (eh->LE16(eh_depth) == 0) {
ext4_extent *extents = (ext4_extent*)( ((uint32_t)&inode->i_block) + 12);
for (int i=0; i<eh->eh_entries; i++) {
for (int i=0; i < LE16(eh->eh_entries); i++) {
#if 0
printf("extent %d\n", i);
printf(" ee_block: %d\n", extents[i].ee_block);
printf(" ee_len: %d\n", extents[i].ee_len);
printf(" ee_start_hi: %d\n", extents[i].ee_start_hi);
printf(" ee_start_lo: %d\n", extents[i].ee_start_lo);
printf(" ee_block: %d\n", LE32(extents[i].ee_block));
printf(" ee_len: %d\n", LE16(extents[i].ee_len));
printf(" ee_start_hi: %d\n", LE16(extents[i].ee_start_hi));
printf(" ee_start_lo: %d\n", LE32(extents[i].ee_start_lo));
#endif
if ((fileblock >= extents[i].ee_block) && (fileblock < (extents[i].ee_block + extents[i].ee_len))) {
if (extents[i].ee_start_hi != 0) {
if ((fileblock >= LE32(extents[i].ee_block)) && (fileblock < (LE32(extents[i].ee_block) + LE16(extents[i].ee_len)))) {
if (LE16(extents[i].ee_start_hi) != 0) {
puts("unsupported >32bit blocknr");
return 0;
}
block = extents[i].ee_start_lo + (fileblock - extents[i].ee_block);
block = LE32(extents[i].ee_start_lo) + (fileblock - LE32(extents[i].ee_block));
}
}
}
Expand Down

0 comments on commit d7f8c5e

Please sign in to comment.