~cypheon/xos

34e5dbf48248d66cd08ee3468d091c2c1f64861f — Johann Rudloff 6 years ago b254758
Enable accessing indirect blocks.
1 files changed, 12 insertions(+), 5 deletions(-)

M boot2.c
M boot2.c => boot2.c +12 -5
@@ 8,6 8,8 @@

#define PATH_MAX 255

#define EXT2_DIRECT_BLOCKS 12

extern void pread_sector(uint32_t lba, uint8_t *dst);

typedef struct ext2_info_struct {


@@ 95,21 97,26 @@ fs_inode_read_block_direct(ext2_info *fs, ext2_inode *inode, uint32_t block_inde
static void
fs_inode_read_block_indirect(ext2_info *fs, ext2_inode *inode, uint32_t block_index, void *dst) {
  uint8_t buf[EXT2_MAX_BLOCK_SIZE];
  uint32_t block_address = inode->i_block[12];
  uint32_t block_address = inode->i_block[EXT2_DIRECT_BLOCKS];
  fs_read_block(fs, block_address, buf);

  uint32_t indirect_block_index = block_index - 12;
  uint32_t indirect_block_index = block_index - EXT2_DIRECT_BLOCKS;
  uint32_t real_block_address = ((uint32_t *)buf)[indirect_block_index];
  fs_read_block(fs, real_block_address, dst);
}

static void
fs_inode_read_block(ext2_info *fs, ext2_inode *inode, uint32_t block_index, void *dst) {
  if (block_index > 10) {
    khexprint("ERROR maybe indirect block?", block_index);
  if (block_index < EXT2_DIRECT_BLOCKS) {
    fs_inode_read_block_direct(fs, inode, block_index, dst);
  } else if (block_index >= EXT2_DIRECT_BLOCKS &&
      (block_index - EXT2_DIRECT_BLOCKS) < (fs->block_size / 4)
      ) {
    fs_inode_read_block_indirect(fs, inode, block_index, dst);
  } else {
    khexprint("ERROR double indirect block", block_index);
    while(1) {};
  }
  fs_inode_read_block_direct(fs, inode, block_index, dst);
}